]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/util.c
Update bcachefs sources to 99750eab4d bcachefs: Persist stripe blocks_used
[bcachefs-tools-debian] / libbcachefs / util.c
1 /*
2  * random utiility code, for bcache but in theory not specific to bcache
3  *
4  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5  * Copyright 2012 Google, Inc.
6  */
7
8 #include <linux/bio.h>
9 #include <linux/blkdev.h>
10 #include <linux/ctype.h>
11 #include <linux/debugfs.h>
12 #include <linux/freezer.h>
13 #include <linux/kthread.h>
14 #include <linux/log2.h>
15 #include <linux/math64.h>
16 #include <linux/percpu.h>
17 #include <linux/preempt.h>
18 #include <linux/random.h>
19 #include <linux/seq_file.h>
20 #include <linux/string.h>
21 #include <linux/types.h>
22 #include <linux/sched/clock.h>
23
24 #include "eytzinger.h"
25 #include "util.h"
26
27 static const char si_units[] = "?kMGTPEZY";
28
29 static int __bch2_strtoh(const char *cp, u64 *res,
30                          u64 t_max, bool t_signed)
31 {
32         bool positive = *cp != '-';
33         unsigned u;
34         u64 v = 0;
35
36         if (*cp == '+' || *cp == '-')
37                 cp++;
38
39         if (!isdigit(*cp))
40                 return -EINVAL;
41
42         do {
43                 if (v > U64_MAX / 10)
44                         return -ERANGE;
45                 v *= 10;
46                 if (v > U64_MAX - (*cp - '0'))
47                         return -ERANGE;
48                 v += *cp - '0';
49                 cp++;
50         } while (isdigit(*cp));
51
52         for (u = 1; u < strlen(si_units); u++)
53                 if (*cp == si_units[u]) {
54                         cp++;
55                         goto got_unit;
56                 }
57         u = 0;
58 got_unit:
59         if (*cp == '\n')
60                 cp++;
61         if (*cp)
62                 return -EINVAL;
63
64         if (fls64(v) + u * 10 > 64)
65                 return -ERANGE;
66
67         v <<= u * 10;
68
69         if (positive) {
70                 if (v > t_max)
71                         return -ERANGE;
72         } else {
73                 if (v && !t_signed)
74                         return -ERANGE;
75
76                 if (v > t_max + 1)
77                         return -ERANGE;
78                 v = -v;
79         }
80
81         *res = v;
82         return 0;
83 }
84
85 #define STRTO_H(name, type)                                     \
86 int bch2_ ## name ## _h(const char *cp, type *res)              \
87 {                                                               \
88         u64 v;                                                  \
89         int ret = __bch2_strtoh(cp, &v, ANYSINT_MAX(type),      \
90                         ANYSINT_MAX(type) != ((type) ~0ULL));   \
91         *res = v;                                               \
92         return ret;                                             \
93 }
94
95 STRTO_H(strtoint, int)
96 STRTO_H(strtouint, unsigned int)
97 STRTO_H(strtoll, long long)
98 STRTO_H(strtoull, unsigned long long)
99 STRTO_H(strtou64, u64)
100
101 void bch2_hprint(struct printbuf *buf, s64 v)
102 {
103         int u, t = 0;
104
105         for (u = 0; v >= 1024 || v <= -1024; u++) {
106                 t = v & ~(~0U << 10);
107                 v >>= 10;
108         }
109
110         pr_buf(buf, "%lli", v);
111
112         /*
113          * 103 is magic: t is in the range [-1023, 1023] and we want
114          * to turn it into [-9, 9]
115          */
116         if (u && v < 100 && v > -100)
117                 pr_buf(buf, ".%i", t / 103);
118         if (u)
119                 pr_buf(buf, "%c", si_units[u]);
120 }
121
122 void bch2_string_opt_to_text(struct printbuf *out,
123                              const char * const list[],
124                              size_t selected)
125 {
126         size_t i;
127
128         for (i = 0; list[i]; i++)
129                 pr_buf(out, i == selected ? "[%s] " : "%s ", list[i]);
130 }
131
132 void bch2_flags_to_text(struct printbuf *out,
133                         const char * const list[], u64 flags)
134 {
135         unsigned bit, nr = 0;
136         bool first = true;
137
138         if (out->pos != out->end)
139                 *out->pos = '\0';
140
141         while (list[nr])
142                 nr++;
143
144         while (flags && (bit = __ffs(flags)) < nr) {
145                 pr_buf(out, "%s", list[bit]);
146                 if (!first)
147                         pr_buf(out, ",");
148                 first = false;
149                 flags ^= 1 << bit;
150         }
151 }
152
153 u64 bch2_read_flag_list(char *opt, const char * const list[])
154 {
155         u64 ret = 0;
156         char *p, *s, *d = kstrndup(opt, PAGE_SIZE - 1, GFP_KERNEL);
157
158         if (!d)
159                 return -ENOMEM;
160
161         s = strim(d);
162
163         while ((p = strsep(&s, ","))) {
164                 int flag = match_string(list, -1, p);
165                 if (flag < 0) {
166                         ret = -1;
167                         break;
168                 }
169
170                 ret |= 1 << flag;
171         }
172
173         kfree(d);
174
175         return ret;
176 }
177
178 bool bch2_is_zero(const void *_p, size_t n)
179 {
180         const char *p = _p;
181         size_t i;
182
183         for (i = 0; i < n; i++)
184                 if (p[i])
185                         return false;
186         return true;
187 }
188
189 static void bch2_quantiles_update(struct quantiles *q, u64 v)
190 {
191         unsigned i = 0;
192
193         while (i < ARRAY_SIZE(q->entries)) {
194                 struct quantile_entry *e = q->entries + i;
195
196                 if (unlikely(!e->step)) {
197                         e->m = v;
198                         e->step = max_t(unsigned, v / 2, 1024);
199                 } else if (e->m > v) {
200                         e->m = e->m >= e->step
201                                 ? e->m - e->step
202                                 : 0;
203                 } else if (e->m < v) {
204                         e->m = e->m + e->step > e->m
205                                 ? e->m + e->step
206                                 : U32_MAX;
207                 }
208
209                 if ((e->m > v ? e->m - v : v - e->m) < e->step)
210                         e->step = max_t(unsigned, e->step / 2, 1);
211
212                 if (v >= e->m)
213                         break;
214
215                 i = eytzinger0_child(i, v > e->m);
216         }
217 }
218
219 /* time stats: */
220
221 static void bch2_time_stats_update_one(struct time_stats *stats,
222                                        u64 start, u64 end)
223 {
224         u64 duration, freq;
225
226         duration        = time_after64(end, start)
227                 ? end - start : 0;
228         freq            = time_after64(end, stats->last_event)
229                 ? end - stats->last_event : 0;
230
231         stats->count++;
232
233         stats->average_duration = stats->average_duration
234                 ? ewma_add(stats->average_duration, duration, 6)
235                 : duration;
236
237         stats->average_frequency = stats->average_frequency
238                 ? ewma_add(stats->average_frequency, freq, 6)
239                 : freq;
240
241         stats->max_duration = max(stats->max_duration, duration);
242
243         stats->last_event = end;
244
245         bch2_quantiles_update(&stats->quantiles, duration);
246 }
247
248 void __bch2_time_stats_update(struct time_stats *stats, u64 start, u64 end)
249 {
250         unsigned long flags;
251
252         if (!stats->buffer) {
253                 spin_lock_irqsave(&stats->lock, flags);
254                 bch2_time_stats_update_one(stats, start, end);
255
256                 if (stats->average_frequency < 32 &&
257                     stats->count > 1024)
258                         stats->buffer =
259                                 alloc_percpu_gfp(struct time_stat_buffer,
260                                                  GFP_ATOMIC);
261                 spin_unlock_irqrestore(&stats->lock, flags);
262         } else {
263                 struct time_stat_buffer_entry *i;
264                 struct time_stat_buffer *b;
265
266                 preempt_disable();
267                 b = this_cpu_ptr(stats->buffer);
268
269                 BUG_ON(b->nr >= ARRAY_SIZE(b->entries));
270                 b->entries[b->nr++] = (struct time_stat_buffer_entry) {
271                         .start = start,
272                         .end = end
273                 };
274
275                 if (b->nr == ARRAY_SIZE(b->entries)) {
276                         spin_lock_irqsave(&stats->lock, flags);
277                         for (i = b->entries;
278                              i < b->entries + ARRAY_SIZE(b->entries);
279                              i++)
280                                 bch2_time_stats_update_one(stats, i->start, i->end);
281                         spin_unlock_irqrestore(&stats->lock, flags);
282
283                         b->nr = 0;
284                 }
285
286                 preempt_enable();
287         }
288 }
289
290 static const struct time_unit {
291         const char      *name;
292         u32             nsecs;
293 } time_units[] = {
294         { "ns",         1               },
295         { "us",         NSEC_PER_USEC   },
296         { "ms",         NSEC_PER_MSEC   },
297         { "sec",        NSEC_PER_SEC    },
298 };
299
300 static const struct time_unit *pick_time_units(u64 ns)
301 {
302         const struct time_unit *u;
303
304         for (u = time_units;
305              u + 1 < time_units + ARRAY_SIZE(time_units) &&
306              ns >= u[1].nsecs << 1;
307              u++)
308                 ;
309
310         return u;
311 }
312
313 static void pr_time_units(struct printbuf *out, u64 ns)
314 {
315         const struct time_unit *u = pick_time_units(ns);
316
317         pr_buf(out, "%llu %s", div_u64(ns, u->nsecs), u->name);
318 }
319
320 size_t bch2_time_stats_print(struct time_stats *stats, char *buf, size_t len)
321 {
322         struct printbuf out = _PBUF(buf, len);
323         const struct time_unit *u;
324         u64 freq = READ_ONCE(stats->average_frequency);
325         u64 q, last_q = 0;
326         int i;
327
328         pr_buf(&out, "count:\t\t%llu\n",
329                          stats->count);
330         pr_buf(&out, "rate:\t\t%llu/sec\n",
331                freq ?  div64_u64(NSEC_PER_SEC, freq) : 0);
332
333         pr_buf(&out, "frequency:\t");
334         pr_time_units(&out, freq);
335
336         pr_buf(&out, "\navg duration:\t");
337         pr_time_units(&out, stats->average_duration);
338
339         pr_buf(&out, "\nmax duration:\t");
340         pr_time_units(&out, stats->max_duration);
341
342         i = eytzinger0_first(NR_QUANTILES);
343         u = pick_time_units(stats->quantiles.entries[i].m);
344
345         pr_buf(&out, "\nquantiles (%s):\t", u->name);
346         eytzinger0_for_each(i, NR_QUANTILES) {
347                 bool is_last = eytzinger0_next(i, NR_QUANTILES) == -1;
348
349                 q = max(stats->quantiles.entries[i].m, last_q);
350                 pr_buf(&out, "%llu%s",
351                        div_u64(q, u->nsecs),
352                        is_last ? "\n" : " ");
353                 last_q = q;
354         }
355
356         return out.pos - buf;
357 }
358
359 void bch2_time_stats_exit(struct time_stats *stats)
360 {
361         free_percpu(stats->buffer);
362 }
363
364 void bch2_time_stats_init(struct time_stats *stats)
365 {
366         memset(stats, 0, sizeof(*stats));
367         spin_lock_init(&stats->lock);
368 }
369
370 /* ratelimit: */
371
372 /**
373  * bch2_ratelimit_delay() - return how long to delay until the next time to do
374  * some work
375  *
376  * @d - the struct bch_ratelimit to update
377  *
378  * Returns the amount of time to delay by, in jiffies
379  */
380 u64 bch2_ratelimit_delay(struct bch_ratelimit *d)
381 {
382         u64 now = local_clock();
383
384         return time_after64(d->next, now)
385                 ? nsecs_to_jiffies(d->next - now)
386                 : 0;
387 }
388
389 /**
390  * bch2_ratelimit_increment() - increment @d by the amount of work done
391  *
392  * @d - the struct bch_ratelimit to update
393  * @done - the amount of work done, in arbitrary units
394  */
395 void bch2_ratelimit_increment(struct bch_ratelimit *d, u64 done)
396 {
397         u64 now = local_clock();
398
399         d->next += div_u64(done * NSEC_PER_SEC, d->rate);
400
401         if (time_before64(now + NSEC_PER_SEC, d->next))
402                 d->next = now + NSEC_PER_SEC;
403
404         if (time_after64(now - NSEC_PER_SEC * 2, d->next))
405                 d->next = now - NSEC_PER_SEC * 2;
406 }
407
408 /* pd controller: */
409
410 /*
411  * Updates pd_controller. Attempts to scale inputed values to units per second.
412  * @target: desired value
413  * @actual: current value
414  *
415  * @sign: 1 or -1; 1 if increasing the rate makes actual go up, -1 if increasing
416  * it makes actual go down.
417  */
418 void bch2_pd_controller_update(struct bch_pd_controller *pd,
419                               s64 target, s64 actual, int sign)
420 {
421         s64 proportional, derivative, change;
422
423         unsigned long seconds_since_update = (jiffies - pd->last_update) / HZ;
424
425         if (seconds_since_update == 0)
426                 return;
427
428         pd->last_update = jiffies;
429
430         proportional = actual - target;
431         proportional *= seconds_since_update;
432         proportional = div_s64(proportional, pd->p_term_inverse);
433
434         derivative = actual - pd->last_actual;
435         derivative = div_s64(derivative, seconds_since_update);
436         derivative = ewma_add(pd->smoothed_derivative, derivative,
437                               (pd->d_term / seconds_since_update) ?: 1);
438         derivative = derivative * pd->d_term;
439         derivative = div_s64(derivative, pd->p_term_inverse);
440
441         change = proportional + derivative;
442
443         /* Don't increase rate if not keeping up */
444         if (change > 0 &&
445             pd->backpressure &&
446             time_after64(local_clock(),
447                          pd->rate.next + NSEC_PER_MSEC))
448                 change = 0;
449
450         change *= (sign * -1);
451
452         pd->rate.rate = clamp_t(s64, (s64) pd->rate.rate + change,
453                                 1, UINT_MAX);
454
455         pd->last_actual         = actual;
456         pd->last_derivative     = derivative;
457         pd->last_proportional   = proportional;
458         pd->last_change         = change;
459         pd->last_target         = target;
460 }
461
462 void bch2_pd_controller_init(struct bch_pd_controller *pd)
463 {
464         pd->rate.rate           = 1024;
465         pd->last_update         = jiffies;
466         pd->p_term_inverse      = 6000;
467         pd->d_term              = 30;
468         pd->d_smooth            = pd->d_term;
469         pd->backpressure        = 1;
470 }
471
472 size_t bch2_pd_controller_print_debug(struct bch_pd_controller *pd, char *buf)
473 {
474         /* 2^64 - 1 is 20 digits, plus null byte */
475         char rate[21];
476         char actual[21];
477         char target[21];
478         char proportional[21];
479         char derivative[21];
480         char change[21];
481         s64 next_io;
482
483         bch2_hprint(&PBUF(rate),        pd->rate.rate);
484         bch2_hprint(&PBUF(actual),      pd->last_actual);
485         bch2_hprint(&PBUF(target),      pd->last_target);
486         bch2_hprint(&PBUF(proportional), pd->last_proportional);
487         bch2_hprint(&PBUF(derivative),  pd->last_derivative);
488         bch2_hprint(&PBUF(change),      pd->last_change);
489
490         next_io = div64_s64(pd->rate.next - local_clock(), NSEC_PER_MSEC);
491
492         return sprintf(buf,
493                        "rate:\t\t%s/sec\n"
494                        "target:\t\t%s\n"
495                        "actual:\t\t%s\n"
496                        "proportional:\t%s\n"
497                        "derivative:\t%s\n"
498                        "change:\t\t%s/sec\n"
499                        "next io:\t%llims\n",
500                        rate, target, actual, proportional,
501                        derivative, change, next_io);
502 }
503
504 /* misc: */
505
506 void bch2_bio_map(struct bio *bio, void *base)
507 {
508         size_t size = bio->bi_iter.bi_size;
509         struct bio_vec *bv = bio->bi_io_vec;
510
511         BUG_ON(!bio->bi_iter.bi_size);
512         BUG_ON(bio->bi_vcnt);
513         BUG_ON(!bio->bi_max_vecs);
514
515         bv->bv_offset = base ? offset_in_page(base) : 0;
516         goto start;
517
518         for (; size; bio->bi_vcnt++, bv++) {
519                 BUG_ON(bio->bi_vcnt >= bio->bi_max_vecs);
520
521                 bv->bv_offset   = 0;
522 start:          bv->bv_len      = min_t(size_t, PAGE_SIZE - bv->bv_offset,
523                                         size);
524                 if (base) {
525                         bv->bv_page = is_vmalloc_addr(base)
526                                 ? vmalloc_to_page(base)
527                                 : virt_to_page(base);
528
529                         base += bv->bv_len;
530                 }
531
532                 size -= bv->bv_len;
533         }
534 }
535
536 int bch2_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask)
537 {
538         int i;
539         struct bio_vec *bv;
540
541         bio_for_each_segment_all(bv, bio, i) {
542                 bv->bv_page = alloc_page(gfp_mask);
543                 if (!bv->bv_page) {
544                         while (--bv >= bio->bi_io_vec)
545                                 __free_page(bv->bv_page);
546                         return -ENOMEM;
547                 }
548         }
549
550         return 0;
551 }
552
553 size_t bch2_rand_range(size_t max)
554 {
555         size_t rand;
556
557         if (!max)
558                 return 0;
559
560         do {
561                 rand = get_random_long();
562                 rand &= roundup_pow_of_two(max) - 1;
563         } while (rand >= max);
564
565         return rand;
566 }
567
568 void memcpy_to_bio(struct bio *dst, struct bvec_iter dst_iter, void *src)
569 {
570         struct bio_vec bv;
571         struct bvec_iter iter;
572
573         __bio_for_each_segment(bv, dst, iter, dst_iter) {
574                 void *dstp = kmap_atomic(bv.bv_page);
575                 memcpy(dstp + bv.bv_offset, src, bv.bv_len);
576                 kunmap_atomic(dstp);
577
578                 src += bv.bv_len;
579         }
580 }
581
582 void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter)
583 {
584         struct bio_vec bv;
585         struct bvec_iter iter;
586
587         __bio_for_each_segment(bv, src, iter, src_iter) {
588                 void *srcp = kmap_atomic(bv.bv_page);
589                 memcpy(dst, srcp + bv.bv_offset, bv.bv_len);
590                 kunmap_atomic(srcp);
591
592                 dst += bv.bv_len;
593         }
594 }
595
596 void bch_scnmemcpy(struct printbuf *out,
597                    const char *src, size_t len)
598 {
599         size_t n = printbuf_remaining(out);
600
601         if (n) {
602                 n = min(n - 1, len);
603                 memcpy(out->pos, src, n);
604                 out->pos += n;
605                 *out->pos = '\0';
606         }
607 }
608
609 #include "eytzinger.h"
610
611 static int alignment_ok(const void *base, size_t align)
612 {
613         return IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
614                 ((unsigned long)base & (align - 1)) == 0;
615 }
616
617 static void u32_swap(void *a, void *b, size_t size)
618 {
619         u32 t = *(u32 *)a;
620         *(u32 *)a = *(u32 *)b;
621         *(u32 *)b = t;
622 }
623
624 static void u64_swap(void *a, void *b, size_t size)
625 {
626         u64 t = *(u64 *)a;
627         *(u64 *)a = *(u64 *)b;
628         *(u64 *)b = t;
629 }
630
631 static void generic_swap(void *a, void *b, size_t size)
632 {
633         char t;
634
635         do {
636                 t = *(char *)a;
637                 *(char *)a++ = *(char *)b;
638                 *(char *)b++ = t;
639         } while (--size > 0);
640 }
641
642 static inline int do_cmp(void *base, size_t n, size_t size,
643                          int (*cmp_func)(const void *, const void *, size_t),
644                          size_t l, size_t r)
645 {
646         return cmp_func(base + inorder_to_eytzinger0(l, n) * size,
647                         base + inorder_to_eytzinger0(r, n) * size,
648                         size);
649 }
650
651 static inline void do_swap(void *base, size_t n, size_t size,
652                            void (*swap_func)(void *, void *, size_t),
653                            size_t l, size_t r)
654 {
655         swap_func(base + inorder_to_eytzinger0(l, n) * size,
656                   base + inorder_to_eytzinger0(r, n) * size,
657                   size);
658 }
659
660 void eytzinger0_sort(void *base, size_t n, size_t size,
661                      int (*cmp_func)(const void *, const void *, size_t),
662                      void (*swap_func)(void *, void *, size_t))
663 {
664         int i, c, r;
665
666         if (!swap_func) {
667                 if (size == 4 && alignment_ok(base, 4))
668                         swap_func = u32_swap;
669                 else if (size == 8 && alignment_ok(base, 8))
670                         swap_func = u64_swap;
671                 else
672                         swap_func = generic_swap;
673         }
674
675         /* heapify */
676         for (i = n / 2 - 1; i >= 0; --i) {
677                 for (r = i; r * 2 + 1 < n; r = c) {
678                         c = r * 2 + 1;
679
680                         if (c + 1 < n &&
681                             do_cmp(base, n, size, cmp_func, c, c + 1) < 0)
682                                 c++;
683
684                         if (do_cmp(base, n, size, cmp_func, r, c) >= 0)
685                                 break;
686
687                         do_swap(base, n, size, swap_func, r, c);
688                 }
689         }
690
691         /* sort */
692         for (i = n - 1; i > 0; --i) {
693                 do_swap(base, n, size, swap_func, 0, i);
694
695                 for (r = 0; r * 2 + 1 < i; r = c) {
696                         c = r * 2 + 1;
697
698                         if (c + 1 < i &&
699                             do_cmp(base, n, size, cmp_func, c, c + 1) < 0)
700                                 c++;
701
702                         if (do_cmp(base, n, size, cmp_func, r, c) >= 0)
703                                 break;
704
705                         do_swap(base, n, size, swap_func, r, c);
706                 }
707         }
708 }
709
710 void sort_cmp_size(void *base, size_t num, size_t size,
711           int (*cmp_func)(const void *, const void *, size_t),
712           void (*swap_func)(void *, void *, size_t size))
713 {
714         /* pre-scale counters for performance */
715         int i = (num/2 - 1) * size, n = num * size, c, r;
716
717         if (!swap_func) {
718                 if (size == 4 && alignment_ok(base, 4))
719                         swap_func = u32_swap;
720                 else if (size == 8 && alignment_ok(base, 8))
721                         swap_func = u64_swap;
722                 else
723                         swap_func = generic_swap;
724         }
725
726         /* heapify */
727         for ( ; i >= 0; i -= size) {
728                 for (r = i; r * 2 + size < n; r  = c) {
729                         c = r * 2 + size;
730                         if (c < n - size &&
731                             cmp_func(base + c, base + c + size, size) < 0)
732                                 c += size;
733                         if (cmp_func(base + r, base + c, size) >= 0)
734                                 break;
735                         swap_func(base + r, base + c, size);
736                 }
737         }
738
739         /* sort */
740         for (i = n - size; i > 0; i -= size) {
741                 swap_func(base, base + i, size);
742                 for (r = 0; r * 2 + size < i; r = c) {
743                         c = r * 2 + size;
744                         if (c < i - size &&
745                             cmp_func(base + c, base + c + size, size) < 0)
746                                 c += size;
747                         if (cmp_func(base + r, base + c, size) >= 0)
748                                 break;
749                         swap_func(base + r, base + c, size);
750                 }
751         }
752 }
753
754 static void mempool_free_vp(void *element, void *pool_data)
755 {
756         size_t size = (size_t) pool_data;
757
758         vpfree(element, size);
759 }
760
761 static void *mempool_alloc_vp(gfp_t gfp_mask, void *pool_data)
762 {
763         size_t size = (size_t) pool_data;
764
765         return vpmalloc(size, gfp_mask);
766 }
767
768 int mempool_init_kvpmalloc_pool(mempool_t *pool, int min_nr, size_t size)
769 {
770         return size < PAGE_SIZE
771                 ? mempool_init_kmalloc_pool(pool, min_nr, size)
772                 : mempool_init(pool, min_nr, mempool_alloc_vp,
773                                mempool_free_vp, (void *) size);
774 }
775
776 #if 0
777 void eytzinger1_test(void)
778 {
779         unsigned inorder, eytz, size;
780
781         pr_info("1 based eytzinger test:");
782
783         for (size = 2;
784              size < 65536;
785              size++) {
786                 unsigned extra = eytzinger1_extra(size);
787
788                 if (!(size % 4096))
789                         pr_info("tree size %u", size);
790
791                 BUG_ON(eytzinger1_prev(0, size) != eytzinger1_last(size));
792                 BUG_ON(eytzinger1_next(0, size) != eytzinger1_first(size));
793
794                 BUG_ON(eytzinger1_prev(eytzinger1_first(size), size)    != 0);
795                 BUG_ON(eytzinger1_next(eytzinger1_last(size), size)     != 0);
796
797                 inorder = 1;
798                 eytzinger1_for_each(eytz, size) {
799                         BUG_ON(__inorder_to_eytzinger1(inorder, size, extra) != eytz);
800                         BUG_ON(__eytzinger1_to_inorder(eytz, size, extra) != inorder);
801                         BUG_ON(eytz != eytzinger1_last(size) &&
802                                eytzinger1_prev(eytzinger1_next(eytz, size), size) != eytz);
803
804                         inorder++;
805                 }
806         }
807 }
808
809 void eytzinger0_test(void)
810 {
811
812         unsigned inorder, eytz, size;
813
814         pr_info("0 based eytzinger test:");
815
816         for (size = 1;
817              size < 65536;
818              size++) {
819                 unsigned extra = eytzinger0_extra(size);
820
821                 if (!(size % 4096))
822                         pr_info("tree size %u", size);
823
824                 BUG_ON(eytzinger0_prev(-1, size) != eytzinger0_last(size));
825                 BUG_ON(eytzinger0_next(-1, size) != eytzinger0_first(size));
826
827                 BUG_ON(eytzinger0_prev(eytzinger0_first(size), size)    != -1);
828                 BUG_ON(eytzinger0_next(eytzinger0_last(size), size)     != -1);
829
830                 inorder = 0;
831                 eytzinger0_for_each(eytz, size) {
832                         BUG_ON(__inorder_to_eytzinger0(inorder, size, extra) != eytz);
833                         BUG_ON(__eytzinger0_to_inorder(eytz, size, extra) != inorder);
834                         BUG_ON(eytz != eytzinger0_last(size) &&
835                                eytzinger0_prev(eytzinger0_next(eytz, size), size) != eytz);
836
837                         inorder++;
838                 }
839         }
840 }
841
842 static inline int cmp_u16(const void *_l, const void *_r, size_t size)
843 {
844         const u16 *l = _l, *r = _r;
845
846         return (*l > *r) - (*r - *l);
847 }
848
849 static void eytzinger0_find_test_val(u16 *test_array, unsigned nr, u16 search)
850 {
851         int i, c1 = -1, c2 = -1;
852         ssize_t r;
853
854         r = eytzinger0_find_le(test_array, nr,
855                                sizeof(test_array[0]),
856                                cmp_u16, &search);
857         if (r >= 0)
858                 c1 = test_array[r];
859
860         for (i = 0; i < nr; i++)
861                 if (test_array[i] <= search && test_array[i] > c2)
862                         c2 = test_array[i];
863
864         if (c1 != c2) {
865                 eytzinger0_for_each(i, nr)
866                         pr_info("[%3u] = %12u", i, test_array[i]);
867                 pr_info("find_le(%2u) -> [%2zi] = %2i should be %2i",
868                         i, r, c1, c2);
869         }
870 }
871
872 void eytzinger0_find_test(void)
873 {
874         unsigned i, nr, allocated = 1 << 12;
875         u16 *test_array = kmalloc_array(allocated, sizeof(test_array[0]), GFP_KERNEL);
876
877         for (nr = 1; nr < allocated; nr++) {
878                 pr_info("testing %u elems", nr);
879
880                 get_random_bytes(test_array, nr * sizeof(test_array[0]));
881                 eytzinger0_sort(test_array, nr, sizeof(test_array[0]), cmp_u16, NULL);
882
883                 /* verify array is sorted correctly: */
884                 eytzinger0_for_each(i, nr)
885                         BUG_ON(i != eytzinger0_last(nr) &&
886                                test_array[i] > test_array[eytzinger0_next(i, nr)]);
887
888                 for (i = 0; i < U16_MAX; i += 1 << 12)
889                         eytzinger0_find_test_val(test_array, nr, i);
890
891                 for (i = 0; i < nr; i++) {
892                         eytzinger0_find_test_val(test_array, nr, test_array[i] - 1);
893                         eytzinger0_find_test_val(test_array, nr, test_array[i]);
894                         eytzinger0_find_test_val(test_array, nr, test_array[i] + 1);
895                 }
896         }
897
898         kfree(test_array);
899 }
900 #endif
901
902 /*
903  * Accumulate percpu counters onto one cpu's copy - only valid when access
904  * against any percpu counter is guarded against
905  */
906 u64 *bch2_acc_percpu_u64s(u64 __percpu *p, unsigned nr)
907 {
908         u64 *ret;
909         int cpu;
910
911         preempt_disable();
912         ret = this_cpu_ptr(p);
913         preempt_enable();
914
915         for_each_possible_cpu(cpu) {
916                 u64 *i = per_cpu_ptr(p, cpu);
917
918                 if (i != ret) {
919                         acc_u64s(ret, i, nr);
920                         memset(i, 0, nr * sizeof(u64));
921                 }
922         }
923
924         return ret;
925 }