affinity.c 22 KB

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