Whamcloud - gitweb
aa2c6b46e81b8402fd2dab3dfca7829b4e9a2e40
[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  4u
848
849 static unsigned int cfs_cpt_num_estimate(void)
850 {
851         unsigned int nthr;
852         unsigned int ncpu = num_online_cpus();
853         unsigned int ncpt = 1;
854
855         preempt_disable();
856         nthr = cpumask_weight(topology_sibling_cpumask(smp_processor_id()));
857         preempt_enable();
858
859         if (ncpu > CPT_WEIGHT_MIN)
860                 for (ncpt = 2; ncpu > 2 * nthr * ncpt; ncpt++)
861                         ; /* nothing */
862
863 #if (BITS_PER_LONG == 32)
864         /* config many CPU partitions on 32-bit system could consume
865          * too much memory
866          */
867         ncpt = min(2U, ncpt);
868 #endif
869         while (ncpu % ncpt)
870                 ncpt--; /* worst case is 1 */
871
872         return ncpt;
873 }
874
875 static struct cfs_cpt_table *cfs_cpt_table_create(int ncpt)
876 {
877         struct cfs_cpt_table *cptab = NULL;
878         cpumask_var_t node_mask;
879         int cpt = 0;
880         int node;
881         int num;
882         int rem;
883         int rc = 0;
884
885         num = cfs_cpt_num_estimate();
886         if (ncpt <= 0)
887                 ncpt = num;
888
889         if (ncpt > num_online_cpus()) {
890                 rc = -EINVAL;
891                 CERROR("libcfs: CPU partition count %d > cores %d: rc = %d\n",
892                        ncpt, num_online_cpus(), rc);
893                 goto failed;
894         }
895
896         if (ncpt > 4 * num) {
897                 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",
898                       ncpt, num);
899         }
900
901         cptab = cfs_cpt_table_alloc(ncpt);
902         if (!cptab) {
903                 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
904                 rc = -ENOMEM;
905                 goto failed;
906         }
907
908         if (!zalloc_cpumask_var(&node_mask, GFP_NOFS)) {
909                 CERROR("Failed to allocate scratch cpumask\n");
910                 rc = -ENOMEM;
911                 goto failed;
912         }
913
914         num = num_online_cpus() / ncpt;
915         rem = num_online_cpus() % ncpt;
916         for_each_online_node(node) {
917                 cpumask_copy(node_mask, cpumask_of_node(node));
918
919                 while (cpt < ncpt && !cpumask_empty(node_mask)) {
920                         struct cfs_cpu_partition *part = &cptab->ctb_parts[cpt];
921                         int ncpu = cpumask_weight(part->cpt_cpumask);
922
923                         rc = cfs_cpt_choose_ncpus(cptab, cpt, node_mask,
924                                                   (rem > 0) + num - ncpu);
925                         if (rc < 0) {
926                                 rc = -EINVAL;
927                                 goto failed_mask;
928                         }
929
930                         ncpu = cpumask_weight(part->cpt_cpumask);
931                         if (ncpu == num + !!(rem > 0)) {
932                                 cpt++;
933                                 rem--;
934                         }
935                 }
936         }
937
938         free_cpumask_var(node_mask);
939
940         return cptab;
941
942 failed_mask:
943         free_cpumask_var(node_mask);
944 failed:
945         CERROR("Failed (rc = %d) to setup CPU partition table with %d partitions, online HW NUMA nodes: %d, HW CPU cores: %d.\n",
946                rc, ncpt, num_online_nodes(), num_online_cpus());
947
948         if (cptab)
949                 cfs_cpt_table_free(cptab);
950
951         return ERR_PTR(rc);
952 }
953
954 static struct cfs_cpt_table *cfs_cpt_table_create_pattern(const char *pattern)
955 {
956         struct cfs_cpt_table *cptab;
957         char *pattern_dup;
958         char *bracket;
959         char *str;
960         int node = 0;
961         int ncpt = 0;
962         int cpt = 0;
963         int high;
964         int rc;
965         int c;
966         int i;
967
968         pattern_dup = kstrdup(pattern, GFP_KERNEL);
969         if (!pattern_dup) {
970                 CERROR("Failed to duplicate pattern '%s'\n", pattern);
971                 return ERR_PTR(-ENOMEM);
972         }
973
974         str = strim(pattern_dup);
975         if (*str == 'n' || *str == 'N') {
976                 str++; /* skip 'N' char */
977                 node = 1; /* NUMA pattern */
978                 if (*str == '\0') {
979                         node = -1;
980                         for_each_online_node(i) {
981                                 if (!cpumask_empty(cpumask_of_node(i)))
982                                         ncpt++;
983                         }
984                         if (ncpt == 1) { /* single NUMA node */
985                                 kfree(pattern_dup);
986                                 return cfs_cpt_table_create(cpu_npartitions);
987                         }
988                 }
989         }
990
991         if (!ncpt) { /* scanning bracket which is mark of partition */
992                 bracket = str;
993                 while ((bracket = strchr(bracket, '['))) {
994                         bracket++;
995                         ncpt++;
996                 }
997         }
998
999         if (!ncpt ||
1000             (node && ncpt > num_online_nodes()) ||
1001             (!node && ncpt > num_online_cpus())) {
1002                 CERROR("Invalid pattern '%s', or too many partitions %d\n",
1003                        pattern_dup, ncpt);
1004                 rc = -EINVAL;
1005                 goto err_free_str;
1006         }
1007
1008         cptab = cfs_cpt_table_alloc(ncpt);
1009         if (!cptab) {
1010                 CERROR("Failed to allocate CPU partition table\n");
1011                 rc = -ENOMEM;
1012                 goto err_free_str;
1013         }
1014
1015         if (node < 0) { /* shortcut to create CPT from NUMA & CPU topology */
1016                 for_each_online_node(i) {
1017                         if (cpumask_empty(cpumask_of_node(i)))
1018                                 continue;
1019
1020                         rc = cfs_cpt_set_node(cptab, cpt++, i);
1021                         if (!rc) {
1022                                 rc = -EINVAL;
1023                                 goto err_free_table;
1024                         }
1025                 }
1026                 kfree(pattern_dup);
1027                 return cptab;
1028         }
1029
1030         high = node ? nr_node_ids - 1 : nr_cpu_ids - 1;
1031
1032         for (str = strim(str), c = 0; /* until break */; c++) {
1033                 struct cfs_range_expr *range;
1034                 struct cfs_expr_list *el;
1035                 int n;
1036
1037                 bracket = strchr(str, '[');
1038                 if (!bracket) {
1039                         if (*str) {
1040                                 CERROR("Invalid pattern '%s'\n", str);
1041                                 rc = -EINVAL;
1042                                 goto err_free_table;
1043                         } else if (c != ncpt) {
1044                                 CERROR("Expect %d partitions but found %d\n",
1045                                        ncpt, c);
1046                                 rc = -EINVAL;
1047                                 goto err_free_table;
1048                         }
1049                         break;
1050                 }
1051
1052                 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
1053                         CERROR("Invalid CPU pattern '%s'\n", str);
1054                         rc = -EINVAL;
1055                         goto err_free_table;
1056                 }
1057
1058                 if (cpt < 0 || cpt >= ncpt) {
1059                         CERROR("Invalid partition id %d, total partitions %d\n",
1060                                cpt, ncpt);
1061                         rc = -EINVAL;
1062                         goto err_free_table;
1063                 }
1064
1065                 if (cfs_cpt_weight(cptab, cpt)) {
1066                         CERROR("Partition %d has already been set.\n", cpt);
1067                         rc = -EPERM;
1068                         goto err_free_table;
1069                 }
1070
1071                 str = strim(str + n);
1072                 if (str != bracket) {
1073                         CERROR("Invalid pattern '%s'\n", str);
1074                         rc = -EINVAL;
1075                         goto err_free_table;
1076                 }
1077
1078                 bracket = strchr(str, ']');
1079                 if (!bracket) {
1080                         CERROR("Missing right bracket for partition %d in '%s'\n",
1081                                cpt, str);
1082                         rc = -EINVAL;
1083                         goto err_free_table;
1084                 }
1085
1086                 rc = cfs_expr_list_parse(str, (bracket - str) + 1, 0, high,
1087                                          &el);
1088                 if (rc) {
1089                         CERROR("Can't parse number range in '%s'\n", str);
1090                         rc = -ERANGE;
1091                         goto err_free_table;
1092                 }
1093
1094                 list_for_each_entry(range, &el->el_exprs, re_link) {
1095                         for (i = range->re_lo; i <= range->re_hi; i++) {
1096                                 if ((i - range->re_lo) % range->re_stride)
1097                                         continue;
1098
1099                                 rc = node ? cfs_cpt_set_node(cptab, cpt, i)
1100                                           : cfs_cpt_set_cpu(cptab, cpt, i);
1101                                 if (!rc) {
1102                                         cfs_expr_list_free(el);
1103                                         rc = -EINVAL;
1104                                         goto err_free_table;
1105                                 }
1106                         }
1107                 }
1108
1109                 cfs_expr_list_free(el);
1110
1111                 if (!cfs_cpt_online(cptab, cpt)) {
1112                         CERROR("No online CPU is found on partition %d\n", cpt);
1113                         rc = -ENODEV;
1114                         goto err_free_table;
1115                 }
1116
1117                 str = strim(bracket + 1);
1118         }
1119
1120         kfree(pattern_dup);
1121         return cptab;
1122
1123 err_free_table:
1124         cfs_cpt_table_free(cptab);
1125 err_free_str:
1126         kfree(pattern_dup);
1127         return ERR_PTR(rc);
1128 }
1129
1130 #ifdef CONFIG_HOTPLUG_CPU
1131 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1132 static enum cpuhp_state lustre_cpu_online;
1133
1134 static int cfs_cpu_online(unsigned int cpu)
1135 {
1136         return 0;
1137 }
1138 #endif
1139
1140 static int cfs_cpu_dead(unsigned int cpu)
1141 {
1142         bool warn;
1143
1144         /* if all HTs in a core are offline, it may break affinity */
1145         warn = cpumask_any_and(topology_sibling_cpumask(cpu),
1146                                cpu_online_mask) >= nr_cpu_ids;
1147         CDEBUG(warn ? D_WARNING : D_INFO,
1148                "Lustre: can't support CPU plug-out well now, performance and stability could be impacted [CPU %u]\n",
1149                cpu);
1150         return 0;
1151 }
1152
1153 #ifndef HAVE_HOTPLUG_STATE_MACHINE
1154 static int cfs_cpu_notify(struct notifier_block *self, unsigned long action,
1155                           void *hcpu)
1156 {
1157         int cpu = (unsigned long)hcpu;
1158
1159         switch (action) {
1160         case CPU_DEAD:
1161         case CPU_DEAD_FROZEN:
1162         case CPU_ONLINE:
1163         case CPU_ONLINE_FROZEN:
1164         default:
1165                 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
1166                         CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
1167                                cpu, action);
1168                         break;
1169                 }
1170
1171                 cfs_cpu_dead(cpu);
1172         }
1173
1174         return NOTIFY_OK;
1175 }
1176
1177 static struct notifier_block cfs_cpu_notifier = {
1178         .notifier_call  = cfs_cpu_notify,
1179         .priority       = 0
1180 };
1181 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1182 #endif /* CONFIG_HOTPLUG_CPU */
1183
1184 void cfs_cpu_fini(void)
1185 {
1186         if (!IS_ERR_OR_NULL(cfs_cpt_tab))
1187                 cfs_cpt_table_free(cfs_cpt_tab);
1188
1189 #ifdef CONFIG_HOTPLUG_CPU
1190 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1191         if (lustre_cpu_online > 0)
1192                 cpuhp_remove_state_nocalls(lustre_cpu_online);
1193         cpuhp_remove_state_nocalls(CPUHP_LUSTRE_CFS_DEAD);
1194 #else
1195         unregister_hotcpu_notifier(&cfs_cpu_notifier);
1196 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1197 #endif /* CONFIG_HOTPLUG_CPU */
1198 }
1199
1200 int cfs_cpu_init(void)
1201 {
1202         int ret;
1203
1204         LASSERT(!cfs_cpt_tab);
1205
1206 #ifdef CONFIG_HOTPLUG_CPU
1207 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1208         ret = cpuhp_setup_state_nocalls(CPUHP_LUSTRE_CFS_DEAD,
1209                                         "fs/lustre/cfe:dead", NULL,
1210                                         cfs_cpu_dead);
1211         if (ret < 0)
1212                 goto failed_cpu_dead;
1213
1214         ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
1215                                         "fs/lustre/cfe:online",
1216                                         cfs_cpu_online, NULL);
1217         if (ret < 0)
1218                 goto failed_cpu_online;
1219
1220         lustre_cpu_online = ret;
1221 #else
1222         register_hotcpu_notifier(&cfs_cpu_notifier);
1223 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1224 #endif /* CONFIG_HOTPLUG_CPU */
1225
1226         get_online_cpus();
1227         if (*cpu_pattern) {
1228                 cfs_cpt_tab = cfs_cpt_table_create_pattern(cpu_pattern);
1229                 if (IS_ERR(cfs_cpt_tab)) {
1230                         CERROR("Failed to create cptab from pattern '%s'\n",
1231                                cpu_pattern);
1232                         ret = PTR_ERR(cfs_cpt_tab);
1233                         goto failed_alloc_table;
1234                 }
1235
1236         } else {
1237                 cfs_cpt_tab = cfs_cpt_table_create(cpu_npartitions);
1238                 if (IS_ERR(cfs_cpt_tab)) {
1239                         CERROR("Failed to create cptab with npartitions %d\n",
1240                                cpu_npartitions);
1241                         ret = PTR_ERR(cfs_cpt_tab);
1242                         goto failed_alloc_table;
1243                 }
1244         }
1245
1246         put_online_cpus();
1247
1248         LCONSOLE(0, "HW NUMA nodes: %d, HW CPU cores: %d, npartitions: %d\n",
1249                  num_online_nodes(), num_online_cpus(),
1250                  cfs_cpt_number(cfs_cpt_tab));
1251         return 0;
1252
1253 failed_alloc_table:
1254         put_online_cpus();
1255
1256         if (!IS_ERR_OR_NULL(cfs_cpt_tab))
1257                 cfs_cpt_table_free(cfs_cpt_tab);
1258
1259 #ifdef CONFIG_HOTPLUG_CPU
1260 #ifdef HAVE_HOTPLUG_STATE_MACHINE
1261         if (lustre_cpu_online > 0)
1262                 cpuhp_remove_state_nocalls(lustre_cpu_online);
1263 failed_cpu_online:
1264         cpuhp_remove_state_nocalls(CPUHP_LUSTRE_CFS_DEAD);
1265 failed_cpu_dead:
1266 #else
1267         unregister_hotcpu_notifier(&cfs_cpu_notifier);
1268 #endif /* !HAVE_HOTPLUG_STATE_MACHINE */
1269 #endif /* CONFIG_HOTPLUG_CPU */
1270         return ret;
1271 }