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, 2013, 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 <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 CFS_MODULE_PARM(cpu_npartitions, "i", int, 0444, "# of CPU partitions");
53 * modparam for setting CPU partitions patterns:
55 * i.e: "0[0,1,2,3] 1[4,5,6,7]", number before bracket is CPU partition ID,
56 * number in bracket is processor ID (core or HT)
58 * i.e: "N 0[0,1] 1[2,3]" the first character 'N' means numbers in bracket
59 * are NUMA node ID, number before bracket is CPU partition ID.
61 * NB: If user specified cpu_pattern, cpu_npartitions will be ignored
63 static char *cpu_pattern = "";
64 CFS_MODULE_PARM(cpu_pattern, "s", charp, 0444, "CPU partitions pattern");
67 /* serialize hotplug etc */
69 /* reserved for hotplug */
70 unsigned long cpt_version;
71 /* mutex to protect cpt_cpumask */
72 struct semaphore cpt_mutex;
73 /* scratch buffer for set/unset_node */
74 cpumask_t *cpt_cpumask;
77 static struct cfs_cpt_data cpt_data;
80 cfs_cpu_core_siblings(int cpu, cpumask_t *mask)
82 /* return cpumask of cores in the same socket */
83 cpumask_copy(mask, topology_core_cpumask(cpu));
85 EXPORT_SYMBOL(cfs_cpu_core_siblings);
87 /* return number of cores in the same socket of \a cpu */
89 cfs_cpu_core_nsiblings(int cpu)
93 down(&cpt_data.cpt_mutex);
95 cfs_cpu_core_siblings(cpu, cpt_data.cpt_cpumask);
96 num = cpus_weight(*cpt_data.cpt_cpumask);
98 up(&cpt_data.cpt_mutex);
102 EXPORT_SYMBOL(cfs_cpu_core_nsiblings);
104 /* return cpumask of HTs in the same core */
106 cfs_cpu_ht_siblings(int cpu, cpumask_t *mask)
108 cpumask_copy(mask, topology_thread_cpumask(cpu));
110 EXPORT_SYMBOL(cfs_cpu_ht_siblings);
112 /* return number of HTs in the same core of \a cpu */
114 cfs_cpu_ht_nsiblings(int cpu)
118 down(&cpt_data.cpt_mutex);
120 cfs_cpu_ht_siblings(cpu, cpt_data.cpt_cpumask);
121 num = cpus_weight(*cpt_data.cpt_cpumask);
123 up(&cpt_data.cpt_mutex);
127 EXPORT_SYMBOL(cfs_cpu_ht_nsiblings);
130 cfs_node_to_cpumask(int node, cpumask_t *mask)
132 cpumask_copy(mask, cpumask_of_node(node));
134 EXPORT_SYMBOL(cfs_node_to_cpumask);
137 cfs_cpt_table_free(struct cfs_cpt_table *cptab)
141 if (cptab->ctb_cpu2cpt != NULL) {
142 LIBCFS_FREE(cptab->ctb_cpu2cpt,
143 num_possible_cpus() *
144 sizeof(cptab->ctb_cpu2cpt[0]));
147 for (i = 0; cptab->ctb_parts != NULL && i < cptab->ctb_nparts; i++) {
148 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
150 if (part->cpt_nodemask != NULL) {
151 LIBCFS_FREE(part->cpt_nodemask,
152 sizeof(*part->cpt_nodemask));
155 if (part->cpt_cpumask != NULL)
156 LIBCFS_FREE(part->cpt_cpumask, cpumask_size());
159 if (cptab->ctb_parts != NULL) {
160 LIBCFS_FREE(cptab->ctb_parts,
161 cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
164 if (cptab->ctb_nodemask != NULL)
165 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
166 if (cptab->ctb_cpumask != NULL)
167 LIBCFS_FREE(cptab->ctb_cpumask, cpumask_size());
169 LIBCFS_FREE(cptab, sizeof(*cptab));
171 EXPORT_SYMBOL(cfs_cpt_table_free);
173 struct cfs_cpt_table *
174 cfs_cpt_table_alloc(unsigned int ncpt)
176 struct cfs_cpt_table *cptab;
179 LIBCFS_ALLOC(cptab, sizeof(*cptab));
183 cptab->ctb_nparts = ncpt;
185 LIBCFS_ALLOC(cptab->ctb_cpumask, cpumask_size());
186 LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
188 if (cptab->ctb_cpumask == NULL || cptab->ctb_nodemask == NULL)
191 LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
192 num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
193 if (cptab->ctb_cpu2cpt == NULL)
196 memset(cptab->ctb_cpu2cpt, -1,
197 num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
199 LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
200 if (cptab->ctb_parts == NULL)
203 for (i = 0; i < ncpt; i++) {
204 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
206 LIBCFS_ALLOC(part->cpt_cpumask, cpumask_size());
207 LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
208 if (part->cpt_cpumask == NULL || part->cpt_nodemask == NULL)
212 spin_lock(&cpt_data.cpt_lock);
213 /* Reserved for hotplug */
214 cptab->ctb_version = cpt_data.cpt_version;
215 spin_unlock(&cpt_data.cpt_lock);
220 cfs_cpt_table_free(cptab);
223 EXPORT_SYMBOL(cfs_cpt_table_alloc);
226 cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
233 for (i = 0; i < cptab->ctb_nparts; i++) {
235 rc = snprintf(tmp, len, "%d\t: ", i);
245 for_each_cpu_mask(j, *cptab->ctb_parts[i].cpt_cpumask) {
246 rc = snprintf(tmp, len, "%d ", j);
266 EXPORT_SYMBOL(cfs_cpt_table_print);
269 cfs_cpt_number(struct cfs_cpt_table *cptab)
271 return cptab->ctb_nparts;
273 EXPORT_SYMBOL(cfs_cpt_number);
276 cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
278 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
280 return cpt == CFS_CPT_ANY ?
281 cpus_weight(*cptab->ctb_cpumask) :
282 cpus_weight(*cptab->ctb_parts[cpt].cpt_cpumask);
284 EXPORT_SYMBOL(cfs_cpt_weight);
287 cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
289 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
291 return cpt == CFS_CPT_ANY ?
292 any_online_cpu(*cptab->ctb_cpumask) != NR_CPUS :
293 any_online_cpu(*cptab->ctb_parts[cpt].cpt_cpumask) != NR_CPUS;
295 EXPORT_SYMBOL(cfs_cpt_online);
298 cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
300 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
302 return cpt == CFS_CPT_ANY ?
303 cptab->ctb_cpumask : cptab->ctb_parts[cpt].cpt_cpumask;
305 EXPORT_SYMBOL(cfs_cpt_cpumask);
308 cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
310 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
312 return cpt == CFS_CPT_ANY ?
313 cptab->ctb_nodemask : cptab->ctb_parts[cpt].cpt_nodemask;
315 EXPORT_SYMBOL(cfs_cpt_nodemask);
318 cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
322 LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
324 if (cpu < 0 || cpu >= NR_CPUS || !cpu_online(cpu)) {
325 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
329 if (cptab->ctb_cpu2cpt[cpu] != -1) {
330 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
331 cpu, cptab->ctb_cpu2cpt[cpu]);
335 cptab->ctb_cpu2cpt[cpu] = cpt;
337 LASSERT(!cpu_isset(cpu, *cptab->ctb_cpumask));
338 LASSERT(!cpu_isset(cpu, *cptab->ctb_parts[cpt].cpt_cpumask));
340 cpu_set(cpu, *cptab->ctb_cpumask);
341 cpu_set(cpu, *cptab->ctb_parts[cpt].cpt_cpumask);
343 node = cpu_to_node(cpu);
345 /* first CPU of @node in this CPT table */
346 if (!node_isset(node, *cptab->ctb_nodemask))
347 node_set(node, *cptab->ctb_nodemask);
349 /* first CPU of @node in this partition */
350 if (!node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask))
351 node_set(node, *cptab->ctb_parts[cpt].cpt_nodemask);
355 EXPORT_SYMBOL(cfs_cpt_set_cpu);
358 cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
363 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
365 if (cpu < 0 || cpu >= NR_CPUS) {
366 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
370 if (cpt == CFS_CPT_ANY) {
371 /* caller doesn't know the partition ID */
372 cpt = cptab->ctb_cpu2cpt[cpu];
373 if (cpt < 0) { /* not set in this CPT-table */
374 CDEBUG(D_INFO, "Try to unset cpu %d which is "
375 "not in CPT-table %p\n", cpt, cptab);
379 } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
381 "CPU %d is not in cpu-partition %d\n", cpu, cpt);
385 LASSERT(cpu_isset(cpu, *cptab->ctb_parts[cpt].cpt_cpumask));
386 LASSERT(cpu_isset(cpu, *cptab->ctb_cpumask));
388 cpu_clear(cpu, *cptab->ctb_parts[cpt].cpt_cpumask);
389 cpu_clear(cpu, *cptab->ctb_cpumask);
390 cptab->ctb_cpu2cpt[cpu] = -1;
392 node = cpu_to_node(cpu);
394 LASSERT(node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask));
395 LASSERT(node_isset(node, *cptab->ctb_nodemask));
397 for_each_cpu_mask(i, *cptab->ctb_parts[cpt].cpt_cpumask) {
398 /* this CPT has other CPU belonging to this node? */
399 if (cpu_to_node(i) == node)
404 node_clear(node, *cptab->ctb_parts[cpt].cpt_nodemask);
406 for_each_cpu_mask(i, *cptab->ctb_cpumask) {
407 /* this CPT-table has other CPU belonging to this node? */
408 if (cpu_to_node(i) == node)
413 node_clear(node, *cptab->ctb_nodemask);
417 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
420 cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
424 if (cpus_weight(*mask) == 0 || any_online_cpu(*mask) == NR_CPUS) {
425 CDEBUG(D_INFO, "No online CPU is found in the CPU mask "
426 "for CPU partition %d\n", cpt);
430 for_each_cpu_mask(i, *mask) {
431 if (!cfs_cpt_set_cpu(cptab, cpt, i))
437 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
440 cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
444 for_each_cpu_mask(i, *mask)
445 cfs_cpt_unset_cpu(cptab, cpt, i);
447 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
450 cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
455 if (node < 0 || node >= MAX_NUMNODES) {
457 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
461 down(&cpt_data.cpt_mutex);
463 mask = cpt_data.cpt_cpumask;
464 cfs_node_to_cpumask(node, mask);
466 rc = cfs_cpt_set_cpumask(cptab, cpt, mask);
468 up(&cpt_data.cpt_mutex);
472 EXPORT_SYMBOL(cfs_cpt_set_node);
475 cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
479 if (node < 0 || node >= MAX_NUMNODES) {
481 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
485 down(&cpt_data.cpt_mutex);
487 mask = cpt_data.cpt_cpumask;
488 cfs_node_to_cpumask(node, mask);
490 cfs_cpt_unset_cpumask(cptab, cpt, mask);
492 up(&cpt_data.cpt_mutex);
494 EXPORT_SYMBOL(cfs_cpt_unset_node);
497 cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
501 for_each_node_mask(i, *mask) {
502 if (!cfs_cpt_set_node(cptab, cpt, i))
508 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
511 cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
515 for_each_node_mask(i, *mask)
516 cfs_cpt_unset_node(cptab, cpt, i);
518 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
521 cfs_cpt_clear(struct cfs_cpt_table *cptab, int cpt)
526 if (cpt == CFS_CPT_ANY) {
527 last = cptab->ctb_nparts - 1;
533 for (; cpt <= last; cpt++) {
534 for_each_cpu_mask(i, *cptab->ctb_parts[cpt].cpt_cpumask)
535 cfs_cpt_unset_cpu(cptab, cpt, i);
538 EXPORT_SYMBOL(cfs_cpt_clear);
541 cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
548 /* convert CPU partition ID to HW node id */
550 if (cpt < 0 || cpt >= cptab->ctb_nparts) {
551 mask = cptab->ctb_nodemask;
552 rotor = cptab->ctb_spread_rotor++;
554 mask = cptab->ctb_parts[cpt].cpt_nodemask;
555 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
558 weight = nodes_weight(*mask);
563 for_each_node_mask(node, *mask) {
571 EXPORT_SYMBOL(cfs_cpt_spread_node);
574 cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
576 int cpu = smp_processor_id();
577 int cpt = cptab->ctb_cpu2cpt[cpu];
583 /* don't return negative value for safety of upper layer,
584 * instead we shadow the unknown cpu to a valid partition ID */
585 cpt = cpu % cptab->ctb_nparts;
590 EXPORT_SYMBOL(cfs_cpt_current);
593 cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
595 LASSERT(cpu >= 0 && cpu < NR_CPUS);
597 return cptab->ctb_cpu2cpt[cpu];
599 EXPORT_SYMBOL(cfs_cpt_of_cpu);
602 cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
605 nodemask_t *nodemask;
609 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
611 if (cpt == CFS_CPT_ANY) {
612 cpumask = cptab->ctb_cpumask;
613 nodemask = cptab->ctb_nodemask;
615 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
616 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
619 if (any_online_cpu(*cpumask) == NR_CPUS) {
620 CERROR("No online CPU found in CPU partition %d, did someone "
621 "do CPU hotplug on system? You might need to reload "
622 "Lustre modules to keep system working well.\n", cpt);
626 for_each_online_cpu(i) {
627 if (cpu_isset(i, *cpumask))
630 rc = set_cpus_allowed_ptr(current, cpumask);
631 set_mems_allowed(*nodemask);
633 schedule(); /* switch to allowed CPU */
638 /* don't need to set affinity because all online CPUs are covered */
641 EXPORT_SYMBOL(cfs_cpt_bind);
644 * Choose max to \a number CPUs from \a node and set them in \a cpt.
645 * We always prefer to choose CPU in the same core/socket.
648 cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
649 cpumask_t *node, int number)
651 cpumask_t *socket = NULL;
652 cpumask_t *core = NULL;
658 if (number >= cpus_weight(*node)) {
659 while (!cpus_empty(*node)) {
660 cpu = first_cpu(*node);
662 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
665 cpu_clear(cpu, *node);
670 /* allocate scratch buffer */
671 LIBCFS_ALLOC(socket, cpumask_size());
672 LIBCFS_ALLOC(core, cpumask_size());
673 if (socket == NULL || core == NULL) {
678 while (!cpus_empty(*node)) {
679 cpu = first_cpu(*node);
681 /* get cpumask for cores in the same socket */
682 cfs_cpu_core_siblings(cpu, socket);
683 cpus_and(*socket, *socket, *node);
685 LASSERT(!cpus_empty(*socket));
687 while (!cpus_empty(*socket)) {
690 /* get cpumask for hts in the same core */
691 cfs_cpu_ht_siblings(cpu, core);
692 cpus_and(*core, *core, *node);
694 LASSERT(!cpus_empty(*core));
696 for_each_cpu_mask(i, *core) {
697 cpu_clear(i, *socket);
700 rc = cfs_cpt_set_cpu(cptab, cpt, i);
709 cpu = first_cpu(*socket);
715 LIBCFS_FREE(socket, cpumask_size());
717 LIBCFS_FREE(core, cpumask_size());
721 #define CPT_WEIGHT_MIN 4u
724 cfs_cpt_num_estimate(void)
726 unsigned nnode = num_online_nodes();
727 unsigned ncpu = num_online_cpus();
730 if (ncpu <= CPT_WEIGHT_MIN) {
735 /* generate reasonable number of CPU partitions based on total number
736 * of CPUs, Preferred N should be power2 and match this condition:
737 * 2 * (N - 1)^2 < NCPUS <= 2 * N^2 */
738 for (ncpt = 2; ncpu > 2 * ncpt * ncpt; ncpt <<= 1) {}
740 if (ncpt <= nnode) { /* fat numa system */
744 } else { /* ncpt > nnode */
745 while ((nnode << 1) <= ncpt)
752 #if (BITS_PER_LONG == 32)
753 /* config many CPU partitions on 32-bit system could consume
755 ncpt = min(2U, ncpt);
757 while (ncpu % ncpt != 0)
758 ncpt--; /* worst case is 1 */
763 static struct cfs_cpt_table *
764 cfs_cpt_table_create(int ncpt)
766 struct cfs_cpt_table *cptab = NULL;
767 cpumask_t *mask = NULL;
773 rc = cfs_cpt_num_estimate();
777 if (ncpt > num_online_cpus() || ncpt > 4 * rc) {
778 CWARN("CPU partition number %d is larger than suggested "
779 "value (%d), your system may have performance"
780 "issue or run out of memory while under pressure\n",
784 if (num_online_cpus() % ncpt != 0) {
785 CERROR("CPU number %d is not multiple of cpu_npartition %d, "
786 "please try different cpu_npartitions value or"
787 "set pattern string by cpu_pattern=STRING\n",
788 (int)num_online_cpus(), ncpt);
792 cptab = cfs_cpt_table_alloc(ncpt);
794 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
798 num = num_online_cpus() / ncpt;
800 CERROR("CPU changed while setting CPU partition\n");
804 LIBCFS_ALLOC(mask, cpumask_size());
806 CERROR("Failed to allocate scratch cpumask\n");
810 for_each_online_node(i) {
811 cfs_node_to_cpumask(i, mask);
813 while (!cpus_empty(*mask)) {
814 struct cfs_cpu_partition *part;
817 /* Each emulated NUMA node has all allowed CPUs in
819 * End loop when all partitions have assigned CPUs.
824 part = &cptab->ctb_parts[cpt];
826 n = num - cpus_weight(*part->cpt_cpumask);
829 rc = cfs_cpt_choose_ncpus(cptab, cpt, mask, n);
833 LASSERT(num >= cpus_weight(*part->cpt_cpumask));
834 if (num == cpus_weight(*part->cpt_cpumask))
840 num != cpus_weight(*cptab->ctb_parts[ncpt - 1].cpt_cpumask)) {
841 CERROR("Expect %d(%d) CPU partitions but got %d(%d), "
842 "CPU hotplug/unplug while setting?\n",
843 cptab->ctb_nparts, num, cpt,
844 cpus_weight(*cptab->ctb_parts[ncpt - 1].cpt_cpumask));
848 LIBCFS_FREE(mask, cpumask_size());
853 CERROR("Failed to setup CPU-partition-table with %d "
854 "CPU-partitions, online HW nodes: %d, HW cpus: %d.\n",
855 ncpt, num_online_nodes(), num_online_cpus());
858 LIBCFS_FREE(mask, cpumask_size());
861 cfs_cpt_table_free(cptab);
866 static struct cfs_cpt_table *
867 cfs_cpt_table_create_pattern(char *pattern)
869 struct cfs_cpt_table *cptab;
876 for (ncpt = 0;; ncpt++) { /* quick scan bracket */
877 str = strchr(str, '[');
883 str = cfs_trimwhite(pattern);
884 if (*str == 'n' || *str == 'N') {
890 (node && ncpt > num_online_nodes()) ||
891 (!node && ncpt > num_online_cpus())) {
892 CERROR("Invalid pattern %s, or too many partitions %d\n",
897 high = node ? MAX_NUMNODES - 1 : NR_CPUS - 1;
899 cptab = cfs_cpt_table_alloc(ncpt);
901 CERROR("Failed to allocate cpu partition table\n");
905 for (str = cfs_trimwhite(pattern), c = 0;; c++) {
906 struct cfs_range_expr *range;
907 struct cfs_expr_list *el;
908 char *bracket = strchr(str, '[');
914 if (bracket == NULL) {
916 CERROR("Invalid pattern %s\n", str);
918 } else if (c != ncpt) {
919 CERROR("expect %d partitions but found %d\n",
926 if (sscanf(str, "%u%n", &cpt, &n) < 1) {
927 CERROR("Invalid cpu pattern %s\n", str);
931 if (cpt < 0 || cpt >= ncpt) {
932 CERROR("Invalid partition id %d, total partitions %d\n",
937 if (cfs_cpt_weight(cptab, cpt) != 0) {
938 CERROR("Partition %d has already been set.\n", cpt);
942 str = cfs_trimwhite(str + n);
943 if (str != bracket) {
944 CERROR("Invalid pattern %s\n", str);
948 bracket = strchr(str, ']');
949 if (bracket == NULL) {
950 CERROR("missing right bracket for cpt %d, %s\n",
955 if (cfs_expr_list_parse(str, (bracket - str) + 1,
956 0, high, &el) != 0) {
957 CERROR("Can't parse number range: %s\n", str);
961 list_for_each_entry(range, &el->el_exprs, re_link) {
962 for (i = range->re_lo; i <= range->re_hi; i++) {
963 if ((i - range->re_lo) % range->re_stride != 0)
966 rc = node ? cfs_cpt_set_node(cptab, cpt, i) :
967 cfs_cpt_set_cpu(cptab, cpt, i);
969 cfs_expr_list_free(el);
975 cfs_expr_list_free(el);
977 if (!cfs_cpt_online(cptab, cpt)) {
978 CERROR("No online CPU is found on partition %d\n", cpt);
982 str = cfs_trimwhite(bracket + 1);
988 cfs_cpt_table_free(cptab);
992 #ifdef CONFIG_HOTPLUG_CPU
994 cfs_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
996 unsigned int cpu = (unsigned long)hcpu;
1001 case CPU_DEAD_FROZEN:
1003 case CPU_ONLINE_FROZEN:
1004 spin_lock(&cpt_data.cpt_lock);
1005 cpt_data.cpt_version++;
1006 spin_unlock(&cpt_data.cpt_lock);
1008 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
1009 CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
1014 down(&cpt_data.cpt_mutex);
1015 /* if all HTs in a core are offline, it may break affinity */
1016 cfs_cpu_ht_siblings(cpu, cpt_data.cpt_cpumask);
1017 warn = any_online_cpu(*cpt_data.cpt_cpumask) >= nr_cpu_ids;
1018 up(&cpt_data.cpt_mutex);
1019 CDEBUG(warn ? D_WARNING : D_INFO,
1020 "Lustre: can't support CPU plug-out well now, "
1021 "performance and stability could be impacted"
1022 "[CPU %u action: %lx]\n", cpu, action);
1028 static struct notifier_block cfs_cpu_notifier = {
1029 .notifier_call = cfs_cpu_notify,
1038 if (cfs_cpt_table != NULL)
1039 cfs_cpt_table_free(cfs_cpt_table);
1041 #ifdef CONFIG_HOTPLUG_CPU
1042 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1044 if (cpt_data.cpt_cpumask != NULL)
1045 LIBCFS_FREE(cpt_data.cpt_cpumask, cpumask_size());
1051 LASSERT(cfs_cpt_table == NULL);
1053 memset(&cpt_data, 0, sizeof(cpt_data));
1055 LIBCFS_ALLOC(cpt_data.cpt_cpumask, cpumask_size());
1056 if (cpt_data.cpt_cpumask == NULL) {
1057 CERROR("Failed to allocate scratch buffer\n");
1061 spin_lock_init(&cpt_data.cpt_lock);
1062 sema_init(&cpt_data.cpt_mutex, 1);
1064 #ifdef CONFIG_HOTPLUG_CPU
1065 register_hotcpu_notifier(&cfs_cpu_notifier);
1068 if (*cpu_pattern != 0) {
1069 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern);
1070 if (cfs_cpt_table == NULL) {
1071 CERROR("Failed to create cptab from pattern %s\n",
1077 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1078 if (cfs_cpt_table == NULL) {
1079 CERROR("Failed to create ptable with npartitions %d\n",
1085 spin_lock(&cpt_data.cpt_lock);
1086 if (cfs_cpt_table->ctb_version != cpt_data.cpt_version) {
1087 spin_unlock(&cpt_data.cpt_lock);
1088 CERROR("CPU hotplug/unplug during setup\n");
1091 spin_unlock(&cpt_data.cpt_lock);
1093 LCONSOLE(0, "HW CPU cores: %d, npartitions: %d\n",
1094 num_online_cpus(), cfs_cpt_number(cfs_cpt_table));