Whamcloud - gitweb
cfc53f1ecbcd099ddfb20dc2d1d002f5d506ee16
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-cpu.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25  *
26  * Copyright (c) 2012, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * Author: liang@whamcloud.com
33  */
34
35 #define DEBUG_SUBSYSTEM S_LNET
36
37 #include <linux/cpu.h>
38 #include <linux/sched.h>
39 #include <libcfs/libcfs.h>
40
41 #ifdef CONFIG_SMP
42
43 /**
44  * modparam for setting number of partitions
45  *
46  *  0 : estimate best value based on cores or NUMA nodes
47  *  1 : disable multiple partitions
48  * >1 : specify number of partitions
49  */
50 static int      cpu_npartitions;
51 CFS_MODULE_PARM(cpu_npartitions, "i", int, 0444, "# of CPU partitions");
52
53 /**
54  * modparam for setting CPU partitions patterns:
55  *
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)
58  *
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.
61  *
62  * i.e: "N", shortcut expression to create CPT from NUMA & CPU topology
63  *
64  * NB: If user specified cpu_pattern, cpu_npartitions will be ignored
65  */
66 static char     *cpu_pattern = "";
67 CFS_MODULE_PARM(cpu_pattern, "s", charp, 0444, "CPU partitions pattern");
68
69 struct cfs_cpt_data {
70         /* serialize hotplug etc */
71         spinlock_t              cpt_lock;
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;
78 };
79
80 static struct cfs_cpt_data      cpt_data;
81
82 void
83 cfs_cpu_core_siblings(int cpu, cpumask_t *mask)
84 {
85         /* return cpumask of cores in the same socket */
86         cpumask_copy(mask, topology_core_cpumask(cpu));
87 }
88 EXPORT_SYMBOL(cfs_cpu_core_siblings);
89
90 /* return number of cores in the same socket of \a cpu */
91 int
92 cfs_cpu_core_nsiblings(int cpu)
93 {
94         int     num;
95
96         mutex_lock(&cpt_data.cpt_mutex);
97
98         cfs_cpu_core_siblings(cpu, cpt_data.cpt_cpumask);
99         num = cpumask_weight(cpt_data.cpt_cpumask);
100
101         mutex_unlock(&cpt_data.cpt_mutex);
102
103         return num;
104 }
105 EXPORT_SYMBOL(cfs_cpu_core_nsiblings);
106
107 /* return cpumask of HTs in the same core */
108 void
109 cfs_cpu_ht_siblings(int cpu, cpumask_t *mask)
110 {
111         cpumask_copy(mask, topology_thread_cpumask(cpu));
112 }
113 EXPORT_SYMBOL(cfs_cpu_ht_siblings);
114
115 /* return number of HTs in the same core of \a cpu */
116 int
117 cfs_cpu_ht_nsiblings(int cpu)
118 {
119         int     num;
120
121         num = cpumask_weight(topology_thread_cpumask(cpu));
122
123         return num;
124 }
125 EXPORT_SYMBOL(cfs_cpu_ht_nsiblings);
126
127 void
128 cfs_node_to_cpumask(int node, cpumask_t *mask)
129 {
130         const cpumask_t *tmp = cpumask_of_node(node);
131
132         if (tmp != NULL)
133                 cpumask_copy(mask, tmp);
134         else
135                 cpumask_clear(mask);
136 }
137 EXPORT_SYMBOL(cfs_node_to_cpumask);
138
139 void
140 cfs_cpt_table_free(struct cfs_cpt_table *cptab)
141 {
142         int     i;
143
144         if (cptab->ctb_cpu2cpt != NULL) {
145                 LIBCFS_FREE(cptab->ctb_cpu2cpt,
146                             num_possible_cpus() *
147                             sizeof(cptab->ctb_cpu2cpt[0]));
148         }
149
150         for (i = 0; cptab->ctb_parts != NULL && i < cptab->ctb_nparts; i++) {
151                 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
152
153                 if (part->cpt_nodemask != NULL) {
154                         LIBCFS_FREE(part->cpt_nodemask,
155                                     sizeof(*part->cpt_nodemask));
156                 }
157
158                 if (part->cpt_cpumask != NULL)
159                         LIBCFS_FREE(part->cpt_cpumask, cpumask_size());
160         }
161
162         if (cptab->ctb_parts != NULL) {
163                 LIBCFS_FREE(cptab->ctb_parts,
164                             cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
165         }
166
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());
171
172         LIBCFS_FREE(cptab, sizeof(*cptab));
173 }
174 EXPORT_SYMBOL(cfs_cpt_table_free);
175
176 struct cfs_cpt_table *
177 cfs_cpt_table_alloc(unsigned int ncpt)
178 {
179         struct cfs_cpt_table *cptab;
180         int     i;
181
182         LIBCFS_ALLOC(cptab, sizeof(*cptab));
183         if (cptab == NULL)
184                 return NULL;
185
186         cptab->ctb_nparts = ncpt;
187
188         LIBCFS_ALLOC(cptab->ctb_cpumask, cpumask_size());
189         LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
190
191         if (cptab->ctb_cpumask == NULL || cptab->ctb_nodemask == NULL)
192                 goto failed;
193
194         LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
195                      num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
196         if (cptab->ctb_cpu2cpt == NULL)
197                 goto failed;
198
199         memset(cptab->ctb_cpu2cpt, -1,
200                num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
201
202         LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
203         if (cptab->ctb_parts == NULL)
204                 goto failed;
205
206         for (i = 0; i < ncpt; i++) {
207                 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
208
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)
212                         goto failed;
213         }
214
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);
219
220         return cptab;
221
222  failed:
223         cfs_cpt_table_free(cptab);
224         return NULL;
225 }
226 EXPORT_SYMBOL(cfs_cpt_table_alloc);
227
228 int
229 cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
230 {
231         char    *tmp = buf;
232         int     rc = 0;
233         int     i;
234         int     j;
235
236         for (i = 0; i < cptab->ctb_nparts; i++) {
237                 if (len > 0) {
238                         rc = snprintf(tmp, len, "%d\t: ", i);
239                         len -= rc;
240                 }
241
242                 if (len <= 0) {
243                         rc = -EFBIG;
244                         goto out;
245                 }
246
247                 tmp += rc;
248                 for_each_cpu(j, cptab->ctb_parts[i].cpt_cpumask) {
249                         rc = snprintf(tmp, len, "%d ", j);
250                         len -= rc;
251                         if (len <= 0) {
252                                 rc = -EFBIG;
253                                 goto out;
254                         }
255                         tmp += rc;
256                 }
257
258                 *tmp = '\n';
259                 tmp++;
260                 len--;
261         }
262
263  out:
264         if (rc < 0)
265                 return rc;
266
267         return tmp - buf;
268 }
269 EXPORT_SYMBOL(cfs_cpt_table_print);
270
271 int
272 cfs_cpt_number(struct cfs_cpt_table *cptab)
273 {
274         return cptab->ctb_nparts;
275 }
276 EXPORT_SYMBOL(cfs_cpt_number);
277
278 int
279 cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
280 {
281         LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
282
283         return cpt == CFS_CPT_ANY ?
284                cpumask_weight(cptab->ctb_cpumask) :
285                cpumask_weight(cptab->ctb_parts[cpt].cpt_cpumask);
286 }
287 EXPORT_SYMBOL(cfs_cpt_weight);
288
289 int
290 cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
291 {
292         LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
293
294         return cpt == CFS_CPT_ANY ?
295                cpumask_any_and(cptab->ctb_cpumask,
296                                cpu_online_mask) < nr_cpu_ids :
297                cpumask_any_and(cptab->ctb_parts[cpt].cpt_cpumask,
298                                cpu_online_mask) < nr_cpu_ids;
299 }
300 EXPORT_SYMBOL(cfs_cpt_online);
301
302 cpumask_t *
303 cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
304 {
305         LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
306
307         return cpt == CFS_CPT_ANY ?
308                cptab->ctb_cpumask : cptab->ctb_parts[cpt].cpt_cpumask;
309 }
310 EXPORT_SYMBOL(cfs_cpt_cpumask);
311
312 nodemask_t *
313 cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
314 {
315         LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
316
317         return cpt == CFS_CPT_ANY ?
318                cptab->ctb_nodemask : cptab->ctb_parts[cpt].cpt_nodemask;
319 }
320 EXPORT_SYMBOL(cfs_cpt_nodemask);
321
322 int
323 cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
324 {
325         int     node;
326
327         LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
328
329         if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu)) {
330                 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
331                 return 0;
332         }
333
334         if (cptab->ctb_cpu2cpt[cpu] != -1) {
335                 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
336                        cpu, cptab->ctb_cpu2cpt[cpu]);
337                 return 0;
338         }
339
340         cptab->ctb_cpu2cpt[cpu] = cpt;
341
342         LASSERT(!cpumask_test_cpu(cpu, cptab->ctb_cpumask));
343         LASSERT(!cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
344
345         cpumask_set_cpu(cpu, cptab->ctb_cpumask);
346         cpumask_set_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
347
348         node = cpu_to_node(cpu);
349
350         /* first CPU of @node in this CPT table */
351         if (!node_isset(node, *cptab->ctb_nodemask))
352                 node_set(node, *cptab->ctb_nodemask);
353
354         /* first CPU of @node in this partition */
355         if (!node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask))
356                 node_set(node, *cptab->ctb_parts[cpt].cpt_nodemask);
357
358         return 1;
359 }
360 EXPORT_SYMBOL(cfs_cpt_set_cpu);
361
362 void
363 cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
364 {
365         int     node;
366         int     i;
367
368         LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
369
370         if (cpu < 0 || cpu >= nr_cpu_ids) {
371                 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
372                 return;
373         }
374
375         if (cpt == CFS_CPT_ANY) {
376                 /* caller doesn't know the partition ID */
377                 cpt = cptab->ctb_cpu2cpt[cpu];
378                 if (cpt < 0) { /* not set in this CPT-table */
379                         CDEBUG(D_INFO, "Try to unset cpu %d which is "
380                                        "not in CPT-table %p\n", cpt, cptab);
381                         return;
382                 }
383
384         } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
385                 CDEBUG(D_INFO,
386                        "CPU %d is not in cpu-partition %d\n", cpu, cpt);
387                 return;
388         }
389
390         LASSERT(cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
391         LASSERT(cpumask_test_cpu(cpu, cptab->ctb_cpumask));
392
393         cpumask_clear_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
394         cpumask_clear_cpu(cpu, cptab->ctb_cpumask);
395         cptab->ctb_cpu2cpt[cpu] = -1;
396
397         node = cpu_to_node(cpu);
398
399         LASSERT(node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask));
400         LASSERT(node_isset(node, *cptab->ctb_nodemask));
401
402         for_each_cpu(i, cptab->ctb_parts[cpt].cpt_cpumask) {
403                 /* this CPT has other CPU belonging to this node? */
404                 if (cpu_to_node(i) == node)
405                         break;
406         }
407
408         if (i >= nr_cpu_ids)
409                 node_clear(node, *cptab->ctb_parts[cpt].cpt_nodemask);
410
411         for_each_cpu(i, cptab->ctb_cpumask) {
412                 /* this CPT-table has other CPU belonging to this node? */
413                 if (cpu_to_node(i) == node)
414                         break;
415         }
416
417         if (i >= nr_cpu_ids)
418                 node_clear(node, *cptab->ctb_nodemask);
419
420         return;
421 }
422 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
423
424 int
425 cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
426 {
427         int     i;
428
429         if (cpumask_weight(mask) == 0 ||
430             cpumask_any_and(mask, cpu_online_mask) >= nr_cpu_ids) {
431                 CDEBUG(D_INFO, "No online CPU is found in the CPU mask "
432                                "for CPU partition %d\n", cpt);
433                 return 0;
434         }
435
436         for_each_cpu(i, mask) {
437                 if (!cfs_cpt_set_cpu(cptab, cpt, i))
438                         return 0;
439         }
440
441         return 1;
442 }
443 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
444
445 void
446 cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
447 {
448         int     i;
449
450         for_each_cpu(i, mask)
451                 cfs_cpt_unset_cpu(cptab, cpt, i);
452 }
453 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
454
455 int
456 cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
457 {
458         cpumask_t       *mask;
459         int             rc;
460
461         if (node < 0 || node >= MAX_NUMNODES) {
462                 CDEBUG(D_INFO,
463                        "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
464                 return 0;
465         }
466
467         mutex_lock(&cpt_data.cpt_mutex);
468
469         mask = cpt_data.cpt_cpumask;
470         cfs_node_to_cpumask(node, mask);
471
472         rc = cfs_cpt_set_cpumask(cptab, cpt, mask);
473
474         mutex_unlock(&cpt_data.cpt_mutex);
475
476         return rc;
477 }
478 EXPORT_SYMBOL(cfs_cpt_set_node);
479
480 void
481 cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
482 {
483         cpumask_t *mask;
484
485         if (node < 0 || node >= MAX_NUMNODES) {
486                 CDEBUG(D_INFO,
487                        "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
488                 return;
489         }
490
491         mutex_lock(&cpt_data.cpt_mutex);
492
493         mask = cpt_data.cpt_cpumask;
494         cfs_node_to_cpumask(node, mask);
495
496         cfs_cpt_unset_cpumask(cptab, cpt, mask);
497
498         mutex_unlock(&cpt_data.cpt_mutex);
499 }
500 EXPORT_SYMBOL(cfs_cpt_unset_node);
501
502 int
503 cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
504 {
505         int     i;
506
507         for_each_node_mask(i, *mask) {
508                 if (!cfs_cpt_set_node(cptab, cpt, i))
509                         return 0;
510         }
511
512         return 1;
513 }
514 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
515
516 void
517 cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
518 {
519         int     i;
520
521         for_each_node_mask(i, *mask)
522                 cfs_cpt_unset_node(cptab, cpt, i);
523 }
524 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
525
526 void
527 cfs_cpt_clear(struct cfs_cpt_table *cptab, int cpt)
528 {
529         int     last;
530         int     i;
531
532         if (cpt == CFS_CPT_ANY) {
533                 last = cptab->ctb_nparts - 1;
534                 cpt = 0;
535         } else {
536                 last = cpt;
537         }
538
539         for (; cpt <= last; cpt++) {
540                 for_each_cpu(i, cptab->ctb_parts[cpt].cpt_cpumask)
541                         cfs_cpt_unset_cpu(cptab, cpt, i);
542         }
543 }
544 EXPORT_SYMBOL(cfs_cpt_clear);
545
546 int
547 cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
548 {
549         nodemask_t      *mask;
550         int             weight;
551         int             rotor;
552         int             node;
553
554         /* convert CPU partition ID to HW node id */
555
556         if (cpt < 0 || cpt >= cptab->ctb_nparts) {
557                 mask = cptab->ctb_nodemask;
558                 rotor = cptab->ctb_spread_rotor++;
559         } else {
560                 mask = cptab->ctb_parts[cpt].cpt_nodemask;
561                 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
562         }
563
564         weight = nodes_weight(*mask);
565         LASSERT(weight > 0);
566
567         rotor %= weight;
568
569         for_each_node_mask(node, *mask) {
570                 if (rotor-- == 0)
571                         return node;
572         }
573
574         LBUG();
575         return 0;
576 }
577 EXPORT_SYMBOL(cfs_cpt_spread_node);
578
579 int
580 cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
581 {
582         int     cpu = smp_processor_id();
583         int     cpt = cptab->ctb_cpu2cpt[cpu];
584
585         if (cpt < 0) {
586                 if (!remap)
587                         return cpt;
588
589                 /* don't return negative value for safety of upper layer,
590                  * instead we shadow the unknown cpu to a valid partition ID */
591                 cpt = cpu % cptab->ctb_nparts;
592         }
593
594         return cpt;
595 }
596 EXPORT_SYMBOL(cfs_cpt_current);
597
598 int
599 cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
600 {
601         LASSERT(cpu >= 0 && cpu < nr_cpu_ids);
602
603         return cptab->ctb_cpu2cpt[cpu];
604 }
605 EXPORT_SYMBOL(cfs_cpt_of_cpu);
606
607 int
608 cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
609 {
610         cpumask_t       *cpumask;
611         nodemask_t      *nodemask;
612         int             rc;
613         int             i;
614
615         LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
616
617         if (cpt == CFS_CPT_ANY) {
618                 cpumask = cptab->ctb_cpumask;
619                 nodemask = cptab->ctb_nodemask;
620         } else {
621                 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
622                 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
623         }
624
625         if (cpumask_any_and(cpumask, cpu_online_mask) >= nr_cpu_ids) {
626                 CERROR("No online CPU found in CPU partition %d, did someone "
627                        "do CPU hotplug on system? You might need to reload "
628                        "Lustre modules to keep system working well.\n", cpt);
629                 return -EINVAL;
630         }
631
632         for_each_online_cpu(i) {
633                 if (cpumask_test_cpu(i, cpumask))
634                         continue;
635
636                 rc = set_cpus_allowed_ptr(current, cpumask);
637                 set_mems_allowed(*nodemask);
638                 if (rc == 0)
639                         schedule(); /* switch to allowed CPU */
640
641                 return rc;
642         }
643
644         /* don't need to set affinity because all online CPUs are covered */
645         return 0;
646 }
647 EXPORT_SYMBOL(cfs_cpt_bind);
648
649 /**
650  * Choose max to \a number CPUs from \a node and set them in \a cpt.
651  * We always prefer to choose CPU in the same core/socket.
652  */
653 static int
654 cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
655                      cpumask_t *node, int number)
656 {
657         cpumask_t       *socket = NULL;
658         cpumask_t       *core = NULL;
659         int             rc = 0;
660         int             cpu;
661
662         LASSERT(number > 0);
663
664         if (number >= cpumask_weight(node)) {
665                 while (!cpumask_empty(node)) {
666                         cpu = cpumask_first(node);
667
668                         rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
669                         if (!rc)
670                                 return -EINVAL;
671                         cpumask_clear_cpu(cpu, node);
672                 }
673                 return 0;
674         }
675
676         /* allocate scratch buffer */
677         LIBCFS_ALLOC(socket, cpumask_size());
678         LIBCFS_ALLOC(core, cpumask_size());
679         if (socket == NULL || core == NULL) {
680                 rc = -ENOMEM;
681                 goto out;
682         }
683
684         while (!cpumask_empty(node)) {
685                 cpu = cpumask_first(node);
686
687                 /* get cpumask for cores in the same socket */
688                 cfs_cpu_core_siblings(cpu, socket);
689                 cpumask_and(socket, socket, node);
690
691                 LASSERT(!cpumask_empty(socket));
692
693                 while (!cpumask_empty(socket)) {
694                         int     i;
695
696                         /* get cpumask for hts in the same core */
697                         cfs_cpu_ht_siblings(cpu, core);
698                         cpumask_and(core, core, node);
699
700                         LASSERT(!cpumask_empty(core));
701
702                         for_each_cpu(i, core) {
703                                 cpumask_clear_cpu(i, socket);
704                                 cpumask_clear_cpu(i, node);
705
706                                 rc = cfs_cpt_set_cpu(cptab, cpt, i);
707                                 if (!rc) {
708                                         rc = -EINVAL;
709                                         goto out;
710                                 }
711
712                                 if (--number == 0)
713                                         goto out;
714                         }
715                         cpu = cpumask_first(socket);
716                 }
717         }
718
719  out:
720         if (socket != NULL)
721                 LIBCFS_FREE(socket, cpumask_size());
722         if (core != NULL)
723                 LIBCFS_FREE(core, cpumask_size());
724         return rc;
725 }
726
727 #define CPT_WEIGHT_MIN  4u
728
729 static unsigned int
730 cfs_cpt_num_estimate(void)
731 {
732         unsigned nnode = num_online_nodes();
733         unsigned ncpu  = num_online_cpus();
734         unsigned ncpt;
735
736         if (ncpu <= CPT_WEIGHT_MIN) {
737                 ncpt = 1;
738                 goto out;
739         }
740
741         /* generate reasonable number of CPU partitions based on total number
742          * of CPUs, Preferred N should be power2 and match this condition:
743          * 2 * (N - 1)^2 < NCPUS <= 2 * N^2 */
744         for (ncpt = 2; ncpu > 2 * ncpt * ncpt; ncpt <<= 1) {}
745
746         if (ncpt <= nnode) { /* fat numa system */
747                 while (nnode > ncpt)
748                         nnode >>= 1;
749
750         } else { /* ncpt > nnode */
751                 while ((nnode << 1) <= ncpt)
752                         nnode <<= 1;
753         }
754
755         ncpt = nnode;
756
757  out:
758 #if (BITS_PER_LONG == 32)
759         /* config many CPU partitions on 32-bit system could consume
760          * too much memory */
761         ncpt = min(2U, ncpt);
762 #endif
763         while (ncpu % ncpt != 0)
764                 ncpt--; /* worst case is 1 */
765
766         return ncpt;
767 }
768
769 static struct cfs_cpt_table *
770 cfs_cpt_table_create(int ncpt)
771 {
772         struct cfs_cpt_table *cptab = NULL;
773         cpumask_t       *mask = NULL;
774         int             cpt = 0;
775         int             num;
776         int             rc;
777         int             i;
778
779         rc = cfs_cpt_num_estimate();
780         if (ncpt <= 0)
781                 ncpt = rc;
782
783         if (ncpt > num_online_cpus() || ncpt > 4 * rc) {
784                 CWARN("CPU partition number %d is larger than suggested "
785                       "value (%d), your system may have performance"
786                       "issue or run out of memory while under pressure\n",
787                       ncpt, rc);
788         }
789
790         if (num_online_cpus() % ncpt != 0) {
791                 CERROR("CPU number %d is not multiple of cpu_npartition %d, "
792                        "please try different cpu_npartitions value or"
793                        "set pattern string by cpu_pattern=STRING\n",
794                        (int)num_online_cpus(), ncpt);
795                 goto failed;
796         }
797
798         cptab = cfs_cpt_table_alloc(ncpt);
799         if (cptab == NULL) {
800                 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
801                 goto failed;
802         }
803
804         num = num_online_cpus() / ncpt;
805         if (num == 0) {
806                 CERROR("CPU changed while setting CPU partition\n");
807                 goto failed;
808         }
809
810         LIBCFS_ALLOC(mask, cpumask_size());
811         if (mask == NULL) {
812                 CERROR("Failed to allocate scratch cpumask\n");
813                 goto failed;
814         }
815
816         for_each_online_node(i) {
817                 cfs_node_to_cpumask(i, mask);
818
819                 while (!cpumask_empty(mask)) {
820                         struct cfs_cpu_partition *part;
821                         int    n;
822
823                         /* Each emulated NUMA node has all allowed CPUs in
824                          * the mask.
825                          * End loop when all partitions have assigned CPUs.
826                          */
827                         if (cpt == ncpt)
828                                 break;
829
830                         part = &cptab->ctb_parts[cpt];
831
832                         n = num - cpumask_weight(part->cpt_cpumask);
833                         LASSERT(n > 0);
834
835                         rc = cfs_cpt_choose_ncpus(cptab, cpt, mask, n);
836                         if (rc < 0)
837                                 goto failed;
838
839                         LASSERT(num >= cpumask_weight(part->cpt_cpumask));
840                         if (num == cpumask_weight(part->cpt_cpumask))
841                                 cpt++;
842                 }
843         }
844
845         if (cpt != ncpt ||
846             num != cpumask_weight(cptab->ctb_parts[ncpt - 1].cpt_cpumask)) {
847                 CERROR("Expect %d(%d) CPU partitions but got %d(%d), "
848                        "CPU hotplug/unplug while setting?\n",
849                        cptab->ctb_nparts, num, cpt,
850                        cpumask_weight(cptab->ctb_parts[ncpt - 1].cpt_cpumask));
851                 goto failed;
852         }
853
854         LIBCFS_FREE(mask, cpumask_size());
855
856         return cptab;
857
858  failed:
859         CERROR("Failed to setup CPU-partition-table with %d "
860                "CPU-partitions, online HW nodes: %d, HW cpus: %d.\n",
861                ncpt, num_online_nodes(), num_online_cpus());
862
863         if (mask != NULL)
864                 LIBCFS_FREE(mask, cpumask_size());
865
866         if (cptab != NULL)
867                 cfs_cpt_table_free(cptab);
868
869         return NULL;
870 }
871
872 static struct cfs_cpt_table *
873 cfs_cpt_table_create_pattern(char *pattern)
874 {
875         struct cfs_cpt_table    *cptab;
876         char                    *str;
877         int                     node    = 0;
878         int                     ncpt    = 0;
879         int                     high;
880         int                     cpt;
881         int                     rc;
882         int                     c;
883         int                     i;
884
885         str = cfs_trimwhite(pattern);
886         if (*str == 'n' || *str == 'N') {
887                 pattern = str + 1;
888                 if (*pattern != '\0') {
889                         node = 1; /* numa pattern */
890
891                 } else { /* shortcut to create CPT from NUMA & CPU topology */
892                         node = -1;
893                         ncpt = num_online_nodes();
894                 }
895         }
896
897         if (ncpt == 0) { /* scanning bracket which is mark of partition */
898                 for (str = pattern;; str++, ncpt++) {
899                         str = strchr(str, '[');
900                         if (str == NULL)
901                                 break;
902                 }
903         }
904
905         if (ncpt == 0 ||
906             (node && ncpt > num_online_nodes()) ||
907             (!node && ncpt > num_online_cpus())) {
908                 CERROR("Invalid pattern %s, or too many partitions %d\n",
909                        pattern, ncpt);
910                 return NULL;
911         }
912
913         cptab = cfs_cpt_table_alloc(ncpt);
914         if (cptab == NULL) {
915                 CERROR("Failed to allocate cpu partition table\n");
916                 return NULL;
917         }
918
919         if (node < 0) { /* shortcut to create CPT from NUMA & CPU topology */
920                 cpt = 0;
921                 for_each_online_node(i) {
922                         if (cpt >= ncpt) {
923                                 CERROR("CPU changed while setting CPU "
924                                        "partition table, %d/%d\n", cpt, ncpt);
925                                 goto failed;
926                         }
927
928                         rc = cfs_cpt_set_node(cptab, cpt++, i);
929                         if (!rc)
930                                 goto failed;
931                 }
932                 return cptab;
933         }
934
935         high = node ? MAX_NUMNODES - 1 : nr_cpu_ids - 1;
936
937         for (str = cfs_trimwhite(pattern), c = 0;; c++) {
938                 struct cfs_range_expr   *range;
939                 struct cfs_expr_list    *el;
940                 char                    *bracket = strchr(str, '[');
941                 int                     n;
942
943                 if (bracket == NULL) {
944                         if (*str != 0) {
945                                 CERROR("Invalid pattern %s\n", str);
946                                 goto failed;
947                         } else if (c != ncpt) {
948                                 CERROR("expect %d partitions but found %d\n",
949                                        ncpt, c);
950                                 goto failed;
951                         }
952                         break;
953                 }
954
955                 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
956                         CERROR("Invalid cpu pattern %s\n", str);
957                         goto failed;
958                 }
959
960                 if (cpt < 0 || cpt >= ncpt) {
961                         CERROR("Invalid partition id %d, total partitions %d\n",
962                                cpt, ncpt);
963                         goto failed;
964                 }
965
966                 if (cfs_cpt_weight(cptab, cpt) != 0) {
967                         CERROR("Partition %d has already been set.\n", cpt);
968                         goto failed;
969                 }
970
971                 str = cfs_trimwhite(str + n);
972                 if (str != bracket) {
973                         CERROR("Invalid pattern %s\n", str);
974                         goto failed;
975                 }
976
977                 bracket = strchr(str, ']');
978                 if (bracket == NULL) {
979                         CERROR("missing right bracket for cpt %d, %s\n",
980                                cpt, str);
981                         goto failed;
982                 }
983
984                 if (cfs_expr_list_parse(str, (bracket - str) + 1,
985                                         0, high, &el) != 0) {
986                         CERROR("Can't parse number range: %s\n", str);
987                         goto failed;
988                 }
989
990                 list_for_each_entry(range, &el->el_exprs, re_link) {
991                         for (i = range->re_lo; i <= range->re_hi; i++) {
992                                 if ((i - range->re_lo) % range->re_stride != 0)
993                                         continue;
994
995                                 rc = node ? cfs_cpt_set_node(cptab, cpt, i) :
996                                             cfs_cpt_set_cpu(cptab, cpt, i);
997                                 if (!rc) {
998                                         cfs_expr_list_free(el);
999                                         goto failed;
1000                                 }
1001                         }
1002                 }
1003
1004                 cfs_expr_list_free(el);
1005
1006                 if (!cfs_cpt_online(cptab, cpt)) {
1007                         CERROR("No online CPU is found on partition %d\n", cpt);
1008                         goto failed;
1009                 }
1010
1011                 str = cfs_trimwhite(bracket + 1);
1012         }
1013
1014         return cptab;
1015
1016  failed:
1017         cfs_cpt_table_free(cptab);
1018         return NULL;
1019 }
1020
1021 #ifdef CONFIG_HOTPLUG_CPU
1022 static int
1023 cfs_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1024 {
1025         unsigned int cpu = (unsigned long)hcpu;
1026         bool         warn;
1027
1028         switch (action) {
1029         case CPU_DEAD:
1030         case CPU_DEAD_FROZEN:
1031         case CPU_ONLINE:
1032         case CPU_ONLINE_FROZEN:
1033                 spin_lock(&cpt_data.cpt_lock);
1034                 cpt_data.cpt_version++;
1035                 spin_unlock(&cpt_data.cpt_lock);
1036         default:
1037                 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
1038                         CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
1039                                cpu, action);
1040                         break;
1041                 }
1042
1043                 mutex_lock(&cpt_data.cpt_mutex);
1044                 /* if all HTs in a core are offline, it may break affinity */
1045                 cfs_cpu_ht_siblings(cpu, cpt_data.cpt_cpumask);
1046                 warn = cpumask_any_and(cpt_data.cpt_cpumask,
1047                                        cpu_online_mask) >= nr_cpu_ids;
1048                 mutex_unlock(&cpt_data.cpt_mutex);
1049                 CDEBUG(warn ? D_WARNING : D_INFO,
1050                        "Lustre: can't support CPU plug-out well now, "
1051                        "performance and stability could be impacted"
1052                        "[CPU %u action: %lx]\n", cpu, action);
1053         }
1054
1055         return NOTIFY_OK;
1056 }
1057
1058 static struct notifier_block cfs_cpu_notifier = {
1059         .notifier_call  = cfs_cpu_notify,
1060         .priority       = 0
1061 };
1062
1063 #endif
1064
1065 void
1066 cfs_cpu_fini(void)
1067 {
1068         if (cfs_cpt_table != NULL)
1069                 cfs_cpt_table_free(cfs_cpt_table);
1070
1071 #ifdef CONFIG_HOTPLUG_CPU
1072         unregister_hotcpu_notifier(&cfs_cpu_notifier);
1073 #endif
1074         if (cpt_data.cpt_cpumask != NULL)
1075                 LIBCFS_FREE(cpt_data.cpt_cpumask, cpumask_size());
1076 }
1077
1078 int
1079 cfs_cpu_init(void)
1080 {
1081         LASSERT(cfs_cpt_table == NULL);
1082
1083         memset(&cpt_data, 0, sizeof(cpt_data));
1084
1085         LIBCFS_ALLOC(cpt_data.cpt_cpumask, cpumask_size());
1086         if (cpt_data.cpt_cpumask == NULL) {
1087                 CERROR("Failed to allocate scratch buffer\n");
1088                 return -1;
1089         }
1090
1091         spin_lock_init(&cpt_data.cpt_lock);
1092         mutex_init(&cpt_data.cpt_mutex);
1093
1094 #ifdef CONFIG_HOTPLUG_CPU
1095         register_hotcpu_notifier(&cfs_cpu_notifier);
1096 #endif
1097
1098         if (*cpu_pattern != 0) {
1099                 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern);
1100                 if (cfs_cpt_table == NULL) {
1101                         CERROR("Failed to create cptab from pattern %s\n",
1102                                cpu_pattern);
1103                         goto failed;
1104                 }
1105
1106         } else {
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",
1110                                cpu_npartitions);
1111                         goto failed;
1112                 }
1113         }
1114
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");
1119                 goto failed;
1120         }
1121         spin_unlock(&cpt_data.cpt_lock);
1122
1123         LCONSOLE(0, "HW CPU cores: %d, npartitions: %d\n",
1124                  num_online_cpus(), cfs_cpt_number(cfs_cpt_table));
1125         return 0;
1126
1127  failed:
1128         cfs_cpu_fini();
1129         return -1;
1130 }
1131
1132 #endif