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