blob: bb1e42ce626e23f75c8c5402766d6ae8a2c96c57 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __LINUX_CPUMASK_H
2#define __LINUX_CPUMASK_H
3
4/*
5 * Cpumasks provide a bitmap suitable for representing the
Rusty Russell6ba2ef72009-09-24 09:34:53 -06006 * set of CPU's in a system, one bit position per CPU number. In general,
7 * only nr_cpu_ids (<= NR_CPUS) bits are valid.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/kernel.h>
10#include <linux/threads.h>
11#include <linux/bitmap.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050012#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Rusty Russell2d3854a2008-11-05 13:39:10 +110014typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Rusty Russellae7a47e2008-12-30 09:05:15 +103016/**
Rusty Russell6ba2ef72009-09-24 09:34:53 -060017 * cpumask_bits - get the bits in a cpumask
18 * @maskp: the struct cpumask *
Rusty Russellae7a47e2008-12-30 09:05:15 +103019 *
Rusty Russell6ba2ef72009-09-24 09:34:53 -060020 * You should only assume nr_cpu_ids bits of this mask are valid. This is
21 * a macro so it's const-correct.
Rusty Russellae7a47e2008-12-30 09:05:15 +103022 */
Rusty Russell6ba2ef72009-09-24 09:34:53 -060023#define cpumask_bits(maskp) ((maskp)->bits)
Paul Jackson7ea931c2008-04-28 02:12:29 -070024
Mike Travis41df0d612008-05-12 21:21:13 +020025#if NR_CPUS == 1
Mike Travis41df0d612008-05-12 21:21:13 +020026#define nr_cpu_ids 1
Rusty Russell6ba2ef72009-09-24 09:34:53 -060027#else
Mike Travis41df0d612008-05-12 21:21:13 +020028extern int nr_cpu_ids;
Mike Travis41df0d612008-05-12 21:21:13 +020029#endif
30
Rusty Russell6ba2ef72009-09-24 09:34:53 -060031#ifdef CONFIG_CPUMASK_OFFSTACK
32/* Assuming NR_CPUS is huge, a runtime limit is more efficient. Also,
33 * not all bits may be allocated. */
34#define nr_cpumask_bits nr_cpu_ids
35#else
36#define nr_cpumask_bits NR_CPUS
37#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * The following particular system cpumasks and operations manage
Rusty Russellb3199c02008-12-30 09:05:14 +103041 * possible, present, active and online cpus.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 *
Rusty Russellb3199c02008-12-30 09:05:14 +103043 * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
44 * cpu_present_mask - has bit 'cpu' set iff cpu is populated
45 * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler
46 * cpu_active_mask - has bit 'cpu' set iff cpu available to migration
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 *
Rusty Russellb3199c02008-12-30 09:05:14 +103048 * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 *
Rusty Russellb3199c02008-12-30 09:05:14 +103050 * The cpu_possible_mask is fixed at boot time, as the set of CPU id's
51 * that it is possible might ever be plugged in at anytime during the
52 * life of that system boot. The cpu_present_mask is dynamic(*),
53 * representing which CPUs are currently plugged in. And
54 * cpu_online_mask is the dynamic subset of cpu_present_mask,
55 * indicating those CPUs available for scheduling.
56 *
57 * If HOTPLUG is enabled, then cpu_possible_mask is forced to have
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * all NR_CPUS bits set, otherwise it is just the set of CPUs that
59 * ACPI reports present at boot.
60 *
Rusty Russellb3199c02008-12-30 09:05:14 +103061 * If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * depending on what ACPI reports as currently plugged in, otherwise
Rusty Russellb3199c02008-12-30 09:05:14 +103063 * cpu_present_mask is just a copy of cpu_possible_mask.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 *
Rusty Russellb3199c02008-12-30 09:05:14 +103065 * (*) Well, cpu_present_mask is dynamic in the hotplug case. If not
66 * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 *
68 * Subtleties:
69 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
70 * assumption that their single CPU is online. The UP
Rusty Russellb3199c02008-12-30 09:05:14 +103071 * cpu_{online,possible,present}_masks are placebos. Changing them
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * will have no useful affect on the following num_*_cpus()
73 * and cpu_*() macros in the UP case. This ugliness is a UP
74 * optimization - don't waste any instructions or memory references
75 * asking if you're online or how many CPUs there are if there is
76 * only one CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 */
78
Rusty Russellb3199c02008-12-30 09:05:14 +103079extern const struct cpumask *const cpu_possible_mask;
80extern const struct cpumask *const cpu_online_mask;
81extern const struct cpumask *const cpu_present_mask;
82extern const struct cpumask *const cpu_active_mask;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#if NR_CPUS > 1
Rusty Russellae7a47e2008-12-30 09:05:15 +103085#define num_online_cpus() cpumask_weight(cpu_online_mask)
86#define num_possible_cpus() cpumask_weight(cpu_possible_mask)
87#define num_present_cpus() cpumask_weight(cpu_present_mask)
Peter Zijlstra6ad4c182009-11-25 13:31:39 +010088#define num_active_cpus() cpumask_weight(cpu_active_mask)
Rusty Russellae7a47e2008-12-30 09:05:15 +103089#define cpu_online(cpu) cpumask_test_cpu((cpu), cpu_online_mask)
90#define cpu_possible(cpu) cpumask_test_cpu((cpu), cpu_possible_mask)
91#define cpu_present(cpu) cpumask_test_cpu((cpu), cpu_present_mask)
92#define cpu_active(cpu) cpumask_test_cpu((cpu), cpu_active_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#else
Heiko Carstens221e3eb2010-03-05 13:42:41 -080094#define num_online_cpus() 1U
95#define num_possible_cpus() 1U
96#define num_present_cpus() 1U
97#define num_active_cpus() 1U
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define cpu_online(cpu) ((cpu) == 0)
99#define cpu_possible(cpu) ((cpu) == 0)
100#define cpu_present(cpu) ((cpu) == 0)
Max Krasnyanskye761b772008-07-15 04:43:49 -0700101#define cpu_active(cpu) ((cpu) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#endif
103
Rusty Russell2d3854a2008-11-05 13:39:10 +1100104/* verify cpu argument to cpumask_* operators */
105static inline unsigned int cpumask_check(unsigned int cpu)
106{
107#ifdef CONFIG_DEBUG_PER_CPU_MAPS
108 WARN_ON_ONCE(cpu >= nr_cpumask_bits);
109#endif /* CONFIG_DEBUG_PER_CPU_MAPS */
110 return cpu;
111}
112
113#if NR_CPUS == 1
Rusty Russell984f2f32008-11-08 20:24:19 +1100114/* Uniprocessor. Assume all masks are "1". */
115static inline unsigned int cpumask_first(const struct cpumask *srcp)
116{
117 return 0;
118}
119
120/* Valid inputs for n are -1 and 0. */
121static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
122{
123 return n+1;
124}
125
126static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
127{
128 return n+1;
129}
130
131static inline unsigned int cpumask_next_and(int n,
132 const struct cpumask *srcp,
133 const struct cpumask *andp)
134{
135 return n+1;
136}
137
138/* cpu must be a valid cpu, ie 0, so there's no other choice. */
139static inline unsigned int cpumask_any_but(const struct cpumask *mask,
140 unsigned int cpu)
141{
142 return 1;
143}
Rusty Russell2d3854a2008-11-05 13:39:10 +1100144
Rusty Russell706470a2015-05-09 03:14:13 +0930145static inline unsigned int cpumask_local_spread(unsigned int i, int node)
Amir Vadaida913092014-06-09 10:24:38 +0300146{
Amir Vadaida913092014-06-09 10:24:38 +0300147 return 0;
148}
149
Rusty Russell2d3854a2008-11-05 13:39:10 +1100150#define for_each_cpu(cpu, mask) \
151 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
Paul E. McKenney8bd93a22010-02-22 17:04:59 -0800152#define for_each_cpu_not(cpu, mask) \
153 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
Rusty Russell2d3854a2008-11-05 13:39:10 +1100154#define for_each_cpu_and(cpu, mask, and) \
155 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
156#else
157/**
158 * cpumask_first - get the first cpu in a cpumask
159 * @srcp: the cpumask pointer
160 *
161 * Returns >= nr_cpu_ids if no cpus set.
162 */
163static inline unsigned int cpumask_first(const struct cpumask *srcp)
164{
165 return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
166}
167
168/**
169 * cpumask_next - get the next cpu in a cpumask
170 * @n: the cpu prior to the place to search (ie. return will be > @n)
171 * @srcp: the cpumask pointer
172 *
173 * Returns >= nr_cpu_ids if no further cpus set.
174 */
175static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
176{
177 /* -1 is a legal arg here. */
178 if (n != -1)
179 cpumask_check(n);
180 return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
181}
182
183/**
184 * cpumask_next_zero - get the next unset cpu in a cpumask
185 * @n: the cpu prior to the place to search (ie. return will be > @n)
186 * @srcp: the cpumask pointer
187 *
188 * Returns >= nr_cpu_ids if no further cpus unset.
189 */
190static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
191{
192 /* -1 is a legal arg here. */
193 if (n != -1)
194 cpumask_check(n);
195 return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
196}
197
198int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
199int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
Rusty Russell706470a2015-05-09 03:14:13 +0930200unsigned int cpumask_local_spread(unsigned int i, int node);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100201
Rusty Russell984f2f32008-11-08 20:24:19 +1100202/**
203 * for_each_cpu - iterate over every cpu in a mask
204 * @cpu: the (optionally unsigned) integer iterator
205 * @mask: the cpumask pointer
206 *
207 * After the loop, cpu is >= nr_cpu_ids.
208 */
Rusty Russell2d3854a2008-11-05 13:39:10 +1100209#define for_each_cpu(cpu, mask) \
210 for ((cpu) = -1; \
211 (cpu) = cpumask_next((cpu), (mask)), \
212 (cpu) < nr_cpu_ids;)
Rusty Russell984f2f32008-11-08 20:24:19 +1100213
214/**
Paul E. McKenney8bd93a22010-02-22 17:04:59 -0800215 * for_each_cpu_not - iterate over every cpu in a complemented mask
216 * @cpu: the (optionally unsigned) integer iterator
217 * @mask: the cpumask pointer
218 *
219 * After the loop, cpu is >= nr_cpu_ids.
220 */
221#define for_each_cpu_not(cpu, mask) \
222 for ((cpu) = -1; \
223 (cpu) = cpumask_next_zero((cpu), (mask)), \
224 (cpu) < nr_cpu_ids;)
225
226/**
Rusty Russell984f2f32008-11-08 20:24:19 +1100227 * for_each_cpu_and - iterate over every cpu in both masks
228 * @cpu: the (optionally unsigned) integer iterator
229 * @mask: the first cpumask pointer
230 * @and: the second cpumask pointer
231 *
232 * This saves a temporary CPU mask in many places. It is equivalent to:
233 * struct cpumask tmp;
234 * cpumask_and(&tmp, &mask, &and);
235 * for_each_cpu(cpu, &tmp)
236 * ...
237 *
238 * After the loop, cpu is >= nr_cpu_ids.
239 */
Rusty Russell2d3854a2008-11-05 13:39:10 +1100240#define for_each_cpu_and(cpu, mask, and) \
241 for ((cpu) = -1; \
242 (cpu) = cpumask_next_and((cpu), (mask), (and)), \
243 (cpu) < nr_cpu_ids;)
244#endif /* SMP */
245
246#define CPU_BITS_NONE \
247{ \
248 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
249}
250
251#define CPU_BITS_CPU0 \
252{ \
253 [0] = 1UL \
254}
255
256/**
257 * cpumask_set_cpu - set a cpu in a cpumask
258 * @cpu: cpu number (< nr_cpu_ids)
259 * @dstp: the cpumask pointer
260 */
261static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
262{
263 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
264}
265
266/**
267 * cpumask_clear_cpu - clear a cpu in a cpumask
268 * @cpu: cpu number (< nr_cpu_ids)
269 * @dstp: the cpumask pointer
270 */
271static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
272{
273 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
274}
275
276/**
277 * cpumask_test_cpu - test for a cpu in a cpumask
278 * @cpu: cpu number (< nr_cpu_ids)
279 * @cpumask: the cpumask pointer
280 *
Alex Shic777ad62012-05-28 22:23:51 +0800281 * Returns 1 if @cpu is set in @cpumask, else returns 0
282 *
Rusty Russell2d3854a2008-11-05 13:39:10 +1100283 * No static inline type checking - see Subtlety (1) above.
284 */
285#define cpumask_test_cpu(cpu, cpumask) \
Rusty Russellae7a47e2008-12-30 09:05:15 +1030286 test_bit(cpumask_check(cpu), cpumask_bits((cpumask)))
Rusty Russell2d3854a2008-11-05 13:39:10 +1100287
288/**
289 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
290 * @cpu: cpu number (< nr_cpu_ids)
291 * @cpumask: the cpumask pointer
292 *
Alex Shic777ad62012-05-28 22:23:51 +0800293 * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0
294 *
Rusty Russell2d3854a2008-11-05 13:39:10 +1100295 * test_and_set_bit wrapper for cpumasks.
296 */
297static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
298{
299 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
300}
301
302/**
Xiao Guangrong54fdade2009-09-22 16:43:39 -0700303 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
304 * @cpu: cpu number (< nr_cpu_ids)
305 * @cpumask: the cpumask pointer
306 *
Alex Shic777ad62012-05-28 22:23:51 +0800307 * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0
308 *
Xiao Guangrong54fdade2009-09-22 16:43:39 -0700309 * test_and_clear_bit wrapper for cpumasks.
310 */
311static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
312{
313 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
314}
315
316/**
Rusty Russell2d3854a2008-11-05 13:39:10 +1100317 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
318 * @dstp: the cpumask pointer
319 */
320static inline void cpumask_setall(struct cpumask *dstp)
321{
322 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
323}
324
325/**
326 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
327 * @dstp: the cpumask pointer
328 */
329static inline void cpumask_clear(struct cpumask *dstp)
330{
331 bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
332}
333
334/**
335 * cpumask_and - *dstp = *src1p & *src2p
336 * @dstp: the cpumask result
337 * @src1p: the first input
338 * @src2p: the second input
Alex Shic777ad62012-05-28 22:23:51 +0800339 *
340 * If *@dstp is empty, returns 0, else returns 1
Rusty Russell2d3854a2008-11-05 13:39:10 +1100341 */
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700342static inline int cpumask_and(struct cpumask *dstp,
Rusty Russell2d3854a2008-11-05 13:39:10 +1100343 const struct cpumask *src1p,
344 const struct cpumask *src2p)
345{
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700346 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
Rusty Russell2d3854a2008-11-05 13:39:10 +1100347 cpumask_bits(src2p), nr_cpumask_bits);
348}
349
350/**
351 * cpumask_or - *dstp = *src1p | *src2p
352 * @dstp: the cpumask result
353 * @src1p: the first input
354 * @src2p: the second input
355 */
356static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
357 const struct cpumask *src2p)
358{
359 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
360 cpumask_bits(src2p), nr_cpumask_bits);
361}
362
363/**
364 * cpumask_xor - *dstp = *src1p ^ *src2p
365 * @dstp: the cpumask result
366 * @src1p: the first input
367 * @src2p: the second input
368 */
369static inline void cpumask_xor(struct cpumask *dstp,
370 const struct cpumask *src1p,
371 const struct cpumask *src2p)
372{
373 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
374 cpumask_bits(src2p), nr_cpumask_bits);
375}
376
377/**
378 * cpumask_andnot - *dstp = *src1p & ~*src2p
379 * @dstp: the cpumask result
380 * @src1p: the first input
381 * @src2p: the second input
Alex Shic777ad62012-05-28 22:23:51 +0800382 *
383 * If *@dstp is empty, returns 0, else returns 1
Rusty Russell2d3854a2008-11-05 13:39:10 +1100384 */
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700385static inline int cpumask_andnot(struct cpumask *dstp,
Rusty Russell2d3854a2008-11-05 13:39:10 +1100386 const struct cpumask *src1p,
387 const struct cpumask *src2p)
388{
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700389 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
Rusty Russell2d3854a2008-11-05 13:39:10 +1100390 cpumask_bits(src2p), nr_cpumask_bits);
391}
392
393/**
394 * cpumask_complement - *dstp = ~*srcp
395 * @dstp: the cpumask result
396 * @srcp: the input to invert
397 */
398static inline void cpumask_complement(struct cpumask *dstp,
399 const struct cpumask *srcp)
400{
401 bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
402 nr_cpumask_bits);
403}
404
405/**
406 * cpumask_equal - *src1p == *src2p
407 * @src1p: the first input
408 * @src2p: the second input
409 */
410static inline bool cpumask_equal(const struct cpumask *src1p,
411 const struct cpumask *src2p)
412{
413 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
414 nr_cpumask_bits);
415}
416
417/**
418 * cpumask_intersects - (*src1p & *src2p) != 0
419 * @src1p: the first input
420 * @src2p: the second input
421 */
422static inline bool cpumask_intersects(const struct cpumask *src1p,
423 const struct cpumask *src2p)
424{
425 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
426 nr_cpumask_bits);
427}
428
429/**
430 * cpumask_subset - (*src1p & ~*src2p) == 0
431 * @src1p: the first input
432 * @src2p: the second input
Alex Shic777ad62012-05-28 22:23:51 +0800433 *
434 * Returns 1 if *@src1p is a subset of *@src2p, else returns 0
Rusty Russell2d3854a2008-11-05 13:39:10 +1100435 */
436static inline int cpumask_subset(const struct cpumask *src1p,
437 const struct cpumask *src2p)
438{
439 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
440 nr_cpumask_bits);
441}
442
443/**
444 * cpumask_empty - *srcp == 0
445 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
446 */
447static inline bool cpumask_empty(const struct cpumask *srcp)
448{
449 return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
450}
451
452/**
453 * cpumask_full - *srcp == 0xFFFFFFFF...
454 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
455 */
456static inline bool cpumask_full(const struct cpumask *srcp)
457{
458 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
459}
460
461/**
462 * cpumask_weight - Count of bits in *srcp
463 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
464 */
465static inline unsigned int cpumask_weight(const struct cpumask *srcp)
466{
467 return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
468}
469
470/**
471 * cpumask_shift_right - *dstp = *srcp >> n
472 * @dstp: the cpumask result
473 * @srcp: the input to shift
474 * @n: the number of bits to shift by
475 */
476static inline void cpumask_shift_right(struct cpumask *dstp,
477 const struct cpumask *srcp, int n)
478{
479 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
480 nr_cpumask_bits);
481}
482
483/**
484 * cpumask_shift_left - *dstp = *srcp << n
485 * @dstp: the cpumask result
486 * @srcp: the input to shift
487 * @n: the number of bits to shift by
488 */
489static inline void cpumask_shift_left(struct cpumask *dstp,
490 const struct cpumask *srcp, int n)
491{
492 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
493 nr_cpumask_bits);
494}
495
496/**
497 * cpumask_copy - *dstp = *srcp
498 * @dstp: the result
499 * @srcp: the input cpumask
500 */
501static inline void cpumask_copy(struct cpumask *dstp,
502 const struct cpumask *srcp)
503{
504 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
505}
506
507/**
508 * cpumask_any - pick a "random" cpu from *srcp
509 * @srcp: the input cpumask
510 *
511 * Returns >= nr_cpu_ids if no cpus set.
512 */
513#define cpumask_any(srcp) cpumask_first(srcp)
514
515/**
516 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
517 * @src1p: the first input
518 * @src2p: the second input
519 *
520 * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
521 */
522#define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
523
524/**
525 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
526 * @mask1: the first input cpumask
527 * @mask2: the second input cpumask
528 *
529 * Returns >= nr_cpu_ids if no cpus set.
530 */
531#define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
532
533/**
Rusty Russellcd83e422008-11-07 11:12:29 +1100534 * cpumask_of - the cpumask containing just a given cpu
535 * @cpu: the cpu (<= nr_cpu_ids)
536 */
537#define cpumask_of(cpu) (get_cpu_mask(cpu))
538
539/**
Rusty Russell29c01772008-12-13 21:20:25 +1030540 * cpumask_scnprintf - print a cpumask into a string as comma-separated hex
541 * @buf: the buffer to sprintf into
542 * @len: the length of the buffer
543 * @srcp: the cpumask to print
544 *
545 * If len is zero, returns zero. Otherwise returns the length of the
546 * (nul-terminated) @buf string.
547 */
548static inline int cpumask_scnprintf(char *buf, int len,
549 const struct cpumask *srcp)
550{
Rusty Russellae7a47e2008-12-30 09:05:15 +1030551 return bitmap_scnprintf(buf, len, cpumask_bits(srcp), nr_cpumask_bits);
Rusty Russell29c01772008-12-13 21:20:25 +1030552}
553
554/**
555 * cpumask_parse_user - extract a cpumask from a user string
556 * @buf: the buffer to extract from
557 * @len: the length of the buffer
558 * @dstp: the cpumask to set.
559 *
560 * Returns -errno, or 0 for success.
561 */
562static inline int cpumask_parse_user(const char __user *buf, int len,
563 struct cpumask *dstp)
564{
Rusty Russellae7a47e2008-12-30 09:05:15 +1030565 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
Rusty Russell29c01772008-12-13 21:20:25 +1030566}
567
568/**
Mike Travis4b060422011-05-24 17:13:12 -0700569 * cpumask_parselist_user - extract a cpumask from a user string
570 * @buf: the buffer to extract from
571 * @len: the length of the buffer
572 * @dstp: the cpumask to set.
573 *
574 * Returns -errno, or 0 for success.
575 */
576static inline int cpumask_parselist_user(const char __user *buf, int len,
577 struct cpumask *dstp)
578{
579 return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
580 nr_cpumask_bits);
581}
582
583/**
Rusty Russell29c01772008-12-13 21:20:25 +1030584 * cpulist_scnprintf - print a cpumask into a string as comma-separated list
585 * @buf: the buffer to sprintf into
586 * @len: the length of the buffer
587 * @srcp: the cpumask to print
588 *
589 * If len is zero, returns zero. Otherwise returns the length of the
590 * (nul-terminated) @buf string.
591 */
592static inline int cpulist_scnprintf(char *buf, int len,
593 const struct cpumask *srcp)
594{
Rusty Russellae7a47e2008-12-30 09:05:15 +1030595 return bitmap_scnlistprintf(buf, len, cpumask_bits(srcp),
596 nr_cpumask_bits);
Rusty Russell29c01772008-12-13 21:20:25 +1030597}
598
599/**
Tejun Heoba630e42013-03-12 11:30:04 -0700600 * cpumask_parse - extract a cpumask from from a string
601 * @buf: the buffer to extract from
602 * @dstp: the cpumask to set.
603 *
604 * Returns -errno, or 0 for success.
605 */
606static inline int cpumask_parse(const char *buf, struct cpumask *dstp)
607{
608 char *nl = strchr(buf, '\n');
Brian W Hartcea092c2014-05-14 10:33:45 +0930609 unsigned int len = nl ? (unsigned int)(nl - buf) : strlen(buf);
Tejun Heoba630e42013-03-12 11:30:04 -0700610
611 return bitmap_parse(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
612}
613
614/**
Alex Shi231daf02012-07-27 09:29:42 +0930615 * cpulist_parse - extract a cpumask from a user string of ranges
Rusty Russell29c01772008-12-13 21:20:25 +1030616 * @buf: the buffer to extract from
Rusty Russell29c01772008-12-13 21:20:25 +1030617 * @dstp: the cpumask to set.
618 *
619 * Returns -errno, or 0 for success.
620 */
621static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
622{
Rusty Russellae7a47e2008-12-30 09:05:15 +1030623 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100624}
625
626/**
627 * cpumask_size - size to allocate for a 'struct cpumask' in bytes
628 *
629 * This will eventually be a runtime variable, depending on nr_cpu_ids.
630 */
631static inline size_t cpumask_size(void)
632{
633 /* FIXME: Once all cpumask assignments are eliminated, this
634 * can be nr_cpumask_bits */
635 return BITS_TO_LONGS(NR_CPUS) * sizeof(long);
636}
637
638/*
639 * cpumask_var_t: struct cpumask for stack usage.
640 *
641 * Oh, the wicked games we play! In order to make kernel coding a
642 * little more difficult, we typedef cpumask_var_t to an array or a
643 * pointer: doing &mask on an array is a noop, so it still works.
644 *
645 * ie.
646 * cpumask_var_t tmpmask;
647 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
648 * return -ENOMEM;
649 *
650 * ... use 'tmpmask' like a normal struct cpumask * ...
651 *
652 * free_cpumask_var(tmpmask);
KOSAKI Motohiroa64a26e2011-07-26 16:08:45 -0700653 *
654 *
655 * However, one notable exception is there. alloc_cpumask_var() allocates
656 * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has
657 * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t.
658 *
659 * cpumask_var_t tmpmask;
660 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
661 * return -ENOMEM;
662 *
663 * var = *tmpmask;
664 *
665 * This code makes NR_CPUS length memcopy and brings to a memory corruption.
666 * cpumask_copy() provide safe copy functionality.
Christoph Lameter4ba29682014-08-26 19:12:21 -0500667 *
668 * Note that there is another evil here: If you define a cpumask_var_t
669 * as a percpu variable then the way to obtain the address of the cpumask
670 * structure differently influences what this_cpu_* operation needs to be
671 * used. Please use this_cpu_cpumask_var_t in those cases. The direct use
672 * of this_cpu_ptr() or this_cpu_read() will lead to failures when the
673 * other type of cpumask_var_t implementation is configured.
Rusty Russell2d3854a2008-11-05 13:39:10 +1100674 */
675#ifdef CONFIG_CPUMASK_OFFSTACK
676typedef struct cpumask *cpumask_var_t;
677
Christoph Lameter4ba29682014-08-26 19:12:21 -0500678#define this_cpu_cpumask_var_ptr(x) this_cpu_read(x)
679
Mike Travis7b4967c2008-12-19 16:56:37 +1030680bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100681bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
Yinghai Lu0281b5d2009-06-06 14:50:36 -0700682bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
683bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100684void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
685void free_cpumask_var(cpumask_var_t mask);
Rusty Russellcd83e422008-11-07 11:12:29 +1100686void free_bootmem_cpumask_var(cpumask_var_t mask);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100687
688#else
689typedef struct cpumask cpumask_var_t[1];
690
Christoph Lameter4ba29682014-08-26 19:12:21 -0500691#define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x)
692
Rusty Russell2d3854a2008-11-05 13:39:10 +1100693static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
694{
695 return true;
696}
697
Mike Travis7b4967c2008-12-19 16:56:37 +1030698static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
699 int node)
700{
701 return true;
702}
703
Yinghai Lu0281b5d2009-06-06 14:50:36 -0700704static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
705{
706 cpumask_clear(*mask);
707 return true;
708}
709
710static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
711 int node)
712{
713 cpumask_clear(*mask);
714 return true;
715}
716
Rusty Russell2d3854a2008-11-05 13:39:10 +1100717static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
718{
719}
720
721static inline void free_cpumask_var(cpumask_var_t mask)
722{
723}
Rusty Russellcd83e422008-11-07 11:12:29 +1100724
725static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
726{
727}
Rusty Russell2d3854a2008-11-05 13:39:10 +1100728#endif /* CONFIG_CPUMASK_OFFSTACK */
729
Rusty Russell2d3854a2008-11-05 13:39:10 +1100730/* It's common to want to use cpu_all_mask in struct member initializers,
731 * so it has to refer to an address rather than a pointer. */
732extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
733#define cpu_all_mask to_cpumask(cpu_all_bits)
734
735/* First bits of cpu_bit_bitmap are in fact unset. */
736#define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
737
Rusty Russellae7a47e2008-12-30 09:05:15 +1030738#define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
739#define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
740#define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
741
Rusty Russell2d3854a2008-11-05 13:39:10 +1100742/* Wrappers for arch boot code to manipulate normally-constant masks */
Rusty Russell3fa41522008-12-30 09:05:16 +1030743void set_cpu_possible(unsigned int cpu, bool possible);
744void set_cpu_present(unsigned int cpu, bool present);
745void set_cpu_online(unsigned int cpu, bool online);
746void set_cpu_active(unsigned int cpu, bool active);
747void init_cpu_present(const struct cpumask *src);
748void init_cpu_possible(const struct cpumask *src);
749void init_cpu_online(const struct cpumask *src);
Rusty Russell6ba2ef72009-09-24 09:34:53 -0600750
751/**
752 * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
753 * @bitmap: the bitmap
754 *
755 * There are a few places where cpumask_var_t isn't appropriate and
756 * static cpumasks must be used (eg. very early boot), yet we don't
757 * expose the definition of 'struct cpumask'.
758 *
759 * This does the conversion, and can be used as a constant initializer.
760 */
761#define to_cpumask(bitmap) \
762 ((struct cpumask *)(1 ? (bitmap) \
763 : (void *)sizeof(__check_is_bitmap(bitmap))))
764
765static inline int __check_is_bitmap(const unsigned long *bitmap)
766{
767 return 1;
768}
769
770/*
771 * Special-case data structure for "single bit set only" constant CPU masks.
772 *
773 * We pre-generate all the 64 (or 32) possible bit positions, with enough
774 * padding to the left and the right, and return the constant pointer
775 * appropriately offset.
776 */
777extern const unsigned long
778 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
779
780static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
781{
782 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
783 p -= cpu / BITS_PER_LONG;
784 return to_cpumask(p);
785}
786
787#define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
788
789#if NR_CPUS <= BITS_PER_LONG
790#define CPU_BITS_ALL \
791{ \
792 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
793}
794
795#else /* NR_CPUS > BITS_PER_LONG */
796
797#define CPU_BITS_ALL \
798{ \
799 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
800 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
801}
802#endif /* NR_CPUS > BITS_PER_LONG */
803
804/*
805 *
806 * From here down, all obsolete. Use cpumask_ variants!
807 *
808 */
809#ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
Rusty Russell6ba2ef72009-09-24 09:34:53 -0600810#define cpumask_of_cpu(cpu) (*get_cpu_mask(cpu))
811
812#define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
813
814#if NR_CPUS <= BITS_PER_LONG
815
816#define CPU_MASK_ALL \
817(cpumask_t) { { \
818 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
819} }
820
821#else
822
823#define CPU_MASK_ALL \
824(cpumask_t) { { \
825 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
826 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
827} }
828
829#endif
830
831#define CPU_MASK_NONE \
832(cpumask_t) { { \
833 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
834} }
835
836#define CPU_MASK_CPU0 \
837(cpumask_t) { { \
838 [0] = 1UL \
839} }
840
841#if NR_CPUS == 1
842#define first_cpu(src) ({ (void)(src); 0; })
843#define next_cpu(n, src) ({ (void)(src); 1; })
844#define any_online_cpu(mask) 0
845#define for_each_cpu_mask(cpu, mask) \
846 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
847#else /* NR_CPUS > 1 */
848int __first_cpu(const cpumask_t *srcp);
849int __next_cpu(int n, const cpumask_t *srcp);
Rusty Russell6ba2ef72009-09-24 09:34:53 -0600850
851#define first_cpu(src) __first_cpu(&(src))
852#define next_cpu(n, src) __next_cpu((n), &(src))
Srivatsa S. Bhat38b93782012-03-28 14:42:46 -0700853#define any_online_cpu(mask) cpumask_any_and(&mask, cpu_online_mask)
Rusty Russell6ba2ef72009-09-24 09:34:53 -0600854#define for_each_cpu_mask(cpu, mask) \
855 for ((cpu) = -1; \
856 (cpu) = next_cpu((cpu), (mask)), \
857 (cpu) < NR_CPUS; )
858#endif /* SMP */
859
860#if NR_CPUS <= 64
861
862#define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
863
864#else /* NR_CPUS > 64 */
865
866int __next_cpu_nr(int n, const cpumask_t *srcp);
867#define for_each_cpu_mask_nr(cpu, mask) \
868 for ((cpu) = -1; \
869 (cpu) = __next_cpu_nr((cpu), &(mask)), \
870 (cpu) < nr_cpu_ids; )
871
872#endif /* NR_CPUS > 64 */
873
874#define cpus_addr(src) ((src).bits)
875
876#define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
877static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
878{
879 set_bit(cpu, dstp->bits);
880}
881
882#define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
883static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
884{
885 clear_bit(cpu, dstp->bits);
886}
887
888#define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
889static inline void __cpus_setall(cpumask_t *dstp, int nbits)
890{
891 bitmap_fill(dstp->bits, nbits);
892}
893
894#define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
895static inline void __cpus_clear(cpumask_t *dstp, int nbits)
896{
897 bitmap_zero(dstp->bits, nbits);
898}
899
900/* No static inline type checking - see Subtlety (1) above. */
901#define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
902
903#define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
904static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
905{
906 return test_and_set_bit(cpu, addr->bits);
907}
908
909#define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
910static inline int __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
911 const cpumask_t *src2p, int nbits)
912{
913 return bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
914}
915
916#define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
917static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
918 const cpumask_t *src2p, int nbits)
919{
920 bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
921}
922
923#define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
924static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
925 const cpumask_t *src2p, int nbits)
926{
927 bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
928}
929
930#define cpus_andnot(dst, src1, src2) \
931 __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
932static inline int __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
933 const cpumask_t *src2p, int nbits)
934{
935 return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
936}
937
938#define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
939static inline int __cpus_equal(const cpumask_t *src1p,
940 const cpumask_t *src2p, int nbits)
941{
942 return bitmap_equal(src1p->bits, src2p->bits, nbits);
943}
944
945#define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
946static inline int __cpus_intersects(const cpumask_t *src1p,
947 const cpumask_t *src2p, int nbits)
948{
949 return bitmap_intersects(src1p->bits, src2p->bits, nbits);
950}
951
952#define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
953static inline int __cpus_subset(const cpumask_t *src1p,
954 const cpumask_t *src2p, int nbits)
955{
956 return bitmap_subset(src1p->bits, src2p->bits, nbits);
957}
958
959#define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
960static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
961{
962 return bitmap_empty(srcp->bits, nbits);
963}
964
965#define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
966static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
967{
968 return bitmap_weight(srcp->bits, nbits);
969}
970
971#define cpus_shift_left(dst, src, n) \
972 __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
973static inline void __cpus_shift_left(cpumask_t *dstp,
974 const cpumask_t *srcp, int n, int nbits)
975{
976 bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
977}
978#endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980#endif /* __LINUX_CPUMASK_H */