spi: Fix data corruption, uninitialised data leaks and bounds checks
Subsystems (7)
cover spi: Fix data corruption, uninitialised data leaks and bounds checks
These issues were reported by Sashiko.
The series is based on spi/for-7.5, since patch 5 relies on the following commit, which is not yet in Linus's tree:
3515e312e096 ("spi: topcliff-pch: clamp DMA bpw_len by bytes not words")
Witold, patch 10 is derived from the TX-only accounting in the Marvell b0 path, so confirmation that it matches the hardware's READ_DATA behaviour would be appreciated.
Lee Jones (10):
spi: fsl-cpm: Map the zero page's address, not its struct page
spi: bcm2835: Store native chip-select GPIO descriptor per device
spi: ch341: Limit transfers to the hardware packet size
spi: sprd-adi: Do not copy uninitialised data to rx_buf on read
failure
spi: topcliff-pch: Advance tx_buf between DMA chunks
spi: cadence-quadspi: Bound chip select against f_pdata array size
spi: cadence-xspi: Zero the tail word in m_iowriteq()
spi: fsl-qspi: Propagate clock errors from fsl_qspi_select_mem()
spi: cadence-xspi: Advance tx pointer between Marvell b0 chunks
spi: cadence-xspi: Skip tail read for qword-aligned Marvell b0
receives
drivers/spi/spi-bcm2835.c | 18 +++++++++--------- drivers/spi/spi-cadence-quadspi.c | 8 +++++--- drivers/spi/spi-cadence-xspi.c | 8 ++++++-- drivers/spi/spi-ch341.c | 6 ++++++ drivers/spi/spi-fsl-cpm.c | 3 ++- drivers/spi/spi-fsl-qspi.c | 26 +++++++++++++++++--------- drivers/spi/spi-sprd-adi.c | 3 ++- drivers/spi/spi-topcliff-pch.c | 5 +++++ 8 files changed, 52 insertions(+), 25 deletions(-)
base-commit: 9b2d7c8736a6fd27ceda93bd67529758c139e24f
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH 1/10 spi: fsl-cpm: Map the zero page's address, not its struct page
fsl_spi_cpm_init() maps a dummy TX buffer which is used for transfers that have no tx_buf. Since the conversion from empty_zero_page to ZERO_PAGE(0), the struct page pointer returned by ZERO_PAGE() has been handed directly to dma_map_single(), which expects a kernel virtual address.
As a result the mapping covers a PAGE_SIZE window of the memory map rather than a page of zeroes, and that struct page metadata is then clocked out onto the SPI bus for every RX-only transfer.
Pass page_address(ZERO_PAGE(0)) instead, so that the zero page itself is mapped.
--- drivers/spi/spi-fsl-cpm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/spi/spi-fsl-cpm.c +2 -1
@@ -350,7 +350,8 @@ int fsl_spi_cpm_init(struct mpc8xxx_spi *mspi) goto err_bds; } - mspi->dma_dummy_tx =dma_map_single (dev,ZERO_PAGE (0 ),PAGE_SIZE , + mspi->dma_dummy_tx =dma_map_single (dev,page_address (ZERO_PAGE (0 )), +PAGE_SIZE ,DMA_TO_DEVICE );if (dma_mapping_error (dev, mspi->dma_dummy_tx)) {dev_err (dev,"unable to map dummy tx buffer\n" );
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH 2/10 spi: bcm2835: Store native chip-select GPIO descriptor per device
In reply to Lee JonesWhen a device uses a native chip-select, bcm2835_spi_setup() requests a GPIO descriptor for it and stores the result in the controller-wide bs->cs_gpio. A second device using a native chip-select overwrites the first device's descriptor, which is then leaked.
bcm2835_spi_cleanup() subsequently calls gpiod_put(bs->cs_gpio) for every device, including devices whose chip-select was requested by the SPI core from the cs-gpios property. The same descriptor can therefore be released several times, and the descriptor released is not necessarily the one which belongs to the device being cleaned up.
Move the descriptor into the per-device struct bcm2835_spidev, which is zero-initialised, so that each device releases only the descriptor it requested itself.
--- drivers/spi/spi-bcm2835.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/spi/spi-bcm2835.c +9 -9
@@ -85,7 +85,6 @@ MODULE_PARM_DESC(polling_limit_us,* struct bcm2835_spi - BCM2835 SPI controller * @regs: base address of register map * @clk: core clock, divided to calculate serial clock -* @cs_gpio: chip-select GPIO descriptor * @clk_hz: core clock cached speed * @irq: interrupt, signals TX FIFO empty or RX FIFO ¾ full * @tfr: SPI transfer currently processed
@@ -120,7 +119,6 @@ MODULE_PARM_DESC(polling_limit_us,struct bcm2835_spi {void __iomem *regs;struct clk *clk; -struct gpio_desc *cs_gpio;unsigned long clk_hz;int irq;struct spi_transfer *tfr;
@@ -153,6 +151,7 @@ struct bcm2835_spi {* @clear_rx_desc: preallocated RX DMA descriptor used for TX-only transfers * (cyclically clears RX FIFO by writing @clear_rx_cs to CS register) * @clear_rx_addr: bus address of @clear_rx_cs +* @cs_gpio: native chip-select GPIO descriptor requested by the driver * @clear_rx_cs: precalculated CS register value to clear RX FIFO * (uses target-specific clock polarity and phase settings) */
@@ -160,6 +159,7 @@ struct bcm2835_spidev {u32 prepare_cs;struct dma_async_tx_descriptor *clear_rx_desc;dma_addr_t clear_rx_addr; +struct gpio_desc *cs_gpio;u32 clear_rx_cs____cacheline_aligned ; };
@@ -1157,7 +1157,6 @@ static void bcm2835_spi_cleanup(struct spi_device *spi) {struct bcm2835_spidev *target =spi_get_ctldata (spi);struct spi_controller *ctlr = spi->controller; -struct bcm2835_spi *bs =spi_controller_get_devdata (ctlr);if (target->clear_rx_desc)dmaengine_desc_free (target->clear_rx_desc);
@@ -1168,8 +1167,7 @@ static void bcm2835_spi_cleanup(struct spi_device *spi)sizeof (u32), DMA_TO_DEVICE); -if (!IS_ERR (bs->cs_gpio)) -gpiod_put (bs->cs_gpio); +gpiod_put (target->cs_gpio);spi_set_csgpiod (spi,0 ,NULL );kfree (target);
@@ -1232,6 +1230,7 @@ static int bcm2835_spi_setup(struct spi_device *spi)struct bcm2835_spi *bs =spi_controller_get_devdata (ctlr);struct bcm2835_spidev *target =spi_get_ctldata (spi); struct gpiod_lookup_table *lookup __free(kfree) = NULL; +struct gpio_desc *cs_gpio;static const char *const pinctrl_compats[] = {"brcm,bcm2835-gpio" ,"brcm,bcm2711-gpio" ,
@@ -1334,14 +1333,15 @@ static int bcm2835_spi_setup(struct spi_device *spi)gpiod_add_lookup_table (lookup); - bs->cs_gpio =gpiod_get (&spi->dev,"cs" ,GPIOD_OUT_LOW ); + cs_gpio =gpiod_get (&spi->dev,"cs" ,GPIOD_OUT_LOW );gpiod_remove_lookup_table (lookup); -if (IS_ERR (bs->cs_gpio)) { - ret =PTR_ERR (bs->cs_gpio); +if (IS_ERR (cs_gpio)) { + ret =PTR_ERR (cs_gpio); goto err_cleanup; } -spi_set_csgpiod (spi,0 , bs->cs_gpio); + target->cs_gpio = cs_gpio; +spi_set_csgpiod (spi,0 , cs_gpio);/* and set up the "mode" and level */ dev_info(&spi->dev, "setting up native-CS%i to use GPIO\n",
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH 3/10 spi: ch341: Limit transfers to the hardware packet size
In reply to Lee Jonesch341_transfer_one() clamps each transfer to CH341_PACKET_LENGTH bytes, of which the first is the command byte, so at most 31 data bytes are moved. It nevertheless reports success for the whole transfer.
The driver does not provide a max_transfer_size() callback, so the SPI core never splits longer transfers. The remainder of the data is silently dropped, and on the receive side the caller sees whatever was already in its buffer. For spidev this means stale or uninitialised kernel heap from its bounce buffer is copied out to user space.
Report the real limit through max_transfer_size() so that the SPI core splits long transfers into chunks the hardware can handle.
--- drivers/spi/spi-ch341.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/spi/spi-ch341.c +6 -0
@@ -63,6 +63,11 @@ static void ch341_set_cs(struct spi_device *spi, bool is_high) "error sending USB message for setting CS (%d)\n", err); } +static size_t ch341_max_transfer_size (struct spi_device *spi) +{ +return CH341_PACKET_LENGTH -1 ; +} +static int ch341_transfer_one (struct spi_controller *host,struct spi_device *spi,struct spi_transfer *trans)
@@ -187,6 +192,7 @@ static int ch341_probe(struct usb_interface *intf, ctrl->bus_num =-1 ; ctrl->mode_bits =SPI_CPHA ; ctrl->transfer_one = ch341_transfer_one; + ctrl->max_transfer_size = ch341_max_transfer_size; ctrl->set_cs = ch341_set_cs; ctrl->auto_runtime_pm = false;/*
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH 4/10 spi: sprd-adi: Do not copy uninitialised data to rx_buf on read failure
In reply to Lee Jonessprd_adi_read() returns early without writing to *read_val if it fails to take the hardware spinlock, if the register address is out of range, if the read times out or if the read-back check fails.
sprd_adi_transfer_one() stores val into rx_buf regardless of the return value, so on any of those failures the caller's buffer is filled with uninitialised stack data.
Only update rx_buf when the read succeeded. The error code is still propagated as before.
--- drivers/spi/spi-sprd-adi.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/spi/spi-sprd-adi.c +2 -1
@@ -341,7 +341,8 @@ static int sprd_adi_transfer_one(struct spi_controller *ctlr,if (t->rx_buf) { reg = *(u32 *)t->rx_buf; ret =sprd_adi_read (sadi, reg, &val); - *(u32 *)t->rx_buf = val; +if (!ret) + *(u32 *)t->rx_buf = val; }else if (t->tx_buf) {u32 *p = (u32 *)t->tx_buf; reg = *p++;
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH 5/10 spi: topcliff-pch: Advance tx_buf between DMA chunks
In reply to Lee JonesIn DMA mode, pch_spi_process_messages() splits transfers larger than PCH_BUF_SIZE into chunks and calls pch_spi_handle_dma() once per chunk. pch_spi_handle_dma() copies the TX data through a local pointer and never stores the advanced position back into the transfer, so every chunk after the first retransmits the data from the first chunk.
The RX side already writes its advanced pointer back and restores the original rx_buf once the transfer is complete.
Do the same for tx_buf, so that each chunk sends the correct slice of the buffer, and restore the original tx_buf once the transfer has completed or failed.
Advancing tx_buf by the number of bytes copied relies on the following commit, which ensures that each chunk covers no more than the bytes accounted for by the loop in pch_spi_process_messages():
commit 3515e312e096 ("spi: topcliff-pch: clamp DMA bpw_len by bytes not words")
--- drivers/spi/spi-topcliff-pch.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/spi/spi-topcliff-pch.c +5 -0
@@ -947,11 +947,13 @@ static void pch_spi_handle_dma(struct pch_spi_data *data, int *bpw) tx_dma_buf = dma->tx_buf_virt;for (i =0 ; i < data->bpw_len; i++) *tx_dma_buf++ = *tx_buf++; + data->cur_trans->tx_buf = tx_buf; }else { tx_sbuf = data->cur_trans->tx_buf; tx_dma_sbuf = dma->tx_buf_virt;for (i =0 ; i < data->bpw_len; i++) *tx_dma_sbuf++ = *tx_sbuf++; + data->cur_trans->tx_buf = tx_sbuf; } }
@@ -1180,10 +1182,12 @@ static void pch_spi_process_messages(struct work_struct *pwork)if (data->use_dma) {int i;char *save_rx_buf = data->cur_trans->rx_buf; +const void *save_tx_buf = data->cur_trans->tx_buf;for (i =0 ; i < cnt; i++) {pch_spi_handle_dma (data, &bpw);if (!pch_spi_start_transfer (data)) { + data->cur_trans->tx_buf = save_tx_buf; data->transfer_complete = true; data->current_msg->status = -EIO ; data->current_msg->complete
@@ -1196,6 +1200,7 @@ static void pch_spi_process_messages(struct work_struct *pwork)pch_spi_copy_rx_data_for_dma (data, bpw); } data->cur_trans->rx_buf = save_rx_buf; + data->cur_trans->tx_buf = save_tx_buf; }else {pch_spi_set_tx (data, &bpw);pch_spi_set_ir (data);
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH 6/10 spi: cadence-quadspi: Bound chip select against f_pdata array size
In reply to Lee Jonescqspi_setup_flash() uses the chip select taken from each flash child node's reg property to index the fixed-size cqspi->f_pdata[] array, which has CQSPI_MAX_CHIPSELECT entries.
The range check now compares the chip select against num_chipselect, which comes straight from the num-cs property and is never clamped to CQSPI_MAX_CHIPSELECT. A device tree which specifies num-cs greater than CQSPI_MAX_CHIPSELECT together with a flash node at a correspondingly high reg value therefore causes writes beyond the end of f_pdata[].
Reject chip selects which do not fit in f_pdata[] as well as those which exceed num-cs. While at it, store the chip select in a u32, matching of_property_read_u32(), so that values with the top bit set cannot become negative and slip past the bounds checks.
--- drivers/spi/spi-cadence-quadspi.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/spi/spi-cadence-quadspi.c +5 -3
@@ -1751,7 +1751,8 @@ static int cqspi_setup_flash(struct cqspi_st *cqspi)struct platform_device *pdev = cqspi->pdev;struct device *dev = &pdev->dev;struct cqspi_flash_pdata *f_pdata; -int ret, cs, max_cs =-1 ; +int ret, max_cs =-1 ; +u32 cs;/* Get flash device data */ for_each_available_child_of_node_scoped (dev->of_node, np) {
@@ -1761,8 +1762,9 @@ static int cqspi_setup_flash(struct cqspi_st *cqspi)return ret; } -if (cs >= cqspi->num_chipselect) { -dev_err (dev,"Chip select %d out of range.\n" , cs); +if (cs >= cqspi->num_chipselect || + cs >=CQSPI_MAX_CHIPSELECT ) { +dev_err (dev,"Chip select %u out of range.\n" , cs);return -EINVAL ; }
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
Re: [PATCH 06/10] spi: cadence-quadspi: Bound chip select against f_pdata array size
Hello Lee,
Quoted 10 lines · Lee Jones
cqspi_setup_flash()uses the chip select taken from each flash child node's reg property to index the fixed-size cqspi->f_pdata[] array, which hasCQSPI_MAX_CHIPSELECTentries.The range check now compares the chip select against
num_chipselect, which comes straight from the num-cs property and is never clamped toCQSPI_MAX_CHIPSELECT. A device tree which specifies num-cs greater thanCQSPI_MAX_CHIPSELECTtogether with a flash node at a correspondingly high reg value therefore causes writes beyond the end of f_pdata[].
Quoted 4 lines
Reject chip selects which do not fit in f_pdata[] as well as those which exceed num-cs. While at it, store the chip select in a u32, matching
of_property_read_u32(), so that values with the top bit set cannot become negative and slip past the bounds checks.
This commit message was hard to read! Can we do better? The issue at hand is super simple.
Repeating the last sentence:
Quoted 3 lines
While at it, store the chip select in a u32, matching
of_property_read_u32(), so that values with the top bit set cannot become negative and slip past the bounds checks.
Come on! 2147483648 chip selects?
Quoted 4 lines
Fixes 0d62c64a8e48 ("spi: cadence-qspi: assert each subnode flash CS is valid")Signed-off-by Lee Jones <[email protected]>
Is this worthy of backporting to all stable releases? It is a indeed a bug fix but which can only occur with a broken DT, probably during development. It sounds like a lot of churn.
Quoted 19 lines
--- drivers/spi/spi-cadence-quadspi.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)drivers/spi/spi-cadence-quadspi.c +2
-1@@ -1751,7 +1751,8 @@ static int cqspi_setup_flash(struct cqspi_st *cqspi)struct platform_device *pdev = cqspi->pdev;struct device *dev = &pdev->dev;struct cqspi_flash_pdata *f_pdata; -int ret, cs, max_cs =-1 ; +int ret, max_cs =-1 ; +u32 cs;/* Get flash device data */ for_each_available_child_of_node_scoped (dev->of_node, np) {
drivers/spi/spi-cadence-quadspi.c static int cqspi_setup_flash(struct cqspi_st *cqspi) · line 1769
Quoted 10 lines
drivers/spi/spi-cadence-quadspi.c +3
-2@@ -1761,8 +1762,9 @@ static int cqspi_setup_flash(struct cqspi_st *cqspi)return ret; } -if (cs >= cqspi->num_chipselect) { -dev_err (dev,"Chip select %d out of range.\n" , cs); +if (cs >= cqspi->num_chipselect || + cs >=CQSPI_MAX_CHIPSELECT ) { +dev_err (dev,"Chip select %u out of range.\n" , cs);return -EINVAL ; }
OK this does fix the bug. Here is another proposal though:
ensure at probe that cqspi->num_chipselect <= CQSPI_MAX_CHIPSELECT.
Thanks,
--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Re: [PATCH 06/10] spi: cadence-quadspi: Bound chip select against f_pdata array size
Quoted 21 lines · Théo Lebrun
Hello Lee,
Quoted 10 lines · Lee Jones
cqspi_setup_flash()uses the chip select taken from each flash child node's reg property to index the fixed-size cqspi->f_pdata[] array, which hasCQSPI_MAX_CHIPSELECTentries.The range check now compares the chip select against
num_chipselect, which comes straight from the num-cs property and is never clamped toCQSPI_MAX_CHIPSELECT. A device tree which specifies num-cs greater thanCQSPI_MAX_CHIPSELECTtogether with a flash node at a correspondingly high reg value therefore causes writes beyond the end of f_pdata[].Quoted 4 lines
Reject chip selects which do not fit in f_pdata[] as well as those which exceed num-cs. While at it, store the chip select in a u32, matching
of_property_read_u32(), so that values with the top bit set cannot become negative and slip past the bounds checks.This commit message was hard to read! Can we do better? The issue at hand is super simple.
Quoted 7 lines
Repeating the last sentence:
Quoted 3 lines
While at it, store the chip select in a u32, matching
of_property_read_u32(), so that values with the top bit set cannot become negative and slip past the bounds checks.Come on! 2147483648 chip selects?
Fair point!
Since cqspi->num_chipselect is already u32, C's unsigned promotion meant a negative int would only have slipped past if num-cs itself also had BIT(31) set. Matching the u32 * expected by of_property_read_u32() is reason enough on its own without the dramatic justification. =;-D
Will fix.
Quoted 8 lines
Quoted 4 lines
Fixes 0d62c64a8e48 ("spi: cadence-qspi: assert each subnode flash CS is valid")Signed-off-by Lee Jones <[email protected]>Is this worthy of backporting to all stable releases? It is a indeed a bug fix but which can only occur with a broken DT, probably during development. It sounds like a lot of churn.
Fine by me. Will be dropped in v2.
Quoted 22 lines
Quoted 22 lines
--- drivers/spi/spi-cadence-quadspi.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)drivers/spi/spi-cadence-quadspi.c +2
-1@@ -1751,7 +1751,8 @@ static int cqspi_setup_flash(struct cqspi_st *cqspi)struct platform_device *pdev = cqspi->pdev;struct device *dev = &pdev->dev;struct cqspi_flash_pdata *f_pdata; -int ret, cs, max_cs =-1 ; +int ret, max_cs =-1 ; +u32 cs;/* Get flash device data */ for_each_available_child_of_node_scoped (dev->of_node, np) {@@ -1761,8 +1762,9 @@ static int cqspi_setup_flash(struct cqspi_st *cqspi)return ret; }
Quoted 10 lines
drivers/spi/spi-cadence-quadspi.c static int cqspi_setup_flash(struct cqspi_st *cqspi) · line 1769
Quoted 7 lines
drivers/spi/spi-cadence-quadspi.c +3
-2-if (cs >= cqspi->num_chipselect) { -dev_err (dev,"Chip select %d out of range.\n" , cs); +if (cs >= cqspi->num_chipselect || + cs >=CQSPI_MAX_CHIPSELECT ) { +dev_err (dev,"Chip select %u out of range.\n" , cs);return -EINVAL ; }OK this does fix the bug. Here is another proposal though:
ensure at probe thatcqspi->num_chipselect<=CQSPI_MAX_CHIPSELECT.
One slight hesitation with this.
In cqspi_of_get_pdata(), CQSPI_MAX_CHIPSELECT used to be 16 prior to:
7cc3522aedb5 ("spi: cadence-qspi: set maximum chip-select to 4")
... and cqspi_setup_flash() overwrites cqspi->num_chipselect with max_cs
+ 1 at the end anyway.
Thus if an older or out-of-tree DTB (e.g. with cdns,is-decoded-cs) still has num-cs = <16> while only populating flash@0, failing probe in cqspi_of_get_pdata() would break a working board, whereas clamping num_chipselect or checking cs against CQSPI_MAX_CHIPSELECT only rejects child nodes that would actually overflow f_pdata[].
That said, no in-tree DTS hits this, so if you prefer rejecting (cqspi->num_chipselect > CQSPI_MAX_CHIPSELECT) or clamping it, let me know and I will happily make it so.
Signature · 1 line
Lee Jones
Re: [PATCH 06/10] spi: cadence-quadspi: Bound chip select against f_pdata array size
Quoted 29 lines · Lee Jones
Quoted 12 lines · Théo Lebrun
drivers/spi/spi-cadence-quadspi.c static int cqspi_setup_flash(struct cqspi_st *cqspi) · line 1766
Quoted 8 lines · Lee Jones
drivers/spi/spi-cadence-quadspi.c +3
-2@@ -1761,8 +1762,9 @@ static int cqspi_setup_flash(struct cqspi_st *cqspi) -if (cs >= cqspi->num_chipselect) { -dev_err (dev,"Chip select %d out of range.\n" , cs); +if (cs >= cqspi->num_chipselect || + cs >=CQSPI_MAX_CHIPSELECT ) { +dev_err (dev,"Chip select %u out of range.\n" , cs);return -EINVAL ; }OK this does fix the bug. Here is another proposal though:
ensure at probe thatcqspi->num_chipselect<=CQSPI_MAX_CHIPSELECT.One slight hesitation with this.
In
cqspi_of_get_pdata(),CQSPI_MAX_CHIPSELECTused to be 16 prior to:
7cc3522aedb5("spi: cadence-qspi: set maximum chip-select to 4")... and
cqspi_setup_flash()overwritescqspi->num_chipselectwithmax_cs
+ 1 at the end anyway.Thus if an older or out-of-tree DTB (e.g. with cdns,is-decoded-cs) still has
num-cs = <16>while only populating flash@0, failing probe incqspi_of_get_pdata()would break a working board, whereas clampingnum_chipselector checking cs againstCQSPI_MAX_CHIPSELECTonly rejects child nodes that would actually overflow f_pdata[].
Quoted 3 lines
That said, no in-tree DTS hits this, so if you prefer rejecting (
cqspi->num_chipselect>CQSPI_MAX_CHIPSELECT) or clamping it, let me know and I will happily make it so.
I still prefer the clamping or erroring from probe. A devicetree (ie hardware description) which tells us this controller has 16 CS has, is and will always be wrong. I don't see why we'd want to bother with this edge-case.
Thanks,
--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
Re: [PATCH 06/10] spi: cadence-quadspi: Bound chip select against f_pdata array size
Quoted 30 lines · Théo Lebrun
Quoted 29 lines · Lee Jones
Quoted 12 lines · Théo Lebrun
drivers/spi/spi-cadence-quadspi.c static int cqspi_setup_flash(struct cqspi_st *cqspi) · line 1766
Quoted 8 lines · Lee Jones
drivers/spi/spi-cadence-quadspi.c +3
-2@@ -1761,8 +1762,9 @@ static int cqspi_setup_flash(struct cqspi_st *cqspi) -if (cs >= cqspi->num_chipselect) { -dev_err (dev,"Chip select %d out of range.\n" , cs); +if (cs >= cqspi->num_chipselect || + cs >=CQSPI_MAX_CHIPSELECT ) { +dev_err (dev,"Chip select %u out of range.\n" , cs);return -EINVAL ; }OK this does fix the bug. Here is another proposal though:
ensure at probe thatcqspi->num_chipselect<=CQSPI_MAX_CHIPSELECT.One slight hesitation with this.
In
cqspi_of_get_pdata(),CQSPI_MAX_CHIPSELECTused to be 16 prior to:
7cc3522aedb5("spi: cadence-qspi: set maximum chip-select to 4")... and
cqspi_setup_flash()overwritescqspi->num_chipselectwithmax_cs
+ 1 at the end anyway.Thus if an older or out-of-tree DTB (e.g. with cdns,is-decoded-cs) still has
num-cs = <16>while only populating flash@0, failing probe incqspi_of_get_pdata()would break a working board, whereas clampingnum_chipselector checking cs againstCQSPI_MAX_CHIPSELECTonly rejects child nodes that would actually overflow f_pdata[].
Quoted 8 lines
Quoted 3 lines
That said, no in-tree DTS hits this, so if you prefer rejecting (
cqspi->num_chipselect>CQSPI_MAX_CHIPSELECT) or clamping it, let me know and I will happily make it so.I still prefer the clamping or erroring from probe. A devicetree (ie hardware description) which tells us this controller has 16 CS has, is and will always be wrong. I don't see why we'd want to bother with this edge-case.
Noted. Thanks.
Signature · 1 line
Lee Jones
PATCH 7/10 spi: cadence-xspi: Zero the tail word in m_iowriteq()
In reply to Lee Jonesm_iowriteq() writes data which is not 8-byte aligned, as well as any trailing bytes which do not make up a full 64-bit word, through a stack temporary. Only min(len, 8) bytes are copied into that temporary before all 8 bytes are written to the SDMA FIFO with writeq(), so when the remaining length is not a multiple of 8, up to 7 bytes of uninitialised kernel stack are written to the FIFO.
Zero-initialise the temporary so that the padding bytes are always zero.
--- drivers/spi/spi-cadence-xspi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spi-cadence-xspi.c +1 -1
@@ -1088,7 +1088,7 @@ static void m_iowriteq(void __iomem *addr, const void *buf, int len) }while (len) { -u64 tmp_buf; +u64 tmp_buf =0 ;memcpy (&tmp_buf, buf,min (len,8 ));writeq (tmp_buf, addr);
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH 8/10 spi: fsl-qspi: Propagate clock errors from fsl_qspi_select_mem()
In reply to Lee Jonesfsl_qspi_select_mem() returns void and silently gives up if either clk_set_rate() or fsl_qspi_clk_prep_enable() fails. fsl_qspi_exec_op() then carries on with the operation regardless.
When switching between devices, a failed rate change leaves q->selected pointing at the previously selected chip, so the operation is issued through the previous device's SFAR and AHB window. If the failure happens on the first operation, q->selected is still -1 from probe, so those offsets are derived from a negative chip select. On controllers which need the clocks disabled while the rate is changed, a clk_set_rate() failure also leaves the clocks off while registers continue to be accessed.
Re-enable the clocks even when setting the rate failed, return any error to the caller, and have fsl_qspi_exec_op() abort the operation when selecting the device fails.
--- drivers/spi/spi-fsl-qspi.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-)
drivers/spi/spi-fsl-qspi.c +17 -9
@@ -546,15 +546,15 @@ static void fsl_qspi_invalidate(struct fsl_qspi *q)qspi_writel (q, reg, q->iobase +QUADSPI_MCR ); } -static void fsl_qspi_select_mem (struct fsl_qspi *q,struct spi_device *spi, -const struct spi_mem_op *op) +static int fsl_qspi_select_mem (struct fsl_qspi *q,struct spi_device *spi, +const struct spi_mem_op *op) {unsigned long rate = op->max_freq;int ret;if (q->selected ==spi_get_chipselect (spi,0 ) && q->selected_freq == op->max_freq) -return ; +return 0 ;if (needs_4x_clock (q)) rate *=4 ;
@@ -563,19 +563,24 @@ static void fsl_qspi_select_mem(struct fsl_qspi *q, struct spi_device *spi,fsl_qspi_clk_disable_unprep (q); ret =clk_set_rate (q->clk, rate); -if (ret) -return ;if (needs_clk_disable (q)) { - ret =fsl_qspi_clk_prep_enable (q); -if (ret) -return ; +int err; + + err =fsl_qspi_clk_prep_enable (q); +if (!ret) + ret = err; } +if (ret) +return ret; + q->selected =spi_get_chipselect (spi,0 ); q->selected_freq = op->max_freq;fsl_qspi_invalidate (q); + +return 0 ; }static void fsl_qspi_read_ahb (struct fsl_qspi *q,const struct spi_mem_op *op)
@@ -682,7 +687,9 @@ static int fsl_qspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)fsl_qspi_readl_poll_tout (q, base +QUADSPI_SR , (QUADSPI_SR_IP_ACC_MASK |QUADSPI_SR_AHB_ACC_MASK ),10 ,1000 ); -fsl_qspi_select_mem (q, mem->spi, op); + err =fsl_qspi_select_mem (q, mem->spi, op); +if (err) + goto out_unlock;if (needs_amba_base_offset (q)) addr_offset = q->memmap_phy;
@@ -725,6 +732,7 @@ static int fsl_qspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)/* Invalidate the data in the AHB buffer. */ fsl_qspi_invalidate (q); +out_unlock:mutex_unlock (&q->lock);return err;
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH 9/10 spi: cadence-xspi: Advance tx pointer between Marvell b0 chunks
In reply to Lee Jonescdns_xspi_transfer_one_message_b0() splits transfers which are larger than MRVL_XFER_MAX_LEN into chunks, but never advances txd between them. For every chunk after the first, the leading byte is sent from txd[0] again, and a short trailing chunk retransmits the start of the buffer instead of its tail. The out_buffer pointer used for the rest of each chunk is advanced correctly, so the data on the bus ends up inconsistent.
Advance txd by the size of each chunk when the transfer carries TX data. TX-less transfers continue to use the zeroed scratch buffer, which is only ever as large as a single chunk.
--- drivers/spi/spi-cadence-xspi.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/spi/spi-cadence-xspi.c +2 -0
@@ -1319,6 +1319,8 @@ static int cdns_xspi_transfer_one_message_b0(struct spi_controller *controller, cdns_xspi->current_xfer_qword %=MRVL_XFER_QWORD_COUNT ; } cs_change = t->cs_change; +if (t->tx_buf) + txd += current_transfer_len; t->len -= current_transfer_len; }spi_transfer_delay_exec (t);
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
PATCH 10/10 spi: cadence-xspi: Skip tail read for qword-aligned Marvell b0 receives
In reply to Lee JonesIn cdns_xspi_transfer_one_message_b0() the receive path reads len / 8 full qwords and then always calls cdns_xspi_finish_read() to collect the remaining bytes. cdns_xspi_finish_read() unconditionally advances current_xfer_qword, even when there are no remaining bytes to read.
The TX-only path only accounts for an extra qword when the length is not a multiple of 8, so for qword-aligned receives the read index runs one qword ahead and later chunks and transfers in the same message read the wrong READ_DATA registers.
Only call cdns_xspi_finish_read() when there is a partial qword left to read, matching the accounting used by the TX-only path.
--- drivers/spi/spi-cadence-xspi.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/spi/spi-cadence-xspi.c +3 -1
@@ -1309,7 +1309,9 @@ static int cdns_xspi_transfer_one_message_b0(struct spi_controller *controller,for (j =0 ; j < current_transfer_len /8 ; j++)marvell_xspi_read_single_qword (cdns_xspi, &rxd); -cdns_xspi_finish_read (cdns_xspi, &rxd, current_transfer_len); +if (current_transfer_len %MRVL_XFER_QWORD_BYTECOUNT ) +cdns_xspi_finish_read (cdns_xspi, &rxd, + current_transfer_len); }else { cdns_xspi->current_xfer_qword += current_transfer_len /MRVL_XFER_QWORD_BYTECOUNT ;
Signature · 1 line
2.56.1.708.g77570a6a1d-goog