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