1 #ifndef _BCACHEFS_UTIL_H
2 #define _BCACHEFS_UTIL_H
5 #include <linux/blkdev.h>
6 #include <linux/closure.h>
7 #include <linux/errno.h>
8 #include <linux/freezer.h>
9 #include <linux/kernel.h>
10 #include <linux/sched/clock.h>
11 #include <linux/llist.h>
12 #include <linux/log2.h>
13 #include <linux/ratelimit.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/workqueue.h>
18 #define PAGE_SECTOR_SHIFT (PAGE_SHIFT - 9)
19 #define PAGE_SECTORS (1UL << PAGE_SECTOR_SHIFT)
23 #ifdef CONFIG_BCACHEFS_DEBUG
25 #define EBUG_ON(cond) BUG_ON(cond)
26 #define atomic_dec_bug(v) BUG_ON(atomic_dec_return(v) < 0)
27 #define atomic_inc_bug(v, i) BUG_ON(atomic_inc_return(v) <= i)
28 #define atomic_sub_bug(i, v) BUG_ON(atomic_sub_return(i, v) < 0)
29 #define atomic_add_bug(i, v) BUG_ON(atomic_add_return(i, v) < 0)
30 #define atomic_long_dec_bug(v) BUG_ON(atomic_long_dec_return(v) < 0)
31 #define atomic_long_sub_bug(i, v) BUG_ON(atomic_long_sub_return(i, v) < 0)
32 #define atomic64_dec_bug(v) BUG_ON(atomic64_dec_return(v) < 0)
33 #define atomic64_inc_bug(v, i) BUG_ON(atomic64_inc_return(v) <= i)
34 #define atomic64_sub_bug(i, v) BUG_ON(atomic64_sub_return(i, v) < 0)
35 #define atomic64_add_bug(i, v) BUG_ON(atomic64_add_return(i, v) < 0)
37 #define memcpy(dst, src, len) \
40 const void *_src = (src); \
41 size_t _len = (len); \
43 BUG_ON(!((void *) (_dst) >= (void *) (_src) + (_len) || \
44 (void *) (_dst) + (_len) <= (void *) (_src))); \
45 memcpy(_dst, _src, _len); \
51 #define atomic_dec_bug(v) atomic_dec(v)
52 #define atomic_inc_bug(v, i) atomic_inc(v)
53 #define atomic_sub_bug(i, v) atomic_sub(i, v)
54 #define atomic_add_bug(i, v) atomic_add(i, v)
55 #define atomic_long_dec_bug(v) atomic_long_dec(v)
56 #define atomic_long_sub_bug(i, v) atomic_long_sub(i, v)
57 #define atomic64_dec_bug(v) atomic64_dec(v)
58 #define atomic64_inc_bug(v, i) atomic64_inc(v)
59 #define atomic64_sub_bug(i, v) atomic64_sub(i, v)
60 #define atomic64_add_bug(i, v) atomic64_add(i, v)
65 #define __flatten __attribute__((flatten))
67 /* sparse doesn't know about attribute((flatten)) */
71 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
72 #define CPU_BIG_ENDIAN 0
73 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
74 #define CPU_BIG_ENDIAN 1
79 #define type_is_exact(_val, _type) \
80 __builtin_types_compatible_p(typeof(_val), _type)
82 #define type_is(_val, _type) \
83 (__builtin_types_compatible_p(typeof(_val), _type) || \
84 __builtin_types_compatible_p(typeof(_val), const _type))
86 static inline void vpfree(void *p, size_t size)
88 if (is_vmalloc_addr(p))
91 free_pages((unsigned long) p, get_order(size));
94 static inline void *vpmalloc(size_t size, gfp_t gfp_mask)
96 return (void *) __get_free_pages(gfp_mask|__GFP_NOWARN,
98 __vmalloc(size, gfp_mask, PAGE_KERNEL);
101 static inline void kvpfree(void *p, size_t size)
103 if (size < PAGE_SIZE)
109 static inline void *kvpmalloc(size_t size, gfp_t gfp_mask)
111 return size < PAGE_SIZE
112 ? kmalloc(size, gfp_mask)
113 : vpmalloc(size, gfp_mask);
116 int mempool_init_kvpmalloc_pool(mempool_t *, int, size_t);
124 #define DECLARE_HEAP(type, name) HEAP(type) name
126 #define init_heap(heap, _size, gfp) \
129 (heap)->size = (_size); \
130 (heap)->data = kvpmalloc((heap)->size * sizeof((heap)->data[0]),\
134 #define free_heap(heap) \
136 kvpfree((heap)->data, (heap)->size * sizeof((heap)->data[0])); \
137 (heap)->data = NULL; \
140 #define heap_swap(h, i, j) swap((h)->data[i], (h)->data[j])
142 #define heap_peek(h) \
144 EBUG_ON(!(h)->used); \
148 #define heap_full(h) ((h)->used == (h)->size)
150 #define heap_sift_down(h, i, cmp) \
154 for (; _j * 2 + 1 < (h)->used; _j = _c) { \
156 if (_c + 1 < (h)->used && \
157 cmp(h, (h)->data[_c], (h)->data[_c + 1]) >= 0) \
160 if (cmp(h, (h)->data[_c], (h)->data[_j]) >= 0) \
162 heap_swap(h, _c, _j); \
166 #define heap_sift_up(h, i, cmp) \
169 size_t p = (i - 1) / 2; \
170 if (cmp(h, (h)->data[i], (h)->data[p]) >= 0) \
172 heap_swap(h, i, p); \
177 #define __heap_add(h, d, cmp) \
179 size_t _i = (h)->used++; \
182 heap_sift_up(h, _i, cmp); \
185 #define heap_add(h, d, cmp) \
187 bool _r = !heap_full(h); \
189 __heap_add(h, d, cmp); \
193 #define heap_add_or_replace(h, new, cmp) \
195 if (!heap_add(h, new, cmp) && \
196 cmp(h, new, heap_peek(h)) >= 0) { \
197 (h)->data[0] = new; \
198 heap_sift_down(h, 0, cmp); \
202 #define heap_del(h, i, cmp) \
206 BUG_ON(_i >= (h)->used); \
208 heap_swap(h, _i, (h)->used); \
209 heap_sift_up(h, _i, cmp); \
210 heap_sift_down(h, _i, cmp); \
213 #define heap_pop(h, d, cmp) \
215 bool _r = (h)->used; \
217 (d) = (h)->data[0]; \
218 heap_del(h, 0, cmp); \
223 #define heap_resort(heap, cmp) \
226 for (_i = (ssize_t) (heap)->used / 2 - 1; _i >= 0; --_i) \
227 heap_sift_down(heap, _i, cmp); \
231 * Simple array based allocator - preallocates a number of elements and you can
232 * never allocate more than that, also has no locking.
234 * Handy because if you know you only need a fixed number of elements you don't
235 * have to worry about memory allocation failure, and sometimes a mempool isn't
238 * We treat the free elements as entries in a singly linked list, and the
239 * freelist as a stack - allocating and freeing push and pop off the freelist.
242 #define DECLARE_ARRAY_ALLOCATOR(type, name, size) \
248 #define array_alloc(array) \
250 typeof((array)->freelist) _ret = (array)->freelist; \
253 (array)->freelist = *((typeof((array)->freelist) *) _ret);\
258 #define array_free(array, ptr) \
260 typeof((array)->freelist) _ptr = ptr; \
262 *((typeof((array)->freelist) *) _ptr) = (array)->freelist; \
263 (array)->freelist = _ptr; \
266 #define array_allocator_init(array) \
268 typeof((array)->freelist) _i; \
270 BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *)); \
271 (array)->freelist = NULL; \
273 for (_i = (array)->data; \
274 _i < (array)->data + ARRAY_SIZE((array)->data); \
276 array_free(array, _i); \
279 #define array_freelist_empty(array) ((array)->freelist == NULL)
281 #define ANYSINT_MAX(t) \
282 ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
284 int bch2_strtoint_h(const char *, int *);
285 int bch2_strtouint_h(const char *, unsigned int *);
286 int bch2_strtoll_h(const char *, long long *);
287 int bch2_strtoull_h(const char *, unsigned long long *);
289 static inline int bch2_strtol_h(const char *cp, long *res)
291 #if BITS_PER_LONG == 32
292 return bch2_strtoint_h(cp, (int *) res);
294 return bch2_strtoll_h(cp, (long long *) res);
298 static inline int bch2_strtoul_h(const char *cp, long *res)
300 #if BITS_PER_LONG == 32
301 return bch2_strtouint_h(cp, (unsigned int *) res);
303 return bch2_strtoull_h(cp, (unsigned long long *) res);
307 #define strtoi_h(cp, res) \
308 ( type_is(*res, int) ? bch2_strtoint_h(cp, (void *) res)\
309 : type_is(*res, long) ? bch2_strtol_h(cp, (void *) res)\
310 : type_is(*res, long long) ? bch2_strtoll_h(cp, (void *) res)\
311 : type_is(*res, unsigned) ? bch2_strtouint_h(cp, (void *) res)\
312 : type_is(*res, unsigned long) ? bch2_strtoul_h(cp, (void *) res)\
313 : type_is(*res, unsigned long long) ? bch2_strtoull_h(cp, (void *) res)\
316 #define strtoul_safe(cp, var) \
319 int _r = kstrtoul(cp, 10, &_v); \
325 #define strtoul_safe_clamp(cp, var, min, max) \
328 int _r = kstrtoul(cp, 10, &_v); \
330 var = clamp_t(typeof(var), _v, min, max); \
334 #define strtoul_safe_restrict(cp, var, min, max) \
337 int _r = kstrtoul(cp, 10, &_v); \
338 if (!_r && _v >= min && _v <= max) \
345 #define snprint(buf, size, var) \
346 snprintf(buf, size, \
347 type_is(var, int) ? "%i\n" \
348 : type_is(var, unsigned) ? "%u\n" \
349 : type_is(var, long) ? "%li\n" \
350 : type_is(var, unsigned long) ? "%lu\n" \
351 : type_is(var, s64) ? "%lli\n" \
352 : type_is(var, u64) ? "%llu\n" \
353 : type_is(var, char *) ? "%s\n" \
356 ssize_t bch2_hprint(char *buf, s64 v);
358 bool bch2_is_zero(const void *, size_t);
360 ssize_t bch2_scnprint_string_list(char *, size_t, const char * const[], size_t);
362 ssize_t bch2_read_string_list(const char *, const char * const[]);
364 ssize_t bch2_scnprint_flag_list(char *, size_t, const char * const[], u64);
365 u64 bch2_read_flag_list(char *, const char * const[]);
367 #define NR_QUANTILES 15
368 #define QUANTILE_IDX(i) inorder_to_eytzinger0(i, NR_QUANTILES)
369 #define QUANTILE_FIRST eytzinger0_first(NR_QUANTILES)
370 #define QUANTILE_LAST eytzinger0_last(NR_QUANTILES)
373 struct quantile_entry {
376 } entries[NR_QUANTILES];
379 struct time_stat_buffer {
381 struct time_stat_buffer_entry {
390 /* all fields are in nanoseconds */
391 u64 average_duration;
392 u64 average_frequency;
395 struct quantiles quantiles;
397 struct time_stat_buffer __percpu *buffer;
400 void __bch2_time_stats_update(struct time_stats *stats, u64, u64);
402 static inline void bch2_time_stats_update(struct time_stats *stats, u64 start)
404 __bch2_time_stats_update(stats, start, local_clock());
407 size_t bch2_time_stats_print(struct time_stats *, char *, size_t);
409 void bch2_time_stats_exit(struct time_stats *);
410 void bch2_time_stats_init(struct time_stats *);
412 #define ewma_add(ewma, val, weight) \
414 typeof(ewma) _ewma = (ewma); \
415 typeof(weight) _weight = (weight); \
417 (((_ewma << _weight) - _ewma) + (val)) >> _weight; \
420 struct bch_ratelimit {
421 /* Next time we want to do some work, in nanoseconds */
425 * Rate at which we want to do work, in units per nanosecond
426 * The units here correspond to the units passed to
427 * bch2_ratelimit_increment()
432 static inline void bch2_ratelimit_reset(struct bch_ratelimit *d)
434 d->next = local_clock();
437 u64 bch2_ratelimit_delay(struct bch_ratelimit *);
438 void bch2_ratelimit_increment(struct bch_ratelimit *, u64);
439 int bch2_ratelimit_wait_freezable_stoppable(struct bch_ratelimit *);
441 struct bch_pd_controller {
442 struct bch_ratelimit rate;
443 unsigned long last_update;
446 s64 smoothed_derivative;
448 unsigned p_term_inverse;
452 /* for exporting to sysfs (no effect on behavior) */
454 s64 last_proportional;
458 /* If true, the rate will not increase if bch2_ratelimit_delay()
459 * is not being called often enough. */
463 void bch2_pd_controller_update(struct bch_pd_controller *, s64, s64, int);
464 void bch2_pd_controller_init(struct bch_pd_controller *);
465 size_t bch2_pd_controller_print_debug(struct bch_pd_controller *, char *);
467 #define sysfs_pd_controller_attribute(name) \
468 rw_attribute(name##_rate); \
469 rw_attribute(name##_rate_bytes); \
470 rw_attribute(name##_rate_d_term); \
471 rw_attribute(name##_rate_p_term_inverse); \
472 read_attribute(name##_rate_debug)
474 #define sysfs_pd_controller_files(name) \
475 &sysfs_##name##_rate, \
476 &sysfs_##name##_rate_bytes, \
477 &sysfs_##name##_rate_d_term, \
478 &sysfs_##name##_rate_p_term_inverse, \
479 &sysfs_##name##_rate_debug
481 #define sysfs_pd_controller_show(name, var) \
483 sysfs_hprint(name##_rate, (var)->rate.rate); \
484 sysfs_print(name##_rate_bytes, (var)->rate.rate); \
485 sysfs_print(name##_rate_d_term, (var)->d_term); \
486 sysfs_print(name##_rate_p_term_inverse, (var)->p_term_inverse); \
488 if (attr == &sysfs_##name##_rate_debug) \
489 return bch2_pd_controller_print_debug(var, buf); \
492 #define sysfs_pd_controller_store(name, var) \
494 sysfs_strtoul_clamp(name##_rate, \
495 (var)->rate.rate, 1, UINT_MAX); \
496 sysfs_strtoul_clamp(name##_rate_bytes, \
497 (var)->rate.rate, 1, UINT_MAX); \
498 sysfs_strtoul(name##_rate_d_term, (var)->d_term); \
499 sysfs_strtoul_clamp(name##_rate_p_term_inverse, \
500 (var)->p_term_inverse, 1, INT_MAX); \
503 #define __DIV_SAFE(n, d, zero) \
505 typeof(n) _n = (n); \
506 typeof(d) _d = (d); \
507 _d ? _n / _d : zero; \
510 #define DIV_SAFE(n, d) __DIV_SAFE(n, d, 0)
512 #define container_of_or_null(ptr, type, member) \
514 typeof(ptr) _ptr = ptr; \
515 _ptr ? container_of(_ptr, type, member) : NULL; \
518 #define RB_INSERT(root, new, member, cmp) \
521 struct rb_node **n = &(root)->rb_node, *parent = NULL; \
527 this = container_of(*n, typeof(*(new)), member); \
528 res = cmp(new, this); \
536 rb_link_node(&(new)->member, parent, n); \
537 rb_insert_color(&(new)->member, root); \
543 #define RB_SEARCH(root, search, member, cmp) \
545 struct rb_node *n = (root)->rb_node; \
546 typeof(&(search)) this, ret = NULL; \
550 this = container_of(n, typeof(search), member); \
551 res = cmp(&(search), this); \
563 #define RB_GREATER(root, search, member, cmp) \
565 struct rb_node *n = (root)->rb_node; \
566 typeof(&(search)) this, ret = NULL; \
570 this = container_of(n, typeof(search), member); \
571 res = cmp(&(search), this); \
581 #define RB_FIRST(root, type, member) \
582 container_of_or_null(rb_first(root), type, member)
584 #define RB_LAST(root, type, member) \
585 container_of_or_null(rb_last(root), type, member)
587 #define RB_NEXT(ptr, member) \
588 container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
590 #define RB_PREV(ptr, member) \
591 container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
593 /* Does linear interpolation between powers of two */
594 static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
596 unsigned fract = x & ~(~0 << fract_bits);
600 x += (x * fract) >> fract_bits;
605 void bch2_bio_map(struct bio *bio, void *base);
606 int bch2_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask);
608 static inline sector_t bdev_sectors(struct block_device *bdev)
610 return bdev->bd_inode->i_size >> 9;
613 #define closure_bio_submit(bio, cl) \
619 #define kthread_wait_freezable(cond) \
623 set_current_state(TASK_INTERRUPTIBLE); \
624 if (kthread_should_stop()) { \
635 set_current_state(TASK_RUNNING); \
639 size_t bch2_rand_range(size_t);
641 void memcpy_to_bio(struct bio *, struct bvec_iter, void *);
642 void memcpy_from_bio(void *, struct bio *, struct bvec_iter);
644 static inline void __memcpy_u64s(void *dst, const void *src,
649 asm volatile("rep ; movsq"
650 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
651 : "0" (u64s), "1" (dst), "2" (src)
662 static inline void memcpy_u64s(void *dst, const void *src,
665 EBUG_ON(!(dst >= src + u64s * sizeof(u64) ||
666 dst + u64s * sizeof(u64) <= src));
668 __memcpy_u64s(dst, src, u64s);
671 static inline void __memmove_u64s_down(void *dst, const void *src,
674 __memcpy_u64s(dst, src, u64s);
677 static inline void memmove_u64s_down(void *dst, const void *src,
682 __memmove_u64s_down(dst, src, u64s);
685 static inline void __memmove_u64s_up(void *_dst, const void *_src,
688 u64 *dst = (u64 *) _dst + u64s - 1;
689 u64 *src = (u64 *) _src + u64s - 1;
693 asm volatile("std ;\n"
696 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
697 : "0" (u64s), "1" (dst), "2" (src)
705 static inline void memmove_u64s_up(void *dst, const void *src,
710 __memmove_u64s_up(dst, src, u64s);
713 static inline void memmove_u64s(void *dst, const void *src,
717 __memmove_u64s_down(dst, src, u64s);
719 __memmove_u64s_up(dst, src, u64s);
722 static inline struct bio_vec next_contig_bvec(struct bio *bio,
723 struct bvec_iter *iter)
725 struct bio_vec bv = bio_iter_iovec(bio, *iter);
727 bio_advance_iter(bio, iter, bv.bv_len);
728 #ifndef CONFIG_HIGHMEM
729 while (iter->bi_size) {
730 struct bio_vec next = bio_iter_iovec(bio, *iter);
732 if (page_address(bv.bv_page) + bv.bv_offset + bv.bv_len !=
733 page_address(next.bv_page) + next.bv_offset)
736 bv.bv_len += next.bv_len;
737 bio_advance_iter(bio, iter, next.bv_len);
743 #define __bio_for_each_contig_segment(bv, bio, iter, start) \
744 for (iter = (start); \
746 ((bv = next_contig_bvec((bio), &(iter))), 1);)
748 #define bio_for_each_contig_segment(bv, bio, iter) \
749 __bio_for_each_contig_segment(bv, bio, iter, (bio)->bi_iter)
751 size_t bch_scnmemcpy(char *, size_t, const char *, size_t);
753 void sort_cmp_size(void *base, size_t num, size_t size,
754 int (*cmp_func)(const void *, const void *, size_t),
755 void (*swap_func)(void *, void *, size_t));
757 /* just the memmove, doesn't update @_nr */
758 #define __array_insert_item(_array, _nr, _pos) \
759 memmove(&(_array)[(_pos) + 1], \
761 sizeof((_array)[0]) * ((_nr) - (_pos)))
763 #define array_insert_item(_array, _nr, _pos, _new_item) \
765 __array_insert_item(_array, _nr, _pos); \
767 (_array)[(_pos)] = (_new_item); \
770 #define array_remove_items(_array, _nr, _pos, _nr_to_remove) \
772 (_nr) -= (_nr_to_remove); \
773 memmove(&(_array)[(_pos)], \
774 &(_array)[(_pos) + (_nr_to_remove)], \
775 sizeof((_array)[0]) * ((_nr) - (_pos))); \
778 #define array_remove_item(_array, _nr, _pos) \
779 array_remove_items(_array, _nr, _pos, 1)
781 #define bubble_sort(_base, _nr, _cmp) \
784 bool _swapped = true; \
786 for (_end = (ssize_t) (_nr) - 1; _end > 0 && _swapped; --_end) {\
788 for (_i = 0; _i < _end; _i++) \
789 if (_cmp((_base)[_i], (_base)[_i + 1]) > 0) { \
790 swap((_base)[_i], (_base)[_i + 1]); \
796 #endif /* _BCACHEFS_UTIL_H */