affinity.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*
  2. * Copyright(c) 2015 - 2017 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. /* Initialize non-HT cpu cores mask */
  75. void init_real_cpu_mask(void)
  76. {
  77. int possible, curr_cpu, i, ht;
  78. cpumask_clear(&node_affinity.real_cpu_mask);
  79. /* Start with cpu online mask as the real cpu mask */
  80. cpumask_copy(&node_affinity.real_cpu_mask, cpu_online_mask);
  81. /*
  82. * Remove HT cores from the real cpu mask. Do this in two steps below.
  83. */
  84. possible = cpumask_weight(&node_affinity.real_cpu_mask);
  85. ht = cpumask_weight(topology_sibling_cpumask(
  86. cpumask_first(&node_affinity.real_cpu_mask)));
  87. /*
  88. * Step 1. Skip over the first N HT siblings and use them as the
  89. * "real" cores. Assumes that HT cores are not enumerated in
  90. * succession (except in the single core case).
  91. */
  92. curr_cpu = cpumask_first(&node_affinity.real_cpu_mask);
  93. for (i = 0; i < possible / ht; i++)
  94. curr_cpu = cpumask_next(curr_cpu, &node_affinity.real_cpu_mask);
  95. /*
  96. * Step 2. Remove the remaining HT siblings. Use cpumask_next() to
  97. * skip any gaps.
  98. */
  99. for (; i < possible; i++) {
  100. cpumask_clear_cpu(curr_cpu, &node_affinity.real_cpu_mask);
  101. curr_cpu = cpumask_next(curr_cpu, &node_affinity.real_cpu_mask);
  102. }
  103. }
  104. int node_affinity_init(void)
  105. {
  106. int node;
  107. struct pci_dev *dev = NULL;
  108. const struct pci_device_id *ids = hfi1_pci_tbl;
  109. cpumask_clear(&node_affinity.proc.used);
  110. cpumask_copy(&node_affinity.proc.mask, cpu_online_mask);
  111. node_affinity.proc.gen = 0;
  112. node_affinity.num_core_siblings =
  113. cpumask_weight(topology_sibling_cpumask(
  114. cpumask_first(&node_affinity.proc.mask)
  115. ));
  116. node_affinity.num_possible_nodes = num_possible_nodes();
  117. node_affinity.num_online_nodes = num_online_nodes();
  118. node_affinity.num_online_cpus = num_online_cpus();
  119. /*
  120. * The real cpu mask is part of the affinity struct but it has to be
  121. * initialized early. It is needed to calculate the number of user
  122. * contexts in set_up_context_variables().
  123. */
  124. init_real_cpu_mask();
  125. hfi1_per_node_cntr = kcalloc(node_affinity.num_possible_nodes,
  126. sizeof(*hfi1_per_node_cntr), GFP_KERNEL);
  127. if (!hfi1_per_node_cntr)
  128. return -ENOMEM;
  129. while (ids->vendor) {
  130. dev = NULL;
  131. while ((dev = pci_get_device(ids->vendor, ids->device, dev))) {
  132. node = pcibus_to_node(dev->bus);
  133. if (node < 0)
  134. node = numa_node_id();
  135. hfi1_per_node_cntr[node]++;
  136. }
  137. ids++;
  138. }
  139. return 0;
  140. }
  141. void node_affinity_destroy(void)
  142. {
  143. struct list_head *pos, *q;
  144. struct hfi1_affinity_node *entry;
  145. mutex_lock(&node_affinity.lock);
  146. list_for_each_safe(pos, q, &node_affinity.list) {
  147. entry = list_entry(pos, struct hfi1_affinity_node,
  148. list);
  149. list_del(pos);
  150. kfree(entry);
  151. }
  152. mutex_unlock(&node_affinity.lock);
  153. kfree(hfi1_per_node_cntr);
  154. }
  155. static struct hfi1_affinity_node *node_affinity_allocate(int node)
  156. {
  157. struct hfi1_affinity_node *entry;
  158. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  159. if (!entry)
  160. return NULL;
  161. entry->node = node;
  162. INIT_LIST_HEAD(&entry->list);
  163. return entry;
  164. }
  165. /*
  166. * It appends an entry to the list.
  167. * It *must* be called with node_affinity.lock held.
  168. */
  169. static void node_affinity_add_tail(struct hfi1_affinity_node *entry)
  170. {
  171. list_add_tail(&entry->list, &node_affinity.list);
  172. }
  173. /* It must be called with node_affinity.lock held */
  174. static struct hfi1_affinity_node *node_affinity_lookup(int node)
  175. {
  176. struct list_head *pos;
  177. struct hfi1_affinity_node *entry;
  178. list_for_each(pos, &node_affinity.list) {
  179. entry = list_entry(pos, struct hfi1_affinity_node, list);
  180. if (entry->node == node)
  181. return entry;
  182. }
  183. return NULL;
  184. }
  185. /*
  186. * Interrupt affinity.
  187. *
  188. * non-rcv avail gets a default mask that
  189. * starts as possible cpus with threads reset
  190. * and each rcv avail reset.
  191. *
  192. * rcv avail gets node relative 1 wrapping back
  193. * to the node relative 1 as necessary.
  194. *
  195. */
  196. int hfi1_dev_affinity_init(struct hfi1_devdata *dd)
  197. {
  198. int node = pcibus_to_node(dd->pcidev->bus);
  199. struct hfi1_affinity_node *entry;
  200. const struct cpumask *local_mask;
  201. int curr_cpu, possible, i;
  202. if (node < 0)
  203. node = numa_node_id();
  204. dd->node = node;
  205. local_mask = cpumask_of_node(dd->node);
  206. if (cpumask_first(local_mask) >= nr_cpu_ids)
  207. local_mask = topology_core_cpumask(0);
  208. mutex_lock(&node_affinity.lock);
  209. entry = node_affinity_lookup(dd->node);
  210. /*
  211. * If this is the first time this NUMA node's affinity is used,
  212. * create an entry in the global affinity structure and initialize it.
  213. */
  214. if (!entry) {
  215. entry = node_affinity_allocate(node);
  216. if (!entry) {
  217. dd_dev_err(dd,
  218. "Unable to allocate global affinity node\n");
  219. mutex_unlock(&node_affinity.lock);
  220. return -ENOMEM;
  221. }
  222. init_cpu_mask_set(&entry->def_intr);
  223. init_cpu_mask_set(&entry->rcv_intr);
  224. cpumask_clear(&entry->general_intr_mask);
  225. /* Use the "real" cpu mask of this node as the default */
  226. cpumask_and(&entry->def_intr.mask, &node_affinity.real_cpu_mask,
  227. local_mask);
  228. /* fill in the receive list */
  229. possible = cpumask_weight(&entry->def_intr.mask);
  230. curr_cpu = cpumask_first(&entry->def_intr.mask);
  231. if (possible == 1) {
  232. /* only one CPU, everyone will use it */
  233. cpumask_set_cpu(curr_cpu, &entry->rcv_intr.mask);
  234. cpumask_set_cpu(curr_cpu, &entry->general_intr_mask);
  235. } else {
  236. /*
  237. * The general/control context will be the first CPU in
  238. * the default list, so it is removed from the default
  239. * list and added to the general interrupt list.
  240. */
  241. cpumask_clear_cpu(curr_cpu, &entry->def_intr.mask);
  242. cpumask_set_cpu(curr_cpu, &entry->general_intr_mask);
  243. curr_cpu = cpumask_next(curr_cpu,
  244. &entry->def_intr.mask);
  245. /*
  246. * Remove the remaining kernel receive queues from
  247. * the default list and add them to the receive list.
  248. */
  249. for (i = 0;
  250. i < (dd->n_krcv_queues - 1) *
  251. hfi1_per_node_cntr[dd->node];
  252. i++) {
  253. cpumask_clear_cpu(curr_cpu,
  254. &entry->def_intr.mask);
  255. cpumask_set_cpu(curr_cpu,
  256. &entry->rcv_intr.mask);
  257. curr_cpu = cpumask_next(curr_cpu,
  258. &entry->def_intr.mask);
  259. if (curr_cpu >= nr_cpu_ids)
  260. break;
  261. }
  262. /*
  263. * If there ends up being 0 CPU cores leftover for SDMA
  264. * engines, use the same CPU cores as general/control
  265. * context.
  266. */
  267. if (cpumask_weight(&entry->def_intr.mask) == 0)
  268. cpumask_copy(&entry->def_intr.mask,
  269. &entry->general_intr_mask);
  270. }
  271. node_affinity_add_tail(entry);
  272. }
  273. mutex_unlock(&node_affinity.lock);
  274. return 0;
  275. }
  276. /*
  277. * Function updates the irq affinity hint for msix after it has been changed
  278. * by the user using the /proc/irq interface. This function only accepts
  279. * one cpu in the mask.
  280. */
  281. static void hfi1_update_sdma_affinity(struct hfi1_msix_entry *msix, int cpu)
  282. {
  283. struct sdma_engine *sde = msix->arg;
  284. struct hfi1_devdata *dd = sde->dd;
  285. struct hfi1_affinity_node *entry;
  286. struct cpu_mask_set *set;
  287. int i, old_cpu;
  288. if (cpu > num_online_cpus() || cpu == sde->cpu)
  289. return;
  290. mutex_lock(&node_affinity.lock);
  291. entry = node_affinity_lookup(dd->node);
  292. if (!entry)
  293. goto unlock;
  294. old_cpu = sde->cpu;
  295. sde->cpu = cpu;
  296. cpumask_clear(&msix->mask);
  297. cpumask_set_cpu(cpu, &msix->mask);
  298. dd_dev_dbg(dd, "IRQ: %u, type %s engine %u -> cpu: %d\n",
  299. msix->irq, irq_type_names[msix->type],
  300. sde->this_idx, cpu);
  301. irq_set_affinity_hint(msix->irq, &msix->mask);
  302. /*
  303. * Set the new cpu in the hfi1_affinity_node and clean
  304. * the old cpu if it is not used by any other IRQ
  305. */
  306. set = &entry->def_intr;
  307. cpumask_set_cpu(cpu, &set->mask);
  308. cpumask_set_cpu(cpu, &set->used);
  309. for (i = 0; i < dd->num_msix_entries; i++) {
  310. struct hfi1_msix_entry *other_msix;
  311. other_msix = &dd->msix_entries[i];
  312. if (other_msix->type != IRQ_SDMA || other_msix == msix)
  313. continue;
  314. if (cpumask_test_cpu(old_cpu, &other_msix->mask))
  315. goto unlock;
  316. }
  317. cpumask_clear_cpu(old_cpu, &set->mask);
  318. cpumask_clear_cpu(old_cpu, &set->used);
  319. unlock:
  320. mutex_unlock(&node_affinity.lock);
  321. }
  322. static void hfi1_irq_notifier_notify(struct irq_affinity_notify *notify,
  323. const cpumask_t *mask)
  324. {
  325. int cpu = cpumask_first(mask);
  326. struct hfi1_msix_entry *msix = container_of(notify,
  327. struct hfi1_msix_entry,
  328. notify);
  329. /* Only one CPU configuration supported currently */
  330. hfi1_update_sdma_affinity(msix, cpu);
  331. }
  332. static void hfi1_irq_notifier_release(struct kref *ref)
  333. {
  334. /*
  335. * This is required by affinity notifier. We don't have anything to
  336. * free here.
  337. */
  338. }
  339. static void hfi1_setup_sdma_notifier(struct hfi1_msix_entry *msix)
  340. {
  341. struct irq_affinity_notify *notify = &msix->notify;
  342. notify->irq = msix->irq;
  343. notify->notify = hfi1_irq_notifier_notify;
  344. notify->release = hfi1_irq_notifier_release;
  345. if (irq_set_affinity_notifier(notify->irq, notify))
  346. pr_err("Failed to register sdma irq affinity notifier for irq %d\n",
  347. notify->irq);
  348. }
  349. static void hfi1_cleanup_sdma_notifier(struct hfi1_msix_entry *msix)
  350. {
  351. struct irq_affinity_notify *notify = &msix->notify;
  352. if (irq_set_affinity_notifier(notify->irq, NULL))
  353. pr_err("Failed to cleanup sdma irq affinity notifier for irq %d\n",
  354. notify->irq);
  355. }
  356. /*
  357. * Function sets the irq affinity for msix.
  358. * It *must* be called with node_affinity.lock held.
  359. */
  360. static int get_irq_affinity(struct hfi1_devdata *dd,
  361. struct hfi1_msix_entry *msix)
  362. {
  363. cpumask_var_t diff;
  364. struct hfi1_affinity_node *entry;
  365. struct cpu_mask_set *set = NULL;
  366. struct sdma_engine *sde = NULL;
  367. struct hfi1_ctxtdata *rcd = NULL;
  368. char extra[64];
  369. int cpu = -1;
  370. extra[0] = '\0';
  371. cpumask_clear(&msix->mask);
  372. entry = node_affinity_lookup(dd->node);
  373. switch (msix->type) {
  374. case IRQ_SDMA:
  375. sde = (struct sdma_engine *)msix->arg;
  376. scnprintf(extra, 64, "engine %u", sde->this_idx);
  377. set = &entry->def_intr;
  378. break;
  379. case IRQ_GENERAL:
  380. cpu = cpumask_first(&entry->general_intr_mask);
  381. break;
  382. case IRQ_RCVCTXT:
  383. rcd = (struct hfi1_ctxtdata *)msix->arg;
  384. if (rcd->ctxt == HFI1_CTRL_CTXT)
  385. cpu = cpumask_first(&entry->general_intr_mask);
  386. else
  387. set = &entry->rcv_intr;
  388. scnprintf(extra, 64, "ctxt %u", rcd->ctxt);
  389. break;
  390. default:
  391. dd_dev_err(dd, "Invalid IRQ type %d\n", msix->type);
  392. return -EINVAL;
  393. }
  394. /*
  395. * The general and control contexts are placed on a particular
  396. * CPU, which is set above. Skip accounting for it. Everything else
  397. * finds its CPU here.
  398. */
  399. if (cpu == -1 && set) {
  400. if (!zalloc_cpumask_var(&diff, GFP_KERNEL))
  401. return -ENOMEM;
  402. if (cpumask_equal(&set->mask, &set->used)) {
  403. /*
  404. * We've used up all the CPUs, bump up the generation
  405. * and reset the 'used' map
  406. */
  407. set->gen++;
  408. cpumask_clear(&set->used);
  409. }
  410. cpumask_andnot(diff, &set->mask, &set->used);
  411. cpu = cpumask_first(diff);
  412. cpumask_set_cpu(cpu, &set->used);
  413. free_cpumask_var(diff);
  414. }
  415. cpumask_set_cpu(cpu, &msix->mask);
  416. dd_dev_info(dd, "IRQ: %u, type %s %s -> cpu: %d\n",
  417. msix->irq, irq_type_names[msix->type],
  418. extra, cpu);
  419. irq_set_affinity_hint(msix->irq, &msix->mask);
  420. if (msix->type == IRQ_SDMA) {
  421. sde->cpu = cpu;
  422. hfi1_setup_sdma_notifier(msix);
  423. }
  424. return 0;
  425. }
  426. int hfi1_get_irq_affinity(struct hfi1_devdata *dd, struct hfi1_msix_entry *msix)
  427. {
  428. int ret;
  429. mutex_lock(&node_affinity.lock);
  430. ret = get_irq_affinity(dd, msix);
  431. mutex_unlock(&node_affinity.lock);
  432. return ret;
  433. }
  434. void hfi1_put_irq_affinity(struct hfi1_devdata *dd,
  435. struct hfi1_msix_entry *msix)
  436. {
  437. struct cpu_mask_set *set = NULL;
  438. struct hfi1_ctxtdata *rcd;
  439. struct hfi1_affinity_node *entry;
  440. mutex_lock(&node_affinity.lock);
  441. entry = node_affinity_lookup(dd->node);
  442. switch (msix->type) {
  443. case IRQ_SDMA:
  444. set = &entry->def_intr;
  445. hfi1_cleanup_sdma_notifier(msix);
  446. break;
  447. case IRQ_GENERAL:
  448. /* Don't do accounting for general contexts */
  449. break;
  450. case IRQ_RCVCTXT:
  451. rcd = (struct hfi1_ctxtdata *)msix->arg;
  452. /* Don't do accounting for control contexts */
  453. if (rcd->ctxt != HFI1_CTRL_CTXT)
  454. set = &entry->rcv_intr;
  455. break;
  456. default:
  457. mutex_unlock(&node_affinity.lock);
  458. return;
  459. }
  460. if (set) {
  461. cpumask_andnot(&set->used, &set->used, &msix->mask);
  462. if (cpumask_empty(&set->used) && set->gen) {
  463. set->gen--;
  464. cpumask_copy(&set->used, &set->mask);
  465. }
  466. }
  467. irq_set_affinity_hint(msix->irq, NULL);
  468. cpumask_clear(&msix->mask);
  469. mutex_unlock(&node_affinity.lock);
  470. }
  471. /* This should be called with node_affinity.lock held */
  472. static void find_hw_thread_mask(uint hw_thread_no, cpumask_var_t hw_thread_mask,
  473. struct hfi1_affinity_node_list *affinity)
  474. {
  475. int possible, curr_cpu, i;
  476. uint num_cores_per_socket = node_affinity.num_online_cpus /
  477. affinity->num_core_siblings /
  478. node_affinity.num_online_nodes;
  479. cpumask_copy(hw_thread_mask, &affinity->proc.mask);
  480. if (affinity->num_core_siblings > 0) {
  481. /* Removing other siblings not needed for now */
  482. possible = cpumask_weight(hw_thread_mask);
  483. curr_cpu = cpumask_first(hw_thread_mask);
  484. for (i = 0;
  485. i < num_cores_per_socket * node_affinity.num_online_nodes;
  486. i++)
  487. curr_cpu = cpumask_next(curr_cpu, hw_thread_mask);
  488. for (; i < possible; i++) {
  489. cpumask_clear_cpu(curr_cpu, hw_thread_mask);
  490. curr_cpu = cpumask_next(curr_cpu, hw_thread_mask);
  491. }
  492. /* Identifying correct HW threads within physical cores */
  493. cpumask_shift_left(hw_thread_mask, hw_thread_mask,
  494. num_cores_per_socket *
  495. node_affinity.num_online_nodes *
  496. hw_thread_no);
  497. }
  498. }
  499. int hfi1_get_proc_affinity(int node)
  500. {
  501. int cpu = -1, ret, i;
  502. struct hfi1_affinity_node *entry;
  503. cpumask_var_t diff, hw_thread_mask, available_mask, intrs_mask;
  504. const struct cpumask *node_mask,
  505. *proc_mask = &current->cpus_allowed;
  506. struct hfi1_affinity_node_list *affinity = &node_affinity;
  507. struct cpu_mask_set *set = &affinity->proc;
  508. /*
  509. * check whether process/context affinity has already
  510. * been set
  511. */
  512. if (cpumask_weight(proc_mask) == 1) {
  513. hfi1_cdbg(PROC, "PID %u %s affinity set to CPU %*pbl",
  514. current->pid, current->comm,
  515. cpumask_pr_args(proc_mask));
  516. /*
  517. * Mark the pre-set CPU as used. This is atomic so we don't
  518. * need the lock
  519. */
  520. cpu = cpumask_first(proc_mask);
  521. cpumask_set_cpu(cpu, &set->used);
  522. goto done;
  523. } else if (cpumask_weight(proc_mask) < cpumask_weight(&set->mask)) {
  524. hfi1_cdbg(PROC, "PID %u %s affinity set to CPU set(s) %*pbl",
  525. current->pid, current->comm,
  526. cpumask_pr_args(proc_mask));
  527. goto done;
  528. }
  529. /*
  530. * The process does not have a preset CPU affinity so find one to
  531. * recommend using the following algorithm:
  532. *
  533. * For each user process that is opening a context on HFI Y:
  534. * a) If all cores are filled, reinitialize the bitmask
  535. * b) Fill real cores first, then HT cores (First set of HT
  536. * cores on all physical cores, then second set of HT core,
  537. * and, so on) in the following order:
  538. *
  539. * 1. Same NUMA node as HFI Y and not running an IRQ
  540. * handler
  541. * 2. Same NUMA node as HFI Y and running an IRQ handler
  542. * 3. Different NUMA node to HFI Y and not running an IRQ
  543. * handler
  544. * 4. Different NUMA node to HFI Y and running an IRQ
  545. * handler
  546. * c) Mark core as filled in the bitmask. As user processes are
  547. * done, clear cores from the bitmask.
  548. */
  549. ret = zalloc_cpumask_var(&diff, GFP_KERNEL);
  550. if (!ret)
  551. goto done;
  552. ret = zalloc_cpumask_var(&hw_thread_mask, GFP_KERNEL);
  553. if (!ret)
  554. goto free_diff;
  555. ret = zalloc_cpumask_var(&available_mask, GFP_KERNEL);
  556. if (!ret)
  557. goto free_hw_thread_mask;
  558. ret = zalloc_cpumask_var(&intrs_mask, GFP_KERNEL);
  559. if (!ret)
  560. goto free_available_mask;
  561. mutex_lock(&affinity->lock);
  562. /*
  563. * If we've used all available HW threads, clear the mask and start
  564. * overloading.
  565. */
  566. if (cpumask_equal(&set->mask, &set->used)) {
  567. set->gen++;
  568. cpumask_clear(&set->used);
  569. }
  570. /*
  571. * If NUMA node has CPUs used by interrupt handlers, include them in the
  572. * interrupt handler mask.
  573. */
  574. entry = node_affinity_lookup(node);
  575. if (entry) {
  576. cpumask_copy(intrs_mask, (entry->def_intr.gen ?
  577. &entry->def_intr.mask :
  578. &entry->def_intr.used));
  579. cpumask_or(intrs_mask, intrs_mask, (entry->rcv_intr.gen ?
  580. &entry->rcv_intr.mask :
  581. &entry->rcv_intr.used));
  582. cpumask_or(intrs_mask, intrs_mask, &entry->general_intr_mask);
  583. }
  584. hfi1_cdbg(PROC, "CPUs used by interrupts: %*pbl",
  585. cpumask_pr_args(intrs_mask));
  586. cpumask_copy(hw_thread_mask, &set->mask);
  587. /*
  588. * If HT cores are enabled, identify which HW threads within the
  589. * physical cores should be used.
  590. */
  591. if (affinity->num_core_siblings > 0) {
  592. for (i = 0; i < affinity->num_core_siblings; i++) {
  593. find_hw_thread_mask(i, hw_thread_mask, affinity);
  594. /*
  595. * If there's at least one available core for this HW
  596. * thread number, stop looking for a core.
  597. *
  598. * diff will always be not empty at least once in this
  599. * loop as the used mask gets reset when
  600. * (set->mask == set->used) before this loop.
  601. */
  602. cpumask_andnot(diff, hw_thread_mask, &set->used);
  603. if (!cpumask_empty(diff))
  604. break;
  605. }
  606. }
  607. hfi1_cdbg(PROC, "Same available HW thread on all physical CPUs: %*pbl",
  608. cpumask_pr_args(hw_thread_mask));
  609. node_mask = cpumask_of_node(node);
  610. hfi1_cdbg(PROC, "Device on NUMA %u, CPUs %*pbl", node,
  611. cpumask_pr_args(node_mask));
  612. /* Get cpumask of available CPUs on preferred NUMA */
  613. cpumask_and(available_mask, hw_thread_mask, node_mask);
  614. cpumask_andnot(available_mask, available_mask, &set->used);
  615. hfi1_cdbg(PROC, "Available CPUs on NUMA %u: %*pbl", node,
  616. cpumask_pr_args(available_mask));
  617. /*
  618. * At first, we don't want to place processes on the same
  619. * CPUs as interrupt handlers. Then, CPUs running interrupt
  620. * handlers are used.
  621. *
  622. * 1) If diff is not empty, then there are CPUs not running
  623. * non-interrupt handlers available, so diff gets copied
  624. * over to available_mask.
  625. * 2) If diff is empty, then all CPUs not running interrupt
  626. * handlers are taken, so available_mask contains all
  627. * available CPUs running interrupt handlers.
  628. * 3) If available_mask is empty, then all CPUs on the
  629. * preferred NUMA node are taken, so other NUMA nodes are
  630. * used for process assignments using the same method as
  631. * the preferred NUMA node.
  632. */
  633. cpumask_andnot(diff, available_mask, intrs_mask);
  634. if (!cpumask_empty(diff))
  635. cpumask_copy(available_mask, diff);
  636. /* If we don't have CPUs on the preferred node, use other NUMA nodes */
  637. if (cpumask_empty(available_mask)) {
  638. cpumask_andnot(available_mask, hw_thread_mask, &set->used);
  639. /* Excluding preferred NUMA cores */
  640. cpumask_andnot(available_mask, available_mask, node_mask);
  641. hfi1_cdbg(PROC,
  642. "Preferred NUMA node cores are taken, cores available in other NUMA nodes: %*pbl",
  643. cpumask_pr_args(available_mask));
  644. /*
  645. * At first, we don't want to place processes on the same
  646. * CPUs as interrupt handlers.
  647. */
  648. cpumask_andnot(diff, available_mask, intrs_mask);
  649. if (!cpumask_empty(diff))
  650. cpumask_copy(available_mask, diff);
  651. }
  652. hfi1_cdbg(PROC, "Possible CPUs for process: %*pbl",
  653. cpumask_pr_args(available_mask));
  654. cpu = cpumask_first(available_mask);
  655. if (cpu >= nr_cpu_ids) /* empty */
  656. cpu = -1;
  657. else
  658. cpumask_set_cpu(cpu, &set->used);
  659. mutex_unlock(&affinity->lock);
  660. hfi1_cdbg(PROC, "Process assigned to CPU %d", cpu);
  661. free_cpumask_var(intrs_mask);
  662. free_available_mask:
  663. free_cpumask_var(available_mask);
  664. free_hw_thread_mask:
  665. free_cpumask_var(hw_thread_mask);
  666. free_diff:
  667. free_cpumask_var(diff);
  668. done:
  669. return cpu;
  670. }
  671. void hfi1_put_proc_affinity(int cpu)
  672. {
  673. struct hfi1_affinity_node_list *affinity = &node_affinity;
  674. struct cpu_mask_set *set = &affinity->proc;
  675. if (cpu < 0)
  676. return;
  677. mutex_lock(&affinity->lock);
  678. cpumask_clear_cpu(cpu, &set->used);
  679. hfi1_cdbg(PROC, "Returning CPU %d for future process assignment", cpu);
  680. if (cpumask_empty(&set->used) && set->gen) {
  681. set->gen--;
  682. cpumask_copy(&set->used, &set->mask);
  683. }
  684. mutex_unlock(&affinity->lock);
  685. }