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