Skip to contentSkip to conversation

drm/amdgpu: Fix uninitialized return value

PATCH Superseded Lijo Lazar 4 participants 7 messages Nov 27, 2023 – Nov 28, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Lijo Lazar

author
#0 lore raw

PATCH 1/1 drm/amdgpu: Fix uninitialized return value

The return value is uniinitialized if ras context is NULL.

Fixes 0f4c8faa043c (drm/amdgpu: Move mca debug mode decision to ras)
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -1
index 1a8668a63e67..f6b47ebce9d6 100644
@@ -3410,7 +3410,7 @@ int amdgpu_ras_reset_gpu(struct amdgpu_device *adev)
 int amdgpu_ras_set_mca_debug_mode(struct amdgpu_device *adev, bool enable)
 {
 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
-	int ret;
+	int ret = 0;
 
 	if (con) {
 		ret = amdgpu_mca_smu_set_debug_mode(adev, enable);
Signature · 1 line
2.25.1
#1 lore raw

Am 27.11.23 um 19:29 schrieb Lijo Lazar:

Quoted 14 lines

The return value is uniinitialized if ras context is NULL.

Fixes 0f4c8faa043c (drm/amdgpu: Move mca debug mode decision to ras)
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +0 -0
index 1a8668a63e67..f6b47ebce9d6 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c int amdgpu_ras_reset_gpu(struct amdgpu_device *adev) · line 3413

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -1
@@ -3410,7 +3410,7 @@ int amdgpu_ras_reset_gpu(struct amdgpu_device *adev)
  int amdgpu_ras_set_mca_debug_mode(struct amdgpu_device *adev, bool enable)
  {
  	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
-	int ret;
+	int ret = 0;

That's usually considered very bad coding style and complained about by automated checkers.

Instead explicitly set the return value in the code paths not actually setting it.

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +0 -0
  	if (con) {
  		ret = amdgpu_mca_smu_set_debug_mode(adev, enable);
Reviewed-by
#2 lore raw
Quoted 25 lines · Christian König

Am 27.11.23 um 19:29 schrieb Lijo Lazar:

Quoted 14 lines

The return value is uniinitialized if ras context is NULL.

Fixes 0f4c8faa043c (drm/amdgpu: Move mca debug mode decision to ras)
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +0 -0
index 1a8668a63e67..f6b47ebce9d6 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c int amdgpu_ras_reset_gpu(struct amdgpu_device *adev) · line 3413

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -1
@@ -3410,7 +3410,7 @@ int amdgpu_ras_reset_gpu(struct amdgpu_device *adev)
  int amdgpu_ras_set_mca_debug_mode(struct amdgpu_device *adev, bool enable)
  {
      struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
-     int ret;
+     int ret = 0;

That's usually considered very bad coding style and complained about by automated checkers.

Quoted 2 lines

Instead explicitly set the return value in the code paths not actually setting it.

In this case, the function is so short, I think it makes things less readable to do that.

Quoted 8 lines

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +0 -0
      if (con) {
              ret = amdgpu_mca_smu_set_debug_mode(adev, enable);
#3 lore raw

Am 27.11.23 um 22:55 schrieb Alex Deucher:

Quoted 25 lines
Quoted 23 lines · Christian König

Am 27.11.23 um 19:29 schrieb Lijo Lazar:

Quoted 14 lines

The return value is uniinitialized if ras context is NULL.

Fixes 0f4c8faa043c (drm/amdgpu: Move mca debug mode decision to ras)
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +0 -0
index 1a8668a63e67..f6b47ebce9d6 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c int amdgpu_ras_reset_gpu(struct amdgpu_device *adev) · line 3413

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -1
@@ -3410,7 +3410,7 @@ int amdgpu_ras_reset_gpu(struct amdgpu_device *adev)
   int amdgpu_ras_set_mca_debug_mode(struct amdgpu_device *adev, bool enable)
   {
       struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
-     int ret;
+     int ret = 0;

That's usually considered very bad coding style and complained about by automated checkers.

Quoted 4 lines
Quoted 2 lines

Instead explicitly set the return value in the code paths not actually setting it.

In this case, the function is so short, I think it makes things less readable to do that.

Yeah, indeed but that doesn't help us with the coding style checkers.

We could do something like this instead:

if (!con)
    return 0;

ret = amdgpu_mca_smu_set_debug_mode(adev, enable);
...

Regards,
Christian.

Quoted 8 lines
Quoted 5 lines

Regards,
Christian.

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +0 -0
       if (con) {
               ret = amdgpu_mca_smu_set_debug_mode(adev, enable);
#4 lore raw
Quoted 27 lines · Christian König

Am 27.11.23 um 22:55 schrieb Alex Deucher:

Quoted 26 lines
Quoted 24 lines · Christian König

Am 27.11.23 um 19:29 schrieb Lijo Lazar:

Quoted 14 lines

The return value is uniinitialized if ras context is NULL.

Fixes 0f4c8faa043c (drm/amdgpu: Move mca debug mode decision to ras)

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 2 +-

   1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +0 -0
index 1a8668a63e67..f6b47ebce9d6 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c int amdgpu_ras_reset_gpu(struct amdgpu_device · line 3414

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -1
@@ -3410,7 +3410,7 @@ int amdgpu_ras_reset_gpu(struct amdgpu_device 
 *adev)
    int amdgpu_ras_set_mca_debug_mode(struct amdgpu_device *adev, bool enable)
    {
        struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
-     int ret;
+     int ret = 0;

That's usually considered very bad coding style and complained about by automated checkers.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Instead explicitly set the return value in the code paths not actually setting it.

In this case, the function is so short, I think it makes things less readable to do that.

Yeah, indeed but that doesn't help us with the coding style checkers.

Are these checkers for real? I see many instances of variable initialization including in core kernel code (ex: workqueue) code.

Thanks

Lijo

Quoted 21 lines

We could do something like this instead:

if (!con)
    return 0;

ret = amdgpu_mca_smu_set_debug_mode(adev, enable);
...

Regards,
Christian.

Quoted 8 lines
Quoted 5 lines

Regards,
Christian.

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +0 -0
        if (con) {
                ret = amdgpu_mca_smu_set_debug_mode(adev, enable);
#5 lore raw

Am 28.11.23 um 10:49 schrieb Lazar, Lijo:

Quoted 29 lines · Christian König
Quoted 27 lines

Am 27.11.23 um 22:55 schrieb Alex Deucher:

Quoted 26 lines
Quoted 24 lines · Christian König

Am 27.11.23 um 19:29 schrieb Lijo Lazar:

Quoted 14 lines

The return value is uniinitialized if ras context is NULL.

Fixes 0f4c8faa043c (drm/amdgpu: Move mca debug mode decision to ras)

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 2 +-

   1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +0 -0
index 1a8668a63e67..f6b47ebce9d6 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c int amdgpu_ras_reset_gpu(struct · line 3414

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -1
@@ -3410,7 +3410,7 @@ int amdgpu_ras_reset_gpu(struct 
 amdgpu_device *adev)
    int amdgpu_ras_set_mca_debug_mode(struct amdgpu_device *adev, bool enable)
    {
        struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
-     int ret;
+     int ret = 0;

That's usually considered very bad coding style and complained about by automated checkers.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Instead explicitly set the return value in the code paths not actually setting it.

In this case, the function is so short, I think it makes things less readable to do that.

Yeah, indeed but that doesn't help us with the coding style checkers.

Are these checkers for real? I see many instances of variable initialization including in core kernel code (ex: workqueue) code.

Yes, I've got multiple complains about that already.

What people basically seem to do is to search for patterns like "int ret = 0;... ret = whatever();.. return ret;" with cocci.

This then results in a note that an initialization isn't necessary and should be avoided.

Same for things like return after else, e.g. when you have something like this "if (...) { ret = whatever(); if (ret) return ret; } else { ... ret = 0;} return ret;".

Regards,
Christian.

Quoted 26 lines

Thanks

Lijo

Quoted 21 lines

We could do something like this instead:

if (!con)
    return 0;

ret = amdgpu_mca_smu_set_debug_mode(adev, enable);
...

Regards,
Christian.

Quoted 8 lines
Quoted 5 lines

Regards,
Christian.

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +0 -0
        if (con) {
                ret = amdgpu_mca_smu_set_debug_mode(adev, enable);
#6 lore raw
Quoted 37 lines · Christian König

Am 28.11.23 um 10:49 schrieb Lazar, Lijo:

Quoted 36 lines · Christian König
Quoted 33 lines

Am 27.11.23 um 22:55 schrieb Alex Deucher:

Quoted 26 lines
Quoted 24 lines · Christian König

Am 27.11.23 um 19:29 schrieb Lijo Lazar:

Quoted 14 lines

The return value is uniinitialized if ras context is NULL.

Fixes 0f4c8faa043c (drm/amdgpu: Move mca debug mode decision to ras)

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c | 2 +-

   1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +0 -0
index 1a8668a63e67..f6b47ebce9d6 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c int amdgpu_ras_reset_gpu(struct · line 3414

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -1
@@ -3410,7 +3410,7 @@ int amdgpu_ras_reset_gpu(struct 
 amdgpu_device *adev)
    int amdgpu_ras_set_mca_debug_mode(struct amdgpu_device *adev, bool enable)
    {
        struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
-     int ret;
+     int ret = 0;

That's usually considered very bad coding style and complained about by automated checkers.

Quoted 4 lines
Quoted 2 lines

Instead explicitly set the return value in the code paths not actually setting it.

In this case, the function is so short, I think it makes things less readable to do that.

Yeah, indeed but that doesn't help us with the coding style checkers.

Quoted 7 lines
Quoted 1 line

Are these checkers for real? I see many instances of variable initialization including in core kernel code (ex: workqueue) code.

Yes, I've got multiple complains about that already.

What people basically seem to do is to search for patterns like "int ret = 0;... ret = whatever();.. return ret;" with cocci.

This then results in a note that an initialization isn't necessary and should be avoided.

Isn't that the opposite of defensive programming? If you write your kernel code in Rust, all your local variables will be implicitly initialized. In C you have to do it yourself. And the compiler is notoriously inconsistent warning about uninitialized variables.

Regards,
  Felix

Quoted 33 lines

Same for things like return after else, e.g. when you have something like this "if (...) { ret = whatever(); if (ret) return ret; } else { ... ret = 0;} return ret;".

Regards,
Christian.

Quoted 26 lines

Thanks

Lijo

Quoted 21 lines

We could do something like this instead:

if (!con)
    return 0;

ret = amdgpu_mca_smu_set_debug_mode(adev, enable);
...

Regards,
Christian.

Quoted 8 lines
Quoted 5 lines

Regards,
Christian.

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +0 -0
        if (con) {
                ret = amdgpu_mca_smu_set_debug_mode(adev, enable);