pblk-rl.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. void pblk_rl_update_rates(struct pblk_rl *rl)
  81. {
  82. struct pblk *pblk = container_of(rl, struct pblk, rl);
  83. unsigned long free_blocks = pblk_rl_nr_free_blks(rl);
  84. int max = rl->rb_budget;
  85. if (free_blocks >= rl->high) {
  86. rl->rb_user_max = max;
  87. rl->rb_gc_max = 0;
  88. rl->rb_state = PBLK_RL_HIGH;
  89. } else if (free_blocks < rl->high) {
  90. int shift = rl->high_pw - rl->rb_windows_pw;
  91. int user_windows = free_blocks >> shift;
  92. int user_max = user_windows << PBLK_MAX_REQ_ADDRS_PW;
  93. rl->rb_user_max = user_max;
  94. rl->rb_gc_max = max - user_max;
  95. if (free_blocks <= rl->rsv_blocks) {
  96. rl->rb_user_max = 0;
  97. rl->rb_gc_max = max;
  98. }
  99. /* In the worst case, we will need to GC lines in the low list
  100. * (high valid sector count). If there are lines to GC on high
  101. * or mid lists, these will be prioritized
  102. */
  103. rl->rb_state = PBLK_RL_LOW;
  104. }
  105. if (rl->rb_state == (PBLK_RL_MID | PBLK_RL_LOW))
  106. pblk_gc_should_start(pblk);
  107. else
  108. pblk_gc_should_stop(pblk);
  109. }
  110. void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line)
  111. {
  112. int blk_in_line = atomic_read(&line->blk_in_line);
  113. atomic_add(blk_in_line, &rl->free_blocks);
  114. pblk_rl_update_rates(rl);
  115. }
  116. void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line)
  117. {
  118. int blk_in_line = atomic_read(&line->blk_in_line);
  119. atomic_sub(blk_in_line, &rl->free_blocks);
  120. pblk_rl_update_rates(rl);
  121. }
  122. int pblk_rl_high_thrs(struct pblk_rl *rl)
  123. {
  124. return rl->high;
  125. }
  126. int pblk_rl_max_io(struct pblk_rl *rl)
  127. {
  128. return rl->rb_max_io;
  129. }
  130. static void pblk_rl_u_timer(unsigned long data)
  131. {
  132. struct pblk_rl *rl = (struct pblk_rl *)data;
  133. /* Release user I/O state. Protect from GC */
  134. smp_store_release(&rl->rb_user_active, 0);
  135. }
  136. void pblk_rl_free(struct pblk_rl *rl)
  137. {
  138. del_timer(&rl->u_timer);
  139. }
  140. void pblk_rl_init(struct pblk_rl *rl, int budget)
  141. {
  142. struct pblk *pblk = container_of(rl, struct pblk, rl);
  143. struct pblk_line_meta *lm = &pblk->lm;
  144. int min_blocks = lm->blk_per_line * PBLK_GC_RSV_LINE;
  145. unsigned int rb_windows;
  146. rl->high = rl->total_blocks / PBLK_USER_HIGH_THRS;
  147. rl->high_pw = get_count_order(rl->high);
  148. rl->low = rl->total_blocks / PBLK_USER_LOW_THRS;
  149. if (rl->low < min_blocks)
  150. rl->low = min_blocks;
  151. rl->rsv_blocks = min_blocks;
  152. /* This will always be a power-of-2 */
  153. rb_windows = budget / PBLK_MAX_REQ_ADDRS;
  154. rl->rb_windows_pw = get_count_order(rb_windows);
  155. /* To start with, all buffer is available to user I/O writers */
  156. rl->rb_budget = budget;
  157. rl->rb_user_max = budget;
  158. rl->rb_max_io = budget >> 1;
  159. rl->rb_gc_max = 0;
  160. rl->rb_state = PBLK_RL_HIGH;
  161. atomic_set(&rl->rb_user_cnt, 0);
  162. atomic_set(&rl->rb_gc_cnt, 0);
  163. atomic_set(&rl->rb_space, -1);
  164. setup_timer(&rl->u_timer, pblk_rl_u_timer, (unsigned long)rl);
  165. rl->rb_user_active = 0;
  166. rl->rb_gc_active = 0;
  167. }