1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_CPUMASK_H
3 #define __LINUX_CPUMASK_H
4
5 /*
6 * Cpumasks provide a bitmap suitable for representing the
7 * set of CPUs in a system, one bit position per CPU number. In general,
8 * only nr_cpu_ids (<= NR_CPUS) bits are valid.
9 */
10 #include <linux/atomic.h>
11 #include <linux/bitmap.h>
12 #include <linux/cleanup.h>
13 #include <linux/cpumask_types.h>
14 #include <linux/gfp_types.h>
15 #include <linux/numa.h>
16 #include <linux/threads.h>
17 #include <linux/types.h>
18 #include <vdso/page.h>
19
20 #include <asm/bug.h>
21
22 /**
23 * cpumask_pr_args - printf args to output a cpumask
24 * @maskp: cpumask to be printed
25 *
26 * Can be used to provide arguments for '%*pb[l]' when printing a cpumask.
27 */
28 #define cpumask_pr_args(maskp) nr_cpu_ids, cpumask_bits(maskp)
29
30 #if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS)
31 #define nr_cpu_ids ((unsigned int)NR_CPUS)
32 #else
33 extern unsigned int nr_cpu_ids;
34 #endif
35
set_nr_cpu_ids(unsigned int nr)36 static __always_inline void set_nr_cpu_ids(unsigned int nr)
37 {
38 #if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS)
39 WARN_ON(nr != nr_cpu_ids);
40 #else
41 nr_cpu_ids = nr;
42 #endif
43 }
44
45 /*
46 * We have several different "preferred sizes" for the cpumask
47 * operations, depending on operation.
48 *
49 * For example, the bitmap scanning and operating operations have
50 * optimized routines that work for the single-word case, but only when
51 * the size is constant. So if NR_CPUS fits in one single word, we are
52 * better off using that small constant, in order to trigger the
53 * optimized bit finding. That is 'small_cpumask_size'.
54 *
55 * The clearing and copying operations will similarly perform better
56 * with a constant size, but we limit that size arbitrarily to four
57 * words. We call this 'large_cpumask_size'.
58 *
59 * Finally, some operations just want the exact limit, either because
60 * they set bits or just don't have any faster fixed-sized versions. We
61 * call this just 'nr_cpumask_bits'.
62 *
63 * Note that these optional constants are always guaranteed to be at
64 * least as big as 'nr_cpu_ids' itself is, and all our cpumask
65 * allocations are at least that size (see cpumask_size()). The
66 * optimization comes from being able to potentially use a compile-time
67 * constant instead of a run-time generated exact number of CPUs.
68 */
69 #if NR_CPUS <= BITS_PER_LONG
70 #define small_cpumask_bits ((unsigned int)NR_CPUS)
71 #define large_cpumask_bits ((unsigned int)NR_CPUS)
72 #elif NR_CPUS <= 4*BITS_PER_LONG
73 #define small_cpumask_bits nr_cpu_ids
74 #define large_cpumask_bits ((unsigned int)NR_CPUS)
75 #else
76 #define small_cpumask_bits nr_cpu_ids
77 #define large_cpumask_bits nr_cpu_ids
78 #endif
79 #define nr_cpumask_bits nr_cpu_ids
80
81 /*
82 * The following particular system cpumasks and operations manage
83 * possible, present, active and online cpus.
84 *
85 * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
86 * cpu_present_mask - has bit 'cpu' set iff cpu is populated
87 * cpu_enabled_mask - has bit 'cpu' set iff cpu can be brought online
88 * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler
89 * cpu_active_mask - has bit 'cpu' set iff cpu available to migration
90 *
91 * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
92 *
93 * The cpu_possible_mask is fixed at boot time, as the set of CPU IDs
94 * that it is possible might ever be plugged in at anytime during the
95 * life of that system boot. The cpu_present_mask is dynamic(*),
96 * representing which CPUs are currently plugged in. And
97 * cpu_online_mask is the dynamic subset of cpu_present_mask,
98 * indicating those CPUs available for scheduling.
99 *
100 * If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
101 * depending on what ACPI reports as currently plugged in, otherwise
102 * cpu_present_mask is just a copy of cpu_possible_mask.
103 *
104 * (*) Well, cpu_present_mask is dynamic in the hotplug case. If not
105 * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
106 *
107 * Subtleties:
108 * 1) UP ARCHes (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
109 * assumption that their single CPU is online. The UP
110 * cpu_{online,possible,present}_masks are placebos. Changing them
111 * will have no useful affect on the following num_*_cpus()
112 * and cpu_*() macros in the UP case. This ugliness is a UP
113 * optimization - don't waste any instructions or memory references
114 * asking if you're online or how many CPUs there are if there is
115 * only one CPU.
116 */
117
118 extern struct cpumask __cpu_possible_mask;
119 extern struct cpumask __cpu_online_mask;
120 extern struct cpumask __cpu_enabled_mask;
121 extern struct cpumask __cpu_present_mask;
122 extern struct cpumask __cpu_active_mask;
123 extern struct cpumask __cpu_dying_mask;
124
125 #ifdef CONFIG_PREFERRED_CPU
126 extern struct cpumask __cpu_preferred_mask;
127 #else
128 #define __cpu_preferred_mask __cpu_active_mask
129 #endif
130
131 #define cpu_possible_mask ((const struct cpumask *)&__cpu_possible_mask)
132 #define cpu_online_mask ((const struct cpumask *)&__cpu_online_mask)
133 #define cpu_enabled_mask ((const struct cpumask *)&__cpu_enabled_mask)
134 #define cpu_present_mask ((const struct cpumask *)&__cpu_present_mask)
135 #define cpu_active_mask ((const struct cpumask *)&__cpu_active_mask)
136 #define cpu_dying_mask ((const struct cpumask *)&__cpu_dying_mask)
137 #define cpu_preferred_mask ((const struct cpumask *)&__cpu_preferred_mask)
138
139 extern atomic_t __num_online_cpus;
140 extern unsigned int __num_possible_cpus;
141
142 extern cpumask_t cpus_booted_once_mask;
143
cpu_max_bits_warn(unsigned int cpu,unsigned int bits)144 static __always_inline void cpu_max_bits_warn(unsigned int cpu, unsigned int bits)
145 {
146 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
147 WARN_ON_ONCE(cpu >= bits);
148 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
149 }
150
151 /* verify cpu argument to cpumask_* operators */
cpumask_check(unsigned int cpu)152 static __always_inline unsigned int cpumask_check(unsigned int cpu)
153 {
154 cpu_max_bits_warn(cpu, small_cpumask_bits);
155 return cpu;
156 }
157
158 /**
159 * cpumask_first - get the first cpu in a cpumask
160 * @srcp: the cpumask pointer
161 *
162 * Return: >= nr_cpu_ids if no cpus set.
163 */
cpumask_first(const struct cpumask * srcp)164 static __always_inline unsigned int cpumask_first(const struct cpumask *srcp)
165 {
166 return find_first_bit(cpumask_bits(srcp), small_cpumask_bits);
167 }
168
169 /**
170 * cpumask_first_zero - get the first unset cpu in a cpumask
171 * @srcp: the cpumask pointer
172 *
173 * Return: >= nr_cpu_ids if all cpus are set.
174 */
cpumask_first_zero(const struct cpumask * srcp)175 static __always_inline unsigned int cpumask_first_zero(const struct cpumask *srcp)
176 {
177 return find_first_zero_bit(cpumask_bits(srcp), small_cpumask_bits);
178 }
179
180 /**
181 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
182 * @srcp1: the first input
183 * @srcp2: the second input
184 *
185 * Return: >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
186 */
187 static __always_inline
cpumask_first_and(const struct cpumask * srcp1,const struct cpumask * srcp2)188 unsigned int cpumask_first_and(const struct cpumask *srcp1, const struct cpumask *srcp2)
189 {
190 return find_first_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
191 }
192
193 /**
194 * cpumask_first_andnot - return the first cpu from *srcp1 & ~*srcp2
195 * @srcp1: the first input
196 * @srcp2: the second input
197 *
198 * Return: >= nr_cpu_ids if no such cpu found.
199 */
200 static __always_inline
cpumask_first_andnot(const struct cpumask * srcp1,const struct cpumask * srcp2)201 unsigned int cpumask_first_andnot(const struct cpumask *srcp1, const struct cpumask *srcp2)
202 {
203 return find_first_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
204 }
205
206 /**
207 * cpumask_first_and_and - return the first cpu from *srcp1 & *srcp2 & *srcp3
208 * @srcp1: the first input
209 * @srcp2: the second input
210 * @srcp3: the third input
211 *
212 * Return: >= nr_cpu_ids if no cpus set in all.
213 */
214 static __always_inline
cpumask_first_and_and(const struct cpumask * srcp1,const struct cpumask * srcp2,const struct cpumask * srcp3)215 unsigned int cpumask_first_and_and(const struct cpumask *srcp1,
216 const struct cpumask *srcp2,
217 const struct cpumask *srcp3)
218 {
219 return find_first_and_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
220 cpumask_bits(srcp3), small_cpumask_bits);
221 }
222
223 /**
224 * cpumask_last - get the last CPU in a cpumask
225 * @srcp: - the cpumask pointer
226 *
227 * Return: >= nr_cpumask_bits if no CPUs set.
228 */
cpumask_last(const struct cpumask * srcp)229 static __always_inline unsigned int cpumask_last(const struct cpumask *srcp)
230 {
231 return find_last_bit(cpumask_bits(srcp), small_cpumask_bits);
232 }
233
234 /**
235 * cpumask_next - get the next cpu in a cpumask
236 * @n: the cpu prior to the place to search (i.e. return will be > @n)
237 * @srcp: the cpumask pointer
238 *
239 * Return: >= nr_cpu_ids if no further cpus set.
240 */
241 static __always_inline
cpumask_next(int n,const struct cpumask * srcp)242 unsigned int cpumask_next(int n, const struct cpumask *srcp)
243 {
244 /* -1 is a legal arg here. */
245 if (n != -1)
246 cpumask_check(n);
247 return find_next_bit(cpumask_bits(srcp), small_cpumask_bits, n + 1);
248 }
249
250 /**
251 * cpumask_next_zero - get the next unset cpu in a cpumask
252 * @n: the cpu prior to the place to search (i.e. return will be > @n)
253 * @srcp: the cpumask pointer
254 *
255 * Return: >= nr_cpu_ids if no further cpus unset.
256 */
257 static __always_inline
cpumask_next_zero(int n,const struct cpumask * srcp)258 unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
259 {
260 /* -1 is a legal arg here. */
261 if (n != -1)
262 cpumask_check(n);
263 return find_next_zero_bit(cpumask_bits(srcp), small_cpumask_bits, n+1);
264 }
265
266 #if NR_CPUS == 1
267 /* Uniprocessor: there is only one valid CPU */
268 static __always_inline
cpumask_local_spread(unsigned int i,int node)269 unsigned int cpumask_local_spread(unsigned int i, int node)
270 {
271 return 0;
272 }
273
274 static __always_inline
cpumask_any_and_distribute(const struct cpumask * src1p,const struct cpumask * src2p)275 unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
276 const struct cpumask *src2p)
277 {
278 return cpumask_first_and(src1p, src2p);
279 }
280
281 static __always_inline
cpumask_any_distribute(const struct cpumask * srcp)282 unsigned int cpumask_any_distribute(const struct cpumask *srcp)
283 {
284 return cpumask_first(srcp);
285 }
286 #else
287 unsigned int cpumask_local_spread(unsigned int i, int node);
288 unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
289 const struct cpumask *src2p);
290 unsigned int cpumask_any_distribute(const struct cpumask *srcp);
291 #endif /* NR_CPUS */
292
293 /**
294 * cpumask_next_and - get the next cpu in *src1p & *src2p
295 * @n: the cpu prior to the place to search (i.e. return will be > @n)
296 * @src1p: the first cpumask pointer
297 * @src2p: the second cpumask pointer
298 *
299 * Return: >= nr_cpu_ids if no further cpus set in both.
300 */
301 static __always_inline
cpumask_next_and(int n,const struct cpumask * src1p,const struct cpumask * src2p)302 unsigned int cpumask_next_and(int n, const struct cpumask *src1p,
303 const struct cpumask *src2p)
304 {
305 /* -1 is a legal arg here. */
306 if (n != -1)
307 cpumask_check(n);
308 return find_next_and_bit(cpumask_bits(src1p), cpumask_bits(src2p),
309 small_cpumask_bits, n + 1);
310 }
311
312 /**
313 * cpumask_next_andnot - get the next cpu in *src1p & ~*src2p
314 * @n: the cpu prior to the place to search (i.e. return will be > @n)
315 * @src1p: the first cpumask pointer
316 * @src2p: the second cpumask pointer
317 *
318 * Return: >= nr_cpu_ids if no further cpus set in both.
319 */
320 static __always_inline
cpumask_next_andnot(int n,const struct cpumask * src1p,const struct cpumask * src2p)321 unsigned int cpumask_next_andnot(int n, const struct cpumask *src1p,
322 const struct cpumask *src2p)
323 {
324 /* -1 is a legal arg here. */
325 if (n != -1)
326 cpumask_check(n);
327 return find_next_andnot_bit(cpumask_bits(src1p), cpumask_bits(src2p),
328 small_cpumask_bits, n + 1);
329 }
330
331 /**
332 * cpumask_next_and_wrap - get the next cpu in *src1p & *src2p, starting from
333 * @n+1. If nothing found, wrap around and start from
334 * the beginning
335 * @n: the cpu prior to the place to search (i.e. search starts from @n+1)
336 * @src1p: the first cpumask pointer
337 * @src2p: the second cpumask pointer
338 *
339 * Return: next set bit, wrapped if needed, or >= nr_cpu_ids if @src1p & @src2p is empty.
340 */
341 static __always_inline
cpumask_next_and_wrap(int n,const struct cpumask * src1p,const struct cpumask * src2p)342 unsigned int cpumask_next_and_wrap(int n, const struct cpumask *src1p,
343 const struct cpumask *src2p)
344 {
345 /* -1 is a legal arg here. */
346 if (n != -1)
347 cpumask_check(n);
348 return find_next_and_bit_wrap(cpumask_bits(src1p), cpumask_bits(src2p),
349 small_cpumask_bits, n + 1);
350 }
351
352 /**
353 * cpumask_next_wrap - get the next cpu in *src, starting from @n+1. If nothing
354 * found, wrap around and start from the beginning
355 * @n: the cpu prior to the place to search (i.e. search starts from @n+1)
356 * @src: cpumask pointer
357 *
358 * Return: next set bit, wrapped if needed, or >= nr_cpu_ids if @src is empty.
359 */
360 static __always_inline
cpumask_next_wrap(int n,const struct cpumask * src)361 unsigned int cpumask_next_wrap(int n, const struct cpumask *src)
362 {
363 /* -1 is a legal arg here. */
364 if (n != -1)
365 cpumask_check(n);
366 return find_next_bit_wrap(cpumask_bits(src), small_cpumask_bits, n + 1);
367 }
368
369 /**
370 * cpumask_random - get random cpu in *src.
371 * @src: cpumask pointer
372 *
373 * Return: random set bit, or >= nr_cpu_ids if @src is empty.
374 */
375 static __always_inline
cpumask_random(const struct cpumask * src)376 unsigned int cpumask_random(const struct cpumask *src)
377 {
378 return find_random_bit(cpumask_bits(src), nr_cpu_ids);
379 }
380
381 /**
382 * for_each_cpu - iterate over every cpu in a mask
383 * @cpu: the (optionally unsigned) integer iterator
384 * @mask: the cpumask pointer
385 *
386 * After the loop, cpu is >= nr_cpu_ids.
387 */
388 #define for_each_cpu(cpu, mask) \
389 for_each_set_bit(cpu, cpumask_bits(mask), small_cpumask_bits)
390
391 /**
392 * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a specified location
393 * @cpu: the (optionally unsigned) integer iterator
394 * @mask: the cpumask pointer
395 * @start: the start location
396 *
397 * The implementation does not assume any bit in @mask is set (including @start).
398 *
399 * After the loop, cpu is >= nr_cpu_ids.
400 */
401 #define for_each_cpu_wrap(cpu, mask, start) \
402 for_each_set_bit_wrap(cpu, cpumask_bits(mask), small_cpumask_bits, start)
403
404 /**
405 * for_each_cpu_and - iterate over every cpu in both masks
406 * @cpu: the (optionally unsigned) integer iterator
407 * @mask1: the first cpumask pointer
408 * @mask2: the second cpumask pointer
409 *
410 * This saves a temporary CPU mask in many places. It is equivalent to:
411 * struct cpumask tmp;
412 * cpumask_and(&tmp, &mask1, &mask2);
413 * for_each_cpu(cpu, &tmp)
414 * ...
415 *
416 * After the loop, cpu is >= nr_cpu_ids.
417 */
418 #define for_each_cpu_and(cpu, mask1, mask2) \
419 for_each_and_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
420
421 /**
422 * for_each_cpu_andnot - iterate over every cpu present in one mask, excluding
423 * those present in another.
424 * @cpu: the (optionally unsigned) integer iterator
425 * @mask1: the first cpumask pointer
426 * @mask2: the second cpumask pointer
427 *
428 * This saves a temporary CPU mask in many places. It is equivalent to:
429 * struct cpumask tmp;
430 * cpumask_andnot(&tmp, &mask1, &mask2);
431 * for_each_cpu(cpu, &tmp)
432 * ...
433 *
434 * After the loop, cpu is >= nr_cpu_ids.
435 */
436 #define for_each_cpu_andnot(cpu, mask1, mask2) \
437 for_each_andnot_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
438
439 /**
440 * for_each_cpu_or - iterate over every cpu present in either mask
441 * @cpu: the (optionally unsigned) integer iterator
442 * @mask1: the first cpumask pointer
443 * @mask2: the second cpumask pointer
444 *
445 * This saves a temporary CPU mask in many places. It is equivalent to:
446 * struct cpumask tmp;
447 * cpumask_or(&tmp, &mask1, &mask2);
448 * for_each_cpu(cpu, &tmp)
449 * ...
450 *
451 * After the loop, cpu is >= nr_cpu_ids.
452 */
453 #define for_each_cpu_or(cpu, mask1, mask2) \
454 for_each_or_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
455
456 /**
457 * for_each_cpu_from - iterate over CPUs present in @mask, from @cpu to the end of @mask.
458 * @cpu: the (optionally unsigned) integer iterator
459 * @mask: the cpumask pointer
460 *
461 * After the loop, cpu is >= nr_cpu_ids.
462 */
463 #define for_each_cpu_from(cpu, mask) \
464 for_each_set_bit_from(cpu, cpumask_bits(mask), small_cpumask_bits)
465
466 /**
467 * cpumask_any_but - return an arbitrary cpu in a cpumask, but not this one.
468 * @mask: the cpumask to search
469 * @cpu: the cpu to ignore.
470 *
471 * Often used to find any cpu but smp_processor_id() in a mask.
472 * If @cpu == -1, the function is equivalent to cpumask_any().
473 * Return: >= nr_cpu_ids if no cpus set.
474 */
475 static __always_inline
cpumask_any_but(const struct cpumask * mask,int cpu)476 unsigned int cpumask_any_but(const struct cpumask *mask, int cpu)
477 {
478 unsigned int i;
479
480 /* -1 is a legal arg here. */
481 if (cpu != -1)
482 cpumask_check(cpu);
483
484 for_each_cpu(i, mask)
485 if (i != cpu)
486 break;
487 return i;
488 }
489
490 /**
491 * cpumask_any_and_but - pick an arbitrary cpu from *mask1 & *mask2, but not this one.
492 * @mask1: the first input cpumask
493 * @mask2: the second input cpumask
494 * @cpu: the cpu to ignore
495 *
496 * If @cpu == -1, the function is equivalent to cpumask_any_and().
497 * Returns >= nr_cpu_ids if no cpus set.
498 */
499 static __always_inline
cpumask_any_and_but(const struct cpumask * mask1,const struct cpumask * mask2,int cpu)500 unsigned int cpumask_any_and_but(const struct cpumask *mask1,
501 const struct cpumask *mask2,
502 int cpu)
503 {
504 unsigned int i;
505
506 /* -1 is a legal arg here. */
507 if (cpu != -1)
508 cpumask_check(cpu);
509
510 i = cpumask_first_and(mask1, mask2);
511 if (i != cpu)
512 return i;
513
514 return cpumask_next_and(cpu, mask1, mask2);
515 }
516
517 /**
518 * cpumask_any_andnot_but - pick an arbitrary cpu from *mask1 & ~*mask2, but not this one.
519 * @mask1: the first input cpumask
520 * @mask2: the second input cpumask
521 * @cpu: the cpu to ignore
522 *
523 * If @cpu == -1, the function returns the first matching cpu.
524 * Returns >= nr_cpu_ids if no cpus set.
525 */
526 static __always_inline
cpumask_any_andnot_but(const struct cpumask * mask1,const struct cpumask * mask2,int cpu)527 unsigned int cpumask_any_andnot_but(const struct cpumask *mask1,
528 const struct cpumask *mask2,
529 int cpu)
530 {
531 unsigned int i;
532
533 /* -1 is a legal arg here. */
534 if (cpu != -1)
535 cpumask_check(cpu);
536
537 i = cpumask_first_andnot(mask1, mask2);
538 if (i != cpu)
539 return i;
540
541 return cpumask_next_andnot(cpu, mask1, mask2);
542 }
543
544 /**
545 * cpumask_nth - get the Nth cpu in a cpumask
546 * @srcp: the cpumask pointer
547 * @cpu: the Nth cpu to find, starting from 0
548 *
549 * Return: >= nr_cpu_ids if such cpu doesn't exist.
550 */
551 static __always_inline
cpumask_nth(unsigned int cpu,const struct cpumask * srcp)552 unsigned int cpumask_nth(unsigned int cpu, const struct cpumask *srcp)
553 {
554 return find_nth_bit(cpumask_bits(srcp), small_cpumask_bits, cpumask_check(cpu));
555 }
556
557 /**
558 * cpumask_nth_and - get the Nth cpu in 2 cpumasks
559 * @srcp1: the cpumask pointer
560 * @srcp2: the cpumask pointer
561 * @cpu: the Nth cpu to find, starting from 0
562 *
563 * Return: >= nr_cpu_ids if such cpu doesn't exist.
564 */
565 static __always_inline
cpumask_nth_and(unsigned int cpu,const struct cpumask * srcp1,const struct cpumask * srcp2)566 unsigned int cpumask_nth_and(unsigned int cpu, const struct cpumask *srcp1,
567 const struct cpumask *srcp2)
568 {
569 return find_nth_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
570 small_cpumask_bits, cpumask_check(cpu));
571 }
572
573 /**
574 * cpumask_nth_and_andnot - get the Nth cpu set in 1st and 2nd cpumask, and clear in 3rd.
575 * @srcp1: the cpumask pointer
576 * @srcp2: the cpumask pointer
577 * @srcp3: the cpumask pointer
578 * @cpu: the Nth cpu to find, starting from 0
579 *
580 * Return: >= nr_cpu_ids if such cpu doesn't exist.
581 */
582 static __always_inline
cpumask_nth_and_andnot(unsigned int cpu,const struct cpumask * srcp1,const struct cpumask * srcp2,const struct cpumask * srcp3)583 unsigned int cpumask_nth_and_andnot(unsigned int cpu, const struct cpumask *srcp1,
584 const struct cpumask *srcp2,
585 const struct cpumask *srcp3)
586 {
587 return find_nth_and_andnot_bit(cpumask_bits(srcp1),
588 cpumask_bits(srcp2),
589 cpumask_bits(srcp3),
590 small_cpumask_bits, cpumask_check(cpu));
591 }
592
593 #define CPU_BITS_NONE \
594 { \
595 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
596 }
597
598 #define CPU_BITS_CPU0 \
599 { \
600 [0] = 1UL \
601 }
602
603 /**
604 * cpumask_set_cpu - set a cpu in a cpumask
605 * @cpu: cpu number (< nr_cpu_ids)
606 * @dstp: the cpumask pointer
607 */
608 static __always_inline
cpumask_set_cpu(unsigned int cpu,struct cpumask * dstp)609 void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
610 {
611 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
612 }
613
614 static __always_inline
__cpumask_set_cpu(unsigned int cpu,struct cpumask * dstp)615 void __cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
616 {
617 __set_bit(cpumask_check(cpu), cpumask_bits(dstp));
618 }
619
620 /**
621 * cpumask_clear_cpus - clear cpus in a cpumask
622 * @dstp: the cpumask pointer
623 * @cpu: cpu number (< nr_cpu_ids)
624 * @ncpus: number of cpus to clear (< nr_cpu_ids)
625 */
cpumask_clear_cpus(struct cpumask * dstp,unsigned int cpu,unsigned int ncpus)626 static __always_inline void cpumask_clear_cpus(struct cpumask *dstp,
627 unsigned int cpu, unsigned int ncpus)
628 {
629 cpumask_check(cpu + ncpus - 1);
630 bitmap_clear(cpumask_bits(dstp), cpumask_check(cpu), ncpus);
631 }
632
633 /**
634 * cpumask_clear_cpu - clear a cpu in a cpumask
635 * @cpu: cpu number (< nr_cpu_ids)
636 * @dstp: the cpumask pointer
637 */
cpumask_clear_cpu(int cpu,struct cpumask * dstp)638 static __always_inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
639 {
640 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
641 }
642
__cpumask_clear_cpu(int cpu,struct cpumask * dstp)643 static __always_inline void __cpumask_clear_cpu(int cpu, struct cpumask *dstp)
644 {
645 __clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
646 }
647
648 /**
649 * cpumask_test_cpu - test for a cpu in a cpumask
650 * @cpu: cpu number (< nr_cpu_ids)
651 * @cpumask: the cpumask pointer
652 *
653 * Return: true if @cpu is set in @cpumask, else returns false
654 */
655 static __always_inline
cpumask_test_cpu(int cpu,const struct cpumask * cpumask)656 bool cpumask_test_cpu(int cpu, const struct cpumask *cpumask)
657 {
658 return test_bit(cpumask_check(cpu), cpumask_bits((cpumask)));
659 }
660
661 /**
662 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
663 * @cpu: cpu number (< nr_cpu_ids)
664 * @cpumask: the cpumask pointer
665 *
666 * test_and_set_bit wrapper for cpumasks.
667 *
668 * Return: true if @cpu is set in old bitmap of @cpumask, else returns false
669 */
670 static __always_inline
cpumask_test_and_set_cpu(int cpu,struct cpumask * cpumask)671 bool cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
672 {
673 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
674 }
675
676 /**
677 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
678 * @cpu: cpu number (< nr_cpu_ids)
679 * @cpumask: the cpumask pointer
680 *
681 * test_and_clear_bit wrapper for cpumasks.
682 *
683 * Return: true if @cpu is set in old bitmap of @cpumask, else returns false
684 */
685 static __always_inline
cpumask_test_and_clear_cpu(int cpu,struct cpumask * cpumask)686 bool cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
687 {
688 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
689 }
690
691 /**
692 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
693 * @dstp: the cpumask pointer
694 */
cpumask_setall(struct cpumask * dstp)695 static __always_inline void cpumask_setall(struct cpumask *dstp)
696 {
697 if (small_const_nbits(small_cpumask_bits)) {
698 cpumask_bits(dstp)[0] = BITMAP_LAST_WORD_MASK(nr_cpumask_bits);
699 return;
700 }
701 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
702 }
703
704 /**
705 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
706 * @dstp: the cpumask pointer
707 */
cpumask_clear(struct cpumask * dstp)708 static __always_inline void cpumask_clear(struct cpumask *dstp)
709 {
710 bitmap_zero(cpumask_bits(dstp), large_cpumask_bits);
711 }
712
713 /**
714 * cpumask_and - *dstp = *src1p & *src2p
715 * @dstp: the cpumask result
716 * @src1p: the first input
717 * @src2p: the second input
718 *
719 * Return: false if *@dstp is empty, else returns true
720 */
721 static __always_inline
cpumask_and(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)722 bool cpumask_and(struct cpumask *dstp, const struct cpumask *src1p,
723 const struct cpumask *src2p)
724 {
725 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
726 cpumask_bits(src2p), small_cpumask_bits);
727 }
728
729 /**
730 * cpumask_or - *dstp = *src1p | *src2p
731 * @dstp: the cpumask result
732 * @src1p: the first input
733 * @src2p: the second input
734 */
735 static __always_inline
cpumask_or(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)736 void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
737 const struct cpumask *src2p)
738 {
739 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
740 cpumask_bits(src2p), small_cpumask_bits);
741 }
742
743 /**
744 * cpumask_weighted_or - *dstp = *src1p | *src2p and return the weight of the result
745 * @dstp: the cpumask result
746 * @src1p: the first input
747 * @src2p: the second input
748 *
749 * Return: The number of bits set in the resulting cpumask @dstp
750 */
751 static __always_inline
cpumask_weighted_or(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)752 unsigned int cpumask_weighted_or(struct cpumask *dstp, const struct cpumask *src1p,
753 const struct cpumask *src2p)
754 {
755 return bitmap_weighted_or(cpumask_bits(dstp), cpumask_bits(src1p),
756 cpumask_bits(src2p), small_cpumask_bits);
757 }
758
759 /**
760 * cpumask_xor - *dstp = *src1p ^ *src2p
761 * @dstp: the cpumask result
762 * @src1p: the first input
763 * @src2p: the second input
764 */
765 static __always_inline
cpumask_xor(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)766 void cpumask_xor(struct cpumask *dstp, const struct cpumask *src1p,
767 const struct cpumask *src2p)
768 {
769 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
770 cpumask_bits(src2p), small_cpumask_bits);
771 }
772
773 /**
774 * cpumask_andnot - *dstp = *src1p & ~*src2p
775 * @dstp: the cpumask result
776 * @src1p: the first input
777 * @src2p: the second input
778 *
779 * Return: false if *@dstp is empty, else returns true
780 */
781 static __always_inline
cpumask_andnot(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)782 bool cpumask_andnot(struct cpumask *dstp, const struct cpumask *src1p,
783 const struct cpumask *src2p)
784 {
785 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
786 cpumask_bits(src2p), small_cpumask_bits);
787 }
788
789 /**
790 * cpumask_equal - *src1p == *src2p
791 * @src1p: the first input
792 * @src2p: the second input
793 *
794 * Return: true if the cpumasks are equal, false if not
795 */
796 static __always_inline
cpumask_equal(const struct cpumask * src1p,const struct cpumask * src2p)797 bool cpumask_equal(const struct cpumask *src1p, const struct cpumask *src2p)
798 {
799 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
800 small_cpumask_bits);
801 }
802
803 /**
804 * cpumask_or_equal - *src1p | *src2p == *src3p
805 * @src1p: the first input
806 * @src2p: the second input
807 * @src3p: the third input
808 *
809 * Return: true if first cpumask ORed with second cpumask == third cpumask,
810 * otherwise false
811 */
812 static __always_inline
cpumask_or_equal(const struct cpumask * src1p,const struct cpumask * src2p,const struct cpumask * src3p)813 bool cpumask_or_equal(const struct cpumask *src1p, const struct cpumask *src2p,
814 const struct cpumask *src3p)
815 {
816 return bitmap_or_equal(cpumask_bits(src1p), cpumask_bits(src2p),
817 cpumask_bits(src3p), small_cpumask_bits);
818 }
819
820 /**
821 * cpumask_intersects - (*src1p & *src2p) != 0
822 * @src1p: the first input
823 * @src2p: the second input
824 *
825 * Return: true if first cpumask ANDed with second cpumask is non-empty,
826 * otherwise false
827 */
828 static __always_inline
cpumask_intersects(const struct cpumask * src1p,const struct cpumask * src2p)829 bool cpumask_intersects(const struct cpumask *src1p, const struct cpumask *src2p)
830 {
831 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
832 small_cpumask_bits);
833 }
834
835 /**
836 * cpumask_intersects_and - (*src1p & *src2p & *src3p) != 0
837 * @src1p: the first input
838 * @src2p: the second input
839 * @src3p: the third input
840 *
841 * Return: true if AND of the three cpumasks is non-empty,
842 * otherwise false
843 */
844 static __always_inline
cpumask_intersects_and(const struct cpumask * src1p,const struct cpumask * src2p,const struct cpumask * src3p)845 bool cpumask_intersects_and(const struct cpumask *src1p,
846 const struct cpumask *src2p,
847 const struct cpumask *src3p)
848 {
849 return bitmap_intersects_and(cpumask_bits(src1p), cpumask_bits(src2p),
850 cpumask_bits(src3p), small_cpumask_bits);
851 }
852
853 /**
854 * cpumask_subset - (*src1p & ~*src2p) == 0
855 * @src1p: the first input
856 * @src2p: the second input
857 *
858 * Return: true if *@src1p is a subset of *@src2p, else returns false
859 */
860 static __always_inline
cpumask_subset(const struct cpumask * src1p,const struct cpumask * src2p)861 bool cpumask_subset(const struct cpumask *src1p, const struct cpumask *src2p)
862 {
863 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
864 small_cpumask_bits);
865 }
866
867 /**
868 * cpumask_empty - *srcp == 0
869 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
870 *
871 * Return: true if srcp is empty (has no bits set), else false
872 */
cpumask_empty(const struct cpumask * srcp)873 static __always_inline bool cpumask_empty(const struct cpumask *srcp)
874 {
875 return bitmap_empty(cpumask_bits(srcp), small_cpumask_bits);
876 }
877
878 /**
879 * cpumask_full - *srcp == 0xFFFFFFFF...
880 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
881 *
882 * Return: true if srcp is full (has all bits set), else false
883 */
cpumask_full(const struct cpumask * srcp)884 static __always_inline bool cpumask_full(const struct cpumask *srcp)
885 {
886 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
887 }
888
889 /**
890 * cpumask_weight - Count of bits in *srcp
891 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
892 *
893 * Return: count of bits set in *srcp
894 */
cpumask_weight(const struct cpumask * srcp)895 static __always_inline unsigned int cpumask_weight(const struct cpumask *srcp)
896 {
897 return bitmap_weight(cpumask_bits(srcp), small_cpumask_bits);
898 }
899
900 /**
901 * cpumask_weight_and - Count of bits in (*srcp1 & *srcp2)
902 * @srcp1: the cpumask to count bits (< nr_cpu_ids) in.
903 * @srcp2: the cpumask to count bits (< nr_cpu_ids) in.
904 *
905 * Return: count of bits set in both *srcp1 and *srcp2
906 */
907 static __always_inline
cpumask_weight_and(const struct cpumask * srcp1,const struct cpumask * srcp2)908 unsigned int cpumask_weight_and(const struct cpumask *srcp1, const struct cpumask *srcp2)
909 {
910 return bitmap_weight_and(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
911 }
912
913 /**
914 * cpumask_weight_andnot - Count of bits in (*srcp1 & ~*srcp2)
915 * @srcp1: the cpumask to count bits (< nr_cpu_ids) in.
916 * @srcp2: the cpumask to count bits (< nr_cpu_ids) in.
917 *
918 * Return: count of bits set in both *srcp1 and *srcp2
919 */
920 static __always_inline
cpumask_weight_andnot(const struct cpumask * srcp1,const struct cpumask * srcp2)921 unsigned int cpumask_weight_andnot(const struct cpumask *srcp1,
922 const struct cpumask *srcp2)
923 {
924 return bitmap_weight_andnot(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
925 }
926
927 /**
928 * cpumask_shift_right - *dstp = *srcp >> n
929 * @dstp: the cpumask result
930 * @srcp: the input to shift
931 * @n: the number of bits to shift by
932 */
933 static __always_inline
cpumask_shift_right(struct cpumask * dstp,const struct cpumask * srcp,int n)934 void cpumask_shift_right(struct cpumask *dstp, const struct cpumask *srcp, int n)
935 {
936 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
937 small_cpumask_bits);
938 }
939
940 /**
941 * cpumask_shift_left - *dstp = *srcp << n
942 * @dstp: the cpumask result
943 * @srcp: the input to shift
944 * @n: the number of bits to shift by
945 */
946 static __always_inline
cpumask_shift_left(struct cpumask * dstp,const struct cpumask * srcp,int n)947 void cpumask_shift_left(struct cpumask *dstp, const struct cpumask *srcp, int n)
948 {
949 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
950 nr_cpumask_bits);
951 }
952
953 /**
954 * cpumask_copy - *dstp = *srcp
955 * @dstp: the result
956 * @srcp: the input cpumask
957 */
958 static __always_inline
cpumask_copy(struct cpumask * dstp,const struct cpumask * srcp)959 void cpumask_copy(struct cpumask *dstp, const struct cpumask *srcp)
960 {
961 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), large_cpumask_bits);
962 }
963
964 /**
965 * cpumask_any - pick an arbitrary cpu from *srcp
966 * @srcp: the input cpumask
967 *
968 * Return: >= nr_cpu_ids if no cpus set.
969 */
970 #define cpumask_any(srcp) cpumask_first(srcp)
971
972 /**
973 * cpumask_any_and - pick an arbitrary cpu from *mask1 & *mask2
974 * @mask1: the first input cpumask
975 * @mask2: the second input cpumask
976 *
977 * Return: >= nr_cpu_ids if no cpus set.
978 */
979 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
980
981 /**
982 * cpumask_of - the cpumask containing just a given cpu
983 * @cpu: the cpu (<= nr_cpu_ids)
984 */
985 #define cpumask_of(cpu) (get_cpu_mask(cpu))
986
987 /**
988 * cpumask_parse_user - extract a cpumask from a user string
989 * @buf: the buffer to extract from
990 * @len: the length of the buffer
991 * @dstp: the cpumask to set.
992 *
993 * Return: -errno, or 0 for success.
994 */
995 static __always_inline
cpumask_parse_user(const char __user * buf,int len,struct cpumask * dstp)996 int cpumask_parse_user(const char __user *buf, int len, struct cpumask *dstp)
997 {
998 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
999 }
1000
1001 /**
1002 * cpumask_parselist_user - extract a cpumask from a user string
1003 * @buf: the buffer to extract from
1004 * @len: the length of the buffer
1005 * @dstp: the cpumask to set.
1006 *
1007 * Return: -errno, or 0 for success.
1008 */
1009 static __always_inline
cpumask_parselist_user(const char __user * buf,int len,struct cpumask * dstp)1010 int cpumask_parselist_user(const char __user *buf, int len, struct cpumask *dstp)
1011 {
1012 return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
1013 nr_cpumask_bits);
1014 }
1015
1016 /**
1017 * cpumask_parse - extract a cpumask from a string
1018 * @buf: the buffer to extract from
1019 * @dstp: the cpumask to set.
1020 *
1021 * Return: -errno, or 0 for success.
1022 */
cpumask_parse(const char * buf,struct cpumask * dstp)1023 static __always_inline int cpumask_parse(const char *buf, struct cpumask *dstp)
1024 {
1025 return bitmap_parse(buf, UINT_MAX, cpumask_bits(dstp), nr_cpumask_bits);
1026 }
1027
1028 /**
1029 * cpulist_parse - extract a cpumask from a user string of ranges
1030 * @buf: the buffer to extract from
1031 * @dstp: the cpumask to set.
1032 *
1033 * Return: -errno, or 0 for success.
1034 */
cpulist_parse(const char * buf,struct cpumask * dstp)1035 static __always_inline int cpulist_parse(const char *buf, struct cpumask *dstp)
1036 {
1037 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
1038 }
1039
1040 /**
1041 * cpumask_size - calculate size to allocate for a 'struct cpumask' in bytes
1042 *
1043 * Return: size to allocate for a &struct cpumask in bytes
1044 */
cpumask_size(void)1045 static __always_inline unsigned int cpumask_size(void)
1046 {
1047 return bitmap_size(large_cpumask_bits);
1048 }
1049
1050 #ifdef CONFIG_CPUMASK_OFFSTACK
1051
1052 #define this_cpu_cpumask_var_ptr(x) this_cpu_read(x)
1053 #define __cpumask_var_read_mostly __read_mostly
1054 #define CPUMASK_VAR_NULL NULL
1055
1056 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
1057
1058 static __always_inline
zalloc_cpumask_var_node(cpumask_var_t * mask,gfp_t flags,int node)1059 bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
1060 {
1061 return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node);
1062 }
1063
1064 /**
1065 * alloc_cpumask_var - allocate a struct cpumask
1066 * @mask: pointer to cpumask_var_t where the cpumask is returned
1067 * @flags: GFP_ flags
1068 *
1069 * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
1070 * a nop returning a constant 1 (in <linux/cpumask.h>).
1071 *
1072 * See alloc_cpumask_var_node.
1073 *
1074 * Return: %true if allocation succeeded, %false if not
1075 */
1076 static __always_inline
alloc_cpumask_var(cpumask_var_t * mask,gfp_t flags)1077 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1078 {
1079 return alloc_cpumask_var_node(mask, flags, NUMA_NO_NODE);
1080 }
1081
1082 static __always_inline
zalloc_cpumask_var(cpumask_var_t * mask,gfp_t flags)1083 bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1084 {
1085 return alloc_cpumask_var(mask, flags | __GFP_ZERO);
1086 }
1087
1088 void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
1089 void free_cpumask_var(cpumask_var_t mask);
1090 void free_bootmem_cpumask_var(cpumask_var_t mask);
1091
cpumask_available(cpumask_var_t mask)1092 static __always_inline bool cpumask_available(cpumask_var_t mask)
1093 {
1094 return mask != NULL;
1095 }
1096
1097 #else
1098
1099 #define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x)
1100 #define __cpumask_var_read_mostly
1101 #define CPUMASK_VAR_NULL {}
1102
alloc_cpumask_var(cpumask_var_t * mask,gfp_t flags)1103 static __always_inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1104 {
1105 return true;
1106 }
1107
alloc_cpumask_var_node(cpumask_var_t * mask,gfp_t flags,int node)1108 static __always_inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
1109 int node)
1110 {
1111 return true;
1112 }
1113
zalloc_cpumask_var(cpumask_var_t * mask,gfp_t flags)1114 static __always_inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1115 {
1116 cpumask_clear(*mask);
1117 return true;
1118 }
1119
zalloc_cpumask_var_node(cpumask_var_t * mask,gfp_t flags,int node)1120 static __always_inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
1121 int node)
1122 {
1123 cpumask_clear(*mask);
1124 return true;
1125 }
1126
alloc_bootmem_cpumask_var(cpumask_var_t * mask)1127 static __always_inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
1128 {
1129 }
1130
free_cpumask_var(cpumask_var_t mask)1131 static __always_inline void free_cpumask_var(cpumask_var_t mask)
1132 {
1133 }
1134
free_bootmem_cpumask_var(cpumask_var_t mask)1135 static __always_inline void free_bootmem_cpumask_var(cpumask_var_t mask)
1136 {
1137 }
1138
cpumask_available(cpumask_var_t mask)1139 static __always_inline bool cpumask_available(cpumask_var_t mask)
1140 {
1141 return true;
1142 }
1143 #endif /* CONFIG_CPUMASK_OFFSTACK */
1144
1145 DEFINE_FREE(free_cpumask_var, struct cpumask *, if (_T) free_cpumask_var(_T));
1146
1147 /* It's common to want to use cpu_all_mask in struct member initializers,
1148 * so it has to refer to an address rather than a pointer. */
1149 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
1150 #define cpu_all_mask to_cpumask(cpu_all_bits)
1151
1152 /* First bits of cpu_bit_bitmap are in fact unset. */
1153 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
1154
1155 #if NR_CPUS == 1
1156 /* Uniprocessor: the possible/online/present masks are always "1" */
1157 #define for_each_possible_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
1158 #define for_each_online_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
1159 #define for_each_present_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
1160
1161 #define for_each_possible_cpu_wrap(cpu, start) \
1162 for ((void)(start), (cpu) = 0; (cpu) < 1; (cpu)++)
1163 #define for_each_online_cpu_wrap(cpu, start) \
1164 for ((void)(start), (cpu) = 0; (cpu) < 1; (cpu)++)
1165 #else
1166 #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
1167 #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
1168 #define for_each_enabled_cpu(cpu) for_each_cpu((cpu), cpu_enabled_mask)
1169 #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
1170
1171 #define for_each_possible_cpu_wrap(cpu, start) \
1172 for_each_cpu_wrap((cpu), cpu_possible_mask, (start))
1173 #define for_each_online_cpu_wrap(cpu, start) \
1174 for_each_cpu_wrap((cpu), cpu_online_mask, (start))
1175 #endif
1176
1177 /* Wrappers for arch boot code to manipulate normally-constant masks */
1178 void init_cpu_present(const struct cpumask *src);
1179 void init_cpu_possible(const struct cpumask *src);
1180
1181 #define assign_cpu(cpu, mask, val) \
1182 assign_bit(cpumask_check(cpu), cpumask_bits(mask), (val))
1183
1184 #define __assign_cpu(cpu, mask, val) \
1185 __assign_bit(cpumask_check(cpu), cpumask_bits(mask), (val))
1186
1187 #define set_cpu_enabled(cpu, enabled) assign_cpu((cpu), &__cpu_enabled_mask, (enabled))
1188 #define set_cpu_present(cpu, present) assign_cpu((cpu), &__cpu_present_mask, (present))
1189 #define set_cpu_active(cpu, active) assign_cpu((cpu), &__cpu_active_mask, (active))
1190 #define set_cpu_dying(cpu, dying) assign_cpu((cpu), &__cpu_dying_mask, (dying))
1191
1192 #ifdef CONFIG_PREFERRED_CPU
1193 #define set_cpu_preferred(cpu, preferred) assign_cpu((cpu), &__cpu_preferred_mask, (preferred))
1194 #else
1195 #define set_cpu_preferred(cpu, preferred) do { } while (0)
1196 #endif
1197
1198 void set_cpu_online(unsigned int cpu, bool online);
1199 void set_cpu_possible(unsigned int cpu, bool possible);
1200
1201 /**
1202 * to_cpumask - convert a NR_CPUS bitmap to a struct cpumask *
1203 * @bitmap: the bitmap
1204 *
1205 * There are a few places where cpumask_var_t isn't appropriate and
1206 * static cpumasks must be used (eg. very early boot), yet we don't
1207 * expose the definition of 'struct cpumask'.
1208 *
1209 * This does the conversion, and can be used as a constant initializer.
1210 */
1211 #define to_cpumask(bitmap) \
1212 ((struct cpumask *)(1 ? (bitmap) \
1213 : (void *)sizeof(__check_is_bitmap(bitmap))))
1214
__check_is_bitmap(const unsigned long * bitmap)1215 static __always_inline int __check_is_bitmap(const unsigned long *bitmap)
1216 {
1217 return 1;
1218 }
1219
1220 /*
1221 * Special-case data structure for "single bit set only" constant CPU masks.
1222 *
1223 * We pre-generate all the 64 (or 32) possible bit positions, with enough
1224 * padding to the left and the right, and return the constant pointer
1225 * appropriately offset.
1226 */
1227 extern const unsigned long
1228 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
1229
get_cpu_mask(unsigned int cpu)1230 static __always_inline const struct cpumask *get_cpu_mask(unsigned int cpu)
1231 {
1232 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
1233 p -= cpu / BITS_PER_LONG;
1234 return to_cpumask(p);
1235 }
1236
1237 #if NR_CPUS > 1
1238 /**
1239 * num_online_cpus() - Read the number of online CPUs
1240 *
1241 * Despite the fact that __num_online_cpus is of type atomic_t, this
1242 * interface gives only a momentary snapshot and is not protected against
1243 * concurrent CPU hotplug operations unless invoked from a cpuhp_lock held
1244 * region.
1245 *
1246 * Return: momentary snapshot of the number of online CPUs
1247 */
num_online_cpus(void)1248 static __always_inline unsigned int num_online_cpus(void)
1249 {
1250 return raw_atomic_read(&__num_online_cpus);
1251 }
1252
num_possible_cpus(void)1253 static __always_inline unsigned int num_possible_cpus(void)
1254 {
1255 return __num_possible_cpus;
1256 }
1257
1258 #define num_enabled_cpus() cpumask_weight(cpu_enabled_mask)
1259 #define num_present_cpus() cpumask_weight(cpu_present_mask)
1260 #define num_active_cpus() cpumask_weight(cpu_active_mask)
1261
cpu_online(unsigned int cpu)1262 static __always_inline bool cpu_online(unsigned int cpu)
1263 {
1264 return cpumask_test_cpu(cpu, cpu_online_mask);
1265 }
1266
cpu_enabled(unsigned int cpu)1267 static __always_inline bool cpu_enabled(unsigned int cpu)
1268 {
1269 return cpumask_test_cpu(cpu, cpu_enabled_mask);
1270 }
1271
cpu_possible(unsigned int cpu)1272 static __always_inline bool cpu_possible(unsigned int cpu)
1273 {
1274 return cpumask_test_cpu(cpu, cpu_possible_mask);
1275 }
1276
cpu_present(unsigned int cpu)1277 static __always_inline bool cpu_present(unsigned int cpu)
1278 {
1279 return cpumask_test_cpu(cpu, cpu_present_mask);
1280 }
1281
cpu_active(unsigned int cpu)1282 static __always_inline bool cpu_active(unsigned int cpu)
1283 {
1284 return cpumask_test_cpu(cpu, cpu_active_mask);
1285 }
1286
cpu_dying(unsigned int cpu)1287 static __always_inline bool cpu_dying(unsigned int cpu)
1288 {
1289 return cpumask_test_cpu(cpu, cpu_dying_mask);
1290 }
1291
cpu_preferred(unsigned int cpu)1292 static __always_inline bool cpu_preferred(unsigned int cpu)
1293 {
1294 return cpumask_test_cpu(cpu, cpu_preferred_mask);
1295 }
1296
1297 #else
1298
1299 #define num_online_cpus() 1U
1300 #define num_possible_cpus() 1U
1301 #define num_enabled_cpus() 1U
1302 #define num_present_cpus() 1U
1303 #define num_active_cpus() 1U
1304
cpu_online(unsigned int cpu)1305 static __always_inline bool cpu_online(unsigned int cpu)
1306 {
1307 return cpu == 0;
1308 }
1309
cpu_possible(unsigned int cpu)1310 static __always_inline bool cpu_possible(unsigned int cpu)
1311 {
1312 return cpu == 0;
1313 }
1314
cpu_enabled(unsigned int cpu)1315 static __always_inline bool cpu_enabled(unsigned int cpu)
1316 {
1317 return cpu == 0;
1318 }
1319
cpu_present(unsigned int cpu)1320 static __always_inline bool cpu_present(unsigned int cpu)
1321 {
1322 return cpu == 0;
1323 }
1324
cpu_active(unsigned int cpu)1325 static __always_inline bool cpu_active(unsigned int cpu)
1326 {
1327 return cpu == 0;
1328 }
1329
cpu_dying(unsigned int cpu)1330 static __always_inline bool cpu_dying(unsigned int cpu)
1331 {
1332 return false;
1333 }
1334
cpu_preferred(unsigned int cpu)1335 static __always_inline bool cpu_preferred(unsigned int cpu)
1336 {
1337 return cpu == 0;
1338 }
1339
1340 #endif /* NR_CPUS > 1 */
1341
1342 #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
1343
1344 #if NR_CPUS <= BITS_PER_LONG
1345 #define CPU_BITS_ALL \
1346 { \
1347 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1348 }
1349
1350 #else /* NR_CPUS > BITS_PER_LONG */
1351
1352 #define CPU_BITS_ALL \
1353 { \
1354 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
1355 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1356 }
1357 #endif /* NR_CPUS > BITS_PER_LONG */
1358
1359 /**
1360 * cpumap_print_bitmask_to_buf - copies the cpumask into the buffer as
1361 * hex values of cpumask
1362 *
1363 * @buf: the buffer to copy into
1364 * @mask: the cpumask to copy
1365 * @off: in the string from which we are copying, we copy to @buf
1366 * @count: the maximum number of bytes to print
1367 *
1368 * The function prints the cpumask into the buffer as hex values of
1369 * cpumask; Typically used by bin_attribute to export cpumask bitmask
1370 * ABI.
1371 *
1372 * Return: the length of how many bytes have been copied, excluding
1373 * terminating '\0'.
1374 */
1375 static __always_inline
cpumap_print_bitmask_to_buf(char * buf,const struct cpumask * mask,loff_t off,size_t count)1376 ssize_t cpumap_print_bitmask_to_buf(char *buf, const struct cpumask *mask,
1377 loff_t off, size_t count)
1378 {
1379 return bitmap_print_bitmask_to_buf(buf, cpumask_bits(mask),
1380 nr_cpu_ids, off, count) - 1;
1381 }
1382
1383 /**
1384 * cpumap_print_list_to_buf - copies the cpumask into the buffer as
1385 * comma-separated list of cpus
1386 * @buf: the buffer to copy into
1387 * @mask: the cpumask to copy
1388 * @off: in the string from which we are copying, we copy to @buf
1389 * @count: the maximum number of bytes to print
1390 *
1391 * Everything is same with the above cpumap_print_bitmask_to_buf()
1392 * except the print format.
1393 *
1394 * Return: the length of how many bytes have been copied, excluding
1395 * terminating '\0'.
1396 */
1397 static __always_inline
cpumap_print_list_to_buf(char * buf,const struct cpumask * mask,loff_t off,size_t count)1398 ssize_t cpumap_print_list_to_buf(char *buf, const struct cpumask *mask,
1399 loff_t off, size_t count)
1400 {
1401 return bitmap_print_list_to_buf(buf, cpumask_bits(mask),
1402 nr_cpu_ids, off, count) - 1;
1403 }
1404
1405 #if NR_CPUS <= BITS_PER_LONG
1406 #define CPU_MASK_ALL \
1407 (cpumask_t) { { \
1408 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1409 } }
1410 #else
1411 #define CPU_MASK_ALL \
1412 (cpumask_t) { { \
1413 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
1414 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1415 } }
1416 #endif /* NR_CPUS > BITS_PER_LONG */
1417
1418 #define CPU_MASK_NONE \
1419 (cpumask_t) { { \
1420 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
1421 } }
1422
1423 #define CPU_MASK_CPU0 \
1424 (cpumask_t) { { \
1425 [0] = 1UL \
1426 } }
1427
1428 /*
1429 * Provide a valid theoretical max size for cpumap and cpulist sysfs files
1430 * to avoid breaking userspace which may allocate a buffer based on the size
1431 * reported by e.g. fstat.
1432 *
1433 * for cpumap NR_CPUS * 9/32 - 1 should be an exact length.
1434 *
1435 * For cpulist 7 is (ceil(log10(NR_CPUS)) + 1) allowing for NR_CPUS to be up
1436 * to 2 orders of magnitude larger than 8192. And then we divide by 2 to
1437 * cover a worst-case of every other cpu being on one of two nodes for a
1438 * very large NR_CPUS.
1439 *
1440 * Use PAGE_SIZE as a minimum for smaller configurations while avoiding
1441 * unsigned comparison to -1.
1442 */
1443 #define CPUMAP_FILE_MAX_BYTES (((NR_CPUS * 9)/32 > PAGE_SIZE) \
1444 ? (NR_CPUS * 9)/32 - 1 : PAGE_SIZE)
1445 #define CPULIST_FILE_MAX_BYTES (((NR_CPUS * 7)/2 > PAGE_SIZE) ? (NR_CPUS * 7)/2 : PAGE_SIZE)
1446
1447 #endif /* __LINUX_CPUMASK_H */
1448