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