From d1f5ad7543e8f4dd32b676f054e5e25f2882d5b1 Mon Sep 17 00:00:00 2001 From: Matt Wagantall Date: Wed, 23 Apr 2014 17:14:47 -0700 Subject: [PATCH] iopoll: Use ktime_get() instead of jiffies for timeout calculations Presently, small timeout values will round up to 1 jiffy which, on CONFIG_HZ=100 systems, corresponds to a full 10 milliseconds. This is undesirable for drivers specifying timeouts of only a few 10's of microseconds (which is common), since they may end up tight-loop spinning for hundreds or thousands of times longer than expected before reporting a timeout. Additionally, jiffies cannot be reliably used with time_after() when the value of jiffies is small (like 1). In rare but real scenarios, jiffies may actually increment by more than 1 at a time. Specifically, this will occur if interrupts are disabled on for more than 1 jiffy (10 milliseconds for CONFIG_HZ=100) on the CPU responsible for incrementing jiffies. If interrupts are re-enabled on that CPU between the time the iopoll code (on another CPU) calculates the timeout value of jiffies and when the time_after() comparison is made, then the iopoll APIs may return -ETIMEDOUT prematurely, even though the specified timeout has not actually expired. Using ktime_get() avoid this problem (which arguably also needs a generic fix to avoid similar problems for other code which use jiffies to calculate timeouts). CRs-Fixed: 587801 Change-Id: I19150f41965b918c59c3fb98e29a8bd2e2c9609f Signed-off-by: Matt Wagantall --- include/linux/iopoll.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/linux/iopoll.h b/include/linux/iopoll.h index b882fe2cb9e..d085e03a2c4 100644 --- a/include/linux/iopoll.h +++ b/include/linux/iopoll.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2013 The Linux Foundation. All rights reserved. + * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 and @@ -17,7 +17,7 @@ #include #include -#include +#include #include #include #include @@ -36,13 +36,13 @@ */ #define readl_poll_timeout(addr, val, cond, sleep_us, timeout_us) \ ({ \ - unsigned long timeout = jiffies + usecs_to_jiffies(timeout_us); \ + ktime_t timeout = ktime_add_us(ktime_get(), timeout_us); \ might_sleep_if(timeout_us); \ for (;;) { \ (val) = readl(addr); \ if (cond) \ break; \ - if (timeout_us && time_after(jiffies, timeout)) { \ + if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \ (val) = readl(addr); \ break; \ } \