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