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