Skip to contentSkip to conversation

drm/amdgpu: Optimization of AMDGPU_BO_LIST_OP_CREATE (v2)

PATCH In review Alex Xie 4 participants 7 messages Jun 16, 2017 – Jun 17, 2017 4 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

Alex Xie

author
#0 lore raw

PATCH 1/2 drm/amdgpu: Optimization of AMDGPU_BO_LIST_OP_CREATE (v2)

v2: Remove duplication of zeroing of bo list (Christian König)
Move idr_alloc function to end of ioctl (Christian König) Call kfree bo_list when amdgpu_bo_list_set return error. Combine the previous two patches into this patch. Add amdgpu_bo_list_set function prototype.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 53 ++++++++++++++++-------------
 1 file changed, 30 insertions(+), 23 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +30 -23
index a664987..5af956f 100644
@@ -35,33 +35,45 @@
 #define AMDGPU_BO_LIST_MAX_PRIORITY	32u
 #define AMDGPU_BO_LIST_NUM_BUCKETS	(AMDGPU_BO_LIST_MAX_PRIORITY + 1)
 
-static int amdgpu_bo_list_create(struct amdgpu_fpriv *fpriv,
-				 struct amdgpu_bo_list **result,
+static int amdgpu_bo_list_set(struct amdgpu_device *adev,
+				     struct drm_file *filp,
+				     struct amdgpu_bo_list *list,
+				     struct drm_amdgpu_bo_list_entry *info,
+				     unsigned num_entries);
+
+static int amdgpu_bo_list_create(struct amdgpu_device *adev,
+				 struct drm_file *filp,
+				 struct drm_amdgpu_bo_list_entry *info,
+				 unsigned num_entries,
 				 int *id)
 {
 	int r;
+	struct amdgpu_fpriv *fpriv = filp->driver_priv;
+	struct amdgpu_bo_list *list;
 
-	*result = kzalloc(sizeof(struct amdgpu_bo_list), GFP_KERNEL);
-	if (!*result)
+	list = kzalloc(sizeof(struct amdgpu_bo_list), GFP_KERNEL);
+	if (!list)
 		return -ENOMEM;
 
+	/* initialize bo list*/
+	mutex_init(&list->lock);
+
+	r = amdgpu_bo_list_set(adev, filp, list, info, num_entries);
+	if (r) {
+		kfree(list);
+		return r;
+	}
+
+	/* idr alloc should be called only after initialization of bo list. */
 	mutex_lock(&fpriv->bo_list_lock);
-	r = idr_alloc(&fpriv->bo_list_handles, *result,
-		      1, 0, GFP_KERNEL);
+	r = idr_alloc(&fpriv->bo_list_handles, list, 1, 0, GFP_KERNEL);
+	mutex_unlock(&fpriv->bo_list_lock);
 	if (r < 0) {
-		mutex_unlock(&fpriv->bo_list_lock);
-		kfree(*result);
+		kfree(list);
 		return r;
 	}
 	*id = r;
 
-	mutex_init(&(*result)->lock);
-	(*result)->num_entries = 0;
-	(*result)->array = NULL;
-
-	mutex_lock(&(*result)->lock);
-	mutex_unlock(&fpriv->bo_list_lock);
-
 	return 0;
 }
 
@@ -77,6 +89,7 @@ static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
 		mutex_unlock(&list->lock);
 		amdgpu_bo_list_free(list);
 	}
+
 	mutex_unlock(&fpriv->bo_list_lock);
 }
 
@@ -273,16 +286,10 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
 
 	switch (args->in.operation) {
 	case AMDGPU_BO_LIST_OP_CREATE:
-		r = amdgpu_bo_list_create(fpriv, &list, &handle);
+		r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
+					  &handle);
 		if (r)
 			goto error_free;
-
-		r = amdgpu_bo_list_set(adev, filp, list, info,
-					      args->in.bo_number);
-		amdgpu_bo_list_put(list);
-		if (r)
-			goto error_free;
-
 		break;
 
 	case AMDGPU_BO_LIST_OP_DESTROY:
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1497589410-17779-1-git-send-email-AlexBin.Xie-5C7GfCeVMHo@public.gmane.org

author
#2 lore raw

PATCH 2/2 drm/amdgpu: Optimize mutex usage (v2)

Use rw_semaphore instead of mutex for bo_lists.

In original function amdgpu_bo_list_get, the waiting for result->lock can be quite long while mutex bo_list_lock was holding. It can make other tasks waiting for bo_list_lock for long period too. Change bo_list_lock to rw_semaphore can avoid most of such long waiting.

Secondly, this patch allows several tasks(readers of idr) to proceed at the same time.

v2: use rcu and kref (Dave Airlie and Christian König)

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h         |  2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 40 ++++++++++++++++++++---------
 2 files changed, 30 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 063fc73..e9b3981 100644
@@ -871,6 +871,8 @@ struct amdgpu_fpriv {
 
 struct amdgpu_bo_list {
 	struct mutex lock;
+	struct rcu_head rhead;
+	struct kref refcount;
 	struct amdgpu_bo *gds_obj;
 	struct amdgpu_bo *gws_obj;
 	struct amdgpu_bo *oa_obj;
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +28 -12
index 5af956f..efa6903 100644
@@ -41,6 +41,20 @@ static int amdgpu_bo_list_set(struct amdgpu_device *adev,
 				     struct drm_amdgpu_bo_list_entry *info,
 				     unsigned num_entries);
 
+static void amdgpu_bo_list_release_rcu(struct kref *ref)
+{
+	unsigned i;
+	struct amdgpu_bo_list *list = container_of(ref, struct amdgpu_bo_list,
+						   refcount);
+
+	for (i = 0; i < list->num_entries; ++i)
+		amdgpu_bo_unref(&list->array[i].robj);
+
+	mutex_destroy(&list->lock);
+	drm_free_large(list->array);
+	kfree_rcu(list, rhead);
+}
+
 static int amdgpu_bo_list_create(struct amdgpu_device *adev,
 				 struct drm_file *filp,
 				 struct drm_amdgpu_bo_list_entry *info,
@@ -57,7 +71,7 @@ static int amdgpu_bo_list_create(struct amdgpu_device *adev,
 
 	/* initialize bo list*/
 	mutex_init(&list->lock);
-
+	kref_init(&list->refcount);
 	r = amdgpu_bo_list_set(adev, filp, list, info, num_entries);
 	if (r) {
 		kfree(list);
@@ -83,14 +97,9 @@ static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
 
 	mutex_lock(&fpriv->bo_list_lock);
 	list = idr_remove(&fpriv->bo_list_handles, id);
-	if (list) {
-		/* Another user may have a reference to this list still */
-		mutex_lock(&list->lock);
-		mutex_unlock(&list->lock);
-		amdgpu_bo_list_free(list);
-	}
-
 	mutex_unlock(&fpriv->bo_list_lock);
+	if (list)
+		kref_put(&list->refcount, amdgpu_bo_list_release_rcu);
 }
 
 static int amdgpu_bo_list_set(struct amdgpu_device *adev,
@@ -185,11 +194,17 @@ amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id)
 {
 	struct amdgpu_bo_list *result;
 
-	mutex_lock(&fpriv->bo_list_lock);
+	rcu_read_lock();
 	result = idr_find(&fpriv->bo_list_handles, id);
-	if (result)
-		mutex_lock(&result->lock);
-	mutex_unlock(&fpriv->bo_list_lock);
+
+	if (result) {
+		if (kref_get_unless_zero(&result->refcount))
+			mutex_lock(&result->lock);
+		else
+			result = NULL;
+	}
+	rcu_read_unlock();
+
 	return result;
 }
 
@@ -227,6 +242,7 @@ void amdgpu_bo_list_get_list(struct amdgpu_bo_list *list,
 void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
 {
 	mutex_unlock(&list->lock);
+	kref_put(&list->refcount, amdgpu_bo_list_release_rcu);
 }
 
 void amdgpu_bo_list_free(struct amdgpu_bo_list *list)
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1497589410-17779-2-git-send-email-AlexBin.Xie-5C7GfCeVMHo@public.gmane.org

#4 lore raw

Re: [PATCH 2/2] drm/amdgpu: Optimize mutex usage (v2)

Quoted 9 lines · Alex Xie

Use rw_semaphore instead of mutex for bo_lists.

In original function amdgpu_bo_list_get, the waiting for result->lock can be quite long while mutex bo_list_lock was holding. It can make other tasks waiting for bo_list_lock for long period too. Change bo_list_lock to rw_semaphore can avoid most of such long waiting.

Quoted 4 lines

Secondly, this patch allows several tasks(readers of idr) to proceed at the same time.

v2: use rcu and kref (Dave Airlie and Christian König)

At least the first two paragraphs above are inaccurate and confusing in v2. Please modify the commit log to reflect what the patch actually does now.

Signature · 6 lines
Earthling Michel Dänzer               |               http://www.amd.com
Libre software enthusiast             |             Mesa and X developer
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#5 lore raw

Re: [PATCH 2/2] drm/amdgpu: Optimize mutex usage (v2)

In reply to a message not in the archive

Am 16.06.2017 um 07:03 schrieb Alex Xie:

Quoted 45 lines

Use rw_semaphore instead of mutex for bo_lists.

In original function amdgpu_bo_list_get, the waiting for result->lock can be quite long while mutex bo_list_lock was holding. It can make other tasks waiting for bo_list_lock for long period too. Change bo_list_lock to rw_semaphore can avoid most of such long waiting.

Secondly, this patch allows several tasks(readers of idr) to proceed at the same time.

v2: use rcu and kref (Dave Airlie and Christian König)

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h         |  2 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 40 ++++++++++++++++++++---------
  2 files changed, 30 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 063fc73..e9b3981 100644
@@ -871,6 +871,8 @@ struct amdgpu_fpriv {
 
  struct amdgpu_bo_list {
  	struct mutex lock;
+	struct rcu_head rhead;
+	struct kref refcount;
  	struct amdgpu_bo *gds_obj;
  	struct amdgpu_bo *gws_obj;
  	struct amdgpu_bo *oa_obj;
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +4 -0
index 5af956f..efa6903 100644
@@ -41,6 +41,20 @@ static int amdgpu_bo_list_set(struct amdgpu_device *adev,
  				     struct drm_amdgpu_bo_list_entry *info,
  				     unsigned num_entries);
 
+static void amdgpu_bo_list_release_rcu(struct kref *ref)
+{
+	unsigned i;
+	struct amdgpu_bo_list *list = container_of(ref, struct amdgpu_bo_list,

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c static int amdgpu_bo_list_set(struct amdgpu_device *adev, · line 57

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +10 -0
+						   refcount);
+
+	for (i = 0; i < list->num_entries; ++i)
+		amdgpu_bo_unref(&list->array[i].robj);
+
+	mutex_destroy(&list->lock);
+	drm_free_large(list->array);
+	kfree_rcu(list, rhead);
+}
+

I'm probably missing something here: Why a new function and not change the existing amdgpu_bo_list_free() to use kfree_rcu instead?

Apart from that the patch looks good to me,
Christian.

Quoted 52 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +13 -12
  static int amdgpu_bo_list_create(struct amdgpu_device *adev,
  				 struct drm_file *filp,
  				 struct drm_amdgpu_bo_list_entry *info,
@@ -57,7 +71,7 @@ static int amdgpu_bo_list_create(struct amdgpu_device *adev,
 
  	/* initialize bo list*/
  	mutex_init(&list->lock);
-
+	kref_init(&list->refcount);
  	r = amdgpu_bo_list_set(adev, filp, list, info, num_entries);
  	if (r) {
  		kfree(list);
@@ -83,14 +97,9 @@ static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
 
  	mutex_lock(&fpriv->bo_list_lock);
  	list = idr_remove(&fpriv->bo_list_handles, id);
-	if (list) {
-		/* Another user may have a reference to this list still */
-		mutex_lock(&list->lock);
-		mutex_unlock(&list->lock);
-		amdgpu_bo_list_free(list);
-	}
-
  	mutex_unlock(&fpriv->bo_list_lock);
+	if (list)
+		kref_put(&list->refcount, amdgpu_bo_list_release_rcu);
  }
 
  static int amdgpu_bo_list_set(struct amdgpu_device *adev,
@@ -185,11 +194,17 @@ amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id)
  {
  	struct amdgpu_bo_list *result;
 
-	mutex_lock(&fpriv->bo_list_lock);
+	rcu_read_lock();
  	result = idr_find(&fpriv->bo_list_handles, id);
-	if (result)
-		mutex_lock(&result->lock);
-	mutex_unlock(&fpriv->bo_list_lock);
+
+	if (result) {
+		if (kref_get_unless_zero(&result->refcount))
+			mutex_lock(&result->lock);
+		else
+			result = NULL;
+	}
+	rcu_read_unlock();
+
  	return result;
  }
 

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c void amdgpu_bo_list_get_list(struct amdgpu_bo_list *list, · line 248

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +1 -0
@@ -227,6 +242,7 @@ void amdgpu_bo_list_get_list(struct amdgpu_bo_list *list,
  void amdgpu_bo_list_put(struct amdgpu_bo_list *list)
  {
  	mutex_unlock(&list->lock);
+	kref_put(&list->refcount, amdgpu_bo_list_release_rcu);
  }
 
  void amdgpu_bo_list_free(struct amdgpu_bo_list *list)

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 86ceaa66-955d-ada4-358b-24334193a169-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#7 lore raw

Re: [PATCH 2/2] drm/amdgpu: Optimize mutex usage (v2)

Quoted 60 lines · Christian König

Am 16.06.2017 um 07:03 schrieb Alex Xie:

Quoted 46 lines

Use rw_semaphore instead of mutex for bo_lists.

In original function amdgpu_bo_list_get, the waiting for result->lock can be quite long while mutex bo_list_lock was holding. It can make other tasks waiting for bo_list_lock for long period too. Change bo_list_lock to rw_semaphore can avoid most of such long waiting.

Secondly, this patch allows several tasks(readers of idr) to proceed at the same time.

v2: use rcu and kref (Dave Airlie and Christian König)

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h         |  2 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 40 
++++++++++++++++++++---------
  2 files changed, 30 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 063fc73..e9b3981 100644
@@ -871,6 +871,8 @@ struct amdgpu_fpriv {
    struct amdgpu_bo_list {
      struct mutex lock;
+    struct rcu_head rhead;
+    struct kref refcount;
      struct amdgpu_bo *gds_obj;
      struct amdgpu_bo *gws_obj;
      struct amdgpu_bo *oa_obj;
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +3 -0
index 5af956f..efa6903 100644
@@ -41,6 +41,20 @@ static int amdgpu_bo_list_set(struct amdgpu_device 
 *adev,
                       struct drm_amdgpu_bo_list_entry *info,
                       unsigned num_entries);
  +static void amdgpu_bo_list_release_rcu(struct kref *ref)
+{
+    unsigned i;
+    struct amdgpu_bo_list *list = container_of(ref, struct 
 amdgpu_bo_list,

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c static int amdgpu_bo_list_set(struct amdgpu_device · line 58

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +10 -0
+                           refcount);
+
+    for (i = 0; i < list->num_entries; ++i)
+        amdgpu_bo_unref(&list->array[i].robj);
+
+    mutex_destroy(&list->lock);
+    drm_free_large(list->array);
+    kfree_rcu(list, rhead);
+}
+

I'm probably missing something here: Why a new function and not change the existing amdgpu_bo_list_free() to use kfree_rcu instead?

Quoted 2 lines

Apart from that the patch looks good to me,
Christian.

amdgpu_bo_list_free() is called by amdgpu_driver_postclose_kms in file amdgpu_kms.c too. For amdgpu_driver_postclose_kms, there is no need to call kfree_rcu. It is more suitable to call kfree.

So we need two different release/free functions. Then I created a new function for kref_put.

Quoted 50 lines
Quoted 50 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +13 -11
  static int amdgpu_bo_list_create(struct amdgpu_device *adev,
                   struct drm_file *filp,
                   struct drm_amdgpu_bo_list_entry *info,
@@ -57,7 +71,7 @@ static int amdgpu_bo_list_create(struct 
 amdgpu_device *adev,
        /* initialize bo list*/
      mutex_init(&list->lock);
-
+    kref_init(&list->refcount);
      r = amdgpu_bo_list_set(adev, filp, list, info, num_entries);
      if (r) {
          kfree(list);
@@ -83,14 +97,9 @@ static void amdgpu_bo_list_destroy(struct 
 amdgpu_fpriv *fpriv, int id)
        mutex_lock(&fpriv->bo_list_lock);
      list = idr_remove(&fpriv->bo_list_handles, id);
-    if (list) {
-        /* Another user may have a reference to this list still */
-        mutex_lock(&list->lock);
-        mutex_unlock(&list->lock);
-        amdgpu_bo_list_free(list);
-    }
-
      mutex_unlock(&fpriv->bo_list_lock);
+    if (list)
+        kref_put(&list->refcount, amdgpu_bo_list_release_rcu);
  }
    static int amdgpu_bo_list_set(struct amdgpu_device *adev,
@@ -185,11 +194,17 @@ amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, 
 int id)
  {
      struct amdgpu_bo_list *result;
  -    mutex_lock(&fpriv->bo_list_lock);
+    rcu_read_lock();
      result = idr_find(&fpriv->bo_list_handles, id);
-    if (result)
-        mutex_lock(&result->lock);
-    mutex_unlock(&fpriv->bo_list_lock);
+
+    if (result) {
+        if (kref_get_unless_zero(&result->refcount))
+            mutex_lock(&result->lock);
+        else
+            result = NULL;
+    }
+    rcu_read_unlock();
+
      return result;
  }
Quoted 10 lines
Quoted 8 lines

@@ -227,6 +242,7 @@ void amdgpu_bo_list_get_list(struct
amdgpu_bo_list *list,
void amdgpu_bo_list_put(struct amdgpu_bo_list *list)

  {
      mutex_unlock(&list->lock);

+ kref_put(&list->refcount, amdgpu_bo_list_release_rcu);
}
void amdgpu_bo_list_free(struct amdgpu_bo_list *list)

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 07c69549-9ec8-572a-797d-b8fc477f4aa8-5C7GfCeVMHo@public.gmane.org

#9 lore raw

Re: [PATCH 2/2] drm/amdgpu: Optimize mutex usage (v2)

Am 17.06.2017 um 00:00 schrieb axie:

Quoted 63 lines · Christian König
Quoted 60 lines

Am 16.06.2017 um 07:03 schrieb Alex Xie:

Quoted 46 lines

Use rw_semaphore instead of mutex for bo_lists.

In original function amdgpu_bo_list_get, the waiting for result->lock can be quite long while mutex bo_list_lock was holding. It can make other tasks waiting for bo_list_lock for long period too. Change bo_list_lock to rw_semaphore can avoid most of such long waiting.

Secondly, this patch allows several tasks(readers of idr) to proceed at the same time.

v2: use rcu and kref (Dave Airlie and Christian König)

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h         |  2 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 40 
++++++++++++++++++++---------
  2 files changed, 30 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 063fc73..e9b3981 100644
@@ -871,6 +871,8 @@ struct amdgpu_fpriv {
    struct amdgpu_bo_list {
      struct mutex lock;
+    struct rcu_head rhead;
+    struct kref refcount;
      struct amdgpu_bo *gds_obj;
      struct amdgpu_bo *gws_obj;
      struct amdgpu_bo *oa_obj;
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +3 -0
index 5af956f..efa6903 100644
@@ -41,6 +41,20 @@ static int amdgpu_bo_list_set(struct 
 amdgpu_device *adev,
                       struct drm_amdgpu_bo_list_entry *info,
                       unsigned num_entries);
  +static void amdgpu_bo_list_release_rcu(struct kref *ref)
+{
+    unsigned i;
+    struct amdgpu_bo_list *list = container_of(ref, struct 
 amdgpu_bo_list,

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c static int amdgpu_bo_list_set(struct · line 58

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +10 -0
+                           refcount);
+
+    for (i = 0; i < list->num_entries; ++i)
+        amdgpu_bo_unref(&list->array[i].robj);
+
+    mutex_destroy(&list->lock);
+    drm_free_large(list->array);
+    kfree_rcu(list, rhead);
+}
+

I'm probably missing something here: Why a new function and not change the existing amdgpu_bo_list_free() to use kfree_rcu instead?

Quoted 7 lines
Quoted 2 lines

Apart from that the patch looks good to me,
Christian.

amdgpu_bo_list_free() is called by amdgpu_driver_postclose_kms in file amdgpu_kms.c too. For amdgpu_driver_postclose_kms, there is no need to call kfree_rcu. It is more suitable to call kfree.

So we need two different release/free functions. Then I created a new function for kref_put.

Ah, I see. Well call from amdgpu_driver_postclose_kms() is only used when the application crashes and doesn't clean up properly after itself.

Not sure if it's a good idea to duplicate the code because of this. Waiting for an RCU grace period is a no-op in most cases (it's very likely that all other CPUs are doing something in userspace), so kfree_rcu is rather cheap.

Either way the patch is Reviewed-by: Christian König <[email protected]>.

Quoted 62 lines
Quoted 51 lines
Quoted 51 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +13 -11
  static int amdgpu_bo_list_create(struct amdgpu_device *adev,
                   struct drm_file *filp,
                   struct drm_amdgpu_bo_list_entry *info,
@@ -57,7 +71,7 @@ static int amdgpu_bo_list_create(struct 
 amdgpu_device *adev,
        /* initialize bo list*/
      mutex_init(&list->lock);
-
+    kref_init(&list->refcount);
      r = amdgpu_bo_list_set(adev, filp, list, info, num_entries);
      if (r) {
          kfree(list);
@@ -83,14 +97,9 @@ static void amdgpu_bo_list_destroy(struct 
 amdgpu_fpriv *fpriv, int id)
        mutex_lock(&fpriv->bo_list_lock);
      list = idr_remove(&fpriv->bo_list_handles, id);
-    if (list) {
-        /* Another user may have a reference to this list still */
-        mutex_lock(&list->lock);
-        mutex_unlock(&list->lock);
-        amdgpu_bo_list_free(list);
-    }
-
      mutex_unlock(&fpriv->bo_list_lock);
+    if (list)
+        kref_put(&list->refcount, amdgpu_bo_list_release_rcu);
  }
    static int amdgpu_bo_list_set(struct amdgpu_device *adev,
@@ -185,11 +194,17 @@ amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, 
 int id)
  {
      struct amdgpu_bo_list *result;
  -    mutex_lock(&fpriv->bo_list_lock);
+    rcu_read_lock();
      result = idr_find(&fpriv->bo_list_handles, id);
-    if (result)
-        mutex_lock(&result->lock);
-    mutex_unlock(&fpriv->bo_list_lock);
+
+    if (result) {
+        if (kref_get_unless_zero(&result->refcount))
+            mutex_lock(&result->lock);
+        else
+            result = NULL;
+    }
+    rcu_read_unlock();
+
      return result;
  }
Quoted 10 lines
Quoted 8 lines

@@ -227,6 +242,7 @@ void amdgpu_bo_list_get_list(struct
amdgpu_bo_list *list,
void amdgpu_bo_list_put(struct amdgpu_bo_list *list)

  {
      mutex_unlock(&list->lock);

+ kref_put(&list->refcount, amdgpu_bo_list_release_rcu);
}
void amdgpu_bo_list_free(struct amdgpu_bo_list *list)

Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Reviewed-by
#10 lore raw
In reply to a message not in the archive

Am 16.06.2017 um 07:03 schrieb Alex Xie:

Quoted 7 lines

v2: Remove duplication of zeroing of bo list (Christian König)
Move idr_alloc function to end of ioctl (Christian König) Call kfree bo_list when amdgpu_bo_list_set return error. Combine the previous two patches into this patch. Add amdgpu_bo_list_set function prototype.

Quoted 86 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 53 ++++++++++++++++-------------
  1 file changed, 30 insertions(+), 23 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +30 -16
index a664987..5af956f 100644
@@ -35,33 +35,45 @@
  #define AMDGPU_BO_LIST_MAX_PRIORITY	32u
  #define AMDGPU_BO_LIST_NUM_BUCKETS	(AMDGPU_BO_LIST_MAX_PRIORITY + 1)
 
-static int amdgpu_bo_list_create(struct amdgpu_fpriv *fpriv,
-				 struct amdgpu_bo_list **result,
+static int amdgpu_bo_list_set(struct amdgpu_device *adev,
+				     struct drm_file *filp,
+				     struct amdgpu_bo_list *list,
+				     struct drm_amdgpu_bo_list_entry *info,
+				     unsigned num_entries);
+
+static int amdgpu_bo_list_create(struct amdgpu_device *adev,
+				 struct drm_file *filp,
+				 struct drm_amdgpu_bo_list_entry *info,
+				 unsigned num_entries,
  				 int *id)
  {
  	int r;
+	struct amdgpu_fpriv *fpriv = filp->driver_priv;
+	struct amdgpu_bo_list *list;
 
-	*result = kzalloc(sizeof(struct amdgpu_bo_list), GFP_KERNEL);
-	if (!*result)
+	list = kzalloc(sizeof(struct amdgpu_bo_list), GFP_KERNEL);
+	if (!list)
  		return -ENOMEM;
 
+	/* initialize bo list*/
+	mutex_init(&list->lock);
+
+	r = amdgpu_bo_list_set(adev, filp, list, info, num_entries);
+	if (r) {
+		kfree(list);
+		return r;
+	}
+
+	/* idr alloc should be called only after initialization of bo list. */
  	mutex_lock(&fpriv->bo_list_lock);
-	r = idr_alloc(&fpriv->bo_list_handles, *result,
-		      1, 0, GFP_KERNEL);
+	r = idr_alloc(&fpriv->bo_list_handles, list, 1, 0, GFP_KERNEL);
+	mutex_unlock(&fpriv->bo_list_lock);
  	if (r < 0) {
-		mutex_unlock(&fpriv->bo_list_lock);
-		kfree(*result);
+		kfree(list);
  		return r;
  	}
  	*id = r;
 
-	mutex_init(&(*result)->lock);
-	(*result)->num_entries = 0;
-	(*result)->array = NULL;
-
-	mutex_lock(&(*result)->lock);
-	mutex_unlock(&fpriv->bo_list_lock);
-
  	return 0;
  }
 
@@ -77,6 +89,7 @@ static void amdgpu_bo_list_destroy(struct amdgpu_fpriv *fpriv, int id)
  		mutex_unlock(&list->lock);
  		amdgpu_bo_list_free(list);
  	}
+
  	mutex_unlock(&fpriv->bo_list_lock);
  }
 
@@ -273,16 +286,10 @@ int amdgpu_bo_list_ioctl(struct drm_device *dev, void *data,
 
  	switch (args->in.operation) {
  	case AMDGPU_BO_LIST_OP_CREATE:
-		r = amdgpu_bo_list_create(fpriv, &list, &handle);
+		r = amdgpu_bo_list_create(adev, filp, info, args->in.bo_number,
+					  &handle);
  		if (r)
  			goto error_free;

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

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +0 -7
-
-		r = amdgpu_bo_list_set(adev, filp, list, info,
-					      args->in.bo_number);
-		amdgpu_bo_list_put(list);
-		if (r)
-			goto error_free;
-
  		break;
 
  	case AMDGPU_BO_LIST_OP_DESTROY:

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx