xref: /linux-6.15/include/linux/cpumask.h (revision b81a6774)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_CPUMASK_H
3 #define __LINUX_CPUMASK_H
4 
5 /*
6  * Cpumasks provide a bitmap suitable for representing the
7  * set of CPU's in a system, one bit position per CPU number.  In general,
8  * only nr_cpu_ids (<= NR_CPUS) bits are valid.
9  */
10 #include <linux/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>
17 
18 /* Don't assign or return these: may not be this big! */
19 typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
20 
21 /**
22  * cpumask_bits - get the bits in a cpumask
23  * @maskp: the struct cpumask *
24  *
25  * You should only assume nr_cpu_ids bits of this mask are valid.  This is
26  * a macro so it's const-correct.
27  */
28 #define cpumask_bits(maskp) ((maskp)->bits)
29 
30 /**
31  * cpumask_pr_args - printf args to output a cpumask
32  * @maskp: cpumask to be printed
33  *
34  * Can be used to provide arguments for '%*pb[l]' when printing a cpumask.
35  */
36 #define cpumask_pr_args(maskp)		nr_cpu_ids, cpumask_bits(maskp)
37 
38 #if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS)
39 #define nr_cpu_ids ((unsigned int)NR_CPUS)
40 #else
41 extern unsigned int nr_cpu_ids;
42 #endif
43 
44 static inline void set_nr_cpu_ids(unsigned int nr)
45 {
46 #if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS)
47 	WARN_ON(nr != nr_cpu_ids);
48 #else
49 	nr_cpu_ids = nr;
50 #endif
51 }
52 
53 /* Deprecated. Always use nr_cpu_ids. */
54 #define nr_cpumask_bits	nr_cpu_ids
55 
56 /*
57  * The following particular system cpumasks and operations manage
58  * possible, present, active and online cpus.
59  *
60  *     cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
61  *     cpu_present_mask - has bit 'cpu' set iff cpu is populated
62  *     cpu_online_mask  - has bit 'cpu' set iff cpu available to scheduler
63  *     cpu_active_mask  - has bit 'cpu' set iff cpu available to migration
64  *
65  *  If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
66  *
67  *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
68  *  that it is possible might ever be plugged in at anytime during the
69  *  life of that system boot.  The cpu_present_mask is dynamic(*),
70  *  representing which CPUs are currently plugged in.  And
71  *  cpu_online_mask is the dynamic subset of cpu_present_mask,
72  *  indicating those CPUs available for scheduling.
73  *
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.
77  *
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.
80  *
81  * Subtleties:
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
89  *    only one CPU.
90  */
91 
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)
102 
103 extern atomic_t __num_online_cpus;
104 
105 extern cpumask_t cpus_booted_once_mask;
106 
107 static __always_inline void cpu_max_bits_warn(unsigned int cpu, unsigned int bits)
108 {
109 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
110 	WARN_ON_ONCE(cpu >= bits);
111 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
112 }
113 
114 /* verify cpu argument to cpumask_* operators */
115 static __always_inline unsigned int cpumask_check(unsigned int cpu)
116 {
117 	cpu_max_bits_warn(cpu, nr_cpumask_bits);
118 	return cpu;
119 }
120 
121 /**
122  * cpumask_first - get the first cpu in a cpumask
123  * @srcp: the cpumask pointer
124  *
125  * Returns >= nr_cpu_ids if no cpus set.
126  */
127 static inline unsigned int cpumask_first(const struct cpumask *srcp)
128 {
129 	return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
130 }
131 
132 /**
133  * cpumask_first_zero - get the first unset cpu in a cpumask
134  * @srcp: the cpumask pointer
135  *
136  * Returns >= nr_cpu_ids if all cpus are set.
137  */
138 static inline unsigned int cpumask_first_zero(const struct cpumask *srcp)
139 {
140 	return find_first_zero_bit(cpumask_bits(srcp), nr_cpumask_bits);
141 }
142 
143 /**
144  * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
145  * @src1p: the first input
146  * @src2p: the second input
147  *
148  * Returns >= nr_cpu_ids if no cpus set in both.  See also cpumask_next_and().
149  */
150 static inline
151 unsigned int cpumask_first_and(const struct cpumask *srcp1, const struct cpumask *srcp2)
152 {
153 	return find_first_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), nr_cpumask_bits);
154 }
155 
156 /**
157  * cpumask_last - get the last CPU in a cpumask
158  * @srcp:	- the cpumask pointer
159  *
160  * Returns	>= nr_cpumask_bits if no CPUs set.
161  */
162 static inline unsigned int cpumask_last(const struct cpumask *srcp)
163 {
164 	return find_last_bit(cpumask_bits(srcp), nr_cpumask_bits);
165 }
166 
167 /**
168  * cpumask_next - get the next cpu in a cpumask
169  * @n: the cpu prior to the place to search (ie. return will be > @n)
170  * @srcp: the cpumask pointer
171  *
172  * Returns >= nr_cpu_ids if no further cpus set.
173  */
174 static inline
175 unsigned int cpumask_next(int n, const struct cpumask *srcp)
176 {
177 	/* n is a prior cpu */
178 	cpumask_check(n + 1);
179 	return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n + 1);
180 }
181 
182 /**
183  * cpumask_next_zero - get the next unset cpu in a cpumask
184  * @n: the cpu prior to the place to search (ie. return will be > @n)
185  * @srcp: the cpumask pointer
186  *
187  * Returns >= nr_cpu_ids if no further cpus unset.
188  */
189 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
190 {
191 	/* n is a prior cpu */
192 	cpumask_check(n + 1);
193 	return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
194 }
195 
196 #if NR_CPUS == 1
197 /* Uniprocessor: there is only one valid CPU */
198 static inline unsigned int cpumask_local_spread(unsigned int i, int node)
199 {
200 	return 0;
201 }
202 
203 static inline unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
204 						      const struct cpumask *src2p)
205 {
206 	return cpumask_first_and(src1p, src2p);
207 }
208 
209 static inline unsigned int cpumask_any_distribute(const struct cpumask *srcp)
210 {
211 	return cpumask_first(srcp);
212 }
213 #else
214 unsigned int cpumask_local_spread(unsigned int i, int node);
215 unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
216 			       const struct cpumask *src2p);
217 unsigned int cpumask_any_distribute(const struct cpumask *srcp);
218 #endif /* NR_CPUS */
219 
220 /**
221  * cpumask_next_and - get the next cpu in *src1p & *src2p
222  * @n: the cpu prior to the place to search (ie. return will be > @n)
223  * @src1p: the first cpumask pointer
224  * @src2p: the second cpumask pointer
225  *
226  * Returns >= nr_cpu_ids if no further cpus set in both.
227  */
228 static inline
229 unsigned int cpumask_next_and(int n, const struct cpumask *src1p,
230 		     const struct cpumask *src2p)
231 {
232 	/* n is a prior cpu */
233 	cpumask_check(n + 1);
234 	return find_next_and_bit(cpumask_bits(src1p), cpumask_bits(src2p),
235 		nr_cpumask_bits, n + 1);
236 }
237 
238 /**
239  * for_each_cpu - iterate over every cpu in a mask
240  * @cpu: the (optionally unsigned) integer iterator
241  * @mask: the cpumask pointer
242  *
243  * After the loop, cpu is >= nr_cpu_ids.
244  */
245 #define for_each_cpu(cpu, mask)				\
246 	for_each_set_bit(cpu, cpumask_bits(mask), nr_cpumask_bits)
247 
248 /**
249  * for_each_cpu_not - iterate over every cpu in a complemented mask
250  * @cpu: the (optionally unsigned) integer iterator
251  * @mask: the cpumask pointer
252  *
253  * After the loop, cpu is >= nr_cpu_ids.
254  */
255 #define for_each_cpu_not(cpu, mask)				\
256 	for_each_clear_bit(cpu, cpumask_bits(mask), nr_cpumask_bits)
257 
258 #if NR_CPUS == 1
259 static inline
260 unsigned int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap)
261 {
262 	cpumask_check(start);
263 	/* n is a prior cpu */
264 	cpumask_check(n + 1);
265 
266 	/*
267 	 * Return the first available CPU when wrapping, or when starting before cpu0,
268 	 * since there is only one valid option.
269 	 */
270 	if (wrap && n >= 0)
271 		return nr_cpumask_bits;
272 
273 	return cpumask_first(mask);
274 }
275 #else
276 unsigned int __pure cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap);
277 #endif
278 
279 /**
280  * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a specified location
281  * @cpu: the (optionally unsigned) integer iterator
282  * @mask: the cpumask pointer
283  * @start: the start location
284  *
285  * The implementation does not assume any bit in @mask is set (including @start).
286  *
287  * After the loop, cpu is >= nr_cpu_ids.
288  */
289 #define for_each_cpu_wrap(cpu, mask, start)				\
290 	for_each_set_bit_wrap(cpu, cpumask_bits(mask), nr_cpumask_bits, start)
291 
292 /**
293  * for_each_cpu_and - iterate over every cpu in both masks
294  * @cpu: the (optionally unsigned) integer iterator
295  * @mask1: the first cpumask pointer
296  * @mask2: the second cpumask pointer
297  *
298  * This saves a temporary CPU mask in many places.  It is equivalent to:
299  *	struct cpumask tmp;
300  *	cpumask_and(&tmp, &mask1, &mask2);
301  *	for_each_cpu(cpu, &tmp)
302  *		...
303  *
304  * After the loop, cpu is >= nr_cpu_ids.
305  */
306 #define for_each_cpu_and(cpu, mask1, mask2)				\
307 	for_each_and_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), nr_cpumask_bits)
308 
309 /**
310  * for_each_cpu_andnot - iterate over every cpu present in one mask, excluding
311  *			 those present in another.
312  * @cpu: the (optionally unsigned) integer iterator
313  * @mask1: the first cpumask pointer
314  * @mask2: the second cpumask pointer
315  *
316  * This saves a temporary CPU mask in many places.  It is equivalent to:
317  *	struct cpumask tmp;
318  *	cpumask_andnot(&tmp, &mask1, &mask2);
319  *	for_each_cpu(cpu, &tmp)
320  *		...
321  *
322  * After the loop, cpu is >= nr_cpu_ids.
323  */
324 #define for_each_cpu_andnot(cpu, mask1, mask2)				\
325 	for_each_andnot_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), nr_cpumask_bits)
326 
327 /**
328  * cpumask_any_but - return a "random" in a cpumask, but not this one.
329  * @mask: the cpumask to search
330  * @cpu: the cpu to ignore.
331  *
332  * Often used to find any cpu but smp_processor_id() in a mask.
333  * Returns >= nr_cpu_ids if no cpus set.
334  */
335 static inline
336 unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
337 {
338 	unsigned int i;
339 
340 	cpumask_check(cpu);
341 	for_each_cpu(i, mask)
342 		if (i != cpu)
343 			break;
344 	return i;
345 }
346 
347 /**
348  * cpumask_nth - get the first cpu in a cpumask
349  * @srcp: the cpumask pointer
350  * @cpu: the N'th cpu to find, starting from 0
351  *
352  * Returns >= nr_cpu_ids if such cpu doesn't exist.
353  */
354 static inline unsigned int cpumask_nth(unsigned int cpu, const struct cpumask *srcp)
355 {
356 	return find_nth_bit(cpumask_bits(srcp), nr_cpumask_bits, cpumask_check(cpu));
357 }
358 
359 /**
360  * cpumask_nth_and - get the first cpu in 2 cpumasks
361  * @srcp1: the cpumask pointer
362  * @srcp2: the cpumask pointer
363  * @cpu: the N'th cpu to find, starting from 0
364  *
365  * Returns >= nr_cpu_ids if such cpu doesn't exist.
366  */
367 static inline
368 unsigned int cpumask_nth_and(unsigned int cpu, const struct cpumask *srcp1,
369 							const struct cpumask *srcp2)
370 {
371 	return find_nth_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
372 				nr_cpumask_bits, cpumask_check(cpu));
373 }
374 
375 /**
376  * cpumask_nth_andnot - get the first cpu set in 1st cpumask, and clear in 2nd.
377  * @srcp1: the cpumask pointer
378  * @srcp2: the cpumask pointer
379  * @cpu: the N'th cpu to find, starting from 0
380  *
381  * Returns >= nr_cpu_ids if such cpu doesn't exist.
382  */
383 static inline
384 unsigned int cpumask_nth_andnot(unsigned int cpu, const struct cpumask *srcp1,
385 							const struct cpumask *srcp2)
386 {
387 	return find_nth_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
388 				nr_cpumask_bits, cpumask_check(cpu));
389 }
390 
391 #define CPU_BITS_NONE						\
392 {								\
393 	[0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL			\
394 }
395 
396 #define CPU_BITS_CPU0						\
397 {								\
398 	[0] =  1UL						\
399 }
400 
401 /**
402  * cpumask_set_cpu - set a cpu in a cpumask
403  * @cpu: cpu number (< nr_cpu_ids)
404  * @dstp: the cpumask pointer
405  */
406 static __always_inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
407 {
408 	set_bit(cpumask_check(cpu), cpumask_bits(dstp));
409 }
410 
411 static __always_inline void __cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
412 {
413 	__set_bit(cpumask_check(cpu), cpumask_bits(dstp));
414 }
415 
416 
417 /**
418  * cpumask_clear_cpu - clear a cpu in a cpumask
419  * @cpu: cpu number (< nr_cpu_ids)
420  * @dstp: the cpumask pointer
421  */
422 static __always_inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
423 {
424 	clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
425 }
426 
427 static __always_inline void __cpumask_clear_cpu(int cpu, struct cpumask *dstp)
428 {
429 	__clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
430 }
431 
432 /**
433  * cpumask_test_cpu - test for a cpu in a cpumask
434  * @cpu: cpu number (< nr_cpu_ids)
435  * @cpumask: the cpumask pointer
436  *
437  * Returns true if @cpu is set in @cpumask, else returns false
438  */
439 static __always_inline bool cpumask_test_cpu(int cpu, const struct cpumask *cpumask)
440 {
441 	return test_bit(cpumask_check(cpu), cpumask_bits((cpumask)));
442 }
443 
444 /**
445  * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
446  * @cpu: cpu number (< nr_cpu_ids)
447  * @cpumask: the cpumask pointer
448  *
449  * Returns true if @cpu is set in old bitmap of @cpumask, else returns false
450  *
451  * test_and_set_bit wrapper for cpumasks.
452  */
453 static __always_inline bool cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
454 {
455 	return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
456 }
457 
458 /**
459  * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
460  * @cpu: cpu number (< nr_cpu_ids)
461  * @cpumask: the cpumask pointer
462  *
463  * Returns true if @cpu is set in old bitmap of @cpumask, else returns false
464  *
465  * test_and_clear_bit wrapper for cpumasks.
466  */
467 static __always_inline bool cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
468 {
469 	return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
470 }
471 
472 /**
473  * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
474  * @dstp: the cpumask pointer
475  */
476 static inline void cpumask_setall(struct cpumask *dstp)
477 {
478 	bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
479 }
480 
481 /**
482  * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
483  * @dstp: the cpumask pointer
484  */
485 static inline void cpumask_clear(struct cpumask *dstp)
486 {
487 	bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
488 }
489 
490 /**
491  * cpumask_and - *dstp = *src1p & *src2p
492  * @dstp: the cpumask result
493  * @src1p: the first input
494  * @src2p: the second input
495  *
496  * If *@dstp is empty, returns false, else returns true
497  */
498 static inline bool cpumask_and(struct cpumask *dstp,
499 			       const struct cpumask *src1p,
500 			       const struct cpumask *src2p)
501 {
502 	return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
503 				       cpumask_bits(src2p), nr_cpumask_bits);
504 }
505 
506 /**
507  * cpumask_or - *dstp = *src1p | *src2p
508  * @dstp: the cpumask result
509  * @src1p: the first input
510  * @src2p: the second input
511  */
512 static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
513 			      const struct cpumask *src2p)
514 {
515 	bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
516 				      cpumask_bits(src2p), nr_cpumask_bits);
517 }
518 
519 /**
520  * cpumask_xor - *dstp = *src1p ^ *src2p
521  * @dstp: the cpumask result
522  * @src1p: the first input
523  * @src2p: the second input
524  */
525 static inline void cpumask_xor(struct cpumask *dstp,
526 			       const struct cpumask *src1p,
527 			       const struct cpumask *src2p)
528 {
529 	bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
530 				       cpumask_bits(src2p), nr_cpumask_bits);
531 }
532 
533 /**
534  * cpumask_andnot - *dstp = *src1p & ~*src2p
535  * @dstp: the cpumask result
536  * @src1p: the first input
537  * @src2p: the second input
538  *
539  * If *@dstp is empty, returns false, else returns true
540  */
541 static inline bool cpumask_andnot(struct cpumask *dstp,
542 				  const struct cpumask *src1p,
543 				  const struct cpumask *src2p)
544 {
545 	return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
546 					  cpumask_bits(src2p), nr_cpumask_bits);
547 }
548 
549 /**
550  * cpumask_complement - *dstp = ~*srcp
551  * @dstp: the cpumask result
552  * @srcp: the input to invert
553  */
554 static inline void cpumask_complement(struct cpumask *dstp,
555 				      const struct cpumask *srcp)
556 {
557 	bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
558 					      nr_cpumask_bits);
559 }
560 
561 /**
562  * cpumask_equal - *src1p == *src2p
563  * @src1p: the first input
564  * @src2p: the second input
565  */
566 static inline bool cpumask_equal(const struct cpumask *src1p,
567 				const struct cpumask *src2p)
568 {
569 	return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
570 						 nr_cpumask_bits);
571 }
572 
573 /**
574  * cpumask_or_equal - *src1p | *src2p == *src3p
575  * @src1p: the first input
576  * @src2p: the second input
577  * @src3p: the third input
578  */
579 static inline bool cpumask_or_equal(const struct cpumask *src1p,
580 				    const struct cpumask *src2p,
581 				    const struct cpumask *src3p)
582 {
583 	return bitmap_or_equal(cpumask_bits(src1p), cpumask_bits(src2p),
584 			       cpumask_bits(src3p), nr_cpumask_bits);
585 }
586 
587 /**
588  * cpumask_intersects - (*src1p & *src2p) != 0
589  * @src1p: the first input
590  * @src2p: the second input
591  */
592 static inline bool cpumask_intersects(const struct cpumask *src1p,
593 				     const struct cpumask *src2p)
594 {
595 	return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
596 						      nr_cpumask_bits);
597 }
598 
599 /**
600  * cpumask_subset - (*src1p & ~*src2p) == 0
601  * @src1p: the first input
602  * @src2p: the second input
603  *
604  * Returns true if *@src1p is a subset of *@src2p, else returns false
605  */
606 static inline bool cpumask_subset(const struct cpumask *src1p,
607 				 const struct cpumask *src2p)
608 {
609 	return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
610 						  nr_cpumask_bits);
611 }
612 
613 /**
614  * cpumask_empty - *srcp == 0
615  * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
616  */
617 static inline bool cpumask_empty(const struct cpumask *srcp)
618 {
619 	return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
620 }
621 
622 /**
623  * cpumask_full - *srcp == 0xFFFFFFFF...
624  * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
625  */
626 static inline bool cpumask_full(const struct cpumask *srcp)
627 {
628 	return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
629 }
630 
631 /**
632  * cpumask_weight - Count of bits in *srcp
633  * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
634  */
635 static inline unsigned int cpumask_weight(const struct cpumask *srcp)
636 {
637 	return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
638 }
639 
640 /**
641  * cpumask_weight_and - Count of bits in (*srcp1 & *srcp2)
642  * @srcp1: the cpumask to count bits (< nr_cpu_ids) in.
643  * @srcp2: the cpumask to count bits (< nr_cpu_ids) in.
644  */
645 static inline unsigned int cpumask_weight_and(const struct cpumask *srcp1,
646 						const struct cpumask *srcp2)
647 {
648 	return bitmap_weight_and(cpumask_bits(srcp1), cpumask_bits(srcp2), nr_cpumask_bits);
649 }
650 
651 /**
652  * cpumask_shift_right - *dstp = *srcp >> n
653  * @dstp: the cpumask result
654  * @srcp: the input to shift
655  * @n: the number of bits to shift by
656  */
657 static inline void cpumask_shift_right(struct cpumask *dstp,
658 				       const struct cpumask *srcp, int n)
659 {
660 	bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
661 					       nr_cpumask_bits);
662 }
663 
664 /**
665  * cpumask_shift_left - *dstp = *srcp << n
666  * @dstp: the cpumask result
667  * @srcp: the input to shift
668  * @n: the number of bits to shift by
669  */
670 static inline void cpumask_shift_left(struct cpumask *dstp,
671 				      const struct cpumask *srcp, int n)
672 {
673 	bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
674 					      nr_cpumask_bits);
675 }
676 
677 /**
678  * cpumask_copy - *dstp = *srcp
679  * @dstp: the result
680  * @srcp: the input cpumask
681  */
682 static inline void cpumask_copy(struct cpumask *dstp,
683 				const struct cpumask *srcp)
684 {
685 	bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
686 }
687 
688 /**
689  * cpumask_any - pick a "random" cpu from *srcp
690  * @srcp: the input cpumask
691  *
692  * Returns >= nr_cpu_ids if no cpus set.
693  */
694 #define cpumask_any(srcp) cpumask_first(srcp)
695 
696 /**
697  * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
698  * @mask1: the first input cpumask
699  * @mask2: the second input cpumask
700  *
701  * Returns >= nr_cpu_ids if no cpus set.
702  */
703 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
704 
705 /**
706  * cpumask_of - the cpumask containing just a given cpu
707  * @cpu: the cpu (<= nr_cpu_ids)
708  */
709 #define cpumask_of(cpu) (get_cpu_mask(cpu))
710 
711 /**
712  * cpumask_parse_user - extract a cpumask from a user string
713  * @buf: the buffer to extract from
714  * @len: the length of the buffer
715  * @dstp: the cpumask to set.
716  *
717  * Returns -errno, or 0 for success.
718  */
719 static inline int cpumask_parse_user(const char __user *buf, int len,
720 				     struct cpumask *dstp)
721 {
722 	return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
723 }
724 
725 /**
726  * cpumask_parselist_user - extract a cpumask from a user string
727  * @buf: the buffer to extract from
728  * @len: the length of the buffer
729  * @dstp: the cpumask to set.
730  *
731  * Returns -errno, or 0 for success.
732  */
733 static inline int cpumask_parselist_user(const char __user *buf, int len,
734 				     struct cpumask *dstp)
735 {
736 	return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
737 				     nr_cpumask_bits);
738 }
739 
740 /**
741  * cpumask_parse - extract a cpumask from a string
742  * @buf: the buffer to extract from
743  * @dstp: the cpumask to set.
744  *
745  * Returns -errno, or 0 for success.
746  */
747 static inline int cpumask_parse(const char *buf, struct cpumask *dstp)
748 {
749 	return bitmap_parse(buf, UINT_MAX, cpumask_bits(dstp), nr_cpumask_bits);
750 }
751 
752 /**
753  * cpulist_parse - extract a cpumask from a user string of ranges
754  * @buf: the buffer to extract from
755  * @dstp: the cpumask to set.
756  *
757  * Returns -errno, or 0 for success.
758  */
759 static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
760 {
761 	return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
762 }
763 
764 /**
765  * cpumask_size - size to allocate for a 'struct cpumask' in bytes
766  */
767 static inline unsigned int cpumask_size(void)
768 {
769 	return BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long);
770 }
771 
772 /*
773  * cpumask_var_t: struct cpumask for stack usage.
774  *
775  * Oh, the wicked games we play!  In order to make kernel coding a
776  * little more difficult, we typedef cpumask_var_t to an array or a
777  * pointer: doing &mask on an array is a noop, so it still works.
778  *
779  * ie.
780  *	cpumask_var_t tmpmask;
781  *	if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
782  *		return -ENOMEM;
783  *
784  *	  ... use 'tmpmask' like a normal struct cpumask * ...
785  *
786  *	free_cpumask_var(tmpmask);
787  *
788  *
789  * However, one notable exception is there. alloc_cpumask_var() allocates
790  * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has
791  * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t.
792  *
793  *	cpumask_var_t tmpmask;
794  *	if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
795  *		return -ENOMEM;
796  *
797  *	var = *tmpmask;
798  *
799  * This code makes NR_CPUS length memcopy and brings to a memory corruption.
800  * cpumask_copy() provide safe copy functionality.
801  *
802  * Note that there is another evil here: If you define a cpumask_var_t
803  * as a percpu variable then the way to obtain the address of the cpumask
804  * structure differently influences what this_cpu_* operation needs to be
805  * used. Please use this_cpu_cpumask_var_t in those cases. The direct use
806  * of this_cpu_ptr() or this_cpu_read() will lead to failures when the
807  * other type of cpumask_var_t implementation is configured.
808  *
809  * Please also note that __cpumask_var_read_mostly can be used to declare
810  * a cpumask_var_t variable itself (not its content) as read mostly.
811  */
812 #ifdef CONFIG_CPUMASK_OFFSTACK
813 typedef struct cpumask *cpumask_var_t;
814 
815 #define this_cpu_cpumask_var_ptr(x)	this_cpu_read(x)
816 #define __cpumask_var_read_mostly	__read_mostly
817 
818 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
819 
820 static inline
821 bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
822 {
823 	return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node);
824 }
825 
826 /**
827  * alloc_cpumask_var - allocate a struct cpumask
828  * @mask: pointer to cpumask_var_t where the cpumask is returned
829  * @flags: GFP_ flags
830  *
831  * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
832  * a nop returning a constant 1 (in <linux/cpumask.h>).
833  *
834  * See alloc_cpumask_var_node.
835  */
836 static inline
837 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
838 {
839 	return alloc_cpumask_var_node(mask, flags, NUMA_NO_NODE);
840 }
841 
842 static inline
843 bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
844 {
845 	return alloc_cpumask_var(mask, flags | __GFP_ZERO);
846 }
847 
848 void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
849 void free_cpumask_var(cpumask_var_t mask);
850 void free_bootmem_cpumask_var(cpumask_var_t mask);
851 
852 static inline bool cpumask_available(cpumask_var_t mask)
853 {
854 	return mask != NULL;
855 }
856 
857 #else
858 typedef struct cpumask cpumask_var_t[1];
859 
860 #define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x)
861 #define __cpumask_var_read_mostly
862 
863 static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
864 {
865 	return true;
866 }
867 
868 static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
869 					  int node)
870 {
871 	return true;
872 }
873 
874 static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
875 {
876 	cpumask_clear(*mask);
877 	return true;
878 }
879 
880 static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
881 					  int node)
882 {
883 	cpumask_clear(*mask);
884 	return true;
885 }
886 
887 static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
888 {
889 }
890 
891 static inline void free_cpumask_var(cpumask_var_t mask)
892 {
893 }
894 
895 static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
896 {
897 }
898 
899 static inline bool cpumask_available(cpumask_var_t mask)
900 {
901 	return true;
902 }
903 #endif /* CONFIG_CPUMASK_OFFSTACK */
904 
905 /* It's common to want to use cpu_all_mask in struct member initializers,
906  * so it has to refer to an address rather than a pointer. */
907 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
908 #define cpu_all_mask to_cpumask(cpu_all_bits)
909 
910 /* First bits of cpu_bit_bitmap are in fact unset. */
911 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
912 
913 #if NR_CPUS == 1
914 /* Uniprocessor: the possible/online/present masks are always "1" */
915 #define for_each_possible_cpu(cpu)	for ((cpu) = 0; (cpu) < 1; (cpu)++)
916 #define for_each_online_cpu(cpu)	for ((cpu) = 0; (cpu) < 1; (cpu)++)
917 #define for_each_present_cpu(cpu)	for ((cpu) = 0; (cpu) < 1; (cpu)++)
918 #else
919 #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
920 #define for_each_online_cpu(cpu)   for_each_cpu((cpu), cpu_online_mask)
921 #define for_each_present_cpu(cpu)  for_each_cpu((cpu), cpu_present_mask)
922 #endif
923 
924 /* Wrappers for arch boot code to manipulate normally-constant masks */
925 void init_cpu_present(const struct cpumask *src);
926 void init_cpu_possible(const struct cpumask *src);
927 void init_cpu_online(const struct cpumask *src);
928 
929 static inline void reset_cpu_possible_mask(void)
930 {
931 	bitmap_zero(cpumask_bits(&__cpu_possible_mask), NR_CPUS);
932 }
933 
934 static inline void
935 set_cpu_possible(unsigned int cpu, bool possible)
936 {
937 	if (possible)
938 		cpumask_set_cpu(cpu, &__cpu_possible_mask);
939 	else
940 		cpumask_clear_cpu(cpu, &__cpu_possible_mask);
941 }
942 
943 static inline void
944 set_cpu_present(unsigned int cpu, bool present)
945 {
946 	if (present)
947 		cpumask_set_cpu(cpu, &__cpu_present_mask);
948 	else
949 		cpumask_clear_cpu(cpu, &__cpu_present_mask);
950 }
951 
952 void set_cpu_online(unsigned int cpu, bool online);
953 
954 static inline void
955 set_cpu_active(unsigned int cpu, bool active)
956 {
957 	if (active)
958 		cpumask_set_cpu(cpu, &__cpu_active_mask);
959 	else
960 		cpumask_clear_cpu(cpu, &__cpu_active_mask);
961 }
962 
963 static inline void
964 set_cpu_dying(unsigned int cpu, bool dying)
965 {
966 	if (dying)
967 		cpumask_set_cpu(cpu, &__cpu_dying_mask);
968 	else
969 		cpumask_clear_cpu(cpu, &__cpu_dying_mask);
970 }
971 
972 /**
973  * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
974  * @bitmap: the bitmap
975  *
976  * There are a few places where cpumask_var_t isn't appropriate and
977  * static cpumasks must be used (eg. very early boot), yet we don't
978  * expose the definition of 'struct cpumask'.
979  *
980  * This does the conversion, and can be used as a constant initializer.
981  */
982 #define to_cpumask(bitmap)						\
983 	((struct cpumask *)(1 ? (bitmap)				\
984 			    : (void *)sizeof(__check_is_bitmap(bitmap))))
985 
986 static inline int __check_is_bitmap(const unsigned long *bitmap)
987 {
988 	return 1;
989 }
990 
991 /*
992  * Special-case data structure for "single bit set only" constant CPU masks.
993  *
994  * We pre-generate all the 64 (or 32) possible bit positions, with enough
995  * padding to the left and the right, and return the constant pointer
996  * appropriately offset.
997  */
998 extern const unsigned long
999 	cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
1000 
1001 static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
1002 {
1003 	const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
1004 	p -= cpu / BITS_PER_LONG;
1005 	return to_cpumask(p);
1006 }
1007 
1008 #if NR_CPUS > 1
1009 /**
1010  * num_online_cpus() - Read the number of online CPUs
1011  *
1012  * Despite the fact that __num_online_cpus is of type atomic_t, this
1013  * interface gives only a momentary snapshot and is not protected against
1014  * concurrent CPU hotplug operations unless invoked from a cpuhp_lock held
1015  * region.
1016  */
1017 static inline unsigned int num_online_cpus(void)
1018 {
1019 	return atomic_read(&__num_online_cpus);
1020 }
1021 #define num_possible_cpus()	cpumask_weight(cpu_possible_mask)
1022 #define num_present_cpus()	cpumask_weight(cpu_present_mask)
1023 #define num_active_cpus()	cpumask_weight(cpu_active_mask)
1024 
1025 static inline bool cpu_online(unsigned int cpu)
1026 {
1027 	return cpumask_test_cpu(cpu, cpu_online_mask);
1028 }
1029 
1030 static inline bool cpu_possible(unsigned int cpu)
1031 {
1032 	return cpumask_test_cpu(cpu, cpu_possible_mask);
1033 }
1034 
1035 static inline bool cpu_present(unsigned int cpu)
1036 {
1037 	return cpumask_test_cpu(cpu, cpu_present_mask);
1038 }
1039 
1040 static inline bool cpu_active(unsigned int cpu)
1041 {
1042 	return cpumask_test_cpu(cpu, cpu_active_mask);
1043 }
1044 
1045 static inline bool cpu_dying(unsigned int cpu)
1046 {
1047 	return cpumask_test_cpu(cpu, cpu_dying_mask);
1048 }
1049 
1050 #else
1051 
1052 #define num_online_cpus()	1U
1053 #define num_possible_cpus()	1U
1054 #define num_present_cpus()	1U
1055 #define num_active_cpus()	1U
1056 
1057 static inline bool cpu_online(unsigned int cpu)
1058 {
1059 	return cpu == 0;
1060 }
1061 
1062 static inline bool cpu_possible(unsigned int cpu)
1063 {
1064 	return cpu == 0;
1065 }
1066 
1067 static inline bool cpu_present(unsigned int cpu)
1068 {
1069 	return cpu == 0;
1070 }
1071 
1072 static inline bool cpu_active(unsigned int cpu)
1073 {
1074 	return cpu == 0;
1075 }
1076 
1077 static inline bool cpu_dying(unsigned int cpu)
1078 {
1079 	return false;
1080 }
1081 
1082 #endif /* NR_CPUS > 1 */
1083 
1084 #define cpu_is_offline(cpu)	unlikely(!cpu_online(cpu))
1085 
1086 #if NR_CPUS <= BITS_PER_LONG
1087 #define CPU_BITS_ALL						\
1088 {								\
1089 	[BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS)	\
1090 }
1091 
1092 #else /* NR_CPUS > BITS_PER_LONG */
1093 
1094 #define CPU_BITS_ALL						\
1095 {								\
1096 	[0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,		\
1097 	[BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS)	\
1098 }
1099 #endif /* NR_CPUS > BITS_PER_LONG */
1100 
1101 /**
1102  * cpumap_print_to_pagebuf  - copies the cpumask into the buffer either
1103  *	as comma-separated list of cpus or hex values of cpumask
1104  * @list: indicates whether the cpumap must be list
1105  * @mask: the cpumask to copy
1106  * @buf: the buffer to copy into
1107  *
1108  * Returns the length of the (null-terminated) @buf string, zero if
1109  * nothing is copied.
1110  */
1111 static inline ssize_t
1112 cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask)
1113 {
1114 	return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask),
1115 				      nr_cpu_ids);
1116 }
1117 
1118 /**
1119  * cpumap_print_bitmask_to_buf  - copies the cpumask into the buffer as
1120  *	hex values of cpumask
1121  *
1122  * @buf: the buffer to copy into
1123  * @mask: the cpumask to copy
1124  * @off: in the string from which we are copying, we copy to @buf
1125  * @count: the maximum number of bytes to print
1126  *
1127  * The function prints the cpumask into the buffer as hex values of
1128  * cpumask; Typically used by bin_attribute to export cpumask bitmask
1129  * ABI.
1130  *
1131  * Returns the length of how many bytes have been copied, excluding
1132  * terminating '\0'.
1133  */
1134 static inline ssize_t
1135 cpumap_print_bitmask_to_buf(char *buf, const struct cpumask *mask,
1136 		loff_t off, size_t count)
1137 {
1138 	return bitmap_print_bitmask_to_buf(buf, cpumask_bits(mask),
1139 				   nr_cpu_ids, off, count) - 1;
1140 }
1141 
1142 /**
1143  * cpumap_print_list_to_buf  - copies the cpumask into the buffer as
1144  *	comma-separated list of cpus
1145  *
1146  * Everything is same with the above cpumap_print_bitmask_to_buf()
1147  * except the print format.
1148  */
1149 static inline ssize_t
1150 cpumap_print_list_to_buf(char *buf, const struct cpumask *mask,
1151 		loff_t off, size_t count)
1152 {
1153 	return bitmap_print_list_to_buf(buf, cpumask_bits(mask),
1154 				   nr_cpu_ids, off, count) - 1;
1155 }
1156 
1157 #if NR_CPUS <= BITS_PER_LONG
1158 #define CPU_MASK_ALL							\
1159 (cpumask_t) { {								\
1160 	[BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS)	\
1161 } }
1162 #else
1163 #define CPU_MASK_ALL							\
1164 (cpumask_t) { {								\
1165 	[0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,			\
1166 	[BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS)	\
1167 } }
1168 #endif /* NR_CPUS > BITS_PER_LONG */
1169 
1170 #define CPU_MASK_NONE							\
1171 (cpumask_t) { {								\
1172 	[0 ... BITS_TO_LONGS(NR_CPUS)-1] =  0UL				\
1173 } }
1174 
1175 #define CPU_MASK_CPU0							\
1176 (cpumask_t) { {								\
1177 	[0] =  1UL							\
1178 } }
1179 
1180 /*
1181  * Provide a valid theoretical max size for cpumap and cpulist sysfs files
1182  * to avoid breaking userspace which may allocate a buffer based on the size
1183  * reported by e.g. fstat.
1184  *
1185  * for cpumap NR_CPUS * 9/32 - 1 should be an exact length.
1186  *
1187  * For cpulist 7 is (ceil(log10(NR_CPUS)) + 1) allowing for NR_CPUS to be up
1188  * to 2 orders of magnitude larger than 8192. And then we divide by 2 to
1189  * cover a worst-case of every other cpu being on one of two nodes for a
1190  * very large NR_CPUS.
1191  *
1192  *  Use PAGE_SIZE as a minimum for smaller configurations while avoiding
1193  *  unsigned comparison to -1.
1194  */
1195 #define CPUMAP_FILE_MAX_BYTES  (((NR_CPUS * 9)/32 > PAGE_SIZE) \
1196 					? (NR_CPUS * 9)/32 - 1 : PAGE_SIZE)
1197 #define CPULIST_FILE_MAX_BYTES  (((NR_CPUS * 7)/2 > PAGE_SIZE) ? (NR_CPUS * 7)/2 : PAGE_SIZE)
1198 
1199 #endif /* __LINUX_CPUMASK_H */
1200