Skip to contentSkip to conversation

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

PATCH In review Huang Rui 4 participants 7 messages Sep 8, 2016

Subsystems (2)
lore
Share & tools
lore

Huang Rui

author
#0 lore raw

PATCH v3 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 V2 -> V3:
- Use duplicate mutex release to avoid "goto" in non-error patch.
- Rename error label

---
 drivers/gpu/drm/drm_global.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/gpu/drm/drm_global.c +14 -10
index 3d2e91c..b404287 100644
@@ -65,30 +65,34 @@ 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 error_unlock;
 		}
-
-		ref->object = item->object;
 		ret = ref->init(ref);
 		if (unlikely(ret != 0))
-			goto out_err;
+			goto error_free;
 
+		item->object = ref->object;
+	} else {
+		ref->object = item->object;
 	}
+
 	++item->refcount;
-	ref->object = item->object;
 	mutex_unlock(&item->mutex);
 	return 0;
-out_err:
+
+error_free:
+	kfree(ref->object);
+	ref->object = NULL;
+error_unlock:
 	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
#1 lore raw
Quoted 24 lines · Huang Rui

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 V2 -> V3:
- Use duplicate mutex release to avoid "goto" in non-error patch.
- Rename error label

---
 drivers/gpu/drm/drm_global.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/gpu/drm/drm_global.c +0 -0
index 3d2e91c..b404287 100644
@@ -65,30 +65,34 @@ void drm_global_release(void)
 
 int drm_global_item_ref(struct drm_global_reference *ref)

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

Quoted 10 lines
drivers/gpu/drm/drm_global.c +2 -3
 {
-	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);

So the item refcount is 0, we operate on ref, whereas previous we inspected item and operated on item. Not an improvement. -Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw
Quoted 30 lines · Chris Wilson
Quoted 29 lines · Huang Rui

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 V2 -> V3:
- Use duplicate mutex release to avoid "goto" in non-error patch.
- Rename error label

---
 drivers/gpu/drm/drm_global.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/gpu/drm/drm_global.c +1 -1
index 3d2e91c..b404287 100644
@@ -65,30 +65,34 @@ 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];
 
Quoted 8 lines

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

Quoted 5 lines
drivers/gpu/drm/drm_global.c +1 -2
 	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);

So the item refcount is 0, we operate on ref, whereas previous we inspected item and operated on item. Not an improvement.

Hmm, when item refcount is 0, we operate on ref to create object and init it for item, so although item->object and ref->object here should point the same thing, but we should alloc and init ref firstly and pass the ref->object address to item (item actually points a static area). This make the code logic more clearly and readable. So I updated it to "ref" here. :-)

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

Reviewed-by on v3 1/1
#6 lore raw
In reply to Huang Rui

Am 08.09.2016 um 04:07 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.

Quoted 49 lines

---

Changes from V2 -> V3:
- Use duplicate mutex release to avoid "goto" in non-error patch.
- Rename error label

---
  drivers/gpu/drm/drm_global.c | 24 ++++++++++++++----------
  1 file changed, 14 insertions(+), 10 deletions(-)
drivers/gpu/drm/drm_global.c +9 -9
index 3d2e91c..b404287 100644
@@ -65,30 +65,34 @@ 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 error_unlock;
  		}
-
-		ref->object = item->object;
  		ret = ref->init(ref);
  		if (unlikely(ret != 0))
-			goto out_err;
+			goto error_free;
 
+		item->object = ref->object;
+	} else {
+		ref->object = item->object;
  	}
+
  	++item->refcount;
-	ref->object = item->object;
  	mutex_unlock(&item->mutex);
  	return 0;
-out_err:

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

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

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

#3 lore raw
In reply to Huang Rui
Quoted 40 lines · Huang Rui
Quoted 30 lines · Chris Wilson
Quoted 29 lines · Huang Rui

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 V2 -> V3:
- Use duplicate mutex release to avoid "goto" in non-error patch.
- Rename error label

---
 drivers/gpu/drm/drm_global.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/gpu/drm/drm_global.c +1 -1
index 3d2e91c..b404287 100644
@@ -65,30 +65,34 @@ 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];
 
Quoted 8 lines

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

Quoted 5 lines
drivers/gpu/drm/drm_global.c +1 -2
 	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);

So the item refcount is 0, we operate on ref, whereas previous we inspected item and operated on item. Not an improvement.

Quoted 6 lines

Hmm, when item refcount is 0, we operate on ref to create object and init it for item, so although item->object and ref->object here should point the same thing, but we should alloc and init ref firstly and pass the ref->object address to item (item actually points a static area). This make the code logic more clearly and readable. So I updated it to "ref" here. :-)

The object is owned by item. ref is just a pointer to it. -Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#4 lore raw

Am 08.09.2016 um 09:35 schrieb Chris Wilson:

Quoted 39 lines · Huang Rui
Quoted 38 lines · Chris Wilson
Quoted 37 lines · Huang Rui
Quoted 24 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.

---

Changes from V2 -> V3:
- Use duplicate mutex release to avoid "goto" in non-error patch.
- Rename error label

---
  drivers/gpu/drm/drm_global.c | 24 ++++++++++++++----------
  1 file changed, 14 insertions(+), 10 deletions(-)
drivers/gpu/drm/drm_global.c +0 -0
index 3d2e91c..b404287 100644
@@ -65,30 +65,34 @@ void drm_global_release(void)
 
  int drm_global_item_ref(struct drm_global_reference *ref)

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

Quoted 10 lines
drivers/gpu/drm/drm_global.c +2 -3
  {
-	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);

So the item refcount is 0, we operate on ref, whereas previous we inspected item and operated on item. Not an improvement.

Quoted 7 lines
Quoted 6 lines

Hmm, when item refcount is 0, we operate on ref to create object and init it for item, so although item->object and ref->object here should point the same thing, but we should alloc and init ref firstly and pass the ref->object address to item (item actually points a static area). This make the code logic more clearly and readable. So I updated it to "ref" here. :-)

The object is owned by item. ref is just a pointer to it.

Yeah, so what? We initialized ref->item when the refcount was 0 before as well.

Why not create the reference first and initialize the item later on?

Regards,
Christian.

Quoted 2 lines

-Chris

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

#5 lore raw
Quoted 44 lines · Christian König

Am 08.09.2016 um 09:35 schrieb Chris Wilson:

Quoted 43 lines · Huang Rui
Quoted 42 lines · Chris Wilson
Quoted 27 lines · Huang Rui
Quoted 26 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.

---

Changes from V2 -> V3:
- Use duplicate mutex release to avoid "goto" in non-error patch.
- Rename error label

---
 drivers/gpu/drm/drm_global.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/gpu/drm/drm_global.c +1 -1
index 3d2e91c..b404287 100644
@@ -65,30 +65,34 @@ void drm_global_release(void)
 int drm_global_item_ref(struct drm_global_reference *ref)
 {
-	int ret;
+	int ret = 0;
Quoted 8 lines

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

Quoted 6 lines
drivers/gpu/drm/drm_global.c +1 -2
 	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);

So the item refcount is 0, we operate on ref, whereas previous we inspected item and operated on item. Not an improvement.

Hmm, when item refcount is 0, we operate on ref to create object and init it for item, so although item->object and ref->object here should point the same thing, but we should alloc and init ref firstly and pass the ref->object address to item (item actually points a static area). This make the code logic more clearly and readable. So I updated it to "ref" here. :-)

Quoted 6 lines
Quoted 1 line

The object is owned by item. ref is just a pointer to it.

Yeah, so what? We initialized ref->item when the refcount was 0 before as well.

Why not create the reference first and initialize the item later on?

More to the point, there is a genuine bug in the code. It happens to be fixed by this patch, but not purported to be. There is also a leak of item->object, and the globals conflict between drivers. If radeon instantiates the DRM_GLOBAL_TTM_MEM, nouveau bumps the reference and radeon subsequently unloaded, when nouveau unloads it will crash due to executing a random address. As a singleton class for ttm, it could do with a revamp.
-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel