util.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * random utiility code, for bcache but in theory not specific to bcache
  4. *
  5. * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
  6. * Copyright 2012 Google, Inc.
  7. */
  8. #include <linux/bio.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/ctype.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/module.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/types.h>
  15. #include <linux/sched/clock.h>
  16. #include "util.h"
  17. #define simple_strtoint(c, end, base) simple_strtol(c, end, base)
  18. #define simple_strtouint(c, end, base) simple_strtoul(c, end, base)
  19. #define STRTO_H(name, type) \
  20. int bch_ ## name ## _h(const char *cp, type *res) \
  21. { \
  22. int u = 0; \
  23. char *e; \
  24. type i = simple_ ## name(cp, &e, 10); \
  25. \
  26. switch (tolower(*e)) { \
  27. default: \
  28. return -EINVAL; \
  29. case 'y': \
  30. case 'z': \
  31. u++; \
  32. /* fall through */ \
  33. case 'e': \
  34. u++; \
  35. /* fall through */ \
  36. case 'p': \
  37. u++; \
  38. /* fall through */ \
  39. case 't': \
  40. u++; \
  41. /* fall through */ \
  42. case 'g': \
  43. u++; \
  44. /* fall through */ \
  45. case 'm': \
  46. u++; \
  47. /* fall through */ \
  48. case 'k': \
  49. u++; \
  50. if (e++ == cp) \
  51. return -EINVAL; \
  52. /* fall through */ \
  53. case '\n': \
  54. case '\0': \
  55. if (*e == '\n') \
  56. e++; \
  57. } \
  58. \
  59. if (*e) \
  60. return -EINVAL; \
  61. \
  62. while (u--) { \
  63. if ((type) ~0 > 0 && \
  64. (type) ~0 / 1024 <= i) \
  65. return -EINVAL; \
  66. if ((i > 0 && ANYSINT_MAX(type) / 1024 < i) || \
  67. (i < 0 && -ANYSINT_MAX(type) / 1024 > i)) \
  68. return -EINVAL; \
  69. i *= 1024; \
  70. } \
  71. \
  72. *res = i; \
  73. return 0; \
  74. } \
  75. STRTO_H(strtoint, int)
  76. STRTO_H(strtouint, unsigned int)
  77. STRTO_H(strtoll, long long)
  78. STRTO_H(strtoull, unsigned long long)
  79. /**
  80. * bch_hprint - formats @v to human readable string for sysfs.
  81. * @buf: the (at least 8 byte) buffer to format the result into.
  82. * @v: signed 64 bit integer
  83. *
  84. * Returns the number of bytes used by format.
  85. */
  86. ssize_t bch_hprint(char *buf, int64_t v)
  87. {
  88. static const char units[] = "?kMGTPEZY";
  89. int u = 0, t;
  90. uint64_t q;
  91. if (v < 0)
  92. q = -v;
  93. else
  94. q = v;
  95. /* For as long as the number is more than 3 digits, but at least
  96. * once, shift right / divide by 1024. Keep the remainder for
  97. * a digit after the decimal point.
  98. */
  99. do {
  100. u++;
  101. t = q & ~(~0 << 10);
  102. q >>= 10;
  103. } while (q >= 1000);
  104. if (v < 0)
  105. /* '-', up to 3 digits, '.', 1 digit, 1 character, null;
  106. * yields 8 bytes.
  107. */
  108. return sprintf(buf, "-%llu.%i%c", q, t * 10 / 1024, units[u]);
  109. else
  110. return sprintf(buf, "%llu.%i%c", q, t * 10 / 1024, units[u]);
  111. }
  112. bool bch_is_zero(const char *p, size_t n)
  113. {
  114. size_t i;
  115. for (i = 0; i < n; i++)
  116. if (p[i])
  117. return false;
  118. return true;
  119. }
  120. int bch_parse_uuid(const char *s, char *uuid)
  121. {
  122. size_t i, j, x;
  123. memset(uuid, 0, 16);
  124. for (i = 0, j = 0;
  125. i < strspn(s, "-0123456789:ABCDEFabcdef") && j < 32;
  126. i++) {
  127. x = s[i] | 32;
  128. switch (x) {
  129. case '0'...'9':
  130. x -= '0';
  131. break;
  132. case 'a'...'f':
  133. x -= 'a' - 10;
  134. break;
  135. default:
  136. continue;
  137. }
  138. if (!(j & 1))
  139. x <<= 4;
  140. uuid[j++ >> 1] |= x;
  141. }
  142. return i;
  143. }
  144. void bch_time_stats_update(struct time_stats *stats, uint64_t start_time)
  145. {
  146. uint64_t now, duration, last;
  147. spin_lock(&stats->lock);
  148. now = local_clock();
  149. duration = time_after64(now, start_time)
  150. ? now - start_time : 0;
  151. last = time_after64(now, stats->last)
  152. ? now - stats->last : 0;
  153. stats->max_duration = max(stats->max_duration, duration);
  154. if (stats->last) {
  155. ewma_add(stats->average_duration, duration, 8, 8);
  156. if (stats->average_frequency)
  157. ewma_add(stats->average_frequency, last, 8, 8);
  158. else
  159. stats->average_frequency = last << 8;
  160. } else {
  161. stats->average_duration = duration << 8;
  162. }
  163. stats->last = now ?: 1;
  164. spin_unlock(&stats->lock);
  165. }
  166. /**
  167. * bch_next_delay() - update ratelimiting statistics and calculate next delay
  168. * @d: the struct bch_ratelimit to update
  169. * @done: the amount of work done, in arbitrary units
  170. *
  171. * Increment @d by the amount of work done, and return how long to delay in
  172. * jiffies until the next time to do some work.
  173. */
  174. uint64_t bch_next_delay(struct bch_ratelimit *d, uint64_t done)
  175. {
  176. uint64_t now = local_clock();
  177. d->next += div_u64(done * NSEC_PER_SEC, atomic_long_read(&d->rate));
  178. /* Bound the time. Don't let us fall further than 2 seconds behind
  179. * (this prevents unnecessary backlog that would make it impossible
  180. * to catch up). If we're ahead of the desired writeback rate,
  181. * don't let us sleep more than 2.5 seconds (so we can notice/respond
  182. * if the control system tells us to speed up!).
  183. */
  184. if (time_before64(now + NSEC_PER_SEC * 5LLU / 2LLU, d->next))
  185. d->next = now + NSEC_PER_SEC * 5LLU / 2LLU;
  186. if (time_after64(now - NSEC_PER_SEC * 2, d->next))
  187. d->next = now - NSEC_PER_SEC * 2;
  188. return time_after64(d->next, now)
  189. ? div_u64(d->next - now, NSEC_PER_SEC / HZ)
  190. : 0;
  191. }
  192. /*
  193. * Generally it isn't good to access .bi_io_vec and .bi_vcnt directly,
  194. * the preferred way is bio_add_page, but in this case, bch_bio_map()
  195. * supposes that the bvec table is empty, so it is safe to access
  196. * .bi_vcnt & .bi_io_vec in this way even after multipage bvec is
  197. * supported.
  198. */
  199. void bch_bio_map(struct bio *bio, void *base)
  200. {
  201. size_t size = bio->bi_iter.bi_size;
  202. struct bio_vec *bv = bio->bi_io_vec;
  203. BUG_ON(!bio->bi_iter.bi_size);
  204. BUG_ON(bio->bi_vcnt);
  205. bv->bv_offset = base ? offset_in_page(base) : 0;
  206. goto start;
  207. for (; size; bio->bi_vcnt++, bv++) {
  208. bv->bv_offset = 0;
  209. start: bv->bv_len = min_t(size_t, PAGE_SIZE - bv->bv_offset,
  210. size);
  211. if (base) {
  212. bv->bv_page = is_vmalloc_addr(base)
  213. ? vmalloc_to_page(base)
  214. : virt_to_page(base);
  215. base += bv->bv_len;
  216. }
  217. size -= bv->bv_len;
  218. }
  219. }
  220. /**
  221. * bch_bio_alloc_pages - allocates a single page for each bvec in a bio
  222. * @bio: bio to allocate pages for
  223. * @gfp_mask: flags for allocation
  224. *
  225. * Allocates pages up to @bio->bi_vcnt.
  226. *
  227. * Returns 0 on success, -ENOMEM on failure. On failure, any allocated pages are
  228. * freed.
  229. */
  230. int bch_bio_alloc_pages(struct bio *bio, gfp_t gfp_mask)
  231. {
  232. int i;
  233. struct bio_vec *bv;
  234. /*
  235. * This is called on freshly new bio, so it is safe to access the
  236. * bvec table directly.
  237. */
  238. for (i = 0, bv = bio->bi_io_vec; i < bio->bi_vcnt; bv++, i++) {
  239. bv->bv_page = alloc_page(gfp_mask);
  240. if (!bv->bv_page) {
  241. while (--bv >= bio->bi_io_vec)
  242. __free_page(bv->bv_page);
  243. return -ENOMEM;
  244. }
  245. }
  246. return 0;
  247. }