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