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