Skip to contentSkip to conversation

drm/amdgpu: off by on in amdgpu_device_attr_create_groups() error handling

PATCH Superseded Dan Carpenter 6 participants 10 messages May 20, 2020

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

PATCH 1/1 drm/amdgpu: off by on in amdgpu_device_attr_create_groups() error handling

This loop in the error handling code should start a "i - 1" and end at "i = 0". Currently it starts a "i" and ends at "i = 1". The result is that it removes one attribute that wasn't created yet, and leaks the zeroeth attribute.

Fixes 4e01847c38f7 ("drm/amdgpu: optimize amdgpu device attribute code")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +2 -3
index b75362bf0742..ee4a8e44fbeb 100644
@@ -1931,7 +1931,7 @@ static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
 					    uint32_t mask)
 {
 	int ret = 0;
-	uint32_t i = 0;
+	int i;
 
 	for (i = 0; i < counts; i++) {
 		ret = amdgpu_device_attr_create(adev, &attrs[i], mask);
@@ -1942,9 +1942,8 @@ static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
 	return 0;
 
 failed:
-	for (; i > 0; i--) {
+	while (--i >= 0)
 		amdgpu_device_attr_remove(adev, &attrs[i]);
-	}
 
 	return ret;
 }
Signature · 1 line
2.26.2
#1 lore raw

Am 20.05.20 um 14:00 schrieb Dan Carpenter:

Quoted 25 lines

This loop in the error handling code should start a "i - 1" and end at "i = 0". Currently it starts a "i" and ends at "i = 1". The result is that it removes one attribute that wasn't created yet, and leaks the zeroeth attribute.

Fixes 4e01847c38f7 ("drm/amdgpu: optimize amdgpu device attribute code")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +1 -1
index b75362bf0742..ee4a8e44fbeb 100644
@@ -1931,7 +1931,7 @@ static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
  					    uint32_t mask)
  {
  	int ret = 0;
-	uint32_t i = 0;
+	int i;
 
  	for (i = 0; i < counts; i++) {
  		ret = amdgpu_device_attr_create(adev, &attrs[i], mask);

drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev, · line 1945

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +1 -1
@@ -1942,9 +1942,8 @@ static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
  	return 0;
 
  failed:
-	for (; i > 0; i--) {
+	while (--i >= 0)

As far as I know the common idiom for this is while (i--) which even works without changing the type of i to signed.

Christian.

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +0 -1
  		amdgpu_device_attr_remove(adev, &attrs[i]);
-	}
 
  	return ret;
  }
#2 lore raw
Quoted 24 lines · Christian König

Am 20.05.20 um 14:00 schrieb Dan Carpenter:

Quoted 23 lines

This loop in the error handling code should start a "i - 1" and end at "i = 0". Currently it starts a "i" and ends at "i = 1". The result is that it removes one attribute that wasn't created yet, and leaks the zeroeth attribute.

Fixes 4e01847c38f7 ("drm/amdgpu: optimize amdgpu device attribute code")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +1 -1
index b75362bf0742..ee4a8e44fbeb 100644
@@ -1931,7 +1931,7 @@ static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
  					    uint32_t mask)
  {
  	int ret = 0;
-	uint32_t i = 0;
+	int i;
  	for (i = 0; i < counts; i++) {
  		ret = amdgpu_device_attr_create(adev, &attrs[i], mask);
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev, · line 1944

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +1 -1
@@ -1942,9 +1942,8 @@ static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
  	return 0;
  failed:
-	for (; i > 0; i--) {
+	while (--i >= 0)

As far as I know the common idiom for this is while (i--) which even works without changing the type of i to signed.

It's about 50/50, one way or the other. To me --i >= 0 seems far more readable.

I've been trying to figure out which tool tells people to make iterators unsigned so I can help them avoid it. :/ I understand how in theory iterators could go above INT_MAX but if we're going above INT_MAX then probably we should use a 64 bit type. There are very few times where 2 billion iterations is not enough but in those situations probably 4 billion is not enough either. So unsigned int iterators never or seldom solve real life bugs but they regularly cause them.

regards,
dan carpenter

#3 lore raw

PATCH v2 1/1 drm/amdgpu: off by on in amdgpu_device_attr_create_groups() error handling

This loop in the error handling code should start a "i - 1" and end at "i = 0". Currently it starts a "i" and ends at "i = 1". The result is that it removes one attribute that wasn't created yet, and leaks the zeroeth attribute.

Fixes 4e01847c38f7 ("drm/amdgpu: optimize amdgpu device attribute code")

---
v2: style change

 drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c           | 3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +1 -2
index b75362bf0742..e809534fabd4 100644
@@ -1942,9 +1942,8 @@ static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
 	return 0;
 
 failed:
-	for (; i > 0; i--) {
+	while (i--)
 		amdgpu_device_attr_remove(adev, &attrs[i]);
-	}
 
 	return ret;
 }
Reviewed-by on v2 1/1
#4 lore raw

[AMD Official Use Only - Internal Distribution Only]

thanks.

Best Regads,
Kevin
________________________________
From: Dan Carpenter <[email protected]>
Sent: Wednesday, May 20, 2020 9:08 PM
To: Deucher, Alexander <[email protected]>; Wang, Kevin(Yang) <[email protected]>
Cc: Koenig, Christian <[email protected]>; David Airlie <[email protected]>; Daniel Vetter <[email protected]>; Quan, Evan <[email protected]>; Huang, Ray <[email protected]>; Feng, Kenneth <[email protected]>; Tao, Yintian <[email protected]>; Zhang, Hawking <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>
Subject: [PATCH v2] drm/amdgpu: off by on in amdgpu_device_attr_create_groups() error handling

This loop in the error handling code should start a "i - 1" and end at "i == 0". Currently it starts a "i" and ends at "i == 1". The result is that it removes one attribute that wasn't created yet, and leaks the zeroeth attribute.

Fixes 4e01847c38f7 ("drm/amdgpu: optimize amdgpu device attribute code")

---
v2: style change

 drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c           | 3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +1 -2
index b75362bf0742..e809534fabd4 100644
@@ -1942,9 +1942,8 @@ static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
         return 0;
 
 failed:
-       for (; i > 0; i--) {
+       while (i--)
                 amdgpu_device_attr_remove(adev, &attrs[i]);
-       }
 
         return ret;
 }

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

#5 lore raw
In reply to Dan Carpenter

"off by on"

or

"off by one"

?

M

Quoted 49 lines

-----Original Message-----
From: dri-devel <[email protected]> On Behalf Of Dan Carpenter
Sent: Wednesday, May 20, 2020 9:08 AM
To: Alex Deucher <[email protected]>; Kevin Wang <[email protected]>
Cc: David Airlie <[email protected]>; [email protected]; linux- [email protected]; [email protected]; Hawking Zhang <[email protected]>; Rui Huang <[email protected]>; dri- [email protected]; Evan Quan <[email protected]>; Kenneth Feng <[email protected]>; Christian König <[email protected]>; Yintian Tao <[email protected]>
Subject: [PATCH v2] drm/amdgpu: off by on in amdgpu_device_attr_create_groups() error handling

This loop in the error handling code should start a "i - 1" and end at "i = 0". Currently it starts a "i" and ends at "i = 1". The result is that it removes one attribute that wasn't created yet, and leaks the zeroeth attribute.

Fixes 4e01847c38f7 ("drm/amdgpu: optimize amdgpu device attribute code")

---
v2: style change

drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c | 3 +--

1 files changed, 1 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +0 -0
index b75362bf0742..e809534fabd4 100644
@@ -1942,9 +1942,8 @@ static int amdgpu_device_attr_create_groups(struct
 amdgpu_device *adev,
 	return 0;
 

failed:

drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +1 -1
-	for (; i > 0; i--) {
+	while (i--)
		amdgpu_device_attr_remove(adev, &attrs[i]);
drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +0 -1
-	}
	return ret;
}

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

#6 lore raw

PATCH v3 1/1 drm/amdgpu: off by one in amdgpu_device_attr_create_groups() error handling

This loop in the error handling code should start a "i - 1" and end at "i = 0". Currently it starts a "i" and ends at "i = 1". The result is that it removes one attribute that wasn't created yet, and leaks the zeroeth attribute.

Fixes 4e01847c38f7 ("drm/amdgpu: optimize amdgpu device attribute code")

---
v2: style change
v3: Fix embarrassing typo in the subject

 drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c           | 3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +1 -2
index b75362bf0742..e809534fabd4 100644
@@ -1942,9 +1942,8 @@ static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
 	return 0;
 
 failed:
-	for (; i > 0; i--) {
+	while (i--)
 		amdgpu_device_attr_remove(adev, &attrs[i]);
-	}
 
 	return ret;
 }
Acked-by on v3 1/1
#7 lore raw

RE: [PATCH v3] drm/amdgpu: off by one in amdgpu_device_attr_create_groups() error handling

Pi0tLS0tT3JpZ2luYWwgTWVzc2FnZS0tLS0tDQo+RnJvbTogRGFuIENhcnBlbnRlciA8ZGFuLmNh cnBlbnRlckBvcmFjbGUuY29tPg0KPlNlbnQ6IFdlZG5lc2RheSwgTWF5IDIwLCAyMDIwIDExOjI2 IEFNDQo+VG86IEFsZXggRGV1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Y3QN Cj5hbWRncHVfZGV2aWNlICphZGV2LA0KPiAJcmV0dXJuIDA7DQo+DQo+IGZhaWxlZDoNCj4tCWZv ciAoOyBpID4gMDsgaS0tKSB7DQo+Kwl3aGlsZSAoaS0tKQ0KPiAJCWFtZGdwdV9kZXZpY2VfYXR0 cl9yZW1vdmUoYWRldiwgJmF0dHJzW2ldKTsNCj4tCX0NCj4NCj4gCXJldHVybiByZXQ7DQo+IH0N Cg=

maintainer Reviewed-by
#8 lore raw

Re: [PATCH v3] drm/amdgpu: off by one in amdgpu_device_attr_create_groups() error handling

Am 20.05.20 um 17:31 schrieb Ruhl, Michael J:

Quoted 20 lines
Quoted 20 lines

-----Original Message-----
From: Dan Carpenter <[email protected]>
Sent: Wednesday, May 20, 2020 11:26 AM
To: Alex Deucher <[email protected]>; Kevin Wang <[email protected]>; Ruhl, Michael J <[email protected]>
Cc: Christian König <[email protected]>; David Airlie <[email protected]>; Daniel Vetter <[email protected]>; Evan Quan <[email protected]>; Rui Huang <[email protected]>; Kenneth Feng <[email protected]>; Yintian Tao <[email protected]>; Hawking Zhang <[email protected]>; [email protected]; dri- [email protected]; [email protected]; kernel- [email protected]
Subject: [PATCH v3] drm/amdgpu: off by one in amdgpu_device_attr_create_groups() error handling

This loop in the error handling code should start a "i - 1" and end at "i = 0". Currently it starts a "i" and ends at "i = 1". The result is that it removes one attribute that wasn't created yet, and leaks the zeroeth attribute.

Quoted 8 lines
Quoted 5 lines
Fixes 4e01847c38f7 ("drm/amdgpu: optimize amdgpu device attribute code")

---
v2: style change
v3: Fix embarrassing typo in the subject

😊

Quoted 22 lines

m

Quoted 20 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c | 3 +--

1 files changed, 1 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +0 -0
index b75362bf0742..e809534fabd4 100644
@@ -1942,9 +1942,8 @@ static int amdgpu_device_attr_create_groups(struct
 amdgpu_device *adev,
 	return 0;
 

failed:

drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +1 -1
-	for (; i > 0; i--) {
+	while (i--)
		amdgpu_device_attr_remove(adev, &attrs[i]);
drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +0 -1
-	}
	return ret;
}
Applied
#9 lore raw

Re: [PATCH v3] drm/amdgpu: off by one in amdgpu_device_attr_create_groups() error handling

Applied. Thanks!

Alex

Quoted 53 lines · Christian König

Am 20.05.20 um 17:31 schrieb Ruhl, Michael J:

Quoted 15 lines
Quoted 15 lines

-----Original Message-----
From: Dan Carpenter <[email protected]>
Sent: Wednesday, May 20, 2020 11:26 AM
To: Alex Deucher <[email protected]>; Kevin Wang <[email protected]>; Ruhl, Michael J <[email protected]>
Cc: Christian König <[email protected]>; David Airlie <[email protected]>; Daniel Vetter <[email protected]>; Evan Quan <[email protected]>; Rui Huang <[email protected]>; Kenneth Feng <[email protected]>; Yintian Tao <[email protected]>; Hawking Zhang <[email protected]>; [email protected]; dri- [email protected]; [email protected]; kernel- [email protected]
Subject: [PATCH v3] drm/amdgpu: off by one in amdgpu_device_attr_create_groups() error handling

Quoted 6 lines
Quoted 6 lines

This loop in the error handling code should start a "i - 1" and end at "i == 0". Currently it starts a "i" and ends at "i == 1". The result is that it removes one attribute that wasn't created yet, and leaks the zeroeth attribute.

Fixes 4e01847c38f7 ("drm/amdgpu: optimize amdgpu device attribute

code")

Quoted 7 lines
Quoted 4 lines

---
v2: style change
v3: Fix embarrassing typo in the subject

😊

Quoted 20 lines

m

Quoted 18 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c | 3 +--

1 files changed, 1 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +0 -0
index b75362bf0742..e809534fabd4 100644
@@ -1942,9 +1942,8 @@ static int amdgpu_device_attr_create_groups(struct
 amdgpu_device *adev,
     return 0;
 

failed:

drivers/gpu/drm/amd/amdgpu/amdgpu_pm.c +1 -2
-    for (; i > 0; i--) {
+    while (i--)
             amdgpu_device_attr_remove(adev, &attrs[i]);
-    }
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
     return ret;
}

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

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