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