affinity.c 32 KB

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