drm/amdgpu: replace PASID IDR with XArray
Subsystems (2)
PATCH v6 1/1 drm/amdgpu: replace PASID IDR with XArray
Commit 8f1de51f49be ("drm/amdgpu: prevent immediate PASID reuse case") converted the global PASID allocator from IDA to IDR with a spinlock for cyclic allocation, but introduced two locking bugs:
1) idr_alloc_cyclic() is called with GFP_KERNEL under spin_lock(), which can sleep.
2) amdgpu_pasid_free() can be called from hardirq context via the fence signal path (amdgpu_pasid_free_cb), but the lock is taken with plain spin_lock() in process context, creating a potential deadlock:
CPU0
----
spin_lock(&amdgpu_pasid_idr_lock) // process context, IRQs on
<Interrupt>
spin_lock(&amdgpu_pasid_idr_lock) // deadlock
The hardirq call chain is:
sdma_v6_0_process_trap_irq
-> amdgpu_fence_process
-> dma_fence_signal
-> drm_sched_job_done
-> dma_fence_signal
-> amdgpu_pasid_free_cb
-> amdgpu_pasid_freeThis was observed on an RX 7900 XTX when exiting a Vulkan game running under Proton/Wine, which triggers the fence callback path during VM teardown.
Replace the IDR + spinlock with XArray using XA_FLAGS_LOCK_IRQ (all xa operations use IRQ-safe locking internally) and XA_FLAGS_ALLOC1 (zero is not a valid PASID). Both xa_alloc_cyclic() and xa_erase() then handle locking consistently, fixing both bugs.
---
v6: Use DEFINE_XARRAY_FLAGS with XA_FLAGS_LOCK_IRQ | XA_FLAGS_ALLOC1
so all xa operations use IRQ-safe locking internally. Drop
Cc: stable since the regression was never released to any stable kernel. (Christian König)
v5: Use explicit xa_lock_irqsave/__xa_erase for amdgpu_pasid_free()
since xa_erase() only uses plain xa_lock() which is not safe from hardirq context.
https://lore.kernel.org/all/[email protected]/
v4: Use xa_alloc_cyclic/xa_erase directly instead of explicit
xa_lock_irqsave, as suggested by Lijo Lazar.
https://lore.kernel.org/all/[email protected]/
v3: Replace IDR with XArray instead of fixing the spinlock, as
suggested by Lijo Lazar.
https://lore.kernel.org/all/[email protected]/
v2: Added second patch fixing the {HARDIRQ-ON-W} -> {IN-HARDIRQ-W}
lock inconsistency (spin_lock -> spin_lock_irqsave).
https://lore.kernel.org/all/[email protected]/
v1: Fixed sleeping-under-spinlock (idr_alloc_cyclic with GFP_KERNEL)
using idr_preload/GFP_NOWAIT.
https://lore.kernel.org/all/[email protected]/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 43 +++++++++++-------------- 1 file changed, 19 insertions(+), 24 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +19 -24
@@ -22,7 +22,7 @@*/ #include "amdgpu_ids.h" -#include <linux/idr.h> +#include <linux/xarray.h> #include <linux/dma-fence-array.h>
@@ -35,13 +35,13 @@* PASIDs are global address space identifiers that can be shared * between the GPU, an IOMMU and the driver. VMs on different devices * may use the same PASID if they share the same address -* space. Therefore PASIDs are allocated using IDR cyclic allocator -* (similar to kernel PID allocation) which naturally delays reuse. -* VMs are looked up from the PASID per amdgpu_device. +* space. Therefore PASIDs are allocated using an XArray cyclic +* allocator (similar to kernel PID allocation) which naturally delays +* reuse. VMs are looked up from the PASID per amdgpu_device. */ -static DEFINE_IDR (amdgpu_pasid_idr); -static DEFINE_SPINLOCK (amdgpu_pasid_idr_lock); +static DEFINE_XARRAY_FLAGS(amdgpu_pasid_xa, XA_FLAGS_LOCK_IRQ | XA_FLAGS_ALLOC1); +static u32 amdgpu_pasid_xa_next;/* Helper to free pasid from a fence callback */ struct amdgpu_pasid_cb {
@@ -53,8 +53,7 @@ struct amdgpu_pasid_cb {* amdgpu_pasid_alloc - Allocate a PASID * @bits: Maximum width of the PASID in bits, must be at least 1 * -* Uses kernel's IDR cyclic allocator (same as PID allocation). -* Allocates sequentially with automatic wrap-around. +* Uses XArray cyclic allocator for sequential allocation with wrap-around. * * Returns a positive integer on success. Returns %-EINVAL if bits==0. * Returns %-ENOSPC if no PASID was available. Returns %-ENOMEM on
@@ -62,20 +61,22 @@ struct amdgpu_pasid_cb {*/ int amdgpu_pasid_alloc (unsigned int bits) { -int pasid; +u32 pasid; +int r;if (bits ==0 )return -EINVAL ; -spin_lock (&amdgpu_pasid_idr_lock); - pasid =idr_alloc_cyclic (&amdgpu_pasid_idr,NULL ,1 , -1U << bits,GFP_KERNEL ); -spin_unlock (&amdgpu_pasid_idr_lock); + r =xa_alloc_cyclic (&amdgpu_pasid_xa, &pasid,xa_mk_value (0 ), +XA_LIMIT (1 , (1U << bits) -1 ), + &amdgpu_pasid_xa_next,GFP_KERNEL ); -if (pasid >=0 ) +if (r >=0 ) {trace_amdgpu_pasid_allocated (pasid); +return pasid; + } -return pasid; +return r; }/**
@@ -86,9 +87,7 @@ void amdgpu_pasid_free(u32 pasid) {trace_amdgpu_pasid_freed (pasid); -spin_lock (&amdgpu_pasid_idr_lock); -idr_remove (&amdgpu_pasid_idr, pasid); -spin_unlock (&amdgpu_pasid_idr_lock); +xa_erase (&amdgpu_pasid_xa, pasid); } static void amdgpu_pasid_free_cb(struct dma_fence *fence,
@@ -625,13 +624,9 @@ void amdgpu_vmid_mgr_fini(struct amdgpu_device *adev) }/** -* amdgpu_pasid_mgr_cleanup - cleanup PASID manager -* -* Cleanup the IDR allocator. +* amdgpu_pasid_mgr_cleanup - Cleanup PASID manager */ void amdgpu_pasid_mgr_cleanup (void ) { -spin_lock (&amdgpu_pasid_idr_lock); -idr_destroy (&amdgpu_pasid_idr); -spin_unlock (&amdgpu_pasid_idr_lock); +xa_destroy (&amdgpu_pasid_xa); }
Signature · 1 line
2.53.0
Christian König
maintainerQuoted 32 lines · Mikhail Gavrilov
Commit
8f1de51f49be("drm/amdgpu: prevent immediate PASID reuse case") converted the global PASID allocator from IDA to IDR with a spinlock for cyclic allocation, but introduced two locking bugs:1)
idr_alloc_cyclic()is called withGFP_KERNELunderspin_lock(), which can sleep.2)
amdgpu_pasid_free()can be called from hardirq context via the fence signal path (amdgpu_pasid_free_cb), but the lock is taken with plainspin_lock()in process context, creating a potential deadlock:CPU0
----
spin_lock(&amdgpu_pasid_idr_lock)// process context, IRQs on
<Interrupt>
spin_lock(&amdgpu_pasid_idr_lock)// deadlockThe hardirq call chain is: sdma_v6_0_process_trap_irq -> amdgpu_fence_process -> dma_fence_signal -> drm_sched_job_done -> dma_fence_signal -> amdgpu_pasid_free_cb -> amdgpu_pasid_free This was observed on an RX 7900 XTX when exiting a Vulkan game running under Proton/Wine, which triggers the fence callback path during VM teardown.
Quoted 8 lines
Replace the IDR + spinlock with XArray using
XA_FLAGS_LOCK_IRQ(all xa operations use IRQ-safe locking internally) andXA_FLAGS_ALLOC1(zero is not a valid PASID). Bothxa_alloc_cyclic()andxa_erase()then handle locking consistently, fixing both bugs.Suggested-by Lijo Lazar <[email protected]>Fixes 8f1de51f49be ("drm/amdgpu: prevent immediate PASID reuse case")
That should be unecessary. We already replaced GFP_KERNEL with GFP_ATOMIC in Alex fixes pull.
Quoted 80 lines
Signed-off-by Mikhail Gavrilov <[email protected]>---
v6: Use
DEFINE_XARRAY_FLAGSwithXA_FLAGS_LOCK_IRQ|XA_FLAGS_ALLOC1
so all xa operations use IRQ-safe locking internally. Drop
Cc: stable since the regression was never released to any stable kernel. (Christian König)
v5: Use explicit xa_lock_irqsave/__xa_erase foramdgpu_pasid_free()
sincexa_erase()only uses plainxa_lock()which is not safe from hardirq context.
https://lore.kernel.org/all/[email protected]/
v4: Use xa_alloc_cyclic/xa_erase directly instead of explicit
xa_lock_irqsave, as suggested by Lijo Lazar.
https://lore.kernel.org/all/[email protected]/
v3: Replace IDR with XArray instead of fixing the spinlock, as
suggested by Lijo Lazar.
https://lore.kernel.org/all/[email protected]/
v2: Added second patch fixing the {HARDIRQ-ON-W} -> {IN-HARDIRQ-W}lock inconsistency (spin_lock -> spin_lock_irqsave). https://lore.kernel.org/all/[email protected]/ v1: Fixed sleeping-under-spinlock (idr_alloc_cyclic with GFP_KERNEL) using idr_preload/GFP_NOWAIT. https://lore.kernel.org/all/[email protected]/drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c | 43 +++++++++++-------------- 1 file changed, 19 insertions(+), 24 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +9
-9@@ -22,7 +22,7 @@*/ #include "amdgpu_ids.h" -#include <linux/idr.h> +#include <linux/xarray.h> #include <linux/dma-fence-array.h> @@ -35,13 +35,13 @@* PASIDs are global address space identifiers that can be shared * between the GPU, an IOMMU and the driver. VMs on different devices * may use the same PASID if they share the same address -* space. Therefore PASIDs are allocated using IDR cyclic allocator -* (similar to kernel PID allocation) which naturally delays reuse. -* VMs are looked up from the PASID per amdgpu_device. +* space. Therefore PASIDs are allocated using an XArray cyclic +* allocator (similar to kernel PID allocation) which naturally delays +* reuse. VMs are looked up from the PASID per amdgpu_device. */ -static DEFINE_IDR (amdgpu_pasid_idr); -static DEFINE_SPINLOCK (amdgpu_pasid_idr_lock); +static DEFINE_XARRAY_FLAGS(amdgpu_pasid_xa, XA_FLAGS_LOCK_IRQ | XA_FLAGS_ALLOC1); +static u32 amdgpu_pasid_xa_next;/* Helper to free pasid from a fence callback */ struct amdgpu_pasid_cb {@@ -53,8 +53,7 @@ struct amdgpu_pasid_cb {* amdgpu_pasid_alloc - Allocate a PASID * @bits: Maximum width of the PASID in bits, must be at least 1 * -* Uses kernel's IDR cyclic allocator (same as PID allocation). -* Allocates sequentially with automatic wrap-around. +* Uses XArray cyclic allocator for sequential allocation with wrap-around. * * Returns a positive integer on success. Returns %-EINVAL if bits==0. * Returns %-ENOSPC if no PASID was available. Returns %-ENOMEM on @@ -62,20 +61,22 @@ struct amdgpu_pasid_cb {*/ int amdgpu_pasid_alloc (unsigned int bits) { -int pasid; +u32 pasid; +int r;if (bits ==0 )return -EINVAL ;
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c struct amdgpu_pasid_cb { · line 74
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +4
-5-spin_lock (&amdgpu_pasid_idr_lock); - pasid =idr_alloc_cyclic (&amdgpu_pasid_idr,NULL ,1 , -1U << bits,GFP_KERNEL ); -spin_unlock (&amdgpu_pasid_idr_lock); + r =xa_alloc_cyclic (&amdgpu_pasid_xa, &pasid,xa_mk_value (0 ), +XA_LIMIT (1 , (1U << bits) -1 ), + &amdgpu_pasid_xa_next,GFP_KERNEL ); -if (pasid >=0 ) +if (r >=0 ) {
I would turn that around, e.g. if (r < 0) return r;
Apart from that looks good to me.
Regards,
Christian.
Quoted 36 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +6
-10trace_amdgpu_pasid_allocated (pasid); +return pasid; + } -return pasid; +return r; }/** @@ -86,9 +87,7 @@ void amdgpu_pasid_free(u32 pasid) {trace_amdgpu_pasid_freed (pasid); -spin_lock (&amdgpu_pasid_idr_lock); -idr_remove (&amdgpu_pasid_idr, pasid); -spin_unlock (&amdgpu_pasid_idr_lock); +xa_erase (&amdgpu_pasid_xa, pasid); } static void amdgpu_pasid_free_cb(struct dma_fence *fence,@@ -625,13 +624,9 @@ void amdgpu_vmid_mgr_fini(struct amdgpu_device *adev) }/** -* amdgpu_pasid_mgr_cleanup - cleanup PASID manager -* -* Cleanup the IDR allocator. +* amdgpu_pasid_mgr_cleanup - Cleanup PASID manager */ void amdgpu_pasid_mgr_cleanup (void ) { -spin_lock (&amdgpu_pasid_idr_lock); -idr_destroy (&amdgpu_pasid_idr); -spin_unlock (&amdgpu_pasid_idr_lock); +xa_destroy (&amdgpu_pasid_xa); }
Quoted 6 lines · Christian König
That should be unecessary. We already replaced
GFP_KERNELwithGFP_ATOMICin Alex fixes pull.I would turn that around, e.g. if (r < 0) return r;
Apart from that looks good to me.
Hi Christian,
Thanks for the review. I've sent v7 rebased on amd-staging-drm-next (on top of dccd79bb1c7f) with the inverted error check as you suggested.
The Fixes: tag now points to e6d765de3d6b which introduced the plain spin_lock() for the PASID IDR. The sleeping-under-spinlock is already fixed by dccd79bb1c7f, but the IRQ safety issue remains — amdgpu_pasid_free() is reachable from hardirq via the fence callback path while the lock is taken without irqsave in process context. I hit this lockdep WARNING on an RX 7900 XTX when exiting a Vulkan game under Proton. The xarray conversion with XA_FLAGS_LOCK_IRQ fixes this.
https://lore.kernel.org/all/[email protected]/
Signature · 2 lines
Thanks, Mike Gavrilov.