Whamcloud - gitweb
855117451ec07ad21021d5af1902a63bb0bde68c
[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, 2017, 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 nthr = cpumask_weight(topology_sibling_cpumask(smp_processor_id()));
781         int ncpu  = num_online_cpus();
782         int ncpt = 1;
783
784         if (ncpu > CPT_WEIGHT_MIN)
785                 for (ncpt = 2; ncpu > 2 * nthr * ncpt; ncpt++);
786                         /* nothing */
787
788 #if (BITS_PER_LONG == 32)
789         /* config many CPU partitions on 32-bit system could consume
790          * too much memory */
791         ncpt = min(2, ncpt);
792 #endif
793         while (ncpu % ncpt != 0)
794                 ncpt--; /* worst case is 1 */
795
796         return ncpt;
797 }
798
799 static struct cfs_cpt_table *cfs_cpt_table_create(int ncpt)
800 {
801         struct cfs_cpt_table *cptab = NULL;
802         cpumask_t *node_mask = NULL;
803         int cpt = 0;
804         int node;
805         int num;
806         int rem;
807         int rc = 0;
808
809         num = cfs_cpt_num_estimate();
810         if (ncpt <= 0)
811                 ncpt = num;
812
813         if (ncpt > num_online_cpus() || ncpt > 4 * num) {
814                 CWARN("CPU partition number %d is larger than suggested "
815                       "value (%d), your system may have performance "
816                       "issue or run out of memory while under pressure\n",
817                       ncpt, num);
818         }
819
820         cptab = cfs_cpt_table_alloc(ncpt);
821         if (cptab == NULL) {
822                 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
823                 rc = -ENOMEM;
824                 goto failed;
825         }
826
827         LIBCFS_ALLOC(node_mask, cpumask_size());
828         if (node_mask == NULL) {
829                 CERROR("Failed to allocate scratch cpumask\n");
830                 rc = -ENOMEM;
831                 goto failed;
832         }
833
834         num = num_online_cpus() / ncpt;
835         rem = num_online_cpus() % ncpt;
836         for_each_online_node(node) {
837                 cpumask_copy(node_mask, cpumask_of_node(node));
838
839                 while (cpt < ncpt && !cpumask_empty(node_mask)) {
840                         struct cfs_cpu_partition *part = &cptab->ctb_parts[cpt];
841                         int ncpu = cpumask_weight(part->cpt_cpumask);
842
843                         rc = cfs_cpt_choose_ncpus(cptab, cpt, node_mask,
844                                                   num - ncpu);
845                         if (rc < 0) {
846                                 rc = -EINVAL;
847                                 goto failed;
848                         }
849
850                         ncpu = cpumask_weight(part->cpt_cpumask);
851                         if (ncpu == num + !!(rem > 0)) {
852                                 cpt++;
853                                 rem--;
854                         }
855                 }
856         }
857
858         LIBCFS_FREE(node_mask, cpumask_size());
859         return cptab;
860
861 failed:
862         CERROR("Failed (rc=%d) to setup CPU partition table with %d "
863                 "partitions, online HW NUMA nodes: %d, HW CPU cores: %d.\n",
864                 rc, ncpt, num_online_nodes(), num_online_cpus());
865
866         if (node_mask != NULL)
867                 LIBCFS_FREE(node_mask, cpumask_size());
868
869         if (cptab != NULL)
870                 cfs_cpt_table_free(cptab);
871
872         return ERR_PTR(rc);
873 }
874
875 static struct cfs_cpt_table *cfs_cpt_table_create_pattern(const char *pattern)
876 {
877         struct cfs_cpt_table *cptab;
878         char *pattern_dup;
879         char *bracket;
880         char *str;
881         int node = 0;
882         int ncpt = 0;
883         int cpt  = 0;
884         int high;
885         int rc;
886         int c;
887         int i;
888
889         pattern_dup = kstrdup(pattern, GFP_KERNEL);
890         if (pattern_dup == NULL) {
891                 CERROR("Failed to duplicate pattern '%s'\n", pattern);
892                 return ERR_PTR(-ENOMEM);
893         }
894
895         str = cfs_trimwhite(pattern_dup);
896         if (*str == 'n' || *str == 'N') {
897                 str++; /* skip 'N' char */
898                 node = 1; /* NUMA pattern */
899                 if (*str == '\0') {
900                         node = -1;
901                         for_each_online_node(i) {
902                                 if (!cpumask_empty(cpumask_of_node(i)))
903                                         ncpt++;
904                         }
905                         if (ncpt == 1) { /* single NUMA node */
906                                 kfree(pattern_dup);
907                                 return cfs_cpt_table_create(cpu_npartitions);
908                         }
909                 }
910         }
911
912         if (ncpt == 0) { /* scanning bracket which is mark of partition */
913                 bracket = str;
914                 while ((bracket = strchr(bracket, '['))) {
915                         bracket++;
916                         ncpt++;
917                 }
918         }
919
920         if (ncpt == 0 ||
921             (node && ncpt > num_online_nodes()) ||
922             (!node && ncpt > num_online_cpus())) {
923                 CERROR("Invalid pattern '%s', or too many partitions %d\n",
924                        pattern_dup, ncpt);
925                 rc = -EINVAL;
926                 goto err_free_str;
927         }
928
929         cptab = cfs_cpt_table_alloc(ncpt);
930         if (cptab == NULL) {
931                 CERROR("Failed to allocate CPU partition table\n");
932                 rc = -ENOMEM;
933                 goto err_free_str;
934         }
935
936         if (node < 0) { /* shortcut to create CPT from NUMA & CPU topology */
937                 for_each_online_node(i) {
938                         if (cpumask_empty(cpumask_of_node(i)))
939                                 continue;
940
941                         rc = cfs_cpt_set_node(cptab, cpt++, i);
942                         if (!rc) {
943                                 rc = -EINVAL;
944                                 goto err_free_table;
945                         }
946                 }
947                 kfree(pattern_dup);
948                 return cptab;
949         }
950
951         high = node ? nr_node_ids - 1 : nr_cpu_ids - 1;
952
953         for (str = cfs_trimwhite(str), c = 0; /* until break */; c++) {
954                 struct cfs_range_expr *range;
955                 struct cfs_expr_list *el;
956                 int n;
957
958                 bracket = strchr(str, '[');
959                 if (bracket == NULL) {
960                         if (*str != 0) {
961                                 CERROR("Invalid pattern '%s'\n", str);
962                                 rc = -EINVAL;
963                                 goto err_free_table;
964                         } else if (c != ncpt) {
965                                 CERROR("Expect %d partitions but found %d\n",
966                                         ncpt, c);
967                                 rc = -EINVAL;
968                                 goto err_free_table;
969                         }
970                         break;
971                 }
972
973                 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
974                         CERROR("Invalid CPU pattern '%s'\n", str);
975                         rc = -EINVAL;
976                         goto err_free_table;
977                 }
978
979                 if (cpt < 0 || cpt >= ncpt) {
980                         CERROR("Invalid partition id %d, total partitions %d\n",
981                                cpt, ncpt);
982                         rc = -EINVAL;
983                         goto err_free_table;
984                 }
985
986                 if (cfs_cpt_weight(cptab, cpt) != 0) {
987                         CERROR("Partition %d has already been set.\n", cpt);
988                         rc = -EPERM;
989                         goto err_free_table;
990                 }
991
992                 str = cfs_trimwhite(str + n);
993                 if (str != bracket) {
994                         CERROR("Invalid pattern '%s'\n", str);
995                         rc = -EINVAL;
996                         goto err_free_table;
997                 }
998
999                 bracket = strchr(str, ']');
1000                 if (bracket == NULL) {
1001                         CERROR("Missing right bracket for partition "
1002                                 "%d in '%s'\n", cpt, str);
1003                         rc = -EINVAL;
1004                         goto err_free_table;
1005                 }
1006
1007                 rc = cfs_expr_list_parse(str, (bracket - str) + 1, 0, high,
1008                                          &el);
1009                 if (rc) {
1010                         CERROR("Can't parse number range in '%s'\n", str);
1011                         rc = -ERANGE;
1012                         goto err_free_table;
1013                 }
1014
1015                 list_for_each_entry(range, &el->el_exprs, re_link) {
1016                         for (i = range->re_lo; i <= range->re_hi; i++) {
1017                                 if ((i - range->re_lo) % range->re_stride != 0)
1018                                         continue;
1019
1020                                 rc = node ? cfs_cpt_set_node(cptab, cpt, i)
1021                                           : cfs_cpt_set_cpu(cptab, cpt, i);
1022                                 if (!rc) {
1023                                         cfs_expr_list_free(el);
1024                                         rc = -EINVAL;
1025                                         goto err_free_table;
1026                                 }
1027                         }
1028                 }
1029
1030                 cfs_expr_list_free(el);
1031
1032                 if (!cfs_cpt_online(cptab, cpt)) {
1033                         CERROR("No online CPU is found on partition %d\n", cpt);
1034                         rc = -ENODEV;
1035                         goto err_free_table;
1036                 }
1037
1038                 str = cfs_trimwhite(bracket + 1);
1039         }
1040
1041         kfree(pattern_dup);
1042         return cptab;
1043
1044 err_free_table:
1045         cfs_cpt_table_free(cptab);
1046 err_free_str:
1047         kfree(pattern_dup);
1048         return ERR_PTR(rc);
1049 }
1050
1051 #ifdef CONFIG_HOTPLUG_CPU
1052 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1053 static enum cpuhp_state lustre_cpu_online;
1054
1055 static int cfs_cpu_online(unsigned int cpu)
1056 {
1057         return 0;
1058 }
1059 #endif
1060
1061 static int cfs_cpu_dead(unsigned int cpu)
1062 {
1063         bool warn;
1064
1065         /* if all HTs in a core are offline, it may break affinity */
1066         warn = cpumask_any_and(topology_sibling_cpumask(cpu),
1067                                cpu_online_mask) >= nr_cpu_ids;
1068         CDEBUG(warn ? D_WARNING : D_INFO,
1069                "Lustre: can't support CPU plug-out well now, performance and stability could be impacted [CPU %u]\n",
1070                cpu);
1071         return 0;
1072 }
1073
1074 #ifndef HAVE_HOTPLUG_STATE_MACHINE
1075 static int cfs_cpu_notify(struct notifier_block *self, unsigned long action,
1076                           void *hcpu)
1077 {
1078         int cpu = (unsigned long)hcpu;
1079
1080         switch (action) {
1081         case CPU_DEAD:
1082         case CPU_DEAD_FROZEN:
1083         case CPU_ONLINE:
1084         case CPU_ONLINE_FROZEN:
1085         default:
1086                 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
1087                         CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
1088                                cpu, action);
1089                         break;
1090                 }
1091
1092                 cfs_cpu_dead(cpu);
1093         }
1094
1095         return NOTIFY_OK;
1096 }
1097
1098 static struct notifier_block cfs_cpu_notifier = {
1099         .notifier_call  = cfs_cpu_notify,
1100         .priority       = 0
1101 };
1102 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1103 #endif /* CONFIG_HOTPLUG_CPU */
1104
1105 void cfs_cpu_fini(void)
1106 {
1107         if (!IS_ERR_OR_NULL(cfs_cpt_table))
1108                 cfs_cpt_table_free(cfs_cpt_table);
1109
1110 #ifdef CONFIG_HOTPLUG_CPU
1111 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1112         if (lustre_cpu_online > 0)
1113                 cpuhp_remove_state_nocalls(lustre_cpu_online);
1114         cpuhp_remove_state_nocalls(CPUHP_LUSTRE_CFS_DEAD);
1115 #else
1116         unregister_hotcpu_notifier(&cfs_cpu_notifier);
1117 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1118 #endif /* CONFIG_HOTPLUG_CPU */
1119 }
1120
1121 int cfs_cpu_init(void)
1122 {
1123         int ret = -EINVAL;
1124
1125         LASSERT(!cfs_cpt_table);
1126
1127 #ifdef CONFIG_HOTPLUG_CPU
1128 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1129         ret = cpuhp_setup_state_nocalls(CPUHP_LUSTRE_CFS_DEAD,
1130                                         "fs/lustre/cfe:dead", NULL,
1131                                         cfs_cpu_dead);
1132         if (ret < 0)
1133                 goto failed;
1134         ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
1135                                         "fs/lustre/cfe:online",
1136                                         cfs_cpu_online, NULL);
1137         if (ret < 0)
1138                 goto failed;
1139         lustre_cpu_online = ret;
1140 #else
1141         register_hotcpu_notifier(&cfs_cpu_notifier);
1142 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1143 #endif /* CONFIG_HOTPLUG_CPU */
1144         ret = -EINVAL;
1145
1146         get_online_cpus();
1147         if (*cpu_pattern != 0) {
1148                 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern);
1149                 if (IS_ERR(cfs_cpt_table)) {
1150                         CERROR("Failed to create cptab from pattern '%s'\n",
1151                                 cpu_pattern);
1152                         ret = PTR_ERR(cfs_cpt_table);
1153                         goto failed;
1154                 }
1155
1156         } else {
1157                 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1158                 if (IS_ERR(cfs_cpt_table)) {
1159                         CERROR("Failed to create cptab with npartitions %d\n",
1160                                 cpu_npartitions);
1161                         ret = PTR_ERR(cfs_cpt_table);
1162                         goto failed;
1163                 }
1164         }
1165         put_online_cpus();
1166
1167         LCONSOLE(0, "HW NUMA nodes: %d, HW CPU cores: %d, npartitions: %d\n",
1168                  num_online_nodes(), num_online_cpus(),
1169                  cfs_cpt_number(cfs_cpt_table));
1170         return 0;
1171
1172 failed:
1173         put_online_cpus();
1174         cfs_cpu_fini();
1175         return ret;
1176 }
1177
1178 #endif