kernel-fxtec-pro1x/drivers/spi
Ian Abbott 9a12bff7c3 spi: spidev: only use up TX/RX bounce buffer space when needed
This patch changes the way space is reserved in spidev's pre-allocated
TX and RX bounce buffers to avoid wasting space in the buffers for an
SPI message consisting of multiple, half-duplex transfers in different
directions.

Background:

spidev data structures have separate, pre-allocated TX and RX bounce
buffers (`spidev->tx_buffer` and `spidev->rx_buffer`) of fixed size
(`bufsiz`).  The `SPI_IOC_MESSAGE(N)` ioctl processing uses a kernel
copy of the N `struct spi_ioc_transfer` elements copied from the
userspace ioctl arg pointer.  In these elements: `.len` is the length of
transfer in bytes; `.rx_buf` is either a userspace pointer to a buffer
to copy the RX data to or is set to 0 to discard the data; and `.tx_buf`
is either a userspace pointer to TX data supplied by the user or is set
to 0 to transmit zeros for this transfer.

`spidev_message()` uses the array of N `struct spi_ioc_transfer`
elements to construct a kernel SPI message consisting of a `struct
spi_message` containing a linked list (allocated as an array) of N
`struct spi_transfer` elements.  This involves iterating through the
`struct spi_ioc_transfer` and `struct spi_transfer` elements (variables
`u_tmp` and `k_tmp` respectively).  Before the first iteration,
variables `tx_buf` and `rx_buf` point to the start of the TX and RX
bounce buffers `spidev->tx_buffer` and `spidev->rx_buffer` and variable
`total` is set to 0.  These variables keep track of the next available
space in the bounce buffers and the total length of the SPI message.
Each iteration checks that there is enough room left in the buffers for
the transfer.  If `u_tmp->rx_buf` is non-zero, `k_tmp->rx_buf` is set to
`rx_buf`, otherwise it remains set to NULL.  If `u_tmp->tx_buf` is
non-zero, `k_tmp->tx_buf` is set to `tx_buf` and the userspace TX data
copied there, otherwise it remains set to NULL.  The variables `total`,
`rx_buf` and `tx_buf` are advanced by the length of the transfer.

The "problem":

While iterating through the transfers, the local bounce buffer "free
space" pointer variables `tx_buf` and `rx_buf` are always advanced by
the length of the transfer.  If `u_tmp->rx_buf` is 0 (so `k_tmp->rx_buf`
is NULL), then `rx_buf` is advanced unnecessarily and that part of
`spidev->rx_buffer` is wasted.  Similarly, if `u_tmp->tx_buf` is 0 (so
`k_tmp->tx_buf` is NULL), part of `spidev->tx_buffer` is wasted.

What this patch does:

To avoid wasting space unnecessarily in the RX bounce buffer, only
advance `rx_buf` by the transfer length if `u_tmp->rx_buf` is non-zero.
Similarly, to avoid wasting space unnecessarily in the TX bounce buffer,
only advance `tx_buf` if `u_tmp->tx_buf is non-zero.  To avoid pointer
subtraction, use new variables `rx_total` and `tx_total` to keep track
of the amount of space allocated in each of the bounce buffers.  If
these exceed the available space, a `-EMSGSIZE` error will be returned.

Limit the total length of the transfers (tracked by variable `total`) to
`INT_MAX` instead of `bufsiz`, returning an `-EMSGSIZE` error if
exceeded.  The total length is returned by `spidev_message()` on success
and we want that to be non-negative.  The message size limits for the
`SPI_IOC_MESSAGE(N)` ioctl are now as follows:

(a) total length of transfers is <= INTMAX;
(b) total length of transfers with non-NULL rx_buf is <= bufsiz;
(c) total length of transfers with non-NULL tx_buf is <= bufsiz.

Some transfers may have NULL rx_buf and NULL tx_buf.

If the transfer is completed successfully by the SPI core,
`spidev_message()` iterates through the transfers to copy any RX data
from the bounce buffer back to userspace on those transfers where
`u_tmp->rx_buf` is non-zero.  The variable `rx_buf` is again used to
keep track of the corresponding positions in the bounce buffer.  Now it
is only advanced for those transfers that use the RX bounce buffer.

Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Mark Brown <broonie@kernel.org>
2015-02-24 10:38:25 +09:00
..
Kconfig
Makefile
spi-adi-v3.c
spi-altera.c
spi-ath79.c
spi-atmel.c
spi-au1550.c
spi-bcm53xx.c
spi-bcm53xx.h
spi-bcm63xx-hsspi.c
spi-bcm63xx.c
spi-bcm2835.c
spi-bfin-sport.c
spi-bfin5xx.c
spi-bitbang-txrx.h
spi-bitbang.c
spi-butterfly.c
spi-cadence.c
spi-clps711x.c
spi-coldfire-qspi.c
spi-davinci.c
spi-dln2.c
spi-dw-mid.c
spi-dw-mmio.c
spi-dw-pci.c
spi-dw.c
spi-dw.h
spi-efm32.c
spi-ep93xx.c
spi-falcon.c
spi-fsl-cpm.c
spi-fsl-cpm.h
spi-fsl-dspi.c
spi-fsl-espi.c
spi-fsl-lib.c
spi-fsl-lib.h
spi-fsl-spi.c
spi-fsl-spi.h
spi-gpio.c
spi-img-spfi.c
spi-imx.c
spi-lm70llp.c
spi-meson-spifc.c
spi-mpc52xx-psc.c
spi-mpc52xx.c
spi-mpc512x-psc.c
spi-mxs.c
spi-nuc900.c
spi-oc-tiny.c
spi-octeon.c
spi-omap-100k.c
spi-omap-uwire.c
spi-omap2-mcspi.c
spi-orion.c
spi-pl022.c
spi-ppc4xx.c
spi-pxa2xx-dma.c
spi-pxa2xx-pci.c
spi-pxa2xx-pxadma.c
spi-pxa2xx.c
spi-pxa2xx.h
spi-qup.c
spi-rockchip.c
spi-rspi.c
spi-s3c24xx-fiq.h
spi-s3c24xx-fiq.S
spi-s3c24xx.c
spi-s3c64xx.c
spi-sc18is602.c
spi-sh-hspi.c
spi-sh-msiof.c
spi-sh-sci.c
spi-sh.c
spi-sirf.c
spi-st-ssc4.c
spi-sun4i.c
spi-sun6i.c
spi-tegra20-sflash.c
spi-tegra20-slink.c
spi-tegra114.c
spi-ti-qspi.c
spi-tle62x0.c
spi-topcliff-pch.c
spi-txx9.c
spi-xcomm.c
spi-xilinx.c
spi-xtensa-xtfpga.c
spi.c
spidev.c spi: spidev: only use up TX/RX bounce buffer space when needed 2015-02-24 10:38:25 +09:00