xref: /linux/fs/bcachefs/util.c (revision d0b50524f1d9b60318b92830546b45cd3325cfe2)
11c6fdbd8SKent Overstreet // SPDX-License-Identifier: GPL-2.0
21c6fdbd8SKent Overstreet /*
31c6fdbd8SKent Overstreet  * random utiility code, for bcache but in theory not specific to bcache
41c6fdbd8SKent Overstreet  *
51c6fdbd8SKent Overstreet  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
61c6fdbd8SKent Overstreet  * Copyright 2012 Google, Inc.
71c6fdbd8SKent Overstreet  */
81c6fdbd8SKent Overstreet 
91c6fdbd8SKent Overstreet #include <linux/bio.h>
101c6fdbd8SKent Overstreet #include <linux/blkdev.h>
111c6fdbd8SKent Overstreet #include <linux/ctype.h>
121c6fdbd8SKent Overstreet #include <linux/debugfs.h>
131c6fdbd8SKent Overstreet #include <linux/freezer.h>
141c6fdbd8SKent Overstreet #include <linux/kthread.h>
151c6fdbd8SKent Overstreet #include <linux/log2.h>
161c6fdbd8SKent Overstreet #include <linux/math64.h>
171c6fdbd8SKent Overstreet #include <linux/percpu.h>
181c6fdbd8SKent Overstreet #include <linux/preempt.h>
191c6fdbd8SKent Overstreet #include <linux/random.h>
201c6fdbd8SKent Overstreet #include <linux/seq_file.h>
211c6fdbd8SKent Overstreet #include <linux/string.h>
221c6fdbd8SKent Overstreet #include <linux/types.h>
231c6fdbd8SKent Overstreet #include <linux/sched/clock.h>
241c6fdbd8SKent Overstreet 
251c6fdbd8SKent Overstreet #include "eytzinger.h"
261c6fdbd8SKent Overstreet #include "util.h"
271c6fdbd8SKent Overstreet 
281c6fdbd8SKent Overstreet static const char si_units[] = "?kMGTPEZY";
291c6fdbd8SKent Overstreet 
30a5d18f9eSKent Overstreet /* string_get_size units: */
31a5d18f9eSKent Overstreet static const char *const units_2[] = {
32a5d18f9eSKent Overstreet 	"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
33a5d18f9eSKent Overstreet };
34a5d18f9eSKent Overstreet static const char *const units_10[] = {
35a5d18f9eSKent Overstreet 	"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
36a5d18f9eSKent Overstreet };
371c6fdbd8SKent Overstreet 
38a5d18f9eSKent Overstreet static int parse_u64(const char *cp, u64 *res)
39a5d18f9eSKent Overstreet {
40a5d18f9eSKent Overstreet 	const char *start = cp;
41a5d18f9eSKent Overstreet 	u64 v = 0;
421c6fdbd8SKent Overstreet 
431c6fdbd8SKent Overstreet 	if (!isdigit(*cp))
441c6fdbd8SKent Overstreet 		return -EINVAL;
451c6fdbd8SKent Overstreet 
461c6fdbd8SKent Overstreet 	do {
471c6fdbd8SKent Overstreet 		if (v > U64_MAX / 10)
481c6fdbd8SKent Overstreet 			return -ERANGE;
491c6fdbd8SKent Overstreet 		v *= 10;
501c6fdbd8SKent Overstreet 		if (v > U64_MAX - (*cp - '0'))
511c6fdbd8SKent Overstreet 			return -ERANGE;
521c6fdbd8SKent Overstreet 		v += *cp - '0';
531c6fdbd8SKent Overstreet 		cp++;
541c6fdbd8SKent Overstreet 	} while (isdigit(*cp));
551c6fdbd8SKent Overstreet 
56a5d18f9eSKent Overstreet 	*res = v;
57a5d18f9eSKent Overstreet 	return cp - start;
58a5d18f9eSKent Overstreet }
59a5d18f9eSKent Overstreet 
60a5d18f9eSKent Overstreet static int bch2_pow(u64 n, u64 p, u64 *res)
61a5d18f9eSKent Overstreet {
62a5d18f9eSKent Overstreet 	*res = 1;
63a5d18f9eSKent Overstreet 
64a5d18f9eSKent Overstreet 	while (p--) {
65a5d18f9eSKent Overstreet 		if (*res > div_u64(U64_MAX, n))
66a5d18f9eSKent Overstreet 			return -ERANGE;
67a5d18f9eSKent Overstreet 		*res *= n;
68a5d18f9eSKent Overstreet 	}
69a5d18f9eSKent Overstreet 	return 0;
70a5d18f9eSKent Overstreet }
71a5d18f9eSKent Overstreet 
72a5d18f9eSKent Overstreet static int parse_unit_suffix(const char *cp, u64 *res)
73a5d18f9eSKent Overstreet {
74a5d18f9eSKent Overstreet 	const char *start = cp;
75a5d18f9eSKent Overstreet 	u64 base = 1024;
76a5d18f9eSKent Overstreet 	unsigned u;
77a5d18f9eSKent Overstreet 	int ret;
78a5d18f9eSKent Overstreet 
79a5d18f9eSKent Overstreet 	if (*cp == ' ')
80a5d18f9eSKent Overstreet 		cp++;
81a5d18f9eSKent Overstreet 
821c6fdbd8SKent Overstreet 	for (u = 1; u < strlen(si_units); u++)
831c6fdbd8SKent Overstreet 		if (*cp == si_units[u]) {
841c6fdbd8SKent Overstreet 			cp++;
851c6fdbd8SKent Overstreet 			goto got_unit;
861c6fdbd8SKent Overstreet 		}
87a5d18f9eSKent Overstreet 
88a5d18f9eSKent Overstreet 	for (u = 0; u < ARRAY_SIZE(units_2); u++)
89a5d18f9eSKent Overstreet 		if (!strncmp(cp, units_2[u], strlen(units_2[u]))) {
90a5d18f9eSKent Overstreet 			cp += strlen(units_2[u]);
91a5d18f9eSKent Overstreet 			goto got_unit;
92a5d18f9eSKent Overstreet 		}
93a5d18f9eSKent Overstreet 
94a5d18f9eSKent Overstreet 	for (u = 0; u < ARRAY_SIZE(units_10); u++)
95a5d18f9eSKent Overstreet 		if (!strncmp(cp, units_10[u], strlen(units_10[u]))) {
96a5d18f9eSKent Overstreet 			cp += strlen(units_10[u]);
97a5d18f9eSKent Overstreet 			base = 1000;
98a5d18f9eSKent Overstreet 			goto got_unit;
99a5d18f9eSKent Overstreet 		}
100a5d18f9eSKent Overstreet 
101a5d18f9eSKent Overstreet 	*res = 1;
102a5d18f9eSKent Overstreet 	return 0;
1031c6fdbd8SKent Overstreet got_unit:
104a5d18f9eSKent Overstreet 	ret = bch2_pow(base, u, res);
105a5d18f9eSKent Overstreet 	if (ret)
106a5d18f9eSKent Overstreet 		return ret;
107a5d18f9eSKent Overstreet 
108a5d18f9eSKent Overstreet 	return cp - start;
109a5d18f9eSKent Overstreet }
110a5d18f9eSKent Overstreet 
111a5d18f9eSKent Overstreet #define parse_or_ret(cp, _f)			\
112a5d18f9eSKent Overstreet do {						\
113a5d18f9eSKent Overstreet 	int ret = _f;				\
114a5d18f9eSKent Overstreet 	if (ret < 0)				\
115a5d18f9eSKent Overstreet 		return ret;			\
116a5d18f9eSKent Overstreet 	cp += ret;				\
117a5d18f9eSKent Overstreet } while (0)
118a5d18f9eSKent Overstreet 
119a5d18f9eSKent Overstreet static int __bch2_strtou64_h(const char *cp, u64 *res)
120a5d18f9eSKent Overstreet {
121a5d18f9eSKent Overstreet 	const char *start = cp;
122a5d18f9eSKent Overstreet 	u64 v = 0, b, f_n = 0, f_d = 1;
123a5d18f9eSKent Overstreet 	int ret;
124a5d18f9eSKent Overstreet 
125a5d18f9eSKent Overstreet 	parse_or_ret(cp, parse_u64(cp, &v));
126a5d18f9eSKent Overstreet 
127a5d18f9eSKent Overstreet 	if (*cp == '.') {
128a5d18f9eSKent Overstreet 		cp++;
129a5d18f9eSKent Overstreet 		ret = parse_u64(cp, &f_n);
130a5d18f9eSKent Overstreet 		if (ret < 0)
131a5d18f9eSKent Overstreet 			return ret;
132a5d18f9eSKent Overstreet 		cp += ret;
133a5d18f9eSKent Overstreet 
134a5d18f9eSKent Overstreet 		ret = bch2_pow(10, ret, &f_d);
135a5d18f9eSKent Overstreet 		if (ret)
136a5d18f9eSKent Overstreet 			return ret;
137a5d18f9eSKent Overstreet 	}
138a5d18f9eSKent Overstreet 
139a5d18f9eSKent Overstreet 	parse_or_ret(cp, parse_unit_suffix(cp, &b));
140a5d18f9eSKent Overstreet 
141a5d18f9eSKent Overstreet 	if (v > div_u64(U64_MAX, b))
142a5d18f9eSKent Overstreet 		return -ERANGE;
143a5d18f9eSKent Overstreet 	v *= b;
144a5d18f9eSKent Overstreet 
145a5d18f9eSKent Overstreet 	if (f_n > div_u64(U64_MAX, b))
146a5d18f9eSKent Overstreet 		return -ERANGE;
147a5d18f9eSKent Overstreet 
148a5d18f9eSKent Overstreet 	f_n = div_u64(f_n * b, f_d);
149a5d18f9eSKent Overstreet 	if (v + f_n < v)
150a5d18f9eSKent Overstreet 		return -ERANGE;
151a5d18f9eSKent Overstreet 	v += f_n;
152a5d18f9eSKent Overstreet 
153a5d18f9eSKent Overstreet 	*res = v;
154a5d18f9eSKent Overstreet 	return cp - start;
155a5d18f9eSKent Overstreet }
156a5d18f9eSKent Overstreet 
157a5d18f9eSKent Overstreet static int __bch2_strtoh(const char *cp, u64 *res,
158a5d18f9eSKent Overstreet 			 u64 t_max, bool t_signed)
159a5d18f9eSKent Overstreet {
160a5d18f9eSKent Overstreet 	bool positive = *cp != '-';
161a5d18f9eSKent Overstreet 	u64 v = 0;
162a5d18f9eSKent Overstreet 
163a5d18f9eSKent Overstreet 	if (*cp == '+' || *cp == '-')
164a5d18f9eSKent Overstreet 		cp++;
165a5d18f9eSKent Overstreet 
166a5d18f9eSKent Overstreet 	parse_or_ret(cp, __bch2_strtou64_h(cp, &v));
167a5d18f9eSKent Overstreet 
1681c6fdbd8SKent Overstreet 	if (*cp == '\n')
1691c6fdbd8SKent Overstreet 		cp++;
1701c6fdbd8SKent Overstreet 	if (*cp)
1711c6fdbd8SKent Overstreet 		return -EINVAL;
1721c6fdbd8SKent Overstreet 
1731c6fdbd8SKent Overstreet 	if (positive) {
1741c6fdbd8SKent Overstreet 		if (v > t_max)
1751c6fdbd8SKent Overstreet 			return -ERANGE;
1761c6fdbd8SKent Overstreet 	} else {
1771c6fdbd8SKent Overstreet 		if (v && !t_signed)
1781c6fdbd8SKent Overstreet 			return -ERANGE;
1791c6fdbd8SKent Overstreet 
1801c6fdbd8SKent Overstreet 		if (v > t_max + 1)
1811c6fdbd8SKent Overstreet 			return -ERANGE;
1821c6fdbd8SKent Overstreet 		v = -v;
1831c6fdbd8SKent Overstreet 	}
1841c6fdbd8SKent Overstreet 
1851c6fdbd8SKent Overstreet 	*res = v;
1861c6fdbd8SKent Overstreet 	return 0;
1871c6fdbd8SKent Overstreet }
1881c6fdbd8SKent Overstreet 
1891c6fdbd8SKent Overstreet #define STRTO_H(name, type)					\
1901c6fdbd8SKent Overstreet int bch2_ ## name ## _h(const char *cp, type *res)		\
1911c6fdbd8SKent Overstreet {								\
192a5d18f9eSKent Overstreet 	u64 v = 0;						\
1931c6fdbd8SKent Overstreet 	int ret = __bch2_strtoh(cp, &v, ANYSINT_MAX(type),	\
1941c6fdbd8SKent Overstreet 			ANYSINT_MAX(type) != ((type) ~0ULL));	\
1951c6fdbd8SKent Overstreet 	*res = v;						\
1961c6fdbd8SKent Overstreet 	return ret;						\
1971c6fdbd8SKent Overstreet }
1981c6fdbd8SKent Overstreet 
1991c6fdbd8SKent Overstreet STRTO_H(strtoint, int)
2001c6fdbd8SKent Overstreet STRTO_H(strtouint, unsigned int)
2011c6fdbd8SKent Overstreet STRTO_H(strtoll, long long)
2021c6fdbd8SKent Overstreet STRTO_H(strtoull, unsigned long long)
2030b847a19SKent Overstreet STRTO_H(strtou64, u64)
2041c6fdbd8SKent Overstreet 
2051c6fdbd8SKent Overstreet u64 bch2_read_flag_list(char *opt, const char * const list[])
2061c6fdbd8SKent Overstreet {
2071c6fdbd8SKent Overstreet 	u64 ret = 0;
2089d8022dbSKent Overstreet 	char *p, *s, *d = kstrdup(opt, GFP_KERNEL);
2091c6fdbd8SKent Overstreet 
2101c6fdbd8SKent Overstreet 	if (!d)
2111c6fdbd8SKent Overstreet 		return -ENOMEM;
2121c6fdbd8SKent Overstreet 
2131c6fdbd8SKent Overstreet 	s = strim(d);
2141c6fdbd8SKent Overstreet 
2151c6fdbd8SKent Overstreet 	while ((p = strsep(&s, ","))) {
2161c6fdbd8SKent Overstreet 		int flag = match_string(list, -1, p);
2171c6fdbd8SKent Overstreet 		if (flag < 0) {
2181c6fdbd8SKent Overstreet 			ret = -1;
2191c6fdbd8SKent Overstreet 			break;
2201c6fdbd8SKent Overstreet 		}
2211c6fdbd8SKent Overstreet 
2221c6fdbd8SKent Overstreet 		ret |= 1 << flag;
2231c6fdbd8SKent Overstreet 	}
2241c6fdbd8SKent Overstreet 
2251c6fdbd8SKent Overstreet 	kfree(d);
2261c6fdbd8SKent Overstreet 
2271c6fdbd8SKent Overstreet 	return ret;
2281c6fdbd8SKent Overstreet }
2291c6fdbd8SKent Overstreet 
2301c6fdbd8SKent Overstreet bool bch2_is_zero(const void *_p, size_t n)
2311c6fdbd8SKent Overstreet {
2321c6fdbd8SKent Overstreet 	const char *p = _p;
2331c6fdbd8SKent Overstreet 	size_t i;
2341c6fdbd8SKent Overstreet 
2351c6fdbd8SKent Overstreet 	for (i = 0; i < n; i++)
2361c6fdbd8SKent Overstreet 		if (p[i])
2371c6fdbd8SKent Overstreet 			return false;
2381c6fdbd8SKent Overstreet 	return true;
2391c6fdbd8SKent Overstreet }
2401c6fdbd8SKent Overstreet 
241*d0b50524SKent Overstreet void bch2_prt_u64_binary(struct printbuf *out, u64 v, unsigned nr_bits)
242*d0b50524SKent Overstreet {
243*d0b50524SKent Overstreet 	while (nr_bits)
244*d0b50524SKent Overstreet 		prt_char(out, '0' + ((v >> --nr_bits) & 1));
245*d0b50524SKent Overstreet }
246*d0b50524SKent Overstreet 
2471c6fdbd8SKent Overstreet /* time stats: */
2481c6fdbd8SKent Overstreet 
2491c6fdbd8SKent Overstreet #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
2501c6fdbd8SKent Overstreet static void bch2_quantiles_update(struct bch2_quantiles *q, u64 v)
2511c6fdbd8SKent Overstreet {
2521c6fdbd8SKent Overstreet 	unsigned i = 0;
2531c6fdbd8SKent Overstreet 
2541c6fdbd8SKent Overstreet 	while (i < ARRAY_SIZE(q->entries)) {
2551c6fdbd8SKent Overstreet 		struct bch2_quantile_entry *e = q->entries + i;
2561c6fdbd8SKent Overstreet 
2571c6fdbd8SKent Overstreet 		if (unlikely(!e->step)) {
2581c6fdbd8SKent Overstreet 			e->m = v;
2591c6fdbd8SKent Overstreet 			e->step = max_t(unsigned, v / 2, 1024);
2601c6fdbd8SKent Overstreet 		} else if (e->m > v) {
2611c6fdbd8SKent Overstreet 			e->m = e->m >= e->step
2621c6fdbd8SKent Overstreet 				? e->m - e->step
2631c6fdbd8SKent Overstreet 				: 0;
2641c6fdbd8SKent Overstreet 		} else if (e->m < v) {
2651c6fdbd8SKent Overstreet 			e->m = e->m + e->step > e->m
2661c6fdbd8SKent Overstreet 				? e->m + e->step
2671c6fdbd8SKent Overstreet 				: U32_MAX;
2681c6fdbd8SKent Overstreet 		}
2691c6fdbd8SKent Overstreet 
2701c6fdbd8SKent Overstreet 		if ((e->m > v ? e->m - v : v - e->m) < e->step)
2711c6fdbd8SKent Overstreet 			e->step = max_t(unsigned, e->step / 2, 1);
2721c6fdbd8SKent Overstreet 
2731c6fdbd8SKent Overstreet 		if (v >= e->m)
2741c6fdbd8SKent Overstreet 			break;
2751c6fdbd8SKent Overstreet 
2761c6fdbd8SKent Overstreet 		i = eytzinger0_child(i, v > e->m);
2771c6fdbd8SKent Overstreet 	}
2781c6fdbd8SKent Overstreet }
2791c6fdbd8SKent Overstreet 
2801c6fdbd8SKent Overstreet static void bch2_time_stats_update_one(struct bch2_time_stats *stats,
2811c6fdbd8SKent Overstreet 				       u64 start, u64 end)
2821c6fdbd8SKent Overstreet {
2831c6fdbd8SKent Overstreet 	u64 duration, freq;
2841c6fdbd8SKent Overstreet 
2851c6fdbd8SKent Overstreet 	duration	= time_after64(end, start)
2861c6fdbd8SKent Overstreet 		? end - start : 0;
2871c6fdbd8SKent Overstreet 	freq		= time_after64(end, stats->last_event)
2881c6fdbd8SKent Overstreet 		? end - stats->last_event : 0;
2891c6fdbd8SKent Overstreet 
2901c6fdbd8SKent Overstreet 	stats->count++;
2911c6fdbd8SKent Overstreet 
2921c6fdbd8SKent Overstreet 	stats->average_duration = stats->average_duration
2931c6fdbd8SKent Overstreet 		? ewma_add(stats->average_duration, duration, 6)
2941c6fdbd8SKent Overstreet 		: duration;
2951c6fdbd8SKent Overstreet 
2961c6fdbd8SKent Overstreet 	stats->average_frequency = stats->average_frequency
2971c6fdbd8SKent Overstreet 		? ewma_add(stats->average_frequency, freq, 6)
2981c6fdbd8SKent Overstreet 		: freq;
2991c6fdbd8SKent Overstreet 
3001c6fdbd8SKent Overstreet 	stats->max_duration = max(stats->max_duration, duration);
3011c6fdbd8SKent Overstreet 
3021c6fdbd8SKent Overstreet 	stats->last_event = end;
3031c6fdbd8SKent Overstreet 
3041c6fdbd8SKent Overstreet 	bch2_quantiles_update(&stats->quantiles, duration);
3051c6fdbd8SKent Overstreet }
3061c6fdbd8SKent Overstreet 
3071c6fdbd8SKent Overstreet void __bch2_time_stats_update(struct bch2_time_stats *stats, u64 start, u64 end)
3081c6fdbd8SKent Overstreet {
3091c6fdbd8SKent Overstreet 	unsigned long flags;
3101c6fdbd8SKent Overstreet 
3111c6fdbd8SKent Overstreet 	if (!stats->buffer) {
3121c6fdbd8SKent Overstreet 		spin_lock_irqsave(&stats->lock, flags);
3131c6fdbd8SKent Overstreet 		bch2_time_stats_update_one(stats, start, end);
3141c6fdbd8SKent Overstreet 
3151c6fdbd8SKent Overstreet 		if (stats->average_frequency < 32 &&
3161c6fdbd8SKent Overstreet 		    stats->count > 1024)
3171c6fdbd8SKent Overstreet 			stats->buffer =
3181c6fdbd8SKent Overstreet 				alloc_percpu_gfp(struct bch2_time_stat_buffer,
3191c6fdbd8SKent Overstreet 						 GFP_ATOMIC);
3201c6fdbd8SKent Overstreet 		spin_unlock_irqrestore(&stats->lock, flags);
3211c6fdbd8SKent Overstreet 	} else {
3221c6fdbd8SKent Overstreet 		struct bch2_time_stat_buffer_entry *i;
3231c6fdbd8SKent Overstreet 		struct bch2_time_stat_buffer *b;
3241c6fdbd8SKent Overstreet 
3251c6fdbd8SKent Overstreet 		preempt_disable();
3261c6fdbd8SKent Overstreet 		b = this_cpu_ptr(stats->buffer);
3271c6fdbd8SKent Overstreet 
3281c6fdbd8SKent Overstreet 		BUG_ON(b->nr >= ARRAY_SIZE(b->entries));
3291c6fdbd8SKent Overstreet 		b->entries[b->nr++] = (struct bch2_time_stat_buffer_entry) {
3301c6fdbd8SKent Overstreet 			.start = start,
3311c6fdbd8SKent Overstreet 			.end = end
3321c6fdbd8SKent Overstreet 		};
3331c6fdbd8SKent Overstreet 
3341c6fdbd8SKent Overstreet 		if (b->nr == ARRAY_SIZE(b->entries)) {
3351c6fdbd8SKent Overstreet 			spin_lock_irqsave(&stats->lock, flags);
3361c6fdbd8SKent Overstreet 			for (i = b->entries;
3371c6fdbd8SKent Overstreet 			     i < b->entries + ARRAY_SIZE(b->entries);
3381c6fdbd8SKent Overstreet 			     i++)
3391c6fdbd8SKent Overstreet 				bch2_time_stats_update_one(stats, i->start, i->end);
3401c6fdbd8SKent Overstreet 			spin_unlock_irqrestore(&stats->lock, flags);
3411c6fdbd8SKent Overstreet 
3421c6fdbd8SKent Overstreet 			b->nr = 0;
3431c6fdbd8SKent Overstreet 		}
3441c6fdbd8SKent Overstreet 
3451c6fdbd8SKent Overstreet 		preempt_enable();
3461c6fdbd8SKent Overstreet 	}
3471c6fdbd8SKent Overstreet }
3481c6fdbd8SKent Overstreet #endif
3491c6fdbd8SKent Overstreet 
3501c6fdbd8SKent Overstreet static const struct time_unit {
3511c6fdbd8SKent Overstreet 	const char	*name;
3521c6fdbd8SKent Overstreet 	u32		nsecs;
3531c6fdbd8SKent Overstreet } time_units[] = {
3541c6fdbd8SKent Overstreet 	{ "ns",		1		},
3551c6fdbd8SKent Overstreet 	{ "us",		NSEC_PER_USEC	},
3561c6fdbd8SKent Overstreet 	{ "ms",		NSEC_PER_MSEC	},
3571c6fdbd8SKent Overstreet 	{ "sec",	NSEC_PER_SEC	},
3581c6fdbd8SKent Overstreet };
3591c6fdbd8SKent Overstreet 
3601c6fdbd8SKent Overstreet static const struct time_unit *pick_time_units(u64 ns)
3611c6fdbd8SKent Overstreet {
3621c6fdbd8SKent Overstreet 	const struct time_unit *u;
3631c6fdbd8SKent Overstreet 
3641c6fdbd8SKent Overstreet 	for (u = time_units;
3651c6fdbd8SKent Overstreet 	     u + 1 < time_units + ARRAY_SIZE(time_units) &&
3661c6fdbd8SKent Overstreet 	     ns >= u[1].nsecs << 1;
3671c6fdbd8SKent Overstreet 	     u++)
3681c6fdbd8SKent Overstreet 		;
3691c6fdbd8SKent Overstreet 
3701c6fdbd8SKent Overstreet 	return u;
3711c6fdbd8SKent Overstreet }
3721c6fdbd8SKent Overstreet 
373b17d3cecSKent Overstreet void bch2_pr_time_units(struct printbuf *out, u64 ns)
3741c6fdbd8SKent Overstreet {
3751c6fdbd8SKent Overstreet 	const struct time_unit *u = pick_time_units(ns);
3761c6fdbd8SKent Overstreet 
377401ec4dbSKent Overstreet 	prt_printf(out, "%llu %s", div_u64(ns, u->nsecs), u->name);
3781c6fdbd8SKent Overstreet }
3791c6fdbd8SKent Overstreet 
3807807e143SKent Overstreet void bch2_time_stats_to_text(struct printbuf *out, struct bch2_time_stats *stats)
3811c6fdbd8SKent Overstreet {
3821c6fdbd8SKent Overstreet 	const struct time_unit *u;
3831c6fdbd8SKent Overstreet 	u64 freq = READ_ONCE(stats->average_frequency);
3841c6fdbd8SKent Overstreet 	u64 q, last_q = 0;
3851c6fdbd8SKent Overstreet 	int i;
3861c6fdbd8SKent Overstreet 
38725055c69SDaniel Hill 	prt_printf(out, "count:\t\t%llu",
3881c6fdbd8SKent Overstreet 			 stats->count);
38925055c69SDaniel Hill 	prt_newline(out);
39025055c69SDaniel Hill 	prt_printf(out, "rate:\t\t%llu/sec",
3911c6fdbd8SKent Overstreet 	       freq ?  div64_u64(NSEC_PER_SEC, freq) : 0);
39225055c69SDaniel Hill 	prt_newline(out);
3931c6fdbd8SKent Overstreet 
394401ec4dbSKent Overstreet 	prt_printf(out, "frequency:\t");
395b17d3cecSKent Overstreet 	bch2_pr_time_units(out, freq);
3961c6fdbd8SKent Overstreet 
39725055c69SDaniel Hill 	prt_newline(out);
39825055c69SDaniel Hill 	prt_printf(out, "avg duration:\t");
399b17d3cecSKent Overstreet 	bch2_pr_time_units(out, stats->average_duration);
4001c6fdbd8SKent Overstreet 
40125055c69SDaniel Hill 	prt_newline(out);
40225055c69SDaniel Hill 	prt_printf(out, "max duration:\t");
403b17d3cecSKent Overstreet 	bch2_pr_time_units(out, stats->max_duration);
4041c6fdbd8SKent Overstreet 
4051c6fdbd8SKent Overstreet 	i = eytzinger0_first(NR_QUANTILES);
4061c6fdbd8SKent Overstreet 	u = pick_time_units(stats->quantiles.entries[i].m);
4071c6fdbd8SKent Overstreet 
40825055c69SDaniel Hill 	prt_newline(out);
40925055c69SDaniel Hill 	prt_printf(out, "quantiles (%s):\t", u->name);
4101c6fdbd8SKent Overstreet 	eytzinger0_for_each(i, NR_QUANTILES) {
4111c6fdbd8SKent Overstreet 		bool is_last = eytzinger0_next(i, NR_QUANTILES) == -1;
4121c6fdbd8SKent Overstreet 
4131c6fdbd8SKent Overstreet 		q = max(stats->quantiles.entries[i].m, last_q);
41425055c69SDaniel Hill 		prt_printf(out, "%llu ",
41525055c69SDaniel Hill 		       div_u64(q, u->nsecs));
41625055c69SDaniel Hill 		if (is_last)
41725055c69SDaniel Hill 			prt_newline(out);
4181c6fdbd8SKent Overstreet 		last_q = q;
4191c6fdbd8SKent Overstreet 	}
4201c6fdbd8SKent Overstreet }
4211c6fdbd8SKent Overstreet 
4221c6fdbd8SKent Overstreet void bch2_time_stats_exit(struct bch2_time_stats *stats)
4231c6fdbd8SKent Overstreet {
4241c6fdbd8SKent Overstreet 	free_percpu(stats->buffer);
4251c6fdbd8SKent Overstreet }
4261c6fdbd8SKent Overstreet 
4271c6fdbd8SKent Overstreet void bch2_time_stats_init(struct bch2_time_stats *stats)
4281c6fdbd8SKent Overstreet {
4291c6fdbd8SKent Overstreet 	memset(stats, 0, sizeof(*stats));
4301c6fdbd8SKent Overstreet 	spin_lock_init(&stats->lock);
4311c6fdbd8SKent Overstreet }
4321c6fdbd8SKent Overstreet 
4331c6fdbd8SKent Overstreet /* ratelimit: */
4341c6fdbd8SKent Overstreet 
4351c6fdbd8SKent Overstreet /**
4361c6fdbd8SKent Overstreet  * bch2_ratelimit_delay() - return how long to delay until the next time to do
4371c6fdbd8SKent Overstreet  * some work
4381c6fdbd8SKent Overstreet  *
4391c6fdbd8SKent Overstreet  * @d - the struct bch_ratelimit to update
4401c6fdbd8SKent Overstreet  *
4411c6fdbd8SKent Overstreet  * Returns the amount of time to delay by, in jiffies
4421c6fdbd8SKent Overstreet  */
4431c6fdbd8SKent Overstreet u64 bch2_ratelimit_delay(struct bch_ratelimit *d)
4441c6fdbd8SKent Overstreet {
4451c6fdbd8SKent Overstreet 	u64 now = local_clock();
4461c6fdbd8SKent Overstreet 
4471c6fdbd8SKent Overstreet 	return time_after64(d->next, now)
4481c6fdbd8SKent Overstreet 		? nsecs_to_jiffies(d->next - now)
4491c6fdbd8SKent Overstreet 		: 0;
4501c6fdbd8SKent Overstreet }
4511c6fdbd8SKent Overstreet 
4521c6fdbd8SKent Overstreet /**
4531c6fdbd8SKent Overstreet  * bch2_ratelimit_increment() - increment @d by the amount of work done
4541c6fdbd8SKent Overstreet  *
4551c6fdbd8SKent Overstreet  * @d - the struct bch_ratelimit to update
4561c6fdbd8SKent Overstreet  * @done - the amount of work done, in arbitrary units
4571c6fdbd8SKent Overstreet  */
4581c6fdbd8SKent Overstreet void bch2_ratelimit_increment(struct bch_ratelimit *d, u64 done)
4591c6fdbd8SKent Overstreet {
4601c6fdbd8SKent Overstreet 	u64 now = local_clock();
4611c6fdbd8SKent Overstreet 
4621c6fdbd8SKent Overstreet 	d->next += div_u64(done * NSEC_PER_SEC, d->rate);
4631c6fdbd8SKent Overstreet 
4641c6fdbd8SKent Overstreet 	if (time_before64(now + NSEC_PER_SEC, d->next))
4651c6fdbd8SKent Overstreet 		d->next = now + NSEC_PER_SEC;
4661c6fdbd8SKent Overstreet 
4671c6fdbd8SKent Overstreet 	if (time_after64(now - NSEC_PER_SEC * 2, d->next))
4681c6fdbd8SKent Overstreet 		d->next = now - NSEC_PER_SEC * 2;
4691c6fdbd8SKent Overstreet }
4701c6fdbd8SKent Overstreet 
4711c6fdbd8SKent Overstreet /* pd controller: */
4721c6fdbd8SKent Overstreet 
4731c6fdbd8SKent Overstreet /*
4741c6fdbd8SKent Overstreet  * Updates pd_controller. Attempts to scale inputed values to units per second.
4751c6fdbd8SKent Overstreet  * @target: desired value
4761c6fdbd8SKent Overstreet  * @actual: current value
4771c6fdbd8SKent Overstreet  *
4781c6fdbd8SKent Overstreet  * @sign: 1 or -1; 1 if increasing the rate makes actual go up, -1 if increasing
4791c6fdbd8SKent Overstreet  * it makes actual go down.
4801c6fdbd8SKent Overstreet  */
4811c6fdbd8SKent Overstreet void bch2_pd_controller_update(struct bch_pd_controller *pd,
4821c6fdbd8SKent Overstreet 			      s64 target, s64 actual, int sign)
4831c6fdbd8SKent Overstreet {
4841c6fdbd8SKent Overstreet 	s64 proportional, derivative, change;
4851c6fdbd8SKent Overstreet 
4861c6fdbd8SKent Overstreet 	unsigned long seconds_since_update = (jiffies - pd->last_update) / HZ;
4871c6fdbd8SKent Overstreet 
4881c6fdbd8SKent Overstreet 	if (seconds_since_update == 0)
4891c6fdbd8SKent Overstreet 		return;
4901c6fdbd8SKent Overstreet 
4911c6fdbd8SKent Overstreet 	pd->last_update = jiffies;
4921c6fdbd8SKent Overstreet 
4931c6fdbd8SKent Overstreet 	proportional = actual - target;
4941c6fdbd8SKent Overstreet 	proportional *= seconds_since_update;
4951c6fdbd8SKent Overstreet 	proportional = div_s64(proportional, pd->p_term_inverse);
4961c6fdbd8SKent Overstreet 
4971c6fdbd8SKent Overstreet 	derivative = actual - pd->last_actual;
4981c6fdbd8SKent Overstreet 	derivative = div_s64(derivative, seconds_since_update);
4991c6fdbd8SKent Overstreet 	derivative = ewma_add(pd->smoothed_derivative, derivative,
5001c6fdbd8SKent Overstreet 			      (pd->d_term / seconds_since_update) ?: 1);
5011c6fdbd8SKent Overstreet 	derivative = derivative * pd->d_term;
5021c6fdbd8SKent Overstreet 	derivative = div_s64(derivative, pd->p_term_inverse);
5031c6fdbd8SKent Overstreet 
5041c6fdbd8SKent Overstreet 	change = proportional + derivative;
5051c6fdbd8SKent Overstreet 
5061c6fdbd8SKent Overstreet 	/* Don't increase rate if not keeping up */
5071c6fdbd8SKent Overstreet 	if (change > 0 &&
5081c6fdbd8SKent Overstreet 	    pd->backpressure &&
5091c6fdbd8SKent Overstreet 	    time_after64(local_clock(),
5101c6fdbd8SKent Overstreet 			 pd->rate.next + NSEC_PER_MSEC))
5111c6fdbd8SKent Overstreet 		change = 0;
5121c6fdbd8SKent Overstreet 
5131c6fdbd8SKent Overstreet 	change *= (sign * -1);
5141c6fdbd8SKent Overstreet 
5151c6fdbd8SKent Overstreet 	pd->rate.rate = clamp_t(s64, (s64) pd->rate.rate + change,
5161c6fdbd8SKent Overstreet 				1, UINT_MAX);
5171c6fdbd8SKent Overstreet 
5181c6fdbd8SKent Overstreet 	pd->last_actual		= actual;
5191c6fdbd8SKent Overstreet 	pd->last_derivative	= derivative;
5201c6fdbd8SKent Overstreet 	pd->last_proportional	= proportional;
5211c6fdbd8SKent Overstreet 	pd->last_change		= change;
5221c6fdbd8SKent Overstreet 	pd->last_target		= target;
5231c6fdbd8SKent Overstreet }
5241c6fdbd8SKent Overstreet 
5251c6fdbd8SKent Overstreet void bch2_pd_controller_init(struct bch_pd_controller *pd)
5261c6fdbd8SKent Overstreet {
5271c6fdbd8SKent Overstreet 	pd->rate.rate		= 1024;
5281c6fdbd8SKent Overstreet 	pd->last_update		= jiffies;
5291c6fdbd8SKent Overstreet 	pd->p_term_inverse	= 6000;
5301c6fdbd8SKent Overstreet 	pd->d_term		= 30;
5311c6fdbd8SKent Overstreet 	pd->d_smooth		= pd->d_term;
5321c6fdbd8SKent Overstreet 	pd->backpressure	= 1;
5331c6fdbd8SKent Overstreet }
5341c6fdbd8SKent Overstreet 
5352be7b16eSKent Overstreet void bch2_pd_controller_debug_to_text(struct printbuf *out, struct bch_pd_controller *pd)
5361c6fdbd8SKent Overstreet {
537401ec4dbSKent Overstreet 	if (!out->nr_tabstops)
538401ec4dbSKent Overstreet 		printbuf_tabstop_push(out, 20);
5391c6fdbd8SKent Overstreet 
540401ec4dbSKent Overstreet 	prt_printf(out, "rate:");
541401ec4dbSKent Overstreet 	prt_tab(out);
542401ec4dbSKent Overstreet 	prt_human_readable_s64(out, pd->rate.rate);
543401ec4dbSKent Overstreet 	prt_newline(out);
5441c6fdbd8SKent Overstreet 
545401ec4dbSKent Overstreet 	prt_printf(out, "target:");
546401ec4dbSKent Overstreet 	prt_tab(out);
547401ec4dbSKent Overstreet 	prt_human_readable_u64(out, pd->last_target);
548401ec4dbSKent Overstreet 	prt_newline(out);
5491c6fdbd8SKent Overstreet 
550401ec4dbSKent Overstreet 	prt_printf(out, "actual:");
551401ec4dbSKent Overstreet 	prt_tab(out);
552401ec4dbSKent Overstreet 	prt_human_readable_u64(out, pd->last_actual);
553401ec4dbSKent Overstreet 	prt_newline(out);
5542be7b16eSKent Overstreet 
555401ec4dbSKent Overstreet 	prt_printf(out, "proportional:");
556401ec4dbSKent Overstreet 	prt_tab(out);
557401ec4dbSKent Overstreet 	prt_human_readable_s64(out, pd->last_proportional);
558401ec4dbSKent Overstreet 	prt_newline(out);
5592be7b16eSKent Overstreet 
560401ec4dbSKent Overstreet 	prt_printf(out, "derivative:");
561401ec4dbSKent Overstreet 	prt_tab(out);
562401ec4dbSKent Overstreet 	prt_human_readable_s64(out, pd->last_derivative);
563401ec4dbSKent Overstreet 	prt_newline(out);
5642be7b16eSKent Overstreet 
565401ec4dbSKent Overstreet 	prt_printf(out, "change:");
566401ec4dbSKent Overstreet 	prt_tab(out);
567401ec4dbSKent Overstreet 	prt_human_readable_s64(out, pd->last_change);
568401ec4dbSKent Overstreet 	prt_newline(out);
5692be7b16eSKent Overstreet 
570401ec4dbSKent Overstreet 	prt_printf(out, "next io:");
571401ec4dbSKent Overstreet 	prt_tab(out);
572401ec4dbSKent Overstreet 	prt_printf(out, "%llims", div64_s64(pd->rate.next - local_clock(), NSEC_PER_MSEC));
573401ec4dbSKent Overstreet 	prt_newline(out);
5741c6fdbd8SKent Overstreet }
5751c6fdbd8SKent Overstreet 
5761c6fdbd8SKent Overstreet /* misc: */
5771c6fdbd8SKent Overstreet 
578885678f6SKent Overstreet void bch2_bio_map(struct bio *bio, void *base, size_t size)
5791c6fdbd8SKent Overstreet {
580885678f6SKent Overstreet 	while (size) {
581885678f6SKent Overstreet 		struct page *page = is_vmalloc_addr(base)
5821c6fdbd8SKent Overstreet 				? vmalloc_to_page(base)
5831c6fdbd8SKent Overstreet 				: virt_to_page(base);
584885678f6SKent Overstreet 		unsigned offset = offset_in_page(base);
585885678f6SKent Overstreet 		unsigned len = min_t(size_t, PAGE_SIZE - offset, size);
5861c6fdbd8SKent Overstreet 
587885678f6SKent Overstreet 		BUG_ON(!bio_add_page(bio, page, len, offset));
588885678f6SKent Overstreet 		size -= len;
589885678f6SKent Overstreet 		base += len;
5901c6fdbd8SKent Overstreet 	}
5911c6fdbd8SKent Overstreet }
5921c6fdbd8SKent Overstreet 
5931c6fdbd8SKent Overstreet int bch2_bio_alloc_pages(struct bio *bio, size_t size, gfp_t gfp_mask)
5941c6fdbd8SKent Overstreet {
5951c6fdbd8SKent Overstreet 	while (size) {
5961c6fdbd8SKent Overstreet 		struct page *page = alloc_pages(gfp_mask, 0);
5971c6fdbd8SKent Overstreet 		unsigned len = min_t(size_t, PAGE_SIZE, size);
5981c6fdbd8SKent Overstreet 
5991c6fdbd8SKent Overstreet 		if (!page)
6001c6fdbd8SKent Overstreet 			return -ENOMEM;
6011c6fdbd8SKent Overstreet 
6021c6fdbd8SKent Overstreet 		if (unlikely(!bio_add_page(bio, page, len, 0))) {
6031c6fdbd8SKent Overstreet 			__free_page(page);
6041c6fdbd8SKent Overstreet 			break;
6051c6fdbd8SKent Overstreet 		}
6061c6fdbd8SKent Overstreet 
6071c6fdbd8SKent Overstreet 		size -= len;
6081c6fdbd8SKent Overstreet 	}
6091c6fdbd8SKent Overstreet 
6101c6fdbd8SKent Overstreet 	return 0;
6111c6fdbd8SKent Overstreet }
6121c6fdbd8SKent Overstreet 
6131c6fdbd8SKent Overstreet size_t bch2_rand_range(size_t max)
6141c6fdbd8SKent Overstreet {
6151c6fdbd8SKent Overstreet 	size_t rand;
6161c6fdbd8SKent Overstreet 
6171c6fdbd8SKent Overstreet 	if (!max)
6181c6fdbd8SKent Overstreet 		return 0;
6191c6fdbd8SKent Overstreet 
6201c6fdbd8SKent Overstreet 	do {
6211c6fdbd8SKent Overstreet 		rand = get_random_long();
6221c6fdbd8SKent Overstreet 		rand &= roundup_pow_of_two(max) - 1;
6231c6fdbd8SKent Overstreet 	} while (rand >= max);
6241c6fdbd8SKent Overstreet 
6251c6fdbd8SKent Overstreet 	return rand;
6261c6fdbd8SKent Overstreet }
6271c6fdbd8SKent Overstreet 
62803c8c747SKent Overstreet void memcpy_to_bio(struct bio *dst, struct bvec_iter dst_iter, const void *src)
6291c6fdbd8SKent Overstreet {
6301c6fdbd8SKent Overstreet 	struct bio_vec bv;
6311c6fdbd8SKent Overstreet 	struct bvec_iter iter;
6321c6fdbd8SKent Overstreet 
6331c6fdbd8SKent Overstreet 	__bio_for_each_segment(bv, dst, iter, dst_iter) {
6341c6fdbd8SKent Overstreet 		void *dstp = kmap_atomic(bv.bv_page);
6351c6fdbd8SKent Overstreet 		memcpy(dstp + bv.bv_offset, src, bv.bv_len);
6361c6fdbd8SKent Overstreet 		kunmap_atomic(dstp);
6371c6fdbd8SKent Overstreet 
6381c6fdbd8SKent Overstreet 		src += bv.bv_len;
6391c6fdbd8SKent Overstreet 	}
6401c6fdbd8SKent Overstreet }
6411c6fdbd8SKent Overstreet 
6421c6fdbd8SKent Overstreet void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter)
6431c6fdbd8SKent Overstreet {
6441c6fdbd8SKent Overstreet 	struct bio_vec bv;
6451c6fdbd8SKent Overstreet 	struct bvec_iter iter;
6461c6fdbd8SKent Overstreet 
6471c6fdbd8SKent Overstreet 	__bio_for_each_segment(bv, src, iter, src_iter) {
6481c6fdbd8SKent Overstreet 		void *srcp = kmap_atomic(bv.bv_page);
6491c6fdbd8SKent Overstreet 		memcpy(dst, srcp + bv.bv_offset, bv.bv_len);
6501c6fdbd8SKent Overstreet 		kunmap_atomic(srcp);
6511c6fdbd8SKent Overstreet 
6521c6fdbd8SKent Overstreet 		dst += bv.bv_len;
6531c6fdbd8SKent Overstreet 	}
6541c6fdbd8SKent Overstreet }
6551c6fdbd8SKent Overstreet 
6561c6fdbd8SKent Overstreet #include "eytzinger.h"
6571c6fdbd8SKent Overstreet 
6581c6fdbd8SKent Overstreet static int alignment_ok(const void *base, size_t align)
6591c6fdbd8SKent Overstreet {
6601c6fdbd8SKent Overstreet 	return IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
6611c6fdbd8SKent Overstreet 		((unsigned long)base & (align - 1)) == 0;
6621c6fdbd8SKent Overstreet }
6631c6fdbd8SKent Overstreet 
6641c6fdbd8SKent Overstreet static void u32_swap(void *a, void *b, size_t size)
6651c6fdbd8SKent Overstreet {
6661c6fdbd8SKent Overstreet 	u32 t = *(u32 *)a;
6671c6fdbd8SKent Overstreet 	*(u32 *)a = *(u32 *)b;
6681c6fdbd8SKent Overstreet 	*(u32 *)b = t;
6691c6fdbd8SKent Overstreet }
6701c6fdbd8SKent Overstreet 
6711c6fdbd8SKent Overstreet static void u64_swap(void *a, void *b, size_t size)
6721c6fdbd8SKent Overstreet {
6731c6fdbd8SKent Overstreet 	u64 t = *(u64 *)a;
6741c6fdbd8SKent Overstreet 	*(u64 *)a = *(u64 *)b;
6751c6fdbd8SKent Overstreet 	*(u64 *)b = t;
6761c6fdbd8SKent Overstreet }
6771c6fdbd8SKent Overstreet 
6781c6fdbd8SKent Overstreet static void generic_swap(void *a, void *b, size_t size)
6791c6fdbd8SKent Overstreet {
6801c6fdbd8SKent Overstreet 	char t;
6811c6fdbd8SKent Overstreet 
6821c6fdbd8SKent Overstreet 	do {
6831c6fdbd8SKent Overstreet 		t = *(char *)a;
6841c6fdbd8SKent Overstreet 		*(char *)a++ = *(char *)b;
6851c6fdbd8SKent Overstreet 		*(char *)b++ = t;
6861c6fdbd8SKent Overstreet 	} while (--size > 0);
6871c6fdbd8SKent Overstreet }
6881c6fdbd8SKent Overstreet 
6891c6fdbd8SKent Overstreet static inline int do_cmp(void *base, size_t n, size_t size,
6901c6fdbd8SKent Overstreet 			 int (*cmp_func)(const void *, const void *, size_t),
6911c6fdbd8SKent Overstreet 			 size_t l, size_t r)
6921c6fdbd8SKent Overstreet {
6931c6fdbd8SKent Overstreet 	return cmp_func(base + inorder_to_eytzinger0(l, n) * size,
6941c6fdbd8SKent Overstreet 			base + inorder_to_eytzinger0(r, n) * size,
6951c6fdbd8SKent Overstreet 			size);
6961c6fdbd8SKent Overstreet }
6971c6fdbd8SKent Overstreet 
6981c6fdbd8SKent Overstreet static inline void do_swap(void *base, size_t n, size_t size,
6991c6fdbd8SKent Overstreet 			   void (*swap_func)(void *, void *, size_t),
7001c6fdbd8SKent Overstreet 			   size_t l, size_t r)
7011c6fdbd8SKent Overstreet {
7021c6fdbd8SKent Overstreet 	swap_func(base + inorder_to_eytzinger0(l, n) * size,
7031c6fdbd8SKent Overstreet 		  base + inorder_to_eytzinger0(r, n) * size,
7041c6fdbd8SKent Overstreet 		  size);
7051c6fdbd8SKent Overstreet }
7061c6fdbd8SKent Overstreet 
7071c6fdbd8SKent Overstreet void eytzinger0_sort(void *base, size_t n, size_t size,
7081c6fdbd8SKent Overstreet 		     int (*cmp_func)(const void *, const void *, size_t),
7091c6fdbd8SKent Overstreet 		     void (*swap_func)(void *, void *, size_t))
7101c6fdbd8SKent Overstreet {
7111c6fdbd8SKent Overstreet 	int i, c, r;
7121c6fdbd8SKent Overstreet 
7131c6fdbd8SKent Overstreet 	if (!swap_func) {
7141c6fdbd8SKent Overstreet 		if (size == 4 && alignment_ok(base, 4))
7151c6fdbd8SKent Overstreet 			swap_func = u32_swap;
7161c6fdbd8SKent Overstreet 		else if (size == 8 && alignment_ok(base, 8))
7171c6fdbd8SKent Overstreet 			swap_func = u64_swap;
7181c6fdbd8SKent Overstreet 		else
7191c6fdbd8SKent Overstreet 			swap_func = generic_swap;
7201c6fdbd8SKent Overstreet 	}
7211c6fdbd8SKent Overstreet 
7221c6fdbd8SKent Overstreet 	/* heapify */
7231c6fdbd8SKent Overstreet 	for (i = n / 2 - 1; i >= 0; --i) {
7241c6fdbd8SKent Overstreet 		for (r = i; r * 2 + 1 < n; r = c) {
7251c6fdbd8SKent Overstreet 			c = r * 2 + 1;
7261c6fdbd8SKent Overstreet 
7271c6fdbd8SKent Overstreet 			if (c + 1 < n &&
7281c6fdbd8SKent Overstreet 			    do_cmp(base, n, size, cmp_func, c, c + 1) < 0)
7291c6fdbd8SKent Overstreet 				c++;
7301c6fdbd8SKent Overstreet 
7311c6fdbd8SKent Overstreet 			if (do_cmp(base, n, size, cmp_func, r, c) >= 0)
7321c6fdbd8SKent Overstreet 				break;
7331c6fdbd8SKent Overstreet 
7341c6fdbd8SKent Overstreet 			do_swap(base, n, size, swap_func, r, c);
7351c6fdbd8SKent Overstreet 		}
7361c6fdbd8SKent Overstreet 	}
7371c6fdbd8SKent Overstreet 
7381c6fdbd8SKent Overstreet 	/* sort */
7391c6fdbd8SKent Overstreet 	for (i = n - 1; i > 0; --i) {
7401c6fdbd8SKent Overstreet 		do_swap(base, n, size, swap_func, 0, i);
7411c6fdbd8SKent Overstreet 
7421c6fdbd8SKent Overstreet 		for (r = 0; r * 2 + 1 < i; r = c) {
7431c6fdbd8SKent Overstreet 			c = r * 2 + 1;
7441c6fdbd8SKent Overstreet 
7451c6fdbd8SKent Overstreet 			if (c + 1 < i &&
7461c6fdbd8SKent Overstreet 			    do_cmp(base, n, size, cmp_func, c, c + 1) < 0)
7471c6fdbd8SKent Overstreet 				c++;
7481c6fdbd8SKent Overstreet 
7491c6fdbd8SKent Overstreet 			if (do_cmp(base, n, size, cmp_func, r, c) >= 0)
7501c6fdbd8SKent Overstreet 				break;
7511c6fdbd8SKent Overstreet 
7521c6fdbd8SKent Overstreet 			do_swap(base, n, size, swap_func, r, c);
7531c6fdbd8SKent Overstreet 		}
7541c6fdbd8SKent Overstreet 	}
7551c6fdbd8SKent Overstreet }
7561c6fdbd8SKent Overstreet 
7571c6fdbd8SKent Overstreet void sort_cmp_size(void *base, size_t num, size_t size,
7581c6fdbd8SKent Overstreet 	  int (*cmp_func)(const void *, const void *, size_t),
7591c6fdbd8SKent Overstreet 	  void (*swap_func)(void *, void *, size_t size))
7601c6fdbd8SKent Overstreet {
7611c6fdbd8SKent Overstreet 	/* pre-scale counters for performance */
7621c6fdbd8SKent Overstreet 	int i = (num/2 - 1) * size, n = num * size, c, r;
7631c6fdbd8SKent Overstreet 
7641c6fdbd8SKent Overstreet 	if (!swap_func) {
7651c6fdbd8SKent Overstreet 		if (size == 4 && alignment_ok(base, 4))
7661c6fdbd8SKent Overstreet 			swap_func = u32_swap;
7671c6fdbd8SKent Overstreet 		else if (size == 8 && alignment_ok(base, 8))
7681c6fdbd8SKent Overstreet 			swap_func = u64_swap;
7691c6fdbd8SKent Overstreet 		else
7701c6fdbd8SKent Overstreet 			swap_func = generic_swap;
7711c6fdbd8SKent Overstreet 	}
7721c6fdbd8SKent Overstreet 
7731c6fdbd8SKent Overstreet 	/* heapify */
7741c6fdbd8SKent Overstreet 	for ( ; i >= 0; i -= size) {
7751c6fdbd8SKent Overstreet 		for (r = i; r * 2 + size < n; r  = c) {
7761c6fdbd8SKent Overstreet 			c = r * 2 + size;
7771c6fdbd8SKent Overstreet 			if (c < n - size &&
7781c6fdbd8SKent Overstreet 			    cmp_func(base + c, base + c + size, size) < 0)
7791c6fdbd8SKent Overstreet 				c += size;
7801c6fdbd8SKent Overstreet 			if (cmp_func(base + r, base + c, size) >= 0)
7811c6fdbd8SKent Overstreet 				break;
7821c6fdbd8SKent Overstreet 			swap_func(base + r, base + c, size);
7831c6fdbd8SKent Overstreet 		}
7841c6fdbd8SKent Overstreet 	}
7851c6fdbd8SKent Overstreet 
7861c6fdbd8SKent Overstreet 	/* sort */
7871c6fdbd8SKent Overstreet 	for (i = n - size; i > 0; i -= size) {
7881c6fdbd8SKent Overstreet 		swap_func(base, base + i, size);
7891c6fdbd8SKent Overstreet 		for (r = 0; r * 2 + size < i; r = c) {
7901c6fdbd8SKent Overstreet 			c = r * 2 + size;
7911c6fdbd8SKent Overstreet 			if (c < i - size &&
7921c6fdbd8SKent Overstreet 			    cmp_func(base + c, base + c + size, size) < 0)
7931c6fdbd8SKent Overstreet 				c += size;
7941c6fdbd8SKent Overstreet 			if (cmp_func(base + r, base + c, size) >= 0)
7951c6fdbd8SKent Overstreet 				break;
7961c6fdbd8SKent Overstreet 			swap_func(base + r, base + c, size);
7971c6fdbd8SKent Overstreet 		}
7981c6fdbd8SKent Overstreet 	}
7991c6fdbd8SKent Overstreet }
8001c6fdbd8SKent Overstreet 
8011c6fdbd8SKent Overstreet static void mempool_free_vp(void *element, void *pool_data)
8021c6fdbd8SKent Overstreet {
8031c6fdbd8SKent Overstreet 	size_t size = (size_t) pool_data;
8041c6fdbd8SKent Overstreet 
8051c6fdbd8SKent Overstreet 	vpfree(element, size);
8061c6fdbd8SKent Overstreet }
8071c6fdbd8SKent Overstreet 
8081c6fdbd8SKent Overstreet static void *mempool_alloc_vp(gfp_t gfp_mask, void *pool_data)
8091c6fdbd8SKent Overstreet {
8101c6fdbd8SKent Overstreet 	size_t size = (size_t) pool_data;
8111c6fdbd8SKent Overstreet 
8121c6fdbd8SKent Overstreet 	return vpmalloc(size, gfp_mask);
8131c6fdbd8SKent Overstreet }
8141c6fdbd8SKent Overstreet 
8151c6fdbd8SKent Overstreet int mempool_init_kvpmalloc_pool(mempool_t *pool, int min_nr, size_t size)
8161c6fdbd8SKent Overstreet {
8171c6fdbd8SKent Overstreet 	return size < PAGE_SIZE
8181c6fdbd8SKent Overstreet 		? mempool_init_kmalloc_pool(pool, min_nr, size)
8191c6fdbd8SKent Overstreet 		: mempool_init(pool, min_nr, mempool_alloc_vp,
8201c6fdbd8SKent Overstreet 			       mempool_free_vp, (void *) size);
8211c6fdbd8SKent Overstreet }
8221c6fdbd8SKent Overstreet 
8231c6fdbd8SKent Overstreet #if 0
8241c6fdbd8SKent Overstreet void eytzinger1_test(void)
8251c6fdbd8SKent Overstreet {
8261c6fdbd8SKent Overstreet 	unsigned inorder, eytz, size;
8271c6fdbd8SKent Overstreet 
8281c6fdbd8SKent Overstreet 	pr_info("1 based eytzinger test:");
8291c6fdbd8SKent Overstreet 
8301c6fdbd8SKent Overstreet 	for (size = 2;
8311c6fdbd8SKent Overstreet 	     size < 65536;
8321c6fdbd8SKent Overstreet 	     size++) {
8331c6fdbd8SKent Overstreet 		unsigned extra = eytzinger1_extra(size);
8341c6fdbd8SKent Overstreet 
8351c6fdbd8SKent Overstreet 		if (!(size % 4096))
8361c6fdbd8SKent Overstreet 			pr_info("tree size %u", size);
8371c6fdbd8SKent Overstreet 
8381c6fdbd8SKent Overstreet 		BUG_ON(eytzinger1_prev(0, size) != eytzinger1_last(size));
8391c6fdbd8SKent Overstreet 		BUG_ON(eytzinger1_next(0, size) != eytzinger1_first(size));
8401c6fdbd8SKent Overstreet 
8411c6fdbd8SKent Overstreet 		BUG_ON(eytzinger1_prev(eytzinger1_first(size), size)	!= 0);
8421c6fdbd8SKent Overstreet 		BUG_ON(eytzinger1_next(eytzinger1_last(size), size)	!= 0);
8431c6fdbd8SKent Overstreet 
8441c6fdbd8SKent Overstreet 		inorder = 1;
8451c6fdbd8SKent Overstreet 		eytzinger1_for_each(eytz, size) {
8461c6fdbd8SKent Overstreet 			BUG_ON(__inorder_to_eytzinger1(inorder, size, extra) != eytz);
8471c6fdbd8SKent Overstreet 			BUG_ON(__eytzinger1_to_inorder(eytz, size, extra) != inorder);
8481c6fdbd8SKent Overstreet 			BUG_ON(eytz != eytzinger1_last(size) &&
8491c6fdbd8SKent Overstreet 			       eytzinger1_prev(eytzinger1_next(eytz, size), size) != eytz);
8501c6fdbd8SKent Overstreet 
8511c6fdbd8SKent Overstreet 			inorder++;
8521c6fdbd8SKent Overstreet 		}
8531c6fdbd8SKent Overstreet 	}
8541c6fdbd8SKent Overstreet }
8551c6fdbd8SKent Overstreet 
8561c6fdbd8SKent Overstreet void eytzinger0_test(void)
8571c6fdbd8SKent Overstreet {
8581c6fdbd8SKent Overstreet 
8591c6fdbd8SKent Overstreet 	unsigned inorder, eytz, size;
8601c6fdbd8SKent Overstreet 
8611c6fdbd8SKent Overstreet 	pr_info("0 based eytzinger test:");
8621c6fdbd8SKent Overstreet 
8631c6fdbd8SKent Overstreet 	for (size = 1;
8641c6fdbd8SKent Overstreet 	     size < 65536;
8651c6fdbd8SKent Overstreet 	     size++) {
8661c6fdbd8SKent Overstreet 		unsigned extra = eytzinger0_extra(size);
8671c6fdbd8SKent Overstreet 
8681c6fdbd8SKent Overstreet 		if (!(size % 4096))
8691c6fdbd8SKent Overstreet 			pr_info("tree size %u", size);
8701c6fdbd8SKent Overstreet 
8711c6fdbd8SKent Overstreet 		BUG_ON(eytzinger0_prev(-1, size) != eytzinger0_last(size));
8721c6fdbd8SKent Overstreet 		BUG_ON(eytzinger0_next(-1, size) != eytzinger0_first(size));
8731c6fdbd8SKent Overstreet 
8741c6fdbd8SKent Overstreet 		BUG_ON(eytzinger0_prev(eytzinger0_first(size), size)	!= -1);
8751c6fdbd8SKent Overstreet 		BUG_ON(eytzinger0_next(eytzinger0_last(size), size)	!= -1);
8761c6fdbd8SKent Overstreet 
8771c6fdbd8SKent Overstreet 		inorder = 0;
8781c6fdbd8SKent Overstreet 		eytzinger0_for_each(eytz, size) {
8791c6fdbd8SKent Overstreet 			BUG_ON(__inorder_to_eytzinger0(inorder, size, extra) != eytz);
8801c6fdbd8SKent Overstreet 			BUG_ON(__eytzinger0_to_inorder(eytz, size, extra) != inorder);
8811c6fdbd8SKent Overstreet 			BUG_ON(eytz != eytzinger0_last(size) &&
8821c6fdbd8SKent Overstreet 			       eytzinger0_prev(eytzinger0_next(eytz, size), size) != eytz);
8831c6fdbd8SKent Overstreet 
8841c6fdbd8SKent Overstreet 			inorder++;
8851c6fdbd8SKent Overstreet 		}
8861c6fdbd8SKent Overstreet 	}
8871c6fdbd8SKent Overstreet }
8881c6fdbd8SKent Overstreet 
8891c6fdbd8SKent Overstreet static inline int cmp_u16(const void *_l, const void *_r, size_t size)
8901c6fdbd8SKent Overstreet {
8911c6fdbd8SKent Overstreet 	const u16 *l = _l, *r = _r;
8921c6fdbd8SKent Overstreet 
8931c6fdbd8SKent Overstreet 	return (*l > *r) - (*r - *l);
8941c6fdbd8SKent Overstreet }
8951c6fdbd8SKent Overstreet 
8961c6fdbd8SKent Overstreet static void eytzinger0_find_test_val(u16 *test_array, unsigned nr, u16 search)
8971c6fdbd8SKent Overstreet {
8981c6fdbd8SKent Overstreet 	int i, c1 = -1, c2 = -1;
8991c6fdbd8SKent Overstreet 	ssize_t r;
9001c6fdbd8SKent Overstreet 
9011c6fdbd8SKent Overstreet 	r = eytzinger0_find_le(test_array, nr,
9021c6fdbd8SKent Overstreet 			       sizeof(test_array[0]),
9031c6fdbd8SKent Overstreet 			       cmp_u16, &search);
9041c6fdbd8SKent Overstreet 	if (r >= 0)
9051c6fdbd8SKent Overstreet 		c1 = test_array[r];
9061c6fdbd8SKent Overstreet 
9071c6fdbd8SKent Overstreet 	for (i = 0; i < nr; i++)
9081c6fdbd8SKent Overstreet 		if (test_array[i] <= search && test_array[i] > c2)
9091c6fdbd8SKent Overstreet 			c2 = test_array[i];
9101c6fdbd8SKent Overstreet 
9111c6fdbd8SKent Overstreet 	if (c1 != c2) {
9121c6fdbd8SKent Overstreet 		eytzinger0_for_each(i, nr)
9131c6fdbd8SKent Overstreet 			pr_info("[%3u] = %12u", i, test_array[i]);
9141c6fdbd8SKent Overstreet 		pr_info("find_le(%2u) -> [%2zi] = %2i should be %2i",
9151c6fdbd8SKent Overstreet 			i, r, c1, c2);
9161c6fdbd8SKent Overstreet 	}
9171c6fdbd8SKent Overstreet }
9181c6fdbd8SKent Overstreet 
9191c6fdbd8SKent Overstreet void eytzinger0_find_test(void)
9201c6fdbd8SKent Overstreet {
9211c6fdbd8SKent Overstreet 	unsigned i, nr, allocated = 1 << 12;
9221c6fdbd8SKent Overstreet 	u16 *test_array = kmalloc_array(allocated, sizeof(test_array[0]), GFP_KERNEL);
9231c6fdbd8SKent Overstreet 
9241c6fdbd8SKent Overstreet 	for (nr = 1; nr < allocated; nr++) {
9251c6fdbd8SKent Overstreet 		pr_info("testing %u elems", nr);
9261c6fdbd8SKent Overstreet 
9271c6fdbd8SKent Overstreet 		get_random_bytes(test_array, nr * sizeof(test_array[0]));
9281c6fdbd8SKent Overstreet 		eytzinger0_sort(test_array, nr, sizeof(test_array[0]), cmp_u16, NULL);
9291c6fdbd8SKent Overstreet 
9301c6fdbd8SKent Overstreet 		/* verify array is sorted correctly: */
9311c6fdbd8SKent Overstreet 		eytzinger0_for_each(i, nr)
9321c6fdbd8SKent Overstreet 			BUG_ON(i != eytzinger0_last(nr) &&
9331c6fdbd8SKent Overstreet 			       test_array[i] > test_array[eytzinger0_next(i, nr)]);
9341c6fdbd8SKent Overstreet 
9351c6fdbd8SKent Overstreet 		for (i = 0; i < U16_MAX; i += 1 << 12)
9361c6fdbd8SKent Overstreet 			eytzinger0_find_test_val(test_array, nr, i);
9371c6fdbd8SKent Overstreet 
9381c6fdbd8SKent Overstreet 		for (i = 0; i < nr; i++) {
9391c6fdbd8SKent Overstreet 			eytzinger0_find_test_val(test_array, nr, test_array[i] - 1);
9401c6fdbd8SKent Overstreet 			eytzinger0_find_test_val(test_array, nr, test_array[i]);
9411c6fdbd8SKent Overstreet 			eytzinger0_find_test_val(test_array, nr, test_array[i] + 1);
9421c6fdbd8SKent Overstreet 		}
9431c6fdbd8SKent Overstreet 	}
9441c6fdbd8SKent Overstreet 
9451c6fdbd8SKent Overstreet 	kfree(test_array);
9461c6fdbd8SKent Overstreet }
9471c6fdbd8SKent Overstreet #endif
9487ef2a73aSKent Overstreet 
9497ef2a73aSKent Overstreet /*
9507ef2a73aSKent Overstreet  * Accumulate percpu counters onto one cpu's copy - only valid when access
9517ef2a73aSKent Overstreet  * against any percpu counter is guarded against
9527ef2a73aSKent Overstreet  */
9537ef2a73aSKent Overstreet u64 *bch2_acc_percpu_u64s(u64 __percpu *p, unsigned nr)
9547ef2a73aSKent Overstreet {
955107fe5afSKent Overstreet 	u64 *ret;
9567ef2a73aSKent Overstreet 	int cpu;
9577ef2a73aSKent Overstreet 
958107fe5afSKent Overstreet 	/* access to pcpu vars has to be blocked by other locking */
959107fe5afSKent Overstreet 	preempt_disable();
960107fe5afSKent Overstreet 	ret = this_cpu_ptr(p);
961107fe5afSKent Overstreet 	preempt_enable();
962107fe5afSKent Overstreet 
9637ef2a73aSKent Overstreet 	for_each_possible_cpu(cpu) {
9647ef2a73aSKent Overstreet 		u64 *i = per_cpu_ptr(p, cpu);
9657ef2a73aSKent Overstreet 
9667ef2a73aSKent Overstreet 		if (i != ret) {
9677ef2a73aSKent Overstreet 			acc_u64s(ret, i, nr);
9687ef2a73aSKent Overstreet 			memset(i, 0, nr * sizeof(u64));
9697ef2a73aSKent Overstreet 		}
9707ef2a73aSKent Overstreet 	}
9717ef2a73aSKent Overstreet 
9727ef2a73aSKent Overstreet 	return ret;
9737ef2a73aSKent Overstreet }
974