Linux Audio

Check our new training course

Loading...
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2016 Thomas Gleixner.
  4 * Copyright (C) 2016-2017 Christoph Hellwig.
  5 */
  6#include <linux/interrupt.h>
  7#include <linux/kernel.h>
  8#include <linux/slab.h>
  9#include <linux/cpu.h>
 
 10
 11static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
 12				int cpus_per_vec)
 13{
 14	const struct cpumask *siblmsk;
 15	int cpu, sibl;
 16
 17	for ( ; cpus_per_vec > 0; ) {
 18		cpu = cpumask_first(nmsk);
 19
 20		/* Should not happen, but I'm too lazy to think about it */
 21		if (cpu >= nr_cpu_ids)
 22			return;
 23
 24		cpumask_clear_cpu(cpu, nmsk);
 25		cpumask_set_cpu(cpu, irqmsk);
 26		cpus_per_vec--;
 27
 28		/* If the cpu has siblings, use them first */
 29		siblmsk = topology_sibling_cpumask(cpu);
 30		for (sibl = -1; cpus_per_vec > 0; ) {
 31			sibl = cpumask_next(sibl, siblmsk);
 32			if (sibl >= nr_cpu_ids)
 33				break;
 34			if (!cpumask_test_and_clear_cpu(sibl, nmsk))
 35				continue;
 36			cpumask_set_cpu(sibl, irqmsk);
 37			cpus_per_vec--;
 38		}
 39	}
 40}
 41
 42static cpumask_var_t *alloc_node_to_cpumask(void)
 43{
 44	cpumask_var_t *masks;
 45	int node;
 46
 47	masks = kcalloc(nr_node_ids, sizeof(cpumask_var_t), GFP_KERNEL);
 48	if (!masks)
 49		return NULL;
 50
 51	for (node = 0; node < nr_node_ids; node++) {
 52		if (!zalloc_cpumask_var(&masks[node], GFP_KERNEL))
 53			goto out_unwind;
 54	}
 55
 56	return masks;
 57
 58out_unwind:
 59	while (--node >= 0)
 60		free_cpumask_var(masks[node]);
 61	kfree(masks);
 62	return NULL;
 63}
 64
 65static void free_node_to_cpumask(cpumask_var_t *masks)
 66{
 67	int node;
 68
 69	for (node = 0; node < nr_node_ids; node++)
 70		free_cpumask_var(masks[node]);
 71	kfree(masks);
 72}
 73
 74static void build_node_to_cpumask(cpumask_var_t *masks)
 75{
 76	int cpu;
 77
 78	for_each_possible_cpu(cpu)
 79		cpumask_set_cpu(cpu, masks[cpu_to_node(cpu)]);
 80}
 81
 82static int get_nodes_in_cpumask(cpumask_var_t *node_to_cpumask,
 83				const struct cpumask *mask, nodemask_t *nodemsk)
 84{
 85	int n, nodes = 0;
 86
 87	/* Calculate the number of nodes in the supplied affinity mask */
 88	for_each_node(n) {
 89		if (cpumask_intersects(mask, node_to_cpumask[n])) {
 90			node_set(n, *nodemsk);
 91			nodes++;
 92		}
 93	}
 94	return nodes;
 95}
 96
 97static int irq_build_affinity_masks(const struct irq_affinity *affd,
 98				    int startvec, int numvecs,
 99				    cpumask_var_t *node_to_cpumask,
100				    const struct cpumask *cpu_mask,
101				    struct cpumask *nmsk,
102				    struct cpumask *masks)
103{
104	int n, nodes, cpus_per_vec, extra_vecs, done = 0;
105	int last_affv = affd->pre_vectors + numvecs;
106	int curvec = startvec;
107	nodemask_t nodemsk = NODE_MASK_NONE;
108
109	if (!cpumask_weight(cpu_mask))
110		return 0;
111
112	nodes = get_nodes_in_cpumask(node_to_cpumask, cpu_mask, &nodemsk);
113
114	/*
115	 * If the number of nodes in the mask is greater than or equal the
116	 * number of vectors we just spread the vectors across the nodes.
117	 */
118	if (numvecs <= nodes) {
119		for_each_node_mask(n, nodemsk) {
120			cpumask_copy(masks + curvec, node_to_cpumask[n]);
121			if (++done == numvecs)
122				break;
123			if (++curvec == last_affv)
124				curvec = affd->pre_vectors;
125		}
126		goto out;
127	}
128
129	for_each_node_mask(n, nodemsk) {
130		int ncpus, v, vecs_to_assign, vecs_per_node;
131
132		/* Spread the vectors per node */
133		vecs_per_node = (numvecs - (curvec - affd->pre_vectors)) / nodes;
134
135		/* Get the cpus on this node which are in the mask */
136		cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]);
137
138		/* Calculate the number of cpus per vector */
139		ncpus = cpumask_weight(nmsk);
140		vecs_to_assign = min(vecs_per_node, ncpus);
141
142		/* Account for rounding errors */
143		extra_vecs = ncpus - vecs_to_assign * (ncpus / vecs_to_assign);
144
145		for (v = 0; curvec < last_affv && v < vecs_to_assign;
146		     curvec++, v++) {
147			cpus_per_vec = ncpus / vecs_to_assign;
148
149			/* Account for extra vectors to compensate rounding errors */
150			if (extra_vecs) {
151				cpus_per_vec++;
152				--extra_vecs;
153			}
154			irq_spread_init_one(masks + curvec, nmsk, cpus_per_vec);
155		}
156
157		done += v;
158		if (done >= numvecs)
159			break;
160		if (curvec >= last_affv)
161			curvec = affd->pre_vectors;
162		--nodes;
163	}
164
165out:
166	return done;
167}
168
169/**
170 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
171 * @nvecs:	The total number of vectors
172 * @affd:	Description of the affinity requirements
173 *
174 * Returns the masks pointer or NULL if allocation failed.
175 */
176struct cpumask *
177irq_create_affinity_masks(int nvecs, const struct irq_affinity *affd)
178{
179	int affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
180	int curvec, usedvecs;
181	cpumask_var_t nmsk, npresmsk, *node_to_cpumask;
182	struct cpumask *masks = NULL;
183
184	/*
185	 * If there aren't any vectors left after applying the pre/post
186	 * vectors don't bother with assigning affinity.
 
 
187	 */
188	if (nvecs == affd->pre_vectors + affd->post_vectors)
189		return NULL;
 
 
190
191	if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
192		return NULL;
 
 
 
 
193
194	if (!zalloc_cpumask_var(&npresmsk, GFP_KERNEL))
195		goto outcpumsk;
196
197	node_to_cpumask = alloc_node_to_cpumask();
198	if (!node_to_cpumask)
199		goto outnpresmsk;
 
 
 
200
201	masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
202	if (!masks)
203		goto outnodemsk;
204
205	/* Fill out vectors at the beginning that don't need affinity */
206	for (curvec = 0; curvec < affd->pre_vectors; curvec++)
207		cpumask_copy(masks + curvec, irq_default_affinity);
208
209	/* Stabilize the cpumasks */
210	get_online_cpus();
211	build_node_to_cpumask(node_to_cpumask);
212
213	/* Spread on present CPUs starting from affd->pre_vectors */
214	usedvecs = irq_build_affinity_masks(affd, curvec, affvecs,
215					    node_to_cpumask, cpu_present_mask,
216					    nmsk, masks);
217
218	/*
219	 * Spread on non present CPUs starting from the next vector to be
220	 * handled. If the spreading of present CPUs already exhausted the
221	 * vector space, assign the non present CPUs to the already spread
222	 * out vectors.
223	 */
224	if (usedvecs >= affvecs)
225		curvec = affd->pre_vectors;
226	else
227		curvec = affd->pre_vectors + usedvecs;
228	cpumask_andnot(npresmsk, cpu_possible_mask, cpu_present_mask);
229	usedvecs += irq_build_affinity_masks(affd, curvec, affvecs,
230					     node_to_cpumask, npresmsk,
231					     nmsk, masks);
232	put_online_cpus();
 
 
 
 
 
 
 
 
233
234	/* Fill out vectors at the end that don't need affinity */
235	if (usedvecs >= affvecs)
236		curvec = affd->pre_vectors + affvecs;
237	else
238		curvec = affd->pre_vectors + usedvecs;
239	for (; curvec < nvecs; curvec++)
240		cpumask_copy(masks + curvec, irq_default_affinity);
 
 
 
 
241
242outnodemsk:
243	free_node_to_cpumask(node_to_cpumask);
244outnpresmsk:
245	free_cpumask_var(npresmsk);
246outcpumsk:
247	free_cpumask_var(nmsk);
248	return masks;
249}
250
251/**
252 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
253 * @minvec:	The minimum number of vectors available
254 * @maxvec:	The maximum number of vectors available
255 * @affd:	Description of the affinity requirements
256 */
257int irq_calc_affinity_vectors(int minvec, int maxvec, const struct irq_affinity *affd)
 
258{
259	int resv = affd->pre_vectors + affd->post_vectors;
260	int vecs = maxvec - resv;
261	int ret;
262
263	if (resv > minvec)
264		return 0;
265
266	get_online_cpus();
267	ret = min_t(int, cpumask_weight(cpu_possible_mask), vecs) + resv;
268	put_online_cpus();
269	return ret;
 
 
 
 
 
270}
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2016 Thomas Gleixner.
  4 * Copyright (C) 2016-2017 Christoph Hellwig.
  5 */
  6#include <linux/interrupt.h>
  7#include <linux/kernel.h>
  8#include <linux/slab.h>
  9#include <linux/cpu.h>
 10#include <linux/group_cpus.h>
 11
 12static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs)
 
 13{
 14	affd->nr_sets = 1;
 15	affd->set_size[0] = affvecs;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 16}
 17
 18/**
 19 * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
 20 * @nvecs:	The total number of vectors
 21 * @affd:	Description of the affinity requirements
 22 *
 23 * Returns the irq_affinity_desc pointer or NULL if allocation failed.
 24 */
 25struct irq_affinity_desc *
 26irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
 27{
 28	unsigned int affvecs, curvec, usedvecs, i;
 29	struct irq_affinity_desc *masks = NULL;
 
 
 30
 31	/*
 32	 * Determine the number of vectors which need interrupt affinities
 33	 * assigned. If the pre/post request exhausts the available vectors
 34	 * then nothing to do here except for invoking the calc_sets()
 35	 * callback so the device driver can adjust to the situation.
 36	 */
 37	if (nvecs > affd->pre_vectors + affd->post_vectors)
 38		affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
 39	else
 40		affvecs = 0;
 41
 42	/*
 43	 * Simple invocations do not provide a calc_sets() callback. Install
 44	 * the generic one.
 45	 */
 46	if (!affd->calc_sets)
 47		affd->calc_sets = default_calc_sets;
 48
 49	/* Recalculate the sets */
 50	affd->calc_sets(affd, affvecs);
 51
 52	if (WARN_ON_ONCE(affd->nr_sets > IRQ_AFFINITY_MAX_SETS))
 53		return NULL;
 54
 55	/* Nothing to assign? */
 56	if (!affvecs)
 57		return NULL;
 58
 59	masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
 60	if (!masks)
 61		return NULL;
 62
 63	/* Fill out vectors at the beginning that don't need affinity */
 64	for (curvec = 0; curvec < affd->pre_vectors; curvec++)
 65		cpumask_copy(&masks[curvec].mask, irq_default_affinity);
 
 
 
 
 
 
 
 
 
 66
 67	/*
 68	 * Spread on present CPUs starting from affd->pre_vectors. If we
 69	 * have multiple sets, build each sets affinity mask separately.
 
 
 70	 */
 71	for (i = 0, usedvecs = 0; i < affd->nr_sets; i++) {
 72		unsigned int this_vecs = affd->set_size[i];
 73		int j;
 74		struct cpumask *result = group_cpus_evenly(this_vecs);
 75
 76		if (!result) {
 77			kfree(masks);
 78			return NULL;
 79		}
 80
 81		for (j = 0; j < this_vecs; j++)
 82			cpumask_copy(&masks[curvec + j].mask, &result[j]);
 83		kfree(result);
 84
 85		curvec += this_vecs;
 86		usedvecs += this_vecs;
 87	}
 88
 89	/* Fill out vectors at the end that don't need affinity */
 90	if (usedvecs >= affvecs)
 91		curvec = affd->pre_vectors + affvecs;
 92	else
 93		curvec = affd->pre_vectors + usedvecs;
 94	for (; curvec < nvecs; curvec++)
 95		cpumask_copy(&masks[curvec].mask, irq_default_affinity);
 96
 97	/* Mark the managed interrupts */
 98	for (i = affd->pre_vectors; i < nvecs - affd->post_vectors; i++)
 99		masks[i].is_managed = 1;
100
 
 
 
 
 
 
101	return masks;
102}
103
104/**
105 * irq_calc_affinity_vectors - Calculate the optimal number of vectors
106 * @minvec:	The minimum number of vectors available
107 * @maxvec:	The maximum number of vectors available
108 * @affd:	Description of the affinity requirements
109 */
110unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
111				       const struct irq_affinity *affd)
112{
113	unsigned int resv = affd->pre_vectors + affd->post_vectors;
114	unsigned int set_vecs;
 
115
116	if (resv > minvec)
117		return 0;
118
119	if (affd->calc_sets) {
120		set_vecs = maxvec - resv;
121	} else {
122		cpus_read_lock();
123		set_vecs = cpumask_weight(cpu_possible_mask);
124		cpus_read_unlock();
125	}
126
127	return resv + min(set_vecs, maxvec - resv);
128}