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