1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * drivers/cpufreq/cpufreq_governor.c 4 * 5 * CPUFREQ governors common code 6 * 7 * Copyright (C) 2001 Russell King 8 * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>. 9 * (C) 2003 Jun Nakajima <jun.nakajima@intel.com> 10 * (C) 2009 Alexander Clouter <alex@digriz.org.uk> 11 * (c) 2012 Viresh Kumar <viresh.kumar@linaro.org> 12 */ 13 14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 15 16 #include <linux/export.h> 17 #include <linux/kernel_stat.h> 18 #include <linux/slab.h> 19 20 #include "cpufreq_governor.h" 21 22 #define CPUFREQ_DBS_MIN_SAMPLING_INTERVAL (2 * TICK_NSEC / NSEC_PER_USEC) 23 24 static DEFINE_PER_CPU(struct cpu_dbs_info, cpu_dbs); 25 26 static DEFINE_MUTEX(gov_dbs_data_mutex); 27 28 /* Common sysfs tunables */ 29 /* 30 * sampling_rate_store - update sampling rate effective immediately if needed. 31 * 32 * If new rate is smaller than the old, simply updating 33 * dbs.sampling_rate might not be appropriate. For example, if the 34 * original sampling_rate was 1 second and the requested new sampling rate is 10 35 * ms because the user needs immediate reaction from ondemand governor, but not 36 * sure if higher frequency will be required or not, then, the governor may 37 * change the sampling rate too late; up to 1 second later. Thus, if we are 38 * reducing the sampling rate, we need to make the new value effective 39 * immediately. 40 * 41 * This must be called with dbs_data->mutex held, otherwise traversing 42 * policy_dbs_list isn't safe. 43 */ 44 ssize_t sampling_rate_store(struct gov_attr_set *attr_set, const char *buf, 45 size_t count) 46 { 47 struct dbs_data *dbs_data = to_dbs_data(attr_set); 48 struct policy_dbs_info *policy_dbs; 49 unsigned int sampling_interval; 50 int ret; 51 52 ret = sscanf(buf, "%u", &sampling_interval); 53 if (ret != 1 || sampling_interval < CPUFREQ_DBS_MIN_SAMPLING_INTERVAL) 54 return -EINVAL; 55 56 dbs_data->sampling_rate = sampling_interval; 57 58 /* 59 * We are operating under dbs_data->mutex and so the list and its 60 * entries can't be freed concurrently. 61 */ 62 list_for_each_entry(policy_dbs, &attr_set->policy_list, list) { 63 mutex_lock(&policy_dbs->update_mutex); 64 /* 65 * On 32-bit architectures this may race with the 66 * sample_delay_ns read in dbs_update_util_handler(), but that 67 * really doesn't matter. If the read returns a value that's 68 * too big, the sample will be skipped, but the next invocation 69 * of dbs_update_util_handler() (when the update has been 70 * completed) will take a sample. 71 * 72 * If this runs in parallel with dbs_work_handler(), we may end 73 * up overwriting the sample_delay_ns value that it has just 74 * written, but it will be corrected next time a sample is 75 * taken, so it shouldn't be significant. 76 */ 77 gov_update_sample_delay(policy_dbs, 0); 78 mutex_unlock(&policy_dbs->update_mutex); 79 } 80 81 return count; 82 } 83 EXPORT_SYMBOL_GPL(sampling_rate_store); 84 85 /** 86 * gov_update_cpu_data - Update CPU load data. 87 * @dbs_data: Top-level governor data pointer. 88 * 89 * Update CPU load data for all CPUs in the domain governed by @dbs_data 90 * (that may be a single policy or a bunch of them if governor tunables are 91 * system-wide). 92 * 93 * Call under the @dbs_data->attr_set.update_lock. The per-policy 94 * update_mutex is acquired and released internally for each policy. 95 * 96 * Note: prev_cpu_nice is reset here unconditionally alongside prev_cpu_idle. 97 * When io_is_busy changes, both baselines must be advanced to the same 98 * timestamp so that the next dbs_update() computes idle_time and nice_delta 99 * over the same interval, preventing an artificially inflated idle_time when 100 * ignore_nice_load is enabled. 101 */ 102 void gov_update_cpu_data(struct dbs_data *dbs_data) 103 { 104 struct policy_dbs_info *policy_dbs; 105 106 list_for_each_entry(policy_dbs, &dbs_data->attr_set.policy_list, list) { 107 unsigned int j; 108 109 mutex_lock(&policy_dbs->update_mutex); 110 for_each_cpu(j, policy_dbs->policy->cpus) { 111 struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j); 112 113 j_cdbs->prev_cpu_idle = get_cpu_idle_time(j, &j_cdbs->prev_update_time, 114 dbs_data->io_is_busy); 115 j_cdbs->prev_cpu_nice = kcpustat_field(CPUTIME_NICE, j); 116 } 117 mutex_unlock(&policy_dbs->update_mutex); 118 } 119 } 120 EXPORT_SYMBOL_GPL(gov_update_cpu_data); 121 122 unsigned int dbs_update(struct cpufreq_policy *policy) 123 { 124 struct policy_dbs_info *policy_dbs = policy->governor_data; 125 struct dbs_data *dbs_data = policy_dbs->dbs_data; 126 unsigned int ignore_nice = dbs_data->ignore_nice_load; 127 unsigned int max_load = 0, max_sample_load = 0; 128 unsigned int idle_periods = UINT_MAX; 129 unsigned int sampling_rate, io_busy, j; 130 u64 cur_nice; 131 132 /* 133 * Sometimes governors may use an additional multiplier to increase 134 * sample delays temporarily. Apply that multiplier to sampling_rate 135 * so as to keep the wake-up-from-idle detection logic a bit 136 * conservative. 137 */ 138 sampling_rate = dbs_data->sampling_rate * policy_dbs->rate_mult; 139 /* 140 * For the purpose of ondemand, waiting for disk IO is an indication 141 * that you're performance critical, and not that the system is actually 142 * idle, so do not add the iowait time to the CPU idle time then. 143 */ 144 io_busy = dbs_data->io_is_busy; 145 146 /* Get Absolute Load */ 147 for_each_cpu(j, policy->cpus) { 148 struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j); 149 u64 update_time, cur_idle_time; 150 unsigned int idle_time, time_elapsed; 151 unsigned int load, sample_load; 152 153 cur_idle_time = get_cpu_idle_time(j, &update_time, io_busy); 154 155 time_elapsed = update_time - j_cdbs->prev_update_time; 156 j_cdbs->prev_update_time = update_time; 157 158 /* 159 * cur_idle_time could be smaller than j_cdbs->prev_cpu_idle if 160 * it's obtained from get_cpu_idle_time_jiffy() when NOHZ is 161 * off, where idle_time is calculated by the difference between 162 * time elapsed in jiffies and "busy time" obtained from CPU 163 * statistics. If a CPU is 100% busy, the time elapsed and busy 164 * time should grow with the same amount in two consecutive 165 * samples, but in practice there could be a tiny difference, 166 * making the accumulated idle time decrease sometimes. Hence, 167 * in this case, idle_time should be regarded as 0 in order to 168 * make the further process correct. 169 */ 170 if (cur_idle_time > j_cdbs->prev_cpu_idle) 171 idle_time = cur_idle_time - j_cdbs->prev_cpu_idle; 172 else 173 idle_time = 0; 174 175 j_cdbs->prev_cpu_idle = cur_idle_time; 176 177 /* 178 * Always sample cur_nice and advance prev_cpu_nice, regardless 179 * of ignore_nice. This keeps prev_cpu_nice current so that 180 * enabling ignore_nice_load via sysfs never produces a 181 * stale-baseline spike (the delta will be at most one sampling 182 * interval of accumulated nice time, not since boot). 183 */ 184 cur_nice = kcpustat_field(CPUTIME_NICE, j); 185 if (ignore_nice) 186 idle_time += div_u64(cur_nice - j_cdbs->prev_cpu_nice, NSEC_PER_USEC); 187 188 j_cdbs->prev_cpu_nice = cur_nice; 189 190 /* 191 * Compute the sample load separately from the prev_load value 192 * that may be reused after a long idle interval. The conservative 193 * governor uses it to decide whether to apply deferred down steps. 194 * If no time has elapsed, retain the existing behavior and use 195 * prev_load. 196 */ 197 if (unlikely(!time_elapsed)) 198 sample_load = j_cdbs->prev_load; 199 else if (time_elapsed > idle_time) 200 sample_load = 100 * (time_elapsed - idle_time) / time_elapsed; 201 else 202 sample_load = 0; 203 204 if (unlikely(!time_elapsed)) { 205 /* 206 * That can only happen when this function is called 207 * twice in a row with a very short interval between the 208 * calls, so the previous load value can be used then. 209 */ 210 load = j_cdbs->prev_load; 211 } else if (unlikely(idle_time > 2 * sampling_rate && 212 j_cdbs->prev_load)) { 213 /* 214 * If the CPU had gone completely idle and a task has 215 * just woken up on this CPU now, it would be unfair to 216 * calculate 'load' the usual way for this elapsed 217 * time-window, because it would show near-zero load, 218 * irrespective of how CPU intensive that task actually 219 * was. This is undesirable for latency-sensitive bursty 220 * workloads. 221 * 222 * To avoid this, reuse the 'load' from the previous 223 * time-window and give this task a chance to start with 224 * a reasonably high CPU frequency. However, that 225 * shouldn't be over-done, lest we get stuck at a high 226 * load (high frequency) for too long, even when the 227 * current system load has actually dropped down, so 228 * clear prev_load to guarantee that the load will be 229 * computed again next time. 230 * 231 * Detecting this situation is easy: an unusually large 232 * 'idle_time' (as compared to the sampling rate) 233 * indicates this scenario. 234 */ 235 load = j_cdbs->prev_load; 236 j_cdbs->prev_load = 0; 237 } else { 238 load = sample_load; 239 j_cdbs->prev_load = load; 240 } 241 242 if (unlikely(idle_time > 2 * sampling_rate)) { 243 unsigned int periods = idle_time / sampling_rate; 244 245 if (periods < idle_periods) 246 idle_periods = periods; 247 } 248 249 if (load > max_load) 250 max_load = load; 251 252 if (sample_load > max_sample_load) 253 max_sample_load = sample_load; 254 } 255 256 policy_dbs->idle_periods = idle_periods; 257 policy_dbs->max_sample_load = max_sample_load; 258 259 return max_load; 260 } 261 EXPORT_SYMBOL_GPL(dbs_update); 262 263 static void dbs_work_handler(struct work_struct *work) 264 { 265 struct policy_dbs_info *policy_dbs; 266 struct cpufreq_policy *policy; 267 struct dbs_governor *gov; 268 269 policy_dbs = container_of(work, struct policy_dbs_info, work); 270 policy = policy_dbs->policy; 271 gov = dbs_governor_of(policy); 272 273 /* 274 * Make sure cpufreq_governor_limits() isn't evaluating load or the 275 * ondemand governor isn't updating the sampling rate in parallel. 276 */ 277 mutex_lock(&policy_dbs->update_mutex); 278 gov_update_sample_delay(policy_dbs, gov->gov_dbs_update(policy)); 279 mutex_unlock(&policy_dbs->update_mutex); 280 281 /* Allow the utilization update handler to queue up more work. */ 282 atomic_set(&policy_dbs->work_count, 0); 283 /* 284 * If the update below is reordered with respect to the sample delay 285 * modification, the utilization update handler may end up using a stale 286 * sample delay value. 287 */ 288 smp_wmb(); 289 policy_dbs->work_in_progress = false; 290 } 291 292 static void dbs_irq_work(struct irq_work *irq_work) 293 { 294 struct policy_dbs_info *policy_dbs; 295 296 policy_dbs = container_of(irq_work, struct policy_dbs_info, irq_work); 297 schedule_work_on(smp_processor_id(), &policy_dbs->work); 298 } 299 300 static void dbs_update_util_handler(struct update_util_data *data, u64 time, 301 unsigned int flags) 302 { 303 struct cpu_dbs_info *cdbs = container_of(data, struct cpu_dbs_info, update_util); 304 struct policy_dbs_info *policy_dbs = cdbs->policy_dbs; 305 u64 delta_ns, lst; 306 307 if (!cpufreq_this_cpu_can_update(policy_dbs->policy)) 308 return; 309 310 /* 311 * The work may not be allowed to be queued up right now. 312 * Possible reasons: 313 * - Work has already been queued up or is in progress. 314 * - It is too early (too little time from the previous sample). 315 */ 316 if (policy_dbs->work_in_progress) 317 return; 318 319 /* 320 * If the reads below are reordered before the check above, the value 321 * of sample_delay_ns used in the computation may be stale. 322 */ 323 smp_rmb(); 324 lst = READ_ONCE(policy_dbs->last_sample_time); 325 delta_ns = time - lst; 326 if ((s64)delta_ns < policy_dbs->sample_delay_ns) 327 return; 328 329 /* 330 * If the policy is not shared, the irq_work may be queued up right away 331 * at this point. Otherwise, we need to ensure that only one of the 332 * CPUs sharing the policy will do that. 333 */ 334 if (policy_dbs->is_shared) { 335 if (!atomic_add_unless(&policy_dbs->work_count, 1, 1)) 336 return; 337 338 /* 339 * If another CPU updated last_sample_time in the meantime, we 340 * shouldn't be here, so clear the work counter and bail out. 341 */ 342 if (unlikely(lst != READ_ONCE(policy_dbs->last_sample_time))) { 343 atomic_set(&policy_dbs->work_count, 0); 344 return; 345 } 346 } 347 348 policy_dbs->last_sample_time = time; 349 policy_dbs->work_in_progress = true; 350 irq_work_queue(&policy_dbs->irq_work); 351 } 352 353 static void gov_set_update_util(struct policy_dbs_info *policy_dbs, 354 unsigned int delay_us) 355 { 356 struct cpufreq_policy *policy = policy_dbs->policy; 357 int cpu; 358 359 gov_update_sample_delay(policy_dbs, delay_us); 360 policy_dbs->last_sample_time = 0; 361 362 for_each_cpu(cpu, policy->cpus) { 363 struct cpu_dbs_info *cdbs = &per_cpu(cpu_dbs, cpu); 364 365 cpufreq_add_update_util_hook(cpu, &cdbs->update_util, 366 dbs_update_util_handler); 367 } 368 } 369 370 static inline void gov_clear_update_util(struct cpufreq_policy *policy) 371 { 372 int i; 373 374 for_each_cpu(i, policy->cpus) 375 cpufreq_remove_update_util_hook(i); 376 377 synchronize_rcu(); 378 } 379 380 static struct policy_dbs_info *alloc_policy_dbs_info(struct cpufreq_policy *policy, 381 struct dbs_governor *gov) 382 { 383 struct policy_dbs_info *policy_dbs; 384 int j; 385 386 /* Allocate memory for per-policy governor data. */ 387 policy_dbs = gov->alloc(); 388 if (!policy_dbs) 389 return NULL; 390 391 policy_dbs->policy = policy; 392 mutex_init(&policy_dbs->update_mutex); 393 atomic_set(&policy_dbs->work_count, 0); 394 init_irq_work(&policy_dbs->irq_work, dbs_irq_work); 395 INIT_WORK(&policy_dbs->work, dbs_work_handler); 396 397 /* Set policy_dbs for all CPUs, online+offline */ 398 for_each_cpu(j, policy->related_cpus) { 399 struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j); 400 401 j_cdbs->policy_dbs = policy_dbs; 402 } 403 return policy_dbs; 404 } 405 406 static void free_policy_dbs_info(struct policy_dbs_info *policy_dbs, 407 struct dbs_governor *gov) 408 { 409 int j; 410 411 mutex_destroy(&policy_dbs->update_mutex); 412 413 for_each_cpu(j, policy_dbs->policy->related_cpus) { 414 struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j); 415 416 j_cdbs->policy_dbs = NULL; 417 j_cdbs->update_util.func = NULL; 418 } 419 gov->free(policy_dbs); 420 } 421 422 static void cpufreq_dbs_data_release(struct kobject *kobj) 423 { 424 struct dbs_data *dbs_data = to_dbs_data(to_gov_attr_set(kobj)); 425 struct dbs_governor *gov = dbs_data->gov; 426 427 gov->exit(dbs_data); 428 kfree(dbs_data); 429 } 430 431 int cpufreq_dbs_governor_init(struct cpufreq_policy *policy) 432 { 433 struct dbs_governor *gov = dbs_governor_of(policy); 434 struct dbs_data *dbs_data; 435 struct policy_dbs_info *policy_dbs; 436 int ret = 0; 437 438 /* State should be equivalent to EXIT */ 439 if (policy->governor_data) 440 return -EBUSY; 441 442 policy_dbs = alloc_policy_dbs_info(policy, gov); 443 if (!policy_dbs) 444 return -ENOMEM; 445 446 /* Protect gov->gdbs_data against concurrent updates. */ 447 mutex_lock(&gov_dbs_data_mutex); 448 449 dbs_data = gov->gdbs_data; 450 if (dbs_data) { 451 if (WARN_ON(have_governor_per_policy())) { 452 ret = -EINVAL; 453 goto free_policy_dbs_info; 454 } 455 policy_dbs->dbs_data = dbs_data; 456 policy->governor_data = policy_dbs; 457 458 gov_attr_set_get(&dbs_data->attr_set, &policy_dbs->list); 459 goto out; 460 } 461 462 dbs_data = kzalloc_obj(*dbs_data); 463 if (!dbs_data) { 464 ret = -ENOMEM; 465 goto free_policy_dbs_info; 466 } 467 468 dbs_data->gov = gov; 469 gov_attr_set_init(&dbs_data->attr_set, &policy_dbs->list); 470 471 ret = gov->init(dbs_data); 472 if (ret) 473 goto free_dbs_data; 474 475 /* 476 * The sampling interval should not be less than the transition latency 477 * of the CPU and it also cannot be too small for dbs_update() to work 478 * correctly. 479 */ 480 dbs_data->sampling_rate = max_t(unsigned int, 481 CPUFREQ_DBS_MIN_SAMPLING_INTERVAL, 482 cpufreq_policy_transition_delay_us(policy)); 483 484 if (!have_governor_per_policy()) 485 gov->gdbs_data = dbs_data; 486 487 policy_dbs->dbs_data = dbs_data; 488 policy->governor_data = policy_dbs; 489 490 gov->kobj_type.sysfs_ops = &governor_sysfs_ops; 491 gov->kobj_type.release = cpufreq_dbs_data_release; 492 ret = kobject_init_and_add(&dbs_data->attr_set.kobj, &gov->kobj_type, 493 get_governor_parent_kobj(policy), 494 "%s", gov->gov.name); 495 if (!ret) 496 goto out; 497 498 /* Failure, so roll back. */ 499 pr_err("initialization failed (dbs_data kobject init error %d)\n", ret); 500 501 policy->governor_data = NULL; 502 503 if (!have_governor_per_policy()) 504 gov->gdbs_data = NULL; 505 506 kobject_put(&dbs_data->attr_set.kobj); 507 goto free_policy_dbs_info; 508 509 free_dbs_data: 510 kfree(dbs_data); 511 512 free_policy_dbs_info: 513 free_policy_dbs_info(policy_dbs, gov); 514 515 out: 516 mutex_unlock(&gov_dbs_data_mutex); 517 return ret; 518 } 519 EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_init); 520 521 void cpufreq_dbs_governor_exit(struct cpufreq_policy *policy) 522 { 523 struct dbs_governor *gov = dbs_governor_of(policy); 524 struct policy_dbs_info *policy_dbs = policy->governor_data; 525 struct dbs_data *dbs_data = policy_dbs->dbs_data; 526 unsigned int count; 527 528 /* Protect gov->gdbs_data against concurrent updates. */ 529 mutex_lock(&gov_dbs_data_mutex); 530 531 count = gov_attr_set_put(&dbs_data->attr_set, &policy_dbs->list); 532 533 policy->governor_data = NULL; 534 535 if (!count && !have_governor_per_policy()) 536 gov->gdbs_data = NULL; 537 538 free_policy_dbs_info(policy_dbs, gov); 539 540 mutex_unlock(&gov_dbs_data_mutex); 541 } 542 EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_exit); 543 544 int cpufreq_dbs_governor_start(struct cpufreq_policy *policy) 545 { 546 struct dbs_governor *gov = dbs_governor_of(policy); 547 struct policy_dbs_info *policy_dbs = policy->governor_data; 548 struct dbs_data *dbs_data = policy_dbs->dbs_data; 549 unsigned int sampling_rate, j; 550 unsigned int io_busy; 551 552 if (!policy->cur) 553 return -EINVAL; 554 555 policy_dbs->is_shared = policy_is_shared(policy); 556 policy_dbs->rate_mult = 1; 557 558 sampling_rate = dbs_data->sampling_rate; 559 560 mutex_lock(&policy_dbs->update_mutex); 561 io_busy = dbs_data->io_is_busy; 562 for_each_cpu(j, policy->cpus) { 563 struct cpu_dbs_info *j_cdbs = &per_cpu(cpu_dbs, j); 564 565 j_cdbs->prev_cpu_idle = get_cpu_idle_time(j, &j_cdbs->prev_update_time, io_busy); 566 /* 567 * Make the first invocation of dbs_update() compute the load. 568 */ 569 j_cdbs->prev_load = 0; 570 j_cdbs->prev_cpu_nice = kcpustat_field(CPUTIME_NICE, j); 571 } 572 mutex_unlock(&policy_dbs->update_mutex); 573 574 gov->start(policy); 575 576 gov_set_update_util(policy_dbs, sampling_rate); 577 return 0; 578 } 579 EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_start); 580 581 void cpufreq_dbs_governor_stop(struct cpufreq_policy *policy) 582 { 583 struct policy_dbs_info *policy_dbs = policy->governor_data; 584 585 gov_clear_update_util(policy_dbs->policy); 586 irq_work_sync(&policy_dbs->irq_work); 587 cancel_work_sync(&policy_dbs->work); 588 atomic_set(&policy_dbs->work_count, 0); 589 policy_dbs->work_in_progress = false; 590 } 591 EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_stop); 592 593 void cpufreq_dbs_governor_limits(struct cpufreq_policy *policy) 594 { 595 struct dbs_governor *gov = dbs_governor_of(policy); 596 struct policy_dbs_info *policy_dbs; 597 598 /* Protect gov->gdbs_data against cpufreq_dbs_governor_exit() */ 599 mutex_lock(&gov_dbs_data_mutex); 600 policy_dbs = policy->governor_data; 601 if (!policy_dbs) 602 goto out; 603 604 mutex_lock(&policy_dbs->update_mutex); 605 cpufreq_policy_apply_limits(policy); 606 gov_update_sample_delay(policy_dbs, 0); 607 if (gov->limits) 608 gov->limits(policy); 609 mutex_unlock(&policy_dbs->update_mutex); 610 611 out: 612 mutex_unlock(&gov_dbs_data_mutex); 613 } 614 EXPORT_SYMBOL_GPL(cpufreq_dbs_governor_limits); 615