Skip to content

Commit 9a12bff

Browse files
ian-abbottbroonie
authored andcommitted
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 <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent c517d83 commit 9a12bff

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

drivers/spi/spidev.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ static int spidev_message(struct spidev_data *spidev,
223223
struct spi_transfer *k_xfers;
224224
struct spi_transfer *k_tmp;
225225
struct spi_ioc_transfer *u_tmp;
226-
unsigned n, total;
226+
unsigned n, total, tx_total, rx_total;
227227
u8 *tx_buf, *rx_buf;
228228
int status = -EFAULT;
229229

@@ -239,33 +239,51 @@ static int spidev_message(struct spidev_data *spidev,
239239
tx_buf = spidev->tx_buffer;
240240
rx_buf = spidev->rx_buffer;
241241
total = 0;
242+
tx_total = 0;
243+
rx_total = 0;
242244
for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
243245
n;
244246
n--, k_tmp++, u_tmp++) {
245247
k_tmp->len = u_tmp->len;
246248

247249
total += k_tmp->len;
248-
if (total > bufsiz) {
250+
/* Since the function returns the total length of transfers
251+
* on success, restrict the total to positive int values to
252+
* avoid the return value looking like an error.
253+
*/
254+
if (total > INT_MAX) {
249255
status = -EMSGSIZE;
250256
goto done;
251257
}
252258

253259
if (u_tmp->rx_buf) {
260+
/* this transfer needs space in RX bounce buffer */
261+
rx_total += k_tmp->len;
262+
if (rx_total > bufsiz) {
263+
status = -EMSGSIZE;
264+
goto done;
265+
}
254266
k_tmp->rx_buf = rx_buf;
255267
if (!access_ok(VERIFY_WRITE, (u8 __user *)
256268
(uintptr_t) u_tmp->rx_buf,
257269
u_tmp->len))
258270
goto done;
271+
rx_buf += k_tmp->len;
259272
}
260273
if (u_tmp->tx_buf) {
274+
/* this transfer needs space in TX bounce buffer */
275+
tx_total += k_tmp->len;
276+
if (tx_total > bufsiz) {
277+
status = -EMSGSIZE;
278+
goto done;
279+
}
261280
k_tmp->tx_buf = tx_buf;
262281
if (copy_from_user(tx_buf, (const u8 __user *)
263282
(uintptr_t) u_tmp->tx_buf,
264283
u_tmp->len))
265284
goto done;
285+
tx_buf += k_tmp->len;
266286
}
267-
tx_buf += k_tmp->len;
268-
rx_buf += k_tmp->len;
269287

270288
k_tmp->cs_change = !!u_tmp->cs_change;
271289
k_tmp->tx_nbits = u_tmp->tx_nbits;
@@ -303,8 +321,8 @@ static int spidev_message(struct spidev_data *spidev,
303321
status = -EFAULT;
304322
goto done;
305323
}
324+
rx_buf += u_tmp->len;
306325
}
307-
rx_buf += u_tmp->len;
308326
}
309327
status = total;
310328

0 commit comments

Comments
 (0)