1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_CPUMASK_H
3 #define __LINUX_CPUMASK_H
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.
10 #include <linux/cleanup.h>
11 #include <linux/kernel.h>
12 #include <linux/bitmap.h>
13 #include <linux/cpumask_types.h>
14 #include <linux/atomic.h>
15 #include <linux/bug.h>
16 #include <linux/gfp_types.h>
17 #include <linux/numa.h>
20 * cpumask_pr_args - printf args to output a cpumask
21 * @maskp: cpumask to be printed
23 * Can be used to provide arguments for '%*pb[l]' when printing a cpumask.
25 #define cpumask_pr_args(maskp) nr_cpu_ids, cpumask_bits(maskp)
27 #if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS)
28 #define nr_cpu_ids ((unsigned int)NR_CPUS)
30 extern unsigned int nr_cpu_ids;
33 static __always_inline void set_nr_cpu_ids(unsigned int nr)
35 #if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS)
36 WARN_ON(nr != nr_cpu_ids);
43 * We have several different "preferred sizes" for the cpumask
44 * operations, depending on operation.
46 * For example, the bitmap scanning and operating operations have
47 * optimized routines that work for the single-word case, but only when
48 * the size is constant. So if NR_CPUS fits in one single word, we are
49 * better off using that small constant, in order to trigger the
50 * optimized bit finding. That is 'small_cpumask_size'.
52 * The clearing and copying operations will similarly perform better
53 * with a constant size, but we limit that size arbitrarily to four
54 * words. We call this 'large_cpumask_size'.
56 * Finally, some operations just want the exact limit, either because
57 * they set bits or just don't have any faster fixed-sized versions. We
58 * call this just 'nr_cpumask_bits'.
60 * Note that these optional constants are always guaranteed to be at
61 * least as big as 'nr_cpu_ids' itself is, and all our cpumask
62 * allocations are at least that size (see cpumask_size()). The
63 * optimization comes from being able to potentially use a compile-time
64 * constant instead of a run-time generated exact number of CPUs.
66 #if NR_CPUS <= BITS_PER_LONG
67 #define small_cpumask_bits ((unsigned int)NR_CPUS)
68 #define large_cpumask_bits ((unsigned int)NR_CPUS)
69 #elif NR_CPUS <= 4*BITS_PER_LONG
70 #define small_cpumask_bits nr_cpu_ids
71 #define large_cpumask_bits ((unsigned int)NR_CPUS)
73 #define small_cpumask_bits nr_cpu_ids
74 #define large_cpumask_bits nr_cpu_ids
76 #define nr_cpumask_bits nr_cpu_ids
79 * The following particular system cpumasks and operations manage
80 * possible, present, active and online cpus.
82 * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
83 * cpu_present_mask - has bit 'cpu' set iff cpu is populated
84 * cpu_enabled_mask - has bit 'cpu' set iff cpu can be brought online
85 * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler
86 * cpu_active_mask - has bit 'cpu' set iff cpu available to migration
88 * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
90 * The cpu_possible_mask is fixed at boot time, as the set of CPU IDs
91 * that it is possible might ever be plugged in at anytime during the
92 * life of that system boot. The cpu_present_mask is dynamic(*),
93 * representing which CPUs are currently plugged in. And
94 * cpu_online_mask is the dynamic subset of cpu_present_mask,
95 * indicating those CPUs available for scheduling.
97 * If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
98 * depending on what ACPI reports as currently plugged in, otherwise
99 * cpu_present_mask is just a copy of cpu_possible_mask.
101 * (*) Well, cpu_present_mask is dynamic in the hotplug case. If not
102 * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
105 * 1) UP ARCHes (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
106 * assumption that their single CPU is online. The UP
107 * cpu_{online,possible,present}_masks are placebos. Changing them
108 * will have no useful affect on the following num_*_cpus()
109 * and cpu_*() macros in the UP case. This ugliness is a UP
110 * optimization - don't waste any instructions or memory references
111 * asking if you're online or how many CPUs there are if there is
115 extern struct cpumask __cpu_possible_mask;
116 extern struct cpumask __cpu_online_mask;
117 extern struct cpumask __cpu_enabled_mask;
118 extern struct cpumask __cpu_present_mask;
119 extern struct cpumask __cpu_active_mask;
120 extern struct cpumask __cpu_dying_mask;
121 #define cpu_possible_mask ((const struct cpumask *)&__cpu_possible_mask)
122 #define cpu_online_mask ((const struct cpumask *)&__cpu_online_mask)
123 #define cpu_enabled_mask ((const struct cpumask *)&__cpu_enabled_mask)
124 #define cpu_present_mask ((const struct cpumask *)&__cpu_present_mask)
125 #define cpu_active_mask ((const struct cpumask *)&__cpu_active_mask)
126 #define cpu_dying_mask ((const struct cpumask *)&__cpu_dying_mask)
128 extern atomic_t __num_online_cpus;
130 extern cpumask_t cpus_booted_once_mask;
132 static __always_inline void cpu_max_bits_warn(unsigned int cpu, unsigned int bits)
134 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
135 WARN_ON_ONCE(cpu >= bits);
136 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
139 /* verify cpu argument to cpumask_* operators */
140 static __always_inline unsigned int cpumask_check(unsigned int cpu)
142 cpu_max_bits_warn(cpu, small_cpumask_bits);
147 * cpumask_first - get the first cpu in a cpumask
148 * @srcp: the cpumask pointer
150 * Return: >= nr_cpu_ids if no cpus set.
152 static __always_inline unsigned int cpumask_first(const struct cpumask *srcp)
154 return find_first_bit(cpumask_bits(srcp), small_cpumask_bits);
158 * cpumask_first_zero - get the first unset cpu in a cpumask
159 * @srcp: the cpumask pointer
161 * Return: >= nr_cpu_ids if all cpus are set.
163 static __always_inline unsigned int cpumask_first_zero(const struct cpumask *srcp)
165 return find_first_zero_bit(cpumask_bits(srcp), small_cpumask_bits);
169 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
170 * @srcp1: the first input
171 * @srcp2: the second input
173 * Return: >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
175 static __always_inline
176 unsigned int cpumask_first_and(const struct cpumask *srcp1, const struct cpumask *srcp2)
178 return find_first_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
182 * cpumask_first_andnot - return the first cpu from *srcp1 & ~*srcp2
183 * @srcp1: the first input
184 * @srcp2: the second input
186 * Return: >= nr_cpu_ids if no such cpu found.
188 static __always_inline
189 unsigned int cpumask_first_andnot(const struct cpumask *srcp1, const struct cpumask *srcp2)
191 return find_first_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
195 * cpumask_first_and_and - return the first cpu from *srcp1 & *srcp2 & *srcp3
196 * @srcp1: the first input
197 * @srcp2: the second input
198 * @srcp3: the third input
200 * Return: >= nr_cpu_ids if no cpus set in all.
202 static __always_inline
203 unsigned int cpumask_first_and_and(const struct cpumask *srcp1,
204 const struct cpumask *srcp2,
205 const struct cpumask *srcp3)
207 return find_first_and_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
208 cpumask_bits(srcp3), small_cpumask_bits);
212 * cpumask_last - get the last CPU in a cpumask
213 * @srcp: - the cpumask pointer
215 * Return: >= nr_cpumask_bits if no CPUs set.
217 static __always_inline unsigned int cpumask_last(const struct cpumask *srcp)
219 return find_last_bit(cpumask_bits(srcp), small_cpumask_bits);
223 * cpumask_next - get the next cpu in a cpumask
224 * @n: the cpu prior to the place to search (i.e. return will be > @n)
225 * @srcp: the cpumask pointer
227 * Return: >= nr_cpu_ids if no further cpus set.
229 static __always_inline
230 unsigned int cpumask_next(int n, const struct cpumask *srcp)
232 /* -1 is a legal arg here. */
235 return find_next_bit(cpumask_bits(srcp), small_cpumask_bits, n + 1);
239 * cpumask_next_zero - get the next unset cpu in a cpumask
240 * @n: the cpu prior to the place to search (i.e. return will be > @n)
241 * @srcp: the cpumask pointer
243 * Return: >= nr_cpu_ids if no further cpus unset.
245 static __always_inline
246 unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
248 /* -1 is a legal arg here. */
251 return find_next_zero_bit(cpumask_bits(srcp), small_cpumask_bits, n+1);
255 /* Uniprocessor: there is only one valid CPU */
256 static __always_inline
257 unsigned int cpumask_local_spread(unsigned int i, int node)
262 static __always_inline
263 unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
264 const struct cpumask *src2p)
266 return cpumask_first_and(src1p, src2p);
269 static __always_inline
270 unsigned int cpumask_any_distribute(const struct cpumask *srcp)
272 return cpumask_first(srcp);
275 unsigned int cpumask_local_spread(unsigned int i, int node);
276 unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
277 const struct cpumask *src2p);
278 unsigned int cpumask_any_distribute(const struct cpumask *srcp);
282 * cpumask_next_and - get the next cpu in *src1p & *src2p
283 * @n: the cpu prior to the place to search (i.e. return will be > @n)
284 * @src1p: the first cpumask pointer
285 * @src2p: the second cpumask pointer
287 * Return: >= nr_cpu_ids if no further cpus set in both.
289 static __always_inline
290 unsigned int cpumask_next_and(int n, const struct cpumask *src1p,
291 const struct cpumask *src2p)
293 /* -1 is a legal arg here. */
296 return find_next_and_bit(cpumask_bits(src1p), cpumask_bits(src2p),
297 small_cpumask_bits, n + 1);
301 * cpumask_next_andnot - get the next cpu in *src1p & ~*src2p
302 * @n: the cpu prior to the place to search (i.e. return will be > @n)
303 * @src1p: the first cpumask pointer
304 * @src2p: the second cpumask pointer
306 * Return: >= nr_cpu_ids if no further cpus set in both.
308 static __always_inline
309 unsigned int cpumask_next_andnot(int n, const struct cpumask *src1p,
310 const struct cpumask *src2p)
312 /* -1 is a legal arg here. */
315 return find_next_andnot_bit(cpumask_bits(src1p), cpumask_bits(src2p),
316 small_cpumask_bits, n + 1);
320 * cpumask_next_and_wrap - get the next cpu in *src1p & *src2p, starting from
321 * @n+1. If nothing found, wrap around and start from
323 * @n: the cpu prior to the place to search (i.e. search starts from @n+1)
324 * @src1p: the first cpumask pointer
325 * @src2p: the second cpumask pointer
327 * Return: next set bit, wrapped if needed, or >= nr_cpu_ids if @src1p & @src2p is empty.
329 static __always_inline
330 unsigned int cpumask_next_and_wrap(int n, const struct cpumask *src1p,
331 const struct cpumask *src2p)
333 /* -1 is a legal arg here. */
336 return find_next_and_bit_wrap(cpumask_bits(src1p), cpumask_bits(src2p),
337 small_cpumask_bits, n + 1);
341 * cpumask_next_wrap - get the next cpu in *src, starting from @n+1. If nothing
342 * found, wrap around and start from the beginning
343 * @n: the cpu prior to the place to search (i.e. search starts from @n+1)
344 * @src: cpumask pointer
346 * Return: next set bit, wrapped if needed, or >= nr_cpu_ids if @src is empty.
348 static __always_inline
349 unsigned int cpumask_next_wrap(int n, const struct cpumask *src)
351 /* -1 is a legal arg here. */
354 return find_next_bit_wrap(cpumask_bits(src), small_cpumask_bits, n + 1);
358 * for_each_cpu - iterate over every cpu in a mask
359 * @cpu: the (optionally unsigned) integer iterator
360 * @mask: the cpumask pointer
362 * After the loop, cpu is >= nr_cpu_ids.
364 #define for_each_cpu(cpu, mask) \
365 for_each_set_bit(cpu, cpumask_bits(mask), small_cpumask_bits)
368 * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a specified location
369 * @cpu: the (optionally unsigned) integer iterator
370 * @mask: the cpumask pointer
371 * @start: the start location
373 * The implementation does not assume any bit in @mask is set (including @start).
375 * After the loop, cpu is >= nr_cpu_ids.
377 #define for_each_cpu_wrap(cpu, mask, start) \
378 for_each_set_bit_wrap(cpu, cpumask_bits(mask), small_cpumask_bits, start)
381 * for_each_cpu_and - iterate over every cpu in both masks
382 * @cpu: the (optionally unsigned) integer iterator
383 * @mask1: the first cpumask pointer
384 * @mask2: the second cpumask pointer
386 * This saves a temporary CPU mask in many places. It is equivalent to:
387 * struct cpumask tmp;
388 * cpumask_and(&tmp, &mask1, &mask2);
389 * for_each_cpu(cpu, &tmp)
392 * After the loop, cpu is >= nr_cpu_ids.
394 #define for_each_cpu_and(cpu, mask1, mask2) \
395 for_each_and_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
398 * for_each_cpu_andnot - iterate over every cpu present in one mask, excluding
399 * those present in another.
400 * @cpu: the (optionally unsigned) integer iterator
401 * @mask1: the first cpumask pointer
402 * @mask2: the second cpumask pointer
404 * This saves a temporary CPU mask in many places. It is equivalent to:
405 * struct cpumask tmp;
406 * cpumask_andnot(&tmp, &mask1, &mask2);
407 * for_each_cpu(cpu, &tmp)
410 * After the loop, cpu is >= nr_cpu_ids.
412 #define for_each_cpu_andnot(cpu, mask1, mask2) \
413 for_each_andnot_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
416 * for_each_cpu_or - iterate over every cpu present in either mask
417 * @cpu: the (optionally unsigned) integer iterator
418 * @mask1: the first cpumask pointer
419 * @mask2: the second cpumask pointer
421 * This saves a temporary CPU mask in many places. It is equivalent to:
422 * struct cpumask tmp;
423 * cpumask_or(&tmp, &mask1, &mask2);
424 * for_each_cpu(cpu, &tmp)
427 * After the loop, cpu is >= nr_cpu_ids.
429 #define for_each_cpu_or(cpu, mask1, mask2) \
430 for_each_or_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
433 * for_each_cpu_from - iterate over CPUs present in @mask, from @cpu to the end of @mask.
434 * @cpu: the (optionally unsigned) integer iterator
435 * @mask: the cpumask pointer
437 * After the loop, cpu is >= nr_cpu_ids.
439 #define for_each_cpu_from(cpu, mask) \
440 for_each_set_bit_from(cpu, cpumask_bits(mask), small_cpumask_bits)
443 * cpumask_any_but - return an arbitrary cpu in a cpumask, but not this one.
444 * @mask: the cpumask to search
445 * @cpu: the cpu to ignore.
447 * Often used to find any cpu but smp_processor_id() in a mask.
448 * If @cpu == -1, the function is equivalent to cpumask_any().
449 * Return: >= nr_cpu_ids if no cpus set.
451 static __always_inline
452 unsigned int cpumask_any_but(const struct cpumask *mask, int cpu)
456 /* -1 is a legal arg here. */
460 for_each_cpu(i, mask)
467 * cpumask_any_and_but - pick an arbitrary cpu from *mask1 & *mask2, but not this one.
468 * @mask1: the first input cpumask
469 * @mask2: the second input cpumask
470 * @cpu: the cpu to ignore
472 * If @cpu == -1, the function is equivalent to cpumask_any_and().
473 * Returns >= nr_cpu_ids if no cpus set.
475 static __always_inline
476 unsigned int cpumask_any_and_but(const struct cpumask *mask1,
477 const struct cpumask *mask2,
482 /* -1 is a legal arg here. */
486 i = cpumask_first_and(mask1, mask2);
490 return cpumask_next_and(cpu, mask1, mask2);
494 * cpumask_any_andnot_but - pick an arbitrary cpu from *mask1 & ~*mask2, but not this one.
495 * @mask1: the first input cpumask
496 * @mask2: the second input cpumask
497 * @cpu: the cpu to ignore
499 * If @cpu == -1, the function returns the first matching cpu.
500 * Returns >= nr_cpu_ids if no cpus set.
502 static __always_inline
503 unsigned int cpumask_any_andnot_but(const struct cpumask *mask1,
504 const struct cpumask *mask2,
509 /* -1 is a legal arg here. */
513 i = cpumask_first_andnot(mask1, mask2);
517 return cpumask_next_andnot(cpu, mask1, mask2);
521 * cpumask_nth - get the Nth cpu in a cpumask
522 * @srcp: the cpumask pointer
523 * @cpu: the Nth cpu to find, starting from 0
525 * Return: >= nr_cpu_ids if such cpu doesn't exist.
527 static __always_inline
528 unsigned int cpumask_nth(unsigned int cpu, const struct cpumask *srcp)
530 return find_nth_bit(cpumask_bits(srcp), small_cpumask_bits, cpumask_check(cpu));
534 * cpumask_nth_and - get the Nth cpu in 2 cpumasks
535 * @srcp1: the cpumask pointer
536 * @srcp2: the cpumask pointer
537 * @cpu: the Nth cpu to find, starting from 0
539 * Return: >= nr_cpu_ids if such cpu doesn't exist.
541 static __always_inline
542 unsigned int cpumask_nth_and(unsigned int cpu, const struct cpumask *srcp1,
543 const struct cpumask *srcp2)
545 return find_nth_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
546 small_cpumask_bits, cpumask_check(cpu));
550 * cpumask_nth_andnot - get the Nth cpu set in 1st cpumask, and clear in 2nd.
551 * @srcp1: the cpumask pointer
552 * @srcp2: the cpumask pointer
553 * @cpu: the Nth cpu to find, starting from 0
555 * Return: >= nr_cpu_ids if such cpu doesn't exist.
557 static __always_inline
558 unsigned int cpumask_nth_andnot(unsigned int cpu, const struct cpumask *srcp1,
559 const struct cpumask *srcp2)
561 return find_nth_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
562 small_cpumask_bits, cpumask_check(cpu));
566 * cpumask_nth_and_andnot - get the Nth cpu set in 1st and 2nd cpumask, and clear in 3rd.
567 * @srcp1: the cpumask pointer
568 * @srcp2: the cpumask pointer
569 * @srcp3: the cpumask pointer
570 * @cpu: the Nth cpu to find, starting from 0
572 * Return: >= nr_cpu_ids if such cpu doesn't exist.
574 static __always_inline
575 unsigned int cpumask_nth_and_andnot(unsigned int cpu, const struct cpumask *srcp1,
576 const struct cpumask *srcp2,
577 const struct cpumask *srcp3)
579 return find_nth_and_andnot_bit(cpumask_bits(srcp1),
582 small_cpumask_bits, cpumask_check(cpu));
585 #define CPU_BITS_NONE \
587 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
590 #define CPU_BITS_CPU0 \
596 * cpumask_set_cpu - set a cpu in a cpumask
597 * @cpu: cpu number (< nr_cpu_ids)
598 * @dstp: the cpumask pointer
600 static __always_inline
601 void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
603 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
606 static __always_inline
607 void __cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
609 __set_bit(cpumask_check(cpu), cpumask_bits(dstp));
614 * cpumask_clear_cpu - clear a cpu in a cpumask
615 * @cpu: cpu number (< nr_cpu_ids)
616 * @dstp: the cpumask pointer
618 static __always_inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
620 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
623 static __always_inline void __cpumask_clear_cpu(int cpu, struct cpumask *dstp)
625 __clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
629 * cpumask_test_cpu - test for a cpu in a cpumask
630 * @cpu: cpu number (< nr_cpu_ids)
631 * @cpumask: the cpumask pointer
633 * Return: true if @cpu is set in @cpumask, else returns false
635 static __always_inline
636 bool cpumask_test_cpu(int cpu, const struct cpumask *cpumask)
638 return test_bit(cpumask_check(cpu), cpumask_bits((cpumask)));
642 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
643 * @cpu: cpu number (< nr_cpu_ids)
644 * @cpumask: the cpumask pointer
646 * test_and_set_bit wrapper for cpumasks.
648 * Return: true if @cpu is set in old bitmap of @cpumask, else returns false
650 static __always_inline
651 bool cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
653 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
657 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
658 * @cpu: cpu number (< nr_cpu_ids)
659 * @cpumask: the cpumask pointer
661 * test_and_clear_bit wrapper for cpumasks.
663 * Return: true if @cpu is set in old bitmap of @cpumask, else returns false
665 static __always_inline
666 bool cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
668 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
672 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
673 * @dstp: the cpumask pointer
675 static __always_inline void cpumask_setall(struct cpumask *dstp)
677 if (small_const_nbits(small_cpumask_bits)) {
678 cpumask_bits(dstp)[0] = BITMAP_LAST_WORD_MASK(nr_cpumask_bits);
681 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
685 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
686 * @dstp: the cpumask pointer
688 static __always_inline void cpumask_clear(struct cpumask *dstp)
690 bitmap_zero(cpumask_bits(dstp), large_cpumask_bits);
694 * cpumask_and - *dstp = *src1p & *src2p
695 * @dstp: the cpumask result
696 * @src1p: the first input
697 * @src2p: the second input
699 * Return: false if *@dstp is empty, else returns true
701 static __always_inline
702 bool cpumask_and(struct cpumask *dstp, const struct cpumask *src1p,
703 const struct cpumask *src2p)
705 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
706 cpumask_bits(src2p), small_cpumask_bits);
710 * cpumask_or - *dstp = *src1p | *src2p
711 * @dstp: the cpumask result
712 * @src1p: the first input
713 * @src2p: the second input
715 static __always_inline
716 void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
717 const struct cpumask *src2p)
719 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
720 cpumask_bits(src2p), small_cpumask_bits);
724 * cpumask_xor - *dstp = *src1p ^ *src2p
725 * @dstp: the cpumask result
726 * @src1p: the first input
727 * @src2p: the second input
729 static __always_inline
730 void cpumask_xor(struct cpumask *dstp, const struct cpumask *src1p,
731 const struct cpumask *src2p)
733 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
734 cpumask_bits(src2p), small_cpumask_bits);
738 * cpumask_andnot - *dstp = *src1p & ~*src2p
739 * @dstp: the cpumask result
740 * @src1p: the first input
741 * @src2p: the second input
743 * Return: false if *@dstp is empty, else returns true
745 static __always_inline
746 bool cpumask_andnot(struct cpumask *dstp, const struct cpumask *src1p,
747 const struct cpumask *src2p)
749 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
750 cpumask_bits(src2p), small_cpumask_bits);
754 * cpumask_equal - *src1p == *src2p
755 * @src1p: the first input
756 * @src2p: the second input
758 * Return: true if the cpumasks are equal, false if not
760 static __always_inline
761 bool cpumask_equal(const struct cpumask *src1p, const struct cpumask *src2p)
763 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
768 * cpumask_or_equal - *src1p | *src2p == *src3p
769 * @src1p: the first input
770 * @src2p: the second input
771 * @src3p: the third input
773 * Return: true if first cpumask ORed with second cpumask == third cpumask,
776 static __always_inline
777 bool cpumask_or_equal(const struct cpumask *src1p, const struct cpumask *src2p,
778 const struct cpumask *src3p)
780 return bitmap_or_equal(cpumask_bits(src1p), cpumask_bits(src2p),
781 cpumask_bits(src3p), small_cpumask_bits);
785 * cpumask_intersects - (*src1p & *src2p) != 0
786 * @src1p: the first input
787 * @src2p: the second input
789 * Return: true if first cpumask ANDed with second cpumask is non-empty,
792 static __always_inline
793 bool cpumask_intersects(const struct cpumask *src1p, const struct cpumask *src2p)
795 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
800 * cpumask_subset - (*src1p & ~*src2p) == 0
801 * @src1p: the first input
802 * @src2p: the second input
804 * Return: true if *@src1p is a subset of *@src2p, else returns false
806 static __always_inline
807 bool cpumask_subset(const struct cpumask *src1p, const struct cpumask *src2p)
809 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
814 * cpumask_empty - *srcp == 0
815 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
817 * Return: true if srcp is empty (has no bits set), else false
819 static __always_inline bool cpumask_empty(const struct cpumask *srcp)
821 return bitmap_empty(cpumask_bits(srcp), small_cpumask_bits);
825 * cpumask_full - *srcp == 0xFFFFFFFF...
826 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
828 * Return: true if srcp is full (has all bits set), else false
830 static __always_inline bool cpumask_full(const struct cpumask *srcp)
832 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
836 * cpumask_weight - Count of bits in *srcp
837 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
839 * Return: count of bits set in *srcp
841 static __always_inline unsigned int cpumask_weight(const struct cpumask *srcp)
843 return bitmap_weight(cpumask_bits(srcp), small_cpumask_bits);
847 * cpumask_weight_and - Count of bits in (*srcp1 & *srcp2)
848 * @srcp1: the cpumask to count bits (< nr_cpu_ids) in.
849 * @srcp2: the cpumask to count bits (< nr_cpu_ids) in.
851 * Return: count of bits set in both *srcp1 and *srcp2
853 static __always_inline
854 unsigned int cpumask_weight_and(const struct cpumask *srcp1, const struct cpumask *srcp2)
856 return bitmap_weight_and(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
860 * cpumask_weight_andnot - Count of bits in (*srcp1 & ~*srcp2)
861 * @srcp1: the cpumask to count bits (< nr_cpu_ids) in.
862 * @srcp2: the cpumask to count bits (< nr_cpu_ids) in.
864 * Return: count of bits set in both *srcp1 and *srcp2
866 static __always_inline
867 unsigned int cpumask_weight_andnot(const struct cpumask *srcp1,
868 const struct cpumask *srcp2)
870 return bitmap_weight_andnot(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
874 * cpumask_shift_right - *dstp = *srcp >> n
875 * @dstp: the cpumask result
876 * @srcp: the input to shift
877 * @n: the number of bits to shift by
879 static __always_inline
880 void cpumask_shift_right(struct cpumask *dstp, const struct cpumask *srcp, int n)
882 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
887 * cpumask_shift_left - *dstp = *srcp << n
888 * @dstp: the cpumask result
889 * @srcp: the input to shift
890 * @n: the number of bits to shift by
892 static __always_inline
893 void cpumask_shift_left(struct cpumask *dstp, const struct cpumask *srcp, int n)
895 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
900 * cpumask_copy - *dstp = *srcp
902 * @srcp: the input cpumask
904 static __always_inline
905 void cpumask_copy(struct cpumask *dstp, const struct cpumask *srcp)
907 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), large_cpumask_bits);
911 * cpumask_any - pick an arbitrary cpu from *srcp
912 * @srcp: the input cpumask
914 * Return: >= nr_cpu_ids if no cpus set.
916 #define cpumask_any(srcp) cpumask_first(srcp)
919 * cpumask_any_and - pick an arbitrary cpu from *mask1 & *mask2
920 * @mask1: the first input cpumask
921 * @mask2: the second input cpumask
923 * Return: >= nr_cpu_ids if no cpus set.
925 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
928 * cpumask_of - the cpumask containing just a given cpu
929 * @cpu: the cpu (<= nr_cpu_ids)
931 #define cpumask_of(cpu) (get_cpu_mask(cpu))
934 * cpumask_parse_user - extract a cpumask from a user string
935 * @buf: the buffer to extract from
936 * @len: the length of the buffer
937 * @dstp: the cpumask to set.
939 * Return: -errno, or 0 for success.
941 static __always_inline
942 int cpumask_parse_user(const char __user *buf, int len, struct cpumask *dstp)
944 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
948 * cpumask_parselist_user - extract a cpumask from a user string
949 * @buf: the buffer to extract from
950 * @len: the length of the buffer
951 * @dstp: the cpumask to set.
953 * Return: -errno, or 0 for success.
955 static __always_inline
956 int cpumask_parselist_user(const char __user *buf, int len, struct cpumask *dstp)
958 return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
963 * cpumask_parse - extract a cpumask from a string
964 * @buf: the buffer to extract from
965 * @dstp: the cpumask to set.
967 * Return: -errno, or 0 for success.
969 static __always_inline int cpumask_parse(const char *buf, struct cpumask *dstp)
971 return bitmap_parse(buf, UINT_MAX, cpumask_bits(dstp), nr_cpumask_bits);
975 * cpulist_parse - extract a cpumask from a user string of ranges
976 * @buf: the buffer to extract from
977 * @dstp: the cpumask to set.
979 * Return: -errno, or 0 for success.
981 static __always_inline int cpulist_parse(const char *buf, struct cpumask *dstp)
983 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
987 * cpumask_size - calculate size to allocate for a 'struct cpumask' in bytes
989 * Return: size to allocate for a &struct cpumask in bytes
991 static __always_inline unsigned int cpumask_size(void)
993 return bitmap_size(large_cpumask_bits);
996 #ifdef CONFIG_CPUMASK_OFFSTACK
998 #define this_cpu_cpumask_var_ptr(x) this_cpu_read(x)
999 #define __cpumask_var_read_mostly __read_mostly
1001 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
1003 static __always_inline
1004 bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
1006 return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node);
1010 * alloc_cpumask_var - allocate a struct cpumask
1011 * @mask: pointer to cpumask_var_t where the cpumask is returned
1012 * @flags: GFP_ flags
1014 * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
1015 * a nop returning a constant 1 (in <linux/cpumask.h>).
1017 * See alloc_cpumask_var_node.
1019 * Return: %true if allocation succeeded, %false if not
1021 static __always_inline
1022 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1024 return alloc_cpumask_var_node(mask, flags, NUMA_NO_NODE);
1027 static __always_inline
1028 bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1030 return alloc_cpumask_var(mask, flags | __GFP_ZERO);
1033 void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
1034 void free_cpumask_var(cpumask_var_t mask);
1035 void free_bootmem_cpumask_var(cpumask_var_t mask);
1037 static __always_inline bool cpumask_available(cpumask_var_t mask)
1039 return mask != NULL;
1044 #define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x)
1045 #define __cpumask_var_read_mostly
1047 static __always_inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1052 static __always_inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
1058 static __always_inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1060 cpumask_clear(*mask);
1064 static __always_inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
1067 cpumask_clear(*mask);
1071 static __always_inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
1075 static __always_inline void free_cpumask_var(cpumask_var_t mask)
1079 static __always_inline void free_bootmem_cpumask_var(cpumask_var_t mask)
1083 static __always_inline bool cpumask_available(cpumask_var_t mask)
1087 #endif /* CONFIG_CPUMASK_OFFSTACK */
1089 DEFINE_FREE(free_cpumask_var, struct cpumask *, if (_T) free_cpumask_var(_T));
1091 /* It's common to want to use cpu_all_mask in struct member initializers,
1092 * so it has to refer to an address rather than a pointer. */
1093 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
1094 #define cpu_all_mask to_cpumask(cpu_all_bits)
1096 /* First bits of cpu_bit_bitmap are in fact unset. */
1097 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
1100 /* Uniprocessor: the possible/online/present masks are always "1" */
1101 #define for_each_possible_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
1102 #define for_each_online_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
1103 #define for_each_present_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
1105 #define for_each_possible_cpu_wrap(cpu, start) \
1106 for ((void)(start), (cpu) = 0; (cpu) < 1; (cpu)++)
1107 #define for_each_online_cpu_wrap(cpu, start) \
1108 for ((void)(start), (cpu) = 0; (cpu) < 1; (cpu)++)
1110 #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
1111 #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
1112 #define for_each_enabled_cpu(cpu) for_each_cpu((cpu), cpu_enabled_mask)
1113 #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
1115 #define for_each_possible_cpu_wrap(cpu, start) \
1116 for_each_cpu_wrap((cpu), cpu_possible_mask, (start))
1117 #define for_each_online_cpu_wrap(cpu, start) \
1118 for_each_cpu_wrap((cpu), cpu_online_mask, (start))
1121 /* Wrappers for arch boot code to manipulate normally-constant masks */
1122 void init_cpu_present(const struct cpumask *src);
1123 void init_cpu_possible(const struct cpumask *src);
1125 #define assign_cpu(cpu, mask, val) \
1126 assign_bit(cpumask_check(cpu), cpumask_bits(mask), (val))
1128 #define __assign_cpu(cpu, mask, val) \
1129 __assign_bit(cpumask_check(cpu), cpumask_bits(mask), (val))
1131 #define set_cpu_possible(cpu, possible) assign_cpu((cpu), &__cpu_possible_mask, (possible))
1132 #define set_cpu_enabled(cpu, enabled) assign_cpu((cpu), &__cpu_enabled_mask, (enabled))
1133 #define set_cpu_present(cpu, present) assign_cpu((cpu), &__cpu_present_mask, (present))
1134 #define set_cpu_active(cpu, active) assign_cpu((cpu), &__cpu_active_mask, (active))
1135 #define set_cpu_dying(cpu, dying) assign_cpu((cpu), &__cpu_dying_mask, (dying))
1137 void set_cpu_online(unsigned int cpu, bool online);
1140 * to_cpumask - convert a NR_CPUS bitmap to a struct cpumask *
1141 * @bitmap: the bitmap
1143 * There are a few places where cpumask_var_t isn't appropriate and
1144 * static cpumasks must be used (eg. very early boot), yet we don't
1145 * expose the definition of 'struct cpumask'.
1147 * This does the conversion, and can be used as a constant initializer.
1149 #define to_cpumask(bitmap) \
1150 ((struct cpumask *)(1 ? (bitmap) \
1151 : (void *)sizeof(__check_is_bitmap(bitmap))))
1153 static __always_inline int __check_is_bitmap(const unsigned long *bitmap)
1159 * Special-case data structure for "single bit set only" constant CPU masks.
1161 * We pre-generate all the 64 (or 32) possible bit positions, with enough
1162 * padding to the left and the right, and return the constant pointer
1163 * appropriately offset.
1165 extern const unsigned long
1166 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
1168 static __always_inline const struct cpumask *get_cpu_mask(unsigned int cpu)
1170 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
1171 p -= cpu / BITS_PER_LONG;
1172 return to_cpumask(p);
1177 * num_online_cpus() - Read the number of online CPUs
1179 * Despite the fact that __num_online_cpus is of type atomic_t, this
1180 * interface gives only a momentary snapshot and is not protected against
1181 * concurrent CPU hotplug operations unless invoked from a cpuhp_lock held
1184 * Return: momentary snapshot of the number of online CPUs
1186 static __always_inline unsigned int num_online_cpus(void)
1188 return raw_atomic_read(&__num_online_cpus);
1190 #define num_possible_cpus() cpumask_weight(cpu_possible_mask)
1191 #define num_enabled_cpus() cpumask_weight(cpu_enabled_mask)
1192 #define num_present_cpus() cpumask_weight(cpu_present_mask)
1193 #define num_active_cpus() cpumask_weight(cpu_active_mask)
1195 static __always_inline bool cpu_online(unsigned int cpu)
1197 return cpumask_test_cpu(cpu, cpu_online_mask);
1200 static __always_inline bool cpu_enabled(unsigned int cpu)
1202 return cpumask_test_cpu(cpu, cpu_enabled_mask);
1205 static __always_inline bool cpu_possible(unsigned int cpu)
1207 return cpumask_test_cpu(cpu, cpu_possible_mask);
1210 static __always_inline bool cpu_present(unsigned int cpu)
1212 return cpumask_test_cpu(cpu, cpu_present_mask);
1215 static __always_inline bool cpu_active(unsigned int cpu)
1217 return cpumask_test_cpu(cpu, cpu_active_mask);
1220 static __always_inline bool cpu_dying(unsigned int cpu)
1222 return cpumask_test_cpu(cpu, cpu_dying_mask);
1227 #define num_online_cpus() 1U
1228 #define num_possible_cpus() 1U
1229 #define num_enabled_cpus() 1U
1230 #define num_present_cpus() 1U
1231 #define num_active_cpus() 1U
1233 static __always_inline bool cpu_online(unsigned int cpu)
1238 static __always_inline bool cpu_possible(unsigned int cpu)
1243 static __always_inline bool cpu_enabled(unsigned int cpu)
1248 static __always_inline bool cpu_present(unsigned int cpu)
1253 static __always_inline bool cpu_active(unsigned int cpu)
1258 static __always_inline bool cpu_dying(unsigned int cpu)
1263 #endif /* NR_CPUS > 1 */
1265 #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
1267 #if NR_CPUS <= BITS_PER_LONG
1268 #define CPU_BITS_ALL \
1270 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1273 #else /* NR_CPUS > BITS_PER_LONG */
1275 #define CPU_BITS_ALL \
1277 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
1278 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1280 #endif /* NR_CPUS > BITS_PER_LONG */
1283 * cpumap_print_to_pagebuf - copies the cpumask into the buffer either
1284 * as comma-separated list of cpus or hex values of cpumask
1285 * @list: indicates whether the cpumap must be list
1286 * @mask: the cpumask to copy
1287 * @buf: the buffer to copy into
1289 * Return: the length of the (null-terminated) @buf string, zero if
1290 * nothing is copied.
1292 static __always_inline ssize_t
1293 cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask)
1295 return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask),
1300 * cpumap_print_bitmask_to_buf - copies the cpumask into the buffer as
1301 * hex values of cpumask
1303 * @buf: the buffer to copy into
1304 * @mask: the cpumask to copy
1305 * @off: in the string from which we are copying, we copy to @buf
1306 * @count: the maximum number of bytes to print
1308 * The function prints the cpumask into the buffer as hex values of
1309 * cpumask; Typically used by bin_attribute to export cpumask bitmask
1312 * Return: the length of how many bytes have been copied, excluding
1315 static __always_inline
1316 ssize_t cpumap_print_bitmask_to_buf(char *buf, const struct cpumask *mask,
1317 loff_t off, size_t count)
1319 return bitmap_print_bitmask_to_buf(buf, cpumask_bits(mask),
1320 nr_cpu_ids, off, count) - 1;
1324 * cpumap_print_list_to_buf - copies the cpumask into the buffer as
1325 * comma-separated list of cpus
1326 * @buf: the buffer to copy into
1327 * @mask: the cpumask to copy
1328 * @off: in the string from which we are copying, we copy to @buf
1329 * @count: the maximum number of bytes to print
1331 * Everything is same with the above cpumap_print_bitmask_to_buf()
1332 * except the print format.
1334 * Return: the length of how many bytes have been copied, excluding
1337 static __always_inline
1338 ssize_t cpumap_print_list_to_buf(char *buf, const struct cpumask *mask,
1339 loff_t off, size_t count)
1341 return bitmap_print_list_to_buf(buf, cpumask_bits(mask),
1342 nr_cpu_ids, off, count) - 1;
1345 #if NR_CPUS <= BITS_PER_LONG
1346 #define CPU_MASK_ALL \
1348 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1351 #define CPU_MASK_ALL \
1353 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
1354 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1356 #endif /* NR_CPUS > BITS_PER_LONG */
1358 #define CPU_MASK_NONE \
1360 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
1363 #define CPU_MASK_CPU0 \
1369 * Provide a valid theoretical max size for cpumap and cpulist sysfs files
1370 * to avoid breaking userspace which may allocate a buffer based on the size
1371 * reported by e.g. fstat.
1373 * for cpumap NR_CPUS * 9/32 - 1 should be an exact length.
1375 * For cpulist 7 is (ceil(log10(NR_CPUS)) + 1) allowing for NR_CPUS to be up
1376 * to 2 orders of magnitude larger than 8192. And then we divide by 2 to
1377 * cover a worst-case of every other cpu being on one of two nodes for a
1378 * very large NR_CPUS.
1380 * Use PAGE_SIZE as a minimum for smaller configurations while avoiding
1381 * unsigned comparison to -1.
1383 #define CPUMAP_FILE_MAX_BYTES (((NR_CPUS * 9)/32 > PAGE_SIZE) \
1384 ? (NR_CPUS * 9)/32 - 1 : PAGE_SIZE)
1385 #define CPULIST_FILE_MAX_BYTES (((NR_CPUS * 7)/2 > PAGE_SIZE) ? (NR_CPUS * 7)/2 : PAGE_SIZE)
1387 #endif /* __LINUX_CPUMASK_H */