blob: 57f20a0a7794908b47fdb151e530d53e3a598b54 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __LINUX_CPUMASK_H
3#define __LINUX_CPUMASK_H
4
5/*
6 * Cpumasks provide a bitmap suitable for representing the
Rusty Russell6ba2ef72009-09-24 09:34:53 -06007 * set of CPU's in a system, one bit position per CPU number. In general,
8 * only nr_cpu_ids (<= NR_CPUS) bits are valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/kernel.h>
11#include <linux/threads.h>
12#include <linux/bitmap.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050013#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Rusty Russellcdfdef72015-03-05 10:49:19 +103015/* Don't assign or return these: may not be this big! */
Rusty Russell2d3854a2008-11-05 13:39:10 +110016typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Rusty Russellae7a47e2008-12-30 09:05:15 +103018/**
Rusty Russell6ba2ef72009-09-24 09:34:53 -060019 * cpumask_bits - get the bits in a cpumask
20 * @maskp: the struct cpumask *
Rusty Russellae7a47e2008-12-30 09:05:15 +103021 *
Rusty Russell6ba2ef72009-09-24 09:34:53 -060022 * You should only assume nr_cpu_ids bits of this mask are valid. This is
23 * a macro so it's const-correct.
Rusty Russellae7a47e2008-12-30 09:05:15 +103024 */
Rusty Russell6ba2ef72009-09-24 09:34:53 -060025#define cpumask_bits(maskp) ((maskp)->bits)
Paul Jackson7ea931c2008-04-28 02:12:29 -070026
Tejun Heof1bbc032015-02-13 14:36:57 -080027/**
28 * cpumask_pr_args - printf args to output a cpumask
29 * @maskp: cpumask to be printed
30 *
31 * Can be used to provide arguments for '%*pb[l]' when printing a cpumask.
32 */
33#define cpumask_pr_args(maskp) nr_cpu_ids, cpumask_bits(maskp)
34
Mike Travis41df0d612008-05-12 21:21:13 +020035#if NR_CPUS == 1
Alexey Dobriyan9b130ad2017-09-08 16:14:18 -070036#define nr_cpu_ids 1U
Rusty Russell6ba2ef72009-09-24 09:34:53 -060037#else
Alexey Dobriyan9b130ad2017-09-08 16:14:18 -070038extern unsigned int nr_cpu_ids;
Mike Travis41df0d612008-05-12 21:21:13 +020039#endif
40
Rusty Russell6ba2ef72009-09-24 09:34:53 -060041#ifdef CONFIG_CPUMASK_OFFSTACK
42/* Assuming NR_CPUS is huge, a runtime limit is more efficient. Also,
43 * not all bits may be allocated. */
Alexey Dobriyan9b130ad2017-09-08 16:14:18 -070044#define nr_cpumask_bits nr_cpu_ids
Rusty Russell6ba2ef72009-09-24 09:34:53 -060045#else
Alexey Dobriyanc311c792017-05-08 15:56:15 -070046#define nr_cpumask_bits ((unsigned int)NR_CPUS)
Rusty Russell6ba2ef72009-09-24 09:34:53 -060047#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * The following particular system cpumasks and operations manage
Rusty Russellb3199c02008-12-30 09:05:14 +103051 * possible, present, active and online cpus.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 *
Rusty Russellb3199c02008-12-30 09:05:14 +103053 * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
54 * cpu_present_mask - has bit 'cpu' set iff cpu is populated
55 * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler
56 * cpu_active_mask - has bit 'cpu' set iff cpu available to migration
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
Rusty Russellb3199c02008-12-30 09:05:14 +103058 * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 *
Rusty Russellb3199c02008-12-30 09:05:14 +103060 * The cpu_possible_mask is fixed at boot time, as the set of CPU id's
61 * that it is possible might ever be plugged in at anytime during the
62 * life of that system boot. The cpu_present_mask is dynamic(*),
63 * representing which CPUs are currently plugged in. And
64 * cpu_online_mask is the dynamic subset of cpu_present_mask,
65 * indicating those CPUs available for scheduling.
66 *
67 * If HOTPLUG is enabled, then cpu_possible_mask is forced to have
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * all NR_CPUS bits set, otherwise it is just the set of CPUs that
69 * ACPI reports present at boot.
70 *
Rusty Russellb3199c02008-12-30 09:05:14 +103071 * If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * depending on what ACPI reports as currently plugged in, otherwise
Rusty Russellb3199c02008-12-30 09:05:14 +103073 * cpu_present_mask is just a copy of cpu_possible_mask.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 *
Rusty Russellb3199c02008-12-30 09:05:14 +103075 * (*) Well, cpu_present_mask is dynamic in the hotplug case. If not
76 * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 *
78 * Subtleties:
79 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
80 * assumption that their single CPU is online. The UP
Rusty Russellb3199c02008-12-30 09:05:14 +103081 * cpu_{online,possible,present}_masks are placebos. Changing them
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * will have no useful affect on the following num_*_cpus()
83 * and cpu_*() macros in the UP case. This ugliness is a UP
84 * optimization - don't waste any instructions or memory references
85 * asking if you're online or how many CPUs there are if there is
86 * only one CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
88
Rasmus Villemoes4b804c82016-01-20 15:00:19 -080089extern struct cpumask __cpu_possible_mask;
90extern struct cpumask __cpu_online_mask;
91extern struct cpumask __cpu_present_mask;
92extern struct cpumask __cpu_active_mask;
Rasmus Villemoes5aec01b2016-01-20 15:00:25 -080093#define cpu_possible_mask ((const struct cpumask *)&__cpu_possible_mask)
94#define cpu_online_mask ((const struct cpumask *)&__cpu_online_mask)
95#define cpu_present_mask ((const struct cpumask *)&__cpu_present_mask)
96#define cpu_active_mask ((const struct cpumask *)&__cpu_active_mask)
Rusty Russellb3199c02008-12-30 09:05:14 +103097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#if NR_CPUS > 1
Rusty Russellae7a47e2008-12-30 09:05:15 +103099#define num_online_cpus() cpumask_weight(cpu_online_mask)
100#define num_possible_cpus() cpumask_weight(cpu_possible_mask)
101#define num_present_cpus() cpumask_weight(cpu_present_mask)
Peter Zijlstra6ad4c182009-11-25 13:31:39 +0100102#define num_active_cpus() cpumask_weight(cpu_active_mask)
Rusty Russellae7a47e2008-12-30 09:05:15 +1030103#define cpu_online(cpu) cpumask_test_cpu((cpu), cpu_online_mask)
104#define cpu_possible(cpu) cpumask_test_cpu((cpu), cpu_possible_mask)
105#define cpu_present(cpu) cpumask_test_cpu((cpu), cpu_present_mask)
106#define cpu_active(cpu) cpumask_test_cpu((cpu), cpu_active_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#else
Heiko Carstens221e3eb2010-03-05 13:42:41 -0800108#define num_online_cpus() 1U
109#define num_possible_cpus() 1U
110#define num_present_cpus() 1U
111#define num_active_cpus() 1U
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define cpu_online(cpu) ((cpu) == 0)
113#define cpu_possible(cpu) ((cpu) == 0)
114#define cpu_present(cpu) ((cpu) == 0)
Max Krasnyanskye761b772008-07-15 04:43:49 -0700115#define cpu_active(cpu) ((cpu) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#endif
117
Amritha Nambiar80d19662018-06-29 21:26:41 -0700118static inline void cpu_max_bits_warn(unsigned int cpu, unsigned int bits)
119{
120#ifdef CONFIG_DEBUG_PER_CPU_MAPS
121 WARN_ON_ONCE(cpu >= bits);
122#endif /* CONFIG_DEBUG_PER_CPU_MAPS */
123}
124
Rusty Russell2d3854a2008-11-05 13:39:10 +1100125/* verify cpu argument to cpumask_* operators */
126static inline unsigned int cpumask_check(unsigned int cpu)
127{
Amritha Nambiar80d19662018-06-29 21:26:41 -0700128 cpu_max_bits_warn(cpu, nr_cpumask_bits);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100129 return cpu;
130}
131
132#if NR_CPUS == 1
Rusty Russell984f2f32008-11-08 20:24:19 +1100133/* Uniprocessor. Assume all masks are "1". */
134static inline unsigned int cpumask_first(const struct cpumask *srcp)
135{
136 return 0;
137}
138
Rakib Mullicke22cdc32017-10-23 19:01:54 +0600139static inline unsigned int cpumask_last(const struct cpumask *srcp)
140{
141 return 0;
142}
143
Rusty Russell984f2f32008-11-08 20:24:19 +1100144/* Valid inputs for n are -1 and 0. */
145static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
146{
147 return n+1;
148}
149
150static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
151{
152 return n+1;
153}
154
155static inline unsigned int cpumask_next_and(int n,
156 const struct cpumask *srcp,
157 const struct cpumask *andp)
158{
159 return n+1;
160}
161
162/* cpu must be a valid cpu, ie 0, so there's no other choice. */
163static inline unsigned int cpumask_any_but(const struct cpumask *mask,
164 unsigned int cpu)
165{
166 return 1;
167}
Rusty Russell2d3854a2008-11-05 13:39:10 +1100168
Rusty Russellf36963c2015-05-09 03:14:13 +0930169static inline unsigned int cpumask_local_spread(unsigned int i, int node)
Amir Vadaida913092014-06-09 10:24:38 +0300170{
Amir Vadaida913092014-06-09 10:24:38 +0300171 return 0;
172}
173
Rusty Russell2d3854a2008-11-05 13:39:10 +1100174#define for_each_cpu(cpu, mask) \
175 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
Paul E. McKenney8bd93a22010-02-22 17:04:59 -0800176#define for_each_cpu_not(cpu, mask) \
177 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
Michael Kelleyd207af22018-02-14 02:54:03 +0000178#define for_each_cpu_wrap(cpu, mask, start) \
179 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)(start))
Rusty Russell2d3854a2008-11-05 13:39:10 +1100180#define for_each_cpu_and(cpu, mask, and) \
181 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
182#else
183/**
184 * cpumask_first - get the first cpu in a cpumask
185 * @srcp: the cpumask pointer
186 *
187 * Returns >= nr_cpu_ids if no cpus set.
188 */
189static inline unsigned int cpumask_first(const struct cpumask *srcp)
190{
191 return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
192}
193
Rakib Mullicke22cdc32017-10-23 19:01:54 +0600194/**
195 * cpumask_last - get the last CPU in a cpumask
196 * @srcp: - the cpumask pointer
197 *
198 * Returns >= nr_cpumask_bits if no CPUs set.
199 */
200static inline unsigned int cpumask_last(const struct cpumask *srcp)
201{
202 return find_last_bit(cpumask_bits(srcp), nr_cpumask_bits);
203}
204
Alexey Dobriyanf22ef332017-09-08 16:17:15 -0700205unsigned int cpumask_next(int n, const struct cpumask *srcp);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100206
207/**
208 * cpumask_next_zero - get the next unset cpu in a cpumask
209 * @n: the cpu prior to the place to search (ie. return will be > @n)
210 * @srcp: the cpumask pointer
211 *
212 * Returns >= nr_cpu_ids if no further cpus unset.
213 */
214static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
215{
216 /* -1 is a legal arg here. */
217 if (n != -1)
218 cpumask_check(n);
219 return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
220}
221
222int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
223int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
Rusty Russellf36963c2015-05-09 03:14:13 +0930224unsigned int cpumask_local_spread(unsigned int i, int node);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100225
Rusty Russell984f2f32008-11-08 20:24:19 +1100226/**
227 * for_each_cpu - iterate over every cpu in a mask
228 * @cpu: the (optionally unsigned) integer iterator
229 * @mask: the cpumask pointer
230 *
231 * After the loop, cpu is >= nr_cpu_ids.
232 */
Rusty Russell2d3854a2008-11-05 13:39:10 +1100233#define for_each_cpu(cpu, mask) \
234 for ((cpu) = -1; \
235 (cpu) = cpumask_next((cpu), (mask)), \
236 (cpu) < nr_cpu_ids;)
Rusty Russell984f2f32008-11-08 20:24:19 +1100237
238/**
Paul E. McKenney8bd93a22010-02-22 17:04:59 -0800239 * for_each_cpu_not - iterate over every cpu in a complemented mask
240 * @cpu: the (optionally unsigned) integer iterator
241 * @mask: the cpumask pointer
242 *
243 * After the loop, cpu is >= nr_cpu_ids.
244 */
245#define for_each_cpu_not(cpu, mask) \
246 for ((cpu) = -1; \
247 (cpu) = cpumask_next_zero((cpu), (mask)), \
248 (cpu) < nr_cpu_ids;)
249
Peter Zijlstrac743f0a2017-04-14 14:20:05 +0200250extern int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap);
251
252/**
253 * for_each_cpu_wrap - iterate over every cpu in a mask, starting at a specified location
254 * @cpu: the (optionally unsigned) integer iterator
255 * @mask: the cpumask poiter
256 * @start: the start location
257 *
258 * The implementation does not assume any bit in @mask is set (including @start).
259 *
260 * After the loop, cpu is >= nr_cpu_ids.
261 */
262#define for_each_cpu_wrap(cpu, mask, start) \
263 for ((cpu) = cpumask_next_wrap((start)-1, (mask), (start), false); \
264 (cpu) < nr_cpumask_bits; \
265 (cpu) = cpumask_next_wrap((cpu), (mask), (start), true))
266
Paul E. McKenney8bd93a22010-02-22 17:04:59 -0800267/**
Rusty Russell984f2f32008-11-08 20:24:19 +1100268 * for_each_cpu_and - iterate over every cpu in both masks
269 * @cpu: the (optionally unsigned) integer iterator
270 * @mask: the first cpumask pointer
271 * @and: the second cpumask pointer
272 *
273 * This saves a temporary CPU mask in many places. It is equivalent to:
274 * struct cpumask tmp;
275 * cpumask_and(&tmp, &mask, &and);
276 * for_each_cpu(cpu, &tmp)
277 * ...
278 *
279 * After the loop, cpu is >= nr_cpu_ids.
280 */
Rusty Russell2d3854a2008-11-05 13:39:10 +1100281#define for_each_cpu_and(cpu, mask, and) \
282 for ((cpu) = -1; \
283 (cpu) = cpumask_next_and((cpu), (mask), (and)), \
284 (cpu) < nr_cpu_ids;)
285#endif /* SMP */
286
287#define CPU_BITS_NONE \
288{ \
289 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
290}
291
292#define CPU_BITS_CPU0 \
293{ \
294 [0] = 1UL \
295}
296
297/**
298 * cpumask_set_cpu - set a cpu in a cpumask
299 * @cpu: cpu number (< nr_cpu_ids)
300 * @dstp: the cpumask pointer
301 */
302static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
303{
304 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
305}
306
Peter Zijlstra6c8557b2017-05-19 12:58:25 +0200307static inline void __cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
308{
309 __set_bit(cpumask_check(cpu), cpumask_bits(dstp));
310}
311
312
Rusty Russell2d3854a2008-11-05 13:39:10 +1100313/**
314 * cpumask_clear_cpu - clear a cpu in a cpumask
315 * @cpu: cpu number (< nr_cpu_ids)
316 * @dstp: the cpumask pointer
317 */
318static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
319{
320 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
321}
322
Peter Zijlstra6c8557b2017-05-19 12:58:25 +0200323static inline void __cpumask_clear_cpu(int cpu, struct cpumask *dstp)
324{
325 __clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
326}
327
Rusty Russell2d3854a2008-11-05 13:39:10 +1100328/**
329 * cpumask_test_cpu - test for a cpu in a cpumask
330 * @cpu: cpu number (< nr_cpu_ids)
331 * @cpumask: the cpumask pointer
332 *
Alex Shic777ad62012-05-28 22:23:51 +0800333 * Returns 1 if @cpu is set in @cpumask, else returns 0
Rusty Russell2d3854a2008-11-05 13:39:10 +1100334 */
Rasmus Villemoes3bbf7f42015-03-31 13:25:05 +1030335static inline int cpumask_test_cpu(int cpu, const struct cpumask *cpumask)
336{
337 return test_bit(cpumask_check(cpu), cpumask_bits((cpumask)));
338}
Rusty Russell2d3854a2008-11-05 13:39:10 +1100339
340/**
341 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
342 * @cpu: cpu number (< nr_cpu_ids)
343 * @cpumask: the cpumask pointer
344 *
Alex Shic777ad62012-05-28 22:23:51 +0800345 * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0
346 *
Rusty Russell2d3854a2008-11-05 13:39:10 +1100347 * test_and_set_bit wrapper for cpumasks.
348 */
349static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
350{
351 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
352}
353
354/**
Xiao Guangrong54fdade2009-09-22 16:43:39 -0700355 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
356 * @cpu: cpu number (< nr_cpu_ids)
357 * @cpumask: the cpumask pointer
358 *
Alex Shic777ad62012-05-28 22:23:51 +0800359 * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0
360 *
Xiao Guangrong54fdade2009-09-22 16:43:39 -0700361 * test_and_clear_bit wrapper for cpumasks.
362 */
363static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
364{
365 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
366}
367
368/**
Rusty Russell2d3854a2008-11-05 13:39:10 +1100369 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
370 * @dstp: the cpumask pointer
371 */
372static inline void cpumask_setall(struct cpumask *dstp)
373{
374 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
375}
376
377/**
378 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
379 * @dstp: the cpumask pointer
380 */
381static inline void cpumask_clear(struct cpumask *dstp)
382{
383 bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
384}
385
386/**
387 * cpumask_and - *dstp = *src1p & *src2p
388 * @dstp: the cpumask result
389 * @src1p: the first input
390 * @src2p: the second input
Alex Shic777ad62012-05-28 22:23:51 +0800391 *
392 * If *@dstp is empty, returns 0, else returns 1
Rusty Russell2d3854a2008-11-05 13:39:10 +1100393 */
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700394static inline int cpumask_and(struct cpumask *dstp,
Rusty Russell2d3854a2008-11-05 13:39:10 +1100395 const struct cpumask *src1p,
396 const struct cpumask *src2p)
397{
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700398 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
Rusty Russell2d3854a2008-11-05 13:39:10 +1100399 cpumask_bits(src2p), nr_cpumask_bits);
400}
401
402/**
403 * cpumask_or - *dstp = *src1p | *src2p
404 * @dstp: the cpumask result
405 * @src1p: the first input
406 * @src2p: the second input
407 */
408static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
409 const struct cpumask *src2p)
410{
411 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
412 cpumask_bits(src2p), nr_cpumask_bits);
413}
414
415/**
416 * cpumask_xor - *dstp = *src1p ^ *src2p
417 * @dstp: the cpumask result
418 * @src1p: the first input
419 * @src2p: the second input
420 */
421static inline void cpumask_xor(struct cpumask *dstp,
422 const struct cpumask *src1p,
423 const struct cpumask *src2p)
424{
425 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
426 cpumask_bits(src2p), nr_cpumask_bits);
427}
428
429/**
430 * cpumask_andnot - *dstp = *src1p & ~*src2p
431 * @dstp: the cpumask result
432 * @src1p: the first input
433 * @src2p: the second input
Alex Shic777ad62012-05-28 22:23:51 +0800434 *
435 * If *@dstp is empty, returns 0, else returns 1
Rusty Russell2d3854a2008-11-05 13:39:10 +1100436 */
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700437static inline int cpumask_andnot(struct cpumask *dstp,
Rusty Russell2d3854a2008-11-05 13:39:10 +1100438 const struct cpumask *src1p,
439 const struct cpumask *src2p)
440{
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700441 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
Rusty Russell2d3854a2008-11-05 13:39:10 +1100442 cpumask_bits(src2p), nr_cpumask_bits);
443}
444
445/**
446 * cpumask_complement - *dstp = ~*srcp
447 * @dstp: the cpumask result
448 * @srcp: the input to invert
449 */
450static inline void cpumask_complement(struct cpumask *dstp,
451 const struct cpumask *srcp)
452{
453 bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
454 nr_cpumask_bits);
455}
456
457/**
458 * cpumask_equal - *src1p == *src2p
459 * @src1p: the first input
460 * @src2p: the second input
461 */
462static inline bool cpumask_equal(const struct cpumask *src1p,
463 const struct cpumask *src2p)
464{
465 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
466 nr_cpumask_bits);
467}
468
469/**
470 * cpumask_intersects - (*src1p & *src2p) != 0
471 * @src1p: the first input
472 * @src2p: the second input
473 */
474static inline bool cpumask_intersects(const struct cpumask *src1p,
475 const struct cpumask *src2p)
476{
477 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
478 nr_cpumask_bits);
479}
480
481/**
482 * cpumask_subset - (*src1p & ~*src2p) == 0
483 * @src1p: the first input
484 * @src2p: the second input
Alex Shic777ad62012-05-28 22:23:51 +0800485 *
486 * Returns 1 if *@src1p is a subset of *@src2p, else returns 0
Rusty Russell2d3854a2008-11-05 13:39:10 +1100487 */
488static inline int cpumask_subset(const struct cpumask *src1p,
489 const struct cpumask *src2p)
490{
491 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
492 nr_cpumask_bits);
493}
494
495/**
496 * cpumask_empty - *srcp == 0
497 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
498 */
499static inline bool cpumask_empty(const struct cpumask *srcp)
500{
501 return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
502}
503
504/**
505 * cpumask_full - *srcp == 0xFFFFFFFF...
506 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
507 */
508static inline bool cpumask_full(const struct cpumask *srcp)
509{
510 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
511}
512
513/**
514 * cpumask_weight - Count of bits in *srcp
515 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
516 */
517static inline unsigned int cpumask_weight(const struct cpumask *srcp)
518{
519 return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
520}
521
522/**
523 * cpumask_shift_right - *dstp = *srcp >> n
524 * @dstp: the cpumask result
525 * @srcp: the input to shift
526 * @n: the number of bits to shift by
527 */
528static inline void cpumask_shift_right(struct cpumask *dstp,
529 const struct cpumask *srcp, int n)
530{
531 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
532 nr_cpumask_bits);
533}
534
535/**
536 * cpumask_shift_left - *dstp = *srcp << n
537 * @dstp: the cpumask result
538 * @srcp: the input to shift
539 * @n: the number of bits to shift by
540 */
541static inline void cpumask_shift_left(struct cpumask *dstp,
542 const struct cpumask *srcp, int n)
543{
544 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
545 nr_cpumask_bits);
546}
547
548/**
549 * cpumask_copy - *dstp = *srcp
550 * @dstp: the result
551 * @srcp: the input cpumask
552 */
553static inline void cpumask_copy(struct cpumask *dstp,
554 const struct cpumask *srcp)
555{
556 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
557}
558
559/**
560 * cpumask_any - pick a "random" cpu from *srcp
561 * @srcp: the input cpumask
562 *
563 * Returns >= nr_cpu_ids if no cpus set.
564 */
565#define cpumask_any(srcp) cpumask_first(srcp)
566
567/**
568 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
569 * @src1p: the first input
570 * @src2p: the second input
571 *
572 * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
573 */
574#define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
575
576/**
577 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
578 * @mask1: the first input cpumask
579 * @mask2: the second input cpumask
580 *
581 * Returns >= nr_cpu_ids if no cpus set.
582 */
583#define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
584
585/**
Rusty Russellcd83e422008-11-07 11:12:29 +1100586 * cpumask_of - the cpumask containing just a given cpu
587 * @cpu: the cpu (<= nr_cpu_ids)
588 */
589#define cpumask_of(cpu) (get_cpu_mask(cpu))
590
591/**
Rusty Russell29c01772008-12-13 21:20:25 +1030592 * cpumask_parse_user - extract a cpumask from a user string
593 * @buf: the buffer to extract from
594 * @len: the length of the buffer
595 * @dstp: the cpumask to set.
596 *
597 * Returns -errno, or 0 for success.
598 */
599static inline int cpumask_parse_user(const char __user *buf, int len,
600 struct cpumask *dstp)
601{
Tejun Heo4d59b6c2017-02-08 14:30:56 -0800602 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
Rusty Russell29c01772008-12-13 21:20:25 +1030603}
604
605/**
Mike Travis4b0604202011-05-24 17:13:12 -0700606 * cpumask_parselist_user - extract a cpumask from a user string
607 * @buf: the buffer to extract from
608 * @len: the length of the buffer
609 * @dstp: the cpumask to set.
610 *
611 * Returns -errno, or 0 for success.
612 */
613static inline int cpumask_parselist_user(const char __user *buf, int len,
614 struct cpumask *dstp)
615{
616 return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
Tejun Heo4d59b6c2017-02-08 14:30:56 -0800617 nr_cpumask_bits);
Mike Travis4b0604202011-05-24 17:13:12 -0700618}
619
620/**
Geliang Tangb06fb412016-08-02 14:05:42 -0700621 * cpumask_parse - extract a cpumask from a string
Tejun Heoba630e42013-03-12 11:30:04 -0700622 * @buf: the buffer to extract from
623 * @dstp: the cpumask to set.
624 *
625 * Returns -errno, or 0 for success.
626 */
627static inline int cpumask_parse(const char *buf, struct cpumask *dstp)
628{
629 char *nl = strchr(buf, '\n');
Brian W Hartcea092c2014-05-14 10:33:45 +0930630 unsigned int len = nl ? (unsigned int)(nl - buf) : strlen(buf);
Tejun Heoba630e42013-03-12 11:30:04 -0700631
Tejun Heo4d59b6c2017-02-08 14:30:56 -0800632 return bitmap_parse(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
Tejun Heoba630e42013-03-12 11:30:04 -0700633}
634
635/**
Alex Shi231daf02012-07-27 09:29:42 +0930636 * cpulist_parse - extract a cpumask from a user string of ranges
Rusty Russell29c01772008-12-13 21:20:25 +1030637 * @buf: the buffer to extract from
Rusty Russell29c01772008-12-13 21:20:25 +1030638 * @dstp: the cpumask to set.
639 *
640 * Returns -errno, or 0 for success.
641 */
642static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
643{
Tejun Heo4d59b6c2017-02-08 14:30:56 -0800644 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100645}
646
647/**
648 * cpumask_size - size to allocate for a 'struct cpumask' in bytes
Rusty Russell2d3854a2008-11-05 13:39:10 +1100649 */
Alexey Dobriyan4de373a2018-02-06 15:39:37 -0800650static inline unsigned int cpumask_size(void)
Rusty Russell2d3854a2008-11-05 13:39:10 +1100651{
Rusty Russellcdfdef72015-03-05 10:49:19 +1030652 return BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100653}
654
655/*
656 * cpumask_var_t: struct cpumask for stack usage.
657 *
658 * Oh, the wicked games we play! In order to make kernel coding a
659 * little more difficult, we typedef cpumask_var_t to an array or a
660 * pointer: doing &mask on an array is a noop, so it still works.
661 *
662 * ie.
663 * cpumask_var_t tmpmask;
664 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
665 * return -ENOMEM;
666 *
667 * ... use 'tmpmask' like a normal struct cpumask * ...
668 *
669 * free_cpumask_var(tmpmask);
KOSAKI Motohiroa64a26e2011-07-26 16:08:45 -0700670 *
671 *
672 * However, one notable exception is there. alloc_cpumask_var() allocates
673 * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has
674 * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t.
675 *
676 * cpumask_var_t tmpmask;
677 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
678 * return -ENOMEM;
679 *
680 * var = *tmpmask;
681 *
682 * This code makes NR_CPUS length memcopy and brings to a memory corruption.
683 * cpumask_copy() provide safe copy functionality.
Christoph Lameter4ba29682014-08-26 19:12:21 -0500684 *
685 * Note that there is another evil here: If you define a cpumask_var_t
686 * as a percpu variable then the way to obtain the address of the cpumask
687 * structure differently influences what this_cpu_* operation needs to be
688 * used. Please use this_cpu_cpumask_var_t in those cases. The direct use
689 * of this_cpu_ptr() or this_cpu_read() will lead to failures when the
690 * other type of cpumask_var_t implementation is configured.
Waiman Long668802c2017-01-30 12:57:43 -0500691 *
692 * Please also note that __cpumask_var_read_mostly can be used to declare
693 * a cpumask_var_t variable itself (not its content) as read mostly.
Rusty Russell2d3854a2008-11-05 13:39:10 +1100694 */
695#ifdef CONFIG_CPUMASK_OFFSTACK
696typedef struct cpumask *cpumask_var_t;
697
Waiman Long668802c2017-01-30 12:57:43 -0500698#define this_cpu_cpumask_var_ptr(x) this_cpu_read(x)
699#define __cpumask_var_read_mostly __read_mostly
Christoph Lameter4ba29682014-08-26 19:12:21 -0500700
Mike Travis7b4967c2008-12-19 16:56:37 +1030701bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100702bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
Yinghai Lu0281b5d2009-06-06 14:50:36 -0700703bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
704bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100705void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
706void free_cpumask_var(cpumask_var_t mask);
Rusty Russellcd83e422008-11-07 11:12:29 +1100707void free_bootmem_cpumask_var(cpumask_var_t mask);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100708
Matthias Kaehlckef7e30f02017-04-12 11:20:29 -0700709static inline bool cpumask_available(cpumask_var_t mask)
710{
711 return mask != NULL;
712}
713
Rusty Russell2d3854a2008-11-05 13:39:10 +1100714#else
715typedef struct cpumask cpumask_var_t[1];
716
Christoph Lameter4ba29682014-08-26 19:12:21 -0500717#define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x)
Waiman Long668802c2017-01-30 12:57:43 -0500718#define __cpumask_var_read_mostly
Christoph Lameter4ba29682014-08-26 19:12:21 -0500719
Rusty Russell2d3854a2008-11-05 13:39:10 +1100720static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
721{
722 return true;
723}
724
Mike Travis7b4967c2008-12-19 16:56:37 +1030725static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
726 int node)
727{
728 return true;
729}
730
Yinghai Lu0281b5d2009-06-06 14:50:36 -0700731static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
732{
733 cpumask_clear(*mask);
734 return true;
735}
736
737static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
738 int node)
739{
740 cpumask_clear(*mask);
741 return true;
742}
743
Rusty Russell2d3854a2008-11-05 13:39:10 +1100744static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
745{
746}
747
748static inline void free_cpumask_var(cpumask_var_t mask)
749{
750}
Rusty Russellcd83e422008-11-07 11:12:29 +1100751
752static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
753{
754}
Matthias Kaehlckef7e30f02017-04-12 11:20:29 -0700755
756static inline bool cpumask_available(cpumask_var_t mask)
757{
758 return true;
759}
Rusty Russell2d3854a2008-11-05 13:39:10 +1100760#endif /* CONFIG_CPUMASK_OFFSTACK */
761
Rusty Russell2d3854a2008-11-05 13:39:10 +1100762/* It's common to want to use cpu_all_mask in struct member initializers,
763 * so it has to refer to an address rather than a pointer. */
764extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
765#define cpu_all_mask to_cpumask(cpu_all_bits)
766
767/* First bits of cpu_bit_bitmap are in fact unset. */
768#define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
769
Rusty Russellae7a47e2008-12-30 09:05:15 +1030770#define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
771#define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
772#define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
773
Rusty Russell2d3854a2008-11-05 13:39:10 +1100774/* Wrappers for arch boot code to manipulate normally-constant masks */
Rusty Russell3fa41522008-12-30 09:05:16 +1030775void init_cpu_present(const struct cpumask *src);
776void init_cpu_possible(const struct cpumask *src);
777void init_cpu_online(const struct cpumask *src);
Rusty Russell6ba2ef72009-09-24 09:34:53 -0600778
Thomas Gleixner427d77a2016-12-13 19:32:28 +0100779static inline void reset_cpu_possible_mask(void)
780{
781 bitmap_zero(cpumask_bits(&__cpu_possible_mask), NR_CPUS);
782}
783
Rasmus Villemoes94256762016-01-20 15:00:28 -0800784static inline void
785set_cpu_possible(unsigned int cpu, bool possible)
786{
787 if (possible)
788 cpumask_set_cpu(cpu, &__cpu_possible_mask);
789 else
790 cpumask_clear_cpu(cpu, &__cpu_possible_mask);
791}
792
793static inline void
794set_cpu_present(unsigned int cpu, bool present)
795{
796 if (present)
797 cpumask_set_cpu(cpu, &__cpu_present_mask);
798 else
799 cpumask_clear_cpu(cpu, &__cpu_present_mask);
800}
801
802static inline void
803set_cpu_online(unsigned int cpu, bool online)
804{
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +0100805 if (online)
Rasmus Villemoes94256762016-01-20 15:00:28 -0800806 cpumask_set_cpu(cpu, &__cpu_online_mask);
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +0100807 else
Rasmus Villemoes94256762016-01-20 15:00:28 -0800808 cpumask_clear_cpu(cpu, &__cpu_online_mask);
Rasmus Villemoes94256762016-01-20 15:00:28 -0800809}
810
811static inline void
812set_cpu_active(unsigned int cpu, bool active)
813{
814 if (active)
815 cpumask_set_cpu(cpu, &__cpu_active_mask);
816 else
817 cpumask_clear_cpu(cpu, &__cpu_active_mask);
818}
819
820
Rusty Russell6ba2ef72009-09-24 09:34:53 -0600821/**
822 * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
823 * @bitmap: the bitmap
824 *
825 * There are a few places where cpumask_var_t isn't appropriate and
826 * static cpumasks must be used (eg. very early boot), yet we don't
827 * expose the definition of 'struct cpumask'.
828 *
829 * This does the conversion, and can be used as a constant initializer.
830 */
831#define to_cpumask(bitmap) \
832 ((struct cpumask *)(1 ? (bitmap) \
833 : (void *)sizeof(__check_is_bitmap(bitmap))))
834
835static inline int __check_is_bitmap(const unsigned long *bitmap)
836{
837 return 1;
838}
839
840/*
841 * Special-case data structure for "single bit set only" constant CPU masks.
842 *
843 * We pre-generate all the 64 (or 32) possible bit positions, with enough
844 * padding to the left and the right, and return the constant pointer
845 * appropriately offset.
846 */
847extern const unsigned long
848 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
849
850static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
851{
852 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
853 p -= cpu / BITS_PER_LONG;
854 return to_cpumask(p);
855}
856
857#define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
858
859#if NR_CPUS <= BITS_PER_LONG
860#define CPU_BITS_ALL \
861{ \
Rusty Russell9941a382015-03-05 10:49:19 +1030862 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
Rusty Russell6ba2ef72009-09-24 09:34:53 -0600863}
864
865#else /* NR_CPUS > BITS_PER_LONG */
866
867#define CPU_BITS_ALL \
868{ \
869 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
Rusty Russell9941a382015-03-05 10:49:19 +1030870 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
Rusty Russell6ba2ef72009-09-24 09:34:53 -0600871}
872#endif /* NR_CPUS > BITS_PER_LONG */
873
Sudeep Holla5aaba362014-09-30 14:48:22 +0100874/**
875 * cpumap_print_to_pagebuf - copies the cpumask into the buffer either
876 * as comma-separated list of cpus or hex values of cpumask
877 * @list: indicates whether the cpumap must be list
878 * @mask: the cpumask to copy
879 * @buf: the buffer to copy into
880 *
881 * Returns the length of the (null-terminated) @buf string, zero if
882 * nothing is copied.
883 */
884static inline ssize_t
885cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask)
886{
887 return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask),
Tejun Heo513e3d22015-02-13 14:36:50 -0800888 nr_cpu_ids);
Sudeep Holla5aaba362014-09-30 14:48:22 +0100889}
890
Rusty Russell9941a382015-03-05 10:49:19 +1030891#if NR_CPUS <= BITS_PER_LONG
892#define CPU_MASK_ALL \
893(cpumask_t) { { \
894 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
895} }
896#else
897#define CPU_MASK_ALL \
898(cpumask_t) { { \
899 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
900 [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \
901} }
902#endif /* NR_CPUS > BITS_PER_LONG */
903
904#define CPU_MASK_NONE \
905(cpumask_t) { { \
906 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
907} }
908
Rusty Russell15277812015-04-16 12:33:51 +0930909#define CPU_MASK_CPU0 \
910(cpumask_t) { { \
911 [0] = 1UL \
912} }
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914#endif /* __LINUX_CPUMASK_H */