15a0e3ad6STejun Heo #include <linux/slab.h> 2ccb46000SAndrew Morton #include <linux/kernel.h> 3ccb46000SAndrew Morton #include <linux/bitops.h> 4ccb46000SAndrew Morton #include <linux/cpumask.h> 58bc3bcc9SPaul Gortmaker #include <linux/export.h> 62d3854a3SRusty Russell #include <linux/bootmem.h> 7ccb46000SAndrew Morton 82d3854a3SRusty Russell /** 9*f22ef333SAlexey Dobriyan * cpumask_next - get the next cpu in a cpumask 10*f22ef333SAlexey Dobriyan * @n: the cpu prior to the place to search (ie. return will be > @n) 11*f22ef333SAlexey Dobriyan * @srcp: the cpumask pointer 12*f22ef333SAlexey Dobriyan * 13*f22ef333SAlexey Dobriyan * Returns >= nr_cpu_ids if no further cpus set. 14*f22ef333SAlexey Dobriyan */ 15*f22ef333SAlexey Dobriyan unsigned int cpumask_next(int n, const struct cpumask *srcp) 16*f22ef333SAlexey Dobriyan { 17*f22ef333SAlexey Dobriyan /* -1 is a legal arg here. */ 18*f22ef333SAlexey Dobriyan if (n != -1) 19*f22ef333SAlexey Dobriyan cpumask_check(n); 20*f22ef333SAlexey Dobriyan return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n + 1); 21*f22ef333SAlexey Dobriyan } 22*f22ef333SAlexey Dobriyan EXPORT_SYMBOL(cpumask_next); 23*f22ef333SAlexey Dobriyan 24*f22ef333SAlexey Dobriyan /** 252d3854a3SRusty Russell * cpumask_next_and - get the next cpu in *src1p & *src2p 262d3854a3SRusty Russell * @n: the cpu prior to the place to search (ie. return will be > @n) 272d3854a3SRusty Russell * @src1p: the first cpumask pointer 282d3854a3SRusty Russell * @src2p: the second cpumask pointer 292d3854a3SRusty Russell * 302d3854a3SRusty Russell * Returns >= nr_cpu_ids if no further cpus set in both. 312d3854a3SRusty Russell */ 322d3854a3SRusty Russell int cpumask_next_and(int n, const struct cpumask *src1p, 332d3854a3SRusty Russell const struct cpumask *src2p) 342d3854a3SRusty Russell { 355ca62d65SAndrew Morton while ((n = cpumask_next(n, src1p)) < nr_cpu_ids) 365ca62d65SAndrew Morton if (cpumask_test_cpu(n, src2p)) 375ca62d65SAndrew Morton break; 385ca62d65SAndrew Morton return n; 392d3854a3SRusty Russell } 402d3854a3SRusty Russell EXPORT_SYMBOL(cpumask_next_and); 412d3854a3SRusty Russell 422d3854a3SRusty Russell /** 432d3854a3SRusty Russell * cpumask_any_but - return a "random" in a cpumask, but not this one. 442d3854a3SRusty Russell * @mask: the cpumask to search 452d3854a3SRusty Russell * @cpu: the cpu to ignore. 462d3854a3SRusty Russell * 472d3854a3SRusty Russell * Often used to find any cpu but smp_processor_id() in a mask. 482d3854a3SRusty Russell * Returns >= nr_cpu_ids if no cpus set. 492d3854a3SRusty Russell */ 502d3854a3SRusty Russell int cpumask_any_but(const struct cpumask *mask, unsigned int cpu) 512d3854a3SRusty Russell { 522d3854a3SRusty Russell unsigned int i; 532d3854a3SRusty Russell 54984f2f37SRusty Russell cpumask_check(cpu); 552d3854a3SRusty Russell for_each_cpu(i, mask) 562d3854a3SRusty Russell if (i != cpu) 572d3854a3SRusty Russell break; 582d3854a3SRusty Russell return i; 592d3854a3SRusty Russell } 603712bba1SThomas Gleixner EXPORT_SYMBOL(cpumask_any_but); 612d3854a3SRusty Russell 62c743f0a5SPeter Zijlstra /** 63c743f0a5SPeter Zijlstra * cpumask_next_wrap - helper to implement for_each_cpu_wrap 64c743f0a5SPeter Zijlstra * @n: the cpu prior to the place to search 65c743f0a5SPeter Zijlstra * @mask: the cpumask pointer 66c743f0a5SPeter Zijlstra * @start: the start point of the iteration 67c743f0a5SPeter Zijlstra * @wrap: assume @n crossing @start terminates the iteration 68c743f0a5SPeter Zijlstra * 69c743f0a5SPeter Zijlstra * Returns >= nr_cpu_ids on completion 70c743f0a5SPeter Zijlstra * 71c743f0a5SPeter Zijlstra * Note: the @wrap argument is required for the start condition when 72c743f0a5SPeter Zijlstra * we cannot assume @start is set in @mask. 73c743f0a5SPeter Zijlstra */ 74c743f0a5SPeter Zijlstra int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap) 75c743f0a5SPeter Zijlstra { 76c743f0a5SPeter Zijlstra int next; 77c743f0a5SPeter Zijlstra 78c743f0a5SPeter Zijlstra again: 79c743f0a5SPeter Zijlstra next = cpumask_next(n, mask); 80c743f0a5SPeter Zijlstra 81c743f0a5SPeter Zijlstra if (wrap && n < start && next >= start) { 82c743f0a5SPeter Zijlstra return nr_cpumask_bits; 83c743f0a5SPeter Zijlstra 84c743f0a5SPeter Zijlstra } else if (next >= nr_cpumask_bits) { 85c743f0a5SPeter Zijlstra wrap = true; 86c743f0a5SPeter Zijlstra n = -1; 87c743f0a5SPeter Zijlstra goto again; 88c743f0a5SPeter Zijlstra } 89c743f0a5SPeter Zijlstra 90c743f0a5SPeter Zijlstra return next; 91c743f0a5SPeter Zijlstra } 92c743f0a5SPeter Zijlstra EXPORT_SYMBOL(cpumask_next_wrap); 93c743f0a5SPeter Zijlstra 942d3854a3SRusty Russell /* These are not inline because of header tangles. */ 952d3854a3SRusty Russell #ifdef CONFIG_CPUMASK_OFFSTACK 96ec26b805SMike Travis /** 97ec26b805SMike Travis * alloc_cpumask_var_node - allocate a struct cpumask on a given node 98ec26b805SMike Travis * @mask: pointer to cpumask_var_t where the cpumask is returned 99ec26b805SMike Travis * @flags: GFP_ flags 100ec26b805SMike Travis * 101ec26b805SMike Travis * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is 102ec26b805SMike Travis * a nop returning a constant 1 (in <linux/cpumask.h>) 103ec26b805SMike Travis * Returns TRUE if memory allocation succeeded, FALSE otherwise. 104ec26b805SMike Travis * 105ec26b805SMike Travis * In addition, mask will be NULL if this fails. Note that gcc is 106ec26b805SMike Travis * usually smart enough to know that mask can never be NULL if 107ec26b805SMike Travis * CONFIG_CPUMASK_OFFSTACK=n, so does code elimination in that case 108ec26b805SMike Travis * too. 109ec26b805SMike Travis */ 1107b4967c5SMike Travis bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node) 1112d3854a3SRusty Russell { 1127b4967c5SMike Travis *mask = kmalloc_node(cpumask_size(), flags, node); 11338c7fed2SYinghai Lu 1142d3854a3SRusty Russell #ifdef CONFIG_DEBUG_PER_CPU_MAPS 1152d3854a3SRusty Russell if (!*mask) { 1162d3854a3SRusty Russell printk(KERN_ERR "=> alloc_cpumask_var: failed!\n"); 1172d3854a3SRusty Russell dump_stack(); 1182d3854a3SRusty Russell } 1192d3854a3SRusty Russell #endif 1202a530080SRusty Russell 1212d3854a3SRusty Russell return *mask != NULL; 1222d3854a3SRusty Russell } 1237b4967c5SMike Travis EXPORT_SYMBOL(alloc_cpumask_var_node); 1247b4967c5SMike Travis 1250281b5dcSYinghai Lu bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node) 1260281b5dcSYinghai Lu { 1270281b5dcSYinghai Lu return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node); 1280281b5dcSYinghai Lu } 1290281b5dcSYinghai Lu EXPORT_SYMBOL(zalloc_cpumask_var_node); 1300281b5dcSYinghai Lu 131ec26b805SMike Travis /** 132ec26b805SMike Travis * alloc_cpumask_var - allocate a struct cpumask 133ec26b805SMike Travis * @mask: pointer to cpumask_var_t where the cpumask is returned 134ec26b805SMike Travis * @flags: GFP_ flags 135ec26b805SMike Travis * 136ec26b805SMike Travis * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is 137ec26b805SMike Travis * a nop returning a constant 1 (in <linux/cpumask.h>). 138ec26b805SMike Travis * 139ec26b805SMike Travis * See alloc_cpumask_var_node. 140ec26b805SMike Travis */ 1417b4967c5SMike Travis bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) 1427b4967c5SMike Travis { 14337e7b5f1SKOSAKI Motohiro return alloc_cpumask_var_node(mask, flags, NUMA_NO_NODE); 1447b4967c5SMike Travis } 1452d3854a3SRusty Russell EXPORT_SYMBOL(alloc_cpumask_var); 1462d3854a3SRusty Russell 1470281b5dcSYinghai Lu bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) 1480281b5dcSYinghai Lu { 1490281b5dcSYinghai Lu return alloc_cpumask_var(mask, flags | __GFP_ZERO); 1500281b5dcSYinghai Lu } 1510281b5dcSYinghai Lu EXPORT_SYMBOL(zalloc_cpumask_var); 1520281b5dcSYinghai Lu 153ec26b805SMike Travis /** 154ec26b805SMike Travis * alloc_bootmem_cpumask_var - allocate a struct cpumask from the bootmem arena. 155ec26b805SMike Travis * @mask: pointer to cpumask_var_t where the cpumask is returned 156ec26b805SMike Travis * 157ec26b805SMike Travis * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is 158e9690a6eSLi Zefan * a nop (in <linux/cpumask.h>). 159ec26b805SMike Travis * Either returns an allocated (zero-filled) cpumask, or causes the 160ec26b805SMike Travis * system to panic. 161ec26b805SMike Travis */ 1622d3854a3SRusty Russell void __init alloc_bootmem_cpumask_var(cpumask_var_t *mask) 1632d3854a3SRusty Russell { 164c1529500SSantosh Shilimkar *mask = memblock_virt_alloc(cpumask_size(), 0); 1652d3854a3SRusty Russell } 1662d3854a3SRusty Russell 167ec26b805SMike Travis /** 168ec26b805SMike Travis * free_cpumask_var - frees memory allocated for a struct cpumask. 169ec26b805SMike Travis * @mask: cpumask to free 170ec26b805SMike Travis * 171ec26b805SMike Travis * This is safe on a NULL mask. 172ec26b805SMike Travis */ 1732d3854a3SRusty Russell void free_cpumask_var(cpumask_var_t mask) 1742d3854a3SRusty Russell { 1752d3854a3SRusty Russell kfree(mask); 1762d3854a3SRusty Russell } 1772d3854a3SRusty Russell EXPORT_SYMBOL(free_cpumask_var); 178cd83e42cSRusty Russell 179ec26b805SMike Travis /** 180ec26b805SMike Travis * free_bootmem_cpumask_var - frees result of alloc_bootmem_cpumask_var 181ec26b805SMike Travis * @mask: cpumask to free 182ec26b805SMike Travis */ 183984f2f37SRusty Russell void __init free_bootmem_cpumask_var(cpumask_var_t mask) 184cd83e42cSRusty Russell { 185c1529500SSantosh Shilimkar memblock_free_early(__pa(mask), cpumask_size()); 186cd83e42cSRusty Russell } 1872d3854a3SRusty Russell #endif 188da91309eSAmir Vadai 189da91309eSAmir Vadai /** 190f36963c9SRusty Russell * cpumask_local_spread - select the i'th cpu with local numa cpu's first 191da91309eSAmir Vadai * @i: index number 192f36963c9SRusty Russell * @node: local numa_node 193da91309eSAmir Vadai * 194f36963c9SRusty Russell * This function selects an online CPU according to a numa aware policy; 195f36963c9SRusty Russell * local cpus are returned first, followed by non-local ones, then it 196f36963c9SRusty Russell * wraps around. 197da91309eSAmir Vadai * 198f36963c9SRusty Russell * It's not very efficient, but useful for setup. 199da91309eSAmir Vadai */ 200f36963c9SRusty Russell unsigned int cpumask_local_spread(unsigned int i, int node) 201da91309eSAmir Vadai { 202da91309eSAmir Vadai int cpu; 203da91309eSAmir Vadai 204f36963c9SRusty Russell /* Wrap: we always want a cpu. */ 205da91309eSAmir Vadai i %= num_online_cpus(); 206da91309eSAmir Vadai 207f36963c9SRusty Russell if (node == -1) { 208f36963c9SRusty Russell for_each_cpu(cpu, cpu_online_mask) 209f36963c9SRusty Russell if (i-- == 0) 210f36963c9SRusty Russell return cpu; 211da91309eSAmir Vadai } else { 212f36963c9SRusty Russell /* NUMA first. */ 213f36963c9SRusty Russell for_each_cpu_and(cpu, cpumask_of_node(node), cpu_online_mask) 214f36963c9SRusty Russell if (i-- == 0) 215f36963c9SRusty Russell return cpu; 216da91309eSAmir Vadai 217f36963c9SRusty Russell for_each_cpu(cpu, cpu_online_mask) { 218f36963c9SRusty Russell /* Skip NUMA nodes, done above. */ 219f36963c9SRusty Russell if (cpumask_test_cpu(cpu, cpumask_of_node(node))) 220f36963c9SRusty Russell continue; 221da91309eSAmir Vadai 222f36963c9SRusty Russell if (i-- == 0) 223f36963c9SRusty Russell return cpu; 224da91309eSAmir Vadai } 225da91309eSAmir Vadai } 226f36963c9SRusty Russell BUG(); 227da91309eSAmir Vadai } 228f36963c9SRusty Russell EXPORT_SYMBOL(cpumask_local_spread); 229