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