affinity.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
  2. /*
  3. * Copyright(c) 2015 - 2020 Intel Corporation.
  4. */
  5. #include <linux/topology.h>
  6. #include <linux/cpumask.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/numa.h>
  9. #include "hfi.h"
  10. #include "affinity.h"
  11. #include "sdma.h"
  12. #include "trace.h"
  13. struct hfi1_affinity_node_list node_affinity = {
  14. .list = LIST_HEAD_INIT(node_affinity.list),
  15. .lock = __MUTEX_INITIALIZER(node_affinity.lock)
  16. };
  17. /* Name of IRQ types, indexed by enum irq_type */
  18. static const char * const irq_type_names[] = {
  19. "SDMA",
  20. "RCVCTXT",
  21. "NETDEVCTXT",
  22. "GENERAL",
  23. "OTHER",
  24. };
  25. /* Per NUMA node count of HFI devices */
  26. static unsigned int *hfi1_per_node_cntr;
  27. static inline void init_cpu_mask_set(struct cpu_mask_set *set)
  28. {
  29. cpumask_clear(&set->mask);
  30. cpumask_clear(&set->used);
  31. set->gen = 0;
  32. }
  33. /* Increment generation of CPU set if needed */
  34. static void _cpu_mask_set_gen_inc(struct cpu_mask_set *set)
  35. {
  36. if (cpumask_equal(&set->mask, &set->used)) {
  37. /*
  38. * We've used up all the CPUs, bump up the generation
  39. * and reset the 'used' map
  40. */
  41. set->gen++;
  42. cpumask_clear(&set->used);
  43. }
  44. }
  45. static void _cpu_mask_set_gen_dec(struct cpu_mask_set *set)
  46. {
  47. if (cpumask_empty(&set->used) && set->gen) {
  48. set->gen--;
  49. cpumask_copy(&set->used, &set->mask);
  50. }
  51. }
  52. /* Get the first CPU from the list of unused CPUs in a CPU set data structure */
  53. static int cpu_mask_set_get_first(struct cpu_mask_set *set, cpumask_var_t diff)
  54. {
  55. int cpu;
  56. if (!diff || !set)
  57. return -EINVAL;
  58. _cpu_mask_set_gen_inc(set);
  59. /* Find out CPUs left in CPU mask */
  60. cpumask_andnot(diff, &set->mask, &set->used);
  61. cpu = cpumask_first(diff);
  62. if (cpu >= nr_cpu_ids) /* empty */
  63. cpu = -EINVAL;
  64. else
  65. cpumask_set_cpu(cpu, &set->used);
  66. return cpu;
  67. }
  68. static void cpu_mask_set_put(struct cpu_mask_set *set, int cpu)
  69. {
  70. if (!set)
  71. return;
  72. cpumask_clear_cpu(cpu, &set->used);
  73. _cpu_mask_set_gen_dec(set);
  74. }
  75. /* Initialize non-HT cpu cores mask */
  76. void init_real_cpu_mask(void)
  77. {
  78. int possible, curr_cpu, i, ht;
  79. cpumask_clear(&node_affinity.real_cpu_mask);
  80. /* Start with cpu online mask as the real cpu mask */
  81. cpumask_copy(&node_affinity.real_cpu_mask, cpu_online_mask);
  82. /*
  83. * Remove HT cores from the real cpu mask. Do this in two steps below.
  84. */
  85. possible = cpumask_weight(&node_affinity.real_cpu_mask);
  86. ht = cpumask_weight(topology_sibling_cpumask(
  87. cpumask_first(&node_affinity.real_cpu_mask)));
  88. /*
  89. * Step 1. Skip over the first N HT siblings and use them as the
  90. * "real" cores. Assumes that HT cores are not enumerated in
  91. * succession (except in the single core case).
  92. */
  93. curr_cpu = cpumask_first(&node_affinity.real_cpu_mask);
  94. for (i = 0; i < possible / ht; i++)
  95. curr_cpu = cpumask_next(curr_cpu, &node_affinity.real_cpu_mask);
  96. /*
  97. * Step 2. Remove the remaining HT siblings. Use cpumask_next() to
  98. * skip any gaps.
  99. */
  100. for (; i < possible; i++) {
  101. cpumask_clear_cpu(curr_cpu, &node_affinity.real_cpu_mask);
  102. curr_cpu = cpumask_next(curr_cpu, &node_affinity.real_cpu_mask);
  103. }
  104. }
  105. int node_affinity_init(void)
  106. {
  107. int node;
  108. struct pci_dev *dev = NULL;
  109. const struct pci_device_id *ids = hfi1_pci_tbl;
  110. cpumask_clear(&node_affinity.proc.used);
  111. cpumask_copy(&node_affinity.proc.mask, cpu_online_mask);
  112. node_affinity.proc.gen = 0;
  113. node_affinity.num_core_siblings =
  114. cpumask_weight(topology_sibling_cpumask(
  115. cpumask_first(&node_affinity.proc.mask)
  116. ));
  117. node_affinity.num_possible_nodes = num_possible_nodes();
  118. node_affinity.num_online_nodes = num_online_nodes();
  119. node_affinity.num_online_cpus = num_online_cpus();
  120. /*
  121. * The real cpu mask is part of the affinity struct but it has to be
  122. * initialized early. It is needed to calculate the number of user
  123. * contexts in set_up_context_variables().
  124. */
  125. init_real_cpu_mask();
  126. hfi1_per_node_cntr = kcalloc(node_affinity.num_possible_nodes,
  127. sizeof(*hfi1_per_node_cntr), GFP_KERNEL);
  128. if (!hfi1_per_node_cntr)
  129. return -ENOMEM;
  130. while (ids->vendor) {
  131. dev = NULL;
  132. while ((dev = pci_get_device(ids->vendor, ids->device, dev))) {
  133. node = pcibus_to_node(dev->bus);
  134. if (node < 0)
  135. goto out;
  136. hfi1_per_node_cntr[node]++;
  137. }
  138. ids++;
  139. }
  140. return 0;
  141. out:
  142. /*
  143. * Invalid PCI NUMA node information found, note it, and populate
  144. * our database 1:1.
  145. */
  146. pr_err("HFI: Invalid PCI NUMA node. Performance may be affected\n");
  147. pr_err("HFI: System BIOS may need to be upgraded\n");
  148. for (node = 0; node < node_affinity.num_possible_nodes; node++)
  149. hfi1_per_node_cntr[node] = 1;
  150. pci_dev_put(dev);
  151. return 0;
  152. }
  153. static void node_affinity_destroy(struct hfi1_affinity_node *entry)
  154. {
  155. free_percpu(entry->comp_vect_affinity);
  156. kfree(entry);
  157. }
  158. void node_affinity_destroy_all(void)
  159. {
  160. struct list_head *pos, *q;
  161. struct hfi1_affinity_node *entry;
  162. mutex_lock(&node_affinity.lock);
  163. list_for_each_safe(pos, q, &node_affinity.list) {
  164. entry = list_entry(pos, struct hfi1_affinity_node,
  165. list);
  166. list_del(pos);
  167. node_affinity_destroy(entry);
  168. }
  169. mutex_unlock(&node_affinity.lock);
  170. kfree(hfi1_per_node_cntr);
  171. }
  172. static struct hfi1_affinity_node *node_affinity_allocate(int node)
  173. {
  174. struct hfi1_affinity_node *entry;
  175. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  176. if (!entry)
  177. return NULL;
  178. entry->node = node;
  179. entry->comp_vect_affinity = alloc_percpu(u16);
  180. INIT_LIST_HEAD(&entry->list);
  181. return entry;
  182. }
  183. /*
  184. * It appends an entry to the list.
  185. * It *must* be called with node_affinity.lock held.
  186. */
  187. static void node_affinity_add_tail(struct hfi1_affinity_node *entry)
  188. {
  189. list_add_tail(&entry->list, &node_affinity.list);
  190. }
  191. /* It must be called with node_affinity.lock held */
  192. static struct hfi1_affinity_node *node_affinity_lookup(int node)
  193. {
  194. struct list_head *pos;
  195. struct hfi1_affinity_node *entry;
  196. list_for_each(pos, &node_affinity.list) {
  197. entry = list_entry(pos, struct hfi1_affinity_node, list);
  198. if (entry->node == node)
  199. return entry;
  200. }
  201. return NULL;
  202. }
  203. static int per_cpu_affinity_get(cpumask_var_t possible_cpumask,
  204. u16 __percpu *comp_vect_affinity)
  205. {
  206. int curr_cpu;
  207. u16 cntr;
  208. u16 prev_cntr;
  209. int ret_cpu;
  210. if (!possible_cpumask) {
  211. ret_cpu = -EINVAL;
  212. goto fail;
  213. }
  214. if (!comp_vect_affinity) {
  215. ret_cpu = -EINVAL;
  216. goto fail;
  217. }
  218. ret_cpu = cpumask_first(possible_cpumask);
  219. if (ret_cpu >= nr_cpu_ids) {
  220. ret_cpu = -EINVAL;
  221. goto fail;
  222. }
  223. prev_cntr = *per_cpu_ptr(comp_vect_affinity, ret_cpu);
  224. for_each_cpu(curr_cpu, possible_cpumask) {
  225. cntr = *per_cpu_ptr(comp_vect_affinity, curr_cpu);
  226. if (cntr < prev_cntr) {
  227. ret_cpu = curr_cpu;
  228. prev_cntr = cntr;
  229. }
  230. }
  231. *per_cpu_ptr(comp_vect_affinity, ret_cpu) += 1;
  232. fail:
  233. return ret_cpu;
  234. }
  235. static int per_cpu_affinity_put_max(cpumask_var_t possible_cpumask,
  236. u16 __percpu *comp_vect_affinity)
  237. {
  238. int curr_cpu;
  239. int max_cpu;
  240. u16 cntr;
  241. u16 prev_cntr;
  242. if (!possible_cpumask)
  243. return -EINVAL;
  244. if (!comp_vect_affinity)
  245. return -EINVAL;
  246. max_cpu = cpumask_first(possible_cpumask);
  247. if (max_cpu >= nr_cpu_ids)
  248. return -EINVAL;
  249. prev_cntr = *per_cpu_ptr(comp_vect_affinity, max_cpu);
  250. for_each_cpu(curr_cpu, possible_cpumask) {
  251. cntr = *per_cpu_ptr(comp_vect_affinity, curr_cpu);
  252. if (cntr > prev_cntr) {
  253. max_cpu = curr_cpu;
  254. prev_cntr = cntr;
  255. }
  256. }
  257. *per_cpu_ptr(comp_vect_affinity, max_cpu) -= 1;
  258. return max_cpu;
  259. }
  260. /*
  261. * Non-interrupt CPUs are used first, then interrupt CPUs.
  262. * Two already allocated cpu masks must be passed.
  263. */
  264. static int _dev_comp_vect_cpu_get(struct hfi1_devdata *dd,
  265. struct hfi1_affinity_node *entry,
  266. cpumask_var_t non_intr_cpus,
  267. cpumask_var_t available_cpus)
  268. __must_hold(&node_affinity.lock)
  269. {
  270. int cpu;
  271. struct cpu_mask_set *set = dd->comp_vect;
  272. lockdep_assert_held(&node_affinity.lock);
  273. if (!non_intr_cpus) {
  274. cpu = -1;
  275. goto fail;
  276. }
  277. if (!available_cpus) {
  278. cpu = -1;
  279. goto fail;
  280. }
  281. /* Available CPUs for pinning completion vectors */
  282. _cpu_mask_set_gen_inc(set);
  283. cpumask_andnot(available_cpus, &set->mask, &set->used);
  284. /* Available CPUs without SDMA engine interrupts */
  285. cpumask_andnot(non_intr_cpus, available_cpus,
  286. &entry->def_intr.used);
  287. /* If there are non-interrupt CPUs available, use them first */
  288. if (!cpumask_empty(non_intr_cpus))
  289. cpu = cpumask_first(non_intr_cpus);
  290. else /* Otherwise, use interrupt CPUs */
  291. cpu = cpumask_first(available_cpus);
  292. if (cpu >= nr_cpu_ids) { /* empty */
  293. cpu = -1;
  294. goto fail;
  295. }
  296. cpumask_set_cpu(cpu, &set->used);
  297. fail:
  298. return cpu;
  299. }
  300. static void _dev_comp_vect_cpu_put(struct hfi1_devdata *dd, int cpu)
  301. {
  302. struct cpu_mask_set *set = dd->comp_vect;
  303. if (cpu < 0)
  304. return;
  305. cpu_mask_set_put(set, cpu);
  306. }
  307. /* _dev_comp_vect_mappings_destroy() is reentrant */
  308. static void _dev_comp_vect_mappings_destroy(struct hfi1_devdata *dd)
  309. {
  310. int i, cpu;
  311. if (!dd->comp_vect_mappings)
  312. return;
  313. for (i = 0; i < dd->comp_vect_possible_cpus; i++) {
  314. cpu = dd->comp_vect_mappings[i];
  315. _dev_comp_vect_cpu_put(dd, cpu);
  316. dd->comp_vect_mappings[i] = -1;
  317. hfi1_cdbg(AFFINITY,
  318. "[%s] Release CPU %d from completion vector %d",
  319. rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), cpu, i);
  320. }
  321. kfree(dd->comp_vect_mappings);
  322. dd->comp_vect_mappings = NULL;
  323. }
  324. /*
  325. * This function creates the table for looking up CPUs for completion vectors.
  326. * num_comp_vectors needs to have been initilized before calling this function.
  327. */
  328. static int _dev_comp_vect_mappings_create(struct hfi1_devdata *dd,
  329. struct hfi1_affinity_node *entry)
  330. __must_hold(&node_affinity.lock)
  331. {
  332. int i, cpu, ret;
  333. cpumask_var_t non_intr_cpus;
  334. cpumask_var_t available_cpus;
  335. lockdep_assert_held(&node_affinity.lock);
  336. if (!zalloc_cpumask_var(&non_intr_cpus, GFP_KERNEL))
  337. return -ENOMEM;
  338. if (!zalloc_cpumask_var(&available_cpus, GFP_KERNEL)) {
  339. free_cpumask_var(non_intr_cpus);
  340. return -ENOMEM;
  341. }
  342. dd->comp_vect_mappings = kcalloc(dd->comp_vect_possible_cpus,
  343. sizeof(*dd->comp_vect_mappings),
  344. GFP_KERNEL);
  345. if (!dd->comp_vect_mappings) {
  346. ret = -ENOMEM;
  347. goto fail;
  348. }
  349. for (i = 0; i < dd->comp_vect_possible_cpus; i++)
  350. dd->comp_vect_mappings[i] = -1;
  351. for (i = 0; i < dd->comp_vect_possible_cpus; i++) {
  352. cpu = _dev_comp_vect_cpu_get(dd, entry, non_intr_cpus,
  353. available_cpus);
  354. if (cpu < 0) {
  355. ret = -EINVAL;
  356. goto fail;
  357. }
  358. dd->comp_vect_mappings[i] = cpu;
  359. hfi1_cdbg(AFFINITY,
  360. "[%s] Completion Vector %d -> CPU %d",
  361. rvt_get_ibdev_name(&(dd)->verbs_dev.rdi), i, cpu);
  362. }
  363. free_cpumask_var(available_cpus);
  364. free_cpumask_var(non_intr_cpus);
  365. return 0;
  366. fail:
  367. free_cpumask_var(available_cpus);
  368. free_cpumask_var(non_intr_cpus);
  369. _dev_comp_vect_mappings_destroy(dd);
  370. return ret;
  371. }
  372. int hfi1_comp_vectors_set_up(struct hfi1_devdata *dd)
  373. {
  374. int ret;
  375. struct hfi1_affinity_node *entry;
  376. mutex_lock(&node_affinity.lock);
  377. entry = node_affinity_lookup(dd->node);
  378. if (!entry) {
  379. ret = -EINVAL;
  380. goto unlock;
  381. }
  382. ret = _dev_comp_vect_mappings_create(dd, entry);
  383. unlock:
  384. mutex_unlock(&node_affinity.lock);
  385. return ret;
  386. }
  387. void hfi1_comp_vectors_clean_up(struct hfi1_devdata *dd)
  388. {
  389. _dev_comp_vect_mappings_destroy(dd);
  390. }
  391. int hfi1_comp_vect_mappings_lookup(struct rvt_dev_info *rdi, int comp_vect)
  392. {
  393. struct hfi1_ibdev *verbs_dev = dev_from_rdi(rdi);
  394. struct hfi1_devdata *dd = dd_from_dev(verbs_dev);
  395. if (!dd->comp_vect_mappings)
  396. return -EINVAL;
  397. if (comp_vect >= dd->comp_vect_possible_cpus)
  398. return -EINVAL;
  399. return dd->comp_vect_mappings[comp_vect];
  400. }
  401. /*
  402. * It assumes dd->comp_vect_possible_cpus is available.
  403. */
  404. static int _dev_comp_vect_cpu_mask_init(struct hfi1_devdata *dd,
  405. struct hfi1_affinity_node *entry,
  406. bool first_dev_init)
  407. __must_hold(&node_affinity.lock)
  408. {
  409. int i, j, curr_cpu;
  410. int possible_cpus_comp_vect = 0;
  411. struct cpumask *dev_comp_vect_mask = &dd->comp_vect->mask;
  412. lockdep_assert_held(&node_affinity.lock);
  413. /*
  414. * If there's only one CPU available for completion vectors, then
  415. * there will only be one completion vector available. Othewise,
  416. * the number of completion vector available will be the number of
  417. * available CPUs divide it by the number of devices in the
  418. * local NUMA node.
  419. */
  420. if (cpumask_weight(&entry->comp_vect_mask) == 1) {
  421. possible_cpus_comp_vect = 1;
  422. dd_dev_warn(dd,
  423. "Number of kernel receive queues is too large for completion vector affinity to be effective\n");
  424. } else {
  425. possible_cpus_comp_vect +=
  426. cpumask_weight(&entry->comp_vect_mask) /
  427. hfi1_per_node_cntr[dd->node];
  428. /*
  429. * If the completion vector CPUs available doesn't divide
  430. * evenly among devices, then the first device device to be
  431. * initialized gets an extra CPU.
  432. */
  433. if (first_dev_init &&
  434. cpumask_weight(&entry->comp_vect_mask) %
  435. hfi1_per_node_cntr[dd->node] != 0)
  436. possible_cpus_comp_vect++;
  437. }
  438. dd->comp_vect_possible_cpus = possible_cpus_comp_vect;
  439. /* Reserving CPUs for device completion vector */
  440. for (i = 0; i < dd->comp_vect_possible_cpus; i++) {
  441. curr_cpu = per_cpu_affinity_get(&entry->comp_vect_mask,
  442. entry->comp_vect_affinity);
  443. if (curr_cpu < 0)
  444. goto fail;
  445. cpumask_set_cpu(curr_cpu, dev_comp_vect_mask);
  446. }
  447. hfi1_cdbg(AFFINITY,
  448. "[%s] Completion vector affinity CPU set(s) %*pbl",
  449. rvt_get_ibdev_name(&(dd)->verbs_dev.rdi),
  450. cpumask_pr_args(dev_comp_vect_mask));
  451. return 0;
  452. fail:
  453. for (j = 0; j < i; j++)
  454. per_cpu_affinity_put_max(&entry->comp_vect_mask,
  455. entry->comp_vect_affinity);
  456. return curr_cpu;
  457. }
  458. /*
  459. * It assumes dd->comp_vect_possible_cpus is available.
  460. */
  461. static void _dev_comp_vect_cpu_mask_clean_up(struct hfi1_devdata *dd,
  462. struct hfi1_affinity_node *entry)
  463. __must_hold(&node_affinity.lock)
  464. {
  465. int i, cpu;
  466. lockdep_assert_held(&node_affinity.lock);
  467. if (!dd->comp_vect_possible_cpus)
  468. return;
  469. for (i = 0; i < dd->comp_vect_possible_cpus; i++) {
  470. cpu = per_cpu_affinity_put_max(&dd->comp_vect->mask,
  471. entry->comp_vect_affinity);
  472. /* Clearing CPU in device completion vector cpu mask */
  473. if (cpu >= 0)
  474. cpumask_clear_cpu(cpu, &dd->comp_vect->mask);
  475. }
  476. dd->comp_vect_possible_cpus = 0;
  477. }
  478. /*
  479. * Interrupt affinity.
  480. *
  481. * non-rcv avail gets a default mask that
  482. * starts as possible cpus with threads reset
  483. * and each rcv avail reset.
  484. *
  485. * rcv avail gets node relative 1 wrapping back
  486. * to the node relative 1 as necessary.
  487. *
  488. */
  489. int hfi1_dev_affinity_init(struct hfi1_devdata *dd)
  490. {
  491. struct hfi1_affinity_node *entry;
  492. const struct cpumask *local_mask;
  493. int curr_cpu, possible, i, ret;
  494. bool new_entry = false;
  495. local_mask = cpumask_of_node(dd->node);
  496. if (cpumask_first(local_mask) >= nr_cpu_ids)
  497. local_mask = topology_core_cpumask(0);
  498. mutex_lock(&node_affinity.lock);
  499. entry = node_affinity_lookup(dd->node);
  500. /*
  501. * If this is the first time this NUMA node's affinity is used,
  502. * create an entry in the global affinity structure and initialize it.
  503. */
  504. if (!entry) {
  505. entry = node_affinity_allocate(dd->node);
  506. if (!entry) {
  507. dd_dev_err(dd,
  508. "Unable to allocate global affinity node\n");
  509. ret = -ENOMEM;
  510. goto fail;
  511. }
  512. new_entry = true;
  513. init_cpu_mask_set(&entry->def_intr);
  514. init_cpu_mask_set(&entry->rcv_intr);
  515. cpumask_clear(&entry->comp_vect_mask);
  516. cpumask_clear(&entry->general_intr_mask);
  517. /* Use the "real" cpu mask of this node as the default */
  518. cpumask_and(&entry->def_intr.mask, &node_affinity.real_cpu_mask,
  519. local_mask);
  520. /* fill in the receive list */
  521. possible = cpumask_weight(&entry->def_intr.mask);
  522. curr_cpu = cpumask_first(&entry->def_intr.mask);
  523. if (possible == 1) {
  524. /* only one CPU, everyone will use it */
  525. cpumask_set_cpu(curr_cpu, &entry->rcv_intr.mask);
  526. cpumask_set_cpu(curr_cpu, &entry->general_intr_mask);
  527. } else {
  528. /*
  529. * The general/control context will be the first CPU in
  530. * the default list, so it is removed from the default
  531. * list and added to the general interrupt list.
  532. */
  533. cpumask_clear_cpu(curr_cpu, &entry->def_intr.mask);
  534. cpumask_set_cpu(curr_cpu, &entry->general_intr_mask);
  535. curr_cpu = cpumask_next(curr_cpu,
  536. &entry->def_intr.mask);
  537. /*
  538. * Remove the remaining kernel receive queues from
  539. * the default list and add them to the receive list.
  540. */
  541. for (i = 0;
  542. i < (dd->n_krcv_queues - 1) *
  543. hfi1_per_node_cntr[dd->node];
  544. i++) {
  545. cpumask_clear_cpu(curr_cpu,
  546. &entry->def_intr.mask);
  547. cpumask_set_cpu(curr_cpu,
  548. &entry->rcv_intr.mask);
  549. curr_cpu = cpumask_next(curr_cpu,
  550. &entry->def_intr.mask);
  551. if (curr_cpu >= nr_cpu_ids)
  552. break;
  553. }
  554. /*
  555. * If there ends up being 0 CPU cores leftover for SDMA
  556. * engines, use the same CPU cores as general/control
  557. * context.
  558. */
  559. if (cpumask_empty(&entry->def_intr.mask))
  560. cpumask_copy(&entry->def_intr.mask,
  561. &entry->general_intr_mask);
  562. }
  563. /* Determine completion vector CPUs for the entire node */
  564. cpumask_and(&entry->comp_vect_mask,
  565. &node_affinity.real_cpu_mask, local_mask);
  566. cpumask_andnot(&entry->comp_vect_mask,
  567. &entry->comp_vect_mask,
  568. &entry->rcv_intr.mask);
  569. cpumask_andnot(&entry->comp_vect_mask,
  570. &entry->comp_vect_mask,
  571. &entry->general_intr_mask);
  572. /*
  573. * If there ends up being 0 CPU cores leftover for completion
  574. * vectors, use the same CPU core as the general/control
  575. * context.
  576. */
  577. if (cpumask_empty(&entry->comp_vect_mask))
  578. cpumask_copy(&entry->comp_vect_mask,
  579. &entry->general_intr_mask);
  580. }
  581. ret = _dev_comp_vect_cpu_mask_init(dd, entry, new_entry);
  582. if (ret < 0)
  583. goto fail;
  584. if (new_entry)
  585. node_affinity_add_tail(entry);
  586. dd->affinity_entry = entry;
  587. mutex_unlock(&node_affinity.lock);
  588. return 0;
  589. fail:
  590. if (new_entry)
  591. node_affinity_destroy(entry);
  592. mutex_unlock(&node_affinity.lock);
  593. return ret;
  594. }
  595. void hfi1_dev_affinity_clean_up(struct hfi1_devdata *dd)
  596. {
  597. struct hfi1_affinity_node *entry;
  598. mutex_lock(&node_affinity.lock);
  599. if (!dd->affinity_entry)
  600. goto unlock;
  601. entry = node_affinity_lookup(dd->node);
  602. if (!entry)
  603. goto unlock;
  604. /*
  605. * Free device completion vector CPUs to be used by future
  606. * completion vectors
  607. */
  608. _dev_comp_vect_cpu_mask_clean_up(dd, entry);
  609. unlock:
  610. dd->affinity_entry = NULL;
  611. mutex_unlock(&node_affinity.lock);
  612. }
  613. /*
  614. * Function updates the irq affinity hint for msix after it has been changed
  615. * by the user using the /proc/irq interface. This function only accepts
  616. * one cpu in the mask.
  617. */
  618. static void hfi1_update_sdma_affinity(struct hfi1_msix_entry *msix, int cpu)
  619. {
  620. struct sdma_engine *sde = msix->arg;
  621. struct hfi1_devdata *dd = sde->dd;
  622. struct hfi1_affinity_node *entry;
  623. struct cpu_mask_set *set;
  624. int i, old_cpu;
  625. if (cpu > num_online_cpus() || cpu == sde->cpu)
  626. return;
  627. mutex_lock(&node_affinity.lock);
  628. entry = node_affinity_lookup(dd->node);
  629. if (!entry)
  630. goto unlock;
  631. old_cpu = sde->cpu;
  632. sde->cpu = cpu;
  633. cpumask_clear(&msix->mask);
  634. cpumask_set_cpu(cpu, &msix->mask);
  635. dd_dev_dbg(dd, "IRQ: %u, type %s engine %u -> cpu: %d\n",
  636. msix->irq, irq_type_names[msix->type],
  637. sde->this_idx, cpu);
  638. irq_set_affinity_hint(msix->irq, &msix->mask);
  639. /*
  640. * Set the new cpu in the hfi1_affinity_node and clean
  641. * the old cpu if it is not used by any other IRQ
  642. */
  643. set = &entry->def_intr;
  644. cpumask_set_cpu(cpu, &set->mask);
  645. cpumask_set_cpu(cpu, &set->used);
  646. for (i = 0; i < dd->msix_info.max_requested; i++) {
  647. struct hfi1_msix_entry *other_msix;
  648. other_msix = &dd->msix_info.msix_entries[i];
  649. if (other_msix->type != IRQ_SDMA || other_msix == msix)
  650. continue;
  651. if (cpumask_test_cpu(old_cpu, &other_msix->mask))
  652. goto unlock;
  653. }
  654. cpumask_clear_cpu(old_cpu, &set->mask);
  655. cpumask_clear_cpu(old_cpu, &set->used);
  656. unlock:
  657. mutex_unlock(&node_affinity.lock);
  658. }
  659. static void hfi1_irq_notifier_notify(struct irq_affinity_notify *notify,
  660. const cpumask_t *mask)
  661. {
  662. int cpu = cpumask_first(mask);
  663. struct hfi1_msix_entry *msix = container_of(notify,
  664. struct hfi1_msix_entry,
  665. notify);
  666. /* Only one CPU configuration supported currently */
  667. hfi1_update_sdma_affinity(msix, cpu);
  668. }
  669. static void hfi1_irq_notifier_release(struct kref *ref)
  670. {
  671. /*
  672. * This is required by affinity notifier. We don't have anything to
  673. * free here.
  674. */
  675. }
  676. static void hfi1_setup_sdma_notifier(struct hfi1_msix_entry *msix)
  677. {
  678. struct irq_affinity_notify *notify = &msix->notify;
  679. notify->irq = msix->irq;
  680. notify->notify = hfi1_irq_notifier_notify;
  681. notify->release = hfi1_irq_notifier_release;
  682. if (irq_set_affinity_notifier(notify->irq, notify))
  683. pr_err("Failed to register sdma irq affinity notifier for irq %d\n",
  684. notify->irq);
  685. }
  686. static void hfi1_cleanup_sdma_notifier(struct hfi1_msix_entry *msix)
  687. {
  688. struct irq_affinity_notify *notify = &msix->notify;
  689. if (irq_set_affinity_notifier(notify->irq, NULL))
  690. pr_err("Failed to cleanup sdma irq affinity notifier for irq %d\n",
  691. notify->irq);
  692. }
  693. /*
  694. * Function sets the irq affinity for msix.
  695. * It *must* be called with node_affinity.lock held.
  696. */
  697. static int get_irq_affinity(struct hfi1_devdata *dd,
  698. struct hfi1_msix_entry *msix)
  699. {
  700. cpumask_var_t diff;
  701. struct hfi1_affinity_node *entry;
  702. struct cpu_mask_set *set = NULL;
  703. struct sdma_engine *sde = NULL;
  704. struct hfi1_ctxtdata *rcd = NULL;
  705. char extra[64];
  706. int cpu = -1;
  707. extra[0] = '\0';
  708. cpumask_clear(&msix->mask);
  709. entry = node_affinity_lookup(dd->node);
  710. switch (msix->type) {
  711. case IRQ_SDMA:
  712. sde = (struct sdma_engine *)msix->arg;
  713. scnprintf(extra, 64, "engine %u", sde->this_idx);
  714. set = &entry->def_intr;
  715. break;
  716. case IRQ_GENERAL:
  717. cpu = cpumask_first(&entry->general_intr_mask);
  718. break;
  719. case IRQ_RCVCTXT:
  720. rcd = (struct hfi1_ctxtdata *)msix->arg;
  721. if (rcd->ctxt == HFI1_CTRL_CTXT)
  722. cpu = cpumask_first(&entry->general_intr_mask);
  723. else
  724. set = &entry->rcv_intr;
  725. scnprintf(extra, 64, "ctxt %u", rcd->ctxt);
  726. break;
  727. case IRQ_NETDEVCTXT:
  728. rcd = (struct hfi1_ctxtdata *)msix->arg;
  729. set = &entry->def_intr;
  730. scnprintf(extra, 64, "ctxt %u", rcd->ctxt);
  731. break;
  732. default:
  733. dd_dev_err(dd, "Invalid IRQ type %d\n", msix->type);
  734. return -EINVAL;
  735. }
  736. /*
  737. * The general and control contexts are placed on a particular
  738. * CPU, which is set above. Skip accounting for it. Everything else
  739. * finds its CPU here.
  740. */
  741. if (cpu == -1 && set) {
  742. if (!zalloc_cpumask_var(&diff, GFP_KERNEL))
  743. return -ENOMEM;
  744. cpu = cpu_mask_set_get_first(set, diff);
  745. if (cpu < 0) {
  746. free_cpumask_var(diff);
  747. dd_dev_err(dd, "Failure to obtain CPU for IRQ\n");
  748. return cpu;
  749. }
  750. free_cpumask_var(diff);
  751. }
  752. cpumask_set_cpu(cpu, &msix->mask);
  753. dd_dev_info(dd, "IRQ: %u, type %s %s -> cpu: %d\n",
  754. msix->irq, irq_type_names[msix->type],
  755. extra, cpu);
  756. irq_set_affinity_hint(msix->irq, &msix->mask);
  757. if (msix->type == IRQ_SDMA) {
  758. sde->cpu = cpu;
  759. hfi1_setup_sdma_notifier(msix);
  760. }
  761. return 0;
  762. }
  763. int hfi1_get_irq_affinity(struct hfi1_devdata *dd, struct hfi1_msix_entry *msix)
  764. {
  765. int ret;
  766. mutex_lock(&node_affinity.lock);
  767. ret = get_irq_affinity(dd, msix);
  768. mutex_unlock(&node_affinity.lock);
  769. return ret;
  770. }
  771. void hfi1_put_irq_affinity(struct hfi1_devdata *dd,
  772. struct hfi1_msix_entry *msix)
  773. {
  774. struct cpu_mask_set *set = NULL;
  775. struct hfi1_affinity_node *entry;
  776. mutex_lock(&node_affinity.lock);
  777. entry = node_affinity_lookup(dd->node);
  778. switch (msix->type) {
  779. case IRQ_SDMA:
  780. set = &entry->def_intr;
  781. hfi1_cleanup_sdma_notifier(msix);
  782. break;
  783. case IRQ_GENERAL:
  784. /* Don't do accounting for general contexts */
  785. break;
  786. case IRQ_RCVCTXT: {
  787. struct hfi1_ctxtdata *rcd = msix->arg;
  788. /* Don't do accounting for control contexts */
  789. if (rcd->ctxt != HFI1_CTRL_CTXT)
  790. set = &entry->rcv_intr;
  791. break;
  792. }
  793. case IRQ_NETDEVCTXT:
  794. set = &entry->def_intr;
  795. break;
  796. default:
  797. mutex_unlock(&node_affinity.lock);
  798. return;
  799. }
  800. if (set) {
  801. cpumask_andnot(&set->used, &set->used, &msix->mask);
  802. _cpu_mask_set_gen_dec(set);
  803. }
  804. irq_set_affinity_hint(msix->irq, NULL);
  805. cpumask_clear(&msix->mask);
  806. mutex_unlock(&node_affinity.lock);
  807. }
  808. /* This should be called with node_affinity.lock held */
  809. static void find_hw_thread_mask(uint hw_thread_no, cpumask_var_t hw_thread_mask,
  810. struct hfi1_affinity_node_list *affinity)
  811. {
  812. int possible, curr_cpu, i;
  813. uint num_cores_per_socket = node_affinity.num_online_cpus /
  814. affinity->num_core_siblings /
  815. node_affinity.num_online_nodes;
  816. cpumask_copy(hw_thread_mask, &affinity->proc.mask);
  817. if (affinity->num_core_siblings > 0) {
  818. /* Removing other siblings not needed for now */
  819. possible = cpumask_weight(hw_thread_mask);
  820. curr_cpu = cpumask_first(hw_thread_mask);
  821. for (i = 0;
  822. i < num_cores_per_socket * node_affinity.num_online_nodes;
  823. i++)
  824. curr_cpu = cpumask_next(curr_cpu, hw_thread_mask);
  825. for (; i < possible; i++) {
  826. cpumask_clear_cpu(curr_cpu, hw_thread_mask);
  827. curr_cpu = cpumask_next(curr_cpu, hw_thread_mask);
  828. }
  829. /* Identifying correct HW threads within physical cores */
  830. cpumask_shift_left(hw_thread_mask, hw_thread_mask,
  831. num_cores_per_socket *
  832. node_affinity.num_online_nodes *
  833. hw_thread_no);
  834. }
  835. }
  836. int hfi1_get_proc_affinity(int node)
  837. {
  838. int cpu = -1, ret, i;
  839. struct hfi1_affinity_node *entry;
  840. cpumask_var_t diff, hw_thread_mask, available_mask, intrs_mask;
  841. const struct cpumask *node_mask,
  842. *proc_mask = current->cpus_ptr;
  843. struct hfi1_affinity_node_list *affinity = &node_affinity;
  844. struct cpu_mask_set *set = &affinity->proc;
  845. /*
  846. * check whether process/context affinity has already
  847. * been set
  848. */
  849. if (current->nr_cpus_allowed == 1) {
  850. hfi1_cdbg(PROC, "PID %u %s affinity set to CPU %*pbl",
  851. current->pid, current->comm,
  852. cpumask_pr_args(proc_mask));
  853. /*
  854. * Mark the pre-set CPU as used. This is atomic so we don't
  855. * need the lock
  856. */
  857. cpu = cpumask_first(proc_mask);
  858. cpumask_set_cpu(cpu, &set->used);
  859. goto done;
  860. } else if (current->nr_cpus_allowed < cpumask_weight(&set->mask)) {
  861. hfi1_cdbg(PROC, "PID %u %s affinity set to CPU set(s) %*pbl",
  862. current->pid, current->comm,
  863. cpumask_pr_args(proc_mask));
  864. goto done;
  865. }
  866. /*
  867. * The process does not have a preset CPU affinity so find one to
  868. * recommend using the following algorithm:
  869. *
  870. * For each user process that is opening a context on HFI Y:
  871. * a) If all cores are filled, reinitialize the bitmask
  872. * b) Fill real cores first, then HT cores (First set of HT
  873. * cores on all physical cores, then second set of HT core,
  874. * and, so on) in the following order:
  875. *
  876. * 1. Same NUMA node as HFI Y and not running an IRQ
  877. * handler
  878. * 2. Same NUMA node as HFI Y and running an IRQ handler
  879. * 3. Different NUMA node to HFI Y and not running an IRQ
  880. * handler
  881. * 4. Different NUMA node to HFI Y and running an IRQ
  882. * handler
  883. * c) Mark core as filled in the bitmask. As user processes are
  884. * done, clear cores from the bitmask.
  885. */
  886. ret = zalloc_cpumask_var(&diff, GFP_KERNEL);
  887. if (!ret)
  888. goto done;
  889. ret = zalloc_cpumask_var(&hw_thread_mask, GFP_KERNEL);
  890. if (!ret)
  891. goto free_diff;
  892. ret = zalloc_cpumask_var(&available_mask, GFP_KERNEL);
  893. if (!ret)
  894. goto free_hw_thread_mask;
  895. ret = zalloc_cpumask_var(&intrs_mask, GFP_KERNEL);
  896. if (!ret)
  897. goto free_available_mask;
  898. mutex_lock(&affinity->lock);
  899. /*
  900. * If we've used all available HW threads, clear the mask and start
  901. * overloading.
  902. */
  903. _cpu_mask_set_gen_inc(set);
  904. /*
  905. * If NUMA node has CPUs used by interrupt handlers, include them in the
  906. * interrupt handler mask.
  907. */
  908. entry = node_affinity_lookup(node);
  909. if (entry) {
  910. cpumask_copy(intrs_mask, (entry->def_intr.gen ?
  911. &entry->def_intr.mask :
  912. &entry->def_intr.used));
  913. cpumask_or(intrs_mask, intrs_mask, (entry->rcv_intr.gen ?
  914. &entry->rcv_intr.mask :
  915. &entry->rcv_intr.used));
  916. cpumask_or(intrs_mask, intrs_mask, &entry->general_intr_mask);
  917. }
  918. hfi1_cdbg(PROC, "CPUs used by interrupts: %*pbl",
  919. cpumask_pr_args(intrs_mask));
  920. cpumask_copy(hw_thread_mask, &set->mask);
  921. /*
  922. * If HT cores are enabled, identify which HW threads within the
  923. * physical cores should be used.
  924. */
  925. if (affinity->num_core_siblings > 0) {
  926. for (i = 0; i < affinity->num_core_siblings; i++) {
  927. find_hw_thread_mask(i, hw_thread_mask, affinity);
  928. /*
  929. * If there's at least one available core for this HW
  930. * thread number, stop looking for a core.
  931. *
  932. * diff will always be not empty at least once in this
  933. * loop as the used mask gets reset when
  934. * (set->mask == set->used) before this loop.
  935. */
  936. cpumask_andnot(diff, hw_thread_mask, &set->used);
  937. if (!cpumask_empty(diff))
  938. break;
  939. }
  940. }
  941. hfi1_cdbg(PROC, "Same available HW thread on all physical CPUs: %*pbl",
  942. cpumask_pr_args(hw_thread_mask));
  943. node_mask = cpumask_of_node(node);
  944. hfi1_cdbg(PROC, "Device on NUMA %u, CPUs %*pbl", node,
  945. cpumask_pr_args(node_mask));
  946. /* Get cpumask of available CPUs on preferred NUMA */
  947. cpumask_and(available_mask, hw_thread_mask, node_mask);
  948. cpumask_andnot(available_mask, available_mask, &set->used);
  949. hfi1_cdbg(PROC, "Available CPUs on NUMA %u: %*pbl", node,
  950. cpumask_pr_args(available_mask));
  951. /*
  952. * At first, we don't want to place processes on the same
  953. * CPUs as interrupt handlers. Then, CPUs running interrupt
  954. * handlers are used.
  955. *
  956. * 1) If diff is not empty, then there are CPUs not running
  957. * non-interrupt handlers available, so diff gets copied
  958. * over to available_mask.
  959. * 2) If diff is empty, then all CPUs not running interrupt
  960. * handlers are taken, so available_mask contains all
  961. * available CPUs running interrupt handlers.
  962. * 3) If available_mask is empty, then all CPUs on the
  963. * preferred NUMA node are taken, so other NUMA nodes are
  964. * used for process assignments using the same method as
  965. * the preferred NUMA node.
  966. */
  967. cpumask_andnot(diff, available_mask, intrs_mask);
  968. if (!cpumask_empty(diff))
  969. cpumask_copy(available_mask, diff);
  970. /* If we don't have CPUs on the preferred node, use other NUMA nodes */
  971. if (cpumask_empty(available_mask)) {
  972. cpumask_andnot(available_mask, hw_thread_mask, &set->used);
  973. /* Excluding preferred NUMA cores */
  974. cpumask_andnot(available_mask, available_mask, node_mask);
  975. hfi1_cdbg(PROC,
  976. "Preferred NUMA node cores are taken, cores available in other NUMA nodes: %*pbl",
  977. cpumask_pr_args(available_mask));
  978. /*
  979. * At first, we don't want to place processes on the same
  980. * CPUs as interrupt handlers.
  981. */
  982. cpumask_andnot(diff, available_mask, intrs_mask);
  983. if (!cpumask_empty(diff))
  984. cpumask_copy(available_mask, diff);
  985. }
  986. hfi1_cdbg(PROC, "Possible CPUs for process: %*pbl",
  987. cpumask_pr_args(available_mask));
  988. cpu = cpumask_first(available_mask);
  989. if (cpu >= nr_cpu_ids) /* empty */
  990. cpu = -1;
  991. else
  992. cpumask_set_cpu(cpu, &set->used);
  993. mutex_unlock(&affinity->lock);
  994. hfi1_cdbg(PROC, "Process assigned to CPU %d", cpu);
  995. free_cpumask_var(intrs_mask);
  996. free_available_mask:
  997. free_cpumask_var(available_mask);
  998. free_hw_thread_mask:
  999. free_cpumask_var(hw_thread_mask);
  1000. free_diff:
  1001. free_cpumask_var(diff);
  1002. done:
  1003. return cpu;
  1004. }
  1005. void hfi1_put_proc_affinity(int cpu)
  1006. {
  1007. struct hfi1_affinity_node_list *affinity = &node_affinity;
  1008. struct cpu_mask_set *set = &affinity->proc;
  1009. if (cpu < 0)
  1010. return;
  1011. mutex_lock(&affinity->lock);
  1012. cpu_mask_set_put(set, cpu);
  1013. hfi1_cdbg(PROC, "Returning CPU %d for future process assignment", cpu);
  1014. mutex_unlock(&affinity->lock);
  1015. }