1#ifndef __LINUX_CPUMASK_H
2#define __LINUX_CPUMASK_H
3
4
5
6
7
8
9#include <linux/kernel.h>
10#include <linux/threads.h>
11#include <linux/bitmap.h>
12
13typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
14
15
16
17
18
19
20
21
22#define cpumask_bits(maskp) ((maskp)->bits)
23
24#if NR_CPUS == 1
25#define nr_cpu_ids 1
26#else
27extern int nr_cpu_ids;
28#endif
29
30#ifdef CONFIG_CPUMASK_OFFSTACK
31
32
33#define nr_cpumask_bits nr_cpu_ids
34#else
35#define nr_cpumask_bits NR_CPUS
36#endif
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78extern 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
83#if NR_CPUS > 1
84#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)
87#define num_active_cpus() cpumask_weight(cpu_active_mask)
88#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)
92#else
93#define num_online_cpus() 1
94#define num_possible_cpus() 1
95#define num_present_cpus() 1
96#define num_active_cpus() 1
97#define cpu_online(cpu) ((cpu) == 0)
98#define cpu_possible(cpu) ((cpu) == 0)
99#define cpu_present(cpu) ((cpu) == 0)
100#define cpu_active(cpu) ((cpu) == 0)
101#endif
102
103
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
109 return cpu;
110}
111
112#if NR_CPUS == 1
113
114static inline unsigned int cpumask_first(const struct cpumask *srcp)
115{
116 return 0;
117}
118
119
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
138static inline unsigned int cpumask_any_but(const struct cpumask *mask,
139 unsigned int cpu)
140{
141 return 1;
142}
143
144#define for_each_cpu(cpu, mask) \
145 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
146#define for_each_cpu_and(cpu, mask, and) \
147 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and)
148#else
149
150
151
152
153
154
155static inline unsigned int cpumask_first(const struct cpumask *srcp)
156{
157 return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits);
158}
159
160
161
162
163
164
165
166
167static inline unsigned int cpumask_next(int n, const struct cpumask *srcp)
168{
169
170 if (n != -1)
171 cpumask_check(n);
172 return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
173}
174
175
176
177
178
179
180
181
182static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp)
183{
184
185 if (n != -1)
186 cpumask_check(n);
187 return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1);
188}
189
190int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *);
191int cpumask_any_but(const struct cpumask *mask, unsigned int cpu);
192
193
194
195
196
197
198
199
200#define for_each_cpu(cpu, mask) \
201 for ((cpu) = -1; \
202 (cpu) = cpumask_next((cpu), (mask)), \
203 (cpu) < nr_cpu_ids;)
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219#define for_each_cpu_and(cpu, mask, and) \
220 for ((cpu) = -1; \
221 (cpu) = cpumask_next_and((cpu), (mask), (and)), \
222 (cpu) < nr_cpu_ids;)
223#endif
224
225#define CPU_BITS_NONE \
226{ \
227 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
228}
229
230#define CPU_BITS_CPU0 \
231{ \
232 [0] = 1UL \
233}
234
235
236
237
238
239
240static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp)
241{
242 set_bit(cpumask_check(cpu), cpumask_bits(dstp));
243}
244
245
246
247
248
249
250static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp)
251{
252 clear_bit(cpumask_check(cpu), cpumask_bits(dstp));
253}
254
255
256
257
258
259
260
261
262#define cpumask_test_cpu(cpu, cpumask) \
263 test_bit(cpumask_check(cpu), cpumask_bits((cpumask)))
264
265
266
267
268
269
270
271
272static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask)
273{
274 return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask));
275}
276
277
278
279
280
281
282
283
284static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask)
285{
286 return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask));
287}
288
289
290
291
292
293static inline void cpumask_setall(struct cpumask *dstp)
294{
295 bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits);
296}
297
298
299
300
301
302static inline void cpumask_clear(struct cpumask *dstp)
303{
304 bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits);
305}
306
307
308
309
310
311
312
313static inline int cpumask_and(struct cpumask *dstp,
314 const struct cpumask *src1p,
315 const struct cpumask *src2p)
316{
317 return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p),
318 cpumask_bits(src2p), nr_cpumask_bits);
319}
320
321
322
323
324
325
326
327static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p,
328 const struct cpumask *src2p)
329{
330 bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p),
331 cpumask_bits(src2p), nr_cpumask_bits);
332}
333
334
335
336
337
338
339
340static inline void cpumask_xor(struct cpumask *dstp,
341 const struct cpumask *src1p,
342 const struct cpumask *src2p)
343{
344 bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p),
345 cpumask_bits(src2p), nr_cpumask_bits);
346}
347
348
349
350
351
352
353
354static inline int cpumask_andnot(struct cpumask *dstp,
355 const struct cpumask *src1p,
356 const struct cpumask *src2p)
357{
358 return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p),
359 cpumask_bits(src2p), nr_cpumask_bits);
360}
361
362
363
364
365
366
367static inline void cpumask_complement(struct cpumask *dstp,
368 const struct cpumask *srcp)
369{
370 bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp),
371 nr_cpumask_bits);
372}
373
374
375
376
377
378
379static inline bool cpumask_equal(const struct cpumask *src1p,
380 const struct cpumask *src2p)
381{
382 return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p),
383 nr_cpumask_bits);
384}
385
386
387
388
389
390
391static inline bool cpumask_intersects(const struct cpumask *src1p,
392 const struct cpumask *src2p)
393{
394 return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p),
395 nr_cpumask_bits);
396}
397
398
399
400
401
402
403static inline int cpumask_subset(const struct cpumask *src1p,
404 const struct cpumask *src2p)
405{
406 return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p),
407 nr_cpumask_bits);
408}
409
410
411
412
413
414static inline bool cpumask_empty(const struct cpumask *srcp)
415{
416 return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits);
417}
418
419
420
421
422
423static inline bool cpumask_full(const struct cpumask *srcp)
424{
425 return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits);
426}
427
428
429
430
431
432static inline unsigned int cpumask_weight(const struct cpumask *srcp)
433{
434 return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits);
435}
436
437
438
439
440
441
442
443static inline void cpumask_shift_right(struct cpumask *dstp,
444 const struct cpumask *srcp, int n)
445{
446 bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n,
447 nr_cpumask_bits);
448}
449
450
451
452
453
454
455
456static inline void cpumask_shift_left(struct cpumask *dstp,
457 const struct cpumask *srcp, int n)
458{
459 bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n,
460 nr_cpumask_bits);
461}
462
463
464
465
466
467
468static inline void cpumask_copy(struct cpumask *dstp,
469 const struct cpumask *srcp)
470{
471 bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits);
472}
473
474
475
476
477
478
479
480#define cpumask_any(srcp) cpumask_first(srcp)
481
482
483
484
485
486
487
488
489#define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p))
490
491
492
493
494
495
496
497
498#define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2))
499
500
501
502
503
504#define cpumask_of(cpu) (get_cpu_mask(cpu))
505
506
507
508
509
510
511
512
513
514
515static inline int cpumask_scnprintf(char *buf, int len,
516 const struct cpumask *srcp)
517{
518 return bitmap_scnprintf(buf, len, cpumask_bits(srcp), nr_cpumask_bits);
519}
520
521
522
523
524
525
526
527
528
529static inline int cpumask_parse_user(const char __user *buf, int len,
530 struct cpumask *dstp)
531{
532 return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpumask_bits);
533}
534
535
536
537
538
539
540
541
542
543
544static inline int cpulist_scnprintf(char *buf, int len,
545 const struct cpumask *srcp)
546{
547 return bitmap_scnlistprintf(buf, len, cpumask_bits(srcp),
548 nr_cpumask_bits);
549}
550
551
552
553
554
555
556
557
558
559static inline int cpulist_parse(const char *buf, struct cpumask *dstp)
560{
561 return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpumask_bits);
562}
563
564
565
566
567
568
569static inline size_t cpumask_size(void)
570{
571
572
573 return BITS_TO_LONGS(NR_CPUS) * sizeof(long);
574}
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592#ifdef CONFIG_CPUMASK_OFFSTACK
593typedef struct cpumask *cpumask_var_t;
594
595bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
596bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
597bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node);
598bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags);
599void alloc_bootmem_cpumask_var(cpumask_var_t *mask);
600void free_cpumask_var(cpumask_var_t mask);
601void free_bootmem_cpumask_var(cpumask_var_t mask);
602
603#else
604typedef struct cpumask cpumask_var_t[1];
605
606static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
607{
608 return true;
609}
610
611static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
612 int node)
613{
614 return true;
615}
616
617static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
618{
619 cpumask_clear(*mask);
620 return true;
621}
622
623static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags,
624 int node)
625{
626 cpumask_clear(*mask);
627 return true;
628}
629
630static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask)
631{
632}
633
634static inline void free_cpumask_var(cpumask_var_t mask)
635{
636}
637
638static inline void free_bootmem_cpumask_var(cpumask_var_t mask)
639{
640}
641#endif
642
643
644
645extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS);
646#define cpu_all_mask to_cpumask(cpu_all_bits)
647
648
649#define cpu_none_mask to_cpumask(cpu_bit_bitmap[0])
650
651#define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask)
652#define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask)
653#define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask)
654
655
656void set_cpu_possible(unsigned int cpu, bool possible);
657void set_cpu_present(unsigned int cpu, bool present);
658void set_cpu_online(unsigned int cpu, bool online);
659void set_cpu_active(unsigned int cpu, bool active);
660void init_cpu_present(const struct cpumask *src);
661void init_cpu_possible(const struct cpumask *src);
662void init_cpu_online(const struct cpumask *src);
663
664
665
666
667
668
669
670
671
672
673
674#define to_cpumask(bitmap) \
675 ((struct cpumask *)(1 ? (bitmap) \
676 : (void *)sizeof(__check_is_bitmap(bitmap))))
677
678static inline int __check_is_bitmap(const unsigned long *bitmap)
679{
680 return 1;
681}
682
683
684
685
686
687
688
689
690extern const unsigned long
691 cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)];
692
693static inline const struct cpumask *get_cpu_mask(unsigned int cpu)
694{
695 const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG];
696 p -= cpu / BITS_PER_LONG;
697 return to_cpumask(p);
698}
699
700#define cpu_is_offline(cpu) unlikely(!cpu_online(cpu))
701
702#if NR_CPUS <= BITS_PER_LONG
703#define CPU_BITS_ALL \
704{ \
705 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
706}
707
708#else
709
710#define CPU_BITS_ALL \
711{ \
712 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
713 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
714}
715#endif
716
717
718
719
720
721
722#ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS
723
724#define cpu_possible_map (*(cpumask_t *)cpu_possible_mask)
725#define cpu_online_map (*(cpumask_t *)cpu_online_mask)
726#define cpu_present_map (*(cpumask_t *)cpu_present_mask)
727#define cpu_active_map (*(cpumask_t *)cpu_active_mask)
728
729#define cpumask_of_cpu(cpu) (*get_cpu_mask(cpu))
730
731#define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
732
733#if NR_CPUS <= BITS_PER_LONG
734
735#define CPU_MASK_ALL \
736(cpumask_t) { { \
737 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
738} }
739
740#else
741
742#define CPU_MASK_ALL \
743(cpumask_t) { { \
744 [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \
745 [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD \
746} }
747
748#endif
749
750#define CPU_MASK_NONE \
751(cpumask_t) { { \
752 [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \
753} }
754
755#define CPU_MASK_CPU0 \
756(cpumask_t) { { \
757 [0] = 1UL \
758} }
759
760#if NR_CPUS == 1
761#define first_cpu(src) ({ (void)(src); 0; })
762#define next_cpu(n, src) ({ (void)(src); 1; })
763#define any_online_cpu(mask) 0
764#define for_each_cpu_mask(cpu, mask) \
765 for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask)
766#else
767int __first_cpu(const cpumask_t *srcp);
768int __next_cpu(int n, const cpumask_t *srcp);
769int __any_online_cpu(const cpumask_t *mask);
770
771#define first_cpu(src) __first_cpu(&(src))
772#define next_cpu(n, src) __next_cpu((n), &(src))
773#define any_online_cpu(mask) __any_online_cpu(&(mask))
774#define for_each_cpu_mask(cpu, mask) \
775 for ((cpu) = -1; \
776 (cpu) = next_cpu((cpu), (mask)), \
777 (cpu) < NR_CPUS; )
778#endif
779
780#if NR_CPUS <= 64
781
782#define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask)
783
784#else
785
786int __next_cpu_nr(int n, const cpumask_t *srcp);
787#define for_each_cpu_mask_nr(cpu, mask) \
788 for ((cpu) = -1; \
789 (cpu) = __next_cpu_nr((cpu), &(mask)), \
790 (cpu) < nr_cpu_ids; )
791
792#endif
793
794#define cpus_addr(src) ((src).bits)
795
796#define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
797static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
798{
799 set_bit(cpu, dstp->bits);
800}
801
802#define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
803static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
804{
805 clear_bit(cpu, dstp->bits);
806}
807
808#define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
809static inline void __cpus_setall(cpumask_t *dstp, int nbits)
810{
811 bitmap_fill(dstp->bits, nbits);
812}
813
814#define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
815static inline void __cpus_clear(cpumask_t *dstp, int nbits)
816{
817 bitmap_zero(dstp->bits, nbits);
818}
819
820
821#define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
822
823#define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
824static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
825{
826 return test_and_set_bit(cpu, addr->bits);
827}
828
829#define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
830static inline int __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
831 const cpumask_t *src2p, int nbits)
832{
833 return bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
834}
835
836#define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
837static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
838 const cpumask_t *src2p, int nbits)
839{
840 bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
841}
842
843#define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
844static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
845 const cpumask_t *src2p, int nbits)
846{
847 bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
848}
849
850#define cpus_andnot(dst, src1, src2) \
851 __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
852static inline int __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
853 const cpumask_t *src2p, int nbits)
854{
855 return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
856}
857
858#define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
859static inline int __cpus_equal(const cpumask_t *src1p,
860 const cpumask_t *src2p, int nbits)
861{
862 return bitmap_equal(src1p->bits, src2p->bits, nbits);
863}
864
865#define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
866static inline int __cpus_intersects(const cpumask_t *src1p,
867 const cpumask_t *src2p, int nbits)
868{
869 return bitmap_intersects(src1p->bits, src2p->bits, nbits);
870}
871
872#define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
873static inline int __cpus_subset(const cpumask_t *src1p,
874 const cpumask_t *src2p, int nbits)
875{
876 return bitmap_subset(src1p->bits, src2p->bits, nbits);
877}
878
879#define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
880static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
881{
882 return bitmap_empty(srcp->bits, nbits);
883}
884
885#define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
886static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
887{
888 return bitmap_weight(srcp->bits, nbits);
889}
890
891#define cpus_shift_left(dst, src, n) \
892 __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
893static inline void __cpus_shift_left(cpumask_t *dstp,
894 const cpumask_t *srcp, int n, int nbits)
895{
896 bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
897}
898#endif
899
900#endif
901