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