util.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 "closure.h"
  13. #define PAGE_SECTORS (PAGE_SIZE / 512)
  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); } 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 heap_empty(h) ((h)->used == 0)
  95. #define DECLARE_FIFO(type, name) \
  96. struct { \
  97. size_t front, back, size, mask; \
  98. type *data; \
  99. } name
  100. #define fifo_for_each(c, fifo, iter) \
  101. for (iter = (fifo)->front; \
  102. c = (fifo)->data[iter], iter != (fifo)->back; \
  103. iter = (iter + 1) & (fifo)->mask)
  104. #define __init_fifo(fifo, gfp) \
  105. ({ \
  106. size_t _allocated_size, _bytes; \
  107. BUG_ON(!(fifo)->size); \
  108. \
  109. _allocated_size = roundup_pow_of_two((fifo)->size + 1); \
  110. _bytes = _allocated_size * sizeof(*(fifo)->data); \
  111. \
  112. (fifo)->mask = _allocated_size - 1; \
  113. (fifo)->front = (fifo)->back = 0; \
  114. \
  115. (fifo)->data = kvmalloc(_bytes, (gfp) & GFP_KERNEL); \
  116. (fifo)->data; \
  117. })
  118. #define init_fifo_exact(fifo, _size, gfp) \
  119. ({ \
  120. (fifo)->size = (_size); \
  121. __init_fifo(fifo, gfp); \
  122. })
  123. #define init_fifo(fifo, _size, gfp) \
  124. ({ \
  125. (fifo)->size = (_size); \
  126. if ((fifo)->size > 4) \
  127. (fifo)->size = roundup_pow_of_two((fifo)->size) - 1; \
  128. __init_fifo(fifo, gfp); \
  129. })
  130. #define free_fifo(fifo) \
  131. do { \
  132. kvfree((fifo)->data); \
  133. (fifo)->data = NULL; \
  134. } while (0)
  135. #define fifo_used(fifo) (((fifo)->back - (fifo)->front) & (fifo)->mask)
  136. #define fifo_free(fifo) ((fifo)->size - fifo_used(fifo))
  137. #define fifo_empty(fifo) (!fifo_used(fifo))
  138. #define fifo_full(fifo) (!fifo_free(fifo))
  139. #define fifo_front(fifo) ((fifo)->data[(fifo)->front])
  140. #define fifo_back(fifo) \
  141. ((fifo)->data[((fifo)->back - 1) & (fifo)->mask])
  142. #define fifo_idx(fifo, p) (((p) - &fifo_front(fifo)) & (fifo)->mask)
  143. #define fifo_push_back(fifo, i) \
  144. ({ \
  145. bool _r = !fifo_full((fifo)); \
  146. if (_r) { \
  147. (fifo)->data[(fifo)->back++] = (i); \
  148. (fifo)->back &= (fifo)->mask; \
  149. } \
  150. _r; \
  151. })
  152. #define fifo_pop_front(fifo, i) \
  153. ({ \
  154. bool _r = !fifo_empty((fifo)); \
  155. if (_r) { \
  156. (i) = (fifo)->data[(fifo)->front++]; \
  157. (fifo)->front &= (fifo)->mask; \
  158. } \
  159. _r; \
  160. })
  161. #define fifo_push_front(fifo, i) \
  162. ({ \
  163. bool _r = !fifo_full((fifo)); \
  164. if (_r) { \
  165. --(fifo)->front; \
  166. (fifo)->front &= (fifo)->mask; \
  167. (fifo)->data[(fifo)->front] = (i); \
  168. } \
  169. _r; \
  170. })
  171. #define fifo_pop_back(fifo, i) \
  172. ({ \
  173. bool _r = !fifo_empty((fifo)); \
  174. if (_r) { \
  175. --(fifo)->back; \
  176. (fifo)->back &= (fifo)->mask; \
  177. (i) = (fifo)->data[(fifo)->back] \
  178. } \
  179. _r; \
  180. })
  181. #define fifo_push(fifo, i) fifo_push_back(fifo, (i))
  182. #define fifo_pop(fifo, i) fifo_pop_front(fifo, (i))
  183. #define fifo_swap(l, r) \
  184. do { \
  185. swap((l)->front, (r)->front); \
  186. swap((l)->back, (r)->back); \
  187. swap((l)->size, (r)->size); \
  188. swap((l)->mask, (r)->mask); \
  189. swap((l)->data, (r)->data); \
  190. } while (0)
  191. #define fifo_move(dest, src) \
  192. do { \
  193. typeof(*((dest)->data)) _t; \
  194. while (!fifo_full(dest) && \
  195. fifo_pop(src, _t)) \
  196. fifo_push(dest, _t); \
  197. } while (0)
  198. /*
  199. * Simple array based allocator - preallocates a number of elements and you can
  200. * never allocate more than that, also has no locking.
  201. *
  202. * Handy because if you know you only need a fixed number of elements you don't
  203. * have to worry about memory allocation failure, and sometimes a mempool isn't
  204. * what you want.
  205. *
  206. * We treat the free elements as entries in a singly linked list, and the
  207. * freelist as a stack - allocating and freeing push and pop off the freelist.
  208. */
  209. #define DECLARE_ARRAY_ALLOCATOR(type, name, size) \
  210. struct { \
  211. type *freelist; \
  212. type data[size]; \
  213. } name
  214. #define array_alloc(array) \
  215. ({ \
  216. typeof((array)->freelist) _ret = (array)->freelist; \
  217. \
  218. if (_ret) \
  219. (array)->freelist = *((typeof((array)->freelist) *) _ret);\
  220. \
  221. _ret; \
  222. })
  223. #define array_free(array, ptr) \
  224. do { \
  225. typeof((array)->freelist) _ptr = ptr; \
  226. \
  227. *((typeof((array)->freelist) *) _ptr) = (array)->freelist; \
  228. (array)->freelist = _ptr; \
  229. } while (0)
  230. #define array_allocator_init(array) \
  231. do { \
  232. typeof((array)->freelist) _i; \
  233. \
  234. BUILD_BUG_ON(sizeof((array)->data[0]) < sizeof(void *)); \
  235. (array)->freelist = NULL; \
  236. \
  237. for (_i = (array)->data; \
  238. _i < (array)->data + ARRAY_SIZE((array)->data); \
  239. _i++) \
  240. array_free(array, _i); \
  241. } while (0)
  242. #define array_freelist_empty(array) ((array)->freelist == NULL)
  243. #define ANYSINT_MAX(t) \
  244. ((((t) 1 << (sizeof(t) * 8 - 2)) - (t) 1) * (t) 2 + (t) 1)
  245. int bch_strtoint_h(const char *, int *);
  246. int bch_strtouint_h(const char *, unsigned int *);
  247. int bch_strtoll_h(const char *, long long *);
  248. int bch_strtoull_h(const char *, unsigned long long *);
  249. static inline int bch_strtol_h(const char *cp, long *res)
  250. {
  251. #if BITS_PER_LONG == 32
  252. return bch_strtoint_h(cp, (int *) res);
  253. #else
  254. return bch_strtoll_h(cp, (long long *) res);
  255. #endif
  256. }
  257. static inline int bch_strtoul_h(const char *cp, long *res)
  258. {
  259. #if BITS_PER_LONG == 32
  260. return bch_strtouint_h(cp, (unsigned int *) res);
  261. #else
  262. return bch_strtoull_h(cp, (unsigned long long *) res);
  263. #endif
  264. }
  265. #define strtoi_h(cp, res) \
  266. (__builtin_types_compatible_p(typeof(*res), int) \
  267. ? bch_strtoint_h(cp, (void *) res) \
  268. : __builtin_types_compatible_p(typeof(*res), long) \
  269. ? bch_strtol_h(cp, (void *) res) \
  270. : __builtin_types_compatible_p(typeof(*res), long long) \
  271. ? bch_strtoll_h(cp, (void *) res) \
  272. : __builtin_types_compatible_p(typeof(*res), unsigned int) \
  273. ? bch_strtouint_h(cp, (void *) res) \
  274. : __builtin_types_compatible_p(typeof(*res), unsigned long) \
  275. ? bch_strtoul_h(cp, (void *) res) \
  276. : __builtin_types_compatible_p(typeof(*res), unsigned long long)\
  277. ? bch_strtoull_h(cp, (void *) res) : -EINVAL)
  278. #define strtoul_safe(cp, var) \
  279. ({ \
  280. unsigned long _v; \
  281. int _r = kstrtoul(cp, 10, &_v); \
  282. if (!_r) \
  283. var = _v; \
  284. _r; \
  285. })
  286. #define strtoul_safe_clamp(cp, var, min, max) \
  287. ({ \
  288. unsigned long _v; \
  289. int _r = kstrtoul(cp, 10, &_v); \
  290. if (!_r) \
  291. var = clamp_t(typeof(var), _v, min, max); \
  292. _r; \
  293. })
  294. #define snprint(buf, size, var) \
  295. snprintf(buf, size, \
  296. __builtin_types_compatible_p(typeof(var), int) \
  297. ? "%i\n" : \
  298. __builtin_types_compatible_p(typeof(var), unsigned) \
  299. ? "%u\n" : \
  300. __builtin_types_compatible_p(typeof(var), long) \
  301. ? "%li\n" : \
  302. __builtin_types_compatible_p(typeof(var), unsigned long)\
  303. ? "%lu\n" : \
  304. __builtin_types_compatible_p(typeof(var), int64_t) \
  305. ? "%lli\n" : \
  306. __builtin_types_compatible_p(typeof(var), uint64_t) \
  307. ? "%llu\n" : \
  308. __builtin_types_compatible_p(typeof(var), const char *) \
  309. ? "%s\n" : "%i\n", var)
  310. ssize_t bch_hprint(char *buf, int64_t v);
  311. bool bch_is_zero(const char *p, size_t n);
  312. int bch_parse_uuid(const char *s, char *uuid);
  313. ssize_t bch_snprint_string_list(char *buf, size_t size, const char * const list[],
  314. size_t selected);
  315. ssize_t bch_read_string_list(const char *buf, const char * const list[]);
  316. struct time_stats {
  317. spinlock_t lock;
  318. /*
  319. * all fields are in nanoseconds, averages are ewmas stored left shifted
  320. * by 8
  321. */
  322. uint64_t max_duration;
  323. uint64_t average_duration;
  324. uint64_t average_frequency;
  325. uint64_t last;
  326. };
  327. void bch_time_stats_update(struct time_stats *stats, uint64_t time);
  328. static inline unsigned local_clock_us(void)
  329. {
  330. return local_clock() >> 10;
  331. }
  332. #define NSEC_PER_ns 1L
  333. #define NSEC_PER_us NSEC_PER_USEC
  334. #define NSEC_PER_ms NSEC_PER_MSEC
  335. #define NSEC_PER_sec NSEC_PER_SEC
  336. #define __print_time_stat(stats, name, stat, units) \
  337. sysfs_print(name ## _ ## stat ## _ ## units, \
  338. div_u64((stats)->stat >> 8, NSEC_PER_ ## units))
  339. #define sysfs_print_time_stats(stats, name, \
  340. frequency_units, \
  341. duration_units) \
  342. do { \
  343. __print_time_stat(stats, name, \
  344. average_frequency, frequency_units); \
  345. __print_time_stat(stats, name, \
  346. average_duration, duration_units); \
  347. sysfs_print(name ## _ ##max_duration ## _ ## duration_units, \
  348. div_u64((stats)->max_duration, NSEC_PER_ ## duration_units));\
  349. \
  350. sysfs_print(name ## _last_ ## frequency_units, (stats)->last \
  351. ? div_s64(local_clock() - (stats)->last, \
  352. NSEC_PER_ ## frequency_units) \
  353. : -1LL); \
  354. } while (0)
  355. #define sysfs_time_stats_attribute(name, \
  356. frequency_units, \
  357. duration_units) \
  358. read_attribute(name ## _average_frequency_ ## frequency_units); \
  359. read_attribute(name ## _average_duration_ ## duration_units); \
  360. read_attribute(name ## _max_duration_ ## duration_units); \
  361. read_attribute(name ## _last_ ## frequency_units)
  362. #define sysfs_time_stats_attribute_list(name, \
  363. frequency_units, \
  364. duration_units) \
  365. &sysfs_ ## name ## _average_frequency_ ## frequency_units, \
  366. &sysfs_ ## name ## _average_duration_ ## duration_units, \
  367. &sysfs_ ## name ## _max_duration_ ## duration_units, \
  368. &sysfs_ ## name ## _last_ ## frequency_units,
  369. #define ewma_add(ewma, val, weight, factor) \
  370. ({ \
  371. (ewma) *= (weight) - 1; \
  372. (ewma) += (val) << factor; \
  373. (ewma) /= (weight); \
  374. (ewma) >> factor; \
  375. })
  376. struct bch_ratelimit {
  377. /* Next time we want to do some work, in nanoseconds */
  378. uint64_t next;
  379. /*
  380. * Rate at which we want to do work, in units per second
  381. * The units here correspond to the units passed to bch_next_delay()
  382. */
  383. uint32_t rate;
  384. };
  385. static inline void bch_ratelimit_reset(struct bch_ratelimit *d)
  386. {
  387. d->next = local_clock();
  388. }
  389. uint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done);
  390. #define __DIV_SAFE(n, d, zero) \
  391. ({ \
  392. typeof(n) _n = (n); \
  393. typeof(d) _d = (d); \
  394. _d ? _n / _d : zero; \
  395. })
  396. #define DIV_SAFE(n, d) __DIV_SAFE(n, d, 0)
  397. #define container_of_or_null(ptr, type, member) \
  398. ({ \
  399. typeof(ptr) _ptr = ptr; \
  400. _ptr ? container_of(_ptr, type, member) : NULL; \
  401. })
  402. #define RB_INSERT(root, new, member, cmp) \
  403. ({ \
  404. __label__ dup; \
  405. struct rb_node **n = &(root)->rb_node, *parent = NULL; \
  406. typeof(new) this; \
  407. int res, ret = -1; \
  408. \
  409. while (*n) { \
  410. parent = *n; \
  411. this = container_of(*n, typeof(*(new)), member); \
  412. res = cmp(new, this); \
  413. if (!res) \
  414. goto dup; \
  415. n = res < 0 \
  416. ? &(*n)->rb_left \
  417. : &(*n)->rb_right; \
  418. } \
  419. \
  420. rb_link_node(&(new)->member, parent, n); \
  421. rb_insert_color(&(new)->member, root); \
  422. ret = 0; \
  423. dup: \
  424. ret; \
  425. })
  426. #define RB_SEARCH(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) { \
  436. ret = this; \
  437. break; \
  438. } \
  439. n = res < 0 \
  440. ? n->rb_left \
  441. : n->rb_right; \
  442. } \
  443. ret; \
  444. })
  445. #define RB_GREATER(root, search, member, cmp) \
  446. ({ \
  447. struct rb_node *n = (root)->rb_node; \
  448. typeof(&(search)) this, ret = NULL; \
  449. int res; \
  450. \
  451. while (n) { \
  452. this = container_of(n, typeof(search), member); \
  453. res = cmp(&(search), this); \
  454. if (res < 0) { \
  455. ret = this; \
  456. n = n->rb_left; \
  457. } else \
  458. n = n->rb_right; \
  459. } \
  460. ret; \
  461. })
  462. #define RB_FIRST(root, type, member) \
  463. container_of_or_null(rb_first(root), type, member)
  464. #define RB_LAST(root, type, member) \
  465. container_of_or_null(rb_last(root), type, member)
  466. #define RB_NEXT(ptr, member) \
  467. container_of_or_null(rb_next(&(ptr)->member), typeof(*ptr), member)
  468. #define RB_PREV(ptr, member) \
  469. container_of_or_null(rb_prev(&(ptr)->member), typeof(*ptr), member)
  470. /* Does linear interpolation between powers of two */
  471. static inline unsigned fract_exp_two(unsigned x, unsigned fract_bits)
  472. {
  473. unsigned fract = x & ~(~0 << fract_bits);
  474. x >>= fract_bits;
  475. x = 1 << x;
  476. x += (x * fract) >> fract_bits;
  477. return x;
  478. }
  479. void bch_bio_map(struct bio *bio, void *base);
  480. int bch_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask);
  481. static inline sector_t bdev_sectors(struct block_device *bdev)
  482. {
  483. return bdev->bd_inode->i_size >> 9;
  484. }
  485. uint64_t bch_crc64_update(uint64_t, const void *, size_t);
  486. uint64_t bch_crc64(const void *, size_t);
  487. #endif /* _BCACHE_UTIL_H */