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");
72 cfs_cpt_table_free(struct cfs_cpt_table *cptab)
76 if (cptab->ctb_cpu2cpt != NULL) {
77 LIBCFS_FREE(cptab->ctb_cpu2cpt,
78 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
81 if (cptab->ctb_node2cpt != NULL) {
82 LIBCFS_FREE(cptab->ctb_node2cpt,
83 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
86 for (i = 0; cptab->ctb_parts != NULL && i < cptab->ctb_nparts; i++) {
87 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
89 if (part->cpt_nodemask != NULL) {
90 LIBCFS_FREE(part->cpt_nodemask,
91 sizeof(*part->cpt_nodemask));
94 if (part->cpt_cpumask != NULL)
95 LIBCFS_FREE(part->cpt_cpumask, cpumask_size());
97 if (part->cpt_distance) {
98 LIBCFS_FREE(part->cpt_distance,
100 sizeof(part->cpt_distance[0]));
104 if (cptab->ctb_parts != NULL) {
105 LIBCFS_FREE(cptab->ctb_parts,
106 cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
109 if (cptab->ctb_nodemask != NULL)
110 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
111 if (cptab->ctb_cpumask != NULL)
112 LIBCFS_FREE(cptab->ctb_cpumask, cpumask_size());
114 LIBCFS_FREE(cptab, sizeof(*cptab));
116 EXPORT_SYMBOL(cfs_cpt_table_free);
118 struct cfs_cpt_table *
119 cfs_cpt_table_alloc(unsigned int ncpt)
121 struct cfs_cpt_table *cptab;
124 LIBCFS_ALLOC(cptab, sizeof(*cptab));
128 cptab->ctb_nparts = ncpt;
130 LIBCFS_ALLOC(cptab->ctb_cpumask, cpumask_size());
131 LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
133 if (cptab->ctb_cpumask == NULL || cptab->ctb_nodemask == NULL)
136 LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
137 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
138 if (cptab->ctb_cpu2cpt == NULL)
141 memset(cptab->ctb_cpu2cpt, -1,
142 nr_cpu_ids * sizeof(cptab->ctb_cpu2cpt[0]));
144 LIBCFS_ALLOC(cptab->ctb_node2cpt,
145 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
146 if (cptab->ctb_node2cpt == NULL)
149 memset(cptab->ctb_node2cpt, -1,
150 nr_node_ids * sizeof(cptab->ctb_node2cpt[0]));
152 LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
153 if (cptab->ctb_parts == NULL)
156 for (i = 0; i < ncpt; i++) {
157 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
159 LIBCFS_ALLOC(part->cpt_cpumask, cpumask_size());
160 if (!part->cpt_cpumask)
163 LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
164 if (!part->cpt_nodemask)
167 LIBCFS_ALLOC(part->cpt_distance,
168 cptab->ctb_nparts * sizeof(part->cpt_distance[0]));
169 if (!part->cpt_distance)
176 cfs_cpt_table_free(cptab);
179 EXPORT_SYMBOL(cfs_cpt_table_alloc);
182 cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
189 for (i = 0; i < cptab->ctb_nparts; i++) {
193 rc = snprintf(tmp, len, "%d\t:", i);
200 for_each_cpu(j, cptab->ctb_parts[i].cpt_cpumask) {
201 rc = snprintf(tmp, len, " %d", j);
219 EXPORT_SYMBOL(cfs_cpt_table_print);
222 cfs_cpt_distance_print(struct cfs_cpt_table *cptab, char *buf, int len)
229 for (i = 0; i < cptab->ctb_nparts; i++) {
233 rc = snprintf(tmp, len, "%d\t:", i);
240 for (j = 0; j < cptab->ctb_nparts; j++) {
241 rc = snprintf(tmp, len, " %d:%d",
242 j, cptab->ctb_parts[i].cpt_distance[j]);
260 EXPORT_SYMBOL(cfs_cpt_distance_print);
263 cfs_cpt_number(struct cfs_cpt_table *cptab)
265 return cptab->ctb_nparts;
267 EXPORT_SYMBOL(cfs_cpt_number);
270 cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
272 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
274 return cpt == CFS_CPT_ANY ?
275 cpumask_weight(cptab->ctb_cpumask) :
276 cpumask_weight(cptab->ctb_parts[cpt].cpt_cpumask);
278 EXPORT_SYMBOL(cfs_cpt_weight);
281 cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
283 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
285 return cpt == CFS_CPT_ANY ?
286 cpumask_any_and(cptab->ctb_cpumask,
287 cpu_online_mask) < nr_cpu_ids :
288 cpumask_any_and(cptab->ctb_parts[cpt].cpt_cpumask,
289 cpu_online_mask) < nr_cpu_ids;
291 EXPORT_SYMBOL(cfs_cpt_online);
294 cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
296 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
298 return cpt == CFS_CPT_ANY ?
299 cptab->ctb_cpumask : cptab->ctb_parts[cpt].cpt_cpumask;
301 EXPORT_SYMBOL(cfs_cpt_cpumask);
304 cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
306 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
308 return cpt == CFS_CPT_ANY ?
309 cptab->ctb_nodemask : cptab->ctb_parts[cpt].cpt_nodemask;
311 EXPORT_SYMBOL(cfs_cpt_nodemask);
314 cfs_cpt_distance(struct cfs_cpt_table *cptab, int cpt1, int cpt2)
316 LASSERT(cpt1 == CFS_CPT_ANY || (cpt1 >= 0 && cpt1 < cptab->ctb_nparts));
317 LASSERT(cpt2 == CFS_CPT_ANY || (cpt2 >= 0 && cpt2 < cptab->ctb_nparts));
319 if (cpt1 == CFS_CPT_ANY || cpt2 == CFS_CPT_ANY)
320 return cptab->ctb_distance;
322 return cptab->ctb_parts[cpt1].cpt_distance[cpt2];
324 EXPORT_SYMBOL(cfs_cpt_distance);
327 * Calculate the maximum NUMA distance between all nodes in the
328 * from_mask and all nodes in the to_mask.
331 cfs_cpt_distance_calculate(nodemask_t *from_mask, nodemask_t *to_mask)
339 for_each_node_mask(from, *from_mask) {
340 for_each_node_mask(to, *to_mask) {
341 distance = node_distance(from, to);
342 if (maximum < distance)
349 static void cfs_cpt_add_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
351 cptab->ctb_cpu2cpt[cpu] = cpt;
353 cpumask_set_cpu(cpu, cptab->ctb_cpumask);
354 cpumask_set_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
357 static void cfs_cpt_del_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
359 cpumask_clear_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
360 cpumask_clear_cpu(cpu, cptab->ctb_cpumask);
362 cptab->ctb_cpu2cpt[cpu] = -1;
365 static void cfs_cpt_add_node(struct cfs_cpt_table *cptab, int cpt, int node)
368 struct cfs_cpu_partition *part;
369 struct cfs_cpu_partition *part2;
371 if (!node_isset(node, *cptab->ctb_nodemask)) {
372 /* first time node is added to the CPT table */
373 node_set(node, *cptab->ctb_nodemask);
374 cptab->ctb_node2cpt[node] = cpt;
375 cptab->ctb_distance = cfs_cpt_distance_calculate(
377 cptab->ctb_nodemask);
380 part = &cptab->ctb_parts[cpt];
381 if (!node_isset(node, *part->cpt_nodemask)) {
382 /* first time node is added to this CPT */
383 node_set(node, *part->cpt_nodemask);
384 for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
385 part2 = &cptab->ctb_parts[cpt2];
386 part->cpt_distance[cpt2] = cfs_cpt_distance_calculate(
388 part2->cpt_nodemask);
389 part2->cpt_distance[cpt] = cfs_cpt_distance_calculate(
396 static void cfs_cpt_del_node(struct cfs_cpt_table *cptab, int cpt, int node)
400 struct cfs_cpu_partition *part;
401 struct cfs_cpu_partition *part2;
403 part = &cptab->ctb_parts[cpt];
405 for_each_cpu(cpu, part->cpt_cpumask) {
406 /* this CPT has other CPU belonging to this node? */
407 if (cpu_to_node(cpu) == node)
411 if (cpu >= nr_cpu_ids && node_isset(node, *part->cpt_nodemask)) {
412 /* No more CPUs in the node for this CPT. */
413 node_clear(node, *part->cpt_nodemask);
414 for (cpt2 = 0; cpt2 < cptab->ctb_nparts; cpt2++) {
415 part2 = &cptab->ctb_parts[cpt2];
416 if (node_isset(node, *part2->cpt_nodemask))
417 cptab->ctb_node2cpt[node] = cpt2;
418 part->cpt_distance[cpt2] = cfs_cpt_distance_calculate(
420 part2->cpt_nodemask);
421 part2->cpt_distance[cpt] = cfs_cpt_distance_calculate(
427 for_each_cpu(cpu, cptab->ctb_cpumask) {
428 /* this CPT-table has other CPUs belonging to this node? */
429 if (cpu_to_node(cpu) == node)
433 if (cpu >= nr_cpu_ids && node_isset(node, *cptab->ctb_nodemask)) {
434 /* No more CPUs in the table for this node. */
435 node_clear(node, *cptab->ctb_nodemask);
436 cptab->ctb_node2cpt[node] = -1;
437 cptab->ctb_distance =
438 cfs_cpt_distance_calculate(cptab->ctb_nodemask,
439 cptab->ctb_nodemask);
444 cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
446 LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
448 if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu)) {
449 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
453 if (cptab->ctb_cpu2cpt[cpu] != -1) {
454 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
455 cpu, cptab->ctb_cpu2cpt[cpu]);
459 LASSERT(!cpumask_test_cpu(cpu, cptab->ctb_cpumask));
460 LASSERT(!cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
462 cfs_cpt_add_cpu(cptab, cpt, cpu);
463 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
467 EXPORT_SYMBOL(cfs_cpt_set_cpu);
470 cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
472 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
474 if (cpu < 0 || cpu >= nr_cpu_ids) {
475 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
479 if (cpt == CFS_CPT_ANY) {
480 /* caller doesn't know the partition ID */
481 cpt = cptab->ctb_cpu2cpt[cpu];
482 if (cpt < 0) { /* not set in this CPT-table */
483 CDEBUG(D_INFO, "Try to unset cpu %d which is "
484 "not in CPT-table %p\n", cpt, cptab);
488 } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
490 "CPU %d is not in cpu-partition %d\n", cpu, cpt);
494 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
495 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_cpumask));
497 cfs_cpt_del_cpu(cptab, cpt, cpu);
498 cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
500 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
503 cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt, const cpumask_t *mask)
507 if (cpumask_weight(mask) == 0 ||
508 cpumask_any_and(mask, cpu_online_mask) >= nr_cpu_ids) {
509 CDEBUG(D_INFO, "No online CPU is found in the CPU mask "
510 "for CPU partition %d\n", cpt);
514 for_each_cpu(cpu, mask) {
515 cfs_cpt_add_cpu(cptab, cpt, cpu);
516 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
521 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
524 cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt,
525 const cpumask_t *mask)
529 for_each_cpu(cpu, mask)
530 cfs_cpt_unset_cpu(cptab, cpt, cpu);
532 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
535 cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
537 const cpumask_t *mask;
540 if (node < 0 || node >= nr_node_ids) {
542 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
546 mask = cpumask_of_node(node);
548 for_each_cpu(cpu, mask)
549 cfs_cpt_add_cpu(cptab, cpt, cpu);
551 cfs_cpt_add_node(cptab, cpt, node);
555 EXPORT_SYMBOL(cfs_cpt_set_node);
558 cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
560 const cpumask_t *mask;
563 if (node < 0 || node >= nr_node_ids) {
565 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
569 mask = cpumask_of_node(node);
571 for_each_cpu(cpu, mask)
572 cfs_cpt_del_cpu(cptab, cpt, cpu);
574 cfs_cpt_del_node(cptab, cpt, node);
576 EXPORT_SYMBOL(cfs_cpt_unset_node);
579 cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
583 for_each_node_mask(i, *mask) {
584 if (!cfs_cpt_set_node(cptab, cpt, i))
590 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
593 cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
597 for_each_node_mask(i, *mask)
598 cfs_cpt_unset_node(cptab, cpt, i);
600 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
602 int cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
609 /* convert CPU partition ID to HW node id */
611 if (cpt < 0 || cpt >= cptab->ctb_nparts) {
612 mask = cptab->ctb_nodemask;
613 rotor = cptab->ctb_spread_rotor++;
615 mask = cptab->ctb_parts[cpt].cpt_nodemask;
616 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
619 weight = nodes_weight(*mask);
624 for_each_node_mask(node, *mask) {
632 EXPORT_SYMBOL(cfs_cpt_spread_node);
635 cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
637 int cpu = smp_processor_id();
638 int cpt = cptab->ctb_cpu2cpt[cpu];
644 /* don't return negative value for safety of upper layer,
645 * instead we shadow the unknown cpu to a valid partition ID */
646 cpt = cpu % cptab->ctb_nparts;
651 EXPORT_SYMBOL(cfs_cpt_current);
654 cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
656 LASSERT(cpu >= 0 && cpu < nr_cpu_ids);
658 return cptab->ctb_cpu2cpt[cpu];
660 EXPORT_SYMBOL(cfs_cpt_of_cpu);
663 cfs_cpt_of_node(struct cfs_cpt_table *cptab, int node)
665 if (node < 0 || node > nr_node_ids)
668 return cptab->ctb_node2cpt[node];
670 EXPORT_SYMBOL(cfs_cpt_of_node);
673 cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
676 nodemask_t *nodemask;
680 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
682 if (cpt == CFS_CPT_ANY) {
683 cpumask = cptab->ctb_cpumask;
684 nodemask = cptab->ctb_nodemask;
686 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
687 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
690 if (cpumask_any_and(cpumask, cpu_online_mask) >= nr_cpu_ids) {
691 CERROR("No online CPU found in CPU partition %d, did someone "
692 "do CPU hotplug on system? You might need to reload "
693 "Lustre modules to keep system working well.\n", cpt);
697 for_each_online_cpu(i) {
698 if (cpumask_test_cpu(i, cpumask))
701 rc = set_cpus_allowed_ptr(current, cpumask);
702 set_mems_allowed(*nodemask);
704 schedule(); /* switch to allowed CPU */
709 /* don't need to set affinity because all online CPUs are covered */
712 EXPORT_SYMBOL(cfs_cpt_bind);
715 * Choose max to \a number CPUs from \a node and set them in \a cpt.
716 * We always prefer to choose CPU in the same core/socket.
719 cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
720 cpumask_t *node, int number)
722 cpumask_t *socket = NULL;
723 cpumask_t *core = NULL;
729 if (number >= cpumask_weight(node)) {
730 while (!cpumask_empty(node)) {
731 cpu = cpumask_first(node);
733 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
736 cpumask_clear_cpu(cpu, node);
741 /* allocate scratch buffer */
742 LIBCFS_ALLOC(socket, cpumask_size());
743 LIBCFS_ALLOC(core, cpumask_size());
744 if (socket == NULL || core == NULL) {
749 while (!cpumask_empty(node)) {
750 cpu = cpumask_first(node);
752 /* get cpumask for cores in the same socket */
753 cpumask_copy(socket, topology_core_cpumask(cpu));
754 cpumask_and(socket, socket, node);
756 LASSERT(!cpumask_empty(socket));
758 while (!cpumask_empty(socket)) {
761 /* get cpumask for hts in the same core */
762 cpumask_copy(core, topology_sibling_cpumask(cpu));
763 cpumask_and(core, core, node);
765 LASSERT(!cpumask_empty(core));
767 for_each_cpu(i, core) {
768 cpumask_clear_cpu(i, socket);
769 cpumask_clear_cpu(i, node);
771 rc = cfs_cpt_set_cpu(cptab, cpt, i);
780 cpu = cpumask_first(socket);
786 LIBCFS_FREE(socket, cpumask_size());
788 LIBCFS_FREE(core, cpumask_size());
792 #define CPT_WEIGHT_MIN 4u
795 cfs_cpt_num_estimate(void)
797 unsigned nnode = num_online_nodes();
798 unsigned ncpu = num_online_cpus();
801 if (ncpu <= CPT_WEIGHT_MIN) {
806 /* generate reasonable number of CPU partitions based on total number
807 * of CPUs, Preferred N should be power2 and match this condition:
808 * 2 * (N - 1)^2 < NCPUS <= 2 * N^2 */
809 for (ncpt = 2; ncpu > 2 * ncpt * ncpt; ncpt <<= 1) {}
811 if (ncpt <= nnode) { /* fat numa system */
815 } else { /* ncpt > nnode */
816 while ((nnode << 1) <= ncpt)
823 #if (BITS_PER_LONG == 32)
824 /* config many CPU partitions on 32-bit system could consume
826 ncpt = min(2U, ncpt);
828 while (ncpu % ncpt != 0)
829 ncpt--; /* worst case is 1 */
834 static struct cfs_cpt_table *
835 cfs_cpt_table_create(int ncpt)
837 struct cfs_cpt_table *cptab = NULL;
838 cpumask_t *mask = NULL;
844 rc = cfs_cpt_num_estimate();
848 if (ncpt > num_online_cpus() || ncpt > 4 * rc) {
849 CWARN("CPU partition number %d is larger than suggested "
850 "value (%d), your system may have performance"
851 "issue or run out of memory while under pressure\n",
855 if (num_online_cpus() % ncpt != 0) {
856 CERROR("CPU number %d is not multiple of cpu_npartition %d, "
857 "please try different cpu_npartitions value or"
858 "set pattern string by cpu_pattern=STRING\n",
859 (int)num_online_cpus(), ncpt);
863 cptab = cfs_cpt_table_alloc(ncpt);
865 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
869 num = num_online_cpus() / ncpt;
871 CERROR("CPU changed while setting CPU partition\n");
875 LIBCFS_ALLOC(mask, cpumask_size());
877 CERROR("Failed to allocate scratch cpumask\n");
881 for_each_online_node(i) {
882 cpumask_copy(mask, cpumask_of_node(i));
884 while (!cpumask_empty(mask)) {
885 struct cfs_cpu_partition *part;
888 /* Each emulated NUMA node has all allowed CPUs in
890 * End loop when all partitions have assigned CPUs.
895 part = &cptab->ctb_parts[cpt];
897 n = num - cpumask_weight(part->cpt_cpumask);
900 rc = cfs_cpt_choose_ncpus(cptab, cpt, mask, n);
904 LASSERT(num >= cpumask_weight(part->cpt_cpumask));
905 if (num == cpumask_weight(part->cpt_cpumask))
911 num != cpumask_weight(cptab->ctb_parts[ncpt - 1].cpt_cpumask)) {
912 CERROR("Expect %d(%d) CPU partitions but got %d(%d), "
913 "CPU hotplug/unplug while setting?\n",
914 cptab->ctb_nparts, num, cpt,
915 cpumask_weight(cptab->ctb_parts[ncpt - 1].cpt_cpumask));
919 LIBCFS_FREE(mask, cpumask_size());
924 CERROR("Failed to setup CPU-partition-table with %d "
925 "CPU-partitions, online HW nodes: %d, HW cpus: %d.\n",
926 ncpt, num_online_nodes(), num_online_cpus());
929 LIBCFS_FREE(mask, cpumask_size());
932 cfs_cpt_table_free(cptab);
937 static struct cfs_cpt_table *
938 cfs_cpt_table_create_pattern(char *pattern)
940 struct cfs_cpt_table *cptab;
950 str = cfs_trimwhite(pattern);
951 if (*str == 'n' || *str == 'N') {
953 if (*pattern != '\0') {
954 node = 1; /* numa pattern */
956 } else { /* shortcut to create CPT from NUMA & CPU topology */
958 ncpt = num_online_nodes();
962 if (ncpt == 0) { /* scanning bracket which is mark of partition */
963 for (str = pattern;; str++, ncpt++) {
964 str = strchr(str, '[');
971 (node && ncpt > num_online_nodes()) ||
972 (!node && ncpt > num_online_cpus())) {
973 CERROR("Invalid pattern %s, or too many partitions %d\n",
978 cptab = cfs_cpt_table_alloc(ncpt);
980 CERROR("Failed to allocate cpu partition table\n");
984 if (node < 0) { /* shortcut to create CPT from NUMA & CPU topology */
986 for_each_online_node(i) {
988 CERROR("CPU changed while setting CPU "
989 "partition table, %d/%d\n", cpt, ncpt);
993 rc = cfs_cpt_set_node(cptab, cpt++, i);
1000 high = node ? nr_node_ids - 1 : nr_cpu_ids - 1;
1002 for (str = cfs_trimwhite(pattern), c = 0;; c++) {
1003 struct cfs_range_expr *range;
1004 struct cfs_expr_list *el;
1005 char *bracket = strchr(str, '[');
1008 if (bracket == NULL) {
1010 CERROR("Invalid pattern %s\n", str);
1012 } else if (c != ncpt) {
1013 CERROR("expect %d partitions but found %d\n",
1020 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
1021 CERROR("Invalid cpu pattern %s\n", str);
1025 if (cpt < 0 || cpt >= ncpt) {
1026 CERROR("Invalid partition id %d, total partitions %d\n",
1031 if (cfs_cpt_weight(cptab, cpt) != 0) {
1032 CERROR("Partition %d has already been set.\n", cpt);
1036 str = cfs_trimwhite(str + n);
1037 if (str != bracket) {
1038 CERROR("Invalid pattern %s\n", str);
1042 bracket = strchr(str, ']');
1043 if (bracket == NULL) {
1044 CERROR("missing right bracket for cpt %d, %s\n",
1049 if (cfs_expr_list_parse(str, (bracket - str) + 1,
1050 0, high, &el) != 0) {
1051 CERROR("Can't parse number range: %s\n", str);
1055 list_for_each_entry(range, &el->el_exprs, re_link) {
1056 for (i = range->re_lo; i <= range->re_hi; i++) {
1057 if ((i - range->re_lo) % range->re_stride != 0)
1060 rc = node ? cfs_cpt_set_node(cptab, cpt, i) :
1061 cfs_cpt_set_cpu(cptab, cpt, i);
1063 cfs_expr_list_free(el);
1069 cfs_expr_list_free(el);
1071 if (!cfs_cpt_online(cptab, cpt)) {
1072 CERROR("No online CPU is found on partition %d\n", cpt);
1076 str = cfs_trimwhite(bracket + 1);
1082 cfs_cpt_table_free(cptab);
1086 #ifdef CONFIG_HOTPLUG_CPU
1088 cfs_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1090 unsigned int cpu = (unsigned long)hcpu;
1095 case CPU_DEAD_FROZEN:
1097 case CPU_ONLINE_FROZEN:
1099 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
1100 CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
1105 /* if all HTs in a core are offline, it may break affinity */
1106 warn = cpumask_any_and(topology_sibling_cpumask(cpu),
1107 cpu_online_mask) >= nr_cpu_ids;
1108 CDEBUG(warn ? D_WARNING : D_INFO,
1109 "Lustre: can't support CPU plug-out well now, "
1110 "performance and stability could be impacted"
1111 "[CPU %u action: %lx]\n", cpu, action);
1117 static struct notifier_block cfs_cpu_notifier = {
1118 .notifier_call = cfs_cpu_notify,
1127 if (cfs_cpt_table != NULL)
1128 cfs_cpt_table_free(cfs_cpt_table);
1130 #ifdef CONFIG_HOTPLUG_CPU
1131 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1138 LASSERT(cfs_cpt_table == NULL);
1140 #ifdef CONFIG_HOTPLUG_CPU
1141 register_hotcpu_notifier(&cfs_cpu_notifier);
1144 if (*cpu_pattern != 0) {
1145 char *cpu_pattern_dup = kstrdup(cpu_pattern, GFP_KERNEL);
1147 if (cpu_pattern_dup == NULL) {
1148 CERROR("Failed to duplicate cpu_pattern\n");
1152 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern_dup);
1153 kfree(cpu_pattern_dup);
1154 if (cfs_cpt_table == NULL) {
1155 CERROR("Failed to create cptab from pattern %s\n",
1161 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1162 if (cfs_cpt_table == NULL) {
1163 CERROR("Failed to create ptable with npartitions %d\n",
1170 LCONSOLE(0, "HW nodes: %d, HW CPU cores: %d, npartitions: %d\n",
1171 num_online_nodes(), num_online_cpus(),
1172 cfs_cpt_number(cfs_cpt_table));