blob: 123ea07a3f3b048089dc1e3e1c6ad8f422ed50ab [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
Frederic Weisbeckerde201552017-10-27 04:42:35 +020017int housekeeping_any_cpu(enum hk_flags flags)
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020018{
Ingo Molnardfcb2452018-12-03 10:05:56 +010019 if (static_branch_unlikely(&housekeeping_overridden))
Frederic Weisbeckerde201552017-10-27 04:42:35 +020020 if (housekeeping_flags & flags)
21 return cpumask_any_and(housekeeping_mask, cpu_online_mask);
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020022 return smp_processor_id();
23}
24EXPORT_SYMBOL_GPL(housekeeping_any_cpu);
25
Frederic Weisbeckerde201552017-10-27 04:42:35 +020026const struct cpumask *housekeeping_cpumask(enum hk_flags flags)
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020027{
Ingo Molnardfcb2452018-12-03 10:05:56 +010028 if (static_branch_unlikely(&housekeeping_overridden))
Frederic Weisbeckerde201552017-10-27 04:42:35 +020029 if (housekeeping_flags & flags)
30 return housekeeping_mask;
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020031 return cpu_possible_mask;
32}
33EXPORT_SYMBOL_GPL(housekeeping_cpumask);
34
Frederic Weisbeckerde201552017-10-27 04:42:35 +020035void housekeeping_affine(struct task_struct *t, enum hk_flags flags)
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020036{
Ingo Molnardfcb2452018-12-03 10:05:56 +010037 if (static_branch_unlikely(&housekeeping_overridden))
Frederic Weisbeckerde201552017-10-27 04:42:35 +020038 if (housekeeping_flags & flags)
39 set_cpus_allowed_ptr(t, housekeeping_mask);
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020040}
41EXPORT_SYMBOL_GPL(housekeeping_affine);
42
Frederic Weisbeckerde201552017-10-27 04:42:35 +020043bool housekeeping_test_cpu(int cpu, enum hk_flags flags)
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020044{
Ingo Molnardfcb2452018-12-03 10:05:56 +010045 if (static_branch_unlikely(&housekeeping_overridden))
Frederic Weisbeckerde201552017-10-27 04:42:35 +020046 if (housekeeping_flags & flags)
47 return cpumask_test_cpu(cpu, housekeeping_mask);
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020048 return true;
49}
50EXPORT_SYMBOL_GPL(housekeeping_test_cpu);
Frederic Weisbecker78634062017-10-27 04:42:28 +020051
52void __init housekeeping_init(void)
53{
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020054 if (!housekeeping_flags)
Frederic Weisbecker78634062017-10-27 04:42:28 +020055 return;
56
Ingo Molnardfcb2452018-12-03 10:05:56 +010057 static_branch_enable(&housekeeping_overridden);
Frederic Weisbeckere179f5a2017-10-27 04:42:32 +020058
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +010059 if (housekeeping_flags & HK_FLAG_TICK)
60 sched_tick_offload_init();
61
Frederic Weisbecker78634062017-10-27 04:42:28 +020062 /* We need at least one CPU to handle housekeeping work */
63 WARN_ON_ONCE(cpumask_empty(housekeeping_mask));
64}
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020065
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020066static int __init housekeeping_setup(char *str, enum hk_flags flags)
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020067{
68 cpumask_var_t non_housekeeping_mask;
Nicholas Piggin92195652019-04-11 13:34:47 +100069 cpumask_var_t tmp;
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020070 int err;
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020071
72 alloc_bootmem_cpumask_var(&non_housekeeping_mask);
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020073 err = cpulist_parse(str, non_housekeeping_mask);
74 if (err < 0 || cpumask_last(non_housekeeping_mask) >= nr_cpu_ids) {
75 pr_warn("Housekeeping: nohz_full= or isolcpus= incorrect CPU range\n");
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020076 free_bootmem_cpumask_var(non_housekeeping_mask);
77 return 0;
78 }
79
Nicholas Piggin92195652019-04-11 13:34:47 +100080 alloc_bootmem_cpumask_var(&tmp);
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020081 if (!housekeeping_flags) {
82 alloc_bootmem_cpumask_var(&housekeeping_mask);
83 cpumask_andnot(housekeeping_mask,
84 cpu_possible_mask, non_housekeeping_mask);
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020085
Nicholas Piggin92195652019-04-11 13:34:47 +100086 cpumask_andnot(tmp, cpu_present_mask, non_housekeeping_mask);
87 if (cpumask_empty(tmp)) {
88 pr_warn("Housekeeping: must include one present CPU, "
89 "using boot CPU:%d\n", smp_processor_id());
90 __cpumask_set_cpu(smp_processor_id(), housekeeping_mask);
91 __cpumask_clear_cpu(smp_processor_id(), non_housekeeping_mask);
92 }
93 } else {
94 cpumask_andnot(tmp, cpu_present_mask, non_housekeeping_mask);
95 if (cpumask_empty(tmp))
96 __cpumask_clear_cpu(smp_processor_id(), non_housekeeping_mask);
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020097 cpumask_andnot(tmp, cpu_possible_mask, non_housekeeping_mask);
98 if (!cpumask_equal(tmp, housekeeping_mask)) {
99 pr_warn("Housekeeping: nohz_full= must match isolcpus=\n");
100 free_bootmem_cpumask_var(tmp);
101 free_bootmem_cpumask_var(non_housekeeping_mask);
102 return 0;
103 }
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200104 }
Nicholas Piggin92195652019-04-11 13:34:47 +1000105 free_bootmem_cpumask_var(tmp);
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200106
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200107 if ((flags & HK_FLAG_TICK) && !(housekeeping_flags & HK_FLAG_TICK)) {
108 if (IS_ENABLED(CONFIG_NO_HZ_FULL)) {
109 tick_nohz_full_setup(non_housekeeping_mask);
110 } else {
111 pr_warn("Housekeeping: nohz unsupported."
112 " Build with CONFIG_NO_HZ_FULL\n");
113 free_bootmem_cpumask_var(non_housekeeping_mask);
114 return 0;
115 }
116 }
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200117
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200118 housekeeping_flags |= flags;
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200119
120 free_bootmem_cpumask_var(non_housekeeping_mask);
121
122 return 1;
123}
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200124
125static int __init housekeeping_nohz_full_setup(char *str)
126{
127 unsigned int flags;
128
Frederic Weisbecker1bda3f82018-02-21 05:17:26 +0100129 flags = HK_FLAG_TICK | HK_FLAG_WQ | HK_FLAG_TIMER | HK_FLAG_RCU | HK_FLAG_MISC;
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200130
131 return housekeeping_setup(str, flags);
132}
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200133__setup("nohz_full=", housekeeping_nohz_full_setup);
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200134
135static int __init housekeeping_isolcpus_setup(char *str)
136{
Frederic Weisbecker150dfee2017-10-27 04:42:38 +0200137 unsigned int flags = 0;
138
139 while (isalpha(*str)) {
140 if (!strncmp(str, "nohz,", 5)) {
141 str += 5;
142 flags |= HK_FLAG_TICK;
143 continue;
144 }
145
146 if (!strncmp(str, "domain,", 7)) {
147 str += 7;
148 flags |= HK_FLAG_DOMAIN;
149 continue;
150 }
151
152 pr_warn("isolcpus: Error, unknown flag\n");
153 return 0;
154 }
155
156 /* Default behaviour for isolcpus without flags */
157 if (!flags)
158 flags |= HK_FLAG_DOMAIN;
159
160 return housekeeping_setup(str, flags);
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200161}
162__setup("isolcpus=", housekeeping_isolcpus_setup);