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