Skip to contentSkip to conversation

drm/amdgpu: replace PASID IDR with XArray

PATCH Applied Mikhail Gavrilov 3 participants 8 messages Mar 31 – Apr 1

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v7 1/1 drm/amdgpu: replace PASID IDR with XArray

Replace the PASID IDR + spinlock with XArray as noted in the TODO left by commit dccd79bb1c7f ("drm/amdgpu: fix the idr allocation flags").

The IDR conversion still has an IRQ safety issue:
amdgpu_pasid_free() can be called from hardirq context via the fence signal path, but amdgpu_pasid_idr_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_free

Use XArray with 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 the IRQ safety issue and removing the need for an explicit spinlock.

Fixes e6d765de3d6b ("drm/amdgpu: prevent immediate PASID reuse case")

---

v7: Rebased on amd-staging-drm-next which already includes
dccd79bb1c7f ("drm/amdgpu: fix the idr allocation flags"). Updated commit message to reflect that sleeping-under-spinlock is already fixed and the xarray conversion now addresses the remaining IRQ safety issue. Inverted error check to if (r < 0) return r; per Christian König.
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)
https://lore.kernel.org/all/[email protected]/
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 | 34 ++++++++++---------------
 1 file changed, 13 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +13 -21
index e495a8fa13fd..a6ac3b4ce0df 100644
@@ -22,7 +22,7 @@
  */
 #include "amdgpu_ids.h"
 
-#include <linux/idr.h>
+#include <linux/xarray.h>
 #include <linux/dma-fence-array.h>
 
 
@@ -40,8 +40,8 @@
  * 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 {
@@ -62,22 +62,19 @@ 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);
-	/* TODO: Need to replace the idr with an xarry, and then
-	 * handle the internal locking with ATOMIC safe paths.
-	 */
-	pasid = idr_alloc_cyclic(&amdgpu_pasid_idr, NULL, 1,
-				 1U << bits, GFP_ATOMIC);
-	spin_unlock(&amdgpu_pasid_idr_lock);
-
-	if (pasid >= 0)
-		trace_amdgpu_pasid_allocated(pasid);
+	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 (r < 0)
+		return r;
 
+	trace_amdgpu_pasid_allocated(pasid);
 	return pasid;
 }
 
@@ -88,10 +85,7 @@ int amdgpu_pasid_alloc(unsigned int bits)
 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,
@@ -634,7 +628,5 @@ void amdgpu_vmid_mgr_fini(struct amdgpu_device *adev)
  */
 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
maintainer Reviewed-by on v7 1/1
#1 lore raw
Quoted 31 lines · Mikhail Gavrilov

Replace the PASID IDR + spinlock with XArray as noted in the TODO left by commit dccd79bb1c7f ("drm/amdgpu: fix the idr allocation flags").

The IDR conversion still has an IRQ safety issue:
amdgpu_pasid_free() can be called from hardirq context via the fence signal path, but amdgpu_pasid_idr_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_free

Use XArray with 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 the IRQ safety issue and removing the need for an explicit spinlock.

Quoted 3 lines
Fixes e6d765de3d6b ("drm/amdgpu: prevent immediate PASID reuse case")
Quoted 109 lines

---

v7: Rebased on amd-staging-drm-next which already includes
dccd79bb1c7f ("drm/amdgpu: fix the idr allocation flags"). Updated commit message to reflect that sleeping-under-spinlock is already fixed and the xarray conversion now addresses the remaining IRQ safety issue. Inverted error check to if (r < 0) return r; per Christian König.
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)
https://lore.kernel.org/all/[email protected]/
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 | 34 ++++++++++---------------
 1 file changed, 13 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +13 -21
index e495a8fa13fd..a6ac3b4ce0df 100644
@@ -22,7 +22,7 @@
  */
 #include "amdgpu_ids.h"
 
-#include <linux/idr.h>
+#include <linux/xarray.h>
 #include <linux/dma-fence-array.h>
 
 
@@ -40,8 +40,8 @@
  * 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 {
@@ -62,22 +62,19 @@ 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);
-	/* TODO: Need to replace the idr with an xarry, and then
-	 * handle the internal locking with ATOMIC safe paths.
-	 */
-	pasid = idr_alloc_cyclic(&amdgpu_pasid_idr, NULL, 1,
-				 1U << bits, GFP_ATOMIC);
-	spin_unlock(&amdgpu_pasid_idr_lock);
-
-	if (pasid >= 0)
-		trace_amdgpu_pasid_allocated(pasid);
+	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 (r < 0)
+		return r;
 
+	trace_amdgpu_pasid_allocated(pasid);
 	return pasid;
 }
 
@@ -88,10 +85,7 @@ int amdgpu_pasid_alloc(unsigned int bits)
 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,
@@ -634,7 +628,5 @@ void amdgpu_vmid_mgr_fini(struct amdgpu_device *adev)
  */
 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);
 }
Applied
#2 lore raw

Applied. Thanks!

Alex

Quoted 151 lines · Christian König
Quoted 31 lines · Mikhail Gavrilov

Replace the PASID IDR + spinlock with XArray as noted in the TODO left by commit dccd79bb1c7f ("drm/amdgpu: fix the idr allocation flags").

The IDR conversion still has an IRQ safety issue:
amdgpu_pasid_free() can be called from hardirq context via the fence signal path, but amdgpu_pasid_idr_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_free

Use XArray with 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 the IRQ safety issue and removing the need for an explicit spinlock.

Quoted 3 lines
Fixes e6d765de3d6b ("drm/amdgpu: prevent immediate PASID reuse case")
Quoted 109 lines

---

v7: Rebased on amd-staging-drm-next which already includes
dccd79bb1c7f ("drm/amdgpu: fix the idr allocation flags"). Updated commit message to reflect that sleeping-under-spinlock is already fixed and the xarray conversion now addresses the remaining IRQ safety issue. Inverted error check to if (r < 0) return r; per Christian König.
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)
https://lore.kernel.org/all/[email protected]/
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 | 34 ++++++++++---------------
 1 file changed, 13 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +13 -21
index e495a8fa13fd..a6ac3b4ce0df 100644
@@ -22,7 +22,7 @@
  */
 #include "amdgpu_ids.h"
 
-#include <linux/idr.h>
+#include <linux/xarray.h>
 #include <linux/dma-fence-array.h>
 
 
@@ -40,8 +40,8 @@
  * 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 {
@@ -62,22 +62,19 @@ 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);
-     /* TODO: Need to replace the idr with an xarry, and then
-      * handle the internal locking with ATOMIC safe paths.
-      */
-     pasid = idr_alloc_cyclic(&amdgpu_pasid_idr, NULL, 1,
-                              1U << bits, GFP_ATOMIC);
-     spin_unlock(&amdgpu_pasid_idr_lock);
-
-     if (pasid >= 0)
-             trace_amdgpu_pasid_allocated(pasid);
+     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 (r < 0)
+             return r;
 
+     trace_amdgpu_pasid_allocated(pasid);
      return pasid;
 }
 
@@ -88,10 +85,7 @@ int amdgpu_pasid_alloc(unsigned int bits)
 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,
@@ -634,7 +628,5 @@ void amdgpu_vmid_mgr_fini(struct amdgpu_device *adev)
  */
 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);
 }
#3 lore raw
Quoted 3 lines · Alex Deucher

Applied. Thanks!

Hi Christian, Alex,

While testing v7, I noticed that xa_erase() and xa_alloc_cyclic() use plain xa_lock()/xa_unlock() regardless of XA_FLAGS_LOCK_IRQ — the flag only affects lockdep annotations, not runtime locking.

The XArray API provides separate _irq variants for this:
xa_alloc_cyclic_irq() and xa_erase_irq(), both defined as inlines in include/linux/xarray.h using xa_lock_irq/xa_unlock_irq.

I confirmed this by hitting the same lockdep WARNING with v6 (which has the same locking), and by reading lib/xarray.c:

  void *xa_erase(struct xarray *xa, unsigned long index)
  {
      xa_lock(xa);              // plain spin_lock, not _irq
      entry = __xa_erase(xa, index);
      xa_unlock(xa);
      return entry;
  }

Should I send a v8 using xa_alloc_cyclic_irq() and xa_erase_irq()?

Thanks,
Mikhail

maintainer
#4 lore raw
Quoted 7 lines · Mikhail Gavrilov
Quoted 3 lines · Alex Deucher

Applied. Thanks!

Hi Christian, Alex,

Quoted 7 lines

While testing v7, I noticed that xa_erase() and xa_alloc_cyclic() use plain xa_lock()/xa_unlock() regardless of XA_FLAGS_LOCK_IRQ — the flag only affects lockdep annotations, not runtime locking.

The XArray API provides separate _irq variants for this:
xa_alloc_cyclic_irq() and xa_erase_irq(), both defined as inlines in include/linux/xarray.h using xa_lock_irq/xa_unlock_irq.

Ah, crap I wanted to double check exactly that but then had no time for it.

Quoted 12 lines

I confirmed this by hitting the same lockdep WARNING with v6 (which has the same locking), and by reading lib/xarray.c:

  void *xa_erase(struct xarray *xa, unsigned long index)
  {
      xa_lock(xa);              // plain spin_lock, not _irq
      entry = __xa_erase(xa, index);
      xa_unlock(xa);
      return entry;
  }
Quoted 1 line

Should I send a v8 using xa_alloc_cyclic_irq() and xa_erase_irq()?

Alex already applied the v7 so it would probably be best if you provide a delta patch on top of that.

It's then up to Alex if he can squash the patches together before upstreaming.

Regards,
Christian.

Quoted 3 lines

Thanks,
Mikhail

#6 lore raw
In reply to Christian König
Quoted 31 lines · Christian König
Quoted 7 lines · Mikhail Gavrilov
Quoted 3 lines · Alex Deucher

Applied. Thanks!

Hi Christian, Alex,

Quoted 7 lines

While testing v7, I noticed that xa_erase() and xa_alloc_cyclic() use plain xa_lock()/xa_unlock() regardless of XA_FLAGS_LOCK_IRQ — the flag only affects lockdep annotations, not runtime locking.

The XArray API provides separate _irq variants for this:
xa_alloc_cyclic_irq() and xa_erase_irq(), both defined as inlines in include/linux/xarray.h using xa_lock_irq/xa_unlock_irq.

Ah, crap I wanted to double check exactly that but then had no time for it.

Quoted 12 lines

I confirmed this by hitting the same lockdep WARNING with v6 (which has the same locking), and by reading lib/xarray.c:

  void *xa_erase(struct xarray *xa, unsigned long index)
  {
      xa_lock(xa);              // plain spin_lock, not _irq
      entry = __xa_erase(xa, index);
      xa_unlock(xa);
      return entry;
  }
Quoted 5 lines
Quoted 1 line

Should I send a v8 using xa_alloc_cyclic_irq() and xa_erase_irq()?

Alex already applied the v7 so it would probably be best if you provide a delta patch on top of that.

It's then up to Alex if he can squash the patches together before upstreaming.

I dropped v7 so I can either apply v8 or squash v7 with the fixup patch.

Alex

Quoted 8 lines

Regards,
Christian.

Quoted 3 lines

Thanks,
Mikhail

maintainer
#7 lore raw
In reply to Alex Deucher

Alex please make sure to include this one in your -fixes pull request.

Thanks,
Christian.

Quoted 157 lines · Alex Deucher

Applied. Thanks!

Alex

Quoted 151 lines · Christian König
Quoted 31 lines · Mikhail Gavrilov

Replace the PASID IDR + spinlock with XArray as noted in the TODO left by commit dccd79bb1c7f ("drm/amdgpu: fix the idr allocation flags").

The IDR conversion still has an IRQ safety issue:
amdgpu_pasid_free() can be called from hardirq context via the fence signal path, but amdgpu_pasid_idr_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_free

Use XArray with 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 the IRQ safety issue and removing the need for an explicit spinlock.

Quoted 3 lines
Fixes e6d765de3d6b ("drm/amdgpu: prevent immediate PASID reuse case")
Quoted 109 lines

---

v7: Rebased on amd-staging-drm-next which already includes
dccd79bb1c7f ("drm/amdgpu: fix the idr allocation flags"). Updated commit message to reflect that sleeping-under-spinlock is already fixed and the xarray conversion now addresses the remaining IRQ safety issue. Inverted error check to if (r < 0) return r; per Christian König.
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)
https://lore.kernel.org/all/[email protected]/
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 | 34 ++++++++++---------------
 1 file changed, 13 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ids.c +13 -21
index e495a8fa13fd..a6ac3b4ce0df 100644
@@ -22,7 +22,7 @@
  */
 #include "amdgpu_ids.h"
 
-#include <linux/idr.h>
+#include <linux/xarray.h>
 #include <linux/dma-fence-array.h>
 
 
@@ -40,8 +40,8 @@
  * 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 {
@@ -62,22 +62,19 @@ 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);
-     /* TODO: Need to replace the idr with an xarry, and then
-      * handle the internal locking with ATOMIC safe paths.
-      */
-     pasid = idr_alloc_cyclic(&amdgpu_pasid_idr, NULL, 1,
-                              1U << bits, GFP_ATOMIC);
-     spin_unlock(&amdgpu_pasid_idr_lock);
-
-     if (pasid >= 0)
-             trace_amdgpu_pasid_allocated(pasid);
+     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 (r < 0)
+             return r;
 
+     trace_amdgpu_pasid_allocated(pasid);
      return pasid;
 }
 
@@ -88,10 +85,7 @@ int amdgpu_pasid_alloc(unsigned int bits)
 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,
@@ -634,7 +628,5 @@ void amdgpu_vmid_mgr_fini(struct amdgpu_device *adev)
  */
 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);
 }