Whamcloud - gitweb
d351eaa79f03438cbd9e828e987b38e812f6b9b7
[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, "CPU %d is not in CPU partition %d\n", cpu, cpt);
483                 return;
484         }
485
486         LASSERT(cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
487         LASSERT(cpumask_test_cpu(cpu, cptab->ctb_cpumask));
488
489         cfs_cpt_del_cpu(cptab, cpt, cpu);
490         cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
491 }
492 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
493
494 int cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt,
495                         const cpumask_t *mask)
496 {
497         int cpu;
498
499         if (cpumask_weight(mask) == 0 ||
500             cpumask_any_and(mask, cpu_online_mask) >= nr_cpu_ids) {
501                 CDEBUG(D_INFO, "No online CPU is found in the CPU mask "
502                                "for CPU partition %d\n", cpt);
503                 return 0;
504         }
505
506         for_each_cpu(cpu, mask) {
507                 cfs_cpt_add_cpu(cptab, cpt, cpu);
508                 cfs_cpt_add_node(cptab, cpt, cpu_to_node(cpu));
509         }
510
511         return 1;
512 }
513 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
514
515 void cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt,
516                            const cpumask_t *mask)
517 {
518         int cpu;
519
520         for_each_cpu(cpu, mask) {
521                 cfs_cpt_del_cpu(cptab, cpt, cpu);
522                 cfs_cpt_del_node(cptab, cpt, cpu_to_node(cpu));
523         }
524 }
525 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
526
527 int cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
528 {
529         const cpumask_t *mask;
530         int cpu;
531
532         if (node < 0 || node >= nr_node_ids) {
533                 CDEBUG(D_INFO,
534                        "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
535                 return 0;
536         }
537
538         mask = cpumask_of_node(node);
539
540         for_each_cpu(cpu, mask)
541                 cfs_cpt_add_cpu(cptab, cpt, cpu);
542
543         cfs_cpt_add_node(cptab, cpt, node);
544
545         return 1;
546 }
547 EXPORT_SYMBOL(cfs_cpt_set_node);
548
549 void cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
550 {
551         const cpumask_t *mask;
552         int cpu;
553
554         if (node < 0 || node >= nr_node_ids) {
555                 CDEBUG(D_INFO,
556                        "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
557                 return;
558         }
559
560         mask = cpumask_of_node(node);
561
562         for_each_cpu(cpu, mask)
563                 cfs_cpt_del_cpu(cptab, cpt, cpu);
564
565         cfs_cpt_del_node(cptab, cpt, node);
566 }
567 EXPORT_SYMBOL(cfs_cpt_unset_node);
568
569 int cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt,
570                          const nodemask_t *mask)
571 {
572         int node;
573
574         for_each_node_mask(node, *mask)
575                 cfs_cpt_set_node(cptab, cpt, node);
576
577         return 1;
578 }
579 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
580
581 void cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt,
582                             const nodemask_t *mask)
583 {
584         int node;
585
586         for_each_node_mask(node, *mask)
587                 cfs_cpt_unset_node(cptab, cpt, node);
588 }
589 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
590
591 int cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
592 {
593         nodemask_t *mask;
594         int weight;
595         int rotor;
596         int node = 0;
597
598         /* convert CPU partition ID to HW node id */
599
600         if (cpt < 0 || cpt >= cptab->ctb_nparts) {
601                 mask  = cptab->ctb_nodemask;
602                 rotor = cptab->ctb_spread_rotor++;
603         } else {
604                 mask  = cptab->ctb_parts[cpt].cpt_nodemask;
605                 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
606                 node  = cptab->ctb_parts[cpt].cpt_node;
607         }
608
609         weight = nodes_weight(*mask);
610         if (weight > 0) {
611                 rotor %= weight;
612
613                 for_each_node_mask(node, *mask) {
614                         if (rotor-- == 0)
615                                 return node;
616                 }
617         }
618
619         return node;
620 }
621 EXPORT_SYMBOL(cfs_cpt_spread_node);
622
623 int cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
624 {
625         int cpu = smp_processor_id();
626         int cpt = cptab->ctb_cpu2cpt[cpu];
627
628         if (cpt < 0) {
629                 if (!remap)
630                         return cpt;
631
632                 /* don't return negative value for safety of upper layer,
633                  * instead we shadow the unknown cpu to a valid partition ID */
634                 cpt = cpu % cptab->ctb_nparts;
635         }
636
637         return cpt;
638 }
639 EXPORT_SYMBOL(cfs_cpt_current);
640
641 int cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
642 {
643         LASSERT(cpu >= 0 && cpu < nr_cpu_ids);
644
645         return cptab->ctb_cpu2cpt[cpu];
646 }
647 EXPORT_SYMBOL(cfs_cpt_of_cpu);
648
649 int cfs_cpt_of_node(struct cfs_cpt_table *cptab, int node)
650 {
651         if (node < 0 || node > nr_node_ids)
652                 return CFS_CPT_ANY;
653
654         return cptab->ctb_node2cpt[node];
655 }
656 EXPORT_SYMBOL(cfs_cpt_of_node);
657
658 int cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
659 {
660         nodemask_t *nodemask;
661         cpumask_t *cpumask;
662         int cpu;
663         int rc;
664
665         LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
666
667         if (cpt == CFS_CPT_ANY) {
668                 cpumask = cptab->ctb_cpumask;
669                 nodemask = cptab->ctb_nodemask;
670         } else {
671                 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
672                 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
673         }
674
675         if (!cpumask_intersects(cpumask, cpu_online_mask)) {
676                 CDEBUG(D_INFO, "No online CPU found in CPU partition %d, did "
677                         "someone do CPU hotplug on system? You might need to "
678                         "reload Lustre modules to keep system working well.\n",
679                         cpt);
680                 return -ENODEV;
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                 rc = -ENOMEM;
843                 goto failed;
844         }
845
846         LIBCFS_ALLOC(node_mask, cpumask_size());
847         if (node_mask == NULL) {
848                 CERROR("Failed to allocate scratch cpumask\n");
849                 rc = -ENOMEM;
850                 goto failed;
851         }
852
853         num = num_online_cpus() / ncpt;
854         rem = num_online_cpus() % ncpt;
855         for_each_online_node(node) {
856                 cpumask_copy(node_mask, cpumask_of_node(node));
857
858                 while (cpt < ncpt && !cpumask_empty(node_mask)) {
859                         struct cfs_cpu_partition *part = &cptab->ctb_parts[cpt];
860                         int ncpu = cpumask_weight(part->cpt_cpumask);
861
862                         rc = cfs_cpt_choose_ncpus(cptab, cpt, node_mask,
863                                                   num - ncpu);
864                         if (rc < 0) {
865                                 rc = -EINVAL;
866                                 goto failed;
867                         }
868
869                         ncpu = cpumask_weight(part->cpt_cpumask);
870                         if (ncpu == num + !!(rem > 0)) {
871                                 cpt++;
872                                 rem--;
873                         }
874                 }
875         }
876
877         LIBCFS_FREE(node_mask, cpumask_size());
878         return cptab;
879
880 failed:
881         CERROR("Failed (rc=%d) to setup CPU partition table with %d "
882                 "partitions, online HW NUMA nodes: %d, HW CPU cores: %d.\n",
883                 rc, ncpt, num_online_nodes(), num_online_cpus());
884
885         if (node_mask != NULL)
886                 LIBCFS_FREE(node_mask, cpumask_size());
887
888         if (cptab != NULL)
889                 cfs_cpt_table_free(cptab);
890
891         return ERR_PTR(rc);
892 }
893
894 static struct cfs_cpt_table *cfs_cpt_table_create_pattern(const char *pattern)
895 {
896         struct cfs_cpt_table *cptab;
897         char *pattern_dup;
898         char *bracket;
899         char *str;
900         int node = 0;
901         int ncpt = 0;
902         int cpt  = 0;
903         int high;
904         int rc;
905         int c;
906         int i;
907
908         pattern_dup = kstrdup(pattern, GFP_KERNEL);
909         if (pattern_dup == NULL) {
910                 CERROR("Failed to duplicate pattern '%s'\n", pattern);
911                 return ERR_PTR(-ENOMEM);
912         }
913
914         str = cfs_trimwhite(pattern_dup);
915         if (*str == 'n' || *str == 'N') {
916                 str++; /* skip 'N' char */
917                 node = 1; /* NUMA pattern */
918                 if (*str == '\0') {
919                         node = -1;
920                         for_each_online_node(i) {
921                                 if (!cpumask_empty(cpumask_of_node(i)))
922                                         ncpt++;
923                         }
924                 }
925         }
926
927         if (ncpt == 0) { /* scanning bracket which is mark of partition */
928                 bracket = str;
929                 while ((bracket = strchr(bracket, '['))) {
930                         bracket++;
931                         ncpt++;
932                 }
933         }
934
935         if (ncpt == 0 ||
936             (node && ncpt > num_online_nodes()) ||
937             (!node && ncpt > num_online_cpus())) {
938                 CERROR("Invalid pattern '%s', or too many partitions %d\n",
939                        pattern_dup, ncpt);
940                 rc = -EINVAL;
941                 goto err_free_str;
942         }
943
944         cptab = cfs_cpt_table_alloc(ncpt);
945         if (cptab == NULL) {
946                 CERROR("Failed to allocate CPU partition table\n");
947                 rc = -ENOMEM;
948                 goto err_free_str;
949         }
950
951         if (node < 0) { /* shortcut to create CPT from NUMA & CPU topology */
952                 for_each_online_node(i) {
953                         if (cpumask_empty(cpumask_of_node(i)))
954                                 continue;
955
956                         rc = cfs_cpt_set_node(cptab, cpt++, i);
957                         if (!rc) {
958                                 rc = -EINVAL;
959                                 goto err_free_table;
960                         }
961                 }
962                 kfree(pattern_dup);
963                 return cptab;
964         }
965
966         high = node ? nr_node_ids - 1 : nr_cpu_ids - 1;
967
968         for (str = cfs_trimwhite(str), c = 0; /* until break */; c++) {
969                 struct cfs_range_expr *range;
970                 struct cfs_expr_list *el;
971                 int n;
972
973                 bracket = strchr(str, '[');
974                 if (bracket == NULL) {
975                         if (*str != 0) {
976                                 CERROR("Invalid pattern '%s'\n", str);
977                                 rc = -EINVAL;
978                                 goto err_free_table;
979                         } else if (c != ncpt) {
980                                 CERROR("Expect %d partitions but found %d\n",
981                                         ncpt, c);
982                                 rc = -EINVAL;
983                                 goto err_free_table;
984                         }
985                         break;
986                 }
987
988                 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
989                         CERROR("Invalid CPU pattern '%s'\n", str);
990                         rc = -EINVAL;
991                         goto err_free_table;
992                 }
993
994                 if (cpt < 0 || cpt >= ncpt) {
995                         CERROR("Invalid partition id %d, total partitions %d\n",
996                                cpt, ncpt);
997                         rc = -EINVAL;
998                         goto err_free_table;
999                 }
1000
1001                 if (cfs_cpt_weight(cptab, cpt) != 0) {
1002                         CERROR("Partition %d has already been set.\n", cpt);
1003                         rc = -EPERM;
1004                         goto err_free_table;
1005                 }
1006
1007                 str = cfs_trimwhite(str + n);
1008                 if (str != bracket) {
1009                         CERROR("Invalid pattern '%s'\n", str);
1010                         rc = -EINVAL;
1011                         goto err_free_table;
1012                 }
1013
1014                 bracket = strchr(str, ']');
1015                 if (bracket == NULL) {
1016                         CERROR("Missing right bracket for partition "
1017                                 "%d in '%s'\n", cpt, str);
1018                         rc = -EINVAL;
1019                         goto err_free_table;
1020                 }
1021
1022                 rc = cfs_expr_list_parse(str, (bracket - str) + 1, 0, high,
1023                                          &el);
1024                 if (rc) {
1025                         CERROR("Can't parse number range in '%s'\n", str);
1026                         rc = -ERANGE;
1027                         goto err_free_table;
1028                 }
1029
1030                 list_for_each_entry(range, &el->el_exprs, re_link) {
1031                         for (i = range->re_lo; i <= range->re_hi; i++) {
1032                                 if ((i - range->re_lo) % range->re_stride != 0)
1033                                         continue;
1034
1035                                 rc = node ? cfs_cpt_set_node(cptab, cpt, i)
1036                                           : cfs_cpt_set_cpu(cptab, cpt, i);
1037                                 if (!rc) {
1038                                         cfs_expr_list_free(el);
1039                                         rc = -EINVAL;
1040                                         goto err_free_table;
1041                                 }
1042                         }
1043                 }
1044
1045                 cfs_expr_list_free(el);
1046
1047                 if (!cfs_cpt_online(cptab, cpt)) {
1048                         CERROR("No online CPU is found on partition %d\n", cpt);
1049                         rc = -ENODEV;
1050                         goto err_free_table;
1051                 }
1052
1053                 str = cfs_trimwhite(bracket + 1);
1054         }
1055
1056         kfree(pattern_dup);
1057         return cptab;
1058
1059 err_free_table:
1060         cfs_cpt_table_free(cptab);
1061 err_free_str:
1062         kfree(pattern_dup);
1063         return ERR_PTR(rc);
1064 }
1065
1066 #ifdef CONFIG_HOTPLUG_CPU
1067 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1068 static enum cpuhp_state lustre_cpu_online;
1069
1070 static int cfs_cpu_online(unsigned int cpu)
1071 {
1072         return 0;
1073 }
1074 #endif
1075
1076 static int cfs_cpu_dead(unsigned int cpu)
1077 {
1078         bool warn;
1079
1080         /* if all HTs in a core are offline, it may break affinity */
1081         warn = cpumask_any_and(topology_sibling_cpumask(cpu),
1082                                cpu_online_mask) >= nr_cpu_ids;
1083         CDEBUG(warn ? D_WARNING : D_INFO,
1084                "Lustre: can't support CPU plug-out well now, performance and stability could be impacted [CPU %u]\n",
1085                cpu);
1086         return 0;
1087 }
1088
1089 #ifndef HAVE_HOTPLUG_STATE_MACHINE
1090 static int cfs_cpu_notify(struct notifier_block *self, unsigned long action,
1091                           void *hcpu)
1092 {
1093         int cpu = (unsigned long)hcpu;
1094
1095         switch (action) {
1096         case CPU_DEAD:
1097         case CPU_DEAD_FROZEN:
1098         case CPU_ONLINE:
1099         case CPU_ONLINE_FROZEN:
1100         default:
1101                 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
1102                         CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
1103                                cpu, action);
1104                         break;
1105                 }
1106
1107                 cfs_cpu_dead(cpu);
1108         }
1109
1110         return NOTIFY_OK;
1111 }
1112
1113 static struct notifier_block cfs_cpu_notifier = {
1114         .notifier_call  = cfs_cpu_notify,
1115         .priority       = 0
1116 };
1117 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1118 #endif /* CONFIG_HOTPLUG_CPU */
1119
1120 void cfs_cpu_fini(void)
1121 {
1122         if (!IS_ERR_OR_NULL(cfs_cpt_table))
1123                 cfs_cpt_table_free(cfs_cpt_table);
1124
1125 #ifdef CONFIG_HOTPLUG_CPU
1126 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1127         if (lustre_cpu_online > 0)
1128                 cpuhp_remove_state_nocalls(lustre_cpu_online);
1129         cpuhp_remove_state_nocalls(CPUHP_LUSTRE_CFS_DEAD);
1130 #else
1131         unregister_hotcpu_notifier(&cfs_cpu_notifier);
1132 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1133 #endif /* CONFIG_HOTPLUG_CPU */
1134 }
1135
1136 int cfs_cpu_init(void)
1137 {
1138         int ret = -EINVAL;
1139
1140         LASSERT(!cfs_cpt_table);
1141
1142 #ifdef CONFIG_HOTPLUG_CPU
1143 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1144         ret = cpuhp_setup_state_nocalls(CPUHP_LUSTRE_CFS_DEAD,
1145                                         "fs/lustre/cfe:dead", NULL,
1146                                         cfs_cpu_dead);
1147         if (ret < 0)
1148                 goto failed;
1149         ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
1150                                         "fs/lustre/cfe:online",
1151                                         cfs_cpu_online, NULL);
1152         if (ret < 0)
1153                 goto failed;
1154         lustre_cpu_online = ret;
1155 #else
1156         register_hotcpu_notifier(&cfs_cpu_notifier);
1157 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1158 #endif /* CONFIG_HOTPLUG_CPU */
1159         ret = -EINVAL;
1160
1161         get_online_cpus();
1162         if (*cpu_pattern != 0) {
1163                 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern);
1164                 if (IS_ERR(cfs_cpt_table)) {
1165                         CERROR("Failed to create cptab from pattern '%s'\n",
1166                                 cpu_pattern);
1167                         ret = PTR_ERR(cfs_cpt_table);
1168                         goto failed;
1169                 }
1170
1171         } else {
1172                 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1173                 if (IS_ERR(cfs_cpt_table)) {
1174                         CERROR("Failed to create cptab with npartitions %d\n",
1175                                 cpu_npartitions);
1176                         ret = PTR_ERR(cfs_cpt_table);
1177                         goto failed;
1178                 }
1179         }
1180         put_online_cpus();
1181
1182         LCONSOLE(0, "HW NUMA nodes: %d, HW CPU cores: %d, npartitions: %d\n",
1183                  num_online_nodes(), num_online_cpus(),
1184                  cfs_cpt_number(cfs_cpt_table));
1185         return 0;
1186
1187 failed:
1188         put_online_cpus();
1189         cfs_cpu_fini();
1190         return ret;
1191 }
1192
1193 #endif