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