Message ID | 691d52b72f978f562136c587319852f5c65f08fe.1644460444.git.lhjeff911@gmail.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | spi: Fix warning for Clang build | expand |
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.
> > 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?
On 2/10/22 7:32 PM, Lh Kuo 郭力豪 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 > >
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; }
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.
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; }
Thank you, I will revise it to the next submit based on these suggestions
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);
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 <trix@redhat.com> Reported-by: kernel test robot <lkp@intel.com> Signed-off-by: Li-hao Kuo <lhjeff911@gmail.com> --- drivers/spi/spi-sunplus-sp7021.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)