Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 
0003 #ifndef _BCACHE_UTIL_H
0004 #define _BCACHE_UTIL_H
0005 
0006 #include <linux/blkdev.h>
0007 #include <linux/errno.h>
0008 #include <linux/kernel.h>
0009 #include <linux/sched/clock.h>
0010 #include <linux/llist.h>
0011 #include <linux/ratelimit.h>
0012 #include <linux/vmalloc.h>
0013 #include <linux/workqueue.h>
0014 #include <linux/crc64.h>
0015 
0016 #include "closure.h"
0017 
0018 struct closure;
0019 
0020 #ifdef CONFIG_BCACHE_DEBUG
0021 
0022 #define EBUG_ON(cond)           BUG_ON(cond)
0023 #define atomic_dec_bug(v)   BUG_ON(atomic_dec_return(v) < 0)
0024 #define atomic_inc_bug(v, i)    BUG_ON(atomic_inc_return(v) <= i)
0025 
0026 #else /* DEBUG */
0027 
0028 #define EBUG_ON(cond)       do { if (cond) do {} while (0); } while (0)
0029 #define atomic_dec_bug(v)   atomic_dec(v)
0030 #define atomic_inc_bug(v, i)    atomic_inc(v)
0031 
0032 #endif
0033 
0034 #define DECLARE_HEAP(type, name)                    \
0035     struct {                            \
0036         size_t size, used;                  \
0037         type *data;                     \
0038     } name
0039 
0040 #define init_heap(heap, _size, gfp)                 \
0041 ({                                  \
0042     size_t _bytes;                          \
0043     (heap)->used = 0;                       \
0044     (heap)->size = (_size);                     \
0045     _bytes = (heap)->size * sizeof(*(heap)->data);          \
0046     (heap)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL);        \
0047     (heap)->data;                           \
0048 })
0049 
0050 #define free_heap(heap)                         \
0051 do {                                    \
0052     kvfree((heap)->data);                       \
0053     (heap)->data = NULL;                        \
0054 } while (0)
0055 
0056 #define heap_swap(h, i, j)  swap((h)->data[i], (h)->data[j])
0057 
0058 #define heap_sift(h, i, cmp)                        \
0059 do {                                    \
0060     size_t _r, _j = i;                      \
0061                                     \
0062     for (; _j * 2 + 1 < (h)->used; _j = _r) {           \
0063         _r = _j * 2 + 1;                    \
0064         if (_r + 1 < (h)->used &&               \
0065             cmp((h)->data[_r], (h)->data[_r + 1]))      \
0066             _r++;                       \
0067                                     \
0068         if (cmp((h)->data[_r], (h)->data[_j]))          \
0069             break;                      \
0070         heap_swap(h, _r, _j);                   \
0071     }                               \
0072 } while (0)
0073 
0074 #define heap_sift_down(h, i, cmp)                   \
0075 do {                                    \
0076     while (i) {                         \
0077         size_t p = (i - 1) / 2;                 \
0078         if (cmp((h)->data[i], (h)->data[p]))            \
0079             break;                      \
0080         heap_swap(h, i, p);                 \
0081         i = p;                          \
0082     }                               \
0083 } while (0)
0084 
0085 #define heap_add(h, d, cmp)                     \
0086 ({                                  \
0087     bool _r = !heap_full(h);                    \
0088     if (_r) {                           \
0089         size_t _i = (h)->used++;                \
0090         (h)->data[_i] = d;                  \
0091                                     \
0092         heap_sift_down(h, _i, cmp);             \
0093         heap_sift(h, _i, cmp);                  \
0094     }                               \
0095     _r;                             \
0096 })
0097 
0098 #define heap_pop(h, d, cmp)                     \
0099 ({                                  \
0100     bool _r = (h)->used;                        \
0101     if (_r) {                           \
0102         (d) = (h)->data[0];                 \
0103         (h)->used--;                        \
0104         heap_swap(h, 0, (h)->used);             \
0105         heap_sift(h, 0, cmp);                   \
0106     }                               \
0107     _r;                             \
0108 })
0109 
0110 #define heap_peek(h)    ((h)->used ? (h)->data[0] : NULL)
0111 
0112 #define heap_full(h)    ((h)->used == (h)->size)
0113 
0114 #define DECLARE_FIFO(type, name)                    \
0115     struct {                            \
0116         size_t front, back, size, mask;             \
0117         type *data;                     \
0118     } name
0119 
0120 #define fifo_for_each(c, fifo, iter)                    \
0121     for (iter = (fifo)->front;                  \
0122          c = (fifo)->data[iter], iter != (fifo)->back;      \
0123          iter = (iter + 1) & (fifo)->mask)
0124 
0125 #define __init_fifo(fifo, gfp)                      \
0126 ({                                  \
0127     size_t _allocated_size, _bytes;                 \
0128     BUG_ON(!(fifo)->size);                      \
0129                                     \
0130     _allocated_size = roundup_pow_of_two((fifo)->size + 1);     \
0131     _bytes = _allocated_size * sizeof(*(fifo)->data);       \
0132                                     \
0133     (fifo)->mask = _allocated_size - 1;             \
0134     (fifo)->front = (fifo)->back = 0;               \
0135                                     \
0136     (fifo)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL);        \
0137     (fifo)->data;                           \
0138 })
0139 
0140 #define init_fifo_exact(fifo, _size, gfp)               \
0141 ({                                  \
0142     (fifo)->size = (_size);                     \
0143     __init_fifo(fifo, gfp);                     \
0144 })
0145 
0146 #define init_fifo(fifo, _size, gfp)                 \
0147 ({                                  \
0148     (fifo)->size = (_size);                     \
0149     if ((fifo)->size > 4)                       \
0150         (fifo)->size = roundup_pow_of_two((fifo)->size) - 1;    \
0151     __init_fifo(fifo, gfp);                     \
0152 })
0153 
0154 #define free_fifo(fifo)                         \
0155 do {                                    \
0156     kvfree((fifo)->data);                       \
0157     (fifo)->data = NULL;                        \
0158 } while (0)
0159 
0160 #define fifo_used(fifo)     (((fifo)->back - (fifo)->front) & (fifo)->mask)
0161 #define fifo_free(fifo)     ((fifo)->size - fifo_used(fifo))
0162 
0163 #define fifo_empty(fifo)    (!fifo_used(fifo))
0164 #define fifo_full(fifo)     (!fifo_free(fifo))
0165 
0166 #define fifo_front(fifo)    ((fifo)->data[(fifo)->front])
0167 #define fifo_back(fifo)                         \
0168     ((fifo)->data[((fifo)->back - 1) & (fifo)->mask])
0169 
0170 #define fifo_idx(fifo, p)   (((p) - &fifo_front(fifo)) & (fifo)->mask)
0171 
0172 #define fifo_push_back(fifo, i)                     \
0173 ({                                  \
0174     bool _r = !fifo_full((fifo));                   \
0175     if (_r) {                           \
0176         (fifo)->data[(fifo)->back++] = (i);         \
0177         (fifo)->back &= (fifo)->mask;               \
0178     }                               \
0179     _r;                             \
0180 })
0181 
0182 #define fifo_pop_front(fifo, i)                     \
0183 ({                                  \
0184     bool _r = !fifo_empty((fifo));                  \
0185     if (_r) {                           \
0186         (i) = (fifo)->data[(fifo)->front++];            \
0187         (fifo)->front &= (fifo)->mask;              \
0188     }                               \
0189     _r;                             \
0190 })
0191 
0192 #define fifo_push_front(fifo, i)                    \
0193 ({                                  \
0194     bool _r = !fifo_full((fifo));                   \
0195     if (_r) {                           \
0196         --(fifo)->front;                    \
0197         (fifo)->front &= (fifo)->mask;              \
0198         (fifo)->data[(fifo)->front] = (i);          \
0199     }                               \
0200     _r;                             \
0201 })
0202 
0203 #define fifo_pop_back(fifo, i)                      \
0204 ({                                  \
0205     bool _r = !fifo_empty((fifo));                  \
0206     if (_r) {                           \
0207         --(fifo)->back;                     \
0208         (fifo)->back &= (fifo)->mask;               \
0209         (i) = (fifo)->data[(fifo)->back]            \
0210     }                               \
0211     _r;                             \
0212 })
0213 
0214 #define fifo_push(fifo, i)  fifo_push_back(fifo, (i))
0215 #define fifo_pop(fifo, i)   fifo_pop_front(fifo, (i))
0216 
0217 #define fifo_swap(l, r)                         \
0218 do {                                    \
0219     swap((l)->front, (r)->front);                   \
0220     swap((l)->back, (r)->back);                 \
0221     swap((l)->size, (r)->size);                 \
0222     swap((l)->mask, (r)->mask);                 \
0223     swap((l)->data, (r)->data);                 \
0224 } while (0)
0225 
0226 #define fifo_move(dest, src)                        \
0227 do {                                    \
0228     typeof(*((dest)->data)) _t;                 \
0229     while (!fifo_full(dest) &&                  \
0230            fifo_pop(src, _t))                   \
0231         fifo_push(dest, _t);                    \
0232 } while (0)
0233 
0234 /*
0235  * Simple array based allocator - preallocates a number of elements and you can
0236  * never allocate more than that, also has no locking.
0237  *
0238  * Handy because if you know you only need a fixed number of elements you don't
0239  * have to worry about memory allocation failure, and sometimes a mempool isn't
0240  * what you want.
0241  *
0242  * We treat the free elements as entries in a singly linked list, and the
0243  * freelist as a stack - allocating and freeing push and pop off the freelist.
0244  */
0245 
0246 #define DECLARE_ARRAY_ALLOCATOR(type, name, size)           \
0247     struct {                            \
0248         type    *freelist;                  \
0249         type    data[size];                 \
0250     } name
0251 
0252 #define array_alloc(array)                      \
0253 ({                                  \
0254     typeof((array)->freelist) _ret = (array)->freelist;     \
0255                                     \
0256     if (_ret)                           \
0257         (array)->freelist = *((typeof((array)->freelist) *) _ret);\
0258                                     \
0259     _ret;                               \
0260 })
0261 
0262 #define array_free(array, ptr)                      \
0263 do {                                    \
0264     typeof((array)->freelist) _ptr = ptr;               \
0265                                     \
0266     *((typeof((array)->freelist) *) _ptr) = (array)->freelist;  \
0267     (array)->freelist = _ptr;                   \
0268 } while (0)
0269 
0270 #define array_allocator_init(array)                 \
0271 do {                                    \
0272     typeof((array)->freelist) _i;                   \
0273                                     \
0274     BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *));    \
0275     (array)->freelist = NULL;                   \
0276                                     \
0277     for (_i = (array)->data;                    \
0278          _i < (array)->data + ARRAY_SIZE((array)->data);        \
0279          _i++)                          \
0280         array_free(array, _i);                  \
0281 } while (0)
0282 
0283 #define array_freelist_empty(array) ((array)->freelist == NULL)
0284 
0285 #define ANYSINT_MAX(t)                          \
0286     ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
0287 
0288 int bch_strtoint_h(const char *cp, int *res);
0289 int bch_strtouint_h(const char *cp, unsigned int *res);
0290 int bch_strtoll_h(const char *cp, long long *res);
0291 int bch_strtoull_h(const char *cp, unsigned long long *res);
0292 
0293 static inline int bch_strtol_h(const char *cp, long *res)
0294 {
0295 #if BITS_PER_LONG == 32
0296     return bch_strtoint_h(cp, (int *) res);
0297 #else
0298     return bch_strtoll_h(cp, (long long *) res);
0299 #endif
0300 }
0301 
0302 static inline int bch_strtoul_h(const char *cp, long *res)
0303 {
0304 #if BITS_PER_LONG == 32
0305     return bch_strtouint_h(cp, (unsigned int *) res);
0306 #else
0307     return bch_strtoull_h(cp, (unsigned long long *) res);
0308 #endif
0309 }
0310 
0311 #define strtoi_h(cp, res)                       \
0312     (__builtin_types_compatible_p(typeof(*res), int)        \
0313     ? bch_strtoint_h(cp, (void *) res)              \
0314     : __builtin_types_compatible_p(typeof(*res), long)      \
0315     ? bch_strtol_h(cp, (void *) res)                \
0316     : __builtin_types_compatible_p(typeof(*res), long long)     \
0317     ? bch_strtoll_h(cp, (void *) res)               \
0318     : __builtin_types_compatible_p(typeof(*res), unsigned int)  \
0319     ? bch_strtouint_h(cp, (void *) res)             \
0320     : __builtin_types_compatible_p(typeof(*res), unsigned long) \
0321     ? bch_strtoul_h(cp, (void *) res)               \
0322     : __builtin_types_compatible_p(typeof(*res), unsigned long long)\
0323     ? bch_strtoull_h(cp, (void *) res) : -EINVAL)
0324 
0325 #define strtoul_safe(cp, var)                       \
0326 ({                                  \
0327     unsigned long _v;                       \
0328     int _r = kstrtoul(cp, 10, &_v);                 \
0329     if (!_r)                            \
0330         var = _v;                       \
0331     _r;                             \
0332 })
0333 
0334 #define strtoul_safe_clamp(cp, var, min, max)               \
0335 ({                                  \
0336     unsigned long _v;                       \
0337     int _r = kstrtoul(cp, 10, &_v);                 \
0338     if (!_r)                            \
0339         var = clamp_t(typeof(var), _v, min, max);       \
0340     _r;                             \
0341 })
0342 
0343 ssize_t bch_hprint(char *buf, int64_t v);
0344 
0345 bool bch_is_zero(const char *p, size_t n);
0346 int bch_parse_uuid(const char *s, char *uuid);
0347 
0348 struct time_stats {
0349     spinlock_t  lock;
0350     /*
0351      * all fields are in nanoseconds, averages are ewmas stored left shifted
0352      * by 8
0353      */
0354     uint64_t    max_duration;
0355     uint64_t    average_duration;
0356     uint64_t    average_frequency;
0357     uint64_t    last;
0358 };
0359 
0360 void bch_time_stats_update(struct time_stats *stats, uint64_t time);
0361 
0362 static inline unsigned int local_clock_us(void)
0363 {
0364     return local_clock() >> 10;
0365 }
0366 
0367 #define NSEC_PER_ns         1L
0368 #define NSEC_PER_us         NSEC_PER_USEC
0369 #define NSEC_PER_ms         NSEC_PER_MSEC
0370 #define NSEC_PER_sec            NSEC_PER_SEC
0371 
0372 #define __print_time_stat(stats, name, stat, units)         \
0373     sysfs_print(name ## _ ## stat ## _ ## units,            \
0374             div_u64((stats)->stat >> 8, NSEC_PER_ ## units))
0375 
0376 #define sysfs_print_time_stats(stats, name,             \
0377                    frequency_units,             \
0378                    duration_units)              \
0379 do {                                    \
0380     __print_time_stat(stats, name,                  \
0381               average_frequency,    frequency_units);   \
0382     __print_time_stat(stats, name,                  \
0383               average_duration, duration_units);    \
0384     sysfs_print(name ## _ ##max_duration ## _ ## duration_units,    \
0385             div_u64((stats)->max_duration,          \
0386                 NSEC_PER_ ## duration_units));      \
0387                                     \
0388     sysfs_print(name ## _last_ ## frequency_units, (stats)->last    \
0389             ? div_s64(local_clock() - (stats)->last,        \
0390                   NSEC_PER_ ## frequency_units)     \
0391             : -1LL);                        \
0392 } while (0)
0393 
0394 #define sysfs_time_stats_attribute(name,                \
0395                    frequency_units,         \
0396                    duration_units)          \
0397 read_attribute(name ## _average_frequency_ ## frequency_units);     \
0398 read_attribute(name ## _average_duration_ ## duration_units);       \
0399 read_attribute(name ## _max_duration_ ## duration_units);       \
0400 read_attribute(name ## _last_ ## frequency_units)
0401 
0402 #define sysfs_time_stats_attribute_list(name,               \
0403                     frequency_units,        \
0404                     duration_units)         \
0405 &sysfs_ ## name ## _average_frequency_ ## frequency_units,      \
0406 &sysfs_ ## name ## _average_duration_ ## duration_units,        \
0407 &sysfs_ ## name ## _max_duration_ ## duration_units,            \
0408 &sysfs_ ## name ## _last_ ## frequency_units,
0409 
0410 #define ewma_add(ewma, val, weight, factor)             \
0411 ({                                  \
0412     (ewma) *= (weight) - 1;                     \
0413     (ewma) += (val) << factor;                  \
0414     (ewma) /= (weight);                     \
0415     (ewma) >> factor;                       \
0416 })
0417 
0418 struct bch_ratelimit {
0419     /* Next time we want to do some work, in nanoseconds */
0420     uint64_t        next;
0421 
0422     /*
0423      * Rate at which we want to do work, in units per second
0424      * The units here correspond to the units passed to bch_next_delay()
0425      */
0426     atomic_long_t       rate;
0427 };
0428 
0429 static inline void bch_ratelimit_reset(struct bch_ratelimit *d)
0430 {
0431     d->next = local_clock();
0432 }
0433 
0434 uint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done);
0435 
0436 #define __DIV_SAFE(n, d, zero)                      \
0437 ({                                  \
0438     typeof(n) _n = (n);                     \
0439     typeof(d) _d = (d);                     \
0440     _d ? _n / _d : zero;                        \
0441 })
0442 
0443 #define DIV_SAFE(n, d)  __DIV_SAFE(n, d, 0)
0444 
0445 #define container_of_or_null(ptr, type, member)             \
0446 ({                                  \
0447     typeof(ptr) _ptr = ptr;                     \
0448     _ptr ? container_of(_ptr, type, member) : NULL;         \
0449 })
0450 
0451 #define RB_INSERT(root, new, member, cmp)               \
0452 ({                                  \
0453     __label__ dup;                          \
0454     struct rb_node **n = &(root)->rb_node, *parent = NULL;      \
0455     typeof(new) this;                       \
0456     int res, ret = -1;                      \
0457                                     \
0458     while (*n) {                            \
0459         parent = *n;                        \
0460         this = container_of(*n, typeof(*(new)), member);    \
0461         res = cmp(new, this);                   \
0462         if (!res)                       \
0463             goto dup;                   \
0464         n = res < 0                     \
0465             ? &(*n)->rb_left                \
0466             : &(*n)->rb_right;              \
0467     }                               \
0468                                     \
0469     rb_link_node(&(new)->member, parent, n);            \
0470     rb_insert_color(&(new)->member, root);              \
0471     ret = 0;                            \
0472 dup:                                    \
0473     ret;                                \
0474 })
0475 
0476 #define RB_SEARCH(root, search, member, cmp)                \
0477 ({                                  \
0478     struct rb_node *n = (root)->rb_node;                \
0479     typeof(&(search)) this, ret = NULL;             \
0480     int res;                            \
0481                                     \
0482     while (n) {                         \
0483         this = container_of(n, typeof(search), member);     \
0484         res = cmp(&(search), this);             \
0485         if (!res) {                     \
0486             ret = this;                 \
0487             break;                      \
0488         }                           \
0489         n = res < 0                     \
0490             ? n->rb_left                    \
0491             : n->rb_right;                  \
0492     }                               \
0493     ret;                                \
0494 })
0495 
0496 #define RB_GREATER(root, search, member, cmp)               \
0497 ({                                  \
0498     struct rb_node *n = (root)->rb_node;                \
0499     typeof(&(search)) this, ret = NULL;             \
0500     int res;                            \
0501                                     \
0502     while (n) {                         \
0503         this = container_of(n, typeof(search), member);     \
0504         res = cmp(&(search), this);             \
0505         if (res < 0) {                      \
0506             ret = this;                 \
0507             n = n->rb_left;                 \
0508         } else                          \
0509             n = n->rb_right;                \
0510     }                               \
0511     ret;                                \
0512 })
0513 
0514 #define RB_FIRST(root, type, member)                    \
0515     container_of_or_null(rb_first(root), type, member)
0516 
0517 #define RB_LAST(root, type, member)                 \
0518     container_of_or_null(rb_last(root), type, member)
0519 
0520 #define RB_NEXT(ptr, member)                        \
0521     container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
0522 
0523 #define RB_PREV(ptr, member)                        \
0524     container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
0525 
0526 static inline uint64_t bch_crc64(const void *p, size_t len)
0527 {
0528     uint64_t crc = 0xffffffffffffffffULL;
0529 
0530     crc = crc64_be(crc, p, len);
0531     return crc ^ 0xffffffffffffffffULL;
0532 }
0533 
0534 /*
0535  * A stepwise-linear pseudo-exponential.  This returns 1 << (x >>
0536  * frac_bits), with the less-significant bits filled in by linear
0537  * interpolation.
0538  *
0539  * This can also be interpreted as a floating-point number format,
0540  * where the low frac_bits are the mantissa (with implicit leading
0541  * 1 bit), and the more significant bits are the exponent.
0542  * The return value is 1.mantissa * 2^exponent.
0543  *
0544  * The way this is used, fract_bits is 6 and the largest possible
0545  * input is CONGESTED_MAX-1 = 1023 (exponent 16, mantissa 0x1.fc),
0546  * so the maximum output is 0x1fc00.
0547  */
0548 static inline unsigned int fract_exp_two(unsigned int x,
0549                      unsigned int fract_bits)
0550 {
0551     unsigned int mantissa = 1 << fract_bits;    /* Implicit bit */
0552 
0553     mantissa += x & (mantissa - 1);
0554     x >>= fract_bits;   /* The exponent */
0555     /* Largest intermediate value 0x7f0000 */
0556     return mantissa << x >> fract_bits;
0557 }
0558 
0559 void bch_bio_map(struct bio *bio, void *base);
0560 int bch_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask);
0561 
0562 #endif /* _BCACHE_UTIL_H */