Skip to contentSkip to conversation

drm/amdgpu: Add double-sscanf but invert

PATCH In review Luben Tuikov 2 participants 4 messages Apr 14, 2021

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

PATCH 1/1 drm/amdgpu: Add double-sscanf but invert

Add back the double-sscanf so that both decimal and hexadecimal values could be read in, but this time invert the scan so that hexadecimal format with a leading 0x is tried first, and if that fails, then try decimal format.

Also use a logical-AND instead of nesting double if-conditional.

See commit "drm/amdgpu: Fix a bug for input with double sscanf"

Cc Alexander Deucher <[email protected]>
Cc John Clements <[email protected]>
Cc Hawking Zhang <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +5 -2
index 44dfb3613e37..38a691a3b600 100644
@@ -229,7 +229,8 @@ static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
 
 	if (op != -1) {
 		if (op == 3) {
-			if (sscanf(str, "%*s %llx", &address) != 1)
+			if (sscanf(str, "%*s 0x%llx", &address) != 1 &&
+			    sscanf(str, "%*s %llu", &address) != 1)
 				return -EINVAL;
 
 			data->op = op;
@@ -253,7 +254,9 @@ static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
 		data->op = op;
 
 		if (op == 2) {
-			if (sscanf(str, "%*s %*s %*s %x %llx %llx",
+			if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
+				   &sub_block, &address, &value) != 3 &&
+			    sscanf(str, "%*s %*s %*s %u %llu %llu",
 				   &sub_block, &address, &value) != 3)
 				return -EINVAL;
 			data->head.sub_block_index = sub_block;
Signature · 6 lines
2.31.0.97.g1424303384

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

PATCH 1/1 drm/amdgpu: Add double-sscanf but invert

Add back the double-sscanf so that both decimal and hexadecimal values could be read in, but this time invert the scan so that hexadecimal format with a leading 0x is tried first, and if that fails, then try decimal format.

Also use a logical-AND instead of nesting double if-conditional.

See commit "drm/amdgpu: Fix a bug for input with double sscanf"

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +5 -2
index 44dfb3613e37..38a691a3b600 100644
@@ -229,7 +229,8 @@ static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
 
 	if (op != -1) {
 		if (op == 3) {
-			if (sscanf(str, "%*s %llx", &address) != 1)
+			if (sscanf(str, "%*s 0x%llx", &address) != 1 &&
+			    sscanf(str, "%*s %llu", &address) != 1)
 				return -EINVAL;
 
 			data->op = op;
@@ -253,7 +254,9 @@ static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
 		data->op = op;
 
 		if (op == 2) {
-			if (sscanf(str, "%*s %*s %*s %x %llx %llx",
+			if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
+				   &sub_block, &address, &value) != 3 &&
+			    sscanf(str, "%*s %*s %*s %u %llu %llu",
 				   &sub_block, &address, &value) != 3)
 				return -EINVAL;
 			data->head.sub_block_index = sub_block;
Signature · 6 lines
2.31.0.97.g1424303384

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#2 lore raw
In reply to Luben Tuikov

From the sscanf manpage:

       i      Matches  an optionally signed integer; the next pointer must be
              a pointer to int.  The integer is read in base 16 if it begins
              with  0x  or  0X, in base 8 if it begins with 0, and in base 10
              otherwise.  Only characters that correspond  to  the base  are
              used.

So a single sscanf("%*s %i", ...) would to the job. The only problem is that accepts signed integers, not unsigned. I'm not sure if that's a problem in this use case.

Regards,
  Felix

Am 2021-04-14 um 11:21 a.m. schrieb Luben Tuikov:

Quoted 34 lines

Add back the double-sscanf so that both decimal and hexadecimal values could be read in, but this time invert the scan so that hexadecimal format with a leading 0x is tried first, and if that fails, then try decimal format.

Also use a logical-AND instead of nesting double if-conditional.

See commit "drm/amdgpu: Fix a bug for input with double sscanf"

Cc Alexander Deucher <[email protected]>
Cc John Clements <[email protected]>
Cc Hawking Zhang <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +2 -1
index 44dfb3613e37..38a691a3b600 100644
@@ -229,7 +229,8 @@ static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
 
 	if (op != -1) {
 		if (op == 3) {
-			if (sscanf(str, "%*s %llx", &address) != 1)
+			if (sscanf(str, "%*s 0x%llx", &address) != 1 &&
+			    sscanf(str, "%*s %llu", &address) != 1)
 				return -EINVAL;
 
 			data->op = op;

drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f, · line 262

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +3 -1
@@ -253,7 +254,9 @@ static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
 		data->op = op;
 
 		if (op == 2) {
-			if (sscanf(str, "%*s %*s %*s %x %llx %llx",
+			if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
+				   &sub_block, &address, &value) != 3 &&
+			    sscanf(str, "%*s %*s %*s %u %llu %llu",
 				   &sub_block, &address, &value) != 3)
 				return -EINVAL;
 			data->head.sub_block_index = sub_block;

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

#3 lore raw

Yeah, %i would do, and certainly more elegant, except for the signed input. Not sure if that's a problem, either, thus the two sscanf...

Regards,
Luben

Quoted 56 lines · Felix Kuehling

From the sscanf manpage:

       i      Matches  an optionally signed integer; the next pointer must be
              a pointer to int.  The integer is read in base 16 if it begins
              with  0x  or  0X, in base 8 if it begins with 0, and in base 10
              otherwise.  Only characters that correspond  to  the base  are
              used.

So a single sscanf("%*s %i", ...) would to the job. The only problem is that accepts signed integers, not unsigned. I'm not sure if that's a problem in this use case.

Regards,
  Felix

Am 2021-04-14 um 11:21 a.m. schrieb Luben Tuikov:

Quoted 36 lines

Add back the double-sscanf so that both decimal and hexadecimal values could be read in, but this time invert the scan so that hexadecimal format with a leading 0x is tried first, and if that fails, then try decimal format.

Also use a logical-AND instead of nesting double if-conditional.

See commit "drm/amdgpu: Fix a bug for input with double sscanf"

Cc Alexander Deucher <[email protected]>
Cc John Clements <[email protected]>
Cc Hawking Zhang <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +2 -1
index 44dfb3613e37..38a691a3b600 100644
@@ -229,7 +229,8 @@ static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
 
 	if (op != -1) {
 		if (op == 3) {
-			if (sscanf(str, "%*s %llx", &address) != 1)
+			if (sscanf(str, "%*s 0x%llx", &address) != 1 &&
+			    sscanf(str, "%*s %llu", &address) != 1)
 				return -EINVAL;
 
 			data->op = op;
@@ -253,7 +254,9 @@ static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f,
 		data->op = op;
 
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c static int amdgpu_ras_debugfs_ctrl_parse_data(struct file *f, · line 262

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +3 -1
 		if (op == 2) {
-			if (sscanf(str, "%*s %*s %*s %x %llx %llx",
+			if (sscanf(str, "%*s %*s %*s 0x%x 0x%llx 0x%llx",
+				   &sub_block, &address, &value) != 3 &&
+			    sscanf(str, "%*s %*s %*s %u %llu %llu",
 				   &sub_block, &address, &value) != 3)
 				return -EINVAL;
 			data->head.sub_block_index = sub_block;

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