Skip to content

Commit e477901

Browse files
sjp38torvalds
authored andcommitted
timers: implement usleep_idle_range()
Patch series "mm/damon: Fix fake /proc/loadavg reports", v3. This patchset fixes DAMON's fake load report issue. The first patch makes yet another variant of usleep_range() for this fix, and the second patch fixes the issue of DAMON by making it using the newly introduced function. This patch (of 2): Some kernel threads such as DAMON could need to repeatedly sleep in micro seconds level. Because usleep_range() sleeps in uninterruptible state, however, such threads would make /proc/loadavg reports fake load. To help such cases, this commit implements a variant of usleep_range() called usleep_idle_range(). It is same to usleep_range() but sets the state of the current task as TASK_IDLE while sleeping. Link: https://lkml.kernel.org/r/20211126145015.15862-1-sj@kernel.org Link: https://lkml.kernel.org/r/20211126145015.15862-2-sj@kernel.org Signed-off-by: SeongJae Park <sj@kernel.org> Suggested-by: Andrew Morton <akpm@linux-foundation.org> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Tested-by: Oleksandr Natalenko <oleksandr@natalenko.name> Cc: John Stultz <john.stultz@linaro.org> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0c941cf commit e477901

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

‎include/linux/delay.h‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121

2222
#include <linux/math.h>
23+
#include <linux/sched.h>
2324

2425
extern unsigned long loops_per_jiffy;
2526

@@ -58,7 +59,18 @@ void calibrate_delay(void);
5859
void __attribute__((weak)) calibration_delay_done(void);
5960
void msleep(unsigned int msecs);
6061
unsigned long msleep_interruptible(unsigned int msecs);
61-
void usleep_range(unsigned long min, unsigned long max);
62+
void usleep_range_state(unsigned long min, unsigned long max,
63+
unsigned int state);
64+
65+
static inline void usleep_range(unsigned long min, unsigned long max)
66+
{
67+
usleep_range_state(min, max, TASK_UNINTERRUPTIBLE);
68+
}
69+
70+
static inline void usleep_idle_range(unsigned long min, unsigned long max)
71+
{
72+
usleep_range_state(min, max, TASK_IDLE);
73+
}
6274

6375
static inline void ssleep(unsigned int seconds)
6476
{

‎kernel/time/timer.c‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2054,26 +2054,28 @@ unsigned long msleep_interruptible(unsigned int msecs)
20542054
EXPORT_SYMBOL(msleep_interruptible);
20552055

20562056
/**
2057-
* usleep_range - Sleep for an approximate time
2058-
* @min: Minimum time in usecs to sleep
2059-
* @max: Maximum time in usecs to sleep
2057+
* usleep_range_state - Sleep for an approximate time in a given state
2058+
* @min: Minimum time in usecs to sleep
2059+
* @max: Maximum time in usecs to sleep
2060+
* @state: State of the current task that will be while sleeping
20602061
*
20612062
* In non-atomic context where the exact wakeup time is flexible, use
2062-
* usleep_range() instead of udelay(). The sleep improves responsiveness
2063+
* usleep_range_state() instead of udelay(). The sleep improves responsiveness
20632064
* by avoiding the CPU-hogging busy-wait of udelay(), and the range reduces
20642065
* power usage by allowing hrtimers to take advantage of an already-
20652066
* scheduled interrupt instead of scheduling a new one just for this sleep.
20662067
*/
2067-
void __sched usleep_range(unsigned long min, unsigned long max)
2068+
void __sched usleep_range_state(unsigned long min, unsigned long max,
2069+
unsigned int state)
20682070
{
20692071
ktime_t exp = ktime_add_us(ktime_get(), min);
20702072
u64 delta = (u64)(max - min) * NSEC_PER_USEC;
20712073

20722074
for (;;) {
2073-
__set_current_state(TASK_UNINTERRUPTIBLE);
2075+
__set_current_state(state);
20742076
/* Do not return before the requested sleep time has elapsed */
20752077
if (!schedule_hrtimeout_range(&exp, delta, HRTIMER_MODE_ABS))
20762078
break;
20772079
}
20782080
}
2079-
EXPORT_SYMBOL(usleep_range);
2081+
EXPORT_SYMBOL(usleep_range_state);

0 commit comments

Comments
 (0)