1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/blkdev.h>
7 #include <linux/closure.h>
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/sched/clock.h>
11 #include <linux/llist.h>
12 #include <linux/ratelimit.h>
13 #include <linux/vmalloc.h>
14 #include <linux/workqueue.h>
15 #include <linux/crc64.h>
19 #ifdef CONFIG_BCACHE_DEBUG
21 #define EBUG_ON(cond) BUG_ON(cond)
22 #define atomic_dec_bug(v) BUG_ON(atomic_dec_return(v) < 0)
23 #define atomic_inc_bug(v, i) BUG_ON(atomic_inc_return(v) <= i)
27 #define EBUG_ON(cond) do { if (cond) do {} while (0); } while (0)
28 #define atomic_dec_bug(v) atomic_dec(v)
29 #define atomic_inc_bug(v, i) atomic_inc(v)
33 #define DECLARE_HEAP(type, name) \
39 #define init_heap(heap, _size, gfp) \
43 (heap)->size = (_size); \
44 _bytes = (heap)->size * sizeof(*(heap)->data); \
45 (heap)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL); \
49 #define free_heap(heap) \
51 kvfree((heap)->data); \
52 (heap)->data = NULL; \
55 #define heap_swap(h, i, j) swap((h)->data[i], (h)->data[j])
57 #define heap_sift(h, i, cmp) \
61 for (; _j * 2 + 1 < (h)->used; _j = _r) { \
63 if (_r + 1 < (h)->used && \
64 cmp((h)->data[_r], (h)->data[_r + 1])) \
67 if (cmp((h)->data[_r], (h)->data[_j])) \
69 heap_swap(h, _r, _j); \
73 #define heap_sift_down(h, i, cmp) \
76 size_t p = (i - 1) / 2; \
77 if (cmp((h)->data[i], (h)->data[p])) \
84 #define heap_add(h, d, cmp) \
86 bool _r = !heap_full(h); \
88 size_t _i = (h)->used++; \
91 heap_sift_down(h, _i, cmp); \
92 heap_sift(h, _i, cmp); \
97 #define heap_pop(h, d, cmp) \
99 bool _r = (h)->used; \
101 (d) = (h)->data[0]; \
103 heap_swap(h, 0, (h)->used); \
104 heap_sift(h, 0, cmp); \
109 #define heap_peek(h) ((h)->used ? (h)->data[0] : NULL)
111 #define heap_full(h) ((h)->used == (h)->size)
113 #define DECLARE_FIFO(type, name) \
115 size_t front, back, size, mask; \
119 #define fifo_for_each(c, fifo, iter) \
120 for (iter = (fifo)->front; \
121 c = (fifo)->data[iter], iter != (fifo)->back; \
122 iter = (iter + 1) & (fifo)->mask)
124 #define __init_fifo(fifo, gfp) \
126 size_t _allocated_size, _bytes; \
127 BUG_ON(!(fifo)->size); \
129 _allocated_size = roundup_pow_of_two((fifo)->size + 1); \
130 _bytes = _allocated_size * sizeof(*(fifo)->data); \
132 (fifo)->mask = _allocated_size - 1; \
133 (fifo)->front = (fifo)->back = 0; \
135 (fifo)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL); \
139 #define init_fifo_exact(fifo, _size, gfp) \
141 (fifo)->size = (_size); \
142 __init_fifo(fifo, gfp); \
145 #define init_fifo(fifo, _size, gfp) \
147 (fifo)->size = (_size); \
148 if ((fifo)->size > 4) \
149 (fifo)->size = roundup_pow_of_two((fifo)->size) - 1; \
150 __init_fifo(fifo, gfp); \
153 #define free_fifo(fifo) \
155 kvfree((fifo)->data); \
156 (fifo)->data = NULL; \
159 #define fifo_used(fifo) (((fifo)->back - (fifo)->front) & (fifo)->mask)
160 #define fifo_free(fifo) ((fifo)->size - fifo_used(fifo))
162 #define fifo_empty(fifo) (!fifo_used(fifo))
163 #define fifo_full(fifo) (!fifo_free(fifo))
165 #define fifo_front(fifo) ((fifo)->data[(fifo)->front])
166 #define fifo_back(fifo) \
167 ((fifo)->data[((fifo)->back - 1) & (fifo)->mask])
169 #define fifo_idx(fifo, p) (((p) - &fifo_front(fifo)) & (fifo)->mask)
171 #define fifo_push_back(fifo, i) \
173 bool _r = !fifo_full((fifo)); \
175 (fifo)->data[(fifo)->back++] = (i); \
176 (fifo)->back &= (fifo)->mask; \
181 #define fifo_pop_front(fifo, i) \
183 bool _r = !fifo_empty((fifo)); \
185 (i) = (fifo)->data[(fifo)->front++]; \
186 (fifo)->front &= (fifo)->mask; \
191 #define fifo_push_front(fifo, i) \
193 bool _r = !fifo_full((fifo)); \
196 (fifo)->front &= (fifo)->mask; \
197 (fifo)->data[(fifo)->front] = (i); \
202 #define fifo_pop_back(fifo, i) \
204 bool _r = !fifo_empty((fifo)); \
207 (fifo)->back &= (fifo)->mask; \
208 (i) = (fifo)->data[(fifo)->back] \
213 #define fifo_push(fifo, i) fifo_push_back(fifo, (i))
214 #define fifo_pop(fifo, i) fifo_pop_front(fifo, (i))
216 #define fifo_swap(l, r) \
218 swap((l)->front, (r)->front); \
219 swap((l)->back, (r)->back); \
220 swap((l)->size, (r)->size); \
221 swap((l)->mask, (r)->mask); \
222 swap((l)->data, (r)->data); \
225 #define fifo_move(dest, src) \
227 typeof(*((dest)->data)) _t; \
228 while (!fifo_full(dest) && \
230 fifo_push(dest, _t); \
234 * Simple array based allocator - preallocates a number of elements and you can
235 * never allocate more than that, also has no locking.
237 * Handy because if you know you only need a fixed number of elements you don't
238 * have to worry about memory allocation failure, and sometimes a mempool isn't
241 * We treat the free elements as entries in a singly linked list, and the
242 * freelist as a stack - allocating and freeing push and pop off the freelist.
245 #define DECLARE_ARRAY_ALLOCATOR(type, name, size) \
251 #define array_alloc(array) \
253 typeof((array)->freelist) _ret = (array)->freelist; \
256 (array)->freelist = *((typeof((array)->freelist) *) _ret);\
261 #define array_free(array, ptr) \
263 typeof((array)->freelist) _ptr = ptr; \
265 *((typeof((array)->freelist) *) _ptr) = (array)->freelist; \
266 (array)->freelist = _ptr; \
269 #define array_allocator_init(array) \
271 typeof((array)->freelist) _i; \
273 BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *)); \
274 (array)->freelist = NULL; \
276 for (_i = (array)->data; \
277 _i < (array)->data + ARRAY_SIZE((array)->data); \
279 array_free(array, _i); \
282 #define array_freelist_empty(array) ((array)->freelist == NULL)
284 #define ANYSINT_MAX(t) \
285 ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
287 int bch_strtoint_h(const char *cp, int *res);
288 int bch_strtouint_h(const char *cp, unsigned int *res);
289 int bch_strtoll_h(const char *cp, long long *res);
290 int bch_strtoull_h(const char *cp, unsigned long long *res);
292 static inline int bch_strtol_h(const char *cp, long *res)
294 #if BITS_PER_LONG == 32
295 return bch_strtoint_h(cp, (int *) res);
297 return bch_strtoll_h(cp, (long long *) res);
301 static inline int bch_strtoul_h(const char *cp, long *res)
303 #if BITS_PER_LONG == 32
304 return bch_strtouint_h(cp, (unsigned int *) res);
306 return bch_strtoull_h(cp, (unsigned long long *) res);
310 #define strtoi_h(cp, res) \
311 (__builtin_types_compatible_p(typeof(*res), int) \
312 ? bch_strtoint_h(cp, (void *) res) \
313 : __builtin_types_compatible_p(typeof(*res), long) \
314 ? bch_strtol_h(cp, (void *) res) \
315 : __builtin_types_compatible_p(typeof(*res), long long) \
316 ? bch_strtoll_h(cp, (void *) res) \
317 : __builtin_types_compatible_p(typeof(*res), unsigned int) \
318 ? bch_strtouint_h(cp, (void *) res) \
319 : __builtin_types_compatible_p(typeof(*res), unsigned long) \
320 ? bch_strtoul_h(cp, (void *) res) \
321 : __builtin_types_compatible_p(typeof(*res), unsigned long long)\
322 ? bch_strtoull_h(cp, (void *) res) : -EINVAL)
324 #define strtoul_safe(cp, var) \
327 int _r = kstrtoul(cp, 10, &_v); \
333 #define strtoul_safe_clamp(cp, var, min, max) \
336 int _r = kstrtoul(cp, 10, &_v); \
338 var = clamp_t(typeof(var), _v, min, max); \
342 ssize_t bch_hprint(char *buf, int64_t v);
344 bool bch_is_zero(const char *p, size_t n);
345 int bch_parse_uuid(const char *s, char *uuid);
350 * all fields are in nanoseconds, averages are ewmas stored left shifted
353 uint64_t max_duration;
354 uint64_t average_duration;
355 uint64_t average_frequency;
359 void bch_time_stats_update(struct time_stats *stats, uint64_t time);
361 static inline unsigned int local_clock_us(void)
363 return local_clock() >> 10;
366 #define NSEC_PER_ns 1L
367 #define NSEC_PER_us NSEC_PER_USEC
368 #define NSEC_PER_ms NSEC_PER_MSEC
369 #define NSEC_PER_sec NSEC_PER_SEC
371 #define __print_time_stat(stats, name, stat, units) \
372 sysfs_print(name ## _ ## stat ## _ ## units, \
373 div_u64((stats)->stat >> 8, NSEC_PER_ ## units))
375 #define sysfs_print_time_stats(stats, name, \
379 __print_time_stat(stats, name, \
380 average_frequency, frequency_units); \
381 __print_time_stat(stats, name, \
382 average_duration, duration_units); \
383 sysfs_print(name ## _ ##max_duration ## _ ## duration_units, \
384 div_u64((stats)->max_duration, \
385 NSEC_PER_ ## duration_units)); \
387 sysfs_print(name ## _last_ ## frequency_units, (stats)->last \
388 ? div_s64(local_clock() - (stats)->last, \
389 NSEC_PER_ ## frequency_units) \
393 #define sysfs_time_stats_attribute(name, \
396 read_attribute(name ## _average_frequency_ ## frequency_units); \
397 read_attribute(name ## _average_duration_ ## duration_units); \
398 read_attribute(name ## _max_duration_ ## duration_units); \
399 read_attribute(name ## _last_ ## frequency_units)
401 #define sysfs_time_stats_attribute_list(name, \
404 &sysfs_ ## name ## _average_frequency_ ## frequency_units, \
405 &sysfs_ ## name ## _average_duration_ ## duration_units, \
406 &sysfs_ ## name ## _max_duration_ ## duration_units, \
407 &sysfs_ ## name ## _last_ ## frequency_units,
409 #define ewma_add(ewma, val, weight, factor) \
411 (ewma) *= (weight) - 1; \
412 (ewma) += (val) << factor; \
413 (ewma) /= (weight); \
417 struct bch_ratelimit {
418 /* Next time we want to do some work, in nanoseconds */
422 * Rate at which we want to do work, in units per second
423 * The units here correspond to the units passed to bch_next_delay()
428 static inline void bch_ratelimit_reset(struct bch_ratelimit *d)
430 d->next = local_clock();
433 uint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done);
435 #define __DIV_SAFE(n, d, zero) \
437 typeof(n) _n = (n); \
438 typeof(d) _d = (d); \
439 _d ? _n / _d : zero; \
442 #define DIV_SAFE(n, d) __DIV_SAFE(n, d, 0)
444 #define container_of_or_null(ptr, type, member) \
446 typeof(ptr) _ptr = ptr; \
447 _ptr ? container_of(_ptr, type, member) : NULL; \
450 #define RB_INSERT(root, new, member, cmp) \
453 struct rb_node **n = &(root)->rb_node, *parent = NULL; \
459 this = container_of(*n, typeof(*(new)), member); \
460 res = cmp(new, this); \
468 rb_link_node(&(new)->member, parent, n); \
469 rb_insert_color(&(new)->member, root); \
475 #define RB_SEARCH(root, search, member, cmp) \
477 struct rb_node *n = (root)->rb_node; \
478 typeof(&(search)) this, ret = NULL; \
482 this = container_of(n, typeof(search), member); \
483 res = cmp(&(search), this); \
495 #define RB_GREATER(root, search, member, cmp) \
497 struct rb_node *n = (root)->rb_node; \
498 typeof(&(search)) this, ret = NULL; \
502 this = container_of(n, typeof(search), member); \
503 res = cmp(&(search), this); \
513 #define RB_FIRST(root, type, member) \
514 container_of_or_null(rb_first(root), type, member)
516 #define RB_LAST(root, type, member) \
517 container_of_or_null(rb_last(root), type, member)
519 #define RB_NEXT(ptr, member) \
520 container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
522 #define RB_PREV(ptr, member) \
523 container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
525 static inline uint64_t bch_crc64(const void *p, size_t len)
527 uint64_t crc = 0xffffffffffffffffULL;
529 crc = crc64_be(crc, p, len);
530 return crc ^ 0xffffffffffffffffULL;
534 * A stepwise-linear pseudo-exponential. This returns 1 << (x >>
535 * frac_bits), with the less-significant bits filled in by linear
538 * This can also be interpreted as a floating-point number format,
539 * where the low frac_bits are the mantissa (with implicit leading
540 * 1 bit), and the more significant bits are the exponent.
541 * The return value is 1.mantissa * 2^exponent.
543 * The way this is used, fract_bits is 6 and the largest possible
544 * input is CONGESTED_MAX-1 = 1023 (exponent 16, mantissa 0x1.fc),
545 * so the maximum output is 0x1fc00.
547 static inline unsigned int fract_exp_two(unsigned int x,
548 unsigned int fract_bits)
550 unsigned int mantissa = 1 << fract_bits; /* Implicit bit */
552 mantissa += x & (mantissa - 1);
553 x >>= fract_bits; /* The exponent */
554 /* Largest intermediate value 0x7f0000 */
555 return mantissa << x >> fract_bits;
558 void bch_bio_map(struct bio *bio, void *base);
559 int bch_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask);
561 #endif /* _BCACHE_UTIL_H */