Linux Audio

Check our new training course

Loading...
v3.15
 
  1/*
  2 * cpu_rmap.c: CPU affinity reverse-map support
  3 * Copyright 2011 Solarflare Communications Inc.
  4 *
  5 * This program is free software; you can redistribute it and/or modify it
  6 * under the terms of the GNU General Public License version 2 as published
  7 * by the Free Software Foundation, incorporated herein by reference.
  8 */
  9
 10#include <linux/cpu_rmap.h>
 11#include <linux/interrupt.h>
 12#include <linux/export.h>
 13
 14/*
 15 * These functions maintain a mapping from CPUs to some ordered set of
 16 * objects with CPU affinities.  This can be seen as a reverse-map of
 17 * CPU affinity.  However, we do not assume that the object affinities
 18 * cover all CPUs in the system.  For those CPUs not directly covered
 19 * by object affinities, we attempt to find a nearest object based on
 20 * CPU topology.
 21 */
 22
 23/**
 24 * alloc_cpu_rmap - allocate CPU affinity reverse-map
 25 * @size: Number of objects to be mapped
 26 * @flags: Allocation flags e.g. %GFP_KERNEL
 27 */
 28struct cpu_rmap *alloc_cpu_rmap(unsigned int size, gfp_t flags)
 29{
 30	struct cpu_rmap *rmap;
 31	unsigned int cpu;
 32	size_t obj_offset;
 33
 34	/* This is a silly number of objects, and we use u16 indices. */
 35	if (size > 0xffff)
 36		return NULL;
 37
 38	/* Offset of object pointer array from base structure */
 39	obj_offset = ALIGN(offsetof(struct cpu_rmap, near[nr_cpu_ids]),
 40			   sizeof(void *));
 41
 42	rmap = kzalloc(obj_offset + size * sizeof(rmap->obj[0]), flags);
 43	if (!rmap)
 44		return NULL;
 45
 46	kref_init(&rmap->refcount);
 47	rmap->obj = (void **)((char *)rmap + obj_offset);
 48
 49	/* Initially assign CPUs to objects on a rota, since we have
 50	 * no idea where the objects are.  Use infinite distance, so
 51	 * any object with known distance is preferable.  Include the
 52	 * CPUs that are not present/online, since we definitely want
 53	 * any newly-hotplugged CPUs to have some object assigned.
 54	 */
 55	for_each_possible_cpu(cpu) {
 56		rmap->near[cpu].index = cpu % size;
 57		rmap->near[cpu].dist = CPU_RMAP_DIST_INF;
 58	}
 59
 60	rmap->size = size;
 61	return rmap;
 62}
 63EXPORT_SYMBOL(alloc_cpu_rmap);
 64
 65/**
 66 * cpu_rmap_release - internal reclaiming helper called from kref_put
 67 * @ref: kref to struct cpu_rmap
 68 */
 69static void cpu_rmap_release(struct kref *ref)
 70{
 71	struct cpu_rmap *rmap = container_of(ref, struct cpu_rmap, refcount);
 72	kfree(rmap);
 73}
 74
 75/**
 76 * cpu_rmap_get - internal helper to get new ref on a cpu_rmap
 77 * @rmap: reverse-map allocated with alloc_cpu_rmap()
 78 */
 79static inline void cpu_rmap_get(struct cpu_rmap *rmap)
 80{
 81	kref_get(&rmap->refcount);
 82}
 83
 84/**
 85 * cpu_rmap_put - release ref on a cpu_rmap
 86 * @rmap: reverse-map allocated with alloc_cpu_rmap()
 87 */
 88int cpu_rmap_put(struct cpu_rmap *rmap)
 89{
 90	return kref_put(&rmap->refcount, cpu_rmap_release);
 91}
 92EXPORT_SYMBOL(cpu_rmap_put);
 93
 94/* Reevaluate nearest object for given CPU, comparing with the given
 95 * neighbours at the given distance.
 96 */
 97static bool cpu_rmap_copy_neigh(struct cpu_rmap *rmap, unsigned int cpu,
 98				const struct cpumask *mask, u16 dist)
 99{
100	int neigh;
101
102	for_each_cpu(neigh, mask) {
103		if (rmap->near[cpu].dist > dist &&
104		    rmap->near[neigh].dist <= dist) {
105			rmap->near[cpu].index = rmap->near[neigh].index;
106			rmap->near[cpu].dist = dist;
107			return true;
108		}
109	}
110	return false;
111}
112
113#ifdef DEBUG
114static void debug_print_rmap(const struct cpu_rmap *rmap, const char *prefix)
115{
116	unsigned index;
117	unsigned int cpu;
118
119	pr_info("cpu_rmap %p, %s:\n", rmap, prefix);
120
121	for_each_possible_cpu(cpu) {
122		index = rmap->near[cpu].index;
123		pr_info("cpu %d -> obj %u (distance %u)\n",
124			cpu, index, rmap->near[cpu].dist);
125	}
126}
127#else
128static inline void
129debug_print_rmap(const struct cpu_rmap *rmap, const char *prefix)
130{
131}
132#endif
133
 
 
 
 
 
 
 
 
 
 
 
134/**
135 * cpu_rmap_add - add object to a rmap
136 * @rmap: CPU rmap allocated with alloc_cpu_rmap()
137 * @obj: Object to add to rmap
138 *
139 * Return index of object.
140 */
141int cpu_rmap_add(struct cpu_rmap *rmap, void *obj)
142{
143	u16 index;
 
 
 
144
145	BUG_ON(rmap->used >= rmap->size);
146	index = rmap->used++;
147	rmap->obj[index] = obj;
148	return index;
149}
150EXPORT_SYMBOL(cpu_rmap_add);
151
152/**
153 * cpu_rmap_update - update CPU rmap following a change of object affinity
154 * @rmap: CPU rmap to update
155 * @index: Index of object whose affinity changed
156 * @affinity: New CPU affinity of object
157 */
158int cpu_rmap_update(struct cpu_rmap *rmap, u16 index,
159		    const struct cpumask *affinity)
160{
161	cpumask_var_t update_mask;
162	unsigned int cpu;
163
164	if (unlikely(!zalloc_cpumask_var(&update_mask, GFP_KERNEL)))
165		return -ENOMEM;
166
167	/* Invalidate distance for all CPUs for which this used to be
168	 * the nearest object.  Mark those CPUs for update.
169	 */
170	for_each_online_cpu(cpu) {
171		if (rmap->near[cpu].index == index) {
172			rmap->near[cpu].dist = CPU_RMAP_DIST_INF;
173			cpumask_set_cpu(cpu, update_mask);
174		}
175	}
176
177	debug_print_rmap(rmap, "after invalidating old distances");
178
179	/* Set distance to 0 for all CPUs in the new affinity mask.
180	 * Mark all CPUs within their NUMA nodes for update.
181	 */
182	for_each_cpu(cpu, affinity) {
183		rmap->near[cpu].index = index;
184		rmap->near[cpu].dist = 0;
185		cpumask_or(update_mask, update_mask,
186			   cpumask_of_node(cpu_to_node(cpu)));
187	}
188
189	debug_print_rmap(rmap, "after updating neighbours");
190
191	/* Update distances based on topology */
192	for_each_cpu(cpu, update_mask) {
193		if (cpu_rmap_copy_neigh(rmap, cpu,
194					topology_thread_cpumask(cpu), 1))
195			continue;
196		if (cpu_rmap_copy_neigh(rmap, cpu,
197					topology_core_cpumask(cpu), 2))
198			continue;
199		if (cpu_rmap_copy_neigh(rmap, cpu,
200					cpumask_of_node(cpu_to_node(cpu)), 3))
201			continue;
202		/* We could continue into NUMA node distances, but for now
203		 * we give up.
204		 */
205	}
206
207	debug_print_rmap(rmap, "after copying neighbours");
208
209	free_cpumask_var(update_mask);
210	return 0;
211}
212EXPORT_SYMBOL(cpu_rmap_update);
213
214/* Glue between IRQ affinity notifiers and CPU rmaps */
215
216struct irq_glue {
217	struct irq_affinity_notify notify;
218	struct cpu_rmap *rmap;
219	u16 index;
220};
221
222/**
223 * free_irq_cpu_rmap - free a CPU affinity reverse-map used for IRQs
224 * @rmap: Reverse-map allocated with alloc_irq_cpu_map(), or %NULL
225 *
226 * Must be called in process context, before freeing the IRQs.
227 */
228void free_irq_cpu_rmap(struct cpu_rmap *rmap)
229{
230	struct irq_glue *glue;
231	u16 index;
232
233	if (!rmap)
234		return;
235
236	for (index = 0; index < rmap->used; index++) {
237		glue = rmap->obj[index];
238		irq_set_affinity_notifier(glue->notify.irq, NULL);
 
239	}
240
241	cpu_rmap_put(rmap);
242}
243EXPORT_SYMBOL(free_irq_cpu_rmap);
244
245/**
246 * irq_cpu_rmap_notify - callback for IRQ subsystem when IRQ affinity updated
247 * @notify: struct irq_affinity_notify passed by irq/manage.c
248 * @mask: cpu mask for new SMP affinity
249 *
250 * This is executed in workqueue context.
251 */
252static void
253irq_cpu_rmap_notify(struct irq_affinity_notify *notify, const cpumask_t *mask)
254{
255	struct irq_glue *glue =
256		container_of(notify, struct irq_glue, notify);
257	int rc;
258
259	rc = cpu_rmap_update(glue->rmap, glue->index, mask);
260	if (rc)
261		pr_warning("irq_cpu_rmap_notify: update failed: %d\n", rc);
262}
263
264/**
265 * irq_cpu_rmap_release - reclaiming callback for IRQ subsystem
266 * @ref: kref to struct irq_affinity_notify passed by irq/manage.c
267 */
268static void irq_cpu_rmap_release(struct kref *ref)
269{
270	struct irq_glue *glue =
271		container_of(ref, struct irq_glue, notify.kref);
272
 
273	cpu_rmap_put(glue->rmap);
274	kfree(glue);
275}
276
277/**
 
 
 
 
 
 
 
 
 
 
 
278 * irq_cpu_rmap_add - add an IRQ to a CPU affinity reverse-map
279 * @rmap: The reverse-map
280 * @irq: The IRQ number
281 *
282 * This adds an IRQ affinity notifier that will update the reverse-map
283 * automatically.
284 *
285 * Must be called in process context, after the IRQ is allocated but
286 * before it is bound with request_irq().
287 */
288int irq_cpu_rmap_add(struct cpu_rmap *rmap, int irq)
289{
290	struct irq_glue *glue = kzalloc(sizeof(*glue), GFP_KERNEL);
291	int rc;
292
293	if (!glue)
294		return -ENOMEM;
295	glue->notify.notify = irq_cpu_rmap_notify;
296	glue->notify.release = irq_cpu_rmap_release;
297	glue->rmap = rmap;
298	cpu_rmap_get(rmap);
299	glue->index = cpu_rmap_add(rmap, glue);
 
 
 
 
300	rc = irq_set_affinity_notifier(irq, &glue->notify);
301	if (rc) {
302		cpu_rmap_put(glue->rmap);
303		kfree(glue);
304	}
 
 
 
 
 
 
305	return rc;
306}
307EXPORT_SYMBOL(irq_cpu_rmap_add);
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * cpu_rmap.c: CPU affinity reverse-map support
  4 * Copyright 2011 Solarflare Communications Inc.
 
 
 
 
  5 */
  6
  7#include <linux/cpu_rmap.h>
  8#include <linux/interrupt.h>
  9#include <linux/export.h>
 10
 11/*
 12 * These functions maintain a mapping from CPUs to some ordered set of
 13 * objects with CPU affinities.  This can be seen as a reverse-map of
 14 * CPU affinity.  However, we do not assume that the object affinities
 15 * cover all CPUs in the system.  For those CPUs not directly covered
 16 * by object affinities, we attempt to find a nearest object based on
 17 * CPU topology.
 18 */
 19
 20/**
 21 * alloc_cpu_rmap - allocate CPU affinity reverse-map
 22 * @size: Number of objects to be mapped
 23 * @flags: Allocation flags e.g. %GFP_KERNEL
 24 */
 25struct cpu_rmap *alloc_cpu_rmap(unsigned int size, gfp_t flags)
 26{
 27	struct cpu_rmap *rmap;
 28	unsigned int cpu;
 29	size_t obj_offset;
 30
 31	/* This is a silly number of objects, and we use u16 indices. */
 32	if (size > 0xffff)
 33		return NULL;
 34
 35	/* Offset of object pointer array from base structure */
 36	obj_offset = ALIGN(offsetof(struct cpu_rmap, near[nr_cpu_ids]),
 37			   sizeof(void *));
 38
 39	rmap = kzalloc(obj_offset + size * sizeof(rmap->obj[0]), flags);
 40	if (!rmap)
 41		return NULL;
 42
 43	kref_init(&rmap->refcount);
 44	rmap->obj = (void **)((char *)rmap + obj_offset);
 45
 46	/* Initially assign CPUs to objects on a rota, since we have
 47	 * no idea where the objects are.  Use infinite distance, so
 48	 * any object with known distance is preferable.  Include the
 49	 * CPUs that are not present/online, since we definitely want
 50	 * any newly-hotplugged CPUs to have some object assigned.
 51	 */
 52	for_each_possible_cpu(cpu) {
 53		rmap->near[cpu].index = cpu % size;
 54		rmap->near[cpu].dist = CPU_RMAP_DIST_INF;
 55	}
 56
 57	rmap->size = size;
 58	return rmap;
 59}
 60EXPORT_SYMBOL(alloc_cpu_rmap);
 61
 62/**
 63 * cpu_rmap_release - internal reclaiming helper called from kref_put
 64 * @ref: kref to struct cpu_rmap
 65 */
 66static void cpu_rmap_release(struct kref *ref)
 67{
 68	struct cpu_rmap *rmap = container_of(ref, struct cpu_rmap, refcount);
 69	kfree(rmap);
 70}
 71
 72/**
 73 * cpu_rmap_get - internal helper to get new ref on a cpu_rmap
 74 * @rmap: reverse-map allocated with alloc_cpu_rmap()
 75 */
 76static inline void cpu_rmap_get(struct cpu_rmap *rmap)
 77{
 78	kref_get(&rmap->refcount);
 79}
 80
 81/**
 82 * cpu_rmap_put - release ref on a cpu_rmap
 83 * @rmap: reverse-map allocated with alloc_cpu_rmap()
 84 */
 85int cpu_rmap_put(struct cpu_rmap *rmap)
 86{
 87	return kref_put(&rmap->refcount, cpu_rmap_release);
 88}
 89EXPORT_SYMBOL(cpu_rmap_put);
 90
 91/* Reevaluate nearest object for given CPU, comparing with the given
 92 * neighbours at the given distance.
 93 */
 94static bool cpu_rmap_copy_neigh(struct cpu_rmap *rmap, unsigned int cpu,
 95				const struct cpumask *mask, u16 dist)
 96{
 97	int neigh;
 98
 99	for_each_cpu(neigh, mask) {
100		if (rmap->near[cpu].dist > dist &&
101		    rmap->near[neigh].dist <= dist) {
102			rmap->near[cpu].index = rmap->near[neigh].index;
103			rmap->near[cpu].dist = dist;
104			return true;
105		}
106	}
107	return false;
108}
109
110#ifdef DEBUG
111static void debug_print_rmap(const struct cpu_rmap *rmap, const char *prefix)
112{
113	unsigned index;
114	unsigned int cpu;
115
116	pr_info("cpu_rmap %p, %s:\n", rmap, prefix);
117
118	for_each_possible_cpu(cpu) {
119		index = rmap->near[cpu].index;
120		pr_info("cpu %d -> obj %u (distance %u)\n",
121			cpu, index, rmap->near[cpu].dist);
122	}
123}
124#else
125static inline void
126debug_print_rmap(const struct cpu_rmap *rmap, const char *prefix)
127{
128}
129#endif
130
131static int get_free_index(struct cpu_rmap *rmap)
132{
133	int i;
134
135	for (i = 0; i < rmap->size; i++)
136		if (!rmap->obj[i])
137			return i;
138
139	return -ENOSPC;
140}
141
142/**
143 * cpu_rmap_add - add object to a rmap
144 * @rmap: CPU rmap allocated with alloc_cpu_rmap()
145 * @obj: Object to add to rmap
146 *
147 * Return index of object or -ENOSPC if no free entry was found
148 */
149int cpu_rmap_add(struct cpu_rmap *rmap, void *obj)
150{
151	int index = get_free_index(rmap);
152
153	if (index < 0)
154		return index;
155
 
 
156	rmap->obj[index] = obj;
157	return index;
158}
159EXPORT_SYMBOL(cpu_rmap_add);
160
161/**
162 * cpu_rmap_update - update CPU rmap following a change of object affinity
163 * @rmap: CPU rmap to update
164 * @index: Index of object whose affinity changed
165 * @affinity: New CPU affinity of object
166 */
167int cpu_rmap_update(struct cpu_rmap *rmap, u16 index,
168		    const struct cpumask *affinity)
169{
170	cpumask_var_t update_mask;
171	unsigned int cpu;
172
173	if (unlikely(!zalloc_cpumask_var(&update_mask, GFP_KERNEL)))
174		return -ENOMEM;
175
176	/* Invalidate distance for all CPUs for which this used to be
177	 * the nearest object.  Mark those CPUs for update.
178	 */
179	for_each_online_cpu(cpu) {
180		if (rmap->near[cpu].index == index) {
181			rmap->near[cpu].dist = CPU_RMAP_DIST_INF;
182			cpumask_set_cpu(cpu, update_mask);
183		}
184	}
185
186	debug_print_rmap(rmap, "after invalidating old distances");
187
188	/* Set distance to 0 for all CPUs in the new affinity mask.
189	 * Mark all CPUs within their NUMA nodes for update.
190	 */
191	for_each_cpu(cpu, affinity) {
192		rmap->near[cpu].index = index;
193		rmap->near[cpu].dist = 0;
194		cpumask_or(update_mask, update_mask,
195			   cpumask_of_node(cpu_to_node(cpu)));
196	}
197
198	debug_print_rmap(rmap, "after updating neighbours");
199
200	/* Update distances based on topology */
201	for_each_cpu(cpu, update_mask) {
202		if (cpu_rmap_copy_neigh(rmap, cpu,
203					topology_sibling_cpumask(cpu), 1))
204			continue;
205		if (cpu_rmap_copy_neigh(rmap, cpu,
206					topology_core_cpumask(cpu), 2))
207			continue;
208		if (cpu_rmap_copy_neigh(rmap, cpu,
209					cpumask_of_node(cpu_to_node(cpu)), 3))
210			continue;
211		/* We could continue into NUMA node distances, but for now
212		 * we give up.
213		 */
214	}
215
216	debug_print_rmap(rmap, "after copying neighbours");
217
218	free_cpumask_var(update_mask);
219	return 0;
220}
221EXPORT_SYMBOL(cpu_rmap_update);
222
223/* Glue between IRQ affinity notifiers and CPU rmaps */
224
225struct irq_glue {
226	struct irq_affinity_notify notify;
227	struct cpu_rmap *rmap;
228	u16 index;
229};
230
231/**
232 * free_irq_cpu_rmap - free a CPU affinity reverse-map used for IRQs
233 * @rmap: Reverse-map allocated with alloc_irq_cpu_map(), or %NULL
234 *
235 * Must be called in process context, before freeing the IRQs.
236 */
237void free_irq_cpu_rmap(struct cpu_rmap *rmap)
238{
239	struct irq_glue *glue;
240	u16 index;
241
242	if (!rmap)
243		return;
244
245	for (index = 0; index < rmap->size; index++) {
246		glue = rmap->obj[index];
247		if (glue)
248			irq_set_affinity_notifier(glue->notify.irq, NULL);
249	}
250
251	cpu_rmap_put(rmap);
252}
253EXPORT_SYMBOL(free_irq_cpu_rmap);
254
255/**
256 * irq_cpu_rmap_notify - callback for IRQ subsystem when IRQ affinity updated
257 * @notify: struct irq_affinity_notify passed by irq/manage.c
258 * @mask: cpu mask for new SMP affinity
259 *
260 * This is executed in workqueue context.
261 */
262static void
263irq_cpu_rmap_notify(struct irq_affinity_notify *notify, const cpumask_t *mask)
264{
265	struct irq_glue *glue =
266		container_of(notify, struct irq_glue, notify);
267	int rc;
268
269	rc = cpu_rmap_update(glue->rmap, glue->index, mask);
270	if (rc)
271		pr_warn("irq_cpu_rmap_notify: update failed: %d\n", rc);
272}
273
274/**
275 * irq_cpu_rmap_release - reclaiming callback for IRQ subsystem
276 * @ref: kref to struct irq_affinity_notify passed by irq/manage.c
277 */
278static void irq_cpu_rmap_release(struct kref *ref)
279{
280	struct irq_glue *glue =
281		container_of(ref, struct irq_glue, notify.kref);
282
283	glue->rmap->obj[glue->index] = NULL;
284	cpu_rmap_put(glue->rmap);
285	kfree(glue);
286}
287
288/**
289 * irq_cpu_rmap_remove - remove an IRQ from a CPU affinity reverse-map
290 * @rmap: The reverse-map
291 * @irq: The IRQ number
292 */
293int irq_cpu_rmap_remove(struct cpu_rmap *rmap, int irq)
294{
295	return irq_set_affinity_notifier(irq, NULL);
296}
297EXPORT_SYMBOL(irq_cpu_rmap_remove);
298
299/**
300 * irq_cpu_rmap_add - add an IRQ to a CPU affinity reverse-map
301 * @rmap: The reverse-map
302 * @irq: The IRQ number
303 *
304 * This adds an IRQ affinity notifier that will update the reverse-map
305 * automatically.
306 *
307 * Must be called in process context, after the IRQ is allocated but
308 * before it is bound with request_irq().
309 */
310int irq_cpu_rmap_add(struct cpu_rmap *rmap, int irq)
311{
312	struct irq_glue *glue = kzalloc(sizeof(*glue), GFP_KERNEL);
313	int rc;
314
315	if (!glue)
316		return -ENOMEM;
317	glue->notify.notify = irq_cpu_rmap_notify;
318	glue->notify.release = irq_cpu_rmap_release;
319	glue->rmap = rmap;
320	cpu_rmap_get(rmap);
321	rc = cpu_rmap_add(rmap, glue);
322	if (rc < 0)
323		goto err_add;
324
325	glue->index = rc;
326	rc = irq_set_affinity_notifier(irq, &glue->notify);
327	if (rc)
328		goto err_set;
329
330	return rc;
331
332err_set:
333	rmap->obj[glue->index] = NULL;
334err_add:
335	cpu_rmap_put(glue->rmap);
336	kfree(glue);
337	return rc;
338}
339EXPORT_SYMBOL(irq_cpu_rmap_add);