Frederic Weisbecker | 7863406 | 2017-10-27 04:42:28 +0200 | [diff] [blame] | 1 | /* |
| 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 |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <linux/sched/isolation.h> |
| 10 | #include <linux/tick.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/kernel.h> |
Frederic Weisbecker | e179f5a | 2017-10-27 04:42:32 +0200 | [diff] [blame] | 13 | #include <linux/static_key.h> |
Frederic Weisbecker | 7863406 | 2017-10-27 04:42:28 +0200 | [diff] [blame] | 14 | |
Frederic Weisbecker | e179f5a | 2017-10-27 04:42:32 +0200 | [diff] [blame] | 15 | DEFINE_STATIC_KEY_FALSE(housekeeping_overriden); |
| 16 | EXPORT_SYMBOL_GPL(housekeeping_overriden); |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 17 | static cpumask_var_t housekeeping_mask; |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 18 | static unsigned int housekeeping_flags; |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 19 | |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 20 | int housekeeping_any_cpu(enum hk_flags flags) |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 21 | { |
Frederic Weisbecker | e179f5a | 2017-10-27 04:42:32 +0200 | [diff] [blame] | 22 | if (static_branch_unlikely(&housekeeping_overriden)) |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 23 | if (housekeeping_flags & flags) |
| 24 | return cpumask_any_and(housekeeping_mask, cpu_online_mask); |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 25 | return smp_processor_id(); |
| 26 | } |
| 27 | EXPORT_SYMBOL_GPL(housekeeping_any_cpu); |
| 28 | |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 29 | const struct cpumask *housekeeping_cpumask(enum hk_flags flags) |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 30 | { |
Frederic Weisbecker | e179f5a | 2017-10-27 04:42:32 +0200 | [diff] [blame] | 31 | if (static_branch_unlikely(&housekeeping_overriden)) |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 32 | if (housekeeping_flags & flags) |
| 33 | return housekeeping_mask; |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 34 | return cpu_possible_mask; |
| 35 | } |
| 36 | EXPORT_SYMBOL_GPL(housekeeping_cpumask); |
| 37 | |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 38 | void housekeeping_affine(struct task_struct *t, enum hk_flags flags) |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 39 | { |
Frederic Weisbecker | e179f5a | 2017-10-27 04:42:32 +0200 | [diff] [blame] | 40 | if (static_branch_unlikely(&housekeeping_overriden)) |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 41 | if (housekeeping_flags & flags) |
| 42 | set_cpus_allowed_ptr(t, housekeeping_mask); |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 43 | } |
| 44 | EXPORT_SYMBOL_GPL(housekeeping_affine); |
| 45 | |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 46 | bool housekeeping_test_cpu(int cpu, enum hk_flags flags) |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 47 | { |
Frederic Weisbecker | e179f5a | 2017-10-27 04:42:32 +0200 | [diff] [blame] | 48 | if (static_branch_unlikely(&housekeeping_overriden)) |
Frederic Weisbecker | de20155 | 2017-10-27 04:42:35 +0200 | [diff] [blame] | 49 | if (housekeeping_flags & flags) |
| 50 | return cpumask_test_cpu(cpu, housekeeping_mask); |
Frederic Weisbecker | 7e56a1c | 2017-10-27 04:42:31 +0200 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | EXPORT_SYMBOL_GPL(housekeeping_test_cpu); |
Frederic Weisbecker | 7863406 | 2017-10-27 04:42:28 +0200 | [diff] [blame] | 54 | |
| 55 | void __init housekeeping_init(void) |
| 56 | { |
Frederic Weisbecker | 6f1982f | 2017-10-27 04:42:36 +0200 | [diff] [blame^] | 57 | if (!housekeeping_flags) |
Frederic Weisbecker | 7863406 | 2017-10-27 04:42:28 +0200 | [diff] [blame] | 58 | return; |
| 59 | |
Frederic Weisbecker | e179f5a | 2017-10-27 04:42:32 +0200 | [diff] [blame] | 60 | static_branch_enable(&housekeeping_overriden); |
| 61 | |
Frederic Weisbecker | 7863406 | 2017-10-27 04:42:28 +0200 | [diff] [blame] | 62 | /* We need at least one CPU to handle housekeeping work */ |
| 63 | WARN_ON_ONCE(cpumask_empty(housekeeping_mask)); |
| 64 | } |
Frederic Weisbecker | 6f1982f | 2017-10-27 04:42:36 +0200 | [diff] [blame^] | 65 | |
| 66 | #ifdef CONFIG_NO_HZ_FULL |
| 67 | static int __init housekeeping_nohz_full_setup(char *str) |
| 68 | { |
| 69 | cpumask_var_t non_housekeeping_mask; |
| 70 | |
| 71 | alloc_bootmem_cpumask_var(&non_housekeeping_mask); |
| 72 | if (cpulist_parse(str, non_housekeeping_mask) < 0) { |
| 73 | pr_warn("Housekeeping: Incorrect nohz_full cpumask\n"); |
| 74 | free_bootmem_cpumask_var(non_housekeeping_mask); |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | alloc_bootmem_cpumask_var(&housekeeping_mask); |
| 79 | cpumask_andnot(housekeeping_mask, cpu_possible_mask, non_housekeeping_mask); |
| 80 | |
| 81 | if (cpumask_empty(housekeeping_mask)) |
| 82 | cpumask_set_cpu(smp_processor_id(), housekeeping_mask); |
| 83 | |
| 84 | housekeeping_flags = HK_FLAG_TICK | HK_FLAG_TIMER | |
| 85 | HK_FLAG_RCU | HK_FLAG_MISC; |
| 86 | |
| 87 | tick_nohz_full_setup(non_housekeeping_mask); |
| 88 | |
| 89 | free_bootmem_cpumask_var(non_housekeeping_mask); |
| 90 | |
| 91 | return 1; |
| 92 | } |
| 93 | __setup("nohz_full=", housekeeping_nohz_full_setup); |
| 94 | #endif |