xref: /openbmc/linux/drivers/md/bcache/util.h (revision aac5987a)
1 
2 #ifndef _BCACHE_UTIL_H
3 #define _BCACHE_UTIL_H
4 
5 #include <linux/blkdev.h>
6 #include <linux/errno.h>
7 #include <linux/blkdev.h>
8 #include <linux/kernel.h>
9 #include <linux/sched/clock.h>
10 #include <linux/llist.h>
11 #include <linux/ratelimit.h>
12 #include <linux/vmalloc.h>
13 #include <linux/workqueue.h>
14 
15 #include "closure.h"
16 
17 #define PAGE_SECTORS		(PAGE_SIZE / 512)
18 
19 struct closure;
20 
21 #ifdef CONFIG_BCACHE_DEBUG
22 
23 #define EBUG_ON(cond)			BUG_ON(cond)
24 #define atomic_dec_bug(v)	BUG_ON(atomic_dec_return(v) < 0)
25 #define atomic_inc_bug(v, i)	BUG_ON(atomic_inc_return(v) <= i)
26 
27 #else /* DEBUG */
28 
29 #define EBUG_ON(cond)			do { if (cond); } while (0)
30 #define atomic_dec_bug(v)	atomic_dec(v)
31 #define atomic_inc_bug(v, i)	atomic_inc(v)
32 
33 #endif
34 
35 #define DECLARE_HEAP(type, name)					\
36 	struct {							\
37 		size_t size, used;					\
38 		type *data;						\
39 	} name
40 
41 #define init_heap(heap, _size, gfp)					\
42 ({									\
43 	size_t _bytes;							\
44 	(heap)->used = 0;						\
45 	(heap)->size = (_size);						\
46 	_bytes = (heap)->size * sizeof(*(heap)->data);			\
47 	(heap)->data = NULL;						\
48 	if (_bytes < KMALLOC_MAX_SIZE)					\
49 		(heap)->data = kmalloc(_bytes, (gfp));			\
50 	if ((!(heap)->data) && ((gfp) & GFP_KERNEL))			\
51 		(heap)->data = vmalloc(_bytes);				\
52 	(heap)->data;							\
53 })
54 
55 #define free_heap(heap)							\
56 do {									\
57 	kvfree((heap)->data);						\
58 	(heap)->data = NULL;						\
59 } while (0)
60 
61 #define heap_swap(h, i, j)	swap((h)->data[i], (h)->data[j])
62 
63 #define heap_sift(h, i, cmp)						\
64 do {									\
65 	size_t _r, _j = i;						\
66 									\
67 	for (; _j * 2 + 1 < (h)->used; _j = _r) {			\
68 		_r = _j * 2 + 1;					\
69 		if (_r + 1 < (h)->used &&				\
70 		    cmp((h)->data[_r], (h)->data[_r + 1]))		\
71 			_r++;						\
72 									\
73 		if (cmp((h)->data[_r], (h)->data[_j]))			\
74 			break;						\
75 		heap_swap(h, _r, _j);					\
76 	}								\
77 } while (0)
78 
79 #define heap_sift_down(h, i, cmp)					\
80 do {									\
81 	while (i) {							\
82 		size_t p = (i - 1) / 2;					\
83 		if (cmp((h)->data[i], (h)->data[p]))			\
84 			break;						\
85 		heap_swap(h, i, p);					\
86 		i = p;							\
87 	}								\
88 } while (0)
89 
90 #define heap_add(h, d, cmp)						\
91 ({									\
92 	bool _r = !heap_full(h);					\
93 	if (_r) {							\
94 		size_t _i = (h)->used++;				\
95 		(h)->data[_i] = d;					\
96 									\
97 		heap_sift_down(h, _i, cmp);				\
98 		heap_sift(h, _i, cmp);					\
99 	}								\
100 	_r;								\
101 })
102 
103 #define heap_pop(h, d, cmp)						\
104 ({									\
105 	bool _r = (h)->used;						\
106 	if (_r) {							\
107 		(d) = (h)->data[0];					\
108 		(h)->used--;						\
109 		heap_swap(h, 0, (h)->used);				\
110 		heap_sift(h, 0, cmp);					\
111 	}								\
112 	_r;								\
113 })
114 
115 #define heap_peek(h)	((h)->used ? (h)->data[0] : NULL)
116 
117 #define heap_full(h)	((h)->used == (h)->size)
118 
119 #define DECLARE_FIFO(type, name)					\
120 	struct {							\
121 		size_t front, back, size, mask;				\
122 		type *data;						\
123 	} name
124 
125 #define fifo_for_each(c, fifo, iter)					\
126 	for (iter = (fifo)->front;					\
127 	     c = (fifo)->data[iter], iter != (fifo)->back;		\
128 	     iter = (iter + 1) & (fifo)->mask)
129 
130 #define __init_fifo(fifo, gfp)						\
131 ({									\
132 	size_t _allocated_size, _bytes;					\
133 	BUG_ON(!(fifo)->size);						\
134 									\
135 	_allocated_size = roundup_pow_of_two((fifo)->size + 1);		\
136 	_bytes = _allocated_size * sizeof(*(fifo)->data);		\
137 									\
138 	(fifo)->mask = _allocated_size - 1;				\
139 	(fifo)->front = (fifo)->back = 0;				\
140 	(fifo)->data = NULL;						\
141 									\
142 	if (_bytes < KMALLOC_MAX_SIZE)					\
143 		(fifo)->data = kmalloc(_bytes, (gfp));			\
144 	if ((!(fifo)->data) && ((gfp) & GFP_KERNEL))			\
145 		(fifo)->data = vmalloc(_bytes);				\
146 	(fifo)->data;							\
147 })
148 
149 #define init_fifo_exact(fifo, _size, gfp)				\
150 ({									\
151 	(fifo)->size = (_size);						\
152 	__init_fifo(fifo, gfp);						\
153 })
154 
155 #define init_fifo(fifo, _size, gfp)					\
156 ({									\
157 	(fifo)->size = (_size);						\
158 	if ((fifo)->size > 4)						\
159 		(fifo)->size = roundup_pow_of_two((fifo)->size) - 1;	\
160 	__init_fifo(fifo, gfp);						\
161 })
162 
163 #define free_fifo(fifo)							\
164 do {									\
165 	kvfree((fifo)->data);						\
166 	(fifo)->data = NULL;						\
167 } while (0)
168 
169 #define fifo_used(fifo)		(((fifo)->back - (fifo)->front) & (fifo)->mask)
170 #define fifo_free(fifo)		((fifo)->size - fifo_used(fifo))
171 
172 #define fifo_empty(fifo)	(!fifo_used(fifo))
173 #define fifo_full(fifo)		(!fifo_free(fifo))
174 
175 #define fifo_front(fifo)	((fifo)->data[(fifo)->front])
176 #define fifo_back(fifo)							\
177 	((fifo)->data[((fifo)->back - 1) & (fifo)->mask])
178 
179 #define fifo_idx(fifo, p)	(((p) - &fifo_front(fifo)) & (fifo)->mask)
180 
181 #define fifo_push_back(fifo, i)						\
182 ({									\
183 	bool _r = !fifo_full((fifo));					\
184 	if (_r) {							\
185 		(fifo)->data[(fifo)->back++] = (i);			\
186 		(fifo)->back &= (fifo)->mask;				\
187 	}								\
188 	_r;								\
189 })
190 
191 #define fifo_pop_front(fifo, i)						\
192 ({									\
193 	bool _r = !fifo_empty((fifo));					\
194 	if (_r) {							\
195 		(i) = (fifo)->data[(fifo)->front++];			\
196 		(fifo)->front &= (fifo)->mask;				\
197 	}								\
198 	_r;								\
199 })
200 
201 #define fifo_push_front(fifo, i)					\
202 ({									\
203 	bool _r = !fifo_full((fifo));					\
204 	if (_r) {							\
205 		--(fifo)->front;					\
206 		(fifo)->front &= (fifo)->mask;				\
207 		(fifo)->data[(fifo)->front] = (i);			\
208 	}								\
209 	_r;								\
210 })
211 
212 #define fifo_pop_back(fifo, i)						\
213 ({									\
214 	bool _r = !fifo_empty((fifo));					\
215 	if (_r) {							\
216 		--(fifo)->back;						\
217 		(fifo)->back &= (fifo)->mask;				\
218 		(i) = (fifo)->data[(fifo)->back]			\
219 	}								\
220 	_r;								\
221 })
222 
223 #define fifo_push(fifo, i)	fifo_push_back(fifo, (i))
224 #define fifo_pop(fifo, i)	fifo_pop_front(fifo, (i))
225 
226 #define fifo_swap(l, r)							\
227 do {									\
228 	swap((l)->front, (r)->front);					\
229 	swap((l)->back, (r)->back);					\
230 	swap((l)->size, (r)->size);					\
231 	swap((l)->mask, (r)->mask);					\
232 	swap((l)->data, (r)->data);					\
233 } while (0)
234 
235 #define fifo_move(dest, src)						\
236 do {									\
237 	typeof(*((dest)->data)) _t;					\
238 	while (!fifo_full(dest) &&					\
239 	       fifo_pop(src, _t))					\
240 		fifo_push(dest, _t);					\
241 } while (0)
242 
243 /*
244  * Simple array based allocator - preallocates a number of elements and you can
245  * never allocate more than that, also has no locking.
246  *
247  * Handy because if you know you only need a fixed number of elements you don't
248  * have to worry about memory allocation failure, and sometimes a mempool isn't
249  * what you want.
250  *
251  * We treat the free elements as entries in a singly linked list, and the
252  * freelist as a stack - allocating and freeing push and pop off the freelist.
253  */
254 
255 #define DECLARE_ARRAY_ALLOCATOR(type, name, size)			\
256 	struct {							\
257 		type	*freelist;					\
258 		type	data[size];					\
259 	} name
260 
261 #define array_alloc(array)						\
262 ({									\
263 	typeof((array)->freelist) _ret = (array)->freelist;		\
264 									\
265 	if (_ret)							\
266 		(array)->freelist = *((typeof((array)->freelist) *) _ret);\
267 									\
268 	_ret;								\
269 })
270 
271 #define array_free(array, ptr)						\
272 do {									\
273 	typeof((array)->freelist) _ptr = ptr;				\
274 									\
275 	*((typeof((array)->freelist) *) _ptr) = (array)->freelist;	\
276 	(array)->freelist = _ptr;					\
277 } while (0)
278 
279 #define array_allocator_init(array)					\
280 do {									\
281 	typeof((array)->freelist) _i;					\
282 									\
283 	BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *));	\
284 	(array)->freelist = NULL;					\
285 									\
286 	for (_i = (array)->data;					\
287 	     _i < (array)->data + ARRAY_SIZE((array)->data);		\
288 	     _i++)							\
289 		array_free(array, _i);					\
290 } while (0)
291 
292 #define array_freelist_empty(array)	((array)->freelist == NULL)
293 
294 #define ANYSINT_MAX(t)							\
295 	((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
296 
297 int bch_strtoint_h(const char *, int *);
298 int bch_strtouint_h(const char *, unsigned int *);
299 int bch_strtoll_h(const char *, long long *);
300 int bch_strtoull_h(const char *, unsigned long long *);
301 
302 static inline int bch_strtol_h(const char *cp, long *res)
303 {
304 #if BITS_PER_LONG == 32
305 	return bch_strtoint_h(cp, (int *) res);
306 #else
307 	return bch_strtoll_h(cp, (long long *) res);
308 #endif
309 }
310 
311 static inline int bch_strtoul_h(const char *cp, long *res)
312 {
313 #if BITS_PER_LONG == 32
314 	return bch_strtouint_h(cp, (unsigned int *) res);
315 #else
316 	return bch_strtoull_h(cp, (unsigned long long *) res);
317 #endif
318 }
319 
320 #define strtoi_h(cp, res)						\
321 	(__builtin_types_compatible_p(typeof(*res), int)		\
322 	? bch_strtoint_h(cp, (void *) res)				\
323 	: __builtin_types_compatible_p(typeof(*res), long)		\
324 	? bch_strtol_h(cp, (void *) res)				\
325 	: __builtin_types_compatible_p(typeof(*res), long long)		\
326 	? bch_strtoll_h(cp, (void *) res)				\
327 	: __builtin_types_compatible_p(typeof(*res), unsigned int)	\
328 	? bch_strtouint_h(cp, (void *) res)				\
329 	: __builtin_types_compatible_p(typeof(*res), unsigned long)	\
330 	? bch_strtoul_h(cp, (void *) res)				\
331 	: __builtin_types_compatible_p(typeof(*res), unsigned long long)\
332 	? bch_strtoull_h(cp, (void *) res) : -EINVAL)
333 
334 #define strtoul_safe(cp, var)						\
335 ({									\
336 	unsigned long _v;						\
337 	int _r = kstrtoul(cp, 10, &_v);					\
338 	if (!_r)							\
339 		var = _v;						\
340 	_r;								\
341 })
342 
343 #define strtoul_safe_clamp(cp, var, min, max)				\
344 ({									\
345 	unsigned long _v;						\
346 	int _r = kstrtoul(cp, 10, &_v);					\
347 	if (!_r)							\
348 		var = clamp_t(typeof(var), _v, min, max);		\
349 	_r;								\
350 })
351 
352 #define snprint(buf, size, var)						\
353 	snprintf(buf, size,						\
354 		__builtin_types_compatible_p(typeof(var), int)		\
355 		     ? "%i\n" :						\
356 		__builtin_types_compatible_p(typeof(var), unsigned)	\
357 		     ? "%u\n" :						\
358 		__builtin_types_compatible_p(typeof(var), long)		\
359 		     ? "%li\n" :					\
360 		__builtin_types_compatible_p(typeof(var), unsigned long)\
361 		     ? "%lu\n" :					\
362 		__builtin_types_compatible_p(typeof(var), int64_t)	\
363 		     ? "%lli\n" :					\
364 		__builtin_types_compatible_p(typeof(var), uint64_t)	\
365 		     ? "%llu\n" :					\
366 		__builtin_types_compatible_p(typeof(var), const char *)	\
367 		     ? "%s\n" : "%i\n", var)
368 
369 ssize_t bch_hprint(char *buf, int64_t v);
370 
371 bool bch_is_zero(const char *p, size_t n);
372 int bch_parse_uuid(const char *s, char *uuid);
373 
374 ssize_t bch_snprint_string_list(char *buf, size_t size, const char * const list[],
375 			    size_t selected);
376 
377 ssize_t bch_read_string_list(const char *buf, const char * const list[]);
378 
379 struct time_stats {
380 	spinlock_t	lock;
381 	/*
382 	 * all fields are in nanoseconds, averages are ewmas stored left shifted
383 	 * by 8
384 	 */
385 	uint64_t	max_duration;
386 	uint64_t	average_duration;
387 	uint64_t	average_frequency;
388 	uint64_t	last;
389 };
390 
391 void bch_time_stats_update(struct time_stats *stats, uint64_t time);
392 
393 static inline unsigned local_clock_us(void)
394 {
395 	return local_clock() >> 10;
396 }
397 
398 #define NSEC_PER_ns			1L
399 #define NSEC_PER_us			NSEC_PER_USEC
400 #define NSEC_PER_ms			NSEC_PER_MSEC
401 #define NSEC_PER_sec			NSEC_PER_SEC
402 
403 #define __print_time_stat(stats, name, stat, units)			\
404 	sysfs_print(name ## _ ## stat ## _ ## units,			\
405 		    div_u64((stats)->stat >> 8, NSEC_PER_ ## units))
406 
407 #define sysfs_print_time_stats(stats, name,				\
408 			       frequency_units,				\
409 			       duration_units)				\
410 do {									\
411 	__print_time_stat(stats, name,					\
412 			  average_frequency,	frequency_units);	\
413 	__print_time_stat(stats, name,					\
414 			  average_duration,	duration_units);	\
415 	sysfs_print(name ## _ ##max_duration ## _ ## duration_units,	\
416 			div_u64((stats)->max_duration, NSEC_PER_ ## duration_units));\
417 									\
418 	sysfs_print(name ## _last_ ## frequency_units, (stats)->last	\
419 		    ? div_s64(local_clock() - (stats)->last,		\
420 			      NSEC_PER_ ## frequency_units)		\
421 		    : -1LL);						\
422 } while (0)
423 
424 #define sysfs_time_stats_attribute(name,				\
425 				   frequency_units,			\
426 				   duration_units)			\
427 read_attribute(name ## _average_frequency_ ## frequency_units);		\
428 read_attribute(name ## _average_duration_ ## duration_units);		\
429 read_attribute(name ## _max_duration_ ## duration_units);		\
430 read_attribute(name ## _last_ ## frequency_units)
431 
432 #define sysfs_time_stats_attribute_list(name,				\
433 					frequency_units,		\
434 					duration_units)			\
435 &sysfs_ ## name ## _average_frequency_ ## frequency_units,		\
436 &sysfs_ ## name ## _average_duration_ ## duration_units,		\
437 &sysfs_ ## name ## _max_duration_ ## duration_units,			\
438 &sysfs_ ## name ## _last_ ## frequency_units,
439 
440 #define ewma_add(ewma, val, weight, factor)				\
441 ({									\
442 	(ewma) *= (weight) - 1;						\
443 	(ewma) += (val) << factor;					\
444 	(ewma) /= (weight);						\
445 	(ewma) >> factor;						\
446 })
447 
448 struct bch_ratelimit {
449 	/* Next time we want to do some work, in nanoseconds */
450 	uint64_t		next;
451 
452 	/*
453 	 * Rate at which we want to do work, in units per nanosecond
454 	 * The units here correspond to the units passed to bch_next_delay()
455 	 */
456 	unsigned		rate;
457 };
458 
459 static inline void bch_ratelimit_reset(struct bch_ratelimit *d)
460 {
461 	d->next = local_clock();
462 }
463 
464 uint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done);
465 
466 #define __DIV_SAFE(n, d, zero)						\
467 ({									\
468 	typeof(n) _n = (n);						\
469 	typeof(d) _d = (d);						\
470 	_d ? _n / _d : zero;						\
471 })
472 
473 #define DIV_SAFE(n, d)	__DIV_SAFE(n, d, 0)
474 
475 #define container_of_or_null(ptr, type, member)				\
476 ({									\
477 	typeof(ptr) _ptr = ptr;						\
478 	_ptr ? container_of(_ptr, type, member) : NULL;			\
479 })
480 
481 #define RB_INSERT(root, new, member, cmp)				\
482 ({									\
483 	__label__ dup;							\
484 	struct rb_node **n = &(root)->rb_node, *parent = NULL;		\
485 	typeof(new) this;						\
486 	int res, ret = -1;						\
487 									\
488 	while (*n) {							\
489 		parent = *n;						\
490 		this = container_of(*n, typeof(*(new)), member);	\
491 		res = cmp(new, this);					\
492 		if (!res)						\
493 			goto dup;					\
494 		n = res < 0						\
495 			? &(*n)->rb_left				\
496 			: &(*n)->rb_right;				\
497 	}								\
498 									\
499 	rb_link_node(&(new)->member, parent, n);			\
500 	rb_insert_color(&(new)->member, root);				\
501 	ret = 0;							\
502 dup:									\
503 	ret;								\
504 })
505 
506 #define RB_SEARCH(root, search, member, cmp)				\
507 ({									\
508 	struct rb_node *n = (root)->rb_node;				\
509 	typeof(&(search)) this, ret = NULL;				\
510 	int res;							\
511 									\
512 	while (n) {							\
513 		this = container_of(n, typeof(search), member);		\
514 		res = cmp(&(search), this);				\
515 		if (!res) {						\
516 			ret = this;					\
517 			break;						\
518 		}							\
519 		n = res < 0						\
520 			? n->rb_left					\
521 			: n->rb_right;					\
522 	}								\
523 	ret;								\
524 })
525 
526 #define RB_GREATER(root, search, member, cmp)				\
527 ({									\
528 	struct rb_node *n = (root)->rb_node;				\
529 	typeof(&(search)) this, ret = NULL;				\
530 	int res;							\
531 									\
532 	while (n) {							\
533 		this = container_of(n, typeof(search), member);		\
534 		res = cmp(&(search), this);				\
535 		if (res < 0) {						\
536 			ret = this;					\
537 			n = n->rb_left;					\
538 		} else							\
539 			n = n->rb_right;				\
540 	}								\
541 	ret;								\
542 })
543 
544 #define RB_FIRST(root, type, member)					\
545 	container_of_or_null(rb_first(root), type, member)
546 
547 #define RB_LAST(root, type, member)					\
548 	container_of_or_null(rb_last(root), type, member)
549 
550 #define RB_NEXT(ptr, member)						\
551 	container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
552 
553 #define RB_PREV(ptr, member)						\
554 	container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
555 
556 /* Does linear interpolation between powers of two */
557 static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
558 {
559 	unsigned fract = x & ~(~0 << fract_bits);
560 
561 	x >>= fract_bits;
562 	x   = 1 << x;
563 	x  += (x * fract) >> fract_bits;
564 
565 	return x;
566 }
567 
568 void bch_bio_map(struct bio *bio, void *base);
569 
570 static inline sector_t bdev_sectors(struct block_device *bdev)
571 {
572 	return bdev->bd_inode->i_size >> 9;
573 }
574 
575 #define closure_bio_submit(bio, cl)					\
576 do {									\
577 	closure_get(cl);						\
578 	generic_make_request(bio);					\
579 } while (0)
580 
581 uint64_t bch_crc64_update(uint64_t, const void *, size_t);
582 uint64_t bch_crc64(const void *, size_t);
583 
584 #endif /* _BCACHE_UTIL_H */
585