1 // SPDX-License-Identifier: GPL-2.0-only
3 * cpu_rmap.c: CPU affinity reverse-map support
4 * Copyright 2011 Solarflare Communications Inc.
7 #include <linux/cpu_rmap.h>
8 #include <linux/interrupt.h>
9 #include <linux/export.h>
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
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
25 struct cpu_rmap *alloc_cpu_rmap(unsigned int size, gfp_t flags)
27 struct cpu_rmap *rmap;
31 /* This is a silly number of objects, and we use u16 indices. */
35 /* Offset of object pointer array from base structure */
36 obj_offset = ALIGN(offsetof(struct cpu_rmap, near[nr_cpu_ids]),
39 rmap = kzalloc(obj_offset + size * sizeof(rmap->obj[0]), flags);
43 kref_init(&rmap->refcount);
44 rmap->obj = (void **)((char *)rmap + obj_offset);
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.
52 for_each_possible_cpu(cpu) {
53 rmap->near[cpu].index = cpu % size;
54 rmap->near[cpu].dist = CPU_RMAP_DIST_INF;
60 EXPORT_SYMBOL(alloc_cpu_rmap);
63 * cpu_rmap_release - internal reclaiming helper called from kref_put
64 * @ref: kref to struct cpu_rmap
66 static void cpu_rmap_release(struct kref *ref)
68 struct cpu_rmap *rmap = container_of(ref, struct cpu_rmap, refcount);
73 * cpu_rmap_get - internal helper to get new ref on a cpu_rmap
74 * @rmap: reverse-map allocated with alloc_cpu_rmap()
76 static inline void cpu_rmap_get(struct cpu_rmap *rmap)
78 kref_get(&rmap->refcount);
82 * cpu_rmap_put - release ref on a cpu_rmap
83 * @rmap: reverse-map allocated with alloc_cpu_rmap()
85 int cpu_rmap_put(struct cpu_rmap *rmap)
87 return kref_put(&rmap->refcount, cpu_rmap_release);
89 EXPORT_SYMBOL(cpu_rmap_put);
91 /* Reevaluate nearest object for given CPU, comparing with the given
92 * neighbours at the given distance.
94 static bool cpu_rmap_copy_neigh(struct cpu_rmap *rmap, unsigned int cpu,
95 const struct cpumask *mask, u16 dist)
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;
111 static void debug_print_rmap(const struct cpu_rmap *rmap, const char *prefix)
116 pr_info("cpu_rmap %p, %s:\n", rmap, prefix);
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);
126 debug_print_rmap(const struct cpu_rmap *rmap, const char *prefix)
131 static int get_free_index(struct cpu_rmap *rmap)
135 for (i = 0; i < rmap->size; i++)
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
147 * Return index of object or -ENOSPC if no free entry was found
149 int cpu_rmap_add(struct cpu_rmap *rmap, void *obj)
151 int index = get_free_index(rmap);
156 rmap->obj[index] = obj;
159 EXPORT_SYMBOL(cpu_rmap_add);
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
167 int cpu_rmap_update(struct cpu_rmap *rmap, u16 index,
168 const struct cpumask *affinity)
170 cpumask_var_t update_mask;
173 if (unlikely(!zalloc_cpumask_var(&update_mask, GFP_KERNEL)))
176 /* Invalidate distance for all CPUs for which this used to be
177 * the nearest object. Mark those CPUs for update.
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);
186 debug_print_rmap(rmap, "after invalidating old distances");
188 /* Set distance to 0 for all CPUs in the new affinity mask.
189 * Mark all CPUs within their NUMA nodes for update.
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)));
198 debug_print_rmap(rmap, "after updating neighbours");
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))
205 if (cpu_rmap_copy_neigh(rmap, cpu,
206 topology_core_cpumask(cpu), 2))
208 if (cpu_rmap_copy_neigh(rmap, cpu,
209 cpumask_of_node(cpu_to_node(cpu)), 3))
211 /* We could continue into NUMA node distances, but for now
216 debug_print_rmap(rmap, "after copying neighbours");
218 free_cpumask_var(update_mask);
221 EXPORT_SYMBOL(cpu_rmap_update);
223 /* Glue between IRQ affinity notifiers and CPU rmaps */
226 struct irq_affinity_notify notify;
227 struct cpu_rmap *rmap;
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
235 * Must be called in process context, before freeing the IRQs.
237 void free_irq_cpu_rmap(struct cpu_rmap *rmap)
239 struct irq_glue *glue;
245 for (index = 0; index < rmap->size; index++) {
246 glue = rmap->obj[index];
248 irq_set_affinity_notifier(glue->notify.irq, NULL);
253 EXPORT_SYMBOL(free_irq_cpu_rmap);
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
260 * This is executed in workqueue context.
263 irq_cpu_rmap_notify(struct irq_affinity_notify *notify, const cpumask_t *mask)
265 struct irq_glue *glue =
266 container_of(notify, struct irq_glue, notify);
269 rc = cpu_rmap_update(glue->rmap, glue->index, mask);
271 pr_warn("irq_cpu_rmap_notify: update failed: %d\n", rc);
275 * irq_cpu_rmap_release - reclaiming callback for IRQ subsystem
276 * @ref: kref to struct irq_affinity_notify passed by irq/manage.c
278 static void irq_cpu_rmap_release(struct kref *ref)
280 struct irq_glue *glue =
281 container_of(ref, struct irq_glue, notify.kref);
283 cpu_rmap_put(glue->rmap);
284 glue->rmap->obj[glue->index] = NULL;
289 * irq_cpu_rmap_remove - remove an IRQ from a CPU affinity reverse-map
290 * @rmap: The reverse-map
291 * @irq: The IRQ number
293 int irq_cpu_rmap_remove(struct cpu_rmap *rmap, int irq)
295 return irq_set_affinity_notifier(irq, NULL);
297 EXPORT_SYMBOL(irq_cpu_rmap_remove);
300 * irq_cpu_rmap_add - add an IRQ to a CPU affinity reverse-map
301 * @rmap: The reverse-map
302 * @irq: The IRQ number
304 * This adds an IRQ affinity notifier that will update the reverse-map
307 * Must be called in process context, after the IRQ is allocated but
308 * before it is bound with request_irq().
310 int irq_cpu_rmap_add(struct cpu_rmap *rmap, int irq)
312 struct irq_glue *glue = kzalloc(sizeof(*glue), GFP_KERNEL);
317 glue->notify.notify = irq_cpu_rmap_notify;
318 glue->notify.release = irq_cpu_rmap_release;
321 rc = cpu_rmap_add(rmap, glue);
326 rc = irq_set_affinity_notifier(irq, &glue->notify);
333 rmap->obj[glue->index] = NULL;
335 cpu_rmap_put(glue->rmap);
339 EXPORT_SYMBOL(irq_cpu_rmap_add);