ppm_get_cpu_entries() must not acquire a sleeping lock while preemption is disabled: get_cpu() disables preemption, and spin_lock_bh() may sleep on PREEMPT_RT kernels. Use raw_cpu_ptr() instead, which reads the current CPU's per-CPU pointer without touching preempt_count. The task is then free to migrate before taking pool->lock, so it may end up allocating from a different CPU's pool. That is harmless: each per-CPU pool is just a shard of the global page pod (ppod) index space, and every read-modify-write of a pool's bitmap and next index happens under pool->lock. The free path, ppm_unmark_entries(), re-derives the owning pool from the global index rather than from the running CPU and takes that same lock, so any thread can service any shard. Derive the global index base from pool->base so it always matches the pool that was locked. Found by static analysis and not triggered. The bug is only reachable on PREEMPT_RT kernels, where spin_lock_bh() is a sleeping lock: there it would trigger a "scheduling while atomic" BUG under CONFIG_DEBUG_ATOMIC_SLEEP, or a deadlock if pool->lock is contended. Fixes: 47da7e62b391 ("cxgb4: add iSCSI DDP page pod manager") Signed-off-by: Haoran Wang --- Changes since v4: - Reword the commit message (review). - pr_debug(): drop the running CPU that David suggested in v3; it is of limited use, so keep only the pool-owning CPU. - Add a Fixes: tag and record how the issue was found (netdev bot). Changes since v3: - Explain in the commit message why a foreign CPU's pool is harmless and trim the PREEMPT_RT problem description (review). - pr_debug(): label the pool-owning CPU and also print the running CPU. Changes since v2: - Keep the pool-owning CPU in the pr_debug() output, derived from pool->base (suggested by Sebastian). - Reword the commit message; spell PREEMPT_RT. Changes since v1: - Read the global-index base from pool->base (initialised at pool allocation) instead of a cpu captured before the lock. drivers/net/ethernet/chelsio/libcxgb/libcxgb_ppm.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/drivers/net/ethernet/chelsio/libcxgb/libcxgb_ppm.c b/drivers/net/ethernet/chelsio/libcxgb/libcxgb_ppm.c index 2e3973a32..837bdf412 100644 --- a/drivers/net/ethernet/chelsio/libcxgb/libcxgb_ppm.c +++ b/drivers/net/ethernet/chelsio/libcxgb/libcxgb_ppm.c @@ -119,16 +119,13 @@ static int ppm_get_cpu_entries(struct cxgbi_ppm *ppm, unsigned int count, unsigned long caller_data) { struct cxgbi_ppm_pool *pool; - unsigned int cpu; int i; if (!ppm->pool) return -EINVAL; - cpu = get_cpu(); - pool = per_cpu_ptr(ppm->pool, cpu); + pool = raw_cpu_ptr(ppm->pool); spin_lock_bh(&pool->lock); - put_cpu(); i = ppm_find_unused_entries(pool->bmap, ppm->pool_index_max, pool->next, count, 0); @@ -144,11 +141,11 @@ static int ppm_get_cpu_entries(struct cxgbi_ppm *ppm, unsigned int count, spin_unlock_bh(&pool->lock); - pr_debug("%s: cpu %u, idx %d + %d (%d), next %u.\n", - __func__, cpu, i, count, i + cpu * ppm->pool_index_max, - pool->next); + pr_debug("%s: pool cpu %u, idx %d + %d (%d), next %u.\n", + __func__, pool->base / ppm->pool_index_max, + i, count, i + pool->base, pool->next); - i += cpu * ppm->pool_index_max; + i += pool->base; ppm_mark_entries(ppm, i, count, caller_data); return i; @@ -375,6 +372,7 @@ ppm_alloc_cpu_pool(unsigned int *total, unsigned int *pcpu_ppmax) memset(ppool, 0, alloc_sz); spin_lock_init(&ppool->lock); + ppool->base = count; count += ppmax; }