Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Frederic Weisbecker | 7863406 | 2017-10-27 04:42:28 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Housekeeping management. Manage the targets for routine code that can run on |
| 4 | * any CPU: unbound workqueues, timers, kthreads and any offloadable work. |
| 5 | * |
| 6 | * Copyright (C) 2017 Red Hat, Inc., Frederic Weisbecker |
Frederic Weisbecker | 1bda3f8 | 2018-02-21 05:17:26 +0100 | [diff] [blame] | 7 | * Copyright (C) 2017-2018 SUSE, Frederic Weisbecker |
Frederic Weisbecker | 7863406 | 2017-10-27 04:42:28 +0200 | [diff] [blame] | 8 | * |
| 9 | */ |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 10 | #include "sched.h" |
Frederic Weisbecker | 7863406 | 2017-10-27 04:42:28 +0200 | [diff] [blame] | 11 | |
Ingo Molnar | dfcb245 | 2018-12-03 10:05:56 +0100 | [diff] [blame] | 12 | DEFINE_STATIC_KEY_FALSE(housekeeping_overridden); |
| 13 | EXPORT_SYMBOL_GPL(housekeeping_overridden); |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 14 | static cpumask_var_t housekeeping_mask; |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 15 | static unsigned int housekeeping_flags; |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 16 | |
Wanpeng Li | 0c5f81d | 2019-07-06 09:26:51 +0800 | [diff] [blame] | 17 | bool housekeeping_enabled(enum hk_flags flags) |
| 18 | { |
| 19 | return !!(housekeeping_flags & flags); |
| 20 | } |
| 21 | EXPORT_SYMBOL_GPL(housekeeping_enabled); |
| 22 | |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 23 | int housekeeping_any_cpu(enum hk_flags flags) |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 24 | { |
Wanpeng Li | e0e8d49 | 2019-06-28 16:51:41 +0800 | [diff] [blame] | 25 | int cpu; |
| 26 | |
| 27 | if (static_branch_unlikely(&housekeeping_overridden)) { |
| 28 | if (housekeeping_flags & flags) { |
| 29 | cpu = sched_numa_find_closest(housekeeping_mask, smp_processor_id()); |
| 30 | if (cpu < nr_cpu_ids) |
| 31 | return cpu; |
| 32 | |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 33 | return cpumask_any_and(housekeeping_mask, cpu_online_mask); |
Wanpeng Li | e0e8d49 | 2019-06-28 16:51:41 +0800 | [diff] [blame] | 34 | } |
| 35 | } |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 36 | return smp_processor_id(); |
| 37 | } |
| 38 | EXPORT_SYMBOL_GPL(housekeeping_any_cpu); |
| 39 | |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 40 | const struct cpumask *housekeeping_cpumask(enum hk_flags flags) |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 41 | { |
Ingo Molnar | dfcb245 | 2018-12-03 10:05:56 +0100 | [diff] [blame] | 42 | if (static_branch_unlikely(&housekeeping_overridden)) |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 43 | if (housekeeping_flags & flags) |
| 44 | return housekeeping_mask; |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 45 | return cpu_possible_mask; |
| 46 | } |
| 47 | EXPORT_SYMBOL_GPL(housekeeping_cpumask); |
| 48 | |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 49 | void housekeeping_affine(struct task_struct *t, enum hk_flags flags) |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 50 | { |
Ingo Molnar | dfcb245 | 2018-12-03 10:05:56 +0100 | [diff] [blame] | 51 | if (static_branch_unlikely(&housekeeping_overridden)) |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 52 | if (housekeeping_flags & flags) |
| 53 | set_cpus_allowed_ptr(t, housekeeping_mask); |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 54 | } |
| 55 | EXPORT_SYMBOL_GPL(housekeeping_affine); |
| 56 | |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 57 | bool housekeeping_test_cpu(int cpu, enum hk_flags flags) |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 58 | { |
Ingo Molnar | dfcb245 | 2018-12-03 10:05:56 +0100 | [diff] [blame] | 59 | if (static_branch_unlikely(&housekeeping_overridden)) |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 60 | if (housekeeping_flags & flags) |
| 61 | return cpumask_test_cpu(cpu, housekeeping_mask); |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 62 | return true; |
| 63 | } |
| 64 | EXPORT_SYMBOL_GPL(housekeeping_test_cpu); |
Frederic Weisbecker | 7863406 | 2017-10-27 04:42:28 +0200 | [diff] [blame] | 65 | |
| 66 | void __init housekeeping_init(void) |
| 67 | { |
Frederic Weisbecker | 6f1982f | 2017-10-27 04:42:36 +0200 | [diff] [blame] | 68 | if (!housekeeping_flags) |
Frederic Weisbecker | 7863406 | 2017-10-27 04:42:28 +0200 | [diff] [blame] | 69 | return; |
| 70 | |
Ingo Molnar | dfcb245 | 2018-12-03 10:05:56 +0100 | [diff] [blame] | 71 | static_branch_enable(&housekeeping_overridden); |
Frederic Weisbecker | e179f5a | 2017-10-27 04:42:32 +0200 | [diff] [blame] | 72 | |
Frederic Weisbecker | d84b313 | 2018-02-21 05:17:27 +0100 | [diff] [blame] | 73 | if (housekeeping_flags & HK_FLAG_TICK) |
| 74 | sched_tick_offload_init(); |
| 75 | |
Frederic Weisbecker | 7863406 | 2017-10-27 04:42:28 +0200 | [diff] [blame] | 76 | /* We need at least one CPU to handle housekeeping work */ |
| 77 | WARN_ON_ONCE(cpumask_empty(housekeeping_mask)); |
| 78 | } |
Frederic Weisbecker | 6f1982f | 2017-10-27 04:42:36 +0200 | [diff] [blame] | 79 | |
Frederic Weisbecker | edb9382 | 2017-10-27 04:42:37 +0200 | [diff] [blame] | 80 | static int __init housekeeping_setup(char *str, enum hk_flags flags) |
Frederic Weisbecker | 6f1982f | 2017-10-27 04:42:36 +0200 | [diff] [blame] | 81 | { |
| 82 | cpumask_var_t non_housekeeping_mask; |
Nicholas Piggin | 9219565 | 2019-04-11 13:34:47 +1000 | [diff] [blame] | 83 | cpumask_var_t tmp; |
Frederic Weisbecker | 6f1982f | 2017-10-27 04:42:36 +0200 | [diff] [blame] | 84 | |
| 85 | alloc_bootmem_cpumask_var(&non_housekeeping_mask); |
Paul Gortmaker | 915a2bc | 2021-04-19 00:26:59 -0400 | [diff] [blame] | 86 | if (cpulist_parse(str, non_housekeeping_mask) < 0) { |
Frederic Weisbecker | edb9382 | 2017-10-27 04:42:37 +0200 | [diff] [blame] | 87 | pr_warn("Housekeeping: nohz_full= or isolcpus= incorrect CPU range\n"); |
Frederic Weisbecker | 6f1982f | 2017-10-27 04:42:36 +0200 | [diff] [blame] | 88 | free_bootmem_cpumask_var(non_housekeeping_mask); |
| 89 | return 0; |
| 90 | } |
| 91 | |
Nicholas Piggin | 9219565 | 2019-04-11 13:34:47 +1000 | [diff] [blame] | 92 | alloc_bootmem_cpumask_var(&tmp); |
Frederic Weisbecker | edb9382 | 2017-10-27 04:42:37 +0200 | [diff] [blame] | 93 | if (!housekeeping_flags) { |
| 94 | alloc_bootmem_cpumask_var(&housekeeping_mask); |
| 95 | cpumask_andnot(housekeeping_mask, |
| 96 | cpu_possible_mask, non_housekeeping_mask); |
Frederic Weisbecker | 6f1982f | 2017-10-27 04:42:36 +0200 | [diff] [blame] | 97 | |
Nicholas Piggin | 9219565 | 2019-04-11 13:34:47 +1000 | [diff] [blame] | 98 | cpumask_andnot(tmp, cpu_present_mask, non_housekeeping_mask); |
| 99 | if (cpumask_empty(tmp)) { |
| 100 | pr_warn("Housekeeping: must include one present CPU, " |
| 101 | "using boot CPU:%d\n", smp_processor_id()); |
| 102 | __cpumask_set_cpu(smp_processor_id(), housekeeping_mask); |
| 103 | __cpumask_clear_cpu(smp_processor_id(), non_housekeeping_mask); |
| 104 | } |
| 105 | } else { |
| 106 | cpumask_andnot(tmp, cpu_present_mask, non_housekeeping_mask); |
| 107 | if (cpumask_empty(tmp)) |
| 108 | __cpumask_clear_cpu(smp_processor_id(), non_housekeeping_mask); |
Frederic Weisbecker | edb9382 | 2017-10-27 04:42:37 +0200 | [diff] [blame] | 109 | cpumask_andnot(tmp, cpu_possible_mask, non_housekeeping_mask); |
| 110 | if (!cpumask_equal(tmp, housekeeping_mask)) { |
| 111 | pr_warn("Housekeeping: nohz_full= must match isolcpus=\n"); |
| 112 | free_bootmem_cpumask_var(tmp); |
| 113 | free_bootmem_cpumask_var(non_housekeeping_mask); |
| 114 | return 0; |
| 115 | } |
Frederic Weisbecker | edb9382 | 2017-10-27 04:42:37 +0200 | [diff] [blame] | 116 | } |
Nicholas Piggin | 9219565 | 2019-04-11 13:34:47 +1000 | [diff] [blame] | 117 | free_bootmem_cpumask_var(tmp); |
Frederic Weisbecker | 6f1982f | 2017-10-27 04:42:36 +0200 | [diff] [blame] | 118 | |
Frederic Weisbecker | edb9382 | 2017-10-27 04:42:37 +0200 | [diff] [blame] | 119 | if ((flags & HK_FLAG_TICK) && !(housekeeping_flags & HK_FLAG_TICK)) { |
| 120 | if (IS_ENABLED(CONFIG_NO_HZ_FULL)) { |
| 121 | tick_nohz_full_setup(non_housekeeping_mask); |
| 122 | } else { |
| 123 | pr_warn("Housekeeping: nohz unsupported." |
| 124 | " Build with CONFIG_NO_HZ_FULL\n"); |
| 125 | free_bootmem_cpumask_var(non_housekeeping_mask); |
| 126 | return 0; |
| 127 | } |
| 128 | } |
Frederic Weisbecker | 6f1982f | 2017-10-27 04:42:36 +0200 | [diff] [blame] | 129 | |
Frederic Weisbecker | edb9382 | 2017-10-27 04:42:37 +0200 | [diff] [blame] | 130 | housekeeping_flags |= flags; |
Frederic Weisbecker | 6f1982f | 2017-10-27 04:42:36 +0200 | [diff] [blame] | 131 | |
| 132 | free_bootmem_cpumask_var(non_housekeeping_mask); |
| 133 | |
| 134 | return 1; |
| 135 | } |
Frederic Weisbecker | edb9382 | 2017-10-27 04:42:37 +0200 | [diff] [blame] | 136 | |
| 137 | static int __init housekeeping_nohz_full_setup(char *str) |
| 138 | { |
| 139 | unsigned int flags; |
| 140 | |
Marcelo Tosatti | 9cc5b86 | 2020-05-27 16:29:09 +0200 | [diff] [blame] | 141 | flags = HK_FLAG_TICK | HK_FLAG_WQ | HK_FLAG_TIMER | HK_FLAG_RCU | |
| 142 | HK_FLAG_MISC | HK_FLAG_KTHREAD; |
Frederic Weisbecker | edb9382 | 2017-10-27 04:42:37 +0200 | [diff] [blame] | 143 | |
| 144 | return housekeeping_setup(str, flags); |
| 145 | } |
Frederic Weisbecker | 6f1982f | 2017-10-27 04:42:36 +0200 | [diff] [blame] | 146 | __setup("nohz_full=", housekeeping_nohz_full_setup); |
Frederic Weisbecker | edb9382 | 2017-10-27 04:42:37 +0200 | [diff] [blame] | 147 | |
| 148 | static int __init housekeeping_isolcpus_setup(char *str) |
| 149 | { |
Frederic Weisbecker | 150dfee | 2017-10-27 04:42:38 +0200 | [diff] [blame] | 150 | unsigned int flags = 0; |
Peter Xu | 3662daf | 2020-04-03 18:35:17 -0400 | [diff] [blame] | 151 | bool illegal = false; |
| 152 | char *par; |
| 153 | int len; |
Frederic Weisbecker | 150dfee | 2017-10-27 04:42:38 +0200 | [diff] [blame] | 154 | |
| 155 | while (isalpha(*str)) { |
| 156 | if (!strncmp(str, "nohz,", 5)) { |
| 157 | str += 5; |
| 158 | flags |= HK_FLAG_TICK; |
| 159 | continue; |
| 160 | } |
| 161 | |
| 162 | if (!strncmp(str, "domain,", 7)) { |
| 163 | str += 7; |
| 164 | flags |= HK_FLAG_DOMAIN; |
| 165 | continue; |
| 166 | } |
| 167 | |
Ming Lei | 11ea68f | 2020-01-20 17:16:25 +0800 | [diff] [blame] | 168 | if (!strncmp(str, "managed_irq,", 12)) { |
| 169 | str += 12; |
| 170 | flags |= HK_FLAG_MANAGED_IRQ; |
| 171 | continue; |
| 172 | } |
| 173 | |
Peter Xu | 3662daf | 2020-04-03 18:35:17 -0400 | [diff] [blame] | 174 | /* |
| 175 | * Skip unknown sub-parameter and validate that it is not |
| 176 | * containing an invalid character. |
| 177 | */ |
| 178 | for (par = str, len = 0; *str && *str != ','; str++, len++) { |
| 179 | if (!isalpha(*str) && *str != '_') |
| 180 | illegal = true; |
| 181 | } |
| 182 | |
| 183 | if (illegal) { |
| 184 | pr_warn("isolcpus: Invalid flag %.*s\n", len, par); |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | pr_info("isolcpus: Skipped unknown flag %.*s\n", len, par); |
| 189 | str++; |
Frederic Weisbecker | 150dfee | 2017-10-27 04:42:38 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* Default behaviour for isolcpus without flags */ |
| 193 | if (!flags) |
| 194 | flags |= HK_FLAG_DOMAIN; |
| 195 | |
| 196 | return housekeeping_setup(str, flags); |
Frederic Weisbecker | edb9382 | 2017-10-27 04:42:37 +0200 | [diff] [blame] | 197 | } |
| 198 | __setup("isolcpus=", housekeeping_isolcpus_setup); |