Skip to content

Commit 51798de

Browse files
committed
Merge branches 'pm-cpuidle' and 'pm-qos'
* pm-cpuidle: tick-sched: avoid a maybe-uninitialized warning cpuidle: Add definition of residency to sysfs documentation time: hrtimer: Use timerqueue_iterate_next() to get to the next timer nohz: Avoid duplication of code related to got_idle_tick nohz: Gather tick_sched booleans under a common flag field cpuidle: menu: Avoid selecting shallow states with stopped tick cpuidle: menu: Refine idle state selection for running tick sched: idle: Select idle state before stopping the tick time: hrtimer: Introduce hrtimer_next_event_without() time: tick-sched: Split tick_nohz_stop_sched_tick() cpuidle: Return nohz hint from cpuidle_select() jiffies: Introduce USER_TICK_USEC and redefine TICK_USEC sched: idle: Do not stop the tick before cpuidle_idle_call() sched: idle: Do not stop the tick upfront in the idle loop time: tick-sched: Reorganize idle tick management code * pm-qos: PM / QoS: mark expected switch fall-throughs
3 parents 2dd0df8 + bbe9a70 + fe43e2c commit 51798de

16 files changed

Lines changed: 415 additions & 120 deletions

File tree

‎Documentation/cpuidle/sysfs.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ total 0
4040
-r--r--r-- 1 root root 4096 Feb 8 10:42 latency
4141
-r--r--r-- 1 root root 4096 Feb 8 10:42 name
4242
-r--r--r-- 1 root root 4096 Feb 8 10:42 power
43+
-r--r--r-- 1 root root 4096 Feb 8 10:42 residency
4344
-r--r--r-- 1 root root 4096 Feb 8 10:42 time
4445
-r--r--r-- 1 root root 4096 Feb 8 10:42 usage
4546

@@ -50,6 +51,7 @@ total 0
5051
-r--r--r-- 1 root root 4096 Feb 8 10:42 latency
5152
-r--r--r-- 1 root root 4096 Feb 8 10:42 name
5253
-r--r--r-- 1 root root 4096 Feb 8 10:42 power
54+
-r--r--r-- 1 root root 4096 Feb 8 10:42 residency
5355
-r--r--r-- 1 root root 4096 Feb 8 10:42 time
5456
-r--r--r-- 1 root root 4096 Feb 8 10:42 usage
5557

@@ -60,6 +62,7 @@ total 0
6062
-r--r--r-- 1 root root 4096 Feb 8 10:42 latency
6163
-r--r--r-- 1 root root 4096 Feb 8 10:42 name
6264
-r--r--r-- 1 root root 4096 Feb 8 10:42 power
65+
-r--r--r-- 1 root root 4096 Feb 8 10:42 residency
6366
-r--r--r-- 1 root root 4096 Feb 8 10:42 time
6467
-r--r--r-- 1 root root 4096 Feb 8 10:42 usage
6568

@@ -70,6 +73,7 @@ total 0
7073
-r--r--r-- 1 root root 4096 Feb 8 10:42 latency
7174
-r--r--r-- 1 root root 4096 Feb 8 10:42 name
7275
-r--r--r-- 1 root root 4096 Feb 8 10:42 power
76+
-r--r--r-- 1 root root 4096 Feb 8 10:42 residency
7377
-r--r--r-- 1 root root 4096 Feb 8 10:42 time
7478
-r--r--r-- 1 root root 4096 Feb 8 10:42 usage
7579
--------------------------------------------------------------------------------
@@ -78,6 +82,8 @@ total 0
7882
* desc : Small description about the idle state (string)
7983
* disable : Option to disable this idle state (bool) -> see note below
8084
* latency : Latency to exit out of this idle state (in microseconds)
85+
* residency : Time after which a state becomes more effecient than any
86+
shallower state (in microseconds)
8187
* name : Name of the idle state (string)
8288
* power : Power consumed while in this idle state (in milliwatts)
8389
* time : Total time spent in this idle state (in microseconds)

‎arch/x86/xen/smp_pv.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ static void xen_pv_play_dead(void) /* used only with HOTPLUG_CPU */
425425
* data back is to call:
426426
*/
427427
tick_nohz_idle_enter();
428+
tick_nohz_idle_stop_tick_protected();
428429

429430
cpuhp_online_idle(CPUHP_AP_ONLINE_IDLE);
430431
}

‎drivers/cpuidle/cpuidle.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,18 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
272272
*
273273
* @drv: the cpuidle driver
274274
* @dev: the cpuidle device
275+
* @stop_tick: indication on whether or not to stop the tick
275276
*
276277
* Returns the index of the idle state. The return value must not be negative.
278+
*
279+
* The memory location pointed to by @stop_tick is expected to be written the
280+
* 'false' boolean value if the scheduler tick should not be stopped before
281+
* entering the returned state.
277282
*/
278-
int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
283+
int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
284+
bool *stop_tick)
279285
{
280-
return cpuidle_curr_governor->select(drv, dev);
286+
return cpuidle_curr_governor->select(drv, dev, stop_tick);
281287
}
282288

283289
/**

‎drivers/cpuidle/governors/ladder.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ static inline void ladder_do_selection(struct ladder_device *ldev,
6363
* ladder_select_state - selects the next state to enter
6464
* @drv: cpuidle driver
6565
* @dev: the CPU
66+
* @dummy: not used
6667
*/
6768
static int ladder_select_state(struct cpuidle_driver *drv,
68-
struct cpuidle_device *dev)
69+
struct cpuidle_device *dev, bool *dummy)
6970
{
7071
struct ladder_device *ldev = this_cpu_ptr(&ladder_devices);
7172
struct device *device = get_cpu_device(dev->cpu);

‎drivers/cpuidle/governors/menu.c‎

Lines changed: 93 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
struct menu_device {
124124
int last_state_idx;
125125
int needs_update;
126+
int tick_wakeup;
126127

127128
unsigned int next_timer_us;
128129
unsigned int predicted_us;
@@ -279,8 +280,10 @@ static unsigned int get_typical_interval(struct menu_device *data)
279280
* menu_select - selects the next idle state to enter
280281
* @drv: cpuidle driver containing state data
281282
* @dev: the CPU
283+
* @stop_tick: indication on whether or not to stop the tick
282284
*/
283-
static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
285+
static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
286+
bool *stop_tick)
284287
{
285288
struct menu_device *data = this_cpu_ptr(&menu_devices);
286289
struct device *device = get_cpu_device(dev->cpu);
@@ -292,6 +295,7 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
292295
unsigned int expected_interval;
293296
unsigned long nr_iowaiters, cpu_load;
294297
int resume_latency = dev_pm_qos_raw_read_value(device);
298+
ktime_t delta_next;
295299

296300
if (data->needs_update) {
297301
menu_update(drv, dev);
@@ -303,11 +307,13 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
303307
latency_req = resume_latency;
304308

305309
/* Special case when user has set very strict latency requirement */
306-
if (unlikely(latency_req == 0))
310+
if (unlikely(latency_req == 0)) {
311+
*stop_tick = false;
307312
return 0;
313+
}
308314

309315
/* determine the expected residency time, round up */
310-
data->next_timer_us = ktime_to_us(tick_nohz_get_sleep_length());
316+
data->next_timer_us = ktime_to_us(tick_nohz_get_sleep_length(&delta_next));
311317

312318
get_iowait_load(&nr_iowaiters, &cpu_load);
313319
data->bucket = which_bucket(data->next_timer_us, nr_iowaiters);
@@ -346,14 +352,30 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
346352
*/
347353
data->predicted_us = min(data->predicted_us, expected_interval);
348354

349-
/*
350-
* Use the performance multiplier and the user-configurable
351-
* latency_req to determine the maximum exit latency.
352-
*/
353-
interactivity_req = data->predicted_us / performance_multiplier(nr_iowaiters, cpu_load);
354-
if (latency_req > interactivity_req)
355-
latency_req = interactivity_req;
355+
if (tick_nohz_tick_stopped()) {
356+
/*
357+
* If the tick is already stopped, the cost of possible short
358+
* idle duration misprediction is much higher, because the CPU
359+
* may be stuck in a shallow idle state for a long time as a
360+
* result of it. In that case say we might mispredict and try
361+
* to force the CPU into a state for which we would have stopped
362+
* the tick, unless a timer is going to expire really soon
363+
* anyway.
364+
*/
365+
if (data->predicted_us < TICK_USEC)
366+
data->predicted_us = min_t(unsigned int, TICK_USEC,
367+
ktime_to_us(delta_next));
368+
} else {
369+
/*
370+
* Use the performance multiplier and the user-configurable
371+
* latency_req to determine the maximum exit latency.
372+
*/
373+
interactivity_req = data->predicted_us / performance_multiplier(nr_iowaiters, cpu_load);
374+
if (latency_req > interactivity_req)
375+
latency_req = interactivity_req;
376+
}
356377

378+
expected_interval = data->predicted_us;
357379
/*
358380
* Find the idle state with the lowest power while satisfying
359381
* our constraints.
@@ -369,15 +391,52 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
369391
idx = i; /* first enabled state */
370392
if (s->target_residency > data->predicted_us)
371393
break;
372-
if (s->exit_latency > latency_req)
394+
if (s->exit_latency > latency_req) {
395+
/*
396+
* If we break out of the loop for latency reasons, use
397+
* the target residency of the selected state as the
398+
* expected idle duration so that the tick is retained
399+
* as long as that target residency is low enough.
400+
*/
401+
expected_interval = drv->states[idx].target_residency;
373402
break;
374-
403+
}
375404
idx = i;
376405
}
377406

378407
if (idx == -1)
379408
idx = 0; /* No states enabled. Must use 0. */
380409

410+
/*
411+
* Don't stop the tick if the selected state is a polling one or if the
412+
* expected idle duration is shorter than the tick period length.
413+
*/
414+
if ((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) ||
415+
expected_interval < TICK_USEC) {
416+
unsigned int delta_next_us = ktime_to_us(delta_next);
417+
418+
*stop_tick = false;
419+
420+
if (!tick_nohz_tick_stopped() && idx > 0 &&
421+
drv->states[idx].target_residency > delta_next_us) {
422+
/*
423+
* The tick is not going to be stopped and the target
424+
* residency of the state to be returned is not within
425+
* the time until the next timer event including the
426+
* tick, so try to correct that.
427+
*/
428+
for (i = idx - 1; i >= 0; i--) {
429+
if (drv->states[i].disabled ||
430+
dev->states_usage[i].disable)
431+
continue;
432+
433+
idx = i;
434+
if (drv->states[i].target_residency <= delta_next_us)
435+
break;
436+
}
437+
}
438+
}
439+
381440
data->last_state_idx = idx;
382441

383442
return data->last_state_idx;
@@ -397,6 +456,7 @@ static void menu_reflect(struct cpuidle_device *dev, int index)
397456

398457
data->last_state_idx = index;
399458
data->needs_update = 1;
459+
data->tick_wakeup = tick_nohz_idle_got_tick();
400460
}
401461

402462
/**
@@ -427,14 +487,27 @@ static void menu_update(struct cpuidle_driver *drv, struct cpuidle_device *dev)
427487
* assume the state was never reached and the exit latency is 0.
428488
*/
429489

430-
/* measured value */
431-
measured_us = cpuidle_get_last_residency(dev);
432-
433-
/* Deduct exit latency */
434-
if (measured_us > 2 * target->exit_latency)
435-
measured_us -= target->exit_latency;
436-
else
437-
measured_us /= 2;
490+
if (data->tick_wakeup && data->next_timer_us > TICK_USEC) {
491+
/*
492+
* The nohz code said that there wouldn't be any events within
493+
* the tick boundary (if the tick was stopped), but the idle
494+
* duration predictor had a differing opinion. Since the CPU
495+
* was woken up by a tick (that wasn't stopped after all), the
496+
* predictor was not quite right, so assume that the CPU could
497+
* have been idle long (but not forever) to help the idle
498+
* duration predictor do a better job next time.
499+
*/
500+
measured_us = 9 * MAX_INTERESTING / 10;
501+
} else {
502+
/* measured value */
503+
measured_us = cpuidle_get_last_residency(dev);
504+
505+
/* Deduct exit latency */
506+
if (measured_us > 2 * target->exit_latency)
507+
measured_us -= target->exit_latency;
508+
else
509+
measured_us /= 2;
510+
}
438511

439512
/* Make sure our coefficients do not exceed unity */
440513
if (measured_us > data->next_timer_us)

‎drivers/net/ethernet/sfc/mcdi.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ static int efx_mcdi_poll(struct efx_nic *efx)
375375
* because generally mcdi responses are fast. After that, back off
376376
* and poll once a jiffy (approximately)
377377
*/
378-
spins = TICK_USEC;
378+
spins = USER_TICK_USEC;
379379
finish = jiffies + MCDI_RPC_TIMEOUT;
380380

381381
while (1) {

‎include/linux/cpuidle.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ extern bool cpuidle_not_available(struct cpuidle_driver *drv,
135135
struct cpuidle_device *dev);
136136

137137
extern int cpuidle_select(struct cpuidle_driver *drv,
138-
struct cpuidle_device *dev);
138+
struct cpuidle_device *dev,
139+
bool *stop_tick);
139140
extern int cpuidle_enter(struct cpuidle_driver *drv,
140141
struct cpuidle_device *dev, int index);
141142
extern void cpuidle_reflect(struct cpuidle_device *dev, int index);
@@ -167,7 +168,7 @@ static inline bool cpuidle_not_available(struct cpuidle_driver *drv,
167168
struct cpuidle_device *dev)
168169
{return true; }
169170
static inline int cpuidle_select(struct cpuidle_driver *drv,
170-
struct cpuidle_device *dev)
171+
struct cpuidle_device *dev, bool *stop_tick)
171172
{return -ENODEV; }
172173
static inline int cpuidle_enter(struct cpuidle_driver *drv,
173174
struct cpuidle_device *dev, int index)
@@ -250,7 +251,8 @@ struct cpuidle_governor {
250251
struct cpuidle_device *dev);
251252

252253
int (*select) (struct cpuidle_driver *drv,
253-
struct cpuidle_device *dev);
254+
struct cpuidle_device *dev,
255+
bool *stop_tick);
254256
void (*reflect) (struct cpuidle_device *dev, int index);
255257
};
256258

‎include/linux/hrtimer.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ static inline ktime_t hrtimer_get_remaining(const struct hrtimer *timer)
424424
}
425425

426426
extern u64 hrtimer_get_next_event(void);
427+
extern u64 hrtimer_next_event_without(const struct hrtimer *exclude);
427428

428429
extern bool hrtimer_active(const struct hrtimer *timer);
429430

‎include/linux/jiffies.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ extern int register_refined_jiffies(long clock_tick_rate);
6262
/* TICK_NSEC is the time between ticks in nsec assuming SHIFTED_HZ */
6363
#define TICK_NSEC ((NSEC_PER_SEC+HZ/2)/HZ)
6464

65-
/* TICK_USEC is the time between ticks in usec assuming fake USER_HZ */
66-
#define TICK_USEC ((1000000UL + USER_HZ/2) / USER_HZ)
65+
/* TICK_USEC is the time between ticks in usec assuming SHIFTED_HZ */
66+
#define TICK_USEC ((USEC_PER_SEC + HZ/2) / HZ)
67+
68+
/* USER_TICK_USEC is the time between ticks in usec assuming fake USER_HZ */
69+
#define USER_TICK_USEC ((1000000UL + USER_HZ/2) / USER_HZ)
6770

6871
#ifndef __jiffy_arch_data
6972
#define __jiffy_arch_data

‎include/linux/tick.h‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,27 +115,46 @@ enum tick_dep_bits {
115115
extern bool tick_nohz_enabled;
116116
extern bool tick_nohz_tick_stopped(void);
117117
extern bool tick_nohz_tick_stopped_cpu(int cpu);
118+
extern void tick_nohz_idle_stop_tick(void);
119+
extern void tick_nohz_idle_retain_tick(void);
120+
extern void tick_nohz_idle_restart_tick(void);
118121
extern void tick_nohz_idle_enter(void);
119122
extern void tick_nohz_idle_exit(void);
120123
extern void tick_nohz_irq_exit(void);
121-
extern ktime_t tick_nohz_get_sleep_length(void);
124+
extern bool tick_nohz_idle_got_tick(void);
125+
extern ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next);
122126
extern unsigned long tick_nohz_get_idle_calls(void);
123127
extern unsigned long tick_nohz_get_idle_calls_cpu(int cpu);
124128
extern u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time);
125129
extern u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time);
130+
131+
static inline void tick_nohz_idle_stop_tick_protected(void)
132+
{
133+
local_irq_disable();
134+
tick_nohz_idle_stop_tick();
135+
local_irq_enable();
136+
}
137+
126138
#else /* !CONFIG_NO_HZ_COMMON */
127139
#define tick_nohz_enabled (0)
128140
static inline int tick_nohz_tick_stopped(void) { return 0; }
129141
static inline int tick_nohz_tick_stopped_cpu(int cpu) { return 0; }
142+
static inline void tick_nohz_idle_stop_tick(void) { }
143+
static inline void tick_nohz_idle_retain_tick(void) { }
144+
static inline void tick_nohz_idle_restart_tick(void) { }
130145
static inline void tick_nohz_idle_enter(void) { }
131146
static inline void tick_nohz_idle_exit(void) { }
147+
static inline bool tick_nohz_idle_got_tick(void) { return false; }
132148

133-
static inline ktime_t tick_nohz_get_sleep_length(void)
149+
static inline ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next)
134150
{
135-
return NSEC_PER_SEC / HZ;
151+
*delta_next = TICK_NSEC;
152+
return *delta_next;
136153
}
137154
static inline u64 get_cpu_idle_time_us(int cpu, u64 *unused) { return -1; }
138155
static inline u64 get_cpu_iowait_time_us(int cpu, u64 *unused) { return -1; }
156+
157+
static inline void tick_nohz_idle_stop_tick_protected(void) { }
139158
#endif /* !CONFIG_NO_HZ_COMMON */
140159

141160
#ifdef CONFIG_NO_HZ_FULL

0 commit comments

Comments
 (0)