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