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 CPU's in a system, one bit position per CPU number. In general,
8 * only nr_cpu_ids (<= NR_CPUS) bits are valid.
10 #include <linux/kernel.h>
11 #include <linux/threads.h>
12 #include <linux/bitmap.h>
13 #include <linux/atomic.h>
14 #include <linux/bug.h>
15 #include <linux/gfp_types.h>
16 #include <linux/numa.h>
18 /* Don't assign or return these: may not be this big! */
19 typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
22 * cpumask_bits - get the bits in a cpumask
23 * @maskp: the struct cpumask *
25 * You should only assume nr_cpu_ids bits of this mask are valid. This is
26 * a macro so it's const-correct.
28 #define cpumask_bits(maskp) ((maskp)->bits)
31 * cpumask_pr_args - printf args to output a cpumask
32 * @maskp: cpumask to be printed
34 * Can be used to provide arguments for '%*pb[l]' when printing a cpumask.
36 #define cpumask_pr_args(maskp) nr_cpu_ids, cpumask_bits(maskp)
41 extern unsigned int nr_cpu_ids;
44 #ifdef CONFIG_CPUMASK_OFFSTACK
45 /* Assuming NR_CPUS is huge, a runtime limit is more efficient. Also,
46 * not all bits may be allocated. */
47 #define nr_cpumask_bits nr_cpu_ids
49 #define nr_cpumask_bits ((unsigned int)NR_CPUS)
53 * The following particular system cpumasks and operations manage
54 * possible, present, active and online cpus.
56 * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
57 * cpu_present_mask - has bit 'cpu' set iff cpu is populated
58 * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler
59 * cpu_active_mask - has bit 'cpu' set iff cpu available to migration
61 * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
63 * The cpu_possible_mask is fixed at boot time, as the set of CPU id's
64 * that it is possible might ever be plugged in at anytime during the
65 * life of that system boot. The cpu_present_mask is dynamic(*),
66 * representing which CPUs are currently plugged in. And
67 * cpu_online_mask is the dynamic subset of cpu_present_mask,
68 * indicating those CPUs available for scheduling.
70 * If HOTPLUG is enabled, then cpu_possible_mask is forced to have
71 * all NR_CPUS bits set, otherwise it is just the set of CPUs that
72 * ACPI reports present at boot.
74 * If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
75 * depending on what ACPI reports as currently plugged in, otherwise
76 * cpu_present_mask is just a copy of cpu_possible_mask.
78 * (*) Well, cpu_present_mask is dynamic in the hotplug case. If not
79 * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
82 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
83 * assumption that their single CPU is online. The UP
84 * cpu_{online,possible,present}_masks are placebos. Changing them
85 * will have no useful affect on the following num_*_cpus()
86 * and cpu_*() macros in the UP case. This ugliness is a UP
87 * optimization - don't waste any instructions or memory references
88 * asking if you're online or how many CPUs there are if there is
92 extern struct cpumask __cpu_possible_mask;
93 extern struct cpumask __cpu_online_mask;
94 extern struct cpumask __cpu_present_mask;
95 extern struct cpumask __cpu_active_mask;
96 extern struct cpumask __cpu_dying_mask;
97 #define cpu_possible_mask ((const struct cpumask *)&__cpu_possible_mask)
98 #define cpu_online_mask ((const struct cpumask *)&__cpu_online_mask)
99 #define cpu_present_mask ((const struct cpumask *)&__cpu_present_mask)
100 #define cpu_active_mask ((const struct cpumask *)&__cpu_active_mask)
101 #define cpu_dying_mask ((const struct cpumask *)&__cpu_dying_mask)
103 extern atomic_t __num_online_cpus;
105 extern cpumask_t cpus_booted_once_mask;
107 static __always_inline void cpu_max_bits_warn(unsigned int cpu, unsigned int bits)
109 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
110 WARN_ON_ONCE(cpu >= bits);
111 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
114 /* verify cpu argument to cpumask_* operators */
115 static __always_inline unsigned int cpumask_check(unsigned int cpu)
117 cpu_max_bits_warn(cpu, nr_cpumask_bits);
122 /* Uniprocessor. Assume all masks are "1". */
123 static inline unsigned int cpumask_first(const struct cpumask *srcp)
128 static inline unsigned int cpumask_first_zero(const struct cpumask *srcp)
133 static inline unsigned int cpumask_first_and(const struct cpumask *srcp1,
134 const struct cpumask *srcp2)
139 static inline unsigned int cpumask_last(const struct cpumask *srcp)
144 /* Valid inputs for n are -1 and 0. */
145 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
150 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
155 static inline unsigned int cpumask_next_and(int n,
156 const struct cpumask *srcp,
157 const struct cpumask *andp)
162 static inline unsigned int cpumask_next_wrap(int n, const struct cpumask *mask,
163 int start, bool wrap)
165 /* cpu0 unless stop condition, wrap and at cpu0, then nr_cpumask_bits */
166 return (wrap && n == 0);
169 /* cpu must be a valid cpu, ie 0, so there's no other choice. */
170 static inline unsigned int cpumask_any_but(const struct cpumask *mask,
176 static inline unsigned int cpumask_local_spread(unsigned int i, int node)
181 static inline unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
182 const struct cpumask *src2p) {
183 return cpumask_first_and(src1p, src2p);
186 static inline unsigned int cpumask_any_distribute(const struct cpumask *srcp)
188 return cpumask_first(srcp);
191 #define for_each_cpu(cpu, mask) \
192 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
193 #define for_each_cpu_not(cpu, mask) \
194 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
195 #define for_each_cpu_wrap(cpu, mask, start) \
196 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)(start))
197 #define for_each_cpu_and(cpu, mask1, mask2) \
198 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask1, (void)mask2)
201 * cpumask_first - get the first cpu in a cpumask
202 * @srcp: the cpumask pointer
204 * Returns >= nr_cpu_ids if no cpus set.
206 static inline unsigned int cpumask_first(const struct cpumask *srcp)
208 return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
212 * cpumask_first_zero - get the first unset cpu in a cpumask
213 * @srcp: the cpumask pointer
215 * Returns >= nr_cpu_ids if all cpus are set.
217 static inline unsigned int cpumask_first_zero(const struct cpumask *srcp)
219 return find_first_zero_bit(cpumask_bits(srcp), nr_cpumask_bits);
223 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
224 * @src1p: the first input
225 * @src2p: the second input
227 * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
230 unsigned int cpumask_first_and(const struct cpumask *srcp1, const struct cpumask *srcp2)
232 return find_first_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), nr_cpumask_bits);
236 * cpumask_last - get the last CPU in a cpumask
237 * @srcp: - the cpumask pointer
239 * Returns >= nr_cpumask_bits if no CPUs set.
241 static inline unsigned int cpumask_last(const struct cpumask *srcp)
243 return find_last_bit(cpumask_bits(srcp), nr_cpumask_bits);
247 * cpumask_next - get the next cpu in a cpumask
248 * @n: the cpu prior to the place to search (ie. return will be > @n)
249 * @srcp: the cpumask pointer
251 * Returns >= nr_cpu_ids if no further cpus set.
254 unsigned int cpumask_next(int n, const struct cpumask *srcp)
256 /* -1 is a legal arg here. */
259 return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n + 1);
263 * cpumask_next_zero - get the next unset cpu in a cpumask
264 * @n: the cpu prior to the place to search (ie. return will be > @n)
265 * @srcp: the cpumask pointer
267 * Returns >= nr_cpu_ids if no further cpus unset.
269 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
271 /* -1 is a legal arg here. */
274 return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
278 * cpumask_next_and - get the next cpu in *src1p & *src2p
279 * @n: the cpu prior to the place to search (ie. return will be > @n)
280 * @src1p: the first cpumask pointer
281 * @src2p: the second cpumask pointer
283 * Returns >= nr_cpu_ids if no further cpus set in both.
286 unsigned int cpumask_next_and(int n, const struct cpumask *src1p,
287 const struct cpumask *src2p)
289 /* -1 is a legal arg here. */
292 return find_next_and_bit(cpumask_bits(src1p), cpumask_bits(src2p),
293 nr_cpumask_bits, n + 1);
296 unsigned int cpumask_local_spread(unsigned int i, int node);
297 unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
298 const struct cpumask *src2p);
299 unsigned int cpumask_any_distribute(const struct cpumask *srcp);
302 * for_each_cpu - iterate over every cpu in a mask
303 * @cpu: the (optionally unsigned) integer iterator
304 * @mask: the cpumask pointer
306 * After the loop, cpu is >= nr_cpu_ids.
308 #define for_each_cpu(cpu, mask) \
310 (cpu) = cpumask_next((cpu), (mask)), \
314 * for_each_cpu_not - iterate over every cpu in a complemented mask
315 * @cpu: the (optionally unsigned) integer iterator
316 * @mask: the cpumask pointer
318 * After the loop, cpu is >= nr_cpu_ids.
320 #define for_each_cpu_not(cpu, mask) \
322 (cpu) = cpumask_next_zero((cpu), (mask)), \
325 unsigned int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap);
328 * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a specified location
329 * @cpu: the (optionally unsigned) integer iterator
330 * @mask: the cpumask pointer
331 * @start: the start location
333 * The implementation does not assume any bit in @mask is set (including @start).
335 * After the loop, cpu is >= nr_cpu_ids.
337 #define for_each_cpu_wrap(cpu, mask, start) \
338 for ((cpu) = cpumask_next_wrap((start)-1, (mask), (start), false); \
339 (cpu) < nr_cpumask_bits; \
340 (cpu) = cpumask_next_wrap((cpu), (mask), (start), true))
343 * for_each_cpu_and - iterate over every cpu in both masks
344 * @cpu: the (optionally unsigned) integer iterator
345 * @mask1: the first cpumask pointer
346 * @mask2: the second cpumask pointer
348 * This saves a temporary CPU mask in many places. It is equivalent to:
349 * struct cpumask tmp;
350 * cpumask_and(&tmp, &mask1, &mask2);
351 * for_each_cpu(cpu, &tmp)
354 * After the loop, cpu is >= nr_cpu_ids.
356 #define for_each_cpu_and(cpu, mask1, mask2) \
358 (cpu) = cpumask_next_and((cpu), (mask1), (mask2)), \
362 * cpumask_any_but - return a "random" in a cpumask, but not this one.
363 * @mask: the cpumask to search
364 * @cpu: the cpu to ignore.
366 * Often used to find any cpu but smp_processor_id() in a mask.
367 * Returns >= nr_cpu_ids if no cpus set.
370 unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
375 for_each_cpu(i, mask)
382 #define CPU_BITS_NONE \
384 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
387 #define CPU_BITS_CPU0 \
393 * cpumask_set_cpu - set a cpu in a cpumask
394 * @cpu: cpu number (< nr_cpu_ids)
395 * @dstp: the cpumask pointer
397 static __always_inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
399 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
402 static __always_inline void __cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
404 __set_bit(cpumask_check(cpu), cpumask_bits(dstp));
409 * cpumask_clear_cpu - clear a cpu in a cpumask
410 * @cpu: cpu number (< nr_cpu_ids)
411 * @dstp: the cpumask pointer
413 static __always_inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
415 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
418 static __always_inline void __cpumask_clear_cpu(int cpu, struct cpumask *dstp)
420 __clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
424 * cpumask_test_cpu - test for a cpu in a cpumask
425 * @cpu: cpu number (< nr_cpu_ids)
426 * @cpumask: the cpumask pointer
428 * Returns true if @cpu is set in @cpumask, else returns false
430 static __always_inline bool cpumask_test_cpu(int cpu, const struct cpumask *cpumask)
432 return test_bit(cpumask_check(cpu), cpumask_bits((cpumask)));
436 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
437 * @cpu: cpu number (< nr_cpu_ids)
438 * @cpumask: the cpumask pointer
440 * Returns true if @cpu is set in old bitmap of @cpumask, else returns false
442 * test_and_set_bit wrapper for cpumasks.
444 static __always_inline bool cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
446 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
450 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
451 * @cpu: cpu number (< nr_cpu_ids)
452 * @cpumask: the cpumask pointer
454 * Returns true if @cpu is set in old bitmap of @cpumask, else returns false
456 * test_and_clear_bit wrapper for cpumasks.
458 static __always_inline bool cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
460 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
464 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
465 * @dstp: the cpumask pointer
467 static inline void cpumask_setall(struct cpumask *dstp)
469 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
473 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
474 * @dstp: the cpumask pointer
476 static inline void cpumask_clear(struct cpumask *dstp)
478 bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
482 * cpumask_and - *dstp = *src1p & *src2p
483 * @dstp: the cpumask result
484 * @src1p: the first input
485 * @src2p: the second input
487 * If *@dstp is empty, returns false, else returns true
489 static inline bool cpumask_and(struct cpumask *dstp,
490 const struct cpumask *src1p,
491 const struct cpumask *src2p)
493 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
494 cpumask_bits(src2p), nr_cpumask_bits);
498 * cpumask_or - *dstp = *src1p | *src2p
499 * @dstp: the cpumask result
500 * @src1p: the first input
501 * @src2p: the second input
503 static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
504 const struct cpumask *src2p)
506 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
507 cpumask_bits(src2p), nr_cpumask_bits);
511 * cpumask_xor - *dstp = *src1p ^ *src2p
512 * @dstp: the cpumask result
513 * @src1p: the first input
514 * @src2p: the second input
516 static inline void cpumask_xor(struct cpumask *dstp,
517 const struct cpumask *src1p,
518 const struct cpumask *src2p)
520 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
521 cpumask_bits(src2p), nr_cpumask_bits);
525 * cpumask_andnot - *dstp = *src1p & ~*src2p
526 * @dstp: the cpumask result
527 * @src1p: the first input
528 * @src2p: the second input
530 * If *@dstp is empty, returns false, else returns true
532 static inline bool cpumask_andnot(struct cpumask *dstp,
533 const struct cpumask *src1p,
534 const struct cpumask *src2p)
536 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
537 cpumask_bits(src2p), nr_cpumask_bits);
541 * cpumask_complement - *dstp = ~*srcp
542 * @dstp: the cpumask result
543 * @srcp: the input to invert
545 static inline void cpumask_complement(struct cpumask *dstp,
546 const struct cpumask *srcp)
548 bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
553 * cpumask_equal - *src1p == *src2p
554 * @src1p: the first input
555 * @src2p: the second input
557 static inline bool cpumask_equal(const struct cpumask *src1p,
558 const struct cpumask *src2p)
560 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
565 * cpumask_or_equal - *src1p | *src2p == *src3p
566 * @src1p: the first input
567 * @src2p: the second input
568 * @src3p: the third input
570 static inline bool cpumask_or_equal(const struct cpumask *src1p,
571 const struct cpumask *src2p,
572 const struct cpumask *src3p)
574 return bitmap_or_equal(cpumask_bits(src1p), cpumask_bits(src2p),
575 cpumask_bits(src3p), nr_cpumask_bits);
579 * cpumask_intersects - (*src1p & *src2p) != 0
580 * @src1p: the first input
581 * @src2p: the second input
583 static inline bool cpumask_intersects(const struct cpumask *src1p,
584 const struct cpumask *src2p)
586 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
591 * cpumask_subset - (*src1p & ~*src2p) == 0
592 * @src1p: the first input
593 * @src2p: the second input
595 * Returns true if *@src1p is a subset of *@src2p, else returns false
597 static inline bool cpumask_subset(const struct cpumask *src1p,
598 const struct cpumask *src2p)
600 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
605 * cpumask_empty - *srcp == 0
606 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
608 static inline bool cpumask_empty(const struct cpumask *srcp)
610 return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
614 * cpumask_full - *srcp == 0xFFFFFFFF...
615 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
617 static inline bool cpumask_full(const struct cpumask *srcp)
619 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
623 * cpumask_weight - Count of bits in *srcp
624 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
626 static inline unsigned int cpumask_weight(const struct cpumask *srcp)
628 return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
632 * cpumask_shift_right - *dstp = *srcp >> n
633 * @dstp: the cpumask result
634 * @srcp: the input to shift
635 * @n: the number of bits to shift by
637 static inline void cpumask_shift_right(struct cpumask *dstp,
638 const struct cpumask *srcp, int n)
640 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
645 * cpumask_shift_left - *dstp = *srcp << n
646 * @dstp: the cpumask result
647 * @srcp: the input to shift
648 * @n: the number of bits to shift by
650 static inline void cpumask_shift_left(struct cpumask *dstp,
651 const struct cpumask *srcp, int n)
653 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
658 * cpumask_copy - *dstp = *srcp
660 * @srcp: the input cpumask
662 static inline void cpumask_copy(struct cpumask *dstp,
663 const struct cpumask *srcp)
665 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
669 * cpumask_any - pick a "random" cpu from *srcp
670 * @srcp: the input cpumask
672 * Returns >= nr_cpu_ids if no cpus set.
674 #define cpumask_any(srcp) cpumask_first(srcp)
677 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
678 * @mask1: the first input cpumask
679 * @mask2: the second input cpumask
681 * Returns >= nr_cpu_ids if no cpus set.
683 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
686 * cpumask_of - the cpumask containing just a given cpu
687 * @cpu: the cpu (<= nr_cpu_ids)
689 #define cpumask_of(cpu) (get_cpu_mask(cpu))
692 * cpumask_parse_user - extract a cpumask from a user string
693 * @buf: the buffer to extract from
694 * @len: the length of the buffer
695 * @dstp: the cpumask to set.
697 * Returns -errno, or 0 for success.
699 static inline int cpumask_parse_user(const char __user *buf, int len,
700 struct cpumask *dstp)
702 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
706 * cpumask_parselist_user - extract a cpumask from a user string
707 * @buf: the buffer to extract from
708 * @len: the length of the buffer
709 * @dstp: the cpumask to set.
711 * Returns -errno, or 0 for success.
713 static inline int cpumask_parselist_user(const char __user *buf, int len,
714 struct cpumask *dstp)
716 return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
721 * cpumask_parse - extract a cpumask from a string
722 * @buf: the buffer to extract from
723 * @dstp: the cpumask to set.
725 * Returns -errno, or 0 for success.
727 static inline int cpumask_parse(const char *buf, struct cpumask *dstp)
729 return bitmap_parse(buf, UINT_MAX, cpumask_bits(dstp), nr_cpumask_bits);
733 * cpulist_parse - extract a cpumask from a user string of ranges
734 * @buf: the buffer to extract from
735 * @dstp: the cpumask to set.
737 * Returns -errno, or 0 for success.
739 static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
741 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
745 * cpumask_size - size to allocate for a 'struct cpumask' in bytes
747 static inline unsigned int cpumask_size(void)
749 return BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long);
753 * cpumask_var_t: struct cpumask for stack usage.
755 * Oh, the wicked games we play! In order to make kernel coding a
756 * little more difficult, we typedef cpumask_var_t to an array or a
757 * pointer: doing &mask on an array is a noop, so it still works.
760 * cpumask_var_t tmpmask;
761 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
764 * ... use 'tmpmask' like a normal struct cpumask * ...
766 * free_cpumask_var(tmpmask);
769 * However, one notable exception is there. alloc_cpumask_var() allocates
770 * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has
771 * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t.
773 * cpumask_var_t tmpmask;
774 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
779 * This code makes NR_CPUS length memcopy and brings to a memory corruption.
780 * cpumask_copy() provide safe copy functionality.
782 * Note that there is another evil here: If you define a cpumask_var_t
783 * as a percpu variable then the way to obtain the address of the cpumask
784 * structure differently influences what this_cpu_* operation needs to be
785 * used. Please use this_cpu_cpumask_var_t in those cases. The direct use
786 * of this_cpu_ptr() or this_cpu_read() will lead to failures when the
787 * other type of cpumask_var_t implementation is configured.
789 * Please also note that __cpumask_var_read_mostly can be used to declare
790 * a cpumask_var_t variable itself (not its content) as read mostly.
792 #ifdef CONFIG_CPUMASK_OFFSTACK
793 typedef struct cpumask *cpumask_var_t;
795 #define this_cpu_cpumask_var_ptr(x) this_cpu_read(x)
796 #define __cpumask_var_read_mostly __read_mostly
798 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
801 bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
803 return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node);
807 * alloc_cpumask_var - allocate a struct cpumask
808 * @mask: pointer to cpumask_var_t where the cpumask is returned
811 * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
812 * a nop returning a constant 1 (in <linux/cpumask.h>).
814 * See alloc_cpumask_var_node.
817 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
819 return alloc_cpumask_var_node(mask, flags, NUMA_NO_NODE);
823 bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
825 return alloc_cpumask_var(mask, flags | __GFP_ZERO);
828 void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
829 void free_cpumask_var(cpumask_var_t mask);
830 void free_bootmem_cpumask_var(cpumask_var_t mask);
832 static inline bool cpumask_available(cpumask_var_t mask)
838 typedef struct cpumask cpumask_var_t[1];
840 #define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x)
841 #define __cpumask_var_read_mostly
843 static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
848 static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
854 static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
856 cpumask_clear(*mask);
860 static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
863 cpumask_clear(*mask);
867 static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
871 static inline void free_cpumask_var(cpumask_var_t mask)
875 static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
879 static inline bool cpumask_available(cpumask_var_t mask)
883 #endif /* CONFIG_CPUMASK_OFFSTACK */
885 /* It's common to want to use cpu_all_mask in struct member initializers,
886 * so it has to refer to an address rather than a pointer. */
887 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
888 #define cpu_all_mask to_cpumask(cpu_all_bits)
890 /* First bits of cpu_bit_bitmap are in fact unset. */
891 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
893 #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
894 #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
895 #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
897 /* Wrappers for arch boot code to manipulate normally-constant masks */
898 void init_cpu_present(const struct cpumask *src);
899 void init_cpu_possible(const struct cpumask *src);
900 void init_cpu_online(const struct cpumask *src);
902 static inline void reset_cpu_possible_mask(void)
904 bitmap_zero(cpumask_bits(&__cpu_possible_mask), NR_CPUS);
908 set_cpu_possible(unsigned int cpu, bool possible)
911 cpumask_set_cpu(cpu, &__cpu_possible_mask);
913 cpumask_clear_cpu(cpu, &__cpu_possible_mask);
917 set_cpu_present(unsigned int cpu, bool present)
920 cpumask_set_cpu(cpu, &__cpu_present_mask);
922 cpumask_clear_cpu(cpu, &__cpu_present_mask);
925 void set_cpu_online(unsigned int cpu, bool online);
928 set_cpu_active(unsigned int cpu, bool active)
931 cpumask_set_cpu(cpu, &__cpu_active_mask);
933 cpumask_clear_cpu(cpu, &__cpu_active_mask);
937 set_cpu_dying(unsigned int cpu, bool dying)
940 cpumask_set_cpu(cpu, &__cpu_dying_mask);
942 cpumask_clear_cpu(cpu, &__cpu_dying_mask);
946 * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
947 * @bitmap: the bitmap
949 * There are a few places where cpumask_var_t isn't appropriate and
950 * static cpumasks must be used (eg. very early boot), yet we don't
951 * expose the definition of 'struct cpumask'.
953 * This does the conversion, and can be used as a constant initializer.
955 #define to_cpumask(bitmap) \
956 ((struct cpumask *)(1 ? (bitmap) \
957 : (void *)sizeof(__check_is_bitmap(bitmap))))
959 static inline int __check_is_bitmap(const unsigned long *bitmap)
965 * Special-case data structure for "single bit set only" constant CPU masks.
967 * We pre-generate all the 64 (or 32) possible bit positions, with enough
968 * padding to the left and the right, and return the constant pointer
969 * appropriately offset.
971 extern const unsigned long
972 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
974 static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
976 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
977 p -= cpu / BITS_PER_LONG;
978 return to_cpumask(p);
983 * num_online_cpus() - Read the number of online CPUs
985 * Despite the fact that __num_online_cpus is of type atomic_t, this
986 * interface gives only a momentary snapshot and is not protected against
987 * concurrent CPU hotplug operations unless invoked from a cpuhp_lock held
990 static inline unsigned int num_online_cpus(void)
992 return atomic_read(&__num_online_cpus);
994 #define num_possible_cpus() cpumask_weight(cpu_possible_mask)
995 #define num_present_cpus() cpumask_weight(cpu_present_mask)
996 #define num_active_cpus() cpumask_weight(cpu_active_mask)
998 static inline bool cpu_online(unsigned int cpu)
1000 return cpumask_test_cpu(cpu, cpu_online_mask);
1003 static inline bool cpu_possible(unsigned int cpu)
1005 return cpumask_test_cpu(cpu, cpu_possible_mask);
1008 static inline bool cpu_present(unsigned int cpu)
1010 return cpumask_test_cpu(cpu, cpu_present_mask);
1013 static inline bool cpu_active(unsigned int cpu)
1015 return cpumask_test_cpu(cpu, cpu_active_mask);
1018 static inline bool cpu_dying(unsigned int cpu)
1020 return cpumask_test_cpu(cpu, cpu_dying_mask);
1025 #define num_online_cpus() 1U
1026 #define num_possible_cpus() 1U
1027 #define num_present_cpus() 1U
1028 #define num_active_cpus() 1U
1030 static inline bool cpu_online(unsigned int cpu)
1035 static inline bool cpu_possible(unsigned int cpu)
1040 static inline bool cpu_present(unsigned int cpu)
1045 static inline bool cpu_active(unsigned int cpu)
1050 static inline bool cpu_dying(unsigned int cpu)
1055 #endif /* NR_CPUS > 1 */
1057 #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
1059 #if NR_CPUS <= BITS_PER_LONG
1060 #define CPU_BITS_ALL \
1062 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1065 #else /* NR_CPUS > BITS_PER_LONG */
1067 #define CPU_BITS_ALL \
1069 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
1070 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1072 #endif /* NR_CPUS > BITS_PER_LONG */
1075 * cpumap_print_to_pagebuf - copies the cpumask into the buffer either
1076 * as comma-separated list of cpus or hex values of cpumask
1077 * @list: indicates whether the cpumap must be list
1078 * @mask: the cpumask to copy
1079 * @buf: the buffer to copy into
1081 * Returns the length of the (null-terminated) @buf string, zero if
1082 * nothing is copied.
1084 static inline ssize_t
1085 cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask)
1087 return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask),
1092 * cpumap_print_bitmask_to_buf - copies the cpumask into the buffer as
1093 * hex values of cpumask
1095 * @buf: the buffer to copy into
1096 * @mask: the cpumask to copy
1097 * @off: in the string from which we are copying, we copy to @buf
1098 * @count: the maximum number of bytes to print
1100 * The function prints the cpumask into the buffer as hex values of
1101 * cpumask; Typically used by bin_attribute to export cpumask bitmask
1104 * Returns the length of how many bytes have been copied, excluding
1107 static inline ssize_t
1108 cpumap_print_bitmask_to_buf(char *buf, const struct cpumask *mask,
1109 loff_t off, size_t count)
1111 return bitmap_print_bitmask_to_buf(buf, cpumask_bits(mask),
1112 nr_cpu_ids, off, count) - 1;
1116 * cpumap_print_list_to_buf - copies the cpumask into the buffer as
1117 * comma-separated list of cpus
1119 * Everything is same with the above cpumap_print_bitmask_to_buf()
1120 * except the print format.
1122 static inline ssize_t
1123 cpumap_print_list_to_buf(char *buf, const struct cpumask *mask,
1124 loff_t off, size_t count)
1126 return bitmap_print_list_to_buf(buf, cpumask_bits(mask),
1127 nr_cpu_ids, off, count) - 1;
1130 #if NR_CPUS <= BITS_PER_LONG
1131 #define CPU_MASK_ALL \
1133 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1136 #define CPU_MASK_ALL \
1138 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
1139 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
1141 #endif /* NR_CPUS > BITS_PER_LONG */
1143 #define CPU_MASK_NONE \
1145 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
1148 #define CPU_MASK_CPU0 \
1153 #endif /* __LINUX_CPUMASK_H */