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).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA
24 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
26 * Copyright (c) 2012, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Author: liang@whamcloud.com
35 #define DEBUG_SUBSYSTEM S_LNET
37 #include <linux/cpu.h>
38 #include <linux/sched.h>
39 #include <libcfs/libcfs.h>
44 * modparam for setting number of partitions
46 * 0 : estimate best value based on cores or NUMA nodes
47 * 1 : disable multiple partitions
48 * >1 : specify number of partitions
50 static int cpu_npartitions;
51 module_param(cpu_npartitions, int, 0444);
52 MODULE_PARM_DESC(cpu_npartitions, "# of CPU partitions");
55 * modparam for setting CPU partitions patterns:
57 * i.e: "0[0,1,2,3] 1[4,5,6,7]", number before bracket is CPU partition ID,
58 * number in bracket is processor ID (core or HT)
60 * i.e: "N 0[0,1] 1[2,3]" the first character 'N' means numbers in bracket
61 * are NUMA node ID, number before bracket is CPU partition ID.
63 * i.e: "N", shortcut expression to create CPT from NUMA & CPU topology
65 * NB: If user specified cpu_pattern, cpu_npartitions will be ignored
67 static char *cpu_pattern = "N";
68 module_param(cpu_pattern, charp, 0444);
69 MODULE_PARM_DESC(cpu_pattern, "CPU partitions pattern");
71 void cfs_cpt_table_free(struct cfs_cpt_table *cptab)
75 if (cptab->ctb_cpu2cpt != NULL) {
76 LIBCFS_FREE(cptab->ctb_cpu2cpt,
77 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
80 if (cptab->ctb_node2cpt != NULL) {
81 LIBCFS_FREE(cptab->ctb_node2cpt,
82 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
85 for (i = 0; cptab->ctb_parts != NULL && i < cptab->ctb_nparts; i++) {
86 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
88 if (part->cpt_nodemask != NULL) {
89 LIBCFS_FREE(part->cpt_nodemask,
90 sizeof(*part->cpt_nodemask));
93 if (part->cpt_cpumask != NULL)
94 LIBCFS_FREE(part->cpt_cpumask, cpumask_size());
96 if (part->cpt_distance) {
97 LIBCFS_FREE(part->cpt_distance,
99 sizeof(part->cpt_distance[0]));
103 if (cptab->ctb_parts != NULL) {
104 LIBCFS_FREE(cptab->ctb_parts,
105 cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
108 if (cptab->ctb_nodemask != NULL)
109 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
110 if (cptab->ctb_cpumask != NULL)
111 LIBCFS_FREE(cptab->ctb_cpumask, cpumask_size());
113 LIBCFS_FREE(cptab, sizeof(*cptab));
115 EXPORT_SYMBOL(cfs_cpt_table_free);
117 struct cfs_cpt_table *cfs_cpt_table_alloc(int ncpt)
119 struct cfs_cpt_table *cptab;
122 LIBCFS_ALLOC(cptab, sizeof(*cptab));
126 cptab->ctb_nparts = ncpt;
128 LIBCFS_ALLOC(cptab->ctb_cpumask, cpumask_size());
129 LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
131 if (cptab->ctb_cpumask == NULL || cptab->ctb_nodemask == NULL)
134 LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
135 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
136 if (cptab->ctb_cpu2cpt == NULL)
139 memset(cptab->ctb_cpu2cpt, -1,
140 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
142 LIBCFS_ALLOC(cptab->ctb_node2cpt,
143 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
144 if (cptab->ctb_node2cpt == NULL)
147 memset(cptab->ctb_node2cpt, -1,
148 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
150 LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
151 if (cptab->ctb_parts == NULL)
154 for (i = 0; i < ncpt; i++) {
155 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
157 LIBCFS_ALLOC(part->cpt_cpumask, cpumask_size());
158 if (!part->cpt_cpumask)
161 LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
162 if (!part->cpt_nodemask)
165 LIBCFS_ALLOC(part->cpt_distance,
166 cptab->ctb_nparts * sizeof(part->cpt_distance[0]));
167 if (!part->cpt_distance)
174 cfs_cpt_table_free(cptab);
177 EXPORT_SYMBOL(cfs_cpt_table_alloc);
179 int cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
186 for (i = 0; i < cptab->ctb_nparts; i++) {
190 rc = snprintf(tmp, len, "%d\t:", i);
197 for_each_cpu(j, cptab->ctb_parts[i].cpt_cpumask) {
198 rc = snprintf(tmp, len, " %d", j);
216 EXPORT_SYMBOL(cfs_cpt_table_print);
218 int cfs_cpt_distance_print(struct cfs_cpt_table *cptab, char *buf, int len)
225 for (i = 0; i < cptab->ctb_nparts; i++) {
229 rc = snprintf(tmp, len, "%d\t:", i);
236 for (j = 0; j < cptab->ctb_nparts; j++) {
237 rc = snprintf(tmp, len, " %d:%d",
238 j, cptab->ctb_parts[i].cpt_distance[j]);
256 EXPORT_SYMBOL(cfs_cpt_distance_print);
258 int cfs_cpt_number(struct cfs_cpt_table *cptab)
260 return cptab->ctb_nparts;
262 EXPORT_SYMBOL(cfs_cpt_number);
264 int cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
266 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
268 return cpt == CFS_CPT_ANY ?
269 cpumask_weight(cptab->ctb_cpumask) :
270 cpumask_weight(cptab->ctb_parts[cpt].cpt_cpumask);
272 EXPORT_SYMBOL(cfs_cpt_weight);
274 int cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
276 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
278 return cpt == CFS_CPT_ANY ?
279 cpumask_any_and(cptab->ctb_cpumask,
280 cpu_online_mask) < nr_cpu_ids :
281 cpumask_any_and(cptab->ctb_parts[cpt].cpt_cpumask,
282 cpu_online_mask) < nr_cpu_ids;
284 EXPORT_SYMBOL(cfs_cpt_online);
286 cpumask_t *cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
288 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
290 return cpt == CFS_CPT_ANY ?
291 cptab->ctb_cpumask : cptab->ctb_parts[cpt].cpt_cpumask;
293 EXPORT_SYMBOL(cfs_cpt_cpumask);
295 nodemask_t *cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
297 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
299 return cpt == CFS_CPT_ANY ?
300 cptab->ctb_nodemask : cptab->ctb_parts[cpt].cpt_nodemask;
302 EXPORT_SYMBOL(cfs_cpt_nodemask);
304 unsigned cfs_cpt_distance(struct cfs_cpt_table *cptab, int cpt1, int cpt2)
306 LASSERT(cpt1 == CFS_CPT_ANY || (cpt1 >= 0 && cpt1 < cptab->ctb_nparts));
307 LASSERT(cpt2 == CFS_CPT_ANY || (cpt2 >= 0 && cpt2 < cptab->ctb_nparts));
309 if (cpt1 == CFS_CPT_ANY || cpt2 == CFS_CPT_ANY)
310 return cptab->ctb_distance;
312 return cptab->ctb_parts[cpt1].cpt_distance[cpt2];
314 EXPORT_SYMBOL(cfs_cpt_distance);
317 * Calculate the maximum NUMA distance between all nodes in the
318 * from_mask and all nodes in the to_mask.
320 static unsigned cfs_cpt_distance_calculate(nodemask_t *from_mask,
329 for_each_node_mask(from, *from_mask) {
330 for_each_node_mask(to, *to_mask) {
331 distance = node_distance(from, to);
332 if (maximum < distance)
339 static void cfs_cpt_add_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
341 cptab->ctb_cpu2cpt[cpu] = cpt;
343 cpumask_set_cpu(cpu, cptab->ctb_cpumask);
344 cpumask_set_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
347 static void cfs_cpt_del_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
349 cpumask_clear_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
350 cpumask_clear_cpu(cpu, cptab->ctb_cpumask);
352 cptab->ctb_cpu2cpt[cpu] = -1;
355 static void cfs_cpt_add_node(struct cfs_cpt_table *cptab, int cpt, int node)
358 struct cfs_cpu_partition *part;
359 struct cfs_cpu_partition *part2;
361 if (!node_isset(node, *cptab->ctb_nodemask)) {
362 /* first time node is added to the CPT table */
363 node_set(node, *cptab->ctb_nodemask);
364 cptab->ctb_node2cpt[node] = cpt;
365 cptab->ctb_distance = cfs_cpt_distance_calculate(
367 cptab->ctb_nodemask);
370 part = &cptab->ctb_parts[cpt];
371 if (!node_isset(node, *part->cpt_nodemask)) {
372 /* first time node is added to this CPT */
373 node_set(node, *part->cpt_nodemask);
374 for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
375 part2 = &cptab->ctb_parts[cpt2];
376 part->cpt_distance[cpt2] = cfs_cpt_distance_calculate(
378 part2->cpt_nodemask);
379 part2->cpt_distance[cpt] = cfs_cpt_distance_calculate(
386 static void cfs_cpt_del_node(struct cfs_cpt_table *cptab, int cpt, int node)
390 struct cfs_cpu_partition *part;
391 struct cfs_cpu_partition *part2;
393 part = &cptab->ctb_parts[cpt];
395 for_each_cpu(cpu, part->cpt_cpumask) {
396 /* this CPT has other CPU belonging to this node? */
397 if (cpu_to_node(cpu) == node)
401 if (cpu >= nr_cpu_ids && node_isset(node, *part->cpt_nodemask)) {
402 /* No more CPUs in the node for this CPT. */
403 node_clear(node, *part->cpt_nodemask);
404 for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
405 part2 = &cptab->ctb_parts[cpt2];
406 if (node_isset(node, *part2->cpt_nodemask))
407 cptab->ctb_node2cpt[node] = cpt2;
408 part->cpt_distance[cpt2] = cfs_cpt_distance_calculate(
410 part2->cpt_nodemask);
411 part2->cpt_distance[cpt] = cfs_cpt_distance_calculate(
417 for_each_cpu(cpu, cptab->ctb_cpumask) {
418 /* this CPT-table has other CPUs belonging to this node? */
419 if (cpu_to_node(cpu) == node)
423 if (cpu >= nr_cpu_ids && node_isset(node, *cptab->ctb_nodemask)) {
424 /* No more CPUs in the table for this node. */
425 node_clear(node, *cptab->ctb_nodemask);
426 cptab->ctb_node2cpt[node] = -1;
427 cptab->ctb_distance =
428 cfs_cpt_distance_calculate(cptab->ctb_nodemask,
429 cptab->ctb_nodemask);
433 int cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
435 LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
437 if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu)) {
438 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
442 if (cptab->ctb_cpu2cpt[cpu] != -1) {
443 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
444 cpu, cptab->ctb_cpu2cpt[cpu]);
448 LASSERT(!cpumask_test_cpu(cpu, cptab->ctb_cpumask));
449 LASSERT(!cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
451 cfs_cpt_add_cpu(cptab, cpt, cpu);
452 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
456 EXPORT_SYMBOL(cfs_cpt_set_cpu);
458 void cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
460 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
462 if (cpu < 0 || cpu >= nr_cpu_ids) {
463 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
467 if (cpt == CFS_CPT_ANY) {
468 /* caller doesn't know the partition ID */
469 cpt = cptab->ctb_cpu2cpt[cpu];
470 if (cpt < 0) { /* not set in this CPT-table */
471 CDEBUG(D_INFO, "Try to unset cpu %d which is "
472 "not in CPT-table %p\n", cpt, cptab);
476 } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
478 "CPU %d is not in cpu-partition %d\n", cpu, cpt);
482 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
483 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_cpumask));
485 cfs_cpt_del_cpu(cptab, cpt, cpu);
486 cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
488 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
490 int cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt,
491 const cpumask_t *mask)
495 if (cpumask_weight(mask) == 0 ||
496 cpumask_any_and(mask, cpu_online_mask) >= nr_cpu_ids) {
497 CDEBUG(D_INFO, "No online CPU is found in the CPU mask "
498 "for CPU partition %d\n", cpt);
502 for_each_cpu(cpu, mask) {
503 cfs_cpt_add_cpu(cptab, cpt, cpu);
504 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
509 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
511 void cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt,
512 const cpumask_t *mask)
516 for_each_cpu(cpu, mask)
517 cfs_cpt_unset_cpu(cptab, cpt, cpu);
519 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
521 int cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
523 const cpumask_t *mask;
526 if (node < 0 || node >= nr_node_ids) {
528 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
532 mask = cpumask_of_node(node);
534 for_each_cpu(cpu, mask)
535 cfs_cpt_add_cpu(cptab, cpt, cpu);
537 cfs_cpt_add_node(cptab, cpt, node);
541 EXPORT_SYMBOL(cfs_cpt_set_node);
543 void cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
545 const cpumask_t *mask;
548 if (node < 0 || node >= nr_node_ids) {
550 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
554 mask = cpumask_of_node(node);
556 for_each_cpu(cpu, mask)
557 cfs_cpt_del_cpu(cptab, cpt, cpu);
559 cfs_cpt_del_node(cptab, cpt, node);
561 EXPORT_SYMBOL(cfs_cpt_unset_node);
563 int cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt,
564 const nodemask_t *mask)
568 for_each_node_mask(i, *mask) {
569 if (!cfs_cpt_set_node(cptab, cpt, i))
575 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
577 void cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt,
578 const nodemask_t *mask)
582 for_each_node_mask(i, *mask)
583 cfs_cpt_unset_node(cptab, cpt, i);
585 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
587 int cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
594 /* convert CPU partition ID to HW node id */
596 if (cpt < 0 || cpt >= cptab->ctb_nparts) {
597 mask = cptab->ctb_nodemask;
598 rotor = cptab->ctb_spread_rotor++;
600 mask = cptab->ctb_parts[cpt].cpt_nodemask;
601 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
604 weight = nodes_weight(*mask);
609 for_each_node_mask(node, *mask) {
617 EXPORT_SYMBOL(cfs_cpt_spread_node);
619 int cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
621 int cpu = smp_processor_id();
622 int cpt = cptab->ctb_cpu2cpt[cpu];
628 /* don't return negative value for safety of upper layer,
629 * instead we shadow the unknown cpu to a valid partition ID */
630 cpt = cpu % cptab->ctb_nparts;
635 EXPORT_SYMBOL(cfs_cpt_current);
637 int cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
639 LASSERT(cpu >= 0 && cpu < nr_cpu_ids);
641 return cptab->ctb_cpu2cpt[cpu];
643 EXPORT_SYMBOL(cfs_cpt_of_cpu);
645 int cfs_cpt_of_node(struct cfs_cpt_table *cptab, int node)
647 if (node < 0 || node > nr_node_ids)
650 return cptab->ctb_node2cpt[node];
652 EXPORT_SYMBOL(cfs_cpt_of_node);
654 int cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
657 nodemask_t *nodemask;
661 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
663 if (cpt == CFS_CPT_ANY) {
664 cpumask = cptab->ctb_cpumask;
665 nodemask = cptab->ctb_nodemask;
667 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
668 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
671 if (cpumask_any_and(cpumask, cpu_online_mask) >= nr_cpu_ids) {
672 CERROR("No online CPU found in CPU partition %d, did someone "
673 "do CPU hotplug on system? You might need to reload "
674 "Lustre modules to keep system working well.\n", cpt);
678 for_each_online_cpu(i) {
679 if (cpumask_test_cpu(i, cpumask))
682 rc = set_cpus_allowed_ptr(current, cpumask);
683 set_mems_allowed(*nodemask);
685 schedule(); /* switch to allowed CPU */
690 /* don't need to set affinity because all online CPUs are covered */
693 EXPORT_SYMBOL(cfs_cpt_bind);
696 * Choose max to \a number CPUs from \a node and set them in \a cpt.
697 * We always prefer to choose CPU in the same core/socket.
699 static int cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
700 cpumask_t *node, int number)
702 cpumask_t *socket = NULL;
703 cpumask_t *core = NULL;
710 if (number >= cpumask_weight(node)) {
711 while (!cpumask_empty(node)) {
712 cpu = cpumask_first(node);
714 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
717 cpumask_clear_cpu(cpu, node);
722 /* allocate scratch buffer */
723 LIBCFS_ALLOC(socket, cpumask_size());
724 LIBCFS_ALLOC(core, cpumask_size());
725 if (socket == NULL || core == NULL) {
730 while (!cpumask_empty(node)) {
731 cpu = cpumask_first(node);
733 /* get cpumask for cores in the same socket */
734 cpumask_copy(socket, topology_core_cpumask(cpu));
735 cpumask_and(socket, socket, node);
737 LASSERT(!cpumask_empty(socket));
739 while (!cpumask_empty(socket)) {
740 /* get cpumask for hts in the same core */
741 cpumask_copy(core, topology_sibling_cpumask(cpu));
742 cpumask_and(core, core, node);
744 LASSERT(!cpumask_empty(core));
746 for_each_cpu(i, core) {
747 cpumask_clear_cpu(i, socket);
748 cpumask_clear_cpu(i, node);
750 rc = cfs_cpt_set_cpu(cptab, cpt, i);
759 cpu = cpumask_first(socket);
765 LIBCFS_FREE(socket, cpumask_size());
767 LIBCFS_FREE(core, cpumask_size());
771 #define CPT_WEIGHT_MIN 4
773 static int cfs_cpt_num_estimate(void)
775 int nnode = num_online_nodes();
776 int ncpu = num_online_cpus();
779 if (ncpu <= CPT_WEIGHT_MIN) {
784 /* generate reasonable number of CPU partitions based on total number
785 * of CPUs, Preferred N should be power2 and match this condition:
786 * 2 * (N - 1)^2 < NCPUS <= 2 * N^2 */
787 for (ncpt = 2; ncpu > 2 * ncpt * ncpt; ncpt <<= 1) {}
789 if (ncpt <= nnode) { /* fat numa system */
793 } else { /* ncpt > nnode */
794 while ((nnode << 1) <= ncpt)
801 #if (BITS_PER_LONG == 32)
802 /* config many CPU partitions on 32-bit system could consume
806 while (ncpu % ncpt != 0)
807 ncpt--; /* worst case is 1 */
812 static struct cfs_cpt_table *cfs_cpt_table_create(int ncpt)
814 struct cfs_cpt_table *cptab = NULL;
815 cpumask_t *mask = NULL;
821 rc = cfs_cpt_num_estimate();
825 if (ncpt > num_online_cpus() || ncpt > 4 * rc) {
826 CWARN("CPU partition number %d is larger than suggested "
827 "value (%d), your system may have performance"
828 "issue or run out of memory while under pressure\n",
832 if (num_online_cpus() % ncpt != 0) {
833 CERROR("CPU number %d is not multiple of cpu_npartition %d, "
834 "please try different cpu_npartitions value or"
835 "set pattern string by cpu_pattern=STRING\n",
836 (int)num_online_cpus(), ncpt);
840 cptab = cfs_cpt_table_alloc(ncpt);
842 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
846 num = num_online_cpus() / ncpt;
848 CERROR("CPU changed while setting CPU partition\n");
852 LIBCFS_ALLOC(mask, cpumask_size());
854 CERROR("Failed to allocate scratch cpumask\n");
858 for_each_online_node(i) {
859 cpumask_copy(mask, cpumask_of_node(i));
861 while (!cpumask_empty(mask)) {
862 struct cfs_cpu_partition *part;
865 /* Each emulated NUMA node has all allowed CPUs in
867 * End loop when all partitions have assigned CPUs.
872 part = &cptab->ctb_parts[cpt];
874 n = num - cpumask_weight(part->cpt_cpumask);
877 rc = cfs_cpt_choose_ncpus(cptab, cpt, mask, n);
881 LASSERT(num >= cpumask_weight(part->cpt_cpumask));
882 if (num == cpumask_weight(part->cpt_cpumask))
888 num != cpumask_weight(cptab->ctb_parts[ncpt - 1].cpt_cpumask)) {
889 CERROR("Expect %d(%d) CPU partitions but got %d(%d), "
890 "CPU hotplug/unplug while setting?\n",
891 cptab->ctb_nparts, num, cpt,
892 cpumask_weight(cptab->ctb_parts[ncpt - 1].cpt_cpumask));
896 LIBCFS_FREE(mask, cpumask_size());
901 CERROR("Failed to setup CPU-partition-table with %d "
902 "CPU-partitions, online HW nodes: %d, HW cpus: %d.\n",
903 ncpt, num_online_nodes(), num_online_cpus());
906 LIBCFS_FREE(mask, cpumask_size());
909 cfs_cpt_table_free(cptab);
914 static struct cfs_cpt_table *
915 cfs_cpt_table_create_pattern(char *pattern)
917 struct cfs_cpt_table *cptab;
927 str = cfs_trimwhite(pattern);
928 if (*str == 'n' || *str == 'N') {
930 if (*pattern != '\0') {
931 node = 1; /* numa pattern */
933 } else { /* shortcut to create CPT from NUMA & CPU topology */
935 ncpt = num_online_nodes();
939 if (ncpt == 0) { /* scanning bracket which is mark of partition */
940 for (str = pattern;; str++, ncpt++) {
941 str = strchr(str, '[');
948 (node && ncpt > num_online_nodes()) ||
949 (!node && ncpt > num_online_cpus())) {
950 CERROR("Invalid pattern %s, or too many partitions %d\n",
955 cptab = cfs_cpt_table_alloc(ncpt);
957 CERROR("Failed to allocate cpu partition table\n");
961 if (node < 0) { /* shortcut to create CPT from NUMA & CPU topology */
963 for_each_online_node(i) {
965 CERROR("CPU changed while setting CPU "
966 "partition table, %d/%d\n", cpt, ncpt);
970 rc = cfs_cpt_set_node(cptab, cpt++, i);
977 high = node ? nr_node_ids - 1 : nr_cpu_ids - 1;
979 for (str = cfs_trimwhite(pattern), c = 0;; c++) {
980 struct cfs_range_expr *range;
981 struct cfs_expr_list *el;
982 char *bracket = strchr(str, '[');
985 if (bracket == NULL) {
987 CERROR("Invalid pattern %s\n", str);
989 } else if (c != ncpt) {
990 CERROR("expect %d partitions but found %d\n",
997 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
998 CERROR("Invalid cpu pattern %s\n", str);
1002 if (cpt < 0 || cpt >= ncpt) {
1003 CERROR("Invalid partition id %d, total partitions %d\n",
1008 if (cfs_cpt_weight(cptab, cpt) != 0) {
1009 CERROR("Partition %d has already been set.\n", cpt);
1013 str = cfs_trimwhite(str + n);
1014 if (str != bracket) {
1015 CERROR("Invalid pattern %s\n", str);
1019 bracket = strchr(str, ']');
1020 if (bracket == NULL) {
1021 CERROR("missing right bracket for cpt %d, %s\n",
1026 if (cfs_expr_list_parse(str, (bracket - str) + 1,
1027 0, high, &el) != 0) {
1028 CERROR("Can't parse number range: %s\n", str);
1032 list_for_each_entry(range, &el->el_exprs, re_link) {
1033 for (i = range->re_lo; i <= range->re_hi; i++) {
1034 if ((i - range->re_lo) % range->re_stride != 0)
1037 rc = node ? cfs_cpt_set_node(cptab, cpt, i) :
1038 cfs_cpt_set_cpu(cptab, cpt, i);
1040 cfs_expr_list_free(el);
1046 cfs_expr_list_free(el);
1048 if (!cfs_cpt_online(cptab, cpt)) {
1049 CERROR("No online CPU is found on partition %d\n", cpt);
1053 str = cfs_trimwhite(bracket + 1);
1059 cfs_cpt_table_free(cptab);
1063 #ifdef CONFIG_HOTPLUG_CPU
1064 static int cfs_cpu_notify(struct notifier_block *self, unsigned long action,
1067 int cpu = (unsigned long)hcpu;
1072 case CPU_DEAD_FROZEN:
1074 case CPU_ONLINE_FROZEN:
1076 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
1077 CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
1082 /* if all HTs in a core are offline, it may break affinity */
1083 warn = cpumask_any_and(topology_sibling_cpumask(cpu),
1084 cpu_online_mask) >= nr_cpu_ids;
1085 CDEBUG(warn ? D_WARNING : D_INFO,
1086 "Lustre: can't support CPU plug-out well now, "
1087 "performance and stability could be impacted"
1088 "[CPU %u action: %lx]\n", cpu, action);
1094 static struct notifier_block cfs_cpu_notifier = {
1095 .notifier_call = cfs_cpu_notify,
1101 void cfs_cpu_fini(void)
1103 if (cfs_cpt_table != NULL)
1104 cfs_cpt_table_free(cfs_cpt_table);
1106 #ifdef CONFIG_HOTPLUG_CPU
1107 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1111 int cfs_cpu_init(void)
1113 LASSERT(cfs_cpt_table == NULL);
1115 #ifdef CONFIG_HOTPLUG_CPU
1116 register_hotcpu_notifier(&cfs_cpu_notifier);
1119 if (*cpu_pattern != 0) {
1120 char *cpu_pattern_dup = kstrdup(cpu_pattern, GFP_KERNEL);
1122 if (cpu_pattern_dup == NULL) {
1123 CERROR("Failed to duplicate cpu_pattern\n");
1127 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern_dup);
1128 kfree(cpu_pattern_dup);
1129 if (cfs_cpt_table == NULL) {
1130 CERROR("Failed to create cptab from pattern %s\n",
1136 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1137 if (cfs_cpt_table == NULL) {
1138 CERROR("Failed to create ptable with npartitions %d\n",
1145 LCONSOLE(0, "HW nodes: %d, HW CPU cores: %d, npartitions: %d\n",
1146 num_online_nodes(), num_online_cpus(),
1147 cfs_cpt_number(cfs_cpt_table));