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