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/threads.h>
13 #include <linux/bitmap.h>
14 #include <linux/atomic.h>
15 #include <linux/bug.h>
16 #include <linux/gfp_types.h>
17 #include <linux/numa.h>
19 /* Don't assign or return these: may not be this big! */
20 typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
23 * cpumask_bits - get the bits in a cpumask
24 * @maskp: the struct cpumask *
26 * You should only assume nr_cpu_ids bits of this mask are valid. This is
27 * a macro so it's const-correct.
29 #define cpumask_bits(maskp) ((maskp)->bits)
32 * cpumask_pr_args - printf args to output a cpumask
33 * @maskp: cpumask to be printed
35 * Can be used to provide arguments for '%*pb[l]' when printing a cpumask.
37 #define cpumask_pr_args(maskp) nr_cpu_ids, cpumask_bits(maskp)
39 #if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS)
40 #define nr_cpu_ids ((unsigned int)NR_CPUS)
42 extern unsigned int nr_cpu_ids;
45 static inline void set_nr_cpu_ids(unsigned int nr)
47 #if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS)
48 WARN_ON(nr != nr_cpu_ids);
55 * We have several different "preferred sizes" for the cpumask
56 * operations, depending on operation.
58 * For example, the bitmap scanning and operating operations have
59 * optimized routines that work for the single-word case, but only when
60 * the size is constant. So if NR_CPUS fits in one single word, we are
61 * better off using that small constant, in order to trigger the
62 * optimized bit finding. That is 'small_cpumask_size'.
64 * The clearing and copying operations will similarly perform better
65 * with a constant size, but we limit that size arbitrarily to four
66 * words. We call this 'large_cpumask_size'.
68 * Finally, some operations just want the exact limit, either because
69 * they set bits or just don't have any faster fixed-sized versions. We
70 * call this just 'nr_cpumask_bits'.
72 * Note that these optional constants are always guaranteed to be at
73 * least as big as 'nr_cpu_ids' itself is, and all our cpumask
74 * allocations are at least that size (see cpumask_size()). The
75 * optimization comes from being able to potentially use a compile-time
76 * constant instead of a run-time generated exact number of CPUs.
78 #if NR_CPUS <= BITS_PER_LONG
79 #define small_cpumask_bits ((unsigned int)NR_CPUS)
80 #define large_cpumask_bits ((unsigned int)NR_CPUS)
81 #elif NR_CPUS <= 4*BITS_PER_LONG
82 #define small_cpumask_bits nr_cpu_ids
83 #define large_cpumask_bits ((unsigned int)NR_CPUS)
85 #define small_cpumask_bits nr_cpu_ids
86 #define large_cpumask_bits nr_cpu_ids
88 #define nr_cpumask_bits nr_cpu_ids
91 * The following particular system cpumasks and operations manage
92 * possible, present, active and online cpus.
94 * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
95 * cpu_present_mask - has bit 'cpu' set iff cpu is populated
96 * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler
97 * cpu_active_mask - has bit 'cpu' set iff cpu available to migration
99 * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
101 * The cpu_possible_mask is fixed at boot time, as the set of CPU IDs
102 * that it is possible might ever be plugged in at anytime during the
103 * life of that system boot. The cpu_present_mask is dynamic(*),
104 * representing which CPUs are currently plugged in. And
105 * cpu_online_mask is the dynamic subset of cpu_present_mask,
106 * indicating those CPUs available for scheduling.
108 * If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
109 * depending on what ACPI reports as currently plugged in, otherwise
110 * cpu_present_mask is just a copy of cpu_possible_mask.
112 * (*) Well, cpu_present_mask is dynamic in the hotplug case. If not
113 * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
116 * 1) UP ARCHes (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
117 * assumption that their single CPU is online. The UP
118 * cpu_{online,possible,present}_masks are placebos. Changing them
119 * will have no useful affect on the following num_*_cpus()
120 * and cpu_*() macros in the UP case. This ugliness is a UP
121 * optimization - don't waste any instructions or memory references
122 * asking if you're online or how many CPUs there are if there is
126 extern struct cpumask __cpu_possible_mask;
127 extern struct cpumask __cpu_online_mask;
128 extern struct cpumask __cpu_present_mask;
129 extern struct cpumask __cpu_active_mask;
130 extern struct cpumask __cpu_dying_mask;
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_present_mask ((const struct cpumask *)&__cpu_present_mask)
134 #define cpu_active_mask ((const struct cpumask *)&__cpu_active_mask)
135 #define cpu_dying_mask ((const struct cpumask *)&__cpu_dying_mask)
137 extern atomic_t __num_online_cpus;
139 extern cpumask_t cpus_booted_once_mask;
141 static __always_inline void cpu_max_bits_warn(unsigned int cpu, unsigned int bits)
143 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
144 WARN_ON_ONCE(cpu >= bits);
145 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
148 /* verify cpu argument to cpumask_* operators */
149 static __always_inline unsigned int cpumask_check(unsigned int cpu)
151 cpu_max_bits_warn(cpu, small_cpumask_bits);
156 * cpumask_first - get the first cpu in a cpumask
157 * @srcp: the cpumask pointer
159 * Return: >= nr_cpu_ids if no cpus set.
161 static inline unsigned int cpumask_first(const struct cpumask *srcp)
163 return find_first_bit(cpumask_bits(srcp), small_cpumask_bits);
167 * cpumask_first_zero - get the first unset cpu in a cpumask
168 * @srcp: the cpumask pointer
170 * Return: >= nr_cpu_ids if all cpus are set.
172 static inline unsigned int cpumask_first_zero(const struct cpumask *srcp)
174 return find_first_zero_bit(cpumask_bits(srcp), small_cpumask_bits);
178 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
179 * @srcp1: the first input
180 * @srcp2: the second input
182 * Return: >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
185 unsigned int cpumask_first_and(const struct cpumask *srcp1, const struct cpumask *srcp2)
187 return find_first_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
191 * cpumask_first_and_and - return the first cpu from *srcp1 & *srcp2 & *srcp3
192 * @srcp1: the first input
193 * @srcp2: the second input
194 * @srcp3: the third input
196 * Return: >= nr_cpu_ids if no cpus set in all.
199 unsigned int cpumask_first_and_and(const struct cpumask *srcp1,
200 const struct cpumask *srcp2,
201 const struct cpumask *srcp3)
203 return find_first_and_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
204 cpumask_bits(srcp3), small_cpumask_bits);
208 * cpumask_last - get the last CPU in a cpumask
209 * @srcp: - the cpumask pointer
211 * Return: >= nr_cpumask_bits if no CPUs set.
213 static inline unsigned int cpumask_last(const struct cpumask *srcp)
215 return find_last_bit(cpumask_bits(srcp), small_cpumask_bits);
219 * cpumask_next - get the next cpu in a cpumask
220 * @n: the cpu prior to the place to search (i.e. return will be > @n)
221 * @srcp: the cpumask pointer
223 * Return: >= nr_cpu_ids if no further cpus set.
226 unsigned int cpumask_next(int n, const struct cpumask *srcp)
228 /* -1 is a legal arg here. */
231 return find_next_bit(cpumask_bits(srcp), small_cpumask_bits, n + 1);
235 * cpumask_next_zero - get the next unset 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
239 * Return: >= nr_cpu_ids if no further cpus unset.
241 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
243 /* -1 is a legal arg here. */
246 return find_next_zero_bit(cpumask_bits(srcp), small_cpumask_bits, n+1);
250 /* Uniprocessor: there is only one valid CPU */
251 static inline unsigned int cpumask_local_spread(unsigned int i, int node)
256 static inline unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
257 const struct cpumask *src2p)
259 return cpumask_first_and(src1p, src2p);
262 static inline unsigned int cpumask_any_distribute(const struct cpumask *srcp)
264 return cpumask_first(srcp);
267 unsigned int cpumask_local_spread(unsigned int i, int node);
268 unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
269 const struct cpumask *src2p);
270 unsigned int cpumask_any_distribute(const struct cpumask *srcp);
274 * cpumask_next_and - get the next cpu in *src1p & *src2p
275 * @n: the cpu prior to the place to search (i.e. return will be > @n)
276 * @src1p: the first cpumask pointer
277 * @src2p: the second cpumask pointer
279 * Return: >= nr_cpu_ids if no further cpus set in both.
282 unsigned int cpumask_next_and(int n, const struct cpumask *src1p,
283 const struct cpumask *src2p)
285 /* -1 is a legal arg here. */
288 return find_next_and_bit(cpumask_bits(src1p), cpumask_bits(src2p),
289 small_cpumask_bits, n + 1);
293 * for_each_cpu - iterate over every cpu in a mask
294 * @cpu: the (optionally unsigned) integer iterator
295 * @mask: the cpumask pointer
297 * After the loop, cpu is >= nr_cpu_ids.
299 #define for_each_cpu(cpu, mask) \
300 for_each_set_bit(cpu, cpumask_bits(mask), small_cpumask_bits)
304 unsigned int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap)
306 cpumask_check(start);
311 * Return the first available CPU when wrapping, or when starting before cpu0,
312 * since there is only one valid option.
315 return nr_cpumask_bits;
317 return cpumask_first(mask);
320 unsigned int __pure cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap);
324 * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a specified location
325 * @cpu: the (optionally unsigned) integer iterator
326 * @mask: the cpumask pointer
327 * @start: the start location
329 * The implementation does not assume any bit in @mask is set (including @start).
331 * After the loop, cpu is >= nr_cpu_ids.
333 #define for_each_cpu_wrap(cpu, mask, start) \
334 for_each_set_bit_wrap(cpu, cpumask_bits(mask), small_cpumask_bits, start)
337 * for_each_cpu_and - iterate over every cpu in both masks
338 * @cpu: the (optionally unsigned) integer iterator
339 * @mask1: the first cpumask pointer
340 * @mask2: the second cpumask pointer
342 * This saves a temporary CPU mask in many places. It is equivalent to:
343 * struct cpumask tmp;
344 * cpumask_and(&tmp, &mask1, &mask2);
345 * for_each_cpu(cpu, &tmp)
348 * After the loop, cpu is >= nr_cpu_ids.
350 #define for_each_cpu_and(cpu, mask1, mask2) \
351 for_each_and_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
354 * for_each_cpu_andnot - iterate over every cpu present in one mask, excluding
355 * those present in another.
356 * @cpu: the (optionally unsigned) integer iterator
357 * @mask1: the first cpumask pointer
358 * @mask2: the second cpumask pointer
360 * This saves a temporary CPU mask in many places. It is equivalent to:
361 * struct cpumask tmp;
362 * cpumask_andnot(&tmp, &mask1, &mask2);
363 * for_each_cpu(cpu, &tmp)
366 * After the loop, cpu is >= nr_cpu_ids.
368 #define for_each_cpu_andnot(cpu, mask1, mask2) \
369 for_each_andnot_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
372 * for_each_cpu_or - iterate over every cpu present in either mask
373 * @cpu: the (optionally unsigned) integer iterator
374 * @mask1: the first cpumask pointer
375 * @mask2: the second cpumask pointer
377 * This saves a temporary CPU mask in many places. It is equivalent to:
378 * struct cpumask tmp;
379 * cpumask_or(&tmp, &mask1, &mask2);
380 * for_each_cpu(cpu, &tmp)
383 * After the loop, cpu is >= nr_cpu_ids.
385 #define for_each_cpu_or(cpu, mask1, mask2) \
386 for_each_or_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
389 * for_each_cpu_from - iterate over CPUs present in @mask, from @cpu to the end of @mask.
390 * @cpu: the (optionally unsigned) integer iterator
391 * @mask: the cpumask pointer
393 * After the loop, cpu is >= nr_cpu_ids.
395 #define for_each_cpu_from(cpu, mask) \
396 for_each_set_bit_from(cpu, cpumask_bits(mask), small_cpumask_bits)
399 * cpumask_any_but - return a "random" in a cpumask, but not this one.
400 * @mask: the cpumask to search
401 * @cpu: the cpu to ignore.
403 * Often used to find any cpu but smp_processor_id() in a mask.
404 * Return: >= nr_cpu_ids if no cpus set.
407 unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
412 for_each_cpu(i, mask)
419 * cpumask_any_and_but - pick a "random" cpu from *mask1 & *mask2, but not this one.
420 * @mask1: the first input cpumask
421 * @mask2: the second input cpumask
422 * @cpu: the cpu to ignore
424 * Returns >= nr_cpu_ids if no cpus set.
427 unsigned int cpumask_any_and_but(const struct cpumask *mask1,
428 const struct cpumask *mask2,
434 i = cpumask_first_and(mask1, mask2);
438 return cpumask_next_and(cpu, mask1, mask2);
442 * cpumask_nth - get the Nth cpu in a cpumask
443 * @srcp: the cpumask pointer
444 * @cpu: the Nth cpu to find, starting from 0
446 * Return: >= nr_cpu_ids if such cpu doesn't exist.
448 static inline unsigned int cpumask_nth(unsigned int cpu, const struct cpumask *srcp)
450 return find_nth_bit(cpumask_bits(srcp), small_cpumask_bits, cpumask_check(cpu));
454 * cpumask_nth_and - get the Nth cpu in 2 cpumasks
455 * @srcp1: the cpumask pointer
456 * @srcp2: the cpumask pointer
457 * @cpu: the Nth cpu to find, starting from 0
459 * Return: >= nr_cpu_ids if such cpu doesn't exist.
462 unsigned int cpumask_nth_and(unsigned int cpu, const struct cpumask *srcp1,
463 const struct cpumask *srcp2)
465 return find_nth_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
466 small_cpumask_bits, cpumask_check(cpu));
470 * cpumask_nth_andnot - get the Nth cpu set in 1st cpumask, and clear in 2nd.
471 * @srcp1: the cpumask pointer
472 * @srcp2: the cpumask pointer
473 * @cpu: the Nth cpu to find, starting from 0
475 * Return: >= nr_cpu_ids if such cpu doesn't exist.
478 unsigned int cpumask_nth_andnot(unsigned int cpu, const struct cpumask *srcp1,
479 const struct cpumask *srcp2)
481 return find_nth_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
482 small_cpumask_bits, cpumask_check(cpu));
486 * cpumask_nth_and_andnot - get the Nth cpu set in 1st and 2nd cpumask, and clear in 3rd.
487 * @srcp1: the cpumask pointer
488 * @srcp2: the cpumask pointer
489 * @srcp3: the cpumask pointer
490 * @cpu: the Nth cpu to find, starting from 0
492 * Return: >= nr_cpu_ids if such cpu doesn't exist.
494 static __always_inline
495 unsigned int cpumask_nth_and_andnot(unsigned int cpu, const struct cpumask *srcp1,
496 const struct cpumask *srcp2,
497 const struct cpumask *srcp3)
499 return find_nth_and_andnot_bit(cpumask_bits(srcp1),
502 small_cpumask_bits, cpumask_check(cpu));
505 #define CPU_BITS_NONE \
507 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
510 #define CPU_BITS_CPU0 \
516 * cpumask_set_cpu - set a cpu in a cpumask
517 * @cpu: cpu number (< nr_cpu_ids)
518 * @dstp: the cpumask pointer
520 static __always_inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
522 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
525 static __always_inline void __cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
527 __set_bit(cpumask_check(cpu), cpumask_bits(dstp));
532 * cpumask_clear_cpu - clear a cpu in a cpumask
533 * @cpu: cpu number (< nr_cpu_ids)
534 * @dstp: the cpumask pointer
536 static __always_inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
538 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
541 static __always_inline void __cpumask_clear_cpu(int cpu, struct cpumask *dstp)
543 __clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
547 * cpumask_assign_cpu - assign a cpu in a cpumask
548 * @cpu: cpu number (< nr_cpu_ids)
549 * @dstp: the cpumask pointer
550 * @bool: the value to assign
552 static __always_inline void cpumask_assign_cpu(int cpu, struct cpumask *dstp, bool value)
554 assign_bit(cpumask_check(cpu), cpumask_bits(dstp), value);
557 static __always_inline void __cpumask_assign_cpu(int cpu, struct cpumask *dstp, bool value)
559 __assign_bit(cpumask_check(cpu), cpumask_bits(dstp), value);
563 * cpumask_test_cpu - test for a cpu in a cpumask
564 * @cpu: cpu number (< nr_cpu_ids)
565 * @cpumask: the cpumask pointer
567 * Return: true if @cpu is set in @cpumask, else returns false
569 static __always_inline bool cpumask_test_cpu(int cpu, const struct cpumask *cpumask)
571 return test_bit(cpumask_check(cpu), cpumask_bits((cpumask)));
575 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
576 * @cpu: cpu number (< nr_cpu_ids)
577 * @cpumask: the cpumask pointer
579 * test_and_set_bit wrapper for cpumasks.
581 * Return: true if @cpu is set in old bitmap of @cpumask, else returns false
583 static __always_inline bool cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
585 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
589 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
590 * @cpu: cpu number (< nr_cpu_ids)
591 * @cpumask: the cpumask pointer
593 * test_and_clear_bit wrapper for cpumasks.
595 * Return: true if @cpu is set in old bitmap of @cpumask, else returns false
597 static __always_inline bool cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
599 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
603 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
604 * @dstp: the cpumask pointer
606 static inline void cpumask_setall(struct cpumask *dstp)
608 if (small_const_nbits(small_cpumask_bits)) {
609 cpumask_bits(dstp)[0] = BITMAP_LAST_WORD_MASK(nr_cpumask_bits);
612 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
616 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
617 * @dstp: the cpumask pointer
619 static inline void cpumask_clear(struct cpumask *dstp)
621 bitmap_zero(cpumask_bits(dstp), large_cpumask_bits);
625 * cpumask_and - *dstp = *src1p & *src2p
626 * @dstp: the cpumask result
627 * @src1p: the first input
628 * @src2p: the second input
630 * Return: false if *@dstp is empty, else returns true
632 static inline bool cpumask_and(struct cpumask *dstp,
633 const struct cpumask *src1p,
634 const struct cpumask *src2p)
636 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
637 cpumask_bits(src2p), small_cpumask_bits);
641 * cpumask_or - *dstp = *src1p | *src2p
642 * @dstp: the cpumask result
643 * @src1p: the first input
644 * @src2p: the second input
646 static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
647 const struct cpumask *src2p)
649 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
650 cpumask_bits(src2p), small_cpumask_bits);
654 * cpumask_xor - *dstp = *src1p ^ *src2p
655 * @dstp: the cpumask result
656 * @src1p: the first input
657 * @src2p: the second input
659 static inline void cpumask_xor(struct cpumask *dstp,
660 const struct cpumask *src1p,
661 const struct cpumask *src2p)
663 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
664 cpumask_bits(src2p), small_cpumask_bits);
668 * cpumask_andnot - *dstp = *src1p & ~*src2p
669 * @dstp: the cpumask result
670 * @src1p: the first input
671 * @src2p: the second input
673 * Return: false if *@dstp is empty, else returns true
675 static inline bool cpumask_andnot(struct cpumask *dstp,
676 const struct cpumask *src1p,
677 const struct cpumask *src2p)
679 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
680 cpumask_bits(src2p), small_cpumask_bits);
684 * cpumask_equal - *src1p == *src2p
685 * @src1p: the first input
686 * @src2p: the second input
688 * Return: true if the cpumasks are equal, false if not
690 static inline bool cpumask_equal(const struct cpumask *src1p,
691 const struct cpumask *src2p)
693 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
698 * cpumask_or_equal - *src1p | *src2p == *src3p
699 * @src1p: the first input
700 * @src2p: the second input
701 * @src3p: the third input
703 * Return: true if first cpumask ORed with second cpumask == third cpumask,
706 static inline bool cpumask_or_equal(const struct cpumask *src1p,
707 const struct cpumask *src2p,
708 const struct cpumask *src3p)
710 return bitmap_or_equal(cpumask_bits(src1p), cpumask_bits(src2p),
711 cpumask_bits(src3p), small_cpumask_bits);
715 * cpumask_intersects - (*src1p & *src2p) != 0
716 * @src1p: the first input
717 * @src2p: the second input
719 * Return: true if first cpumask ANDed with second cpumask is non-empty,
722 static inline bool cpumask_intersects(const struct cpumask *src1p,
723 const struct cpumask *src2p)
725 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
730 * cpumask_subset - (*src1p & ~*src2p) == 0
731 * @src1p: the first input
732 * @src2p: the second input
734 * Return: true if *@src1p is a subset of *@src2p, else returns false
736 static inline bool cpumask_subset(const struct cpumask *src1p,
737 const struct cpumask *src2p)
739 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
744 * cpumask_empty - *srcp == 0
745 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
747 * Return: true if srcp is empty (has no bits set), else false
749 static inline bool cpumask_empty(const struct cpumask *srcp)
751 return bitmap_empty(cpumask_bits(srcp), small_cpumask_bits);
755 * cpumask_full - *srcp == 0xFFFFFFFF...
756 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
758 * Return: true if srcp is full (has all bits set), else false
760 static inline bool cpumask_full(const struct cpumask *srcp)
762 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
766 * cpumask_weight - Count of bits in *srcp
767 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
769 * Return: count of bits set in *srcp
771 static inline unsigned int cpumask_weight(const struct cpumask *srcp)
773 return bitmap_weight(cpumask_bits(srcp), small_cpumask_bits);
777 * cpumask_weight_and - Count of bits in (*srcp1 & *srcp2)
778 * @srcp1: the cpumask to count bits (< nr_cpu_ids) in.
779 * @srcp2: the cpumask to count bits (< nr_cpu_ids) in.
781 * Return: count of bits set in both *srcp1 and *srcp2
783 static inline unsigned int cpumask_weight_and(const struct cpumask *srcp1,
784 const struct cpumask *srcp2)
786 return bitmap_weight_and(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
790 * cpumask_weight_andnot - Count of bits in (*srcp1 & ~*srcp2)
791 * @srcp1: the cpumask to count bits (< nr_cpu_ids) in.
792 * @srcp2: the cpumask to count bits (< nr_cpu_ids) in.
794 * Return: count of bits set in both *srcp1 and *srcp2
796 static inline unsigned int cpumask_weight_andnot(const struct cpumask *srcp1,
797 const struct cpumask *srcp2)
799 return bitmap_weight_andnot(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
803 * cpumask_shift_right - *dstp = *srcp >> n
804 * @dstp: the cpumask result
805 * @srcp: the input to shift
806 * @n: the number of bits to shift by
808 static inline void cpumask_shift_right(struct cpumask *dstp,
809 const struct cpumask *srcp, int n)
811 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
816 * cpumask_shift_left - *dstp = *srcp << n
817 * @dstp: the cpumask result
818 * @srcp: the input to shift
819 * @n: the number of bits to shift by
821 static inline void cpumask_shift_left(struct cpumask *dstp,
822 const struct cpumask *srcp, int n)
824 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
829 * cpumask_copy - *dstp = *srcp
831 * @srcp: the input cpumask
833 static inline void cpumask_copy(struct cpumask *dstp,
834 const struct cpumask *srcp)
836 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), large_cpumask_bits);
840 * cpumask_any - pick a "random" cpu from *srcp
841 * @srcp: the input cpumask
843 * Return: >= nr_cpu_ids if no cpus set.
845 #define cpumask_any(srcp) cpumask_first(srcp)
848 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
849 * @mask1: the first input cpumask
850 * @mask2: the second input cpumask
852 * Return: >= nr_cpu_ids if no cpus set.
854 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
857 * cpumask_of - the cpumask containing just a given cpu
858 * @cpu: the cpu (<= nr_cpu_ids)
860 #define cpumask_of(cpu) (get_cpu_mask(cpu))
863 * cpumask_parse_user - extract a cpumask from a user string
864 * @buf: the buffer to extract from
865 * @len: the length of the buffer
866 * @dstp: the cpumask to set.
868 * Return: -errno, or 0 for success.
870 static inline int cpumask_parse_user(const char __user *buf, int len,
871 struct cpumask *dstp)
873 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
877 * cpumask_parselist_user - extract a cpumask from a user string
878 * @buf: the buffer to extract from
879 * @len: the length of the buffer
880 * @dstp: the cpumask to set.
882 * Return: -errno, or 0 for success.
884 static inline int cpumask_parselist_user(const char __user *buf, int len,
885 struct cpumask *dstp)
887 return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
892 * cpumask_parse - extract a cpumask from a string
893 * @buf: the buffer to extract from
894 * @dstp: the cpumask to set.
896 * Return: -errno, or 0 for success.
898 static inline int cpumask_parse(const char *buf, struct cpumask *dstp)
900 return bitmap_parse(buf, UINT_MAX, cpumask_bits(dstp), nr_cpumask_bits);
904 * cpulist_parse - extract a cpumask from a user string of ranges
905 * @buf: the buffer to extract from
906 * @dstp: the cpumask to set.
908 * Return: -errno, or 0 for success.
910 static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
912 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
916 * cpumask_size - calculate size to allocate for a 'struct cpumask' in bytes
918 * Return: size to allocate for a &struct cpumask in bytes
920 static inline unsigned int cpumask_size(void)
922 return bitmap_size(large_cpumask_bits);
926 * cpumask_var_t: struct cpumask for stack usage.
928 * Oh, the wicked games we play! In order to make kernel coding a
929 * little more difficult, we typedef cpumask_var_t to an array or a
930 * pointer: doing &mask on an array is a noop, so it still works.
933 * cpumask_var_t tmpmask;
934 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
937 * ... use 'tmpmask' like a normal struct cpumask * ...
939 * free_cpumask_var(tmpmask);
942 * However, one notable exception is there. alloc_cpumask_var() allocates
943 * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has
944 * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t.
946 * cpumask_var_t tmpmask;
947 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
952 * This code makes NR_CPUS length memcopy and brings to a memory corruption.
953 * cpumask_copy() provide safe copy functionality.
955 * Note that there is another evil here: If you define a cpumask_var_t
956 * as a percpu variable then the way to obtain the address of the cpumask
957 * structure differently influences what this_cpu_* operation needs to be
958 * used. Please use this_cpu_cpumask_var_t in those cases. The direct use
959 * of this_cpu_ptr() or this_cpu_read() will lead to failures when the
960 * other type of cpumask_var_t implementation is configured.
962 * Please also note that __cpumask_var_read_mostly can be used to declare
963 * a cpumask_var_t variable itself (not its content) as read mostly.
965 #ifdef CONFIG_CPUMASK_OFFSTACK
966 typedef struct cpumask *cpumask_var_t;
968 #define this_cpu_cpumask_var_ptr(x) this_cpu_read(x)
969 #define __cpumask_var_read_mostly __read_mostly
971 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
974 bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
976 return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node);
980 * alloc_cpumask_var - allocate a struct cpumask
981 * @mask: pointer to cpumask_var_t where the cpumask is returned
984 * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
985 * a nop returning a constant 1 (in <linux/cpumask.h>).
987 * See alloc_cpumask_var_node.
989 * Return: %true if allocation succeeded, %false if not
992 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
994 return alloc_cpumask_var_node(mask, flags, NUMA_NO_NODE);
998 bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1000 return alloc_cpumask_var(mask, flags | __GFP_ZERO);
1003 void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
1004 void free_cpumask_var(cpumask_var_t mask);
1005 void free_bootmem_cpumask_var(cpumask_var_t mask);
1007 static inline bool cpumask_available(cpumask_var_t mask)
1009 return mask != NULL;
1013 typedef struct cpumask cpumask_var_t[1];
1015 #define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x)
1016 #define __cpumask_var_read_mostly
1018 static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1023 static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
1029 static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1031 cpumask_clear(*mask);
1035 static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
1038 cpumask_clear(*mask);
1042 static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
1046 static inline void free_cpumask_var(cpumask_var_t mask)
1050 static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
1054 static inline bool cpumask_available(cpumask_var_t mask)
1058 #endif /* CONFIG_CPUMASK_OFFSTACK */
1060 DEFINE_FREE(free_cpumask_var, struct cpumask *, if (_T) free_cpumask_var(_T));
1062 /* It's common to want to use cpu_all_mask in struct member initializers,
1063 * so it has to refer to an address rather than a pointer. */
1064 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
1065 #define cpu_all_mask to_cpumask(cpu_all_bits)
1067 /* First bits of cpu_bit_bitmap are in fact unset. */
1068 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
1071 /* Uniprocessor: the possible/online/present masks are always "1" */
1072 #define for_each_possible_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
1073 #define for_each_online_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
1074 #define for_each_present_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
1076 #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
1077 #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
1078 #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
1081 /* Wrappers for arch boot code to manipulate normally-constant masks */
1082 void init_cpu_present(const struct cpumask *src);
1083 void init_cpu_possible(const struct cpumask *src);
1084 void init_cpu_online(const struct cpumask *src);
1087 set_cpu_possible(unsigned int cpu, bool possible)
1090 cpumask_set_cpu(cpu, &__cpu_possible_mask);
1092 cpumask_clear_cpu(cpu, &__cpu_possible_mask);
1096 set_cpu_present(unsigned int cpu, bool present)
1099 cpumask_set_cpu(cpu, &__cpu_present_mask);
1101 cpumask_clear_cpu(cpu, &__cpu_present_mask);
1104 void set_cpu_online(unsigned int cpu, bool online);
1107 set_cpu_active(unsigned int cpu, bool active)
1110 cpumask_set_cpu(cpu, &__cpu_active_mask);
1112 cpumask_clear_cpu(cpu, &__cpu_active_mask);
1116 set_cpu_dying(unsigned int cpu, bool dying)
1119 cpumask_set_cpu(cpu, &__cpu_dying_mask);
1121 cpumask_clear_cpu(cpu, &__cpu_dying_mask);
1125 * to_cpumask - convert a NR_CPUS bitmap to a struct cpumask *
1126 * @bitmap: the bitmap
1128 * There are a few places where cpumask_var_t isn't appropriate and
1129 * static cpumasks must be used (eg. very early boot), yet we don't
1130 * expose the definition of 'struct cpumask'.
1132 * This does the conversion, and can be used as a constant initializer.
1134 #define to_cpumask(bitmap) \
1135 ((struct cpumask *)(1 ? (bitmap) \
1136 : (void *)sizeof(__check_is_bitmap(bitmap))))
1138 static inline int __check_is_bitmap(const unsigned long *bitmap)
1144 * Special-case data structure for "single bit set only" constant CPU masks.
1146 * We pre-generate all the 64 (or 32) possible bit positions, with enough
1147 * padding to the left and the right, and return the constant pointer
1148 * appropriately offset.
1150 extern const unsigned long
1151 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
1153 static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
1155 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
1156 p -= cpu / BITS_PER_LONG;
1157 return to_cpumask(p);
1162 * num_online_cpus() - Read the number of online CPUs
1164 * Despite the fact that __num_online_cpus is of type atomic_t, this
1165 * interface gives only a momentary snapshot and is not protected against
1166 * concurrent CPU hotplug operations unless invoked from a cpuhp_lock held
1169 * Return: momentary snapshot of the number of online CPUs
1171 static __always_inline unsigned int num_online_cpus(void)
1173 return raw_atomic_read(&__num_online_cpus);
1175 #define num_possible_cpus() cpumask_weight(cpu_possible_mask)
1176 #define num_present_cpus() cpumask_weight(cpu_present_mask)
1177 #define num_active_cpus() cpumask_weight(cpu_active_mask)
1179 static inline bool cpu_online(unsigned int cpu)
1181 return cpumask_test_cpu(cpu, cpu_online_mask);
1184 static inline bool cpu_possible(unsigned int cpu)
1186 return cpumask_test_cpu(cpu, cpu_possible_mask);
1189 static inline bool cpu_present(unsigned int cpu)
1191 return cpumask_test_cpu(cpu, cpu_present_mask);
1194 static inline bool cpu_active(unsigned int cpu)
1196 return cpumask_test_cpu(cpu, cpu_active_mask);
1199 static inline bool cpu_dying(unsigned int cpu)
1201 return cpumask_test_cpu(cpu, cpu_dying_mask);
1206 #define num_online_cpus() 1U
1207 #define num_possible_cpus() 1U
1208 #define num_present_cpus() 1U
1209 #define num_active_cpus() 1U
1211 static inline bool cpu_online(unsigned int cpu)
1216 static inline bool cpu_possible(unsigned int cpu)
1221 static inline bool cpu_present(unsigned int cpu)
1226 static inline bool cpu_active(unsigned int cpu)
1231 static inline bool cpu_dying(unsigned int cpu)
1236 #endif /* NR_CPUS > 1 */
1238 #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
1240 #if NR_CPUS <= BITS_PER_LONG
1241 #define CPU_BITS_ALL \
1243 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1246 #else /* NR_CPUS > BITS_PER_LONG */
1248 #define CPU_BITS_ALL \
1250 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
1251 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1253 #endif /* NR_CPUS > BITS_PER_LONG */
1256 * cpumap_print_to_pagebuf - copies the cpumask into the buffer either
1257 * as comma-separated list of cpus or hex values of cpumask
1258 * @list: indicates whether the cpumap must be list
1259 * @mask: the cpumask to copy
1260 * @buf: the buffer to copy into
1262 * Return: the length of the (null-terminated) @buf string, zero if
1263 * nothing is copied.
1265 static inline ssize_t
1266 cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask)
1268 return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask),
1273 * cpumap_print_bitmask_to_buf - copies the cpumask into the buffer as
1274 * hex values of cpumask
1276 * @buf: the buffer to copy into
1277 * @mask: the cpumask to copy
1278 * @off: in the string from which we are copying, we copy to @buf
1279 * @count: the maximum number of bytes to print
1281 * The function prints the cpumask into the buffer as hex values of
1282 * cpumask; Typically used by bin_attribute to export cpumask bitmask
1285 * Return: the length of how many bytes have been copied, excluding
1288 static inline ssize_t
1289 cpumap_print_bitmask_to_buf(char *buf, const struct cpumask *mask,
1290 loff_t off, size_t count)
1292 return bitmap_print_bitmask_to_buf(buf, cpumask_bits(mask),
1293 nr_cpu_ids, off, count) - 1;
1297 * cpumap_print_list_to_buf - copies the cpumask into the buffer as
1298 * comma-separated list of cpus
1299 * @buf: the buffer to copy into
1300 * @mask: the cpumask to copy
1301 * @off: in the string from which we are copying, we copy to @buf
1302 * @count: the maximum number of bytes to print
1304 * Everything is same with the above cpumap_print_bitmask_to_buf()
1305 * except the print format.
1307 * Return: the length of how many bytes have been copied, excluding
1310 static inline ssize_t
1311 cpumap_print_list_to_buf(char *buf, const struct cpumask *mask,
1312 loff_t off, size_t count)
1314 return bitmap_print_list_to_buf(buf, cpumask_bits(mask),
1315 nr_cpu_ids, off, count) - 1;
1318 #if NR_CPUS <= BITS_PER_LONG
1319 #define CPU_MASK_ALL \
1321 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1324 #define CPU_MASK_ALL \
1326 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
1327 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1329 #endif /* NR_CPUS > BITS_PER_LONG */
1331 #define CPU_MASK_NONE \
1333 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
1336 #define CPU_MASK_CPU0 \
1342 * Provide a valid theoretical max size for cpumap and cpulist sysfs files
1343 * to avoid breaking userspace which may allocate a buffer based on the size
1344 * reported by e.g. fstat.
1346 * for cpumap NR_CPUS * 9/32 - 1 should be an exact length.
1348 * For cpulist 7 is (ceil(log10(NR_CPUS)) + 1) allowing for NR_CPUS to be up
1349 * to 2 orders of magnitude larger than 8192. And then we divide by 2 to
1350 * cover a worst-case of every other cpu being on one of two nodes for a
1351 * very large NR_CPUS.
1353 * Use PAGE_SIZE as a minimum for smaller configurations while avoiding
1354 * unsigned comparison to -1.
1356 #define CPUMAP_FILE_MAX_BYTES (((NR_CPUS * 9)/32 > PAGE_SIZE) \
1357 ? (NR_CPUS * 9)/32 - 1 : PAGE_SIZE)
1358 #define CPULIST_FILE_MAX_BYTES (((NR_CPUS * 7)/2 > PAGE_SIZE) ? (NR_CPUS * 7)/2 : PAGE_SIZE)
1360 #endif /* __LINUX_CPUMASK_H */