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