Documentation: embargoed-hardware-issues.rst: Add myself for Power
[sfrench/cifs-2.6.git] / fs / bcachefs / util.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * random utiility code, for bcache but in theory not specific to bcache
4  *
5  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6  * Copyright 2012 Google, Inc.
7  */
8
9 #include <linux/bio.h>
10 #include <linux/blkdev.h>
11 #include <linux/console.h>
12 #include <linux/ctype.h>
13 #include <linux/debugfs.h>
14 #include <linux/freezer.h>
15 #include <linux/kthread.h>
16 #include <linux/log2.h>
17 #include <linux/math64.h>
18 #include <linux/percpu.h>
19 #include <linux/preempt.h>
20 #include <linux/random.h>
21 #include <linux/seq_file.h>
22 #include <linux/string.h>
23 #include <linux/types.h>
24 #include <linux/sched/clock.h>
25
26 #include "eytzinger.h"
27 #include "mean_and_variance.h"
28 #include "util.h"
29
30 static const char si_units[] = "?kMGTPEZY";
31
32 /* string_get_size units: */
33 static const char *const units_2[] = {
34         "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
35 };
36 static const char *const units_10[] = {
37         "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
38 };
39
40 static int parse_u64(const char *cp, u64 *res)
41 {
42         const char *start = cp;
43         u64 v = 0;
44
45         if (!isdigit(*cp))
46                 return -EINVAL;
47
48         do {
49                 if (v > U64_MAX / 10)
50                         return -ERANGE;
51                 v *= 10;
52                 if (v > U64_MAX - (*cp - '0'))
53                         return -ERANGE;
54                 v += *cp - '0';
55                 cp++;
56         } while (isdigit(*cp));
57
58         *res = v;
59         return cp - start;
60 }
61
62 static int bch2_pow(u64 n, u64 p, u64 *res)
63 {
64         *res = 1;
65
66         while (p--) {
67                 if (*res > div_u64(U64_MAX, n))
68                         return -ERANGE;
69                 *res *= n;
70         }
71         return 0;
72 }
73
74 static int parse_unit_suffix(const char *cp, u64 *res)
75 {
76         const char *start = cp;
77         u64 base = 1024;
78         unsigned u;
79         int ret;
80
81         if (*cp == ' ')
82                 cp++;
83
84         for (u = 1; u < strlen(si_units); u++)
85                 if (*cp == si_units[u]) {
86                         cp++;
87                         goto got_unit;
88                 }
89
90         for (u = 0; u < ARRAY_SIZE(units_2); u++)
91                 if (!strncmp(cp, units_2[u], strlen(units_2[u]))) {
92                         cp += strlen(units_2[u]);
93                         goto got_unit;
94                 }
95
96         for (u = 0; u < ARRAY_SIZE(units_10); u++)
97                 if (!strncmp(cp, units_10[u], strlen(units_10[u]))) {
98                         cp += strlen(units_10[u]);
99                         base = 1000;
100                         goto got_unit;
101                 }
102
103         *res = 1;
104         return 0;
105 got_unit:
106         ret = bch2_pow(base, u, res);
107         if (ret)
108                 return ret;
109
110         return cp - start;
111 }
112
113 #define parse_or_ret(cp, _f)                    \
114 do {                                            \
115         int _ret = _f;                          \
116         if (_ret < 0)                           \
117                 return _ret;                    \
118         cp += _ret;                             \
119 } while (0)
120
121 static int __bch2_strtou64_h(const char *cp, u64 *res)
122 {
123         const char *start = cp;
124         u64 v = 0, b, f_n = 0, f_d = 1;
125         int ret;
126
127         parse_or_ret(cp, parse_u64(cp, &v));
128
129         if (*cp == '.') {
130                 cp++;
131                 ret = parse_u64(cp, &f_n);
132                 if (ret < 0)
133                         return ret;
134                 cp += ret;
135
136                 ret = bch2_pow(10, ret, &f_d);
137                 if (ret)
138                         return ret;
139         }
140
141         parse_or_ret(cp, parse_unit_suffix(cp, &b));
142
143         if (v > div_u64(U64_MAX, b))
144                 return -ERANGE;
145         v *= b;
146
147         if (f_n > div_u64(U64_MAX, b))
148                 return -ERANGE;
149
150         f_n = div_u64(f_n * b, f_d);
151         if (v + f_n < v)
152                 return -ERANGE;
153         v += f_n;
154
155         *res = v;
156         return cp - start;
157 }
158
159 static int __bch2_strtoh(const char *cp, u64 *res,
160                          u64 t_max, bool t_signed)
161 {
162         bool positive = *cp != '-';
163         u64 v = 0;
164
165         if (*cp == '+' || *cp == '-')
166                 cp++;
167
168         parse_or_ret(cp, __bch2_strtou64_h(cp, &v));
169
170         if (*cp == '\n')
171                 cp++;
172         if (*cp)
173                 return -EINVAL;
174
175         if (positive) {
176                 if (v > t_max)
177                         return -ERANGE;
178         } else {
179                 if (v && !t_signed)
180                         return -ERANGE;
181
182                 if (v > t_max + 1)
183                         return -ERANGE;
184                 v = -v;
185         }
186
187         *res = v;
188         return 0;
189 }
190
191 #define STRTO_H(name, type)                                     \
192 int bch2_ ## name ## _h(const char *cp, type *res)              \
193 {                                                               \
194         u64 v = 0;                                              \
195         int ret = __bch2_strtoh(cp, &v, ANYSINT_MAX(type),      \
196                         ANYSINT_MAX(type) != ((type) ~0ULL));   \
197         *res = v;                                               \
198         return ret;                                             \
199 }
200
201 STRTO_H(strtoint, int)
202 STRTO_H(strtouint, unsigned int)
203 STRTO_H(strtoll, long long)
204 STRTO_H(strtoull, unsigned long long)
205 STRTO_H(strtou64, u64)
206
207 u64 bch2_read_flag_list(char *opt, const char * const list[])
208 {
209         u64 ret = 0;
210         char *p, *s, *d = kstrdup(opt, GFP_KERNEL);
211
212         if (!d)
213                 return -ENOMEM;
214
215         s = strim(d);
216
217         while ((p = strsep(&s, ","))) {
218                 int flag = match_string(list, -1, p);
219
220                 if (flag < 0) {
221                         ret = -1;
222                         break;
223                 }
224
225                 ret |= 1 << flag;
226         }
227
228         kfree(d);
229
230         return ret;
231 }
232
233 bool bch2_is_zero(const void *_p, size_t n)
234 {
235         const char *p = _p;
236         size_t i;
237
238         for (i = 0; i < n; i++)
239                 if (p[i])
240                         return false;
241         return true;
242 }
243
244 void bch2_prt_u64_base2_nbits(struct printbuf *out, u64 v, unsigned nr_bits)
245 {
246         while (nr_bits)
247                 prt_char(out, '0' + ((v >> --nr_bits) & 1));
248 }
249
250 void bch2_prt_u64_base2(struct printbuf *out, u64 v)
251 {
252         bch2_prt_u64_base2_nbits(out, v, fls64(v) ?: 1);
253 }
254
255 void bch2_print_string_as_lines(const char *prefix, const char *lines)
256 {
257         const char *p;
258
259         if (!lines) {
260                 printk("%s (null)\n", prefix);
261                 return;
262         }
263
264         console_lock();
265         while (1) {
266                 p = strchrnul(lines, '\n');
267                 printk("%s%.*s\n", prefix, (int) (p - lines), lines);
268                 if (!*p)
269                         break;
270                 lines = p + 1;
271         }
272         console_unlock();
273 }
274
275 int bch2_save_backtrace(bch_stacktrace *stack, struct task_struct *task, unsigned skipnr,
276                         gfp_t gfp)
277 {
278 #ifdef CONFIG_STACKTRACE
279         unsigned nr_entries = 0;
280
281         stack->nr = 0;
282         int ret = darray_make_room_gfp(stack, 32, gfp);
283         if (ret)
284                 return ret;
285
286         if (!down_read_trylock(&task->signal->exec_update_lock))
287                 return -1;
288
289         do {
290                 nr_entries = stack_trace_save_tsk(task, stack->data, stack->size, skipnr + 1);
291         } while (nr_entries == stack->size &&
292                  !(ret = darray_make_room_gfp(stack, stack->size * 2, gfp)));
293
294         stack->nr = nr_entries;
295         up_read(&task->signal->exec_update_lock);
296
297         return ret;
298 #else
299         return 0;
300 #endif
301 }
302
303 void bch2_prt_backtrace(struct printbuf *out, bch_stacktrace *stack)
304 {
305         darray_for_each(*stack, i) {
306                 prt_printf(out, "[<0>] %pB", (void *) *i);
307                 prt_newline(out);
308         }
309 }
310
311 int bch2_prt_task_backtrace(struct printbuf *out, struct task_struct *task, unsigned skipnr, gfp_t gfp)
312 {
313         bch_stacktrace stack = { 0 };
314         int ret = bch2_save_backtrace(&stack, task, skipnr + 1, gfp);
315
316         bch2_prt_backtrace(out, &stack);
317         darray_exit(&stack);
318         return ret;
319 }
320
321 #ifndef __KERNEL__
322 #include <time.h>
323 void bch2_prt_datetime(struct printbuf *out, time64_t sec)
324 {
325         time_t t = sec;
326         char buf[64];
327         ctime_r(&t, buf);
328         strim(buf);
329         prt_str(out, buf);
330 }
331 #else
332 void bch2_prt_datetime(struct printbuf *out, time64_t sec)
333 {
334         char buf[64];
335         snprintf(buf, sizeof(buf), "%ptT", &sec);
336         prt_u64(out, sec);
337 }
338 #endif
339
340 void bch2_pr_time_units(struct printbuf *out, u64 ns)
341 {
342         const struct time_unit *u = bch2_pick_time_units(ns);
343
344         prt_printf(out, "%llu %s", div_u64(ns, u->nsecs), u->name);
345 }
346
347 static void bch2_pr_time_units_aligned(struct printbuf *out, u64 ns)
348 {
349         const struct time_unit *u = bch2_pick_time_units(ns);
350
351         prt_printf(out, "%llu ", div64_u64(ns, u->nsecs));
352         prt_tab_rjust(out);
353         prt_printf(out, "%s", u->name);
354 }
355
356 static inline void pr_name_and_units(struct printbuf *out, const char *name, u64 ns)
357 {
358         prt_str(out, name);
359         prt_tab(out);
360         bch2_pr_time_units_aligned(out, ns);
361         prt_newline(out);
362 }
363
364 #define TABSTOP_SIZE 12
365
366 void bch2_time_stats_to_text(struct printbuf *out, struct bch2_time_stats *stats)
367 {
368         struct quantiles *quantiles = time_stats_to_quantiles(stats);
369         s64 f_mean = 0, d_mean = 0;
370         u64 f_stddev = 0, d_stddev = 0;
371
372         if (stats->buffer) {
373                 int cpu;
374
375                 spin_lock_irq(&stats->lock);
376                 for_each_possible_cpu(cpu)
377                         __bch2_time_stats_clear_buffer(stats, per_cpu_ptr(stats->buffer, cpu));
378                 spin_unlock_irq(&stats->lock);
379         }
380
381         /*
382          * avoid divide by zero
383          */
384         if (stats->freq_stats.n) {
385                 f_mean = mean_and_variance_get_mean(stats->freq_stats);
386                 f_stddev = mean_and_variance_get_stddev(stats->freq_stats);
387                 d_mean = mean_and_variance_get_mean(stats->duration_stats);
388                 d_stddev = mean_and_variance_get_stddev(stats->duration_stats);
389         }
390
391         printbuf_tabstop_push(out, out->indent + TABSTOP_SIZE);
392         prt_printf(out, "count:");
393         prt_tab(out);
394         prt_printf(out, "%llu ",
395                          stats->duration_stats.n);
396         printbuf_tabstop_pop(out);
397         prt_newline(out);
398
399         printbuf_tabstops_reset(out);
400
401         printbuf_tabstop_push(out, out->indent + 20);
402         printbuf_tabstop_push(out, TABSTOP_SIZE + 2);
403         printbuf_tabstop_push(out, 0);
404         printbuf_tabstop_push(out, TABSTOP_SIZE + 2);
405
406         prt_tab(out);
407         prt_printf(out, "since mount");
408         prt_tab_rjust(out);
409         prt_tab(out);
410         prt_printf(out, "recent");
411         prt_tab_rjust(out);
412         prt_newline(out);
413
414         printbuf_tabstops_reset(out);
415         printbuf_tabstop_push(out, out->indent + 20);
416         printbuf_tabstop_push(out, TABSTOP_SIZE);
417         printbuf_tabstop_push(out, 2);
418         printbuf_tabstop_push(out, TABSTOP_SIZE);
419
420         prt_printf(out, "duration of events");
421         prt_newline(out);
422         printbuf_indent_add(out, 2);
423
424         pr_name_and_units(out, "min:", stats->min_duration);
425         pr_name_and_units(out, "max:", stats->max_duration);
426         pr_name_and_units(out, "total:", stats->total_duration);
427
428         prt_printf(out, "mean:");
429         prt_tab(out);
430         bch2_pr_time_units_aligned(out, d_mean);
431         prt_tab(out);
432         bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->duration_stats_weighted, TIME_STATS_MV_WEIGHT));
433         prt_newline(out);
434
435         prt_printf(out, "stddev:");
436         prt_tab(out);
437         bch2_pr_time_units_aligned(out, d_stddev);
438         prt_tab(out);
439         bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->duration_stats_weighted, TIME_STATS_MV_WEIGHT));
440
441         printbuf_indent_sub(out, 2);
442         prt_newline(out);
443
444         prt_printf(out, "time between events");
445         prt_newline(out);
446         printbuf_indent_add(out, 2);
447
448         pr_name_and_units(out, "min:", stats->min_freq);
449         pr_name_and_units(out, "max:", stats->max_freq);
450
451         prt_printf(out, "mean:");
452         prt_tab(out);
453         bch2_pr_time_units_aligned(out, f_mean);
454         prt_tab(out);
455         bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->freq_stats_weighted, TIME_STATS_MV_WEIGHT));
456         prt_newline(out);
457
458         prt_printf(out, "stddev:");
459         prt_tab(out);
460         bch2_pr_time_units_aligned(out, f_stddev);
461         prt_tab(out);
462         bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->freq_stats_weighted, TIME_STATS_MV_WEIGHT));
463
464         printbuf_indent_sub(out, 2);
465         prt_newline(out);
466
467         printbuf_tabstops_reset(out);
468
469         if (quantiles) {
470                 int i = eytzinger0_first(NR_QUANTILES);
471                 const struct time_unit *u =
472                         bch2_pick_time_units(quantiles->entries[i].m);
473                 u64 last_q = 0;
474
475                 prt_printf(out, "quantiles (%s):\t", u->name);
476                 eytzinger0_for_each(i, NR_QUANTILES) {
477                         bool is_last = eytzinger0_next(i, NR_QUANTILES) == -1;
478
479                         u64 q = max(quantiles->entries[i].m, last_q);
480                         prt_printf(out, "%llu ", div_u64(q, u->nsecs));
481                         if (is_last)
482                                 prt_newline(out);
483                         last_q = q;
484                 }
485         }
486 }
487
488 /* ratelimit: */
489
490 /**
491  * bch2_ratelimit_delay() - return how long to delay until the next time to do
492  *              some work
493  * @d:          the struct bch_ratelimit to update
494  * Returns:     the amount of time to delay by, in jiffies
495  */
496 u64 bch2_ratelimit_delay(struct bch_ratelimit *d)
497 {
498         u64 now = local_clock();
499
500         return time_after64(d->next, now)
501                 ? nsecs_to_jiffies(d->next - now)
502                 : 0;
503 }
504
505 /**
506  * bch2_ratelimit_increment() - increment @d by the amount of work done
507  * @d:          the struct bch_ratelimit to update
508  * @done:       the amount of work done, in arbitrary units
509  */
510 void bch2_ratelimit_increment(struct bch_ratelimit *d, u64 done)
511 {
512         u64 now = local_clock();
513
514         d->next += div_u64(done * NSEC_PER_SEC, d->rate);
515
516         if (time_before64(now + NSEC_PER_SEC, d->next))
517                 d->next = now + NSEC_PER_SEC;
518
519         if (time_after64(now - NSEC_PER_SEC * 2, d->next))
520                 d->next = now - NSEC_PER_SEC * 2;
521 }
522
523 /* pd controller: */
524
525 /*
526  * Updates pd_controller. Attempts to scale inputed values to units per second.
527  * @target: desired value
528  * @actual: current value
529  *
530  * @sign: 1 or -1; 1 if increasing the rate makes actual go up, -1 if increasing
531  * it makes actual go down.
532  */
533 void bch2_pd_controller_update(struct bch_pd_controller *pd,
534                               s64 target, s64 actual, int sign)
535 {
536         s64 proportional, derivative, change;
537
538         unsigned long seconds_since_update = (jiffies - pd->last_update) / HZ;
539
540         if (seconds_since_update == 0)
541                 return;
542
543         pd->last_update = jiffies;
544
545         proportional = actual - target;
546         proportional *= seconds_since_update;
547         proportional = div_s64(proportional, pd->p_term_inverse);
548
549         derivative = actual - pd->last_actual;
550         derivative = div_s64(derivative, seconds_since_update);
551         derivative = ewma_add(pd->smoothed_derivative, derivative,
552                               (pd->d_term / seconds_since_update) ?: 1);
553         derivative = derivative * pd->d_term;
554         derivative = div_s64(derivative, pd->p_term_inverse);
555
556         change = proportional + derivative;
557
558         /* Don't increase rate if not keeping up */
559         if (change > 0 &&
560             pd->backpressure &&
561             time_after64(local_clock(),
562                          pd->rate.next + NSEC_PER_MSEC))
563                 change = 0;
564
565         change *= (sign * -1);
566
567         pd->rate.rate = clamp_t(s64, (s64) pd->rate.rate + change,
568                                 1, UINT_MAX);
569
570         pd->last_actual         = actual;
571         pd->last_derivative     = derivative;
572         pd->last_proportional   = proportional;
573         pd->last_change         = change;
574         pd->last_target         = target;
575 }
576
577 void bch2_pd_controller_init(struct bch_pd_controller *pd)
578 {
579         pd->rate.rate           = 1024;
580         pd->last_update         = jiffies;
581         pd->p_term_inverse      = 6000;
582         pd->d_term              = 30;
583         pd->d_smooth            = pd->d_term;
584         pd->backpressure        = 1;
585 }
586
587 void bch2_pd_controller_debug_to_text(struct printbuf *out, struct bch_pd_controller *pd)
588 {
589         if (!out->nr_tabstops)
590                 printbuf_tabstop_push(out, 20);
591
592         prt_printf(out, "rate:");
593         prt_tab(out);
594         prt_human_readable_s64(out, pd->rate.rate);
595         prt_newline(out);
596
597         prt_printf(out, "target:");
598         prt_tab(out);
599         prt_human_readable_u64(out, pd->last_target);
600         prt_newline(out);
601
602         prt_printf(out, "actual:");
603         prt_tab(out);
604         prt_human_readable_u64(out, pd->last_actual);
605         prt_newline(out);
606
607         prt_printf(out, "proportional:");
608         prt_tab(out);
609         prt_human_readable_s64(out, pd->last_proportional);
610         prt_newline(out);
611
612         prt_printf(out, "derivative:");
613         prt_tab(out);
614         prt_human_readable_s64(out, pd->last_derivative);
615         prt_newline(out);
616
617         prt_printf(out, "change:");
618         prt_tab(out);
619         prt_human_readable_s64(out, pd->last_change);
620         prt_newline(out);
621
622         prt_printf(out, "next io:");
623         prt_tab(out);
624         prt_printf(out, "%llims", div64_s64(pd->rate.next - local_clock(), NSEC_PER_MSEC));
625         prt_newline(out);
626 }
627
628 /* misc: */
629
630 void bch2_bio_map(struct bio *bio, void *base, size_t size)
631 {
632         while (size) {
633                 struct page *page = is_vmalloc_addr(base)
634                                 ? vmalloc_to_page(base)
635                                 : virt_to_page(base);
636                 unsigned offset = offset_in_page(base);
637                 unsigned len = min_t(size_t, PAGE_SIZE - offset, size);
638
639                 BUG_ON(!bio_add_page(bio, page, len, offset));
640                 size -= len;
641                 base += len;
642         }
643 }
644
645 int bch2_bio_alloc_pages(struct bio *bio, size_t size, gfp_t gfp_mask)
646 {
647         while (size) {
648                 struct page *page = alloc_pages(gfp_mask, 0);
649                 unsigned len = min_t(size_t, PAGE_SIZE, size);
650
651                 if (!page)
652                         return -ENOMEM;
653
654                 if (unlikely(!bio_add_page(bio, page, len, 0))) {
655                         __free_page(page);
656                         break;
657                 }
658
659                 size -= len;
660         }
661
662         return 0;
663 }
664
665 size_t bch2_rand_range(size_t max)
666 {
667         size_t rand;
668
669         if (!max)
670                 return 0;
671
672         do {
673                 rand = get_random_long();
674                 rand &= roundup_pow_of_two(max) - 1;
675         } while (rand >= max);
676
677         return rand;
678 }
679
680 void memcpy_to_bio(struct bio *dst, struct bvec_iter dst_iter, const void *src)
681 {
682         struct bio_vec bv;
683         struct bvec_iter iter;
684
685         __bio_for_each_segment(bv, dst, iter, dst_iter) {
686                 void *dstp = kmap_local_page(bv.bv_page);
687
688                 memcpy(dstp + bv.bv_offset, src, bv.bv_len);
689                 kunmap_local(dstp);
690
691                 src += bv.bv_len;
692         }
693 }
694
695 void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter)
696 {
697         struct bio_vec bv;
698         struct bvec_iter iter;
699
700         __bio_for_each_segment(bv, src, iter, src_iter) {
701                 void *srcp = kmap_local_page(bv.bv_page);
702
703                 memcpy(dst, srcp + bv.bv_offset, bv.bv_len);
704                 kunmap_local(srcp);
705
706                 dst += bv.bv_len;
707         }
708 }
709
710 static int alignment_ok(const void *base, size_t align)
711 {
712         return IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
713                 ((unsigned long)base & (align - 1)) == 0;
714 }
715
716 static void u32_swap(void *a, void *b, size_t size)
717 {
718         u32 t = *(u32 *)a;
719         *(u32 *)a = *(u32 *)b;
720         *(u32 *)b = t;
721 }
722
723 static void u64_swap(void *a, void *b, size_t size)
724 {
725         u64 t = *(u64 *)a;
726         *(u64 *)a = *(u64 *)b;
727         *(u64 *)b = t;
728 }
729
730 static void generic_swap(void *a, void *b, size_t size)
731 {
732         char t;
733
734         do {
735                 t = *(char *)a;
736                 *(char *)a++ = *(char *)b;
737                 *(char *)b++ = t;
738         } while (--size > 0);
739 }
740
741 static inline int do_cmp(void *base, size_t n, size_t size,
742                          int (*cmp_func)(const void *, const void *, size_t),
743                          size_t l, size_t r)
744 {
745         return cmp_func(base + inorder_to_eytzinger0(l, n) * size,
746                         base + inorder_to_eytzinger0(r, n) * size,
747                         size);
748 }
749
750 static inline void do_swap(void *base, size_t n, size_t size,
751                            void (*swap_func)(void *, void *, size_t),
752                            size_t l, size_t r)
753 {
754         swap_func(base + inorder_to_eytzinger0(l, n) * size,
755                   base + inorder_to_eytzinger0(r, n) * size,
756                   size);
757 }
758
759 void eytzinger0_sort(void *base, size_t n, size_t size,
760                      int (*cmp_func)(const void *, const void *, size_t),
761                      void (*swap_func)(void *, void *, size_t))
762 {
763         int i, c, r;
764
765         if (!swap_func) {
766                 if (size == 4 && alignment_ok(base, 4))
767                         swap_func = u32_swap;
768                 else if (size == 8 && alignment_ok(base, 8))
769                         swap_func = u64_swap;
770                 else
771                         swap_func = generic_swap;
772         }
773
774         /* heapify */
775         for (i = n / 2 - 1; i >= 0; --i) {
776                 for (r = i; r * 2 + 1 < n; r = c) {
777                         c = r * 2 + 1;
778
779                         if (c + 1 < n &&
780                             do_cmp(base, n, size, cmp_func, c, c + 1) < 0)
781                                 c++;
782
783                         if (do_cmp(base, n, size, cmp_func, r, c) >= 0)
784                                 break;
785
786                         do_swap(base, n, size, swap_func, r, c);
787                 }
788         }
789
790         /* sort */
791         for (i = n - 1; i > 0; --i) {
792                 do_swap(base, n, size, swap_func, 0, i);
793
794                 for (r = 0; r * 2 + 1 < i; r = c) {
795                         c = r * 2 + 1;
796
797                         if (c + 1 < i &&
798                             do_cmp(base, n, size, cmp_func, c, c + 1) < 0)
799                                 c++;
800
801                         if (do_cmp(base, n, size, cmp_func, r, c) >= 0)
802                                 break;
803
804                         do_swap(base, n, size, swap_func, r, c);
805                 }
806         }
807 }
808
809 void sort_cmp_size(void *base, size_t num, size_t size,
810           int (*cmp_func)(const void *, const void *, size_t),
811           void (*swap_func)(void *, void *, size_t size))
812 {
813         /* pre-scale counters for performance */
814         int i = (num/2 - 1) * size, n = num * size, c, r;
815
816         if (!swap_func) {
817                 if (size == 4 && alignment_ok(base, 4))
818                         swap_func = u32_swap;
819                 else if (size == 8 && alignment_ok(base, 8))
820                         swap_func = u64_swap;
821                 else
822                         swap_func = generic_swap;
823         }
824
825         /* heapify */
826         for ( ; i >= 0; i -= size) {
827                 for (r = i; r * 2 + size < n; r  = c) {
828                         c = r * 2 + size;
829                         if (c < n - size &&
830                             cmp_func(base + c, base + c + size, size) < 0)
831                                 c += size;
832                         if (cmp_func(base + r, base + c, size) >= 0)
833                                 break;
834                         swap_func(base + r, base + c, size);
835                 }
836         }
837
838         /* sort */
839         for (i = n - size; i > 0; i -= size) {
840                 swap_func(base, base + i, size);
841                 for (r = 0; r * 2 + size < i; r = c) {
842                         c = r * 2 + size;
843                         if (c < i - size &&
844                             cmp_func(base + c, base + c + size, size) < 0)
845                                 c += size;
846                         if (cmp_func(base + r, base + c, size) >= 0)
847                                 break;
848                         swap_func(base + r, base + c, size);
849                 }
850         }
851 }
852
853 #if 0
854 void eytzinger1_test(void)
855 {
856         unsigned inorder, eytz, size;
857
858         pr_info("1 based eytzinger test:");
859
860         for (size = 2;
861              size < 65536;
862              size++) {
863                 unsigned extra = eytzinger1_extra(size);
864
865                 if (!(size % 4096))
866                         pr_info("tree size %u", size);
867
868                 BUG_ON(eytzinger1_prev(0, size) != eytzinger1_last(size));
869                 BUG_ON(eytzinger1_next(0, size) != eytzinger1_first(size));
870
871                 BUG_ON(eytzinger1_prev(eytzinger1_first(size), size)    != 0);
872                 BUG_ON(eytzinger1_next(eytzinger1_last(size), size)     != 0);
873
874                 inorder = 1;
875                 eytzinger1_for_each(eytz, size) {
876                         BUG_ON(__inorder_to_eytzinger1(inorder, size, extra) != eytz);
877                         BUG_ON(__eytzinger1_to_inorder(eytz, size, extra) != inorder);
878                         BUG_ON(eytz != eytzinger1_last(size) &&
879                                eytzinger1_prev(eytzinger1_next(eytz, size), size) != eytz);
880
881                         inorder++;
882                 }
883         }
884 }
885
886 void eytzinger0_test(void)
887 {
888
889         unsigned inorder, eytz, size;
890
891         pr_info("0 based eytzinger test:");
892
893         for (size = 1;
894              size < 65536;
895              size++) {
896                 unsigned extra = eytzinger0_extra(size);
897
898                 if (!(size % 4096))
899                         pr_info("tree size %u", size);
900
901                 BUG_ON(eytzinger0_prev(-1, size) != eytzinger0_last(size));
902                 BUG_ON(eytzinger0_next(-1, size) != eytzinger0_first(size));
903
904                 BUG_ON(eytzinger0_prev(eytzinger0_first(size), size)    != -1);
905                 BUG_ON(eytzinger0_next(eytzinger0_last(size), size)     != -1);
906
907                 inorder = 0;
908                 eytzinger0_for_each(eytz, size) {
909                         BUG_ON(__inorder_to_eytzinger0(inorder, size, extra) != eytz);
910                         BUG_ON(__eytzinger0_to_inorder(eytz, size, extra) != inorder);
911                         BUG_ON(eytz != eytzinger0_last(size) &&
912                                eytzinger0_prev(eytzinger0_next(eytz, size), size) != eytz);
913
914                         inorder++;
915                 }
916         }
917 }
918
919 static inline int cmp_u16(const void *_l, const void *_r, size_t size)
920 {
921         const u16 *l = _l, *r = _r;
922
923         return (*l > *r) - (*r - *l);
924 }
925
926 static void eytzinger0_find_test_val(u16 *test_array, unsigned nr, u16 search)
927 {
928         int i, c1 = -1, c2 = -1;
929         ssize_t r;
930
931         r = eytzinger0_find_le(test_array, nr,
932                                sizeof(test_array[0]),
933                                cmp_u16, &search);
934         if (r >= 0)
935                 c1 = test_array[r];
936
937         for (i = 0; i < nr; i++)
938                 if (test_array[i] <= search && test_array[i] > c2)
939                         c2 = test_array[i];
940
941         if (c1 != c2) {
942                 eytzinger0_for_each(i, nr)
943                         pr_info("[%3u] = %12u", i, test_array[i]);
944                 pr_info("find_le(%2u) -> [%2zi] = %2i should be %2i",
945                         i, r, c1, c2);
946         }
947 }
948
949 void eytzinger0_find_test(void)
950 {
951         unsigned i, nr, allocated = 1 << 12;
952         u16 *test_array = kmalloc_array(allocated, sizeof(test_array[0]), GFP_KERNEL);
953
954         for (nr = 1; nr < allocated; nr++) {
955                 pr_info("testing %u elems", nr);
956
957                 get_random_bytes(test_array, nr * sizeof(test_array[0]));
958                 eytzinger0_sort(test_array, nr, sizeof(test_array[0]), cmp_u16, NULL);
959
960                 /* verify array is sorted correctly: */
961                 eytzinger0_for_each(i, nr)
962                         BUG_ON(i != eytzinger0_last(nr) &&
963                                test_array[i] > test_array[eytzinger0_next(i, nr)]);
964
965                 for (i = 0; i < U16_MAX; i += 1 << 12)
966                         eytzinger0_find_test_val(test_array, nr, i);
967
968                 for (i = 0; i < nr; i++) {
969                         eytzinger0_find_test_val(test_array, nr, test_array[i] - 1);
970                         eytzinger0_find_test_val(test_array, nr, test_array[i]);
971                         eytzinger0_find_test_val(test_array, nr, test_array[i] + 1);
972                 }
973         }
974
975         kfree(test_array);
976 }
977 #endif
978
979 /*
980  * Accumulate percpu counters onto one cpu's copy - only valid when access
981  * against any percpu counter is guarded against
982  */
983 u64 *bch2_acc_percpu_u64s(u64 __percpu *p, unsigned nr)
984 {
985         u64 *ret;
986         int cpu;
987
988         /* access to pcpu vars has to be blocked by other locking */
989         preempt_disable();
990         ret = this_cpu_ptr(p);
991         preempt_enable();
992
993         for_each_possible_cpu(cpu) {
994                 u64 *i = per_cpu_ptr(p, cpu);
995
996                 if (i != ret) {
997                         acc_u64s(ret, i, nr);
998                         memset(i, 0, nr * sizeof(u64));
999                 }
1000         }
1001
1002         return ret;
1003 }
1004
1005 void bch2_darray_str_exit(darray_str *d)
1006 {
1007         darray_for_each(*d, i)
1008                 kfree(*i);
1009         darray_exit(d);
1010 }
1011
1012 int bch2_split_devs(const char *_dev_name, darray_str *ret)
1013 {
1014         darray_init(ret);
1015
1016         char *dev_name, *s, *orig;
1017
1018         dev_name = orig = kstrdup(_dev_name, GFP_KERNEL);
1019         if (!dev_name)
1020                 return -ENOMEM;
1021
1022         while ((s = strsep(&dev_name, ":"))) {
1023                 char *p = kstrdup(s, GFP_KERNEL);
1024                 if (!p)
1025                         goto err;
1026
1027                 if (darray_push(ret, p)) {
1028                         kfree(p);
1029                         goto err;
1030                 }
1031         }
1032
1033         kfree(orig);
1034         return 0;
1035 err:
1036         bch2_darray_str_exit(ret);
1037         kfree(orig);
1038         return -ENOMEM;
1039 }