xref: /openbmc/linux/lib/cpumask.c (revision 8bc3bcc93a2b4e47d5d410146f6546bca6171663)
15a0e3ad6STejun Heo #include <linux/slab.h>
2ccb46000SAndrew Morton #include <linux/kernel.h>
3ccb46000SAndrew Morton #include <linux/bitops.h>
4ccb46000SAndrew Morton #include <linux/cpumask.h>
5*8bc3bcc9SPaul Gortmaker #include <linux/export.h>
62d3854a3SRusty Russell #include <linux/bootmem.h>
7ccb46000SAndrew Morton 
8ccb46000SAndrew Morton int __first_cpu(const cpumask_t *srcp)
9ccb46000SAndrew Morton {
10ccb46000SAndrew Morton 	return min_t(int, NR_CPUS, find_first_bit(srcp->bits, NR_CPUS));
11ccb46000SAndrew Morton }
12ccb46000SAndrew Morton EXPORT_SYMBOL(__first_cpu);
13ccb46000SAndrew Morton 
143d18bd74SAndrew Morton int __next_cpu(int n, const cpumask_t *srcp)
153d18bd74SAndrew Morton {
163d18bd74SAndrew Morton 	return min_t(int, NR_CPUS, find_next_bit(srcp->bits, NR_CPUS, n+1));
173d18bd74SAndrew Morton }
183d18bd74SAndrew Morton EXPORT_SYMBOL(__next_cpu);
1986302820SAndrew Morton 
2041df0d61SMike Travis #if NR_CPUS > 64
2141df0d61SMike Travis int __next_cpu_nr(int n, const cpumask_t *srcp)
2241df0d61SMike Travis {
2341df0d61SMike Travis 	return min_t(int, nr_cpu_ids,
2441df0d61SMike Travis 				find_next_bit(srcp->bits, nr_cpu_ids, n+1));
2541df0d61SMike Travis }
2641df0d61SMike Travis EXPORT_SYMBOL(__next_cpu_nr);
2741df0d61SMike Travis #endif
2841df0d61SMike Travis 
2996a9b4d3SAndrew Morton int __any_online_cpu(const cpumask_t *mask)
3096a9b4d3SAndrew Morton {
3196a9b4d3SAndrew Morton 	int cpu;
3296a9b4d3SAndrew Morton 
3395918f4aSKOSAKI Motohiro 	for_each_cpu(cpu, mask) {
3496a9b4d3SAndrew Morton 		if (cpu_online(cpu))
3596a9b4d3SAndrew Morton 			break;
3696a9b4d3SAndrew Morton 	}
3796a9b4d3SAndrew Morton 	return cpu;
3896a9b4d3SAndrew Morton }
3996a9b4d3SAndrew Morton EXPORT_SYMBOL(__any_online_cpu);
402d3854a3SRusty Russell 
412d3854a3SRusty Russell /**
422d3854a3SRusty Russell  * cpumask_next_and - get the next cpu in *src1p & *src2p
432d3854a3SRusty Russell  * @n: the cpu prior to the place to search (ie. return will be > @n)
442d3854a3SRusty Russell  * @src1p: the first cpumask pointer
452d3854a3SRusty Russell  * @src2p: the second cpumask pointer
462d3854a3SRusty Russell  *
472d3854a3SRusty Russell  * Returns >= nr_cpu_ids if no further cpus set in both.
482d3854a3SRusty Russell  */
492d3854a3SRusty Russell int cpumask_next_and(int n, const struct cpumask *src1p,
502d3854a3SRusty Russell 		     const struct cpumask *src2p)
512d3854a3SRusty Russell {
522d3854a3SRusty Russell 	while ((n = cpumask_next(n, src1p)) < nr_cpu_ids)
532d3854a3SRusty Russell 		if (cpumask_test_cpu(n, src2p))
542d3854a3SRusty Russell 			break;
552d3854a3SRusty Russell 	return n;
562d3854a3SRusty Russell }
572d3854a3SRusty Russell EXPORT_SYMBOL(cpumask_next_and);
582d3854a3SRusty Russell 
592d3854a3SRusty Russell /**
602d3854a3SRusty Russell  * cpumask_any_but - return a "random" in a cpumask, but not this one.
612d3854a3SRusty Russell  * @mask: the cpumask to search
622d3854a3SRusty Russell  * @cpu: the cpu to ignore.
632d3854a3SRusty Russell  *
642d3854a3SRusty Russell  * Often used to find any cpu but smp_processor_id() in a mask.
652d3854a3SRusty Russell  * Returns >= nr_cpu_ids if no cpus set.
662d3854a3SRusty Russell  */
672d3854a3SRusty Russell int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
682d3854a3SRusty Russell {
692d3854a3SRusty Russell 	unsigned int i;
702d3854a3SRusty Russell 
71984f2f37SRusty Russell 	cpumask_check(cpu);
722d3854a3SRusty Russell 	for_each_cpu(i, mask)
732d3854a3SRusty Russell 		if (i != cpu)
742d3854a3SRusty Russell 			break;
752d3854a3SRusty Russell 	return i;
762d3854a3SRusty Russell }
772d3854a3SRusty Russell 
782d3854a3SRusty Russell /* These are not inline because of header tangles. */
792d3854a3SRusty Russell #ifdef CONFIG_CPUMASK_OFFSTACK
80ec26b805SMike Travis /**
81ec26b805SMike Travis  * alloc_cpumask_var_node - allocate a struct cpumask on a given node
82ec26b805SMike Travis  * @mask: pointer to cpumask_var_t where the cpumask is returned
83ec26b805SMike Travis  * @flags: GFP_ flags
84ec26b805SMike Travis  *
85ec26b805SMike Travis  * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
86ec26b805SMike Travis  * a nop returning a constant 1 (in <linux/cpumask.h>)
87ec26b805SMike Travis  * Returns TRUE if memory allocation succeeded, FALSE otherwise.
88ec26b805SMike Travis  *
89ec26b805SMike Travis  * In addition, mask will be NULL if this fails.  Note that gcc is
90ec26b805SMike Travis  * usually smart enough to know that mask can never be NULL if
91ec26b805SMike Travis  * CONFIG_CPUMASK_OFFSTACK=n, so does code elimination in that case
92ec26b805SMike Travis  * too.
93ec26b805SMike Travis  */
947b4967c5SMike Travis bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
952d3854a3SRusty Russell {
967b4967c5SMike Travis 	*mask = kmalloc_node(cpumask_size(), flags, node);
9738c7fed2SYinghai Lu 
982d3854a3SRusty Russell #ifdef CONFIG_DEBUG_PER_CPU_MAPS
992d3854a3SRusty Russell 	if (!*mask) {
1002d3854a3SRusty Russell 		printk(KERN_ERR "=> alloc_cpumask_var: failed!\n");
1012d3854a3SRusty Russell 		dump_stack();
1022d3854a3SRusty Russell 	}
1032d3854a3SRusty Russell #endif
1042a530080SRusty Russell 	/* FIXME: Bandaid to save us from old primitives which go to NR_CPUS. */
1052a530080SRusty Russell 	if (*mask) {
1064f032ac4SJack Steiner 		unsigned char *ptr = (unsigned char *)cpumask_bits(*mask);
1072a530080SRusty Russell 		unsigned int tail;
1082a530080SRusty Russell 		tail = BITS_TO_LONGS(NR_CPUS - nr_cpumask_bits) * sizeof(long);
1094f032ac4SJack Steiner 		memset(ptr + cpumask_size() - tail, 0, tail);
1102a530080SRusty Russell 	}
1112a530080SRusty Russell 
1122d3854a3SRusty Russell 	return *mask != NULL;
1132d3854a3SRusty Russell }
1147b4967c5SMike Travis EXPORT_SYMBOL(alloc_cpumask_var_node);
1157b4967c5SMike Travis 
1160281b5dcSYinghai Lu bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
1170281b5dcSYinghai Lu {
1180281b5dcSYinghai Lu 	return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node);
1190281b5dcSYinghai Lu }
1200281b5dcSYinghai Lu EXPORT_SYMBOL(zalloc_cpumask_var_node);
1210281b5dcSYinghai Lu 
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 {
13437e7b5f1SKOSAKI Motohiro 	return alloc_cpumask_var_node(mask, flags, NUMA_NO_NODE);
1357b4967c5SMike Travis }
1362d3854a3SRusty Russell EXPORT_SYMBOL(alloc_cpumask_var);
1372d3854a3SRusty Russell 
1380281b5dcSYinghai Lu bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1390281b5dcSYinghai Lu {
1400281b5dcSYinghai Lu 	return alloc_cpumask_var(mask, flags | __GFP_ZERO);
1410281b5dcSYinghai Lu }
1420281b5dcSYinghai Lu EXPORT_SYMBOL(zalloc_cpumask_var);
1430281b5dcSYinghai Lu 
144ec26b805SMike Travis /**
145ec26b805SMike Travis  * alloc_bootmem_cpumask_var - allocate a struct cpumask from the bootmem arena.
146ec26b805SMike Travis  * @mask: pointer to cpumask_var_t where the cpumask is returned
147ec26b805SMike Travis  *
148ec26b805SMike Travis  * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
149e9690a6eSLi Zefan  * a nop (in <linux/cpumask.h>).
150ec26b805SMike Travis  * Either returns an allocated (zero-filled) cpumask, or causes the
151ec26b805SMike Travis  * system to panic.
152ec26b805SMike Travis  */
1532d3854a3SRusty Russell void __init alloc_bootmem_cpumask_var(cpumask_var_t *mask)
1542d3854a3SRusty Russell {
1552d3854a3SRusty Russell 	*mask = alloc_bootmem(cpumask_size());
1562d3854a3SRusty Russell }
1572d3854a3SRusty Russell 
158ec26b805SMike Travis /**
159ec26b805SMike Travis  * free_cpumask_var - frees memory allocated for a struct cpumask.
160ec26b805SMike Travis  * @mask: cpumask to free
161ec26b805SMike Travis  *
162ec26b805SMike Travis  * This is safe on a NULL mask.
163ec26b805SMike Travis  */
1642d3854a3SRusty Russell void free_cpumask_var(cpumask_var_t mask)
1652d3854a3SRusty Russell {
1662d3854a3SRusty Russell 	kfree(mask);
1672d3854a3SRusty Russell }
1682d3854a3SRusty Russell EXPORT_SYMBOL(free_cpumask_var);
169cd83e42cSRusty Russell 
170ec26b805SMike Travis /**
171ec26b805SMike Travis  * free_bootmem_cpumask_var - frees result of alloc_bootmem_cpumask_var
172ec26b805SMike Travis  * @mask: cpumask to free
173ec26b805SMike Travis  */
174984f2f37SRusty Russell void __init free_bootmem_cpumask_var(cpumask_var_t mask)
175cd83e42cSRusty Russell {
176cd83e42cSRusty Russell 	free_bootmem((unsigned long)mask, cpumask_size());
177cd83e42cSRusty Russell }
1782d3854a3SRusty Russell #endif
179