scsi: gdth: Only call dma_free_coherent when buf is not NULL in ioc_general
PATCH 1/1 scsi: gdth: Only call dma_free_coherent when buf is not NULL in ioc_general
When building with -Wsometimes-uninitialized, Clang warns:
drivers/scsi/gdth.c:3662:6: warning: variable 'paddr' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
Don't attempt to call dma_free_coherent when buf is NULL (meaning that we never called dma_alloc_coherent and initialized paddr), which avoids this warning.
--- drivers/scsi/gdth.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/scsi/gdth.c +3 -2
@@ -3697,8 +3697,9 @@ static int ioc_general(void __user *arg, char *cmnd) rval =0 ; out_free_buf: -dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, buf, - paddr); +if (buf) +dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, + buf, paddr);return rval; }
Signature · 1 line
2.21.0
Quoted 34 lines · Nathan Chancellor
When building with -Wsometimes-uninitialized, Clang warns:
drivers/scsi/gdth.c:3662:6: warning: variable 'paddr' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]Don't attempt to call
dma_free_coherentwhen buf is NULL (meaning that we never calleddma_alloc_coherentand initialized paddr), which avoids this warning.Signed-off-by Nathan Chancellor <[email protected]>--- drivers/scsi/gdth.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/scsi/gdth.c +3
-3@@ -3697,8 +3697,9 @@ static int ioc_general(void __user *arg, char *cmnd) rval =0 ; out_free_buf: -dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, buf, - paddr); +if (buf) +dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, + buf, paddr);return rval; } --
Quoted 2 lines
2.21.0
Alternatively, paddr is a dma_addr_t defined in include/linux/types.h:
#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
typedef u64 dma_addr_t;
#else
typedef u32 dma_addr_t;
#endif
Just initializing it to zero might be simpler than complicating the control flow of this function further. Thoughts?
drivers/scsi/gdth.c +1 -1
@@ -3643,7 +3643,7 @@ static int ioc_general(void __user *arg, char *cmnd)gdth_ioctl_general gen;gdth_ha_str *ha;char *buf =NULL ; -dma_addr_t paddr; +dma_addr_t paddr =0U ;int rval;if (copy_from_user (&gen, arg,sizeof (gdth_ioctl_general)))
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 66 lines · Nick Desaulniers
Quoted 34 lines · Nathan Chancellor
When building with -Wsometimes-uninitialized, Clang warns:
drivers/scsi/gdth.c:3662:6: warning: variable 'paddr' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]Don't attempt to call
dma_free_coherentwhen buf is NULL (meaning that we never calleddma_alloc_coherentand initialized paddr), which avoids this warning.Signed-off-by Nathan Chancellor <[email protected]>--- drivers/scsi/gdth.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/scsi/gdth.c +3
-3@@ -3697,8 +3697,9 @@ static int ioc_general(void __user *arg, char *cmnd) rval =0 ; out_free_buf: -dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, buf, - paddr); +if (buf) +dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, + buf, paddr);return rval; } --Quoted 2 lines
2.21.0
Alternatively, paddr is a
dma_addr_tdefined ininclude/linux/types.h:#ifdef
CONFIG_ARCH_DMA_ADDR_T_64BIT
typedef u64dma_addr_t;
#else
typedef u32dma_addr_t;
#endifJust initializing it to zero might be simpler than complicating the control flow of this function further. Thoughts?
drivers/scsi/gdth.c +1
-1@@ -3643,7 +3643,7 @@ static int ioc_general(void __user *arg, char *cmnd)gdth_ioctl_general gen;gdth_ha_str *ha;char *buf =NULL ; -dma_addr_t paddr; +dma_addr_t paddr =0U ;int rval;if (copy_from_user (&gen, arg,sizeof (gdth_ioctl_general)))Signature · 2 lines
Thanks, ~Nick Desaulniers
I suppose it depends on if it's okay to call dma_free_coherent without dma_alloc_coherent. I did a scan of the tree before sending this out and pretty much all sites that have error handling check that the third parameter is not NULL before calling it.
I should have added Christoph to this thread when I initially sent it out since commit 9f475ebff8e4 ("scsi: gdth: refactor ioc_general") introduced this. Done now.
Cheers,
Nathan
Arnd Bergmann
Reviewed-byQuoted 26 lines · Nick Desaulniers' via Clang Built Linux
Quoted 23 lines · Nathan Chancellor
When building with -Wsometimes-uninitialized, Clang warns:
drivers/scsi/gdth.c:3662:6: warning: variable 'paddr' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]Don't attempt to call
dma_free_coherentwhen buf is NULL (meaning that we never calleddma_alloc_coherentand initialized paddr), which avoids this warning.Signed-off-by Nathan Chancellor <[email protected]>--- drivers/scsi/gdth.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/scsi/gdth.c +0
-0@@ -3697,8 +3697,9 @@ static int ioc_general(void __user *arg, char *cmnd)
Quoted 9 lines
drivers/scsi/gdth.c static int ioc_general(void __user *arg, char *cmnd) · line 3704
Quoted 9 lines
drivers/scsi/gdth.c +3
-2rval =0 ; out_free_buf: -dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, buf, - paddr); +if (buf) +dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, + buf, paddr);return rval; }
I came up with a different fix for this, but I think yours is better
For reference, this was my version:
drivers/scsi/gdth.c +2 -2
@@ -3697,8 +3697,9 @@ static int ioc_general(void __user *arg, char *cmnd) rval =0 ; out_free_buf: -dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, buf, - paddr); +if (gen.data_len + gen.sense_len >0 ) + dma_free_coherent(&ha->pdev->dev, gen.data_len +
gen.sense_len, buf,
+ paddr);
return rval;}
Quoted 10 lines
Alternatively, paddr is a
dma_addr_tdefined ininclude/linux/types.h:#ifdef
CONFIG_ARCH_DMA_ADDR_T_64BIT
typedef u64dma_addr_t;
#else
typedef u32dma_addr_t;
#endifJust initializing it to zero might be simpler than complicating the control flow of this function further. Thoughts?
No, blindly shutting up warnings is almost never the right solution, even when they are false positives. The code might change in the future and the bogus initialization would then prevent the compiler from warning about a new bug.
Arnd
Quoted 40 lines · Arnd Bergmann
Quoted 26 lines · Nick Desaulniers' via Clang Built Linux
Quoted 23 lines · Nathan Chancellor
When building with -Wsometimes-uninitialized, Clang warns:
drivers/scsi/gdth.c:3662:6: warning: variable 'paddr' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]Don't attempt to call
dma_free_coherentwhen buf is NULL (meaning that we never calleddma_alloc_coherentand initialized paddr), which avoids this warning.Signed-off-by Nathan Chancellor <[email protected]>--- drivers/scsi/gdth.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/scsi/gdth.c +0
-0@@ -3697,8 +3697,9 @@ static int ioc_general(void __user *arg, char *cmnd)Quoted 9 lines
drivers/scsi/gdth.c static int ioc_general(void __user *arg, char *cmnd) · line 3704
Quoted 9 lines
drivers/scsi/gdth.c +3
-2rval =0 ; out_free_buf: -dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, buf, - paddr); +if (buf) +dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, + buf, paddr);return rval; }I came up with a different fix for this, but I think yours is better
Quoted 3 lines
Reviewed-by Arnd Bergmann <[email protected]>For reference, this was my version:
This is also what I had initially but I felt this was more future proof and matches how the rest of the tree handles calls to dma_free_coherent.
Thanks for the review!
Nathan
Quoted 36 lines
drivers/scsi/gdth.c +3
-2@@ -3697,8 +3697,9 @@ static int ioc_general(void __user *arg, char *cmnd) rval =0 ; out_free_buf: -dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, buf, - paddr); +if (gen.data_len + gen.sense_len >0 ) +dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, buf, + paddr);return rval; }Quoted 4 lines
Alternatively, paddr is a
dma_addr_tdefined ininclude/linux/types.h:Quoted 8 lines
#ifdef
CONFIG_ARCH_DMA_ADDR_T_64BIT
typedef u64dma_addr_t;
#else
typedef u32dma_addr_t;
#endifJust initializing it to zero might be simpler than complicating the control flow of this function further. Thoughts?
No, blindly shutting up warnings is almost never the right solution, even when they are false positives. The code might change in the future and the bogus initialization would then prevent the compiler from warning about a new bug.
Arnd
Quoted 35 lines · Nathan Chancellor
When building with -Wsometimes-uninitialized, Clang warns:
drivers/scsi/gdth.c:3662:6: warning: variable 'paddr' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]Don't attempt to call
dma_free_coherentwhen buf is NULL (meaning that we never calleddma_alloc_coherentand initialized paddr), which avoids this warning.Signed-off-by Nathan Chancellor <[email protected]>--- drivers/scsi/gdth.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)drivers/scsi/gdth.c +3
-2@@ -3697,8 +3697,9 @@ static int ioc_general(void __user *arg, char *cmnd) rval =0 ; out_free_buf: -dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, buf, - paddr); +if (buf) +dma_free_coherent (&ha->pdev->dev, gen.data_len + gen.sense_len, + buf, paddr);return rval; }Signature · 1 line
2.21.0
Gentle ping (if there was a response to this, I didn't receive it). I know I sent it in the middle of a merge window so I get if it slipped through the cracks.
Thanks,
Nathan
Martin K. Petersen
AppliedNathan,
Quoted 9 lines
When building with -Wsometimes-uninitialized, Clang warns:
drivers/scsi/gdth.c:3662:6: warning: variable 'paddr' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]Don't attempt to call
dma_free_coherentwhen buf is NULL (meaning that we never calleddma_alloc_coherentand initialized paddr), which avoids this warning.
Applied to 5.2/scsi-queue, thanks.
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering