2022-02-10 03:12:33

by Li-hao Kuo

[permalink] [raw]
Subject: [PATCH] spi: Fix warning for Clang build

Clang build fails with
spi-sunplus-sp7021.c:405:2: error: variable 'ret' is used
uninitialized whenever switch default is taken
default:

Restore initializing ret. and add return error at default

Fixes: 47e8fe57a66f ("spi: Modify irq request position and modify parameters")
Reported-by: Tom Rix <[email protected]>
Reported-by: kernel test robot <[email protected]>
Signed-off-by: Li-hao Kuo <[email protected]>
---
drivers/spi/spi-sunplus-sp7021.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/spi/spi-sunplus-sp7021.c b/drivers/spi/spi-sunplus-sp7021.c
index ba5ed9f..460993a 100644
--- a/drivers/spi/spi-sunplus-sp7021.c
+++ b/drivers/spi/spi-sunplus-sp7021.c
@@ -375,7 +375,7 @@ static int sp7021_spi_slave_transfer_one(struct spi_controller *ctlr, struct spi
{
struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
struct device *dev = pspim->dev;
- int mode, ret;
+ int mode, ret = 0;

mode = SP7021_SPI_IDLE;
if (xfer->tx_buf && xfer->rx_buf) {
@@ -403,7 +403,7 @@ static int sp7021_spi_slave_transfer_one(struct spi_controller *ctlr, struct spi
ret = sp7021_spi_slave_rx(spi, xfer);
break;
default:
- break;
+ return -EINVAL;
}
if (xfer->tx_buf)
dma_unmap_single(dev, xfer->tx_dma, xfer->len, DMA_TO_DEVICE);
--
2.7.4



2022-02-10 15:35:13

by Mark Brown

[permalink] [raw]
Subject: Re: [PATCH] spi: Fix warning for Clang build

On Thu, Feb 10, 2022 at 10:36:56AM +0800, Li-hao Kuo wrote:

> - int mode, ret;
> + int mode, ret = 0;
>
> mode = SP7021_SPI_IDLE;
> if (xfer->tx_buf && xfer->rx_buf) {
> @@ -403,7 +403,7 @@ static int sp7021_spi_slave_transfer_one(struct spi_controller *ctlr, struct spi
> ret = sp7021_spi_slave_rx(spi, xfer);
> break;
> default:
> - break;
> + return -EINVAL;

The return here means that the initialization is now redundant and will
stop the compiler spotting any future similar issues which isn't ideal.


Attachments:
(No filename) (540.00 B)
signature.asc (499.00 B)
Download all attachments

2022-02-11 09:24:39

by Lh Kuo 郭力豪

[permalink] [raw]
Subject: RE: [PATCH] spi: Fix warning for Clang build

>
> The return here means that the initialization is now redundant and will stop the compiler spotting any
> future similar issues which isn't ideal.

I got it, so do I need to submit a new patch?


2022-02-11 11:58:00

by Tom Rix

[permalink] [raw]
Subject: Re: [PATCH] spi: Fix warning for Clang build


On 2/10/22 7:32 PM, Lh Kuo ???O?? wrote:
>> The return here means that the initialization is now redundant and will stop the compiler spotting any
>> future similar issues which isn't ideal.
> I got it, so do I need to submit a new patch?

Assuming yes, so something else..

Looking again at the function, there are 3 sets of if-check blocks these
could be combined into the first one.

The later two are variations on is this an rx or a tx, the first check
does that.

T

>
>


2022-02-11 14:59:42

by Mark Brown

[permalink] [raw]
Subject: Re: [PATCH] spi: Fix warning for Clang build

On Fri, Feb 11, 2022 at 04:59:00AM +0000, Lh Kuo 郭力豪 wrote:
> Yes. I think the function can be simplified as follows

> static int sp7021_spi_slave_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
> struct spi_transfer *xfer)
> {
> struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
> struct device *dev = pspim->dev;
> int ret;
>
> mode = SP7021_SPI_IDLE;
> if (xfer->tx_buf && xfer->rx_buf) {
> dev_dbg(&ctlr->dev, "%s() wrong command\n", __func__);
> return -EINVAL;

Since only unidirectional transfers are supported...

> } else if (xfer->tx_buf) {
> xfer->tx_dma = dma_map_single(dev, (void *)xfer->tx_buf,
> xfer->len, DMA_TO_DEVICE);
> if (dma_mapping_error(dev, xfer->tx_dma))
> return -ENOMEM;
> ret = sp7021_spi_slave_tx(spi, xfer);
> } else if (xfer->rx_buf) {
> xfer->rx_dma = dma_map_single(dev, xfer->rx_buf, xfer->len,
> DMA_FROM_DEVICE);
> if (dma_mapping_error(dev, xfer->rx_dma))
> return -ENOMEM;
> ret = sp7021_spi_slave_rx(spi, xfer);
> }
>
> if (xfer->tx_buf)
> dma_unmap_single(dev, xfer->tx_dma, xfer->len, DMA_TO_DEVICE);
> if (xfer->rx_buf)
> dma_unmap_single(dev, xfer->rx_dma, xfer->len, DMA_FROM_DEVICE);

...you could even fold the unmapping into the if/else tree above.
Otherwise this looks good to me, please send a patch.


Attachments:
(No filename) (1.37 kB)
signature.asc (499.00 B)
Download all attachments

2022-02-11 16:10:02

by Lh Kuo 郭力豪

[permalink] [raw]
Subject: RE: [PATCH] spi: Fix warning for Clang build

Yes. I think the function can be simplified as follows

static int sp7021_spi_slave_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
struct spi_transfer *xfer)
{
struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
struct device *dev = pspim->dev;
int ret;

mode = SP7021_SPI_IDLE;
if (xfer->tx_buf && xfer->rx_buf) {
dev_dbg(&ctlr->dev, "%s() wrong command\n", __func__);
return -EINVAL;
} else if (xfer->tx_buf) {
xfer->tx_dma = dma_map_single(dev, (void *)xfer->tx_buf,
xfer->len, DMA_TO_DEVICE);
if (dma_mapping_error(dev, xfer->tx_dma))
return -ENOMEM;
ret = sp7021_spi_slave_tx(spi, xfer);
} else if (xfer->rx_buf) {
xfer->rx_dma = dma_map_single(dev, xfer->rx_buf, xfer->len,
DMA_FROM_DEVICE);
if (dma_mapping_error(dev, xfer->rx_dma))
return -ENOMEM;
ret = sp7021_spi_slave_rx(spi, xfer);
}

if (xfer->tx_buf)
dma_unmap_single(dev, xfer->tx_dma, xfer->len, DMA_TO_DEVICE);
if (xfer->rx_buf)
dma_unmap_single(dev, xfer->rx_dma, xfer->len, DMA_FROM_DEVICE);

spi_finalize_current_transfer(ctlr);
return ret;
}



2022-02-14 09:48:40

by Lh Kuo 郭力豪

[permalink] [raw]
Subject: RE: [PATCH] spi: Fix warning for Clang build

Thank you, I will revise it to the next submit based on these suggestions


2022-02-14 10:27:53

by Nathan Chancellor

[permalink] [raw]
Subject: Re: [PATCH] spi: Fix warning for Clang build

On Fri, Feb 11, 2022 at 04:59:00AM +0000, Lh Kuo 郭力豪 wrote:
> Yes. I think the function can be simplified as follows
>
> static int sp7021_spi_slave_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
> struct spi_transfer *xfer)
> {
> struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
> struct device *dev = pspim->dev;
> int ret;
>
> mode = SP7021_SPI_IDLE;
> if (xfer->tx_buf && xfer->rx_buf) {
> dev_dbg(&ctlr->dev, "%s() wrong command\n", __func__);
> return -EINVAL;
> } else if (xfer->tx_buf) {
> xfer->tx_dma = dma_map_single(dev, (void *)xfer->tx_buf,
> xfer->len, DMA_TO_DEVICE);
> if (dma_mapping_error(dev, xfer->tx_dma))
> return -ENOMEM;
> ret = sp7021_spi_slave_tx(spi, xfer);
> } else if (xfer->rx_buf) {
> xfer->rx_dma = dma_map_single(dev, xfer->rx_buf, xfer->len,
> DMA_FROM_DEVICE);
> if (dma_mapping_error(dev, xfer->rx_dma))
> return -ENOMEM;
> ret = sp7021_spi_slave_rx(spi, xfer);
> }
>
> if (xfer->tx_buf)
> dma_unmap_single(dev, xfer->tx_dma, xfer->len, DMA_TO_DEVICE);
> if (xfer->rx_buf)
> dma_unmap_single(dev, xfer->rx_dma, xfer->len, DMA_FROM_DEVICE);
>
> spi_finalize_current_transfer(ctlr);
> return ret;
> }

Clang will still warn that ret is uninitialized when the else if
branches are not taken.

How about something like:

static int sp7021_spi_slave_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
struct spi_transfer *xfer)
{
struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
struct device *dev = pspim->dev;
int ret;

if (xfer->tx_buf && !xfer->rx_buf) {
xfer->tx_dma = dma_map_single(dev, (void *)xfer->tx_buf,
xfer->len, DMA_TO_DEVICE);
if (dma_mapping_error(dev, xfer->tx_dma))
return -ENOMEM;
ret = sp7021_spi_slave_tx(spi, xfer);
dma_unmap_single(dev, xfer->tx_dma, xfer->len, DMA_TO_DEVICE);
} else if (xfer->rx_buf && !xfer->tx_buf) {
xfer->rx_dma = dma_map_single(dev, xfer->rx_buf, xfer->len,
DMA_FROM_DEVICE);
if (dma_mapping_error(dev, xfer->rx_dma))
return -ENOMEM;
ret = sp7021_spi_slave_rx(spi, xfer);
dma_unmap_single(dev, xfer->rx_dma, xfer->len, DMA_FROM_DEVICE);
} else {
dev_dbg(&ctlr->dev, "%s() wrong command\n", __func__);
return -EINVAL;
}

spi_finalize_current_transfer(ctlr);
return ret;
}