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 
16 /* Don't assign or return these: may not be this big! */
17 typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
18 
19 /**
20  * cpumask_bits - get the bits in a cpumask
21  * @maskp: the struct cpumask *
22  *
23  * You should only assume nr_cpu_ids bits of this mask are valid.  This is
24  * a macro so it's const-correct.
25  */
26 #define cpumask_bits(maskp) ((maskp)->bits)
27 
28 /**
29  * cpumask_pr_args - printf args to output a cpumask
30  * @maskp: cpumask to be printed
31  *
32  * Can be used to provide arguments for '%*pb[l]' when printing a cpumask.
33  */
34 #define cpumask_pr_args(maskp)		nr_cpu_ids, cpumask_bits(maskp)
35 
36 #if NR_CPUS == 1
37 #define nr_cpu_ids		1U
38 #else
39 extern unsigned int nr_cpu_ids;
40 #endif
41 
42 #ifdef CONFIG_CPUMASK_OFFSTACK
43 /* Assuming NR_CPUS is huge, a runtime limit is more efficient.  Also,
44  * not all bits may be allocated. */
45 #define nr_cpumask_bits	nr_cpu_ids
46 #else
47 #define nr_cpumask_bits	((unsigned int)NR_CPUS)
48 #endif
49 
50 /*
51  * The following particular system cpumasks and operations manage
52  * possible, present, active and online cpus.
53  *
54  *     cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
55  *     cpu_present_mask - has bit 'cpu' set iff cpu is populated
56  *     cpu_online_mask  - has bit 'cpu' set iff cpu available to scheduler
57  *     cpu_active_mask  - has bit 'cpu' set iff cpu available to migration
58  *
59  *  If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
60  *
61  *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
62  *  that it is possible might ever be plugged in at anytime during the
63  *  life of that system boot.  The cpu_present_mask is dynamic(*),
64  *  representing which CPUs are currently plugged in.  And
65  *  cpu_online_mask is the dynamic subset of cpu_present_mask,
66  *  indicating those CPUs available for scheduling.
67  *
68  *  If HOTPLUG is enabled, then cpu_possible_mask is forced to have
69  *  all NR_CPUS bits set, otherwise it is just the set of CPUs that
70  *  ACPI reports present at boot.
71  *
72  *  If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
73  *  depending on what ACPI reports as currently plugged in, otherwise
74  *  cpu_present_mask is just a copy of cpu_possible_mask.
75  *
76  *  (*) Well, cpu_present_mask is dynamic in the hotplug case.  If not
77  *      hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
78  *
79  * Subtleties:
80  * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
81  *    assumption that their single CPU is online.  The UP
82  *    cpu_{online,possible,present}_masks are placebos.  Changing them
83  *    will have no useful affect on the following num_*_cpus()
84  *    and cpu_*() macros in the UP case.  This ugliness is a UP
85  *    optimization - don't waste any instructions or memory references
86  *    asking if you're online or how many CPUs there are if there is
87  *    only one CPU.
88  */
89 
90 extern struct cpumask __cpu_possible_mask;
91 extern struct cpumask __cpu_online_mask;
92 extern struct cpumask __cpu_present_mask;
93 extern struct cpumask __cpu_active_mask;
94 extern struct cpumask __cpu_dying_mask;
95 ///cpu位图
96 #define cpu_possible_mask ((const struct cpumask *)&__cpu_possible_mask)
97 #define cpu_online_mask   ((const struct cpumask *)&__cpu_online_mask)
98 #define cpu_present_mask  ((const struct cpumask *)&__cpu_present_mask)
99 #define cpu_active_mask   ((const struct cpumask *)&__cpu_active_mask)
100 #define cpu_dying_mask    ((const struct cpumask *)&__cpu_dying_mask)
101 
102 extern atomic_t __num_online_cpus;
103 
104 extern cpumask_t cpus_booted_once_mask;
105 
cpu_max_bits_warn(unsigned int cpu,unsigned int bits)106 static inline void cpu_max_bits_warn(unsigned int cpu, unsigned int bits)
107 {
108 #ifdef CONFIG_DEBUG_PER_CPU_MAPS
109 	WARN_ON_ONCE(cpu >= bits);
110 #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
111 }
112 
113 /* verify cpu argument to cpumask_* operators */
cpumask_check(unsigned int cpu)114 static inline unsigned int cpumask_check(unsigned int cpu)
115 {
116 	cpu_max_bits_warn(cpu, nr_cpumask_bits);
117 	return cpu;
118 }
119 
120 #if NR_CPUS == 1
121 /* Uniprocessor.  Assume all masks are "1". */
cpumask_first(const struct cpumask * srcp)122 static inline unsigned int cpumask_first(const struct cpumask *srcp)
123 {
124 	return 0;
125 }
126 
cpumask_last(const struct cpumask * srcp)127 static inline unsigned int cpumask_last(const struct cpumask *srcp)
128 {
129 	return 0;
130 }
131 
132 /* Valid inputs for n are -1 and 0. */
cpumask_next(int n,const struct cpumask * srcp)133 static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
134 {
135 	return n+1;
136 }
137 
cpumask_next_zero(int n,const struct cpumask * srcp)138 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
139 {
140 	return n+1;
141 }
142 
cpumask_next_and(int n,const struct cpumask * srcp,const struct cpumask * andp)143 static inline unsigned int cpumask_next_and(int n,
144 					    const struct cpumask *srcp,
145 					    const struct cpumask *andp)
146 {
147 	return n+1;
148 }
149 
cpumask_next_wrap(int n,const struct cpumask * mask,int start,bool wrap)150 static inline unsigned int cpumask_next_wrap(int n, const struct cpumask *mask,
151 					     int start, bool wrap)
152 {
153 	/* cpu0 unless stop condition, wrap and at cpu0, then nr_cpumask_bits */
154 	return (wrap && n == 0);
155 }
156 
157 /* cpu must be a valid cpu, ie 0, so there's no other choice. */
cpumask_any_but(const struct cpumask * mask,unsigned int cpu)158 static inline unsigned int cpumask_any_but(const struct cpumask *mask,
159 					   unsigned int cpu)
160 {
161 	return 1;
162 }
163 
cpumask_local_spread(unsigned int i,int node)164 static inline unsigned int cpumask_local_spread(unsigned int i, int node)
165 {
166 	return 0;
167 }
168 
cpumask_any_and_distribute(const struct cpumask * src1p,const struct cpumask * src2p)169 static inline int cpumask_any_and_distribute(const struct cpumask *src1p,
170 					     const struct cpumask *src2p) {
171 	return cpumask_next_and(-1, src1p, src2p);
172 }
173 
cpumask_any_distribute(const struct cpumask * srcp)174 static inline int cpumask_any_distribute(const struct cpumask *srcp)
175 {
176 	return cpumask_first(srcp);
177 }
178 
179 #define for_each_cpu(cpu, mask)			\
180 	for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
181 #define for_each_cpu_not(cpu, mask)		\
182 	for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
183 #define for_each_cpu_wrap(cpu, mask, start)	\
184 	for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)(start))
185 #define for_each_cpu_and(cpu, mask1, mask2)	\
186 	for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask1, (void)mask2)
187 #else
188 /**
189  * cpumask_first - get the first cpu in a cpumask
190  * @srcp: the cpumask pointer
191  *
192  * Returns >= nr_cpu_ids if no cpus set.
193  */
cpumask_first(const struct cpumask * srcp)194 static inline unsigned int cpumask_first(const struct cpumask *srcp)
195 {
196 	return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
197 }
198 
199 /**
200  * cpumask_last - get the last CPU in a cpumask
201  * @srcp:	- the cpumask pointer
202  *
203  * Returns	>= nr_cpumask_bits if no CPUs set.
204  */
cpumask_last(const struct cpumask * srcp)205 static inline unsigned int cpumask_last(const struct cpumask *srcp)
206 {
207 	return find_last_bit(cpumask_bits(srcp), nr_cpumask_bits);
208 }
209 
210 unsigned int __pure cpumask_next(int n, const struct cpumask *srcp);
211 
212 /**
213  * cpumask_next_zero - get the next unset cpu in a cpumask
214  * @n: the cpu prior to the place to search (ie. return will be > @n)
215  * @srcp: the cpumask pointer
216  *
217  * Returns >= nr_cpu_ids if no further cpus unset.
218  */
cpumask_next_zero(int n,const struct cpumask * srcp)219 static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
220 {
221 	/* -1 is a legal arg here. */
222 	if (n != -1)
223 		cpumask_check(n);
224 	return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
225 }
226 
227 int __pure cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
228 int __pure cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
229 unsigned int cpumask_local_spread(unsigned int i, int node);
230 int cpumask_any_and_distribute(const struct cpumask *src1p,
231 			       const struct cpumask *src2p);
232 int cpumask_any_distribute(const struct cpumask *srcp);
233 
234 /**
235  * for_each_cpu - iterate over every cpu in a mask
236  * @cpu: the (optionally unsigned) integer iterator
237  * @mask: the cpumask pointer
238  *
239  * After the loop, cpu is >= nr_cpu_ids.
240  */
241 #define for_each_cpu(cpu, mask)				\
242 	for ((cpu) = -1;				\
243 		(cpu) = cpumask_next((cpu), (mask)),	\
244 		(cpu) < nr_cpu_ids;)
245 
246 /**
247  * for_each_cpu_not - iterate over every cpu in a complemented mask
248  * @cpu: the (optionally unsigned) integer iterator
249  * @mask: the cpumask pointer
250  *
251  * After the loop, cpu is >= nr_cpu_ids.
252  */
253 #define for_each_cpu_not(cpu, mask)				\
254 	for ((cpu) = -1;					\
255 		(cpu) = cpumask_next_zero((cpu), (mask)),	\
256 		(cpu) < nr_cpu_ids;)
257 
258 extern int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap);
259 
260 /**
261  * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a specified location
262  * @cpu: the (optionally unsigned) integer iterator
263  * @mask: the cpumask pointer
264  * @start: the start location
265  *
266  * The implementation does not assume any bit in @mask is set (including @start).
267  *
268  * After the loop, cpu is >= nr_cpu_ids.
269  */
270 #define for_each_cpu_wrap(cpu, mask, start)					\
271 	for ((cpu) = cpumask_next_wrap((start)-1, (mask), (start), false);	\
272 	     (cpu) < nr_cpumask_bits;						\
273 	     (cpu) = cpumask_next_wrap((cpu), (mask), (start), true))
274 
275 /**
276  * for_each_cpu_and - iterate over every cpu in both masks
277  * @cpu: the (optionally unsigned) integer iterator
278  * @mask1: the first cpumask pointer
279  * @mask2: the second cpumask pointer
280  *
281  * This saves a temporary CPU mask in many places.  It is equivalent to:
282  *	struct cpumask tmp;
283  *	cpumask_and(&tmp, &mask1, &mask2);
284  *	for_each_cpu(cpu, &tmp)
285  *		...
286  *
287  * After the loop, cpu is >= nr_cpu_ids.
288  */
289 #define for_each_cpu_and(cpu, mask1, mask2)				\
290 	for ((cpu) = -1;						\
291 		(cpu) = cpumask_next_and((cpu), (mask1), (mask2)),	\
292 		(cpu) < nr_cpu_ids;)
293 #endif /* SMP */
294 
295 #define CPU_BITS_NONE						\
296 {								\
297 	[0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL			\
298 }
299 
300 #define CPU_BITS_CPU0						\
301 {								\
302 	[0] =  1UL						\
303 }
304 
305 /**
306  * cpumask_set_cpu - set a cpu in a cpumask
307  * @cpu: cpu number (< nr_cpu_ids)
308  * @dstp: the cpumask pointer
309  */
cpumask_set_cpu(unsigned int cpu,struct cpumask * dstp)310 static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
311 {
312 	set_bit(cpumask_check(cpu), cpumask_bits(dstp));
313 }
314 
__cpumask_set_cpu(unsigned int cpu,struct cpumask * dstp)315 static inline void __cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
316 {
317 	__set_bit(cpumask_check(cpu), cpumask_bits(dstp));
318 }
319 
320 
321 /**
322  * cpumask_clear_cpu - clear a cpu in a cpumask
323  * @cpu: cpu number (< nr_cpu_ids)
324  * @dstp: the cpumask pointer
325  */
cpumask_clear_cpu(int cpu,struct cpumask * dstp)326 static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
327 {
328 	clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
329 }
330 
__cpumask_clear_cpu(int cpu,struct cpumask * dstp)331 static inline void __cpumask_clear_cpu(int cpu, struct cpumask *dstp)
332 {
333 	__clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
334 }
335 
336 /**
337  * cpumask_test_cpu - test for a cpu in a cpumask
338  * @cpu: cpu number (< nr_cpu_ids)
339  * @cpumask: the cpumask pointer
340  *
341  * Returns 1 if @cpu is set in @cpumask, else returns 0
342  */
cpumask_test_cpu(int cpu,const struct cpumask * cpumask)343 static inline int cpumask_test_cpu(int cpu, const struct cpumask *cpumask)
344 {
345 	return test_bit(cpumask_check(cpu), cpumask_bits((cpumask)));
346 }
347 
348 /**
349  * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
350  * @cpu: cpu number (< nr_cpu_ids)
351  * @cpumask: the cpumask pointer
352  *
353  * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0
354  *
355  * test_and_set_bit wrapper for cpumasks.
356  */
cpumask_test_and_set_cpu(int cpu,struct cpumask * cpumask)357 static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
358 {
359 	return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
360 }
361 
362 /**
363  * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
364  * @cpu: cpu number (< nr_cpu_ids)
365  * @cpumask: the cpumask pointer
366  *
367  * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0
368  *
369  * test_and_clear_bit wrapper for cpumasks.
370  */
cpumask_test_and_clear_cpu(int cpu,struct cpumask * cpumask)371 static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
372 {
373 	return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
374 }
375 
376 /**
377  * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
378  * @dstp: the cpumask pointer
379  */
cpumask_setall(struct cpumask * dstp)380 static inline void cpumask_setall(struct cpumask *dstp)
381 {
382 	bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
383 }
384 
385 /**
386  * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
387  * @dstp: the cpumask pointer
388  */
cpumask_clear(struct cpumask * dstp)389 static inline void cpumask_clear(struct cpumask *dstp)
390 {
391 	bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
392 }
393 
394 /**
395  * cpumask_and - *dstp = *src1p & *src2p
396  * @dstp: the cpumask result
397  * @src1p: the first input
398  * @src2p: the second input
399  *
400  * If *@dstp is empty, returns 0, else returns 1
401  */
cpumask_and(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)402 static inline int cpumask_and(struct cpumask *dstp,
403 			       const struct cpumask *src1p,
404 			       const struct cpumask *src2p)
405 {
406 	return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
407 				       cpumask_bits(src2p), nr_cpumask_bits);
408 }
409 
410 /**
411  * cpumask_or - *dstp = *src1p | *src2p
412  * @dstp: the cpumask result
413  * @src1p: the first input
414  * @src2p: the second input
415  */
cpumask_or(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)416 static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
417 			      const struct cpumask *src2p)
418 {
419 	bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
420 				      cpumask_bits(src2p), nr_cpumask_bits);
421 }
422 
423 /**
424  * cpumask_xor - *dstp = *src1p ^ *src2p
425  * @dstp: the cpumask result
426  * @src1p: the first input
427  * @src2p: the second input
428  */
cpumask_xor(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)429 static inline void cpumask_xor(struct cpumask *dstp,
430 			       const struct cpumask *src1p,
431 			       const struct cpumask *src2p)
432 {
433 	bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
434 				       cpumask_bits(src2p), nr_cpumask_bits);
435 }
436 
437 /**
438  * cpumask_andnot - *dstp = *src1p & ~*src2p
439  * @dstp: the cpumask result
440  * @src1p: the first input
441  * @src2p: the second input
442  *
443  * If *@dstp is empty, returns 0, else returns 1
444  */
cpumask_andnot(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)445 static inline int cpumask_andnot(struct cpumask *dstp,
446 				  const struct cpumask *src1p,
447 				  const struct cpumask *src2p)
448 {
449 	return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
450 					  cpumask_bits(src2p), nr_cpumask_bits);
451 }
452 
453 /**
454  * cpumask_complement - *dstp = ~*srcp
455  * @dstp: the cpumask result
456  * @srcp: the input to invert
457  */
cpumask_complement(struct cpumask * dstp,const struct cpumask * srcp)458 static inline void cpumask_complement(struct cpumask *dstp,
459 				      const struct cpumask *srcp)
460 {
461 	bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
462 					      nr_cpumask_bits);
463 }
464 
465 /**
466  * cpumask_equal - *src1p == *src2p
467  * @src1p: the first input
468  * @src2p: the second input
469  */
cpumask_equal(const struct cpumask * src1p,const struct cpumask * src2p)470 static inline bool cpumask_equal(const struct cpumask *src1p,
471 				const struct cpumask *src2p)
472 {
473 	return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
474 						 nr_cpumask_bits);
475 }
476 
477 /**
478  * cpumask_or_equal - *src1p | *src2p == *src3p
479  * @src1p: the first input
480  * @src2p: the second input
481  * @src3p: the third input
482  */
cpumask_or_equal(const struct cpumask * src1p,const struct cpumask * src2p,const struct cpumask * src3p)483 static inline bool cpumask_or_equal(const struct cpumask *src1p,
484 				    const struct cpumask *src2p,
485 				    const struct cpumask *src3p)
486 {
487 	return bitmap_or_equal(cpumask_bits(src1p), cpumask_bits(src2p),
488 			       cpumask_bits(src3p), nr_cpumask_bits);
489 }
490 
491 /**
492  * cpumask_intersects - (*src1p & *src2p) != 0
493  * @src1p: the first input
494  * @src2p: the second input
495  */
cpumask_intersects(const struct cpumask * src1p,const struct cpumask * src2p)496 static inline bool cpumask_intersects(const struct cpumask *src1p,
497 				     const struct cpumask *src2p)
498 {
499 	return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
500 						      nr_cpumask_bits);
501 }
502 
503 /**
504  * cpumask_subset - (*src1p & ~*src2p) == 0
505  * @src1p: the first input
506  * @src2p: the second input
507  *
508  * Returns 1 if *@src1p is a subset of *@src2p, else returns 0
509  */
cpumask_subset(const struct cpumask * src1p,const struct cpumask * src2p)510 static inline int cpumask_subset(const struct cpumask *src1p,
511 				 const struct cpumask *src2p)
512 {
513 	return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
514 						  nr_cpumask_bits);
515 }
516 
517 /**
518  * cpumask_empty - *srcp == 0
519  * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
520  */
cpumask_empty(const struct cpumask * srcp)521 static inline bool cpumask_empty(const struct cpumask *srcp)
522 {
523 	return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
524 }
525 
526 /**
527  * cpumask_full - *srcp == 0xFFFFFFFF...
528  * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
529  */
cpumask_full(const struct cpumask * srcp)530 static inline bool cpumask_full(const struct cpumask *srcp)
531 {
532 	return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
533 }
534 
535 /**
536  * cpumask_weight - Count of bits in *srcp
537  * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
538  */
cpumask_weight(const struct cpumask * srcp)539 static inline unsigned int cpumask_weight(const struct cpumask *srcp)
540 {
541 	return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
542 }
543 
544 /**
545  * cpumask_shift_right - *dstp = *srcp >> n
546  * @dstp: the cpumask result
547  * @srcp: the input to shift
548  * @n: the number of bits to shift by
549  */
cpumask_shift_right(struct cpumask * dstp,const struct cpumask * srcp,int n)550 static inline void cpumask_shift_right(struct cpumask *dstp,
551 				       const struct cpumask *srcp, int n)
552 {
553 	bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
554 					       nr_cpumask_bits);
555 }
556 
557 /**
558  * cpumask_shift_left - *dstp = *srcp << n
559  * @dstp: the cpumask result
560  * @srcp: the input to shift
561  * @n: the number of bits to shift by
562  */
cpumask_shift_left(struct cpumask * dstp,const struct cpumask * srcp,int n)563 static inline void cpumask_shift_left(struct cpumask *dstp,
564 				      const struct cpumask *srcp, int n)
565 {
566 	bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
567 					      nr_cpumask_bits);
568 }
569 
570 /**
571  * cpumask_copy - *dstp = *srcp
572  * @dstp: the result
573  * @srcp: the input cpumask
574  */
cpumask_copy(struct cpumask * dstp,const struct cpumask * srcp)575 static inline void cpumask_copy(struct cpumask *dstp,
576 				const struct cpumask *srcp)
577 {
578 	bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
579 }
580 
581 /**
582  * cpumask_any - pick a "random" cpu from *srcp
583  * @srcp: the input cpumask
584  *
585  * Returns >= nr_cpu_ids if no cpus set.
586  */
587 #define cpumask_any(srcp) cpumask_first(srcp)
588 
589 /**
590  * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
591  * @src1p: the first input
592  * @src2p: the second input
593  *
594  * Returns >= nr_cpu_ids if no cpus set in both.  See also cpumask_next_and().
595  */
596 #define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
597 
598 /**
599  * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
600  * @mask1: the first input cpumask
601  * @mask2: the second input cpumask
602  *
603  * Returns >= nr_cpu_ids if no cpus set.
604  */
605 #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
606 
607 /**
608  * cpumask_of - the cpumask containing just a given cpu
609  * @cpu: the cpu (<= nr_cpu_ids)
610  */
611 #define cpumask_of(cpu) (get_cpu_mask(cpu))
612 
613 /**
614  * cpumask_parse_user - extract a cpumask from a user string
615  * @buf: the buffer to extract from
616  * @len: the length of the buffer
617  * @dstp: the cpumask to set.
618  *
619  * Returns -errno, or 0 for success.
620  */
cpumask_parse_user(const char __user * buf,int len,struct cpumask * dstp)621 static inline int cpumask_parse_user(const char __user *buf, int len,
622 				     struct cpumask *dstp)
623 {
624 	return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
625 }
626 
627 /**
628  * cpumask_parselist_user - extract a cpumask from a user string
629  * @buf: the buffer to extract from
630  * @len: the length of the buffer
631  * @dstp: the cpumask to set.
632  *
633  * Returns -errno, or 0 for success.
634  */
cpumask_parselist_user(const char __user * buf,int len,struct cpumask * dstp)635 static inline int cpumask_parselist_user(const char __user *buf, int len,
636 				     struct cpumask *dstp)
637 {
638 	return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
639 				     nr_cpumask_bits);
640 }
641 
642 /**
643  * cpumask_parse - extract a cpumask from a string
644  * @buf: the buffer to extract from
645  * @dstp: the cpumask to set.
646  *
647  * Returns -errno, or 0 for success.
648  */
cpumask_parse(const char * buf,struct cpumask * dstp)649 static inline int cpumask_parse(const char *buf, struct cpumask *dstp)
650 {
651 	return bitmap_parse(buf, UINT_MAX, cpumask_bits(dstp), nr_cpumask_bits);
652 }
653 
654 /**
655  * cpulist_parse - extract a cpumask from a user string of ranges
656  * @buf: the buffer to extract from
657  * @dstp: the cpumask to set.
658  *
659  * Returns -errno, or 0 for success.
660  */
cpulist_parse(const char * buf,struct cpumask * dstp)661 static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
662 {
663 	return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
664 }
665 
666 /**
667  * cpumask_size - size to allocate for a 'struct cpumask' in bytes
668  */
cpumask_size(void)669 static inline unsigned int cpumask_size(void)
670 {
671 	return BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long);
672 }
673 
674 /*
675  * cpumask_var_t: struct cpumask for stack usage.
676  *
677  * Oh, the wicked games we play!  In order to make kernel coding a
678  * little more difficult, we typedef cpumask_var_t to an array or a
679  * pointer: doing &mask on an array is a noop, so it still works.
680  *
681  * ie.
682  *	cpumask_var_t tmpmask;
683  *	if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
684  *		return -ENOMEM;
685  *
686  *	  ... use 'tmpmask' like a normal struct cpumask * ...
687  *
688  *	free_cpumask_var(tmpmask);
689  *
690  *
691  * However, one notable exception is there. alloc_cpumask_var() allocates
692  * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has
693  * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t.
694  *
695  *	cpumask_var_t tmpmask;
696  *	if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
697  *		return -ENOMEM;
698  *
699  *	var = *tmpmask;
700  *
701  * This code makes NR_CPUS length memcopy and brings to a memory corruption.
702  * cpumask_copy() provide safe copy functionality.
703  *
704  * Note that there is another evil here: If you define a cpumask_var_t
705  * as a percpu variable then the way to obtain the address of the cpumask
706  * structure differently influences what this_cpu_* operation needs to be
707  * used. Please use this_cpu_cpumask_var_t in those cases. The direct use
708  * of this_cpu_ptr() or this_cpu_read() will lead to failures when the
709  * other type of cpumask_var_t implementation is configured.
710  *
711  * Please also note that __cpumask_var_read_mostly can be used to declare
712  * a cpumask_var_t variable itself (not its content) as read mostly.
713  */
714 #ifdef CONFIG_CPUMASK_OFFSTACK
715 typedef struct cpumask *cpumask_var_t;
716 
717 #define this_cpu_cpumask_var_ptr(x)	this_cpu_read(x)
718 #define __cpumask_var_read_mostly	__read_mostly
719 
720 bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
721 bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
722 bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
723 bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
724 void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
725 void free_cpumask_var(cpumask_var_t mask);
726 void free_bootmem_cpumask_var(cpumask_var_t mask);
727 
cpumask_available(cpumask_var_t mask)728 static inline bool cpumask_available(cpumask_var_t mask)
729 {
730 	return mask != NULL;
731 }
732 
733 #else
734 typedef struct cpumask cpumask_var_t[1];
735 
736 #define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x)
737 #define __cpumask_var_read_mostly
738 
alloc_cpumask_var(cpumask_var_t * mask,gfp_t flags)739 static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
740 {
741 	return true;
742 }
743 
alloc_cpumask_var_node(cpumask_var_t * mask,gfp_t flags,int node)744 static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
745 					  int node)
746 {
747 	return true;
748 }
749 
zalloc_cpumask_var(cpumask_var_t * mask,gfp_t flags)750 static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
751 {
752 	cpumask_clear(*mask);
753 	return true;
754 }
755 
zalloc_cpumask_var_node(cpumask_var_t * mask,gfp_t flags,int node)756 static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
757 					  int node)
758 {
759 	cpumask_clear(*mask);
760 	return true;
761 }
762 
alloc_bootmem_cpumask_var(cpumask_var_t * mask)763 static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
764 {
765 }
766 
free_cpumask_var(cpumask_var_t mask)767 static inline void free_cpumask_var(cpumask_var_t mask)
768 {
769 }
770 
free_bootmem_cpumask_var(cpumask_var_t mask)771 static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
772 {
773 }
774 
cpumask_available(cpumask_var_t mask)775 static inline bool cpumask_available(cpumask_var_t mask)
776 {
777 	return true;
778 }
779 #endif /* CONFIG_CPUMASK_OFFSTACK */
780 
781 /* It's common to want to use cpu_all_mask in struct member initializers,
782  * so it has to refer to an address rather than a pointer. */
783 extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
784 #define cpu_all_mask to_cpumask(cpu_all_bits)
785 
786 /* First bits of cpu_bit_bitmap are in fact unset. */
787 #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
788 
789 #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
790 #define for_each_online_cpu(cpu)   for_each_cpu((cpu), cpu_online_mask)
791 #define for_each_present_cpu(cpu)  for_each_cpu((cpu), cpu_present_mask)
792 
793 /* Wrappers for arch boot code to manipulate normally-constant masks */
794 void init_cpu_present(const struct cpumask *src);
795 void init_cpu_possible(const struct cpumask *src);
796 void init_cpu_online(const struct cpumask *src);
797 
reset_cpu_possible_mask(void)798 static inline void reset_cpu_possible_mask(void)
799 {
800 	bitmap_zero(cpumask_bits(&__cpu_possible_mask), NR_CPUS);
801 }
802 
803 static inline void
set_cpu_possible(unsigned int cpu,bool possible)804 set_cpu_possible(unsigned int cpu, bool possible)
805 {
806 	if (possible)
807 		cpumask_set_cpu(cpu, &__cpu_possible_mask);
808 	else
809 		cpumask_clear_cpu(cpu, &__cpu_possible_mask);
810 }
811 
812 static inline void
set_cpu_present(unsigned int cpu,bool present)813 set_cpu_present(unsigned int cpu, bool present)
814 {
815 	if (present)
816 		cpumask_set_cpu(cpu, &__cpu_present_mask);
817 	else
818 		cpumask_clear_cpu(cpu, &__cpu_present_mask);
819 }
820 
821 void set_cpu_online(unsigned int cpu, bool online);
822 
823 static inline void
set_cpu_active(unsigned int cpu,bool active)824 set_cpu_active(unsigned int cpu, bool active)
825 {
826 	if (active)
827 		cpumask_set_cpu(cpu, &__cpu_active_mask);
828 	else
829 		cpumask_clear_cpu(cpu, &__cpu_active_mask);
830 }
831 
832 static inline void
set_cpu_dying(unsigned int cpu,bool dying)833 set_cpu_dying(unsigned int cpu, bool dying)
834 {
835 	if (dying)
836 		cpumask_set_cpu(cpu, &__cpu_dying_mask);
837 	else
838 		cpumask_clear_cpu(cpu, &__cpu_dying_mask);
839 }
840 
841 /**
842  * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
843  * @bitmap: the bitmap
844  *
845  * There are a few places where cpumask_var_t isn't appropriate and
846  * static cpumasks must be used (eg. very early boot), yet we don't
847  * expose the definition of 'struct cpumask'.
848  *
849  * This does the conversion, and can be used as a constant initializer.
850  */
851 #define to_cpumask(bitmap)						\
852 	((struct cpumask *)(1 ? (bitmap)				\
853 			    : (void *)sizeof(__check_is_bitmap(bitmap))))
854 
__check_is_bitmap(const unsigned long * bitmap)855 static inline int __check_is_bitmap(const unsigned long *bitmap)
856 {
857 	return 1;
858 }
859 
860 /*
861  * Special-case data structure for "single bit set only" constant CPU masks.
862  *
863  * We pre-generate all the 64 (or 32) possible bit positions, with enough
864  * padding to the left and the right, and return the constant pointer
865  * appropriately offset.
866  */
867 extern const unsigned long
868 	cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
869 
get_cpu_mask(unsigned int cpu)870 static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
871 {
872 	const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
873 	p -= cpu / BITS_PER_LONG;
874 	return to_cpumask(p);
875 }
876 
877 #if NR_CPUS > 1
878 /**
879  * num_online_cpus() - Read the number of online CPUs
880  *
881  * Despite the fact that __num_online_cpus is of type atomic_t, this
882  * interface gives only a momentary snapshot and is not protected against
883  * concurrent CPU hotplug operations unless invoked from a cpuhp_lock held
884  * region.
885  */
num_online_cpus(void)886 static inline unsigned int num_online_cpus(void)
887 {
888 	return atomic_read(&__num_online_cpus);
889 }
890 #define num_possible_cpus()	cpumask_weight(cpu_possible_mask)
891 #define num_present_cpus()	cpumask_weight(cpu_present_mask)
892 #define num_active_cpus()	cpumask_weight(cpu_active_mask)
893 
cpu_online(unsigned int cpu)894 static inline bool cpu_online(unsigned int cpu)
895 {
896 	return cpumask_test_cpu(cpu, cpu_online_mask);
897 }
898 
cpu_possible(unsigned int cpu)899 static inline bool cpu_possible(unsigned int cpu)
900 {
901 	return cpumask_test_cpu(cpu, cpu_possible_mask);
902 }
903 
cpu_present(unsigned int cpu)904 static inline bool cpu_present(unsigned int cpu)
905 {
906 	return cpumask_test_cpu(cpu, cpu_present_mask);
907 }
908 
cpu_active(unsigned int cpu)909 static inline bool cpu_active(unsigned int cpu)
910 {
911 	return cpumask_test_cpu(cpu, cpu_active_mask);
912 }
913 
cpu_dying(unsigned int cpu)914 static inline bool cpu_dying(unsigned int cpu)
915 {
916 	return cpumask_test_cpu(cpu, cpu_dying_mask);
917 }
918 
919 #else
920 
921 #define num_online_cpus()	1U
922 #define num_possible_cpus()	1U
923 #define num_present_cpus()	1U
924 #define num_active_cpus()	1U
925 
cpu_online(unsigned int cpu)926 static inline bool cpu_online(unsigned int cpu)
927 {
928 	return cpu == 0;
929 }
930 
cpu_possible(unsigned int cpu)931 static inline bool cpu_possible(unsigned int cpu)
932 {
933 	return cpu == 0;
934 }
935 
cpu_present(unsigned int cpu)936 static inline bool cpu_present(unsigned int cpu)
937 {
938 	return cpu == 0;
939 }
940 
cpu_active(unsigned int cpu)941 static inline bool cpu_active(unsigned int cpu)
942 {
943 	return cpu == 0;
944 }
945 
cpu_dying(unsigned int cpu)946 static inline bool cpu_dying(unsigned int cpu)
947 {
948 	return false;
949 }
950 
951 #endif /* NR_CPUS > 1 */
952 
953 #define cpu_is_offline(cpu)	unlikely(!cpu_online(cpu))
954 
955 #if NR_CPUS <= BITS_PER_LONG
956 #define CPU_BITS_ALL						\
957 {								\
958 	[BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS)	\
959 }
960 
961 #else /* NR_CPUS > BITS_PER_LONG */
962 
963 #define CPU_BITS_ALL						\
964 {								\
965 	[0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,		\
966 	[BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS)	\
967 }
968 #endif /* NR_CPUS > BITS_PER_LONG */
969 
970 /**
971  * cpumap_print_to_pagebuf  - copies the cpumask into the buffer either
972  *	as comma-separated list of cpus or hex values of cpumask
973  * @list: indicates whether the cpumap must be list
974  * @mask: the cpumask to copy
975  * @buf: the buffer to copy into
976  *
977  * Returns the length of the (null-terminated) @buf string, zero if
978  * nothing is copied.
979  */
980 static inline ssize_t
cpumap_print_to_pagebuf(bool list,char * buf,const struct cpumask * mask)981 cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask)
982 {
983 	return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask),
984 				      nr_cpu_ids);
985 }
986 
987 /**
988  * cpumap_print_bitmask_to_buf  - copies the cpumask into the buffer as
989  *	hex values of cpumask
990  *
991  * @buf: the buffer to copy into
992  * @mask: the cpumask to copy
993  * @off: in the string from which we are copying, we copy to @buf
994  * @count: the maximum number of bytes to print
995  *
996  * The function prints the cpumask into the buffer as hex values of
997  * cpumask; Typically used by bin_attribute to export cpumask bitmask
998  * ABI.
999  *
1000  * Returns the length of how many bytes have been copied, excluding
1001  * terminating '\0'.
1002  */
1003 static inline ssize_t
cpumap_print_bitmask_to_buf(char * buf,const struct cpumask * mask,loff_t off,size_t count)1004 cpumap_print_bitmask_to_buf(char *buf, const struct cpumask *mask,
1005 		loff_t off, size_t count)
1006 {
1007 	return bitmap_print_bitmask_to_buf(buf, cpumask_bits(mask),
1008 				   nr_cpu_ids, off, count) - 1;
1009 }
1010 
1011 /**
1012  * cpumap_print_list_to_buf  - copies the cpumask into the buffer as
1013  *	comma-separated list of cpus
1014  *
1015  * Everything is same with the above cpumap_print_bitmask_to_buf()
1016  * except the print format.
1017  */
1018 static inline ssize_t
cpumap_print_list_to_buf(char * buf,const struct cpumask * mask,loff_t off,size_t count)1019 cpumap_print_list_to_buf(char *buf, const struct cpumask *mask,
1020 		loff_t off, size_t count)
1021 {
1022 	return bitmap_print_list_to_buf(buf, cpumask_bits(mask),
1023 				   nr_cpu_ids, off, count) - 1;
1024 }
1025 
1026 #if NR_CPUS <= BITS_PER_LONG
1027 #define CPU_MASK_ALL							\
1028 (cpumask_t) { {								\
1029 	[BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS)	\
1030 } }
1031 #else
1032 #define CPU_MASK_ALL							\
1033 (cpumask_t) { {								\
1034 	[0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,			\
1035 	[BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS)	\
1036 } }
1037 #endif /* NR_CPUS > BITS_PER_LONG */
1038 
1039 #define CPU_MASK_NONE							\
1040 (cpumask_t) { {								\
1041 	[0 ... BITS_TO_LONGS(NR_CPUS)-1] =  0UL				\
1042 } }
1043 
1044 #define CPU_MASK_CPU0							\
1045 (cpumask_t) { {								\
1046 	[0] =  1UL							\
1047 } }
1048 
1049 #endif /* __LINUX_CPUMASK_H */
1050