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