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