cpu_rmap.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. 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. static int get_free_index(struct cpu_rmap *rmap)
  114. {
  115. int i;
  116. for (i = 0; i < rmap->size; i++)
  117. if (!rmap->obj[i])
  118. return i;
  119. return -ENOSPC;
  120. }
  121. /**
  122. * cpu_rmap_add - add object to a rmap
  123. * @rmap: CPU rmap allocated with alloc_cpu_rmap()
  124. * @obj: Object to add to rmap
  125. *
  126. * Return index of object or -ENOSPC if no free entry was found
  127. */
  128. int cpu_rmap_add(struct cpu_rmap *rmap, void *obj)
  129. {
  130. int index = get_free_index(rmap);
  131. if (index < 0)
  132. return index;
  133. rmap->obj[index] = obj;
  134. return index;
  135. }
  136. EXPORT_SYMBOL(cpu_rmap_add);
  137. /**
  138. * cpu_rmap_update - update CPU rmap following a change of object affinity
  139. * @rmap: CPU rmap to update
  140. * @index: Index of object whose affinity changed
  141. * @affinity: New CPU affinity of object
  142. */
  143. int cpu_rmap_update(struct cpu_rmap *rmap, u16 index,
  144. const struct cpumask *affinity)
  145. {
  146. cpumask_var_t update_mask;
  147. unsigned int cpu;
  148. if (unlikely(!zalloc_cpumask_var(&update_mask, GFP_KERNEL)))
  149. return -ENOMEM;
  150. /* Invalidate distance for all CPUs for which this used to be
  151. * the nearest object. Mark those CPUs for update.
  152. */
  153. for_each_online_cpu(cpu) {
  154. if (rmap->near[cpu].index == index) {
  155. rmap->near[cpu].dist = CPU_RMAP_DIST_INF;
  156. cpumask_set_cpu(cpu, update_mask);
  157. }
  158. }
  159. debug_print_rmap(rmap, "after invalidating old distances");
  160. /* Set distance to 0 for all CPUs in the new affinity mask.
  161. * Mark all CPUs within their NUMA nodes for update.
  162. */
  163. for_each_cpu(cpu, affinity) {
  164. rmap->near[cpu].index = index;
  165. rmap->near[cpu].dist = 0;
  166. cpumask_or(update_mask, update_mask,
  167. cpumask_of_node(cpu_to_node(cpu)));
  168. }
  169. debug_print_rmap(rmap, "after updating neighbours");
  170. /* Update distances based on topology */
  171. for_each_cpu(cpu, update_mask) {
  172. if (cpu_rmap_copy_neigh(rmap, cpu,
  173. topology_sibling_cpumask(cpu), 1))
  174. continue;
  175. if (cpu_rmap_copy_neigh(rmap, cpu,
  176. topology_core_cpumask(cpu), 2))
  177. continue;
  178. if (cpu_rmap_copy_neigh(rmap, cpu,
  179. cpumask_of_node(cpu_to_node(cpu)), 3))
  180. continue;
  181. /* We could continue into NUMA node distances, but for now
  182. * we give up.
  183. */
  184. }
  185. debug_print_rmap(rmap, "after copying neighbours");
  186. free_cpumask_var(update_mask);
  187. return 0;
  188. }
  189. EXPORT_SYMBOL(cpu_rmap_update);
  190. /* Glue between IRQ affinity notifiers and CPU rmaps */
  191. struct irq_glue {
  192. struct irq_affinity_notify notify;
  193. struct cpu_rmap *rmap;
  194. u16 index;
  195. };
  196. /**
  197. * free_irq_cpu_rmap - free a CPU affinity reverse-map used for IRQs
  198. * @rmap: Reverse-map allocated with alloc_irq_cpu_map(), or %NULL
  199. *
  200. * Must be called in process context, before freeing the IRQs.
  201. */
  202. void free_irq_cpu_rmap(struct cpu_rmap *rmap)
  203. {
  204. struct irq_glue *glue;
  205. u16 index;
  206. if (!rmap)
  207. return;
  208. for (index = 0; index < rmap->size; index++) {
  209. glue = rmap->obj[index];
  210. if (glue)
  211. irq_set_affinity_notifier(glue->notify.irq, NULL);
  212. }
  213. cpu_rmap_put(rmap);
  214. }
  215. EXPORT_SYMBOL(free_irq_cpu_rmap);
  216. /**
  217. * irq_cpu_rmap_notify - callback for IRQ subsystem when IRQ affinity updated
  218. * @notify: struct irq_affinity_notify passed by irq/manage.c
  219. * @mask: cpu mask for new SMP affinity
  220. *
  221. * This is executed in workqueue context.
  222. */
  223. static void
  224. irq_cpu_rmap_notify(struct irq_affinity_notify *notify, const cpumask_t *mask)
  225. {
  226. struct irq_glue *glue =
  227. container_of(notify, struct irq_glue, notify);
  228. int rc;
  229. rc = cpu_rmap_update(glue->rmap, glue->index, mask);
  230. if (rc)
  231. pr_warn("irq_cpu_rmap_notify: update failed: %d\n", rc);
  232. }
  233. /**
  234. * irq_cpu_rmap_release - reclaiming callback for IRQ subsystem
  235. * @ref: kref to struct irq_affinity_notify passed by irq/manage.c
  236. */
  237. static void irq_cpu_rmap_release(struct kref *ref)
  238. {
  239. struct irq_glue *glue =
  240. container_of(ref, struct irq_glue, notify.kref);
  241. glue->rmap->obj[glue->index] = NULL;
  242. cpu_rmap_put(glue->rmap);
  243. kfree(glue);
  244. }
  245. /**
  246. * irq_cpu_rmap_remove - remove an IRQ from a CPU affinity reverse-map
  247. * @rmap: The reverse-map
  248. * @irq: The IRQ number
  249. */
  250. int irq_cpu_rmap_remove(struct cpu_rmap *rmap, int irq)
  251. {
  252. return irq_set_affinity_notifier(irq, NULL);
  253. }
  254. EXPORT_SYMBOL(irq_cpu_rmap_remove);
  255. /**
  256. * irq_cpu_rmap_add - add an IRQ to a CPU affinity reverse-map
  257. * @rmap: The reverse-map
  258. * @irq: The IRQ number
  259. *
  260. * This adds an IRQ affinity notifier that will update the reverse-map
  261. * automatically.
  262. *
  263. * Must be called in process context, after the IRQ is allocated but
  264. * before it is bound with request_irq().
  265. */
  266. int irq_cpu_rmap_add(struct cpu_rmap *rmap, int irq)
  267. {
  268. struct irq_glue *glue = kzalloc_obj(*glue);
  269. int rc;
  270. if (!glue)
  271. return -ENOMEM;
  272. glue->notify.notify = irq_cpu_rmap_notify;
  273. glue->notify.release = irq_cpu_rmap_release;
  274. glue->rmap = rmap;
  275. cpu_rmap_get(rmap);
  276. rc = cpu_rmap_add(rmap, glue);
  277. if (rc < 0)
  278. goto err_add;
  279. glue->index = rc;
  280. rc = irq_set_affinity_notifier(irq, &glue->notify);
  281. if (rc)
  282. goto err_set;
  283. return rc;
  284. err_set:
  285. rmap->obj[glue->index] = NULL;
  286. err_add:
  287. cpu_rmap_put(glue->rmap);
  288. kfree(glue);
  289. return rc;
  290. }
  291. EXPORT_SYMBOL(irq_cpu_rmap_add);