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