xref: /openbmc/linux/lib/cpumask.c (revision 2452483d9546de1c540f330469dc4042ff089731)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
25a0e3ad6STejun Heo #include <linux/slab.h>
3ccb46000SAndrew Morton #include <linux/kernel.h>
4ccb46000SAndrew Morton #include <linux/bitops.h>
5ccb46000SAndrew Morton #include <linux/cpumask.h>
68bc3bcc9SPaul Gortmaker #include <linux/export.h>
757c8a661SMike Rapoport #include <linux/memblock.h>
898fa15f3SAnshuman Khandual #include <linux/numa.h>
9ccb46000SAndrew Morton 
102d3854a3SRusty Russell /**
11f22ef333SAlexey Dobriyan  * cpumask_next - get the next cpu in a cpumask
12f22ef333SAlexey Dobriyan  * @n: the cpu prior to the place to search (ie. return will be > @n)
13f22ef333SAlexey Dobriyan  * @srcp: the cpumask pointer
14f22ef333SAlexey Dobriyan  *
15f22ef333SAlexey Dobriyan  * Returns >= nr_cpu_ids if no further cpus set.
16f22ef333SAlexey Dobriyan  */
17f22ef333SAlexey Dobriyan unsigned int cpumask_next(int n, const struct cpumask *srcp)
18f22ef333SAlexey Dobriyan {
19f22ef333SAlexey Dobriyan 	/* -1 is a legal arg here. */
20f22ef333SAlexey Dobriyan 	if (n != -1)
21f22ef333SAlexey Dobriyan 		cpumask_check(n);
22f22ef333SAlexey Dobriyan 	return find_next_bit(cpumask_bits(srcp), nr_cpumask_bits, n + 1);
23f22ef333SAlexey Dobriyan }
24f22ef333SAlexey Dobriyan EXPORT_SYMBOL(cpumask_next);
25f22ef333SAlexey Dobriyan 
26f22ef333SAlexey Dobriyan /**
272d3854a3SRusty Russell  * cpumask_next_and - get the next cpu in *src1p & *src2p
282d3854a3SRusty Russell  * @n: the cpu prior to the place to search (ie. return will be > @n)
292d3854a3SRusty Russell  * @src1p: the first cpumask pointer
302d3854a3SRusty Russell  * @src2p: the second cpumask pointer
312d3854a3SRusty Russell  *
322d3854a3SRusty Russell  * Returns >= nr_cpu_ids if no further cpus set in both.
332d3854a3SRusty Russell  */
342d3854a3SRusty Russell int cpumask_next_and(int n, const struct cpumask *src1p,
352d3854a3SRusty Russell 		     const struct cpumask *src2p)
362d3854a3SRusty Russell {
370ade34c3SClement Courbet 	/* -1 is a legal arg here. */
380ade34c3SClement Courbet 	if (n != -1)
390ade34c3SClement Courbet 		cpumask_check(n);
400ade34c3SClement Courbet 	return find_next_and_bit(cpumask_bits(src1p), cpumask_bits(src2p),
410ade34c3SClement Courbet 		nr_cpumask_bits, n + 1);
422d3854a3SRusty Russell }
432d3854a3SRusty Russell EXPORT_SYMBOL(cpumask_next_and);
442d3854a3SRusty Russell 
452d3854a3SRusty Russell /**
462d3854a3SRusty Russell  * cpumask_any_but - return a "random" in a cpumask, but not this one.
472d3854a3SRusty Russell  * @mask: the cpumask to search
482d3854a3SRusty Russell  * @cpu: the cpu to ignore.
492d3854a3SRusty Russell  *
502d3854a3SRusty Russell  * Often used to find any cpu but smp_processor_id() in a mask.
512d3854a3SRusty Russell  * Returns >= nr_cpu_ids if no cpus set.
522d3854a3SRusty Russell  */
532d3854a3SRusty Russell int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
542d3854a3SRusty Russell {
552d3854a3SRusty Russell 	unsigned int i;
562d3854a3SRusty Russell 
57984f2f37SRusty Russell 	cpumask_check(cpu);
582d3854a3SRusty Russell 	for_each_cpu(i, mask)
592d3854a3SRusty Russell 		if (i != cpu)
602d3854a3SRusty Russell 			break;
612d3854a3SRusty Russell 	return i;
622d3854a3SRusty Russell }
633712bba1SThomas Gleixner EXPORT_SYMBOL(cpumask_any_but);
642d3854a3SRusty Russell 
65c743f0a5SPeter Zijlstra /**
66c743f0a5SPeter Zijlstra  * cpumask_next_wrap - helper to implement for_each_cpu_wrap
67c743f0a5SPeter Zijlstra  * @n: the cpu prior to the place to search
68c743f0a5SPeter Zijlstra  * @mask: the cpumask pointer
69c743f0a5SPeter Zijlstra  * @start: the start point of the iteration
70c743f0a5SPeter Zijlstra  * @wrap: assume @n crossing @start terminates the iteration
71c743f0a5SPeter Zijlstra  *
72c743f0a5SPeter Zijlstra  * Returns >= nr_cpu_ids on completion
73c743f0a5SPeter Zijlstra  *
74c743f0a5SPeter Zijlstra  * Note: the @wrap argument is required for the start condition when
75c743f0a5SPeter Zijlstra  * we cannot assume @start is set in @mask.
76c743f0a5SPeter Zijlstra  */
77c743f0a5SPeter Zijlstra int cpumask_next_wrap(int n, const struct cpumask *mask, int start, bool wrap)
78c743f0a5SPeter Zijlstra {
79c743f0a5SPeter Zijlstra 	int next;
80c743f0a5SPeter Zijlstra 
81c743f0a5SPeter Zijlstra again:
82c743f0a5SPeter Zijlstra 	next = cpumask_next(n, mask);
83c743f0a5SPeter Zijlstra 
84c743f0a5SPeter Zijlstra 	if (wrap && n < start && next >= start) {
85c743f0a5SPeter Zijlstra 		return nr_cpumask_bits;
86c743f0a5SPeter Zijlstra 
87c743f0a5SPeter Zijlstra 	} else if (next >= nr_cpumask_bits) {
88c743f0a5SPeter Zijlstra 		wrap = true;
89c743f0a5SPeter Zijlstra 		n = -1;
90c743f0a5SPeter Zijlstra 		goto again;
91c743f0a5SPeter Zijlstra 	}
92c743f0a5SPeter Zijlstra 
93c743f0a5SPeter Zijlstra 	return next;
94c743f0a5SPeter Zijlstra }
95c743f0a5SPeter Zijlstra EXPORT_SYMBOL(cpumask_next_wrap);
96c743f0a5SPeter Zijlstra 
972d3854a3SRusty Russell /* These are not inline because of header tangles. */
982d3854a3SRusty Russell #ifdef CONFIG_CPUMASK_OFFSTACK
99ec26b805SMike Travis /**
100ec26b805SMike Travis  * alloc_cpumask_var_node - allocate a struct cpumask on a given node
101ec26b805SMike Travis  * @mask: pointer to cpumask_var_t where the cpumask is returned
102ec26b805SMike Travis  * @flags: GFP_ flags
103ec26b805SMike Travis  *
104ec26b805SMike Travis  * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
105ec26b805SMike Travis  * a nop returning a constant 1 (in <linux/cpumask.h>)
106ec26b805SMike Travis  * Returns TRUE if memory allocation succeeded, FALSE otherwise.
107ec26b805SMike Travis  *
108ec26b805SMike Travis  * In addition, mask will be NULL if this fails.  Note that gcc is
109ec26b805SMike Travis  * usually smart enough to know that mask can never be NULL if
110ec26b805SMike Travis  * CONFIG_CPUMASK_OFFSTACK=n, so does code elimination in that case
111ec26b805SMike Travis  * too.
112ec26b805SMike Travis  */
1137b4967c5SMike Travis bool alloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
1142d3854a3SRusty Russell {
1157b4967c5SMike Travis 	*mask = kmalloc_node(cpumask_size(), flags, node);
11638c7fed2SYinghai Lu 
1172d3854a3SRusty Russell #ifdef CONFIG_DEBUG_PER_CPU_MAPS
1182d3854a3SRusty Russell 	if (!*mask) {
1192d3854a3SRusty Russell 		printk(KERN_ERR "=> alloc_cpumask_var: failed!\n");
1202d3854a3SRusty Russell 		dump_stack();
1212d3854a3SRusty Russell 	}
1222d3854a3SRusty Russell #endif
1232a530080SRusty Russell 
1242d3854a3SRusty Russell 	return *mask != NULL;
1252d3854a3SRusty Russell }
1267b4967c5SMike Travis EXPORT_SYMBOL(alloc_cpumask_var_node);
1277b4967c5SMike Travis 
1280281b5dcSYinghai Lu bool zalloc_cpumask_var_node(cpumask_var_t *mask, gfp_t flags, int node)
1290281b5dcSYinghai Lu {
1300281b5dcSYinghai Lu 	return alloc_cpumask_var_node(mask, flags | __GFP_ZERO, node);
1310281b5dcSYinghai Lu }
1320281b5dcSYinghai Lu EXPORT_SYMBOL(zalloc_cpumask_var_node);
1330281b5dcSYinghai Lu 
134ec26b805SMike Travis /**
135ec26b805SMike Travis  * alloc_cpumask_var - allocate a struct cpumask
136ec26b805SMike Travis  * @mask: pointer to cpumask_var_t where the cpumask is returned
137ec26b805SMike Travis  * @flags: GFP_ flags
138ec26b805SMike Travis  *
139ec26b805SMike Travis  * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
140ec26b805SMike Travis  * a nop returning a constant 1 (in <linux/cpumask.h>).
141ec26b805SMike Travis  *
142ec26b805SMike Travis  * See alloc_cpumask_var_node.
143ec26b805SMike Travis  */
1447b4967c5SMike Travis bool alloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1457b4967c5SMike Travis {
14637e7b5f1SKOSAKI Motohiro 	return alloc_cpumask_var_node(mask, flags, NUMA_NO_NODE);
1477b4967c5SMike Travis }
1482d3854a3SRusty Russell EXPORT_SYMBOL(alloc_cpumask_var);
1492d3854a3SRusty Russell 
1500281b5dcSYinghai Lu bool zalloc_cpumask_var(cpumask_var_t *mask, gfp_t flags)
1510281b5dcSYinghai Lu {
1520281b5dcSYinghai Lu 	return alloc_cpumask_var(mask, flags | __GFP_ZERO);
1530281b5dcSYinghai Lu }
1540281b5dcSYinghai Lu EXPORT_SYMBOL(zalloc_cpumask_var);
1550281b5dcSYinghai Lu 
156ec26b805SMike Travis /**
157ec26b805SMike Travis  * alloc_bootmem_cpumask_var - allocate a struct cpumask from the bootmem arena.
158ec26b805SMike Travis  * @mask: pointer to cpumask_var_t where the cpumask is returned
159ec26b805SMike Travis  *
160ec26b805SMike Travis  * Only defined when CONFIG_CPUMASK_OFFSTACK=y, otherwise is
161e9690a6eSLi Zefan  * a nop (in <linux/cpumask.h>).
162ec26b805SMike Travis  * Either returns an allocated (zero-filled) cpumask, or causes the
163ec26b805SMike Travis  * system to panic.
164ec26b805SMike Travis  */
1652d3854a3SRusty Russell void __init alloc_bootmem_cpumask_var(cpumask_var_t *mask)
1662d3854a3SRusty Russell {
1677e1c4e27SMike Rapoport 	*mask = memblock_alloc(cpumask_size(), SMP_CACHE_BYTES);
1688a7f97b9SMike Rapoport 	if (!*mask)
1698a7f97b9SMike Rapoport 		panic("%s: Failed to allocate %u bytes\n", __func__,
1708a7f97b9SMike Rapoport 		      cpumask_size());
1712d3854a3SRusty Russell }
1722d3854a3SRusty Russell 
173ec26b805SMike Travis /**
174ec26b805SMike Travis  * free_cpumask_var - frees memory allocated for a struct cpumask.
175ec26b805SMike Travis  * @mask: cpumask to free
176ec26b805SMike Travis  *
177ec26b805SMike Travis  * This is safe on a NULL mask.
178ec26b805SMike Travis  */
1792d3854a3SRusty Russell void free_cpumask_var(cpumask_var_t mask)
1802d3854a3SRusty Russell {
1812d3854a3SRusty Russell 	kfree(mask);
1822d3854a3SRusty Russell }
1832d3854a3SRusty Russell EXPORT_SYMBOL(free_cpumask_var);
184cd83e42cSRusty Russell 
185ec26b805SMike Travis /**
186ec26b805SMike Travis  * free_bootmem_cpumask_var - frees result of alloc_bootmem_cpumask_var
187ec26b805SMike Travis  * @mask: cpumask to free
188ec26b805SMike Travis  */
189984f2f37SRusty Russell void __init free_bootmem_cpumask_var(cpumask_var_t mask)
190cd83e42cSRusty Russell {
191c1529500SSantosh Shilimkar 	memblock_free_early(__pa(mask), cpumask_size());
192cd83e42cSRusty Russell }
1932d3854a3SRusty Russell #endif
194da91309eSAmir Vadai 
195da91309eSAmir Vadai /**
196f36963c9SRusty Russell  * cpumask_local_spread - select the i'th cpu with local numa cpu's first
197da91309eSAmir Vadai  * @i: index number
198f36963c9SRusty Russell  * @node: local numa_node
199da91309eSAmir Vadai  *
200f36963c9SRusty Russell  * This function selects an online CPU according to a numa aware policy;
201f36963c9SRusty Russell  * local cpus are returned first, followed by non-local ones, then it
202f36963c9SRusty Russell  * wraps around.
203da91309eSAmir Vadai  *
204f36963c9SRusty Russell  * It's not very efficient, but useful for setup.
205da91309eSAmir Vadai  */
206f36963c9SRusty Russell unsigned int cpumask_local_spread(unsigned int i, int node)
207da91309eSAmir Vadai {
208*2452483dSThomas Gleixner 	int cpu;
209da91309eSAmir Vadai 
210f36963c9SRusty Russell 	/* Wrap: we always want a cpu. */
211*2452483dSThomas Gleixner 	i %= num_online_cpus();
212da91309eSAmir Vadai 
21398fa15f3SAnshuman Khandual 	if (node == NUMA_NO_NODE) {
214*2452483dSThomas Gleixner 		for_each_cpu(cpu, cpu_online_mask)
215f36963c9SRusty Russell 			if (i-- == 0)
216f36963c9SRusty Russell 				return cpu;
217da91309eSAmir Vadai 	} else {
218f36963c9SRusty Russell 		/* NUMA first. */
219*2452483dSThomas Gleixner 		for_each_cpu_and(cpu, cpumask_of_node(node), cpu_online_mask)
220f36963c9SRusty Russell 			if (i-- == 0)
221f36963c9SRusty Russell 				return cpu;
222da91309eSAmir Vadai 
223*2452483dSThomas Gleixner 		for_each_cpu(cpu, cpu_online_mask) {
224f36963c9SRusty Russell 			/* Skip NUMA nodes, done above. */
225f36963c9SRusty Russell 			if (cpumask_test_cpu(cpu, cpumask_of_node(node)))
226f36963c9SRusty Russell 				continue;
227da91309eSAmir Vadai 
228f36963c9SRusty Russell 			if (i-- == 0)
229f36963c9SRusty Russell 				return cpu;
230da91309eSAmir Vadai 		}
231da91309eSAmir Vadai 	}
232f36963c9SRusty Russell 	BUG();
233da91309eSAmir Vadai }
234f36963c9SRusty Russell EXPORT_SYMBOL(cpumask_local_spread);
23546a87b38SPaul Turner 
23646a87b38SPaul Turner static DEFINE_PER_CPU(int, distribute_cpu_mask_prev);
23746a87b38SPaul Turner 
23846a87b38SPaul Turner /**
23946a87b38SPaul Turner  * Returns an arbitrary cpu within srcp1 & srcp2.
24046a87b38SPaul Turner  *
24146a87b38SPaul Turner  * Iterated calls using the same srcp1 and srcp2 will be distributed within
24246a87b38SPaul Turner  * their intersection.
24346a87b38SPaul Turner  *
24446a87b38SPaul Turner  * Returns >= nr_cpu_ids if the intersection is empty.
24546a87b38SPaul Turner  */
24646a87b38SPaul Turner int cpumask_any_and_distribute(const struct cpumask *src1p,
24746a87b38SPaul Turner 			       const struct cpumask *src2p)
24846a87b38SPaul Turner {
24946a87b38SPaul Turner 	int next, prev;
25046a87b38SPaul Turner 
25146a87b38SPaul Turner 	/* NOTE: our first selection will skip 0. */
25246a87b38SPaul Turner 	prev = __this_cpu_read(distribute_cpu_mask_prev);
25346a87b38SPaul Turner 
25446a87b38SPaul Turner 	next = cpumask_next_and(prev, src1p, src2p);
25546a87b38SPaul Turner 	if (next >= nr_cpu_ids)
25646a87b38SPaul Turner 		next = cpumask_first_and(src1p, src2p);
25746a87b38SPaul Turner 
25846a87b38SPaul Turner 	if (next < nr_cpu_ids)
25946a87b38SPaul Turner 		__this_cpu_write(distribute_cpu_mask_prev, next);
26046a87b38SPaul Turner 
26146a87b38SPaul Turner 	return next;
26246a87b38SPaul Turner }
26346a87b38SPaul Turner EXPORT_SYMBOL(cpumask_any_and_distribute);
26414e292f8SPeter Zijlstra 
26514e292f8SPeter Zijlstra int cpumask_any_distribute(const struct cpumask *srcp)
26614e292f8SPeter Zijlstra {
26714e292f8SPeter Zijlstra 	int next, prev;
26814e292f8SPeter Zijlstra 
26914e292f8SPeter Zijlstra 	/* NOTE: our first selection will skip 0. */
27014e292f8SPeter Zijlstra 	prev = __this_cpu_read(distribute_cpu_mask_prev);
27114e292f8SPeter Zijlstra 
27214e292f8SPeter Zijlstra 	next = cpumask_next(prev, srcp);
27314e292f8SPeter Zijlstra 	if (next >= nr_cpu_ids)
27414e292f8SPeter Zijlstra 		next = cpumask_first(srcp);
27514e292f8SPeter Zijlstra 
27614e292f8SPeter Zijlstra 	if (next < nr_cpu_ids)
27714e292f8SPeter Zijlstra 		__this_cpu_write(distribute_cpu_mask_prev, next);
27814e292f8SPeter Zijlstra 
27914e292f8SPeter Zijlstra 	return next;
28014e292f8SPeter Zijlstra }
28114e292f8SPeter Zijlstra EXPORT_SYMBOL(cpumask_any_distribute);
282