blob: 81faddba9e2011e2dff9a83f64e28fcc932e81c2 [file] [log] [blame]
Frederic Weisbecker78634062017-10-27 04:42:28 +02001/*
2 * Housekeeping management. Manage the targets for routine code that can run on
3 * any CPU: unbound workqueues, timers, kthreads and any offloadable work.
4 *
5 * Copyright (C) 2017 Red Hat, Inc., Frederic Weisbecker
Frederic Weisbecker1bda3f82018-02-21 05:17:26 +01006 * Copyright (C) 2017-2018 SUSE, Frederic Weisbecker
Frederic Weisbecker78634062017-10-27 04:42:28 +02007 *
8 */
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01009#include "sched.h"
Frederic Weisbecker78634062017-10-27 04:42:28 +020010
Ingo Molnardfcb2452018-12-03 10:05:56 +010011DEFINE_STATIC_KEY_FALSE(housekeeping_overridden);
12EXPORT_SYMBOL_GPL(housekeeping_overridden);
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020013static cpumask_var_t housekeeping_mask;
Frederic Weisbeckerde201552017-10-27 04:42:35 +020014static unsigned int housekeeping_flags;
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020015
Frederic Weisbeckerde201552017-10-27 04:42:35 +020016int housekeeping_any_cpu(enum hk_flags flags)
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020017{
Ingo Molnardfcb2452018-12-03 10:05:56 +010018 if (static_branch_unlikely(&housekeeping_overridden))
Frederic Weisbeckerde201552017-10-27 04:42:35 +020019 if (housekeeping_flags & flags)
20 return cpumask_any_and(housekeeping_mask, cpu_online_mask);
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020021 return smp_processor_id();
22}
23EXPORT_SYMBOL_GPL(housekeeping_any_cpu);
24
Frederic Weisbeckerde201552017-10-27 04:42:35 +020025const struct cpumask *housekeeping_cpumask(enum hk_flags flags)
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020026{
Ingo Molnardfcb2452018-12-03 10:05:56 +010027 if (static_branch_unlikely(&housekeeping_overridden))
Frederic Weisbeckerde201552017-10-27 04:42:35 +020028 if (housekeeping_flags & flags)
29 return housekeeping_mask;
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020030 return cpu_possible_mask;
31}
32EXPORT_SYMBOL_GPL(housekeeping_cpumask);
33
Frederic Weisbeckerde201552017-10-27 04:42:35 +020034void housekeeping_affine(struct task_struct *t, enum hk_flags flags)
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020035{
Ingo Molnardfcb2452018-12-03 10:05:56 +010036 if (static_branch_unlikely(&housekeeping_overridden))
Frederic Weisbeckerde201552017-10-27 04:42:35 +020037 if (housekeeping_flags & flags)
38 set_cpus_allowed_ptr(t, housekeeping_mask);
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020039}
40EXPORT_SYMBOL_GPL(housekeeping_affine);
41
Frederic Weisbeckerde201552017-10-27 04:42:35 +020042bool housekeeping_test_cpu(int cpu, enum hk_flags flags)
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020043{
Ingo Molnardfcb2452018-12-03 10:05:56 +010044 if (static_branch_unlikely(&housekeeping_overridden))
Frederic Weisbeckerde201552017-10-27 04:42:35 +020045 if (housekeeping_flags & flags)
46 return cpumask_test_cpu(cpu, housekeeping_mask);
Frederic Weisbecker7e56a1c2017-10-27 04:42:31 +020047 return true;
48}
49EXPORT_SYMBOL_GPL(housekeeping_test_cpu);
Frederic Weisbecker78634062017-10-27 04:42:28 +020050
51void __init housekeeping_init(void)
52{
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020053 if (!housekeeping_flags)
Frederic Weisbecker78634062017-10-27 04:42:28 +020054 return;
55
Ingo Molnardfcb2452018-12-03 10:05:56 +010056 static_branch_enable(&housekeeping_overridden);
Frederic Weisbeckere179f5a2017-10-27 04:42:32 +020057
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +010058 if (housekeeping_flags & HK_FLAG_TICK)
59 sched_tick_offload_init();
60
Frederic Weisbecker78634062017-10-27 04:42:28 +020061 /* We need at least one CPU to handle housekeeping work */
62 WARN_ON_ONCE(cpumask_empty(housekeeping_mask));
63}
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020064
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020065static int __init housekeeping_setup(char *str, enum hk_flags flags)
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020066{
67 cpumask_var_t non_housekeeping_mask;
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020068 int err;
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020069
70 alloc_bootmem_cpumask_var(&non_housekeeping_mask);
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020071 err = cpulist_parse(str, non_housekeeping_mask);
72 if (err < 0 || cpumask_last(non_housekeeping_mask) >= nr_cpu_ids) {
73 pr_warn("Housekeeping: nohz_full= or isolcpus= incorrect CPU range\n");
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020074 free_bootmem_cpumask_var(non_housekeeping_mask);
75 return 0;
76 }
77
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020078 if (!housekeeping_flags) {
79 alloc_bootmem_cpumask_var(&housekeeping_mask);
80 cpumask_andnot(housekeeping_mask,
81 cpu_possible_mask, non_housekeeping_mask);
82 if (cpumask_empty(housekeeping_mask))
83 cpumask_set_cpu(smp_processor_id(), housekeeping_mask);
84 } else {
85 cpumask_var_t tmp;
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020086
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020087 alloc_bootmem_cpumask_var(&tmp);
88 cpumask_andnot(tmp, cpu_possible_mask, non_housekeeping_mask);
89 if (!cpumask_equal(tmp, housekeeping_mask)) {
90 pr_warn("Housekeeping: nohz_full= must match isolcpus=\n");
91 free_bootmem_cpumask_var(tmp);
92 free_bootmem_cpumask_var(non_housekeeping_mask);
93 return 0;
94 }
95 free_bootmem_cpumask_var(tmp);
96 }
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +020097
Frederic Weisbeckeredb93822017-10-27 04:42:37 +020098 if ((flags & HK_FLAG_TICK) && !(housekeeping_flags & HK_FLAG_TICK)) {
99 if (IS_ENABLED(CONFIG_NO_HZ_FULL)) {
100 tick_nohz_full_setup(non_housekeeping_mask);
101 } else {
102 pr_warn("Housekeeping: nohz unsupported."
103 " Build with CONFIG_NO_HZ_FULL\n");
104 free_bootmem_cpumask_var(non_housekeeping_mask);
105 return 0;
106 }
107 }
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200108
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200109 housekeeping_flags |= flags;
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200110
111 free_bootmem_cpumask_var(non_housekeeping_mask);
112
113 return 1;
114}
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200115
116static int __init housekeeping_nohz_full_setup(char *str)
117{
118 unsigned int flags;
119
Frederic Weisbecker1bda3f82018-02-21 05:17:26 +0100120 flags = HK_FLAG_TICK | HK_FLAG_WQ | HK_FLAG_TIMER | HK_FLAG_RCU | HK_FLAG_MISC;
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200121
122 return housekeeping_setup(str, flags);
123}
Frederic Weisbecker6f1982f2017-10-27 04:42:36 +0200124__setup("nohz_full=", housekeeping_nohz_full_setup);
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200125
126static int __init housekeeping_isolcpus_setup(char *str)
127{
Frederic Weisbecker150dfee2017-10-27 04:42:38 +0200128 unsigned int flags = 0;
129
130 while (isalpha(*str)) {
131 if (!strncmp(str, "nohz,", 5)) {
132 str += 5;
133 flags |= HK_FLAG_TICK;
134 continue;
135 }
136
137 if (!strncmp(str, "domain,", 7)) {
138 str += 7;
139 flags |= HK_FLAG_DOMAIN;
140 continue;
141 }
142
143 pr_warn("isolcpus: Error, unknown flag\n");
144 return 0;
145 }
146
147 /* Default behaviour for isolcpus without flags */
148 if (!flags)
149 flags |= HK_FLAG_DOMAIN;
150
151 return housekeeping_setup(str, flags);
Frederic Weisbeckeredb93822017-10-27 04:42:37 +0200152}
153__setup("isolcpus=", housekeeping_isolcpus_setup);