4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
19 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
20 * Copyright (c) 2012, 2017, Intel Corporation.
23 * This file is part of Lustre, http://www.lustre.org/
25 * Please see comments in libcfs/include/libcfs/libcfs_cpu.h for introduction
27 * Author: liang@whamcloud.com
30 #define DEBUG_SUBSYSTEM S_LNET
32 #include <linux/cpu.h>
33 #include <linux/sched.h>
34 #include <libcfs/libcfs.h>
35 #include <libcfs/libcfs_cpu.h>
37 /** virtual processing unit */
38 struct cfs_cpu_partition {
39 /* CPUs mask for this partition */
40 cpumask_var_t cpt_cpumask;
41 /* nodes mask for this partition */
42 nodemask_t *cpt_nodemask;
43 /* NUMA distance between CPTs */
44 unsigned int *cpt_distance;
45 /* spread rotor for NUMA allocator */
46 unsigned int cpt_spread_rotor;
47 /* NUMA node if cpt_nodemask is empty */
51 /** descriptor for CPU partitions */
52 struct cfs_cpt_table {
53 /* spread rotor for NUMA allocator */
54 unsigned int ctb_spread_rotor;
55 /* maximum NUMA distance between all nodes in table */
56 unsigned int ctb_distance;
57 /* # of CPU partitions */
59 /* partitions tables */
60 struct cfs_cpu_partition *ctb_parts;
61 /* shadow HW CPU to CPU partition ID */
63 /* all cpus in this partition table */
64 cpumask_var_t ctb_cpumask;
65 /* shadow HW node to CPU partition ID */
67 /* all nodes in this partition table */
68 nodemask_t *ctb_nodemask;
71 /** Global CPU partition table */
72 struct cfs_cpt_table *cfs_cpt_tab __read_mostly;
73 EXPORT_SYMBOL(cfs_cpt_tab);
76 * modparam for setting number of partitions
78 * 0 : estimate best value based on cores or NUMA nodes
79 * 1 : disable multiple partitions
80 * >1 : specify number of partitions
82 static int cpu_npartitions;
83 module_param(cpu_npartitions, int, 0444);
84 MODULE_PARM_DESC(cpu_npartitions, "# of CPU partitions");
87 * modparam for setting CPU partitions patterns:
89 * i.e: "0[0,1,2,3] 1[4,5,6,7]", number before bracket is CPU partition ID,
90 * number in bracket is processor ID (core or HT)
92 * i.e: "N 0[0,1] 1[2,3]" the first character 'N' means numbers in bracket
93 * are NUMA node ID, number before bracket is CPU partition ID.
95 * i.e: "N", shortcut expression to create CPT from NUMA & CPU topology
97 * NB: If user specified cpu_pattern, cpu_npartitions will be ignored
99 static char *cpu_pattern = "N";
100 module_param(cpu_pattern, charp, 0444);
101 MODULE_PARM_DESC(cpu_pattern, "CPU partitions pattern");
103 struct cfs_cpt_table *cfs_cpt_table_alloc(int ncpt)
105 struct cfs_cpt_table *cptab;
108 LIBCFS_ALLOC(cptab, sizeof(*cptab));
112 cptab->ctb_nparts = ncpt;
114 if (!zalloc_cpumask_var(&cptab->ctb_cpumask, GFP_NOFS))
115 goto failed_alloc_cpumask;
117 LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
118 if (!cptab->ctb_nodemask)
119 goto failed_alloc_nodemask;
121 CFS_ALLOC_PTR_ARRAY(cptab->ctb_cpu2cpt, nr_cpu_ids);
122 if (!cptab->ctb_cpu2cpt)
123 goto failed_alloc_cpu2cpt;
125 memset(cptab->ctb_cpu2cpt, -1,
126 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
128 CFS_ALLOC_PTR_ARRAY(cptab->ctb_node2cpt, nr_node_ids);
129 if (!cptab->ctb_node2cpt)
130 goto failed_alloc_node2cpt;
132 memset(cptab->ctb_node2cpt, -1,
133 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
135 CFS_ALLOC_PTR_ARRAY(cptab->ctb_parts, ncpt);
136 if (!cptab->ctb_parts)
137 goto failed_alloc_ctb_parts;
139 memset(cptab->ctb_parts, -1, ncpt * sizeof(cptab->ctb_parts[0]));
141 for (i = 0; i < ncpt; i++) {
142 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
144 if (!zalloc_cpumask_var(&part->cpt_cpumask, GFP_NOFS))
145 goto failed_setting_ctb_parts;
147 LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
148 if (!part->cpt_nodemask)
149 goto failed_setting_ctb_parts;
151 CFS_ALLOC_PTR_ARRAY(part->cpt_distance, cptab->ctb_nparts);
152 if (!part->cpt_distance)
153 goto failed_setting_ctb_parts;
155 memset(part->cpt_distance, -1,
156 cptab->ctb_nparts * sizeof(part->cpt_distance[0]));
161 failed_setting_ctb_parts:
163 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
165 if (part->cpt_nodemask) {
166 LIBCFS_FREE(part->cpt_nodemask,
167 sizeof(*part->cpt_nodemask));
170 free_cpumask_var(part->cpt_cpumask);
172 if (part->cpt_distance) {
173 CFS_FREE_PTR_ARRAY(part->cpt_distance,
178 if (cptab->ctb_parts)
179 CFS_FREE_PTR_ARRAY(cptab->ctb_parts, cptab->ctb_nparts);
181 failed_alloc_ctb_parts:
182 if (cptab->ctb_node2cpt)
183 CFS_FREE_PTR_ARRAY(cptab->ctb_node2cpt, nr_node_ids);
185 failed_alloc_node2cpt:
186 if (cptab->ctb_cpu2cpt)
187 CFS_FREE_PTR_ARRAY(cptab->ctb_cpu2cpt, nr_cpu_ids);
189 failed_alloc_cpu2cpt:
190 if (cptab->ctb_nodemask)
191 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
192 failed_alloc_nodemask:
193 free_cpumask_var(cptab->ctb_cpumask);
194 failed_alloc_cpumask:
195 LIBCFS_FREE(cptab, sizeof(*cptab));
198 EXPORT_SYMBOL(cfs_cpt_table_alloc);
200 void cfs_cpt_table_free(struct cfs_cpt_table *cptab)
204 if (cptab->ctb_cpu2cpt)
205 CFS_FREE_PTR_ARRAY(cptab->ctb_cpu2cpt, nr_cpu_ids);
207 if (cptab->ctb_node2cpt)
208 CFS_FREE_PTR_ARRAY(cptab->ctb_node2cpt, nr_node_ids);
210 for (i = 0; cptab->ctb_parts && i < cptab->ctb_nparts; i++) {
211 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
213 if (part->cpt_nodemask) {
214 LIBCFS_FREE(part->cpt_nodemask,
215 sizeof(*part->cpt_nodemask));
218 free_cpumask_var(part->cpt_cpumask);
220 if (part->cpt_distance)
221 CFS_FREE_PTR_ARRAY(part->cpt_distance,
225 if (cptab->ctb_parts)
226 CFS_FREE_PTR_ARRAY(cptab->ctb_parts, cptab->ctb_nparts);
228 if (cptab->ctb_nodemask)
229 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
230 free_cpumask_var(cptab->ctb_cpumask);
232 LIBCFS_FREE(cptab, sizeof(*cptab));
234 EXPORT_SYMBOL(cfs_cpt_table_free);
236 int cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
243 for (i = 0; i < cptab->ctb_nparts; i++) {
247 rc = snprintf(tmp, len, "%d\t:", i);
254 for_each_cpu(j, cptab->ctb_parts[i].cpt_cpumask) {
255 rc = snprintf(tmp, len, " %d", j);
271 EXPORT_SYMBOL(cfs_cpt_table_print);
273 int cfs_cpt_distance_print(struct cfs_cpt_table *cptab, char *buf, int len)
280 for (i = 0; i < cptab->ctb_nparts; i++) {
284 rc = snprintf(tmp, len, "%d\t:", i);
291 for (j = 0; j < cptab->ctb_nparts; j++) {
292 rc = snprintf(tmp, len, " %d:%d", j,
293 cptab->ctb_parts[i].cpt_distance[j]);
309 EXPORT_SYMBOL(cfs_cpt_distance_print);
311 int cfs_cpt_number(struct cfs_cpt_table *cptab)
313 return cptab->ctb_nparts;
315 EXPORT_SYMBOL(cfs_cpt_number);
317 int cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
319 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
321 return cpt == CFS_CPT_ANY ?
322 cpumask_weight(cptab->ctb_cpumask) :
323 cpumask_weight(cptab->ctb_parts[cpt].cpt_cpumask);
325 EXPORT_SYMBOL(cfs_cpt_weight);
327 int cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
329 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
331 return cpt == CFS_CPT_ANY ?
332 cpumask_any_and(cptab->ctb_cpumask,
333 cpu_online_mask) < nr_cpu_ids :
334 cpumask_any_and(cptab->ctb_parts[cpt].cpt_cpumask,
335 cpu_online_mask) < nr_cpu_ids;
337 EXPORT_SYMBOL(cfs_cpt_online);
339 cpumask_var_t *cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
341 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
343 return cpt == CFS_CPT_ANY ?
344 &cptab->ctb_cpumask : &cptab->ctb_parts[cpt].cpt_cpumask;
346 EXPORT_SYMBOL(cfs_cpt_cpumask);
348 nodemask_t *cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
350 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
352 return cpt == CFS_CPT_ANY ?
353 cptab->ctb_nodemask : cptab->ctb_parts[cpt].cpt_nodemask;
355 EXPORT_SYMBOL(cfs_cpt_nodemask);
357 unsigned int cfs_cpt_distance(struct cfs_cpt_table *cptab, int cpt1, int cpt2)
359 LASSERT(cpt1 == CFS_CPT_ANY || (cpt1 >= 0 && cpt1 < cptab->ctb_nparts));
360 LASSERT(cpt2 == CFS_CPT_ANY || (cpt2 >= 0 && cpt2 < cptab->ctb_nparts));
362 if (cpt1 == CFS_CPT_ANY || cpt2 == CFS_CPT_ANY)
363 return cptab->ctb_distance;
365 return cptab->ctb_parts[cpt1].cpt_distance[cpt2];
367 EXPORT_SYMBOL(cfs_cpt_distance);
370 * Calculate the maximum NUMA distance between all nodes in the
371 * from_mask and all nodes in the to_mask.
373 static unsigned int cfs_cpt_distance_calculate(nodemask_t *from_mask,
376 unsigned int maximum;
377 unsigned int distance;
382 for_each_node_mask(from, *from_mask) {
383 for_each_node_mask(to, *to_mask) {
384 distance = node_distance(from, to);
385 if (maximum < distance)
392 static void cfs_cpt_add_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
394 cptab->ctb_cpu2cpt[cpu] = cpt;
396 cpumask_set_cpu(cpu, cptab->ctb_cpumask);
397 cpumask_set_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
400 static void cfs_cpt_del_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
402 cpumask_clear_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
403 cpumask_clear_cpu(cpu, cptab->ctb_cpumask);
405 cptab->ctb_cpu2cpt[cpu] = -1;
408 static void cfs_cpt_add_node(struct cfs_cpt_table *cptab, int cpt, int node)
410 struct cfs_cpu_partition *part;
412 if (!node_isset(node, *cptab->ctb_nodemask)) {
415 /* first time node is added to the CPT table */
416 node_set(node, *cptab->ctb_nodemask);
417 cptab->ctb_node2cpt[node] = cpt;
419 dist = cfs_cpt_distance_calculate(cptab->ctb_nodemask,
420 cptab->ctb_nodemask);
421 cptab->ctb_distance = dist;
424 part = &cptab->ctb_parts[cpt];
425 if (!node_isset(node, *part->cpt_nodemask)) {
428 /* first time node is added to this CPT */
429 node_set(node, *part->cpt_nodemask);
430 for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
431 struct cfs_cpu_partition *part2;
434 part2 = &cptab->ctb_parts[cpt2];
435 dist = cfs_cpt_distance_calculate(part->cpt_nodemask,
436 part2->cpt_nodemask);
437 part->cpt_distance[cpt2] = dist;
438 dist = cfs_cpt_distance_calculate(part2->cpt_nodemask,
440 part2->cpt_distance[cpt] = dist;
445 static void cfs_cpt_del_node(struct cfs_cpt_table *cptab, int cpt, int node)
447 struct cfs_cpu_partition *part = &cptab->ctb_parts[cpt];
450 for_each_cpu(cpu, part->cpt_cpumask) {
451 /* this CPT has other CPU belonging to this node? */
452 if (cpu_to_node(cpu) == node)
456 if (cpu >= nr_cpu_ids && node_isset(node, *part->cpt_nodemask)) {
459 /* No more CPUs in the node for this CPT. */
460 node_clear(node, *part->cpt_nodemask);
461 for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
462 struct cfs_cpu_partition *part2;
465 part2 = &cptab->ctb_parts[cpt2];
466 if (node_isset(node, *part2->cpt_nodemask))
467 cptab->ctb_node2cpt[node] = cpt2;
469 dist = cfs_cpt_distance_calculate(part->cpt_nodemask,
470 part2->cpt_nodemask);
471 part->cpt_distance[cpt2] = dist;
472 dist = cfs_cpt_distance_calculate(part2->cpt_nodemask,
474 part2->cpt_distance[cpt] = dist;
478 for_each_cpu(cpu, cptab->ctb_cpumask) {
479 /* this CPT-table has other CPUs belonging to this node? */
480 if (cpu_to_node(cpu) == node)
484 if (cpu >= nr_cpu_ids && node_isset(node, *cptab->ctb_nodemask)) {
485 /* No more CPUs in the table for this node. */
486 node_clear(node, *cptab->ctb_nodemask);
487 cptab->ctb_node2cpt[node] = -1;
488 cptab->ctb_distance =
489 cfs_cpt_distance_calculate(cptab->ctb_nodemask,
490 cptab->ctb_nodemask);
494 int cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
496 LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
498 if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu)) {
499 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
503 if (cptab->ctb_cpu2cpt[cpu] != -1) {
504 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
505 cpu, cptab->ctb_cpu2cpt[cpu]);
509 if (cpumask_test_cpu(cpu, cptab->ctb_cpumask)) {
510 CDEBUG(D_INFO, "CPU %d is already in cpumask\n", cpu);
514 if (cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask)) {
515 CDEBUG(D_INFO, "CPU %d is already in partition %d cpumask\n",
516 cpu, cptab->ctb_cpu2cpt[cpu]);
520 cfs_cpt_add_cpu(cptab, cpt, cpu);
521 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
525 EXPORT_SYMBOL(cfs_cpt_set_cpu);
527 void cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
529 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
531 if (cpu < 0 || cpu >= nr_cpu_ids) {
532 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
536 if (cpt == CFS_CPT_ANY) {
537 /* caller doesn't know the partition ID */
538 cpt = cptab->ctb_cpu2cpt[cpu];
539 if (cpt < 0) { /* not set in this CPT-table */
541 "Try to unset cpu %d which is not in CPT-table %p\n",
546 } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
548 "CPU %d is not in CPU partition %d\n", cpu, cpt);
552 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
553 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_cpumask));
555 cfs_cpt_del_cpu(cptab, cpt, cpu);
556 cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
558 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
560 int cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt,
561 const cpumask_t *mask)
565 if (!cpumask_weight(mask) ||
566 cpumask_any_and(mask, cpu_online_mask) >= nr_cpu_ids) {
568 "No online CPU is found in the CPU mask for CPU partition %d\n",
573 for_each_cpu(cpu, mask) {
574 cfs_cpt_add_cpu(cptab, cpt, cpu);
575 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
580 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
582 void cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt,
583 const cpumask_t *mask)
587 for_each_cpu(cpu, mask) {
588 cfs_cpt_del_cpu(cptab, cpt, cpu);
589 cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
592 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
594 int cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
596 const cpumask_t *mask;
599 if (node < 0 || node >= nr_node_ids) {
601 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
605 mask = cpumask_of_node(node);
607 for_each_cpu(cpu, mask)
608 cfs_cpt_add_cpu(cptab, cpt, cpu);
610 cfs_cpt_add_node(cptab, cpt, node);
614 EXPORT_SYMBOL(cfs_cpt_set_node);
616 void cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
618 const cpumask_t *mask;
621 if (node < 0 || node >= nr_node_ids) {
623 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
627 mask = cpumask_of_node(node);
629 for_each_cpu(cpu, mask)
630 cfs_cpt_del_cpu(cptab, cpt, cpu);
632 cfs_cpt_del_node(cptab, cpt, node);
634 EXPORT_SYMBOL(cfs_cpt_unset_node);
636 int cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt,
637 const nodemask_t *mask)
641 for_each_node_mask(node, *mask)
642 cfs_cpt_set_node(cptab, cpt, node);
646 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
648 void cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt,
649 const nodemask_t *mask)
653 for_each_node_mask(node, *mask)
654 cfs_cpt_unset_node(cptab, cpt, node);
656 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
658 int cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
665 /* convert CPU partition ID to HW node id */
667 if (cpt < 0 || cpt >= cptab->ctb_nparts) {
668 mask = cptab->ctb_nodemask;
669 rotor = cptab->ctb_spread_rotor++;
671 mask = cptab->ctb_parts[cpt].cpt_nodemask;
672 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
673 node = cptab->ctb_parts[cpt].cpt_node;
676 weight = nodes_weight(*mask);
680 for_each_node_mask(node, *mask) {
688 EXPORT_SYMBOL(cfs_cpt_spread_node);
690 int cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
696 cpu = smp_processor_id();
697 cpt = cptab->ctb_cpu2cpt[cpu];
699 if (cpt < 0 && remap) {
700 /* don't return negative value for safety of upper layer,
701 * instead we shadow the unknown cpu to a valid partition ID
703 cpt = cpu % cptab->ctb_nparts;
708 EXPORT_SYMBOL(cfs_cpt_current);
710 int cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
712 LASSERT(cpu >= 0 && cpu < nr_cpu_ids);
714 return cptab->ctb_cpu2cpt[cpu];
716 EXPORT_SYMBOL(cfs_cpt_of_cpu);
718 int cfs_cpt_of_node(struct cfs_cpt_table *cptab, int node)
720 if (node < 0 || node > nr_node_ids)
723 return cptab->ctb_node2cpt[node];
725 EXPORT_SYMBOL(cfs_cpt_of_node);
727 int cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
729 nodemask_t *nodemask;
734 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
736 if (cpt == CFS_CPT_ANY) {
737 cpumask = cptab->ctb_cpumask;
738 nodemask = cptab->ctb_nodemask;
740 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
741 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
744 if (!cpumask_intersects(cpumask, cpu_online_mask)) {
746 "No online CPU found in CPU partition %d, did someone do CPU hotplug on system? You might need to reload Lustre modules to keep system working well.\n",
751 for_each_online_cpu(cpu) {
752 if (cpumask_test_cpu(cpu, cpumask))
755 rc = set_cpus_allowed_ptr(current, cpumask);
756 set_mems_allowed(*nodemask);
758 schedule(); /* switch to allowed CPU */
763 /* don't need to set affinity because all online CPUs are covered */
766 EXPORT_SYMBOL(cfs_cpt_bind);
769 * Choose max to \a number CPUs from \a node and set them in \a cpt.
770 * We always prefer to choose CPU in the same core/socket.
772 static int cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
773 cpumask_t *node_mask, int number)
775 cpumask_var_t socket_mask;
776 cpumask_var_t core_mask;
783 if (number >= cpumask_weight(node_mask)) {
784 while (!cpumask_empty(node_mask)) {
785 cpu = cpumask_first(node_mask);
786 cpumask_clear_cpu(cpu, node_mask);
788 if (!cpu_online(cpu))
791 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
799 * Allocate scratch buffers
800 * As we cannot initialize a cpumask_var_t, we need
801 * to alloc both before we can risk trying to free either
803 if (!zalloc_cpumask_var(&socket_mask, GFP_NOFS))
805 if (!zalloc_cpumask_var(&core_mask, GFP_NOFS))
810 while (!cpumask_empty(node_mask)) {
811 cpu = cpumask_first(node_mask);
813 /* get cpumask for cores in the same socket */
814 cpumask_and(socket_mask, topology_core_cpumask(cpu), node_mask);
815 while (!cpumask_empty(socket_mask)) {
816 /* get cpumask for hts in the same core */
817 cpumask_and(core_mask, topology_sibling_cpumask(cpu),
820 for_each_cpu(i, core_mask) {
821 cpumask_clear_cpu(i, socket_mask);
822 cpumask_clear_cpu(i, node_mask);
827 rc = cfs_cpt_set_cpu(cptab, cpt, i);
836 cpu = cpumask_first(socket_mask);
841 free_cpumask_var(socket_mask);
842 free_cpumask_var(core_mask);
846 #define CPT_WEIGHT_MIN 4u
848 static unsigned int cfs_cpt_num_estimate(void)
851 unsigned int ncpu = num_online_cpus();
852 unsigned int ncpt = 1;
855 nthr = cpumask_weight(topology_sibling_cpumask(smp_processor_id()));
858 if (ncpu > CPT_WEIGHT_MIN)
859 for (ncpt = 2; ncpu > 2 * nthr * ncpt; ncpt++)
862 #if (BITS_PER_LONG == 32)
863 /* config many CPU partitions on 32-bit system could consume
866 ncpt = min(2U, ncpt);
869 ncpt--; /* worst case is 1 */
874 static struct cfs_cpt_table *cfs_cpt_table_create(int ncpt)
876 struct cfs_cpt_table *cptab = NULL;
877 cpumask_var_t node_mask;
884 num = cfs_cpt_num_estimate();
888 if (ncpt > num_online_cpus()) {
890 CERROR("libcfs: CPU partition count %d > cores %d: rc = %d\n",
891 ncpt, num_online_cpus(), rc);
895 if (ncpt > 4 * num) {
896 CWARN("CPU partition number %d is larger than suggested value (%d), your system may have performance issue or run out of memory while under pressure\n",
900 cptab = cfs_cpt_table_alloc(ncpt);
902 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
907 if (!zalloc_cpumask_var(&node_mask, GFP_NOFS)) {
908 CERROR("Failed to allocate scratch cpumask\n");
913 num = num_online_cpus() / ncpt;
914 rem = num_online_cpus() % ncpt;
915 for_each_online_node(node) {
916 cpumask_copy(node_mask, cpumask_of_node(node));
918 while (cpt < ncpt && !cpumask_empty(node_mask)) {
919 struct cfs_cpu_partition *part = &cptab->ctb_parts[cpt];
920 int ncpu = cpumask_weight(part->cpt_cpumask);
922 rc = cfs_cpt_choose_ncpus(cptab, cpt, node_mask,
923 (rem > 0) + num - ncpu);
929 ncpu = cpumask_weight(part->cpt_cpumask);
930 if (ncpu == num + !!(rem > 0)) {
937 free_cpumask_var(node_mask);
942 free_cpumask_var(node_mask);
944 CERROR("Failed (rc = %d) to setup CPU partition table with %d partitions, online HW NUMA nodes: %d, HW CPU cores: %d.\n",
945 rc, ncpt, num_online_nodes(), num_online_cpus());
948 cfs_cpt_table_free(cptab);
953 static struct cfs_cpt_table *cfs_cpt_table_create_pattern(const char *pattern)
955 struct cfs_cpt_table *cptab;
967 pattern_dup = kstrdup(pattern, GFP_KERNEL);
969 CERROR("Failed to duplicate pattern '%s'\n", pattern);
970 return ERR_PTR(-ENOMEM);
973 str = strim(pattern_dup);
974 if (*str == 'n' || *str == 'N') {
975 str++; /* skip 'N' char */
976 node = 1; /* NUMA pattern */
979 for_each_online_node(i) {
980 if (!cpumask_empty(cpumask_of_node(i)))
983 if (ncpt == 1) { /* single NUMA node */
985 return cfs_cpt_table_create(cpu_npartitions);
990 if (!ncpt) { /* scanning bracket which is mark of partition */
992 while ((bracket = strchr(bracket, '['))) {
999 (node && ncpt > num_online_nodes()) ||
1000 (!node && ncpt > num_online_cpus())) {
1001 CERROR("Invalid pattern '%s', or too many partitions %d\n",
1007 cptab = cfs_cpt_table_alloc(ncpt);
1009 CERROR("Failed to allocate CPU partition table\n");
1014 if (node < 0) { /* shortcut to create CPT from NUMA & CPU topology */
1015 for_each_online_node(i) {
1016 if (cpumask_empty(cpumask_of_node(i)))
1019 rc = cfs_cpt_set_node(cptab, cpt++, i);
1022 goto err_free_table;
1029 high = node ? nr_node_ids - 1 : nr_cpu_ids - 1;
1031 for (str = strim(str), c = 0; /* until break */; c++) {
1032 struct cfs_range_expr *range;
1033 struct cfs_expr_list *el;
1036 bracket = strchr(str, '[');
1039 CERROR("Invalid pattern '%s'\n", str);
1041 goto err_free_table;
1042 } else if (c != ncpt) {
1043 CERROR("Expect %d partitions but found %d\n",
1046 goto err_free_table;
1051 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
1052 CERROR("Invalid CPU pattern '%s'\n", str);
1054 goto err_free_table;
1057 if (cpt < 0 || cpt >= ncpt) {
1058 CERROR("Invalid partition id %d, total partitions %d\n",
1061 goto err_free_table;
1064 if (cfs_cpt_weight(cptab, cpt)) {
1065 CERROR("Partition %d has already been set.\n", cpt);
1067 goto err_free_table;
1070 str = strim(str + n);
1071 if (str != bracket) {
1072 CERROR("Invalid pattern '%s'\n", str);
1074 goto err_free_table;
1077 bracket = strchr(str, ']');
1079 CERROR("Missing right bracket for partition %d in '%s'\n",
1082 goto err_free_table;
1085 rc = cfs_expr_list_parse(str, (bracket - str) + 1, 0, high,
1088 CERROR("Can't parse number range in '%s'\n", str);
1090 goto err_free_table;
1093 list_for_each_entry(range, &el->el_exprs, re_link) {
1094 for (i = range->re_lo; i <= range->re_hi; i++) {
1095 if ((i - range->re_lo) % range->re_stride)
1098 rc = node ? cfs_cpt_set_node(cptab, cpt, i)
1099 : cfs_cpt_set_cpu(cptab, cpt, i);
1101 cfs_expr_list_free(el);
1103 goto err_free_table;
1108 cfs_expr_list_free(el);
1110 if (!cfs_cpt_online(cptab, cpt)) {
1111 CERROR("No online CPU is found on partition %d\n", cpt);
1113 goto err_free_table;
1116 str = strim(bracket + 1);
1123 cfs_cpt_table_free(cptab);
1129 #ifdef CONFIG_HOTPLUG_CPU
1130 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1131 static enum cpuhp_state lustre_cpu_online;
1133 static int cfs_cpu_online(unsigned int cpu)
1139 static int cfs_cpu_dead(unsigned int cpu)
1143 /* if all HTs in a core are offline, it may break affinity */
1144 warn = cpumask_any_and(topology_sibling_cpumask(cpu),
1145 cpu_online_mask) >= nr_cpu_ids;
1146 CDEBUG(warn ? D_WARNING : D_INFO,
1147 "Lustre: can't support CPU plug-out well now, performance and stability could be impacted [CPU %u]\n",
1152 #ifndef HAVE_HOTPLUG_STATE_MACHINE
1153 static int cfs_cpu_notify(struct notifier_block *self, unsigned long action,
1156 int cpu = (unsigned long)hcpu;
1160 case CPU_DEAD_FROZEN:
1162 case CPU_ONLINE_FROZEN:
1164 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
1165 CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
1176 static struct notifier_block cfs_cpu_notifier = {
1177 .notifier_call = cfs_cpu_notify,
1180 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1181 #endif /* CONFIG_HOTPLUG_CPU */
1183 void cfs_cpu_fini(void)
1185 if (!IS_ERR_OR_NULL(cfs_cpt_tab))
1186 cfs_cpt_table_free(cfs_cpt_tab);
1188 #ifdef CONFIG_HOTPLUG_CPU
1189 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1190 if (lustre_cpu_online > 0)
1191 cpuhp_remove_state_nocalls(lustre_cpu_online);
1192 cpuhp_remove_state_nocalls(CPUHP_LUSTRE_CFS_DEAD);
1194 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1195 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1196 #endif /* CONFIG_HOTPLUG_CPU */
1199 int cfs_cpu_init(void)
1203 LASSERT(!cfs_cpt_tab);
1205 #ifdef CONFIG_HOTPLUG_CPU
1206 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1207 ret = cpuhp_setup_state_nocalls(CPUHP_LUSTRE_CFS_DEAD,
1208 "fs/lustre/cfe:dead", NULL,
1211 goto failed_cpu_dead;
1213 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
1214 "fs/lustre/cfe:online",
1215 cfs_cpu_online, NULL);
1217 goto failed_cpu_online;
1219 lustre_cpu_online = ret;
1221 register_hotcpu_notifier(&cfs_cpu_notifier);
1222 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1223 #endif /* CONFIG_HOTPLUG_CPU */
1227 cfs_cpt_tab = cfs_cpt_table_create_pattern(cpu_pattern);
1228 if (IS_ERR(cfs_cpt_tab)) {
1229 CERROR("Failed to create cptab from pattern '%s'\n",
1231 ret = PTR_ERR(cfs_cpt_tab);
1232 goto failed_alloc_table;
1236 cfs_cpt_tab = cfs_cpt_table_create(cpu_npartitions);
1237 if (IS_ERR(cfs_cpt_tab)) {
1238 CERROR("Failed to create cptab with npartitions %d\n",
1240 ret = PTR_ERR(cfs_cpt_tab);
1241 goto failed_alloc_table;
1247 LCONSOLE(0, "HW NUMA nodes: %d, HW CPU cores: %d, npartitions: %d\n",
1248 num_online_nodes(), num_online_cpus(),
1249 cfs_cpt_number(cfs_cpt_tab));
1255 if (!IS_ERR_OR_NULL(cfs_cpt_tab))
1256 cfs_cpt_table_free(cfs_cpt_tab);
1258 #ifdef CONFIG_HOTPLUG_CPU
1259 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1260 if (lustre_cpu_online > 0)
1261 cpuhp_remove_state_nocalls(lustre_cpu_online);
1263 cpuhp_remove_state_nocalls(CPUHP_LUSTRE_CFS_DEAD);
1266 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1267 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1268 #endif /* CONFIG_HOTPLUG_CPU */