Skip to contentSkip to conversation

IDR to xarray conversion and some small cleanups

RFC Superseded Tvrtko Ursulin 2 participants 19 messages Dec 18, 2025 – Dec 19, 2025

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover IDR to xarray conversion and some small cleanups

RFC because the context conversion needs a lot of scrutiny (and passing through the automated test suites), while the bo list changes are perhaps a bit of a bike shed, although they do result in less lines of code and hopefully easier to read.

BO list sorting is really independent, as is the IDR MES cleanup (which could as well have the warnings removed). Just say the work and I can easily split them out.

Series has been smoke tested, including lockdep and game + desktop workloads so at least shouldn't be too obviously broken.

Tvrtko Ursulin (5):
drm/amdgpu: Replace idr with xarray in amdgpu_bo_list drm/amdgpu: Remove output parameter in bo list handling drm/amdgpu: Simplify sorting of the bo list drm/amdgpu: Convert context manager to xarray drm/amdgpu/mes: Remove idr leftovers

 drivers/gpu/drm/amd/amdgpu/amdgpu.h         |   4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 140 +++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h |  17 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c      |  58 ++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c     | 117 ++++++----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h     |   5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c     |  11 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c     |   9 --
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h     |   3 -
 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c   |   8 +-
 drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c      |  18 +--
 drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c      |  18 +--
 12 files changed, 160 insertions(+), 248 deletions(-)
Signature · 1 line
2.51.1
#1 lore raw

RFC 1/5 drm/amdgpu: Replace idr with xarray in amdgpu_bo_list

IDR is deprecated so let's replace it with xarray.

Conversion is mostly 1:1 apart from AMDGPU_BO_LIST_OP_UPDATE which was implemented with idr_replace, and has now been replaced with a sequence of xa_load and xa_cmpxchg. Should userspace attempt multi-threaded update operations on the same handle it could theoretically hit a new -ENOENT path. But I believe this is purely theoretical and still safe.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h         |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 72 ++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c     | 11 ++--
 4 files changed, 41 insertions(+), 48 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -2
index 9f9774f58ce1..0a5b802bd3c3 100644
@@ -50,6 +50,7 @@
 #include <linux/hashtable.h>
 #include <linux/dma-fence.h>
 #include <linux/pci.h>
+#include <linux/xarray.h>
 
 #include <drm/ttm/ttm_bo.h>
 #include <drm/ttm/ttm_placement.h>
@@ -500,8 +501,7 @@ struct amdgpu_fpriv {
 	struct amdgpu_bo_va	*prt_va;
 	struct amdgpu_bo_va	*csa_va;
 	struct amdgpu_bo_va	*seq64_va;
-	struct mutex		bo_list_lock;
-	struct idr		bo_list_handles;
+	struct xarray		bo_list_handles;
 	struct amdgpu_ctx_mgr	ctx_mgr;
 	struct amdgpu_userq_mgr	userq_mgr;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +34 -38

112 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +1 -1
index 2b5e7c46a39d..0989f1090c63 100644
@@ -58,7 +58,7 @@ struct amdgpu_bo_list {
 	struct amdgpu_bo_list_entry entries[] __counted_by(num_entries);
 };
 
-int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
+int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id,
 		       struct amdgpu_bo_list **result);
 void amdgpu_bo_list_put(struct amdgpu_bo_list *list);
 int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +4 -7
index 6ee77f431d56..88f104041157 100644
@@ -1445,8 +1445,7 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
 	if (r)
 		goto error_vm;
 
-	mutex_init(&fpriv->bo_list_lock);
-	idr_init_base(&fpriv->bo_list_handles, 1);
+	xa_init_flags(&fpriv->bo_list_handles, XA_FLAGS_ALLOC1);
 
 	r = amdgpu_userq_mgr_init(&fpriv->userq_mgr, file_priv, adev);
 	if (r)
@@ -1492,8 +1491,8 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
 	struct amdgpu_bo_list *list;
 	struct amdgpu_bo *pd;
+	unsigned long handle;
 	u32 pasid;
-	int handle;
 
 	if (!fpriv)
 		return;
@@ -1529,11 +1528,9 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
 		amdgpu_pasid_free_delayed(pd->tbo.base.resv, pasid);
 	amdgpu_bo_unref(&pd);
 
-	idr_for_each_entry(&fpriv->bo_list_handles, list, handle)
+	xa_for_each(&fpriv->bo_list_handles, handle, list)
 		amdgpu_bo_list_put(list);
-
-	idr_destroy(&fpriv->bo_list_handles);
-	mutex_destroy(&fpriv->bo_list_lock);
+	xa_destroy(&fpriv->bo_list_handles);
 
 	kfree(fpriv);
 	file_priv->driver_priv = NULL;
Signature · 1 line
2.51.1
maintainer
#2 lore raw

Re: [RFC 1/5] drm/amdgpu: Replace idr with xarray in amdgpu_bo_list

Quoted 63 lines · Tvrtko Ursulin

IDR is deprecated so let's replace it with xarray.

Conversion is mostly 1:1 apart from AMDGPU_BO_LIST_OP_UPDATE which was implemented with idr_replace, and has now been replaced with a sequence of xa_load and xa_cmpxchg. Should userspace attempt multi-threaded update operations on the same handle it could theoretically hit a new -ENOENT path. But I believe this is purely theoretical and still safe.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h         |  4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 72 ++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c     | 11 ++--
 4 files changed, 41 insertions(+), 48 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -2
index 9f9774f58ce1..0a5b802bd3c3 100644
@@ -50,6 +50,7 @@
 #include <linux/hashtable.h>
 #include <linux/dma-fence.h>
 #include <linux/pci.h>
+#include <linux/xarray.h>
 
 #include <drm/ttm/ttm_bo.h>
 #include <drm/ttm/ttm_placement.h>
@@ -500,8 +501,7 @@ struct amdgpu_fpriv {
 	struct amdgpu_bo_va	*prt_va;
 	struct amdgpu_bo_va	*csa_va;
 	struct amdgpu_bo_va	*seq64_va;
-	struct mutex		bo_list_lock;
-	struct idr		bo_list_handles;
+	struct xarray		bo_list_handles;
 	struct amdgpu_ctx_mgr	ctx_mgr;
 	struct amdgpu_userq_mgr	userq_mgr;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +1 -14
index 66fb37b64388..628d32fd2fae 100644
@@ -149,31 +149,20 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
 
 }
 
-static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
-{
-	struct amdgpu_bo_list *list;
-
-	mutex_lock(&fpriv->bo_list_lock);
-	list = idr_remove(&fpriv->bo_list_handles, id);
-	mutex_unlock(&fpriv->bo_list_lock);
-	if (list)
-		kref_put(&list->refcount, amdgpu_bo_list_free);
-}
-
-int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
+int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id,
 		       struct amdgpu_bo_list **result)
 {
-	rcu_read_lock();
-	*result = idr_find(&fpriv->bo_list_handles, id);

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp, · line 160

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +5 -4
+	struct amdgpu_bo_list *list;
 
-	if (*result && kref_get_unless_zero(&(*result)->refcount)) {
-		rcu_read_unlock();
-		return 0;
-	}
+	xa_lock(&fpriv->bo_list_handles);
+	list = xa_load(&fpriv->bo_list_handles, id);
+	if (list && !kref_get_unless_zero(&list->refcount))
+		list = NULL;

Since you have this protected by xa_lock() now you don't need the kref_get_unless_zero() any more and can also drop the RCU handling in amdgpu_bo_list_free().

Alternatively stop using xa_lock() and use the RCU read side lock instead, but I clearly prefer this approach here.

Apart from that looks good to me.

Regards,
Christian.

Quoted 136 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +28 -20
+	xa_unlock(&fpriv->bo_list_handles);
 
-	rcu_read_unlock();
-	*result = NULL;
-	return -ENOENT;
+	*result = list;
+
+	return list ? 0 : -ENOENT;
 }
 
 void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
@@ -219,12 +208,12 @@ int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
 int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
 				struct drm_file *filp)
 {
-	struct amdgpu_device *adev = drm_to_adev(dev);
 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
-	union drm_amdgpu_bo_list *args = data;
-	uint32_t handle = args->in.list_handle;
+	struct amdgpu_device *adev = drm_to_adev(dev);
 	struct drm_amdgpu_bo_list_entry *info = NULL;
-	struct amdgpu_bo_list *list, *old;
+	struct amdgpu_bo_list *list, *prev, *curr;
+	uint32_t handle = args->in.list_handle;
+	union drm_amdgpu_bo_list *args = data;
 	int r;
 
 	r = amdgpu_bo_create_list_entry_array(&args->in, &info);
@@ -238,19 +227,19 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
 		if (r)
 			goto error_free;
 
-		mutex_lock(&fpriv->bo_list_lock);
-		r = idr_alloc(&fpriv->bo_list_handles, list, 1, 0, GFP_KERNEL);
-		mutex_unlock(&fpriv->bo_list_lock);
-		if (r < 0) {
+		r = xa_alloc(&fpriv->bo_list_handles, &handle, list,
+			     xa_limit_32b, GFP_KERNEL);
+		if (r)
 			goto error_put_list;
-		}
 
-		handle = r;
 		break;
 
 	case AMDGPU_BO_LIST_OP_DESTROY:
-		amdgpu_bo_list_destroy(fpriv, handle);
+		list = xa_erase(&fpriv->bo_list_handles, handle);
+		if (list)
+			amdgpu_bo_list_put(list);
 		handle = 0;
+
 		break;
 
 	case AMDGPU_BO_LIST_OP_UPDATE:
@@ -259,16 +248,23 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
 		if (r)
 			goto error_free;
 
-		mutex_lock(&fpriv->bo_list_lock);
-		old = idr_replace(&fpriv->bo_list_handles, list, handle);
-		mutex_unlock(&fpriv->bo_list_lock);
+		curr = xa_load(&fpriv->bo_list_handles, handle);
+		if (!curr) {
+			r = -ENOENT;
+			goto error_put_list;
+		}
 
-		if (IS_ERR(old)) {
-			r = PTR_ERR(old);
+		prev = xa_cmpxchg(&fpriv->bo_list_handles, handle, curr, list,
+				  GFP_KERNEL);
+		if (xa_is_err(prev)) {
+			r = xa_err(prev);
+			goto error_put_list;
+		} else if (prev != curr) {
+			r = -ENOENT;
 			goto error_put_list;
 		}
 
-		amdgpu_bo_list_put(old);
+		amdgpu_bo_list_put(curr);
 		break;
 
 	default:
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +1 -1
index 2b5e7c46a39d..0989f1090c63 100644
@@ -58,7 +58,7 @@ struct amdgpu_bo_list {
 	struct amdgpu_bo_list_entry entries[] __counted_by(num_entries);
 };
 
-int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
+int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id,
 		       struct amdgpu_bo_list **result);
 void amdgpu_bo_list_put(struct amdgpu_bo_list *list);
 int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +4 -7
index 6ee77f431d56..88f104041157 100644
@@ -1445,8 +1445,7 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
 	if (r)
 		goto error_vm;
 
-	mutex_init(&fpriv->bo_list_lock);
-	idr_init_base(&fpriv->bo_list_handles, 1);
+	xa_init_flags(&fpriv->bo_list_handles, XA_FLAGS_ALLOC1);
 
 	r = amdgpu_userq_mgr_init(&fpriv->userq_mgr, file_priv, adev);
 	if (r)
@@ -1492,8 +1491,8 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
 	struct amdgpu_bo_list *list;
 	struct amdgpu_bo *pd;
+	unsigned long handle;
 	u32 pasid;
-	int handle;
 
 	if (!fpriv)
 		return;
@@ -1529,11 +1528,9 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
 		amdgpu_pasid_free_delayed(pd->tbo.base.resv, pasid);
 	amdgpu_bo_unref(&pd);
 
-	idr_for_each_entry(&fpriv->bo_list_handles, list, handle)
+	xa_for_each(&fpriv->bo_list_handles, handle, list)
 		amdgpu_bo_list_put(list);
-
-	idr_destroy(&fpriv->bo_list_handles);
-	mutex_destroy(&fpriv->bo_list_lock);
+	xa_destroy(&fpriv->bo_list_handles);
 
 	kfree(fpriv);
 	file_priv->driver_priv = NULL;
#3 lore raw

Re: [RFC 1/5] drm/amdgpu: Replace idr with xarray in amdgpu_bo_list

Quoted 77 lines · Christian König
Quoted 63 lines · Tvrtko Ursulin

IDR is deprecated so let's replace it with xarray.

Conversion is mostly 1:1 apart from AMDGPU_BO_LIST_OP_UPDATE which was implemented with idr_replace, and has now been replaced with a sequence of xa_load and xa_cmpxchg. Should userspace attempt multi-threaded update operations on the same handle it could theoretically hit a new -ENOENT path. But I believe this is purely theoretical and still safe.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h         |  4 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 72 ++++++++++-----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h |  2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c     | 11 ++--
  4 files changed, 41 insertions(+), 48 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -2
index 9f9774f58ce1..0a5b802bd3c3 100644
@@ -50,6 +50,7 @@
  #include <linux/hashtable.h>
  #include <linux/dma-fence.h>
  #include <linux/pci.h>
+#include <linux/xarray.h>
 
  #include <drm/ttm/ttm_bo.h>
  #include <drm/ttm/ttm_placement.h>
@@ -500,8 +501,7 @@ struct amdgpu_fpriv {
  	struct amdgpu_bo_va	*prt_va;
  	struct amdgpu_bo_va	*csa_va;
  	struct amdgpu_bo_va	*seq64_va;
-	struct mutex		bo_list_lock;
-	struct idr		bo_list_handles;
+	struct xarray		bo_list_handles;
  	struct amdgpu_ctx_mgr	ctx_mgr;
  	struct amdgpu_userq_mgr	userq_mgr;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +1 -14
index 66fb37b64388..628d32fd2fae 100644
@@ -149,31 +149,20 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
 
  }
 
-static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
-{
-	struct amdgpu_bo_list *list;
-
-	mutex_lock(&fpriv->bo_list_lock);
-	list = idr_remove(&fpriv->bo_list_handles, id);
-	mutex_unlock(&fpriv->bo_list_lock);
-	if (list)
-		kref_put(&list->refcount, amdgpu_bo_list_free);
-}
-
-int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
+int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id,
  		       struct amdgpu_bo_list **result)
  {
-	rcu_read_lock();
-	*result = idr_find(&fpriv->bo_list_handles, id);

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp, · line 160

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +5 -4
+	struct amdgpu_bo_list *list;
 
-	if (*result && kref_get_unless_zero(&(*result)->refcount)) {
-		rcu_read_unlock();
-		return 0;
-	}
+	xa_lock(&fpriv->bo_list_handles);
+	list = xa_load(&fpriv->bo_list_handles, id);
+	if (list && !kref_get_unless_zero(&list->refcount))
+		list = NULL;

Since you have this protected by xa_lock() now you don't need the kref_get_unless_zero() any more and can also drop the RCU handling in amdgpu_bo_list_free().

Quoted 1 line

Alternatively stop using xa_lock() and use the RCU read side lock instead, but I clearly prefer this approach here.

True, ok.

Quoted 2 lines

Apart from that looks good to me.

Btw what about the bo_list_mutex? CS parsing looks to be read only with respect to the bo_list so what it is protecting?

Regards,

Tvrtko

Quoted 137 lines
Quoted 136 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +28 -20
+	xa_unlock(&fpriv->bo_list_handles);
 
-	rcu_read_unlock();
-	*result = NULL;
-	return -ENOENT;
+	*result = list;
+
+	return list ? 0 : -ENOENT;
  }
 
  void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
@@ -219,12 +208,12 @@ int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
  int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
  				struct drm_file *filp)
  {
-	struct amdgpu_device *adev = drm_to_adev(dev);
  	struct amdgpu_fpriv *fpriv = filp->driver_priv;
-	union drm_amdgpu_bo_list *args = data;
-	uint32_t handle = args->in.list_handle;
+	struct amdgpu_device *adev = drm_to_adev(dev);
  	struct drm_amdgpu_bo_list_entry *info = NULL;
-	struct amdgpu_bo_list *list, *old;
+	struct amdgpu_bo_list *list, *prev, *curr;
+	uint32_t handle = args->in.list_handle;
+	union drm_amdgpu_bo_list *args = data;
  	int r;
 
  	r = amdgpu_bo_create_list_entry_array(&args->in, &info);
@@ -238,19 +227,19 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
  		if (r)
  			goto error_free;
 
-		mutex_lock(&fpriv->bo_list_lock);
-		r = idr_alloc(&fpriv->bo_list_handles, list, 1, 0, GFP_KERNEL);
-		mutex_unlock(&fpriv->bo_list_lock);
-		if (r < 0) {
+		r = xa_alloc(&fpriv->bo_list_handles, &handle, list,
+			     xa_limit_32b, GFP_KERNEL);
+		if (r)
  			goto error_put_list;
-		}
 
-		handle = r;
  		break;
 
  	case AMDGPU_BO_LIST_OP_DESTROY:
-		amdgpu_bo_list_destroy(fpriv, handle);
+		list = xa_erase(&fpriv->bo_list_handles, handle);
+		if (list)
+			amdgpu_bo_list_put(list);
  		handle = 0;
+
  		break;
 
  	case AMDGPU_BO_LIST_OP_UPDATE:
@@ -259,16 +248,23 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
  		if (r)
  			goto error_free;
 
-		mutex_lock(&fpriv->bo_list_lock);
-		old = idr_replace(&fpriv->bo_list_handles, list, handle);
-		mutex_unlock(&fpriv->bo_list_lock);
+		curr = xa_load(&fpriv->bo_list_handles, handle);
+		if (!curr) {
+			r = -ENOENT;
+			goto error_put_list;
+		}
 
-		if (IS_ERR(old)) {
-			r = PTR_ERR(old);
+		prev = xa_cmpxchg(&fpriv->bo_list_handles, handle, curr, list,
+				  GFP_KERNEL);
+		if (xa_is_err(prev)) {
+			r = xa_err(prev);
+			goto error_put_list;
+		} else if (prev != curr) {
+			r = -ENOENT;
  			goto error_put_list;
  		}
 
-		amdgpu_bo_list_put(old);
+		amdgpu_bo_list_put(curr);
  		break;
 
  	default:
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +1 -1
index 2b5e7c46a39d..0989f1090c63 100644
@@ -58,7 +58,7 @@ struct amdgpu_bo_list {
  	struct amdgpu_bo_list_entry entries[] __counted_by(num_entries);
  };
 
-int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
+int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id,
  		       struct amdgpu_bo_list **result);
  void amdgpu_bo_list_put(struct amdgpu_bo_list *list);
  int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +4 -7
index 6ee77f431d56..88f104041157 100644
@@ -1445,8 +1445,7 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
  	if (r)
  		goto error_vm;
 
-	mutex_init(&fpriv->bo_list_lock);
-	idr_init_base(&fpriv->bo_list_handles, 1);
+	xa_init_flags(&fpriv->bo_list_handles, XA_FLAGS_ALLOC1);
 
  	r = amdgpu_userq_mgr_init(&fpriv->userq_mgr, file_priv, adev);
  	if (r)
@@ -1492,8 +1491,8 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
  	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
  	struct amdgpu_bo_list *list;
  	struct amdgpu_bo *pd;
+	unsigned long handle;
  	u32 pasid;
-	int handle;
 
  	if (!fpriv)
  		return;
@@ -1529,11 +1528,9 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
  		amdgpu_pasid_free_delayed(pd->tbo.base.resv, pasid);
  	amdgpu_bo_unref(&pd);
 
-	idr_for_each_entry(&fpriv->bo_list_handles, list, handle)
+	xa_for_each(&fpriv->bo_list_handles, handle, list)
  		amdgpu_bo_list_put(list);
-
-	idr_destroy(&fpriv->bo_list_handles);
-	mutex_destroy(&fpriv->bo_list_lock);
+	xa_destroy(&fpriv->bo_list_handles);
 
  	kfree(fpriv);
  	file_priv->driver_priv = NULL;
maintainer
#4 lore raw

Re: [RFC 1/5] drm/amdgpu: Replace idr with xarray in amdgpu_bo_list

Quoted 74 lines · Tvrtko Ursulin
Quoted 72 lines · Christian König
Quoted 58 lines · Tvrtko Ursulin

IDR is deprecated so let's replace it with xarray.

Conversion is mostly 1:1 apart from AMDGPU_BO_LIST_OP_UPDATE which was implemented with idr_replace, and has now been replaced with a sequence of xa_load and xa_cmpxchg. Should userspace attempt multi-threaded update operations on the same handle it could theoretically hit a new -ENOENT path. But I believe this is purely theoretical and still safe.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h         |  4 +- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 72 ++++++++++----------- drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h |  2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c     | 11 ++--

  4 files changed, 41 insertions(+), 48 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -2
index 9f9774f58ce1..0a5b802bd3c3 100644
@@ -50,6 +50,7 @@
   #include <linux/hashtable.h>
   #include <linux/dma-fence.h>
   #include <linux/pci.h>
+#include <linux/xarray.h>
     #include <drm/ttm/ttm_bo.h>
   #include <drm/ttm/ttm_placement.h>
@@ -500,8 +501,7 @@ struct amdgpu_fpriv {
       struct amdgpu_bo_va    *prt_va;
       struct amdgpu_bo_va    *csa_va;
       struct amdgpu_bo_va    *seq64_va;
-    struct mutex        bo_list_lock;
-    struct idr        bo_list_handles;
+    struct xarray        bo_list_handles;
       struct amdgpu_ctx_mgr    ctx_mgr;
       struct amdgpu_userq_mgr    userq_mgr;

  diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c index 66fb37b64388..628d32fd2fae 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +1 -12
@@ -149,31 +149,20 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
     }
   -static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
-{
-    struct amdgpu_bo_list *list;
-
-    mutex_lock(&fpriv->bo_list_lock);
-    list = idr_remove(&fpriv->bo_list_handles, id);
-    mutex_unlock(&fpriv->bo_list_lock);
-    if (list)
-        kref_put(&list->refcount, amdgpu_bo_list_free);
-}
-
-int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
+int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id,
                  struct amdgpu_bo_list **result)
   {
-    rcu_read_lock();

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp, · line 159

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +5 -4
-    *result = idr_find(&fpriv->bo_list_handles, id);
+    struct amdgpu_bo_list *list;
   -    if (*result && kref_get_unless_zero(&(*result)->refcount)) {
-        rcu_read_unlock();
-        return 0;
-    }
+    xa_lock(&fpriv->bo_list_handles);
+    list = xa_load(&fpriv->bo_list_handles, id);
+    if (list && !kref_get_unless_zero(&list->refcount))
+        list = NULL;

Since you have this protected by xa_lock() now you don't need the kref_get_unless_zero() any more and can also drop the RCU handling in amdgpu_bo_list_free().

Quoted 8 lines
Quoted 1 line

Alternatively stop using xa_lock() and use the RCU read side lock instead, but I clearly prefer this approach here.

True, ok.

Quoted 2 lines

Apart from that looks good to me.

Btw what about the bo_list_mutex? CS parsing looks to be read only with respect to the bo_list so what it is protecting?

Oh, yeah good point. That one is superflous now as well.

Previously the entries in the BO list where ttm execution manager entries linked in a double linked list.

But that was replaced with drm_exec(), so everything should be read only now.

Regards,
Christian.

Quoted 126 lines

Regards,

Tvrtko

Quoted 120 lines
Quoted 119 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +11 -10
+    xa_unlock(&fpriv->bo_list_handles);
   -    rcu_read_unlock();
-    *result = NULL;
-    return -ENOENT;
+    *result = list;
+
+    return list ? 0 : -ENOENT;
   }
     void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
@@ -219,12 +208,12 @@ int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
   int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
                   struct drm_file *filp)
   {
-    struct amdgpu_device *adev = drm_to_adev(dev);
       struct amdgpu_fpriv *fpriv = filp->driver_priv;
-    union drm_amdgpu_bo_list *args = data;
-    uint32_t handle = args->in.list_handle;
+    struct amdgpu_device *adev = drm_to_adev(dev);
       struct drm_amdgpu_bo_list_entry *info = NULL;
-    struct amdgpu_bo_list *list, *old;
+    struct amdgpu_bo_list *list, *prev, *curr;
+    uint32_t handle = args->in.list_handle;
+    union drm_amdgpu_bo_list *args = data;
       int r;
         r = amdgpu_bo_create_list_entry_array(&args->in, &info);
@@ -238,19 +227,19 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
           if (r)
               goto error_free;
   -        mutex_lock(&fpriv->bo_list_lock);
-        r = idr_alloc(&fpriv->bo_list_handles, list, 1, 0, GFP_KERNEL);
-        mutex_unlock(&fpriv->bo_list_lock);
-        if (r < 0) {
+        r = xa_alloc(&fpriv->bo_list_handles, &handle, list,
+                 xa_limit_32b, GFP_KERNEL);
+        if (r)
               goto error_put_list;
-        }
   -        handle = r;
           break;

        case AMDGPU_BO_LIST_OP_DESTROY:
-        amdgpu_bo_list_destroy(fpriv, handle);
+        list = xa_erase(&fpriv->bo_list_handles, handle); +        if (list)
+            amdgpu_bo_list_put(list);
          handle = 0;
+
          break;
        case AMDGPU_BO_LIST_OP_UPDATE:

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +13 -3
@@ -259,16 +248,23 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
           if (r)
               goto error_free;
   -        mutex_lock(&fpriv->bo_list_lock);
-        old = idr_replace(&fpriv->bo_list_handles, list, handle);
-        mutex_unlock(&fpriv->bo_list_lock);
+        curr = xa_load(&fpriv->bo_list_handles, handle);
+        if (!curr) {
+            r = -ENOENT;
+            goto error_put_list;
+        }
   -        if (IS_ERR(old)) {
-            r = PTR_ERR(old);
+        prev = xa_cmpxchg(&fpriv->bo_list_handles, handle, curr, list,
+                  GFP_KERNEL);
+        if (xa_is_err(prev)) {
+            r = xa_err(prev);
+            goto error_put_list;
+        } else if (prev != curr) {
+            r = -ENOENT;
               goto error_put_list;
           }
   -        amdgpu_bo_list_put(old);
+        amdgpu_bo_list_put(curr);
           break;

        default:

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +1 -0
index 2b5e7c46a39d..0989f1090c63 100644
@@ -58,7 +58,7 @@ struct amdgpu_bo_list {
       struct amdgpu_bo_list_entry entries[] __counted_by(num_entries);
   };
   -int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id,
+int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id,
                  struct amdgpu_bo_list **result);
   void amdgpu_bo_list_put(struct amdgpu_bo_list *list);
   int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +4 -5
index 6ee77f431d56..88f104041157 100644
@@ -1445,8 +1445,7 @@ int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
       if (r)
           goto error_vm;
   -    mutex_init(&fpriv->bo_list_lock);
-    idr_init_base(&fpriv->bo_list_handles, 1);
+    xa_init_flags(&fpriv->bo_list_handles, XA_FLAGS_ALLOC1);
         r = amdgpu_userq_mgr_init(&fpriv->userq_mgr, file_priv, adev);
       if (r)
@@ -1492,8 +1491,8 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
       struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
       struct amdgpu_bo_list *list;
       struct amdgpu_bo *pd;
+    unsigned long handle;
       u32 pasid;
-    int handle;
         if (!fpriv)
           return;
@@ -1529,11 +1528,9 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
           amdgpu_pasid_free_delayed(pd->tbo.base.resv, pasid);
       amdgpu_bo_unref(&pd);
   -    idr_for_each_entry(&fpriv->bo_list_handles, list, handle)
+    xa_for_each(&fpriv->bo_list_handles, handle, list)
           amdgpu_bo_list_put(list);
-
-    idr_destroy(&fpriv->bo_list_handles);
-    mutex_destroy(&fpriv->bo_list_lock);
+    xa_destroy(&fpriv->bo_list_handles);
         kfree(fpriv);
       file_priv->driver_priv = NULL;
#5 lore raw

RFC 2/5 drm/amdgpu: Remove output parameter in bo list handling

In reply to Tvrtko Ursulin

Removing the output parameter from a few functions should result in more readable code and also enables us to save some lines.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 86 ++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 17 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c      | 58 +++++++-------
 3 files changed, 75 insertions(+), 86 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +40 -46
index 628d32fd2fae..0ab307317145 100644
@@ -67,9 +67,9 @@ static int amdgpu_bo_list_entry_cmp(const void *_a, const void *_b)
 	return 0;
 }
 
-int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
-			  struct drm_amdgpu_bo_list_entry *info,
-			  size_t num_entries, struct amdgpu_bo_list **result)
+struct amdgpu_bo_list *
+amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
+		      struct drm_amdgpu_bo_list_entry *info, size_t num_entries)
 {
 	unsigned last_entry = 0, first_userptr = num_entries;
 	struct amdgpu_bo_list_entry *array;
@@ -80,7 +80,7 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
 
 	list = kvzalloc(struct_size(list, entries, num_entries), GFP_KERNEL);
 	if (!list)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
 	kref_init(&list->refcount);
 
@@ -136,8 +136,7 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
 	trace_amdgpu_cs_bo_status(list->num_entries, total_size);
 
 	mutex_init(&list->bo_list_mutex);
-	*result = list;
-	return 0;
+	return list;
 
 error_free:
 	for (i = 0; i < last_entry; ++i)
@@ -145,24 +144,21 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
 	for (i = first_userptr; i < num_entries; ++i)
 		amdgpu_bo_unref(&array[i].bo);
 	kvfree(list);
-	return r;
+	return ERR_PTR(r);
 
 }
 
-int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id,
-		       struct amdgpu_bo_list **result)
+struct amdgpu_bo_list *amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id)
 {
 	struct amdgpu_bo_list *list;
 
 	xa_lock(&fpriv->bo_list_handles);
 	list = xa_load(&fpriv->bo_list_handles, id);
-	if (list && !kref_get_unless_zero(&list->refcount))
-		list = NULL;
+	if (!list || !kref_get_unless_zero(&list->refcount))
+		list = ERR_PTR(-ENOENT);
 	xa_unlock(&fpriv->bo_list_handles);
 
-	*result = list;
-
-	return list ? 0 : -ENOENT;
+	return list;
 }
 
 void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
@@ -170,8 +166,8 @@ void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
 	kref_put(&list->refcount, amdgpu_bo_list_free);
 }
 
-int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
-				      struct drm_amdgpu_bo_list_entry **info_param)
+struct drm_amdgpu_bo_list_entry *
+amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in)
 {
 	const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
 	const void __user *uptr = u64_to_user_ptr(in->bo_info_ptr);
@@ -182,27 +178,24 @@ int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
 	/* copy the handle array from userspace to a kernel buffer */
 	if (likely(info_size == bo_info_size)) {
 		info = vmemdup_array_user(uptr, bo_number, info_size);
-		if (IS_ERR(info))
-			return PTR_ERR(info);
 	} else {
 		const uint32_t bytes = min(bo_info_size, info_size);
 		unsigned i;
 
 		info = kvmalloc_array(bo_number, info_size, GFP_KERNEL);
 		if (!info)
-			return -ENOMEM;
+			return ERR_PTR(-ENOMEM);
 
 		memset(info, 0, bo_number * info_size);
 		for (i = 0; i < bo_number; ++i, uptr += bo_info_size) {
 			if (copy_from_user(&info[i], uptr, bytes)) {
 				kvfree(info);
-				return -EFAULT;
+				return ERR_PTR(-EFAULT);
 			}
 		}
 	}
 
-	*info_param = info;
-	return 0;
+	return info;
 }
 
 int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
@@ -210,27 +203,24 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
 {
 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
 	struct amdgpu_device *adev = drm_to_adev(dev);
-	struct drm_amdgpu_bo_list_entry *info = NULL;
 	struct amdgpu_bo_list *list, *prev, *curr;
-	uint32_t handle = args->in.list_handle;
 	union drm_amdgpu_bo_list *args = data;
+	uint32_t handle = args->in.list_handle;
+	struct drm_amdgpu_bo_list_entry *info;
 	int r;
 
-	r = amdgpu_bo_create_list_entry_array(&args->in, &info);
-	if (r)
-		return r;
-
 	switch (args->in.operation) {
 	case AMDGPU_BO_LIST_OP_CREATE:
-		r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
-					  &list);
-		if (r)
-			goto error_free;
+	case AMDGPU_BO_LIST_OP_UPDATE:
+		info = amdgpu_bo_create_list_entry_array(&args->in);
+		if (IS_ERR(info))
+			return PTR_ERR(info);
 
-		r = xa_alloc(&fpriv->bo_list_handles, &handle, list,
-			     xa_limit_32b, GFP_KERNEL);
-		if (r)
-			goto error_put_list;
+		list = amdgpu_bo_list_create(adev, filp, info,
+					     args->in.bo_number);
+		kvfree(info);
+		if (IS_ERR(list))
+			return PTR_ERR(list);
 
 		break;
 
@@ -242,12 +232,20 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
 
 		break;
 
-	case AMDGPU_BO_LIST_OP_UPDATE:
-		r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
-					  &list);
+	default:
+		return -EINVAL;
+	};
+
+	switch (args->in.operation) {
+	case AMDGPU_BO_LIST_OP_CREATE:
+		r = xa_alloc(&fpriv->bo_list_handles, &handle, list,
+			     xa_limit_32b, GFP_KERNEL);
 		if (r)
-			goto error_free;
+			goto error_put_list;
 
+		break;
+
+	case AMDGPU_BO_LIST_OP_UPDATE:
 		curr = xa_load(&fpriv->bo_list_handles, handle);
 		if (!curr) {
 			r = -ENOENT;
@@ -267,21 +265,17 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
 		amdgpu_bo_list_put(curr);
 		break;
 
+	case AMDGPU_BO_LIST_OP_DESTROY:
 	default:
-		r = -EINVAL;
-		goto error_free;
+		/* Handled above. */
 	}
 
 	memset(args, 0, sizeof(*args));
 	args->out.list_handle = handle;
-	kvfree(info);
 
 	return 0;
 
 error_put_list:
 	amdgpu_bo_list_put(list);
-
-error_free:
-	kvfree(info);
 	return r;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +8 -9
index 0989f1090c63..085ca94f97a2 100644
@@ -58,17 +58,16 @@ struct amdgpu_bo_list {
 	struct amdgpu_bo_list_entry entries[] __counted_by(num_entries);
 };
 
-int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id,
-		       struct amdgpu_bo_list **result);
+struct amdgpu_bo_list *amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id);
 void amdgpu_bo_list_put(struct amdgpu_bo_list *list);
-int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
-				      struct drm_amdgpu_bo_list_entry **info_param);
+struct drm_amdgpu_bo_list_entry *
+amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in);
 
-int amdgpu_bo_list_create(struct amdgpu_device *adev,
-				 struct drm_file *filp,
-				 struct drm_amdgpu_bo_list_entry *info,
-				 size_t num_entries,
-				 struct amdgpu_bo_list **list);
+struct amdgpu_bo_list *
+amdgpu_bo_list_create(struct amdgpu_device *adev,
+		      struct drm_file *filp,
+		      struct drm_amdgpu_bo_list_entry *info,
+		      size_t num_entries);
 
 #define amdgpu_bo_list_for_each_entry(e, list) \
 	for (e = list->entries; \
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +27 -31

113 lines not on this page · raw message

Signature · 1 line
2.51.1
maintainer
#6 lore raw

Re: [RFC 2/5] drm/amdgpu: Remove output parameter in bo list handling

Quoted 2 lines · Tvrtko Ursulin

Removing the output parameter from a few functions should result in more readable code and also enables us to save some lines.

Oh, yes please.

That was just because somebody had a personal preference for this coding style and it resulted in at least one CVE entry for amdgpu a couple of years ago.

I should have pushed back on this harder when I've seen it initially.

Quoted 181 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 86 ++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 17 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c      | 58 +++++++-------
 3 files changed, 75 insertions(+), 86 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +38 -40
index 628d32fd2fae..0ab307317145 100644
@@ -67,9 +67,9 @@ static int amdgpu_bo_list_entry_cmp(const void *_a, const void *_b)
 	return 0;
 }
 
-int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
-			  struct drm_amdgpu_bo_list_entry *info,
-			  size_t num_entries, struct amdgpu_bo_list **result)
+struct amdgpu_bo_list *
+amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
+		      struct drm_amdgpu_bo_list_entry *info, size_t num_entries)
 {
 	unsigned last_entry = 0, first_userptr = num_entries;
 	struct amdgpu_bo_list_entry *array;
@@ -80,7 +80,7 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
 
 	list = kvzalloc(struct_size(list, entries, num_entries), GFP_KERNEL);
 	if (!list)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
 	kref_init(&list->refcount);
 
@@ -136,8 +136,7 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
 	trace_amdgpu_cs_bo_status(list->num_entries, total_size);
 
 	mutex_init(&list->bo_list_mutex);
-	*result = list;
-	return 0;
+	return list;
 
 error_free:
 	for (i = 0; i < last_entry; ++i)
@@ -145,24 +144,21 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
 	for (i = first_userptr; i < num_entries; ++i)
 		amdgpu_bo_unref(&array[i].bo);
 	kvfree(list);
-	return r;
+	return ERR_PTR(r);
 
 }
 
-int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id,
-		       struct amdgpu_bo_list **result)
+struct amdgpu_bo_list *amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id)
 {
 	struct amdgpu_bo_list *list;
 
 	xa_lock(&fpriv->bo_list_handles);
 	list = xa_load(&fpriv->bo_list_handles, id);
-	if (list && !kref_get_unless_zero(&list->refcount))
-		list = NULL;
+	if (!list || !kref_get_unless_zero(&list->refcount))
+		list = ERR_PTR(-ENOENT);
 	xa_unlock(&fpriv->bo_list_handles);
 
-	*result = list;
-
-	return list ? 0 : -ENOENT;
+	return list;
 }
 
 void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
@@ -170,8 +166,8 @@ void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
 	kref_put(&list->refcount, amdgpu_bo_list_free);
 }
 
-int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
-				      struct drm_amdgpu_bo_list_entry **info_param)
+struct drm_amdgpu_bo_list_entry *
+amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in)
 {
 	const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
 	const void __user *uptr = u64_to_user_ptr(in->bo_info_ptr);
@@ -182,27 +178,24 @@ int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
 	/* copy the handle array from userspace to a kernel buffer */
 	if (likely(info_size == bo_info_size)) {
 		info = vmemdup_array_user(uptr, bo_number, info_size);
-		if (IS_ERR(info))
-			return PTR_ERR(info);
 	} else {
 		const uint32_t bytes = min(bo_info_size, info_size);
 		unsigned i;
 
 		info = kvmalloc_array(bo_number, info_size, GFP_KERNEL);
 		if (!info)
-			return -ENOMEM;
+			return ERR_PTR(-ENOMEM);
 
 		memset(info, 0, bo_number * info_size);
 		for (i = 0; i < bo_number; ++i, uptr += bo_info_size) {
 			if (copy_from_user(&info[i], uptr, bytes)) {
 				kvfree(info);
-				return -EFAULT;
+				return ERR_PTR(-EFAULT);
 			}
 		}
 	}
 
-	*info_param = info;
-	return 0;
+	return info;
 }
 
 int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
@@ -210,27 +203,24 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
 {
 	struct amdgpu_fpriv *fpriv = filp->driver_priv;
 	struct amdgpu_device *adev = drm_to_adev(dev);
-	struct drm_amdgpu_bo_list_entry *info = NULL;
 	struct amdgpu_bo_list *list, *prev, *curr;
-	uint32_t handle = args->in.list_handle;
 	union drm_amdgpu_bo_list *args = data;
+	uint32_t handle = args->in.list_handle;
+	struct drm_amdgpu_bo_list_entry *info;
 	int r;
 
-	r = amdgpu_bo_create_list_entry_array(&args->in, &info);
-	if (r)
-		return r;
-
 	switch (args->in.operation) {
 	case AMDGPU_BO_LIST_OP_CREATE:
-		r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
-					  &list);
-		if (r)
-			goto error_free;
+	case AMDGPU_BO_LIST_OP_UPDATE:
+		info = amdgpu_bo_create_list_entry_array(&args->in);
+		if (IS_ERR(info))
+			return PTR_ERR(info);
 
-		r = xa_alloc(&fpriv->bo_list_handles, &handle, list,
-			     xa_limit_32b, GFP_KERNEL);
-		if (r)
-			goto error_put_list;
+		list = amdgpu_bo_list_create(adev, filp, info,
+					     args->in.bo_number);
+		kvfree(info);
+		if (IS_ERR(list))
+			return PTR_ERR(list);
 
 		break;
 
@@ -242,12 +232,20 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
 
 		break;
 
-	case AMDGPU_BO_LIST_OP_UPDATE:
-		r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
-					  &list);
+	default:
+		return -EINVAL;
+	};
+
+	switch (args->in.operation) {
+	case AMDGPU_BO_LIST_OP_CREATE:
+		r = xa_alloc(&fpriv->bo_list_handles, &handle, list,
+			     xa_limit_32b, GFP_KERNEL);
 		if (r)
-			goto error_free;
+			goto error_put_list;
 
+		break;
+
+	case AMDGPU_BO_LIST_OP_UPDATE:
 		curr = xa_load(&fpriv->bo_list_handles, handle);
 		if (!curr) {
 			r = -ENOENT;

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data, · line 270

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +2 -2
@@ -267,21 +265,17 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
 		amdgpu_bo_list_put(curr);
 		break;
 
+	case AMDGPU_BO_LIST_OP_DESTROY:
 	default:
-		r = -EINVAL;
-		goto error_free;
+		/* Handled above. */

I think I prefer to keep that a single switch statement. This looks a bit mixed up to me.

Quoted 92 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +0 -4
 	}
 
 	memset(args, 0, sizeof(*args));
 	args->out.list_handle = handle;
-	kvfree(info);
 
 	return 0;
 
 error_put_list:
 	amdgpu_bo_list_put(list);
-
-error_free:
-	kvfree(info);
 	return r;
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +8 -9
index 0989f1090c63..085ca94f97a2 100644
@@ -58,17 +58,16 @@ struct amdgpu_bo_list {
 	struct amdgpu_bo_list_entry entries[] __counted_by(num_entries);
 };
 
-int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id,
-		       struct amdgpu_bo_list **result);
+struct amdgpu_bo_list *amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id);
 void amdgpu_bo_list_put(struct amdgpu_bo_list *list);
-int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
-				      struct drm_amdgpu_bo_list_entry **info_param);
+struct drm_amdgpu_bo_list_entry *
+amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in);
 
-int amdgpu_bo_list_create(struct amdgpu_device *adev,
-				 struct drm_file *filp,
-				 struct drm_amdgpu_bo_list_entry *info,
-				 size_t num_entries,
-				 struct amdgpu_bo_list **list);
+struct amdgpu_bo_list *
+amdgpu_bo_list_create(struct amdgpu_device *adev,
+		      struct drm_file *filp,
+		      struct drm_amdgpu_bo_list_entry *info,
+		      size_t num_entries);
 
 #define amdgpu_bo_list_for_each_entry(e, list) \
 	for (e = list->entries; \
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +10 -14
index 203223fd0b54..a4cdaebaefe5 100644
@@ -152,24 +152,19 @@ static int amdgpu_cs_p1_bo_handles(struct amdgpu_cs_parser *p,
 				   struct drm_amdgpu_bo_list_in *data)
 {
 	struct drm_amdgpu_bo_list_entry *info;
-	int r;
+	struct amdgpu_bo_list *list;
 
-	r = amdgpu_bo_create_list_entry_array(data, &info);
-	if (r)
-		return r;
-
-	r = amdgpu_bo_list_create(p->adev, p->filp, info, data->bo_number,
-				  &p->bo_list);
-	if (r)
-		goto error_free;
+	info = amdgpu_bo_create_list_entry_array(data);
+	if (IS_ERR(info))
+		return PTR_ERR(info);
 
+	list = amdgpu_bo_list_create(p->adev, p->filp, info, data->bo_number);
 	kvfree(info);
+	if (IS_ERR(list))
+		return PTR_ERR(list);
+
+	p->bo_list = list;
 	return 0;
-
-error_free:
-	kvfree(info);
-
-	return r;
 }
 
 /* Copy the data from userspace and go over it the first time */
@@ -857,6 +852,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 {
 	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
 	struct ttm_operation_ctx ctx = { true, false };
+	struct amdgpu_bo_list *list = NULL;
 	struct amdgpu_vm *vm = &fpriv->vm;
 	struct amdgpu_bo_list_entry *e;
 	struct drm_gem_object *obj;

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, · line 870

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -4
@@ -869,25 +865,26 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 		if (p->bo_list)
 			return -EINVAL;
 
-		r = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle,
-				       &p->bo_list);
-		if (r)
-			return r;
+		list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
 	} else if (!p->bo_list) {
 		/* Create a empty bo_list when no handle is provided */

I think we can drop this handling.

This was just a broken fallback for the closed source driver which the OpenGL stack never used, need to double check with the RADV guys but I don't think they ever used this as well.

Looks good to me otherwise,
Christian.

Quoted 66 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +16 -13
-		r = amdgpu_bo_list_create(p->adev, p->filp, NULL, 0,
-					  &p->bo_list);
-		if (r)
-			return r;
+		list = amdgpu_bo_list_create(p->adev, p->filp, NULL, 0);
 	}
 
-	mutex_lock(&p->bo_list->bo_list_mutex);
+	if (IS_ERR(list))
+		return PTR_ERR(list);
+	else if (list)
+		p->bo_list = list;
+	else
+		list = p->bo_list;
+
+	mutex_lock(&list->bo_list_mutex);
 
 	/* Get userptr backing pages. If pages are updated after registered
 	 * in amdgpu_gem_userptr_ioctl(), amdgpu_cs_list_validate() will do
 	 * amdgpu_ttm_backend_bind() to flush and invalidate new pages
 	 */
-	amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
+	amdgpu_bo_list_for_each_userptr_entry(e, list) {
 		bool userpage_invalidated = false;
 		struct amdgpu_bo *bo = e->bo;
 
@@ -915,7 +912,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 		if (unlikely(r))
 			goto out_free_user_pages;
 
-		amdgpu_bo_list_for_each_entry(e, p->bo_list) {
+		amdgpu_bo_list_for_each_entry(e, list) {
 			/* One fence for TTM and one for each CS job */
 			r = drm_exec_prepare_obj(&p->exec, &e->bo->tbo.base,
 						 1 + p->gang_size);
@@ -935,7 +932,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 		}
 	}
 
-	amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
+	amdgpu_bo_list_for_each_userptr_entry(e, list) {
 		struct mm_struct *usermm;
 
 		usermm = amdgpu_ttm_tt_get_usermm(e->bo->tbo.ttm);
@@ -988,17 +985,16 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
 				     p->bytes_moved_vis);
 
 	for (i = 0; i < p->gang_size; ++i)
-		amdgpu_job_set_resources(p->jobs[i], p->bo_list->gds_obj,
-					 p->bo_list->gws_obj,
-					 p->bo_list->oa_obj);
+		amdgpu_job_set_resources(p->jobs[i], list->gds_obj,
+					 list->gws_obj, list->oa_obj);
 	return 0;
 
 out_free_user_pages:
-	amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
+	amdgpu_bo_list_for_each_userptr_entry(e, list) {
 		amdgpu_hmm_range_free(e->range);
 		e->range = NULL;
 	}
-	mutex_unlock(&p->bo_list->bo_list_mutex);
+	mutex_unlock(&list->bo_list_mutex);
 	return r;
 }
 
#7 lore raw

Re: [RFC 2/5] drm/amdgpu: Remove output parameter in bo list handling

Quoted 194 lines · Christian König
Quoted 2 lines · Tvrtko Ursulin

Removing the output parameter from a few functions should result in more readable code and also enables us to save some lines.

Oh, yes please.

That was just because somebody had a personal preference for this coding style and it resulted in at least one CVE entry for amdgpu a couple of years ago.

I should have pushed back on this harder when I've seen it initially.

Quoted 184 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 86 ++++++++++-----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h | 17 ++--
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c      | 58 +++++++-------
  3 files changed, 75 insertions(+), 86 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +38 -40
index 628d32fd2fae..0ab307317145 100644
@@ -67,9 +67,9 @@ static int amdgpu_bo_list_entry_cmp(const void *_a, const void *_b)
  	return 0;
  }
 
-int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
-			  struct drm_amdgpu_bo_list_entry *info,
-			  size_t num_entries, struct amdgpu_bo_list **result)
+struct amdgpu_bo_list *
+amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
+		      struct drm_amdgpu_bo_list_entry *info, size_t num_entries)
  {
  	unsigned last_entry = 0, first_userptr = num_entries;
  	struct amdgpu_bo_list_entry *array;
@@ -80,7 +80,7 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
 
  	list = kvzalloc(struct_size(list, entries, num_entries), GFP_KERNEL);
  	if (!list)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
  	kref_init(&list->refcount);
 
@@ -136,8 +136,7 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
  	trace_amdgpu_cs_bo_status(list->num_entries, total_size);
 
  	mutex_init(&list->bo_list_mutex);
-	*result = list;
-	return 0;
+	return list;
 
  error_free:
  	for (i = 0; i < last_entry; ++i)
@@ -145,24 +144,21 @@ int amdgpu_bo_list_create(struct amdgpu_device *adev, struct drm_file *filp,
  	for (i = first_userptr; i < num_entries; ++i)
  		amdgpu_bo_unref(&array[i].bo);
  	kvfree(list);
-	return r;
+	return ERR_PTR(r);
 
  }
 
-int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id,
-		       struct amdgpu_bo_list **result)
+struct amdgpu_bo_list *amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id)
  {
  	struct amdgpu_bo_list *list;
 
  	xa_lock(&fpriv->bo_list_handles);
  	list = xa_load(&fpriv->bo_list_handles, id);
-	if (list && !kref_get_unless_zero(&list->refcount))
-		list = NULL;
+	if (!list || !kref_get_unless_zero(&list->refcount))
+		list = ERR_PTR(-ENOENT);
  	xa_unlock(&fpriv->bo_list_handles);
 
-	*result = list;
-
-	return list ? 0 : -ENOENT;
+	return list;
  }
 
  void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
@@ -170,8 +166,8 @@ void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
  	kref_put(&list->refcount, amdgpu_bo_list_free);
  }
 
-int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
-				      struct drm_amdgpu_bo_list_entry **info_param)
+struct drm_amdgpu_bo_list_entry *
+amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in)
  {
  	const uint32_t info_size = sizeof(struct drm_amdgpu_bo_list_entry);
  	const void __user *uptr = u64_to_user_ptr(in->bo_info_ptr);
@@ -182,27 +178,24 @@ int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
  	/* copy the handle array from userspace to a kernel buffer */
  	if (likely(info_size == bo_info_size)) {
  		info = vmemdup_array_user(uptr, bo_number, info_size);
-		if (IS_ERR(info))
-			return PTR_ERR(info);
  	} else {
  		const uint32_t bytes = min(bo_info_size, info_size);
  		unsigned i;
 
  		info = kvmalloc_array(bo_number, info_size, GFP_KERNEL);
  		if (!info)
-			return -ENOMEM;
+			return ERR_PTR(-ENOMEM);
 
  		memset(info, 0, bo_number * info_size);
  		for (i = 0; i < bo_number; ++i, uptr += bo_info_size) {
  			if (copy_from_user(&info[i], uptr, bytes)) {
  				kvfree(info);
-				return -EFAULT;
+				return ERR_PTR(-EFAULT);
  			}
  		}
  	}
 
-	*info_param = info;
-	return 0;
+	return info;
  }
 
  int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
@@ -210,27 +203,24 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
  {
  	struct amdgpu_fpriv *fpriv = filp->driver_priv;
  	struct amdgpu_device *adev = drm_to_adev(dev);
-	struct drm_amdgpu_bo_list_entry *info = NULL;
  	struct amdgpu_bo_list *list, *prev, *curr;
-	uint32_t handle = args->in.list_handle;
  	union drm_amdgpu_bo_list *args = data;
+	uint32_t handle = args->in.list_handle;
+	struct drm_amdgpu_bo_list_entry *info;
  	int r;
 
-	r = amdgpu_bo_create_list_entry_array(&args->in, &info);
-	if (r)
-		return r;
-
  	switch (args->in.operation) {
  	case AMDGPU_BO_LIST_OP_CREATE:
-		r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
-					  &list);
-		if (r)
-			goto error_free;
+	case AMDGPU_BO_LIST_OP_UPDATE:
+		info = amdgpu_bo_create_list_entry_array(&args->in);
+		if (IS_ERR(info))
+			return PTR_ERR(info);
 
-		r = xa_alloc(&fpriv->bo_list_handles, &handle, list,
-			     xa_limit_32b, GFP_KERNEL);
-		if (r)
-			goto error_put_list;
+		list = amdgpu_bo_list_create(adev, filp, info,
+					     args->in.bo_number);
+		kvfree(info);
+		if (IS_ERR(list))
+			return PTR_ERR(list);
 
  		break;
 
@@ -242,12 +232,20 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
 
  		break;
 
-	case AMDGPU_BO_LIST_OP_UPDATE:
-		r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
-					  &list);
+	default:
+		return -EINVAL;
+	};
+
+	switch (args->in.operation) {
+	case AMDGPU_BO_LIST_OP_CREATE:
+		r = xa_alloc(&fpriv->bo_list_handles, &handle, list,
+			     xa_limit_32b, GFP_KERNEL);
  		if (r)
-			goto error_free;
+			goto error_put_list;
 
+		break;
+
+	case AMDGPU_BO_LIST_OP_UPDATE:
  		curr = xa_load(&fpriv->bo_list_handles, handle);
  		if (!curr) {
  			r = -ENOENT;
@@ -267,21 +265,17 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
  		amdgpu_bo_list_put(curr);
  		break;
 
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data, · line 270

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +2 -2
+	case AMDGPU_BO_LIST_OP_DESTROY:
  	default:
-		r = -EINVAL;
-		goto error_free;
+		/* Handled above. */

I think I prefer to keep that a single switch statement. This looks a bit mixed up to me.

I agree it looks odd.

What I wanted to is limit the allocations to only the operations which need it. Otherwise it seems to me it is pointless for AMDGPU_BO_LIST_OP_DESTROY to allocate something it does not touch.

Also the invalid args->in.operation check is currently after the first allocation.

A more elegant solution did not come to me yesterday.

Quoted 106 lines
Quoted 93 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +0 -4
  	}
 
  	memset(args, 0, sizeof(*args));
  	args->out.list_handle = handle;
-	kvfree(info);
 
  	return 0;
 
  error_put_list:
  	amdgpu_bo_list_put(list);
-
-error_free:
-	kvfree(info);
  	return r;
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h +8 -9
index 0989f1090c63..085ca94f97a2 100644
@@ -58,17 +58,16 @@ struct amdgpu_bo_list {
  	struct amdgpu_bo_list_entry entries[] __counted_by(num_entries);
  };
 
-int amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id,
-		       struct amdgpu_bo_list **result);
+struct amdgpu_bo_list *amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, u32 id);
  void amdgpu_bo_list_put(struct amdgpu_bo_list *list);
-int amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in,
-				      struct drm_amdgpu_bo_list_entry **info_param);
+struct drm_amdgpu_bo_list_entry *
+amdgpu_bo_create_list_entry_array(struct drm_amdgpu_bo_list_in *in);
 
-int amdgpu_bo_list_create(struct amdgpu_device *adev,
-				 struct drm_file *filp,
-				 struct drm_amdgpu_bo_list_entry *info,
-				 size_t num_entries,
-				 struct amdgpu_bo_list **list);
+struct amdgpu_bo_list *
+amdgpu_bo_list_create(struct amdgpu_device *adev,
+		      struct drm_file *filp,
+		      struct drm_amdgpu_bo_list_entry *info,
+		      size_t num_entries);
 
  #define amdgpu_bo_list_for_each_entry(e, list) \
  	for (e = list->entries; \
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +10 -14
index 203223fd0b54..a4cdaebaefe5 100644
@@ -152,24 +152,19 @@ static int amdgpu_cs_p1_bo_handles(struct amdgpu_cs_parser *p,
  				   struct drm_amdgpu_bo_list_in *data)
  {
  	struct drm_amdgpu_bo_list_entry *info;
-	int r;
+	struct amdgpu_bo_list *list;
 
-	r = amdgpu_bo_create_list_entry_array(data, &info);
-	if (r)
-		return r;
-
-	r = amdgpu_bo_list_create(p->adev, p->filp, info, data->bo_number,
-				  &p->bo_list);
-	if (r)
-		goto error_free;
+	info = amdgpu_bo_create_list_entry_array(data);
+	if (IS_ERR(info))
+		return PTR_ERR(info);
 
+	list = amdgpu_bo_list_create(p->adev, p->filp, info, data->bo_number);
  	kvfree(info);
+	if (IS_ERR(list))
+		return PTR_ERR(list);
+
+	p->bo_list = list;
  	return 0;
-
-error_free:
-	kvfree(info);
-
-	return r;
  }
 
  /* Copy the data from userspace and go over it the first time */
@@ -857,6 +852,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  {
  	struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
  	struct ttm_operation_ctx ctx = { true, false };
+	struct amdgpu_bo_list *list = NULL;
  	struct amdgpu_vm *vm = &fpriv->vm;
  	struct amdgpu_bo_list_entry *e;
  	struct drm_gem_object *obj;

drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, · line 870

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +1 -4
@@ -869,25 +865,26 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  		if (p->bo_list)
  			return -EINVAL;
 
-		r = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle,
-				       &p->bo_list);
-		if (r)
-			return r;
+		list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle);
  	} else if (!p->bo_list) {
  		/* Create a empty bo_list when no handle is provided */

I think we can drop this handling.

Quoted 1 line

This was just a broken fallback for the closed source driver which the OpenGL stack never used, need to double check with the RADV guys but I don't think they ever used this as well.

The whole else if block? Okay, I will put that as a separate patch in this case.

Regards,

Tvrtko

Quoted 71 lines

Looks good to me otherwise,
Christian.

Quoted 66 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +16 -13
-		r = amdgpu_bo_list_create(p->adev, p->filp, NULL, 0,
-					  &p->bo_list);
-		if (r)
-			return r;
+		list = amdgpu_bo_list_create(p->adev, p->filp, NULL, 0);
  	}
 
-	mutex_lock(&p->bo_list->bo_list_mutex);
+	if (IS_ERR(list))
+		return PTR_ERR(list);
+	else if (list)
+		p->bo_list = list;
+	else
+		list = p->bo_list;
+
+	mutex_lock(&list->bo_list_mutex);
 
  	/* Get userptr backing pages. If pages are updated after registered
  	 * in amdgpu_gem_userptr_ioctl(), amdgpu_cs_list_validate() will do
  	 * amdgpu_ttm_backend_bind() to flush and invalidate new pages
  	 */
-	amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
+	amdgpu_bo_list_for_each_userptr_entry(e, list) {
  		bool userpage_invalidated = false;
  		struct amdgpu_bo *bo = e->bo;
 
@@ -915,7 +912,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  		if (unlikely(r))
  			goto out_free_user_pages;
 
-		amdgpu_bo_list_for_each_entry(e, p->bo_list) {
+		amdgpu_bo_list_for_each_entry(e, list) {
  			/* One fence for TTM and one for each CS job */
  			r = drm_exec_prepare_obj(&p->exec, &e->bo->tbo.base,
  						 1 + p->gang_size);
@@ -935,7 +932,7 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  		}
  	}
 
-	amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
+	amdgpu_bo_list_for_each_userptr_entry(e, list) {
  		struct mm_struct *usermm;
 
  		usermm = amdgpu_ttm_tt_get_usermm(e->bo->tbo.ttm);
@@ -988,17 +985,16 @@ static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
  				     p->bytes_moved_vis);
 
  	for (i = 0; i < p->gang_size; ++i)
-		amdgpu_job_set_resources(p->jobs[i], p->bo_list->gds_obj,
-					 p->bo_list->gws_obj,
-					 p->bo_list->oa_obj);
+		amdgpu_job_set_resources(p->jobs[i], list->gds_obj,
+					 list->gws_obj, list->oa_obj);
  	return 0;
 
  out_free_user_pages:
-	amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
+	amdgpu_bo_list_for_each_userptr_entry(e, list) {
  		amdgpu_hmm_range_free(e->range);
  		e->range = NULL;
  	}
-	mutex_unlock(&p->bo_list->bo_list_mutex);
+	mutex_unlock(&list->bo_list_mutex);
  	return r;
  }
 
#8 lore raw

RFC 3/5 drm/amdgpu: Simplify sorting of the bo list

In reply to Tvrtko Ursulin

Sort function only cares about the sign so we can replace the conditionals with a single subtraction.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +3 -5
index 0ab307317145..16b647b42b0a 100644
@@ -60,11 +60,9 @@ static int amdgpu_bo_list_entry_cmp(const void *_a, const void *_b)
 {
 	const struct amdgpu_bo_list_entry *a = _a, *b = _b;
 
-	if (a->priority > b->priority)
-		return 1;
-	if (a->priority < b->priority)
-		return -1;
-	return 0;
+	BUILD_BUG_ON(AMDGPU_BO_LIST_MAX_PRIORITY >= INT_MAX);
+
+	return (int)a->priority - (int)b->priority;
 }
 
 struct amdgpu_bo_list *
Signature · 1 line
2.51.1
maintainer Reviewed-by on v1 3/5
#9 lore raw

Re: [RFC 3/5] drm/amdgpu: Simplify sorting of the bo list

Quoted 4 lines · Tvrtko Ursulin

Sort function only cares about the sign so we can replace the conditionals with a single subtraction.

Quoted 23 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +3 -5
index 0ab307317145..16b647b42b0a 100644
@@ -60,11 +60,9 @@ static int amdgpu_bo_list_entry_cmp(const void *_a, const void *_b)
 {
 	const struct amdgpu_bo_list_entry *a = _a, *b = _b;
 
-	if (a->priority > b->priority)
-		return 1;
-	if (a->priority < b->priority)
-		return -1;
-	return 0;
+	BUILD_BUG_ON(AMDGPU_BO_LIST_MAX_PRIORITY >= INT_MAX);
+
+	return (int)a->priority - (int)b->priority;
 }
 
 struct amdgpu_bo_list *
#10 lore raw

RFC 4/5 drm/amdgpu: Convert context manager to xarray

In reply to Tvrtko Ursulin

IDR is deprecated so let's convert the context manager to xarray.

In doing so we remove the context manager mutex and switch call sites which required the guarantee context cannot go away while they walk the list of context, or otherwise operate on them, to use reference counting.

This allows us to use the built in xarray spinlock for all operations and just temporarily drop it when we need to call sleeping functions.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c   | 117 ++++++++--------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h   |   5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c |   8 +-
 3 files changed, 49 insertions(+), 81 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +43 -74
index afedea02188d..ee1464f8751a 100644
@@ -484,22 +484,17 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
 	if (!ctx)
 		return -ENOMEM;
 
-	mutex_lock(&mgr->lock);
-	r = idr_alloc(&mgr->ctx_handles, ctx, 1, AMDGPU_VM_MAX_NUM_CTX, GFP_KERNEL);
-	if (r < 0) {
-		mutex_unlock(&mgr->lock);
-		kfree(ctx);
-		return r;
-	}
-
-	*id = (uint32_t)r;
 	r = amdgpu_ctx_init(mgr, priority, filp, ctx);
 	if (r) {
-		idr_remove(&mgr->ctx_handles, *id);
-		*id = 0;
 		kfree(ctx);
+		return r;
 	}
-	mutex_unlock(&mgr->lock);
+
+	r = xa_alloc(&mgr->ctx_handles, id, ctx,
+		     XA_LIMIT(1, AMDGPU_VM_MAX_NUM_CTX - 1), GFP_KERNEL);
+	if (r)
+		kref_put(&ctx->refcount, amdgpu_ctx_fini);
+
 	return r;
 }
 
@@ -523,14 +518,11 @@ static void amdgpu_ctx_do_release(struct kref *ref)
 
 static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
 {
-	struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
 	struct amdgpu_ctx *ctx;
 
-	mutex_lock(&mgr->lock);
-	ctx = idr_remove(&mgr->ctx_handles, id);
-	if (ctx)
-		kref_put(&ctx->refcount, amdgpu_ctx_do_release);
-	mutex_unlock(&mgr->lock);
+	ctx = xa_erase(&fpriv->ctx_mgr.ctx_handles, id);
+	amdgpu_ctx_put(ctx);
+
 	return ctx ? 0 : -EINVAL;
 }
 
@@ -539,20 +531,12 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev,
 			    union drm_amdgpu_ctx_out *out)
 {
 	struct amdgpu_ctx *ctx;
-	struct amdgpu_ctx_mgr *mgr;
 	unsigned reset_counter;
 
-	if (!fpriv)
+	ctx = amdgpu_ctx_get(fpriv, id);
+	if (!ctx)
 		return -EINVAL;
 
-	mgr = &fpriv->ctx_mgr;
-	mutex_lock(&mgr->lock);
-	ctx = idr_find(&mgr->ctx_handles, id);
-	if (!ctx) {
-		mutex_unlock(&mgr->lock);
-		return -EINVAL;
-	}
-
 	/* TODO: these two are always zero */
 	out->state.flags = 0x0;
 	out->state.hangs = 0x0;
@@ -566,7 +550,8 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev,
 		out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
 	ctx->reset_counter_query = reset_counter;
 
-	mutex_unlock(&mgr->lock);
+	amdgpu_ctx_put(ctx);
+
 	return 0;
 }
 
@@ -578,19 +563,11 @@ static int amdgpu_ctx_query2(struct amdgpu_device *adev,
 {
 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
 	struct amdgpu_ctx *ctx;
-	struct amdgpu_ctx_mgr *mgr;
 
-	if (!fpriv)
+	ctx = amdgpu_ctx_get(fpriv, id);
+	if (!ctx)
 		return -EINVAL;
 
-	mgr = &fpriv->ctx_mgr;
-	mutex_lock(&mgr->lock);
-	ctx = idr_find(&mgr->ctx_handles, id);
-	if (!ctx) {
-		mutex_unlock(&mgr->lock);
-		return -EINVAL;
-	}
-
 	out->state.flags = 0x0;
 	out->state.hangs = 0x0;
 
@@ -630,7 +607,8 @@ static int amdgpu_ctx_query2(struct amdgpu_device *adev,
 				      msecs_to_jiffies(AMDGPU_RAS_COUNTE_DELAY_MS));
 	}
 
-	mutex_unlock(&mgr->lock);
+	amdgpu_ctx_put(ctx);
+
 	return 0;
 }
 
@@ -639,26 +617,18 @@ static int amdgpu_ctx_stable_pstate(struct amdgpu_device *adev,
 				    bool set, u32 *stable_pstate)
 {
 	struct amdgpu_ctx *ctx;
-	struct amdgpu_ctx_mgr *mgr;
 	int r;
 
-	if (!fpriv)
+	ctx = amdgpu_ctx_get(fpriv, id);
+	if (!ctx)
 		return -EINVAL;
 
-	mgr = &fpriv->ctx_mgr;
-	mutex_lock(&mgr->lock);
-	ctx = idr_find(&mgr->ctx_handles, id);
-	if (!ctx) {
-		mutex_unlock(&mgr->lock);
-		return -EINVAL;
-	}
-
 	if (set)
 		r = amdgpu_ctx_set_stable_pstate(ctx, *stable_pstate);
 	else
 		r = amdgpu_ctx_get_stable_pstate(ctx, stable_pstate);
 
-	mutex_unlock(&mgr->lock);
+	amdgpu_ctx_put(ctx);
 	return r;
 }
 
@@ -737,11 +707,11 @@ struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
 
 	mgr = &fpriv->ctx_mgr;
 
-	mutex_lock(&mgr->lock);
-	ctx = idr_find(&mgr->ctx_handles, id);
+	xa_lock(&mgr->ctx_handles);
+	ctx = xa_load(&mgr->ctx_handles, id);
 	if (ctx)
 		kref_get(&ctx->refcount);
-	mutex_unlock(&mgr->lock);
+	xa_unlock(&mgr->ctx_handles);
 	return ctx;
 }
 
@@ -886,8 +856,7 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,
 	unsigned int i;
 
 	mgr->adev = adev;
-	mutex_init(&mgr->lock);
-	idr_init_base(&mgr->ctx_handles, 1);
+	xa_init_flags(&mgr->ctx_handles, XA_FLAGS_ALLOC1);
 
 	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i)
 		atomic64_set(&mgr->time_spend[i], 0);
@@ -896,13 +865,14 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,
 long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
 {
 	struct amdgpu_ctx *ctx;
-	struct idr *idp;
-	uint32_t id, i, j;
+	unsigned long id;
+	uint32_t i, j;
 
-	idp = &mgr->ctx_handles;
-
-	mutex_lock(&mgr->lock);
-	idr_for_each_entry(idp, ctx, id) {
+	xa_lock(&mgr->ctx_handles);
+	xa_for_each(&mgr->ctx_handles, id, ctx) {
+		if (!kref_get_unless_zero(&ctx->refcount))
+			continue;
+		xa_unlock(&mgr->ctx_handles);
 		for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
 			for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
 				struct drm_sched_entity *entity;
@@ -914,20 +884,20 @@ long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
 				timeout = drm_sched_entity_flush(entity, timeout);
 			}
 		}
+		amdgpu_ctx_put(ctx);
+		xa_lock(&mgr->ctx_handles);
 	}
-	mutex_unlock(&mgr->lock);
+	xa_unlock(&mgr->ctx_handles);
 	return timeout;
 }
 
 static void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
 {
 	struct amdgpu_ctx *ctx;
-	struct idr *idp;
-	uint32_t id, i, j;
+	unsigned long id;
+	uint32_t i, j;
 
-	idp = &mgr->ctx_handles;
-
-	idr_for_each_entry(idp, ctx, id) {
+	xa_for_each(&mgr->ctx_handles, id, ctx) {
 		if (kref_read(&ctx->refcount) != 1) {
 			DRM_ERROR("ctx %p is still alive\n", ctx);
 			continue;
@@ -951,8 +921,7 @@ static void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
 {
 	amdgpu_ctx_mgr_entity_fini(mgr);
-	idr_destroy(&mgr->ctx_handles);
-	mutex_destroy(&mgr->lock);
+	xa_destroy(&mgr->ctx_handles);
 }
 
 void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
@@ -960,21 +929,21 @@ void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
 {
 	struct amdgpu_ctx *ctx;
 	unsigned int hw_ip, i;
-	uint32_t id;
+	unsigned long id;
 
 	/*
 	 * This is a little bit racy because it can be that a ctx or a fence are
 	 * destroyed just in the moment we try to account them. But that is ok
 	 * since exactly that case is explicitely allowed by the interface.
 	 */
-	mutex_lock(&mgr->lock);
 	for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
 		uint64_t ns = atomic64_read(&mgr->time_spend[hw_ip]);
 
 		usage[hw_ip] = ns_to_ktime(ns);
 	}
 
-	idr_for_each_entry(&mgr->ctx_handles, ctx, id) {
+	xa_lock(&mgr->ctx_handles);
+	xa_for_each(&mgr->ctx_handles, id, ctx) {
 		for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
 			for (i = 0; i < amdgpu_ctx_num_entities[hw_ip]; ++i) {
 				struct amdgpu_ctx_entity *centity;
@@ -988,5 +957,5 @@ void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
 			}
 		}
 	}
-	mutex_unlock(&mgr->lock);
+	xa_unlock(&mgr->ctx_handles);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +2 -3
index 090dfe86f75b..d073cffa455d 100644
@@ -25,6 +25,7 @@
 
 #include <linux/ktime.h>
 #include <linux/types.h>
+#include <linux/xarray.h>
 
 #include "amdgpu_ring.h"
 
@@ -62,9 +63,7 @@ struct amdgpu_ctx {
 
 struct amdgpu_ctx_mgr {
 	struct amdgpu_device	*adev;
-	struct mutex		lock;
-	/* protected by lock */
-	struct idr		ctx_handles;
+	struct xarray		ctx_handles;
 	atomic64_t		time_spend[AMDGPU_HW_IP_NUM];
 };
 
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c +4 -4
index 341beec59537..471d27b2db01 100644
@@ -39,7 +39,7 @@ static int amdgpu_sched_process_priority_override(struct amdgpu_device *adev,
 	struct amdgpu_fpriv *fpriv;
 	struct amdgpu_ctx_mgr *mgr;
 	struct amdgpu_ctx *ctx;
-	uint32_t id;
+	unsigned long id;
 	int r;
 
 	if (fd_empty(f))
@@ -50,10 +50,10 @@ static int amdgpu_sched_process_priority_override(struct amdgpu_device *adev,
 		return r;
 
 	mgr = &fpriv->ctx_mgr;
-	mutex_lock(&mgr->lock);
-	idr_for_each_entry(&mgr->ctx_handles, ctx, id)
+	xa_lock(&mgr->ctx_handles);
+	xa_for_each(&mgr->ctx_handles, id, ctx)
 		amdgpu_ctx_priority_override(ctx, priority);
-	mutex_unlock(&mgr->lock);
+	xa_unlock(&mgr->ctx_handles);
 
 	return 0;
 }
Signature · 1 line
2.51.1
maintainer
#11 lore raw

Re: [RFC 4/5] drm/amdgpu: Convert context manager to xarray

Quoted 34 lines · Tvrtko Ursulin

IDR is deprecated so let's convert the context manager to xarray.

In doing so we remove the context manager mutex and switch call sites which required the guarantee context cannot go away while they walk the list of context, or otherwise operate on them, to use reference counting.

This allows us to use the built in xarray spinlock for all operations and just temporarily drop it when we need to call sleeping functions.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c   | 117 ++++++++--------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h   |   5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c |   8 +-
 3 files changed, 49 insertions(+), 81 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -9
index afedea02188d..ee1464f8751a 100644
@@ -484,22 +484,17 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
 	if (!ctx)
 		return -ENOMEM;
 
-	mutex_lock(&mgr->lock);
-	r = idr_alloc(&mgr->ctx_handles, ctx, 1, AMDGPU_VM_MAX_NUM_CTX, GFP_KERNEL);
-	if (r < 0) {
-		mutex_unlock(&mgr->lock);
-		kfree(ctx);
-		return r;
-	}
-
-	*id = (uint32_t)r;
 	r = amdgpu_ctx_init(mgr, priority, filp, ctx);

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_alloc(struct amdgpu_device *adev, · line 494

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +4 -3
 	if (r) {
-		idr_remove(&mgr->ctx_handles, *id);
-		*id = 0;
 		kfree(ctx);
+		return r;
 	}
-	mutex_unlock(&mgr->lock);
+
+	r = xa_alloc(&mgr->ctx_handles, id, ctx,
+		     XA_LIMIT(1, AMDGPU_VM_MAX_NUM_CTX - 1), GFP_KERNEL);

Please drop the AMDGPU_VM_MAX_NUM_CTX define as well. That is just a totally pointless limitation.

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_alloc(struct amdgpu_device *adev, · line 497

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3 -0
+	if (r)
+		kref_put(&ctx->refcount, amdgpu_ctx_fini);
+

While at it you should probably clean up the unecessary differenciation between amdgpu_ctx_fini and amdgpu_ctx__do_release as well.

Quoted 147 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +18 -46
 	return r;
 }
 
@@ -523,14 +518,11 @@ static void amdgpu_ctx_do_release(struct kref *ref)
 
 static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
 {
-	struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
 	struct amdgpu_ctx *ctx;
 
-	mutex_lock(&mgr->lock);
-	ctx = idr_remove(&mgr->ctx_handles, id);
-	if (ctx)
-		kref_put(&ctx->refcount, amdgpu_ctx_do_release);
-	mutex_unlock(&mgr->lock);
+	ctx = xa_erase(&fpriv->ctx_mgr.ctx_handles, id);
+	amdgpu_ctx_put(ctx);
+
 	return ctx ? 0 : -EINVAL;
 }
 
@@ -539,20 +531,12 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev,
 			    union drm_amdgpu_ctx_out *out)
 {
 	struct amdgpu_ctx *ctx;
-	struct amdgpu_ctx_mgr *mgr;
 	unsigned reset_counter;
 
-	if (!fpriv)
+	ctx = amdgpu_ctx_get(fpriv, id);
+	if (!ctx)
 		return -EINVAL;
 
-	mgr = &fpriv->ctx_mgr;
-	mutex_lock(&mgr->lock);
-	ctx = idr_find(&mgr->ctx_handles, id);
-	if (!ctx) {
-		mutex_unlock(&mgr->lock);
-		return -EINVAL;
-	}
-
 	/* TODO: these two are always zero */
 	out->state.flags = 0x0;
 	out->state.hangs = 0x0;
@@ -566,7 +550,8 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev,
 		out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
 	ctx->reset_counter_query = reset_counter;
 
-	mutex_unlock(&mgr->lock);
+	amdgpu_ctx_put(ctx);
+
 	return 0;
 }
 
@@ -578,19 +563,11 @@ static int amdgpu_ctx_query2(struct amdgpu_device *adev,
 {
 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
 	struct amdgpu_ctx *ctx;
-	struct amdgpu_ctx_mgr *mgr;
 
-	if (!fpriv)
+	ctx = amdgpu_ctx_get(fpriv, id);
+	if (!ctx)
 		return -EINVAL;
 
-	mgr = &fpriv->ctx_mgr;
-	mutex_lock(&mgr->lock);
-	ctx = idr_find(&mgr->ctx_handles, id);
-	if (!ctx) {
-		mutex_unlock(&mgr->lock);
-		return -EINVAL;
-	}
-
 	out->state.flags = 0x0;
 	out->state.hangs = 0x0;
 
@@ -630,7 +607,8 @@ static int amdgpu_ctx_query2(struct amdgpu_device *adev,
 				      msecs_to_jiffies(AMDGPU_RAS_COUNTE_DELAY_MS));
 	}
 
-	mutex_unlock(&mgr->lock);
+	amdgpu_ctx_put(ctx);
+
 	return 0;
 }
 
@@ -639,26 +617,18 @@ static int amdgpu_ctx_stable_pstate(struct amdgpu_device *adev,
 				    bool set, u32 *stable_pstate)
 {
 	struct amdgpu_ctx *ctx;
-	struct amdgpu_ctx_mgr *mgr;
 	int r;
 
-	if (!fpriv)
+	ctx = amdgpu_ctx_get(fpriv, id);
+	if (!ctx)
 		return -EINVAL;
 
-	mgr = &fpriv->ctx_mgr;
-	mutex_lock(&mgr->lock);
-	ctx = idr_find(&mgr->ctx_handles, id);
-	if (!ctx) {
-		mutex_unlock(&mgr->lock);
-		return -EINVAL;
-	}
-
 	if (set)
 		r = amdgpu_ctx_set_stable_pstate(ctx, *stable_pstate);
 	else
 		r = amdgpu_ctx_get_stable_pstate(ctx, stable_pstate);
 
-	mutex_unlock(&mgr->lock);
+	amdgpu_ctx_put(ctx);
 	return r;
 }
 
@@ -737,11 +707,11 @@ struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
 
 	mgr = &fpriv->ctx_mgr;
 
-	mutex_lock(&mgr->lock);
-	ctx = idr_find(&mgr->ctx_handles, id);
+	xa_lock(&mgr->ctx_handles);
+	ctx = xa_load(&mgr->ctx_handles, id);
 	if (ctx)
 		kref_get(&ctx->refcount);
-	mutex_unlock(&mgr->lock);
+	xa_unlock(&mgr->ctx_handles);
 	return ctx;
 }
 
@@ -886,8 +856,7 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,
 	unsigned int i;
 
 	mgr->adev = adev;
-	mutex_init(&mgr->lock);
-	idr_init_base(&mgr->ctx_handles, 1);
+	xa_init_flags(&mgr->ctx_handles, XA_FLAGS_ALLOC1);
 
 	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i)
 		atomic64_set(&mgr->time_spend[i], 0);
@@ -896,13 +865,14 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,
 long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
 {
 	struct amdgpu_ctx *ctx;
-	struct idr *idp;
-	uint32_t id, i, j;

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr, · line 873

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +5 -4
+	unsigned long id;
+	uint32_t i, j;
 
-	idp = &mgr->ctx_handles;
-
-	mutex_lock(&mgr->lock);
-	idr_for_each_entry(idp, ctx, id) {
+	xa_lock(&mgr->ctx_handles);
+	xa_for_each(&mgr->ctx_handles, id, ctx) {
+		if (!kref_get_unless_zero(&ctx->refcount))

This should be pointless, why not use kfre_get instead?

Quoted 32 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +8 -6
+			continue;
+		xa_unlock(&mgr->ctx_handles);
 		for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
 			for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
 				struct drm_sched_entity *entity;
@@ -914,20 +884,20 @@ long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
 				timeout = drm_sched_entity_flush(entity, timeout);
 			}
 		}
+		amdgpu_ctx_put(ctx);
+		xa_lock(&mgr->ctx_handles);
 	}
-	mutex_unlock(&mgr->lock);
+	xa_unlock(&mgr->ctx_handles);
 	return timeout;
 }
 
 static void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
 {
 	struct amdgpu_ctx *ctx;
-	struct idr *idp;
-	uint32_t id, i, j;
+	unsigned long id;
+	uint32_t i, j;
 
-	idp = &mgr->ctx_handles;
-
-	idr_for_each_entry(idp, ctx, id) {
+	xa_for_each(&mgr->ctx_handles, id, ctx) {
 
 		if (kref_read(&ctx->refcount) != 1) {
 			DRM_ERROR("ctx %p is still alive\n", ctx);
Quoted 1 line

continue;

Please drop that check as well. It just leads to memory leaks and errors should anything go wrong and so only make things worse.

We can have something like WARN_ON_ONCE(kref_read(&ctx->refcount) != 1) here, but I think that is superflous as well.

Regards,
Christian.

Quoted 92 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +5 -6
@@ -951,8 +921,7 @@ static void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
 {
 	amdgpu_ctx_mgr_entity_fini(mgr);
-	idr_destroy(&mgr->ctx_handles);
-	mutex_destroy(&mgr->lock);
+	xa_destroy(&mgr->ctx_handles);
 }
 
 void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
@@ -960,21 +929,21 @@ void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
 {
 	struct amdgpu_ctx *ctx;
 	unsigned int hw_ip, i;
-	uint32_t id;
+	unsigned long id;
 
 	/*
 	 * This is a little bit racy because it can be that a ctx or a fence are
 	 * destroyed just in the moment we try to account them. But that is ok
 	 * since exactly that case is explicitely allowed by the interface.
 	 */
-	mutex_lock(&mgr->lock);
 	for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
 		uint64_t ns = atomic64_read(&mgr->time_spend[hw_ip]);
 
 		usage[hw_ip] = ns_to_ktime(ns);
 	}
 
-	idr_for_each_entry(&mgr->ctx_handles, ctx, id) {
+	xa_lock(&mgr->ctx_handles);
+	xa_for_each(&mgr->ctx_handles, id, ctx) {
 		for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
 			for (i = 0; i < amdgpu_ctx_num_entities[hw_ip]; ++i) {
 				struct amdgpu_ctx_entity *centity;
@@ -988,5 +957,5 @@ void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
 			}
 		}
 	}
-	mutex_unlock(&mgr->lock);
+	xa_unlock(&mgr->ctx_handles);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +2 -3
index 090dfe86f75b..d073cffa455d 100644
@@ -25,6 +25,7 @@
 
 #include <linux/ktime.h>
 #include <linux/types.h>
+#include <linux/xarray.h>
 
 #include "amdgpu_ring.h"
 
@@ -62,9 +63,7 @@ struct amdgpu_ctx {
 
 struct amdgpu_ctx_mgr {
 	struct amdgpu_device	*adev;
-	struct mutex		lock;
-	/* protected by lock */
-	struct idr		ctx_handles;
+	struct xarray		ctx_handles;
 	atomic64_t		time_spend[AMDGPU_HW_IP_NUM];
 };
 
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c +4 -4
index 341beec59537..471d27b2db01 100644
@@ -39,7 +39,7 @@ static int amdgpu_sched_process_priority_override(struct amdgpu_device *adev,
 	struct amdgpu_fpriv *fpriv;
 	struct amdgpu_ctx_mgr *mgr;
 	struct amdgpu_ctx *ctx;
-	uint32_t id;
+	unsigned long id;
 	int r;
 
 	if (fd_empty(f))
@@ -50,10 +50,10 @@ static int amdgpu_sched_process_priority_override(struct amdgpu_device *adev,
 		return r;
 
 	mgr = &fpriv->ctx_mgr;
-	mutex_lock(&mgr->lock);
-	idr_for_each_entry(&mgr->ctx_handles, ctx, id)
+	xa_lock(&mgr->ctx_handles);
+	xa_for_each(&mgr->ctx_handles, id, ctx)
 		amdgpu_ctx_priority_override(ctx, priority);
-	mutex_unlock(&mgr->lock);
+	xa_unlock(&mgr->ctx_handles);
 
 	return 0;
 }
#12 lore raw

Re: [RFC 4/5] drm/amdgpu: Convert context manager to xarray

Quoted 39 lines · Christian König
Quoted 38 lines · Tvrtko Ursulin

IDR is deprecated so let's convert the context manager to xarray.

In doing so we remove the context manager mutex and switch call sites which required the guarantee context cannot go away while they walk the list of context, or otherwise operate on them, to use reference counting.

This allows us to use the built in xarray spinlock for all operations and just temporarily drop it when we need to call sleeping functions.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c   | 117 ++++++++--------------
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h   |   5 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c |   8 +-
  3 files changed, 49 insertions(+), 81 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -11
index afedea02188d..ee1464f8751a 100644
@@ -484,22 +484,17 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
  	if (!ctx)
  		return -ENOMEM;
 
-	mutex_lock(&mgr->lock);
-	r = idr_alloc(&mgr->ctx_handles, ctx, 1, AMDGPU_VM_MAX_NUM_CTX, GFP_KERNEL);
-	if (r < 0) {
-		mutex_unlock(&mgr->lock);
-		kfree(ctx);
-		return r;
-	}
-
-	*id = (uint32_t)r;
  	r = amdgpu_ctx_init(mgr, priority, filp, ctx);
  	if (r) {
-		idr_remove(&mgr->ctx_handles, *id);
-		*id = 0;
  		kfree(ctx);
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_alloc(struct amdgpu_device *adev, · line 494

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +4 -1
+		return r;
  	}
-	mutex_unlock(&mgr->lock);
+
+	r = xa_alloc(&mgr->ctx_handles, id, ctx,
+		     XA_LIMIT(1, AMDGPU_VM_MAX_NUM_CTX - 1), GFP_KERNEL);

Please drop the AMDGPU_VM_MAX_NUM_CTX define as well. That is just a totally pointless limitation.

So xa_limit_32b ?

Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_alloc(struct amdgpu_device *adev, · line 497

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3 -0
+	if (r)
+		kref_put(&ctx->refcount, amdgpu_ctx_fini);
+

While at it you should probably clean up the unecessary differenciation between amdgpu_ctx_fini and amdgpu_ctx__do_release as well.

Right, those are very confusing indeed, so I happily will give that a go.

Quoted 151 lines
Quoted 151 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +20 -46
  	return r;
  }
 
@@ -523,14 +518,11 @@ static void amdgpu_ctx_do_release(struct kref *ref)
 
  static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
  {
-	struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  	struct amdgpu_ctx *ctx;
 
-	mutex_lock(&mgr->lock);
-	ctx = idr_remove(&mgr->ctx_handles, id);
-	if (ctx)
-		kref_put(&ctx->refcount, amdgpu_ctx_do_release);
-	mutex_unlock(&mgr->lock);
+	ctx = xa_erase(&fpriv->ctx_mgr.ctx_handles, id);
+	amdgpu_ctx_put(ctx);
+
  	return ctx ? 0 : -EINVAL;
  }
 
@@ -539,20 +531,12 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev,
  			    union drm_amdgpu_ctx_out *out)
  {
  	struct amdgpu_ctx *ctx;
-	struct amdgpu_ctx_mgr *mgr;
  	unsigned reset_counter;
 
-	if (!fpriv)
+	ctx = amdgpu_ctx_get(fpriv, id);
+	if (!ctx)
  		return -EINVAL;
 
-	mgr = &fpriv->ctx_mgr;
-	mutex_lock(&mgr->lock);
-	ctx = idr_find(&mgr->ctx_handles, id);
-	if (!ctx) {
-		mutex_unlock(&mgr->lock);
-		return -EINVAL;
-	}
-
  	/* TODO: these two are always zero */
  	out->state.flags = 0x0;
  	out->state.hangs = 0x0;
@@ -566,7 +550,8 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev,
  		out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
  	ctx->reset_counter_query = reset_counter;
 
-	mutex_unlock(&mgr->lock);
+	amdgpu_ctx_put(ctx);
+
  	return 0;
  }
 
@@ -578,19 +563,11 @@ static int amdgpu_ctx_query2(struct amdgpu_device *adev,
  {
  	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
  	struct amdgpu_ctx *ctx;
-	struct amdgpu_ctx_mgr *mgr;
 
-	if (!fpriv)
+	ctx = amdgpu_ctx_get(fpriv, id);
+	if (!ctx)
  		return -EINVAL;
 
-	mgr = &fpriv->ctx_mgr;
-	mutex_lock(&mgr->lock);
-	ctx = idr_find(&mgr->ctx_handles, id);
-	if (!ctx) {
-		mutex_unlock(&mgr->lock);
-		return -EINVAL;
-	}
-
  	out->state.flags = 0x0;
  	out->state.hangs = 0x0;
 
@@ -630,7 +607,8 @@ static int amdgpu_ctx_query2(struct amdgpu_device *adev,
  				      msecs_to_jiffies(AMDGPU_RAS_COUNTE_DELAY_MS));
  	}
 
-	mutex_unlock(&mgr->lock);
+	amdgpu_ctx_put(ctx);
+
  	return 0;
  }
 
@@ -639,26 +617,18 @@ static int amdgpu_ctx_stable_pstate(struct amdgpu_device *adev,
  				    bool set, u32 *stable_pstate)
  {
  	struct amdgpu_ctx *ctx;
-	struct amdgpu_ctx_mgr *mgr;
  	int r;
 
-	if (!fpriv)
+	ctx = amdgpu_ctx_get(fpriv, id);
+	if (!ctx)
  		return -EINVAL;
 
-	mgr = &fpriv->ctx_mgr;
-	mutex_lock(&mgr->lock);
-	ctx = idr_find(&mgr->ctx_handles, id);
-	if (!ctx) {
-		mutex_unlock(&mgr->lock);
-		return -EINVAL;
-	}
-
  	if (set)
  		r = amdgpu_ctx_set_stable_pstate(ctx, *stable_pstate);
  	else
  		r = amdgpu_ctx_get_stable_pstate(ctx, stable_pstate);
 
-	mutex_unlock(&mgr->lock);
+	amdgpu_ctx_put(ctx);
  	return r;
  }
 
@@ -737,11 +707,11 @@ struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
 
  	mgr = &fpriv->ctx_mgr;
 
-	mutex_lock(&mgr->lock);
-	ctx = idr_find(&mgr->ctx_handles, id);
+	xa_lock(&mgr->ctx_handles);
+	ctx = xa_load(&mgr->ctx_handles, id);
  	if (ctx)
  		kref_get(&ctx->refcount);
-	mutex_unlock(&mgr->lock);
+	xa_unlock(&mgr->ctx_handles);
  	return ctx;
  }
 
@@ -886,8 +856,7 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,
  	unsigned int i;
 
  	mgr->adev = adev;
-	mutex_init(&mgr->lock);
-	idr_init_base(&mgr->ctx_handles, 1);
+	xa_init_flags(&mgr->ctx_handles, XA_FLAGS_ALLOC1);
 
  	for (i = 0; i < AMDGPU_HW_IP_NUM; ++i)
  		atomic64_set(&mgr->time_spend[i], 0);
@@ -896,13 +865,14 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,
  long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
  {
  	struct amdgpu_ctx *ctx;
-	struct idr *idp;
-	uint32_t id, i, j;
+	unsigned long id;
+	uint32_t i, j;
 
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr, · line 873

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3 -4
-	idp = &mgr->ctx_handles;
-
-	mutex_lock(&mgr->lock);
-	idr_for_each_entry(idp, ctx, id) {
+	xa_lock(&mgr->ctx_handles);
+	xa_for_each(&mgr->ctx_handles, id, ctx) {
+		if (!kref_get_unless_zero(&ctx->refcount))

This should be pointless, why not use kfre_get instead?

True, don't know what I was thinking.

Quoted 31 lines
Quoted 30 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +8 -6
+			continue;
+		xa_unlock(&mgr->ctx_handles);
  		for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
  			for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
  				struct drm_sched_entity *entity;
@@ -914,20 +884,20 @@ long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
  				timeout = drm_sched_entity_flush(entity, timeout);
  			}
  		}
+		amdgpu_ctx_put(ctx);
+		xa_lock(&mgr->ctx_handles);
  	}
-	mutex_unlock(&mgr->lock);
+	xa_unlock(&mgr->ctx_handles);
  	return timeout;
  }
 
  static void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
  {
  	struct amdgpu_ctx *ctx;
-	struct idr *idp;
-	uint32_t id, i, j;
+	unsigned long id;
+	uint32_t i, j;
 
-	idp = &mgr->ctx_handles;
-
-	idr_for_each_entry(idp, ctx, id) {
+	xa_for_each(&mgr->ctx_handles, id, ctx) {
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout) · line 903

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
  		if (kref_read(&ctx->refcount) != 1) {
  			DRM_ERROR("ctx %p is still alive\n", ctx);
  			continue;

Please drop that check as well. It just leads to memory leaks and errors should anything go wrong and so only make things worse.

We can have something like WARN_ON_ONCE(kref_read(&ctx->refcount) != 1) here, but I think that is superflous as well.

Hm, you mean leaving the entity around while the rest of the file gets closed causes other problems ie. this is not enough to turn crashes into memory leaks?

Regards,

Tvrtko

Quoted 97 lines

Regards,
Christian.

Quoted 92 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +5 -6
@@ -951,8 +921,7 @@ static void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
  void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
  {
  	amdgpu_ctx_mgr_entity_fini(mgr);
-	idr_destroy(&mgr->ctx_handles);
-	mutex_destroy(&mgr->lock);
+	xa_destroy(&mgr->ctx_handles);
  }
 
  void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
@@ -960,21 +929,21 @@ void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
  {
  	struct amdgpu_ctx *ctx;
  	unsigned int hw_ip, i;
-	uint32_t id;
+	unsigned long id;
 
  	/*
  	 * This is a little bit racy because it can be that a ctx or a fence are
  	 * destroyed just in the moment we try to account them. But that is ok
  	 * since exactly that case is explicitely allowed by the interface.
  	 */
-	mutex_lock(&mgr->lock);
  	for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
  		uint64_t ns = atomic64_read(&mgr->time_spend[hw_ip]);
 
  		usage[hw_ip] = ns_to_ktime(ns);
  	}
 
-	idr_for_each_entry(&mgr->ctx_handles, ctx, id) {
+	xa_lock(&mgr->ctx_handles);
+	xa_for_each(&mgr->ctx_handles, id, ctx) {
  		for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
  			for (i = 0; i < amdgpu_ctx_num_entities[hw_ip]; ++i) {
  				struct amdgpu_ctx_entity *centity;
@@ -988,5 +957,5 @@ void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
  			}
  		}
  	}
-	mutex_unlock(&mgr->lock);
+	xa_unlock(&mgr->ctx_handles);
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +2 -3
index 090dfe86f75b..d073cffa455d 100644
@@ -25,6 +25,7 @@
 
  #include <linux/ktime.h>
  #include <linux/types.h>
+#include <linux/xarray.h>
 
  #include "amdgpu_ring.h"
 
@@ -62,9 +63,7 @@ struct amdgpu_ctx {
 
  struct amdgpu_ctx_mgr {
  	struct amdgpu_device	*adev;
-	struct mutex		lock;
-	/* protected by lock */
-	struct idr		ctx_handles;
+	struct xarray		ctx_handles;
  	atomic64_t		time_spend[AMDGPU_HW_IP_NUM];
  };
 
drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c +4 -4
index 341beec59537..471d27b2db01 100644
@@ -39,7 +39,7 @@ static int amdgpu_sched_process_priority_override(struct amdgpu_device *adev,
  	struct amdgpu_fpriv *fpriv;
  	struct amdgpu_ctx_mgr *mgr;
  	struct amdgpu_ctx *ctx;
-	uint32_t id;
+	unsigned long id;
  	int r;
 
  	if (fd_empty(f))
@@ -50,10 +50,10 @@ static int amdgpu_sched_process_priority_override(struct amdgpu_device *adev,
  		return r;
 
  	mgr = &fpriv->ctx_mgr;
-	mutex_lock(&mgr->lock);
-	idr_for_each_entry(&mgr->ctx_handles, ctx, id)
+	xa_lock(&mgr->ctx_handles);
+	xa_for_each(&mgr->ctx_handles, id, ctx)
  		amdgpu_ctx_priority_override(ctx, priority);
-	mutex_unlock(&mgr->lock);
+	xa_unlock(&mgr->ctx_handles);
 
  	return 0;
  }
maintainer
#13 lore raw

Re: [RFC 4/5] drm/amdgpu: Convert context manager to xarray

Quoted 48 lines · Tvrtko Ursulin
Quoted 38 lines · Christian König
Quoted 37 lines · Tvrtko Ursulin

IDR is deprecated so let's convert the context manager to xarray.

In doing so we remove the context manager mutex and switch call sites which required the guarantee context cannot go away while they walk the list of context, or otherwise operate on them, to use reference counting.

This allows us to use the built in xarray spinlock for all operations and just temporarily drop it when we need to call sleeping functions.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c   | 117 ++++++++-------------- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h   |   5 +- drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c |   8 +-

  3 files changed, 49 insertions(+), 81 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -10
index afedea02188d..ee1464f8751a 100644
@@ -484,22 +484,17 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
       if (!ctx)
           return -ENOMEM;
   -    mutex_lock(&mgr->lock);
-    r = idr_alloc(&mgr->ctx_handles, ctx, 1, AMDGPU_VM_MAX_NUM_CTX, GFP_KERNEL);
-    if (r < 0) {
-        mutex_unlock(&mgr->lock);
-        kfree(ctx);
-        return r;
-    }
-
-    *id = (uint32_t)r;
       r = amdgpu_ctx_init(mgr, priority, filp, ctx);
       if (r) {
-        idr_remove(&mgr->ctx_handles, *id);
-        *id = 0;
           kfree(ctx);
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_alloc(struct amdgpu_device *adev, · line 494

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +4 -1
+        return r;
       }
-    mutex_unlock(&mgr->lock);
+
+    r = xa_alloc(&mgr->ctx_handles, id, ctx,
+             XA_LIMIT(1, AMDGPU_VM_MAX_NUM_CTX - 1), GFP_KERNEL);

Please drop the AMDGPU_VM_MAX_NUM_CTX define as well. That is just a totally pointless limitation.

Quoted 1 line

So xa_limit_32b ?

I think so yes. At least I don't see a good reason to have an artificial limitation.

Quoted 183 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c static int amdgpu_ctx_alloc(struct amdgpu_device *adev, · line 497

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3 -0
+    if (r)
+        kref_put(&ctx->refcount, amdgpu_ctx_fini);
+

While at it you should probably clean up the unecessary differenciation between amdgpu_ctx_fini and amdgpu_ctx__do_release as well.

Right, those are very confusing indeed, so I happily will give that a go.

Quoted 129 lines
Quoted 129 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
       return r;
   }
   @@ -523,14 +518,11 @@ static void amdgpu_ctx_do_release(struct kref *ref)

    static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id) {
-    struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
      struct amdgpu_ctx *ctx;
  -    mutex_lock(&mgr->lock);
-    ctx = idr_remove(&mgr->ctx_handles, id);
-    if (ctx)
-        kref_put(&ctx->refcount, amdgpu_ctx_do_release);
-    mutex_unlock(&mgr->lock);
+    ctx = xa_erase(&fpriv->ctx_mgr.ctx_handles, id);
+    amdgpu_ctx_put(ctx);
+
      return ctx ? 0 : -EINVAL;
  }
  @@ -539,20 +531,12 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev, union drm_amdgpu_ctx_out *out)
  {
      struct amdgpu_ctx *ctx;
-    struct amdgpu_ctx_mgr *mgr;
      unsigned reset_counter;
  -    if (!fpriv)
+    ctx = amdgpu_ctx_get(fpriv, id);
+    if (!ctx)
          return -EINVAL;
  -    mgr = &fpriv->ctx_mgr;
-    mutex_lock(&mgr->lock);
-    ctx = idr_find(&mgr->ctx_handles, id);
-    if (!ctx) {
-        mutex_unlock(&mgr->lock);
-        return -EINVAL;
-    }
-
      /* TODO: these two are always zero */
      out->state.flags = 0x0;
      out->state.hangs = 0x0;

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -0
@@ -566,7 +550,8 @@ static int amdgpu_ctx_query(struct amdgpu_device *adev,
           out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
       ctx->reset_counter_query = reset_counter;
   -    mutex_unlock(&mgr->lock);
+    amdgpu_ctx_put(ctx);
+
       return 0;
   }
   @@ -578,19 +563,11 @@ static int amdgpu_ctx_query2(struct amdgpu_device *adev,

  {
      struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
      struct amdgpu_ctx *ctx;
-    struct amdgpu_ctx_mgr *mgr;
  -    if (!fpriv)
+    ctx = amdgpu_ctx_get(fpriv, id);
+    if (!ctx)
          return -EINVAL;
  -    mgr = &fpriv->ctx_mgr;
-    mutex_lock(&mgr->lock);
-    ctx = idr_find(&mgr->ctx_handles, id);
-    if (!ctx) {
-        mutex_unlock(&mgr->lock);
-        return -EINVAL;
-    }
-
      out->state.flags = 0x0;
      out->state.hangs = 0x0;
  @@ -630,7 +607,8 @@ static int amdgpu_ctx_query2(struct amdgpu_device *adev, msecs_to_jiffies(AMDGPU_RAS_COUNTE_DELAY_MS));
      }
  -    mutex_unlock(&mgr->lock);
+    amdgpu_ctx_put(ctx);
+
      return 0;
  }
  @@ -639,26 +617,18 @@ static int amdgpu_ctx_stable_pstate(struct amdgpu_device *adev, bool set, u32 *stable_pstate)
  {
      struct amdgpu_ctx *ctx;
-    struct amdgpu_ctx_mgr *mgr;
      int r;
  -    if (!fpriv)
+    ctx = amdgpu_ctx_get(fpriv, id);
+    if (!ctx)
          return -EINVAL;
  -    mgr = &fpriv->ctx_mgr;
-    mutex_lock(&mgr->lock);
-    ctx = idr_find(&mgr->ctx_handles, id);
-    if (!ctx) {
-        mutex_unlock(&mgr->lock);
-        return -EINVAL;
-    }
-
      if (set)
          r = amdgpu_ctx_set_stable_pstate(ctx, *stable_pstate);
      else
          r = amdgpu_ctx_get_stable_pstate(ctx, stable_pstate);
  -    mutex_unlock(&mgr->lock);
+    amdgpu_ctx_put(ctx);
      return r;
  }
  @@ -737,11 +707,11 @@ struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id) mgr = &fpriv->ctx_mgr;
  -    mutex_lock(&mgr->lock);
-    ctx = idr_find(&mgr->ctx_handles, id);
+    xa_lock(&mgr->ctx_handles);
+    ctx = xa_load(&mgr->ctx_handles, id);
      if (ctx)
          kref_get(&ctx->refcount);
-    mutex_unlock(&mgr->lock);
+    xa_unlock(&mgr->ctx_handles);
      return ctx;
  }
  @@ -886,8 +856,7 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr, unsigned int i;
        mgr->adev = adev;
-    mutex_init(&mgr->lock);
-    idr_init_base(&mgr->ctx_handles, 1);
+    xa_init_flags(&mgr->ctx_handles, XA_FLAGS_ALLOC1);
        for (i = 0; i < AMDGPU_HW_IP_NUM; ++i)
          atomic64_set(&mgr->time_spend[i], 0);

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -2
@@ -896,13 +865,14 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr,
   long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
   {
       struct amdgpu_ctx *ctx;
-    struct idr *idp;
-    uint32_t id, i, j;
+    unsigned long id;
+    uint32_t i, j;
   -    idp = &mgr->ctx_handles;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr, · line 873

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3 -3
-
-    mutex_lock(&mgr->lock);
-    idr_for_each_entry(idp, ctx, id) {
+    xa_lock(&mgr->ctx_handles);
+    xa_for_each(&mgr->ctx_handles, id, ctx) {
+        if (!kref_get_unless_zero(&ctx->refcount))

This should be pointless, why not use kfre_get instead?

True, don't know what I was thinking.

Quoted 33 lines
Quoted 32 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +8 -5
+            continue;
+        xa_unlock(&mgr->ctx_handles);
           for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
               for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j) {
                   struct drm_sched_entity *entity;
@@ -914,20 +884,20 @@ long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)
                   timeout = drm_sched_entity_flush(entity, timeout);
               }
           }
+        amdgpu_ctx_put(ctx);
+        xa_lock(&mgr->ctx_handles);
       }
-    mutex_unlock(&mgr->lock);
+    xa_unlock(&mgr->ctx_handles);
       return timeout;
   }
     static void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
   {
       struct amdgpu_ctx *ctx;
-    struct idr *idp;
-    uint32_t id, i, j;
+    unsigned long id;
+    uint32_t i, j;
   -    idp = &mgr->ctx_handles;
-
-    idr_for_each_entry(idp, ctx, id) {
+    xa_for_each(&mgr->ctx_handles, id, ctx) {
 
           if (kref_read(&ctx->refcount) != 1) {
               DRM_ERROR("ctx %p is still alive\n", ctx);
               continue;
Quoted 5 lines
Quoted 3 lines

Please drop that check as well. It just leads to memory leaks and errors should anything go wrong and so only make things worse.

We can have something like WARN_ON_ONCE(kref_read(&ctx->refcount) != 1) here, but I think that is superflous as well.

Hm, you mean leaving the entity around while the rest of the file gets closed causes other problems ie. this is not enough to turn crashes into memory leaks?

Yes, exactly that. This is basically just another pointless try to work around problems elsewhere.

Regards,
Christian.

Quoted 91 lines

Regards,

Tvrtko

Quoted 85 lines

Regards,
Christian.

Quoted 80 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +5 -5
@@ -951,8 +921,7 @@ static void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
   void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
   {
       amdgpu_ctx_mgr_entity_fini(mgr);
-    idr_destroy(&mgr->ctx_handles);
-    mutex_destroy(&mgr->lock);
+    xa_destroy(&mgr->ctx_handles);
   }
     void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
@@ -960,21 +929,21 @@ void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
   {
       struct amdgpu_ctx *ctx;
       unsigned int hw_ip, i;
-    uint32_t id;
+    unsigned long id;
         /*
        * This is a little bit racy because it can be that a ctx or a fence are
        * destroyed just in the moment we try to account them. But that is ok
        * since exactly that case is explicitely allowed by the interface.
        */
-    mutex_lock(&mgr->lock);
       for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
           uint64_t ns = atomic64_read(&mgr->time_spend[hw_ip]);
             usage[hw_ip] = ns_to_ktime(ns);
       }
   -    idr_for_each_entry(&mgr->ctx_handles, ctx, id) {
+    xa_lock(&mgr->ctx_handles);
+    xa_for_each(&mgr->ctx_handles, id, ctx) {
           for (hw_ip = 0; hw_ip < AMDGPU_HW_IP_NUM; ++hw_ip) {
               for (i = 0; i < amdgpu_ctx_num_entities[hw_ip]; ++i) {
                   struct amdgpu_ctx_entity *centity;
@@ -988,5 +957,5 @@ void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
               }
           }
       }
-    mutex_unlock(&mgr->lock);
+    xa_unlock(&mgr->ctx_handles);
   }
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +2 -3
index 090dfe86f75b..d073cffa455d 100644
@@ -25,6 +25,7 @@
     #include <linux/ktime.h>
   #include <linux/types.h>
+#include <linux/xarray.h>
     #include "amdgpu_ring.h"
   @@ -62,9 +63,7 @@ struct amdgpu_ctx {
     struct amdgpu_ctx_mgr {
       struct amdgpu_device    *adev;
-    struct mutex        lock;
-    /* protected by lock */
-    struct idr        ctx_handles;
+    struct xarray        ctx_handles;

      atomic64_t        time_spend[AMDGPU_HW_IP_NUM];
  };
  diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c index 341beec59537..471d27b2db01 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c +4 -4
@@ -39,7 +39,7 @@ static int amdgpu_sched_process_priority_override(struct amdgpu_device *adev,
       struct amdgpu_fpriv *fpriv;
       struct amdgpu_ctx_mgr *mgr;
       struct amdgpu_ctx *ctx;
-    uint32_t id;
+    unsigned long id;
       int r;
         if (fd_empty(f))
@@ -50,10 +50,10 @@ static int amdgpu_sched_process_priority_override(struct amdgpu_device *adev,
           return r;
         mgr = &fpriv->ctx_mgr;
-    mutex_lock(&mgr->lock);
-    idr_for_each_entry(&mgr->ctx_handles, ctx, id)
+    xa_lock(&mgr->ctx_handles);
+    xa_for_each(&mgr->ctx_handles, id, ctx)
           amdgpu_ctx_priority_override(ctx, priority);
-    mutex_unlock(&mgr->lock);
+    xa_unlock(&mgr->ctx_handles);
         return 0;
   }
#14 lore raw

RFC 5/5 drm/amdgpu/mes: Remove idr leftovers

In reply to Tvrtko Ursulin

Commit
cb17fff3a254 ("drm/amdgpu/mes: remove unused functions") removed most of the code using these IDRs but forgot to remove the struct members and init/destroy paths.

There is also interrupt handling code in SDMA 5.0 and 5.2 which appears to be using it, but is is unreachable since nothing ever allocates the relevant IDR. We replace those with one time warnings just to avoid any functional difference, but it is also possible they should be removed.

References: cb17fff3a254 ("drm/amdgpu/mes: remove unused functions")
Cc: Alex Deucher <[email protected]>

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c |  9 ---------
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h |  3 ---
 drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c  | 18 +++---------------
 drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c  | 18 +++---------------
 4 files changed, 6 insertions(+), 42 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +0 -9
index 9c182ce501af..505619d504ea 100644
@@ -94,9 +94,6 @@ int amdgpu_mes_init(struct amdgpu_device *adev)
 
 	adev->mes.adev = adev;
 
-	idr_init(&adev->mes.pasid_idr);
-	idr_init(&adev->mes.gang_id_idr);
-	idr_init(&adev->mes.queue_id_idr);
 	ida_init(&adev->mes.doorbell_ida);
 	spin_lock_init(&adev->mes.queue_id_lock);
 	mutex_init(&adev->mes.mutex_hidden);
@@ -218,9 +215,6 @@ int amdgpu_mes_init(struct amdgpu_device *adev)
 				      adev->mes.query_status_fence_offs[i]);
 	}
 
-	idr_destroy(&adev->mes.pasid_idr);
-	idr_destroy(&adev->mes.gang_id_idr);
-	idr_destroy(&adev->mes.queue_id_idr);
 	ida_destroy(&adev->mes.doorbell_ida);
 	mutex_destroy(&adev->mes.mutex_hidden);
 	return r;
@@ -248,9 +242,6 @@ void amdgpu_mes_fini(struct amdgpu_device *adev)
 
 	amdgpu_mes_doorbell_free(adev);
 
-	idr_destroy(&adev->mes.pasid_idr);
-	idr_destroy(&adev->mes.gang_id_idr);
-	idr_destroy(&adev->mes.queue_id_idr);
 	ida_destroy(&adev->mes.doorbell_ida);
 	mutex_destroy(&adev->mes.mutex_hidden);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +0 -3
index e989225b354b..f45129277479 100644
@@ -68,9 +68,6 @@ struct amdgpu_mes {
 
 	struct mutex                    mutex_hidden;
 
-	struct idr                      pasid_idr;
-	struct idr                      gang_id_idr;
-	struct idr                      queue_id_idr;
 	struct ida                      doorbell_ida;
 
 	spinlock_t                      queue_id_lock;
drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c +3 -15
index 8ddc4df06a1f..ab9e6199b01d 100644
@@ -1704,24 +1704,12 @@ static int sdma_v5_0_process_trap_irq(struct amdgpu_device *adev,
 				      struct amdgpu_irq_src *source,
 				      struct amdgpu_iv_entry *entry)
 {
-	uint32_t mes_queue_id = entry->src_data[0];
-
 	DRM_DEBUG("IH: SDMA trap\n");
 
-	if (adev->enable_mes && (mes_queue_id & AMDGPU_FENCE_MES_QUEUE_FLAG)) {
-		struct amdgpu_mes_queue *queue;
-
-		mes_queue_id &= AMDGPU_FENCE_MES_QUEUE_ID_MASK;
-
-		spin_lock(&adev->mes.queue_id_lock);
-		queue = idr_find(&adev->mes.queue_id_idr, mes_queue_id);
-		if (queue) {
-			DRM_DEBUG("process smda queue id = %d\n", mes_queue_id);
-			amdgpu_fence_process(queue->ring);
-		}
-		spin_unlock(&adev->mes.queue_id_lock);
+	if (drm_WARN_ON_ONCE(&adev->ddev,
+			     adev->enable_mes &&
+			     (entry->src_data[0] & AMDGPU_FENCE_MES_QUEUE_FLAG)))
 		return 0;
-	}
 
 	switch (entry->client_id) {
 	case SOC15_IH_CLIENTID_SDMA0:
drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c +3 -15
index 51101b0aa2fa..4f78dd93939c 100644
@@ -1617,24 +1617,12 @@ static int sdma_v5_2_process_trap_irq(struct amdgpu_device *adev,
 				      struct amdgpu_irq_src *source,
 				      struct amdgpu_iv_entry *entry)
 {
-	uint32_t mes_queue_id = entry->src_data[0];
-
 	DRM_DEBUG("IH: SDMA trap\n");
 
-	if (adev->enable_mes && (mes_queue_id & AMDGPU_FENCE_MES_QUEUE_FLAG)) {
-		struct amdgpu_mes_queue *queue;
-
-		mes_queue_id &= AMDGPU_FENCE_MES_QUEUE_ID_MASK;
-
-		spin_lock(&adev->mes.queue_id_lock);
-		queue = idr_find(&adev->mes.queue_id_idr, mes_queue_id);
-		if (queue) {
-			DRM_DEBUG("process smda queue id = %d\n", mes_queue_id);
-			amdgpu_fence_process(queue->ring);
-		}
-		spin_unlock(&adev->mes.queue_id_lock);
+	if (drm_WARN_ON_ONCE(&adev->ddev,
+			     adev->enable_mes &&
+			     (entry->src_data[0] & AMDGPU_FENCE_MES_QUEUE_FLAG)))
 		return 0;
-	}
 
 	switch (entry->client_id) {
 	case SOC15_IH_CLIENTID_SDMA0:
Signature · 1 line
2.51.1
maintainer Reviewed-by on v1 5/5
#15 lore raw

Re: [RFC 5/5] drm/amdgpu/mes: Remove idr leftovers

Quoted 5 lines · Tvrtko Ursulin

Commit
cb17fff3a254 ("drm/amdgpu/mes: remove unused functions") removed most of the code using these IDRs but forgot to remove the struct members and init/destroy paths.

Quoted 8 lines

There is also interrupt handling code in SDMA 5.0 and 5.2 which appears to be using it, but is is unreachable since nothing ever allocates the relevant IDR. We replace those with one time warnings just to avoid any functional difference, but it is also possible they should be removed.

References: cb17fff3a254 ("drm/amdgpu/mes: remove unused functions")
Cc: Alex Deucher <[email protected]>

Quoted 119 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c |  9 ---------
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h |  3 ---
 drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c  | 18 +++---------------
 drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c  | 18 +++---------------
 4 files changed, 6 insertions(+), 42 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +0 -9
index 9c182ce501af..505619d504ea 100644
@@ -94,9 +94,6 @@ int amdgpu_mes_init(struct amdgpu_device *adev)
 
 	adev->mes.adev = adev;
 
-	idr_init(&adev->mes.pasid_idr);
-	idr_init(&adev->mes.gang_id_idr);
-	idr_init(&adev->mes.queue_id_idr);
 	ida_init(&adev->mes.doorbell_ida);
 	spin_lock_init(&adev->mes.queue_id_lock);
 	mutex_init(&adev->mes.mutex_hidden);
@@ -218,9 +215,6 @@ int amdgpu_mes_init(struct amdgpu_device *adev)
 				      adev->mes.query_status_fence_offs[i]);
 	}
 
-	idr_destroy(&adev->mes.pasid_idr);
-	idr_destroy(&adev->mes.gang_id_idr);
-	idr_destroy(&adev->mes.queue_id_idr);
 	ida_destroy(&adev->mes.doorbell_ida);
 	mutex_destroy(&adev->mes.mutex_hidden);
 	return r;
@@ -248,9 +242,6 @@ void amdgpu_mes_fini(struct amdgpu_device *adev)
 
 	amdgpu_mes_doorbell_free(adev);
 
-	idr_destroy(&adev->mes.pasid_idr);
-	idr_destroy(&adev->mes.gang_id_idr);
-	idr_destroy(&adev->mes.queue_id_idr);
 	ida_destroy(&adev->mes.doorbell_ida);
 	mutex_destroy(&adev->mes.mutex_hidden);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +0 -3
index e989225b354b..f45129277479 100644
@@ -68,9 +68,6 @@ struct amdgpu_mes {
 
 	struct mutex                    mutex_hidden;
 
-	struct idr                      pasid_idr;
-	struct idr                      gang_id_idr;
-	struct idr                      queue_id_idr;
 	struct ida                      doorbell_ida;
 
 	spinlock_t                      queue_id_lock;
drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c +3 -15
index 8ddc4df06a1f..ab9e6199b01d 100644
@@ -1704,24 +1704,12 @@ static int sdma_v5_0_process_trap_irq(struct amdgpu_device *adev,
 				      struct amdgpu_irq_src *source,
 				      struct amdgpu_iv_entry *entry)
 {
-	uint32_t mes_queue_id = entry->src_data[0];
-
 	DRM_DEBUG("IH: SDMA trap\n");
 
-	if (adev->enable_mes && (mes_queue_id & AMDGPU_FENCE_MES_QUEUE_FLAG)) {
-		struct amdgpu_mes_queue *queue;
-
-		mes_queue_id &= AMDGPU_FENCE_MES_QUEUE_ID_MASK;
-
-		spin_lock(&adev->mes.queue_id_lock);
-		queue = idr_find(&adev->mes.queue_id_idr, mes_queue_id);
-		if (queue) {
-			DRM_DEBUG("process smda queue id = %d\n", mes_queue_id);
-			amdgpu_fence_process(queue->ring);
-		}
-		spin_unlock(&adev->mes.queue_id_lock);
+	if (drm_WARN_ON_ONCE(&adev->ddev,
+			     adev->enable_mes &&
+			     (entry->src_data[0] & AMDGPU_FENCE_MES_QUEUE_FLAG)))
 		return 0;
-	}
 
 	switch (entry->client_id) {
 	case SOC15_IH_CLIENTID_SDMA0:
drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c +3 -15
index 51101b0aa2fa..4f78dd93939c 100644
@@ -1617,24 +1617,12 @@ static int sdma_v5_2_process_trap_irq(struct amdgpu_device *adev,
 				      struct amdgpu_irq_src *source,
 				      struct amdgpu_iv_entry *entry)
 {
-	uint32_t mes_queue_id = entry->src_data[0];
-
 	DRM_DEBUG("IH: SDMA trap\n");
 
-	if (adev->enable_mes && (mes_queue_id & AMDGPU_FENCE_MES_QUEUE_FLAG)) {
-		struct amdgpu_mes_queue *queue;
-
-		mes_queue_id &= AMDGPU_FENCE_MES_QUEUE_ID_MASK;
-
-		spin_lock(&adev->mes.queue_id_lock);
-		queue = idr_find(&adev->mes.queue_id_idr, mes_queue_id);
-		if (queue) {
-			DRM_DEBUG("process smda queue id = %d\n", mes_queue_id);
-			amdgpu_fence_process(queue->ring);
-		}
-		spin_unlock(&adev->mes.queue_id_lock);
+	if (drm_WARN_ON_ONCE(&adev->ddev,
+			     adev->enable_mes &&
+			     (entry->src_data[0] & AMDGPU_FENCE_MES_QUEUE_FLAG)))
 		return 0;
-	}
 
 	switch (entry->client_id) {
 	case SOC15_IH_CLIENTID_SDMA0:
#16 lore raw

Re: [RFC 5/5] drm/amdgpu/mes: Remove idr leftovers

Quoted 13 lines · Christian König
Quoted 10 lines · Tvrtko Ursulin

Commit
cb17fff3a254 ("drm/amdgpu/mes: remove unused functions") removed most of the code using these IDRs but forgot to remove the struct members and init/destroy paths.

There is also interrupt handling code in SDMA 5.0 and 5.2 which appears to be using it, but is is unreachable since nothing ever allocates the relevant IDR. We replace those with one time warnings just to avoid any functional difference, but it is also possible they should be removed.

Quoted 5 lines
Quoted 3 lines

References: cb17fff3a254 ("drm/amdgpu/mes: remove unused functions")
Cc: Alex Deucher <[email protected]>

Thanks!

Do you think the drm_WARN_ON_ONCE's are worth keeping or I should remove that completely? I wasn't sure if there is a code path or not to enable the required condition:

adev->enable_mes && (entry->src_data[0] & AMDGPU_FENCE_MES_QUEUE_FLAG))

If there is interrupts would get silently not processed. Although that is currently in the code as well. Maybe it is impossible?

Regards,

Tvrtko

Quoted 121 lines
Quoted 120 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c |  9 ---------
  drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h |  3 ---
  drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c  | 18 +++---------------
  drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c  | 18 +++---------------
  4 files changed, 6 insertions(+), 42 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +0 -9
index 9c182ce501af..505619d504ea 100644
@@ -94,9 +94,6 @@ int amdgpu_mes_init(struct amdgpu_device *adev)
 
  	adev->mes.adev = adev;
 
-	idr_init(&adev->mes.pasid_idr);
-	idr_init(&adev->mes.gang_id_idr);
-	idr_init(&adev->mes.queue_id_idr);
  	ida_init(&adev->mes.doorbell_ida);
  	spin_lock_init(&adev->mes.queue_id_lock);
  	mutex_init(&adev->mes.mutex_hidden);
@@ -218,9 +215,6 @@ int amdgpu_mes_init(struct amdgpu_device *adev)
  				      adev->mes.query_status_fence_offs[i]);
  	}
 
-	idr_destroy(&adev->mes.pasid_idr);
-	idr_destroy(&adev->mes.gang_id_idr);
-	idr_destroy(&adev->mes.queue_id_idr);
  	ida_destroy(&adev->mes.doorbell_ida);
  	mutex_destroy(&adev->mes.mutex_hidden);
  	return r;
@@ -248,9 +242,6 @@ void amdgpu_mes_fini(struct amdgpu_device *adev)
 
  	amdgpu_mes_doorbell_free(adev);
 
-	idr_destroy(&adev->mes.pasid_idr);
-	idr_destroy(&adev->mes.gang_id_idr);
-	idr_destroy(&adev->mes.queue_id_idr);
  	ida_destroy(&adev->mes.doorbell_ida);
  	mutex_destroy(&adev->mes.mutex_hidden);
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +0 -3
index e989225b354b..f45129277479 100644
@@ -68,9 +68,6 @@ struct amdgpu_mes {
 
  	struct mutex                    mutex_hidden;
 
-	struct idr                      pasid_idr;
-	struct idr                      gang_id_idr;
-	struct idr                      queue_id_idr;
  	struct ida                      doorbell_ida;
 
  	spinlock_t                      queue_id_lock;
drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c +3 -15
index 8ddc4df06a1f..ab9e6199b01d 100644
@@ -1704,24 +1704,12 @@ static int sdma_v5_0_process_trap_irq(struct amdgpu_device *adev,
  				      struct amdgpu_irq_src *source,
  				      struct amdgpu_iv_entry *entry)
  {
-	uint32_t mes_queue_id = entry->src_data[0];
-
  	DRM_DEBUG("IH: SDMA trap\n");
 
-	if (adev->enable_mes && (mes_queue_id & AMDGPU_FENCE_MES_QUEUE_FLAG)) {
-		struct amdgpu_mes_queue *queue;
-
-		mes_queue_id &= AMDGPU_FENCE_MES_QUEUE_ID_MASK;
-
-		spin_lock(&adev->mes.queue_id_lock);
-		queue = idr_find(&adev->mes.queue_id_idr, mes_queue_id);
-		if (queue) {
-			DRM_DEBUG("process smda queue id = %d\n", mes_queue_id);
-			amdgpu_fence_process(queue->ring);
-		}
-		spin_unlock(&adev->mes.queue_id_lock);
+	if (drm_WARN_ON_ONCE(&adev->ddev,
+			     adev->enable_mes &&
+			     (entry->src_data[0] & AMDGPU_FENCE_MES_QUEUE_FLAG)))
  		return 0;
-	}
 
  	switch (entry->client_id) {
  	case SOC15_IH_CLIENTID_SDMA0:
drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c +3 -15
index 51101b0aa2fa..4f78dd93939c 100644
@@ -1617,24 +1617,12 @@ static int sdma_v5_2_process_trap_irq(struct amdgpu_device *adev,
  				      struct amdgpu_irq_src *source,
  				      struct amdgpu_iv_entry *entry)
  {
-	uint32_t mes_queue_id = entry->src_data[0];
-
  	DRM_DEBUG("IH: SDMA trap\n");
 
-	if (adev->enable_mes && (mes_queue_id & AMDGPU_FENCE_MES_QUEUE_FLAG)) {
-		struct amdgpu_mes_queue *queue;
-
-		mes_queue_id &= AMDGPU_FENCE_MES_QUEUE_ID_MASK;
-
-		spin_lock(&adev->mes.queue_id_lock);
-		queue = idr_find(&adev->mes.queue_id_idr, mes_queue_id);
-		if (queue) {
-			DRM_DEBUG("process smda queue id = %d\n", mes_queue_id);
-			amdgpu_fence_process(queue->ring);
-		}
-		spin_unlock(&adev->mes.queue_id_lock);
+	if (drm_WARN_ON_ONCE(&adev->ddev,
+			     adev->enable_mes &&
+			     (entry->src_data[0] & AMDGPU_FENCE_MES_QUEUE_FLAG)))
  		return 0;
-	}
 
  	switch (entry->client_id) {
  	case SOC15_IH_CLIENTID_SDMA0:
maintainer
#17 lore raw

Re: [RFC 5/5] drm/amdgpu/mes: Remove idr leftovers

Quoted 19 lines · Tvrtko Ursulin
Quoted 17 lines · Christian König
Quoted 13 lines · Tvrtko Ursulin

Commit
cb17fff3a254 ("drm/amdgpu/mes: remove unused functions") removed most of the code using these IDRs but forgot to remove the struct members and init/destroy paths.

There is also interrupt handling code in SDMA 5.0 and 5.2 which appears to be using it, but is is unreachable since nothing ever allocates the relevant IDR. We replace those with one time warnings just to avoid any functional difference, but it is also possible they should be removed.

References: cb17fff3a254 ("drm/amdgpu/mes: remove unused functions")
Cc: Alex Deucher <[email protected]>

Quoted 5 lines
Quoted 1 line

Thanks!

Do you think the drm_WARN_ON_ONCE's are worth keeping or I should remove that completely?

I wanted to discuss that with Alex after the holidays, but my gut feeling is it is superflous.

Quoted 5 lines

I wasn't sure if there is a code path or not to enable the required condition:

 adev->enable_mes && (entry->src_data[0] & AMDGPU_FENCE_MES_QUEUE_FLAG))

If there is interrupts would get silently not processed. Although that is currently in the code as well. Maybe it is impossible?

Yeah that's not so much of a problem. At worst it will create a performance problem because we only check the fence after a timeout.

Regards,
Christian.

Quoted 115 lines

Regards,

Tvrtko

Quoted 109 lines
Quoted 108 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c |  9 --------- drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h |  3 --- drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c  | 18 +++--------------- drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c  | 18 +++---------------

  4 files changed, 6 insertions(+), 42 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c +0 -6
index 9c182ce501af..505619d504ea 100644
@@ -94,9 +94,6 @@ int amdgpu_mes_init(struct amdgpu_device *adev)
         adev->mes.adev = adev;
   -    idr_init(&adev->mes.pasid_idr);
-    idr_init(&adev->mes.gang_id_idr);
-    idr_init(&adev->mes.queue_id_idr);
       ida_init(&adev->mes.doorbell_ida);
       spin_lock_init(&adev->mes.queue_id_lock);
       mutex_init(&adev->mes.mutex_hidden);
@@ -218,9 +215,6 @@ int amdgpu_mes_init(struct amdgpu_device *adev)
                         adev->mes.query_status_fence_offs[i]);
       }
   -    idr_destroy(&adev->mes.pasid_idr);
-    idr_destroy(&adev->mes.gang_id_idr);
-    idr_destroy(&adev->mes.queue_id_idr);
       ida_destroy(&adev->mes.doorbell_ida);
       mutex_destroy(&adev->mes.mutex_hidden);
       return r;
@@ -248,9 +242,6 @@ void amdgpu_mes_fini(struct amdgpu_device *adev)
         amdgpu_mes_doorbell_free(adev);
   -    idr_destroy(&adev->mes.pasid_idr);
-    idr_destroy(&adev->mes.gang_id_idr);
-    idr_destroy(&adev->mes.queue_id_idr);
       ida_destroy(&adev->mes.doorbell_ida);
       mutex_destroy(&adev->mes.mutex_hidden);
   }
drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h +0 -2
index e989225b354b..f45129277479 100644
@@ -68,9 +68,6 @@ struct amdgpu_mes {
         struct mutex                    mutex_hidden;
   -    struct idr                      pasid_idr;
-    struct idr                      gang_id_idr;
-    struct idr                      queue_id_idr;
       struct ida                      doorbell_ida;
         spinlock_t                      queue_id_lock;
drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c +3 -14
index 8ddc4df06a1f..ab9e6199b01d 100644
@@ -1704,24 +1704,12 @@ static int sdma_v5_0_process_trap_irq(struct amdgpu_device *adev,
                         struct amdgpu_irq_src *source,
                         struct amdgpu_iv_entry *entry)
   {
-    uint32_t mes_queue_id = entry->src_data[0];
-
       DRM_DEBUG("IH: SDMA trap\n");
   -    if (adev->enable_mes && (mes_queue_id & AMDGPU_FENCE_MES_QUEUE_FLAG)) {
-        struct amdgpu_mes_queue *queue;
-
-        mes_queue_id &= AMDGPU_FENCE_MES_QUEUE_ID_MASK;
-
-        spin_lock(&adev->mes.queue_id_lock);
-        queue = idr_find(&adev->mes.queue_id_idr, mes_queue_id);
-        if (queue) {
-            DRM_DEBUG("process smda queue id = %d\n", mes_queue_id);
-            amdgpu_fence_process(queue->ring);
-        }
-        spin_unlock(&adev->mes.queue_id_lock);
+    if (drm_WARN_ON_ONCE(&adev->ddev,
+                 adev->enable_mes &&
+                 (entry->src_data[0] & AMDGPU_FENCE_MES_QUEUE_FLAG)))
           return 0;
-    }
         switch (entry->client_id) {

      case SOC15_IH_CLIENTID_SDMA0:

drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c +3 -14
index 51101b0aa2fa..4f78dd93939c 100644
@@ -1617,24 +1617,12 @@ static int sdma_v5_2_process_trap_irq(struct amdgpu_device *adev,
                         struct amdgpu_irq_src *source,
                         struct amdgpu_iv_entry *entry)
   {
-    uint32_t mes_queue_id = entry->src_data[0];
-
       DRM_DEBUG("IH: SDMA trap\n");
   -    if (adev->enable_mes && (mes_queue_id & AMDGPU_FENCE_MES_QUEUE_FLAG)) {
-        struct amdgpu_mes_queue *queue;
-
-        mes_queue_id &= AMDGPU_FENCE_MES_QUEUE_ID_MASK;
-
-        spin_lock(&adev->mes.queue_id_lock);
-        queue = idr_find(&adev->mes.queue_id_idr, mes_queue_id);
-        if (queue) {
-            DRM_DEBUG("process smda queue id = %d\n", mes_queue_id);
-            amdgpu_fence_process(queue->ring);
-        }
-        spin_unlock(&adev->mes.queue_id_lock);
+    if (drm_WARN_ON_ONCE(&adev->ddev,
+                 adev->enable_mes &&
+                 (entry->src_data[0] & AMDGPU_FENCE_MES_QUEUE_FLAG)))
           return 0;
-    }
         switch (entry->client_id) {

      case SOC15_IH_CLIENTID_SDMA0:

maintainer
#18 lore raw
In reply to Tvrtko Ursulin
Quoted 11 lines · Tvrtko Ursulin

RFC because the context conversion needs a lot of scrutiny (and passing through the automated test suites), while the bo list changes are perhaps a bit of a bike shed, although they do result in less lines of code and hopefully easier to read.

BO list sorting is really independent, as is the IDR MES cleanup (which could as well have the warnings removed). Just say the work and I can easily split them out.

Series has been smoke tested, including lockdep and game + desktop workloads so at least shouldn't be too obviously broken.

Oh, yes please! Some of that stuff was on my TODO list ever since amdgpu existed.

Going to take a look right away.

Thanks,
Christian

Quoted 22 lines

Tvrtko Ursulin (5):
drm/amdgpu: Replace idr with xarray in amdgpu_bo_list drm/amdgpu: Remove output parameter in bo list handling drm/amdgpu: Simplify sorting of the bo list drm/amdgpu: Convert context manager to xarray drm/amdgpu/mes: Remove idr leftovers

 drivers/gpu/drm/amd/amdgpu/amdgpu.h         |   4 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 140 +++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.h |  17 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c      |  58 ++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c     | 117 ++++++----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h     |   5 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c     |  11 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.c     |   9 --
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h     |   3 -
 drivers/gpu/drm/amd/amdgpu/amdgpu_sched.c   |   8 +-
 drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c      |  18 +--
 drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c      |  18 +--
 12 files changed, 160 insertions(+), 248 deletions(-)