Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
5a0e3ad6 | 2 | #include <linux/slab.h> |
ccb46000 AM |
3 | #include <linux/kernel.h> |
4 | #include <linux/bitops.h> | |
5 | #include <linux/cpumask.h> | |
8bc3bcc9 | 6 | #include <linux/export.h> |
57c8a661 | 7 | #include <linux/memblock.h> |
98fa15f3 | 8 | #include <linux/numa.h> |
ccb46000 | 9 | |
2d3854a3 RR |
10 | /* These are not inline because of header tangles. */ |
11 | #ifdef CONFIG_CPUMASK_OFFSTACK | |
ec26b805 MT |
12 | /** |
13 | * alloc_cpumask_var_node - allocate a struct cpumask on a given node | |
14 | * @mask: pointer to cpumask_var_t where the cpumask is returned | |
15 | * @flags: GFP_ flags | |
dcb60f9c | 16 | * @node: memory node from which to allocate or %NUMA_NO_NODE |
ec26b805 MT |
17 | * |
18 | * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is | |
57f728d5 RD |
19 | * a nop returning a constant 1 (in <linux/cpumask.h>). |
20 | * | |
21 | * Return: TRUE if memory allocation succeeded, FALSE otherwise. | |
ec26b805 MT |
22 | * |
23 | * In addition, mask will be NULL if this fails. Note that gcc is | |
24 | * usually smart enough to know that mask can never be NULL if | |
25 | * CONFIG_CPUMASK_OFFSTACK=n, so does code elimination in that case | |
26 | * too. | |
27 | */ | |
7b4967c5 | 28 | bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node) |
2d3854a3 | 29 | { |
38c7fed2 YL |
30 | *mask = kmalloc_node(cpumask_size(), flags, node); |
31 | ||
2d3854a3 RR |
32 | #ifdef CONFIG_DEBUG_PER_CPU_MAPS |
33 | if (!*mask) { | |
34 | printk(KERN_ERR "=> alloc_cpumask_var: failed!\n"); | |
35 | dump_stack(); | |
36 | } | |
37 | #endif | |
2a530080 | 38 | |
2d3854a3 RR |
39 | return *mask != NULL; |
40 | } | |
7b4967c5 MT |
41 | EXPORT_SYMBOL(alloc_cpumask_var_node); |
42 | ||
ec26b805 MT |
43 | /** |
44 | * alloc_bootmem_cpumask_var - allocate a struct cpumask from the bootmem arena. | |
45 | * @mask: pointer to cpumask_var_t where the cpumask is returned | |
46 | * | |
47 | * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is | |
e9690a6e | 48 | * a nop (in <linux/cpumask.h>). |
ec26b805 MT |
49 | * Either returns an allocated (zero-filled) cpumask, or causes the |
50 | * system to panic. | |
51 | */ | |
2d3854a3 RR |
52 | void __init alloc_bootmem_cpumask_var(cpumask_var_t *mask) |
53 | { | |
c6f23979 | 54 | *mask = memblock_alloc_or_panic(cpumask_size(), SMP_CACHE_BYTES); |
2d3854a3 RR |
55 | } |
56 | ||
ec26b805 MT |
57 | /** |
58 | * free_cpumask_var - frees memory allocated for a struct cpumask. | |
59 | * @mask: cpumask to free | |
60 | * | |
61 | * This is safe on a NULL mask. | |
62 | */ | |
2d3854a3 RR |
63 | void free_cpumask_var(cpumask_var_t mask) |
64 | { | |
65 | kfree(mask); | |
66 | } | |
67 | EXPORT_SYMBOL(free_cpumask_var); | |
cd83e42c | 68 | |
ec26b805 MT |
69 | /** |
70 | * free_bootmem_cpumask_var - frees result of alloc_bootmem_cpumask_var | |
71 | * @mask: cpumask to free | |
72 | */ | |
984f2f37 | 73 | void __init free_bootmem_cpumask_var(cpumask_var_t mask) |
cd83e42c | 74 | { |
4421cca0 | 75 | memblock_free(mask, cpumask_size()); |
cd83e42c | 76 | } |
2d3854a3 | 77 | #endif |
da91309e AV |
78 | |
79 | /** | |
406d394a | 80 | * cpumask_local_spread - select the i'th cpu based on NUMA distances |
da91309e | 81 | * @i: index number |
f36963c9 | 82 | * @node: local numa_node |
da91309e | 83 | * |
57f728d5 | 84 | * Return: online CPU according to a numa aware policy; local cpus are returned |
2ac4980c | 85 | * first, followed by non-local ones, then it wraps around. |
da91309e | 86 | * |
2ac4980c YN |
87 | * For those who wants to enumerate all CPUs based on their NUMA distances, |
88 | * i.e. call this function in a loop, like: | |
89 | * | |
90 | * for (i = 0; i < num_online_cpus(); i++) { | |
91 | * cpu = cpumask_local_spread(i, node); | |
92 | * do_something(cpu); | |
93 | * } | |
94 | * | |
95 | * There's a better alternative based on for_each()-like iterators: | |
96 | * | |
97 | * for_each_numa_hop_mask(mask, node) { | |
98 | * for_each_cpu_andnot(cpu, mask, prev) | |
99 | * do_something(cpu); | |
100 | * prev = mask; | |
101 | * } | |
102 | * | |
103 | * It's simpler and more verbose than above. Complexity of iterator-based | |
104 | * enumeration is O(sched_domains_numa_levels * nr_cpu_ids), while | |
105 | * cpumask_local_spread() when called for each cpu is | |
106 | * O(sched_domains_numa_levels * nr_cpu_ids * log(nr_cpu_ids)). | |
da91309e | 107 | */ |
f36963c9 | 108 | unsigned int cpumask_local_spread(unsigned int i, int node) |
da91309e | 109 | { |
8b6b795d | 110 | unsigned int cpu; |
da91309e | 111 | |
f36963c9 | 112 | /* Wrap: we always want a cpu. */ |
2452483d | 113 | i %= num_online_cpus(); |
da91309e | 114 | |
9ecea9ae | 115 | cpu = sched_numa_find_nth_cpu(cpu_online_mask, i, node); |
b1beed72 YN |
116 | |
117 | WARN_ON(cpu >= nr_cpu_ids); | |
118 | return cpu; | |
da91309e | 119 | } |
f36963c9 | 120 | EXPORT_SYMBOL(cpumask_local_spread); |
46a87b38 PT |
121 | |
122 | static DEFINE_PER_CPU(int, distribute_cpu_mask_prev); | |
123 | ||
124 | /** | |
dcb60f9c RD |
125 | * cpumask_any_and_distribute - Return an arbitrary cpu within src1p & src2p. |
126 | * @src1p: first &cpumask for intersection | |
127 | * @src2p: second &cpumask for intersection | |
46a87b38 PT |
128 | * |
129 | * Iterated calls using the same srcp1 and srcp2 will be distributed within | |
130 | * their intersection. | |
131 | * | |
57f728d5 | 132 | * Return: >= nr_cpu_ids if the intersection is empty. |
46a87b38 | 133 | */ |
8b6b795d | 134 | unsigned int cpumask_any_and_distribute(const struct cpumask *src1p, |
46a87b38 PT |
135 | const struct cpumask *src2p) |
136 | { | |
8b6b795d | 137 | unsigned int next, prev; |
46a87b38 PT |
138 | |
139 | /* NOTE: our first selection will skip 0. */ | |
140 | prev = __this_cpu_read(distribute_cpu_mask_prev); | |
141 | ||
566babe8 | 142 | next = cpumask_next_and_wrap(prev, src1p, src2p); |
46a87b38 PT |
143 | if (next < nr_cpu_ids) |
144 | __this_cpu_write(distribute_cpu_mask_prev, next); | |
145 | ||
146 | return next; | |
147 | } | |
148 | EXPORT_SYMBOL(cpumask_any_and_distribute); | |
14e292f8 | 149 | |
57f728d5 RD |
150 | /** |
151 | * cpumask_any_distribute - Return an arbitrary cpu from srcp | |
152 | * @srcp: &cpumask for selection | |
153 | * | |
154 | * Return: >= nr_cpu_ids if the intersection is empty. | |
155 | */ | |
8b6b795d | 156 | unsigned int cpumask_any_distribute(const struct cpumask *srcp) |
14e292f8 | 157 | { |
8b6b795d | 158 | unsigned int next, prev; |
14e292f8 PZ |
159 | |
160 | /* NOTE: our first selection will skip 0. */ | |
161 | prev = __this_cpu_read(distribute_cpu_mask_prev); | |
566babe8 | 162 | next = cpumask_next_wrap(prev, srcp); |
14e292f8 PZ |
163 | if (next < nr_cpu_ids) |
164 | __this_cpu_write(distribute_cpu_mask_prev, next); | |
165 | ||
166 | return next; | |
167 | } | |
168 | EXPORT_SYMBOL(cpumask_any_distribute); |