From: "Lazar, Lijo" <lijo.lazar@amd.com>
To: "Mikhail Gavrilov" <mikhail.v.gavrilov@gmail.com>,
"Alex Deucher" <alexander.deucher@amd.com>,
"Christian König" <christian.koenig@amd.com>
Cc: Eric Huang <jinhuieric.huang@amd.com>,
David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
stable@vger.kernel.org
Subject: Re: [PATCH] drm/amdgpu: fix sleeping allocation under spinlock in PASID IDR
Date: Mon, 30 Mar 2026 13:01:38 +0530 [thread overview]
Message-ID: <dbf1994b-945a-4064-b294-5771e2844caa@amd.com> (raw)
In-Reply-To: <20260328213900.19255-1-mikhail.v.gavrilov@gmail.com>
On 29-Mar-26 3:09 AM, Mikhail Gavrilov wrote:
> Commit 14b81abe7bdc ("drm/amdgpu: prevent immediate PASID reuse case")
> switched from ida to idr_alloc_cyclic() protected by a spinlock, but
> passes GFP_KERNEL to the allocator. idr_alloc_cyclic() may need to
> allocate radix-tree nodes, which with GFP_KERNEL can sleep — illegal
> under a spinlock that disables preemption. With CONFIG_PREEMPT or
> lockdep enabled this triggers:
>
> BUG: sleeping function called from invalid context at
> ./include/linux/sched/mm.h:323
> in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 570
> ...
> #1: ffffffffc2cd24f8 (amdgpu_pasid_idr_lock){+.+.}-{3:3},
> at: amdgpu_pasid_alloc+0x24/0x210 [amdgpu]
> ...
> kmem_cache_alloc_noprof+0x41d/0x780
> radix_tree_node_alloc.constprop.0+0x56/0x3a0
> idr_get_free+0x330/0x830
> idr_alloc_u32+0x14a/0x2e0
> idr_alloc_cyclic+0xd3/0x1d0
> amdgpu_pasid_alloc+0x51/0x210 [amdgpu]
>
> A mutex is not an option because amdgpu_pasid_free() is reachable from
> dma-fence callbacks (amdgpu_pasid_free_cb) which may run in IRQ context.
>
> Use idr_preload(GFP_KERNEL) before taking the spinlock to pre-allocate
> radix-tree nodes, then pass GFP_NOWAIT inside the critical section so
> the allocator draws from the preloaded pool and never sleeps. This is
> the standard kernel pattern for IDR allocation under a spinlock.
>
> Fixes: 14b81abe7bdc ("drm/amdgpu: prevent immediate PASID reuse case")
> Cc: stable@vger.kernel.org
> Signed-off-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
> ---
> drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c
> index d88523568b62..515775eab2ef 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c
> @@ -67,10 +67,12 @@ int amdgpu_pasid_alloc(unsigned int bits)
> if (bits == 0)
> return -EINVAL;
>
> + idr_preload(GFP_KERNEL);
It's better/simpler to replace amdgpu_pasid_idr with xarray.
Thanks,
Lijo
> spin_lock(&amdgpu_pasid_idr_lock);
> pasid = idr_alloc_cyclic(&amdgpu_pasid_idr, NULL, 1,
> - 1U << bits, GFP_KERNEL);
> + 1U << bits, GFP_NOWAIT);
> spin_unlock(&amdgpu_pasid_idr_lock);
> + idr_preload_end();
>
> if (pasid >= 0)
> trace_amdgpu_pasid_allocated(pasid);
prev parent reply other threads:[~2026-03-30 7:31 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-28 21:39 [PATCH] drm/amdgpu: fix sleeping allocation under spinlock in PASID IDR Mikhail Gavrilov
2026-03-30 7:31 ` Lazar, Lijo [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=dbf1994b-945a-4064-b294-5771e2844caa@amd.com \
--to=lijo.lazar@amd.com \
--cc=airlied@gmail.com \
--cc=alexander.deucher@amd.com \
--cc=amd-gfx@lists.freedesktop.org \
--cc=christian.koenig@amd.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=jinhuieric.huang@amd.com \
--cc=mikhail.v.gavrilov@gmail.com \
--cc=simona@ffwll.ch \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox