1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef __LINUX_CPUMASK_H
31da177e4SLinus Torvalds #define __LINUX_CPUMASK_H
41da177e4SLinus Torvalds
51da177e4SLinus Torvalds /*
61da177e4SLinus Torvalds * Cpumasks provide a bitmap suitable for representing the
757f728d5SRandy Dunlap * set of CPUs in a system, one bit position per CPU number. In general,
86ba2ef7bSRusty Russell * only nr_cpu_ids (<= NR_CPUS) bits are valid.
91da177e4SLinus Torvalds */
10dcee2280SYury Norov #include <linux/cleanup.h>
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/bitmap.h>
13eb4faa36SYury Norov #include <linux/cpumask_types.h>
140c09ab96SThomas Gleixner #include <linux/atomic.h>
15187f1882SPaul Gortmaker #include <linux/bug.h>
16f0dd891dSYury Norov #include <linux/gfp_types.h>
17f0dd891dSYury Norov #include <linux/numa.h>
181da177e4SLinus Torvalds
19f1bbc032STejun Heo /**
20f1bbc032STejun Heo * cpumask_pr_args - printf args to output a cpumask
21f1bbc032STejun Heo * @maskp: cpumask to be printed
22f1bbc032STejun Heo *
23f1bbc032STejun Heo * Can be used to provide arguments for '%*pb[l]' when printing a cpumask.
24f1bbc032STejun Heo */
25f1bbc032STejun Heo #define cpumask_pr_args(maskp) nr_cpu_ids, cpumask_bits(maskp)
26f1bbc032STejun Heo
276f9c07beSYury Norov #if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS)
286f9c07beSYury Norov #define nr_cpu_ids ((unsigned int)NR_CPUS)
296ba2ef7bSRusty Russell #else
309b130ad5SAlexey Dobriyan extern unsigned int nr_cpu_ids;
316f9c07beSYury Norov #endif
3238bef8e5SYury Norov
set_nr_cpu_ids(unsigned int nr)33ab6b1010SBrian Norris static __always_inline void set_nr_cpu_ids(unsigned int nr)
3438bef8e5SYury Norov {
356f9c07beSYury Norov #if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS)
366f9c07beSYury Norov WARN_ON(nr != nr_cpu_ids);
376f9c07beSYury Norov #else
3838bef8e5SYury Norov nr_cpu_ids = nr;
3941df0d61SMike Travis #endif
406f9c07beSYury Norov }
4141df0d61SMike Travis
42596ff4a0SLinus Torvalds /*
43596ff4a0SLinus Torvalds * We have several different "preferred sizes" for the cpumask
44596ff4a0SLinus Torvalds * operations, depending on operation.
45596ff4a0SLinus Torvalds *
46596ff4a0SLinus Torvalds * For example, the bitmap scanning and operating operations have
47596ff4a0SLinus Torvalds * optimized routines that work for the single-word case, but only when
48596ff4a0SLinus Torvalds * the size is constant. So if NR_CPUS fits in one single word, we are
49596ff4a0SLinus Torvalds * better off using that small constant, in order to trigger the
50596ff4a0SLinus Torvalds * optimized bit finding. That is 'small_cpumask_size'.
51596ff4a0SLinus Torvalds *
52596ff4a0SLinus Torvalds * The clearing and copying operations will similarly perform better
53596ff4a0SLinus Torvalds * with a constant size, but we limit that size arbitrarily to four
54596ff4a0SLinus Torvalds * words. We call this 'large_cpumask_size'.
55596ff4a0SLinus Torvalds *
56596ff4a0SLinus Torvalds * Finally, some operations just want the exact limit, either because
57596ff4a0SLinus Torvalds * they set bits or just don't have any faster fixed-sized versions. We
5880c16b2bSAndy Shevchenko * call this just 'nr_cpumask_bits'.
59596ff4a0SLinus Torvalds *
60596ff4a0SLinus Torvalds * Note that these optional constants are always guaranteed to be at
61596ff4a0SLinus Torvalds * least as big as 'nr_cpu_ids' itself is, and all our cpumask
62596ff4a0SLinus Torvalds * allocations are at least that size (see cpumask_size()). The
63596ff4a0SLinus Torvalds * optimization comes from being able to potentially use a compile-time
64596ff4a0SLinus Torvalds * constant instead of a run-time generated exact number of CPUs.
65596ff4a0SLinus Torvalds */
66596ff4a0SLinus Torvalds #if NR_CPUS <= BITS_PER_LONG
67596ff4a0SLinus Torvalds #define small_cpumask_bits ((unsigned int)NR_CPUS)
68596ff4a0SLinus Torvalds #define large_cpumask_bits ((unsigned int)NR_CPUS)
69596ff4a0SLinus Torvalds #elif NR_CPUS <= 4*BITS_PER_LONG
70596ff4a0SLinus Torvalds #define small_cpumask_bits nr_cpu_ids
71596ff4a0SLinus Torvalds #define large_cpumask_bits ((unsigned int)NR_CPUS)
72596ff4a0SLinus Torvalds #else
73596ff4a0SLinus Torvalds #define small_cpumask_bits nr_cpu_ids
74596ff4a0SLinus Torvalds #define large_cpumask_bits nr_cpu_ids
75596ff4a0SLinus Torvalds #endif
769b130ad5SAlexey Dobriyan #define nr_cpumask_bits nr_cpu_ids
771da177e4SLinus Torvalds
781da177e4SLinus Torvalds /*
791da177e4SLinus Torvalds * The following particular system cpumasks and operations manage
80b3199c02SRusty Russell * possible, present, active and online cpus.
811da177e4SLinus Torvalds *
82b3199c02SRusty Russell * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
83b3199c02SRusty Russell * cpu_present_mask - has bit 'cpu' set iff cpu is populated
844e1a7df4SJames Morse * cpu_enabled_mask - has bit 'cpu' set iff cpu can be brought online
85b3199c02SRusty Russell * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler
86b3199c02SRusty Russell * cpu_active_mask - has bit 'cpu' set iff cpu available to migration
871da177e4SLinus Torvalds *
88b3199c02SRusty Russell * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
891da177e4SLinus Torvalds *
9057f728d5SRandy Dunlap * The cpu_possible_mask is fixed at boot time, as the set of CPU IDs
91b3199c02SRusty Russell * that it is possible might ever be plugged in at anytime during the
92b3199c02SRusty Russell * life of that system boot. The cpu_present_mask is dynamic(*),
93b3199c02SRusty Russell * representing which CPUs are currently plugged in. And
94b3199c02SRusty Russell * cpu_online_mask is the dynamic subset of cpu_present_mask,
95b3199c02SRusty Russell * indicating those CPUs available for scheduling.
96b3199c02SRusty Russell *
97b3199c02SRusty Russell * If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
981da177e4SLinus Torvalds * depending on what ACPI reports as currently plugged in, otherwise
99b3199c02SRusty Russell * cpu_present_mask is just a copy of cpu_possible_mask.
1001da177e4SLinus Torvalds *
101b3199c02SRusty Russell * (*) Well, cpu_present_mask is dynamic in the hotplug case. If not
102b3199c02SRusty Russell * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
1031da177e4SLinus Torvalds *
1041da177e4SLinus Torvalds * Subtleties:
10557f728d5SRandy Dunlap * 1) UP ARCHes (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
1061da177e4SLinus Torvalds * assumption that their single CPU is online. The UP
107b3199c02SRusty Russell * cpu_{online,possible,present}_masks are placebos. Changing them
1081da177e4SLinus Torvalds * will have no useful affect on the following num_*_cpus()
1091da177e4SLinus Torvalds * and cpu_*() macros in the UP case. This ugliness is a UP
1101da177e4SLinus Torvalds * optimization - don't waste any instructions or memory references
1111da177e4SLinus Torvalds * asking if you're online or how many CPUs there are if there is
1121da177e4SLinus Torvalds * only one CPU.
1131da177e4SLinus Torvalds */
1141da177e4SLinus Torvalds
1154b804c85SRasmus Villemoes extern struct cpumask __cpu_possible_mask;
1164b804c85SRasmus Villemoes extern struct cpumask __cpu_online_mask;
1174e1a7df4SJames Morse extern struct cpumask __cpu_enabled_mask;
1184b804c85SRasmus Villemoes extern struct cpumask __cpu_present_mask;
1194b804c85SRasmus Villemoes extern struct cpumask __cpu_active_mask;
120e40f74c5SPeter Zijlstra extern struct cpumask __cpu_dying_mask;
1215aec01b8SRasmus Villemoes #define cpu_possible_mask ((const struct cpumask *)&__cpu_possible_mask)
1225aec01b8SRasmus Villemoes #define cpu_online_mask ((const struct cpumask *)&__cpu_online_mask)
1234e1a7df4SJames Morse #define cpu_enabled_mask ((const struct cpumask *)&__cpu_enabled_mask)
1245aec01b8SRasmus Villemoes #define cpu_present_mask ((const struct cpumask *)&__cpu_present_mask)
1255aec01b8SRasmus Villemoes #define cpu_active_mask ((const struct cpumask *)&__cpu_active_mask)
126e40f74c5SPeter Zijlstra #define cpu_dying_mask ((const struct cpumask *)&__cpu_dying_mask)
127b3199c02SRusty Russell
1280c09ab96SThomas Gleixner extern atomic_t __num_online_cpus;
1290c09ab96SThomas Gleixner
130e797bda3SThomas Gleixner extern cpumask_t cpus_booted_once_mask;
131e797bda3SThomas Gleixner
cpu_max_bits_warn(unsigned int cpu,unsigned int bits)132f5c54f77SBorislav Petkov static __always_inline void cpu_max_bits_warn(unsigned int cpu, unsigned int bits)
13380d19669SAmritha Nambiar {
13480d19669SAmritha Nambiar #ifdef CONFIG_DEBUG_PER_CPU_MAPS
13580d19669SAmritha Nambiar WARN_ON_ONCE(cpu >= bits);
13680d19669SAmritha Nambiar #endif /* CONFIG_DEBUG_PER_CPU_MAPS */
13780d19669SAmritha Nambiar }
13880d19669SAmritha Nambiar
1392d3854a3SRusty Russell /* verify cpu argument to cpumask_* operators */
cpumask_check(unsigned int cpu)140f5c54f77SBorislav Petkov static __always_inline unsigned int cpumask_check(unsigned int cpu)
1412d3854a3SRusty Russell {
142e7304080SLinus Torvalds cpu_max_bits_warn(cpu, small_cpumask_bits);
1432d3854a3SRusty Russell return cpu;
1442d3854a3SRusty Russell }
1452d3854a3SRusty Russell
1462d3854a3SRusty Russell /**
1472d3854a3SRusty Russell * cpumask_first - get the first cpu in a cpumask
1482d3854a3SRusty Russell * @srcp: the cpumask pointer
1492d3854a3SRusty Russell *
15057f728d5SRandy Dunlap * Return: >= nr_cpu_ids if no cpus set.
1512d3854a3SRusty Russell */
cpumask_first(const struct cpumask * srcp)152ab6b1010SBrian Norris static __always_inline unsigned int cpumask_first(const struct cpumask *srcp)
1532d3854a3SRusty Russell {
154596ff4a0SLinus Torvalds return find_first_bit(cpumask_bits(srcp), small_cpumask_bits);
1552d3854a3SRusty Russell }
1562d3854a3SRusty Russell
157e22cdc3fSRakib Mullick /**
1589b51d9d8SYury Norov * cpumask_first_zero - get the first unset cpu in a cpumask
1599b51d9d8SYury Norov * @srcp: the cpumask pointer
1609b51d9d8SYury Norov *
16157f728d5SRandy Dunlap * Return: >= nr_cpu_ids if all cpus are set.
1629b51d9d8SYury Norov */
cpumask_first_zero(const struct cpumask * srcp)163ab6b1010SBrian Norris static __always_inline unsigned int cpumask_first_zero(const struct cpumask *srcp)
1649b51d9d8SYury Norov {
165596ff4a0SLinus Torvalds return find_first_zero_bit(cpumask_bits(srcp), small_cpumask_bits);
1669b51d9d8SYury Norov }
1679b51d9d8SYury Norov
1689b51d9d8SYury Norov /**
16993ba139bSYury Norov * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
170dcb60f9cSRandy Dunlap * @srcp1: the first input
171dcb60f9cSRandy Dunlap * @srcp2: the second input
17293ba139bSYury Norov *
17357f728d5SRandy Dunlap * Return: >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
17493ba139bSYury Norov */
175ab6b1010SBrian Norris static __always_inline
cpumask_first_and(const struct cpumask * srcp1,const struct cpumask * srcp2)17693ba139bSYury Norov unsigned int cpumask_first_and(const struct cpumask *srcp1, const struct cpumask *srcp2)
17793ba139bSYury Norov {
178596ff4a0SLinus Torvalds return find_first_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
17993ba139bSYury Norov }
18093ba139bSYury Norov
18193ba139bSYury Norov /**
182cdc66553SDawei Li * cpumask_first_and_and - return the first cpu from *srcp1 & *srcp2 & *srcp3
183cdc66553SDawei Li * @srcp1: the first input
184cdc66553SDawei Li * @srcp2: the second input
185cdc66553SDawei Li * @srcp3: the third input
186cdc66553SDawei Li *
187cdc66553SDawei Li * Return: >= nr_cpu_ids if no cpus set in all.
188cdc66553SDawei Li */
189ab6b1010SBrian Norris static __always_inline
cpumask_first_and_and(const struct cpumask * srcp1,const struct cpumask * srcp2,const struct cpumask * srcp3)190cdc66553SDawei Li unsigned int cpumask_first_and_and(const struct cpumask *srcp1,
191cdc66553SDawei Li const struct cpumask *srcp2,
192cdc66553SDawei Li const struct cpumask *srcp3)
193cdc66553SDawei Li {
194cdc66553SDawei Li return find_first_and_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
195cdc66553SDawei Li cpumask_bits(srcp3), small_cpumask_bits);
196cdc66553SDawei Li }
197cdc66553SDawei Li
198cdc66553SDawei Li /**
199e22cdc3fSRakib Mullick * cpumask_last - get the last CPU in a cpumask
200e22cdc3fSRakib Mullick * @srcp: - the cpumask pointer
201e22cdc3fSRakib Mullick *
20257f728d5SRandy Dunlap * Return: >= nr_cpumask_bits if no CPUs set.
203e22cdc3fSRakib Mullick */
cpumask_last(const struct cpumask * srcp)204ab6b1010SBrian Norris static __always_inline unsigned int cpumask_last(const struct cpumask *srcp)
205e22cdc3fSRakib Mullick {
206596ff4a0SLinus Torvalds return find_last_bit(cpumask_bits(srcp), small_cpumask_bits);
207e22cdc3fSRakib Mullick }
208e22cdc3fSRakib Mullick
2099b2e7086SYury Norov /**
2109b2e7086SYury Norov * cpumask_next - get the next cpu in a cpumask
21157f728d5SRandy Dunlap * @n: the cpu prior to the place to search (i.e. return will be > @n)
2129b2e7086SYury Norov * @srcp: the cpumask pointer
2139b2e7086SYury Norov *
21457f728d5SRandy Dunlap * Return: >= nr_cpu_ids if no further cpus set.
2159b2e7086SYury Norov */
216ab6b1010SBrian Norris static __always_inline
cpumask_next(int n,const struct cpumask * srcp)2179b2e7086SYury Norov unsigned int cpumask_next(int n, const struct cpumask *srcp)
2189b2e7086SYury Norov {
21980493877STetsuo Handa /* -1 is a legal arg here. */
22080493877STetsuo Handa if (n != -1)
22180493877STetsuo Handa cpumask_check(n);
222596ff4a0SLinus Torvalds return find_next_bit(cpumask_bits(srcp), small_cpumask_bits, n + 1);
2239b2e7086SYury Norov }
2242d3854a3SRusty Russell
2252d3854a3SRusty Russell /**
2262d3854a3SRusty Russell * cpumask_next_zero - get the next unset cpu in a cpumask
22757f728d5SRandy Dunlap * @n: the cpu prior to the place to search (i.e. return will be > @n)
2282d3854a3SRusty Russell * @srcp: the cpumask pointer
2292d3854a3SRusty Russell *
23057f728d5SRandy Dunlap * Return: >= nr_cpu_ids if no further cpus unset.
2312d3854a3SRusty Russell */
232ab6b1010SBrian Norris static __always_inline
cpumask_next_zero(int n,const struct cpumask * srcp)233ab6b1010SBrian Norris unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
2342d3854a3SRusty Russell {
23580493877STetsuo Handa /* -1 is a legal arg here. */
23680493877STetsuo Handa if (n != -1)
23780493877STetsuo Handa cpumask_check(n);
238596ff4a0SLinus Torvalds return find_next_zero_bit(cpumask_bits(srcp), small_cpumask_bits, n+1);
2392d3854a3SRusty Russell }
2402d3854a3SRusty Russell
241b81dce77SSander Vanheule #if NR_CPUS == 1
242b81dce77SSander Vanheule /* Uniprocessor: there is only one valid CPU */
243ab6b1010SBrian Norris static __always_inline
cpumask_local_spread(unsigned int i,int node)244ab6b1010SBrian Norris unsigned int cpumask_local_spread(unsigned int i, int node)
245b81dce77SSander Vanheule {
246b81dce77SSander Vanheule return 0;
247b81dce77SSander Vanheule }
248b81dce77SSander Vanheule
249ab6b1010SBrian Norris static __always_inline
cpumask_any_and_distribute(const struct cpumask * src1p,const struct cpumask * src2p)250ab6b1010SBrian Norris unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
251be599244SSander Vanheule const struct cpumask *src2p)
252be599244SSander Vanheule {
253b81dce77SSander Vanheule return cpumask_first_and(src1p, src2p);
254b81dce77SSander Vanheule }
255b81dce77SSander Vanheule
256ab6b1010SBrian Norris static __always_inline
cpumask_any_distribute(const struct cpumask * srcp)257ab6b1010SBrian Norris unsigned int cpumask_any_distribute(const struct cpumask *srcp)
258b81dce77SSander Vanheule {
259b81dce77SSander Vanheule return cpumask_first(srcp);
260b81dce77SSander Vanheule }
261b81dce77SSander Vanheule #else
262f36963c9SRusty Russell unsigned int cpumask_local_spread(unsigned int i, int node);
2634e23eeebSLinus Torvalds unsigned int cpumask_any_and_distribute(const struct cpumask *src1p,
26446a87b38SPaul Turner const struct cpumask *src2p);
2654e23eeebSLinus Torvalds unsigned int cpumask_any_distribute(const struct cpumask *srcp);
266b81dce77SSander Vanheule #endif /* NR_CPUS */
2672d3854a3SRusty Russell
268984f2f37SRusty Russell /**
2699b2e7086SYury Norov * cpumask_next_and - get the next cpu in *src1p & *src2p
27057f728d5SRandy Dunlap * @n: the cpu prior to the place to search (i.e. return will be > @n)
2719b2e7086SYury Norov * @src1p: the first cpumask pointer
2729b2e7086SYury Norov * @src2p: the second cpumask pointer
2739b2e7086SYury Norov *
27457f728d5SRandy Dunlap * Return: >= nr_cpu_ids if no further cpus set in both.
2759b2e7086SYury Norov */
276ab6b1010SBrian Norris static __always_inline
cpumask_next_and(int n,const struct cpumask * src1p,const struct cpumask * src2p)2779b2e7086SYury Norov unsigned int cpumask_next_and(int n, const struct cpumask *src1p,
2789b2e7086SYury Norov const struct cpumask *src2p)
2799b2e7086SYury Norov {
28080493877STetsuo Handa /* -1 is a legal arg here. */
28180493877STetsuo Handa if (n != -1)
28280493877STetsuo Handa cpumask_check(n);
2839b2e7086SYury Norov return find_next_and_bit(cpumask_bits(src1p), cpumask_bits(src2p),
284596ff4a0SLinus Torvalds small_cpumask_bits, n + 1);
2859b2e7086SYury Norov }
2869b2e7086SYury Norov
287984f2f37SRusty Russell /**
288*3268cb2eSYury Norov * cpumask_next_and_wrap - get the next cpu in *src1p & *src2p, starting from
289*3268cb2eSYury Norov * @n+1. If nothing found, wrap around and start from
290*3268cb2eSYury Norov * the beginning
291*3268cb2eSYury Norov * @n: the cpu prior to the place to search (i.e. search starts from @n+1)
292*3268cb2eSYury Norov * @src1p: the first cpumask pointer
293*3268cb2eSYury Norov * @src2p: the second cpumask pointer
294*3268cb2eSYury Norov *
295*3268cb2eSYury Norov * Return: next set bit, wrapped if needed, or >= nr_cpu_ids if @src1p & @src2p is empty.
296*3268cb2eSYury Norov */
297*3268cb2eSYury Norov static __always_inline
cpumask_next_and_wrap(int n,const struct cpumask * src1p,const struct cpumask * src2p)298*3268cb2eSYury Norov unsigned int cpumask_next_and_wrap(int n, const struct cpumask *src1p,
299*3268cb2eSYury Norov const struct cpumask *src2p)
300*3268cb2eSYury Norov {
301*3268cb2eSYury Norov /* -1 is a legal arg here. */
302*3268cb2eSYury Norov if (n != -1)
303*3268cb2eSYury Norov cpumask_check(n);
304*3268cb2eSYury Norov return find_next_and_bit_wrap(cpumask_bits(src1p), cpumask_bits(src2p),
305*3268cb2eSYury Norov small_cpumask_bits, n + 1);
306*3268cb2eSYury Norov }
307*3268cb2eSYury Norov
308*3268cb2eSYury Norov /**
309*3268cb2eSYury Norov * cpumask_next_wrap - get the next cpu in *src, starting from @n+1. If nothing
310*3268cb2eSYury Norov * found, wrap around and start from the beginning
311*3268cb2eSYury Norov * @n: the cpu prior to the place to search (i.e. search starts from @n+1)
312*3268cb2eSYury Norov * @src: cpumask pointer
313*3268cb2eSYury Norov *
314*3268cb2eSYury Norov * Return: next set bit, wrapped if needed, or >= nr_cpu_ids if @src is empty.
315*3268cb2eSYury Norov */
316*3268cb2eSYury Norov static __always_inline
cpumask_next_wrap(int n,const struct cpumask * src)317*3268cb2eSYury Norov unsigned int cpumask_next_wrap(int n, const struct cpumask *src)
318*3268cb2eSYury Norov {
319*3268cb2eSYury Norov /* -1 is a legal arg here. */
320*3268cb2eSYury Norov if (n != -1)
321*3268cb2eSYury Norov cpumask_check(n);
322*3268cb2eSYury Norov return find_next_bit_wrap(cpumask_bits(src), small_cpumask_bits, n + 1);
323*3268cb2eSYury Norov }
324*3268cb2eSYury Norov
325*3268cb2eSYury Norov /**
326984f2f37SRusty Russell * for_each_cpu - iterate over every cpu in a mask
327984f2f37SRusty Russell * @cpu: the (optionally unsigned) integer iterator
328984f2f37SRusty Russell * @mask: the cpumask pointer
329984f2f37SRusty Russell *
330984f2f37SRusty Russell * After the loop, cpu is >= nr_cpu_ids.
331984f2f37SRusty Russell */
3322d3854a3SRusty Russell #define for_each_cpu(cpu, mask) \
333596ff4a0SLinus Torvalds for_each_set_bit(cpu, cpumask_bits(mask), small_cpumask_bits)
3348bd93a2cSPaul E. McKenney
335c743f0a5SPeter Zijlstra /**
336c743f0a5SPeter Zijlstra * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a specified location
337c743f0a5SPeter Zijlstra * @cpu: the (optionally unsigned) integer iterator
338c23c8082SZhen Lei * @mask: the cpumask pointer
339c743f0a5SPeter Zijlstra * @start: the start location
340c743f0a5SPeter Zijlstra *
341c743f0a5SPeter Zijlstra * The implementation does not assume any bit in @mask is set (including @start).
342c743f0a5SPeter Zijlstra *
343c743f0a5SPeter Zijlstra * After the loop, cpu is >= nr_cpu_ids.
344c743f0a5SPeter Zijlstra */
345c743f0a5SPeter Zijlstra #define for_each_cpu_wrap(cpu, mask, start) \
346596ff4a0SLinus Torvalds for_each_set_bit_wrap(cpu, cpumask_bits(mask), small_cpumask_bits, start)
347c743f0a5SPeter Zijlstra
3488bd93a2cSPaul E. McKenney /**
349984f2f37SRusty Russell * for_each_cpu_and - iterate over every cpu in both masks
350984f2f37SRusty Russell * @cpu: the (optionally unsigned) integer iterator
3512a4a4082SAlexey Dobriyan * @mask1: the first cpumask pointer
3522a4a4082SAlexey Dobriyan * @mask2: the second cpumask pointer
353984f2f37SRusty Russell *
354984f2f37SRusty Russell * This saves a temporary CPU mask in many places. It is equivalent to:
355984f2f37SRusty Russell * struct cpumask tmp;
3562a4a4082SAlexey Dobriyan * cpumask_and(&tmp, &mask1, &mask2);
357984f2f37SRusty Russell * for_each_cpu(cpu, &tmp)
358984f2f37SRusty Russell * ...
359984f2f37SRusty Russell *
360984f2f37SRusty Russell * After the loop, cpu is >= nr_cpu_ids.
361984f2f37SRusty Russell */
3622a4a4082SAlexey Dobriyan #define for_each_cpu_and(cpu, mask1, mask2) \
363596ff4a0SLinus Torvalds for_each_and_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
3642d3854a3SRusty Russell
3659b2e7086SYury Norov /**
3665f75ff29SValentin Schneider * for_each_cpu_andnot - iterate over every cpu present in one mask, excluding
3675f75ff29SValentin Schneider * those present in another.
3685f75ff29SValentin Schneider * @cpu: the (optionally unsigned) integer iterator
3695f75ff29SValentin Schneider * @mask1: the first cpumask pointer
3705f75ff29SValentin Schneider * @mask2: the second cpumask pointer
3715f75ff29SValentin Schneider *
3725f75ff29SValentin Schneider * This saves a temporary CPU mask in many places. It is equivalent to:
3735f75ff29SValentin Schneider * struct cpumask tmp;
3745f75ff29SValentin Schneider * cpumask_andnot(&tmp, &mask1, &mask2);
3755f75ff29SValentin Schneider * for_each_cpu(cpu, &tmp)
3765f75ff29SValentin Schneider * ...
3775f75ff29SValentin Schneider *
3785f75ff29SValentin Schneider * After the loop, cpu is >= nr_cpu_ids.
3795f75ff29SValentin Schneider */
3805f75ff29SValentin Schneider #define for_each_cpu_andnot(cpu, mask1, mask2) \
381596ff4a0SLinus Torvalds for_each_andnot_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
3825f75ff29SValentin Schneider
3835f75ff29SValentin Schneider /**
3841470afefSDave Chinner * for_each_cpu_or - iterate over every cpu present in either mask
3851470afefSDave Chinner * @cpu: the (optionally unsigned) integer iterator
3861470afefSDave Chinner * @mask1: the first cpumask pointer
3871470afefSDave Chinner * @mask2: the second cpumask pointer
3881470afefSDave Chinner *
3891470afefSDave Chinner * This saves a temporary CPU mask in many places. It is equivalent to:
3901470afefSDave Chinner * struct cpumask tmp;
3911470afefSDave Chinner * cpumask_or(&tmp, &mask1, &mask2);
3921470afefSDave Chinner * for_each_cpu(cpu, &tmp)
3931470afefSDave Chinner * ...
3941470afefSDave Chinner *
3951470afefSDave Chinner * After the loop, cpu is >= nr_cpu_ids.
3961470afefSDave Chinner */
3971470afefSDave Chinner #define for_each_cpu_or(cpu, mask1, mask2) \
3981470afefSDave Chinner for_each_or_bit(cpu, cpumask_bits(mask1), cpumask_bits(mask2), small_cpumask_bits)
3991470afefSDave Chinner
4001470afefSDave Chinner /**
4016802f934SKyle Meyer * for_each_cpu_from - iterate over CPUs present in @mask, from @cpu to the end of @mask.
4026802f934SKyle Meyer * @cpu: the (optionally unsigned) integer iterator
4036802f934SKyle Meyer * @mask: the cpumask pointer
4046802f934SKyle Meyer *
4056802f934SKyle Meyer * After the loop, cpu is >= nr_cpu_ids.
4066802f934SKyle Meyer */
4076802f934SKyle Meyer #define for_each_cpu_from(cpu, mask) \
4086802f934SKyle Meyer for_each_set_bit_from(cpu, cpumask_bits(mask), small_cpumask_bits)
4096802f934SKyle Meyer
4106802f934SKyle Meyer /**
411e876695aSI Hsin Cheng * cpumask_any_but - return an arbitrary cpu in a cpumask, but not this one.
4129b2e7086SYury Norov * @mask: the cpumask to search
4139b2e7086SYury Norov * @cpu: the cpu to ignore.
4149b2e7086SYury Norov *
4159b2e7086SYury Norov * Often used to find any cpu but smp_processor_id() in a mask.
41657f728d5SRandy Dunlap * Return: >= nr_cpu_ids if no cpus set.
4179b2e7086SYury Norov */
418ab6b1010SBrian Norris static __always_inline
cpumask_any_but(const struct cpumask * mask,unsigned int cpu)4199b2e7086SYury Norov unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
4209b2e7086SYury Norov {
4219b2e7086SYury Norov unsigned int i;
4229b2e7086SYury Norov
4239b2e7086SYury Norov cpumask_check(cpu);
4249b2e7086SYury Norov for_each_cpu(i, mask)
4259b2e7086SYury Norov if (i != cpu)
4269b2e7086SYury Norov break;
4279b2e7086SYury Norov return i;
4289b2e7086SYury Norov }
4292d3854a3SRusty Russell
430944c417dSYury Norov /**
431e876695aSI Hsin Cheng * cpumask_any_and_but - pick an arbitrary cpu from *mask1 & *mask2, but not this one.
432897fa2c3SMark Rutland * @mask1: the first input cpumask
433897fa2c3SMark Rutland * @mask2: the second input cpumask
434897fa2c3SMark Rutland * @cpu: the cpu to ignore
435897fa2c3SMark Rutland *
436897fa2c3SMark Rutland * Returns >= nr_cpu_ids if no cpus set.
437897fa2c3SMark Rutland */
438ab6b1010SBrian Norris static __always_inline
cpumask_any_and_but(const struct cpumask * mask1,const struct cpumask * mask2,unsigned int cpu)439897fa2c3SMark Rutland unsigned int cpumask_any_and_but(const struct cpumask *mask1,
440897fa2c3SMark Rutland const struct cpumask *mask2,
441897fa2c3SMark Rutland unsigned int cpu)
442897fa2c3SMark Rutland {
443897fa2c3SMark Rutland unsigned int i;
444897fa2c3SMark Rutland
445897fa2c3SMark Rutland cpumask_check(cpu);
446897fa2c3SMark Rutland i = cpumask_first_and(mask1, mask2);
447897fa2c3SMark Rutland if (i != cpu)
448897fa2c3SMark Rutland return i;
449897fa2c3SMark Rutland
450897fa2c3SMark Rutland return cpumask_next_and(cpu, mask1, mask2);
451897fa2c3SMark Rutland }
452897fa2c3SMark Rutland
453897fa2c3SMark Rutland /**
45457f728d5SRandy Dunlap * cpumask_nth - get the Nth cpu in a cpumask
455944c417dSYury Norov * @srcp: the cpumask pointer
45657f728d5SRandy Dunlap * @cpu: the Nth cpu to find, starting from 0
457944c417dSYury Norov *
45857f728d5SRandy Dunlap * Return: >= nr_cpu_ids if such cpu doesn't exist.
459944c417dSYury Norov */
460ab6b1010SBrian Norris static __always_inline
cpumask_nth(unsigned int cpu,const struct cpumask * srcp)461ab6b1010SBrian Norris unsigned int cpumask_nth(unsigned int cpu, const struct cpumask *srcp)
462944c417dSYury Norov {
463596ff4a0SLinus Torvalds return find_nth_bit(cpumask_bits(srcp), small_cpumask_bits, cpumask_check(cpu));
464944c417dSYury Norov }
465944c417dSYury Norov
466944c417dSYury Norov /**
46757f728d5SRandy Dunlap * cpumask_nth_and - get the Nth cpu in 2 cpumasks
468944c417dSYury Norov * @srcp1: the cpumask pointer
469944c417dSYury Norov * @srcp2: the cpumask pointer
47057f728d5SRandy Dunlap * @cpu: the Nth cpu to find, starting from 0
471944c417dSYury Norov *
47257f728d5SRandy Dunlap * Return: >= nr_cpu_ids if such cpu doesn't exist.
473944c417dSYury Norov */
474ab6b1010SBrian Norris static __always_inline
cpumask_nth_and(unsigned int cpu,const struct cpumask * srcp1,const struct cpumask * srcp2)475944c417dSYury Norov unsigned int cpumask_nth_and(unsigned int cpu, const struct cpumask *srcp1,
476944c417dSYury Norov const struct cpumask *srcp2)
477944c417dSYury Norov {
478944c417dSYury Norov return find_nth_and_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
479596ff4a0SLinus Torvalds small_cpumask_bits, cpumask_check(cpu));
480944c417dSYury Norov }
481944c417dSYury Norov
482944c417dSYury Norov /**
48357f728d5SRandy Dunlap * cpumask_nth_andnot - get the Nth cpu set in 1st cpumask, and clear in 2nd.
484944c417dSYury Norov * @srcp1: the cpumask pointer
485944c417dSYury Norov * @srcp2: the cpumask pointer
48657f728d5SRandy Dunlap * @cpu: the Nth cpu to find, starting from 0
487944c417dSYury Norov *
48857f728d5SRandy Dunlap * Return: >= nr_cpu_ids if such cpu doesn't exist.
489944c417dSYury Norov */
490ab6b1010SBrian Norris static __always_inline
cpumask_nth_andnot(unsigned int cpu,const struct cpumask * srcp1,const struct cpumask * srcp2)491944c417dSYury Norov unsigned int cpumask_nth_andnot(unsigned int cpu, const struct cpumask *srcp1,
492944c417dSYury Norov const struct cpumask *srcp2)
493944c417dSYury Norov {
494944c417dSYury Norov return find_nth_andnot_bit(cpumask_bits(srcp1), cpumask_bits(srcp2),
495596ff4a0SLinus Torvalds small_cpumask_bits, cpumask_check(cpu));
496944c417dSYury Norov }
497944c417dSYury Norov
49862f4386eSYury Norov /**
49962f4386eSYury Norov * cpumask_nth_and_andnot - get the Nth cpu set in 1st and 2nd cpumask, and clear in 3rd.
50062f4386eSYury Norov * @srcp1: the cpumask pointer
50162f4386eSYury Norov * @srcp2: the cpumask pointer
50262f4386eSYury Norov * @srcp3: the cpumask pointer
50357f728d5SRandy Dunlap * @cpu: the Nth cpu to find, starting from 0
50462f4386eSYury Norov *
50557f728d5SRandy Dunlap * Return: >= nr_cpu_ids if such cpu doesn't exist.
50662f4386eSYury Norov */
50762f4386eSYury Norov static __always_inline
cpumask_nth_and_andnot(unsigned int cpu,const struct cpumask * srcp1,const struct cpumask * srcp2,const struct cpumask * srcp3)50862f4386eSYury Norov unsigned int cpumask_nth_and_andnot(unsigned int cpu, const struct cpumask *srcp1,
50962f4386eSYury Norov const struct cpumask *srcp2,
51062f4386eSYury Norov const struct cpumask *srcp3)
51162f4386eSYury Norov {
51262f4386eSYury Norov return find_nth_and_andnot_bit(cpumask_bits(srcp1),
51362f4386eSYury Norov cpumask_bits(srcp2),
51462f4386eSYury Norov cpumask_bits(srcp3),
515596ff4a0SLinus Torvalds small_cpumask_bits, cpumask_check(cpu));
51662f4386eSYury Norov }
51762f4386eSYury Norov
5182d3854a3SRusty Russell #define CPU_BITS_NONE \
5192d3854a3SRusty Russell { \
5202d3854a3SRusty Russell [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
5212d3854a3SRusty Russell }
5222d3854a3SRusty Russell
5232d3854a3SRusty Russell #define CPU_BITS_CPU0 \
5242d3854a3SRusty Russell { \
5252d3854a3SRusty Russell [0] = 1UL \
5262d3854a3SRusty Russell }
5272d3854a3SRusty Russell
5282d3854a3SRusty Russell /**
5292d3854a3SRusty Russell * cpumask_set_cpu - set a cpu in a cpumask
5302d3854a3SRusty Russell * @cpu: cpu number (< nr_cpu_ids)
5312d3854a3SRusty Russell * @dstp: the cpumask pointer
5322d3854a3SRusty Russell */
533ab6b1010SBrian Norris static __always_inline
cpumask_set_cpu(unsigned int cpu,struct cpumask * dstp)534ab6b1010SBrian Norris void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
5352d3854a3SRusty Russell {
5362d3854a3SRusty Russell set_bit(cpumask_check(cpu), cpumask_bits(dstp));
5372d3854a3SRusty Russell }
5382d3854a3SRusty Russell
539ab6b1010SBrian Norris static __always_inline
__cpumask_set_cpu(unsigned int cpu,struct cpumask * dstp)540ab6b1010SBrian Norris void __cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
5416c8557bdSPeter Zijlstra {
5426c8557bdSPeter Zijlstra __set_bit(cpumask_check(cpu), cpumask_bits(dstp));
5436c8557bdSPeter Zijlstra }
5446c8557bdSPeter Zijlstra
5456c8557bdSPeter Zijlstra
5462d3854a3SRusty Russell /**
5472d3854a3SRusty Russell * cpumask_clear_cpu - clear a cpu in a cpumask
5482d3854a3SRusty Russell * @cpu: cpu number (< nr_cpu_ids)
5492d3854a3SRusty Russell * @dstp: the cpumask pointer
5502d3854a3SRusty Russell */
cpumask_clear_cpu(int cpu,struct cpumask * dstp)5511dc01abaSBorislav Petkov static __always_inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
5522d3854a3SRusty Russell {
5532d3854a3SRusty Russell clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
5542d3854a3SRusty Russell }
5552d3854a3SRusty Russell
__cpumask_clear_cpu(int cpu,struct cpumask * dstp)5561dc01abaSBorislav Petkov static __always_inline void __cpumask_clear_cpu(int cpu, struct cpumask *dstp)
5576c8557bdSPeter Zijlstra {
5586c8557bdSPeter Zijlstra __clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
5596c8557bdSPeter Zijlstra }
5606c8557bdSPeter Zijlstra
5612d3854a3SRusty Russell /**
562decde1faSCharlie Jenkins * cpumask_assign_cpu - assign a cpu in a cpumask
563decde1faSCharlie Jenkins * @cpu: cpu number (< nr_cpu_ids)
564decde1faSCharlie Jenkins * @dstp: the cpumask pointer
565decde1faSCharlie Jenkins * @bool: the value to assign
566decde1faSCharlie Jenkins */
cpumask_assign_cpu(int cpu,struct cpumask * dstp,bool value)567decde1faSCharlie Jenkins static __always_inline void cpumask_assign_cpu(int cpu, struct cpumask *dstp, bool value)
568decde1faSCharlie Jenkins {
569decde1faSCharlie Jenkins assign_bit(cpumask_check(cpu), cpumask_bits(dstp), value);
570decde1faSCharlie Jenkins }
571decde1faSCharlie Jenkins
__cpumask_assign_cpu(int cpu,struct cpumask * dstp,bool value)572decde1faSCharlie Jenkins static __always_inline void __cpumask_assign_cpu(int cpu, struct cpumask *dstp, bool value)
573decde1faSCharlie Jenkins {
574decde1faSCharlie Jenkins __assign_bit(cpumask_check(cpu), cpumask_bits(dstp), value);
575decde1faSCharlie Jenkins }
576decde1faSCharlie Jenkins
577decde1faSCharlie Jenkins /**
5782d3854a3SRusty Russell * cpumask_test_cpu - test for a cpu in a cpumask
5792d3854a3SRusty Russell * @cpu: cpu number (< nr_cpu_ids)
5802d3854a3SRusty Russell * @cpumask: the cpumask pointer
5812d3854a3SRusty Russell *
58257f728d5SRandy Dunlap * Return: true if @cpu is set in @cpumask, else returns false
5832d3854a3SRusty Russell */
584ab6b1010SBrian Norris static __always_inline
cpumask_test_cpu(int cpu,const struct cpumask * cpumask)585ab6b1010SBrian Norris bool cpumask_test_cpu(int cpu, const struct cpumask *cpumask)
5863bbf7f46SRasmus Villemoes {
5873bbf7f46SRasmus Villemoes return test_bit(cpumask_check(cpu), cpumask_bits((cpumask)));
5883bbf7f46SRasmus Villemoes }
5892d3854a3SRusty Russell
5902d3854a3SRusty Russell /**
5912d3854a3SRusty Russell * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
5922d3854a3SRusty Russell * @cpu: cpu number (< nr_cpu_ids)
5932d3854a3SRusty Russell * @cpumask: the cpumask pointer
5942d3854a3SRusty Russell *
5952d3854a3SRusty Russell * test_and_set_bit wrapper for cpumasks.
59657f728d5SRandy Dunlap *
59757f728d5SRandy Dunlap * Return: true if @cpu is set in old bitmap of @cpumask, else returns false
5982d3854a3SRusty Russell */
599ab6b1010SBrian Norris static __always_inline
cpumask_test_and_set_cpu(int cpu,struct cpumask * cpumask)600ab6b1010SBrian Norris bool cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
6012d3854a3SRusty Russell {
6022d3854a3SRusty Russell return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
6032d3854a3SRusty Russell }
6042d3854a3SRusty Russell
6052d3854a3SRusty Russell /**
60654fdade1SXiao Guangrong * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
60754fdade1SXiao Guangrong * @cpu: cpu number (< nr_cpu_ids)
60854fdade1SXiao Guangrong * @cpumask: the cpumask pointer
60954fdade1SXiao Guangrong *
61054fdade1SXiao Guangrong * test_and_clear_bit wrapper for cpumasks.
61157f728d5SRandy Dunlap *
61257f728d5SRandy Dunlap * Return: true if @cpu is set in old bitmap of @cpumask, else returns false
61354fdade1SXiao Guangrong */
614ab6b1010SBrian Norris static __always_inline
cpumask_test_and_clear_cpu(int cpu,struct cpumask * cpumask)615ab6b1010SBrian Norris bool cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
61654fdade1SXiao Guangrong {
61754fdade1SXiao Guangrong return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
61854fdade1SXiao Guangrong }
61954fdade1SXiao Guangrong
62054fdade1SXiao Guangrong /**
6212d3854a3SRusty Russell * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
6222d3854a3SRusty Russell * @dstp: the cpumask pointer
6232d3854a3SRusty Russell */
cpumask_setall(struct cpumask * dstp)624ab6b1010SBrian Norris static __always_inline void cpumask_setall(struct cpumask *dstp)
6252d3854a3SRusty Russell {
62663355b98SLinus Torvalds if (small_const_nbits(small_cpumask_bits)) {
62763355b98SLinus Torvalds cpumask_bits(dstp)[0] = BITMAP_LAST_WORD_MASK(nr_cpumask_bits);
62863355b98SLinus Torvalds return;
62963355b98SLinus Torvalds }
63063355b98SLinus Torvalds bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
6312d3854a3SRusty Russell }
6322d3854a3SRusty Russell
6332d3854a3SRusty Russell /**
6342d3854a3SRusty Russell * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
6352d3854a3SRusty Russell * @dstp: the cpumask pointer
6362d3854a3SRusty Russell */
cpumask_clear(struct cpumask * dstp)637ab6b1010SBrian Norris static __always_inline void cpumask_clear(struct cpumask *dstp)
6382d3854a3SRusty Russell {
639596ff4a0SLinus Torvalds bitmap_zero(cpumask_bits(dstp), large_cpumask_bits);
6402d3854a3SRusty Russell }
6412d3854a3SRusty Russell
6422d3854a3SRusty Russell /**
6432d3854a3SRusty Russell * cpumask_and - *dstp = *src1p & *src2p
6442d3854a3SRusty Russell * @dstp: the cpumask result
6452d3854a3SRusty Russell * @src1p: the first input
6462d3854a3SRusty Russell * @src2p: the second input
647c777ad69SAlex Shi *
64857f728d5SRandy Dunlap * Return: false if *@dstp is empty, else returns true
6492d3854a3SRusty Russell */
650ab6b1010SBrian Norris static __always_inline
cpumask_and(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)651ab6b1010SBrian Norris bool cpumask_and(struct cpumask *dstp, const struct cpumask *src1p,
6522d3854a3SRusty Russell const struct cpumask *src2p)
6532d3854a3SRusty Russell {
654f4b0373bSLinus Torvalds return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
655596ff4a0SLinus Torvalds cpumask_bits(src2p), small_cpumask_bits);
6562d3854a3SRusty Russell }
6572d3854a3SRusty Russell
6582d3854a3SRusty Russell /**
6592d3854a3SRusty Russell * cpumask_or - *dstp = *src1p | *src2p
6602d3854a3SRusty Russell * @dstp: the cpumask result
6612d3854a3SRusty Russell * @src1p: the first input
6622d3854a3SRusty Russell * @src2p: the second input
6632d3854a3SRusty Russell */
664ab6b1010SBrian Norris static __always_inline
cpumask_or(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)665ab6b1010SBrian Norris void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
6662d3854a3SRusty Russell const struct cpumask *src2p)
6672d3854a3SRusty Russell {
6682d3854a3SRusty Russell bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
669596ff4a0SLinus Torvalds cpumask_bits(src2p), small_cpumask_bits);
6702d3854a3SRusty Russell }
6712d3854a3SRusty Russell
6722d3854a3SRusty Russell /**
6732d3854a3SRusty Russell * cpumask_xor - *dstp = *src1p ^ *src2p
6742d3854a3SRusty Russell * @dstp: the cpumask result
6752d3854a3SRusty Russell * @src1p: the first input
6762d3854a3SRusty Russell * @src2p: the second input
6772d3854a3SRusty Russell */
678ab6b1010SBrian Norris static __always_inline
cpumask_xor(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)679ab6b1010SBrian Norris void cpumask_xor(struct cpumask *dstp, const struct cpumask *src1p,
6802d3854a3SRusty Russell const struct cpumask *src2p)
6812d3854a3SRusty Russell {
6822d3854a3SRusty Russell bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
683596ff4a0SLinus Torvalds cpumask_bits(src2p), small_cpumask_bits);
6842d3854a3SRusty Russell }
6852d3854a3SRusty Russell
6862d3854a3SRusty Russell /**
6872d3854a3SRusty Russell * cpumask_andnot - *dstp = *src1p & ~*src2p
6882d3854a3SRusty Russell * @dstp: the cpumask result
6892d3854a3SRusty Russell * @src1p: the first input
6902d3854a3SRusty Russell * @src2p: the second input
691c777ad69SAlex Shi *
69257f728d5SRandy Dunlap * Return: false if *@dstp is empty, else returns true
6932d3854a3SRusty Russell */
694ab6b1010SBrian Norris static __always_inline
cpumask_andnot(struct cpumask * dstp,const struct cpumask * src1p,const struct cpumask * src2p)695ab6b1010SBrian Norris bool cpumask_andnot(struct cpumask *dstp, const struct cpumask *src1p,
6962d3854a3SRusty Russell const struct cpumask *src2p)
6972d3854a3SRusty Russell {
698f4b0373bSLinus Torvalds return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
699596ff4a0SLinus Torvalds cpumask_bits(src2p), small_cpumask_bits);
7002d3854a3SRusty Russell }
7012d3854a3SRusty Russell
7022d3854a3SRusty Russell /**
7032d3854a3SRusty Russell * cpumask_equal - *src1p == *src2p
7042d3854a3SRusty Russell * @src1p: the first input
7052d3854a3SRusty Russell * @src2p: the second input
70657f728d5SRandy Dunlap *
70757f728d5SRandy Dunlap * Return: true if the cpumasks are equal, false if not
7082d3854a3SRusty Russell */
709ab6b1010SBrian Norris static __always_inline
cpumask_equal(const struct cpumask * src1p,const struct cpumask * src2p)710ab6b1010SBrian Norris bool cpumask_equal(const struct cpumask *src1p, const struct cpumask *src2p)
7112d3854a3SRusty Russell {
7122d3854a3SRusty Russell return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
713596ff4a0SLinus Torvalds small_cpumask_bits);
7142d3854a3SRusty Russell }
7152d3854a3SRusty Russell
7162d3854a3SRusty Russell /**
717b9fa6442SThomas Gleixner * cpumask_or_equal - *src1p | *src2p == *src3p
718b9fa6442SThomas Gleixner * @src1p: the first input
719b9fa6442SThomas Gleixner * @src2p: the second input
720b9fa6442SThomas Gleixner * @src3p: the third input
72157f728d5SRandy Dunlap *
72257f728d5SRandy Dunlap * Return: true if first cpumask ORed with second cpumask == third cpumask,
72357f728d5SRandy Dunlap * otherwise false
724b9fa6442SThomas Gleixner */
725ab6b1010SBrian Norris static __always_inline
cpumask_or_equal(const struct cpumask * src1p,const struct cpumask * src2p,const struct cpumask * src3p)726ab6b1010SBrian Norris bool cpumask_or_equal(const struct cpumask *src1p, const struct cpumask *src2p,
727b9fa6442SThomas Gleixner const struct cpumask *src3p)
728b9fa6442SThomas Gleixner {
729b9fa6442SThomas Gleixner return bitmap_or_equal(cpumask_bits(src1p), cpumask_bits(src2p),
730596ff4a0SLinus Torvalds cpumask_bits(src3p), small_cpumask_bits);
731b9fa6442SThomas Gleixner }
732b9fa6442SThomas Gleixner
733b9fa6442SThomas Gleixner /**
7342d3854a3SRusty Russell * cpumask_intersects - (*src1p & *src2p) != 0
7352d3854a3SRusty Russell * @src1p: the first input
7362d3854a3SRusty Russell * @src2p: the second input
73757f728d5SRandy Dunlap *
73857f728d5SRandy Dunlap * Return: true if first cpumask ANDed with second cpumask is non-empty,
73957f728d5SRandy Dunlap * otherwise false
7402d3854a3SRusty Russell */
741ab6b1010SBrian Norris static __always_inline
cpumask_intersects(const struct cpumask * src1p,const struct cpumask * src2p)742ab6b1010SBrian Norris bool cpumask_intersects(const struct cpumask *src1p, const struct cpumask *src2p)
7432d3854a3SRusty Russell {
7442d3854a3SRusty Russell return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
745596ff4a0SLinus Torvalds small_cpumask_bits);
7462d3854a3SRusty Russell }
7472d3854a3SRusty Russell
7482d3854a3SRusty Russell /**
7492d3854a3SRusty Russell * cpumask_subset - (*src1p & ~*src2p) == 0
7502d3854a3SRusty Russell * @src1p: the first input
7512d3854a3SRusty Russell * @src2p: the second input
752c777ad69SAlex Shi *
75357f728d5SRandy Dunlap * Return: true if *@src1p is a subset of *@src2p, else returns false
7542d3854a3SRusty Russell */
755ab6b1010SBrian Norris static __always_inline
cpumask_subset(const struct cpumask * src1p,const struct cpumask * src2p)756ab6b1010SBrian Norris bool cpumask_subset(const struct cpumask *src1p, const struct cpumask *src2p)
7572d3854a3SRusty Russell {
7582d3854a3SRusty Russell return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
759596ff4a0SLinus Torvalds small_cpumask_bits);
7602d3854a3SRusty Russell }
7612d3854a3SRusty Russell
7622d3854a3SRusty Russell /**
7632d3854a3SRusty Russell * cpumask_empty - *srcp == 0
7642d3854a3SRusty Russell * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
76557f728d5SRandy Dunlap *
76657f728d5SRandy Dunlap * Return: true if srcp is empty (has no bits set), else false
7672d3854a3SRusty Russell */
cpumask_empty(const struct cpumask * srcp)768ab6b1010SBrian Norris static __always_inline bool cpumask_empty(const struct cpumask *srcp)
7692d3854a3SRusty Russell {
770596ff4a0SLinus Torvalds return bitmap_empty(cpumask_bits(srcp), small_cpumask_bits);
7712d3854a3SRusty Russell }
7722d3854a3SRusty Russell
7732d3854a3SRusty Russell /**
7742d3854a3SRusty Russell * cpumask_full - *srcp == 0xFFFFFFFF...
7752d3854a3SRusty Russell * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
77657f728d5SRandy Dunlap *
77757f728d5SRandy Dunlap * Return: true if srcp is full (has all bits set), else false
7782d3854a3SRusty Russell */
cpumask_full(const struct cpumask * srcp)779ab6b1010SBrian Norris static __always_inline bool cpumask_full(const struct cpumask *srcp)
7802d3854a3SRusty Russell {
7812d3854a3SRusty Russell return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
7822d3854a3SRusty Russell }
7832d3854a3SRusty Russell
7842d3854a3SRusty Russell /**
7852d3854a3SRusty Russell * cpumask_weight - Count of bits in *srcp
7862d3854a3SRusty Russell * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
78757f728d5SRandy Dunlap *
78857f728d5SRandy Dunlap * Return: count of bits set in *srcp
7892d3854a3SRusty Russell */
cpumask_weight(const struct cpumask * srcp)790ab6b1010SBrian Norris static __always_inline unsigned int cpumask_weight(const struct cpumask *srcp)
7912d3854a3SRusty Russell {
792596ff4a0SLinus Torvalds return bitmap_weight(cpumask_bits(srcp), small_cpumask_bits);
7932d3854a3SRusty Russell }
7942d3854a3SRusty Russell
7952d3854a3SRusty Russell /**
79624291cafSYury Norov * cpumask_weight_and - Count of bits in (*srcp1 & *srcp2)
79724291cafSYury Norov * @srcp1: the cpumask to count bits (< nr_cpu_ids) in.
79824291cafSYury Norov * @srcp2: the cpumask to count bits (< nr_cpu_ids) in.
79957f728d5SRandy Dunlap *
80057f728d5SRandy Dunlap * Return: count of bits set in both *srcp1 and *srcp2
80124291cafSYury Norov */
802ab6b1010SBrian Norris static __always_inline
cpumask_weight_and(const struct cpumask * srcp1,const struct cpumask * srcp2)803ab6b1010SBrian Norris unsigned int cpumask_weight_and(const struct cpumask *srcp1, const struct cpumask *srcp2)
80424291cafSYury Norov {
805596ff4a0SLinus Torvalds return bitmap_weight_and(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
80624291cafSYury Norov }
80724291cafSYury Norov
80824291cafSYury Norov /**
809c1f5204eSYury Norov * cpumask_weight_andnot - Count of bits in (*srcp1 & ~*srcp2)
810c1f5204eSYury Norov * @srcp1: the cpumask to count bits (< nr_cpu_ids) in.
811c1f5204eSYury Norov * @srcp2: the cpumask to count bits (< nr_cpu_ids) in.
812c1f5204eSYury Norov *
813c1f5204eSYury Norov * Return: count of bits set in both *srcp1 and *srcp2
814c1f5204eSYury Norov */
815ab6b1010SBrian Norris static __always_inline
cpumask_weight_andnot(const struct cpumask * srcp1,const struct cpumask * srcp2)816ab6b1010SBrian Norris unsigned int cpumask_weight_andnot(const struct cpumask *srcp1,
817c1f5204eSYury Norov const struct cpumask *srcp2)
818c1f5204eSYury Norov {
819c1f5204eSYury Norov return bitmap_weight_andnot(cpumask_bits(srcp1), cpumask_bits(srcp2), small_cpumask_bits);
820c1f5204eSYury Norov }
821c1f5204eSYury Norov
822c1f5204eSYury Norov /**
8232d3854a3SRusty Russell * cpumask_shift_right - *dstp = *srcp >> n
8242d3854a3SRusty Russell * @dstp: the cpumask result
8252d3854a3SRusty Russell * @srcp: the input to shift
8262d3854a3SRusty Russell * @n: the number of bits to shift by
8272d3854a3SRusty Russell */
828ab6b1010SBrian Norris static __always_inline
cpumask_shift_right(struct cpumask * dstp,const struct cpumask * srcp,int n)829ab6b1010SBrian Norris void cpumask_shift_right(struct cpumask *dstp, const struct cpumask *srcp, int n)
8302d3854a3SRusty Russell {
8312d3854a3SRusty Russell bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
832596ff4a0SLinus Torvalds small_cpumask_bits);
8332d3854a3SRusty Russell }
8342d3854a3SRusty Russell
8352d3854a3SRusty Russell /**
8362d3854a3SRusty Russell * cpumask_shift_left - *dstp = *srcp << n
8372d3854a3SRusty Russell * @dstp: the cpumask result
8382d3854a3SRusty Russell * @srcp: the input to shift
8392d3854a3SRusty Russell * @n: the number of bits to shift by
8402d3854a3SRusty Russell */
841ab6b1010SBrian Norris static __always_inline
cpumask_shift_left(struct cpumask * dstp,const struct cpumask * srcp,int n)842ab6b1010SBrian Norris void cpumask_shift_left(struct cpumask *dstp, const struct cpumask *srcp, int n)
8432d3854a3SRusty Russell {
8442d3854a3SRusty Russell bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
8452d3854a3SRusty Russell nr_cpumask_bits);
8462d3854a3SRusty Russell }
8472d3854a3SRusty Russell
8482d3854a3SRusty Russell /**
8492d3854a3SRusty Russell * cpumask_copy - *dstp = *srcp
8502d3854a3SRusty Russell * @dstp: the result
8512d3854a3SRusty Russell * @srcp: the input cpumask
8522d3854a3SRusty Russell */
853ab6b1010SBrian Norris static __always_inline
cpumask_copy(struct cpumask * dstp,const struct cpumask * srcp)854ab6b1010SBrian Norris void cpumask_copy(struct cpumask *dstp, const struct cpumask *srcp)
8552d3854a3SRusty Russell {
856596ff4a0SLinus Torvalds bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), large_cpumask_bits);
8572d3854a3SRusty Russell }
8582d3854a3SRusty Russell
8592d3854a3SRusty Russell /**
860e876695aSI Hsin Cheng * cpumask_any - pick an arbitrary cpu from *srcp
8612d3854a3SRusty Russell * @srcp: the input cpumask
8622d3854a3SRusty Russell *
86357f728d5SRandy Dunlap * Return: >= nr_cpu_ids if no cpus set.
8642d3854a3SRusty Russell */
8652d3854a3SRusty Russell #define cpumask_any(srcp) cpumask_first(srcp)
8662d3854a3SRusty Russell
8672d3854a3SRusty Russell /**
868e876695aSI Hsin Cheng * cpumask_any_and - pick an arbitrary cpu from *mask1 & *mask2
8692d3854a3SRusty Russell * @mask1: the first input cpumask
8702d3854a3SRusty Russell * @mask2: the second input cpumask
8712d3854a3SRusty Russell *
87257f728d5SRandy Dunlap * Return: >= nr_cpu_ids if no cpus set.
8732d3854a3SRusty Russell */
8742d3854a3SRusty Russell #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
8752d3854a3SRusty Russell
8762d3854a3SRusty Russell /**
877cd83e42cSRusty Russell * cpumask_of - the cpumask containing just a given cpu
878cd83e42cSRusty Russell * @cpu: the cpu (<= nr_cpu_ids)
879cd83e42cSRusty Russell */
880cd83e42cSRusty Russell #define cpumask_of(cpu) (get_cpu_mask(cpu))
881cd83e42cSRusty Russell
882cd83e42cSRusty Russell /**
88329c0177eSRusty Russell * cpumask_parse_user - extract a cpumask from a user string
88429c0177eSRusty Russell * @buf: the buffer to extract from
88529c0177eSRusty Russell * @len: the length of the buffer
88629c0177eSRusty Russell * @dstp: the cpumask to set.
88729c0177eSRusty Russell *
88857f728d5SRandy Dunlap * Return: -errno, or 0 for success.
88929c0177eSRusty Russell */
890ab6b1010SBrian Norris static __always_inline
cpumask_parse_user(const char __user * buf,int len,struct cpumask * dstp)891ab6b1010SBrian Norris int cpumask_parse_user(const char __user *buf, int len, struct cpumask *dstp)
89229c0177eSRusty Russell {
8934d59b6ccSTejun Heo return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
89429c0177eSRusty Russell }
89529c0177eSRusty Russell
89629c0177eSRusty Russell /**
8974b060420SMike Travis * cpumask_parselist_user - extract a cpumask from a user string
8984b060420SMike Travis * @buf: the buffer to extract from
8994b060420SMike Travis * @len: the length of the buffer
9004b060420SMike Travis * @dstp: the cpumask to set.
9014b060420SMike Travis *
90257f728d5SRandy Dunlap * Return: -errno, or 0 for success.
9034b060420SMike Travis */
904ab6b1010SBrian Norris static __always_inline
cpumask_parselist_user(const char __user * buf,int len,struct cpumask * dstp)905ab6b1010SBrian Norris int cpumask_parselist_user(const char __user *buf, int len, struct cpumask *dstp)
9064b060420SMike Travis {
9074b060420SMike Travis return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
9084d59b6ccSTejun Heo nr_cpumask_bits);
9094b060420SMike Travis }
9104b060420SMike Travis
9114b060420SMike Travis /**
912b06fb415SGeliang Tang * cpumask_parse - extract a cpumask from a string
913ba630e49STejun Heo * @buf: the buffer to extract from
914ba630e49STejun Heo * @dstp: the cpumask to set.
915ba630e49STejun Heo *
91657f728d5SRandy Dunlap * Return: -errno, or 0 for success.
917ba630e49STejun Heo */
cpumask_parse(const char * buf,struct cpumask * dstp)918ab6b1010SBrian Norris static __always_inline int cpumask_parse(const char *buf, struct cpumask *dstp)
919ba630e49STejun Heo {
920190535f7SYury Norov return bitmap_parse(buf, UINT_MAX, cpumask_bits(dstp), nr_cpumask_bits);
921ba630e49STejun Heo }
922ba630e49STejun Heo
923ba630e49STejun Heo /**
924231daf07SAlex Shi * cpulist_parse - extract a cpumask from a user string of ranges
92529c0177eSRusty Russell * @buf: the buffer to extract from
92629c0177eSRusty Russell * @dstp: the cpumask to set.
92729c0177eSRusty Russell *
92857f728d5SRandy Dunlap * Return: -errno, or 0 for success.
92929c0177eSRusty Russell */
cpulist_parse(const char * buf,struct cpumask * dstp)930ab6b1010SBrian Norris static __always_inline int cpulist_parse(const char *buf, struct cpumask *dstp)
93129c0177eSRusty Russell {
9324d59b6ccSTejun Heo return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
9332d3854a3SRusty Russell }
9342d3854a3SRusty Russell
9352d3854a3SRusty Russell /**
93657f728d5SRandy Dunlap * cpumask_size - calculate size to allocate for a 'struct cpumask' in bytes
93757f728d5SRandy Dunlap *
93857f728d5SRandy Dunlap * Return: size to allocate for a &struct cpumask in bytes
9392d3854a3SRusty Russell */
cpumask_size(void)940ab6b1010SBrian Norris static __always_inline unsigned int cpumask_size(void)
9412d3854a3SRusty Russell {
942a37fbe66SAlexander Lobakin return bitmap_size(large_cpumask_bits);
9432d3854a3SRusty Russell }
9442d3854a3SRusty Russell
9452d3854a3SRusty Russell #ifdef CONFIG_CPUMASK_OFFSTACK
9462d3854a3SRusty Russell
9474ba29684SChristoph Lameter #define this_cpu_cpumask_var_ptr(x) this_cpu_read(x)
948668802c2SWaiman Long #define __cpumask_var_read_mostly __read_mostly
9494ba29684SChristoph Lameter
9507b4967c5SMike Travis bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
951f0dd891dSYury Norov
952ab6b1010SBrian Norris static __always_inline
zalloc_cpumask_var_node(cpumask_var_t * mask,gfp_t flags,int node)953f0dd891dSYury Norov bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
954f0dd891dSYury Norov {
955f0dd891dSYury Norov return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node);
956f0dd891dSYury Norov }
957f0dd891dSYury Norov
958f0dd891dSYury Norov /**
959f0dd891dSYury Norov * alloc_cpumask_var - allocate a struct cpumask
960f0dd891dSYury Norov * @mask: pointer to cpumask_var_t where the cpumask is returned
961f0dd891dSYury Norov * @flags: GFP_ flags
962f0dd891dSYury Norov *
963f0dd891dSYury Norov * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
964f0dd891dSYury Norov * a nop returning a constant 1 (in <linux/cpumask.h>).
965f0dd891dSYury Norov *
966f0dd891dSYury Norov * See alloc_cpumask_var_node.
96757f728d5SRandy Dunlap *
96857f728d5SRandy Dunlap * Return: %true if allocation succeeded, %false if not
969f0dd891dSYury Norov */
970ab6b1010SBrian Norris static __always_inline
alloc_cpumask_var(cpumask_var_t * mask,gfp_t flags)971f0dd891dSYury Norov bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
972f0dd891dSYury Norov {
973f0dd891dSYury Norov return alloc_cpumask_var_node(mask, flags, NUMA_NO_NODE);
974f0dd891dSYury Norov }
975f0dd891dSYury Norov
976ab6b1010SBrian Norris static __always_inline
zalloc_cpumask_var(cpumask_var_t * mask,gfp_t flags)977f0dd891dSYury Norov bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
978f0dd891dSYury Norov {
979f0dd891dSYury Norov return alloc_cpumask_var(mask, flags | __GFP_ZERO);
980f0dd891dSYury Norov }
981f0dd891dSYury Norov
9822d3854a3SRusty Russell void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
9832d3854a3SRusty Russell void free_cpumask_var(cpumask_var_t mask);
984cd83e42cSRusty Russell void free_bootmem_cpumask_var(cpumask_var_t mask);
9852d3854a3SRusty Russell
cpumask_available(cpumask_var_t mask)986ab6b1010SBrian Norris static __always_inline bool cpumask_available(cpumask_var_t mask)
987f7e30f01SMatthias Kaehlcke {
988f7e30f01SMatthias Kaehlcke return mask != NULL;
989f7e30f01SMatthias Kaehlcke }
990f7e30f01SMatthias Kaehlcke
9912d3854a3SRusty Russell #else
9922d3854a3SRusty Russell
9934ba29684SChristoph Lameter #define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x)
994668802c2SWaiman Long #define __cpumask_var_read_mostly
9954ba29684SChristoph Lameter
alloc_cpumask_var(cpumask_var_t * mask,gfp_t flags)996ab6b1010SBrian Norris static __always_inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
9972d3854a3SRusty Russell {
9982d3854a3SRusty Russell return true;
9992d3854a3SRusty Russell }
10002d3854a3SRusty Russell
alloc_cpumask_var_node(cpumask_var_t * mask,gfp_t flags,int node)1001ab6b1010SBrian Norris static __always_inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
10027b4967c5SMike Travis int node)
10037b4967c5SMike Travis {
10047b4967c5SMike Travis return true;
10057b4967c5SMike Travis }
10067b4967c5SMike Travis
zalloc_cpumask_var(cpumask_var_t * mask,gfp_t flags)1007ab6b1010SBrian Norris static __always_inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
10080281b5dcSYinghai Lu {
10090281b5dcSYinghai Lu cpumask_clear(*mask);
10100281b5dcSYinghai Lu return true;
10110281b5dcSYinghai Lu }
10120281b5dcSYinghai Lu
zalloc_cpumask_var_node(cpumask_var_t * mask,gfp_t flags,int node)1013ab6b1010SBrian Norris static __always_inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
10140281b5dcSYinghai Lu int node)
10150281b5dcSYinghai Lu {
10160281b5dcSYinghai Lu cpumask_clear(*mask);
10170281b5dcSYinghai Lu return true;
10180281b5dcSYinghai Lu }
10190281b5dcSYinghai Lu
alloc_bootmem_cpumask_var(cpumask_var_t * mask)1020ab6b1010SBrian Norris static __always_inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
10212d3854a3SRusty Russell {
10222d3854a3SRusty Russell }
10232d3854a3SRusty Russell
free_cpumask_var(cpumask_var_t mask)1024ab6b1010SBrian Norris static __always_inline void free_cpumask_var(cpumask_var_t mask)
10252d3854a3SRusty Russell {
10262d3854a3SRusty Russell }
1027cd83e42cSRusty Russell
free_bootmem_cpumask_var(cpumask_var_t mask)1028ab6b1010SBrian Norris static __always_inline void free_bootmem_cpumask_var(cpumask_var_t mask)
1029cd83e42cSRusty Russell {
1030cd83e42cSRusty Russell }
1031f7e30f01SMatthias Kaehlcke
cpumask_available(cpumask_var_t mask)1032ab6b1010SBrian Norris static __always_inline bool cpumask_available(cpumask_var_t mask)
1033f7e30f01SMatthias Kaehlcke {
1034f7e30f01SMatthias Kaehlcke return true;
1035f7e30f01SMatthias Kaehlcke }
10362d3854a3SRusty Russell #endif /* CONFIG_CPUMASK_OFFSTACK */
10372d3854a3SRusty Russell
1038dcee2280SYury Norov DEFINE_FREE(free_cpumask_var, struct cpumask *, if (_T) free_cpumask_var(_T));
1039dcee2280SYury Norov
10402d3854a3SRusty Russell /* It's common to want to use cpu_all_mask in struct member initializers,
10412d3854a3SRusty Russell * so it has to refer to an address rather than a pointer. */
10422d3854a3SRusty Russell extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
10432d3854a3SRusty Russell #define cpu_all_mask to_cpumask(cpu_all_bits)
10442d3854a3SRusty Russell
10452d3854a3SRusty Russell /* First bits of cpu_bit_bitmap are in fact unset. */
10462d3854a3SRusty Russell #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
10472d3854a3SRusty Russell
10484f099030SSander Vanheule #if NR_CPUS == 1
10494f099030SSander Vanheule /* Uniprocessor: the possible/online/present masks are always "1" */
10504f099030SSander Vanheule #define for_each_possible_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
10514f099030SSander Vanheule #define for_each_online_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
10524f099030SSander Vanheule #define for_each_present_cpu(cpu) for ((cpu) = 0; (cpu) < 1; (cpu)++)
10539ffa4b35SYury Norov
10549ffa4b35SYury Norov #define for_each_possible_cpu_wrap(cpu, start) \
10559ffa4b35SYury Norov for ((void)(start), (cpu) = 0; (cpu) < 1; (cpu)++)
10569ffa4b35SYury Norov #define for_each_online_cpu_wrap(cpu, start) \
10579ffa4b35SYury Norov for ((void)(start), (cpu) = 0; (cpu) < 1; (cpu)++)
10584f099030SSander Vanheule #else
1059ae7a47e7SRusty Russell #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
1060ae7a47e7SRusty Russell #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
10614e1a7df4SJames Morse #define for_each_enabled_cpu(cpu) for_each_cpu((cpu), cpu_enabled_mask)
1062ae7a47e7SRusty Russell #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
10639ffa4b35SYury Norov
10649ffa4b35SYury Norov #define for_each_possible_cpu_wrap(cpu, start) \
10659ffa4b35SYury Norov for_each_cpu_wrap((cpu), cpu_possible_mask, (start))
10669ffa4b35SYury Norov #define for_each_online_cpu_wrap(cpu, start) \
10679ffa4b35SYury Norov for_each_cpu_wrap((cpu), cpu_online_mask, (start))
10684f099030SSander Vanheule #endif
1069ae7a47e7SRusty Russell
10702d3854a3SRusty Russell /* Wrappers for arch boot code to manipulate normally-constant masks */
10713fa41520SRusty Russell void init_cpu_present(const struct cpumask *src);
10723fa41520SRusty Russell void init_cpu_possible(const struct cpumask *src);
10736ba2ef7bSRusty Russell
10745c563ee9SYury Norov #define assign_cpu(cpu, mask, val) \
10755c563ee9SYury Norov assign_bit(cpumask_check(cpu), cpumask_bits(mask), (val))
10769425676aSRasmus Villemoes
10775c563ee9SYury Norov #define set_cpu_possible(cpu, possible) assign_cpu((cpu), &__cpu_possible_mask, (possible))
10785819e464SGavin Shan #define set_cpu_enabled(cpu, enabled) assign_cpu((cpu), &__cpu_enabled_mask, (enabled))
10795c563ee9SYury Norov #define set_cpu_present(cpu, present) assign_cpu((cpu), &__cpu_present_mask, (present))
10805c563ee9SYury Norov #define set_cpu_active(cpu, active) assign_cpu((cpu), &__cpu_active_mask, (active))
10815c563ee9SYury Norov #define set_cpu_dying(cpu, dying) assign_cpu((cpu), &__cpu_dying_mask, (dying))
10829425676aSRasmus Villemoes
10830c09ab96SThomas Gleixner void set_cpu_online(unsigned int cpu, bool online);
10849425676aSRasmus Villemoes
10856ba2ef7bSRusty Russell /**
108657f728d5SRandy Dunlap * to_cpumask - convert a NR_CPUS bitmap to a struct cpumask *
10876ba2ef7bSRusty Russell * @bitmap: the bitmap
10886ba2ef7bSRusty Russell *
10896ba2ef7bSRusty Russell * There are a few places where cpumask_var_t isn't appropriate and
10906ba2ef7bSRusty Russell * static cpumasks must be used (eg. very early boot), yet we don't
10916ba2ef7bSRusty Russell * expose the definition of 'struct cpumask'.
10926ba2ef7bSRusty Russell *
10936ba2ef7bSRusty Russell * This does the conversion, and can be used as a constant initializer.
10946ba2ef7bSRusty Russell */
10956ba2ef7bSRusty Russell #define to_cpumask(bitmap) \
10966ba2ef7bSRusty Russell ((struct cpumask *)(1 ? (bitmap) \
10976ba2ef7bSRusty Russell : (void *)sizeof(__check_is_bitmap(bitmap))))
10986ba2ef7bSRusty Russell
__check_is_bitmap(const unsigned long * bitmap)1099ab6b1010SBrian Norris static __always_inline int __check_is_bitmap(const unsigned long *bitmap)
11006ba2ef7bSRusty Russell {
11016ba2ef7bSRusty Russell return 1;
11026ba2ef7bSRusty Russell }
11036ba2ef7bSRusty Russell
11046ba2ef7bSRusty Russell /*
11056ba2ef7bSRusty Russell * Special-case data structure for "single bit set only" constant CPU masks.
11066ba2ef7bSRusty Russell *
11076ba2ef7bSRusty Russell * We pre-generate all the 64 (or 32) possible bit positions, with enough
11086ba2ef7bSRusty Russell * padding to the left and the right, and return the constant pointer
11096ba2ef7bSRusty Russell * appropriately offset.
11106ba2ef7bSRusty Russell */
11116ba2ef7bSRusty Russell extern const unsigned long
11126ba2ef7bSRusty Russell cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
11136ba2ef7bSRusty Russell
get_cpu_mask(unsigned int cpu)1114ab6b1010SBrian Norris static __always_inline const struct cpumask *get_cpu_mask(unsigned int cpu)
11156ba2ef7bSRusty Russell {
11166ba2ef7bSRusty Russell const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
11176ba2ef7bSRusty Russell p -= cpu / BITS_PER_LONG;
11186ba2ef7bSRusty Russell return to_cpumask(p);
11196ba2ef7bSRusty Russell }
11206ba2ef7bSRusty Russell
1121b02a4fd8SPeter Zijlstra #if NR_CPUS > 1
1122b02a4fd8SPeter Zijlstra /**
1123b02a4fd8SPeter Zijlstra * num_online_cpus() - Read the number of online CPUs
1124b02a4fd8SPeter Zijlstra *
1125b02a4fd8SPeter Zijlstra * Despite the fact that __num_online_cpus is of type atomic_t, this
1126b02a4fd8SPeter Zijlstra * interface gives only a momentary snapshot and is not protected against
1127b02a4fd8SPeter Zijlstra * concurrent CPU hotplug operations unless invoked from a cpuhp_lock held
1128b02a4fd8SPeter Zijlstra * region.
112957f728d5SRandy Dunlap *
113057f728d5SRandy Dunlap * Return: momentary snapshot of the number of online CPUs
1131b02a4fd8SPeter Zijlstra */
num_online_cpus(void)11326a123d6aSPeter Zijlstra static __always_inline unsigned int num_online_cpus(void)
1133b02a4fd8SPeter Zijlstra {
11340f613bfaSMark Rutland return raw_atomic_read(&__num_online_cpus);
1135b02a4fd8SPeter Zijlstra }
1136b02a4fd8SPeter Zijlstra #define num_possible_cpus() cpumask_weight(cpu_possible_mask)
11374e1a7df4SJames Morse #define num_enabled_cpus() cpumask_weight(cpu_enabled_mask)
1138b02a4fd8SPeter Zijlstra #define num_present_cpus() cpumask_weight(cpu_present_mask)
1139b02a4fd8SPeter Zijlstra #define num_active_cpus() cpumask_weight(cpu_active_mask)
1140b02a4fd8SPeter Zijlstra
cpu_online(unsigned int cpu)1141ab6b1010SBrian Norris static __always_inline bool cpu_online(unsigned int cpu)
1142b02a4fd8SPeter Zijlstra {
1143b02a4fd8SPeter Zijlstra return cpumask_test_cpu(cpu, cpu_online_mask);
1144b02a4fd8SPeter Zijlstra }
1145b02a4fd8SPeter Zijlstra
cpu_enabled(unsigned int cpu)1146ab6b1010SBrian Norris static __always_inline bool cpu_enabled(unsigned int cpu)
11474e1a7df4SJames Morse {
11484e1a7df4SJames Morse return cpumask_test_cpu(cpu, cpu_enabled_mask);
11494e1a7df4SJames Morse }
11504e1a7df4SJames Morse
cpu_possible(unsigned int cpu)1151ab6b1010SBrian Norris static __always_inline bool cpu_possible(unsigned int cpu)
1152b02a4fd8SPeter Zijlstra {
1153b02a4fd8SPeter Zijlstra return cpumask_test_cpu(cpu, cpu_possible_mask);
1154b02a4fd8SPeter Zijlstra }
1155b02a4fd8SPeter Zijlstra
cpu_present(unsigned int cpu)1156ab6b1010SBrian Norris static __always_inline bool cpu_present(unsigned int cpu)
1157b02a4fd8SPeter Zijlstra {
1158b02a4fd8SPeter Zijlstra return cpumask_test_cpu(cpu, cpu_present_mask);
1159b02a4fd8SPeter Zijlstra }
1160b02a4fd8SPeter Zijlstra
cpu_active(unsigned int cpu)1161ab6b1010SBrian Norris static __always_inline bool cpu_active(unsigned int cpu)
1162b02a4fd8SPeter Zijlstra {
1163b02a4fd8SPeter Zijlstra return cpumask_test_cpu(cpu, cpu_active_mask);
1164b02a4fd8SPeter Zijlstra }
1165b02a4fd8SPeter Zijlstra
cpu_dying(unsigned int cpu)1166ab6b1010SBrian Norris static __always_inline bool cpu_dying(unsigned int cpu)
1167e40f74c5SPeter Zijlstra {
1168e40f74c5SPeter Zijlstra return cpumask_test_cpu(cpu, cpu_dying_mask);
1169e40f74c5SPeter Zijlstra }
1170e40f74c5SPeter Zijlstra
1171b02a4fd8SPeter Zijlstra #else
1172b02a4fd8SPeter Zijlstra
1173b02a4fd8SPeter Zijlstra #define num_online_cpus() 1U
1174b02a4fd8SPeter Zijlstra #define num_possible_cpus() 1U
11754e1a7df4SJames Morse #define num_enabled_cpus() 1U
1176b02a4fd8SPeter Zijlstra #define num_present_cpus() 1U
1177b02a4fd8SPeter Zijlstra #define num_active_cpus() 1U
1178b02a4fd8SPeter Zijlstra
cpu_online(unsigned int cpu)1179ab6b1010SBrian Norris static __always_inline bool cpu_online(unsigned int cpu)
1180b02a4fd8SPeter Zijlstra {
1181b02a4fd8SPeter Zijlstra return cpu == 0;
1182b02a4fd8SPeter Zijlstra }
1183b02a4fd8SPeter Zijlstra
cpu_possible(unsigned int cpu)1184ab6b1010SBrian Norris static __always_inline bool cpu_possible(unsigned int cpu)
1185b02a4fd8SPeter Zijlstra {
1186b02a4fd8SPeter Zijlstra return cpu == 0;
1187b02a4fd8SPeter Zijlstra }
1188b02a4fd8SPeter Zijlstra
cpu_enabled(unsigned int cpu)1189ab6b1010SBrian Norris static __always_inline bool cpu_enabled(unsigned int cpu)
11904e1a7df4SJames Morse {
11914e1a7df4SJames Morse return cpu == 0;
11924e1a7df4SJames Morse }
11934e1a7df4SJames Morse
cpu_present(unsigned int cpu)1194ab6b1010SBrian Norris static __always_inline bool cpu_present(unsigned int cpu)
1195b02a4fd8SPeter Zijlstra {
1196b02a4fd8SPeter Zijlstra return cpu == 0;
1197b02a4fd8SPeter Zijlstra }
1198b02a4fd8SPeter Zijlstra
cpu_active(unsigned int cpu)1199ab6b1010SBrian Norris static __always_inline bool cpu_active(unsigned int cpu)
1200b02a4fd8SPeter Zijlstra {
1201b02a4fd8SPeter Zijlstra return cpu == 0;
1202b02a4fd8SPeter Zijlstra }
1203b02a4fd8SPeter Zijlstra
cpu_dying(unsigned int cpu)1204ab6b1010SBrian Norris static __always_inline bool cpu_dying(unsigned int cpu)
1205e40f74c5SPeter Zijlstra {
1206e40f74c5SPeter Zijlstra return false;
1207e40f74c5SPeter Zijlstra }
1208e40f74c5SPeter Zijlstra
1209b02a4fd8SPeter Zijlstra #endif /* NR_CPUS > 1 */
1210b02a4fd8SPeter Zijlstra
12116ba2ef7bSRusty Russell #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
12126ba2ef7bSRusty Russell
12136ba2ef7bSRusty Russell #if NR_CPUS <= BITS_PER_LONG
12146ba2ef7bSRusty Russell #define CPU_BITS_ALL \
12156ba2ef7bSRusty Russell { \
12169941a383SRusty Russell [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
12176ba2ef7bSRusty Russell }
12186ba2ef7bSRusty Russell
12196ba2ef7bSRusty Russell #else /* NR_CPUS > BITS_PER_LONG */
12206ba2ef7bSRusty Russell
12216ba2ef7bSRusty Russell #define CPU_BITS_ALL \
12226ba2ef7bSRusty Russell { \
12236ba2ef7bSRusty Russell [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
12249941a383SRusty Russell [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
12256ba2ef7bSRusty Russell }
12266ba2ef7bSRusty Russell #endif /* NR_CPUS > BITS_PER_LONG */
12276ba2ef7bSRusty Russell
12285aaba363SSudeep Holla /**
12295aaba363SSudeep Holla * cpumap_print_to_pagebuf - copies the cpumask into the buffer either
12305aaba363SSudeep Holla * as comma-separated list of cpus or hex values of cpumask
12315aaba363SSudeep Holla * @list: indicates whether the cpumap must be list
12325aaba363SSudeep Holla * @mask: the cpumask to copy
12335aaba363SSudeep Holla * @buf: the buffer to copy into
12345aaba363SSudeep Holla *
123557f728d5SRandy Dunlap * Return: the length of the (null-terminated) @buf string, zero if
12365aaba363SSudeep Holla * nothing is copied.
12375aaba363SSudeep Holla */
1238ab6b1010SBrian Norris static __always_inline ssize_t
cpumap_print_to_pagebuf(bool list,char * buf,const struct cpumask * mask)12395aaba363SSudeep Holla cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask)
12405aaba363SSudeep Holla {
12415aaba363SSudeep Holla return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask),
1242513e3d2dSTejun Heo nr_cpu_ids);
12435aaba363SSudeep Holla }
12445aaba363SSudeep Holla
12451fae5629STian Tao /**
12461fae5629STian Tao * cpumap_print_bitmask_to_buf - copies the cpumask into the buffer as
12471fae5629STian Tao * hex values of cpumask
12481fae5629STian Tao *
12491fae5629STian Tao * @buf: the buffer to copy into
12501fae5629STian Tao * @mask: the cpumask to copy
12511fae5629STian Tao * @off: in the string from which we are copying, we copy to @buf
12521fae5629STian Tao * @count: the maximum number of bytes to print
12531fae5629STian Tao *
12541fae5629STian Tao * The function prints the cpumask into the buffer as hex values of
12551fae5629STian Tao * cpumask; Typically used by bin_attribute to export cpumask bitmask
12561fae5629STian Tao * ABI.
12571fae5629STian Tao *
125857f728d5SRandy Dunlap * Return: the length of how many bytes have been copied, excluding
1259c86a2d90STobias Klauser * terminating '\0'.
12601fae5629STian Tao */
1261ab6b1010SBrian Norris static __always_inline
cpumap_print_bitmask_to_buf(char * buf,const struct cpumask * mask,loff_t off,size_t count)1262ab6b1010SBrian Norris ssize_t cpumap_print_bitmask_to_buf(char *buf, const struct cpumask *mask,
12631fae5629STian Tao loff_t off, size_t count)
12641fae5629STian Tao {
12651fae5629STian Tao return bitmap_print_bitmask_to_buf(buf, cpumask_bits(mask),
1266c86a2d90STobias Klauser nr_cpu_ids, off, count) - 1;
12671fae5629STian Tao }
12681fae5629STian Tao
12691fae5629STian Tao /**
12701fae5629STian Tao * cpumap_print_list_to_buf - copies the cpumask into the buffer as
12711fae5629STian Tao * comma-separated list of cpus
1272dcb60f9cSRandy Dunlap * @buf: the buffer to copy into
1273dcb60f9cSRandy Dunlap * @mask: the cpumask to copy
1274dcb60f9cSRandy Dunlap * @off: in the string from which we are copying, we copy to @buf
1275dcb60f9cSRandy Dunlap * @count: the maximum number of bytes to print
12761fae5629STian Tao *
12771fae5629STian Tao * Everything is same with the above cpumap_print_bitmask_to_buf()
12781fae5629STian Tao * except the print format.
127957f728d5SRandy Dunlap *
128057f728d5SRandy Dunlap * Return: the length of how many bytes have been copied, excluding
128157f728d5SRandy Dunlap * terminating '\0'.
12821fae5629STian Tao */
1283ab6b1010SBrian Norris static __always_inline
cpumap_print_list_to_buf(char * buf,const struct cpumask * mask,loff_t off,size_t count)1284ab6b1010SBrian Norris ssize_t cpumap_print_list_to_buf(char *buf, const struct cpumask *mask,
12851fae5629STian Tao loff_t off, size_t count)
12861fae5629STian Tao {
12871fae5629STian Tao return bitmap_print_list_to_buf(buf, cpumask_bits(mask),
1288c86a2d90STobias Klauser nr_cpu_ids, off, count) - 1;
12891fae5629STian Tao }
12901fae5629STian Tao
12919941a383SRusty Russell #if NR_CPUS <= BITS_PER_LONG
12929941a383SRusty Russell #define CPU_MASK_ALL \
12939941a383SRusty Russell (cpumask_t) { { \
12949941a383SRusty Russell [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
12959941a383SRusty Russell } }
12969941a383SRusty Russell #else
12979941a383SRusty Russell #define CPU_MASK_ALL \
12989941a383SRusty Russell (cpumask_t) { { \
12999941a383SRusty Russell [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
13009941a383SRusty Russell [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
13019941a383SRusty Russell } }
13029941a383SRusty Russell #endif /* NR_CPUS > BITS_PER_LONG */
13039941a383SRusty Russell
13049941a383SRusty Russell #define CPU_MASK_NONE \
13059941a383SRusty Russell (cpumask_t) { { \
13069941a383SRusty Russell [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
13079941a383SRusty Russell } }
13089941a383SRusty Russell
13091527781dSRusty Russell #define CPU_MASK_CPU0 \
13101527781dSRusty Russell (cpumask_t) { { \
13111527781dSRusty Russell [0] = 1UL \
13121527781dSRusty Russell } }
13131527781dSRusty Russell
13147ee951acSPhil Auld /*
13157ee951acSPhil Auld * Provide a valid theoretical max size for cpumap and cpulist sysfs files
13167ee951acSPhil Auld * to avoid breaking userspace which may allocate a buffer based on the size
13177ee951acSPhil Auld * reported by e.g. fstat.
13187ee951acSPhil Auld *
13197ee951acSPhil Auld * for cpumap NR_CPUS * 9/32 - 1 should be an exact length.
13207ee951acSPhil Auld *
13217ee951acSPhil Auld * For cpulist 7 is (ceil(log10(NR_CPUS)) + 1) allowing for NR_CPUS to be up
13227ee951acSPhil Auld * to 2 orders of magnitude larger than 8192. And then we divide by 2 to
13237ee951acSPhil Auld * cover a worst-case of every other cpu being on one of two nodes for a
13247ee951acSPhil Auld * very large NR_CPUS.
13257ee951acSPhil Auld *
1326b9be19eeSPhil Auld * Use PAGE_SIZE as a minimum for smaller configurations while avoiding
1327b9be19eeSPhil Auld * unsigned comparison to -1.
13287ee951acSPhil Auld */
1329b9be19eeSPhil Auld #define CPUMAP_FILE_MAX_BYTES (((NR_CPUS * 9)/32 > PAGE_SIZE) \
13307ee951acSPhil Auld ? (NR_CPUS * 9)/32 - 1 : PAGE_SIZE)
13317ee951acSPhil Auld #define CPULIST_FILE_MAX_BYTES (((NR_CPUS * 7)/2 > PAGE_SIZE) ? (NR_CPUS * 7)/2 : PAGE_SIZE)
13327ee951acSPhil Auld
13331da177e4SLinus Torvalds #endif /* __LINUX_CPUMASK_H */
1334