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) \
39 BUG_ON(!((void *) (_dst) >= (void *) (_src) + (_len) || \
40 (void *) (_dst) + (_len) <= (void *) (_src))); \
41 memcpy(_dst, _src, _len); \
47 #define atomic_dec_bug(v) atomic_dec(v)
48 #define atomic_inc_bug(v, i) atomic_inc(v)
49 #define atomic_sub_bug(i, v) atomic_sub(i, v)
50 #define atomic_add_bug(i, v) atomic_add(i, v)
51 #define atomic_long_dec_bug(v) atomic_long_dec(v)
52 #define atomic_long_sub_bug(i, v) atomic_long_sub(i, v)
53 #define atomic64_dec_bug(v) atomic64_dec(v)
54 #define atomic64_inc_bug(v, i) atomic64_inc(v)
55 #define atomic64_sub_bug(i, v) atomic64_sub(i, v)
56 #define atomic64_add_bug(i, v) atomic64_add(i, v)
61 #define __flatten __attribute__((flatten))
63 /* sparse doesn't know about attribute((flatten)) */
67 #ifdef __LITTLE_ENDIAN
68 #define CPU_BIG_ENDIAN 0
70 #define CPU_BIG_ENDIAN 1
75 #define type_is_exact(_val, _type) \
76 __builtin_types_compatible_p(typeof(_val), _type)
78 #define type_is(_val, _type) \
79 (__builtin_types_compatible_p(typeof(_val), _type) || \
80 __builtin_types_compatible_p(typeof(_val), const _type))
82 static inline void kvpfree(void *p, size_t size)
86 else if (is_vmalloc_addr(p))
89 free_pages((unsigned long) p, get_order(size));
93 static inline void *kvpmalloc(size_t size, gfp_t gfp_mask)
95 return size < PAGE_SIZE ? kmalloc(size, gfp_mask)
96 : (void *) __get_free_pages(gfp_mask|__GFP_NOWARN,
98 ?: __vmalloc(size, gfp_mask, PAGE_KERNEL);
107 #define DECLARE_HEAP(type, name) HEAP(type) name
109 #define init_heap(heap, _size, gfp) \
112 (heap)->size = (_size); \
113 (heap)->data = kvpmalloc((heap)->size * sizeof((heap)->data[0]),\
117 #define free_heap(heap) \
119 kvpfree((heap)->data, (heap)->size * sizeof((heap)->data[0])); \
120 (heap)->data = NULL; \
123 #define heap_swap(h, i, j) swap((h)->data[i], (h)->data[j])
125 #define heap_peek(h) \
127 EBUG_ON(!(h)->used); \
131 #define heap_full(h) ((h)->used == (h)->size)
133 #define heap_sift_down(h, i, cmp) \
137 for (; _j * 2 + 1 < (h)->used; _j = _c) { \
139 if (_c + 1 < (h)->used && \
140 cmp(h, (h)->data[_c], (h)->data[_c + 1]) >= 0) \
143 if (cmp(h, (h)->data[_c], (h)->data[_j]) >= 0) \
145 heap_swap(h, _c, _j); \
149 #define heap_sift_up(h, i, cmp) \
152 size_t p = (i - 1) / 2; \
153 if (cmp(h, (h)->data[i], (h)->data[p]) >= 0) \
155 heap_swap(h, i, p); \
160 #define heap_add(h, new, cmp) \
162 bool _r = !heap_full(h); \
164 size_t _i = (h)->used++; \
165 (h)->data[_i] = new; \
167 heap_sift_up(h, _i, cmp); \
172 #define heap_add_or_replace(h, new, cmp) \
174 if (!heap_add(h, new, cmp) && \
175 cmp(h, new, heap_peek(h)) >= 0) { \
176 (h)->data[0] = new; \
177 heap_sift_down(h, 0, cmp); \
181 #define heap_del(h, i, cmp) \
185 BUG_ON(_i >= (h)->used); \
187 heap_swap(h, _i, (h)->used); \
188 heap_sift_up(h, _i, cmp); \
189 heap_sift_down(h, _i, cmp); \
192 #define heap_pop(h, d, cmp) \
194 bool _r = (h)->used; \
196 (d) = (h)->data[0]; \
197 heap_del(h, 0, cmp); \
202 #define heap_resort(heap, cmp) \
205 for (_i = (ssize_t) (heap)->used / 2 - 1; _i >= 0; --_i) \
206 heap_sift_down(heap, _i, cmp); \
210 * Simple array based allocator - preallocates a number of elements and you can
211 * never allocate more than that, also has no locking.
213 * Handy because if you know you only need a fixed number of elements you don't
214 * have to worry about memory allocation failure, and sometimes a mempool isn't
217 * We treat the free elements as entries in a singly linked list, and the
218 * freelist as a stack - allocating and freeing push and pop off the freelist.
221 #define DECLARE_ARRAY_ALLOCATOR(type, name, size) \
227 #define array_alloc(array) \
229 typeof((array)->freelist) _ret = (array)->freelist; \
232 (array)->freelist = *((typeof((array)->freelist) *) _ret);\
237 #define array_free(array, ptr) \
239 typeof((array)->freelist) _ptr = ptr; \
241 *((typeof((array)->freelist) *) _ptr) = (array)->freelist; \
242 (array)->freelist = _ptr; \
245 #define array_allocator_init(array) \
247 typeof((array)->freelist) _i; \
249 BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *)); \
250 (array)->freelist = NULL; \
252 for (_i = (array)->data; \
253 _i < (array)->data + ARRAY_SIZE((array)->data); \
255 array_free(array, _i); \
258 #define array_freelist_empty(array) ((array)->freelist == NULL)
260 #define ANYSINT_MAX(t) \
261 ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
263 int bch2_strtoint_h(const char *, int *);
264 int bch2_strtouint_h(const char *, unsigned int *);
265 int bch2_strtoll_h(const char *, long long *);
266 int bch2_strtoull_h(const char *, unsigned long long *);
268 static inline int bch2_strtol_h(const char *cp, long *res)
270 #if BITS_PER_LONG == 32
271 return bch2_strtoint_h(cp, (int *) res);
273 return bch2_strtoll_h(cp, (long long *) res);
277 static inline int bch2_strtoul_h(const char *cp, long *res)
279 #if BITS_PER_LONG == 32
280 return bch2_strtouint_h(cp, (unsigned int *) res);
282 return bch2_strtoull_h(cp, (unsigned long long *) res);
286 #define strtoi_h(cp, res) \
287 ( type_is(*res, int) ? bch2_strtoint_h(cp, (void *) res)\
288 : type_is(*res, long) ? bch2_strtol_h(cp, (void *) res)\
289 : type_is(*res, long long) ? bch2_strtoll_h(cp, (void *) res)\
290 : type_is(*res, unsigned) ? bch2_strtouint_h(cp, (void *) res)\
291 : type_is(*res, unsigned long) ? bch2_strtoul_h(cp, (void *) res)\
292 : type_is(*res, unsigned long long) ? bch2_strtoull_h(cp, (void *) res)\
295 #define strtoul_safe(cp, var) \
298 int _r = kstrtoul(cp, 10, &_v); \
304 #define strtoul_safe_clamp(cp, var, min, max) \
307 int _r = kstrtoul(cp, 10, &_v); \
309 var = clamp_t(typeof(var), _v, min, max); \
313 #define strtoul_safe_restrict(cp, var, min, max) \
316 int _r = kstrtoul(cp, 10, &_v); \
317 if (!_r && _v >= min && _v <= max) \
324 #define snprint(buf, size, var) \
325 snprintf(buf, size, \
326 type_is(var, int) ? "%i\n" \
327 : type_is(var, unsigned) ? "%u\n" \
328 : type_is(var, long) ? "%li\n" \
329 : type_is(var, unsigned long) ? "%lu\n" \
330 : type_is(var, s64) ? "%lli\n" \
331 : type_is(var, u64) ? "%llu\n" \
332 : type_is(var, char *) ? "%s\n" \
335 ssize_t bch2_hprint(char *buf, s64 v);
337 bool bch2_is_zero(const void *, size_t);
339 ssize_t bch2_snprint_string_list(char *buf, size_t size, const char * const list[],
342 ssize_t bch2_read_string_list(const char *buf, const char * const list[]);
348 * all fields are in nanoseconds, averages are ewmas stored left shifted
353 u64 average_duration;
354 u64 average_frequency;
358 void bch2_time_stats_clear(struct time_stats *stats);
359 void __bch2_time_stats_update(struct time_stats *stats, u64 time);
360 void bch2_time_stats_update(struct time_stats *stats, u64 time);
362 static inline unsigned local_clock_us(void)
364 return local_clock() >> 10;
367 #define NSEC_PER_ns 1L
368 #define NSEC_PER_us NSEC_PER_USEC
369 #define NSEC_PER_ms NSEC_PER_MSEC
370 #define NSEC_PER_sec NSEC_PER_SEC
372 #define __print_time_stat(stats, name, stat, units) \
373 sysfs_print(name ## _ ## stat ## _ ## units, \
374 div_u64((stats)->stat >> 8, NSEC_PER_ ## units))
376 #define sysfs_print_time_stats(stats, name, \
380 __print_time_stat(stats, name, \
381 average_frequency, frequency_units); \
382 __print_time_stat(stats, name, \
383 average_duration, duration_units); \
384 sysfs_print(name ## _ ##count, (stats)->count); \
385 sysfs_print(name ## _ ##last_duration ## _ ## duration_units, \
386 div_u64((stats)->last_duration, \
387 NSEC_PER_ ## duration_units)); \
388 sysfs_print(name ## _ ##max_duration ## _ ## duration_units, \
389 div_u64((stats)->max_duration, \
390 NSEC_PER_ ## duration_units)); \
392 sysfs_print(name ## _last_ ## frequency_units, (stats)->last \
393 ? div_s64(local_clock() - (stats)->last, \
394 NSEC_PER_ ## frequency_units) \
398 #define sysfs_clear_time_stats(stats, name) \
400 if (attr == &sysfs_ ## name ## _clear) \
401 bch2_time_stats_clear(stats); \
404 #define sysfs_time_stats_attribute(name, \
407 write_attribute(name ## _clear); \
408 read_attribute(name ## _count); \
409 read_attribute(name ## _average_frequency_ ## frequency_units); \
410 read_attribute(name ## _average_duration_ ## duration_units); \
411 read_attribute(name ## _last_duration_ ## duration_units); \
412 read_attribute(name ## _max_duration_ ## duration_units); \
413 read_attribute(name ## _last_ ## frequency_units)
415 #define sysfs_time_stats_attribute_list(name, \
418 &sysfs_ ## name ## _clear, \
419 &sysfs_ ## name ## _count, \
420 &sysfs_ ## name ## _average_frequency_ ## frequency_units, \
421 &sysfs_ ## name ## _average_duration_ ## duration_units, \
422 &sysfs_ ## name ## _last_duration_ ## duration_units, \
423 &sysfs_ ## name ## _max_duration_ ## duration_units, \
424 &sysfs_ ## name ## _last_ ## frequency_units,
426 #define ewma_add(ewma, val, weight) \
428 typeof(ewma) _ewma = (ewma); \
429 typeof(weight) _weight = (weight); \
431 (((_ewma << _weight) - _ewma) + (val)) >> _weight; \
434 struct bch_ratelimit {
435 /* Next time we want to do some work, in nanoseconds */
439 * Rate at which we want to do work, in units per nanosecond
440 * The units here correspond to the units passed to
441 * bch2_ratelimit_increment()
446 static inline void bch2_ratelimit_reset(struct bch_ratelimit *d)
448 d->next = local_clock();
451 u64 bch2_ratelimit_delay(struct bch_ratelimit *);
452 void bch2_ratelimit_increment(struct bch_ratelimit *, u64);
453 int bch2_ratelimit_wait_freezable_stoppable(struct bch_ratelimit *);
455 struct bch_pd_controller {
456 struct bch_ratelimit rate;
457 unsigned long last_update;
460 s64 smoothed_derivative;
462 unsigned p_term_inverse;
466 /* for exporting to sysfs (no effect on behavior) */
468 s64 last_proportional;
472 /* If true, the rate will not increase if bch2_ratelimit_delay()
473 * is not being called often enough. */
477 void bch2_pd_controller_update(struct bch_pd_controller *, s64, s64, int);
478 void bch2_pd_controller_init(struct bch_pd_controller *);
479 size_t bch2_pd_controller_print_debug(struct bch_pd_controller *, char *);
481 #define sysfs_pd_controller_attribute(name) \
482 rw_attribute(name##_rate); \
483 rw_attribute(name##_rate_bytes); \
484 rw_attribute(name##_rate_d_term); \
485 rw_attribute(name##_rate_p_term_inverse); \
486 read_attribute(name##_rate_debug)
488 #define sysfs_pd_controller_files(name) \
489 &sysfs_##name##_rate, \
490 &sysfs_##name##_rate_bytes, \
491 &sysfs_##name##_rate_d_term, \
492 &sysfs_##name##_rate_p_term_inverse, \
493 &sysfs_##name##_rate_debug
495 #define sysfs_pd_controller_show(name, var) \
497 sysfs_hprint(name##_rate, (var)->rate.rate); \
498 sysfs_print(name##_rate_bytes, (var)->rate.rate); \
499 sysfs_print(name##_rate_d_term, (var)->d_term); \
500 sysfs_print(name##_rate_p_term_inverse, (var)->p_term_inverse); \
502 if (attr == &sysfs_##name##_rate_debug) \
503 return bch2_pd_controller_print_debug(var, buf); \
506 #define sysfs_pd_controller_store(name, var) \
508 sysfs_strtoul_clamp(name##_rate, \
509 (var)->rate.rate, 1, UINT_MAX); \
510 sysfs_strtoul_clamp(name##_rate_bytes, \
511 (var)->rate.rate, 1, UINT_MAX); \
512 sysfs_strtoul(name##_rate_d_term, (var)->d_term); \
513 sysfs_strtoul_clamp(name##_rate_p_term_inverse, \
514 (var)->p_term_inverse, 1, INT_MAX); \
517 #define __DIV_SAFE(n, d, zero) \
519 typeof(n) _n = (n); \
520 typeof(d) _d = (d); \
521 _d ? _n / _d : zero; \
524 #define DIV_SAFE(n, d) __DIV_SAFE(n, d, 0)
526 #define container_of_or_null(ptr, type, member) \
528 typeof(ptr) _ptr = ptr; \
529 _ptr ? container_of(_ptr, type, member) : NULL; \
532 #define RB_INSERT(root, new, member, cmp) \
535 struct rb_node **n = &(root)->rb_node, *parent = NULL; \
541 this = container_of(*n, typeof(*(new)), member); \
542 res = cmp(new, this); \
550 rb_link_node(&(new)->member, parent, n); \
551 rb_insert_color(&(new)->member, root); \
557 #define RB_SEARCH(root, search, member, cmp) \
559 struct rb_node *n = (root)->rb_node; \
560 typeof(&(search)) this, ret = NULL; \
564 this = container_of(n, typeof(search), member); \
565 res = cmp(&(search), this); \
577 #define RB_GREATER(root, search, member, cmp) \
579 struct rb_node *n = (root)->rb_node; \
580 typeof(&(search)) this, ret = NULL; \
584 this = container_of(n, typeof(search), member); \
585 res = cmp(&(search), this); \
595 #define RB_FIRST(root, type, member) \
596 container_of_or_null(rb_first(root), type, member)
598 #define RB_LAST(root, type, member) \
599 container_of_or_null(rb_last(root), type, member)
601 #define RB_NEXT(ptr, member) \
602 container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
604 #define RB_PREV(ptr, member) \
605 container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
607 /* Does linear interpolation between powers of two */
608 static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
610 unsigned fract = x & ~(~0 << fract_bits);
614 x += (x * fract) >> fract_bits;
619 void bch2_bio_map(struct bio *bio, void *base);
621 static inline sector_t bdev_sectors(struct block_device *bdev)
623 return bdev->bd_inode->i_size >> 9;
626 #define closure_bio_submit(bio, cl) \
632 #define kthread_wait_freezable(cond) \
636 set_current_state(TASK_INTERRUPTIBLE); \
637 if (kthread_should_stop()) { \
648 set_current_state(TASK_RUNNING); \
652 size_t bch2_rand_range(size_t);
654 void memcpy_to_bio(struct bio *, struct bvec_iter, void *);
655 void memcpy_from_bio(void *, struct bio *, struct bvec_iter);
657 static inline void __memcpy_u64s(void *dst, const void *src,
662 asm volatile("rep ; movsq"
663 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
664 : "0" (u64s), "1" (dst), "2" (src)
675 static inline void memcpy_u64s(void *dst, const void *src,
678 EBUG_ON(!(dst >= src + u64s * sizeof(u64) ||
679 dst + u64s * sizeof(u64) <= src));
681 __memcpy_u64s(dst, src, u64s);
684 static inline void __memmove_u64s_down(void *dst, const void *src,
687 __memcpy_u64s(dst, src, u64s);
690 static inline void memmove_u64s_down(void *dst, const void *src,
695 __memmove_u64s_down(dst, src, u64s);
698 static inline void __memmove_u64s_up(void *_dst, const void *_src,
701 u64 *dst = (u64 *) _dst + u64s - 1;
702 u64 *src = (u64 *) _src + u64s - 1;
706 asm volatile("std ;\n"
709 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
710 : "0" (u64s), "1" (dst), "2" (src)
718 static inline void memmove_u64s_up(void *dst, const void *src,
723 __memmove_u64s_up(dst, src, u64s);
726 static inline void memmove_u64s(void *dst, const void *src,
730 __memmove_u64s_down(dst, src, u64s);
732 __memmove_u64s_up(dst, src, u64s);
735 static inline struct bio_vec next_contig_bvec(struct bio *bio,
736 struct bvec_iter *iter)
738 struct bio_vec bv = bio_iter_iovec(bio, *iter);
740 bio_advance_iter(bio, iter, bv.bv_len);
741 #ifndef CONFIG_HIGHMEM
742 while (iter->bi_size) {
743 struct bio_vec next = bio_iter_iovec(bio, *iter);
745 if (page_address(bv.bv_page) + bv.bv_offset + bv.bv_len !=
746 page_address(next.bv_page) + next.bv_offset)
749 bv.bv_len += next.bv_len;
750 bio_advance_iter(bio, iter, next.bv_len);
756 #define __bio_for_each_contig_segment(bv, bio, iter, start) \
757 for (iter = (start); \
759 ((bv = next_contig_bvec((bio), &(iter))), 1);)
761 #define bio_for_each_contig_segment(bv, bio, iter) \
762 __bio_for_each_contig_segment(bv, bio, iter, (bio)->bi_iter)
764 size_t bch_scnmemcpy(char *, size_t, const char *, size_t);
766 #endif /* _BCACHE_UTIL_H */