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.
21 * Copyright (c) 2012, 2017, Intel Corporation.
24 * This file is part of Lustre, http://www.lustre.org/
25 * Lustre is a trademark of Sun Microsystems, Inc.
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>
39 * modparam for setting number of partitions
41 * 0 : estimate best value based on cores or NUMA nodes
42 * 1 : disable multiple partitions
43 * >1 : specify number of partitions
45 static int cpu_npartitions;
46 module_param(cpu_npartitions, int, 0444);
47 MODULE_PARM_DESC(cpu_npartitions, "# of CPU partitions");
50 * modparam for setting CPU partitions patterns:
52 * i.e: "0[0,1,2,3] 1[4,5,6,7]", number before bracket is CPU partition ID,
53 * number in bracket is processor ID (core or HT)
55 * i.e: "N 0[0,1] 1[2,3]" the first character 'N' means numbers in bracket
56 * are NUMA node ID, number before bracket is CPU partition ID.
58 * i.e: "N", shortcut expression to create CPT from NUMA & CPU topology
60 * NB: If user specified cpu_pattern, cpu_npartitions will be ignored
62 static char *cpu_pattern = "N";
63 module_param(cpu_pattern, charp, 0444);
64 MODULE_PARM_DESC(cpu_pattern, "CPU partitions pattern");
66 void cfs_cpt_table_free(struct cfs_cpt_table *cptab)
70 if (cptab->ctb_cpu2cpt) {
71 LIBCFS_FREE(cptab->ctb_cpu2cpt,
72 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
75 if (cptab->ctb_node2cpt) {
76 LIBCFS_FREE(cptab->ctb_node2cpt,
77 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
80 for (i = 0; cptab->ctb_parts && i < cptab->ctb_nparts; i++) {
81 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
83 if (part->cpt_nodemask) {
84 LIBCFS_FREE(part->cpt_nodemask,
85 sizeof(*part->cpt_nodemask));
88 if (part->cpt_cpumask)
89 LIBCFS_FREE(part->cpt_cpumask, cpumask_size());
91 if (part->cpt_distance) {
92 LIBCFS_FREE(part->cpt_distance,
94 sizeof(part->cpt_distance[0]));
98 if (cptab->ctb_parts) {
99 LIBCFS_FREE(cptab->ctb_parts,
100 cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
103 if (cptab->ctb_nodemask)
104 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
105 if (cptab->ctb_cpumask)
106 LIBCFS_FREE(cptab->ctb_cpumask, cpumask_size());
108 LIBCFS_FREE(cptab, sizeof(*cptab));
110 EXPORT_SYMBOL(cfs_cpt_table_free);
112 struct cfs_cpt_table *cfs_cpt_table_alloc(int ncpt)
114 struct cfs_cpt_table *cptab;
117 LIBCFS_ALLOC(cptab, sizeof(*cptab));
121 cptab->ctb_nparts = ncpt;
123 LIBCFS_ALLOC(cptab->ctb_cpumask, cpumask_size());
124 LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
126 if (!cptab->ctb_cpumask || !cptab->ctb_nodemask)
129 LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
130 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
131 if (!cptab->ctb_cpu2cpt)
134 memset(cptab->ctb_cpu2cpt, -1,
135 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
137 LIBCFS_ALLOC(cptab->ctb_node2cpt,
138 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
139 if (!cptab->ctb_node2cpt)
142 memset(cptab->ctb_node2cpt, -1,
143 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
145 LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
146 if (!cptab->ctb_parts)
149 for (i = 0; i < ncpt; i++) {
150 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
152 LIBCFS_ALLOC(part->cpt_cpumask, cpumask_size());
153 if (!part->cpt_cpumask)
156 LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
157 if (!part->cpt_nodemask)
160 LIBCFS_ALLOC(part->cpt_distance,
161 cptab->ctb_nparts * sizeof(part->cpt_distance[0]));
162 if (!part->cpt_distance)
169 cfs_cpt_table_free(cptab);
172 EXPORT_SYMBOL(cfs_cpt_table_alloc);
174 int cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
181 for (i = 0; i < cptab->ctb_nparts; i++) {
185 rc = snprintf(tmp, len, "%d\t:", i);
192 for_each_cpu(j, cptab->ctb_parts[i].cpt_cpumask) {
193 rc = snprintf(tmp, len, " %d", j);
210 EXPORT_SYMBOL(cfs_cpt_table_print);
212 int cfs_cpt_distance_print(struct cfs_cpt_table *cptab, char *buf, int len)
219 for (i = 0; i < cptab->ctb_nparts; i++) {
223 rc = snprintf(tmp, len, "%d\t:", i);
230 for (j = 0; j < cptab->ctb_nparts; j++) {
231 rc = snprintf(tmp, len, " %d:%d",
232 j, cptab->ctb_parts[i].cpt_distance[j]);
249 EXPORT_SYMBOL(cfs_cpt_distance_print);
251 int cfs_cpt_number(struct cfs_cpt_table *cptab)
253 return cptab->ctb_nparts;
255 EXPORT_SYMBOL(cfs_cpt_number);
257 int cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
259 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
261 return cpt == CFS_CPT_ANY ?
262 cpumask_weight(cptab->ctb_cpumask) :
263 cpumask_weight(cptab->ctb_parts[cpt].cpt_cpumask);
265 EXPORT_SYMBOL(cfs_cpt_weight);
267 int cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
269 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
271 return cpt == CFS_CPT_ANY ?
272 cpumask_any_and(cptab->ctb_cpumask,
273 cpu_online_mask) < nr_cpu_ids :
274 cpumask_any_and(cptab->ctb_parts[cpt].cpt_cpumask,
275 cpu_online_mask) < nr_cpu_ids;
277 EXPORT_SYMBOL(cfs_cpt_online);
279 cpumask_t *cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
281 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
283 return cpt == CFS_CPT_ANY ?
284 cptab->ctb_cpumask : cptab->ctb_parts[cpt].cpt_cpumask;
286 EXPORT_SYMBOL(cfs_cpt_cpumask);
288 nodemask_t *cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
290 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
292 return cpt == CFS_CPT_ANY ?
293 cptab->ctb_nodemask : cptab->ctb_parts[cpt].cpt_nodemask;
295 EXPORT_SYMBOL(cfs_cpt_nodemask);
297 unsigned int cfs_cpt_distance(struct cfs_cpt_table *cptab, int cpt1, int cpt2)
299 LASSERT(cpt1 == CFS_CPT_ANY || (cpt1 >= 0 && cpt1 < cptab->ctb_nparts));
300 LASSERT(cpt2 == CFS_CPT_ANY || (cpt2 >= 0 && cpt2 < cptab->ctb_nparts));
302 if (cpt1 == CFS_CPT_ANY || cpt2 == CFS_CPT_ANY)
303 return cptab->ctb_distance;
305 return cptab->ctb_parts[cpt1].cpt_distance[cpt2];
307 EXPORT_SYMBOL(cfs_cpt_distance);
310 * Calculate the maximum NUMA distance between all nodes in the
311 * from_mask and all nodes in the to_mask.
313 static unsigned int cfs_cpt_distance_calculate(nodemask_t *from_mask,
316 unsigned int maximum;
317 unsigned int distance;
322 for_each_node_mask(from, *from_mask) {
323 for_each_node_mask(to, *to_mask) {
324 distance = node_distance(from, to);
325 if (maximum < distance)
332 static void cfs_cpt_add_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
334 cptab->ctb_cpu2cpt[cpu] = cpt;
336 cpumask_set_cpu(cpu, cptab->ctb_cpumask);
337 cpumask_set_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
340 static void cfs_cpt_del_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
342 cpumask_clear_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
343 cpumask_clear_cpu(cpu, cptab->ctb_cpumask);
345 cptab->ctb_cpu2cpt[cpu] = -1;
348 static void cfs_cpt_add_node(struct cfs_cpt_table *cptab, int cpt, int node)
350 struct cfs_cpu_partition *part;
352 if (!node_isset(node, *cptab->ctb_nodemask)) {
355 /* first time node is added to the CPT table */
356 node_set(node, *cptab->ctb_nodemask);
357 cptab->ctb_node2cpt[node] = cpt;
359 dist = cfs_cpt_distance_calculate(cptab->ctb_nodemask,
360 cptab->ctb_nodemask);
361 cptab->ctb_distance = dist;
364 part = &cptab->ctb_parts[cpt];
365 if (!node_isset(node, *part->cpt_nodemask)) {
368 /* first time node is added to this CPT */
369 node_set(node, *part->cpt_nodemask);
370 for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
371 struct cfs_cpu_partition *part2;
374 part2 = &cptab->ctb_parts[cpt2];
375 dist = cfs_cpt_distance_calculate(part->cpt_nodemask,
376 part2->cpt_nodemask);
377 part->cpt_distance[cpt2] = dist;
378 dist = cfs_cpt_distance_calculate(part2->cpt_nodemask,
380 part2->cpt_distance[cpt] = dist;
385 static void cfs_cpt_del_node(struct cfs_cpt_table *cptab, int cpt, int node)
387 struct cfs_cpu_partition *part = &cptab->ctb_parts[cpt];
390 for_each_cpu(cpu, part->cpt_cpumask) {
391 /* this CPT has other CPU belonging to this node? */
392 if (cpu_to_node(cpu) == node)
396 if (cpu >= nr_cpu_ids && node_isset(node, *part->cpt_nodemask)) {
399 /* No more CPUs in the node for this CPT. */
400 node_clear(node, *part->cpt_nodemask);
401 for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
402 struct cfs_cpu_partition *part2;
405 part2 = &cptab->ctb_parts[cpt2];
406 if (node_isset(node, *part2->cpt_nodemask))
407 cptab->ctb_node2cpt[node] = cpt2;
409 dist = cfs_cpt_distance_calculate(part->cpt_nodemask,
410 part2->cpt_nodemask);
411 part->cpt_distance[cpt2] = dist;
412 dist = cfs_cpt_distance_calculate(part2->cpt_nodemask,
414 part2->cpt_distance[cpt] = dist;
418 for_each_cpu(cpu, cptab->ctb_cpumask) {
419 /* this CPT-table has other CPUs belonging to this node? */
420 if (cpu_to_node(cpu) == node)
424 if (cpu >= nr_cpu_ids && node_isset(node, *cptab->ctb_nodemask)) {
425 /* No more CPUs in the table for this node. */
426 node_clear(node, *cptab->ctb_nodemask);
427 cptab->ctb_node2cpt[node] = -1;
428 cptab->ctb_distance =
429 cfs_cpt_distance_calculate(cptab->ctb_nodemask,
430 cptab->ctb_nodemask);
434 int cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
436 LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
438 if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu)) {
439 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
443 if (cptab->ctb_cpu2cpt[cpu] != -1) {
444 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
445 cpu, cptab->ctb_cpu2cpt[cpu]);
449 if (cpumask_test_cpu(cpu, cptab->ctb_cpumask)) {
450 CDEBUG(D_INFO, "CPU %d is already in cpumask\n", cpu);
454 if (cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask)) {
455 CDEBUG(D_INFO, "CPU %d is already in partition %d cpumask\n",
456 cpu, cptab->ctb_cpu2cpt[cpu]);
460 cfs_cpt_add_cpu(cptab, cpt, cpu);
461 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
465 EXPORT_SYMBOL(cfs_cpt_set_cpu);
467 void cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
469 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
471 if (cpu < 0 || cpu >= nr_cpu_ids) {
472 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
476 if (cpt == CFS_CPT_ANY) {
477 /* caller doesn't know the partition ID */
478 cpt = cptab->ctb_cpu2cpt[cpu];
479 if (cpt < 0) { /* not set in this CPT-table */
481 "Try to unset cpu %d which is not in CPT-table %p\n",
486 } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
488 "CPU %d is not in CPU partition %d\n", cpu, cpt);
492 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
493 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_cpumask));
495 cfs_cpt_del_cpu(cptab, cpt, cpu);
496 cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
498 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
500 int cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt,
501 const cpumask_t *mask)
505 if (!cpumask_weight(mask) ||
506 cpumask_any_and(mask, cpu_online_mask) >= nr_cpu_ids) {
508 "No online CPU is found in the CPU mask for CPU partition %d\n",
513 for_each_cpu(cpu, mask) {
514 cfs_cpt_add_cpu(cptab, cpt, cpu);
515 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
520 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
522 void cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt,
523 const cpumask_t *mask)
527 for_each_cpu(cpu, mask) {
528 cfs_cpt_del_cpu(cptab, cpt, cpu);
529 cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
532 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
534 int cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
536 const cpumask_t *mask;
539 if (node < 0 || node >= nr_node_ids) {
541 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
545 mask = cpumask_of_node(node);
547 for_each_cpu(cpu, mask)
548 cfs_cpt_add_cpu(cptab, cpt, cpu);
550 cfs_cpt_add_node(cptab, cpt, node);
554 EXPORT_SYMBOL(cfs_cpt_set_node);
556 void cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
558 const cpumask_t *mask;
561 if (node < 0 || node >= nr_node_ids) {
563 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
567 mask = cpumask_of_node(node);
569 for_each_cpu(cpu, mask)
570 cfs_cpt_del_cpu(cptab, cpt, cpu);
572 cfs_cpt_del_node(cptab, cpt, node);
574 EXPORT_SYMBOL(cfs_cpt_unset_node);
576 int cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt,
577 const nodemask_t *mask)
581 for_each_node_mask(node, *mask)
582 cfs_cpt_set_node(cptab, cpt, node);
586 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
588 void cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt,
589 const nodemask_t *mask)
593 for_each_node_mask(node, *mask)
594 cfs_cpt_unset_node(cptab, cpt, node);
596 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
598 int cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
605 /* convert CPU partition ID to HW node id */
607 if (cpt < 0 || cpt >= cptab->ctb_nparts) {
608 mask = cptab->ctb_nodemask;
609 rotor = cptab->ctb_spread_rotor++;
611 mask = cptab->ctb_parts[cpt].cpt_nodemask;
612 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
613 node = cptab->ctb_parts[cpt].cpt_node;
616 weight = nodes_weight(*mask);
620 for_each_node_mask(node, *mask) {
628 EXPORT_SYMBOL(cfs_cpt_spread_node);
630 int cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
632 int cpu = smp_processor_id();
633 int cpt = cptab->ctb_cpu2cpt[cpu];
639 /* don't return negative value for safety of upper layer,
640 * instead we shadow the unknown cpu to a valid partition ID
642 cpt = cpu % cptab->ctb_nparts;
647 EXPORT_SYMBOL(cfs_cpt_current);
649 int cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
651 LASSERT(cpu >= 0 && cpu < nr_cpu_ids);
653 return cptab->ctb_cpu2cpt[cpu];
655 EXPORT_SYMBOL(cfs_cpt_of_cpu);
657 int cfs_cpt_of_node(struct cfs_cpt_table *cptab, int node)
659 if (node < 0 || node > nr_node_ids)
662 return cptab->ctb_node2cpt[node];
664 EXPORT_SYMBOL(cfs_cpt_of_node);
666 int cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
668 nodemask_t *nodemask;
673 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
675 if (cpt == CFS_CPT_ANY) {
676 cpumask = cptab->ctb_cpumask;
677 nodemask = cptab->ctb_nodemask;
679 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
680 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
683 if (!cpumask_intersects(cpumask, cpu_online_mask)) {
685 "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",
690 for_each_online_cpu(cpu) {
691 if (cpumask_test_cpu(cpu, cpumask))
694 rc = set_cpus_allowed_ptr(current, cpumask);
695 set_mems_allowed(*nodemask);
697 schedule(); /* switch to allowed CPU */
702 /* don't need to set affinity because all online CPUs are covered */
705 EXPORT_SYMBOL(cfs_cpt_bind);
708 * Choose max to \a number CPUs from \a node and set them in \a cpt.
709 * We always prefer to choose CPU in the same core/socket.
711 static int cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
712 cpumask_t *node_mask, int number)
714 cpumask_t *socket_mask = NULL;
715 cpumask_t *core_mask = NULL;
722 if (number >= cpumask_weight(node_mask)) {
723 while (!cpumask_empty(node_mask)) {
724 cpu = cpumask_first(node_mask);
725 cpumask_clear_cpu(cpu, node_mask);
727 if (!cpu_online(cpu))
730 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
737 /* allocate scratch buffer */
738 LIBCFS_ALLOC(socket_mask, cpumask_size());
739 LIBCFS_ALLOC(core_mask, cpumask_size());
740 if (!socket_mask || !core_mask) {
745 while (!cpumask_empty(node_mask)) {
746 cpu = cpumask_first(node_mask);
748 /* get cpumask for cores in the same socket */
749 cpumask_and(socket_mask, topology_core_cpumask(cpu), node_mask);
750 while (!cpumask_empty(socket_mask)) {
751 /* get cpumask for hts in the same core */
752 cpumask_and(core_mask, topology_sibling_cpumask(cpu),
755 for_each_cpu(i, core_mask) {
756 cpumask_clear_cpu(i, socket_mask);
757 cpumask_clear_cpu(i, node_mask);
762 rc = cfs_cpt_set_cpu(cptab, cpt, i);
771 cpu = cpumask_first(socket_mask);
777 LIBCFS_FREE(core_mask, cpumask_size());
779 LIBCFS_FREE(socket_mask, cpumask_size());
783 #define CPT_WEIGHT_MIN 4
785 static int cfs_cpt_num_estimate(void)
787 int nthr = cpumask_weight(topology_sibling_cpumask(smp_processor_id()));
788 int ncpu = num_online_cpus();
791 if (ncpu > CPT_WEIGHT_MIN)
792 for (ncpt = 2; ncpu > 2 * nthr * ncpt; ncpt++)
795 #if (BITS_PER_LONG == 32)
796 /* config many CPU partitions on 32-bit system could consume
802 ncpt--; /* worst case is 1 */
807 static struct cfs_cpt_table *cfs_cpt_table_create(int ncpt)
809 struct cfs_cpt_table *cptab = NULL;
810 cpumask_t *node_mask = NULL;
817 num = cfs_cpt_num_estimate();
821 if (ncpt > num_online_cpus() || ncpt > 4 * num) {
822 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",
826 cptab = cfs_cpt_table_alloc(ncpt);
828 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
833 LIBCFS_ALLOC(node_mask, cpumask_size());
835 CERROR("Failed to allocate scratch cpumask\n");
840 num = num_online_cpus() / ncpt;
841 rem = num_online_cpus() % ncpt;
842 for_each_online_node(node) {
843 cpumask_copy(node_mask, cpumask_of_node(node));
845 while (cpt < ncpt && !cpumask_empty(node_mask)) {
846 struct cfs_cpu_partition *part = &cptab->ctb_parts[cpt];
847 int ncpu = cpumask_weight(part->cpt_cpumask);
849 rc = cfs_cpt_choose_ncpus(cptab, cpt, node_mask,
856 ncpu = cpumask_weight(part->cpt_cpumask);
857 if (ncpu == num + !!(rem > 0)) {
864 LIBCFS_FREE(node_mask, cpumask_size());
870 LIBCFS_FREE(node_mask, cpumask_size());
872 CERROR("Failed (rc = %d) to setup CPU partition table with %d partitions, online HW NUMA nodes: %d, HW CPU cores: %d.\n",
873 rc, ncpt, num_online_nodes(), num_online_cpus());
876 cfs_cpt_table_free(cptab);
881 static struct cfs_cpt_table *cfs_cpt_table_create_pattern(const char *pattern)
883 struct cfs_cpt_table *cptab;
895 pattern_dup = kstrdup(pattern, GFP_KERNEL);
897 CERROR("Failed to duplicate pattern '%s'\n", pattern);
898 return ERR_PTR(-ENOMEM);
901 str = cfs_trimwhite(pattern_dup);
902 if (*str == 'n' || *str == 'N') {
903 str++; /* skip 'N' char */
904 node = 1; /* NUMA pattern */
907 for_each_online_node(i) {
908 if (!cpumask_empty(cpumask_of_node(i)))
911 if (ncpt == 1) { /* single NUMA node */
913 return cfs_cpt_table_create(cpu_npartitions);
918 if (!ncpt) { /* scanning bracket which is mark of partition */
920 while ((bracket = strchr(bracket, '['))) {
927 (node && ncpt > num_online_nodes()) ||
928 (!node && ncpt > num_online_cpus())) {
929 CERROR("Invalid pattern '%s', or too many partitions %d\n",
935 cptab = cfs_cpt_table_alloc(ncpt);
937 CERROR("Failed to allocate CPU partition table\n");
942 if (node < 0) { /* shortcut to create CPT from NUMA & CPU topology */
943 for_each_online_node(i) {
944 if (cpumask_empty(cpumask_of_node(i)))
947 rc = cfs_cpt_set_node(cptab, cpt++, i);
957 high = node ? nr_node_ids - 1 : nr_cpu_ids - 1;
959 for (str = cfs_trimwhite(str), c = 0; /* until break */; c++) {
960 struct cfs_range_expr *range;
961 struct cfs_expr_list *el;
964 bracket = strchr(str, '[');
967 CERROR("Invalid pattern '%s'\n", str);
970 } else if (c != ncpt) {
971 CERROR("Expect %d partitions but found %d\n",
979 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
980 CERROR("Invalid CPU pattern '%s'\n", str);
985 if (cpt < 0 || cpt >= ncpt) {
986 CERROR("Invalid partition id %d, total partitions %d\n",
992 if (cfs_cpt_weight(cptab, cpt)) {
993 CERROR("Partition %d has already been set.\n", cpt);
998 str = cfs_trimwhite(str + n);
999 if (str != bracket) {
1000 CERROR("Invalid pattern '%s'\n", str);
1002 goto err_free_table;
1005 bracket = strchr(str, ']');
1007 CERROR("Missing right bracket for partition %d in '%s'\n",
1010 goto err_free_table;
1013 rc = cfs_expr_list_parse(str, (bracket - str) + 1, 0, high,
1016 CERROR("Can't parse number range in '%s'\n", str);
1018 goto err_free_table;
1021 list_for_each_entry(range, &el->el_exprs, re_link) {
1022 for (i = range->re_lo; i <= range->re_hi; i++) {
1023 if ((i - range->re_lo) % range->re_stride)
1026 rc = node ? cfs_cpt_set_node(cptab, cpt, i)
1027 : cfs_cpt_set_cpu(cptab, cpt, i);
1029 cfs_expr_list_free(el);
1031 goto err_free_table;
1036 cfs_expr_list_free(el);
1038 if (!cfs_cpt_online(cptab, cpt)) {
1039 CERROR("No online CPU is found on partition %d\n", cpt);
1041 goto err_free_table;
1044 str = cfs_trimwhite(bracket + 1);
1051 cfs_cpt_table_free(cptab);
1057 #ifdef CONFIG_HOTPLUG_CPU
1058 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1059 static enum cpuhp_state lustre_cpu_online;
1061 static int cfs_cpu_online(unsigned int cpu)
1067 static int cfs_cpu_dead(unsigned int cpu)
1071 /* if all HTs in a core are offline, it may break affinity */
1072 warn = cpumask_any_and(topology_sibling_cpumask(cpu),
1073 cpu_online_mask) >= nr_cpu_ids;
1074 CDEBUG(warn ? D_WARNING : D_INFO,
1075 "Lustre: can't support CPU plug-out well now, performance and stability could be impacted [CPU %u]\n",
1080 #ifndef HAVE_HOTPLUG_STATE_MACHINE
1081 static int cfs_cpu_notify(struct notifier_block *self, unsigned long action,
1084 int cpu = (unsigned long)hcpu;
1088 case CPU_DEAD_FROZEN:
1090 case CPU_ONLINE_FROZEN:
1092 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
1093 CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
1104 static struct notifier_block cfs_cpu_notifier = {
1105 .notifier_call = cfs_cpu_notify,
1108 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1109 #endif /* CONFIG_HOTPLUG_CPU */
1111 void cfs_cpu_fini(void)
1113 if (!IS_ERR_OR_NULL(cfs_cpt_table))
1114 cfs_cpt_table_free(cfs_cpt_table);
1116 #ifdef CONFIG_HOTPLUG_CPU
1117 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1118 if (lustre_cpu_online > 0)
1119 cpuhp_remove_state_nocalls(lustre_cpu_online);
1120 cpuhp_remove_state_nocalls(CPUHP_LUSTRE_CFS_DEAD);
1122 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1123 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1124 #endif /* CONFIG_HOTPLUG_CPU */
1127 int cfs_cpu_init(void)
1131 LASSERT(!cfs_cpt_table);
1133 #ifdef CONFIG_HOTPLUG_CPU
1134 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1135 ret = cpuhp_setup_state_nocalls(CPUHP_LUSTRE_CFS_DEAD,
1136 "fs/lustre/cfe:dead", NULL,
1140 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
1141 "fs/lustre/cfe:online",
1142 cfs_cpu_online, NULL);
1145 lustre_cpu_online = ret;
1147 register_hotcpu_notifier(&cfs_cpu_notifier);
1148 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1149 #endif /* CONFIG_HOTPLUG_CPU */
1154 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern);
1155 if (IS_ERR(cfs_cpt_table)) {
1156 CERROR("Failed to create cptab from pattern '%s'\n",
1158 ret = PTR_ERR(cfs_cpt_table);
1163 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1164 if (IS_ERR(cfs_cpt_table)) {
1165 CERROR("Failed to create cptab with npartitions %d\n",
1167 ret = PTR_ERR(cfs_cpt_table);
1174 LCONSOLE(0, "HW NUMA nodes: %d, HW CPU cores: %d, npartitions: %d\n",
1175 num_online_nodes(), num_online_cpus(),
1176 cfs_cpt_number(cfs_cpt_table));