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_last - get the last CPU in a cpumask
192 * @srcp: - the cpumask pointer
194 * Return: >= nr_cpumask_bits if no CPUs set.
196 static inline unsigned int cpumask_last(const struct cpumask *srcp)
198 return find_last_bit(cpumask_bits(srcp), small_cpumask_bits);
202 * cpumask_next - get the next cpu in a cpumask
203 * @n: the cpu prior to the place to search (i.e. return will be > @n)
204 * @srcp: the cpumask pointer
206 * Return: >= nr_cpu_ids if no further cpus set.
209 unsigned int cpumask_next(int n, const struct cpumask *srcp)
211 /* -1 is a legal arg here. */
214 return find_next_bit(cpumask_bits(srcp), small_cpumask_bits, n + 1);
218 * cpumask_next_zero - get the next unset cpu in a cpumask
219 * @n: the cpu prior to the place to search (i.e. return will be > @n)
220 * @srcp: the cpumask pointer
222 * Return: >= nr_cpu_ids if no further cpus unset.
224 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
226 /* -1 is a legal arg here. */
229 return find_next_zero_bit(cpumask_bits(srcp), small_cpumask_bits, n+1);
233 /* Uniprocessor: there is only one valid CPU */
234 static inline unsigned int cpumask_local_spread(unsigned int i, int node)
239 static inline unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
240 const struct cpumask *src2p)
242 return cpumask_first_and(src1p, src2p);
245 static inline unsigned int cpumask_any_distribute(const struct cpumask *srcp)
247 return cpumask_first(srcp);
250 unsigned int cpumask_local_spread(unsigned int i, int node);
251 unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
252 const struct cpumask *src2p);
253 unsigned int cpumask_any_distribute(const struct cpumask *srcp);
257 * cpumask_next_and - get the next cpu in *src1p & *src2p
258 * @n: the cpu prior to the place to search (i.e. return will be > @n)
259 * @src1p: the first cpumask pointer
260 * @src2p: the second cpumask pointer
262 * Return: >= nr_cpu_ids if no further cpus set in both.
265 unsigned int cpumask_next_and(int n, const struct cpumask *src1p,
266 const struct cpumask *src2p)
268 /* -1 is a legal arg here. */
271 return find_next_and_bit(cpumask_bits(src1p), cpumask_bits(src2p),
272 small_cpumask_bits, n + 1);
276 * for_each_cpu - iterate over every cpu in a mask
277 * @cpu: the (optionally unsigned) integer iterator
278 * @mask: the cpumask pointer
280 * After the loop, cpu is >= nr_cpu_ids.
282 #define for_each_cpu(cpu, mask) \
283 for_each_set_bit(cpu, cpumask_bits(mask), small_cpumask_bits)
287 unsigned int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap)
289 cpumask_check(start);
294 * Return the first available CPU when wrapping, or when starting before cpu0,
295 * since there is only one valid option.
298 return nr_cpumask_bits;
300 return cpumask_first(mask);
303 unsigned int __pure cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap);
307 * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a specified location
308 * @cpu: the (optionally unsigned) integer iterator
309 * @mask: the cpumask pointer
310 * @start: the start location
312 * The implementation does not assume any bit in @mask is set (including @start).
314 * After the loop, cpu is >= nr_cpu_ids.
316 #define for_each_cpu_wrap(cpu, mask, start) \
317 for_each_set_bit_wrap(cpu, cpumask_bits(mask), small_cpumask_bits, start)
320 * for_each_cpu_and - iterate over every cpu in both masks
321 * @cpu: the (optionally unsigned) integer iterator
322 * @mask1: the first cpumask pointer
323 * @mask2: the second cpumask pointer
325 * This saves a temporary CPU mask in many places. It is equivalent to:
326 * struct cpumask tmp;
327 * cpumask_and(&tmp, &mask1, &mask2);
328 * for_each_cpu(cpu, &tmp)
331 * After the loop, cpu is >= nr_cpu_ids.
333 #define for_each_cpu_and(cpu, mask1, mask2) \
334 for_each_and_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
337 * for_each_cpu_andnot - iterate over every cpu present in one mask, excluding
338 * those present in another.
339 * @cpu: the (optionally unsigned) integer iterator
340 * @mask1: the first cpumask pointer
341 * @mask2: the second cpumask pointer
343 * This saves a temporary CPU mask in many places. It is equivalent to:
344 * struct cpumask tmp;
345 * cpumask_andnot(&tmp, &mask1, &mask2);
346 * for_each_cpu(cpu, &tmp)
349 * After the loop, cpu is >= nr_cpu_ids.
351 #define for_each_cpu_andnot(cpu, mask1, mask2) \
352 for_each_andnot_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
355 * for_each_cpu_or - iterate over every cpu present in either mask
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_or(&tmp, &mask1, &mask2);
363 * for_each_cpu(cpu, &tmp)
366 * After the loop, cpu is >= nr_cpu_ids.
368 #define for_each_cpu_or(cpu, mask1, mask2) \
369 for_each_or_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
372 * cpumask_any_but - return a "random" in a cpumask, but not this one.
373 * @mask: the cpumask to search
374 * @cpu: the cpu to ignore.
376 * Often used to find any cpu but smp_processor_id() in a mask.
377 * Return: >= nr_cpu_ids if no cpus set.
380 unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
385 for_each_cpu(i, mask)
392 * cpumask_nth - get the Nth cpu in a cpumask
393 * @srcp: the cpumask pointer
394 * @cpu: the Nth cpu to find, starting from 0
396 * Return: >= nr_cpu_ids if such cpu doesn't exist.
398 static inline unsigned int cpumask_nth(unsigned int cpu, const struct cpumask *srcp)
400 return find_nth_bit(cpumask_bits(srcp), small_cpumask_bits, cpumask_check(cpu));
404 * cpumask_nth_and - get the Nth cpu in 2 cpumasks
405 * @srcp1: the cpumask pointer
406 * @srcp2: the cpumask pointer
407 * @cpu: the Nth cpu to find, starting from 0
409 * Return: >= nr_cpu_ids if such cpu doesn't exist.
412 unsigned int cpumask_nth_and(unsigned int cpu, const struct cpumask *srcp1,
413 const struct cpumask *srcp2)
415 return find_nth_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
416 small_cpumask_bits, cpumask_check(cpu));
420 * cpumask_nth_andnot - get the Nth cpu set in 1st cpumask, and clear in 2nd.
421 * @srcp1: the cpumask pointer
422 * @srcp2: the cpumask pointer
423 * @cpu: the Nth cpu to find, starting from 0
425 * Return: >= nr_cpu_ids if such cpu doesn't exist.
428 unsigned int cpumask_nth_andnot(unsigned int cpu, const struct cpumask *srcp1,
429 const struct cpumask *srcp2)
431 return find_nth_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
432 small_cpumask_bits, cpumask_check(cpu));
436 * cpumask_nth_and_andnot - get the Nth cpu set in 1st and 2nd cpumask, and clear in 3rd.
437 * @srcp1: the cpumask pointer
438 * @srcp2: the cpumask pointer
439 * @srcp3: the cpumask pointer
440 * @cpu: the Nth cpu to find, starting from 0
442 * Return: >= nr_cpu_ids if such cpu doesn't exist.
444 static __always_inline
445 unsigned int cpumask_nth_and_andnot(unsigned int cpu, const struct cpumask *srcp1,
446 const struct cpumask *srcp2,
447 const struct cpumask *srcp3)
449 return find_nth_and_andnot_bit(cpumask_bits(srcp1),
452 small_cpumask_bits, cpumask_check(cpu));
455 #define CPU_BITS_NONE \
457 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
460 #define CPU_BITS_CPU0 \
466 * cpumask_set_cpu - set a cpu in a cpumask
467 * @cpu: cpu number (< nr_cpu_ids)
468 * @dstp: the cpumask pointer
470 static __always_inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
472 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
475 static __always_inline void __cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
477 __set_bit(cpumask_check(cpu), cpumask_bits(dstp));
482 * cpumask_clear_cpu - clear a cpu in a cpumask
483 * @cpu: cpu number (< nr_cpu_ids)
484 * @dstp: the cpumask pointer
486 static __always_inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
488 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
491 static __always_inline void __cpumask_clear_cpu(int cpu, struct cpumask *dstp)
493 __clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
497 * cpumask_test_cpu - test for a cpu in a cpumask
498 * @cpu: cpu number (< nr_cpu_ids)
499 * @cpumask: the cpumask pointer
501 * Return: true if @cpu is set in @cpumask, else returns false
503 static __always_inline bool cpumask_test_cpu(int cpu, const struct cpumask *cpumask)
505 return test_bit(cpumask_check(cpu), cpumask_bits((cpumask)));
509 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
510 * @cpu: cpu number (< nr_cpu_ids)
511 * @cpumask: the cpumask pointer
513 * test_and_set_bit wrapper for cpumasks.
515 * Return: true if @cpu is set in old bitmap of @cpumask, else returns false
517 static __always_inline bool cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
519 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
523 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
524 * @cpu: cpu number (< nr_cpu_ids)
525 * @cpumask: the cpumask pointer
527 * test_and_clear_bit wrapper for cpumasks.
529 * Return: true if @cpu is set in old bitmap of @cpumask, else returns false
531 static __always_inline bool cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
533 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
537 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
538 * @dstp: the cpumask pointer
540 static inline void cpumask_setall(struct cpumask *dstp)
542 if (small_const_nbits(small_cpumask_bits)) {
543 cpumask_bits(dstp)[0] = BITMAP_LAST_WORD_MASK(nr_cpumask_bits);
546 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
550 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
551 * @dstp: the cpumask pointer
553 static inline void cpumask_clear(struct cpumask *dstp)
555 bitmap_zero(cpumask_bits(dstp), large_cpumask_bits);
559 * cpumask_and - *dstp = *src1p & *src2p
560 * @dstp: the cpumask result
561 * @src1p: the first input
562 * @src2p: the second input
564 * Return: false if *@dstp is empty, else returns true
566 static inline bool cpumask_and(struct cpumask *dstp,
567 const struct cpumask *src1p,
568 const struct cpumask *src2p)
570 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
571 cpumask_bits(src2p), small_cpumask_bits);
575 * cpumask_or - *dstp = *src1p | *src2p
576 * @dstp: the cpumask result
577 * @src1p: the first input
578 * @src2p: the second input
580 static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
581 const struct cpumask *src2p)
583 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
584 cpumask_bits(src2p), small_cpumask_bits);
588 * cpumask_xor - *dstp = *src1p ^ *src2p
589 * @dstp: the cpumask result
590 * @src1p: the first input
591 * @src2p: the second input
593 static inline void cpumask_xor(struct cpumask *dstp,
594 const struct cpumask *src1p,
595 const struct cpumask *src2p)
597 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
598 cpumask_bits(src2p), small_cpumask_bits);
602 * cpumask_andnot - *dstp = *src1p & ~*src2p
603 * @dstp: the cpumask result
604 * @src1p: the first input
605 * @src2p: the second input
607 * Return: false if *@dstp is empty, else returns true
609 static inline bool cpumask_andnot(struct cpumask *dstp,
610 const struct cpumask *src1p,
611 const struct cpumask *src2p)
613 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
614 cpumask_bits(src2p), small_cpumask_bits);
618 * cpumask_equal - *src1p == *src2p
619 * @src1p: the first input
620 * @src2p: the second input
622 * Return: true if the cpumasks are equal, false if not
624 static inline bool cpumask_equal(const struct cpumask *src1p,
625 const struct cpumask *src2p)
627 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
632 * cpumask_or_equal - *src1p | *src2p == *src3p
633 * @src1p: the first input
634 * @src2p: the second input
635 * @src3p: the third input
637 * Return: true if first cpumask ORed with second cpumask == third cpumask,
640 static inline bool cpumask_or_equal(const struct cpumask *src1p,
641 const struct cpumask *src2p,
642 const struct cpumask *src3p)
644 return bitmap_or_equal(cpumask_bits(src1p), cpumask_bits(src2p),
645 cpumask_bits(src3p), small_cpumask_bits);
649 * cpumask_intersects - (*src1p & *src2p) != 0
650 * @src1p: the first input
651 * @src2p: the second input
653 * Return: true if first cpumask ANDed with second cpumask is non-empty,
656 static inline bool cpumask_intersects(const struct cpumask *src1p,
657 const struct cpumask *src2p)
659 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
664 * cpumask_subset - (*src1p & ~*src2p) == 0
665 * @src1p: the first input
666 * @src2p: the second input
668 * Return: true if *@src1p is a subset of *@src2p, else returns false
670 static inline bool cpumask_subset(const struct cpumask *src1p,
671 const struct cpumask *src2p)
673 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
678 * cpumask_empty - *srcp == 0
679 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
681 * Return: true if srcp is empty (has no bits set), else false
683 static inline bool cpumask_empty(const struct cpumask *srcp)
685 return bitmap_empty(cpumask_bits(srcp), small_cpumask_bits);
689 * cpumask_full - *srcp == 0xFFFFFFFF...
690 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
692 * Return: true if srcp is full (has all bits set), else false
694 static inline bool cpumask_full(const struct cpumask *srcp)
696 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
700 * cpumask_weight - Count of bits in *srcp
701 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
703 * Return: count of bits set in *srcp
705 static inline unsigned int cpumask_weight(const struct cpumask *srcp)
707 return bitmap_weight(cpumask_bits(srcp), small_cpumask_bits);
711 * cpumask_weight_and - Count of bits in (*srcp1 & *srcp2)
712 * @srcp1: the cpumask to count bits (< nr_cpu_ids) in.
713 * @srcp2: the cpumask to count bits (< nr_cpu_ids) in.
715 * Return: count of bits set in both *srcp1 and *srcp2
717 static inline unsigned int cpumask_weight_and(const struct cpumask *srcp1,
718 const struct cpumask *srcp2)
720 return bitmap_weight_and(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
724 * cpumask_weight_andnot - Count of bits in (*srcp1 & ~*srcp2)
725 * @srcp1: the cpumask to count bits (< nr_cpu_ids) in.
726 * @srcp2: the cpumask to count bits (< nr_cpu_ids) in.
728 * Return: count of bits set in both *srcp1 and *srcp2
730 static inline unsigned int cpumask_weight_andnot(const struct cpumask *srcp1,
731 const struct cpumask *srcp2)
733 return bitmap_weight_andnot(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
737 * cpumask_shift_right - *dstp = *srcp >> n
738 * @dstp: the cpumask result
739 * @srcp: the input to shift
740 * @n: the number of bits to shift by
742 static inline void cpumask_shift_right(struct cpumask *dstp,
743 const struct cpumask *srcp, int n)
745 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
750 * cpumask_shift_left - *dstp = *srcp << n
751 * @dstp: the cpumask result
752 * @srcp: the input to shift
753 * @n: the number of bits to shift by
755 static inline void cpumask_shift_left(struct cpumask *dstp,
756 const struct cpumask *srcp, int n)
758 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
763 * cpumask_copy - *dstp = *srcp
765 * @srcp: the input cpumask
767 static inline void cpumask_copy(struct cpumask *dstp,
768 const struct cpumask *srcp)
770 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), large_cpumask_bits);
774 * cpumask_any - pick a "random" cpu from *srcp
775 * @srcp: the input cpumask
777 * Return: >= nr_cpu_ids if no cpus set.
779 #define cpumask_any(srcp) cpumask_first(srcp)
782 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
783 * @mask1: the first input cpumask
784 * @mask2: the second input cpumask
786 * Return: >= nr_cpu_ids if no cpus set.
788 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
791 * cpumask_of - the cpumask containing just a given cpu
792 * @cpu: the cpu (<= nr_cpu_ids)
794 #define cpumask_of(cpu) (get_cpu_mask(cpu))
797 * cpumask_parse_user - extract a cpumask from a user string
798 * @buf: the buffer to extract from
799 * @len: the length of the buffer
800 * @dstp: the cpumask to set.
802 * Return: -errno, or 0 for success.
804 static inline int cpumask_parse_user(const char __user *buf, int len,
805 struct cpumask *dstp)
807 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
811 * cpumask_parselist_user - extract a cpumask from a user string
812 * @buf: the buffer to extract from
813 * @len: the length of the buffer
814 * @dstp: the cpumask to set.
816 * Return: -errno, or 0 for success.
818 static inline int cpumask_parselist_user(const char __user *buf, int len,
819 struct cpumask *dstp)
821 return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
826 * cpumask_parse - extract a cpumask from a string
827 * @buf: the buffer to extract from
828 * @dstp: the cpumask to set.
830 * Return: -errno, or 0 for success.
832 static inline int cpumask_parse(const char *buf, struct cpumask *dstp)
834 return bitmap_parse(buf, UINT_MAX, cpumask_bits(dstp), nr_cpumask_bits);
838 * cpulist_parse - extract a cpumask from a user string of ranges
839 * @buf: the buffer to extract from
840 * @dstp: the cpumask to set.
842 * Return: -errno, or 0 for success.
844 static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
846 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
850 * cpumask_size - calculate size to allocate for a 'struct cpumask' in bytes
852 * Return: size to allocate for a &struct cpumask in bytes
854 static inline unsigned int cpumask_size(void)
856 return BITS_TO_LONGS(large_cpumask_bits) * sizeof(long);
860 * cpumask_var_t: struct cpumask for stack usage.
862 * Oh, the wicked games we play! In order to make kernel coding a
863 * little more difficult, we typedef cpumask_var_t to an array or a
864 * pointer: doing &mask on an array is a noop, so it still works.
867 * cpumask_var_t tmpmask;
868 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
871 * ... use 'tmpmask' like a normal struct cpumask * ...
873 * free_cpumask_var(tmpmask);
876 * However, one notable exception is there. alloc_cpumask_var() allocates
877 * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has
878 * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t.
880 * cpumask_var_t tmpmask;
881 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
886 * This code makes NR_CPUS length memcopy and brings to a memory corruption.
887 * cpumask_copy() provide safe copy functionality.
889 * Note that there is another evil here: If you define a cpumask_var_t
890 * as a percpu variable then the way to obtain the address of the cpumask
891 * structure differently influences what this_cpu_* operation needs to be
892 * used. Please use this_cpu_cpumask_var_t in those cases. The direct use
893 * of this_cpu_ptr() or this_cpu_read() will lead to failures when the
894 * other type of cpumask_var_t implementation is configured.
896 * Please also note that __cpumask_var_read_mostly can be used to declare
897 * a cpumask_var_t variable itself (not its content) as read mostly.
899 #ifdef CONFIG_CPUMASK_OFFSTACK
900 typedef struct cpumask *cpumask_var_t;
902 #define this_cpu_cpumask_var_ptr(x) this_cpu_read(x)
903 #define __cpumask_var_read_mostly __read_mostly
905 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
908 bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
910 return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node);
914 * alloc_cpumask_var - allocate a struct cpumask
915 * @mask: pointer to cpumask_var_t where the cpumask is returned
918 * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
919 * a nop returning a constant 1 (in <linux/cpumask.h>).
921 * See alloc_cpumask_var_node.
923 * Return: %true if allocation succeeded, %false if not
926 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
928 return alloc_cpumask_var_node(mask, flags, NUMA_NO_NODE);
932 bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
934 return alloc_cpumask_var(mask, flags | __GFP_ZERO);
937 void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
938 void free_cpumask_var(cpumask_var_t mask);
939 void free_bootmem_cpumask_var(cpumask_var_t mask);
941 static inline bool cpumask_available(cpumask_var_t mask)
947 typedef struct cpumask cpumask_var_t[1];
949 #define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x)
950 #define __cpumask_var_read_mostly
952 static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
957 static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
963 static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
965 cpumask_clear(*mask);
969 static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
972 cpumask_clear(*mask);
976 static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
980 static inline void free_cpumask_var(cpumask_var_t mask)
984 static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
988 static inline bool cpumask_available(cpumask_var_t mask)
992 #endif /* CONFIG_CPUMASK_OFFSTACK */
994 DEFINE_FREE(free_cpumask_var, struct cpumask *, if (_T) free_cpumask_var(_T));
996 /* It's common to want to use cpu_all_mask in struct member initializers,
997 * so it has to refer to an address rather than a pointer. */
998 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
999 #define cpu_all_mask to_cpumask(cpu_all_bits)
1001 /* First bits of cpu_bit_bitmap are in fact unset. */
1002 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
1005 /* Uniprocessor: the possible/online/present masks are always "1" */
1006 #define for_each_possible_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
1007 #define for_each_online_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
1008 #define for_each_present_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
1010 #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
1011 #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
1012 #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
1015 /* Wrappers for arch boot code to manipulate normally-constant masks */
1016 void init_cpu_present(const struct cpumask *src);
1017 void init_cpu_possible(const struct cpumask *src);
1018 void init_cpu_online(const struct cpumask *src);
1020 static inline void reset_cpu_possible_mask(void)
1022 bitmap_zero(cpumask_bits(&__cpu_possible_mask), NR_CPUS);
1026 set_cpu_possible(unsigned int cpu, bool possible)
1029 cpumask_set_cpu(cpu, &__cpu_possible_mask);
1031 cpumask_clear_cpu(cpu, &__cpu_possible_mask);
1035 set_cpu_present(unsigned int cpu, bool present)
1038 cpumask_set_cpu(cpu, &__cpu_present_mask);
1040 cpumask_clear_cpu(cpu, &__cpu_present_mask);
1043 void set_cpu_online(unsigned int cpu, bool online);
1046 set_cpu_active(unsigned int cpu, bool active)
1049 cpumask_set_cpu(cpu, &__cpu_active_mask);
1051 cpumask_clear_cpu(cpu, &__cpu_active_mask);
1055 set_cpu_dying(unsigned int cpu, bool dying)
1058 cpumask_set_cpu(cpu, &__cpu_dying_mask);
1060 cpumask_clear_cpu(cpu, &__cpu_dying_mask);
1064 * to_cpumask - convert a NR_CPUS bitmap to a struct cpumask *
1065 * @bitmap: the bitmap
1067 * There are a few places where cpumask_var_t isn't appropriate and
1068 * static cpumasks must be used (eg. very early boot), yet we don't
1069 * expose the definition of 'struct cpumask'.
1071 * This does the conversion, and can be used as a constant initializer.
1073 #define to_cpumask(bitmap) \
1074 ((struct cpumask *)(1 ? (bitmap) \
1075 : (void *)sizeof(__check_is_bitmap(bitmap))))
1077 static inline int __check_is_bitmap(const unsigned long *bitmap)
1083 * Special-case data structure for "single bit set only" constant CPU masks.
1085 * We pre-generate all the 64 (or 32) possible bit positions, with enough
1086 * padding to the left and the right, and return the constant pointer
1087 * appropriately offset.
1089 extern const unsigned long
1090 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
1092 static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
1094 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
1095 p -= cpu / BITS_PER_LONG;
1096 return to_cpumask(p);
1101 * num_online_cpus() - Read the number of online CPUs
1103 * Despite the fact that __num_online_cpus is of type atomic_t, this
1104 * interface gives only a momentary snapshot and is not protected against
1105 * concurrent CPU hotplug operations unless invoked from a cpuhp_lock held
1108 * Return: momentary snapshot of the number of online CPUs
1110 static __always_inline unsigned int num_online_cpus(void)
1112 return raw_atomic_read(&__num_online_cpus);
1114 #define num_possible_cpus() cpumask_weight(cpu_possible_mask)
1115 #define num_present_cpus() cpumask_weight(cpu_present_mask)
1116 #define num_active_cpus() cpumask_weight(cpu_active_mask)
1118 static inline bool cpu_online(unsigned int cpu)
1120 return cpumask_test_cpu(cpu, cpu_online_mask);
1123 static inline bool cpu_possible(unsigned int cpu)
1125 return cpumask_test_cpu(cpu, cpu_possible_mask);
1128 static inline bool cpu_present(unsigned int cpu)
1130 return cpumask_test_cpu(cpu, cpu_present_mask);
1133 static inline bool cpu_active(unsigned int cpu)
1135 return cpumask_test_cpu(cpu, cpu_active_mask);
1138 static inline bool cpu_dying(unsigned int cpu)
1140 return cpumask_test_cpu(cpu, cpu_dying_mask);
1145 #define num_online_cpus() 1U
1146 #define num_possible_cpus() 1U
1147 #define num_present_cpus() 1U
1148 #define num_active_cpus() 1U
1150 static inline bool cpu_online(unsigned int cpu)
1155 static inline bool cpu_possible(unsigned int cpu)
1160 static inline bool cpu_present(unsigned int cpu)
1165 static inline bool cpu_active(unsigned int cpu)
1170 static inline bool cpu_dying(unsigned int cpu)
1175 #endif /* NR_CPUS > 1 */
1177 #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
1179 #if NR_CPUS <= BITS_PER_LONG
1180 #define CPU_BITS_ALL \
1182 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1185 #else /* NR_CPUS > BITS_PER_LONG */
1187 #define CPU_BITS_ALL \
1189 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
1190 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1192 #endif /* NR_CPUS > BITS_PER_LONG */
1195 * cpumap_print_to_pagebuf - copies the cpumask into the buffer either
1196 * as comma-separated list of cpus or hex values of cpumask
1197 * @list: indicates whether the cpumap must be list
1198 * @mask: the cpumask to copy
1199 * @buf: the buffer to copy into
1201 * Return: the length of the (null-terminated) @buf string, zero if
1202 * nothing is copied.
1204 static inline ssize_t
1205 cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask)
1207 return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask),
1212 * cpumap_print_bitmask_to_buf - copies the cpumask into the buffer as
1213 * hex values of cpumask
1215 * @buf: the buffer to copy into
1216 * @mask: the cpumask to copy
1217 * @off: in the string from which we are copying, we copy to @buf
1218 * @count: the maximum number of bytes to print
1220 * The function prints the cpumask into the buffer as hex values of
1221 * cpumask; Typically used by bin_attribute to export cpumask bitmask
1224 * Return: the length of how many bytes have been copied, excluding
1227 static inline ssize_t
1228 cpumap_print_bitmask_to_buf(char *buf, const struct cpumask *mask,
1229 loff_t off, size_t count)
1231 return bitmap_print_bitmask_to_buf(buf, cpumask_bits(mask),
1232 nr_cpu_ids, off, count) - 1;
1236 * cpumap_print_list_to_buf - copies the cpumask into the buffer as
1237 * comma-separated list of cpus
1238 * @buf: the buffer to copy into
1239 * @mask: the cpumask to copy
1240 * @off: in the string from which we are copying, we copy to @buf
1241 * @count: the maximum number of bytes to print
1243 * Everything is same with the above cpumap_print_bitmask_to_buf()
1244 * except the print format.
1246 * Return: the length of how many bytes have been copied, excluding
1249 static inline ssize_t
1250 cpumap_print_list_to_buf(char *buf, const struct cpumask *mask,
1251 loff_t off, size_t count)
1253 return bitmap_print_list_to_buf(buf, cpumask_bits(mask),
1254 nr_cpu_ids, off, count) - 1;
1257 #if NR_CPUS <= BITS_PER_LONG
1258 #define CPU_MASK_ALL \
1260 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1263 #define CPU_MASK_ALL \
1265 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
1266 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1268 #endif /* NR_CPUS > BITS_PER_LONG */
1270 #define CPU_MASK_NONE \
1272 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
1275 #define CPU_MASK_CPU0 \
1281 * Provide a valid theoretical max size for cpumap and cpulist sysfs files
1282 * to avoid breaking userspace which may allocate a buffer based on the size
1283 * reported by e.g. fstat.
1285 * for cpumap NR_CPUS * 9/32 - 1 should be an exact length.
1287 * For cpulist 7 is (ceil(log10(NR_CPUS)) + 1) allowing for NR_CPUS to be up
1288 * to 2 orders of magnitude larger than 8192. And then we divide by 2 to
1289 * cover a worst-case of every other cpu being on one of two nodes for a
1290 * very large NR_CPUS.
1292 * Use PAGE_SIZE as a minimum for smaller configurations while avoiding
1293 * unsigned comparison to -1.
1295 #define CPUMAP_FILE_MAX_BYTES (((NR_CPUS * 9)/32 > PAGE_SIZE) \
1296 ? (NR_CPUS * 9)/32 - 1 : PAGE_SIZE)
1297 #define CPULIST_FILE_MAX_BYTES (((NR_CPUS * 7)/2 > PAGE_SIZE) ? (NR_CPUS * 7)/2 : PAGE_SIZE)
1299 #endif /* __LINUX_CPUMASK_H */