affinity.c 33 KB

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