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 if (!zalloc_cpumask_var(&cptab->ctb_cpumask, GFP_NOFS))
83 goto failed_alloc_cpumask;
85 LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
86 if (!cptab->ctb_nodemask)
87 goto failed_alloc_nodemask;
89 LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
90 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
91 if (!cptab->ctb_cpu2cpt)
92 goto failed_alloc_cpu2cpt;
94 memset(cptab->ctb_cpu2cpt, -1,
95 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
97 LIBCFS_ALLOC(cptab->ctb_node2cpt,
98 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
99 if (!cptab->ctb_node2cpt)
100 goto failed_alloc_node2cpt;
102 memset(cptab->ctb_node2cpt, -1,
103 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
105 LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
106 if (!cptab->ctb_parts)
107 goto failed_alloc_ctb_parts;
109 memset(cptab->ctb_parts, -1, ncpt * sizeof(cptab->ctb_parts[0]));
111 for (i = 0; i < ncpt; i++) {
112 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
114 if (!zalloc_cpumask_var(&part->cpt_cpumask, GFP_NOFS))
115 goto failed_setting_ctb_parts;
117 LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
118 if (!part->cpt_nodemask)
119 goto failed_setting_ctb_parts;
121 LIBCFS_ALLOC(part->cpt_distance,
122 cptab->ctb_nparts * sizeof(part->cpt_distance[0]));
123 if (!part->cpt_distance)
124 goto failed_setting_ctb_parts;
126 memset(part->cpt_distance, -1,
127 cptab->ctb_nparts * sizeof(part->cpt_distance[0]));
132 failed_setting_ctb_parts:
134 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
136 if (part->cpt_nodemask) {
137 LIBCFS_FREE(part->cpt_nodemask,
138 sizeof(*part->cpt_nodemask));
141 free_cpumask_var(part->cpt_cpumask);
143 if (part->cpt_distance) {
144 LIBCFS_FREE(part->cpt_distance,
146 sizeof(part->cpt_distance[0]));
150 if (cptab->ctb_parts) {
151 LIBCFS_FREE(cptab->ctb_parts,
152 cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
154 failed_alloc_ctb_parts:
155 if (cptab->ctb_node2cpt) {
156 LIBCFS_FREE(cptab->ctb_node2cpt,
157 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
159 failed_alloc_node2cpt:
160 if (cptab->ctb_cpu2cpt) {
161 LIBCFS_FREE(cptab->ctb_cpu2cpt,
162 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
164 failed_alloc_cpu2cpt:
165 if (cptab->ctb_nodemask)
166 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
167 failed_alloc_nodemask:
168 free_cpumask_var(cptab->ctb_cpumask);
169 failed_alloc_cpumask:
170 LIBCFS_FREE(cptab, sizeof(*cptab));
173 EXPORT_SYMBOL(cfs_cpt_table_alloc);
175 void cfs_cpt_table_free(struct cfs_cpt_table *cptab)
179 if (cptab->ctb_cpu2cpt) {
180 LIBCFS_FREE(cptab->ctb_cpu2cpt,
181 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
184 if (cptab->ctb_node2cpt) {
185 LIBCFS_FREE(cptab->ctb_node2cpt,
186 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
189 for (i = 0; cptab->ctb_parts && i < cptab->ctb_nparts; i++) {
190 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
192 if (part->cpt_nodemask) {
193 LIBCFS_FREE(part->cpt_nodemask,
194 sizeof(*part->cpt_nodemask));
197 free_cpumask_var(part->cpt_cpumask);
199 if (part->cpt_distance) {
200 LIBCFS_FREE(part->cpt_distance,
202 sizeof(part->cpt_distance[0]));
206 if (cptab->ctb_parts) {
207 LIBCFS_FREE(cptab->ctb_parts,
208 cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
211 if (cptab->ctb_nodemask)
212 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
213 free_cpumask_var(cptab->ctb_cpumask);
215 LIBCFS_FREE(cptab, sizeof(*cptab));
217 EXPORT_SYMBOL(cfs_cpt_table_free);
219 int cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
226 for (i = 0; i < cptab->ctb_nparts; i++) {
230 rc = snprintf(tmp, len, "%d\t:", i);
237 for_each_cpu(j, cptab->ctb_parts[i].cpt_cpumask) {
238 rc = snprintf(tmp, len, " %d", j);
254 EXPORT_SYMBOL(cfs_cpt_table_print);
256 int cfs_cpt_distance_print(struct cfs_cpt_table *cptab, char *buf, int len)
263 for (i = 0; i < cptab->ctb_nparts; i++) {
267 rc = snprintf(tmp, len, "%d\t:", i);
274 for (j = 0; j < cptab->ctb_nparts; j++) {
275 rc = snprintf(tmp, len, " %d:%d", j,
276 cptab->ctb_parts[i].cpt_distance[j]);
292 EXPORT_SYMBOL(cfs_cpt_distance_print);
294 int cfs_cpt_number(struct cfs_cpt_table *cptab)
296 return cptab->ctb_nparts;
298 EXPORT_SYMBOL(cfs_cpt_number);
300 int cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
302 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
304 return cpt == CFS_CPT_ANY ?
305 cpumask_weight(cptab->ctb_cpumask) :
306 cpumask_weight(cptab->ctb_parts[cpt].cpt_cpumask);
308 EXPORT_SYMBOL(cfs_cpt_weight);
310 int cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
312 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
314 return cpt == CFS_CPT_ANY ?
315 cpumask_any_and(cptab->ctb_cpumask,
316 cpu_online_mask) < nr_cpu_ids :
317 cpumask_any_and(cptab->ctb_parts[cpt].cpt_cpumask,
318 cpu_online_mask) < nr_cpu_ids;
320 EXPORT_SYMBOL(cfs_cpt_online);
322 cpumask_var_t *cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
324 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
326 return cpt == CFS_CPT_ANY ?
327 &cptab->ctb_cpumask : &cptab->ctb_parts[cpt].cpt_cpumask;
329 EXPORT_SYMBOL(cfs_cpt_cpumask);
331 nodemask_t *cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
333 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
335 return cpt == CFS_CPT_ANY ?
336 cptab->ctb_nodemask : cptab->ctb_parts[cpt].cpt_nodemask;
338 EXPORT_SYMBOL(cfs_cpt_nodemask);
340 unsigned int cfs_cpt_distance(struct cfs_cpt_table *cptab, int cpt1, int cpt2)
342 LASSERT(cpt1 == CFS_CPT_ANY || (cpt1 >= 0 && cpt1 < cptab->ctb_nparts));
343 LASSERT(cpt2 == CFS_CPT_ANY || (cpt2 >= 0 && cpt2 < cptab->ctb_nparts));
345 if (cpt1 == CFS_CPT_ANY || cpt2 == CFS_CPT_ANY)
346 return cptab->ctb_distance;
348 return cptab->ctb_parts[cpt1].cpt_distance[cpt2];
350 EXPORT_SYMBOL(cfs_cpt_distance);
353 * Calculate the maximum NUMA distance between all nodes in the
354 * from_mask and all nodes in the to_mask.
356 static unsigned int cfs_cpt_distance_calculate(nodemask_t *from_mask,
359 unsigned int maximum;
360 unsigned int distance;
365 for_each_node_mask(from, *from_mask) {
366 for_each_node_mask(to, *to_mask) {
367 distance = node_distance(from, to);
368 if (maximum < distance)
375 static void cfs_cpt_add_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
377 cptab->ctb_cpu2cpt[cpu] = cpt;
379 cpumask_set_cpu(cpu, cptab->ctb_cpumask);
380 cpumask_set_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
383 static void cfs_cpt_del_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
385 cpumask_clear_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
386 cpumask_clear_cpu(cpu, cptab->ctb_cpumask);
388 cptab->ctb_cpu2cpt[cpu] = -1;
391 static void cfs_cpt_add_node(struct cfs_cpt_table *cptab, int cpt, int node)
393 struct cfs_cpu_partition *part;
395 if (!node_isset(node, *cptab->ctb_nodemask)) {
398 /* first time node is added to the CPT table */
399 node_set(node, *cptab->ctb_nodemask);
400 cptab->ctb_node2cpt[node] = cpt;
402 dist = cfs_cpt_distance_calculate(cptab->ctb_nodemask,
403 cptab->ctb_nodemask);
404 cptab->ctb_distance = dist;
407 part = &cptab->ctb_parts[cpt];
408 if (!node_isset(node, *part->cpt_nodemask)) {
411 /* first time node is added to this CPT */
412 node_set(node, *part->cpt_nodemask);
413 for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
414 struct cfs_cpu_partition *part2;
417 part2 = &cptab->ctb_parts[cpt2];
418 dist = cfs_cpt_distance_calculate(part->cpt_nodemask,
419 part2->cpt_nodemask);
420 part->cpt_distance[cpt2] = dist;
421 dist = cfs_cpt_distance_calculate(part2->cpt_nodemask,
423 part2->cpt_distance[cpt] = dist;
428 static void cfs_cpt_del_node(struct cfs_cpt_table *cptab, int cpt, int node)
430 struct cfs_cpu_partition *part = &cptab->ctb_parts[cpt];
433 for_each_cpu(cpu, part->cpt_cpumask) {
434 /* this CPT has other CPU belonging to this node? */
435 if (cpu_to_node(cpu) == node)
439 if (cpu >= nr_cpu_ids && node_isset(node, *part->cpt_nodemask)) {
442 /* No more CPUs in the node for this CPT. */
443 node_clear(node, *part->cpt_nodemask);
444 for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
445 struct cfs_cpu_partition *part2;
448 part2 = &cptab->ctb_parts[cpt2];
449 if (node_isset(node, *part2->cpt_nodemask))
450 cptab->ctb_node2cpt[node] = cpt2;
452 dist = cfs_cpt_distance_calculate(part->cpt_nodemask,
453 part2->cpt_nodemask);
454 part->cpt_distance[cpt2] = dist;
455 dist = cfs_cpt_distance_calculate(part2->cpt_nodemask,
457 part2->cpt_distance[cpt] = dist;
461 for_each_cpu(cpu, cptab->ctb_cpumask) {
462 /* this CPT-table has other CPUs belonging to this node? */
463 if (cpu_to_node(cpu) == node)
467 if (cpu >= nr_cpu_ids && node_isset(node, *cptab->ctb_nodemask)) {
468 /* No more CPUs in the table for this node. */
469 node_clear(node, *cptab->ctb_nodemask);
470 cptab->ctb_node2cpt[node] = -1;
471 cptab->ctb_distance =
472 cfs_cpt_distance_calculate(cptab->ctb_nodemask,
473 cptab->ctb_nodemask);
477 int cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
479 LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
481 if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu)) {
482 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
486 if (cptab->ctb_cpu2cpt[cpu] != -1) {
487 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
488 cpu, cptab->ctb_cpu2cpt[cpu]);
492 if (cpumask_test_cpu(cpu, cptab->ctb_cpumask)) {
493 CDEBUG(D_INFO, "CPU %d is already in cpumask\n", cpu);
497 if (cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask)) {
498 CDEBUG(D_INFO, "CPU %d is already in partition %d cpumask\n",
499 cpu, cptab->ctb_cpu2cpt[cpu]);
503 cfs_cpt_add_cpu(cptab, cpt, cpu);
504 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
508 EXPORT_SYMBOL(cfs_cpt_set_cpu);
510 void cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
512 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
514 if (cpu < 0 || cpu >= nr_cpu_ids) {
515 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
519 if (cpt == CFS_CPT_ANY) {
520 /* caller doesn't know the partition ID */
521 cpt = cptab->ctb_cpu2cpt[cpu];
522 if (cpt < 0) { /* not set in this CPT-table */
524 "Try to unset cpu %d which is not in CPT-table %p\n",
529 } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
531 "CPU %d is not in CPU partition %d\n", cpu, cpt);
535 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
536 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_cpumask));
538 cfs_cpt_del_cpu(cptab, cpt, cpu);
539 cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
541 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
543 int cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt,
544 const cpumask_t *mask)
548 if (!cpumask_weight(mask) ||
549 cpumask_any_and(mask, cpu_online_mask) >= nr_cpu_ids) {
551 "No online CPU is found in the CPU mask for CPU partition %d\n",
556 for_each_cpu(cpu, mask) {
557 cfs_cpt_add_cpu(cptab, cpt, cpu);
558 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
563 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
565 void cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt,
566 const cpumask_t *mask)
570 for_each_cpu(cpu, mask) {
571 cfs_cpt_del_cpu(cptab, cpt, cpu);
572 cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
575 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
577 int cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
579 const cpumask_t *mask;
582 if (node < 0 || node >= nr_node_ids) {
584 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
588 mask = cpumask_of_node(node);
590 for_each_cpu(cpu, mask)
591 cfs_cpt_add_cpu(cptab, cpt, cpu);
593 cfs_cpt_add_node(cptab, cpt, node);
597 EXPORT_SYMBOL(cfs_cpt_set_node);
599 void cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
601 const cpumask_t *mask;
604 if (node < 0 || node >= nr_node_ids) {
606 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
610 mask = cpumask_of_node(node);
612 for_each_cpu(cpu, mask)
613 cfs_cpt_del_cpu(cptab, cpt, cpu);
615 cfs_cpt_del_node(cptab, cpt, node);
617 EXPORT_SYMBOL(cfs_cpt_unset_node);
619 int cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt,
620 const nodemask_t *mask)
624 for_each_node_mask(node, *mask)
625 cfs_cpt_set_node(cptab, cpt, node);
629 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
631 void cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt,
632 const nodemask_t *mask)
636 for_each_node_mask(node, *mask)
637 cfs_cpt_unset_node(cptab, cpt, node);
639 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
641 int cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
648 /* convert CPU partition ID to HW node id */
650 if (cpt < 0 || cpt >= cptab->ctb_nparts) {
651 mask = cptab->ctb_nodemask;
652 rotor = cptab->ctb_spread_rotor++;
654 mask = cptab->ctb_parts[cpt].cpt_nodemask;
655 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
656 node = cptab->ctb_parts[cpt].cpt_node;
659 weight = nodes_weight(*mask);
663 for_each_node_mask(node, *mask) {
671 EXPORT_SYMBOL(cfs_cpt_spread_node);
673 int cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
679 cpu = smp_processor_id();
680 cpt = cptab->ctb_cpu2cpt[cpu];
682 if (cpt < 0 && remap) {
683 /* don't return negative value for safety of upper layer,
684 * instead we shadow the unknown cpu to a valid partition ID
686 cpt = cpu % cptab->ctb_nparts;
691 EXPORT_SYMBOL(cfs_cpt_current);
693 int cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
695 LASSERT(cpu >= 0 && cpu < nr_cpu_ids);
697 return cptab->ctb_cpu2cpt[cpu];
699 EXPORT_SYMBOL(cfs_cpt_of_cpu);
701 int cfs_cpt_of_node(struct cfs_cpt_table *cptab, int node)
703 if (node < 0 || node > nr_node_ids)
706 return cptab->ctb_node2cpt[node];
708 EXPORT_SYMBOL(cfs_cpt_of_node);
710 int cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
712 nodemask_t *nodemask;
717 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
719 if (cpt == CFS_CPT_ANY) {
720 cpumask = cptab->ctb_cpumask;
721 nodemask = cptab->ctb_nodemask;
723 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
724 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
727 if (!cpumask_intersects(cpumask, cpu_online_mask)) {
729 "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",
734 for_each_online_cpu(cpu) {
735 if (cpumask_test_cpu(cpu, cpumask))
738 rc = set_cpus_allowed_ptr(current, cpumask);
739 set_mems_allowed(*nodemask);
741 schedule(); /* switch to allowed CPU */
746 /* don't need to set affinity because all online CPUs are covered */
749 EXPORT_SYMBOL(cfs_cpt_bind);
752 * Choose max to \a number CPUs from \a node and set them in \a cpt.
753 * We always prefer to choose CPU in the same core/socket.
755 static int cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
756 cpumask_t *node_mask, int number)
758 cpumask_var_t socket_mask;
759 cpumask_var_t core_mask;
766 if (number >= cpumask_weight(node_mask)) {
767 while (!cpumask_empty(node_mask)) {
768 cpu = cpumask_first(node_mask);
769 cpumask_clear_cpu(cpu, node_mask);
771 if (!cpu_online(cpu))
774 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
782 * Allocate scratch buffers
783 * As we cannot initialize a cpumask_var_t, we need
784 * to alloc both before we can risk trying to free either
786 if (!zalloc_cpumask_var(&socket_mask, GFP_NOFS))
788 if (!zalloc_cpumask_var(&core_mask, GFP_NOFS))
793 while (!cpumask_empty(node_mask)) {
794 cpu = cpumask_first(node_mask);
796 /* get cpumask for cores in the same socket */
797 cpumask_and(socket_mask, topology_core_cpumask(cpu), node_mask);
798 while (!cpumask_empty(socket_mask)) {
799 /* get cpumask for hts in the same core */
800 cpumask_and(core_mask, topology_sibling_cpumask(cpu),
803 for_each_cpu(i, core_mask) {
804 cpumask_clear_cpu(i, socket_mask);
805 cpumask_clear_cpu(i, node_mask);
810 rc = cfs_cpt_set_cpu(cptab, cpt, i);
819 cpu = cpumask_first(socket_mask);
824 free_cpumask_var(socket_mask);
825 free_cpumask_var(core_mask);
829 #define CPT_WEIGHT_MIN 4
831 static int cfs_cpt_num_estimate(void)
833 int nthr = cpumask_weight(topology_sibling_cpumask(smp_processor_id()));
834 int ncpu = num_online_cpus();
837 if (ncpu > CPT_WEIGHT_MIN)
838 for (ncpt = 2; ncpu > 2 * nthr * ncpt; ncpt++)
841 #if (BITS_PER_LONG == 32)
842 /* config many CPU partitions on 32-bit system could consume
848 ncpt--; /* worst case is 1 */
853 static struct cfs_cpt_table *cfs_cpt_table_create(int ncpt)
855 struct cfs_cpt_table *cptab = NULL;
856 cpumask_var_t node_mask;
863 num = cfs_cpt_num_estimate();
867 if (ncpt > num_online_cpus()) {
869 CERROR("libcfs: CPU partition count %d > cores %d: rc = %d\n",
870 ncpt, num_online_cpus(), rc);
874 if (ncpt > 4 * num) {
875 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",
879 cptab = cfs_cpt_table_alloc(ncpt);
881 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
886 if (!zalloc_cpumask_var(&node_mask, GFP_NOFS)) {
887 CERROR("Failed to allocate scratch cpumask\n");
892 num = num_online_cpus() / ncpt;
893 rem = num_online_cpus() % ncpt;
894 for_each_online_node(node) {
895 cpumask_copy(node_mask, cpumask_of_node(node));
897 while (cpt < ncpt && !cpumask_empty(node_mask)) {
898 struct cfs_cpu_partition *part = &cptab->ctb_parts[cpt];
899 int ncpu = cpumask_weight(part->cpt_cpumask);
901 rc = cfs_cpt_choose_ncpus(cptab, cpt, node_mask,
902 (rem > 0) + num - ncpu);
908 ncpu = cpumask_weight(part->cpt_cpumask);
909 if (ncpu == num + !!(rem > 0)) {
916 free_cpumask_var(node_mask);
921 free_cpumask_var(node_mask);
923 CERROR("Failed (rc = %d) to setup CPU partition table with %d partitions, online HW NUMA nodes: %d, HW CPU cores: %d.\n",
924 rc, ncpt, num_online_nodes(), num_online_cpus());
927 cfs_cpt_table_free(cptab);
932 static struct cfs_cpt_table *cfs_cpt_table_create_pattern(const char *pattern)
934 struct cfs_cpt_table *cptab;
946 pattern_dup = kstrdup(pattern, GFP_KERNEL);
948 CERROR("Failed to duplicate pattern '%s'\n", pattern);
949 return ERR_PTR(-ENOMEM);
952 str = strim(pattern_dup);
953 if (*str == 'n' || *str == 'N') {
954 str++; /* skip 'N' char */
955 node = 1; /* NUMA pattern */
958 for_each_online_node(i) {
959 if (!cpumask_empty(cpumask_of_node(i)))
962 if (ncpt == 1) { /* single NUMA node */
964 return cfs_cpt_table_create(cpu_npartitions);
969 if (!ncpt) { /* scanning bracket which is mark of partition */
971 while ((bracket = strchr(bracket, '['))) {
978 (node && ncpt > num_online_nodes()) ||
979 (!node && ncpt > num_online_cpus())) {
980 CERROR("Invalid pattern '%s', or too many partitions %d\n",
986 cptab = cfs_cpt_table_alloc(ncpt);
988 CERROR("Failed to allocate CPU partition table\n");
993 if (node < 0) { /* shortcut to create CPT from NUMA & CPU topology */
994 for_each_online_node(i) {
995 if (cpumask_empty(cpumask_of_node(i)))
998 rc = cfs_cpt_set_node(cptab, cpt++, i);
1001 goto err_free_table;
1008 high = node ? nr_node_ids - 1 : nr_cpu_ids - 1;
1010 for (str = strim(str), c = 0; /* until break */; c++) {
1011 struct cfs_range_expr *range;
1012 struct cfs_expr_list *el;
1015 bracket = strchr(str, '[');
1018 CERROR("Invalid pattern '%s'\n", str);
1020 goto err_free_table;
1021 } else if (c != ncpt) {
1022 CERROR("Expect %d partitions but found %d\n",
1025 goto err_free_table;
1030 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
1031 CERROR("Invalid CPU pattern '%s'\n", str);
1033 goto err_free_table;
1036 if (cpt < 0 || cpt >= ncpt) {
1037 CERROR("Invalid partition id %d, total partitions %d\n",
1040 goto err_free_table;
1043 if (cfs_cpt_weight(cptab, cpt)) {
1044 CERROR("Partition %d has already been set.\n", cpt);
1046 goto err_free_table;
1049 str = strim(str + n);
1050 if (str != bracket) {
1051 CERROR("Invalid pattern '%s'\n", str);
1053 goto err_free_table;
1056 bracket = strchr(str, ']');
1058 CERROR("Missing right bracket for partition %d in '%s'\n",
1061 goto err_free_table;
1064 rc = cfs_expr_list_parse(str, (bracket - str) + 1, 0, high,
1067 CERROR("Can't parse number range in '%s'\n", str);
1069 goto err_free_table;
1072 list_for_each_entry(range, &el->el_exprs, re_link) {
1073 for (i = range->re_lo; i <= range->re_hi; i++) {
1074 if ((i - range->re_lo) % range->re_stride)
1077 rc = node ? cfs_cpt_set_node(cptab, cpt, i)
1078 : cfs_cpt_set_cpu(cptab, cpt, i);
1080 cfs_expr_list_free(el);
1082 goto err_free_table;
1087 cfs_expr_list_free(el);
1089 if (!cfs_cpt_online(cptab, cpt)) {
1090 CERROR("No online CPU is found on partition %d\n", cpt);
1092 goto err_free_table;
1095 str = strim(bracket + 1);
1102 cfs_cpt_table_free(cptab);
1108 #ifdef CONFIG_HOTPLUG_CPU
1109 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1110 static enum cpuhp_state lustre_cpu_online;
1112 static int cfs_cpu_online(unsigned int cpu)
1118 static int cfs_cpu_dead(unsigned int cpu)
1122 /* if all HTs in a core are offline, it may break affinity */
1123 warn = cpumask_any_and(topology_sibling_cpumask(cpu),
1124 cpu_online_mask) >= nr_cpu_ids;
1125 CDEBUG(warn ? D_WARNING : D_INFO,
1126 "Lustre: can't support CPU plug-out well now, performance and stability could be impacted [CPU %u]\n",
1131 #ifndef HAVE_HOTPLUG_STATE_MACHINE
1132 static int cfs_cpu_notify(struct notifier_block *self, unsigned long action,
1135 int cpu = (unsigned long)hcpu;
1139 case CPU_DEAD_FROZEN:
1141 case CPU_ONLINE_FROZEN:
1143 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
1144 CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
1155 static struct notifier_block cfs_cpu_notifier = {
1156 .notifier_call = cfs_cpu_notify,
1159 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1160 #endif /* CONFIG_HOTPLUG_CPU */
1162 void cfs_cpu_fini(void)
1164 if (!IS_ERR_OR_NULL(cfs_cpt_table))
1165 cfs_cpt_table_free(cfs_cpt_table);
1167 #ifdef CONFIG_HOTPLUG_CPU
1168 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1169 if (lustre_cpu_online > 0)
1170 cpuhp_remove_state_nocalls(lustre_cpu_online);
1171 cpuhp_remove_state_nocalls(CPUHP_LUSTRE_CFS_DEAD);
1173 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1174 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1175 #endif /* CONFIG_HOTPLUG_CPU */
1178 int cfs_cpu_init(void)
1182 LASSERT(!cfs_cpt_table);
1184 #ifdef CONFIG_HOTPLUG_CPU
1185 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1186 ret = cpuhp_setup_state_nocalls(CPUHP_LUSTRE_CFS_DEAD,
1187 "fs/lustre/cfe:dead", NULL,
1190 goto failed_cpu_dead;
1192 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
1193 "fs/lustre/cfe:online",
1194 cfs_cpu_online, NULL);
1196 goto failed_cpu_online;
1198 lustre_cpu_online = ret;
1200 register_hotcpu_notifier(&cfs_cpu_notifier);
1201 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1202 #endif /* CONFIG_HOTPLUG_CPU */
1206 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern);
1207 if (IS_ERR(cfs_cpt_table)) {
1208 CERROR("Failed to create cptab from pattern '%s'\n",
1210 ret = PTR_ERR(cfs_cpt_table);
1211 goto failed_alloc_table;
1215 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1216 if (IS_ERR(cfs_cpt_table)) {
1217 CERROR("Failed to create cptab with npartitions %d\n",
1219 ret = PTR_ERR(cfs_cpt_table);
1220 goto failed_alloc_table;
1226 LCONSOLE(0, "HW NUMA nodes: %d, HW CPU cores: %d, npartitions: %d\n",
1227 num_online_nodes(), num_online_cpus(),
1228 cfs_cpt_number(cfs_cpt_table));
1234 if (!IS_ERR_OR_NULL(cfs_cpt_table))
1235 cfs_cpt_table_free(cfs_cpt_table);
1237 #ifdef CONFIG_HOTPLUG_CPU
1238 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1239 if (lustre_cpu_online > 0)
1240 cpuhp_remove_state_nocalls(lustre_cpu_online);
1242 cpuhp_remove_state_nocalls(CPUHP_LUSTRE_CFS_DEAD);
1245 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1246 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1247 #endif /* CONFIG_HOTPLUG_CPU */
1251 #else /* ! CONFIG_SMP */
1253 struct cfs_cpt_table *cfs_cpt_table_alloc(int ncpt)
1255 struct cfs_cpt_table *cptab;
1258 CERROR("Can't support cpu partition number %d\n", ncpt);
1262 LIBCFS_ALLOC(cptab, sizeof(*cptab));
1266 cpumask_set_cpu(0, cptab->ctb_cpumask);
1267 node_set(0, cptab->ctb_nodemask);
1271 EXPORT_SYMBOL(cfs_cpt_table_alloc);
1273 int cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
1277 rc = snprintf(buf, len, "0\t: 0\n");
1284 EXPORT_SYMBOL(cfs_cpt_table_print);
1286 int cfs_cpt_distance_print(struct cfs_cpt_table *cptab, char *buf, int len)
1290 rc = snprintf(buf, len, "0\t: 0:1\n");
1297 EXPORT_SYMBOL(cfs_cpt_distance_print);
1299 void cfs_cpu_fini(void)
1301 if (cfs_cpt_table) {
1302 cfs_cpt_table_free(cfs_cpt_table);
1303 cfs_cpt_table = NULL;
1307 int cfs_cpu_init(void)
1309 cfs_cpt_table = cfs_cpt_table_alloc(1);
1311 return cfs_cpt_table ? 0 : -1;
1314 #endif /* !CONFIG_SMP */