Skip to contentSkip to conversation

drm: modify drm_global_item_ref to avoid two times of writing ref->object

PATCH Superseded Huang Rui 4 participants 5 messages Sep 7, 2016 2 messages not in the archive

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Huang Rui

author
#0 lore raw

PATCH v2 1/1 drm: modify drm_global_item_ref to avoid two times of writing ref->object

In previous drm_global_item_ref, there are two times of writing ref->object if item->refcount is 0. So this patch does a minor update to put alloc and init ref firstly, and then to modify the item of glob array. Use "else" to avoid two times of writing ref->object. It can make the code logic more clearly.

---

Changes from V1 -> V2:
- Add kfree exceptional handle to avoid memory leak.
- Improve code style.

---
 drivers/gpu/drm/drm_global.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
drivers/gpu/drm/drm_global.c +13 -10
index 3d2e91c..b181e81 100644
@@ -65,30 +65,33 @@ void drm_global_release(void)
 
 int drm_global_item_ref(struct drm_global_reference *ref)
 {
-	int ret;
+	int ret = 0;
 	struct drm_global_item *item = &glob[ref->global_type];
 
 	mutex_lock(&item->mutex);
 	if (item->refcount == 0) {
-		item->object = kzalloc(ref->size, GFP_KERNEL);
-		if (unlikely(item->object == NULL)) {
+		ref->object = kzalloc(ref->size, GFP_KERNEL);
+		if (unlikely(ref->object == NULL)) {
 			ret = -ENOMEM;
-			goto out_err;
+			goto out;
 		}
-
-		ref->object = item->object;
 		ret = ref->init(ref);
 		if (unlikely(ret != 0))
 			goto out_err;
 
+		item->object = ref->object;
+	} else {
+		ref->object = item->object;
 	}
+
 	++item->refcount;
-	ref->object = item->object;
-	mutex_unlock(&item->mutex);
-	return 0;
+	goto out;
+
 out_err:
+	kfree(ref->object);
+	ref->object = NULL;
+out:
 	mutex_unlock(&item->mutex);
-	item->object = NULL;
 	return ret;
 }
 EXPORT_SYMBOL(drm_global_item_ref);
Signature · 6 lines
1.9.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#2 lore raw
In reply to a message not in the archive

Am 07.09.2016 um 07:24 schrieb Huang Rui:

Quoted 7 lines

In previous drm_global_item_ref, there are two times of writing ref->object if item->refcount is 0. So this patch does a minor update to put alloc and init ref firstly, and then to modify the item of glob array. Use "else" to avoid two times of writing ref->object. It can make the code logic more clearly.

Well when you update your patch, even when it's just fixing a small typo, please increase the version number.

That makes it much easier to track the different instances of a patch.

A few additional notes below.

Quoted 38 lines

---

Changes from V1 -> V2:
- Add kfree exceptional handle to avoid memory leak.
- Improve code style.

---
  drivers/gpu/drm/drm_global.c | 23 +++++++++++++----------
  1 file changed, 13 insertions(+), 10 deletions(-)
drivers/gpu/drm/drm_global.c +4 -6
index 3d2e91c..b181e81 100644
@@ -65,30 +65,33 @@ void drm_global_release(void)
 
  int drm_global_item_ref(struct drm_global_reference *ref)
  {
-	int ret;
+	int ret = 0;
  	struct drm_global_item *item = &glob[ref->global_type];
 
  	mutex_lock(&item->mutex);
  	if (item->refcount == 0) {
-		item->object = kzalloc(ref->size, GFP_KERNEL);
-		if (unlikely(item->object == NULL)) {
+		ref->object = kzalloc(ref->size, GFP_KERNEL);
+		if (unlikely(ref->object == NULL)) {
  			ret = -ENOMEM;
-			goto out_err;
+			goto out;
  		}
-
-		ref->object = item->object;
  		ret = ref->init(ref);
  		if (unlikely(ret != 0))
  			goto out_err;
 

drivers/gpu/drm/drm_global.c void drm_global_release(void) · line 88

Quoted 10 lines
drivers/gpu/drm/drm_global.c +5 -3
+		item->object = ref->object;
+	} else {
+		ref->object = item->object;
  	}
+
  	++item->refcount;
-	ref->object = item->object;
-	mutex_unlock(&item->mutex);
-	return 0;
+	goto out;

A goto in the not error path is a bit unusual. Since out_err is only used once couldn't you just move the code into the if and then goto to out as well?

Alternative you could just duplicate the mutex release in the non-error path.

Regards,
Christian.

drivers/gpu/drm/drm_global.c void drm_global_release(void) · line 97

Quoted 10 lines
drivers/gpu/drm/drm_global.c +4 -1
+
  out_err:
+	kfree(ref->object);
+	ref->object = NULL;
+out:
  	mutex_unlock(&item->mutex);
-	item->object = NULL;
  	return ret;
  }
  EXPORT_SYMBOL(drm_global_item_ref);

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

author
#4 lore raw
In reply to a message not in the archive
Quoted 7 lines · Christian König

Am 07.09.2016 um 07:24 schrieb Huang Rui:

Quoted 6 lines

In previous drm_global_item_ref, there are two times of writing ref->object if item->refcount is 0. So this patch does a minor update to put alloc and init ref firstly, and then to modify the item of glob array. Use "else" to avoid two times of writing ref->object. It can make the code logic more clearly.

Quoted 7 lines
Quoted 1 line

Well when you update your patch, even when it's just fixing a small typo, please increase the version number.

That makes it much easier to track the different instances of a patch.

OK.

Quoted 52 lines

A few additional notes below.

Quoted 35 lines

---

Changes from V1 -> V2:
- Add kfree exceptional handle to avoid memory leak.
- Improve code style.

---
 drivers/gpu/drm/drm_global.c | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
drivers/gpu/drm/drm_global.c +4 -6
index 3d2e91c..b181e81 100644
@@ -65,30 +65,33 @@ void drm_global_release(void)
 int drm_global_item_ref(struct drm_global_reference *ref)
 {
-	int ret;
+	int ret = 0;
 	struct drm_global_item *item = &glob[ref->global_type];
 	mutex_lock(&item->mutex);
 	if (item->refcount == 0) {
-		item->object = kzalloc(ref->size, GFP_KERNEL);
-		if (unlikely(item->object == NULL)) {
+		ref->object = kzalloc(ref->size, GFP_KERNEL);
+		if (unlikely(ref->object == NULL)) {
 			ret = -ENOMEM;
-			goto out_err;
+			goto out;
 		}
-
-		ref->object = item->object;
 		ret = ref->init(ref);
 		if (unlikely(ret != 0))
 			goto out_err;

drivers/gpu/drm/drm_global.c void drm_global_release(void) · line 85

Quoted 10 lines
drivers/gpu/drm/drm_global.c +5 -3
+		item->object = ref->object;
+	} else {
+		ref->object = item->object;
 	}
+
 	++item->refcount;
-	ref->object = item->object;
-	mutex_unlock(&item->mutex);
-	return 0;
+	goto out;

A goto in the not error path is a bit unusual. Since out_err is only used once couldn't you just move the code into the if and then goto to out as well?

Quoted 3 lines

Alternative you could just duplicate the mutex release in the non-error path.

Actually, I also have a little concern with "goto" in non-error path. But as Sean's suggestion, use "goto" can avoid a duplicate mutex release, you know.

@Sean, if you don't have concern with adding a mutex release below, I will update it in V3.

---

        ++item->refcount;
        mutex_unlock(&item->mutex);
        return 0;

out_err:

        kfree(ref->object);
        ref->object = NULL;
out:
        mutex_unlock(&item->mutex);
        return ret;

---

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

#5 lore raw

Am 07.09.2016 um 09:54 schrieb Huang Rui:

Quoted 67 lines · Christian König
Quoted 7 lines

Am 07.09.2016 um 07:24 schrieb Huang Rui:

Quoted 6 lines

In previous drm_global_item_ref, there are two times of writing ref->object if item->refcount is 0. So this patch does a minor update to put alloc and init ref firstly, and then to modify the item of glob array. Use "else" to avoid two times of writing ref->object. It can make the code logic more clearly.

Quoted 6 lines
Quoted 1 line

Well when you update your patch, even when it's just fixing a small typo, please increase the version number.

That makes it much easier to track the different instances of a patch.

OK.

Quoted 51 lines

A few additional notes below.

Quoted 35 lines

---

Changes from V1 -> V2:
- Add kfree exceptional handle to avoid memory leak.
- Improve code style.

---
  drivers/gpu/drm/drm_global.c | 23 +++++++++++++----------
  1 file changed, 13 insertions(+), 10 deletions(-)
drivers/gpu/drm/drm_global.c +4 -6
index 3d2e91c..b181e81 100644
@@ -65,30 +65,33 @@ void drm_global_release(void)
  int drm_global_item_ref(struct drm_global_reference *ref)
  {
-	int ret;
+	int ret = 0;
  	struct drm_global_item *item = &glob[ref->global_type];
  	mutex_lock(&item->mutex);
  	if (item->refcount == 0) {
-		item->object = kzalloc(ref->size, GFP_KERNEL);
-		if (unlikely(item->object == NULL)) {
+		ref->object = kzalloc(ref->size, GFP_KERNEL);
+		if (unlikely(ref->object == NULL)) {
  			ret = -ENOMEM;
-			goto out_err;
+			goto out;
  		}
-
-		ref->object = item->object;
  		ret = ref->init(ref);
  		if (unlikely(ret != 0))
  			goto out_err;

drivers/gpu/drm/drm_global.c void drm_global_release(void) · line 85

Quoted 10 lines
drivers/gpu/drm/drm_global.c +5 -3
+		item->object = ref->object;
+	} else {
+		ref->object = item->object;
  	}
+
  	++item->refcount;
-	ref->object = item->object;
-	mutex_unlock(&item->mutex);
-	return 0;
+	goto out;

A goto in the not error path is a bit unusual. Since out_err is only used once couldn't you just move the code into the if and then goto to out as well?

Quoted 6 lines
Quoted 3 lines

Alternative you could just duplicate the mutex release in the non-error path.

Actually, I also have a little concern with "goto" in non-error path. But as Sean's suggestion, use "goto" can avoid a duplicate mutex release, you know.

I would certainly prefer the duplicate mutex release.

Quoted 5 lines

@Sean, if you don't have concern with adding a mutex release below, I will update it in V3.

---

Quoted 8 lines
         ++item->refcount;
         mutex_unlock(&item->mutex);
         return 0;

out_err:

         kfree(ref->object);
         ref->object = NULL;

out:

BTW: I would name those labels error_free and error_unlock to make clear that they are for error handling and what they are supposed to do.

Regards,
Christian.

Quoted 7 lines
         mutex_unlock(&item->mutex);
         return ret;

---

Thanks,
Rui

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

#6 lore raw
Quoted 3 lines · Christian

Am 07.09.2016 um 09:54 schrieb Huang Rui:

Quoted 2 lines · Christian K?nig
Quoted 1 line

Am 07.09.2016 um 07:24 schrieb Huang Rui:

snip.

Quoted 27 lines
Quoted 9 lines
Quoted 9 lines

drivers/gpu/drm/drm_global.c void drm_global_release(void) · line 88

Quoted 5 lines

++item->refcount;

drivers/gpu/drm/drm_global.c +1 -3
-	ref->object = item->object;
-	mutex_unlock(&item->mutex);
-	return 0;
+	goto out;

A goto in the not error path is a bit unusual. Since out_err is only used once couldn't you just move the code into the if and then goto to out as well?

Quoted 6 lines
Quoted 3 lines

Alternative you could just duplicate the mutex release in the non-error path.

Actually, I also have a little concern with "goto" in non-error path. But as Sean's suggestion, use "goto" can avoid a duplicate mutex release, you know.

I would certainly prefer the duplicate mutex release.

Quoted 9 lines

@Sean, if you don't have concern with adding a mutex release below, I will update it in V3.

---

         ++item->refcount;
         mutex_unlock(&item->mutex);
         return 0;
Quoted 8 lines
Quoted 4 lines

out_err:

         kfree(ref->object);
         ref->object = NULL;

out:

BTW: I would name those labels error_free and error_unlock to make clear that they are for error handling and what they are supposed to do.

OK, will rename the label in V3.

Thanks,
Rui
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel