blob: bae6fe24d1f9de103d75d40b6439f7e17a406b2a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Rusty Russell2d3854a2008-11-05 13:39:10 +110013typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Rusty Russellae7a47e2008-12-30 09:05:15 +103015/**
Rusty Russell6ba2ef72009-09-24 09:34:53 -060016 * cpumask_bits - get the bits in a cpumask
17 * @maskp: the struct cpumask *
Rusty Russellae7a47e2008-12-30 09:05:15 +103018 *
Rusty Russell6ba2ef72009-09-24 09:34:53 -060019 * You should only assume nr_cpu_ids bits of this mask are valid. This is
20 * a macro so it's const-correct.
Rusty Russellae7a47e2008-12-30 09:05:15 +103021 */
Rusty Russell6ba2ef72009-09-24 09:34:53 -060022#define cpumask_bits(maskp) ((maskp)->bits)
Paul Jackson7ea931c2008-04-28 02:12:29 -070023
Mike Travis41df0d612008-05-12 21:21:13 +020024#if NR_CPUS == 1
Mike Travis41df0d612008-05-12 21:21:13 +020025#define nr_cpu_ids 1
Rusty Russell6ba2ef72009-09-24 09:34:53 -060026#else
Mike Travis41df0d612008-05-12 21:21:13 +020027extern int nr_cpu_ids;
Mike Travis41df0d612008-05-12 21:21:13 +020028#endif
29
Rusty Russell6ba2ef72009-09-24 09:34:53 -060030#ifdef CONFIG_CPUMASK_OFFSTACK
31/* Assuming NR_CPUS is huge, a runtime limit is more efficient. Also,
32 * not all bits may be allocated. */
33#define nr_cpumask_bits nr_cpu_ids
34#else
35#define nr_cpumask_bits NR_CPUS
36#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * The following particular system cpumasks and operations manage
Rusty Russellb3199c02008-12-30 09:05:14 +103040 * possible, present, active and online cpus.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 *
Rusty Russellb3199c02008-12-30 09:05:14 +103042 * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
43 * cpu_present_mask - has bit 'cpu' set iff cpu is populated
44 * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler
45 * cpu_active_mask - has bit 'cpu' set iff cpu available to migration
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 *
Rusty Russellb3199c02008-12-30 09:05:14 +103047 * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 *
Rusty Russellb3199c02008-12-30 09:05:14 +103049 * The cpu_possible_mask is fixed at boot time, as the set of CPU id's
50 * that it is possible might ever be plugged in at anytime during the
51 * life of that system boot. The cpu_present_mask is dynamic(*),
52 * representing which CPUs are currently plugged in. And
53 * cpu_online_mask is the dynamic subset of cpu_present_mask,
54 * indicating those CPUs available for scheduling.
55 *
56 * If HOTPLUG is enabled, then cpu_possible_mask is forced to have
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * all NR_CPUS bits set, otherwise it is just the set of CPUs that
58 * ACPI reports present at boot.
59 *
Rusty Russellb3199c02008-12-30 09:05:14 +103060 * If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * depending on what ACPI reports as currently plugged in, otherwise
Rusty Russellb3199c02008-12-30 09:05:14 +103062 * cpu_present_mask is just a copy of cpu_possible_mask.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
Rusty Russellb3199c02008-12-30 09:05:14 +103064 * (*) Well, cpu_present_mask is dynamic in the hotplug case. If not
65 * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 *
67 * Subtleties:
68 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
69 * assumption that their single CPU is online. The UP
Rusty Russellb3199c02008-12-30 09:05:14 +103070 * cpu_{online,possible,present}_masks are placebos. Changing them
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * will have no useful affect on the following num_*_cpus()
72 * and cpu_*() macros in the UP case. This ugliness is a UP
73 * optimization - don't waste any instructions or memory references
74 * asking if you're online or how many CPUs there are if there is
75 * only one CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 */
77
Rusty Russellb3199c02008-12-30 09:05:14 +103078extern const struct cpumask *const cpu_possible_mask;
79extern const struct cpumask *const cpu_online_mask;
80extern const struct cpumask *const cpu_present_mask;
81extern const struct cpumask *const cpu_active_mask;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#if NR_CPUS > 1
Rusty Russellae7a47e2008-12-30 09:05:15 +103084#define num_online_cpus() cpumask_weight(cpu_online_mask)
85#define num_possible_cpus() cpumask_weight(cpu_possible_mask)
86#define num_present_cpus() cpumask_weight(cpu_present_mask)
Peter Zijlstra6ad4c182009-11-25 13:31:39 +010087#define num_active_cpus() cpumask_weight(cpu_active_mask)
Rusty Russellae7a47e2008-12-30 09:05:15 +103088#define cpu_online(cpu) cpumask_test_cpu((cpu), cpu_online_mask)
89#define cpu_possible(cpu) cpumask_test_cpu((cpu), cpu_possible_mask)
90#define cpu_present(cpu) cpumask_test_cpu((cpu), cpu_present_mask)
91#define cpu_active(cpu) cpumask_test_cpu((cpu), cpu_active_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#else
Heiko Carstens221e3eb2010-03-05 13:42:41 -080093#define num_online_cpus() 1U
94#define num_possible_cpus() 1U
95#define num_present_cpus() 1U
96#define num_active_cpus() 1U
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define cpu_online(cpu) ((cpu) == 0)
98#define cpu_possible(cpu) ((cpu) == 0)
99#define cpu_present(cpu) ((cpu) == 0)
Max Krasnyanskye761b772008-07-15 04:43:49 -0700100#define cpu_active(cpu) ((cpu) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#endif
102
Rusty Russell2d3854a2008-11-05 13:39:10 +1100103/* verify cpu argument to cpumask_* operators */
104static inline unsigned int cpumask_check(unsigned int cpu)
105{
106#ifdef CONFIG_DEBUG_PER_CPU_MAPS
107 WARN_ON_ONCE(cpu >= nr_cpumask_bits);
108#endif /* CONFIG_DEBUG_PER_CPU_MAPS */
109 return cpu;
110}
111
112#if NR_CPUS == 1
Rusty Russell984f2f32008-11-08 20:24:19 +1100113/* Uniprocessor. Assume all masks are "1". */
114static inline unsigned int cpumask_first(const struct cpumask *srcp)
115{
116 return 0;
117}
118
119/* Valid inputs for n are -1 and 0. */
120static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
121{
122 return n+1;
123}
124
125static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
126{
127 return n+1;
128}
129
130static inline unsigned int cpumask_next_and(int n,
131 const struct cpumask *srcp,
132 const struct cpumask *andp)
133{
134 return n+1;
135}
136
137/* cpu must be a valid cpu, ie 0, so there's no other choice. */
138static inline unsigned int cpumask_any_but(const struct cpumask *mask,
139 unsigned int cpu)
140{
141 return 1;
142}
Rusty Russell2d3854a2008-11-05 13:39:10 +1100143
144#define for_each_cpu(cpu, mask) \
145 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
Paul E. McKenney8bd93a22010-02-22 17:04:59 -0800146#define for_each_cpu_not(cpu, mask) \
147 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
Rusty Russell2d3854a2008-11-05 13:39:10 +1100148#define for_each_cpu_and(cpu, mask, and) \
149 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
150#else
151/**
152 * cpumask_first - get the first cpu in a cpumask
153 * @srcp: the cpumask pointer
154 *
155 * Returns >= nr_cpu_ids if no cpus set.
156 */
157static inline unsigned int cpumask_first(const struct cpumask *srcp)
158{
159 return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
160}
161
162/**
163 * cpumask_next - get the next cpu in a cpumask
164 * @n: the cpu prior to the place to search (ie. return will be > @n)
165 * @srcp: the cpumask pointer
166 *
167 * Returns >= nr_cpu_ids if no further cpus set.
168 */
169static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
170{
171 /* -1 is a legal arg here. */
172 if (n != -1)
173 cpumask_check(n);
174 return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
175}
176
177/**
178 * cpumask_next_zero - get the next unset cpu in a cpumask
179 * @n: the cpu prior to the place to search (ie. return will be > @n)
180 * @srcp: the cpumask pointer
181 *
182 * Returns >= nr_cpu_ids if no further cpus unset.
183 */
184static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
185{
186 /* -1 is a legal arg here. */
187 if (n != -1)
188 cpumask_check(n);
189 return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
190}
191
192int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
193int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
194
Rusty Russell984f2f32008-11-08 20:24:19 +1100195/**
196 * for_each_cpu - iterate over every cpu in a mask
197 * @cpu: the (optionally unsigned) integer iterator
198 * @mask: the cpumask pointer
199 *
200 * After the loop, cpu is >= nr_cpu_ids.
201 */
Rusty Russell2d3854a2008-11-05 13:39:10 +1100202#define for_each_cpu(cpu, mask) \
203 for ((cpu) = -1; \
204 (cpu) = cpumask_next((cpu), (mask)), \
205 (cpu) < nr_cpu_ids;)
Rusty Russell984f2f32008-11-08 20:24:19 +1100206
207/**
Paul E. McKenney8bd93a22010-02-22 17:04:59 -0800208 * for_each_cpu_not - iterate over every cpu in a complemented mask
209 * @cpu: the (optionally unsigned) integer iterator
210 * @mask: the cpumask pointer
211 *
212 * After the loop, cpu is >= nr_cpu_ids.
213 */
214#define for_each_cpu_not(cpu, mask) \
215 for ((cpu) = -1; \
216 (cpu) = cpumask_next_zero((cpu), (mask)), \
217 (cpu) < nr_cpu_ids;)
218
219/**
Rusty Russell984f2f32008-11-08 20:24:19 +1100220 * for_each_cpu_and - iterate over every cpu in both masks
221 * @cpu: the (optionally unsigned) integer iterator
222 * @mask: the first cpumask pointer
223 * @and: the second cpumask pointer
224 *
225 * This saves a temporary CPU mask in many places. It is equivalent to:
226 * struct cpumask tmp;
227 * cpumask_and(&tmp, &mask, &and);
228 * for_each_cpu(cpu, &tmp)
229 * ...
230 *
231 * After the loop, cpu is >= nr_cpu_ids.
232 */
Rusty Russell2d3854a2008-11-05 13:39:10 +1100233#define for_each_cpu_and(cpu, mask, and) \
234 for ((cpu) = -1; \
235 (cpu) = cpumask_next_and((cpu), (mask), (and)), \
236 (cpu) < nr_cpu_ids;)
237#endif /* SMP */
238
239#define CPU_BITS_NONE \
240{ \
241 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
242}
243
244#define CPU_BITS_CPU0 \
245{ \
246 [0] = 1UL \
247}
248
249/**
250 * cpumask_set_cpu - set a cpu in a cpumask
251 * @cpu: cpu number (< nr_cpu_ids)
252 * @dstp: the cpumask pointer
253 */
254static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
255{
256 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
257}
258
259/**
260 * cpumask_clear_cpu - clear a cpu in a cpumask
261 * @cpu: cpu number (< nr_cpu_ids)
262 * @dstp: the cpumask pointer
263 */
264static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
265{
266 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
267}
268
269/**
270 * cpumask_test_cpu - test for a cpu in a cpumask
271 * @cpu: cpu number (< nr_cpu_ids)
272 * @cpumask: the cpumask pointer
273 *
274 * No static inline type checking - see Subtlety (1) above.
275 */
276#define cpumask_test_cpu(cpu, cpumask) \
Rusty Russellae7a47e2008-12-30 09:05:15 +1030277 test_bit(cpumask_check(cpu), cpumask_bits((cpumask)))
Rusty Russell2d3854a2008-11-05 13:39:10 +1100278
279/**
280 * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask
281 * @cpu: cpu number (< nr_cpu_ids)
282 * @cpumask: the cpumask pointer
283 *
284 * test_and_set_bit wrapper for cpumasks.
285 */
286static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
287{
288 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
289}
290
291/**
Xiao Guangrong54fdade2009-09-22 16:43:39 -0700292 * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask
293 * @cpu: cpu number (< nr_cpu_ids)
294 * @cpumask: the cpumask pointer
295 *
296 * test_and_clear_bit wrapper for cpumasks.
297 */
298static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
299{
300 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
301}
302
303/**
Rusty Russell2d3854a2008-11-05 13:39:10 +1100304 * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask
305 * @dstp: the cpumask pointer
306 */
307static inline void cpumask_setall(struct cpumask *dstp)
308{
309 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
310}
311
312/**
313 * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask
314 * @dstp: the cpumask pointer
315 */
316static inline void cpumask_clear(struct cpumask *dstp)
317{
318 bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
319}
320
321/**
322 * cpumask_and - *dstp = *src1p & *src2p
323 * @dstp: the cpumask result
324 * @src1p: the first input
325 * @src2p: the second input
326 */
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700327static inline int cpumask_and(struct cpumask *dstp,
Rusty Russell2d3854a2008-11-05 13:39:10 +1100328 const struct cpumask *src1p,
329 const struct cpumask *src2p)
330{
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700331 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
Rusty Russell2d3854a2008-11-05 13:39:10 +1100332 cpumask_bits(src2p), nr_cpumask_bits);
333}
334
335/**
336 * cpumask_or - *dstp = *src1p | *src2p
337 * @dstp: the cpumask result
338 * @src1p: the first input
339 * @src2p: the second input
340 */
341static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
342 const struct cpumask *src2p)
343{
344 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
345 cpumask_bits(src2p), nr_cpumask_bits);
346}
347
348/**
349 * cpumask_xor - *dstp = *src1p ^ *src2p
350 * @dstp: the cpumask result
351 * @src1p: the first input
352 * @src2p: the second input
353 */
354static inline void cpumask_xor(struct cpumask *dstp,
355 const struct cpumask *src1p,
356 const struct cpumask *src2p)
357{
358 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
359 cpumask_bits(src2p), nr_cpumask_bits);
360}
361
362/**
363 * cpumask_andnot - *dstp = *src1p & ~*src2p
364 * @dstp: the cpumask result
365 * @src1p: the first input
366 * @src2p: the second input
367 */
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700368static inline int cpumask_andnot(struct cpumask *dstp,
Rusty Russell2d3854a2008-11-05 13:39:10 +1100369 const struct cpumask *src1p,
370 const struct cpumask *src2p)
371{
Linus Torvaldsf4b03732009-08-21 09:26:15 -0700372 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
Rusty Russell2d3854a2008-11-05 13:39:10 +1100373 cpumask_bits(src2p), nr_cpumask_bits);
374}
375
376/**
377 * cpumask_complement - *dstp = ~*srcp
378 * @dstp: the cpumask result
379 * @srcp: the input to invert
380 */
381static inline void cpumask_complement(struct cpumask *dstp,
382 const struct cpumask *srcp)
383{
384 bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
385 nr_cpumask_bits);
386}
387
388/**
389 * cpumask_equal - *src1p == *src2p
390 * @src1p: the first input
391 * @src2p: the second input
392 */
393static inline bool cpumask_equal(const struct cpumask *src1p,
394 const struct cpumask *src2p)
395{
396 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
397 nr_cpumask_bits);
398}
399
400/**
401 * cpumask_intersects - (*src1p & *src2p) != 0
402 * @src1p: the first input
403 * @src2p: the second input
404 */
405static inline bool cpumask_intersects(const struct cpumask *src1p,
406 const struct cpumask *src2p)
407{
408 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
409 nr_cpumask_bits);
410}
411
412/**
413 * cpumask_subset - (*src1p & ~*src2p) == 0
414 * @src1p: the first input
415 * @src2p: the second input
416 */
417static inline int cpumask_subset(const struct cpumask *src1p,
418 const struct cpumask *src2p)
419{
420 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
421 nr_cpumask_bits);
422}
423
424/**
425 * cpumask_empty - *srcp == 0
426 * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear.
427 */
428static inline bool cpumask_empty(const struct cpumask *srcp)
429{
430 return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
431}
432
433/**
434 * cpumask_full - *srcp == 0xFFFFFFFF...
435 * @srcp: the cpumask to that all cpus < nr_cpu_ids are set.
436 */
437static inline bool cpumask_full(const struct cpumask *srcp)
438{
439 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
440}
441
442/**
443 * cpumask_weight - Count of bits in *srcp
444 * @srcp: the cpumask to count bits (< nr_cpu_ids) in.
445 */
446static inline unsigned int cpumask_weight(const struct cpumask *srcp)
447{
448 return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
449}
450
451/**
452 * cpumask_shift_right - *dstp = *srcp >> n
453 * @dstp: the cpumask result
454 * @srcp: the input to shift
455 * @n: the number of bits to shift by
456 */
457static inline void cpumask_shift_right(struct cpumask *dstp,
458 const struct cpumask *srcp, int n)
459{
460 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
461 nr_cpumask_bits);
462}
463
464/**
465 * cpumask_shift_left - *dstp = *srcp << n
466 * @dstp: the cpumask result
467 * @srcp: the input to shift
468 * @n: the number of bits to shift by
469 */
470static inline void cpumask_shift_left(struct cpumask *dstp,
471 const struct cpumask *srcp, int n)
472{
473 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
474 nr_cpumask_bits);
475}
476
477/**
478 * cpumask_copy - *dstp = *srcp
479 * @dstp: the result
480 * @srcp: the input cpumask
481 */
482static inline void cpumask_copy(struct cpumask *dstp,
483 const struct cpumask *srcp)
484{
485 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
486}
487
488/**
489 * cpumask_any - pick a "random" cpu from *srcp
490 * @srcp: the input cpumask
491 *
492 * Returns >= nr_cpu_ids if no cpus set.
493 */
494#define cpumask_any(srcp) cpumask_first(srcp)
495
496/**
497 * cpumask_first_and - return the first cpu from *srcp1 & *srcp2
498 * @src1p: the first input
499 * @src2p: the second input
500 *
501 * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and().
502 */
503#define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
504
505/**
506 * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2
507 * @mask1: the first input cpumask
508 * @mask2: the second input cpumask
509 *
510 * Returns >= nr_cpu_ids if no cpus set.
511 */
512#define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
513
514/**
Rusty Russellcd83e422008-11-07 11:12:29 +1100515 * cpumask_of - the cpumask containing just a given cpu
516 * @cpu: the cpu (<= nr_cpu_ids)
517 */
518#define cpumask_of(cpu) (get_cpu_mask(cpu))
519
520/**
Rusty Russell29c01772008-12-13 21:20:25 +1030521 * cpumask_scnprintf - print a cpumask into a string as comma-separated hex
522 * @buf: the buffer to sprintf into
523 * @len: the length of the buffer
524 * @srcp: the cpumask to print
525 *
526 * If len is zero, returns zero. Otherwise returns the length of the
527 * (nul-terminated) @buf string.
528 */
529static inline int cpumask_scnprintf(char *buf, int len,
530 const struct cpumask *srcp)
531{
Rusty Russellae7a47e2008-12-30 09:05:15 +1030532 return bitmap_scnprintf(buf, len, cpumask_bits(srcp), nr_cpumask_bits);
Rusty Russell29c01772008-12-13 21:20:25 +1030533}
534
535/**
536 * cpumask_parse_user - extract a cpumask from a user string
537 * @buf: the buffer to extract from
538 * @len: the length of the buffer
539 * @dstp: the cpumask to set.
540 *
541 * Returns -errno, or 0 for success.
542 */
543static inline int cpumask_parse_user(const char __user *buf, int len,
544 struct cpumask *dstp)
545{
Rusty Russellae7a47e2008-12-30 09:05:15 +1030546 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
Rusty Russell29c01772008-12-13 21:20:25 +1030547}
548
549/**
550 * cpulist_scnprintf - print a cpumask into a string as comma-separated list
551 * @buf: the buffer to sprintf into
552 * @len: the length of the buffer
553 * @srcp: the cpumask to print
554 *
555 * If len is zero, returns zero. Otherwise returns the length of the
556 * (nul-terminated) @buf string.
557 */
558static inline int cpulist_scnprintf(char *buf, int len,
559 const struct cpumask *srcp)
560{
Rusty Russellae7a47e2008-12-30 09:05:15 +1030561 return bitmap_scnlistprintf(buf, len, cpumask_bits(srcp),
562 nr_cpumask_bits);
Rusty Russell29c01772008-12-13 21:20:25 +1030563}
564
565/**
566 * cpulist_parse_user - extract a cpumask from a user string of ranges
567 * @buf: the buffer to extract from
568 * @len: the length of the buffer
569 * @dstp: the cpumask to set.
570 *
571 * Returns -errno, or 0 for success.
572 */
573static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
574{
Rusty Russellae7a47e2008-12-30 09:05:15 +1030575 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100576}
577
578/**
579 * cpumask_size - size to allocate for a 'struct cpumask' in bytes
580 *
581 * This will eventually be a runtime variable, depending on nr_cpu_ids.
582 */
583static inline size_t cpumask_size(void)
584{
585 /* FIXME: Once all cpumask assignments are eliminated, this
586 * can be nr_cpumask_bits */
587 return BITS_TO_LONGS(NR_CPUS) * sizeof(long);
588}
589
590/*
591 * cpumask_var_t: struct cpumask for stack usage.
592 *
593 * Oh, the wicked games we play! In order to make kernel coding a
594 * little more difficult, we typedef cpumask_var_t to an array or a
595 * pointer: doing &mask on an array is a noop, so it still works.
596 *
597 * ie.
598 * cpumask_var_t tmpmask;
599 * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
600 * return -ENOMEM;
601 *
602 * ... use 'tmpmask' like a normal struct cpumask * ...
603 *
604 * free_cpumask_var(tmpmask);
605 */
606#ifdef CONFIG_CPUMASK_OFFSTACK
607typedef struct cpumask *cpumask_var_t;
608
Mike Travis7b4967c2008-12-19 16:56:37 +1030609bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100610bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
Yinghai Lu0281b5d2009-06-06 14:50:36 -0700611bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
612bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100613void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
614void free_cpumask_var(cpumask_var_t mask);
Rusty Russellcd83e422008-11-07 11:12:29 +1100615void free_bootmem_cpumask_var(cpumask_var_t mask);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100616
617#else
618typedef struct cpumask cpumask_var_t[1];
619
620static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
621{
622 return true;
623}
624
Mike Travis7b4967c2008-12-19 16:56:37 +1030625static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
626 int node)
627{
628 return true;
629}
630
Yinghai Lu0281b5d2009-06-06 14:50:36 -0700631static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
632{
633 cpumask_clear(*mask);
634 return true;
635}
636
637static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
638 int node)
639{
640 cpumask_clear(*mask);
641 return true;
642}
643
Rusty Russell2d3854a2008-11-05 13:39:10 +1100644static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
645{
646}
647
648static inline void free_cpumask_var(cpumask_var_t mask)
649{
650}
Rusty Russellcd83e422008-11-07 11:12:29 +1100651
652static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
653{
654}
Rusty Russell2d3854a2008-11-05 13:39:10 +1100655#endif /* CONFIG_CPUMASK_OFFSTACK */
656
Rusty Russell2d3854a2008-11-05 13:39:10 +1100657/* It's common to want to use cpu_all_mask in struct member initializers,
658 * so it has to refer to an address rather than a pointer. */
659extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
660#define cpu_all_mask to_cpumask(cpu_all_bits)
661
662/* First bits of cpu_bit_bitmap are in fact unset. */
663#define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
664
Rusty Russellae7a47e2008-12-30 09:05:15 +1030665#define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
666#define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
667#define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
668
Rusty Russell2d3854a2008-11-05 13:39:10 +1100669/* Wrappers for arch boot code to manipulate normally-constant masks */
Rusty Russell3fa41522008-12-30 09:05:16 +1030670void set_cpu_possible(unsigned int cpu, bool possible);
671void set_cpu_present(unsigned int cpu, bool present);
672void set_cpu_online(unsigned int cpu, bool online);
673void set_cpu_active(unsigned int cpu, bool active);
674void init_cpu_present(const struct cpumask *src);
675void init_cpu_possible(const struct cpumask *src);
676void init_cpu_online(const struct cpumask *src);
Rusty Russell6ba2ef72009-09-24 09:34:53 -0600677
678/**
679 * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask *
680 * @bitmap: the bitmap
681 *
682 * There are a few places where cpumask_var_t isn't appropriate and
683 * static cpumasks must be used (eg. very early boot), yet we don't
684 * expose the definition of 'struct cpumask'.
685 *
686 * This does the conversion, and can be used as a constant initializer.
687 */
688#define to_cpumask(bitmap) \
689 ((struct cpumask *)(1 ? (bitmap) \
690 : (void *)sizeof(__check_is_bitmap(bitmap))))
691
692static inline int __check_is_bitmap(const unsigned long *bitmap)
693{
694 return 1;
695}
696
697/*
698 * Special-case data structure for "single bit set only" constant CPU masks.
699 *
700 * We pre-generate all the 64 (or 32) possible bit positions, with enough
701 * padding to the left and the right, and return the constant pointer
702 * appropriately offset.
703 */
704extern const unsigned long
705 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
706
707static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
708{
709 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
710 p -= cpu / BITS_PER_LONG;
711 return to_cpumask(p);
712}
713
714#define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
715
716#if NR_CPUS <= BITS_PER_LONG
717#define CPU_BITS_ALL \
718{ \
719 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
720}
721
722#else /* NR_CPUS > BITS_PER_LONG */
723
724#define CPU_BITS_ALL \
725{ \
726 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
727 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
728}
729#endif /* NR_CPUS > BITS_PER_LONG */
730
731/*
732 *
733 * From here down, all obsolete. Use cpumask_ variants!
734 *
735 */
736#ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
737/* These strip const, as traditionally they weren't const. */
738#define cpu_possible_map (*(cpumask_t *)cpu_possible_mask)
739#define cpu_online_map (*(cpumask_t *)cpu_online_mask)
740#define cpu_present_map (*(cpumask_t *)cpu_present_mask)
741#define cpu_active_map (*(cpumask_t *)cpu_active_mask)
742
743#define cpumask_of_cpu(cpu) (*get_cpu_mask(cpu))
744
745#define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
746
747#if NR_CPUS <= BITS_PER_LONG
748
749#define CPU_MASK_ALL \
750(cpumask_t) { { \
751 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
752} }
753
754#else
755
756#define CPU_MASK_ALL \
757(cpumask_t) { { \
758 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
759 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
760} }
761
762#endif
763
764#define CPU_MASK_NONE \
765(cpumask_t) { { \
766 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
767} }
768
769#define CPU_MASK_CPU0 \
770(cpumask_t) { { \
771 [0] = 1UL \
772} }
773
774#if NR_CPUS == 1
775#define first_cpu(src) ({ (void)(src); 0; })
776#define next_cpu(n, src) ({ (void)(src); 1; })
777#define any_online_cpu(mask) 0
778#define for_each_cpu_mask(cpu, mask) \
779 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
780#else /* NR_CPUS > 1 */
781int __first_cpu(const cpumask_t *srcp);
782int __next_cpu(int n, const cpumask_t *srcp);
783int __any_online_cpu(const cpumask_t *mask);
784
785#define first_cpu(src) __first_cpu(&(src))
786#define next_cpu(n, src) __next_cpu((n), &(src))
787#define any_online_cpu(mask) __any_online_cpu(&(mask))
788#define for_each_cpu_mask(cpu, mask) \
789 for ((cpu) = -1; \
790 (cpu) = next_cpu((cpu), (mask)), \
791 (cpu) < NR_CPUS; )
792#endif /* SMP */
793
794#if NR_CPUS <= 64
795
796#define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
797
798#else /* NR_CPUS > 64 */
799
800int __next_cpu_nr(int n, const cpumask_t *srcp);
801#define for_each_cpu_mask_nr(cpu, mask) \
802 for ((cpu) = -1; \
803 (cpu) = __next_cpu_nr((cpu), &(mask)), \
804 (cpu) < nr_cpu_ids; )
805
806#endif /* NR_CPUS > 64 */
807
808#define cpus_addr(src) ((src).bits)
809
810#define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
811static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
812{
813 set_bit(cpu, dstp->bits);
814}
815
816#define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
817static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
818{
819 clear_bit(cpu, dstp->bits);
820}
821
822#define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
823static inline void __cpus_setall(cpumask_t *dstp, int nbits)
824{
825 bitmap_fill(dstp->bits, nbits);
826}
827
828#define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
829static inline void __cpus_clear(cpumask_t *dstp, int nbits)
830{
831 bitmap_zero(dstp->bits, nbits);
832}
833
834/* No static inline type checking - see Subtlety (1) above. */
835#define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
836
837#define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
838static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
839{
840 return test_and_set_bit(cpu, addr->bits);
841}
842
843#define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
844static inline int __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
845 const cpumask_t *src2p, int nbits)
846{
847 return bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
848}
849
850#define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
851static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
852 const cpumask_t *src2p, int nbits)
853{
854 bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
855}
856
857#define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
858static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
859 const cpumask_t *src2p, int nbits)
860{
861 bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
862}
863
864#define cpus_andnot(dst, src1, src2) \
865 __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
866static inline int __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
867 const cpumask_t *src2p, int nbits)
868{
869 return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
870}
871
872#define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
873static inline int __cpus_equal(const cpumask_t *src1p,
874 const cpumask_t *src2p, int nbits)
875{
876 return bitmap_equal(src1p->bits, src2p->bits, nbits);
877}
878
879#define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
880static inline int __cpus_intersects(const cpumask_t *src1p,
881 const cpumask_t *src2p, int nbits)
882{
883 return bitmap_intersects(src1p->bits, src2p->bits, nbits);
884}
885
886#define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
887static inline int __cpus_subset(const cpumask_t *src1p,
888 const cpumask_t *src2p, int nbits)
889{
890 return bitmap_subset(src1p->bits, src2p->bits, nbits);
891}
892
893#define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
894static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
895{
896 return bitmap_empty(srcp->bits, nbits);
897}
898
899#define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
900static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
901{
902 return bitmap_weight(srcp->bits, nbits);
903}
904
905#define cpus_shift_left(dst, src, n) \
906 __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
907static inline void __cpus_shift_left(cpumask_t *dstp,
908 const cpumask_t *srcp, int n, int nbits)
909{
910 bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
911}
912#endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914#endif /* __LINUX_CPUMASK_H */