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/
24 * Lustre is a trademark of Sun Microsystems, Inc.
26 * Please see comments in libcfs/include/libcfs/libcfs_cpu.h for introduction
28 * Author: liang@whamcloud.com
31 #define DEBUG_SUBSYSTEM S_LNET
33 #include <linux/cpu.h>
34 #include <linux/sched.h>
35 #include <libcfs/libcfs.h>
36 #include <libcfs/libcfs_cpu.h>
38 /** virtual processing unit */
39 struct cfs_cpu_partition {
40 /* CPUs mask for this partition */
41 cpumask_var_t cpt_cpumask;
42 /* nodes mask for this partition */
43 nodemask_t *cpt_nodemask;
44 /* NUMA distance between CPTs */
45 unsigned int *cpt_distance;
46 /* spread rotor for NUMA allocator */
47 unsigned int cpt_spread_rotor;
48 /* NUMA node if cpt_nodemask is empty */
52 /** descriptor for CPU partitions */
53 struct cfs_cpt_table {
54 /* spread rotor for NUMA allocator */
55 unsigned int ctb_spread_rotor;
56 /* maximum NUMA distance between all nodes in table */
57 unsigned int ctb_distance;
58 /* # of CPU partitions */
60 /* partitions tables */
61 struct cfs_cpu_partition *ctb_parts;
62 /* shadow HW CPU to CPU partition ID */
64 /* all cpus in this partition table */
65 cpumask_var_t ctb_cpumask;
66 /* shadow HW node to CPU partition ID */
68 /* all nodes in this partition table */
69 nodemask_t *ctb_nodemask;
72 /** Global CPU partition table */
73 struct cfs_cpt_table *cfs_cpt_tab __read_mostly;
74 EXPORT_SYMBOL(cfs_cpt_tab);
77 * modparam for setting number of partitions
79 * 0 : estimate best value based on cores or NUMA nodes
80 * 1 : disable multiple partitions
81 * >1 : specify number of partitions
83 static int cpu_npartitions;
84 module_param(cpu_npartitions, int, 0444);
85 MODULE_PARM_DESC(cpu_npartitions, "# of CPU partitions");
88 * modparam for setting CPU partitions patterns:
90 * i.e: "0[0,1,2,3] 1[4,5,6,7]", number before bracket is CPU partition ID,
91 * number in bracket is processor ID (core or HT)
93 * i.e: "N 0[0,1] 1[2,3]" the first character 'N' means numbers in bracket
94 * are NUMA node ID, number before bracket is CPU partition ID.
96 * i.e: "N", shortcut expression to create CPT from NUMA & CPU topology
98 * NB: If user specified cpu_pattern, cpu_npartitions will be ignored
100 static char *cpu_pattern = "N";
101 module_param(cpu_pattern, charp, 0444);
102 MODULE_PARM_DESC(cpu_pattern, "CPU partitions pattern");
104 struct cfs_cpt_table *cfs_cpt_table_alloc(int ncpt)
106 struct cfs_cpt_table *cptab;
109 LIBCFS_ALLOC(cptab, sizeof(*cptab));
113 cptab->ctb_nparts = ncpt;
115 if (!zalloc_cpumask_var(&cptab->ctb_cpumask, GFP_NOFS))
116 goto failed_alloc_cpumask;
118 LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
119 if (!cptab->ctb_nodemask)
120 goto failed_alloc_nodemask;
122 CFS_ALLOC_PTR_ARRAY(cptab->ctb_cpu2cpt, nr_cpu_ids);
123 if (!cptab->ctb_cpu2cpt)
124 goto failed_alloc_cpu2cpt;
126 memset(cptab->ctb_cpu2cpt, -1,
127 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
129 CFS_ALLOC_PTR_ARRAY(cptab->ctb_node2cpt, nr_node_ids);
130 if (!cptab->ctb_node2cpt)
131 goto failed_alloc_node2cpt;
133 memset(cptab->ctb_node2cpt, -1,
134 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
136 CFS_ALLOC_PTR_ARRAY(cptab->ctb_parts, ncpt);
137 if (!cptab->ctb_parts)
138 goto failed_alloc_ctb_parts;
140 memset(cptab->ctb_parts, -1, ncpt * sizeof(cptab->ctb_parts[0]));
142 for (i = 0; i < ncpt; i++) {
143 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
145 if (!zalloc_cpumask_var(&part->cpt_cpumask, GFP_NOFS))
146 goto failed_setting_ctb_parts;
148 LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
149 if (!part->cpt_nodemask)
150 goto failed_setting_ctb_parts;
152 CFS_ALLOC_PTR_ARRAY(part->cpt_distance, cptab->ctb_nparts);
153 if (!part->cpt_distance)
154 goto failed_setting_ctb_parts;
156 memset(part->cpt_distance, -1,
157 cptab->ctb_nparts * sizeof(part->cpt_distance[0]));
162 failed_setting_ctb_parts:
164 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
166 if (part->cpt_nodemask) {
167 LIBCFS_FREE(part->cpt_nodemask,
168 sizeof(*part->cpt_nodemask));
171 free_cpumask_var(part->cpt_cpumask);
173 if (part->cpt_distance) {
174 CFS_FREE_PTR_ARRAY(part->cpt_distance,
179 if (cptab->ctb_parts)
180 CFS_FREE_PTR_ARRAY(cptab->ctb_parts, cptab->ctb_nparts);
182 failed_alloc_ctb_parts:
183 if (cptab->ctb_node2cpt)
184 CFS_FREE_PTR_ARRAY(cptab->ctb_node2cpt, nr_node_ids);
186 failed_alloc_node2cpt:
187 if (cptab->ctb_cpu2cpt)
188 CFS_FREE_PTR_ARRAY(cptab->ctb_cpu2cpt, nr_cpu_ids);
190 failed_alloc_cpu2cpt:
191 if (cptab->ctb_nodemask)
192 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
193 failed_alloc_nodemask:
194 free_cpumask_var(cptab->ctb_cpumask);
195 failed_alloc_cpumask:
196 LIBCFS_FREE(cptab, sizeof(*cptab));
199 EXPORT_SYMBOL(cfs_cpt_table_alloc);
201 void cfs_cpt_table_free(struct cfs_cpt_table *cptab)
205 if (cptab->ctb_cpu2cpt)
206 CFS_FREE_PTR_ARRAY(cptab->ctb_cpu2cpt, nr_cpu_ids);
208 if (cptab->ctb_node2cpt)
209 CFS_FREE_PTR_ARRAY(cptab->ctb_node2cpt, nr_node_ids);
211 for (i = 0; cptab->ctb_parts && i < cptab->ctb_nparts; i++) {
212 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
214 if (part->cpt_nodemask) {
215 LIBCFS_FREE(part->cpt_nodemask,
216 sizeof(*part->cpt_nodemask));
219 free_cpumask_var(part->cpt_cpumask);
221 if (part->cpt_distance)
222 CFS_FREE_PTR_ARRAY(part->cpt_distance,
226 if (cptab->ctb_parts)
227 CFS_FREE_PTR_ARRAY(cptab->ctb_parts, cptab->ctb_nparts);
229 if (cptab->ctb_nodemask)
230 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
231 free_cpumask_var(cptab->ctb_cpumask);
233 LIBCFS_FREE(cptab, sizeof(*cptab));
235 EXPORT_SYMBOL(cfs_cpt_table_free);
237 int cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
244 for (i = 0; i < cptab->ctb_nparts; i++) {
248 rc = snprintf(tmp, len, "%d\t:", i);
255 for_each_cpu(j, cptab->ctb_parts[i].cpt_cpumask) {
256 rc = snprintf(tmp, len, " %d", j);
272 EXPORT_SYMBOL(cfs_cpt_table_print);
274 int cfs_cpt_distance_print(struct cfs_cpt_table *cptab, char *buf, int len)
281 for (i = 0; i < cptab->ctb_nparts; i++) {
285 rc = snprintf(tmp, len, "%d\t:", i);
292 for (j = 0; j < cptab->ctb_nparts; j++) {
293 rc = snprintf(tmp, len, " %d:%d", j,
294 cptab->ctb_parts[i].cpt_distance[j]);
310 EXPORT_SYMBOL(cfs_cpt_distance_print);
312 int cfs_cpt_number(struct cfs_cpt_table *cptab)
314 return cptab->ctb_nparts;
316 EXPORT_SYMBOL(cfs_cpt_number);
318 int cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
320 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
322 return cpt == CFS_CPT_ANY ?
323 cpumask_weight(cptab->ctb_cpumask) :
324 cpumask_weight(cptab->ctb_parts[cpt].cpt_cpumask);
326 EXPORT_SYMBOL(cfs_cpt_weight);
328 int cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
330 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
332 return cpt == CFS_CPT_ANY ?
333 cpumask_any_and(cptab->ctb_cpumask,
334 cpu_online_mask) < nr_cpu_ids :
335 cpumask_any_and(cptab->ctb_parts[cpt].cpt_cpumask,
336 cpu_online_mask) < nr_cpu_ids;
338 EXPORT_SYMBOL(cfs_cpt_online);
340 cpumask_var_t *cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
342 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
344 return cpt == CFS_CPT_ANY ?
345 &cptab->ctb_cpumask : &cptab->ctb_parts[cpt].cpt_cpumask;
347 EXPORT_SYMBOL(cfs_cpt_cpumask);
349 nodemask_t *cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
351 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
353 return cpt == CFS_CPT_ANY ?
354 cptab->ctb_nodemask : cptab->ctb_parts[cpt].cpt_nodemask;
356 EXPORT_SYMBOL(cfs_cpt_nodemask);
358 unsigned int cfs_cpt_distance(struct cfs_cpt_table *cptab, int cpt1, int cpt2)
360 LASSERT(cpt1 == CFS_CPT_ANY || (cpt1 >= 0 && cpt1 < cptab->ctb_nparts));
361 LASSERT(cpt2 == CFS_CPT_ANY || (cpt2 >= 0 && cpt2 < cptab->ctb_nparts));
363 if (cpt1 == CFS_CPT_ANY || cpt2 == CFS_CPT_ANY)
364 return cptab->ctb_distance;
366 return cptab->ctb_parts[cpt1].cpt_distance[cpt2];
368 EXPORT_SYMBOL(cfs_cpt_distance);
371 * Calculate the maximum NUMA distance between all nodes in the
372 * from_mask and all nodes in the to_mask.
374 static unsigned int cfs_cpt_distance_calculate(nodemask_t *from_mask,
377 unsigned int maximum;
378 unsigned int distance;
383 for_each_node_mask(from, *from_mask) {
384 for_each_node_mask(to, *to_mask) {
385 distance = node_distance(from, to);
386 if (maximum < distance)
393 static void cfs_cpt_add_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
395 cptab->ctb_cpu2cpt[cpu] = cpt;
397 cpumask_set_cpu(cpu, cptab->ctb_cpumask);
398 cpumask_set_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
401 static void cfs_cpt_del_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
403 cpumask_clear_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
404 cpumask_clear_cpu(cpu, cptab->ctb_cpumask);
406 cptab->ctb_cpu2cpt[cpu] = -1;
409 static void cfs_cpt_add_node(struct cfs_cpt_table *cptab, int cpt, int node)
411 struct cfs_cpu_partition *part;
413 if (!node_isset(node, *cptab->ctb_nodemask)) {
416 /* first time node is added to the CPT table */
417 node_set(node, *cptab->ctb_nodemask);
418 cptab->ctb_node2cpt[node] = cpt;
420 dist = cfs_cpt_distance_calculate(cptab->ctb_nodemask,
421 cptab->ctb_nodemask);
422 cptab->ctb_distance = dist;
425 part = &cptab->ctb_parts[cpt];
426 if (!node_isset(node, *part->cpt_nodemask)) {
429 /* first time node is added to this CPT */
430 node_set(node, *part->cpt_nodemask);
431 for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
432 struct cfs_cpu_partition *part2;
435 part2 = &cptab->ctb_parts[cpt2];
436 dist = cfs_cpt_distance_calculate(part->cpt_nodemask,
437 part2->cpt_nodemask);
438 part->cpt_distance[cpt2] = dist;
439 dist = cfs_cpt_distance_calculate(part2->cpt_nodemask,
441 part2->cpt_distance[cpt] = dist;
446 static void cfs_cpt_del_node(struct cfs_cpt_table *cptab, int cpt, int node)
448 struct cfs_cpu_partition *part = &cptab->ctb_parts[cpt];
451 for_each_cpu(cpu, part->cpt_cpumask) {
452 /* this CPT has other CPU belonging to this node? */
453 if (cpu_to_node(cpu) == node)
457 if (cpu >= nr_cpu_ids && node_isset(node, *part->cpt_nodemask)) {
460 /* No more CPUs in the node for this CPT. */
461 node_clear(node, *part->cpt_nodemask);
462 for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
463 struct cfs_cpu_partition *part2;
466 part2 = &cptab->ctb_parts[cpt2];
467 if (node_isset(node, *part2->cpt_nodemask))
468 cptab->ctb_node2cpt[node] = cpt2;
470 dist = cfs_cpt_distance_calculate(part->cpt_nodemask,
471 part2->cpt_nodemask);
472 part->cpt_distance[cpt2] = dist;
473 dist = cfs_cpt_distance_calculate(part2->cpt_nodemask,
475 part2->cpt_distance[cpt] = dist;
479 for_each_cpu(cpu, cptab->ctb_cpumask) {
480 /* this CPT-table has other CPUs belonging to this node? */
481 if (cpu_to_node(cpu) == node)
485 if (cpu >= nr_cpu_ids && node_isset(node, *cptab->ctb_nodemask)) {
486 /* No more CPUs in the table for this node. */
487 node_clear(node, *cptab->ctb_nodemask);
488 cptab->ctb_node2cpt[node] = -1;
489 cptab->ctb_distance =
490 cfs_cpt_distance_calculate(cptab->ctb_nodemask,
491 cptab->ctb_nodemask);
495 int cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
497 LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
499 if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu)) {
500 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
504 if (cptab->ctb_cpu2cpt[cpu] != -1) {
505 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
506 cpu, cptab->ctb_cpu2cpt[cpu]);
510 if (cpumask_test_cpu(cpu, cptab->ctb_cpumask)) {
511 CDEBUG(D_INFO, "CPU %d is already in cpumask\n", cpu);
515 if (cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask)) {
516 CDEBUG(D_INFO, "CPU %d is already in partition %d cpumask\n",
517 cpu, cptab->ctb_cpu2cpt[cpu]);
521 cfs_cpt_add_cpu(cptab, cpt, cpu);
522 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
526 EXPORT_SYMBOL(cfs_cpt_set_cpu);
528 void cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
530 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
532 if (cpu < 0 || cpu >= nr_cpu_ids) {
533 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
537 if (cpt == CFS_CPT_ANY) {
538 /* caller doesn't know the partition ID */
539 cpt = cptab->ctb_cpu2cpt[cpu];
540 if (cpt < 0) { /* not set in this CPT-table */
542 "Try to unset cpu %d which is not in CPT-table %p\n",
547 } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
549 "CPU %d is not in CPU partition %d\n", cpu, cpt);
553 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
554 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_cpumask));
556 cfs_cpt_del_cpu(cptab, cpt, cpu);
557 cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
559 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
561 int cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt,
562 const cpumask_t *mask)
566 if (!cpumask_weight(mask) ||
567 cpumask_any_and(mask, cpu_online_mask) >= nr_cpu_ids) {
569 "No online CPU is found in the CPU mask for CPU partition %d\n",
574 for_each_cpu(cpu, mask) {
575 cfs_cpt_add_cpu(cptab, cpt, cpu);
576 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
581 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
583 void cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt,
584 const cpumask_t *mask)
588 for_each_cpu(cpu, mask) {
589 cfs_cpt_del_cpu(cptab, cpt, cpu);
590 cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
593 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
595 int cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
597 const cpumask_t *mask;
600 if (node < 0 || node >= nr_node_ids) {
602 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
606 mask = cpumask_of_node(node);
608 for_each_cpu(cpu, mask)
609 cfs_cpt_add_cpu(cptab, cpt, cpu);
611 cfs_cpt_add_node(cptab, cpt, node);
615 EXPORT_SYMBOL(cfs_cpt_set_node);
617 void cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
619 const cpumask_t *mask;
622 if (node < 0 || node >= nr_node_ids) {
624 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
628 mask = cpumask_of_node(node);
630 for_each_cpu(cpu, mask)
631 cfs_cpt_del_cpu(cptab, cpt, cpu);
633 cfs_cpt_del_node(cptab, cpt, node);
635 EXPORT_SYMBOL(cfs_cpt_unset_node);
637 int cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt,
638 const nodemask_t *mask)
642 for_each_node_mask(node, *mask)
643 cfs_cpt_set_node(cptab, cpt, node);
647 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
649 void cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt,
650 const nodemask_t *mask)
654 for_each_node_mask(node, *mask)
655 cfs_cpt_unset_node(cptab, cpt, node);
657 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
659 int cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
666 /* convert CPU partition ID to HW node id */
668 if (cpt < 0 || cpt >= cptab->ctb_nparts) {
669 mask = cptab->ctb_nodemask;
670 rotor = cptab->ctb_spread_rotor++;
672 mask = cptab->ctb_parts[cpt].cpt_nodemask;
673 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
674 node = cptab->ctb_parts[cpt].cpt_node;
677 weight = nodes_weight(*mask);
681 for_each_node_mask(node, *mask) {
689 EXPORT_SYMBOL(cfs_cpt_spread_node);
691 int cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
697 cpu = smp_processor_id();
698 cpt = cptab->ctb_cpu2cpt[cpu];
700 if (cpt < 0 && remap) {
701 /* don't return negative value for safety of upper layer,
702 * instead we shadow the unknown cpu to a valid partition ID
704 cpt = cpu % cptab->ctb_nparts;
709 EXPORT_SYMBOL(cfs_cpt_current);
711 int cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
713 LASSERT(cpu >= 0 && cpu < nr_cpu_ids);
715 return cptab->ctb_cpu2cpt[cpu];
717 EXPORT_SYMBOL(cfs_cpt_of_cpu);
719 int cfs_cpt_of_node(struct cfs_cpt_table *cptab, int node)
721 if (node < 0 || node > nr_node_ids)
724 return cptab->ctb_node2cpt[node];
726 EXPORT_SYMBOL(cfs_cpt_of_node);
728 int cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
730 nodemask_t *nodemask;
735 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
737 if (cpt == CFS_CPT_ANY) {
738 cpumask = cptab->ctb_cpumask;
739 nodemask = cptab->ctb_nodemask;
741 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
742 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
745 if (!cpumask_intersects(cpumask, cpu_online_mask)) {
747 "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",
752 for_each_online_cpu(cpu) {
753 if (cpumask_test_cpu(cpu, cpumask))
756 rc = set_cpus_allowed_ptr(current, cpumask);
757 set_mems_allowed(*nodemask);
759 schedule(); /* switch to allowed CPU */
764 /* don't need to set affinity because all online CPUs are covered */
767 EXPORT_SYMBOL(cfs_cpt_bind);
770 * Choose max to \a number CPUs from \a node and set them in \a cpt.
771 * We always prefer to choose CPU in the same core/socket.
773 static int cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
774 cpumask_t *node_mask, int number)
776 cpumask_var_t socket_mask;
777 cpumask_var_t core_mask;
784 if (number >= cpumask_weight(node_mask)) {
785 while (!cpumask_empty(node_mask)) {
786 cpu = cpumask_first(node_mask);
787 cpumask_clear_cpu(cpu, node_mask);
789 if (!cpu_online(cpu))
792 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
800 * Allocate scratch buffers
801 * As we cannot initialize a cpumask_var_t, we need
802 * to alloc both before we can risk trying to free either
804 if (!zalloc_cpumask_var(&socket_mask, GFP_NOFS))
806 if (!zalloc_cpumask_var(&core_mask, GFP_NOFS))
811 while (!cpumask_empty(node_mask)) {
812 cpu = cpumask_first(node_mask);
814 /* get cpumask for cores in the same socket */
815 cpumask_and(socket_mask, topology_core_cpumask(cpu), node_mask);
816 while (!cpumask_empty(socket_mask)) {
817 /* get cpumask for hts in the same core */
818 cpumask_and(core_mask, topology_sibling_cpumask(cpu),
821 for_each_cpu(i, core_mask) {
822 cpumask_clear_cpu(i, socket_mask);
823 cpumask_clear_cpu(i, node_mask);
828 rc = cfs_cpt_set_cpu(cptab, cpt, i);
837 cpu = cpumask_first(socket_mask);
842 free_cpumask_var(socket_mask);
843 free_cpumask_var(core_mask);
847 #define CPT_WEIGHT_MIN 4u
849 static unsigned int cfs_cpt_num_estimate(void)
852 unsigned int ncpu = num_online_cpus();
853 unsigned int ncpt = 1;
856 nthr = cpumask_weight(topology_sibling_cpumask(smp_processor_id()));
859 if (ncpu > CPT_WEIGHT_MIN)
860 for (ncpt = 2; ncpu > 2 * nthr * ncpt; ncpt++)
863 #if (BITS_PER_LONG == 32)
864 /* config many CPU partitions on 32-bit system could consume
867 ncpt = min(2U, ncpt);
870 ncpt--; /* worst case is 1 */
875 static struct cfs_cpt_table *cfs_cpt_table_create(int ncpt)
877 struct cfs_cpt_table *cptab = NULL;
878 cpumask_var_t node_mask;
885 num = cfs_cpt_num_estimate();
889 if (ncpt > num_online_cpus()) {
891 CERROR("libcfs: CPU partition count %d > cores %d: rc = %d\n",
892 ncpt, num_online_cpus(), rc);
896 if (ncpt > 4 * num) {
897 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",
901 cptab = cfs_cpt_table_alloc(ncpt);
903 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
908 if (!zalloc_cpumask_var(&node_mask, GFP_NOFS)) {
909 CERROR("Failed to allocate scratch cpumask\n");
914 num = num_online_cpus() / ncpt;
915 rem = num_online_cpus() % ncpt;
916 for_each_online_node(node) {
917 cpumask_copy(node_mask, cpumask_of_node(node));
919 while (cpt < ncpt && !cpumask_empty(node_mask)) {
920 struct cfs_cpu_partition *part = &cptab->ctb_parts[cpt];
921 int ncpu = cpumask_weight(part->cpt_cpumask);
923 rc = cfs_cpt_choose_ncpus(cptab, cpt, node_mask,
924 (rem > 0) + num - ncpu);
930 ncpu = cpumask_weight(part->cpt_cpumask);
931 if (ncpu == num + !!(rem > 0)) {
938 free_cpumask_var(node_mask);
943 free_cpumask_var(node_mask);
945 CERROR("Failed (rc = %d) to setup CPU partition table with %d partitions, online HW NUMA nodes: %d, HW CPU cores: %d.\n",
946 rc, ncpt, num_online_nodes(), num_online_cpus());
949 cfs_cpt_table_free(cptab);
954 static struct cfs_cpt_table *cfs_cpt_table_create_pattern(const char *pattern)
956 struct cfs_cpt_table *cptab;
968 pattern_dup = kstrdup(pattern, GFP_KERNEL);
970 CERROR("Failed to duplicate pattern '%s'\n", pattern);
971 return ERR_PTR(-ENOMEM);
974 str = strim(pattern_dup);
975 if (*str == 'n' || *str == 'N') {
976 str++; /* skip 'N' char */
977 node = 1; /* NUMA pattern */
980 for_each_online_node(i) {
981 if (!cpumask_empty(cpumask_of_node(i)))
984 if (ncpt == 1) { /* single NUMA node */
986 return cfs_cpt_table_create(cpu_npartitions);
991 if (!ncpt) { /* scanning bracket which is mark of partition */
993 while ((bracket = strchr(bracket, '['))) {
1000 (node && ncpt > num_online_nodes()) ||
1001 (!node && ncpt > num_online_cpus())) {
1002 CERROR("Invalid pattern '%s', or too many partitions %d\n",
1008 cptab = cfs_cpt_table_alloc(ncpt);
1010 CERROR("Failed to allocate CPU partition table\n");
1015 if (node < 0) { /* shortcut to create CPT from NUMA & CPU topology */
1016 for_each_online_node(i) {
1017 if (cpumask_empty(cpumask_of_node(i)))
1020 rc = cfs_cpt_set_node(cptab, cpt++, i);
1023 goto err_free_table;
1030 high = node ? nr_node_ids - 1 : nr_cpu_ids - 1;
1032 for (str = strim(str), c = 0; /* until break */; c++) {
1033 struct cfs_range_expr *range;
1034 struct cfs_expr_list *el;
1037 bracket = strchr(str, '[');
1040 CERROR("Invalid pattern '%s'\n", str);
1042 goto err_free_table;
1043 } else if (c != ncpt) {
1044 CERROR("Expect %d partitions but found %d\n",
1047 goto err_free_table;
1052 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
1053 CERROR("Invalid CPU pattern '%s'\n", str);
1055 goto err_free_table;
1058 if (cpt < 0 || cpt >= ncpt) {
1059 CERROR("Invalid partition id %d, total partitions %d\n",
1062 goto err_free_table;
1065 if (cfs_cpt_weight(cptab, cpt)) {
1066 CERROR("Partition %d has already been set.\n", cpt);
1068 goto err_free_table;
1071 str = strim(str + n);
1072 if (str != bracket) {
1073 CERROR("Invalid pattern '%s'\n", str);
1075 goto err_free_table;
1078 bracket = strchr(str, ']');
1080 CERROR("Missing right bracket for partition %d in '%s'\n",
1083 goto err_free_table;
1086 rc = cfs_expr_list_parse(str, (bracket - str) + 1, 0, high,
1089 CERROR("Can't parse number range in '%s'\n", str);
1091 goto err_free_table;
1094 list_for_each_entry(range, &el->el_exprs, re_link) {
1095 for (i = range->re_lo; i <= range->re_hi; i++) {
1096 if ((i - range->re_lo) % range->re_stride)
1099 rc = node ? cfs_cpt_set_node(cptab, cpt, i)
1100 : cfs_cpt_set_cpu(cptab, cpt, i);
1102 cfs_expr_list_free(el);
1104 goto err_free_table;
1109 cfs_expr_list_free(el);
1111 if (!cfs_cpt_online(cptab, cpt)) {
1112 CERROR("No online CPU is found on partition %d\n", cpt);
1114 goto err_free_table;
1117 str = strim(bracket + 1);
1124 cfs_cpt_table_free(cptab);
1130 #ifdef CONFIG_HOTPLUG_CPU
1131 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1132 static enum cpuhp_state lustre_cpu_online;
1134 static int cfs_cpu_online(unsigned int cpu)
1140 static int cfs_cpu_dead(unsigned int cpu)
1144 /* if all HTs in a core are offline, it may break affinity */
1145 warn = cpumask_any_and(topology_sibling_cpumask(cpu),
1146 cpu_online_mask) >= nr_cpu_ids;
1147 CDEBUG(warn ? D_WARNING : D_INFO,
1148 "Lustre: can't support CPU plug-out well now, performance and stability could be impacted [CPU %u]\n",
1153 #ifndef HAVE_HOTPLUG_STATE_MACHINE
1154 static int cfs_cpu_notify(struct notifier_block *self, unsigned long action,
1157 int cpu = (unsigned long)hcpu;
1161 case CPU_DEAD_FROZEN:
1163 case CPU_ONLINE_FROZEN:
1165 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
1166 CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
1177 static struct notifier_block cfs_cpu_notifier = {
1178 .notifier_call = cfs_cpu_notify,
1181 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1182 #endif /* CONFIG_HOTPLUG_CPU */
1184 void cfs_cpu_fini(void)
1186 if (!IS_ERR_OR_NULL(cfs_cpt_tab))
1187 cfs_cpt_table_free(cfs_cpt_tab);
1189 #ifdef CONFIG_HOTPLUG_CPU
1190 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1191 if (lustre_cpu_online > 0)
1192 cpuhp_remove_state_nocalls(lustre_cpu_online);
1193 cpuhp_remove_state_nocalls(CPUHP_LUSTRE_CFS_DEAD);
1195 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1196 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1197 #endif /* CONFIG_HOTPLUG_CPU */
1200 int cfs_cpu_init(void)
1204 LASSERT(!cfs_cpt_tab);
1206 #ifdef CONFIG_HOTPLUG_CPU
1207 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1208 ret = cpuhp_setup_state_nocalls(CPUHP_LUSTRE_CFS_DEAD,
1209 "fs/lustre/cfe:dead", NULL,
1212 goto failed_cpu_dead;
1214 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
1215 "fs/lustre/cfe:online",
1216 cfs_cpu_online, NULL);
1218 goto failed_cpu_online;
1220 lustre_cpu_online = ret;
1222 register_hotcpu_notifier(&cfs_cpu_notifier);
1223 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1224 #endif /* CONFIG_HOTPLUG_CPU */
1228 cfs_cpt_tab = cfs_cpt_table_create_pattern(cpu_pattern);
1229 if (IS_ERR(cfs_cpt_tab)) {
1230 CERROR("Failed to create cptab from pattern '%s'\n",
1232 ret = PTR_ERR(cfs_cpt_tab);
1233 goto failed_alloc_table;
1237 cfs_cpt_tab = cfs_cpt_table_create(cpu_npartitions);
1238 if (IS_ERR(cfs_cpt_tab)) {
1239 CERROR("Failed to create cptab with npartitions %d\n",
1241 ret = PTR_ERR(cfs_cpt_tab);
1242 goto failed_alloc_table;
1248 LCONSOLE(0, "HW NUMA nodes: %d, HW CPU cores: %d, npartitions: %d\n",
1249 num_online_nodes(), num_online_cpus(),
1250 cfs_cpt_number(cfs_cpt_tab));
1256 if (!IS_ERR_OR_NULL(cfs_cpt_tab))
1257 cfs_cpt_table_free(cfs_cpt_tab);
1259 #ifdef CONFIG_HOTPLUG_CPU
1260 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1261 if (lustre_cpu_online > 0)
1262 cpuhp_remove_state_nocalls(lustre_cpu_online);
1264 cpuhp_remove_state_nocalls(CPUHP_LUSTRE_CFS_DEAD);
1267 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1268 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1269 #endif /* CONFIG_HOTPLUG_CPU */