summaryrefslogtreecommitdiff
path: root/drivers/spi
diff options
context:
space:
mode:
authorSien Wu <sien.wu@ni.com>2016-09-02 02:24:29 +0300
committerMark Brown <broonie@kernel.org>2016-09-03 13:58:13 +0300
commitd0716dde375eb6bff332763bb2137302120d263d (patch)
tree810fecfc8a20b65e031b5586c34ce7a64932025f /drivers/spi
parent29b4817d4018df78086157ea3a55c1d9424a7cfc (diff)
downloadlinux-d0716dde375eb6bff332763bb2137302120d263d.tar.xz
spi: Prevent unexpected SPI time out due to arithmetic overflow
When reading SPI flash as MTD device, the transfer length is directly passed to the spi driver. If the requested data size exceeds 512KB, it will cause the time out calculation to overflow since transfer length is 32-bit unsigned integer. This issue is resolved by using 64-bit unsigned integer to perform the arithmetic. Signed-off-by: Sien Wu <sien.wu@ni.com> Acked-by: Brad Keryan <brad.keryan@ni.com> Acked-by: Gratian Crisan <gratian.crisan@ni.com> Acked-by: Brad Mouring <brad.mouring@ni.com> Natinst-ReviewBoard-ID 150232 Signed-off-by: Mark Brown <broonie@kernel.org>
Diffstat (limited to 'drivers/spi')
-rw-r--r--drivers/spi/spi.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 51ad42fad567..ac889df9b1f3 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -960,7 +960,7 @@ static int spi_transfer_one_message(struct spi_master *master,
struct spi_transfer *xfer;
bool keep_cs = false;
int ret = 0;
- unsigned long ms = 1;
+ unsigned long long ms = 1;
struct spi_statistics *statm = &master->statistics;
struct spi_statistics *stats = &msg->spi->statistics;
@@ -991,9 +991,13 @@ static int spi_transfer_one_message(struct spi_master *master,
if (ret > 0) {
ret = 0;
- ms = xfer->len * 8 * 1000 / xfer->speed_hz;
+ ms = 8LL * 1000LL * xfer->len;
+ do_div(ms, xfer->speed_hz);
ms += ms + 100; /* some tolerance */
+ if (ms > UINT_MAX)
+ ms = UINT_MAX;
+
ms = wait_for_completion_timeout(&master->xfer_completion,
msecs_to_jiffies(ms));
}