Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cpuidle: Consolidate disabled state checks

There are two reasons why CPU idle states may be disabled: either
because the driver has disabled them or because they have been
disabled by user space via sysfs.

In the former case, the state's "disabled" flag is set once during
the initialization of the driver and it is never cleared later (it
is read-only effectively). In the latter case, the "disable" field
of the given state's cpuidle_state_usage struct is set and it may be
changed via sysfs. Thus checking whether or not an idle state has
been disabled involves reading these two flags every time.

In order to avoid the additional check of the state's "disabled" flag
(which is effectively read-only anyway), use the value of it at the
init time to set a (new) flag in the "disable" field of that state's
cpuidle_state_usage structure and use the sysfs interface to
manipulate another (new) flag in it. This way the state is disabled
whenever the "disable" field of its cpuidle_state_usage structure is
nonzero, whatever the reason, and it is the only place to look into
to check whether or not the state has been disabled.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Daniel Lezcano <daniel.lezcano@linaro.org>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>

+54 -48
+2 -5
drivers/cpuidle/cpuidle-powernv.c
··· 56 56 return default_snooze_timeout; 57 57 58 58 for (i = index + 1; i < drv->state_count; i++) { 59 - struct cpuidle_state *s = &drv->states[i]; 60 - struct cpuidle_state_usage *su = &dev->states_usage[i]; 61 - 62 - if (s->disabled || su->disable) 59 + if (dev->states_usage[i].disable) 63 60 continue; 64 61 65 - return s->target_residency * tb_ticks_per_usec; 62 + return drv->states[i].target_residency * tb_ticks_per_usec; 66 63 } 67 64 68 65 return default_snooze_timeout;
+13 -11
drivers/cpuidle/cpuidle.c
··· 84 84 85 85 for (i = 1; i < drv->state_count; i++) { 86 86 struct cpuidle_state *s = &drv->states[i]; 87 - struct cpuidle_state_usage *su = &dev->states_usage[i]; 88 87 89 - if (s->disabled || su->disable || s->exit_latency <= latency_req 90 - || s->exit_latency > max_latency 91 - || (s->flags & forbidden_flags) 92 - || (s2idle && !s->enter_s2idle)) 88 + if (dev->states_usage[i].disable || 89 + s->exit_latency <= latency_req || 90 + s->exit_latency > max_latency || 91 + (s->flags & forbidden_flags) || 92 + (s2idle && !s->enter_s2idle)) 93 93 continue; 94 94 95 95 latency_req = s->exit_latency; ··· 265 265 266 266 if (diff < drv->states[entered_state].target_residency) { 267 267 for (i = entered_state - 1; i >= 0; i--) { 268 - if (drv->states[i].disabled || 269 - dev->states_usage[i].disable) 268 + if (dev->states_usage[i].disable) 270 269 continue; 271 270 272 271 /* Shallower states are enabled, so update. */ ··· 274 275 } 275 276 } else if (diff > delay) { 276 277 for (i = entered_state + 1; i < drv->state_count; i++) { 277 - if (drv->states[i].disabled || 278 - dev->states_usage[i].disable) 278 + if (dev->states_usage[i].disable) 279 279 continue; 280 280 281 281 /* ··· 378 380 379 381 limit_ns = TICK_NSEC; 380 382 for (i = 1; i < drv->state_count; i++) { 381 - if (drv->states[i].disabled || dev->states_usage[i].disable) 383 + if (dev->states_usage[i].disable) 382 384 continue; 383 385 384 386 limit_ns = (u64)drv->states[i].target_residency * NSEC_PER_USEC; ··· 565 567 */ 566 568 static int __cpuidle_register_device(struct cpuidle_device *dev) 567 569 { 568 - int ret; 569 570 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); 571 + int i, ret; 570 572 571 573 if (!try_module_get(drv->owner)) 572 574 return -EINVAL; 575 + 576 + for (i = 0; i < drv->state_count; i++) 577 + if (drv->states[i].disabled) 578 + dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER; 573 579 574 580 per_cpu(cpuidle_devices, dev->cpu) = dev; 575 581 list_add(&dev->device_list, &cpuidle_detected_devices);
+1 -3
drivers/cpuidle/governors/ladder.c
··· 84 84 85 85 /* consider promotion */ 86 86 if (last_idx < drv->state_count - 1 && 87 - !drv->states[last_idx + 1].disabled && 88 87 !dev->states_usage[last_idx + 1].disable && 89 88 last_residency > last_state->threshold.promotion_time && 90 89 drv->states[last_idx + 1].exit_latency <= latency_req) { ··· 97 98 98 99 /* consider demotion */ 99 100 if (last_idx > first_idx && 100 - (drv->states[last_idx].disabled || 101 - dev->states_usage[last_idx].disable || 101 + (dev->states_usage[last_idx].disable || 102 102 drv->states[last_idx].exit_latency > latency_req)) { 103 103 int i; 104 104
+3 -5
drivers/cpuidle/governors/menu.c
··· 298 298 if (unlikely(drv->state_count <= 1 || latency_req == 0) || 299 299 ((data->next_timer_us < drv->states[1].target_residency || 300 300 latency_req < drv->states[1].exit_latency) && 301 - !drv->states[0].disabled && !dev->states_usage[0].disable)) { 301 + !dev->states_usage[0].disable)) { 302 302 /* 303 303 * In this case state[0] will be used no matter what, so return 304 304 * it right away and keep the tick running if state[0] is a ··· 349 349 idx = -1; 350 350 for (i = 0; i < drv->state_count; i++) { 351 351 struct cpuidle_state *s = &drv->states[i]; 352 - struct cpuidle_state_usage *su = &dev->states_usage[i]; 353 352 354 - if (s->disabled || su->disable) 353 + if (dev->states_usage[i].disable) 355 354 continue; 356 355 357 356 if (idx == -1) ··· 421 422 * tick, so try to correct that. 422 423 */ 423 424 for (i = idx - 1; i >= 0; i--) { 424 - if (drv->states[i].disabled || 425 - dev->states_usage[i].disable) 425 + if (dev->states_usage[i].disable) 426 426 continue; 427 427 428 428 idx = i;
+2 -3
drivers/cpuidle/governors/teo.c
··· 212 212 int i; 213 213 214 214 for (i = state_idx - 1; i >= 0; i--) { 215 - if (drv->states[i].disabled || dev->states_usage[i].disable) 215 + if (dev->states_usage[i].disable) 216 216 continue; 217 217 218 218 state_idx = i; ··· 256 256 257 257 for (i = 0; i < drv->state_count; i++) { 258 258 struct cpuidle_state *s = &drv->states[i]; 259 - struct cpuidle_state_usage *su = &dev->states_usage[i]; 260 259 261 - if (s->disabled || su->disable) { 260 + if (dev->states_usage[i].disable) { 262 261 /* 263 262 * Ignore disabled states with target residencies beyond 264 263 * the anticipated idle duration.
+30 -21
drivers/cpuidle/sysfs.c
··· 255 255 return sprintf(buf, "%u\n", state->_name);\ 256 256 } 257 257 258 - #define define_store_state_ull_function(_name) \ 259 - static ssize_t store_state_##_name(struct cpuidle_state *state, \ 260 - struct cpuidle_state_usage *state_usage, \ 261 - const char *buf, size_t size) \ 262 - { \ 263 - unsigned long long value; \ 264 - int err; \ 265 - if (!capable(CAP_SYS_ADMIN)) \ 266 - return -EPERM; \ 267 - err = kstrtoull(buf, 0, &value); \ 268 - if (err) \ 269 - return err; \ 270 - if (value) \ 271 - state_usage->_name = 1; \ 272 - else \ 273 - state_usage->_name = 0; \ 274 - return size; \ 275 - } 276 - 277 258 #define define_show_state_ull_function(_name) \ 278 259 static ssize_t show_state_##_name(struct cpuidle_state *state, \ 279 260 struct cpuidle_state_usage *state_usage, \ ··· 280 299 define_show_state_ull_function(time) 281 300 define_show_state_str_function(name) 282 301 define_show_state_str_function(desc) 283 - define_show_state_ull_function(disable) 284 - define_store_state_ull_function(disable) 285 302 define_show_state_ull_function(above) 286 303 define_show_state_ull_function(below) 304 + 305 + static ssize_t show_state_disable(struct cpuidle_state *state, 306 + struct cpuidle_state_usage *state_usage, 307 + char *buf) 308 + { 309 + return sprintf(buf, "%llu\n", 310 + state_usage->disable & CPUIDLE_STATE_DISABLED_BY_USER); 311 + } 312 + 313 + static ssize_t store_state_disable(struct cpuidle_state *state, 314 + struct cpuidle_state_usage *state_usage, 315 + const char *buf, size_t size) 316 + { 317 + unsigned int value; 318 + int err; 319 + 320 + if (!capable(CAP_SYS_ADMIN)) 321 + return -EPERM; 322 + 323 + err = kstrtouint(buf, 0, &value); 324 + if (err) 325 + return err; 326 + 327 + if (value) 328 + state_usage->disable |= CPUIDLE_STATE_DISABLED_BY_USER; 329 + else 330 + state_usage->disable &= ~CPUIDLE_STATE_DISABLED_BY_USER; 331 + 332 + return size; 333 + } 287 334 288 335 define_one_state_ro(name, show_state_name); 289 336 define_one_state_ro(desc, show_state_desc);
+3
include/linux/cpuidle.h
··· 29 29 * CPUIDLE DEVICE INTERFACE * 30 30 ****************************/ 31 31 32 + #define CPUIDLE_STATE_DISABLED_BY_USER BIT(0) 33 + #define CPUIDLE_STATE_DISABLED_BY_DRIVER BIT(1) 34 + 32 35 struct cpuidle_state_usage { 33 36 unsigned long long disable; 34 37 unsigned long long usage;