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