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