pblk-rl.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (C) 2016 CNEX Labs
  3. * Initial release: Javier Gonzalez <javier@cnexlabs.com>
  4. * Matias Bjorling <matias@cnexlabs.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * pblk-rl.c - pblk's rate limiter for user I/O
  16. *
  17. */
  18. #include "pblk.h"
  19. static void pblk_rl_kick_u_timer(struct pblk_rl *rl)
  20. {
  21. mod_timer(&rl->u_timer, jiffies + msecs_to_jiffies(5000));
  22. }
  23. int pblk_rl_is_limit(struct pblk_rl *rl)
  24. {
  25. int rb_space;
  26. rb_space = atomic_read(&rl->rb_space);
  27. return (rb_space == 0);
  28. }
  29. int pblk_rl_user_may_insert(struct pblk_rl *rl, int nr_entries)
  30. {
  31. int rb_user_cnt = atomic_read(&rl->rb_user_cnt);
  32. int rb_space = atomic_read(&rl->rb_space);
  33. if (unlikely(rb_space >= 0) && (rb_space - nr_entries < 0))
  34. return NVM_IO_ERR;
  35. if (rb_user_cnt >= rl->rb_user_max)
  36. return NVM_IO_REQUEUE;
  37. return NVM_IO_OK;
  38. }
  39. void pblk_rl_inserted(struct pblk_rl *rl, int nr_entries)
  40. {
  41. int rb_space = atomic_read(&rl->rb_space);
  42. if (unlikely(rb_space >= 0))
  43. atomic_sub(nr_entries, &rl->rb_space);
  44. }
  45. int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries)
  46. {
  47. int rb_gc_cnt = atomic_read(&rl->rb_gc_cnt);
  48. int rb_user_active;
  49. /* If there is no user I/O let GC take over space on the write buffer */
  50. rb_user_active = READ_ONCE(rl->rb_user_active);
  51. return (!(rb_gc_cnt >= rl->rb_gc_max && rb_user_active));
  52. }
  53. void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries)
  54. {
  55. atomic_add(nr_entries, &rl->rb_user_cnt);
  56. /* Release user I/O state. Protect from GC */
  57. smp_store_release(&rl->rb_user_active, 1);
  58. pblk_rl_kick_u_timer(rl);
  59. }
  60. void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries)
  61. {
  62. atomic_add(nr_entries, &rl->rb_gc_cnt);
  63. }
  64. void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc)
  65. {
  66. atomic_sub(nr_user, &rl->rb_user_cnt);
  67. atomic_sub(nr_gc, &rl->rb_gc_cnt);
  68. }
  69. unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl)
  70. {
  71. return atomic_read(&rl->free_blocks);
  72. }
  73. /*
  74. * We check for (i) the number of free blocks in the current LUN and (ii) the
  75. * total number of free blocks in the pblk instance. This is to even out the
  76. * number of free blocks on each LUN when GC kicks in.
  77. *
  78. * Only the total number of free blocks is used to configure the rate limiter.
  79. */
  80. static int pblk_rl_update_rates(struct pblk_rl *rl, unsigned long max)
  81. {
  82. unsigned long free_blocks = pblk_rl_nr_free_blks(rl);
  83. if (free_blocks >= rl->high) {
  84. rl->rb_user_max = max;
  85. rl->rb_gc_max = 0;
  86. rl->rb_state = PBLK_RL_HIGH;
  87. } else if (free_blocks < rl->high) {
  88. int shift = rl->high_pw - rl->rb_windows_pw;
  89. int user_windows = free_blocks >> shift;
  90. int user_max = user_windows << PBLK_MAX_REQ_ADDRS_PW;
  91. rl->rb_user_max = user_max;
  92. rl->rb_gc_max = max - user_max;
  93. if (free_blocks <= rl->rsv_blocks) {
  94. rl->rb_user_max = 0;
  95. rl->rb_gc_max = max;
  96. }
  97. /* In the worst case, we will need to GC lines in the low list
  98. * (high valid sector count). If there are lines to GC on high
  99. * or mid lists, these will be prioritized
  100. */
  101. rl->rb_state = PBLK_RL_LOW;
  102. }
  103. return rl->rb_state;
  104. }
  105. void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line)
  106. {
  107. struct pblk *pblk = container_of(rl, struct pblk, rl);
  108. int blk_in_line = atomic_read(&line->blk_in_line);
  109. int ret;
  110. atomic_add(blk_in_line, &rl->free_blocks);
  111. /* Rates will not change that often - no need to lock update */
  112. ret = pblk_rl_update_rates(rl, rl->rb_budget);
  113. if (ret == (PBLK_RL_MID | PBLK_RL_LOW))
  114. pblk_gc_should_start(pblk);
  115. else
  116. pblk_gc_should_stop(pblk);
  117. }
  118. void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line)
  119. {
  120. int blk_in_line = atomic_read(&line->blk_in_line);
  121. atomic_sub(blk_in_line, &rl->free_blocks);
  122. }
  123. void pblk_gc_should_kick(struct pblk *pblk)
  124. {
  125. struct pblk_rl *rl = &pblk->rl;
  126. int ret;
  127. /* Rates will not change that often - no need to lock update */
  128. ret = pblk_rl_update_rates(rl, rl->rb_budget);
  129. if (ret == (PBLK_RL_MID | PBLK_RL_LOW))
  130. pblk_gc_should_start(pblk);
  131. else
  132. pblk_gc_should_stop(pblk);
  133. }
  134. int pblk_rl_high_thrs(struct pblk_rl *rl)
  135. {
  136. return rl->high;
  137. }
  138. int pblk_rl_low_thrs(struct pblk_rl *rl)
  139. {
  140. return rl->low;
  141. }
  142. int pblk_rl_sysfs_rate_show(struct pblk_rl *rl)
  143. {
  144. return rl->rb_user_max;
  145. }
  146. static void pblk_rl_u_timer(unsigned long data)
  147. {
  148. struct pblk_rl *rl = (struct pblk_rl *)data;
  149. /* Release user I/O state. Protect from GC */
  150. smp_store_release(&rl->rb_user_active, 0);
  151. }
  152. void pblk_rl_free(struct pblk_rl *rl)
  153. {
  154. del_timer(&rl->u_timer);
  155. }
  156. void pblk_rl_init(struct pblk_rl *rl, int budget)
  157. {
  158. struct pblk *pblk = container_of(rl, struct pblk, rl);
  159. struct pblk_line_meta *lm = &pblk->lm;
  160. int min_blocks = lm->blk_per_line * PBLK_GC_RSV_LINE;
  161. unsigned int rb_windows;
  162. rl->high = rl->total_blocks / PBLK_USER_HIGH_THRS;
  163. rl->high_pw = get_count_order(rl->high);
  164. rl->low = rl->total_blocks / PBLK_USER_LOW_THRS;
  165. if (rl->low < min_blocks)
  166. rl->low = min_blocks;
  167. rl->rsv_blocks = min_blocks;
  168. /* This will always be a power-of-2 */
  169. rb_windows = budget / PBLK_MAX_REQ_ADDRS;
  170. rl->rb_windows_pw = get_count_order(rb_windows);
  171. /* To start with, all buffer is available to user I/O writers */
  172. rl->rb_budget = budget;
  173. rl->rb_user_max = budget;
  174. rl->rb_gc_max = 0;
  175. rl->rb_state = PBLK_RL_HIGH;
  176. atomic_set(&rl->rb_user_cnt, 0);
  177. atomic_set(&rl->rb_gc_cnt, 0);
  178. atomic_set(&rl->rb_space, -1);
  179. setup_timer(&rl->u_timer, pblk_rl_u_timer, (unsigned long)rl);
  180. rl->rb_user_active = 0;
  181. rl->rb_gc_active = 0;
  182. }