diff mbox series

spi: Fix warning for Clang build

Message ID 691d52b72f978f562136c587319852f5c65f08fe.1644460444.git.lhjeff911@gmail.com (mailing list archive)
State New, archived
Headers show
Series spi: Fix warning for Clang build | expand

Commit Message

Li-hao Kuo Feb. 10, 2022, 2:36 a.m. UTC
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(-)

Comments

Mark Brown Feb. 10, 2022, 11:17 a.m. UTC | #1
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.
Lh Kuo 郭力豪 Feb. 11, 2022, 3:32 a.m. UTC | #2
> 
> 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?
Tom Rix Feb. 11, 2022, 4:49 a.m. UTC | #3
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

>
>
Lh Kuo 郭力豪 Feb. 11, 2022, 4:59 a.m. UTC | #4
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;
}
Mark Brown Feb. 11, 2022, 11:57 a.m. UTC | #5
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.
Nathan Chancellor Feb. 13, 2022, 6:11 p.m. UTC | #6
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;
}
Lh Kuo 郭力豪 Feb. 14, 2022, 2:23 a.m. UTC | #7
Thank you, I will revise it to the next submit based on these suggestions
diff mbox series

Patch

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);