Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c48c1c9b authored by Matt Wagantall's avatar Matt Wagantall
Browse files

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: default avatarMatt Wagantall <mattw@codeaurora.org>
parent f43774a8
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
/*
 * 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 <linux/kernel.h>
#include <linux/types.h>
#include <linux/jiffies.h>
#include <linux/hrtimer.h>
#include <linux/delay.h>
#include <asm-generic/errno.h>
#include <asm/io.h>
@@ -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; \
		} \