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 { 957b4967c5SMike Travis *mask = kmalloc_node(cpumask_size(), flags, node); 96*38c7fed2SYinghai Lu 972d3854a3SRusty Russell #ifdef CONFIG_DEBUG_PER_CPU_MAPS 982d3854a3SRusty Russell if (!*mask) { 992d3854a3SRusty Russell printk(KERN_ERR "=> alloc_cpumask_var: failed!\n"); 1002d3854a3SRusty Russell dump_stack(); 1012d3854a3SRusty Russell } 1022d3854a3SRusty Russell #endif 1032a530080SRusty Russell /* FIXME: Bandaid to save us from old primitives which go to NR_CPUS. */ 1042a530080SRusty Russell if (*mask) { 1054f032ac4SJack Steiner unsigned char *ptr = (unsigned char *)cpumask_bits(*mask); 1062a530080SRusty Russell unsigned int tail; 1072a530080SRusty Russell tail = BITS_TO_LONGS(NR_CPUS - nr_cpumask_bits) * sizeof(long); 1084f032ac4SJack Steiner memset(ptr + cpumask_size() - tail, 0, tail); 1092a530080SRusty Russell } 1102a530080SRusty Russell 1112d3854a3SRusty Russell return *mask != NULL; 1122d3854a3SRusty Russell } 1137b4967c5SMike Travis EXPORT_SYMBOL(alloc_cpumask_var_node); 1147b4967c5SMike Travis 1150281b5dcSYinghai Lu bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node) 1160281b5dcSYinghai Lu { 1170281b5dcSYinghai Lu return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node); 1180281b5dcSYinghai Lu } 1190281b5dcSYinghai Lu EXPORT_SYMBOL(zalloc_cpumask_var_node); 1200281b5dcSYinghai Lu 121ec26b805SMike Travis /** 122ec26b805SMike Travis * alloc_cpumask_var - allocate a struct cpumask 123ec26b805SMike Travis * @mask: pointer to cpumask_var_t where the cpumask is returned 124ec26b805SMike Travis * @flags: GFP_ flags 125ec26b805SMike Travis * 126ec26b805SMike Travis * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is 127ec26b805SMike Travis * a nop returning a constant 1 (in <linux/cpumask.h>). 128ec26b805SMike Travis * 129ec26b805SMike Travis * See alloc_cpumask_var_node. 130ec26b805SMike Travis */ 1317b4967c5SMike Travis bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) 1327b4967c5SMike Travis { 1337b4967c5SMike Travis return alloc_cpumask_var_node(mask, flags, numa_node_id()); 1347b4967c5SMike Travis } 1352d3854a3SRusty Russell EXPORT_SYMBOL(alloc_cpumask_var); 1362d3854a3SRusty Russell 1370281b5dcSYinghai Lu bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags) 1380281b5dcSYinghai Lu { 1390281b5dcSYinghai Lu return alloc_cpumask_var(mask, flags | __GFP_ZERO); 1400281b5dcSYinghai Lu } 1410281b5dcSYinghai Lu EXPORT_SYMBOL(zalloc_cpumask_var); 1420281b5dcSYinghai Lu 143ec26b805SMike Travis /** 144ec26b805SMike Travis * alloc_bootmem_cpumask_var - allocate a struct cpumask from the bootmem arena. 145ec26b805SMike Travis * @mask: pointer to cpumask_var_t where the cpumask is returned 146ec26b805SMike Travis * 147ec26b805SMike Travis * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is 148e9690a6eSLi Zefan * a nop (in <linux/cpumask.h>). 149ec26b805SMike Travis * Either returns an allocated (zero-filled) cpumask, or causes the 150ec26b805SMike Travis * system to panic. 151ec26b805SMike Travis */ 1522d3854a3SRusty Russell void __init alloc_bootmem_cpumask_var(cpumask_var_t *mask) 1532d3854a3SRusty Russell { 1542d3854a3SRusty Russell *mask = alloc_bootmem(cpumask_size()); 1552d3854a3SRusty Russell } 1562d3854a3SRusty Russell 157ec26b805SMike Travis /** 158ec26b805SMike Travis * free_cpumask_var - frees memory allocated for a struct cpumask. 159ec26b805SMike Travis * @mask: cpumask to free 160ec26b805SMike Travis * 161ec26b805SMike Travis * This is safe on a NULL mask. 162ec26b805SMike Travis */ 1632d3854a3SRusty Russell void free_cpumask_var(cpumask_var_t mask) 1642d3854a3SRusty Russell { 1652d3854a3SRusty Russell kfree(mask); 1662d3854a3SRusty Russell } 1672d3854a3SRusty Russell EXPORT_SYMBOL(free_cpumask_var); 168cd83e42cSRusty Russell 169ec26b805SMike Travis /** 170ec26b805SMike Travis * free_bootmem_cpumask_var - frees result of alloc_bootmem_cpumask_var 171ec26b805SMike Travis * @mask: cpumask to free 172ec26b805SMike Travis */ 173984f2f37SRusty Russell void __init free_bootmem_cpumask_var(cpumask_var_t mask) 174cd83e42cSRusty Russell { 175cd83e42cSRusty Russell free_bootmem((unsigned long)mask, cpumask_size()); 176cd83e42cSRusty Russell } 1772d3854a3SRusty Russell #endif 178