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