blob: 5a6ea03f9882d3bb068f1b10cf2da908fc492000 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Frederic Weisbecker78634062017-10-27 04:42:28 +02002/*
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 Weisbecker1bda3f82018-02-21 05:17:26 +01007 * Copyright (C) 2017-2018 SUSE, Frederic Weisbecker
Frederic Weisbecker78634062017-10-27 04:42:28 +02008 *
9 */
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +010010#include "sched.h"
Frederic Weisbecker78634062017-10-27 04:42:28 +020011
Ingo Molnardfcb2452018-12-03 10:05:56 +010012DEFINE_STATIC_KEY_FALSE(housekeeping_overridden);
13EXPORT_SYMBOL_GPL(housekeeping_overridden);
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020014static cpumask_var_t housekeeping_mask;
Frederic Weisbeckerde201552017-10-27 04:42:35 +020015static unsigned int housekeeping_flags;
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020016
Wanpeng Li0c5f81d2019-07-06 09:26:51 +080017bool housekeeping_enabled(enum hk_flags flags)
18{
19 return !!(housekeeping_flags & flags);
20}
21EXPORT_SYMBOL_GPL(housekeeping_enabled);
22
Frederic Weisbeckerde201552017-10-27 04:42:35 +020023int housekeeping_any_cpu(enum hk_flags flags)
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020024{
Wanpeng Lie0e8d492019-06-28 16:51:41 +080025 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 Weisbeckerde201552017-10-27 04:42:35 +020033 return cpumask_any_and(housekeeping_mask, cpu_online_mask);
Wanpeng Lie0e8d492019-06-28 16:51:41 +080034 }
35 }
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020036 return smp_processor_id();
37}
38EXPORT_SYMBOL_GPL(housekeeping_any_cpu);
39
Frederic Weisbeckerde201552017-10-27 04:42:35 +020040const struct cpumask *housekeeping_cpumask(enum hk_flags flags)
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020041{
Ingo Molnardfcb2452018-12-03 10:05:56 +010042 if (static_branch_unlikely(&housekeeping_overridden))
Frederic Weisbeckerde201552017-10-27 04:42:35 +020043 if (housekeeping_flags & flags)
44 return housekeeping_mask;
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020045 return cpu_possible_mask;
46}
47EXPORT_SYMBOL_GPL(housekeeping_cpumask);
48
Frederic Weisbeckerde201552017-10-27 04:42:35 +020049void housekeeping_affine(struct task_struct *t, enum hk_flags flags)
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020050{
Ingo Molnardfcb2452018-12-03 10:05:56 +010051 if (static_branch_unlikely(&housekeeping_overridden))
Frederic Weisbeckerde201552017-10-27 04:42:35 +020052 if (housekeeping_flags & flags)
53 set_cpus_allowed_ptr(t, housekeeping_mask);
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020054}
55EXPORT_SYMBOL_GPL(housekeeping_affine);
56
Frederic Weisbeckerde201552017-10-27 04:42:35 +020057bool housekeeping_test_cpu(int cpu, enum hk_flags flags)
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020058{
Ingo Molnardfcb2452018-12-03 10:05:56 +010059 if (static_branch_unlikely(&housekeeping_overridden))
Frederic Weisbeckerde201552017-10-27 04:42:35 +020060 if (housekeeping_flags & flags)
61 return cpumask_test_cpu(cpu, housekeeping_mask);
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020062 return true;
63}
64EXPORT_SYMBOL_GPL(housekeeping_test_cpu);
Frederic Weisbecker78634062017-10-27 04:42:28 +020065
66void __init housekeeping_init(void)
67{
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020068 if (!housekeeping_flags)
Frederic Weisbecker78634062017-10-27 04:42:28 +020069 return;
70
Ingo Molnardfcb2452018-12-03 10:05:56 +010071 static_branch_enable(&housekeeping_overridden);
Frederic Weisbeckere179f5a2017-10-27 04:42:32 +020072
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +010073 if (housekeeping_flags & HK_FLAG_TICK)
74 sched_tick_offload_init();
75
Frederic Weisbecker78634062017-10-27 04:42:28 +020076 /* We need at least one CPU to handle housekeeping work */
77 WARN_ON_ONCE(cpumask_empty(housekeeping_mask));
78}
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020079
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020080static int __init housekeeping_setup(char *str, enum hk_flags flags)
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020081{
82 cpumask_var_t non_housekeeping_mask;
Nicholas Piggin92195652019-04-11 13:34:47 +100083 cpumask_var_t tmp;
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020084 int err;
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020085
86 alloc_bootmem_cpumask_var(&non_housekeeping_mask);
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020087 err = cpulist_parse(str, non_housekeeping_mask);
88 if (err < 0 || cpumask_last(non_housekeeping_mask) >= nr_cpu_ids) {
89 pr_warn("Housekeeping: nohz_full= or isolcpus= incorrect CPU range\n");
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020090 free_bootmem_cpumask_var(non_housekeeping_mask);
91 return 0;
92 }
93
Nicholas Piggin92195652019-04-11 13:34:47 +100094 alloc_bootmem_cpumask_var(&tmp);
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020095 if (!housekeeping_flags) {
96 alloc_bootmem_cpumask_var(&housekeeping_mask);
97 cpumask_andnot(housekeeping_mask,
98 cpu_possible_mask, non_housekeeping_mask);
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020099
Nicholas Piggin92195652019-04-11 13:34:47 +1000100 cpumask_andnot(tmp, cpu_present_mask, non_housekeeping_mask);
101 if (cpumask_empty(tmp)) {
102 pr_warn("Housekeeping: must include one present CPU, "
103 "using boot CPU:%d\n", smp_processor_id());
104 __cpumask_set_cpu(smp_processor_id(), housekeeping_mask);
105 __cpumask_clear_cpu(smp_processor_id(), non_housekeeping_mask);
106 }
107 } else {
108 cpumask_andnot(tmp, cpu_present_mask, non_housekeeping_mask);
109 if (cpumask_empty(tmp))
110 __cpumask_clear_cpu(smp_processor_id(), non_housekeeping_mask);
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200111 cpumask_andnot(tmp, cpu_possible_mask, non_housekeeping_mask);
112 if (!cpumask_equal(tmp, housekeeping_mask)) {
113 pr_warn("Housekeeping: nohz_full= must match isolcpus=\n");
114 free_bootmem_cpumask_var(tmp);
115 free_bootmem_cpumask_var(non_housekeeping_mask);
116 return 0;
117 }
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200118 }
Nicholas Piggin92195652019-04-11 13:34:47 +1000119 free_bootmem_cpumask_var(tmp);
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200120
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200121 if ((flags & HK_FLAG_TICK) && !(housekeeping_flags & HK_FLAG_TICK)) {
122 if (IS_ENABLED(CONFIG_NO_HZ_FULL)) {
123 tick_nohz_full_setup(non_housekeeping_mask);
124 } else {
125 pr_warn("Housekeeping: nohz unsupported."
126 " Build with CONFIG_NO_HZ_FULL\n");
127 free_bootmem_cpumask_var(non_housekeeping_mask);
128 return 0;
129 }
130 }
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200131
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200132 housekeeping_flags |= flags;
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200133
134 free_bootmem_cpumask_var(non_housekeeping_mask);
135
136 return 1;
137}
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200138
139static int __init housekeeping_nohz_full_setup(char *str)
140{
141 unsigned int flags;
142
Marcelo Tosatti9cc5b862020-05-27 16:29:09 +0200143 flags = HK_FLAG_TICK | HK_FLAG_WQ | HK_FLAG_TIMER | HK_FLAG_RCU |
144 HK_FLAG_MISC | HK_FLAG_KTHREAD;
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200145
146 return housekeeping_setup(str, flags);
147}
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200148__setup("nohz_full=", housekeeping_nohz_full_setup);
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200149
150static int __init housekeeping_isolcpus_setup(char *str)
151{
Frederic Weisbecker150dfee2017-10-27 04:42:38 +0200152 unsigned int flags = 0;
Peter Xu3662daf2020-04-03 18:35:17 -0400153 bool illegal = false;
154 char *par;
155 int len;
Frederic Weisbecker150dfee2017-10-27 04:42:38 +0200156
157 while (isalpha(*str)) {
158 if (!strncmp(str, "nohz,", 5)) {
159 str += 5;
160 flags |= HK_FLAG_TICK;
161 continue;
162 }
163
164 if (!strncmp(str, "domain,", 7)) {
165 str += 7;
166 flags |= HK_FLAG_DOMAIN;
167 continue;
168 }
169
Ming Lei11ea68f2020-01-20 17:16:25 +0800170 if (!strncmp(str, "managed_irq,", 12)) {
171 str += 12;
172 flags |= HK_FLAG_MANAGED_IRQ;
173 continue;
174 }
175
Peter Xu3662daf2020-04-03 18:35:17 -0400176 /*
177 * Skip unknown sub-parameter and validate that it is not
178 * containing an invalid character.
179 */
180 for (par = str, len = 0; *str && *str != ','; str++, len++) {
181 if (!isalpha(*str) && *str != '_')
182 illegal = true;
183 }
184
185 if (illegal) {
186 pr_warn("isolcpus: Invalid flag %.*s\n", len, par);
187 return 0;
188 }
189
190 pr_info("isolcpus: Skipped unknown flag %.*s\n", len, par);
191 str++;
Frederic Weisbecker150dfee2017-10-27 04:42:38 +0200192 }
193
194 /* Default behaviour for isolcpus without flags */
195 if (!flags)
196 flags |= HK_FLAG_DOMAIN;
197
198 return housekeeping_setup(str, flags);
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200199}
200__setup("isolcpus=", housekeeping_isolcpus_setup);