1ccb46000SAndrew Morton #include <linux/kernel.h> 2ccb46000SAndrew Morton #include <linux/bitops.h> 3ccb46000SAndrew Morton #include <linux/cpumask.h> 4ccb46000SAndrew Morton #include <linux/module.h> 52d3854a3SRusty Russell #include <linux/bootmem.h> 6ccb46000SAndrew Morton 7ccb46000SAndrew Morton int __first_cpu(const cpumask_t *srcp) 8ccb46000SAndrew Morton { 9ccb46000SAndrew Morton return min_t(int, NR_CPUS, find_first_bit(srcp->bits, NR_CPUS)); 10ccb46000SAndrew Morton } 11ccb46000SAndrew Morton EXPORT_SYMBOL(__first_cpu); 12ccb46000SAndrew Morton 133d18bd74SAndrew Morton int __next_cpu(int n, const cpumask_t *srcp) 143d18bd74SAndrew Morton { 153d18bd74SAndrew Morton return min_t(int, NR_CPUS, find_next_bit(srcp->bits, NR_CPUS, n+1)); 163d18bd74SAndrew Morton } 173d18bd74SAndrew Morton EXPORT_SYMBOL(__next_cpu); 1886302820SAndrew Morton 1941df0d61SMike Travis #if NR_CPUS > 64 2041df0d61SMike Travis int __next_cpu_nr(int n, const cpumask_t *srcp) 2141df0d61SMike Travis { 2241df0d61SMike Travis return min_t(int, nr_cpu_ids, 2341df0d61SMike Travis find_next_bit(srcp->bits, nr_cpu_ids, n+1)); 2441df0d61SMike Travis } 2541df0d61SMike Travis EXPORT_SYMBOL(__next_cpu_nr); 2641df0d61SMike Travis #endif 2741df0d61SMike Travis 2896a9b4d3SAndrew Morton int __any_online_cpu(const cpumask_t *mask) 2996a9b4d3SAndrew Morton { 3096a9b4d3SAndrew Morton int cpu; 3196a9b4d3SAndrew Morton 3296a9b4d3SAndrew Morton for_each_cpu_mask(cpu, *mask) { 3396a9b4d3SAndrew Morton if (cpu_online(cpu)) 3496a9b4d3SAndrew Morton break; 3596a9b4d3SAndrew Morton } 3696a9b4d3SAndrew Morton return cpu; 3796a9b4d3SAndrew Morton } 3896a9b4d3SAndrew Morton EXPORT_SYMBOL(__any_online_cpu); 392d3854a3SRusty Russell 402d3854a3SRusty Russell /** 412d3854a3SRusty Russell * cpumask_next_and - get the next cpu in *src1p & *src2p 422d3854a3SRusty Russell * @n: the cpu prior to the place to search (ie. return will be > @n) 432d3854a3SRusty Russell * @src1p: the first cpumask pointer 442d3854a3SRusty Russell * @src2p: the second cpumask pointer 452d3854a3SRusty Russell * 462d3854a3SRusty Russell * Returns >= nr_cpu_ids if no further cpus set in both. 472d3854a3SRusty Russell */ 482d3854a3SRusty Russell int cpumask_next_and(int n, const struct cpumask *src1p, 492d3854a3SRusty Russell const struct cpumask *src2p) 502d3854a3SRusty Russell { 512d3854a3SRusty Russell while ((n = cpumask_next(n, src1p)) < nr_cpu_ids) 522d3854a3SRusty Russell if (cpumask_test_cpu(n, src2p)) 532d3854a3SRusty Russell break; 542d3854a3SRusty Russell return n; 552d3854a3SRusty Russell } 562d3854a3SRusty Russell EXPORT_SYMBOL(cpumask_next_and); 572d3854a3SRusty Russell 582d3854a3SRusty Russell /** 592d3854a3SRusty Russell * cpumask_any_but - return a "random" in a cpumask, but not this one. 602d3854a3SRusty Russell * @mask: the cpumask to search 612d3854a3SRusty Russell * @cpu: the cpu to ignore. 622d3854a3SRusty Russell * 632d3854a3SRusty Russell * Often used to find any cpu but smp_processor_id() in a mask. 642d3854a3SRusty Russell * Returns >= nr_cpu_ids if no cpus set. 652d3854a3SRusty Russell */ 662d3854a3SRusty Russell int cpumask_any_but(const struct cpumask *mask, unsigned int cpu) 672d3854a3SRusty Russell { 682d3854a3SRusty Russell unsigned int i; 692d3854a3SRusty Russell 70984f2f37SRusty Russell cpumask_check(cpu); 712d3854a3SRusty Russell for_each_cpu(i, mask) 722d3854a3SRusty Russell if (i != cpu) 732d3854a3SRusty Russell break; 742d3854a3SRusty Russell return i; 752d3854a3SRusty Russell } 762d3854a3SRusty Russell 772d3854a3SRusty Russell /* These are not inline because of header tangles. */ 782d3854a3SRusty Russell #ifdef CONFIG_CPUMASK_OFFSTACK 79ec26b805SMike Travis /** 80ec26b805SMike Travis * alloc_cpumask_var_node - allocate a struct cpumask on a given node 81ec26b805SMike Travis * @mask: pointer to cpumask_var_t where the cpumask is returned 82ec26b805SMike Travis * @flags: GFP_ flags 83ec26b805SMike Travis * 84ec26b805SMike Travis * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is 85ec26b805SMike Travis * a nop returning a constant 1 (in <linux/cpumask.h>) 86ec26b805SMike Travis * Returns TRUE if memory allocation succeeded, FALSE otherwise. 87ec26b805SMike Travis * 88ec26b805SMike Travis * In addition, mask will be NULL if this fails. Note that gcc is 89ec26b805SMike Travis * usually smart enough to know that mask can never be NULL if 90ec26b805SMike Travis * CONFIG_CPUMASK_OFFSTACK=n, so does code elimination in that case 91ec26b805SMike Travis * too. 92ec26b805SMike Travis */ 937b4967c5SMike Travis bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node) 942d3854a3SRusty Russell { 952d3854a3SRusty Russell if (likely(slab_is_available())) 967b4967c5SMike Travis *mask = kmalloc_node(cpumask_size(), flags, node); 972d3854a3SRusty Russell else { 982d3854a3SRusty Russell #ifdef CONFIG_DEBUG_PER_CPU_MAPS 992d3854a3SRusty Russell printk(KERN_ERR 1002d3854a3SRusty Russell "=> alloc_cpumask_var: kmalloc not available!\n"); 1012d3854a3SRusty Russell #endif 1022d3854a3SRusty Russell *mask = NULL; 1032d3854a3SRusty Russell } 1042d3854a3SRusty Russell #ifdef CONFIG_DEBUG_PER_CPU_MAPS 1052d3854a3SRusty Russell if (!*mask) { 1062d3854a3SRusty Russell printk(KERN_ERR "=> alloc_cpumask_var: failed!\n"); 1072d3854a3SRusty Russell dump_stack(); 1082d3854a3SRusty Russell } 1092d3854a3SRusty Russell #endif 110*2a530080SRusty Russell /* FIXME: Bandaid to save us from old primitives which go to NR_CPUS. */ 111*2a530080SRusty Russell if (*mask) { 112*2a530080SRusty Russell unsigned int tail; 113*2a530080SRusty Russell tail = BITS_TO_LONGS(NR_CPUS - nr_cpumask_bits) * sizeof(long); 114*2a530080SRusty Russell memset(cpumask_bits(*mask) + cpumask_size() - tail, 115*2a530080SRusty Russell 0, tail); 116*2a530080SRusty Russell } 117*2a530080SRusty Russell 1182d3854a3SRusty Russell return *mask != NULL; 1192d3854a3SRusty Russell } 1207b4967c5SMike Travis EXPORT_SYMBOL(alloc_cpumask_var_node); 1217b4967c5SMike Travis 122ec26b805SMike Travis /** 123ec26b805SMike Travis * alloc_cpumask_var - allocate a struct cpumask 124ec26b805SMike Travis * @mask: pointer to cpumask_var_t where the cpumask is returned 125ec26b805SMike Travis * @flags: GFP_ flags 126ec26b805SMike Travis * 127ec26b805SMike Travis * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is 128ec26b805SMike Travis * a nop returning a constant 1 (in <linux/cpumask.h>). 129ec26b805SMike Travis * 130ec26b805SMike Travis * See alloc_cpumask_var_node. 131ec26b805SMike Travis */ 1327b4967c5SMike Travis bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) 1337b4967c5SMike Travis { 1347b4967c5SMike Travis return alloc_cpumask_var_node(mask, flags, numa_node_id()); 1357b4967c5SMike Travis } 1362d3854a3SRusty Russell EXPORT_SYMBOL(alloc_cpumask_var); 1372d3854a3SRusty Russell 138ec26b805SMike Travis /** 139ec26b805SMike Travis * alloc_bootmem_cpumask_var - allocate a struct cpumask from the bootmem arena. 140ec26b805SMike Travis * @mask: pointer to cpumask_var_t where the cpumask is returned 141ec26b805SMike Travis * 142ec26b805SMike Travis * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is 143e9690a6eSLi Zefan * a nop (in <linux/cpumask.h>). 144ec26b805SMike Travis * Either returns an allocated (zero-filled) cpumask, or causes the 145ec26b805SMike Travis * system to panic. 146ec26b805SMike Travis */ 1472d3854a3SRusty Russell void __init alloc_bootmem_cpumask_var(cpumask_var_t *mask) 1482d3854a3SRusty Russell { 1492d3854a3SRusty Russell *mask = alloc_bootmem(cpumask_size()); 1502d3854a3SRusty Russell } 1512d3854a3SRusty Russell 152ec26b805SMike Travis /** 153ec26b805SMike Travis * free_cpumask_var - frees memory allocated for a struct cpumask. 154ec26b805SMike Travis * @mask: cpumask to free 155ec26b805SMike Travis * 156ec26b805SMike Travis * This is safe on a NULL mask. 157ec26b805SMike Travis */ 1582d3854a3SRusty Russell void free_cpumask_var(cpumask_var_t mask) 1592d3854a3SRusty Russell { 1602d3854a3SRusty Russell kfree(mask); 1612d3854a3SRusty Russell } 1622d3854a3SRusty Russell EXPORT_SYMBOL(free_cpumask_var); 163cd83e42cSRusty Russell 164ec26b805SMike Travis /** 165ec26b805SMike Travis * free_bootmem_cpumask_var - frees result of alloc_bootmem_cpumask_var 166ec26b805SMike Travis * @mask: cpumask to free 167ec26b805SMike Travis */ 168984f2f37SRusty Russell void __init free_bootmem_cpumask_var(cpumask_var_t mask) 169cd83e42cSRusty Russell { 170cd83e42cSRusty Russell free_bootmem((unsigned long)mask, cpumask_size()); 171cd83e42cSRusty Russell } 1722d3854a3SRusty Russell #endif 173