xref: /linux/fs/bcachefs/util.c (revision c13fbb7de2fc4fd61a44ebfa4ba182f35e0a3286)
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>
11a8f35428SKent Overstreet #include <linux/console.h>
121c6fdbd8SKent Overstreet #include <linux/ctype.h>
131c6fdbd8SKent Overstreet #include <linux/debugfs.h>
141c6fdbd8SKent Overstreet #include <linux/freezer.h>
151c6fdbd8SKent Overstreet #include <linux/kthread.h>
161c6fdbd8SKent Overstreet #include <linux/log2.h>
171c6fdbd8SKent Overstreet #include <linux/math64.h>
181c6fdbd8SKent Overstreet #include <linux/percpu.h>
191c6fdbd8SKent Overstreet #include <linux/preempt.h>
201c6fdbd8SKent Overstreet #include <linux/random.h>
211c6fdbd8SKent Overstreet #include <linux/seq_file.h>
221c6fdbd8SKent Overstreet #include <linux/string.h>
231c6fdbd8SKent Overstreet #include <linux/types.h>
241c6fdbd8SKent Overstreet #include <linux/sched/clock.h>
251c6fdbd8SKent Overstreet 
261c6fdbd8SKent Overstreet #include "eytzinger.h"
27bf8f8b20SDaniel Hill #include "mean_and_variance.h"
281c6fdbd8SKent Overstreet #include "util.h"
291c6fdbd8SKent Overstreet 
301c6fdbd8SKent Overstreet static const char si_units[] = "?kMGTPEZY";
311c6fdbd8SKent Overstreet 
32a5d18f9eSKent Overstreet /* string_get_size units: */
33a5d18f9eSKent Overstreet static const char *const units_2[] = {
34a5d18f9eSKent Overstreet 	"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"
35a5d18f9eSKent Overstreet };
36a5d18f9eSKent Overstreet static const char *const units_10[] = {
37a5d18f9eSKent Overstreet 	"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
38a5d18f9eSKent Overstreet };
391c6fdbd8SKent Overstreet 
40a5d18f9eSKent Overstreet static int parse_u64(const char *cp, u64 *res)
41a5d18f9eSKent Overstreet {
42a5d18f9eSKent Overstreet 	const char *start = cp;
43a5d18f9eSKent Overstreet 	u64 v = 0;
441c6fdbd8SKent Overstreet 
451c6fdbd8SKent Overstreet 	if (!isdigit(*cp))
461c6fdbd8SKent Overstreet 		return -EINVAL;
471c6fdbd8SKent Overstreet 
481c6fdbd8SKent Overstreet 	do {
491c6fdbd8SKent Overstreet 		if (v > U64_MAX / 10)
501c6fdbd8SKent Overstreet 			return -ERANGE;
511c6fdbd8SKent Overstreet 		v *= 10;
521c6fdbd8SKent Overstreet 		if (v > U64_MAX - (*cp - '0'))
531c6fdbd8SKent Overstreet 			return -ERANGE;
541c6fdbd8SKent Overstreet 		v += *cp - '0';
551c6fdbd8SKent Overstreet 		cp++;
561c6fdbd8SKent Overstreet 	} while (isdigit(*cp));
571c6fdbd8SKent Overstreet 
58a5d18f9eSKent Overstreet 	*res = v;
59a5d18f9eSKent Overstreet 	return cp - start;
60a5d18f9eSKent Overstreet }
61a5d18f9eSKent Overstreet 
62a5d18f9eSKent Overstreet static int bch2_pow(u64 n, u64 p, u64 *res)
63a5d18f9eSKent Overstreet {
64a5d18f9eSKent Overstreet 	*res = 1;
65a5d18f9eSKent Overstreet 
66a5d18f9eSKent Overstreet 	while (p--) {
67a5d18f9eSKent Overstreet 		if (*res > div_u64(U64_MAX, n))
68a5d18f9eSKent Overstreet 			return -ERANGE;
69a5d18f9eSKent Overstreet 		*res *= n;
70a5d18f9eSKent Overstreet 	}
71a5d18f9eSKent Overstreet 	return 0;
72a5d18f9eSKent Overstreet }
73a5d18f9eSKent Overstreet 
74a5d18f9eSKent Overstreet static int parse_unit_suffix(const char *cp, u64 *res)
75a5d18f9eSKent Overstreet {
76a5d18f9eSKent Overstreet 	const char *start = cp;
77a5d18f9eSKent Overstreet 	u64 base = 1024;
78a5d18f9eSKent Overstreet 	unsigned u;
79a5d18f9eSKent Overstreet 	int ret;
80a5d18f9eSKent Overstreet 
81a5d18f9eSKent Overstreet 	if (*cp == ' ')
82a5d18f9eSKent Overstreet 		cp++;
83a5d18f9eSKent Overstreet 
841c6fdbd8SKent Overstreet 	for (u = 1; u < strlen(si_units); u++)
851c6fdbd8SKent Overstreet 		if (*cp == si_units[u]) {
861c6fdbd8SKent Overstreet 			cp++;
871c6fdbd8SKent Overstreet 			goto got_unit;
881c6fdbd8SKent Overstreet 		}
89a5d18f9eSKent Overstreet 
90a5d18f9eSKent Overstreet 	for (u = 0; u < ARRAY_SIZE(units_2); u++)
91a5d18f9eSKent Overstreet 		if (!strncmp(cp, units_2[u], strlen(units_2[u]))) {
92a5d18f9eSKent Overstreet 			cp += strlen(units_2[u]);
93a5d18f9eSKent Overstreet 			goto got_unit;
94a5d18f9eSKent Overstreet 		}
95a5d18f9eSKent Overstreet 
96a5d18f9eSKent Overstreet 	for (u = 0; u < ARRAY_SIZE(units_10); u++)
97a5d18f9eSKent Overstreet 		if (!strncmp(cp, units_10[u], strlen(units_10[u]))) {
98a5d18f9eSKent Overstreet 			cp += strlen(units_10[u]);
99a5d18f9eSKent Overstreet 			base = 1000;
100a5d18f9eSKent Overstreet 			goto got_unit;
101a5d18f9eSKent Overstreet 		}
102a5d18f9eSKent Overstreet 
103a5d18f9eSKent Overstreet 	*res = 1;
104a5d18f9eSKent Overstreet 	return 0;
1051c6fdbd8SKent Overstreet got_unit:
106a5d18f9eSKent Overstreet 	ret = bch2_pow(base, u, res);
107a5d18f9eSKent Overstreet 	if (ret)
108a5d18f9eSKent Overstreet 		return ret;
109a5d18f9eSKent Overstreet 
110a5d18f9eSKent Overstreet 	return cp - start;
111a5d18f9eSKent Overstreet }
112a5d18f9eSKent Overstreet 
113a5d18f9eSKent Overstreet #define parse_or_ret(cp, _f)			\
114a5d18f9eSKent Overstreet do {						\
11596dea3d5SKent Overstreet 	int _ret = _f;				\
11696dea3d5SKent Overstreet 	if (_ret < 0)				\
11796dea3d5SKent Overstreet 		return _ret;			\
11896dea3d5SKent Overstreet 	cp += _ret;				\
119a5d18f9eSKent Overstreet } while (0)
120a5d18f9eSKent Overstreet 
121a5d18f9eSKent Overstreet static int __bch2_strtou64_h(const char *cp, u64 *res)
122a5d18f9eSKent Overstreet {
123a5d18f9eSKent Overstreet 	const char *start = cp;
124a5d18f9eSKent Overstreet 	u64 v = 0, b, f_n = 0, f_d = 1;
125a5d18f9eSKent Overstreet 	int ret;
126a5d18f9eSKent Overstreet 
127a5d18f9eSKent Overstreet 	parse_or_ret(cp, parse_u64(cp, &v));
128a5d18f9eSKent Overstreet 
129a5d18f9eSKent Overstreet 	if (*cp == '.') {
130a5d18f9eSKent Overstreet 		cp++;
131a5d18f9eSKent Overstreet 		ret = parse_u64(cp, &f_n);
132a5d18f9eSKent Overstreet 		if (ret < 0)
133a5d18f9eSKent Overstreet 			return ret;
134a5d18f9eSKent Overstreet 		cp += ret;
135a5d18f9eSKent Overstreet 
136a5d18f9eSKent Overstreet 		ret = bch2_pow(10, ret, &f_d);
137a5d18f9eSKent Overstreet 		if (ret)
138a5d18f9eSKent Overstreet 			return ret;
139a5d18f9eSKent Overstreet 	}
140a5d18f9eSKent Overstreet 
141a5d18f9eSKent Overstreet 	parse_or_ret(cp, parse_unit_suffix(cp, &b));
142a5d18f9eSKent Overstreet 
143a5d18f9eSKent Overstreet 	if (v > div_u64(U64_MAX, b))
144a5d18f9eSKent Overstreet 		return -ERANGE;
145a5d18f9eSKent Overstreet 	v *= b;
146a5d18f9eSKent Overstreet 
147a5d18f9eSKent Overstreet 	if (f_n > div_u64(U64_MAX, b))
148a5d18f9eSKent Overstreet 		return -ERANGE;
149a5d18f9eSKent Overstreet 
150a5d18f9eSKent Overstreet 	f_n = div_u64(f_n * b, f_d);
151a5d18f9eSKent Overstreet 	if (v + f_n < v)
152a5d18f9eSKent Overstreet 		return -ERANGE;
153a5d18f9eSKent Overstreet 	v += f_n;
154a5d18f9eSKent Overstreet 
155a5d18f9eSKent Overstreet 	*res = v;
156a5d18f9eSKent Overstreet 	return cp - start;
157a5d18f9eSKent Overstreet }
158a5d18f9eSKent Overstreet 
159a5d18f9eSKent Overstreet static int __bch2_strtoh(const char *cp, u64 *res,
160a5d18f9eSKent Overstreet 			 u64 t_max, bool t_signed)
161a5d18f9eSKent Overstreet {
162a5d18f9eSKent Overstreet 	bool positive = *cp != '-';
163a5d18f9eSKent Overstreet 	u64 v = 0;
164a5d18f9eSKent Overstreet 
165a5d18f9eSKent Overstreet 	if (*cp == '+' || *cp == '-')
166a5d18f9eSKent Overstreet 		cp++;
167a5d18f9eSKent Overstreet 
168a5d18f9eSKent Overstreet 	parse_or_ret(cp, __bch2_strtou64_h(cp, &v));
169a5d18f9eSKent Overstreet 
1701c6fdbd8SKent Overstreet 	if (*cp == '\n')
1711c6fdbd8SKent Overstreet 		cp++;
1721c6fdbd8SKent Overstreet 	if (*cp)
1731c6fdbd8SKent Overstreet 		return -EINVAL;
1741c6fdbd8SKent Overstreet 
1751c6fdbd8SKent Overstreet 	if (positive) {
1761c6fdbd8SKent Overstreet 		if (v > t_max)
1771c6fdbd8SKent Overstreet 			return -ERANGE;
1781c6fdbd8SKent Overstreet 	} else {
1791c6fdbd8SKent Overstreet 		if (v && !t_signed)
1801c6fdbd8SKent Overstreet 			return -ERANGE;
1811c6fdbd8SKent Overstreet 
1821c6fdbd8SKent Overstreet 		if (v > t_max + 1)
1831c6fdbd8SKent Overstreet 			return -ERANGE;
1841c6fdbd8SKent Overstreet 		v = -v;
1851c6fdbd8SKent Overstreet 	}
1861c6fdbd8SKent Overstreet 
1871c6fdbd8SKent Overstreet 	*res = v;
1881c6fdbd8SKent Overstreet 	return 0;
1891c6fdbd8SKent Overstreet }
1901c6fdbd8SKent Overstreet 
1911c6fdbd8SKent Overstreet #define STRTO_H(name, type)					\
1921c6fdbd8SKent Overstreet int bch2_ ## name ## _h(const char *cp, type *res)		\
1931c6fdbd8SKent Overstreet {								\
194a5d18f9eSKent Overstreet 	u64 v = 0;						\
1951c6fdbd8SKent Overstreet 	int ret = __bch2_strtoh(cp, &v, ANYSINT_MAX(type),	\
1961c6fdbd8SKent Overstreet 			ANYSINT_MAX(type) != ((type) ~0ULL));	\
1971c6fdbd8SKent Overstreet 	*res = v;						\
1981c6fdbd8SKent Overstreet 	return ret;						\
1991c6fdbd8SKent Overstreet }
2001c6fdbd8SKent Overstreet 
2011c6fdbd8SKent Overstreet STRTO_H(strtoint, int)
2021c6fdbd8SKent Overstreet STRTO_H(strtouint, unsigned int)
2031c6fdbd8SKent Overstreet STRTO_H(strtoll, long long)
2041c6fdbd8SKent Overstreet STRTO_H(strtoull, unsigned long long)
2050b847a19SKent Overstreet STRTO_H(strtou64, u64)
2061c6fdbd8SKent Overstreet 
2071c6fdbd8SKent Overstreet u64 bch2_read_flag_list(char *opt, const char * const list[])
2081c6fdbd8SKent Overstreet {
2091c6fdbd8SKent Overstreet 	u64 ret = 0;
2109d8022dbSKent Overstreet 	char *p, *s, *d = kstrdup(opt, GFP_KERNEL);
2111c6fdbd8SKent Overstreet 
2121c6fdbd8SKent Overstreet 	if (!d)
2131c6fdbd8SKent Overstreet 		return -ENOMEM;
2141c6fdbd8SKent Overstreet 
2151c6fdbd8SKent Overstreet 	s = strim(d);
2161c6fdbd8SKent Overstreet 
2171c6fdbd8SKent Overstreet 	while ((p = strsep(&s, ","))) {
2181c6fdbd8SKent Overstreet 		int flag = match_string(list, -1, p);
2191e81f89bSKent Overstreet 
2201c6fdbd8SKent Overstreet 		if (flag < 0) {
2211c6fdbd8SKent Overstreet 			ret = -1;
2221c6fdbd8SKent Overstreet 			break;
2231c6fdbd8SKent Overstreet 		}
2241c6fdbd8SKent Overstreet 
2251c6fdbd8SKent Overstreet 		ret |= 1 << flag;
2261c6fdbd8SKent Overstreet 	}
2271c6fdbd8SKent Overstreet 
2281c6fdbd8SKent Overstreet 	kfree(d);
2291c6fdbd8SKent Overstreet 
2301c6fdbd8SKent Overstreet 	return ret;
2311c6fdbd8SKent Overstreet }
2321c6fdbd8SKent Overstreet 
2331c6fdbd8SKent Overstreet bool bch2_is_zero(const void *_p, size_t n)
2341c6fdbd8SKent Overstreet {
2351c6fdbd8SKent Overstreet 	const char *p = _p;
2361c6fdbd8SKent Overstreet 	size_t i;
2371c6fdbd8SKent Overstreet 
2381c6fdbd8SKent Overstreet 	for (i = 0; i < n; i++)
2391c6fdbd8SKent Overstreet 		if (p[i])
2401c6fdbd8SKent Overstreet 			return false;
2411c6fdbd8SKent Overstreet 	return true;
2421c6fdbd8SKent Overstreet }
2431c6fdbd8SKent Overstreet 
244d0b50524SKent Overstreet void bch2_prt_u64_binary(struct printbuf *out, u64 v, unsigned nr_bits)
245d0b50524SKent Overstreet {
246d0b50524SKent Overstreet 	while (nr_bits)
247d0b50524SKent Overstreet 		prt_char(out, '0' + ((v >> --nr_bits) & 1));
248d0b50524SKent Overstreet }
249d0b50524SKent Overstreet 
250a8f35428SKent Overstreet void bch2_print_string_as_lines(const char *prefix, const char *lines)
251a8f35428SKent Overstreet {
252a8f35428SKent Overstreet 	const char *p;
253a8f35428SKent Overstreet 
254a8f35428SKent Overstreet 	if (!lines) {
255a8f35428SKent Overstreet 		printk("%s (null)\n", prefix);
256a8f35428SKent Overstreet 		return;
257a8f35428SKent Overstreet 	}
258a8f35428SKent Overstreet 
259a8f35428SKent Overstreet 	console_lock();
260a8f35428SKent Overstreet 	while (1) {
261a8f35428SKent Overstreet 		p = strchrnul(lines, '\n');
262a8f35428SKent Overstreet 		printk("%s%.*s\n", prefix, (int) (p - lines), lines);
263a8f35428SKent Overstreet 		if (!*p)
264a8f35428SKent Overstreet 			break;
265a8f35428SKent Overstreet 		lines = p + 1;
266a8f35428SKent Overstreet 	}
267a8f35428SKent Overstreet 	console_unlock();
268a8f35428SKent Overstreet }
269a8f35428SKent Overstreet 
270*c13fbb7dSKent Overstreet int bch2_save_backtrace(bch_stacktrace *stack, struct task_struct *task, unsigned skipnr)
2711148a97fSKent Overstreet {
272e5570df2SKent Overstreet #ifdef CONFIG_STACKTRACE
2733ea4219dSKent Overstreet 	unsigned nr_entries = 0;
2743ea4219dSKent Overstreet 	int ret = 0;
2753ea4219dSKent Overstreet 
2763ea4219dSKent Overstreet 	stack->nr = 0;
2773ea4219dSKent Overstreet 	ret = darray_make_room(stack, 32);
2783ea4219dSKent Overstreet 	if (ret)
2793ea4219dSKent Overstreet 		return ret;
2801148a97fSKent Overstreet 
2813e57db65SKent Overstreet 	if (!down_read_trylock(&task->signal->exec_update_lock))
2823ea4219dSKent Overstreet 		return -1;
2831148a97fSKent Overstreet 
2843ea4219dSKent Overstreet 	do {
285*c13fbb7dSKent Overstreet 		nr_entries = stack_trace_save_tsk(task, stack->data, stack->size, skipnr + 1);
2863ea4219dSKent Overstreet 	} while (nr_entries == stack->size &&
2873ea4219dSKent Overstreet 		 !(ret = darray_make_room(stack, stack->size * 2)));
2883ea4219dSKent Overstreet 
2893ea4219dSKent Overstreet 	stack->nr = nr_entries;
2903ea4219dSKent Overstreet 	up_read(&task->signal->exec_update_lock);
2913ea4219dSKent Overstreet 
2923ea4219dSKent Overstreet 	return ret;
293e5570df2SKent Overstreet #else
294e5570df2SKent Overstreet 	return 0;
295e5570df2SKent Overstreet #endif
2961148a97fSKent Overstreet }
2971148a97fSKent Overstreet 
2983ea4219dSKent Overstreet void bch2_prt_backtrace(struct printbuf *out, bch_stacktrace *stack)
2993ea4219dSKent Overstreet {
3003ea4219dSKent Overstreet 	darray_for_each(*stack, i) {
3013ea4219dSKent Overstreet 		prt_printf(out, "[<0>] %pB", (void *) *i);
3023ea4219dSKent Overstreet 		prt_newline(out);
3033ea4219dSKent Overstreet 	}
3043ea4219dSKent Overstreet }
3053ea4219dSKent Overstreet 
306*c13fbb7dSKent Overstreet int bch2_prt_task_backtrace(struct printbuf *out, struct task_struct *task, unsigned skipnr)
3073ea4219dSKent Overstreet {
3083ea4219dSKent Overstreet 	bch_stacktrace stack = { 0 };
309*c13fbb7dSKent Overstreet 	int ret = bch2_save_backtrace(&stack, task, skipnr + 1);
3103ea4219dSKent Overstreet 
3113ea4219dSKent Overstreet 	bch2_prt_backtrace(out, &stack);
3123ea4219dSKent Overstreet 	darray_exit(&stack);
3133ea4219dSKent Overstreet 	return ret;
3141148a97fSKent Overstreet }
3151148a97fSKent Overstreet 
316066a2646SKent Overstreet #ifndef __KERNEL__
317066a2646SKent Overstreet #include <time.h>
318066a2646SKent Overstreet void bch2_prt_datetime(struct printbuf *out, time64_t sec)
319066a2646SKent Overstreet {
320066a2646SKent Overstreet 	time_t t = sec;
321066a2646SKent Overstreet 	char buf[64];
322066a2646SKent Overstreet 	ctime_r(&t, buf);
3235fd24cafSKent Overstreet 	strim(buf);
324066a2646SKent Overstreet 	prt_str(out, buf);
325066a2646SKent Overstreet }
326066a2646SKent Overstreet #else
327066a2646SKent Overstreet void bch2_prt_datetime(struct printbuf *out, time64_t sec)
328066a2646SKent Overstreet {
329066a2646SKent Overstreet 	char buf[64];
330066a2646SKent Overstreet 	snprintf(buf, sizeof(buf), "%ptT", &sec);
331066a2646SKent Overstreet 	prt_u64(out, sec);
332066a2646SKent Overstreet }
333066a2646SKent Overstreet #endif
334066a2646SKent Overstreet 
335066a2646SKent Overstreet static const struct time_unit {
336066a2646SKent Overstreet 	const char	*name;
337066a2646SKent Overstreet 	u64		nsecs;
338066a2646SKent Overstreet } time_units[] = {
339066a2646SKent Overstreet 	{ "ns",		1		 },
340066a2646SKent Overstreet 	{ "us",		NSEC_PER_USEC	 },
341066a2646SKent Overstreet 	{ "ms",		NSEC_PER_MSEC	 },
342066a2646SKent Overstreet 	{ "s",		NSEC_PER_SEC	 },
343066a2646SKent Overstreet 	{ "m",          (u64) NSEC_PER_SEC * 60},
344066a2646SKent Overstreet 	{ "h",          (u64) NSEC_PER_SEC * 3600},
345066a2646SKent Overstreet 	{ "eon",        U64_MAX          },
346066a2646SKent Overstreet };
347066a2646SKent Overstreet 
348066a2646SKent Overstreet static const struct time_unit *pick_time_units(u64 ns)
349066a2646SKent Overstreet {
350066a2646SKent Overstreet 	const struct time_unit *u;
351066a2646SKent Overstreet 
352066a2646SKent Overstreet 	for (u = time_units;
353066a2646SKent Overstreet 	     u + 1 < time_units + ARRAY_SIZE(time_units) &&
354066a2646SKent Overstreet 	     ns >= u[1].nsecs << 1;
355066a2646SKent Overstreet 	     u++)
356066a2646SKent Overstreet 		;
357066a2646SKent Overstreet 
358066a2646SKent Overstreet 	return u;
359066a2646SKent Overstreet }
360066a2646SKent Overstreet 
361066a2646SKent Overstreet void bch2_pr_time_units(struct printbuf *out, u64 ns)
362066a2646SKent Overstreet {
363066a2646SKent Overstreet 	const struct time_unit *u = pick_time_units(ns);
364066a2646SKent Overstreet 
365066a2646SKent Overstreet 	prt_printf(out, "%llu %s", div_u64(ns, u->nsecs), u->name);
366066a2646SKent Overstreet }
367066a2646SKent Overstreet 
3681c6fdbd8SKent Overstreet /* time stats: */
3691c6fdbd8SKent Overstreet 
3701c6fdbd8SKent Overstreet #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
3711c6fdbd8SKent Overstreet static void bch2_quantiles_update(struct bch2_quantiles *q, u64 v)
3721c6fdbd8SKent Overstreet {
3731c6fdbd8SKent Overstreet 	unsigned i = 0;
3741c6fdbd8SKent Overstreet 
3751c6fdbd8SKent Overstreet 	while (i < ARRAY_SIZE(q->entries)) {
3761c6fdbd8SKent Overstreet 		struct bch2_quantile_entry *e = q->entries + i;
3771c6fdbd8SKent Overstreet 
3781c6fdbd8SKent Overstreet 		if (unlikely(!e->step)) {
3791c6fdbd8SKent Overstreet 			e->m = v;
3801c6fdbd8SKent Overstreet 			e->step = max_t(unsigned, v / 2, 1024);
3811c6fdbd8SKent Overstreet 		} else if (e->m > v) {
3821c6fdbd8SKent Overstreet 			e->m = e->m >= e->step
3831c6fdbd8SKent Overstreet 				? e->m - e->step
3841c6fdbd8SKent Overstreet 				: 0;
3851c6fdbd8SKent Overstreet 		} else if (e->m < v) {
3861c6fdbd8SKent Overstreet 			e->m = e->m + e->step > e->m
3871c6fdbd8SKent Overstreet 				? e->m + e->step
3881c6fdbd8SKent Overstreet 				: U32_MAX;
3891c6fdbd8SKent Overstreet 		}
3901c6fdbd8SKent Overstreet 
3911c6fdbd8SKent Overstreet 		if ((e->m > v ? e->m - v : v - e->m) < e->step)
3921c6fdbd8SKent Overstreet 			e->step = max_t(unsigned, e->step / 2, 1);
3931c6fdbd8SKent Overstreet 
3941c6fdbd8SKent Overstreet 		if (v >= e->m)
3951c6fdbd8SKent Overstreet 			break;
3961c6fdbd8SKent Overstreet 
3971c6fdbd8SKent Overstreet 		i = eytzinger0_child(i, v > e->m);
3981c6fdbd8SKent Overstreet 	}
3991c6fdbd8SKent Overstreet }
4001c6fdbd8SKent Overstreet 
40130c92ffeSKent Overstreet static inline void bch2_time_stats_update_one(struct bch2_time_stats *stats,
4021c6fdbd8SKent Overstreet 					      u64 start, u64 end)
4031c6fdbd8SKent Overstreet {
4041c6fdbd8SKent Overstreet 	u64 duration, freq;
4051c6fdbd8SKent Overstreet 
406bf8f8b20SDaniel Hill 	if (time_after64(end, start)) {
407bf8f8b20SDaniel Hill 		duration = end - start;
40865bc4109SKent Overstreet 		mean_and_variance_update(&stats->duration_stats, duration);
409bf8f8b20SDaniel Hill 		mean_and_variance_weighted_update(&stats->duration_stats_weighted, duration);
4101c6fdbd8SKent Overstreet 		stats->max_duration = max(stats->max_duration, duration);
411bf8f8b20SDaniel Hill 		stats->min_duration = min(stats->min_duration, duration);
412066a2646SKent Overstreet 		stats->total_duration += duration;
4131c6fdbd8SKent Overstreet 		bch2_quantiles_update(&stats->quantiles, duration);
4141c6fdbd8SKent Overstreet 	}
4151c6fdbd8SKent Overstreet 
416bf8f8b20SDaniel Hill 	if (time_after64(end, stats->last_event)) {
417bf8f8b20SDaniel Hill 		freq = end - stats->last_event;
41865bc4109SKent Overstreet 		mean_and_variance_update(&stats->freq_stats, freq);
419bf8f8b20SDaniel Hill 		mean_and_variance_weighted_update(&stats->freq_stats_weighted, freq);
420bf8f8b20SDaniel Hill 		stats->max_freq = max(stats->max_freq, freq);
421bf8f8b20SDaniel Hill 		stats->min_freq = min(stats->min_freq, freq);
422bf8f8b20SDaniel Hill 		stats->last_event = end;
423bf8f8b20SDaniel Hill 	}
424bf8f8b20SDaniel Hill }
425bf8f8b20SDaniel Hill 
426066a2646SKent Overstreet static void __bch2_time_stats_clear_buffer(struct bch2_time_stats *stats,
42730c92ffeSKent Overstreet 					   struct bch2_time_stat_buffer *b)
42830c92ffeSKent Overstreet {
429066a2646SKent Overstreet 	for (struct bch2_time_stat_buffer_entry *i = b->entries;
43030c92ffeSKent Overstreet 	     i < b->entries + ARRAY_SIZE(b->entries);
43130c92ffeSKent Overstreet 	     i++)
43230c92ffeSKent Overstreet 		bch2_time_stats_update_one(stats, i->start, i->end);
43330c92ffeSKent Overstreet 	b->nr = 0;
43430c92ffeSKent Overstreet }
43530c92ffeSKent Overstreet 
436066a2646SKent Overstreet static noinline void bch2_time_stats_clear_buffer(struct bch2_time_stats *stats,
437066a2646SKent Overstreet 						  struct bch2_time_stat_buffer *b)
438066a2646SKent Overstreet {
439066a2646SKent Overstreet 	unsigned long flags;
440066a2646SKent Overstreet 
441066a2646SKent Overstreet 	spin_lock_irqsave(&stats->lock, flags);
442066a2646SKent Overstreet 	__bch2_time_stats_clear_buffer(stats, b);
443066a2646SKent Overstreet 	spin_unlock_irqrestore(&stats->lock, flags);
444066a2646SKent Overstreet }
445066a2646SKent Overstreet 
4461c6fdbd8SKent Overstreet void __bch2_time_stats_update(struct bch2_time_stats *stats, u64 start, u64 end)
4471c6fdbd8SKent Overstreet {
4481c6fdbd8SKent Overstreet 	unsigned long flags;
4491c6fdbd8SKent Overstreet 
450ef23397cSKent Overstreet 	WARN_ONCE(!stats->duration_stats_weighted.weight ||
451ef23397cSKent Overstreet 		  !stats->freq_stats_weighted.weight,
452ef23397cSKent Overstreet 		  "uninitialized time_stats");
453bf8f8b20SDaniel Hill 
4541c6fdbd8SKent Overstreet 	if (!stats->buffer) {
4551c6fdbd8SKent Overstreet 		spin_lock_irqsave(&stats->lock, flags);
4561c6fdbd8SKent Overstreet 		bch2_time_stats_update_one(stats, start, end);
4571c6fdbd8SKent Overstreet 
458bf8f8b20SDaniel Hill 		if (mean_and_variance_weighted_get_mean(stats->freq_stats_weighted) < 32 &&
459bf8f8b20SDaniel Hill 		    stats->duration_stats.n > 1024)
4601c6fdbd8SKent Overstreet 			stats->buffer =
4611c6fdbd8SKent Overstreet 				alloc_percpu_gfp(struct bch2_time_stat_buffer,
4621c6fdbd8SKent Overstreet 						 GFP_ATOMIC);
4631c6fdbd8SKent Overstreet 		spin_unlock_irqrestore(&stats->lock, flags);
4641c6fdbd8SKent Overstreet 	} else {
4651c6fdbd8SKent Overstreet 		struct bch2_time_stat_buffer *b;
4661c6fdbd8SKent Overstreet 
4671c6fdbd8SKent Overstreet 		preempt_disable();
4681c6fdbd8SKent Overstreet 		b = this_cpu_ptr(stats->buffer);
4691c6fdbd8SKent Overstreet 
4701c6fdbd8SKent Overstreet 		BUG_ON(b->nr >= ARRAY_SIZE(b->entries));
4711c6fdbd8SKent Overstreet 		b->entries[b->nr++] = (struct bch2_time_stat_buffer_entry) {
4721c6fdbd8SKent Overstreet 			.start = start,
4731c6fdbd8SKent Overstreet 			.end = end
4741c6fdbd8SKent Overstreet 		};
4751c6fdbd8SKent Overstreet 
47630c92ffeSKent Overstreet 		if (unlikely(b->nr == ARRAY_SIZE(b->entries)))
47730c92ffeSKent Overstreet 			bch2_time_stats_clear_buffer(stats, b);
4781c6fdbd8SKent Overstreet 		preempt_enable();
4791c6fdbd8SKent Overstreet 	}
4801c6fdbd8SKent Overstreet }
4811c6fdbd8SKent Overstreet 
482bf8f8b20SDaniel Hill static void bch2_pr_time_units_aligned(struct printbuf *out, u64 ns)
483bf8f8b20SDaniel Hill {
484bf8f8b20SDaniel Hill 	const struct time_unit *u = pick_time_units(ns);
485bf8f8b20SDaniel Hill 
486bf8f8b20SDaniel Hill 	prt_printf(out, "%llu ", div64_u64(ns, u->nsecs));
487bf8f8b20SDaniel Hill 	prt_tab_rjust(out);
488bf8f8b20SDaniel Hill 	prt_printf(out, "%s", u->name);
489bf8f8b20SDaniel Hill }
490bf8f8b20SDaniel Hill 
491bf8f8b20SDaniel Hill static inline void pr_name_and_units(struct printbuf *out, const char *name, u64 ns)
492bf8f8b20SDaniel Hill {
49355b8550dSDaniel B. Hill 	prt_str(out, name);
494bf8f8b20SDaniel Hill 	prt_tab(out);
495bf8f8b20SDaniel Hill 	bch2_pr_time_units_aligned(out, ns);
496bf8f8b20SDaniel Hill 	prt_newline(out);
497bf8f8b20SDaniel Hill }
498bf8f8b20SDaniel Hill 
499066a2646SKent Overstreet #define TABSTOP_SIZE 12
500066a2646SKent Overstreet 
5017807e143SKent Overstreet void bch2_time_stats_to_text(struct printbuf *out, struct bch2_time_stats *stats)
5021c6fdbd8SKent Overstreet {
5031c6fdbd8SKent Overstreet 	const struct time_unit *u;
504bf8f8b20SDaniel Hill 	s64 f_mean = 0, d_mean = 0;
505bf8f8b20SDaniel Hill 	u64 q, last_q = 0, f_stddev = 0, d_stddev = 0;
5061c6fdbd8SKent Overstreet 	int i;
507066a2646SKent Overstreet 
508066a2646SKent Overstreet 	if (stats->buffer) {
509066a2646SKent Overstreet 		int cpu;
510066a2646SKent Overstreet 
511066a2646SKent Overstreet 		spin_lock_irq(&stats->lock);
512066a2646SKent Overstreet 		for_each_possible_cpu(cpu)
513066a2646SKent Overstreet 			__bch2_time_stats_clear_buffer(stats, per_cpu_ptr(stats->buffer, cpu));
514066a2646SKent Overstreet 		spin_unlock_irq(&stats->lock);
515066a2646SKent Overstreet 	}
516066a2646SKent Overstreet 
517bf8f8b20SDaniel Hill 	/*
518bf8f8b20SDaniel Hill 	 * avoid divide by zero
519bf8f8b20SDaniel Hill 	 */
520bf8f8b20SDaniel Hill 	if (stats->freq_stats.n) {
521bf8f8b20SDaniel Hill 		f_mean = mean_and_variance_get_mean(stats->freq_stats);
522bf8f8b20SDaniel Hill 		f_stddev = mean_and_variance_get_stddev(stats->freq_stats);
523bf8f8b20SDaniel Hill 		d_mean = mean_and_variance_get_mean(stats->duration_stats);
524bf8f8b20SDaniel Hill 		d_stddev = mean_and_variance_get_stddev(stats->duration_stats);
525bf8f8b20SDaniel Hill 	}
5261c6fdbd8SKent Overstreet 
527bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, out->indent + TABSTOP_SIZE);
528bf8f8b20SDaniel Hill 	prt_printf(out, "count:");
529bf8f8b20SDaniel Hill 	prt_tab(out);
530bf8f8b20SDaniel Hill 	prt_printf(out, "%llu ",
531bf8f8b20SDaniel Hill 			 stats->duration_stats.n);
532bf8f8b20SDaniel Hill 	printbuf_tabstop_pop(out);
53325055c69SDaniel Hill 	prt_newline(out);
5341c6fdbd8SKent Overstreet 
535bf8f8b20SDaniel Hill 	printbuf_tabstops_reset(out);
5361c6fdbd8SKent Overstreet 
537bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, out->indent + 20);
538bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, TABSTOP_SIZE + 2);
539bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, 0);
540bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, TABSTOP_SIZE + 2);
5411c6fdbd8SKent Overstreet 
542bf8f8b20SDaniel Hill 	prt_tab(out);
543bf8f8b20SDaniel Hill 	prt_printf(out, "since mount");
544bf8f8b20SDaniel Hill 	prt_tab_rjust(out);
545bf8f8b20SDaniel Hill 	prt_tab(out);
546bf8f8b20SDaniel Hill 	prt_printf(out, "recent");
547bf8f8b20SDaniel Hill 	prt_tab_rjust(out);
54825055c69SDaniel Hill 	prt_newline(out);
549bf8f8b20SDaniel Hill 
550bf8f8b20SDaniel Hill 	printbuf_tabstops_reset(out);
551bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, out->indent + 20);
552bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, TABSTOP_SIZE);
553bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, 2);
554bf8f8b20SDaniel Hill 	printbuf_tabstop_push(out, TABSTOP_SIZE);
555bf8f8b20SDaniel Hill 
556bf8f8b20SDaniel Hill 	prt_printf(out, "duration of events");
557bf8f8b20SDaniel Hill 	prt_newline(out);
558bf8f8b20SDaniel Hill 	printbuf_indent_add(out, 2);
559bf8f8b20SDaniel Hill 
560bf8f8b20SDaniel Hill 	pr_name_and_units(out, "min:", stats->min_duration);
561bf8f8b20SDaniel Hill 	pr_name_and_units(out, "max:", stats->max_duration);
562066a2646SKent Overstreet 	pr_name_and_units(out, "total:", stats->total_duration);
563bf8f8b20SDaniel Hill 
564bf8f8b20SDaniel Hill 	prt_printf(out, "mean:");
565bf8f8b20SDaniel Hill 	prt_tab(out);
566bf8f8b20SDaniel Hill 	bch2_pr_time_units_aligned(out, d_mean);
567bf8f8b20SDaniel Hill 	prt_tab(out);
568bf8f8b20SDaniel Hill 	bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->duration_stats_weighted));
569bf8f8b20SDaniel Hill 	prt_newline(out);
570bf8f8b20SDaniel Hill 
571bf8f8b20SDaniel Hill 	prt_printf(out, "stddev:");
572bf8f8b20SDaniel Hill 	prt_tab(out);
573bf8f8b20SDaniel Hill 	bch2_pr_time_units_aligned(out, d_stddev);
574bf8f8b20SDaniel Hill 	prt_tab(out);
575bf8f8b20SDaniel Hill 	bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->duration_stats_weighted));
576bf8f8b20SDaniel Hill 
577bf8f8b20SDaniel Hill 	printbuf_indent_sub(out, 2);
578bf8f8b20SDaniel Hill 	prt_newline(out);
579bf8f8b20SDaniel Hill 
580bf8f8b20SDaniel Hill 	prt_printf(out, "time between events");
581bf8f8b20SDaniel Hill 	prt_newline(out);
582bf8f8b20SDaniel Hill 	printbuf_indent_add(out, 2);
583bf8f8b20SDaniel Hill 
584bf8f8b20SDaniel Hill 	pr_name_and_units(out, "min:", stats->min_freq);
585bf8f8b20SDaniel Hill 	pr_name_and_units(out, "max:", stats->max_freq);
586bf8f8b20SDaniel Hill 
587bf8f8b20SDaniel Hill 	prt_printf(out, "mean:");
588bf8f8b20SDaniel Hill 	prt_tab(out);
589bf8f8b20SDaniel Hill 	bch2_pr_time_units_aligned(out, f_mean);
590bf8f8b20SDaniel Hill 	prt_tab(out);
591bf8f8b20SDaniel Hill 	bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_mean(stats->freq_stats_weighted));
592bf8f8b20SDaniel Hill 	prt_newline(out);
593bf8f8b20SDaniel Hill 
594bf8f8b20SDaniel Hill 	prt_printf(out, "stddev:");
595bf8f8b20SDaniel Hill 	prt_tab(out);
596bf8f8b20SDaniel Hill 	bch2_pr_time_units_aligned(out, f_stddev);
597bf8f8b20SDaniel Hill 	prt_tab(out);
598bf8f8b20SDaniel Hill 	bch2_pr_time_units_aligned(out, mean_and_variance_weighted_get_stddev(stats->freq_stats_weighted));
599bf8f8b20SDaniel Hill 
600bf8f8b20SDaniel Hill 	printbuf_indent_sub(out, 2);
601bf8f8b20SDaniel Hill 	prt_newline(out);
602bf8f8b20SDaniel Hill 
603bf8f8b20SDaniel Hill 	printbuf_tabstops_reset(out);
6041c6fdbd8SKent Overstreet 
6051c6fdbd8SKent Overstreet 	i = eytzinger0_first(NR_QUANTILES);
6061c6fdbd8SKent Overstreet 	u = pick_time_units(stats->quantiles.entries[i].m);
6071c6fdbd8SKent Overstreet 
60825055c69SDaniel Hill 	prt_printf(out, "quantiles (%s):\t", u->name);
6091c6fdbd8SKent Overstreet 	eytzinger0_for_each(i, NR_QUANTILES) {
6101c6fdbd8SKent Overstreet 		bool is_last = eytzinger0_next(i, NR_QUANTILES) == -1;
6111c6fdbd8SKent Overstreet 
6121c6fdbd8SKent Overstreet 		q = max(stats->quantiles.entries[i].m, last_q);
61325055c69SDaniel Hill 		prt_printf(out, "%llu ",
61425055c69SDaniel Hill 		       div_u64(q, u->nsecs));
61525055c69SDaniel Hill 		if (is_last)
61625055c69SDaniel Hill 			prt_newline(out);
6171c6fdbd8SKent Overstreet 		last_q = q;
6181c6fdbd8SKent Overstreet 	}
6191c6fdbd8SKent Overstreet }
620066a2646SKent Overstreet #else
621066a2646SKent Overstreet void bch2_time_stats_to_text(struct printbuf *out, struct bch2_time_stats *stats) {}
622066a2646SKent Overstreet #endif
6231c6fdbd8SKent Overstreet 
6241c6fdbd8SKent Overstreet void bch2_time_stats_exit(struct bch2_time_stats *stats)
6251c6fdbd8SKent Overstreet {
6261c6fdbd8SKent Overstreet 	free_percpu(stats->buffer);
6271c6fdbd8SKent Overstreet }
6281c6fdbd8SKent Overstreet 
6291c6fdbd8SKent Overstreet void bch2_time_stats_init(struct bch2_time_stats *stats)
6301c6fdbd8SKent Overstreet {
6311c6fdbd8SKent Overstreet 	memset(stats, 0, sizeof(*stats));
632bf8f8b20SDaniel Hill 	stats->duration_stats_weighted.weight = 8;
633bf8f8b20SDaniel Hill 	stats->freq_stats_weighted.weight = 8;
634bf8f8b20SDaniel Hill 	stats->min_duration = U64_MAX;
635bf8f8b20SDaniel Hill 	stats->min_freq = U64_MAX;
6361c6fdbd8SKent Overstreet 	spin_lock_init(&stats->lock);
6371c6fdbd8SKent Overstreet }
6381c6fdbd8SKent Overstreet 
6391c6fdbd8SKent Overstreet /* ratelimit: */
6401c6fdbd8SKent Overstreet 
6411c6fdbd8SKent Overstreet /**
6421c6fdbd8SKent Overstreet  * bch2_ratelimit_delay() - return how long to delay until the next time to do
6431c6fdbd8SKent Overstreet  *		some work
64496dea3d5SKent Overstreet  * @d:		the struct bch_ratelimit to update
64596dea3d5SKent Overstreet  * Returns:	the amount of time to delay by, in jiffies
6461c6fdbd8SKent Overstreet  */
6471c6fdbd8SKent Overstreet u64 bch2_ratelimit_delay(struct bch_ratelimit *d)
6481c6fdbd8SKent Overstreet {
6491c6fdbd8SKent Overstreet 	u64 now = local_clock();
6501c6fdbd8SKent Overstreet 
6511c6fdbd8SKent Overstreet 	return time_after64(d->next, now)
6521c6fdbd8SKent Overstreet 		? nsecs_to_jiffies(d->next - now)
6531c6fdbd8SKent Overstreet 		: 0;
6541c6fdbd8SKent Overstreet }
6551c6fdbd8SKent Overstreet 
6561c6fdbd8SKent Overstreet /**
6571c6fdbd8SKent Overstreet  * bch2_ratelimit_increment() - increment @d by the amount of work done
65896dea3d5SKent Overstreet  * @d:		the struct bch_ratelimit to update
65996dea3d5SKent Overstreet  * @done:	the amount of work done, in arbitrary units
6601c6fdbd8SKent Overstreet  */
6611c6fdbd8SKent Overstreet void bch2_ratelimit_increment(struct bch_ratelimit *d, u64 done)
6621c6fdbd8SKent Overstreet {
6631c6fdbd8SKent Overstreet 	u64 now = local_clock();
6641c6fdbd8SKent Overstreet 
6651c6fdbd8SKent Overstreet 	d->next += div_u64(done * NSEC_PER_SEC, d->rate);
6661c6fdbd8SKent Overstreet 
6671c6fdbd8SKent Overstreet 	if (time_before64(now + NSEC_PER_SEC, d->next))
6681c6fdbd8SKent Overstreet 		d->next = now + NSEC_PER_SEC;
6691c6fdbd8SKent Overstreet 
6701c6fdbd8SKent Overstreet 	if (time_after64(now - NSEC_PER_SEC * 2, d->next))
6711c6fdbd8SKent Overstreet 		d->next = now - NSEC_PER_SEC * 2;
6721c6fdbd8SKent Overstreet }
6731c6fdbd8SKent Overstreet 
6741c6fdbd8SKent Overstreet /* pd controller: */
6751c6fdbd8SKent Overstreet 
6761c6fdbd8SKent Overstreet /*
6771c6fdbd8SKent Overstreet  * Updates pd_controller. Attempts to scale inputed values to units per second.
6781c6fdbd8SKent Overstreet  * @target: desired value
6791c6fdbd8SKent Overstreet  * @actual: current value
6801c6fdbd8SKent Overstreet  *
6811c6fdbd8SKent Overstreet  * @sign: 1 or -1; 1 if increasing the rate makes actual go up, -1 if increasing
6821c6fdbd8SKent Overstreet  * it makes actual go down.
6831c6fdbd8SKent Overstreet  */
6841c6fdbd8SKent Overstreet void bch2_pd_controller_update(struct bch_pd_controller *pd,
6851c6fdbd8SKent Overstreet 			      s64 target, s64 actual, int sign)
6861c6fdbd8SKent Overstreet {
6871c6fdbd8SKent Overstreet 	s64 proportional, derivative, change;
6881c6fdbd8SKent Overstreet 
6891c6fdbd8SKent Overstreet 	unsigned long seconds_since_update = (jiffies - pd->last_update) / HZ;
6901c6fdbd8SKent Overstreet 
6911c6fdbd8SKent Overstreet 	if (seconds_since_update == 0)
6921c6fdbd8SKent Overstreet 		return;
6931c6fdbd8SKent Overstreet 
6941c6fdbd8SKent Overstreet 	pd->last_update = jiffies;
6951c6fdbd8SKent Overstreet 
6961c6fdbd8SKent Overstreet 	proportional = actual - target;
6971c6fdbd8SKent Overstreet 	proportional *= seconds_since_update;
6981c6fdbd8SKent Overstreet 	proportional = div_s64(proportional, pd->p_term_inverse);
6991c6fdbd8SKent Overstreet 
7001c6fdbd8SKent Overstreet 	derivative = actual - pd->last_actual;
7011c6fdbd8SKent Overstreet 	derivative = div_s64(derivative, seconds_since_update);
7021c6fdbd8SKent Overstreet 	derivative = ewma_add(pd->smoothed_derivative, derivative,
7031c6fdbd8SKent Overstreet 			      (pd->d_term / seconds_since_update) ?: 1);
7041c6fdbd8SKent Overstreet 	derivative = derivative * pd->d_term;
7051c6fdbd8SKent Overstreet 	derivative = div_s64(derivative, pd->p_term_inverse);
7061c6fdbd8SKent Overstreet 
7071c6fdbd8SKent Overstreet 	change = proportional + derivative;
7081c6fdbd8SKent Overstreet 
7091c6fdbd8SKent Overstreet 	/* Don't increase rate if not keeping up */
7101c6fdbd8SKent Overstreet 	if (change > 0 &&
7111c6fdbd8SKent Overstreet 	    pd->backpressure &&
7121c6fdbd8SKent Overstreet 	    time_after64(local_clock(),
7131c6fdbd8SKent Overstreet 			 pd->rate.next + NSEC_PER_MSEC))
7141c6fdbd8SKent Overstreet 		change = 0;
7151c6fdbd8SKent Overstreet 
7161c6fdbd8SKent Overstreet 	change *= (sign * -1);
7171c6fdbd8SKent Overstreet 
7181c6fdbd8SKent Overstreet 	pd->rate.rate = clamp_t(s64, (s64) pd->rate.rate + change,
7191c6fdbd8SKent Overstreet 				1, UINT_MAX);
7201c6fdbd8SKent Overstreet 
7211c6fdbd8SKent Overstreet 	pd->last_actual		= actual;
7221c6fdbd8SKent Overstreet 	pd->last_derivative	= derivative;
7231c6fdbd8SKent Overstreet 	pd->last_proportional	= proportional;
7241c6fdbd8SKent Overstreet 	pd->last_change		= change;
7251c6fdbd8SKent Overstreet 	pd->last_target		= target;
7261c6fdbd8SKent Overstreet }
7271c6fdbd8SKent Overstreet 
7281c6fdbd8SKent Overstreet void bch2_pd_controller_init(struct bch_pd_controller *pd)
7291c6fdbd8SKent Overstreet {
7301c6fdbd8SKent Overstreet 	pd->rate.rate		= 1024;
7311c6fdbd8SKent Overstreet 	pd->last_update		= jiffies;
7321c6fdbd8SKent Overstreet 	pd->p_term_inverse	= 6000;
7331c6fdbd8SKent Overstreet 	pd->d_term		= 30;
7341c6fdbd8SKent Overstreet 	pd->d_smooth		= pd->d_term;
7351c6fdbd8SKent Overstreet 	pd->backpressure	= 1;
7361c6fdbd8SKent Overstreet }
7371c6fdbd8SKent Overstreet 
7382be7b16eSKent Overstreet void bch2_pd_controller_debug_to_text(struct printbuf *out, struct bch_pd_controller *pd)
7391c6fdbd8SKent Overstreet {
740401ec4dbSKent Overstreet 	if (!out->nr_tabstops)
741401ec4dbSKent Overstreet 		printbuf_tabstop_push(out, 20);
7421c6fdbd8SKent Overstreet 
743401ec4dbSKent Overstreet 	prt_printf(out, "rate:");
744401ec4dbSKent Overstreet 	prt_tab(out);
745401ec4dbSKent Overstreet 	prt_human_readable_s64(out, pd->rate.rate);
746401ec4dbSKent Overstreet 	prt_newline(out);
7471c6fdbd8SKent Overstreet 
748401ec4dbSKent Overstreet 	prt_printf(out, "target:");
749401ec4dbSKent Overstreet 	prt_tab(out);
750401ec4dbSKent Overstreet 	prt_human_readable_u64(out, pd->last_target);
751401ec4dbSKent Overstreet 	prt_newline(out);
7521c6fdbd8SKent Overstreet 
753401ec4dbSKent Overstreet 	prt_printf(out, "actual:");
754401ec4dbSKent Overstreet 	prt_tab(out);
755401ec4dbSKent Overstreet 	prt_human_readable_u64(out, pd->last_actual);
756401ec4dbSKent Overstreet 	prt_newline(out);
7572be7b16eSKent Overstreet 
758401ec4dbSKent Overstreet 	prt_printf(out, "proportional:");
759401ec4dbSKent Overstreet 	prt_tab(out);
760401ec4dbSKent Overstreet 	prt_human_readable_s64(out, pd->last_proportional);
761401ec4dbSKent Overstreet 	prt_newline(out);
7622be7b16eSKent Overstreet 
763401ec4dbSKent Overstreet 	prt_printf(out, "derivative:");
764401ec4dbSKent Overstreet 	prt_tab(out);
765401ec4dbSKent Overstreet 	prt_human_readable_s64(out, pd->last_derivative);
766401ec4dbSKent Overstreet 	prt_newline(out);
7672be7b16eSKent Overstreet 
768401ec4dbSKent Overstreet 	prt_printf(out, "change:");
769401ec4dbSKent Overstreet 	prt_tab(out);
770401ec4dbSKent Overstreet 	prt_human_readable_s64(out, pd->last_change);
771401ec4dbSKent Overstreet 	prt_newline(out);
7722be7b16eSKent Overstreet 
773401ec4dbSKent Overstreet 	prt_printf(out, "next io:");
774401ec4dbSKent Overstreet 	prt_tab(out);
775401ec4dbSKent Overstreet 	prt_printf(out, "%llims", div64_s64(pd->rate.next - local_clock(), NSEC_PER_MSEC));
776401ec4dbSKent Overstreet 	prt_newline(out);
7771c6fdbd8SKent Overstreet }
7781c6fdbd8SKent Overstreet 
7791c6fdbd8SKent Overstreet /* misc: */
7801c6fdbd8SKent Overstreet 
781885678f6SKent Overstreet void bch2_bio_map(struct bio *bio, void *base, size_t size)
7821c6fdbd8SKent Overstreet {
783885678f6SKent Overstreet 	while (size) {
784885678f6SKent Overstreet 		struct page *page = is_vmalloc_addr(base)
7851c6fdbd8SKent Overstreet 				? vmalloc_to_page(base)
7861c6fdbd8SKent Overstreet 				: virt_to_page(base);
787885678f6SKent Overstreet 		unsigned offset = offset_in_page(base);
788885678f6SKent Overstreet 		unsigned len = min_t(size_t, PAGE_SIZE - offset, size);
7891c6fdbd8SKent Overstreet 
790885678f6SKent Overstreet 		BUG_ON(!bio_add_page(bio, page, len, offset));
791885678f6SKent Overstreet 		size -= len;
792885678f6SKent Overstreet 		base += len;
7931c6fdbd8SKent Overstreet 	}
7941c6fdbd8SKent Overstreet }
7951c6fdbd8SKent Overstreet 
7961c6fdbd8SKent Overstreet int bch2_bio_alloc_pages(struct bio *bio, size_t size, gfp_t gfp_mask)
7971c6fdbd8SKent Overstreet {
7981c6fdbd8SKent Overstreet 	while (size) {
7991c6fdbd8SKent Overstreet 		struct page *page = alloc_pages(gfp_mask, 0);
8001c6fdbd8SKent Overstreet 		unsigned len = min_t(size_t, PAGE_SIZE, size);
8011c6fdbd8SKent Overstreet 
8021c6fdbd8SKent Overstreet 		if (!page)
8031c6fdbd8SKent Overstreet 			return -ENOMEM;
8041c6fdbd8SKent Overstreet 
8051c6fdbd8SKent Overstreet 		if (unlikely(!bio_add_page(bio, page, len, 0))) {
8061c6fdbd8SKent Overstreet 			__free_page(page);
8071c6fdbd8SKent Overstreet 			break;
8081c6fdbd8SKent Overstreet 		}
8091c6fdbd8SKent Overstreet 
8101c6fdbd8SKent Overstreet 		size -= len;
8111c6fdbd8SKent Overstreet 	}
8121c6fdbd8SKent Overstreet 
8131c6fdbd8SKent Overstreet 	return 0;
8141c6fdbd8SKent Overstreet }
8151c6fdbd8SKent Overstreet 
8161c6fdbd8SKent Overstreet size_t bch2_rand_range(size_t max)
8171c6fdbd8SKent Overstreet {
8181c6fdbd8SKent Overstreet 	size_t rand;
8191c6fdbd8SKent Overstreet 
8201c6fdbd8SKent Overstreet 	if (!max)
8211c6fdbd8SKent Overstreet 		return 0;
8221c6fdbd8SKent Overstreet 
8231c6fdbd8SKent Overstreet 	do {
8241c6fdbd8SKent Overstreet 		rand = get_random_long();
8251c6fdbd8SKent Overstreet 		rand &= roundup_pow_of_two(max) - 1;
8261c6fdbd8SKent Overstreet 	} while (rand >= max);
8271c6fdbd8SKent Overstreet 
8281c6fdbd8SKent Overstreet 	return rand;
8291c6fdbd8SKent Overstreet }
8301c6fdbd8SKent Overstreet 
83103c8c747SKent Overstreet void memcpy_to_bio(struct bio *dst, struct bvec_iter dst_iter, const void *src)
8321c6fdbd8SKent Overstreet {
8331c6fdbd8SKent Overstreet 	struct bio_vec bv;
8341c6fdbd8SKent Overstreet 	struct bvec_iter iter;
8351c6fdbd8SKent Overstreet 
8361c6fdbd8SKent Overstreet 	__bio_for_each_segment(bv, dst, iter, dst_iter) {
8371e81f89bSKent Overstreet 		void *dstp = kmap_local_page(bv.bv_page);
8381e81f89bSKent Overstreet 
8391c6fdbd8SKent Overstreet 		memcpy(dstp + bv.bv_offset, src, bv.bv_len);
8401e81f89bSKent Overstreet 		kunmap_local(dstp);
8411c6fdbd8SKent Overstreet 
8421c6fdbd8SKent Overstreet 		src += bv.bv_len;
8431c6fdbd8SKent Overstreet 	}
8441c6fdbd8SKent Overstreet }
8451c6fdbd8SKent Overstreet 
8461c6fdbd8SKent Overstreet void memcpy_from_bio(void *dst, struct bio *src, struct bvec_iter src_iter)
8471c6fdbd8SKent Overstreet {
8481c6fdbd8SKent Overstreet 	struct bio_vec bv;
8491c6fdbd8SKent Overstreet 	struct bvec_iter iter;
8501c6fdbd8SKent Overstreet 
8511c6fdbd8SKent Overstreet 	__bio_for_each_segment(bv, src, iter, src_iter) {
8521e81f89bSKent Overstreet 		void *srcp = kmap_local_page(bv.bv_page);
8531e81f89bSKent Overstreet 
8541c6fdbd8SKent Overstreet 		memcpy(dst, srcp + bv.bv_offset, bv.bv_len);
8551e81f89bSKent Overstreet 		kunmap_local(srcp);
8561c6fdbd8SKent Overstreet 
8571c6fdbd8SKent Overstreet 		dst += bv.bv_len;
8581c6fdbd8SKent Overstreet 	}
8591c6fdbd8SKent Overstreet }
8601c6fdbd8SKent Overstreet 
8611c6fdbd8SKent Overstreet static int alignment_ok(const void *base, size_t align)
8621c6fdbd8SKent Overstreet {
8631c6fdbd8SKent Overstreet 	return IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
8641c6fdbd8SKent Overstreet 		((unsigned long)base & (align - 1)) == 0;
8651c6fdbd8SKent Overstreet }
8661c6fdbd8SKent Overstreet 
8671c6fdbd8SKent Overstreet static void u32_swap(void *a, void *b, size_t size)
8681c6fdbd8SKent Overstreet {
8691c6fdbd8SKent Overstreet 	u32 t = *(u32 *)a;
8701c6fdbd8SKent Overstreet 	*(u32 *)a = *(u32 *)b;
8711c6fdbd8SKent Overstreet 	*(u32 *)b = t;
8721c6fdbd8SKent Overstreet }
8731c6fdbd8SKent Overstreet 
8741c6fdbd8SKent Overstreet static void u64_swap(void *a, void *b, size_t size)
8751c6fdbd8SKent Overstreet {
8761c6fdbd8SKent Overstreet 	u64 t = *(u64 *)a;
8771c6fdbd8SKent Overstreet 	*(u64 *)a = *(u64 *)b;
8781c6fdbd8SKent Overstreet 	*(u64 *)b = t;
8791c6fdbd8SKent Overstreet }
8801c6fdbd8SKent Overstreet 
8811c6fdbd8SKent Overstreet static void generic_swap(void *a, void *b, size_t size)
8821c6fdbd8SKent Overstreet {
8831c6fdbd8SKent Overstreet 	char t;
8841c6fdbd8SKent Overstreet 
8851c6fdbd8SKent Overstreet 	do {
8861c6fdbd8SKent Overstreet 		t = *(char *)a;
8871c6fdbd8SKent Overstreet 		*(char *)a++ = *(char *)b;
8881c6fdbd8SKent Overstreet 		*(char *)b++ = t;
8891c6fdbd8SKent Overstreet 	} while (--size > 0);
8901c6fdbd8SKent Overstreet }
8911c6fdbd8SKent Overstreet 
8921c6fdbd8SKent Overstreet static inline int do_cmp(void *base, size_t n, size_t size,
8931c6fdbd8SKent Overstreet 			 int (*cmp_func)(const void *, const void *, size_t),
8941c6fdbd8SKent Overstreet 			 size_t l, size_t r)
8951c6fdbd8SKent Overstreet {
8961c6fdbd8SKent Overstreet 	return cmp_func(base + inorder_to_eytzinger0(l, n) * size,
8971c6fdbd8SKent Overstreet 			base + inorder_to_eytzinger0(r, n) * size,
8981c6fdbd8SKent Overstreet 			size);
8991c6fdbd8SKent Overstreet }
9001c6fdbd8SKent Overstreet 
9011c6fdbd8SKent Overstreet static inline void do_swap(void *base, size_t n, size_t size,
9021c6fdbd8SKent Overstreet 			   void (*swap_func)(void *, void *, size_t),
9031c6fdbd8SKent Overstreet 			   size_t l, size_t r)
9041c6fdbd8SKent Overstreet {
9051c6fdbd8SKent Overstreet 	swap_func(base + inorder_to_eytzinger0(l, n) * size,
9061c6fdbd8SKent Overstreet 		  base + inorder_to_eytzinger0(r, n) * size,
9071c6fdbd8SKent Overstreet 		  size);
9081c6fdbd8SKent Overstreet }
9091c6fdbd8SKent Overstreet 
9101c6fdbd8SKent Overstreet void eytzinger0_sort(void *base, size_t n, size_t size,
9111c6fdbd8SKent Overstreet 		     int (*cmp_func)(const void *, const void *, size_t),
9121c6fdbd8SKent Overstreet 		     void (*swap_func)(void *, void *, size_t))
9131c6fdbd8SKent Overstreet {
9141c6fdbd8SKent Overstreet 	int i, c, r;
9151c6fdbd8SKent Overstreet 
9161c6fdbd8SKent Overstreet 	if (!swap_func) {
9171c6fdbd8SKent Overstreet 		if (size == 4 && alignment_ok(base, 4))
9181c6fdbd8SKent Overstreet 			swap_func = u32_swap;
9191c6fdbd8SKent Overstreet 		else if (size == 8 && alignment_ok(base, 8))
9201c6fdbd8SKent Overstreet 			swap_func = u64_swap;
9211c6fdbd8SKent Overstreet 		else
9221c6fdbd8SKent Overstreet 			swap_func = generic_swap;
9231c6fdbd8SKent Overstreet 	}
9241c6fdbd8SKent Overstreet 
9251c6fdbd8SKent Overstreet 	/* heapify */
9261c6fdbd8SKent Overstreet 	for (i = n / 2 - 1; i >= 0; --i) {
9271c6fdbd8SKent Overstreet 		for (r = i; r * 2 + 1 < n; r = c) {
9281c6fdbd8SKent Overstreet 			c = r * 2 + 1;
9291c6fdbd8SKent Overstreet 
9301c6fdbd8SKent Overstreet 			if (c + 1 < n &&
9311c6fdbd8SKent Overstreet 			    do_cmp(base, n, size, cmp_func, c, c + 1) < 0)
9321c6fdbd8SKent Overstreet 				c++;
9331c6fdbd8SKent Overstreet 
9341c6fdbd8SKent Overstreet 			if (do_cmp(base, n, size, cmp_func, r, c) >= 0)
9351c6fdbd8SKent Overstreet 				break;
9361c6fdbd8SKent Overstreet 
9371c6fdbd8SKent Overstreet 			do_swap(base, n, size, swap_func, r, c);
9381c6fdbd8SKent Overstreet 		}
9391c6fdbd8SKent Overstreet 	}
9401c6fdbd8SKent Overstreet 
9411c6fdbd8SKent Overstreet 	/* sort */
9421c6fdbd8SKent Overstreet 	for (i = n - 1; i > 0; --i) {
9431c6fdbd8SKent Overstreet 		do_swap(base, n, size, swap_func, 0, i);
9441c6fdbd8SKent Overstreet 
9451c6fdbd8SKent Overstreet 		for (r = 0; r * 2 + 1 < i; r = c) {
9461c6fdbd8SKent Overstreet 			c = r * 2 + 1;
9471c6fdbd8SKent Overstreet 
9481c6fdbd8SKent Overstreet 			if (c + 1 < i &&
9491c6fdbd8SKent Overstreet 			    do_cmp(base, n, size, cmp_func, c, c + 1) < 0)
9501c6fdbd8SKent Overstreet 				c++;
9511c6fdbd8SKent Overstreet 
9521c6fdbd8SKent Overstreet 			if (do_cmp(base, n, size, cmp_func, r, c) >= 0)
9531c6fdbd8SKent Overstreet 				break;
9541c6fdbd8SKent Overstreet 
9551c6fdbd8SKent Overstreet 			do_swap(base, n, size, swap_func, r, c);
9561c6fdbd8SKent Overstreet 		}
9571c6fdbd8SKent Overstreet 	}
9581c6fdbd8SKent Overstreet }
9591c6fdbd8SKent Overstreet 
9601c6fdbd8SKent Overstreet void sort_cmp_size(void *base, size_t num, size_t size,
9611c6fdbd8SKent Overstreet 	  int (*cmp_func)(const void *, const void *, size_t),
9621c6fdbd8SKent Overstreet 	  void (*swap_func)(void *, void *, size_t size))
9631c6fdbd8SKent Overstreet {
9641c6fdbd8SKent Overstreet 	/* pre-scale counters for performance */
9651c6fdbd8SKent Overstreet 	int i = (num/2 - 1) * size, n = num * size, c, r;
9661c6fdbd8SKent Overstreet 
9671c6fdbd8SKent Overstreet 	if (!swap_func) {
9681c6fdbd8SKent Overstreet 		if (size == 4 && alignment_ok(base, 4))
9691c6fdbd8SKent Overstreet 			swap_func = u32_swap;
9701c6fdbd8SKent Overstreet 		else if (size == 8 && alignment_ok(base, 8))
9711c6fdbd8SKent Overstreet 			swap_func = u64_swap;
9721c6fdbd8SKent Overstreet 		else
9731c6fdbd8SKent Overstreet 			swap_func = generic_swap;
9741c6fdbd8SKent Overstreet 	}
9751c6fdbd8SKent Overstreet 
9761c6fdbd8SKent Overstreet 	/* heapify */
9771c6fdbd8SKent Overstreet 	for ( ; i >= 0; i -= size) {
9781c6fdbd8SKent Overstreet 		for (r = i; r * 2 + size < n; r  = c) {
9791c6fdbd8SKent Overstreet 			c = r * 2 + size;
9801c6fdbd8SKent Overstreet 			if (c < n - size &&
9811c6fdbd8SKent Overstreet 			    cmp_func(base + c, base + c + size, size) < 0)
9821c6fdbd8SKent Overstreet 				c += size;
9831c6fdbd8SKent Overstreet 			if (cmp_func(base + r, base + c, size) >= 0)
9841c6fdbd8SKent Overstreet 				break;
9851c6fdbd8SKent Overstreet 			swap_func(base + r, base + c, size);
9861c6fdbd8SKent Overstreet 		}
9871c6fdbd8SKent Overstreet 	}
9881c6fdbd8SKent Overstreet 
9891c6fdbd8SKent Overstreet 	/* sort */
9901c6fdbd8SKent Overstreet 	for (i = n - size; i > 0; i -= size) {
9911c6fdbd8SKent Overstreet 		swap_func(base, base + i, size);
9921c6fdbd8SKent Overstreet 		for (r = 0; r * 2 + size < i; r = c) {
9931c6fdbd8SKent Overstreet 			c = r * 2 + size;
9941c6fdbd8SKent Overstreet 			if (c < i - size &&
9951c6fdbd8SKent Overstreet 			    cmp_func(base + c, base + c + size, size) < 0)
9961c6fdbd8SKent Overstreet 				c += size;
9971c6fdbd8SKent Overstreet 			if (cmp_func(base + r, base + c, size) >= 0)
9981c6fdbd8SKent Overstreet 				break;
9991c6fdbd8SKent Overstreet 			swap_func(base + r, base + c, size);
10001c6fdbd8SKent Overstreet 		}
10011c6fdbd8SKent Overstreet 	}
10021c6fdbd8SKent Overstreet }
10031c6fdbd8SKent Overstreet 
10041c6fdbd8SKent Overstreet static void mempool_free_vp(void *element, void *pool_data)
10051c6fdbd8SKent Overstreet {
10061c6fdbd8SKent Overstreet 	size_t size = (size_t) pool_data;
10071c6fdbd8SKent Overstreet 
10081c6fdbd8SKent Overstreet 	vpfree(element, size);
10091c6fdbd8SKent Overstreet }
10101c6fdbd8SKent Overstreet 
10111c6fdbd8SKent Overstreet static void *mempool_alloc_vp(gfp_t gfp_mask, void *pool_data)
10121c6fdbd8SKent Overstreet {
10131c6fdbd8SKent Overstreet 	size_t size = (size_t) pool_data;
10141c6fdbd8SKent Overstreet 
10151c6fdbd8SKent Overstreet 	return vpmalloc(size, gfp_mask);
10161c6fdbd8SKent Overstreet }
10171c6fdbd8SKent Overstreet 
10181c6fdbd8SKent Overstreet int mempool_init_kvpmalloc_pool(mempool_t *pool, int min_nr, size_t size)
10191c6fdbd8SKent Overstreet {
10201c6fdbd8SKent Overstreet 	return size < PAGE_SIZE
10211c6fdbd8SKent Overstreet 		? mempool_init_kmalloc_pool(pool, min_nr, size)
10221c6fdbd8SKent Overstreet 		: mempool_init(pool, min_nr, mempool_alloc_vp,
10231c6fdbd8SKent Overstreet 			       mempool_free_vp, (void *) size);
10241c6fdbd8SKent Overstreet }
10251c6fdbd8SKent Overstreet 
10261c6fdbd8SKent Overstreet #if 0
10271c6fdbd8SKent Overstreet void eytzinger1_test(void)
10281c6fdbd8SKent Overstreet {
10291c6fdbd8SKent Overstreet 	unsigned inorder, eytz, size;
10301c6fdbd8SKent Overstreet 
10311c6fdbd8SKent Overstreet 	pr_info("1 based eytzinger test:");
10321c6fdbd8SKent Overstreet 
10331c6fdbd8SKent Overstreet 	for (size = 2;
10341c6fdbd8SKent Overstreet 	     size < 65536;
10351c6fdbd8SKent Overstreet 	     size++) {
10361c6fdbd8SKent Overstreet 		unsigned extra = eytzinger1_extra(size);
10371c6fdbd8SKent Overstreet 
10381c6fdbd8SKent Overstreet 		if (!(size % 4096))
10391c6fdbd8SKent Overstreet 			pr_info("tree size %u", size);
10401c6fdbd8SKent Overstreet 
10411c6fdbd8SKent Overstreet 		BUG_ON(eytzinger1_prev(0, size) != eytzinger1_last(size));
10421c6fdbd8SKent Overstreet 		BUG_ON(eytzinger1_next(0, size) != eytzinger1_first(size));
10431c6fdbd8SKent Overstreet 
10441c6fdbd8SKent Overstreet 		BUG_ON(eytzinger1_prev(eytzinger1_first(size), size)	!= 0);
10451c6fdbd8SKent Overstreet 		BUG_ON(eytzinger1_next(eytzinger1_last(size), size)	!= 0);
10461c6fdbd8SKent Overstreet 
10471c6fdbd8SKent Overstreet 		inorder = 1;
10481c6fdbd8SKent Overstreet 		eytzinger1_for_each(eytz, size) {
10491c6fdbd8SKent Overstreet 			BUG_ON(__inorder_to_eytzinger1(inorder, size, extra) != eytz);
10501c6fdbd8SKent Overstreet 			BUG_ON(__eytzinger1_to_inorder(eytz, size, extra) != inorder);
10511c6fdbd8SKent Overstreet 			BUG_ON(eytz != eytzinger1_last(size) &&
10521c6fdbd8SKent Overstreet 			       eytzinger1_prev(eytzinger1_next(eytz, size), size) != eytz);
10531c6fdbd8SKent Overstreet 
10541c6fdbd8SKent Overstreet 			inorder++;
10551c6fdbd8SKent Overstreet 		}
10561c6fdbd8SKent Overstreet 	}
10571c6fdbd8SKent Overstreet }
10581c6fdbd8SKent Overstreet 
10591c6fdbd8SKent Overstreet void eytzinger0_test(void)
10601c6fdbd8SKent Overstreet {
10611c6fdbd8SKent Overstreet 
10621c6fdbd8SKent Overstreet 	unsigned inorder, eytz, size;
10631c6fdbd8SKent Overstreet 
10641c6fdbd8SKent Overstreet 	pr_info("0 based eytzinger test:");
10651c6fdbd8SKent Overstreet 
10661c6fdbd8SKent Overstreet 	for (size = 1;
10671c6fdbd8SKent Overstreet 	     size < 65536;
10681c6fdbd8SKent Overstreet 	     size++) {
10691c6fdbd8SKent Overstreet 		unsigned extra = eytzinger0_extra(size);
10701c6fdbd8SKent Overstreet 
10711c6fdbd8SKent Overstreet 		if (!(size % 4096))
10721c6fdbd8SKent Overstreet 			pr_info("tree size %u", size);
10731c6fdbd8SKent Overstreet 
10741c6fdbd8SKent Overstreet 		BUG_ON(eytzinger0_prev(-1, size) != eytzinger0_last(size));
10751c6fdbd8SKent Overstreet 		BUG_ON(eytzinger0_next(-1, size) != eytzinger0_first(size));
10761c6fdbd8SKent Overstreet 
10771c6fdbd8SKent Overstreet 		BUG_ON(eytzinger0_prev(eytzinger0_first(size), size)	!= -1);
10781c6fdbd8SKent Overstreet 		BUG_ON(eytzinger0_next(eytzinger0_last(size), size)	!= -1);
10791c6fdbd8SKent Overstreet 
10801c6fdbd8SKent Overstreet 		inorder = 0;
10811c6fdbd8SKent Overstreet 		eytzinger0_for_each(eytz, size) {
10821c6fdbd8SKent Overstreet 			BUG_ON(__inorder_to_eytzinger0(inorder, size, extra) != eytz);
10831c6fdbd8SKent Overstreet 			BUG_ON(__eytzinger0_to_inorder(eytz, size, extra) != inorder);
10841c6fdbd8SKent Overstreet 			BUG_ON(eytz != eytzinger0_last(size) &&
10851c6fdbd8SKent Overstreet 			       eytzinger0_prev(eytzinger0_next(eytz, size), size) != eytz);
10861c6fdbd8SKent Overstreet 
10871c6fdbd8SKent Overstreet 			inorder++;
10881c6fdbd8SKent Overstreet 		}
10891c6fdbd8SKent Overstreet 	}
10901c6fdbd8SKent Overstreet }
10911c6fdbd8SKent Overstreet 
10921c6fdbd8SKent Overstreet static inline int cmp_u16(const void *_l, const void *_r, size_t size)
10931c6fdbd8SKent Overstreet {
10941c6fdbd8SKent Overstreet 	const u16 *l = _l, *r = _r;
10951c6fdbd8SKent Overstreet 
10961c6fdbd8SKent Overstreet 	return (*l > *r) - (*r - *l);
10971c6fdbd8SKent Overstreet }
10981c6fdbd8SKent Overstreet 
10991c6fdbd8SKent Overstreet static void eytzinger0_find_test_val(u16 *test_array, unsigned nr, u16 search)
11001c6fdbd8SKent Overstreet {
11011c6fdbd8SKent Overstreet 	int i, c1 = -1, c2 = -1;
11021c6fdbd8SKent Overstreet 	ssize_t r;
11031c6fdbd8SKent Overstreet 
11041c6fdbd8SKent Overstreet 	r = eytzinger0_find_le(test_array, nr,
11051c6fdbd8SKent Overstreet 			       sizeof(test_array[0]),
11061c6fdbd8SKent Overstreet 			       cmp_u16, &search);
11071c6fdbd8SKent Overstreet 	if (r >= 0)
11081c6fdbd8SKent Overstreet 		c1 = test_array[r];
11091c6fdbd8SKent Overstreet 
11101c6fdbd8SKent Overstreet 	for (i = 0; i < nr; i++)
11111c6fdbd8SKent Overstreet 		if (test_array[i] <= search && test_array[i] > c2)
11121c6fdbd8SKent Overstreet 			c2 = test_array[i];
11131c6fdbd8SKent Overstreet 
11141c6fdbd8SKent Overstreet 	if (c1 != c2) {
11151c6fdbd8SKent Overstreet 		eytzinger0_for_each(i, nr)
11161c6fdbd8SKent Overstreet 			pr_info("[%3u] = %12u", i, test_array[i]);
11171c6fdbd8SKent Overstreet 		pr_info("find_le(%2u) -> [%2zi] = %2i should be %2i",
11181c6fdbd8SKent Overstreet 			i, r, c1, c2);
11191c6fdbd8SKent Overstreet 	}
11201c6fdbd8SKent Overstreet }
11211c6fdbd8SKent Overstreet 
11221c6fdbd8SKent Overstreet void eytzinger0_find_test(void)
11231c6fdbd8SKent Overstreet {
11241c6fdbd8SKent Overstreet 	unsigned i, nr, allocated = 1 << 12;
11251c6fdbd8SKent Overstreet 	u16 *test_array = kmalloc_array(allocated, sizeof(test_array[0]), GFP_KERNEL);
11261c6fdbd8SKent Overstreet 
11271c6fdbd8SKent Overstreet 	for (nr = 1; nr < allocated; nr++) {
11281c6fdbd8SKent Overstreet 		pr_info("testing %u elems", nr);
11291c6fdbd8SKent Overstreet 
11301c6fdbd8SKent Overstreet 		get_random_bytes(test_array, nr * sizeof(test_array[0]));
11311c6fdbd8SKent Overstreet 		eytzinger0_sort(test_array, nr, sizeof(test_array[0]), cmp_u16, NULL);
11321c6fdbd8SKent Overstreet 
11331c6fdbd8SKent Overstreet 		/* verify array is sorted correctly: */
11341c6fdbd8SKent Overstreet 		eytzinger0_for_each(i, nr)
11351c6fdbd8SKent Overstreet 			BUG_ON(i != eytzinger0_last(nr) &&
11361c6fdbd8SKent Overstreet 			       test_array[i] > test_array[eytzinger0_next(i, nr)]);
11371c6fdbd8SKent Overstreet 
11381c6fdbd8SKent Overstreet 		for (i = 0; i < U16_MAX; i += 1 << 12)
11391c6fdbd8SKent Overstreet 			eytzinger0_find_test_val(test_array, nr, i);
11401c6fdbd8SKent Overstreet 
11411c6fdbd8SKent Overstreet 		for (i = 0; i < nr; i++) {
11421c6fdbd8SKent Overstreet 			eytzinger0_find_test_val(test_array, nr, test_array[i] - 1);
11431c6fdbd8SKent Overstreet 			eytzinger0_find_test_val(test_array, nr, test_array[i]);
11441c6fdbd8SKent Overstreet 			eytzinger0_find_test_val(test_array, nr, test_array[i] + 1);
11451c6fdbd8SKent Overstreet 		}
11461c6fdbd8SKent Overstreet 	}
11471c6fdbd8SKent Overstreet 
11481c6fdbd8SKent Overstreet 	kfree(test_array);
11491c6fdbd8SKent Overstreet }
11501c6fdbd8SKent Overstreet #endif
11517ef2a73aSKent Overstreet 
11527ef2a73aSKent Overstreet /*
11537ef2a73aSKent Overstreet  * Accumulate percpu counters onto one cpu's copy - only valid when access
11547ef2a73aSKent Overstreet  * against any percpu counter is guarded against
11557ef2a73aSKent Overstreet  */
11567ef2a73aSKent Overstreet u64 *bch2_acc_percpu_u64s(u64 __percpu *p, unsigned nr)
11577ef2a73aSKent Overstreet {
1158107fe5afSKent Overstreet 	u64 *ret;
11597ef2a73aSKent Overstreet 	int cpu;
11607ef2a73aSKent Overstreet 
1161107fe5afSKent Overstreet 	/* access to pcpu vars has to be blocked by other locking */
1162107fe5afSKent Overstreet 	preempt_disable();
1163107fe5afSKent Overstreet 	ret = this_cpu_ptr(p);
1164107fe5afSKent Overstreet 	preempt_enable();
1165107fe5afSKent Overstreet 
11667ef2a73aSKent Overstreet 	for_each_possible_cpu(cpu) {
11677ef2a73aSKent Overstreet 		u64 *i = per_cpu_ptr(p, cpu);
11687ef2a73aSKent Overstreet 
11697ef2a73aSKent Overstreet 		if (i != ret) {
11707ef2a73aSKent Overstreet 			acc_u64s(ret, i, nr);
11717ef2a73aSKent Overstreet 			memset(i, 0, nr * sizeof(u64));
11727ef2a73aSKent Overstreet 		}
11737ef2a73aSKent Overstreet 	}
11747ef2a73aSKent Overstreet 
11757ef2a73aSKent Overstreet 	return ret;
11767ef2a73aSKent Overstreet }
1177806ebf2aSKent Overstreet 
1178806ebf2aSKent Overstreet void bch2_darray_str_exit(darray_str *d)
1179806ebf2aSKent Overstreet {
1180806ebf2aSKent Overstreet 	darray_for_each(*d, i)
1181806ebf2aSKent Overstreet 		kfree(*i);
1182806ebf2aSKent Overstreet 	darray_exit(d);
1183806ebf2aSKent Overstreet }
1184806ebf2aSKent Overstreet 
1185806ebf2aSKent Overstreet int bch2_split_devs(const char *_dev_name, darray_str *ret)
1186806ebf2aSKent Overstreet {
1187806ebf2aSKent Overstreet 	darray_init(ret);
1188806ebf2aSKent Overstreet 
1189806ebf2aSKent Overstreet 	char *dev_name = kstrdup(_dev_name, GFP_KERNEL), *s = dev_name;
1190806ebf2aSKent Overstreet 	if (!dev_name)
1191806ebf2aSKent Overstreet 		return -ENOMEM;
1192806ebf2aSKent Overstreet 
1193806ebf2aSKent Overstreet 	while ((s = strsep(&dev_name, ":"))) {
1194806ebf2aSKent Overstreet 		char *p = kstrdup(s, GFP_KERNEL);
1195806ebf2aSKent Overstreet 		if (!p)
1196806ebf2aSKent Overstreet 			goto err;
1197806ebf2aSKent Overstreet 
1198806ebf2aSKent Overstreet 		if (darray_push(ret, p)) {
1199806ebf2aSKent Overstreet 			kfree(p);
1200806ebf2aSKent Overstreet 			goto err;
1201806ebf2aSKent Overstreet 		}
1202806ebf2aSKent Overstreet 	}
1203806ebf2aSKent Overstreet 
1204806ebf2aSKent Overstreet 	kfree(dev_name);
1205806ebf2aSKent Overstreet 	return 0;
1206806ebf2aSKent Overstreet err:
1207806ebf2aSKent Overstreet 	bch2_darray_str_exit(ret);
1208806ebf2aSKent Overstreet 	kfree(dev_name);
1209806ebf2aSKent Overstreet 	return -ENOMEM;
1210806ebf2aSKent Overstreet }
1211