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, 2014, 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 CFS_MODULE_PARM(cpu_npartitions, "i", int, 0444, "# 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 * i.e: "N", shortcut expression to create CPT from NUMA & CPU topology
64 * NB: If user specified cpu_pattern, cpu_npartitions will be ignored
66 static char *cpu_pattern = "";
67 CFS_MODULE_PARM(cpu_pattern, "s", charp, 0444, "CPU partitions pattern");
70 /* serialize hotplug etc */
72 /* reserved for hotplug */
73 unsigned long cpt_version;
74 /* mutex to protect cpt_cpumask */
75 struct mutex cpt_mutex;
76 /* scratch buffer for set/unset_node */
77 cpumask_t *cpt_cpumask;
80 static struct cfs_cpt_data cpt_data;
83 cfs_cpu_core_siblings(int cpu, cpumask_t *mask)
85 /* return cpumask of cores in the same socket */
86 cpumask_copy(mask, topology_core_cpumask(cpu));
88 EXPORT_SYMBOL(cfs_cpu_core_siblings);
90 /* return number of cores in the same socket of \a cpu */
92 cfs_cpu_core_nsiblings(int cpu)
96 mutex_lock(&cpt_data.cpt_mutex);
98 cfs_cpu_core_siblings(cpu, cpt_data.cpt_cpumask);
99 num = cpus_weight(*cpt_data.cpt_cpumask);
101 mutex_unlock(&cpt_data.cpt_mutex);
105 EXPORT_SYMBOL(cfs_cpu_core_nsiblings);
107 /* return cpumask of HTs in the same core */
109 cfs_cpu_ht_siblings(int cpu, cpumask_t *mask)
111 cpumask_copy(mask, topology_thread_cpumask(cpu));
113 EXPORT_SYMBOL(cfs_cpu_ht_siblings);
115 /* return number of HTs in the same core of \a cpu */
117 cfs_cpu_ht_nsiblings(int cpu)
121 num = cpus_weight(*topology_thread_cpumask(cpu));
125 EXPORT_SYMBOL(cfs_cpu_ht_nsiblings);
128 cfs_node_to_cpumask(int node, cpumask_t *mask)
130 const cpumask_t *tmp = cpumask_of_node(node);
133 cpumask_copy(mask, tmp);
137 EXPORT_SYMBOL(cfs_node_to_cpumask);
140 cfs_cpt_table_free(struct cfs_cpt_table *cptab)
144 if (cptab->ctb_cpu2cpt != NULL) {
145 LIBCFS_FREE(cptab->ctb_cpu2cpt,
146 num_possible_cpus() *
147 sizeof(cptab->ctb_cpu2cpt[0]));
150 for (i = 0; cptab->ctb_parts != NULL && i < cptab->ctb_nparts; i++) {
151 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
153 if (part->cpt_nodemask != NULL) {
154 LIBCFS_FREE(part->cpt_nodemask,
155 sizeof(*part->cpt_nodemask));
158 if (part->cpt_cpumask != NULL)
159 LIBCFS_FREE(part->cpt_cpumask, cpumask_size());
162 if (cptab->ctb_parts != NULL) {
163 LIBCFS_FREE(cptab->ctb_parts,
164 cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
167 if (cptab->ctb_nodemask != NULL)
168 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
169 if (cptab->ctb_cpumask != NULL)
170 LIBCFS_FREE(cptab->ctb_cpumask, cpumask_size());
172 LIBCFS_FREE(cptab, sizeof(*cptab));
174 EXPORT_SYMBOL(cfs_cpt_table_free);
176 struct cfs_cpt_table *
177 cfs_cpt_table_alloc(unsigned int ncpt)
179 struct cfs_cpt_table *cptab;
182 LIBCFS_ALLOC(cptab, sizeof(*cptab));
186 cptab->ctb_nparts = ncpt;
188 LIBCFS_ALLOC(cptab->ctb_cpumask, cpumask_size());
189 LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
191 if (cptab->ctb_cpumask == NULL || cptab->ctb_nodemask == NULL)
194 LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
195 num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
196 if (cptab->ctb_cpu2cpt == NULL)
199 memset(cptab->ctb_cpu2cpt, -1,
200 num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
202 LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
203 if (cptab->ctb_parts == NULL)
206 for (i = 0; i < ncpt; i++) {
207 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
209 LIBCFS_ALLOC(part->cpt_cpumask, cpumask_size());
210 LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
211 if (part->cpt_cpumask == NULL || part->cpt_nodemask == NULL)
215 spin_lock(&cpt_data.cpt_lock);
216 /* Reserved for hotplug */
217 cptab->ctb_version = cpt_data.cpt_version;
218 spin_unlock(&cpt_data.cpt_lock);
223 cfs_cpt_table_free(cptab);
226 EXPORT_SYMBOL(cfs_cpt_table_alloc);
229 cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
236 for (i = 0; i < cptab->ctb_nparts; i++) {
238 rc = snprintf(tmp, len, "%d\t: ", i);
248 for_each_cpu_mask(j, *cptab->ctb_parts[i].cpt_cpumask) {
249 rc = snprintf(tmp, len, "%d ", j);
269 EXPORT_SYMBOL(cfs_cpt_table_print);
272 cfs_cpt_number(struct cfs_cpt_table *cptab)
274 return cptab->ctb_nparts;
276 EXPORT_SYMBOL(cfs_cpt_number);
279 cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
281 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
283 return cpt == CFS_CPT_ANY ?
284 cpus_weight(*cptab->ctb_cpumask) :
285 cpus_weight(*cptab->ctb_parts[cpt].cpt_cpumask);
287 EXPORT_SYMBOL(cfs_cpt_weight);
290 cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
292 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
294 return cpt == CFS_CPT_ANY ?
295 any_online_cpu(*cptab->ctb_cpumask) != NR_CPUS :
296 any_online_cpu(*cptab->ctb_parts[cpt].cpt_cpumask) != NR_CPUS;
298 EXPORT_SYMBOL(cfs_cpt_online);
301 cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
303 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
305 return cpt == CFS_CPT_ANY ?
306 cptab->ctb_cpumask : cptab->ctb_parts[cpt].cpt_cpumask;
308 EXPORT_SYMBOL(cfs_cpt_cpumask);
311 cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
313 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
315 return cpt == CFS_CPT_ANY ?
316 cptab->ctb_nodemask : cptab->ctb_parts[cpt].cpt_nodemask;
318 EXPORT_SYMBOL(cfs_cpt_nodemask);
321 cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
325 LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
327 if (cpu < 0 || cpu >= NR_CPUS || !cpu_online(cpu)) {
328 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
332 if (cptab->ctb_cpu2cpt[cpu] != -1) {
333 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
334 cpu, cptab->ctb_cpu2cpt[cpu]);
338 cptab->ctb_cpu2cpt[cpu] = cpt;
340 LASSERT(!cpu_isset(cpu, *cptab->ctb_cpumask));
341 LASSERT(!cpu_isset(cpu, *cptab->ctb_parts[cpt].cpt_cpumask));
343 cpu_set(cpu, *cptab->ctb_cpumask);
344 cpu_set(cpu, *cptab->ctb_parts[cpt].cpt_cpumask);
346 node = cpu_to_node(cpu);
348 /* first CPU of @node in this CPT table */
349 if (!node_isset(node, *cptab->ctb_nodemask))
350 node_set(node, *cptab->ctb_nodemask);
352 /* first CPU of @node in this partition */
353 if (!node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask))
354 node_set(node, *cptab->ctb_parts[cpt].cpt_nodemask);
358 EXPORT_SYMBOL(cfs_cpt_set_cpu);
361 cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
366 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
368 if (cpu < 0 || cpu >= NR_CPUS) {
369 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
373 if (cpt == CFS_CPT_ANY) {
374 /* caller doesn't know the partition ID */
375 cpt = cptab->ctb_cpu2cpt[cpu];
376 if (cpt < 0) { /* not set in this CPT-table */
377 CDEBUG(D_INFO, "Try to unset cpu %d which is "
378 "not in CPT-table %p\n", cpt, cptab);
382 } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
384 "CPU %d is not in cpu-partition %d\n", cpu, cpt);
388 LASSERT(cpu_isset(cpu, *cptab->ctb_parts[cpt].cpt_cpumask));
389 LASSERT(cpu_isset(cpu, *cptab->ctb_cpumask));
391 cpu_clear(cpu, *cptab->ctb_parts[cpt].cpt_cpumask);
392 cpu_clear(cpu, *cptab->ctb_cpumask);
393 cptab->ctb_cpu2cpt[cpu] = -1;
395 node = cpu_to_node(cpu);
397 LASSERT(node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask));
398 LASSERT(node_isset(node, *cptab->ctb_nodemask));
400 for_each_cpu_mask(i, *cptab->ctb_parts[cpt].cpt_cpumask) {
401 /* this CPT has other CPU belonging to this node? */
402 if (cpu_to_node(i) == node)
407 node_clear(node, *cptab->ctb_parts[cpt].cpt_nodemask);
409 for_each_cpu_mask(i, *cptab->ctb_cpumask) {
410 /* this CPT-table has other CPU belonging to this node? */
411 if (cpu_to_node(i) == node)
416 node_clear(node, *cptab->ctb_nodemask);
420 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
423 cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
427 if (cpus_weight(*mask) == 0 || any_online_cpu(*mask) == NR_CPUS) {
428 CDEBUG(D_INFO, "No online CPU is found in the CPU mask "
429 "for CPU partition %d\n", cpt);
433 for_each_cpu_mask(i, *mask) {
434 if (!cfs_cpt_set_cpu(cptab, cpt, i))
440 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
443 cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
447 for_each_cpu_mask(i, *mask)
448 cfs_cpt_unset_cpu(cptab, cpt, i);
450 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
453 cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
458 if (node < 0 || node >= MAX_NUMNODES) {
460 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
464 mutex_lock(&cpt_data.cpt_mutex);
466 mask = cpt_data.cpt_cpumask;
467 cfs_node_to_cpumask(node, mask);
469 rc = cfs_cpt_set_cpumask(cptab, cpt, mask);
471 mutex_unlock(&cpt_data.cpt_mutex);
475 EXPORT_SYMBOL(cfs_cpt_set_node);
478 cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
482 if (node < 0 || node >= MAX_NUMNODES) {
484 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
488 mutex_lock(&cpt_data.cpt_mutex);
490 mask = cpt_data.cpt_cpumask;
491 cfs_node_to_cpumask(node, mask);
493 cfs_cpt_unset_cpumask(cptab, cpt, mask);
495 mutex_unlock(&cpt_data.cpt_mutex);
497 EXPORT_SYMBOL(cfs_cpt_unset_node);
500 cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
504 for_each_node_mask(i, *mask) {
505 if (!cfs_cpt_set_node(cptab, cpt, i))
511 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
514 cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
518 for_each_node_mask(i, *mask)
519 cfs_cpt_unset_node(cptab, cpt, i);
521 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
524 cfs_cpt_clear(struct cfs_cpt_table *cptab, int cpt)
529 if (cpt == CFS_CPT_ANY) {
530 last = cptab->ctb_nparts - 1;
536 for (; cpt <= last; cpt++) {
537 for_each_cpu_mask(i, *cptab->ctb_parts[cpt].cpt_cpumask)
538 cfs_cpt_unset_cpu(cptab, cpt, i);
541 EXPORT_SYMBOL(cfs_cpt_clear);
544 cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
551 /* convert CPU partition ID to HW node id */
553 if (cpt < 0 || cpt >= cptab->ctb_nparts) {
554 mask = cptab->ctb_nodemask;
555 rotor = cptab->ctb_spread_rotor++;
557 mask = cptab->ctb_parts[cpt].cpt_nodemask;
558 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
561 weight = nodes_weight(*mask);
566 for_each_node_mask(node, *mask) {
574 EXPORT_SYMBOL(cfs_cpt_spread_node);
577 cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
579 int cpu = smp_processor_id();
580 int cpt = cptab->ctb_cpu2cpt[cpu];
586 /* don't return negative value for safety of upper layer,
587 * instead we shadow the unknown cpu to a valid partition ID */
588 cpt = cpu % cptab->ctb_nparts;
593 EXPORT_SYMBOL(cfs_cpt_current);
596 cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
598 LASSERT(cpu >= 0 && cpu < NR_CPUS);
600 return cptab->ctb_cpu2cpt[cpu];
602 EXPORT_SYMBOL(cfs_cpt_of_cpu);
605 cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
608 nodemask_t *nodemask;
612 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
614 if (cpt == CFS_CPT_ANY) {
615 cpumask = cptab->ctb_cpumask;
616 nodemask = cptab->ctb_nodemask;
618 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
619 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
622 if (any_online_cpu(*cpumask) == NR_CPUS) {
623 CERROR("No online CPU found in CPU partition %d, did someone "
624 "do CPU hotplug on system? You might need to reload "
625 "Lustre modules to keep system working well.\n", cpt);
629 for_each_online_cpu(i) {
630 if (cpu_isset(i, *cpumask))
633 rc = set_cpus_allowed_ptr(current, cpumask);
634 set_mems_allowed(*nodemask);
636 schedule(); /* switch to allowed CPU */
641 /* don't need to set affinity because all online CPUs are covered */
644 EXPORT_SYMBOL(cfs_cpt_bind);
647 * Choose max to \a number CPUs from \a node and set them in \a cpt.
648 * We always prefer to choose CPU in the same core/socket.
651 cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
652 cpumask_t *node, int number)
654 cpumask_t *socket = NULL;
655 cpumask_t *core = NULL;
661 if (number >= cpus_weight(*node)) {
662 while (!cpus_empty(*node)) {
663 cpu = first_cpu(*node);
665 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
668 cpu_clear(cpu, *node);
673 /* allocate scratch buffer */
674 LIBCFS_ALLOC(socket, cpumask_size());
675 LIBCFS_ALLOC(core, cpumask_size());
676 if (socket == NULL || core == NULL) {
681 while (!cpus_empty(*node)) {
682 cpu = first_cpu(*node);
684 /* get cpumask for cores in the same socket */
685 cfs_cpu_core_siblings(cpu, socket);
686 cpus_and(*socket, *socket, *node);
688 LASSERT(!cpus_empty(*socket));
690 while (!cpus_empty(*socket)) {
693 /* get cpumask for hts in the same core */
694 cfs_cpu_ht_siblings(cpu, core);
695 cpus_and(*core, *core, *node);
697 LASSERT(!cpus_empty(*core));
699 for_each_cpu_mask(i, *core) {
700 cpu_clear(i, *socket);
703 rc = cfs_cpt_set_cpu(cptab, cpt, i);
712 cpu = first_cpu(*socket);
718 LIBCFS_FREE(socket, cpumask_size());
720 LIBCFS_FREE(core, cpumask_size());
724 #define CPT_WEIGHT_MIN 4u
727 cfs_cpt_num_estimate(void)
729 unsigned nnode = num_online_nodes();
730 unsigned ncpu = num_online_cpus();
733 if (ncpu <= CPT_WEIGHT_MIN) {
738 /* generate reasonable number of CPU partitions based on total number
739 * of CPUs, Preferred N should be power2 and match this condition:
740 * 2 * (N - 1)^2 < NCPUS <= 2 * N^2 */
741 for (ncpt = 2; ncpu > 2 * ncpt * ncpt; ncpt <<= 1) {}
743 if (ncpt <= nnode) { /* fat numa system */
747 } else { /* ncpt > nnode */
748 while ((nnode << 1) <= ncpt)
755 #if (BITS_PER_LONG == 32)
756 /* config many CPU partitions on 32-bit system could consume
758 ncpt = min(2U, ncpt);
760 while (ncpu % ncpt != 0)
761 ncpt--; /* worst case is 1 */
766 static struct cfs_cpt_table *
767 cfs_cpt_table_create(int ncpt)
769 struct cfs_cpt_table *cptab = NULL;
770 cpumask_t *mask = NULL;
776 rc = cfs_cpt_num_estimate();
780 if (ncpt > num_online_cpus() || ncpt > 4 * rc) {
781 CWARN("CPU partition number %d is larger than suggested "
782 "value (%d), your system may have performance"
783 "issue or run out of memory while under pressure\n",
787 if (num_online_cpus() % ncpt != 0) {
788 CERROR("CPU number %d is not multiple of cpu_npartition %d, "
789 "please try different cpu_npartitions value or"
790 "set pattern string by cpu_pattern=STRING\n",
791 (int)num_online_cpus(), ncpt);
795 cptab = cfs_cpt_table_alloc(ncpt);
797 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
801 num = num_online_cpus() / ncpt;
803 CERROR("CPU changed while setting CPU partition\n");
807 LIBCFS_ALLOC(mask, cpumask_size());
809 CERROR("Failed to allocate scratch cpumask\n");
813 for_each_online_node(i) {
814 cfs_node_to_cpumask(i, mask);
816 while (!cpus_empty(*mask)) {
817 struct cfs_cpu_partition *part;
820 /* Each emulated NUMA node has all allowed CPUs in
822 * End loop when all partitions have assigned CPUs.
827 part = &cptab->ctb_parts[cpt];
829 n = num - cpus_weight(*part->cpt_cpumask);
832 rc = cfs_cpt_choose_ncpus(cptab, cpt, mask, n);
836 LASSERT(num >= cpus_weight(*part->cpt_cpumask));
837 if (num == cpus_weight(*part->cpt_cpumask))
843 num != cpus_weight(*cptab->ctb_parts[ncpt - 1].cpt_cpumask)) {
844 CERROR("Expect %d(%d) CPU partitions but got %d(%d), "
845 "CPU hotplug/unplug while setting?\n",
846 cptab->ctb_nparts, num, cpt,
847 cpus_weight(*cptab->ctb_parts[ncpt - 1].cpt_cpumask));
851 LIBCFS_FREE(mask, cpumask_size());
856 CERROR("Failed to setup CPU-partition-table with %d "
857 "CPU-partitions, online HW nodes: %d, HW cpus: %d.\n",
858 ncpt, num_online_nodes(), num_online_cpus());
861 LIBCFS_FREE(mask, cpumask_size());
864 cfs_cpt_table_free(cptab);
869 static struct cfs_cpt_table *
870 cfs_cpt_table_create_pattern(char *pattern)
872 struct cfs_cpt_table *cptab;
882 str = cfs_trimwhite(pattern);
883 if (*str == 'n' || *str == 'N') {
885 if (*pattern != '\0') {
886 node = 1; /* numa pattern */
888 } else { /* shortcut to create CPT from NUMA & CPU topology */
890 ncpt = num_online_nodes();
894 if (ncpt == 0) { /* scanning bracket which is mark of partition */
895 for (str = pattern;; str++, ncpt++) {
896 str = strchr(str, '[');
903 (node && ncpt > num_online_nodes()) ||
904 (!node && ncpt > num_online_cpus())) {
905 CERROR("Invalid pattern %s, or too many partitions %d\n",
910 cptab = cfs_cpt_table_alloc(ncpt);
912 CERROR("Failed to allocate cpu partition table\n");
916 if (node < 0) { /* shortcut to create CPT from NUMA & CPU topology */
918 for_each_online_node(i) {
920 CERROR("CPU changed while setting CPU "
921 "partition table, %d/%d\n", cpt, ncpt);
925 rc = cfs_cpt_set_node(cptab, cpt++, i);
932 high = node ? MAX_NUMNODES - 1 : nr_cpu_ids - 1;
934 for (str = cfs_trimwhite(pattern), c = 0;; c++) {
935 struct cfs_range_expr *range;
936 struct cfs_expr_list *el;
937 char *bracket = strchr(str, '[');
940 if (bracket == NULL) {
942 CERROR("Invalid pattern %s\n", str);
944 } else if (c != ncpt) {
945 CERROR("expect %d partitions but found %d\n",
952 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
953 CERROR("Invalid cpu pattern %s\n", str);
957 if (cpt < 0 || cpt >= ncpt) {
958 CERROR("Invalid partition id %d, total partitions %d\n",
963 if (cfs_cpt_weight(cptab, cpt) != 0) {
964 CERROR("Partition %d has already been set.\n", cpt);
968 str = cfs_trimwhite(str + n);
969 if (str != bracket) {
970 CERROR("Invalid pattern %s\n", str);
974 bracket = strchr(str, ']');
975 if (bracket == NULL) {
976 CERROR("missing right bracket for cpt %d, %s\n",
981 if (cfs_expr_list_parse(str, (bracket - str) + 1,
982 0, high, &el) != 0) {
983 CERROR("Can't parse number range: %s\n", str);
987 list_for_each_entry(range, &el->el_exprs, re_link) {
988 for (i = range->re_lo; i <= range->re_hi; i++) {
989 if ((i - range->re_lo) % range->re_stride != 0)
992 rc = node ? cfs_cpt_set_node(cptab, cpt, i) :
993 cfs_cpt_set_cpu(cptab, cpt, i);
995 cfs_expr_list_free(el);
1001 cfs_expr_list_free(el);
1003 if (!cfs_cpt_online(cptab, cpt)) {
1004 CERROR("No online CPU is found on partition %d\n", cpt);
1008 str = cfs_trimwhite(bracket + 1);
1014 cfs_cpt_table_free(cptab);
1018 #ifdef CONFIG_HOTPLUG_CPU
1020 cfs_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1022 unsigned int cpu = (unsigned long)hcpu;
1027 case CPU_DEAD_FROZEN:
1029 case CPU_ONLINE_FROZEN:
1030 spin_lock(&cpt_data.cpt_lock);
1031 cpt_data.cpt_version++;
1032 spin_unlock(&cpt_data.cpt_lock);
1034 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
1035 CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
1040 mutex_lock(&cpt_data.cpt_mutex);
1041 /* if all HTs in a core are offline, it may break affinity */
1042 cfs_cpu_ht_siblings(cpu, cpt_data.cpt_cpumask);
1043 warn = any_online_cpu(*cpt_data.cpt_cpumask) >= nr_cpu_ids;
1044 mutex_unlock(&cpt_data.cpt_mutex);
1045 CDEBUG(warn ? D_WARNING : D_INFO,
1046 "Lustre: can't support CPU plug-out well now, "
1047 "performance and stability could be impacted"
1048 "[CPU %u action: %lx]\n", cpu, action);
1054 static struct notifier_block cfs_cpu_notifier = {
1055 .notifier_call = cfs_cpu_notify,
1064 if (cfs_cpt_table != NULL)
1065 cfs_cpt_table_free(cfs_cpt_table);
1067 #ifdef CONFIG_HOTPLUG_CPU
1068 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1070 if (cpt_data.cpt_cpumask != NULL)
1071 LIBCFS_FREE(cpt_data.cpt_cpumask, cpumask_size());
1077 LASSERT(cfs_cpt_table == NULL);
1079 memset(&cpt_data, 0, sizeof(cpt_data));
1081 LIBCFS_ALLOC(cpt_data.cpt_cpumask, cpumask_size());
1082 if (cpt_data.cpt_cpumask == NULL) {
1083 CERROR("Failed to allocate scratch buffer\n");
1087 spin_lock_init(&cpt_data.cpt_lock);
1088 mutex_init(&cpt_data.cpt_mutex);
1090 #ifdef CONFIG_HOTPLUG_CPU
1091 register_hotcpu_notifier(&cfs_cpu_notifier);
1094 if (*cpu_pattern != 0) {
1095 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern);
1096 if (cfs_cpt_table == NULL) {
1097 CERROR("Failed to create cptab from pattern %s\n",
1103 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1104 if (cfs_cpt_table == NULL) {
1105 CERROR("Failed to create ptable with npartitions %d\n",
1111 spin_lock(&cpt_data.cpt_lock);
1112 if (cfs_cpt_table->ctb_version != cpt_data.cpt_version) {
1113 spin_unlock(&cpt_data.cpt_lock);
1114 CERROR("CPU hotplug/unplug during setup\n");
1117 spin_unlock(&cpt_data.cpt_lock);
1119 LCONSOLE(0, "HW CPU cores: %d, npartitions: %d\n",
1120 num_online_cpus(), cfs_cpt_number(cfs_cpt_table));