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, 2015, 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 /* serialize hotplug etc */
74 /* reserved for hotplug */
75 unsigned long cpt_version;
76 /* mutex to protect cpt_cpumask */
77 struct mutex cpt_mutex;
78 /* scratch buffer for set/unset_node */
79 cpumask_t *cpt_cpumask;
82 static struct cfs_cpt_data cpt_data;
84 /* return number of cores in the same socket of \a cpu */
86 cfs_cpu_core_nsiblings(int cpu)
90 mutex_lock(&cpt_data.cpt_mutex);
92 cpumask_copy(cpt_data.cpt_cpumask, topology_core_cpumask(cpu));
93 num = cpumask_weight(cpt_data.cpt_cpumask);
95 mutex_unlock(&cpt_data.cpt_mutex);
100 /* return cpumask of HTs in the same core */
102 cfs_cpu_ht_siblings(int cpu, cpumask_t *mask)
104 cpumask_copy(mask, topology_sibling_cpumask(cpu));
107 /* return number of HTs in the same core of \a cpu */
109 cfs_cpu_ht_nsiblings(int cpu)
113 num = cpumask_weight(topology_sibling_cpumask(cpu));
117 EXPORT_SYMBOL(cfs_cpu_ht_nsiblings);
120 cfs_node_to_cpumask(int node, cpumask_t *mask)
122 const cpumask_t *tmp = cpumask_of_node(node);
125 cpumask_copy(mask, tmp);
131 cfs_cpt_table_free(struct cfs_cpt_table *cptab)
135 if (cptab->ctb_cpu2cpt != NULL) {
136 LIBCFS_FREE(cptab->ctb_cpu2cpt,
137 num_possible_cpus() *
138 sizeof(cptab->ctb_cpu2cpt[0]));
141 for (i = 0; cptab->ctb_parts != NULL && i < cptab->ctb_nparts; i++) {
142 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
144 if (part->cpt_nodemask != NULL) {
145 LIBCFS_FREE(part->cpt_nodemask,
146 sizeof(*part->cpt_nodemask));
149 if (part->cpt_cpumask != NULL)
150 LIBCFS_FREE(part->cpt_cpumask, cpumask_size());
153 if (cptab->ctb_parts != NULL) {
154 LIBCFS_FREE(cptab->ctb_parts,
155 cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
158 if (cptab->ctb_nodemask != NULL)
159 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
160 if (cptab->ctb_cpumask != NULL)
161 LIBCFS_FREE(cptab->ctb_cpumask, cpumask_size());
163 LIBCFS_FREE(cptab, sizeof(*cptab));
165 EXPORT_SYMBOL(cfs_cpt_table_free);
167 struct cfs_cpt_table *
168 cfs_cpt_table_alloc(unsigned int ncpt)
170 struct cfs_cpt_table *cptab;
173 LIBCFS_ALLOC(cptab, sizeof(*cptab));
177 cptab->ctb_nparts = ncpt;
179 LIBCFS_ALLOC(cptab->ctb_cpumask, cpumask_size());
180 LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
182 if (cptab->ctb_cpumask == NULL || cptab->ctb_nodemask == NULL)
185 LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
186 num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
187 if (cptab->ctb_cpu2cpt == NULL)
190 memset(cptab->ctb_cpu2cpt, -1,
191 num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
193 LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
194 if (cptab->ctb_parts == NULL)
197 for (i = 0; i < ncpt; i++) {
198 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
200 LIBCFS_ALLOC(part->cpt_cpumask, cpumask_size());
201 LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
202 if (part->cpt_cpumask == NULL || part->cpt_nodemask == NULL)
206 spin_lock(&cpt_data.cpt_lock);
207 /* Reserved for hotplug */
208 cptab->ctb_version = cpt_data.cpt_version;
209 spin_unlock(&cpt_data.cpt_lock);
214 cfs_cpt_table_free(cptab);
217 EXPORT_SYMBOL(cfs_cpt_table_alloc);
220 cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
227 for (i = 0; i < cptab->ctb_nparts; i++) {
229 rc = snprintf(tmp, len, "%d\t: ", i);
239 for_each_cpu(j, cptab->ctb_parts[i].cpt_cpumask) {
240 rc = snprintf(tmp, len, "%d ", j);
260 EXPORT_SYMBOL(cfs_cpt_table_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_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
318 LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
320 if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu)) {
321 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
325 if (cptab->ctb_cpu2cpt[cpu] != -1) {
326 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
327 cpu, cptab->ctb_cpu2cpt[cpu]);
331 cptab->ctb_cpu2cpt[cpu] = cpt;
333 LASSERT(!cpumask_test_cpu(cpu, cptab->ctb_cpumask));
334 LASSERT(!cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
336 cpumask_set_cpu(cpu, cptab->ctb_cpumask);
337 cpumask_set_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
339 node = cpu_to_node(cpu);
341 /* first CPU of @node in this CPT table */
342 if (!node_isset(node, *cptab->ctb_nodemask))
343 node_set(node, *cptab->ctb_nodemask);
345 /* first CPU of @node in this partition */
346 if (!node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask))
347 node_set(node, *cptab->ctb_parts[cpt].cpt_nodemask);
351 EXPORT_SYMBOL(cfs_cpt_set_cpu);
354 cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
359 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
361 if (cpu < 0 || cpu >= nr_cpu_ids) {
362 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
366 if (cpt == CFS_CPT_ANY) {
367 /* caller doesn't know the partition ID */
368 cpt = cptab->ctb_cpu2cpt[cpu];
369 if (cpt < 0) { /* not set in this CPT-table */
370 CDEBUG(D_INFO, "Try to unset cpu %d which is "
371 "not in CPT-table %p\n", cpt, cptab);
375 } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
377 "CPU %d is not in cpu-partition %d\n", cpu, cpt);
381 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
382 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_cpumask));
384 cpumask_clear_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
385 cpumask_clear_cpu(cpu, cptab->ctb_cpumask);
386 cptab->ctb_cpu2cpt[cpu] = -1;
388 node = cpu_to_node(cpu);
390 LASSERT(node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask));
391 LASSERT(node_isset(node, *cptab->ctb_nodemask));
393 for_each_cpu(i, cptab->ctb_parts[cpt].cpt_cpumask) {
394 /* this CPT has other CPU belonging to this node? */
395 if (cpu_to_node(i) == node)
400 node_clear(node, *cptab->ctb_parts[cpt].cpt_nodemask);
402 for_each_cpu(i, cptab->ctb_cpumask) {
403 /* this CPT-table has other CPU belonging to this node? */
404 if (cpu_to_node(i) == node)
409 node_clear(node, *cptab->ctb_nodemask);
413 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
416 cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
420 if (cpumask_weight(mask) == 0 ||
421 cpumask_any_and(mask, cpu_online_mask) >= nr_cpu_ids) {
422 CDEBUG(D_INFO, "No online CPU is found in the CPU mask "
423 "for CPU partition %d\n", cpt);
427 for_each_cpu(i, mask) {
428 if (!cfs_cpt_set_cpu(cptab, cpt, i))
434 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
437 cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
441 for_each_cpu(i, mask)
442 cfs_cpt_unset_cpu(cptab, cpt, i);
444 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
447 cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
452 if (node < 0 || node >= MAX_NUMNODES) {
454 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
458 mutex_lock(&cpt_data.cpt_mutex);
460 mask = cpt_data.cpt_cpumask;
461 cfs_node_to_cpumask(node, mask);
463 rc = cfs_cpt_set_cpumask(cptab, cpt, mask);
465 mutex_unlock(&cpt_data.cpt_mutex);
469 EXPORT_SYMBOL(cfs_cpt_set_node);
472 cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
476 if (node < 0 || node >= MAX_NUMNODES) {
478 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
482 mutex_lock(&cpt_data.cpt_mutex);
484 mask = cpt_data.cpt_cpumask;
485 cfs_node_to_cpumask(node, mask);
487 cfs_cpt_unset_cpumask(cptab, cpt, mask);
489 mutex_unlock(&cpt_data.cpt_mutex);
491 EXPORT_SYMBOL(cfs_cpt_unset_node);
494 cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
498 for_each_node_mask(i, *mask) {
499 if (!cfs_cpt_set_node(cptab, cpt, i))
505 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
508 cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
512 for_each_node_mask(i, *mask)
513 cfs_cpt_unset_node(cptab, cpt, i);
515 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
518 cfs_cpt_clear(struct cfs_cpt_table *cptab, int cpt)
523 if (cpt == CFS_CPT_ANY) {
524 last = cptab->ctb_nparts - 1;
530 for (; cpt <= last; cpt++) {
531 for_each_cpu(i, cptab->ctb_parts[cpt].cpt_cpumask)
532 cfs_cpt_unset_cpu(cptab, cpt, i);
535 EXPORT_SYMBOL(cfs_cpt_clear);
538 cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
545 /* convert CPU partition ID to HW node id */
547 if (cpt < 0 || cpt >= cptab->ctb_nparts) {
548 mask = cptab->ctb_nodemask;
549 rotor = cptab->ctb_spread_rotor++;
551 mask = cptab->ctb_parts[cpt].cpt_nodemask;
552 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
555 weight = nodes_weight(*mask);
560 for_each_node_mask(node, *mask) {
568 EXPORT_SYMBOL(cfs_cpt_spread_node);
571 cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
573 int cpu = smp_processor_id();
574 int cpt = cptab->ctb_cpu2cpt[cpu];
580 /* don't return negative value for safety of upper layer,
581 * instead we shadow the unknown cpu to a valid partition ID */
582 cpt = cpu % cptab->ctb_nparts;
587 EXPORT_SYMBOL(cfs_cpt_current);
590 cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
592 LASSERT(cpu >= 0 && cpu < nr_cpu_ids);
594 return cptab->ctb_cpu2cpt[cpu];
596 EXPORT_SYMBOL(cfs_cpt_of_cpu);
599 cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
602 nodemask_t *nodemask;
606 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
608 if (cpt == CFS_CPT_ANY) {
609 cpumask = cptab->ctb_cpumask;
610 nodemask = cptab->ctb_nodemask;
612 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
613 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
616 if (cpumask_any_and(cpumask, cpu_online_mask) >= nr_cpu_ids) {
617 CERROR("No online CPU found in CPU partition %d, did someone "
618 "do CPU hotplug on system? You might need to reload "
619 "Lustre modules to keep system working well.\n", cpt);
623 for_each_online_cpu(i) {
624 if (cpumask_test_cpu(i, cpumask))
627 rc = set_cpus_allowed_ptr(current, cpumask);
628 set_mems_allowed(*nodemask);
630 schedule(); /* switch to allowed CPU */
635 /* don't need to set affinity because all online CPUs are covered */
638 EXPORT_SYMBOL(cfs_cpt_bind);
641 * Choose max to \a number CPUs from \a node and set them in \a cpt.
642 * We always prefer to choose CPU in the same core/socket.
645 cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
646 cpumask_t *node, int number)
648 cpumask_t *socket = NULL;
649 cpumask_t *core = NULL;
655 if (number >= cpumask_weight(node)) {
656 while (!cpumask_empty(node)) {
657 cpu = cpumask_first(node);
659 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
662 cpumask_clear_cpu(cpu, node);
667 /* allocate scratch buffer */
668 LIBCFS_ALLOC(socket, cpumask_size());
669 LIBCFS_ALLOC(core, cpumask_size());
670 if (socket == NULL || core == NULL) {
675 while (!cpumask_empty(node)) {
676 cpu = cpumask_first(node);
678 /* get cpumask for cores in the same socket */
679 cpumask_copy(socket, topology_core_cpumask(cpu));
680 cpumask_and(socket, socket, node);
682 LASSERT(!cpumask_empty(socket));
684 while (!cpumask_empty(socket)) {
687 /* get cpumask for hts in the same core */
688 cpumask_copy(core, topology_sibling_cpumask(cpu));
689 cpumask_and(core, core, node);
691 LASSERT(!cpumask_empty(core));
693 for_each_cpu(i, core) {
694 cpumask_clear_cpu(i, socket);
695 cpumask_clear_cpu(i, node);
697 rc = cfs_cpt_set_cpu(cptab, cpt, i);
706 cpu = cpumask_first(socket);
712 LIBCFS_FREE(socket, cpumask_size());
714 LIBCFS_FREE(core, cpumask_size());
718 #define CPT_WEIGHT_MIN 4u
721 cfs_cpt_num_estimate(void)
723 unsigned nnode = num_online_nodes();
724 unsigned ncpu = num_online_cpus();
727 if (ncpu <= CPT_WEIGHT_MIN) {
732 /* generate reasonable number of CPU partitions based on total number
733 * of CPUs, Preferred N should be power2 and match this condition:
734 * 2 * (N - 1)^2 < NCPUS <= 2 * N^2 */
735 for (ncpt = 2; ncpu > 2 * ncpt * ncpt; ncpt <<= 1) {}
737 if (ncpt <= nnode) { /* fat numa system */
741 } else { /* ncpt > nnode */
742 while ((nnode << 1) <= ncpt)
749 #if (BITS_PER_LONG == 32)
750 /* config many CPU partitions on 32-bit system could consume
752 ncpt = min(2U, ncpt);
754 while (ncpu % ncpt != 0)
755 ncpt--; /* worst case is 1 */
760 static struct cfs_cpt_table *
761 cfs_cpt_table_create(int ncpt)
763 struct cfs_cpt_table *cptab = NULL;
764 cpumask_t *mask = NULL;
770 rc = cfs_cpt_num_estimate();
774 if (ncpt > num_online_cpus() || ncpt > 4 * rc) {
775 CWARN("CPU partition number %d is larger than suggested "
776 "value (%d), your system may have performance"
777 "issue or run out of memory while under pressure\n",
781 if (num_online_cpus() % ncpt != 0) {
782 CERROR("CPU number %d is not multiple of cpu_npartition %d, "
783 "please try different cpu_npartitions value or"
784 "set pattern string by cpu_pattern=STRING\n",
785 (int)num_online_cpus(), ncpt);
789 cptab = cfs_cpt_table_alloc(ncpt);
791 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
795 num = num_online_cpus() / ncpt;
797 CERROR("CPU changed while setting CPU partition\n");
801 LIBCFS_ALLOC(mask, cpumask_size());
803 CERROR("Failed to allocate scratch cpumask\n");
807 for_each_online_node(i) {
808 cfs_node_to_cpumask(i, mask);
810 while (!cpumask_empty(mask)) {
811 struct cfs_cpu_partition *part;
814 /* Each emulated NUMA node has all allowed CPUs in
816 * End loop when all partitions have assigned CPUs.
821 part = &cptab->ctb_parts[cpt];
823 n = num - cpumask_weight(part->cpt_cpumask);
826 rc = cfs_cpt_choose_ncpus(cptab, cpt, mask, n);
830 LASSERT(num >= cpumask_weight(part->cpt_cpumask));
831 if (num == cpumask_weight(part->cpt_cpumask))
837 num != cpumask_weight(cptab->ctb_parts[ncpt - 1].cpt_cpumask)) {
838 CERROR("Expect %d(%d) CPU partitions but got %d(%d), "
839 "CPU hotplug/unplug while setting?\n",
840 cptab->ctb_nparts, num, cpt,
841 cpumask_weight(cptab->ctb_parts[ncpt - 1].cpt_cpumask));
845 LIBCFS_FREE(mask, cpumask_size());
850 CERROR("Failed to setup CPU-partition-table with %d "
851 "CPU-partitions, online HW nodes: %d, HW cpus: %d.\n",
852 ncpt, num_online_nodes(), num_online_cpus());
855 LIBCFS_FREE(mask, cpumask_size());
858 cfs_cpt_table_free(cptab);
863 static struct cfs_cpt_table *
864 cfs_cpt_table_create_pattern(char *pattern)
866 struct cfs_cpt_table *cptab;
876 str = cfs_trimwhite(pattern);
877 if (*str == 'n' || *str == 'N') {
879 if (*pattern != '\0') {
880 node = 1; /* numa pattern */
882 } else { /* shortcut to create CPT from NUMA & CPU topology */
884 ncpt = num_online_nodes();
888 if (ncpt == 0) { /* scanning bracket which is mark of partition */
889 for (str = pattern;; str++, ncpt++) {
890 str = strchr(str, '[');
897 (node && ncpt > num_online_nodes()) ||
898 (!node && ncpt > num_online_cpus())) {
899 CERROR("Invalid pattern %s, or too many partitions %d\n",
904 cptab = cfs_cpt_table_alloc(ncpt);
906 CERROR("Failed to allocate cpu partition table\n");
910 if (node < 0) { /* shortcut to create CPT from NUMA & CPU topology */
912 for_each_online_node(i) {
914 CERROR("CPU changed while setting CPU "
915 "partition table, %d/%d\n", cpt, ncpt);
919 rc = cfs_cpt_set_node(cptab, cpt++, i);
926 high = node ? MAX_NUMNODES - 1 : nr_cpu_ids - 1;
928 for (str = cfs_trimwhite(pattern), c = 0;; c++) {
929 struct cfs_range_expr *range;
930 struct cfs_expr_list *el;
931 char *bracket = strchr(str, '[');
934 if (bracket == NULL) {
936 CERROR("Invalid pattern %s\n", str);
938 } else if (c != ncpt) {
939 CERROR("expect %d partitions but found %d\n",
946 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
947 CERROR("Invalid cpu pattern %s\n", str);
951 if (cpt < 0 || cpt >= ncpt) {
952 CERROR("Invalid partition id %d, total partitions %d\n",
957 if (cfs_cpt_weight(cptab, cpt) != 0) {
958 CERROR("Partition %d has already been set.\n", cpt);
962 str = cfs_trimwhite(str + n);
963 if (str != bracket) {
964 CERROR("Invalid pattern %s\n", str);
968 bracket = strchr(str, ']');
969 if (bracket == NULL) {
970 CERROR("missing right bracket for cpt %d, %s\n",
975 if (cfs_expr_list_parse(str, (bracket - str) + 1,
976 0, high, &el) != 0) {
977 CERROR("Can't parse number range: %s\n", str);
981 list_for_each_entry(range, &el->el_exprs, re_link) {
982 for (i = range->re_lo; i <= range->re_hi; i++) {
983 if ((i - range->re_lo) % range->re_stride != 0)
986 rc = node ? cfs_cpt_set_node(cptab, cpt, i) :
987 cfs_cpt_set_cpu(cptab, cpt, i);
989 cfs_expr_list_free(el);
995 cfs_expr_list_free(el);
997 if (!cfs_cpt_online(cptab, cpt)) {
998 CERROR("No online CPU is found on partition %d\n", cpt);
1002 str = cfs_trimwhite(bracket + 1);
1008 cfs_cpt_table_free(cptab);
1012 #ifdef CONFIG_HOTPLUG_CPU
1014 cfs_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1016 unsigned int cpu = (unsigned long)hcpu;
1021 case CPU_DEAD_FROZEN:
1023 case CPU_ONLINE_FROZEN:
1024 spin_lock(&cpt_data.cpt_lock);
1025 cpt_data.cpt_version++;
1026 spin_unlock(&cpt_data.cpt_lock);
1028 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
1029 CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
1034 mutex_lock(&cpt_data.cpt_mutex);
1035 /* if all HTs in a core are offline, it may break affinity */
1036 cpumask_copy(cpt_data.cpt_cpumask,
1037 topology_sibling_cpumask(cpu));
1038 warn = cpumask_any_and(cpt_data.cpt_cpumask,
1039 cpu_online_mask) >= nr_cpu_ids;
1040 mutex_unlock(&cpt_data.cpt_mutex);
1041 CDEBUG(warn ? D_WARNING : D_INFO,
1042 "Lustre: can't support CPU plug-out well now, "
1043 "performance and stability could be impacted"
1044 "[CPU %u action: %lx]\n", cpu, action);
1050 static struct notifier_block cfs_cpu_notifier = {
1051 .notifier_call = cfs_cpu_notify,
1060 if (cfs_cpt_table != NULL)
1061 cfs_cpt_table_free(cfs_cpt_table);
1063 #ifdef CONFIG_HOTPLUG_CPU
1064 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1066 if (cpt_data.cpt_cpumask != NULL)
1067 LIBCFS_FREE(cpt_data.cpt_cpumask, cpumask_size());
1073 LASSERT(cfs_cpt_table == NULL);
1075 memset(&cpt_data, 0, sizeof(cpt_data));
1077 LIBCFS_ALLOC(cpt_data.cpt_cpumask, cpumask_size());
1078 if (cpt_data.cpt_cpumask == NULL) {
1079 CERROR("Failed to allocate scratch buffer\n");
1083 spin_lock_init(&cpt_data.cpt_lock);
1084 mutex_init(&cpt_data.cpt_mutex);
1086 #ifdef CONFIG_HOTPLUG_CPU
1087 register_hotcpu_notifier(&cfs_cpu_notifier);
1090 if (*cpu_pattern != 0) {
1091 char *cpu_pattern_dup = kstrdup(cpu_pattern, GFP_KERNEL);
1093 if (cpu_pattern_dup == NULL) {
1094 CERROR("Failed to duplicate cpu_pattern\n");
1098 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern_dup);
1099 kfree(cpu_pattern_dup);
1100 if (cfs_cpt_table == NULL) {
1101 CERROR("Failed to create cptab from pattern %s\n",
1107 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1108 if (cfs_cpt_table == NULL) {
1109 CERROR("Failed to create ptable with npartitions %d\n",
1115 spin_lock(&cpt_data.cpt_lock);
1116 if (cfs_cpt_table->ctb_version != cpt_data.cpt_version) {
1117 spin_unlock(&cpt_data.cpt_lock);
1118 CERROR("CPU hotplug/unplug during setup\n");
1121 spin_unlock(&cpt_data.cpt_lock);
1123 LCONSOLE(0, "HW CPU cores: %d, npartitions: %d\n",
1124 num_online_cpus(), cfs_cpt_number(cfs_cpt_table));