util.h 15 KB

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