blob: a2c819d3c96e3d367b4de11b852efea54a79e80d [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
145#define for_each_cpu(cpu, mask) \
146 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
Paul E. McKenney8bd93a22010-02-22 17:04:59 -0800147#define for_each_cpu_not(cpu, mask) \
148 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
Rusty Russell2d3854a2008-11-05 13:39:10 +1100149#define for_each_cpu_and(cpu, mask, and) \
150 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
151#else
152/**
153 * cpumask_first - get the first cpu in a cpumask
154 * @srcp: the cpumask pointer
155 *
156 * Returns >= nr_cpu_ids if no cpus set.
157 */
158static inline unsigned int cpumask_first(const struct cpumask *srcp)
159{
160 return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
161}
162
163/**
164 * cpumask_next - get the next cpu in a cpumask
165 * @n: the cpu prior to the place to search (ie. return will be > @n)
166 * @srcp: the cpumask pointer
167 *
168 * Returns >= nr_cpu_ids if no further cpus set.
169 */
170static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
171{
172 /* -1 is a legal arg here. */
173 if (n != -1)
174 cpumask_check(n);
175 return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
176}
177
178/**
179 * cpumask_next_zero - get the next unset cpu in a cpumask
180 * @n: the cpu prior to the place to search (ie. return will be > @n)
181 * @srcp: the cpumask pointer
182 *
183 * Returns >= nr_cpu_ids if no further cpus unset.
184 */
185static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
186{
187 /* -1 is a legal arg here. */
188 if (n != -1)
189 cpumask_check(n);
190 return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
191}
192
193int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
194int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
195
Rusty Russell984f2f32008-11-08 20:24:19 +1100196/**
197 * for_each_cpu - iterate over every cpu in a mask
198 * @cpu: the (optionally unsigned) integer iterator
199 * @mask: the cpumask pointer
200 *
201 * After the loop, cpu is >= nr_cpu_ids.
202 */
Rusty Russell2d3854a2008-11-05 13:39:10 +1100203#define for_each_cpu(cpu, mask) \
204 for ((cpu) = -1; \
205 (cpu) = cpumask_next((cpu), (mask)), \
206 (cpu) < nr_cpu_ids;)
Rusty Russell984f2f32008-11-08 20:24:19 +1100207
208/**
Paul E. McKenney8bd93a22010-02-22 17:04:59 -0800209 * for_each_cpu_not - iterate over every cpu in a complemented mask
210 * @cpu: the (optionally unsigned) integer iterator
211 * @mask: the cpumask pointer
212 *
213 * After the loop, cpu is >= nr_cpu_ids.
214 */
215#define for_each_cpu_not(cpu, mask) \
216 for ((cpu) = -1; \
217 (cpu) = cpumask_next_zero((cpu), (mask)), \
218 (cpu) < nr_cpu_ids;)
219
220/**
Rusty Russell984f2f32008-11-08 20:24:19 +1100221 * for_each_cpu_and - iterate over every cpu in both masks
222 * @cpu: the (optionally unsigned) integer iterator
223 * @mask: the first cpumask pointer
224 * @and: the second cpumask pointer
225 *
226 * This saves a temporary CPU mask in many places. It is equivalent to:
227 * struct cpumask tmp;
228 * cpumask_and(&tmp, &mask, &and);
229 * for_each_cpu(cpu, &tmp)
230 * ...
231 *
232 * After the loop, cpu is >= nr_cpu_ids.
233 */
Rusty Russell2d3854a2008-11-05 13:39:10 +1100234#define for_each_cpu_and(cpu, mask, and) \
235 for ((cpu) = -1; \
236 (cpu) = cpumask_next_and((cpu), (mask), (and)), \
237 (cpu) < nr_cpu_ids;)
238#endif /* SMP */
239
240#define CPU_BITS_NONE \
241{ \
242 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
243}
244
245#define CPU_BITS_CPU0 \
246{ \
247 [0] = 1UL \
248}
249
250/**
251 * cpumask_set_cpu - set a cpu in a cpumask
252 * @cpu: cpu number (< nr_cpu_ids)
253 * @dstp: the cpumask pointer
254 */
255static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
256{
257 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
258}
259
260/**
261 * cpumask_clear_cpu - clear a cpu in a cpumask
262 * @cpu: cpu number (< nr_cpu_ids)
263 * @dstp: the cpumask pointer
264 */
265static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
266{
267 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
268}
269
270/**
271 * cpumask_test_cpu - test for a cpu in a cpumask
272 * @cpu: cpu number (< nr_cpu_ids)
273 * @cpumask: the cpumask pointer
274 *
275 * No static inline type checking - see Subtlety (1) above.
276 */
277#define cpumask_test_cpu(cpu, cpumask) \
Rusty Russellae7a47e2008-12-30 09:05:15 +1030278 test_bit(cpumask_check(cpu), cpumask_bits((cpumask)))
Rusty Russell2d3854a2008-11-05 13:39:10 +1100279
280/**
281 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
282 * @cpu: cpu number (< nr_cpu_ids)
283 * @cpumask: the cpumask pointer
284 *
285 * test_and_set_bit wrapper for cpumasks.
286 */
287static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
288{
289 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
290}
291
292/**
Xiao Guangrong54fdade2009-09-22 16:43:39 -0700293 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
294 * @cpu: cpu number (< nr_cpu_ids)
295 * @cpumask: the cpumask pointer
296 *
297 * test_and_clear_bit wrapper for cpumasks.
298 */
299static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
300{
301 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
302}
303
304/**
Rusty Russell2d3854a2008-11-05 13:39:10 +1100305 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
306 * @dstp: the cpumask pointer
307 */
308static inline void cpumask_setall(struct cpumask *dstp)
309{
310 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
311}
312
313/**
314 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
315 * @dstp: the cpumask pointer
316 */
317static inline void cpumask_clear(struct cpumask *dstp)
318{
319 bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
320}
321
322/**
323 * cpumask_and - *dstp = *src1p & *src2p
324 * @dstp: the cpumask result
325 * @src1p: the first input
326 * @src2p: the second input
327 */
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700328static inline int cpumask_and(struct cpumask *dstp,
Rusty Russell2d3854a2008-11-05 13:39:10 +1100329 const struct cpumask *src1p,
330 const struct cpumask *src2p)
331{
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700332 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
Rusty Russell2d3854a2008-11-05 13:39:10 +1100333 cpumask_bits(src2p), nr_cpumask_bits);
334}
335
336/**
337 * cpumask_or - *dstp = *src1p | *src2p
338 * @dstp: the cpumask result
339 * @src1p: the first input
340 * @src2p: the second input
341 */
342static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
343 const struct cpumask *src2p)
344{
345 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
346 cpumask_bits(src2p), nr_cpumask_bits);
347}
348
349/**
350 * cpumask_xor - *dstp = *src1p ^ *src2p
351 * @dstp: the cpumask result
352 * @src1p: the first input
353 * @src2p: the second input
354 */
355static inline void cpumask_xor(struct cpumask *dstp,
356 const struct cpumask *src1p,
357 const struct cpumask *src2p)
358{
359 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
360 cpumask_bits(src2p), nr_cpumask_bits);
361}
362
363/**
364 * cpumask_andnot - *dstp = *src1p & ~*src2p
365 * @dstp: the cpumask result
366 * @src1p: the first input
367 * @src2p: the second input
368 */
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700369static inline int cpumask_andnot(struct cpumask *dstp,
Rusty Russell2d3854a2008-11-05 13:39:10 +1100370 const struct cpumask *src1p,
371 const struct cpumask *src2p)
372{
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700373 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
Rusty Russell2d3854a2008-11-05 13:39:10 +1100374 cpumask_bits(src2p), nr_cpumask_bits);
375}
376
377/**
378 * cpumask_complement - *dstp = ~*srcp
379 * @dstp: the cpumask result
380 * @srcp: the input to invert
381 */
382static inline void cpumask_complement(struct cpumask *dstp,
383 const struct cpumask *srcp)
384{
385 bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
386 nr_cpumask_bits);
387}
388
389/**
390 * cpumask_equal - *src1p == *src2p
391 * @src1p: the first input
392 * @src2p: the second input
393 */
394static inline bool cpumask_equal(const struct cpumask *src1p,
395 const struct cpumask *src2p)
396{
397 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
398 nr_cpumask_bits);
399}
400
401/**
402 * cpumask_intersects - (*src1p & *src2p) != 0
403 * @src1p: the first input
404 * @src2p: the second input
405 */
406static inline bool cpumask_intersects(const struct cpumask *src1p,
407 const struct cpumask *src2p)
408{
409 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
410 nr_cpumask_bits);
411}
412
413/**
414 * cpumask_subset - (*src1p & ~*src2p) == 0
415 * @src1p: the first input
416 * @src2p: the second input
417 */
418static inline int cpumask_subset(const struct cpumask *src1p,
419 const struct cpumask *src2p)
420{
421 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
422 nr_cpumask_bits);
423}
424
425/**
426 * cpumask_empty - *srcp == 0
427 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
428 */
429static inline bool cpumask_empty(const struct cpumask *srcp)
430{
431 return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
432}
433
434/**
435 * cpumask_full - *srcp == 0xFFFFFFFF...
436 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
437 */
438static inline bool cpumask_full(const struct cpumask *srcp)
439{
440 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
441}
442
443/**
444 * cpumask_weight - Count of bits in *srcp
445 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
446 */
447static inline unsigned int cpumask_weight(const struct cpumask *srcp)
448{
449 return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
450}
451
452/**
453 * cpumask_shift_right - *dstp = *srcp >> n
454 * @dstp: the cpumask result
455 * @srcp: the input to shift
456 * @n: the number of bits to shift by
457 */
458static inline void cpumask_shift_right(struct cpumask *dstp,
459 const struct cpumask *srcp, int n)
460{
461 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
462 nr_cpumask_bits);
463}
464
465/**
466 * cpumask_shift_left - *dstp = *srcp << n
467 * @dstp: the cpumask result
468 * @srcp: the input to shift
469 * @n: the number of bits to shift by
470 */
471static inline void cpumask_shift_left(struct cpumask *dstp,
472 const struct cpumask *srcp, int n)
473{
474 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
475 nr_cpumask_bits);
476}
477
478/**
479 * cpumask_copy - *dstp = *srcp
480 * @dstp: the result
481 * @srcp: the input cpumask
482 */
483static inline void cpumask_copy(struct cpumask *dstp,
484 const struct cpumask *srcp)
485{
486 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
487}
488
489/**
490 * cpumask_any - pick a "random" cpu from *srcp
491 * @srcp: the input cpumask
492 *
493 * Returns >= nr_cpu_ids if no cpus set.
494 */
495#define cpumask_any(srcp) cpumask_first(srcp)
496
497/**
498 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
499 * @src1p: the first input
500 * @src2p: the second input
501 *
502 * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
503 */
504#define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
505
506/**
507 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
508 * @mask1: the first input cpumask
509 * @mask2: the second input cpumask
510 *
511 * Returns >= nr_cpu_ids if no cpus set.
512 */
513#define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
514
515/**
Rusty Russellcd83e422008-11-07 11:12:29 +1100516 * cpumask_of - the cpumask containing just a given cpu
517 * @cpu: the cpu (<= nr_cpu_ids)
518 */
519#define cpumask_of(cpu) (get_cpu_mask(cpu))
520
521/**
Rusty Russell29c01772008-12-13 21:20:25 +1030522 * cpumask_scnprintf - print a cpumask into a string as comma-separated hex
523 * @buf: the buffer to sprintf into
524 * @len: the length of the buffer
525 * @srcp: the cpumask to print
526 *
527 * If len is zero, returns zero. Otherwise returns the length of the
528 * (nul-terminated) @buf string.
529 */
530static inline int cpumask_scnprintf(char *buf, int len,
531 const struct cpumask *srcp)
532{
Rusty Russellae7a47e2008-12-30 09:05:15 +1030533 return bitmap_scnprintf(buf, len, cpumask_bits(srcp), nr_cpumask_bits);
Rusty Russell29c01772008-12-13 21:20:25 +1030534}
535
536/**
537 * cpumask_parse_user - extract a cpumask from a user string
538 * @buf: the buffer to extract from
539 * @len: the length of the buffer
540 * @dstp: the cpumask to set.
541 *
542 * Returns -errno, or 0 for success.
543 */
544static inline int cpumask_parse_user(const char __user *buf, int len,
545 struct cpumask *dstp)
546{
Rusty Russellae7a47e2008-12-30 09:05:15 +1030547 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
Rusty Russell29c01772008-12-13 21:20:25 +1030548}
549
550/**
Mike Travis4b060422011-05-24 17:13:12 -0700551 * cpumask_parselist_user - extract a cpumask from a user string
552 * @buf: the buffer to extract from
553 * @len: the length of the buffer
554 * @dstp: the cpumask to set.
555 *
556 * Returns -errno, or 0 for success.
557 */
558static inline int cpumask_parselist_user(const char __user *buf, int len,
559 struct cpumask *dstp)
560{
561 return bitmap_parselist_user(buf, len, cpumask_bits(dstp),
562 nr_cpumask_bits);
563}
564
565/**
Rusty Russell29c01772008-12-13 21:20:25 +1030566 * cpulist_scnprintf - print a cpumask into a string as comma-separated list
567 * @buf: the buffer to sprintf into
568 * @len: the length of the buffer
569 * @srcp: the cpumask to print
570 *
571 * If len is zero, returns zero. Otherwise returns the length of the
572 * (nul-terminated) @buf string.
573 */
574static inline int cpulist_scnprintf(char *buf, int len,
575 const struct cpumask *srcp)
576{
Rusty Russellae7a47e2008-12-30 09:05:15 +1030577 return bitmap_scnlistprintf(buf, len, cpumask_bits(srcp),
578 nr_cpumask_bits);
Rusty Russell29c01772008-12-13 21:20:25 +1030579}
580
581/**
582 * cpulist_parse_user - extract a cpumask from a user string of ranges
583 * @buf: the buffer to extract from
584 * @len: the length of the buffer
585 * @dstp: the cpumask to set.
586 *
587 * Returns -errno, or 0 for success.
588 */
589static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
590{
Rusty Russellae7a47e2008-12-30 09:05:15 +1030591 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100592}
593
594/**
595 * cpumask_size - size to allocate for a 'struct cpumask' in bytes
596 *
597 * This will eventually be a runtime variable, depending on nr_cpu_ids.
598 */
599static inline size_t cpumask_size(void)
600{
601 /* FIXME: Once all cpumask assignments are eliminated, this
602 * can be nr_cpumask_bits */
603 return BITS_TO_LONGS(NR_CPUS) * sizeof(long);
604}
605
606/*
607 * cpumask_var_t: struct cpumask for stack usage.
608 *
609 * Oh, the wicked games we play! In order to make kernel coding a
610 * little more difficult, we typedef cpumask_var_t to an array or a
611 * pointer: doing &mask on an array is a noop, so it still works.
612 *
613 * ie.
614 * cpumask_var_t tmpmask;
615 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
616 * return -ENOMEM;
617 *
618 * ... use 'tmpmask' like a normal struct cpumask * ...
619 *
620 * free_cpumask_var(tmpmask);
KOSAKI Motohiroa64a26e2011-07-26 16:08:45 -0700621 *
622 *
623 * However, one notable exception is there. alloc_cpumask_var() allocates
624 * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has
625 * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t.
626 *
627 * cpumask_var_t tmpmask;
628 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
629 * return -ENOMEM;
630 *
631 * var = *tmpmask;
632 *
633 * This code makes NR_CPUS length memcopy and brings to a memory corruption.
634 * cpumask_copy() provide safe copy functionality.
Rusty Russell2d3854a2008-11-05 13:39:10 +1100635 */
636#ifdef CONFIG_CPUMASK_OFFSTACK
637typedef struct cpumask *cpumask_var_t;
638
Mike Travis7b4967c2008-12-19 16:56:37 +1030639bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100640bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
Yinghai Lu0281b5d2009-06-06 14:50:36 -0700641bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
642bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100643void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
644void free_cpumask_var(cpumask_var_t mask);
Rusty Russellcd83e422008-11-07 11:12:29 +1100645void free_bootmem_cpumask_var(cpumask_var_t mask);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100646
647#else
648typedef struct cpumask cpumask_var_t[1];
649
650static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
651{
652 return true;
653}
654
Mike Travis7b4967c2008-12-19 16:56:37 +1030655static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
656 int node)
657{
658 return true;
659}
660
Yinghai Lu0281b5d2009-06-06 14:50:36 -0700661static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
662{
663 cpumask_clear(*mask);
664 return true;
665}
666
667static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
668 int node)
669{
670 cpumask_clear(*mask);
671 return true;
672}
673
Rusty Russell2d3854a2008-11-05 13:39:10 +1100674static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
675{
676}
677
678static inline void free_cpumask_var(cpumask_var_t mask)
679{
680}
Rusty Russellcd83e422008-11-07 11:12:29 +1100681
682static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
683{
684}
Rusty Russell2d3854a2008-11-05 13:39:10 +1100685#endif /* CONFIG_CPUMASK_OFFSTACK */
686
Rusty Russell2d3854a2008-11-05 13:39:10 +1100687/* It's common to want to use cpu_all_mask in struct member initializers,
688 * so it has to refer to an address rather than a pointer. */
689extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
690#define cpu_all_mask to_cpumask(cpu_all_bits)
691
692/* First bits of cpu_bit_bitmap are in fact unset. */
693#define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
694
Rusty Russellae7a47e2008-12-30 09:05:15 +1030695#define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
696#define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
697#define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
698
Rusty Russell2d3854a2008-11-05 13:39:10 +1100699/* Wrappers for arch boot code to manipulate normally-constant masks */
Rusty Russell3fa41522008-12-30 09:05:16 +1030700void set_cpu_possible(unsigned int cpu, bool possible);
701void set_cpu_present(unsigned int cpu, bool present);
702void set_cpu_online(unsigned int cpu, bool online);
703void set_cpu_active(unsigned int cpu, bool active);
704void init_cpu_present(const struct cpumask *src);
705void init_cpu_possible(const struct cpumask *src);
706void init_cpu_online(const struct cpumask *src);
Rusty Russell6ba2ef72009-09-24 09:34:53 -0600707
708/**
709 * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
710 * @bitmap: the bitmap
711 *
712 * There are a few places where cpumask_var_t isn't appropriate and
713 * static cpumasks must be used (eg. very early boot), yet we don't
714 * expose the definition of 'struct cpumask'.
715 *
716 * This does the conversion, and can be used as a constant initializer.
717 */
718#define to_cpumask(bitmap) \
719 ((struct cpumask *)(1 ? (bitmap) \
720 : (void *)sizeof(__check_is_bitmap(bitmap))))
721
722static inline int __check_is_bitmap(const unsigned long *bitmap)
723{
724 return 1;
725}
726
727/*
728 * Special-case data structure for "single bit set only" constant CPU masks.
729 *
730 * We pre-generate all the 64 (or 32) possible bit positions, with enough
731 * padding to the left and the right, and return the constant pointer
732 * appropriately offset.
733 */
734extern const unsigned long
735 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
736
737static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
738{
739 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
740 p -= cpu / BITS_PER_LONG;
741 return to_cpumask(p);
742}
743
744#define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
745
746#if NR_CPUS <= BITS_PER_LONG
747#define CPU_BITS_ALL \
748{ \
749 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
750}
751
752#else /* NR_CPUS > BITS_PER_LONG */
753
754#define CPU_BITS_ALL \
755{ \
756 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
757 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
758}
759#endif /* NR_CPUS > BITS_PER_LONG */
760
761/*
762 *
763 * From here down, all obsolete. Use cpumask_ variants!
764 *
765 */
766#ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
Rusty Russell6ba2ef72009-09-24 09:34:53 -0600767#define cpumask_of_cpu(cpu) (*get_cpu_mask(cpu))
768
769#define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
770
771#if NR_CPUS <= BITS_PER_LONG
772
773#define CPU_MASK_ALL \
774(cpumask_t) { { \
775 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
776} }
777
778#else
779
780#define CPU_MASK_ALL \
781(cpumask_t) { { \
782 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
783 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
784} }
785
786#endif
787
788#define CPU_MASK_NONE \
789(cpumask_t) { { \
790 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
791} }
792
793#define CPU_MASK_CPU0 \
794(cpumask_t) { { \
795 [0] = 1UL \
796} }
797
798#if NR_CPUS == 1
799#define first_cpu(src) ({ (void)(src); 0; })
800#define next_cpu(n, src) ({ (void)(src); 1; })
801#define any_online_cpu(mask) 0
802#define for_each_cpu_mask(cpu, mask) \
803 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
804#else /* NR_CPUS > 1 */
805int __first_cpu(const cpumask_t *srcp);
806int __next_cpu(int n, const cpumask_t *srcp);
Rusty Russell6ba2ef72009-09-24 09:34:53 -0600807
808#define first_cpu(src) __first_cpu(&(src))
809#define next_cpu(n, src) __next_cpu((n), &(src))
Srivatsa S. Bhat38b93782012-03-28 14:42:46 -0700810#define any_online_cpu(mask) cpumask_any_and(&mask, cpu_online_mask)
Rusty Russell6ba2ef72009-09-24 09:34:53 -0600811#define for_each_cpu_mask(cpu, mask) \
812 for ((cpu) = -1; \
813 (cpu) = next_cpu((cpu), (mask)), \
814 (cpu) < NR_CPUS; )
815#endif /* SMP */
816
817#if NR_CPUS <= 64
818
819#define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
820
821#else /* NR_CPUS > 64 */
822
823int __next_cpu_nr(int n, const cpumask_t *srcp);
824#define for_each_cpu_mask_nr(cpu, mask) \
825 for ((cpu) = -1; \
826 (cpu) = __next_cpu_nr((cpu), &(mask)), \
827 (cpu) < nr_cpu_ids; )
828
829#endif /* NR_CPUS > 64 */
830
831#define cpus_addr(src) ((src).bits)
832
833#define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
834static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
835{
836 set_bit(cpu, dstp->bits);
837}
838
839#define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
840static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
841{
842 clear_bit(cpu, dstp->bits);
843}
844
845#define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
846static inline void __cpus_setall(cpumask_t *dstp, int nbits)
847{
848 bitmap_fill(dstp->bits, nbits);
849}
850
851#define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
852static inline void __cpus_clear(cpumask_t *dstp, int nbits)
853{
854 bitmap_zero(dstp->bits, nbits);
855}
856
857/* No static inline type checking - see Subtlety (1) above. */
858#define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
859
860#define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
861static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
862{
863 return test_and_set_bit(cpu, addr->bits);
864}
865
866#define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
867static inline int __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
868 const cpumask_t *src2p, int nbits)
869{
870 return bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
871}
872
873#define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
874static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
875 const cpumask_t *src2p, int nbits)
876{
877 bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
878}
879
880#define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
881static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
882 const cpumask_t *src2p, int nbits)
883{
884 bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
885}
886
887#define cpus_andnot(dst, src1, src2) \
888 __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
889static inline int __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
890 const cpumask_t *src2p, int nbits)
891{
892 return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
893}
894
895#define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
896static inline int __cpus_equal(const cpumask_t *src1p,
897 const cpumask_t *src2p, int nbits)
898{
899 return bitmap_equal(src1p->bits, src2p->bits, nbits);
900}
901
902#define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
903static inline int __cpus_intersects(const cpumask_t *src1p,
904 const cpumask_t *src2p, int nbits)
905{
906 return bitmap_intersects(src1p->bits, src2p->bits, nbits);
907}
908
909#define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
910static inline int __cpus_subset(const cpumask_t *src1p,
911 const cpumask_t *src2p, int nbits)
912{
913 return bitmap_subset(src1p->bits, src2p->bits, nbits);
914}
915
916#define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
917static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
918{
919 return bitmap_empty(srcp->bits, nbits);
920}
921
922#define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
923static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
924{
925 return bitmap_weight(srcp->bits, nbits);
926}
927
928#define cpus_shift_left(dst, src, n) \
929 __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
930static inline void __cpus_shift_left(cpumask_t *dstp,
931 const cpumask_t *srcp, int n, int nbits)
932{
933 bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
934}
935#endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937#endif /* __LINUX_CPUMASK_H */