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