affinity.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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/sort.h>
  11. static void irq_spread_init_one(struct cpumask *irqmsk, struct cpumask *nmsk,
  12. unsigned int cpus_per_vec)
  13. {
  14. const struct cpumask *siblmsk;
  15. int cpu, sibl;
  16. for ( ; cpus_per_vec > 0; ) {
  17. cpu = cpumask_first(nmsk);
  18. /* Should not happen, but I'm too lazy to think about it */
  19. if (cpu >= nr_cpu_ids)
  20. return;
  21. cpumask_clear_cpu(cpu, nmsk);
  22. cpumask_set_cpu(cpu, irqmsk);
  23. cpus_per_vec--;
  24. /* If the cpu has siblings, use them first */
  25. siblmsk = topology_sibling_cpumask(cpu);
  26. for (sibl = -1; cpus_per_vec > 0; ) {
  27. sibl = cpumask_next(sibl, siblmsk);
  28. if (sibl >= nr_cpu_ids)
  29. break;
  30. if (!cpumask_test_and_clear_cpu(sibl, nmsk))
  31. continue;
  32. cpumask_set_cpu(sibl, irqmsk);
  33. cpus_per_vec--;
  34. }
  35. }
  36. }
  37. static cpumask_var_t *alloc_node_to_cpumask(void)
  38. {
  39. cpumask_var_t *masks;
  40. int node;
  41. masks = kcalloc(nr_node_ids, sizeof(cpumask_var_t), GFP_KERNEL);
  42. if (!masks)
  43. return NULL;
  44. for (node = 0; node < nr_node_ids; node++) {
  45. if (!zalloc_cpumask_var(&masks[node], GFP_KERNEL))
  46. goto out_unwind;
  47. }
  48. return masks;
  49. out_unwind:
  50. while (--node >= 0)
  51. free_cpumask_var(masks[node]);
  52. kfree(masks);
  53. return NULL;
  54. }
  55. static void free_node_to_cpumask(cpumask_var_t *masks)
  56. {
  57. int node;
  58. for (node = 0; node < nr_node_ids; node++)
  59. free_cpumask_var(masks[node]);
  60. kfree(masks);
  61. }
  62. static void build_node_to_cpumask(cpumask_var_t *masks)
  63. {
  64. int cpu;
  65. for_each_possible_cpu(cpu)
  66. cpumask_set_cpu(cpu, masks[cpu_to_node(cpu)]);
  67. }
  68. static int get_nodes_in_cpumask(cpumask_var_t *node_to_cpumask,
  69. const struct cpumask *mask, nodemask_t *nodemsk)
  70. {
  71. int n, nodes = 0;
  72. /* Calculate the number of nodes in the supplied affinity mask */
  73. for_each_node(n) {
  74. if (cpumask_intersects(mask, node_to_cpumask[n])) {
  75. node_set(n, *nodemsk);
  76. nodes++;
  77. }
  78. }
  79. return nodes;
  80. }
  81. struct node_vectors {
  82. unsigned id;
  83. union {
  84. unsigned nvectors;
  85. unsigned ncpus;
  86. };
  87. };
  88. static int ncpus_cmp_func(const void *l, const void *r)
  89. {
  90. const struct node_vectors *ln = l;
  91. const struct node_vectors *rn = r;
  92. return ln->ncpus - rn->ncpus;
  93. }
  94. /*
  95. * Allocate vector number for each node, so that for each node:
  96. *
  97. * 1) the allocated number is >= 1
  98. *
  99. * 2) the allocated numbver is <= active CPU number of this node
  100. *
  101. * The actual allocated total vectors may be less than @numvecs when
  102. * active total CPU number is less than @numvecs.
  103. *
  104. * Active CPUs means the CPUs in '@cpu_mask AND @node_to_cpumask[]'
  105. * for each node.
  106. */
  107. static void alloc_nodes_vectors(unsigned int numvecs,
  108. cpumask_var_t *node_to_cpumask,
  109. const struct cpumask *cpu_mask,
  110. const nodemask_t nodemsk,
  111. struct cpumask *nmsk,
  112. struct node_vectors *node_vectors)
  113. {
  114. unsigned n, remaining_ncpus = 0;
  115. for (n = 0; n < nr_node_ids; n++) {
  116. node_vectors[n].id = n;
  117. node_vectors[n].ncpus = UINT_MAX;
  118. }
  119. for_each_node_mask(n, nodemsk) {
  120. unsigned ncpus;
  121. cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]);
  122. ncpus = cpumask_weight(nmsk);
  123. if (!ncpus)
  124. continue;
  125. remaining_ncpus += ncpus;
  126. node_vectors[n].ncpus = ncpus;
  127. }
  128. numvecs = min_t(unsigned, remaining_ncpus, numvecs);
  129. sort(node_vectors, nr_node_ids, sizeof(node_vectors[0]),
  130. ncpus_cmp_func, NULL);
  131. /*
  132. * Allocate vectors for each node according to the ratio of this
  133. * node's nr_cpus to remaining un-assigned ncpus. 'numvecs' is
  134. * bigger than number of active numa nodes. Always start the
  135. * allocation from the node with minimized nr_cpus.
  136. *
  137. * This way guarantees that each active node gets allocated at
  138. * least one vector, and the theory is simple: over-allocation
  139. * is only done when this node is assigned by one vector, so
  140. * other nodes will be allocated >= 1 vector, since 'numvecs' is
  141. * bigger than number of numa nodes.
  142. *
  143. * One perfect invariant is that number of allocated vectors for
  144. * each node is <= CPU count of this node:
  145. *
  146. * 1) suppose there are two nodes: A and B
  147. * ncpu(X) is CPU count of node X
  148. * vecs(X) is the vector count allocated to node X via this
  149. * algorithm
  150. *
  151. * ncpu(A) <= ncpu(B)
  152. * ncpu(A) + ncpu(B) = N
  153. * vecs(A) + vecs(B) = V
  154. *
  155. * vecs(A) = max(1, round_down(V * ncpu(A) / N))
  156. * vecs(B) = V - vecs(A)
  157. *
  158. * both N and V are integer, and 2 <= V <= N, suppose
  159. * V = N - delta, and 0 <= delta <= N - 2
  160. *
  161. * 2) obviously vecs(A) <= ncpu(A) because:
  162. *
  163. * if vecs(A) is 1, then vecs(A) <= ncpu(A) given
  164. * ncpu(A) >= 1
  165. *
  166. * otherwise,
  167. * vecs(A) <= V * ncpu(A) / N <= ncpu(A), given V <= N
  168. *
  169. * 3) prove how vecs(B) <= ncpu(B):
  170. *
  171. * if round_down(V * ncpu(A) / N) == 0, vecs(B) won't be
  172. * over-allocated, so vecs(B) <= ncpu(B),
  173. *
  174. * otherwise:
  175. *
  176. * vecs(A) =
  177. * round_down(V * ncpu(A) / N) =
  178. * round_down((N - delta) * ncpu(A) / N) =
  179. * round_down((N * ncpu(A) - delta * ncpu(A)) / N) >=
  180. * round_down((N * ncpu(A) - delta * N) / N) =
  181. * cpu(A) - delta
  182. *
  183. * then:
  184. *
  185. * vecs(A) - V >= ncpu(A) - delta - V
  186. * =>
  187. * V - vecs(A) <= V + delta - ncpu(A)
  188. * =>
  189. * vecs(B) <= N - ncpu(A)
  190. * =>
  191. * vecs(B) <= cpu(B)
  192. *
  193. * For nodes >= 3, it can be thought as one node and another big
  194. * node given that is exactly what this algorithm is implemented,
  195. * and we always re-calculate 'remaining_ncpus' & 'numvecs', and
  196. * finally for each node X: vecs(X) <= ncpu(X).
  197. *
  198. */
  199. for (n = 0; n < nr_node_ids; n++) {
  200. unsigned nvectors, ncpus;
  201. if (node_vectors[n].ncpus == UINT_MAX)
  202. continue;
  203. WARN_ON_ONCE(numvecs == 0);
  204. ncpus = node_vectors[n].ncpus;
  205. nvectors = max_t(unsigned, 1,
  206. numvecs * ncpus / remaining_ncpus);
  207. WARN_ON_ONCE(nvectors > ncpus);
  208. node_vectors[n].nvectors = nvectors;
  209. remaining_ncpus -= ncpus;
  210. numvecs -= nvectors;
  211. }
  212. }
  213. static int __irq_build_affinity_masks(unsigned int startvec,
  214. unsigned int numvecs,
  215. unsigned int firstvec,
  216. cpumask_var_t *node_to_cpumask,
  217. const struct cpumask *cpu_mask,
  218. struct cpumask *nmsk,
  219. struct irq_affinity_desc *masks)
  220. {
  221. unsigned int i, n, nodes, cpus_per_vec, extra_vecs, done = 0;
  222. unsigned int last_affv = firstvec + numvecs;
  223. unsigned int curvec = startvec;
  224. nodemask_t nodemsk = NODE_MASK_NONE;
  225. struct node_vectors *node_vectors;
  226. if (!cpumask_weight(cpu_mask))
  227. return 0;
  228. nodes = get_nodes_in_cpumask(node_to_cpumask, cpu_mask, &nodemsk);
  229. /*
  230. * If the number of nodes in the mask is greater than or equal the
  231. * number of vectors we just spread the vectors across the nodes.
  232. */
  233. if (numvecs <= nodes) {
  234. for_each_node_mask(n, nodemsk) {
  235. /* Ensure that only CPUs which are in both masks are set */
  236. cpumask_and(nmsk, cpu_mask, node_to_cpumask[n]);
  237. cpumask_or(&masks[curvec].mask, &masks[curvec].mask, nmsk);
  238. if (++curvec == last_affv)
  239. curvec = firstvec;
  240. }
  241. return numvecs;
  242. }
  243. node_vectors = kcalloc(nr_node_ids,
  244. sizeof(struct node_vectors),
  245. GFP_KERNEL);
  246. if (!node_vectors)
  247. return -ENOMEM;
  248. /* allocate vector number for each node */
  249. alloc_nodes_vectors(numvecs, node_to_cpumask, cpu_mask,
  250. nodemsk, nmsk, node_vectors);
  251. for (i = 0; i < nr_node_ids; i++) {
  252. unsigned int ncpus, v;
  253. struct node_vectors *nv = &node_vectors[i];
  254. if (nv->nvectors == UINT_MAX)
  255. continue;
  256. /* Get the cpus on this node which are in the mask */
  257. cpumask_and(nmsk, cpu_mask, node_to_cpumask[nv->id]);
  258. ncpus = cpumask_weight(nmsk);
  259. if (!ncpus)
  260. continue;
  261. WARN_ON_ONCE(nv->nvectors > ncpus);
  262. /* Account for rounding errors */
  263. extra_vecs = ncpus - nv->nvectors * (ncpus / nv->nvectors);
  264. /* Spread allocated vectors on CPUs of the current node */
  265. for (v = 0; v < nv->nvectors; v++, curvec++) {
  266. cpus_per_vec = ncpus / nv->nvectors;
  267. /* Account for extra vectors to compensate rounding errors */
  268. if (extra_vecs) {
  269. cpus_per_vec++;
  270. --extra_vecs;
  271. }
  272. /*
  273. * wrapping has to be considered given 'startvec'
  274. * may start anywhere
  275. */
  276. if (curvec >= last_affv)
  277. curvec = firstvec;
  278. irq_spread_init_one(&masks[curvec].mask, nmsk,
  279. cpus_per_vec);
  280. }
  281. done += nv->nvectors;
  282. }
  283. kfree(node_vectors);
  284. return done;
  285. }
  286. /*
  287. * build affinity in two stages:
  288. * 1) spread present CPU on these vectors
  289. * 2) spread other possible CPUs on these vectors
  290. */
  291. static int irq_build_affinity_masks(unsigned int startvec, unsigned int numvecs,
  292. unsigned int firstvec,
  293. struct irq_affinity_desc *masks)
  294. {
  295. unsigned int curvec = startvec, nr_present = 0, nr_others = 0;
  296. cpumask_var_t *node_to_cpumask;
  297. cpumask_var_t nmsk, npresmsk;
  298. int ret = -ENOMEM;
  299. if (!zalloc_cpumask_var(&nmsk, GFP_KERNEL))
  300. return ret;
  301. if (!zalloc_cpumask_var(&npresmsk, GFP_KERNEL))
  302. goto fail_nmsk;
  303. node_to_cpumask = alloc_node_to_cpumask();
  304. if (!node_to_cpumask)
  305. goto fail_npresmsk;
  306. /* Stabilize the cpumasks */
  307. get_online_cpus();
  308. build_node_to_cpumask(node_to_cpumask);
  309. /* Spread on present CPUs starting from affd->pre_vectors */
  310. ret = __irq_build_affinity_masks(curvec, numvecs, firstvec,
  311. node_to_cpumask, cpu_present_mask,
  312. nmsk, masks);
  313. if (ret < 0)
  314. goto fail_build_affinity;
  315. nr_present = ret;
  316. /*
  317. * Spread on non present CPUs starting from the next vector to be
  318. * handled. If the spreading of present CPUs already exhausted the
  319. * vector space, assign the non present CPUs to the already spread
  320. * out vectors.
  321. */
  322. if (nr_present >= numvecs)
  323. curvec = firstvec;
  324. else
  325. curvec = firstvec + nr_present;
  326. cpumask_andnot(npresmsk, cpu_possible_mask, cpu_present_mask);
  327. ret = __irq_build_affinity_masks(curvec, numvecs, firstvec,
  328. node_to_cpumask, npresmsk, nmsk,
  329. masks);
  330. if (ret >= 0)
  331. nr_others = ret;
  332. fail_build_affinity:
  333. put_online_cpus();
  334. if (ret >= 0)
  335. WARN_ON(nr_present + nr_others < numvecs);
  336. free_node_to_cpumask(node_to_cpumask);
  337. fail_npresmsk:
  338. free_cpumask_var(npresmsk);
  339. fail_nmsk:
  340. free_cpumask_var(nmsk);
  341. return ret < 0 ? ret : 0;
  342. }
  343. static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs)
  344. {
  345. affd->nr_sets = 1;
  346. affd->set_size[0] = affvecs;
  347. }
  348. /**
  349. * irq_create_affinity_masks - Create affinity masks for multiqueue spreading
  350. * @nvecs: The total number of vectors
  351. * @affd: Description of the affinity requirements
  352. *
  353. * Returns the irq_affinity_desc pointer or NULL if allocation failed.
  354. */
  355. struct irq_affinity_desc *
  356. irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
  357. {
  358. unsigned int affvecs, curvec, usedvecs, i;
  359. struct irq_affinity_desc *masks = NULL;
  360. /*
  361. * Determine the number of vectors which need interrupt affinities
  362. * assigned. If the pre/post request exhausts the available vectors
  363. * then nothing to do here except for invoking the calc_sets()
  364. * callback so the device driver can adjust to the situation.
  365. */
  366. if (nvecs > affd->pre_vectors + affd->post_vectors)
  367. affvecs = nvecs - affd->pre_vectors - affd->post_vectors;
  368. else
  369. affvecs = 0;
  370. /*
  371. * Simple invocations do not provide a calc_sets() callback. Install
  372. * the generic one.
  373. */
  374. if (!affd->calc_sets)
  375. affd->calc_sets = default_calc_sets;
  376. /* Recalculate the sets */
  377. affd->calc_sets(affd, affvecs);
  378. if (WARN_ON_ONCE(affd->nr_sets > IRQ_AFFINITY_MAX_SETS))
  379. return NULL;
  380. /* Nothing to assign? */
  381. if (!affvecs)
  382. return NULL;
  383. masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
  384. if (!masks)
  385. return NULL;
  386. /* Fill out vectors at the beginning that don't need affinity */
  387. for (curvec = 0; curvec < affd->pre_vectors; curvec++)
  388. cpumask_copy(&masks[curvec].mask, irq_default_affinity);
  389. /*
  390. * Spread on present CPUs starting from affd->pre_vectors. If we
  391. * have multiple sets, build each sets affinity mask separately.
  392. */
  393. for (i = 0, usedvecs = 0; i < affd->nr_sets; i++) {
  394. unsigned int this_vecs = affd->set_size[i];
  395. int ret;
  396. ret = irq_build_affinity_masks(curvec, this_vecs,
  397. curvec, masks);
  398. if (ret) {
  399. kfree(masks);
  400. return NULL;
  401. }
  402. curvec += this_vecs;
  403. usedvecs += this_vecs;
  404. }
  405. /* Fill out vectors at the end that don't need affinity */
  406. if (usedvecs >= affvecs)
  407. curvec = affd->pre_vectors + affvecs;
  408. else
  409. curvec = affd->pre_vectors + usedvecs;
  410. for (; curvec < nvecs; curvec++)
  411. cpumask_copy(&masks[curvec].mask, irq_default_affinity);
  412. /* Mark the managed interrupts */
  413. for (i = affd->pre_vectors; i < nvecs - affd->post_vectors; i++)
  414. masks[i].is_managed = 1;
  415. return masks;
  416. }
  417. /**
  418. * irq_calc_affinity_vectors - Calculate the optimal number of vectors
  419. * @minvec: The minimum number of vectors available
  420. * @maxvec: The maximum number of vectors available
  421. * @affd: Description of the affinity requirements
  422. */
  423. unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,
  424. const struct irq_affinity *affd)
  425. {
  426. unsigned int resv = affd->pre_vectors + affd->post_vectors;
  427. unsigned int set_vecs;
  428. if (resv > minvec)
  429. return 0;
  430. if (affd->calc_sets) {
  431. set_vecs = maxvec - resv;
  432. } else {
  433. get_online_cpus();
  434. set_vecs = cpumask_weight(cpu_possible_mask);
  435. put_online_cpus();
  436. }
  437. return resv + min(set_vecs, maxvec - resv);
  438. }