Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[sfrench/cifs-2.6.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  * 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 = "";
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 = smp_processor_id();
532         int cpt = cptab->ctb_cpu2cpt[cpu];
533
534         if (cpt < 0) {
535                 if (!remap)
536                         return cpt;
537
538                 /* don't return negative value for safety of upper layer,
539                  * instead we shadow the unknown cpu to a valid partition ID
540                  */
541                 cpt = cpu % cptab->ctb_nparts;
542         }
543
544         return cpt;
545 }
546 EXPORT_SYMBOL(cfs_cpt_current);
547
548 int
549 cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
550 {
551         LASSERT(cpu >= 0 && cpu < nr_cpu_ids);
552
553         return cptab->ctb_cpu2cpt[cpu];
554 }
555 EXPORT_SYMBOL(cfs_cpt_of_cpu);
556
557 int
558 cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
559 {
560         cpumask_t *cpumask;
561         nodemask_t *nodemask;
562         int rc;
563         int i;
564
565         LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
566
567         if (cpt == CFS_CPT_ANY) {
568                 cpumask = cptab->ctb_cpumask;
569                 nodemask = cptab->ctb_nodemask;
570         } else {
571                 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
572                 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
573         }
574
575         if (cpumask_any_and(cpumask, cpu_online_mask) >= nr_cpu_ids) {
576                 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",
577                        cpt);
578                 return -EINVAL;
579         }
580
581         for_each_online_cpu(i) {
582                 if (cpumask_test_cpu(i, cpumask))
583                         continue;
584
585                 rc = set_cpus_allowed_ptr(current, cpumask);
586                 set_mems_allowed(*nodemask);
587                 if (!rc)
588                         schedule(); /* switch to allowed CPU */
589
590                 return rc;
591         }
592
593         /* don't need to set affinity because all online CPUs are covered */
594         return 0;
595 }
596 EXPORT_SYMBOL(cfs_cpt_bind);
597
598 /**
599  * Choose max to \a number CPUs from \a node and set them in \a cpt.
600  * We always prefer to choose CPU in the same core/socket.
601  */
602 static int
603 cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
604                      cpumask_t *node, int number)
605 {
606         cpumask_t *socket = NULL;
607         cpumask_t *core = NULL;
608         int rc = 0;
609         int cpu;
610
611         LASSERT(number > 0);
612
613         if (number >= cpumask_weight(node)) {
614                 while (!cpumask_empty(node)) {
615                         cpu = cpumask_first(node);
616
617                         rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
618                         if (!rc)
619                                 return -EINVAL;
620                         cpumask_clear_cpu(cpu, node);
621                 }
622                 return 0;
623         }
624
625         /* allocate scratch buffer */
626         LIBCFS_ALLOC(socket, cpumask_size());
627         LIBCFS_ALLOC(core, cpumask_size());
628         if (!socket || !core) {
629                 rc = -ENOMEM;
630                 goto out;
631         }
632
633         while (!cpumask_empty(node)) {
634                 cpu = cpumask_first(node);
635
636                 /* get cpumask for cores in the same socket */
637                 cpumask_copy(socket, topology_core_cpumask(cpu));
638                 cpumask_and(socket, socket, node);
639
640                 LASSERT(!cpumask_empty(socket));
641
642                 while (!cpumask_empty(socket)) {
643                         int i;
644
645                         /* get cpumask for hts in the same core */
646                         cpumask_copy(core, topology_sibling_cpumask(cpu));
647                         cpumask_and(core, core, node);
648
649                         LASSERT(!cpumask_empty(core));
650
651                         for_each_cpu(i, core) {
652                                 cpumask_clear_cpu(i, socket);
653                                 cpumask_clear_cpu(i, node);
654
655                                 rc = cfs_cpt_set_cpu(cptab, cpt, i);
656                                 if (!rc) {
657                                         rc = -EINVAL;
658                                         goto out;
659                                 }
660
661                                 if (!--number)
662                                         goto out;
663                         }
664                         cpu = cpumask_first(socket);
665                 }
666         }
667
668 out:
669         if (socket)
670                 LIBCFS_FREE(socket, cpumask_size());
671         if (core)
672                 LIBCFS_FREE(core, cpumask_size());
673         return rc;
674 }
675
676 #define CPT_WEIGHT_MIN  4u
677
678 static unsigned int
679 cfs_cpt_num_estimate(void)
680 {
681         unsigned int nnode = num_online_nodes();
682         unsigned int ncpu = num_online_cpus();
683         unsigned int ncpt;
684
685         if (ncpu <= CPT_WEIGHT_MIN) {
686                 ncpt = 1;
687                 goto out;
688         }
689
690         /* generate reasonable number of CPU partitions based on total number
691          * of CPUs, Preferred N should be power2 and match this condition:
692          * 2 * (N - 1)^2 < NCPUS <= 2 * N^2
693          */
694         for (ncpt = 2; ncpu > 2 * ncpt * ncpt; ncpt <<= 1)
695                 ;
696
697         if (ncpt <= nnode) { /* fat numa system */
698                 while (nnode > ncpt)
699                         nnode >>= 1;
700
701         } else { /* ncpt > nnode */
702                 while ((nnode << 1) <= ncpt)
703                         nnode <<= 1;
704         }
705
706         ncpt = nnode;
707
708 out:
709 #if (BITS_PER_LONG == 32)
710         /* config many CPU partitions on 32-bit system could consume
711          * too much memory
712          */
713         ncpt = min(2U, ncpt);
714 #endif
715         while (ncpu % ncpt)
716                 ncpt--; /* worst case is 1 */
717
718         return ncpt;
719 }
720
721 static struct cfs_cpt_table *
722 cfs_cpt_table_create(int ncpt)
723 {
724         struct cfs_cpt_table *cptab = NULL;
725         cpumask_t *mask = NULL;
726         int cpt = 0;
727         int num;
728         int rc;
729         int i;
730
731         rc = cfs_cpt_num_estimate();
732         if (ncpt <= 0)
733                 ncpt = rc;
734
735         if (ncpt > num_online_cpus() || ncpt > 4 * rc) {
736                 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",
737                       ncpt, rc);
738         }
739
740         if (num_online_cpus() % ncpt) {
741                 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",
742                        (int)num_online_cpus(), ncpt);
743                 goto failed;
744         }
745
746         cptab = cfs_cpt_table_alloc(ncpt);
747         if (!cptab) {
748                 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
749                 goto failed;
750         }
751
752         num = num_online_cpus() / ncpt;
753         if (!num) {
754                 CERROR("CPU changed while setting CPU partition\n");
755                 goto failed;
756         }
757
758         LIBCFS_ALLOC(mask, cpumask_size());
759         if (!mask) {
760                 CERROR("Failed to allocate scratch cpumask\n");
761                 goto failed;
762         }
763
764         for_each_online_node(i) {
765                 cfs_node_to_cpumask(i, mask);
766
767                 while (!cpumask_empty(mask)) {
768                         struct cfs_cpu_partition *part;
769                         int n;
770
771                         /*
772                          * Each emulated NUMA node has all allowed CPUs in
773                          * the mask.
774                          * End loop when all partitions have assigned CPUs.
775                          */
776                         if (cpt == ncpt)
777                                 break;
778
779                         part = &cptab->ctb_parts[cpt];
780
781                         n = num - cpumask_weight(part->cpt_cpumask);
782                         LASSERT(n > 0);
783
784                         rc = cfs_cpt_choose_ncpus(cptab, cpt, mask, n);
785                         if (rc < 0)
786                                 goto failed;
787
788                         LASSERT(num >= cpumask_weight(part->cpt_cpumask));
789                         if (num == cpumask_weight(part->cpt_cpumask))
790                                 cpt++;
791                 }
792         }
793
794         if (cpt != ncpt ||
795             num != cpumask_weight(cptab->ctb_parts[ncpt - 1].cpt_cpumask)) {
796                 CERROR("Expect %d(%d) CPU partitions but got %d(%d), CPU hotplug/unplug while setting?\n",
797                        cptab->ctb_nparts, num, cpt,
798                        cpumask_weight(cptab->ctb_parts[ncpt - 1].cpt_cpumask));
799                 goto failed;
800         }
801
802         LIBCFS_FREE(mask, cpumask_size());
803
804         return cptab;
805
806  failed:
807         CERROR("Failed to setup CPU-partition-table with %d CPU-partitions, online HW nodes: %d, HW cpus: %d.\n",
808                ncpt, num_online_nodes(), num_online_cpus());
809
810         if (mask)
811                 LIBCFS_FREE(mask, cpumask_size());
812
813         if (cptab)
814                 cfs_cpt_table_free(cptab);
815
816         return NULL;
817 }
818
819 static struct cfs_cpt_table *
820 cfs_cpt_table_create_pattern(char *pattern)
821 {
822         struct cfs_cpt_table *cptab;
823         char *str;
824         int node = 0;
825         int high;
826         int ncpt = 0;
827         int cpt;
828         int rc;
829         int c;
830         int i;
831
832         str = cfs_trimwhite(pattern);
833         if (*str == 'n' || *str == 'N') {
834                 pattern = str + 1;
835                 if (*pattern != '\0') {
836                         node = 1;
837                 } else { /* shortcut to create CPT from NUMA & CPU topology */
838                         node = -1;
839                         ncpt = num_online_nodes();
840                 }
841         }
842
843         if (!ncpt) { /* scanning bracket which is mark of partition */
844                 for (str = pattern;; str++, ncpt++) {
845                         str = strchr(str, '[');
846                         if (!str)
847                                 break;
848                 }
849         }
850
851         if (!ncpt ||
852             (node && ncpt > num_online_nodes()) ||
853             (!node && ncpt > num_online_cpus())) {
854                 CERROR("Invalid pattern %s, or too many partitions %d\n",
855                        pattern, ncpt);
856                 return NULL;
857         }
858
859         cptab = cfs_cpt_table_alloc(ncpt);
860         if (!cptab) {
861                 CERROR("Failed to allocate cpu partition table\n");
862                 return NULL;
863         }
864
865         if (node < 0) { /* shortcut to create CPT from NUMA & CPU topology */
866                 cpt = 0;
867
868                 for_each_online_node(i) {
869                         if (cpt >= ncpt) {
870                                 CERROR("CPU changed while setting CPU partition table, %d/%d\n",
871                                        cpt, ncpt);
872                                 goto failed;
873                         }
874
875                         rc = cfs_cpt_set_node(cptab, cpt++, i);
876                         if (!rc)
877                                 goto failed;
878                 }
879                 return cptab;
880         }
881
882         high = node ? MAX_NUMNODES - 1 : nr_cpu_ids - 1;
883
884         for (str = cfs_trimwhite(pattern), c = 0;; c++) {
885                 struct cfs_range_expr *range;
886                 struct cfs_expr_list *el;
887                 char *bracket = strchr(str, '[');
888                 int n;
889
890                 if (!bracket) {
891                         if (*str) {
892                                 CERROR("Invalid pattern %s\n", str);
893                                 goto failed;
894                         }
895                         if (c != ncpt) {
896                                 CERROR("expect %d partitions but found %d\n",
897                                        ncpt, c);
898                                 goto failed;
899                         }
900                         break;
901                 }
902
903                 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
904                         CERROR("Invalid cpu pattern %s\n", str);
905                         goto failed;
906                 }
907
908                 if (cpt < 0 || cpt >= ncpt) {
909                         CERROR("Invalid partition id %d, total partitions %d\n",
910                                cpt, ncpt);
911                         goto failed;
912                 }
913
914                 if (cfs_cpt_weight(cptab, cpt)) {
915                         CERROR("Partition %d has already been set.\n", cpt);
916                         goto failed;
917                 }
918
919                 str = cfs_trimwhite(str + n);
920                 if (str != bracket) {
921                         CERROR("Invalid pattern %s\n", str);
922                         goto failed;
923                 }
924
925                 bracket = strchr(str, ']');
926                 if (!bracket) {
927                         CERROR("missing right bracket for cpt %d, %s\n",
928                                cpt, str);
929                         goto failed;
930                 }
931
932                 if (cfs_expr_list_parse(str, (bracket - str) + 1,
933                                         0, high, &el)) {
934                         CERROR("Can't parse number range: %s\n", str);
935                         goto failed;
936                 }
937
938                 list_for_each_entry(range, &el->el_exprs, re_link) {
939                         for (i = range->re_lo; i <= range->re_hi; i++) {
940                                 if ((i - range->re_lo) % range->re_stride)
941                                         continue;
942
943                                 rc = node ? cfs_cpt_set_node(cptab, cpt, i) :
944                                             cfs_cpt_set_cpu(cptab, cpt, i);
945                                 if (!rc) {
946                                         cfs_expr_list_free(el);
947                                         goto failed;
948                                 }
949                         }
950                 }
951
952                 cfs_expr_list_free(el);
953
954                 if (!cfs_cpt_online(cptab, cpt)) {
955                         CERROR("No online CPU is found on partition %d\n", cpt);
956                         goto failed;
957                 }
958
959                 str = cfs_trimwhite(bracket + 1);
960         }
961
962         return cptab;
963
964  failed:
965         cfs_cpt_table_free(cptab);
966         return NULL;
967 }
968
969 #ifdef CONFIG_HOTPLUG_CPU
970 static enum cpuhp_state lustre_cpu_online;
971
972 static void cfs_cpu_incr_cpt_version(void)
973 {
974         spin_lock(&cpt_data.cpt_lock);
975         cpt_data.cpt_version++;
976         spin_unlock(&cpt_data.cpt_lock);
977 }
978
979 static int cfs_cpu_online(unsigned int cpu)
980 {
981         cfs_cpu_incr_cpt_version();
982         return 0;
983 }
984
985 static int cfs_cpu_dead(unsigned int cpu)
986 {
987         bool warn;
988
989         cfs_cpu_incr_cpt_version();
990
991         mutex_lock(&cpt_data.cpt_mutex);
992         /* if all HTs in a core are offline, it may break affinity */
993         cpumask_copy(cpt_data.cpt_cpumask, topology_sibling_cpumask(cpu));
994         warn = cpumask_any_and(cpt_data.cpt_cpumask,
995                                cpu_online_mask) >= nr_cpu_ids;
996         mutex_unlock(&cpt_data.cpt_mutex);
997         CDEBUG(warn ? D_WARNING : D_INFO,
998                "Lustre: can't support CPU plug-out well now, performance and stability could be impacted [CPU %u]\n",
999                cpu);
1000         return 0;
1001 }
1002 #endif
1003
1004 void
1005 cfs_cpu_fini(void)
1006 {
1007         if (cfs_cpt_table)
1008                 cfs_cpt_table_free(cfs_cpt_table);
1009
1010 #ifdef CONFIG_HOTPLUG_CPU
1011         if (lustre_cpu_online > 0)
1012                 cpuhp_remove_state_nocalls(lustre_cpu_online);
1013         cpuhp_remove_state_nocalls(CPUHP_LUSTRE_CFS_DEAD);
1014 #endif
1015         if (cpt_data.cpt_cpumask)
1016                 LIBCFS_FREE(cpt_data.cpt_cpumask, cpumask_size());
1017 }
1018
1019 int
1020 cfs_cpu_init(void)
1021 {
1022         int ret = 0;
1023
1024         LASSERT(!cfs_cpt_table);
1025
1026         memset(&cpt_data, 0, sizeof(cpt_data));
1027
1028         LIBCFS_ALLOC(cpt_data.cpt_cpumask, cpumask_size());
1029         if (!cpt_data.cpt_cpumask) {
1030                 CERROR("Failed to allocate scratch buffer\n");
1031                 return -1;
1032         }
1033
1034         spin_lock_init(&cpt_data.cpt_lock);
1035         mutex_init(&cpt_data.cpt_mutex);
1036
1037 #ifdef CONFIG_HOTPLUG_CPU
1038         ret = cpuhp_setup_state_nocalls(CPUHP_LUSTRE_CFS_DEAD,
1039                                         "staging/lustre/cfe:dead", NULL,
1040                                         cfs_cpu_dead);
1041         if (ret < 0)
1042                 goto failed;
1043         ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
1044                                         "staging/lustre/cfe:online",
1045                                         cfs_cpu_online, NULL);
1046         if (ret < 0)
1047                 goto failed;
1048         lustre_cpu_online = ret;
1049 #endif
1050         ret = -EINVAL;
1051
1052         if (*cpu_pattern) {
1053                 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern);
1054                 if (!cfs_cpt_table) {
1055                         CERROR("Failed to create cptab from pattern %s\n",
1056                                cpu_pattern);
1057                         goto failed;
1058                 }
1059
1060         } else {
1061                 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1062                 if (!cfs_cpt_table) {
1063                         CERROR("Failed to create ptable with npartitions %d\n",
1064                                cpu_npartitions);
1065                         goto failed;
1066                 }
1067         }
1068
1069         spin_lock(&cpt_data.cpt_lock);
1070         if (cfs_cpt_table->ctb_version != cpt_data.cpt_version) {
1071                 spin_unlock(&cpt_data.cpt_lock);
1072                 CERROR("CPU hotplug/unplug during setup\n");
1073                 goto failed;
1074         }
1075         spin_unlock(&cpt_data.cpt_lock);
1076
1077         LCONSOLE(0, "HW CPU cores: %d, npartitions: %d\n",
1078                  num_online_cpus(), cfs_cpt_number(cfs_cpt_table));
1079         return 0;
1080
1081  failed:
1082         cfs_cpu_fini();
1083         return ret;
1084 }
1085
1086 #endif