affinity.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /*
  2. * Copyright(c) 2015, 2016 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 vector: %u, type %s engine %u -> cpu: %d\n",
  299. msix->msix.vector, irq_type_names[msix->type],
  300. sde->this_idx, cpu);
  301. irq_set_affinity_hint(msix->msix.vector, &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->msix.vector;
  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. int ret;
  364. cpumask_var_t diff;
  365. struct hfi1_affinity_node *entry;
  366. struct cpu_mask_set *set = NULL;
  367. struct sdma_engine *sde = NULL;
  368. struct hfi1_ctxtdata *rcd = NULL;
  369. char extra[64];
  370. int cpu = -1;
  371. extra[0] = '\0';
  372. cpumask_clear(&msix->mask);
  373. ret = zalloc_cpumask_var(&diff, GFP_KERNEL);
  374. if (!ret)
  375. return -ENOMEM;
  376. entry = node_affinity_lookup(dd->node);
  377. switch (msix->type) {
  378. case IRQ_SDMA:
  379. sde = (struct sdma_engine *)msix->arg;
  380. scnprintf(extra, 64, "engine %u", sde->this_idx);
  381. set = &entry->def_intr;
  382. break;
  383. case IRQ_GENERAL:
  384. cpu = cpumask_first(&entry->general_intr_mask);
  385. break;
  386. case IRQ_RCVCTXT:
  387. rcd = (struct hfi1_ctxtdata *)msix->arg;
  388. if (rcd->ctxt == HFI1_CTRL_CTXT)
  389. cpu = cpumask_first(&entry->general_intr_mask);
  390. else
  391. set = &entry->rcv_intr;
  392. scnprintf(extra, 64, "ctxt %u", rcd->ctxt);
  393. break;
  394. default:
  395. dd_dev_err(dd, "Invalid IRQ type %d\n", msix->type);
  396. return -EINVAL;
  397. }
  398. /*
  399. * The general and control contexts are placed on a particular
  400. * CPU, which is set above. Skip accounting for it. Everything else
  401. * finds its CPU here.
  402. */
  403. if (cpu == -1 && set) {
  404. if (cpumask_equal(&set->mask, &set->used)) {
  405. /*
  406. * We've used up all the CPUs, bump up the generation
  407. * and reset the 'used' map
  408. */
  409. set->gen++;
  410. cpumask_clear(&set->used);
  411. }
  412. cpumask_andnot(diff, &set->mask, &set->used);
  413. cpu = cpumask_first(diff);
  414. cpumask_set_cpu(cpu, &set->used);
  415. }
  416. cpumask_set_cpu(cpu, &msix->mask);
  417. dd_dev_info(dd, "IRQ vector: %u, type %s %s -> cpu: %d\n",
  418. msix->msix.vector, irq_type_names[msix->type],
  419. extra, cpu);
  420. irq_set_affinity_hint(msix->msix.vector, &msix->mask);
  421. if (msix->type == IRQ_SDMA) {
  422. sde->cpu = cpu;
  423. hfi1_setup_sdma_notifier(msix);
  424. }
  425. free_cpumask_var(diff);
  426. return 0;
  427. }
  428. int hfi1_get_irq_affinity(struct hfi1_devdata *dd, struct hfi1_msix_entry *msix)
  429. {
  430. int ret;
  431. mutex_lock(&node_affinity.lock);
  432. ret = get_irq_affinity(dd, msix);
  433. mutex_unlock(&node_affinity.lock);
  434. return ret;
  435. }
  436. void hfi1_put_irq_affinity(struct hfi1_devdata *dd,
  437. struct hfi1_msix_entry *msix)
  438. {
  439. struct cpu_mask_set *set = NULL;
  440. struct hfi1_ctxtdata *rcd;
  441. struct hfi1_affinity_node *entry;
  442. mutex_lock(&node_affinity.lock);
  443. entry = node_affinity_lookup(dd->node);
  444. switch (msix->type) {
  445. case IRQ_SDMA:
  446. set = &entry->def_intr;
  447. hfi1_cleanup_sdma_notifier(msix);
  448. break;
  449. case IRQ_GENERAL:
  450. /* Don't do accounting for general contexts */
  451. break;
  452. case IRQ_RCVCTXT:
  453. rcd = (struct hfi1_ctxtdata *)msix->arg;
  454. /* Don't do accounting for control contexts */
  455. if (rcd->ctxt != HFI1_CTRL_CTXT)
  456. set = &entry->rcv_intr;
  457. break;
  458. default:
  459. mutex_unlock(&node_affinity.lock);
  460. return;
  461. }
  462. if (set) {
  463. cpumask_andnot(&set->used, &set->used, &msix->mask);
  464. if (cpumask_empty(&set->used) && set->gen) {
  465. set->gen--;
  466. cpumask_copy(&set->used, &set->mask);
  467. }
  468. }
  469. irq_set_affinity_hint(msix->msix.vector, NULL);
  470. cpumask_clear(&msix->mask);
  471. mutex_unlock(&node_affinity.lock);
  472. }
  473. /* This should be called with node_affinity.lock held */
  474. static void find_hw_thread_mask(uint hw_thread_no, cpumask_var_t hw_thread_mask,
  475. struct hfi1_affinity_node_list *affinity)
  476. {
  477. int possible, curr_cpu, i;
  478. uint num_cores_per_socket = node_affinity.num_online_cpus /
  479. affinity->num_core_siblings /
  480. node_affinity.num_online_nodes;
  481. cpumask_copy(hw_thread_mask, &affinity->proc.mask);
  482. if (affinity->num_core_siblings > 0) {
  483. /* Removing other siblings not needed for now */
  484. possible = cpumask_weight(hw_thread_mask);
  485. curr_cpu = cpumask_first(hw_thread_mask);
  486. for (i = 0;
  487. i < num_cores_per_socket * node_affinity.num_online_nodes;
  488. i++)
  489. curr_cpu = cpumask_next(curr_cpu, hw_thread_mask);
  490. for (; i < possible; i++) {
  491. cpumask_clear_cpu(curr_cpu, hw_thread_mask);
  492. curr_cpu = cpumask_next(curr_cpu, hw_thread_mask);
  493. }
  494. /* Identifying correct HW threads within physical cores */
  495. cpumask_shift_left(hw_thread_mask, hw_thread_mask,
  496. num_cores_per_socket *
  497. node_affinity.num_online_nodes *
  498. hw_thread_no);
  499. }
  500. }
  501. int hfi1_get_proc_affinity(int node)
  502. {
  503. int cpu = -1, ret, i;
  504. struct hfi1_affinity_node *entry;
  505. cpumask_var_t diff, hw_thread_mask, available_mask, intrs_mask;
  506. const struct cpumask *node_mask,
  507. *proc_mask = tsk_cpus_allowed(current);
  508. struct hfi1_affinity_node_list *affinity = &node_affinity;
  509. struct cpu_mask_set *set = &affinity->proc;
  510. /*
  511. * check whether process/context affinity has already
  512. * been set
  513. */
  514. if (cpumask_weight(proc_mask) == 1) {
  515. hfi1_cdbg(PROC, "PID %u %s affinity set to CPU %*pbl",
  516. current->pid, current->comm,
  517. cpumask_pr_args(proc_mask));
  518. /*
  519. * Mark the pre-set CPU as used. This is atomic so we don't
  520. * need the lock
  521. */
  522. cpu = cpumask_first(proc_mask);
  523. cpumask_set_cpu(cpu, &set->used);
  524. goto done;
  525. } else if (cpumask_weight(proc_mask) < cpumask_weight(&set->mask)) {
  526. hfi1_cdbg(PROC, "PID %u %s affinity set to CPU set(s) %*pbl",
  527. current->pid, current->comm,
  528. cpumask_pr_args(proc_mask));
  529. goto done;
  530. }
  531. /*
  532. * The process does not have a preset CPU affinity so find one to
  533. * recommend using the following algorithm:
  534. *
  535. * For each user process that is opening a context on HFI Y:
  536. * a) If all cores are filled, reinitialize the bitmask
  537. * b) Fill real cores first, then HT cores (First set of HT
  538. * cores on all physical cores, then second set of HT core,
  539. * and, so on) in the following order:
  540. *
  541. * 1. Same NUMA node as HFI Y and not running an IRQ
  542. * handler
  543. * 2. Same NUMA node as HFI Y and running an IRQ handler
  544. * 3. Different NUMA node to HFI Y and not running an IRQ
  545. * handler
  546. * 4. Different NUMA node to HFI Y and running an IRQ
  547. * handler
  548. * c) Mark core as filled in the bitmask. As user processes are
  549. * done, clear cores from the bitmask.
  550. */
  551. ret = zalloc_cpumask_var(&diff, GFP_KERNEL);
  552. if (!ret)
  553. goto done;
  554. ret = zalloc_cpumask_var(&hw_thread_mask, GFP_KERNEL);
  555. if (!ret)
  556. goto free_diff;
  557. ret = zalloc_cpumask_var(&available_mask, GFP_KERNEL);
  558. if (!ret)
  559. goto free_hw_thread_mask;
  560. ret = zalloc_cpumask_var(&intrs_mask, GFP_KERNEL);
  561. if (!ret)
  562. goto free_available_mask;
  563. mutex_lock(&affinity->lock);
  564. /*
  565. * If we've used all available HW threads, clear the mask and start
  566. * overloading.
  567. */
  568. if (cpumask_equal(&set->mask, &set->used)) {
  569. set->gen++;
  570. cpumask_clear(&set->used);
  571. }
  572. /*
  573. * If NUMA node has CPUs used by interrupt handlers, include them in the
  574. * interrupt handler mask.
  575. */
  576. entry = node_affinity_lookup(node);
  577. if (entry) {
  578. cpumask_copy(intrs_mask, (entry->def_intr.gen ?
  579. &entry->def_intr.mask :
  580. &entry->def_intr.used));
  581. cpumask_or(intrs_mask, intrs_mask, (entry->rcv_intr.gen ?
  582. &entry->rcv_intr.mask :
  583. &entry->rcv_intr.used));
  584. cpumask_or(intrs_mask, intrs_mask, &entry->general_intr_mask);
  585. }
  586. hfi1_cdbg(PROC, "CPUs used by interrupts: %*pbl",
  587. cpumask_pr_args(intrs_mask));
  588. cpumask_copy(hw_thread_mask, &set->mask);
  589. /*
  590. * If HT cores are enabled, identify which HW threads within the
  591. * physical cores should be used.
  592. */
  593. if (affinity->num_core_siblings > 0) {
  594. for (i = 0; i < affinity->num_core_siblings; i++) {
  595. find_hw_thread_mask(i, hw_thread_mask, affinity);
  596. /*
  597. * If there's at least one available core for this HW
  598. * thread number, stop looking for a core.
  599. *
  600. * diff will always be not empty at least once in this
  601. * loop as the used mask gets reset when
  602. * (set->mask == set->used) before this loop.
  603. */
  604. cpumask_andnot(diff, hw_thread_mask, &set->used);
  605. if (!cpumask_empty(diff))
  606. break;
  607. }
  608. }
  609. hfi1_cdbg(PROC, "Same available HW thread on all physical CPUs: %*pbl",
  610. cpumask_pr_args(hw_thread_mask));
  611. node_mask = cpumask_of_node(node);
  612. hfi1_cdbg(PROC, "Device on NUMA %u, CPUs %*pbl", node,
  613. cpumask_pr_args(node_mask));
  614. /* Get cpumask of available CPUs on preferred NUMA */
  615. cpumask_and(available_mask, hw_thread_mask, node_mask);
  616. cpumask_andnot(available_mask, available_mask, &set->used);
  617. hfi1_cdbg(PROC, "Available CPUs on NUMA %u: %*pbl", node,
  618. cpumask_pr_args(available_mask));
  619. /*
  620. * At first, we don't want to place processes on the same
  621. * CPUs as interrupt handlers. Then, CPUs running interrupt
  622. * handlers are used.
  623. *
  624. * 1) If diff is not empty, then there are CPUs not running
  625. * non-interrupt handlers available, so diff gets copied
  626. * over to available_mask.
  627. * 2) If diff is empty, then all CPUs not running interrupt
  628. * handlers are taken, so available_mask contains all
  629. * available CPUs running interrupt handlers.
  630. * 3) If available_mask is empty, then all CPUs on the
  631. * preferred NUMA node are taken, so other NUMA nodes are
  632. * used for process assignments using the same method as
  633. * the preferred NUMA node.
  634. */
  635. cpumask_andnot(diff, available_mask, intrs_mask);
  636. if (!cpumask_empty(diff))
  637. cpumask_copy(available_mask, diff);
  638. /* If we don't have CPUs on the preferred node, use other NUMA nodes */
  639. if (cpumask_empty(available_mask)) {
  640. cpumask_andnot(available_mask, hw_thread_mask, &set->used);
  641. /* Excluding preferred NUMA cores */
  642. cpumask_andnot(available_mask, available_mask, node_mask);
  643. hfi1_cdbg(PROC,
  644. "Preferred NUMA node cores are taken, cores available in other NUMA nodes: %*pbl",
  645. cpumask_pr_args(available_mask));
  646. /*
  647. * At first, we don't want to place processes on the same
  648. * CPUs as interrupt handlers.
  649. */
  650. cpumask_andnot(diff, available_mask, intrs_mask);
  651. if (!cpumask_empty(diff))
  652. cpumask_copy(available_mask, diff);
  653. }
  654. hfi1_cdbg(PROC, "Possible CPUs for process: %*pbl",
  655. cpumask_pr_args(available_mask));
  656. cpu = cpumask_first(available_mask);
  657. if (cpu >= nr_cpu_ids) /* empty */
  658. cpu = -1;
  659. else
  660. cpumask_set_cpu(cpu, &set->used);
  661. mutex_unlock(&affinity->lock);
  662. hfi1_cdbg(PROC, "Process assigned to CPU %d", cpu);
  663. free_cpumask_var(intrs_mask);
  664. free_available_mask:
  665. free_cpumask_var(available_mask);
  666. free_hw_thread_mask:
  667. free_cpumask_var(hw_thread_mask);
  668. free_diff:
  669. free_cpumask_var(diff);
  670. done:
  671. return cpu;
  672. }
  673. void hfi1_put_proc_affinity(int cpu)
  674. {
  675. struct hfi1_affinity_node_list *affinity = &node_affinity;
  676. struct cpu_mask_set *set = &affinity->proc;
  677. if (cpu < 0)
  678. return;
  679. mutex_lock(&affinity->lock);
  680. cpumask_clear_cpu(cpu, &set->used);
  681. hfi1_cdbg(PROC, "Returning CPU %d for future process assignment", cpu);
  682. if (cpumask_empty(&set->used) && set->gen) {
  683. set->gen--;
  684. cpumask_copy(&set->used, &set->mask);
  685. }
  686. mutex_unlock(&affinity->lock);
  687. }