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.
25 * Copyright (c) 2012, Intel Corporation.
28 * This file is part of Lustre, http://www.lustre.org/
29 * Lustre is a trademark of Sun Microsystems, Inc.
31 * Author: liang@whamcloud.com
34 #define DEBUG_SUBSYSTEM S_LNET
36 #include <linux/cpu.h>
37 #include <linux/sched.h>
38 #include "../../../include/linux/libcfs/libcfs.h"
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 * NB: If user specified cpu_pattern, cpu_npartitions will be ignored
64 static char *cpu_pattern = "";
65 module_param(cpu_pattern, charp, 0444);
66 MODULE_PARM_DESC(cpu_pattern, "CPU partitions pattern");
69 /* serialize hotplug etc */
71 /* reserved for hotplug */
72 unsigned long cpt_version;
73 /* mutex to protect cpt_cpumask */
74 struct mutex cpt_mutex;
75 /* scratch buffer for set/unset_node */
76 cpumask_t *cpt_cpumask;
79 static struct cfs_cpt_data cpt_data;
81 static void cfs_cpu_core_siblings(int cpu, cpumask_t *mask)
83 /* return cpumask of cores in the same socket */
84 cpumask_copy(mask, topology_core_cpumask(cpu));
87 /* return cpumask of HTs in the same core */
88 static void cfs_cpu_ht_siblings(int cpu, cpumask_t *mask)
90 cpumask_copy(mask, topology_thread_cpumask(cpu));
93 static void cfs_node_to_cpumask(int node, cpumask_t *mask)
95 cpumask_copy(mask, cpumask_of_node(node));
99 cfs_cpt_table_free(struct cfs_cpt_table *cptab)
103 if (cptab->ctb_cpu2cpt != NULL) {
104 LIBCFS_FREE(cptab->ctb_cpu2cpt,
105 num_possible_cpus() *
106 sizeof(cptab->ctb_cpu2cpt[0]));
109 for (i = 0; cptab->ctb_parts != NULL && i < cptab->ctb_nparts; i++) {
110 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
112 if (part->cpt_nodemask != NULL) {
113 LIBCFS_FREE(part->cpt_nodemask,
114 sizeof(*part->cpt_nodemask));
117 if (part->cpt_cpumask != NULL)
118 LIBCFS_FREE(part->cpt_cpumask, cpumask_size());
121 if (cptab->ctb_parts != NULL) {
122 LIBCFS_FREE(cptab->ctb_parts,
123 cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
126 if (cptab->ctb_nodemask != NULL)
127 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
128 if (cptab->ctb_cpumask != NULL)
129 LIBCFS_FREE(cptab->ctb_cpumask, cpumask_size());
131 LIBCFS_FREE(cptab, sizeof(*cptab));
133 EXPORT_SYMBOL(cfs_cpt_table_free);
135 struct cfs_cpt_table *
136 cfs_cpt_table_alloc(unsigned int ncpt)
138 struct cfs_cpt_table *cptab;
141 LIBCFS_ALLOC(cptab, sizeof(*cptab));
145 cptab->ctb_nparts = ncpt;
147 LIBCFS_ALLOC(cptab->ctb_cpumask, cpumask_size());
148 LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
150 if (cptab->ctb_cpumask == NULL || cptab->ctb_nodemask == NULL)
153 LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
154 num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
155 if (cptab->ctb_cpu2cpt == NULL)
158 memset(cptab->ctb_cpu2cpt, -1,
159 num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
161 LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
162 if (cptab->ctb_parts == NULL)
165 for (i = 0; i < ncpt; i++) {
166 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
168 LIBCFS_ALLOC(part->cpt_cpumask, cpumask_size());
169 LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
170 if (part->cpt_cpumask == NULL || part->cpt_nodemask == NULL)
174 spin_lock(&cpt_data.cpt_lock);
175 /* Reserved for hotplug */
176 cptab->ctb_version = cpt_data.cpt_version;
177 spin_unlock(&cpt_data.cpt_lock);
182 cfs_cpt_table_free(cptab);
185 EXPORT_SYMBOL(cfs_cpt_table_alloc);
188 cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
195 for (i = 0; i < cptab->ctb_nparts; i++) {
197 rc = snprintf(tmp, len, "%d\t: ", i);
207 for_each_cpu_mask(j, *cptab->ctb_parts[i].cpt_cpumask) {
208 rc = snprintf(tmp, len, "%d ", j);
228 EXPORT_SYMBOL(cfs_cpt_table_print);
231 cfs_cpt_number(struct cfs_cpt_table *cptab)
233 return cptab->ctb_nparts;
235 EXPORT_SYMBOL(cfs_cpt_number);
238 cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
240 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
242 return cpt == CFS_CPT_ANY ?
243 cpus_weight(*cptab->ctb_cpumask) :
244 cpus_weight(*cptab->ctb_parts[cpt].cpt_cpumask);
246 EXPORT_SYMBOL(cfs_cpt_weight);
249 cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
251 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
253 return cpt == CFS_CPT_ANY ?
254 any_online_cpu(*cptab->ctb_cpumask) != NR_CPUS :
255 any_online_cpu(*cptab->ctb_parts[cpt].cpt_cpumask) != NR_CPUS;
257 EXPORT_SYMBOL(cfs_cpt_online);
260 cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
262 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
264 return cpt == CFS_CPT_ANY ?
265 cptab->ctb_cpumask : cptab->ctb_parts[cpt].cpt_cpumask;
267 EXPORT_SYMBOL(cfs_cpt_cpumask);
270 cfs_cpt_nodemask(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 cptab->ctb_nodemask : cptab->ctb_parts[cpt].cpt_nodemask;
277 EXPORT_SYMBOL(cfs_cpt_nodemask);
280 cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
284 LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
286 if (cpu < 0 || cpu >= NR_CPUS || !cpu_online(cpu)) {
287 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
291 if (cptab->ctb_cpu2cpt[cpu] != -1) {
292 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
293 cpu, cptab->ctb_cpu2cpt[cpu]);
297 cptab->ctb_cpu2cpt[cpu] = cpt;
299 LASSERT(!cpu_isset(cpu, *cptab->ctb_cpumask));
300 LASSERT(!cpu_isset(cpu, *cptab->ctb_parts[cpt].cpt_cpumask));
302 cpu_set(cpu, *cptab->ctb_cpumask);
303 cpu_set(cpu, *cptab->ctb_parts[cpt].cpt_cpumask);
305 node = cpu_to_node(cpu);
307 /* first CPU of @node in this CPT table */
308 if (!node_isset(node, *cptab->ctb_nodemask))
309 node_set(node, *cptab->ctb_nodemask);
311 /* first CPU of @node in this partition */
312 if (!node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask))
313 node_set(node, *cptab->ctb_parts[cpt].cpt_nodemask);
317 EXPORT_SYMBOL(cfs_cpt_set_cpu);
320 cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
325 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
327 if (cpu < 0 || cpu >= NR_CPUS) {
328 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
332 if (cpt == CFS_CPT_ANY) {
333 /* caller doesn't know the partition ID */
334 cpt = cptab->ctb_cpu2cpt[cpu];
335 if (cpt < 0) { /* not set in this CPT-table */
336 CDEBUG(D_INFO, "Try to unset cpu %d which is "
337 "not in CPT-table %p\n", cpt, cptab);
341 } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
343 "CPU %d is not in cpu-partition %d\n", cpu, cpt);
347 LASSERT(cpu_isset(cpu, *cptab->ctb_parts[cpt].cpt_cpumask));
348 LASSERT(cpu_isset(cpu, *cptab->ctb_cpumask));
350 cpu_clear(cpu, *cptab->ctb_parts[cpt].cpt_cpumask);
351 cpu_clear(cpu, *cptab->ctb_cpumask);
352 cptab->ctb_cpu2cpt[cpu] = -1;
354 node = cpu_to_node(cpu);
356 LASSERT(node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask));
357 LASSERT(node_isset(node, *cptab->ctb_nodemask));
359 for_each_cpu_mask(i, *cptab->ctb_parts[cpt].cpt_cpumask) {
360 /* this CPT has other CPU belonging to this node? */
361 if (cpu_to_node(i) == node)
366 node_clear(node, *cptab->ctb_parts[cpt].cpt_nodemask);
368 for_each_cpu_mask(i, *cptab->ctb_cpumask) {
369 /* this CPT-table has other CPU belonging to this node? */
370 if (cpu_to_node(i) == node)
375 node_clear(node, *cptab->ctb_nodemask);
379 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
382 cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
386 if (cpus_weight(*mask) == 0 || any_online_cpu(*mask) == NR_CPUS) {
387 CDEBUG(D_INFO, "No online CPU is found in the CPU mask "
388 "for CPU partition %d\n", cpt);
392 for_each_cpu_mask(i, *mask) {
393 if (!cfs_cpt_set_cpu(cptab, cpt, i))
399 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
402 cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
406 for_each_cpu_mask(i, *mask)
407 cfs_cpt_unset_cpu(cptab, cpt, i);
409 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
412 cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
417 if (node < 0 || node >= MAX_NUMNODES) {
419 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
423 mutex_lock(&cpt_data.cpt_mutex);
425 mask = cpt_data.cpt_cpumask;
426 cfs_node_to_cpumask(node, mask);
428 rc = cfs_cpt_set_cpumask(cptab, cpt, mask);
430 mutex_unlock(&cpt_data.cpt_mutex);
434 EXPORT_SYMBOL(cfs_cpt_set_node);
437 cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
441 if (node < 0 || node >= MAX_NUMNODES) {
443 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
447 mutex_lock(&cpt_data.cpt_mutex);
449 mask = cpt_data.cpt_cpumask;
450 cfs_node_to_cpumask(node, mask);
452 cfs_cpt_unset_cpumask(cptab, cpt, mask);
454 mutex_unlock(&cpt_data.cpt_mutex);
456 EXPORT_SYMBOL(cfs_cpt_unset_node);
459 cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
463 for_each_node_mask(i, *mask) {
464 if (!cfs_cpt_set_node(cptab, cpt, i))
470 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
473 cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
477 for_each_node_mask(i, *mask)
478 cfs_cpt_unset_node(cptab, cpt, i);
480 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
483 cfs_cpt_clear(struct cfs_cpt_table *cptab, int cpt)
488 if (cpt == CFS_CPT_ANY) {
489 last = cptab->ctb_nparts - 1;
495 for (; cpt <= last; cpt++) {
496 for_each_cpu_mask(i, *cptab->ctb_parts[cpt].cpt_cpumask)
497 cfs_cpt_unset_cpu(cptab, cpt, i);
500 EXPORT_SYMBOL(cfs_cpt_clear);
503 cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
510 /* convert CPU partition ID to HW node id */
512 if (cpt < 0 || cpt >= cptab->ctb_nparts) {
513 mask = cptab->ctb_nodemask;
514 rotor = cptab->ctb_spread_rotor++;
516 mask = cptab->ctb_parts[cpt].cpt_nodemask;
517 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
520 weight = nodes_weight(*mask);
525 for_each_node_mask(node, *mask) {
533 EXPORT_SYMBOL(cfs_cpt_spread_node);
536 cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
538 int cpu = smp_processor_id();
539 int cpt = cptab->ctb_cpu2cpt[cpu];
545 /* don't return negative value for safety of upper layer,
546 * instead we shadow the unknown cpu to a valid partition ID */
547 cpt = cpu % cptab->ctb_nparts;
552 EXPORT_SYMBOL(cfs_cpt_current);
555 cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
557 LASSERT(cpu >= 0 && cpu < NR_CPUS);
559 return cptab->ctb_cpu2cpt[cpu];
561 EXPORT_SYMBOL(cfs_cpt_of_cpu);
564 cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
567 nodemask_t *nodemask;
571 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
573 if (cpt == CFS_CPT_ANY) {
574 cpumask = cptab->ctb_cpumask;
575 nodemask = cptab->ctb_nodemask;
577 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
578 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
581 if (any_online_cpu(*cpumask) == NR_CPUS) {
582 CERROR("No online CPU found in CPU partition %d, did someone "
583 "do CPU hotplug on system? You might need to reload "
584 "Lustre modules to keep system working well.\n", cpt);
588 for_each_online_cpu(i) {
589 if (cpu_isset(i, *cpumask))
592 rc = set_cpus_allowed_ptr(current, cpumask);
593 set_mems_allowed(*nodemask);
595 schedule(); /* switch to allowed CPU */
600 /* don't need to set affinity because all online CPUs are covered */
603 EXPORT_SYMBOL(cfs_cpt_bind);
606 * Choose max to \a number CPUs from \a node and set them in \a cpt.
607 * We always prefer to choose CPU in the same core/socket.
610 cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
611 cpumask_t *node, int number)
613 cpumask_t *socket = NULL;
614 cpumask_t *core = NULL;
620 if (number >= cpus_weight(*node)) {
621 while (!cpus_empty(*node)) {
622 cpu = first_cpu(*node);
624 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
627 cpu_clear(cpu, *node);
632 /* allocate scratch buffer */
633 LIBCFS_ALLOC(socket, cpumask_size());
634 LIBCFS_ALLOC(core, cpumask_size());
635 if (socket == NULL || core == NULL) {
640 while (!cpus_empty(*node)) {
641 cpu = first_cpu(*node);
643 /* get cpumask for cores in the same socket */
644 cfs_cpu_core_siblings(cpu, socket);
645 cpus_and(*socket, *socket, *node);
647 LASSERT(!cpus_empty(*socket));
649 while (!cpus_empty(*socket)) {
652 /* get cpumask for hts in the same core */
653 cfs_cpu_ht_siblings(cpu, core);
654 cpus_and(*core, *core, *node);
656 LASSERT(!cpus_empty(*core));
658 for_each_cpu_mask(i, *core) {
659 cpu_clear(i, *socket);
662 rc = cfs_cpt_set_cpu(cptab, cpt, i);
671 cpu = first_cpu(*socket);
677 LIBCFS_FREE(socket, cpumask_size());
679 LIBCFS_FREE(core, cpumask_size());
683 #define CPT_WEIGHT_MIN 4u
686 cfs_cpt_num_estimate(void)
688 unsigned nnode = num_online_nodes();
689 unsigned ncpu = num_online_cpus();
692 if (ncpu <= CPT_WEIGHT_MIN) {
697 /* generate reasonable number of CPU partitions based on total number
698 * of CPUs, Preferred N should be power2 and match this condition:
699 * 2 * (N - 1)^2 < NCPUS <= 2 * N^2 */
700 for (ncpt = 2; ncpu > 2 * ncpt * ncpt; ncpt <<= 1) {}
702 if (ncpt <= nnode) { /* fat numa system */
706 } else { /* ncpt > nnode */
707 while ((nnode << 1) <= ncpt)
714 #if (BITS_PER_LONG == 32)
715 /* config many CPU partitions on 32-bit system could consume
717 ncpt = min(2U, ncpt);
719 while (ncpu % ncpt != 0)
720 ncpt--; /* worst case is 1 */
725 static struct cfs_cpt_table *
726 cfs_cpt_table_create(int ncpt)
728 struct cfs_cpt_table *cptab = NULL;
729 cpumask_t *mask = NULL;
735 rc = cfs_cpt_num_estimate();
739 if (ncpt > num_online_cpus() || ncpt > 4 * rc) {
740 CWARN("CPU partition number %d is larger than suggested "
741 "value (%d), your system may have performance"
742 "issue or run out of memory while under pressure\n",
746 if (num_online_cpus() % ncpt != 0) {
747 CERROR("CPU number %d is not multiple of cpu_npartition %d, "
748 "please try different cpu_npartitions value or"
749 "set pattern string by cpu_pattern=STRING\n",
750 (int)num_online_cpus(), ncpt);
754 cptab = cfs_cpt_table_alloc(ncpt);
756 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
760 num = num_online_cpus() / ncpt;
762 CERROR("CPU changed while setting CPU partition\n");
766 LIBCFS_ALLOC(mask, cpumask_size());
768 CERROR("Failed to allocate scratch cpumask\n");
772 for_each_online_node(i) {
773 cfs_node_to_cpumask(i, mask);
775 while (!cpus_empty(*mask)) {
776 struct cfs_cpu_partition *part;
782 part = &cptab->ctb_parts[cpt];
784 n = num - cpus_weight(*part->cpt_cpumask);
787 rc = cfs_cpt_choose_ncpus(cptab, cpt, mask, n);
791 LASSERT(num >= cpus_weight(*part->cpt_cpumask));
792 if (num == cpus_weight(*part->cpt_cpumask))
798 num != cpus_weight(*cptab->ctb_parts[ncpt - 1].cpt_cpumask)) {
799 CERROR("Expect %d(%d) CPU partitions but got %d(%d), "
800 "CPU hotplug/unplug while setting?\n",
801 cptab->ctb_nparts, num, cpt,
802 cpus_weight(*cptab->ctb_parts[ncpt - 1].cpt_cpumask));
806 LIBCFS_FREE(mask, cpumask_size());
811 CERROR("Failed to setup CPU-partition-table with %d "
812 "CPU-partitions, online HW nodes: %d, HW cpus: %d.\n",
813 ncpt, num_online_nodes(), num_online_cpus());
816 LIBCFS_FREE(mask, cpumask_size());
819 cfs_cpt_table_free(cptab);
824 static struct cfs_cpt_table *
825 cfs_cpt_table_create_pattern(char *pattern)
827 struct cfs_cpt_table *cptab;
834 for (ncpt = 0;; ncpt++) { /* quick scan bracket */
835 str = strchr(str, '[');
841 str = cfs_trimwhite(pattern);
842 if (*str == 'n' || *str == 'N') {
848 (node && ncpt > num_online_nodes()) ||
849 (!node && ncpt > num_online_cpus())) {
850 CERROR("Invalid pattern %s, or too many partitions %d\n",
855 high = node ? MAX_NUMNODES - 1 : NR_CPUS - 1;
857 cptab = cfs_cpt_table_alloc(ncpt);
859 CERROR("Failed to allocate cpu partition table\n");
863 for (str = cfs_trimwhite(pattern), c = 0;; c++) {
864 struct cfs_range_expr *range;
865 struct cfs_expr_list *el;
866 char *bracket = strchr(str, '[');
872 if (bracket == NULL) {
874 CERROR("Invalid pattern %s\n", str);
876 } else if (c != ncpt) {
877 CERROR("expect %d partitions but found %d\n",
884 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
885 CERROR("Invalid cpu pattern %s\n", str);
889 if (cpt < 0 || cpt >= ncpt) {
890 CERROR("Invalid partition id %d, total partitions %d\n",
895 if (cfs_cpt_weight(cptab, cpt) != 0) {
896 CERROR("Partition %d has already been set.\n", cpt);
900 str = cfs_trimwhite(str + n);
901 if (str != bracket) {
902 CERROR("Invalid pattern %s\n", str);
906 bracket = strchr(str, ']');
907 if (bracket == NULL) {
908 CERROR("missing right bracket for cpt %d, %s\n",
913 if (cfs_expr_list_parse(str, (bracket - str) + 1,
914 0, high, &el) != 0) {
915 CERROR("Can't parse number range: %s\n", str);
919 list_for_each_entry(range, &el->el_exprs, re_link) {
920 for (i = range->re_lo; i <= range->re_hi; i++) {
921 if ((i - range->re_lo) % range->re_stride != 0)
924 rc = node ? cfs_cpt_set_node(cptab, cpt, i) :
925 cfs_cpt_set_cpu(cptab, cpt, i);
927 cfs_expr_list_free(el);
933 cfs_expr_list_free(el);
935 if (!cfs_cpt_online(cptab, cpt)) {
936 CERROR("No online CPU is found on partition %d\n", cpt);
940 str = cfs_trimwhite(bracket + 1);
946 cfs_cpt_table_free(cptab);
950 #ifdef CONFIG_HOTPLUG_CPU
952 cfs_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
954 unsigned int cpu = (unsigned long)hcpu;
959 case CPU_DEAD_FROZEN:
961 case CPU_ONLINE_FROZEN:
962 spin_lock(&cpt_data.cpt_lock);
963 cpt_data.cpt_version++;
964 spin_unlock(&cpt_data.cpt_lock);
966 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
967 CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
972 mutex_lock(&cpt_data.cpt_mutex);
973 /* if all HTs in a core are offline, it may break affinity */
974 cfs_cpu_ht_siblings(cpu, cpt_data.cpt_cpumask);
975 warn = any_online_cpu(*cpt_data.cpt_cpumask) >= nr_cpu_ids;
976 mutex_unlock(&cpt_data.cpt_mutex);
977 CDEBUG(warn ? D_WARNING : D_INFO,
978 "Lustre: can't support CPU plug-out well now, "
979 "performance and stability could be impacted "
980 "[CPU %u action: %lx]\n", cpu, action);
986 static struct notifier_block cfs_cpu_notifier = {
987 .notifier_call = cfs_cpu_notify,
996 if (cfs_cpt_table != NULL)
997 cfs_cpt_table_free(cfs_cpt_table);
999 #ifdef CONFIG_HOTPLUG_CPU
1000 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1002 if (cpt_data.cpt_cpumask != NULL)
1003 LIBCFS_FREE(cpt_data.cpt_cpumask, cpumask_size());
1009 LASSERT(cfs_cpt_table == NULL);
1011 memset(&cpt_data, 0, sizeof(cpt_data));
1013 LIBCFS_ALLOC(cpt_data.cpt_cpumask, cpumask_size());
1014 if (cpt_data.cpt_cpumask == NULL) {
1015 CERROR("Failed to allocate scratch buffer\n");
1019 spin_lock_init(&cpt_data.cpt_lock);
1020 mutex_init(&cpt_data.cpt_mutex);
1022 #ifdef CONFIG_HOTPLUG_CPU
1023 register_hotcpu_notifier(&cfs_cpu_notifier);
1026 if (*cpu_pattern != 0) {
1027 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern);
1028 if (cfs_cpt_table == NULL) {
1029 CERROR("Failed to create cptab from pattern %s\n",
1035 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1036 if (cfs_cpt_table == NULL) {
1037 CERROR("Failed to create ptable with npartitions %d\n",
1043 spin_lock(&cpt_data.cpt_lock);
1044 if (cfs_cpt_table->ctb_version != cpt_data.cpt_version) {
1045 spin_unlock(&cpt_data.cpt_lock);
1046 CERROR("CPU hotplug/unplug during setup\n");
1049 spin_unlock(&cpt_data.cpt_lock);
1051 LCONSOLE(0, "HW CPU cores: %d, npartitions: %d\n",
1052 num_online_cpus(), cfs_cpt_number(cfs_cpt_table));