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