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