Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __LINUX_CPUMASK_H |
| 2 | #define __LINUX_CPUMASK_H |
| 3 | |
| 4 | /* |
| 5 | * Cpumasks provide a bitmap suitable for representing the |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 6 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | #include <linux/threads.h> |
| 11 | #include <linux/bitmap.h> |
Paul Gortmaker | 187f188 | 2011-11-23 20:12:59 -0500 | [diff] [blame] | 12 | #include <linux/bug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 14 | typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Rusty Russell | ae7a47e | 2008-12-30 09:05:15 +1030 | [diff] [blame] | 16 | /** |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 17 | * cpumask_bits - get the bits in a cpumask |
| 18 | * @maskp: the struct cpumask * |
Rusty Russell | ae7a47e | 2008-12-30 09:05:15 +1030 | [diff] [blame] | 19 | * |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 20 | * You should only assume nr_cpu_ids bits of this mask are valid. This is |
| 21 | * a macro so it's const-correct. |
Rusty Russell | ae7a47e | 2008-12-30 09:05:15 +1030 | [diff] [blame] | 22 | */ |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 23 | #define cpumask_bits(maskp) ((maskp)->bits) |
Paul Jackson | 7ea931c | 2008-04-28 02:12:29 -0700 | [diff] [blame] | 24 | |
Tejun Heo | f1bbc03 | 2015-02-13 14:36:57 -0800 | [diff] [blame] | 25 | /** |
| 26 | * cpumask_pr_args - printf args to output a cpumask |
| 27 | * @maskp: cpumask to be printed |
| 28 | * |
| 29 | * Can be used to provide arguments for '%*pb[l]' when printing a cpumask. |
| 30 | */ |
| 31 | #define cpumask_pr_args(maskp) nr_cpu_ids, cpumask_bits(maskp) |
| 32 | |
Mike Travis | 41df0d61 | 2008-05-12 21:21:13 +0200 | [diff] [blame] | 33 | #if NR_CPUS == 1 |
Mike Travis | 41df0d61 | 2008-05-12 21:21:13 +0200 | [diff] [blame] | 34 | #define nr_cpu_ids 1 |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 35 | #else |
Mike Travis | 41df0d61 | 2008-05-12 21:21:13 +0200 | [diff] [blame] | 36 | extern int nr_cpu_ids; |
Mike Travis | 41df0d61 | 2008-05-12 21:21:13 +0200 | [diff] [blame] | 37 | #endif |
| 38 | |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 39 | #ifdef CONFIG_CPUMASK_OFFSTACK |
| 40 | /* Assuming NR_CPUS is huge, a runtime limit is more efficient. Also, |
| 41 | * not all bits may be allocated. */ |
| 42 | #define nr_cpumask_bits nr_cpu_ids |
| 43 | #else |
| 44 | #define nr_cpumask_bits NR_CPUS |
| 45 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * The following particular system cpumasks and operations manage |
Rusty Russell | b3199c0 | 2008-12-30 09:05:14 +1030 | [diff] [blame] | 49 | * possible, present, active and online cpus. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | * |
Rusty Russell | b3199c0 | 2008-12-30 09:05:14 +1030 | [diff] [blame] | 51 | * cpu_possible_mask- has bit 'cpu' set iff cpu is populatable |
| 52 | * cpu_present_mask - has bit 'cpu' set iff cpu is populated |
| 53 | * cpu_online_mask - has bit 'cpu' set iff cpu available to scheduler |
| 54 | * cpu_active_mask - has bit 'cpu' set iff cpu available to migration |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | * |
Rusty Russell | b3199c0 | 2008-12-30 09:05:14 +1030 | [diff] [blame] | 56 | * If !CONFIG_HOTPLUG_CPU, present == possible, and active == online. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | * |
Rusty Russell | b3199c0 | 2008-12-30 09:05:14 +1030 | [diff] [blame] | 58 | * The cpu_possible_mask is fixed at boot time, as the set of CPU id's |
| 59 | * that it is possible might ever be plugged in at anytime during the |
| 60 | * life of that system boot. The cpu_present_mask is dynamic(*), |
| 61 | * representing which CPUs are currently plugged in. And |
| 62 | * cpu_online_mask is the dynamic subset of cpu_present_mask, |
| 63 | * indicating those CPUs available for scheduling. |
| 64 | * |
| 65 | * If HOTPLUG is enabled, then cpu_possible_mask is forced to have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | * all NR_CPUS bits set, otherwise it is just the set of CPUs that |
| 67 | * ACPI reports present at boot. |
| 68 | * |
Rusty Russell | b3199c0 | 2008-12-30 09:05:14 +1030 | [diff] [blame] | 69 | * If HOTPLUG is enabled, then cpu_present_mask varies dynamically, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * depending on what ACPI reports as currently plugged in, otherwise |
Rusty Russell | b3199c0 | 2008-12-30 09:05:14 +1030 | [diff] [blame] | 71 | * cpu_present_mask is just a copy of cpu_possible_mask. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * |
Rusty Russell | b3199c0 | 2008-12-30 09:05:14 +1030 | [diff] [blame] | 73 | * (*) Well, cpu_present_mask is dynamic in the hotplug case. If not |
| 74 | * hotplug, it's a copy of cpu_possible_mask, hence fixed at boot. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | * |
| 76 | * Subtleties: |
| 77 | * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode |
| 78 | * assumption that their single CPU is online. The UP |
Rusty Russell | b3199c0 | 2008-12-30 09:05:14 +1030 | [diff] [blame] | 79 | * cpu_{online,possible,present}_masks are placebos. Changing them |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | * will have no useful affect on the following num_*_cpus() |
| 81 | * and cpu_*() macros in the UP case. This ugliness is a UP |
| 82 | * optimization - don't waste any instructions or memory references |
| 83 | * asking if you're online or how many CPUs there are if there is |
| 84 | * only one CPU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | */ |
| 86 | |
Rusty Russell | b3199c0 | 2008-12-30 09:05:14 +1030 | [diff] [blame] | 87 | extern const struct cpumask *const cpu_possible_mask; |
| 88 | extern const struct cpumask *const cpu_online_mask; |
| 89 | extern const struct cpumask *const cpu_present_mask; |
| 90 | extern const struct cpumask *const cpu_active_mask; |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | #if NR_CPUS > 1 |
Rusty Russell | ae7a47e | 2008-12-30 09:05:15 +1030 | [diff] [blame] | 93 | #define num_online_cpus() cpumask_weight(cpu_online_mask) |
| 94 | #define num_possible_cpus() cpumask_weight(cpu_possible_mask) |
| 95 | #define num_present_cpus() cpumask_weight(cpu_present_mask) |
Peter Zijlstra | 6ad4c18 | 2009-11-25 13:31:39 +0100 | [diff] [blame] | 96 | #define num_active_cpus() cpumask_weight(cpu_active_mask) |
Rusty Russell | ae7a47e | 2008-12-30 09:05:15 +1030 | [diff] [blame] | 97 | #define cpu_online(cpu) cpumask_test_cpu((cpu), cpu_online_mask) |
| 98 | #define cpu_possible(cpu) cpumask_test_cpu((cpu), cpu_possible_mask) |
| 99 | #define cpu_present(cpu) cpumask_test_cpu((cpu), cpu_present_mask) |
| 100 | #define cpu_active(cpu) cpumask_test_cpu((cpu), cpu_active_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | #else |
Heiko Carstens | 221e3eb | 2010-03-05 13:42:41 -0800 | [diff] [blame] | 102 | #define num_online_cpus() 1U |
| 103 | #define num_possible_cpus() 1U |
| 104 | #define num_present_cpus() 1U |
| 105 | #define num_active_cpus() 1U |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | #define cpu_online(cpu) ((cpu) == 0) |
| 107 | #define cpu_possible(cpu) ((cpu) == 0) |
| 108 | #define cpu_present(cpu) ((cpu) == 0) |
Max Krasnyansky | e761b77 | 2008-07-15 04:43:49 -0700 | [diff] [blame] | 109 | #define cpu_active(cpu) ((cpu) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | #endif |
| 111 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 112 | /* verify cpu argument to cpumask_* operators */ |
| 113 | static inline unsigned int cpumask_check(unsigned int cpu) |
| 114 | { |
| 115 | #ifdef CONFIG_DEBUG_PER_CPU_MAPS |
| 116 | WARN_ON_ONCE(cpu >= nr_cpumask_bits); |
| 117 | #endif /* CONFIG_DEBUG_PER_CPU_MAPS */ |
| 118 | return cpu; |
| 119 | } |
| 120 | |
| 121 | #if NR_CPUS == 1 |
Rusty Russell | 984f2f3 | 2008-11-08 20:24:19 +1100 | [diff] [blame] | 122 | /* Uniprocessor. Assume all masks are "1". */ |
| 123 | static inline unsigned int cpumask_first(const struct cpumask *srcp) |
| 124 | { |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /* Valid inputs for n are -1 and 0. */ |
| 129 | static inline unsigned int cpumask_next(int n, const struct cpumask *srcp) |
| 130 | { |
| 131 | return n+1; |
| 132 | } |
| 133 | |
| 134 | static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp) |
| 135 | { |
| 136 | return n+1; |
| 137 | } |
| 138 | |
| 139 | static inline unsigned int cpumask_next_and(int n, |
| 140 | const struct cpumask *srcp, |
| 141 | const struct cpumask *andp) |
| 142 | { |
| 143 | return n+1; |
| 144 | } |
| 145 | |
| 146 | /* cpu must be a valid cpu, ie 0, so there's no other choice. */ |
| 147 | static inline unsigned int cpumask_any_but(const struct cpumask *mask, |
| 148 | unsigned int cpu) |
| 149 | { |
| 150 | return 1; |
| 151 | } |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 152 | |
Amir Vadai | da91309 | 2014-06-09 10:24:38 +0300 | [diff] [blame] | 153 | static inline int cpumask_set_cpu_local_first(int i, int numa_node, cpumask_t *dstp) |
| 154 | { |
| 155 | set_bit(0, cpumask_bits(dstp)); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 160 | #define for_each_cpu(cpu, mask) \ |
| 161 | for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask) |
Paul E. McKenney | 8bd93a2 | 2010-02-22 17:04:59 -0800 | [diff] [blame] | 162 | #define for_each_cpu_not(cpu, mask) \ |
| 163 | for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask) |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 164 | #define for_each_cpu_and(cpu, mask, and) \ |
| 165 | for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask, (void)and) |
| 166 | #else |
| 167 | /** |
| 168 | * cpumask_first - get the first cpu in a cpumask |
| 169 | * @srcp: the cpumask pointer |
| 170 | * |
| 171 | * Returns >= nr_cpu_ids if no cpus set. |
| 172 | */ |
| 173 | static inline unsigned int cpumask_first(const struct cpumask *srcp) |
| 174 | { |
| 175 | return find_first_bit(cpumask_bits(srcp), nr_cpumask_bits); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * cpumask_next - get the next 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 set. |
| 184 | */ |
| 185 | static inline unsigned int cpumask_next(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_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * cpumask_next_zero - get the next unset cpu in a cpumask |
| 195 | * @n: the cpu prior to the place to search (ie. return will be > @n) |
| 196 | * @srcp: the cpumask pointer |
| 197 | * |
| 198 | * Returns >= nr_cpu_ids if no further cpus unset. |
| 199 | */ |
| 200 | static inline unsigned int cpumask_next_zero(int n, const struct cpumask *srcp) |
| 201 | { |
| 202 | /* -1 is a legal arg here. */ |
| 203 | if (n != -1) |
| 204 | cpumask_check(n); |
| 205 | return find_next_zero_bit(cpumask_bits(srcp), nr_cpumask_bits, n+1); |
| 206 | } |
| 207 | |
| 208 | int cpumask_next_and(int n, const struct cpumask *, const struct cpumask *); |
| 209 | int cpumask_any_but(const struct cpumask *mask, unsigned int cpu); |
Amir Vadai | da91309 | 2014-06-09 10:24:38 +0300 | [diff] [blame] | 210 | int cpumask_set_cpu_local_first(int i, int numa_node, cpumask_t *dstp); |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 211 | |
Rusty Russell | 984f2f3 | 2008-11-08 20:24:19 +1100 | [diff] [blame] | 212 | /** |
| 213 | * for_each_cpu - iterate over every cpu in a mask |
| 214 | * @cpu: the (optionally unsigned) integer iterator |
| 215 | * @mask: the cpumask pointer |
| 216 | * |
| 217 | * After the loop, cpu is >= nr_cpu_ids. |
| 218 | */ |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 219 | #define for_each_cpu(cpu, mask) \ |
| 220 | for ((cpu) = -1; \ |
| 221 | (cpu) = cpumask_next((cpu), (mask)), \ |
| 222 | (cpu) < nr_cpu_ids;) |
Rusty Russell | 984f2f3 | 2008-11-08 20:24:19 +1100 | [diff] [blame] | 223 | |
| 224 | /** |
Paul E. McKenney | 8bd93a2 | 2010-02-22 17:04:59 -0800 | [diff] [blame] | 225 | * for_each_cpu_not - iterate over every cpu in a complemented mask |
| 226 | * @cpu: the (optionally unsigned) integer iterator |
| 227 | * @mask: the cpumask pointer |
| 228 | * |
| 229 | * After the loop, cpu is >= nr_cpu_ids. |
| 230 | */ |
| 231 | #define for_each_cpu_not(cpu, mask) \ |
| 232 | for ((cpu) = -1; \ |
| 233 | (cpu) = cpumask_next_zero((cpu), (mask)), \ |
| 234 | (cpu) < nr_cpu_ids;) |
| 235 | |
| 236 | /** |
Rusty Russell | 984f2f3 | 2008-11-08 20:24:19 +1100 | [diff] [blame] | 237 | * for_each_cpu_and - iterate over every cpu in both masks |
| 238 | * @cpu: the (optionally unsigned) integer iterator |
| 239 | * @mask: the first cpumask pointer |
| 240 | * @and: the second cpumask pointer |
| 241 | * |
| 242 | * This saves a temporary CPU mask in many places. It is equivalent to: |
| 243 | * struct cpumask tmp; |
| 244 | * cpumask_and(&tmp, &mask, &and); |
| 245 | * for_each_cpu(cpu, &tmp) |
| 246 | * ... |
| 247 | * |
| 248 | * After the loop, cpu is >= nr_cpu_ids. |
| 249 | */ |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 250 | #define for_each_cpu_and(cpu, mask, and) \ |
| 251 | for ((cpu) = -1; \ |
| 252 | (cpu) = cpumask_next_and((cpu), (mask), (and)), \ |
| 253 | (cpu) < nr_cpu_ids;) |
| 254 | #endif /* SMP */ |
| 255 | |
| 256 | #define CPU_BITS_NONE \ |
| 257 | { \ |
| 258 | [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \ |
| 259 | } |
| 260 | |
| 261 | #define CPU_BITS_CPU0 \ |
| 262 | { \ |
| 263 | [0] = 1UL \ |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * cpumask_set_cpu - set a cpu in a cpumask |
| 268 | * @cpu: cpu number (< nr_cpu_ids) |
| 269 | * @dstp: the cpumask pointer |
| 270 | */ |
| 271 | static inline void cpumask_set_cpu(unsigned int cpu, struct cpumask *dstp) |
| 272 | { |
| 273 | set_bit(cpumask_check(cpu), cpumask_bits(dstp)); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * cpumask_clear_cpu - clear a cpu in a cpumask |
| 278 | * @cpu: cpu number (< nr_cpu_ids) |
| 279 | * @dstp: the cpumask pointer |
| 280 | */ |
| 281 | static inline void cpumask_clear_cpu(int cpu, struct cpumask *dstp) |
| 282 | { |
| 283 | clear_bit(cpumask_check(cpu), cpumask_bits(dstp)); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * cpumask_test_cpu - test for a cpu in a cpumask |
| 288 | * @cpu: cpu number (< nr_cpu_ids) |
| 289 | * @cpumask: the cpumask pointer |
| 290 | * |
Alex Shi | c777ad6 | 2012-05-28 22:23:51 +0800 | [diff] [blame] | 291 | * Returns 1 if @cpu is set in @cpumask, else returns 0 |
| 292 | * |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 293 | * No static inline type checking - see Subtlety (1) above. |
| 294 | */ |
| 295 | #define cpumask_test_cpu(cpu, cpumask) \ |
Rusty Russell | ae7a47e | 2008-12-30 09:05:15 +1030 | [diff] [blame] | 296 | test_bit(cpumask_check(cpu), cpumask_bits((cpumask))) |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 297 | |
| 298 | /** |
| 299 | * cpumask_test_and_set_cpu - atomically test and set a cpu in a cpumask |
| 300 | * @cpu: cpu number (< nr_cpu_ids) |
| 301 | * @cpumask: the cpumask pointer |
| 302 | * |
Alex Shi | c777ad6 | 2012-05-28 22:23:51 +0800 | [diff] [blame] | 303 | * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0 |
| 304 | * |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 305 | * test_and_set_bit wrapper for cpumasks. |
| 306 | */ |
| 307 | static inline int cpumask_test_and_set_cpu(int cpu, struct cpumask *cpumask) |
| 308 | { |
| 309 | return test_and_set_bit(cpumask_check(cpu), cpumask_bits(cpumask)); |
| 310 | } |
| 311 | |
| 312 | /** |
Xiao Guangrong | 54fdade | 2009-09-22 16:43:39 -0700 | [diff] [blame] | 313 | * cpumask_test_and_clear_cpu - atomically test and clear a cpu in a cpumask |
| 314 | * @cpu: cpu number (< nr_cpu_ids) |
| 315 | * @cpumask: the cpumask pointer |
| 316 | * |
Alex Shi | c777ad6 | 2012-05-28 22:23:51 +0800 | [diff] [blame] | 317 | * Returns 1 if @cpu is set in old bitmap of @cpumask, else returns 0 |
| 318 | * |
Xiao Guangrong | 54fdade | 2009-09-22 16:43:39 -0700 | [diff] [blame] | 319 | * test_and_clear_bit wrapper for cpumasks. |
| 320 | */ |
| 321 | static inline int cpumask_test_and_clear_cpu(int cpu, struct cpumask *cpumask) |
| 322 | { |
| 323 | return test_and_clear_bit(cpumask_check(cpu), cpumask_bits(cpumask)); |
| 324 | } |
| 325 | |
| 326 | /** |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 327 | * cpumask_setall - set all cpus (< nr_cpu_ids) in a cpumask |
| 328 | * @dstp: the cpumask pointer |
| 329 | */ |
| 330 | static inline void cpumask_setall(struct cpumask *dstp) |
| 331 | { |
| 332 | bitmap_fill(cpumask_bits(dstp), nr_cpumask_bits); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * cpumask_clear - clear all cpus (< nr_cpu_ids) in a cpumask |
| 337 | * @dstp: the cpumask pointer |
| 338 | */ |
| 339 | static inline void cpumask_clear(struct cpumask *dstp) |
| 340 | { |
| 341 | bitmap_zero(cpumask_bits(dstp), nr_cpumask_bits); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * cpumask_and - *dstp = *src1p & *src2p |
| 346 | * @dstp: the cpumask result |
| 347 | * @src1p: the first input |
| 348 | * @src2p: the second input |
Alex Shi | c777ad6 | 2012-05-28 22:23:51 +0800 | [diff] [blame] | 349 | * |
| 350 | * If *@dstp is empty, returns 0, else returns 1 |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 351 | */ |
Linus Torvalds | f4b0373 | 2009-08-21 09:26:15 -0700 | [diff] [blame] | 352 | static inline int cpumask_and(struct cpumask *dstp, |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 353 | const struct cpumask *src1p, |
| 354 | const struct cpumask *src2p) |
| 355 | { |
Linus Torvalds | f4b0373 | 2009-08-21 09:26:15 -0700 | [diff] [blame] | 356 | return bitmap_and(cpumask_bits(dstp), cpumask_bits(src1p), |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 357 | cpumask_bits(src2p), nr_cpumask_bits); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * cpumask_or - *dstp = *src1p | *src2p |
| 362 | * @dstp: the cpumask result |
| 363 | * @src1p: the first input |
| 364 | * @src2p: the second input |
| 365 | */ |
| 366 | static inline void cpumask_or(struct cpumask *dstp, const struct cpumask *src1p, |
| 367 | const struct cpumask *src2p) |
| 368 | { |
| 369 | bitmap_or(cpumask_bits(dstp), cpumask_bits(src1p), |
| 370 | cpumask_bits(src2p), nr_cpumask_bits); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * cpumask_xor - *dstp = *src1p ^ *src2p |
| 375 | * @dstp: the cpumask result |
| 376 | * @src1p: the first input |
| 377 | * @src2p: the second input |
| 378 | */ |
| 379 | static inline void cpumask_xor(struct cpumask *dstp, |
| 380 | const struct cpumask *src1p, |
| 381 | const struct cpumask *src2p) |
| 382 | { |
| 383 | bitmap_xor(cpumask_bits(dstp), cpumask_bits(src1p), |
| 384 | cpumask_bits(src2p), nr_cpumask_bits); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * cpumask_andnot - *dstp = *src1p & ~*src2p |
| 389 | * @dstp: the cpumask result |
| 390 | * @src1p: the first input |
| 391 | * @src2p: the second input |
Alex Shi | c777ad6 | 2012-05-28 22:23:51 +0800 | [diff] [blame] | 392 | * |
| 393 | * If *@dstp is empty, returns 0, else returns 1 |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 394 | */ |
Linus Torvalds | f4b0373 | 2009-08-21 09:26:15 -0700 | [diff] [blame] | 395 | static inline int cpumask_andnot(struct cpumask *dstp, |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 396 | const struct cpumask *src1p, |
| 397 | const struct cpumask *src2p) |
| 398 | { |
Linus Torvalds | f4b0373 | 2009-08-21 09:26:15 -0700 | [diff] [blame] | 399 | return bitmap_andnot(cpumask_bits(dstp), cpumask_bits(src1p), |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 400 | cpumask_bits(src2p), nr_cpumask_bits); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * cpumask_complement - *dstp = ~*srcp |
| 405 | * @dstp: the cpumask result |
| 406 | * @srcp: the input to invert |
| 407 | */ |
| 408 | static inline void cpumask_complement(struct cpumask *dstp, |
| 409 | const struct cpumask *srcp) |
| 410 | { |
| 411 | bitmap_complement(cpumask_bits(dstp), cpumask_bits(srcp), |
| 412 | nr_cpumask_bits); |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * cpumask_equal - *src1p == *src2p |
| 417 | * @src1p: the first input |
| 418 | * @src2p: the second input |
| 419 | */ |
| 420 | static inline bool cpumask_equal(const struct cpumask *src1p, |
| 421 | const struct cpumask *src2p) |
| 422 | { |
| 423 | return bitmap_equal(cpumask_bits(src1p), cpumask_bits(src2p), |
| 424 | nr_cpumask_bits); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * cpumask_intersects - (*src1p & *src2p) != 0 |
| 429 | * @src1p: the first input |
| 430 | * @src2p: the second input |
| 431 | */ |
| 432 | static inline bool cpumask_intersects(const struct cpumask *src1p, |
| 433 | const struct cpumask *src2p) |
| 434 | { |
| 435 | return bitmap_intersects(cpumask_bits(src1p), cpumask_bits(src2p), |
| 436 | nr_cpumask_bits); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * cpumask_subset - (*src1p & ~*src2p) == 0 |
| 441 | * @src1p: the first input |
| 442 | * @src2p: the second input |
Alex Shi | c777ad6 | 2012-05-28 22:23:51 +0800 | [diff] [blame] | 443 | * |
| 444 | * Returns 1 if *@src1p is a subset of *@src2p, else returns 0 |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 445 | */ |
| 446 | static inline int cpumask_subset(const struct cpumask *src1p, |
| 447 | const struct cpumask *src2p) |
| 448 | { |
| 449 | return bitmap_subset(cpumask_bits(src1p), cpumask_bits(src2p), |
| 450 | nr_cpumask_bits); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * cpumask_empty - *srcp == 0 |
| 455 | * @srcp: the cpumask to that all cpus < nr_cpu_ids are clear. |
| 456 | */ |
| 457 | static inline bool cpumask_empty(const struct cpumask *srcp) |
| 458 | { |
| 459 | return bitmap_empty(cpumask_bits(srcp), nr_cpumask_bits); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * cpumask_full - *srcp == 0xFFFFFFFF... |
| 464 | * @srcp: the cpumask to that all cpus < nr_cpu_ids are set. |
| 465 | */ |
| 466 | static inline bool cpumask_full(const struct cpumask *srcp) |
| 467 | { |
| 468 | return bitmap_full(cpumask_bits(srcp), nr_cpumask_bits); |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * cpumask_weight - Count of bits in *srcp |
| 473 | * @srcp: the cpumask to count bits (< nr_cpu_ids) in. |
| 474 | */ |
| 475 | static inline unsigned int cpumask_weight(const struct cpumask *srcp) |
| 476 | { |
| 477 | return bitmap_weight(cpumask_bits(srcp), nr_cpumask_bits); |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * cpumask_shift_right - *dstp = *srcp >> n |
| 482 | * @dstp: the cpumask result |
| 483 | * @srcp: the input to shift |
| 484 | * @n: the number of bits to shift by |
| 485 | */ |
| 486 | static inline void cpumask_shift_right(struct cpumask *dstp, |
| 487 | const struct cpumask *srcp, int n) |
| 488 | { |
| 489 | bitmap_shift_right(cpumask_bits(dstp), cpumask_bits(srcp), n, |
| 490 | nr_cpumask_bits); |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * cpumask_shift_left - *dstp = *srcp << n |
| 495 | * @dstp: the cpumask result |
| 496 | * @srcp: the input to shift |
| 497 | * @n: the number of bits to shift by |
| 498 | */ |
| 499 | static inline void cpumask_shift_left(struct cpumask *dstp, |
| 500 | const struct cpumask *srcp, int n) |
| 501 | { |
| 502 | bitmap_shift_left(cpumask_bits(dstp), cpumask_bits(srcp), n, |
| 503 | nr_cpumask_bits); |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * cpumask_copy - *dstp = *srcp |
| 508 | * @dstp: the result |
| 509 | * @srcp: the input cpumask |
| 510 | */ |
| 511 | static inline void cpumask_copy(struct cpumask *dstp, |
| 512 | const struct cpumask *srcp) |
| 513 | { |
| 514 | bitmap_copy(cpumask_bits(dstp), cpumask_bits(srcp), nr_cpumask_bits); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * cpumask_any - pick a "random" cpu from *srcp |
| 519 | * @srcp: the input cpumask |
| 520 | * |
| 521 | * Returns >= nr_cpu_ids if no cpus set. |
| 522 | */ |
| 523 | #define cpumask_any(srcp) cpumask_first(srcp) |
| 524 | |
| 525 | /** |
| 526 | * cpumask_first_and - return the first cpu from *srcp1 & *srcp2 |
| 527 | * @src1p: the first input |
| 528 | * @src2p: the second input |
| 529 | * |
| 530 | * Returns >= nr_cpu_ids if no cpus set in both. See also cpumask_next_and(). |
| 531 | */ |
| 532 | #define cpumask_first_and(src1p, src2p) cpumask_next_and(-1, (src1p), (src2p)) |
| 533 | |
| 534 | /** |
| 535 | * cpumask_any_and - pick a "random" cpu from *mask1 & *mask2 |
| 536 | * @mask1: the first input cpumask |
| 537 | * @mask2: the second input cpumask |
| 538 | * |
| 539 | * Returns >= nr_cpu_ids if no cpus set. |
| 540 | */ |
| 541 | #define cpumask_any_and(mask1, mask2) cpumask_first_and((mask1), (mask2)) |
| 542 | |
| 543 | /** |
Rusty Russell | cd83e42 | 2008-11-07 11:12:29 +1100 | [diff] [blame] | 544 | * cpumask_of - the cpumask containing just a given cpu |
| 545 | * @cpu: the cpu (<= nr_cpu_ids) |
| 546 | */ |
| 547 | #define cpumask_of(cpu) (get_cpu_mask(cpu)) |
| 548 | |
| 549 | /** |
Rusty Russell | 29c0177 | 2008-12-13 21:20:25 +1030 | [diff] [blame] | 550 | * cpumask_parse_user - extract a cpumask from a user string |
| 551 | * @buf: the buffer to extract from |
| 552 | * @len: the length of the buffer |
| 553 | * @dstp: the cpumask to set. |
| 554 | * |
| 555 | * Returns -errno, or 0 for success. |
| 556 | */ |
| 557 | static inline int cpumask_parse_user(const char __user *buf, int len, |
| 558 | struct cpumask *dstp) |
| 559 | { |
Tejun Heo | 513e3d2 | 2015-02-13 14:36:50 -0800 | [diff] [blame] | 560 | return bitmap_parse_user(buf, len, cpumask_bits(dstp), nr_cpu_ids); |
Rusty Russell | 29c0177 | 2008-12-13 21:20:25 +1030 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /** |
Mike Travis | 4b06042 | 2011-05-24 17:13:12 -0700 | [diff] [blame] | 564 | * cpumask_parselist_user - extract a cpumask from a user string |
| 565 | * @buf: the buffer to extract from |
| 566 | * @len: the length of the buffer |
| 567 | * @dstp: the cpumask to set. |
| 568 | * |
| 569 | * Returns -errno, or 0 for success. |
| 570 | */ |
| 571 | static inline int cpumask_parselist_user(const char __user *buf, int len, |
| 572 | struct cpumask *dstp) |
| 573 | { |
| 574 | return bitmap_parselist_user(buf, len, cpumask_bits(dstp), |
Tejun Heo | 513e3d2 | 2015-02-13 14:36:50 -0800 | [diff] [blame] | 575 | nr_cpu_ids); |
Mike Travis | 4b06042 | 2011-05-24 17:13:12 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /** |
Tejun Heo | ba630e4 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 579 | * cpumask_parse - extract a cpumask from from a string |
| 580 | * @buf: the buffer to extract from |
| 581 | * @dstp: the cpumask to set. |
| 582 | * |
| 583 | * Returns -errno, or 0 for success. |
| 584 | */ |
| 585 | static inline int cpumask_parse(const char *buf, struct cpumask *dstp) |
| 586 | { |
| 587 | char *nl = strchr(buf, '\n'); |
Brian W Hart | cea092c | 2014-05-14 10:33:45 +0930 | [diff] [blame] | 588 | unsigned int len = nl ? (unsigned int)(nl - buf) : strlen(buf); |
Tejun Heo | ba630e4 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 589 | |
Tejun Heo | 513e3d2 | 2015-02-13 14:36:50 -0800 | [diff] [blame] | 590 | return bitmap_parse(buf, len, cpumask_bits(dstp), nr_cpu_ids); |
Tejun Heo | ba630e4 | 2013-03-12 11:30:04 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /** |
Alex Shi | 231daf0 | 2012-07-27 09:29:42 +0930 | [diff] [blame] | 594 | * cpulist_parse - extract a cpumask from a user string of ranges |
Rusty Russell | 29c0177 | 2008-12-13 21:20:25 +1030 | [diff] [blame] | 595 | * @buf: the buffer to extract from |
Rusty Russell | 29c0177 | 2008-12-13 21:20:25 +1030 | [diff] [blame] | 596 | * @dstp: the cpumask to set. |
| 597 | * |
| 598 | * Returns -errno, or 0 for success. |
| 599 | */ |
| 600 | static inline int cpulist_parse(const char *buf, struct cpumask *dstp) |
| 601 | { |
Tejun Heo | 513e3d2 | 2015-02-13 14:36:50 -0800 | [diff] [blame] | 602 | return bitmap_parselist(buf, cpumask_bits(dstp), nr_cpu_ids); |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /** |
| 606 | * cpumask_size - size to allocate for a 'struct cpumask' in bytes |
| 607 | * |
| 608 | * This will eventually be a runtime variable, depending on nr_cpu_ids. |
| 609 | */ |
| 610 | static inline size_t cpumask_size(void) |
| 611 | { |
| 612 | /* FIXME: Once all cpumask assignments are eliminated, this |
| 613 | * can be nr_cpumask_bits */ |
| 614 | return BITS_TO_LONGS(NR_CPUS) * sizeof(long); |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * cpumask_var_t: struct cpumask for stack usage. |
| 619 | * |
| 620 | * Oh, the wicked games we play! In order to make kernel coding a |
| 621 | * little more difficult, we typedef cpumask_var_t to an array or a |
| 622 | * pointer: doing &mask on an array is a noop, so it still works. |
| 623 | * |
| 624 | * ie. |
| 625 | * cpumask_var_t tmpmask; |
| 626 | * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL)) |
| 627 | * return -ENOMEM; |
| 628 | * |
| 629 | * ... use 'tmpmask' like a normal struct cpumask * ... |
| 630 | * |
| 631 | * free_cpumask_var(tmpmask); |
KOSAKI Motohiro | a64a26e | 2011-07-26 16:08:45 -0700 | [diff] [blame] | 632 | * |
| 633 | * |
| 634 | * However, one notable exception is there. alloc_cpumask_var() allocates |
| 635 | * only nr_cpumask_bits bits (in the other hand, real cpumask_t always has |
| 636 | * NR_CPUS bits). Therefore you don't have to dereference cpumask_var_t. |
| 637 | * |
| 638 | * cpumask_var_t tmpmask; |
| 639 | * if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL)) |
| 640 | * return -ENOMEM; |
| 641 | * |
| 642 | * var = *tmpmask; |
| 643 | * |
| 644 | * This code makes NR_CPUS length memcopy and brings to a memory corruption. |
| 645 | * cpumask_copy() provide safe copy functionality. |
Christoph Lameter | 4ba2968 | 2014-08-26 19:12:21 -0500 | [diff] [blame] | 646 | * |
| 647 | * Note that there is another evil here: If you define a cpumask_var_t |
| 648 | * as a percpu variable then the way to obtain the address of the cpumask |
| 649 | * structure differently influences what this_cpu_* operation needs to be |
| 650 | * used. Please use this_cpu_cpumask_var_t in those cases. The direct use |
| 651 | * of this_cpu_ptr() or this_cpu_read() will lead to failures when the |
| 652 | * other type of cpumask_var_t implementation is configured. |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 653 | */ |
| 654 | #ifdef CONFIG_CPUMASK_OFFSTACK |
| 655 | typedef struct cpumask *cpumask_var_t; |
| 656 | |
Christoph Lameter | 4ba2968 | 2014-08-26 19:12:21 -0500 | [diff] [blame] | 657 | #define this_cpu_cpumask_var_ptr(x) this_cpu_read(x) |
| 658 | |
Mike Travis | 7b4967c | 2008-12-19 16:56:37 +1030 | [diff] [blame] | 659 | bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node); |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 660 | bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags); |
Yinghai Lu | 0281b5d | 2009-06-06 14:50:36 -0700 | [diff] [blame] | 661 | bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node); |
| 662 | bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags); |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 663 | void alloc_bootmem_cpumask_var(cpumask_var_t *mask); |
| 664 | void free_cpumask_var(cpumask_var_t mask); |
Rusty Russell | cd83e42 | 2008-11-07 11:12:29 +1100 | [diff] [blame] | 665 | void free_bootmem_cpumask_var(cpumask_var_t mask); |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 666 | |
| 667 | #else |
| 668 | typedef struct cpumask cpumask_var_t[1]; |
| 669 | |
Christoph Lameter | 4ba2968 | 2014-08-26 19:12:21 -0500 | [diff] [blame] | 670 | #define this_cpu_cpumask_var_ptr(x) this_cpu_ptr(x) |
| 671 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 672 | static inline bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) |
| 673 | { |
| 674 | return true; |
| 675 | } |
| 676 | |
Mike Travis | 7b4967c | 2008-12-19 16:56:37 +1030 | [diff] [blame] | 677 | static inline bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, |
| 678 | int node) |
| 679 | { |
| 680 | return true; |
| 681 | } |
| 682 | |
Yinghai Lu | 0281b5d | 2009-06-06 14:50:36 -0700 | [diff] [blame] | 683 | static inline bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) |
| 684 | { |
| 685 | cpumask_clear(*mask); |
| 686 | return true; |
| 687 | } |
| 688 | |
| 689 | static inline bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, |
| 690 | int node) |
| 691 | { |
| 692 | cpumask_clear(*mask); |
| 693 | return true; |
| 694 | } |
| 695 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 696 | static inline void alloc_bootmem_cpumask_var(cpumask_var_t *mask) |
| 697 | { |
| 698 | } |
| 699 | |
| 700 | static inline void free_cpumask_var(cpumask_var_t mask) |
| 701 | { |
| 702 | } |
Rusty Russell | cd83e42 | 2008-11-07 11:12:29 +1100 | [diff] [blame] | 703 | |
| 704 | static inline void free_bootmem_cpumask_var(cpumask_var_t mask) |
| 705 | { |
| 706 | } |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 707 | #endif /* CONFIG_CPUMASK_OFFSTACK */ |
| 708 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 709 | /* It's common to want to use cpu_all_mask in struct member initializers, |
| 710 | * so it has to refer to an address rather than a pointer. */ |
| 711 | extern const DECLARE_BITMAP(cpu_all_bits, NR_CPUS); |
| 712 | #define cpu_all_mask to_cpumask(cpu_all_bits) |
| 713 | |
| 714 | /* First bits of cpu_bit_bitmap are in fact unset. */ |
| 715 | #define cpu_none_mask to_cpumask(cpu_bit_bitmap[0]) |
| 716 | |
Rusty Russell | ae7a47e | 2008-12-30 09:05:15 +1030 | [diff] [blame] | 717 | #define for_each_possible_cpu(cpu) for_each_cpu((cpu), cpu_possible_mask) |
| 718 | #define for_each_online_cpu(cpu) for_each_cpu((cpu), cpu_online_mask) |
| 719 | #define for_each_present_cpu(cpu) for_each_cpu((cpu), cpu_present_mask) |
| 720 | |
Rusty Russell | 2d3854a | 2008-11-05 13:39:10 +1100 | [diff] [blame] | 721 | /* Wrappers for arch boot code to manipulate normally-constant masks */ |
Rusty Russell | 3fa4152 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 722 | void set_cpu_possible(unsigned int cpu, bool possible); |
| 723 | void set_cpu_present(unsigned int cpu, bool present); |
| 724 | void set_cpu_online(unsigned int cpu, bool online); |
| 725 | void set_cpu_active(unsigned int cpu, bool active); |
| 726 | void init_cpu_present(const struct cpumask *src); |
| 727 | void init_cpu_possible(const struct cpumask *src); |
| 728 | void init_cpu_online(const struct cpumask *src); |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 729 | |
| 730 | /** |
| 731 | * to_cpumask - convert an NR_CPUS bitmap to a struct cpumask * |
| 732 | * @bitmap: the bitmap |
| 733 | * |
| 734 | * There are a few places where cpumask_var_t isn't appropriate and |
| 735 | * static cpumasks must be used (eg. very early boot), yet we don't |
| 736 | * expose the definition of 'struct cpumask'. |
| 737 | * |
| 738 | * This does the conversion, and can be used as a constant initializer. |
| 739 | */ |
| 740 | #define to_cpumask(bitmap) \ |
| 741 | ((struct cpumask *)(1 ? (bitmap) \ |
| 742 | : (void *)sizeof(__check_is_bitmap(bitmap)))) |
| 743 | |
| 744 | static inline int __check_is_bitmap(const unsigned long *bitmap) |
| 745 | { |
| 746 | return 1; |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * Special-case data structure for "single bit set only" constant CPU masks. |
| 751 | * |
| 752 | * We pre-generate all the 64 (or 32) possible bit positions, with enough |
| 753 | * padding to the left and the right, and return the constant pointer |
| 754 | * appropriately offset. |
| 755 | */ |
| 756 | extern const unsigned long |
| 757 | cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)]; |
| 758 | |
| 759 | static inline const struct cpumask *get_cpu_mask(unsigned int cpu) |
| 760 | { |
| 761 | const unsigned long *p = cpu_bit_bitmap[1 + cpu % BITS_PER_LONG]; |
| 762 | p -= cpu / BITS_PER_LONG; |
| 763 | return to_cpumask(p); |
| 764 | } |
| 765 | |
| 766 | #define cpu_is_offline(cpu) unlikely(!cpu_online(cpu)) |
| 767 | |
| 768 | #if NR_CPUS <= BITS_PER_LONG |
| 769 | #define CPU_BITS_ALL \ |
| 770 | { \ |
Rusty Russell | 9941a38 | 2015-03-05 10:49:19 +1030 | [diff] [blame^] | 771 | [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \ |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | #else /* NR_CPUS > BITS_PER_LONG */ |
| 775 | |
| 776 | #define CPU_BITS_ALL \ |
| 777 | { \ |
| 778 | [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \ |
Rusty Russell | 9941a38 | 2015-03-05 10:49:19 +1030 | [diff] [blame^] | 779 | [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \ |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 780 | } |
| 781 | #endif /* NR_CPUS > BITS_PER_LONG */ |
| 782 | |
Sudeep Holla | 5aaba36 | 2014-09-30 14:48:22 +0100 | [diff] [blame] | 783 | /** |
| 784 | * cpumap_print_to_pagebuf - copies the cpumask into the buffer either |
| 785 | * as comma-separated list of cpus or hex values of cpumask |
| 786 | * @list: indicates whether the cpumap must be list |
| 787 | * @mask: the cpumask to copy |
| 788 | * @buf: the buffer to copy into |
| 789 | * |
| 790 | * Returns the length of the (null-terminated) @buf string, zero if |
| 791 | * nothing is copied. |
| 792 | */ |
| 793 | static inline ssize_t |
| 794 | cpumap_print_to_pagebuf(bool list, char *buf, const struct cpumask *mask) |
| 795 | { |
| 796 | return bitmap_print_to_pagebuf(list, buf, cpumask_bits(mask), |
Tejun Heo | 513e3d2 | 2015-02-13 14:36:50 -0800 | [diff] [blame] | 797 | nr_cpu_ids); |
Sudeep Holla | 5aaba36 | 2014-09-30 14:48:22 +0100 | [diff] [blame] | 798 | } |
| 799 | |
Rusty Russell | 9941a38 | 2015-03-05 10:49:19 +1030 | [diff] [blame^] | 800 | #if NR_CPUS <= BITS_PER_LONG |
| 801 | #define CPU_MASK_ALL \ |
| 802 | (cpumask_t) { { \ |
| 803 | [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \ |
| 804 | } } |
| 805 | #else |
| 806 | #define CPU_MASK_ALL \ |
| 807 | (cpumask_t) { { \ |
| 808 | [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL, \ |
| 809 | [BITS_TO_LONGS(NR_CPUS)-1] = BITMAP_LAST_WORD_MASK(NR_CPUS) \ |
| 810 | } } |
| 811 | #endif /* NR_CPUS > BITS_PER_LONG */ |
| 812 | |
| 813 | #define CPU_MASK_NONE \ |
| 814 | (cpumask_t) { { \ |
| 815 | [0 ... BITS_TO_LONGS(NR_CPUS)-1] = 0UL \ |
| 816 | } } |
| 817 | |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 818 | /* |
| 819 | * |
| 820 | * From here down, all obsolete. Use cpumask_ variants! |
| 821 | * |
| 822 | */ |
| 823 | #ifndef CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 824 | |
| 825 | #define CPU_MASK_CPU0 \ |
| 826 | (cpumask_t) { { \ |
| 827 | [0] = 1UL \ |
| 828 | } } |
| 829 | |
| 830 | #if NR_CPUS == 1 |
| 831 | #define first_cpu(src) ({ (void)(src); 0; }) |
| 832 | #define next_cpu(n, src) ({ (void)(src); 1; }) |
| 833 | #define any_online_cpu(mask) 0 |
| 834 | #define for_each_cpu_mask(cpu, mask) \ |
| 835 | for ((cpu) = 0; (cpu) < 1; (cpu)++, (void)mask) |
| 836 | #else /* NR_CPUS > 1 */ |
| 837 | int __first_cpu(const cpumask_t *srcp); |
| 838 | int __next_cpu(int n, const cpumask_t *srcp); |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 839 | |
| 840 | #define first_cpu(src) __first_cpu(&(src)) |
| 841 | #define next_cpu(n, src) __next_cpu((n), &(src)) |
Srivatsa S. Bhat | 38b9378 | 2012-03-28 14:42:46 -0700 | [diff] [blame] | 842 | #define any_online_cpu(mask) cpumask_any_and(&mask, cpu_online_mask) |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 843 | #define for_each_cpu_mask(cpu, mask) \ |
| 844 | for ((cpu) = -1; \ |
| 845 | (cpu) = next_cpu((cpu), (mask)), \ |
| 846 | (cpu) < NR_CPUS; ) |
| 847 | #endif /* SMP */ |
| 848 | |
| 849 | #if NR_CPUS <= 64 |
| 850 | |
| 851 | #define for_each_cpu_mask_nr(cpu, mask) for_each_cpu_mask(cpu, mask) |
| 852 | |
| 853 | #else /* NR_CPUS > 64 */ |
| 854 | |
| 855 | int __next_cpu_nr(int n, const cpumask_t *srcp); |
| 856 | #define for_each_cpu_mask_nr(cpu, mask) \ |
| 857 | for ((cpu) = -1; \ |
| 858 | (cpu) = __next_cpu_nr((cpu), &(mask)), \ |
| 859 | (cpu) < nr_cpu_ids; ) |
| 860 | |
| 861 | #endif /* NR_CPUS > 64 */ |
| 862 | |
| 863 | #define cpus_addr(src) ((src).bits) |
| 864 | |
| 865 | #define cpu_set(cpu, dst) __cpu_set((cpu), &(dst)) |
| 866 | static inline void __cpu_set(int cpu, volatile cpumask_t *dstp) |
| 867 | { |
| 868 | set_bit(cpu, dstp->bits); |
| 869 | } |
| 870 | |
| 871 | #define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst)) |
| 872 | static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp) |
| 873 | { |
| 874 | clear_bit(cpu, dstp->bits); |
| 875 | } |
| 876 | |
| 877 | #define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS) |
Rasmus Villemoes | f5ac1f5 | 2015-02-12 15:01:59 -0800 | [diff] [blame] | 878 | static inline void __cpus_setall(cpumask_t *dstp, unsigned int nbits) |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 879 | { |
| 880 | bitmap_fill(dstp->bits, nbits); |
| 881 | } |
| 882 | |
| 883 | #define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS) |
Rasmus Villemoes | f5ac1f5 | 2015-02-12 15:01:59 -0800 | [diff] [blame] | 884 | static inline void __cpus_clear(cpumask_t *dstp, unsigned int nbits) |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 885 | { |
| 886 | bitmap_zero(dstp->bits, nbits); |
| 887 | } |
| 888 | |
| 889 | /* No static inline type checking - see Subtlety (1) above. */ |
| 890 | #define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits) |
| 891 | |
| 892 | #define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask)) |
| 893 | static inline int __cpu_test_and_set(int cpu, cpumask_t *addr) |
| 894 | { |
| 895 | return test_and_set_bit(cpu, addr->bits); |
| 896 | } |
| 897 | |
| 898 | #define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS) |
| 899 | static inline int __cpus_and(cpumask_t *dstp, const cpumask_t *src1p, |
Rasmus Villemoes | f5ac1f5 | 2015-02-12 15:01:59 -0800 | [diff] [blame] | 900 | const cpumask_t *src2p, unsigned int nbits) |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 901 | { |
| 902 | return bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits); |
| 903 | } |
| 904 | |
| 905 | #define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS) |
| 906 | static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p, |
Rasmus Villemoes | f5ac1f5 | 2015-02-12 15:01:59 -0800 | [diff] [blame] | 907 | const cpumask_t *src2p, unsigned int nbits) |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 908 | { |
| 909 | bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits); |
| 910 | } |
| 911 | |
| 912 | #define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS) |
| 913 | static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p, |
Rasmus Villemoes | f5ac1f5 | 2015-02-12 15:01:59 -0800 | [diff] [blame] | 914 | const cpumask_t *src2p, unsigned int nbits) |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 915 | { |
| 916 | bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits); |
| 917 | } |
| 918 | |
| 919 | #define cpus_andnot(dst, src1, src2) \ |
| 920 | __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS) |
| 921 | static inline int __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p, |
Rasmus Villemoes | f5ac1f5 | 2015-02-12 15:01:59 -0800 | [diff] [blame] | 922 | const cpumask_t *src2p, unsigned int nbits) |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 923 | { |
| 924 | return bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits); |
| 925 | } |
| 926 | |
| 927 | #define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS) |
| 928 | static inline int __cpus_equal(const cpumask_t *src1p, |
Rasmus Villemoes | f5ac1f5 | 2015-02-12 15:01:59 -0800 | [diff] [blame] | 929 | const cpumask_t *src2p, unsigned int nbits) |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 930 | { |
| 931 | return bitmap_equal(src1p->bits, src2p->bits, nbits); |
| 932 | } |
| 933 | |
| 934 | #define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS) |
| 935 | static inline int __cpus_intersects(const cpumask_t *src1p, |
Rasmus Villemoes | f5ac1f5 | 2015-02-12 15:01:59 -0800 | [diff] [blame] | 936 | const cpumask_t *src2p, unsigned int nbits) |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 937 | { |
| 938 | return bitmap_intersects(src1p->bits, src2p->bits, nbits); |
| 939 | } |
| 940 | |
| 941 | #define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS) |
| 942 | static inline int __cpus_subset(const cpumask_t *src1p, |
Rasmus Villemoes | f5ac1f5 | 2015-02-12 15:01:59 -0800 | [diff] [blame] | 943 | const cpumask_t *src2p, unsigned int nbits) |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 944 | { |
| 945 | return bitmap_subset(src1p->bits, src2p->bits, nbits); |
| 946 | } |
| 947 | |
| 948 | #define cpus_empty(src) __cpus_empty(&(src), NR_CPUS) |
Rasmus Villemoes | f5ac1f5 | 2015-02-12 15:01:59 -0800 | [diff] [blame] | 949 | static inline int __cpus_empty(const cpumask_t *srcp, unsigned int nbits) |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 950 | { |
| 951 | return bitmap_empty(srcp->bits, nbits); |
| 952 | } |
| 953 | |
| 954 | #define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS) |
Rasmus Villemoes | f5ac1f5 | 2015-02-12 15:01:59 -0800 | [diff] [blame] | 955 | static inline int __cpus_weight(const cpumask_t *srcp, unsigned int nbits) |
Rusty Russell | 6ba2ef7 | 2009-09-24 09:34:53 -0600 | [diff] [blame] | 956 | { |
| 957 | return bitmap_weight(srcp->bits, nbits); |
| 958 | } |
| 959 | |
| 960 | #define cpus_shift_left(dst, src, n) \ |
| 961 | __cpus_shift_left(&(dst), &(src), (n), NR_CPUS) |
| 962 | static inline void __cpus_shift_left(cpumask_t *dstp, |
| 963 | const cpumask_t *srcp, int n, int nbits) |
| 964 | { |
| 965 | bitmap_shift_left(dstp->bits, srcp->bits, n, nbits); |
| 966 | } |
| 967 | #endif /* !CONFIG_DISABLE_OBSOLETE_CPUMASK_FUNCTIONS */ |
| 968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | #endif /* __LINUX_CPUMASK_H */ |