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_cpu.h>
36 #include <libcfs/libcfs.h>
38 /** Global CPU partition table */
39 struct cfs_cpt_table *cfs_cpt_table __read_mostly;
40 EXPORT_SYMBOL(cfs_cpt_table);
43 * modparam for setting number of partitions
45 * 0 : estimate best value based on cores or NUMA nodes
46 * 1 : disable multiple partitions
47 * >1 : specify number of partitions
49 static int cpu_npartitions;
50 module_param(cpu_npartitions, int, 0444);
51 MODULE_PARM_DESC(cpu_npartitions, "# of CPU partitions");
54 * modparam for setting CPU partitions patterns:
56 * i.e: "0[0,1,2,3] 1[4,5,6,7]", number before bracket is CPU partition ID,
57 * number in bracket is processor ID (core or HT)
59 * i.e: "N 0[0,1] 1[2,3]" the first character 'N' means numbers in bracket
60 * are NUMA node ID, number before bracket is CPU partition ID.
62 * i.e: "N", shortcut expression to create CPT from NUMA & CPU topology
64 * NB: If user specified cpu_pattern, cpu_npartitions will be ignored
66 static char *cpu_pattern = "N";
67 module_param(cpu_pattern, charp, 0444);
68 MODULE_PARM_DESC(cpu_pattern, "CPU partitions pattern");
71 struct cfs_cpt_table *cfs_cpt_table_alloc(int ncpt)
73 struct cfs_cpt_table *cptab;
76 LIBCFS_ALLOC(cptab, sizeof(*cptab));
80 cptab->ctb_nparts = ncpt;
82 LIBCFS_ALLOC(cptab->ctb_cpumask, cpumask_size());
83 if (!cptab->ctb_cpumask)
84 goto failed_alloc_cpumask;
86 LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
87 if (!cptab->ctb_nodemask)
88 goto failed_alloc_nodemask;
90 LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
91 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
92 if (!cptab->ctb_cpu2cpt)
93 goto failed_alloc_cpu2cpt;
95 memset(cptab->ctb_cpu2cpt, -1,
96 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
98 LIBCFS_ALLOC(cptab->ctb_node2cpt,
99 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
100 if (!cptab->ctb_node2cpt)
101 goto failed_alloc_node2cpt;
103 memset(cptab->ctb_node2cpt, -1,
104 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
106 LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
107 if (!cptab->ctb_parts)
108 goto failed_alloc_ctb_parts;
110 memset(cptab->ctb_parts, -1, ncpt * sizeof(cptab->ctb_parts[0]));
112 for (i = 0; i < ncpt; i++) {
113 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
115 LIBCFS_ALLOC(part->cpt_cpumask, cpumask_size());
116 if (!part->cpt_cpumask)
117 goto failed_setting_ctb_parts;
119 LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
120 if (!part->cpt_nodemask)
121 goto failed_setting_ctb_parts;
123 LIBCFS_ALLOC(part->cpt_distance,
124 cptab->ctb_nparts * sizeof(part->cpt_distance[0]));
125 if (!part->cpt_distance)
126 goto failed_setting_ctb_parts;
128 memset(part->cpt_distance, -1,
129 cptab->ctb_nparts * sizeof(part->cpt_distance[0]));
134 failed_setting_ctb_parts:
136 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
138 if (part->cpt_nodemask) {
139 LIBCFS_FREE(part->cpt_nodemask,
140 sizeof(*part->cpt_nodemask));
143 if (part->cpt_cpumask)
144 LIBCFS_FREE(part->cpt_cpumask, cpumask_size());
146 if (part->cpt_distance) {
147 LIBCFS_FREE(part->cpt_distance,
149 sizeof(part->cpt_distance[0]));
153 if (cptab->ctb_parts) {
154 LIBCFS_FREE(cptab->ctb_parts,
155 cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
157 failed_alloc_ctb_parts:
158 if (cptab->ctb_node2cpt) {
159 LIBCFS_FREE(cptab->ctb_node2cpt,
160 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
162 failed_alloc_node2cpt:
163 if (cptab->ctb_cpu2cpt) {
164 LIBCFS_FREE(cptab->ctb_cpu2cpt,
165 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
167 failed_alloc_cpu2cpt:
168 if (cptab->ctb_nodemask)
169 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
170 failed_alloc_nodemask:
171 if (cptab->ctb_cpumask)
172 LIBCFS_FREE(cptab->ctb_cpumask, cpumask_size());
173 failed_alloc_cpumask:
174 LIBCFS_FREE(cptab, sizeof(*cptab));
177 EXPORT_SYMBOL(cfs_cpt_table_alloc);
179 void cfs_cpt_table_free(struct cfs_cpt_table *cptab)
183 if (cptab->ctb_cpu2cpt) {
184 LIBCFS_FREE(cptab->ctb_cpu2cpt,
185 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
188 if (cptab->ctb_node2cpt) {
189 LIBCFS_FREE(cptab->ctb_node2cpt,
190 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
193 for (i = 0; cptab->ctb_parts && i < cptab->ctb_nparts; i++) {
194 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
196 if (part->cpt_nodemask) {
197 LIBCFS_FREE(part->cpt_nodemask,
198 sizeof(*part->cpt_nodemask));
201 if (part->cpt_cpumask)
202 LIBCFS_FREE(part->cpt_cpumask, cpumask_size());
204 if (part->cpt_distance) {
205 LIBCFS_FREE(part->cpt_distance,
207 sizeof(part->cpt_distance[0]));
211 if (cptab->ctb_parts) {
212 LIBCFS_FREE(cptab->ctb_parts,
213 cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
216 if (cptab->ctb_nodemask)
217 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
218 if (cptab->ctb_cpumask)
219 LIBCFS_FREE(cptab->ctb_cpumask, cpumask_size());
221 LIBCFS_FREE(cptab, sizeof(*cptab));
223 EXPORT_SYMBOL(cfs_cpt_table_free);
225 int cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
232 for (i = 0; i < cptab->ctb_nparts; i++) {
236 rc = snprintf(tmp, len, "%d\t:", i);
243 for_each_cpu(j, cptab->ctb_parts[i].cpt_cpumask) {
244 rc = snprintf(tmp, len, " %d", j);
260 EXPORT_SYMBOL(cfs_cpt_table_print);
262 int cfs_cpt_distance_print(struct cfs_cpt_table *cptab, char *buf, int len)
269 for (i = 0; i < cptab->ctb_nparts; i++) {
273 rc = snprintf(tmp, len, "%d\t:", i);
280 for (j = 0; j < cptab->ctb_nparts; j++) {
281 rc = snprintf(tmp, len, " %d:%d", j,
282 cptab->ctb_parts[i].cpt_distance[j]);
298 EXPORT_SYMBOL(cfs_cpt_distance_print);
300 int cfs_cpt_number(struct cfs_cpt_table *cptab)
302 return cptab->ctb_nparts;
304 EXPORT_SYMBOL(cfs_cpt_number);
306 int cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
308 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
310 return cpt == CFS_CPT_ANY ?
311 cpumask_weight(cptab->ctb_cpumask) :
312 cpumask_weight(cptab->ctb_parts[cpt].cpt_cpumask);
314 EXPORT_SYMBOL(cfs_cpt_weight);
316 int cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
318 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
320 return cpt == CFS_CPT_ANY ?
321 cpumask_any_and(cptab->ctb_cpumask,
322 cpu_online_mask) < nr_cpu_ids :
323 cpumask_any_and(cptab->ctb_parts[cpt].cpt_cpumask,
324 cpu_online_mask) < nr_cpu_ids;
326 EXPORT_SYMBOL(cfs_cpt_online);
328 cpumask_t *cfs_cpt_cpumask(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 cptab->ctb_cpumask : cptab->ctb_parts[cpt].cpt_cpumask;
335 EXPORT_SYMBOL(cfs_cpt_cpumask);
337 nodemask_t *cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
339 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
341 return cpt == CFS_CPT_ANY ?
342 cptab->ctb_nodemask : cptab->ctb_parts[cpt].cpt_nodemask;
344 EXPORT_SYMBOL(cfs_cpt_nodemask);
346 unsigned int cfs_cpt_distance(struct cfs_cpt_table *cptab, int cpt1, int cpt2)
348 LASSERT(cpt1 == CFS_CPT_ANY || (cpt1 >= 0 && cpt1 < cptab->ctb_nparts));
349 LASSERT(cpt2 == CFS_CPT_ANY || (cpt2 >= 0 && cpt2 < cptab->ctb_nparts));
351 if (cpt1 == CFS_CPT_ANY || cpt2 == CFS_CPT_ANY)
352 return cptab->ctb_distance;
354 return cptab->ctb_parts[cpt1].cpt_distance[cpt2];
356 EXPORT_SYMBOL(cfs_cpt_distance);
359 * Calculate the maximum NUMA distance between all nodes in the
360 * from_mask and all nodes in the to_mask.
362 static unsigned int cfs_cpt_distance_calculate(nodemask_t *from_mask,
365 unsigned int maximum;
366 unsigned int distance;
371 for_each_node_mask(from, *from_mask) {
372 for_each_node_mask(to, *to_mask) {
373 distance = node_distance(from, to);
374 if (maximum < distance)
381 static void cfs_cpt_add_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
383 cptab->ctb_cpu2cpt[cpu] = cpt;
385 cpumask_set_cpu(cpu, cptab->ctb_cpumask);
386 cpumask_set_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
389 static void cfs_cpt_del_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
391 cpumask_clear_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
392 cpumask_clear_cpu(cpu, cptab->ctb_cpumask);
394 cptab->ctb_cpu2cpt[cpu] = -1;
397 static void cfs_cpt_add_node(struct cfs_cpt_table *cptab, int cpt, int node)
399 struct cfs_cpu_partition *part;
401 if (!node_isset(node, *cptab->ctb_nodemask)) {
404 /* first time node is added to the CPT table */
405 node_set(node, *cptab->ctb_nodemask);
406 cptab->ctb_node2cpt[node] = cpt;
408 dist = cfs_cpt_distance_calculate(cptab->ctb_nodemask,
409 cptab->ctb_nodemask);
410 cptab->ctb_distance = dist;
413 part = &cptab->ctb_parts[cpt];
414 if (!node_isset(node, *part->cpt_nodemask)) {
417 /* first time node is added to this CPT */
418 node_set(node, *part->cpt_nodemask);
419 for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
420 struct cfs_cpu_partition *part2;
423 part2 = &cptab->ctb_parts[cpt2];
424 dist = cfs_cpt_distance_calculate(part->cpt_nodemask,
425 part2->cpt_nodemask);
426 part->cpt_distance[cpt2] = dist;
427 dist = cfs_cpt_distance_calculate(part2->cpt_nodemask,
429 part2->cpt_distance[cpt] = dist;
434 static void cfs_cpt_del_node(struct cfs_cpt_table *cptab, int cpt, int node)
436 struct cfs_cpu_partition *part = &cptab->ctb_parts[cpt];
439 for_each_cpu(cpu, part->cpt_cpumask) {
440 /* this CPT has other CPU belonging to this node? */
441 if (cpu_to_node(cpu) == node)
445 if (cpu >= nr_cpu_ids && node_isset(node, *part->cpt_nodemask)) {
448 /* No more CPUs in the node for this CPT. */
449 node_clear(node, *part->cpt_nodemask);
450 for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
451 struct cfs_cpu_partition *part2;
454 part2 = &cptab->ctb_parts[cpt2];
455 if (node_isset(node, *part2->cpt_nodemask))
456 cptab->ctb_node2cpt[node] = cpt2;
458 dist = cfs_cpt_distance_calculate(part->cpt_nodemask,
459 part2->cpt_nodemask);
460 part->cpt_distance[cpt2] = dist;
461 dist = cfs_cpt_distance_calculate(part2->cpt_nodemask,
463 part2->cpt_distance[cpt] = dist;
467 for_each_cpu(cpu, cptab->ctb_cpumask) {
468 /* this CPT-table has other CPUs belonging to this node? */
469 if (cpu_to_node(cpu) == node)
473 if (cpu >= nr_cpu_ids && node_isset(node, *cptab->ctb_nodemask)) {
474 /* No more CPUs in the table for this node. */
475 node_clear(node, *cptab->ctb_nodemask);
476 cptab->ctb_node2cpt[node] = -1;
477 cptab->ctb_distance =
478 cfs_cpt_distance_calculate(cptab->ctb_nodemask,
479 cptab->ctb_nodemask);
483 int cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
485 LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
487 if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu)) {
488 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
492 if (cptab->ctb_cpu2cpt[cpu] != -1) {
493 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
494 cpu, cptab->ctb_cpu2cpt[cpu]);
498 if (cpumask_test_cpu(cpu, cptab->ctb_cpumask)) {
499 CDEBUG(D_INFO, "CPU %d is already in cpumask\n", cpu);
503 if (cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask)) {
504 CDEBUG(D_INFO, "CPU %d is already in partition %d cpumask\n",
505 cpu, cptab->ctb_cpu2cpt[cpu]);
509 cfs_cpt_add_cpu(cptab, cpt, cpu);
510 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
514 EXPORT_SYMBOL(cfs_cpt_set_cpu);
516 void cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
518 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
520 if (cpu < 0 || cpu >= nr_cpu_ids) {
521 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
525 if (cpt == CFS_CPT_ANY) {
526 /* caller doesn't know the partition ID */
527 cpt = cptab->ctb_cpu2cpt[cpu];
528 if (cpt < 0) { /* not set in this CPT-table */
530 "Try to unset cpu %d which is not in CPT-table %p\n",
535 } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
537 "CPU %d is not in CPU partition %d\n", cpu, cpt);
541 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
542 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_cpumask));
544 cfs_cpt_del_cpu(cptab, cpt, cpu);
545 cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
547 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
549 int cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt,
550 const cpumask_t *mask)
554 if (!cpumask_weight(mask) ||
555 cpumask_any_and(mask, cpu_online_mask) >= nr_cpu_ids) {
557 "No online CPU is found in the CPU mask for CPU partition %d\n",
562 for_each_cpu(cpu, mask) {
563 cfs_cpt_add_cpu(cptab, cpt, cpu);
564 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
569 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
571 void cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt,
572 const cpumask_t *mask)
576 for_each_cpu(cpu, mask) {
577 cfs_cpt_del_cpu(cptab, cpt, cpu);
578 cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
581 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
583 int cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
585 const cpumask_t *mask;
588 if (node < 0 || node >= nr_node_ids) {
590 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
594 mask = cpumask_of_node(node);
596 for_each_cpu(cpu, mask)
597 cfs_cpt_add_cpu(cptab, cpt, cpu);
599 cfs_cpt_add_node(cptab, cpt, node);
603 EXPORT_SYMBOL(cfs_cpt_set_node);
605 void cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
607 const cpumask_t *mask;
610 if (node < 0 || node >= nr_node_ids) {
612 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
616 mask = cpumask_of_node(node);
618 for_each_cpu(cpu, mask)
619 cfs_cpt_del_cpu(cptab, cpt, cpu);
621 cfs_cpt_del_node(cptab, cpt, node);
623 EXPORT_SYMBOL(cfs_cpt_unset_node);
625 int cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt,
626 const nodemask_t *mask)
630 for_each_node_mask(node, *mask)
631 cfs_cpt_set_node(cptab, cpt, node);
635 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
637 void cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt,
638 const nodemask_t *mask)
642 for_each_node_mask(node, *mask)
643 cfs_cpt_unset_node(cptab, cpt, node);
645 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
647 int cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
654 /* convert CPU partition ID to HW node id */
656 if (cpt < 0 || cpt >= cptab->ctb_nparts) {
657 mask = cptab->ctb_nodemask;
658 rotor = cptab->ctb_spread_rotor++;
660 mask = cptab->ctb_parts[cpt].cpt_nodemask;
661 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
662 node = cptab->ctb_parts[cpt].cpt_node;
665 weight = nodes_weight(*mask);
669 for_each_node_mask(node, *mask) {
677 EXPORT_SYMBOL(cfs_cpt_spread_node);
679 int cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
681 int cpu = smp_processor_id();
682 int cpt = cptab->ctb_cpu2cpt[cpu];
688 /* don't return negative value for safety of upper layer,
689 * instead we shadow the unknown cpu to a valid partition ID
691 cpt = cpu % cptab->ctb_nparts;
696 EXPORT_SYMBOL(cfs_cpt_current);
698 int cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
700 LASSERT(cpu >= 0 && cpu < nr_cpu_ids);
702 return cptab->ctb_cpu2cpt[cpu];
704 EXPORT_SYMBOL(cfs_cpt_of_cpu);
706 int cfs_cpt_of_node(struct cfs_cpt_table *cptab, int node)
708 if (node < 0 || node > nr_node_ids)
711 return cptab->ctb_node2cpt[node];
713 EXPORT_SYMBOL(cfs_cpt_of_node);
715 int cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
717 nodemask_t *nodemask;
722 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
724 if (cpt == CFS_CPT_ANY) {
725 cpumask = cptab->ctb_cpumask;
726 nodemask = cptab->ctb_nodemask;
728 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
729 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
732 if (!cpumask_intersects(cpumask, cpu_online_mask)) {
734 "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",
739 for_each_online_cpu(cpu) {
740 if (cpumask_test_cpu(cpu, cpumask))
743 rc = set_cpus_allowed_ptr(current, cpumask);
744 set_mems_allowed(*nodemask);
746 schedule(); /* switch to allowed CPU */
751 /* don't need to set affinity because all online CPUs are covered */
754 EXPORT_SYMBOL(cfs_cpt_bind);
757 * Choose max to \a number CPUs from \a node and set them in \a cpt.
758 * We always prefer to choose CPU in the same core/socket.
760 static int cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
761 cpumask_t *node_mask, int number)
763 cpumask_t *socket_mask = NULL;
764 cpumask_t *core_mask = NULL;
771 if (number >= cpumask_weight(node_mask)) {
772 while (!cpumask_empty(node_mask)) {
773 cpu = cpumask_first(node_mask);
774 cpumask_clear_cpu(cpu, node_mask);
776 if (!cpu_online(cpu))
779 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
786 /* allocate scratch buffer */
787 LIBCFS_ALLOC(socket_mask, cpumask_size());
788 LIBCFS_ALLOC(core_mask, cpumask_size());
789 if (!socket_mask || !core_mask) {
794 while (!cpumask_empty(node_mask)) {
795 cpu = cpumask_first(node_mask);
797 /* get cpumask for cores in the same socket */
798 cpumask_and(socket_mask, topology_core_cpumask(cpu), node_mask);
799 while (!cpumask_empty(socket_mask)) {
800 /* get cpumask for hts in the same core */
801 cpumask_and(core_mask, topology_sibling_cpumask(cpu),
804 for_each_cpu(i, core_mask) {
805 cpumask_clear_cpu(i, socket_mask);
806 cpumask_clear_cpu(i, node_mask);
811 rc = cfs_cpt_set_cpu(cptab, cpt, i);
820 cpu = cpumask_first(socket_mask);
826 LIBCFS_FREE(core_mask, cpumask_size());
828 LIBCFS_FREE(socket_mask, cpumask_size());
832 #define CPT_WEIGHT_MIN 4
834 static int cfs_cpt_num_estimate(void)
836 int nthr = cpumask_weight(topology_sibling_cpumask(smp_processor_id()));
837 int ncpu = num_online_cpus();
840 if (ncpu > CPT_WEIGHT_MIN)
841 for (ncpt = 2; ncpu > 2 * nthr * ncpt; ncpt++)
844 #if (BITS_PER_LONG == 32)
845 /* config many CPU partitions on 32-bit system could consume
851 ncpt--; /* worst case is 1 */
856 static struct cfs_cpt_table *cfs_cpt_table_create(int ncpt)
858 struct cfs_cpt_table *cptab = NULL;
859 cpumask_t *node_mask = NULL;
866 num = cfs_cpt_num_estimate();
870 if (ncpt > num_online_cpus() || ncpt > 4 * num) {
871 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",
875 cptab = cfs_cpt_table_alloc(ncpt);
877 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
882 LIBCFS_ALLOC(node_mask, cpumask_size());
884 CERROR("Failed to allocate scratch cpumask\n");
889 num = num_online_cpus() / ncpt;
890 rem = num_online_cpus() % ncpt;
891 for_each_online_node(node) {
892 cpumask_copy(node_mask, cpumask_of_node(node));
894 while (cpt < ncpt && !cpumask_empty(node_mask)) {
895 struct cfs_cpu_partition *part = &cptab->ctb_parts[cpt];
896 int ncpu = cpumask_weight(part->cpt_cpumask);
898 rc = cfs_cpt_choose_ncpus(cptab, cpt, node_mask,
905 ncpu = cpumask_weight(part->cpt_cpumask);
906 if (ncpu == num + !!(rem > 0)) {
913 LIBCFS_FREE(node_mask, cpumask_size());
919 LIBCFS_FREE(node_mask, cpumask_size());
921 CERROR("Failed (rc = %d) to setup CPU partition table with %d partitions, online HW NUMA nodes: %d, HW CPU cores: %d.\n",
922 rc, ncpt, num_online_nodes(), num_online_cpus());
925 cfs_cpt_table_free(cptab);
930 static struct cfs_cpt_table *cfs_cpt_table_create_pattern(const char *pattern)
932 struct cfs_cpt_table *cptab;
944 pattern_dup = kstrdup(pattern, GFP_KERNEL);
946 CERROR("Failed to duplicate pattern '%s'\n", pattern);
947 return ERR_PTR(-ENOMEM);
950 str = cfs_trimwhite(pattern_dup);
951 if (*str == 'n' || *str == 'N') {
952 str++; /* skip 'N' char */
953 node = 1; /* NUMA pattern */
956 for_each_online_node(i) {
957 if (!cpumask_empty(cpumask_of_node(i)))
960 if (ncpt == 1) { /* single NUMA node */
962 return cfs_cpt_table_create(cpu_npartitions);
967 if (!ncpt) { /* scanning bracket which is mark of partition */
969 while ((bracket = strchr(bracket, '['))) {
976 (node && ncpt > num_online_nodes()) ||
977 (!node && ncpt > num_online_cpus())) {
978 CERROR("Invalid pattern '%s', or too many partitions %d\n",
984 cptab = cfs_cpt_table_alloc(ncpt);
986 CERROR("Failed to allocate CPU partition table\n");
991 if (node < 0) { /* shortcut to create CPT from NUMA & CPU topology */
992 for_each_online_node(i) {
993 if (cpumask_empty(cpumask_of_node(i)))
996 rc = cfs_cpt_set_node(cptab, cpt++, i);
1006 high = node ? nr_node_ids - 1 : nr_cpu_ids - 1;
1008 for (str = cfs_trimwhite(str), c = 0; /* until break */; c++) {
1009 struct cfs_range_expr *range;
1010 struct cfs_expr_list *el;
1013 bracket = strchr(str, '[');
1016 CERROR("Invalid pattern '%s'\n", str);
1018 goto err_free_table;
1019 } else if (c != ncpt) {
1020 CERROR("Expect %d partitions but found %d\n",
1023 goto err_free_table;
1028 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
1029 CERROR("Invalid CPU pattern '%s'\n", str);
1031 goto err_free_table;
1034 if (cpt < 0 || cpt >= ncpt) {
1035 CERROR("Invalid partition id %d, total partitions %d\n",
1038 goto err_free_table;
1041 if (cfs_cpt_weight(cptab, cpt)) {
1042 CERROR("Partition %d has already been set.\n", cpt);
1044 goto err_free_table;
1047 str = cfs_trimwhite(str + n);
1048 if (str != bracket) {
1049 CERROR("Invalid pattern '%s'\n", str);
1051 goto err_free_table;
1054 bracket = strchr(str, ']');
1056 CERROR("Missing right bracket for partition %d in '%s'\n",
1059 goto err_free_table;
1062 rc = cfs_expr_list_parse(str, (bracket - str) + 1, 0, high,
1065 CERROR("Can't parse number range in '%s'\n", str);
1067 goto err_free_table;
1070 list_for_each_entry(range, &el->el_exprs, re_link) {
1071 for (i = range->re_lo; i <= range->re_hi; i++) {
1072 if ((i - range->re_lo) % range->re_stride)
1075 rc = node ? cfs_cpt_set_node(cptab, cpt, i)
1076 : cfs_cpt_set_cpu(cptab, cpt, i);
1078 cfs_expr_list_free(el);
1080 goto err_free_table;
1085 cfs_expr_list_free(el);
1087 if (!cfs_cpt_online(cptab, cpt)) {
1088 CERROR("No online CPU is found on partition %d\n", cpt);
1090 goto err_free_table;
1093 str = cfs_trimwhite(bracket + 1);
1100 cfs_cpt_table_free(cptab);
1106 #ifdef CONFIG_HOTPLUG_CPU
1107 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1108 static enum cpuhp_state lustre_cpu_online;
1110 static int cfs_cpu_online(unsigned int cpu)
1116 static int cfs_cpu_dead(unsigned int cpu)
1120 /* if all HTs in a core are offline, it may break affinity */
1121 warn = cpumask_any_and(topology_sibling_cpumask(cpu),
1122 cpu_online_mask) >= nr_cpu_ids;
1123 CDEBUG(warn ? D_WARNING : D_INFO,
1124 "Lustre: can't support CPU plug-out well now, performance and stability could be impacted [CPU %u]\n",
1129 #ifndef HAVE_HOTPLUG_STATE_MACHINE
1130 static int cfs_cpu_notify(struct notifier_block *self, unsigned long action,
1133 int cpu = (unsigned long)hcpu;
1137 case CPU_DEAD_FROZEN:
1139 case CPU_ONLINE_FROZEN:
1141 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
1142 CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
1153 static struct notifier_block cfs_cpu_notifier = {
1154 .notifier_call = cfs_cpu_notify,
1157 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1158 #endif /* CONFIG_HOTPLUG_CPU */
1160 void cfs_cpu_fini(void)
1162 if (!IS_ERR_OR_NULL(cfs_cpt_table))
1163 cfs_cpt_table_free(cfs_cpt_table);
1165 #ifdef CONFIG_HOTPLUG_CPU
1166 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1167 if (lustre_cpu_online > 0)
1168 cpuhp_remove_state_nocalls(lustre_cpu_online);
1169 cpuhp_remove_state_nocalls(CPUHP_LUSTRE_CFS_DEAD);
1171 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1172 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1173 #endif /* CONFIG_HOTPLUG_CPU */
1176 int cfs_cpu_init(void)
1180 LASSERT(!cfs_cpt_table);
1182 #ifdef CONFIG_HOTPLUG_CPU
1183 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1184 ret = cpuhp_setup_state_nocalls(CPUHP_LUSTRE_CFS_DEAD,
1185 "fs/lustre/cfe:dead", NULL,
1188 goto failed_cpu_dead;
1190 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
1191 "fs/lustre/cfe:online",
1192 cfs_cpu_online, NULL);
1194 goto failed_cpu_online;
1196 lustre_cpu_online = ret;
1198 register_hotcpu_notifier(&cfs_cpu_notifier);
1199 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1200 #endif /* CONFIG_HOTPLUG_CPU */
1204 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern);
1205 if (IS_ERR(cfs_cpt_table)) {
1206 CERROR("Failed to create cptab from pattern '%s'\n",
1208 ret = PTR_ERR(cfs_cpt_table);
1209 goto failed_alloc_table;
1213 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1214 if (IS_ERR(cfs_cpt_table)) {
1215 CERROR("Failed to create cptab with npartitions %d\n",
1217 ret = PTR_ERR(cfs_cpt_table);
1218 goto failed_alloc_table;
1224 LCONSOLE(0, "HW NUMA nodes: %d, HW CPU cores: %d, npartitions: %d\n",
1225 num_online_nodes(), num_online_cpus(),
1226 cfs_cpt_number(cfs_cpt_table));
1233 cfs_cpt_table_free(cfs_cpt_table);
1235 #ifdef CONFIG_HOTPLUG_CPU
1236 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1237 if (lustre_cpu_online > 0)
1238 cpuhp_remove_state_nocalls(lustre_cpu_online);
1240 cpuhp_remove_state_nocalls(CPUHP_LUSTRE_CFS_DEAD);
1243 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1244 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1245 #endif /* CONFIG_HOTPLUG_CPU */
1249 #else /* ! CONFIG_SMP */
1251 struct cfs_cpt_table *cfs_cpt_table_alloc(int ncpt)
1253 struct cfs_cpt_table *cptab;
1256 CERROR("Can't support cpu partition number %d\n", ncpt);
1260 LIBCFS_ALLOC(cptab, sizeof(*cptab));
1264 cpumask_set_cpu(0, cptab->ctb_cpumask);
1265 node_set(0, cptab->ctb_nodemask);
1269 EXPORT_SYMBOL(cfs_cpt_table_alloc);
1271 int cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
1275 rc = snprintf(buf, len, "0\t: 0\n");
1282 EXPORT_SYMBOL(cfs_cpt_table_print);
1284 int cfs_cpt_distance_print(struct cfs_cpt_table *cptab, char *buf, int len)
1288 rc = snprintf(buf, len, "0\t: 0:1\n");
1295 EXPORT_SYMBOL(cfs_cpt_distance_print);
1297 void cfs_cpu_fini(void)
1299 if (cfs_cpt_table) {
1300 cfs_cpt_table_free(cfs_cpt_table);
1301 cfs_cpt_table = NULL;
1305 int cfs_cpu_init(void)
1307 cfs_cpt_table = cfs_cpt_table_alloc(1);
1309 return cfs_cpt_table ? 0 : -1;
1312 #endif /* !CONFIG_SMP */