cpu_rmap.c 7.6 KB

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