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