util.h 15 KB

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