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