pblk-rl.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_user_may_insert(struct pblk_rl *rl, int nr_entries)
  24. {
  25. int rb_user_cnt = atomic_read(&rl->rb_user_cnt);
  26. return (!(rb_user_cnt + nr_entries > rl->rb_user_max));
  27. }
  28. int pblk_rl_gc_may_insert(struct pblk_rl *rl, int nr_entries)
  29. {
  30. int rb_gc_cnt = atomic_read(&rl->rb_gc_cnt);
  31. int rb_user_active;
  32. /* If there is no user I/O let GC take over space on the write buffer */
  33. rb_user_active = READ_ONCE(rl->rb_user_active);
  34. return (!(rb_gc_cnt + nr_entries > rl->rb_gc_max && rb_user_active));
  35. }
  36. void pblk_rl_user_in(struct pblk_rl *rl, int nr_entries)
  37. {
  38. atomic_add(nr_entries, &rl->rb_user_cnt);
  39. /* Release user I/O state. Protect from GC */
  40. smp_store_release(&rl->rb_user_active, 1);
  41. pblk_rl_kick_u_timer(rl);
  42. }
  43. void pblk_rl_gc_in(struct pblk_rl *rl, int nr_entries)
  44. {
  45. atomic_add(nr_entries, &rl->rb_gc_cnt);
  46. }
  47. void pblk_rl_out(struct pblk_rl *rl, int nr_user, int nr_gc)
  48. {
  49. atomic_sub(nr_user, &rl->rb_user_cnt);
  50. atomic_sub(nr_gc, &rl->rb_gc_cnt);
  51. }
  52. unsigned long pblk_rl_nr_free_blks(struct pblk_rl *rl)
  53. {
  54. return atomic_read(&rl->free_blocks);
  55. }
  56. /*
  57. * We check for (i) the number of free blocks in the current LUN and (ii) the
  58. * total number of free blocks in the pblk instance. This is to even out the
  59. * number of free blocks on each LUN when GC kicks in.
  60. *
  61. * Only the total number of free blocks is used to configure the rate limiter.
  62. */
  63. static int pblk_rl_update_rates(struct pblk_rl *rl, unsigned long max)
  64. {
  65. unsigned long free_blocks = pblk_rl_nr_free_blks(rl);
  66. if (free_blocks >= rl->high) {
  67. rl->rb_user_max = max - rl->rb_gc_rsv;
  68. rl->rb_gc_max = rl->rb_gc_rsv;
  69. rl->rb_state = PBLK_RL_HIGH;
  70. } else if (free_blocks < rl->high) {
  71. int shift = rl->high_pw - rl->rb_windows_pw;
  72. int user_windows = free_blocks >> shift;
  73. int user_max = user_windows << PBLK_MAX_REQ_ADDRS_PW;
  74. int gc_max;
  75. rl->rb_user_max = user_max;
  76. gc_max = max - rl->rb_user_max;
  77. rl->rb_gc_max = max(gc_max, rl->rb_gc_rsv);
  78. if (free_blocks > rl->low)
  79. rl->rb_state = PBLK_RL_MID;
  80. else
  81. rl->rb_state = PBLK_RL_LOW;
  82. }
  83. return rl->rb_state;
  84. }
  85. void pblk_rl_set_gc_rsc(struct pblk_rl *rl, int rsv)
  86. {
  87. rl->rb_gc_rsv = rl->rb_gc_max = rsv;
  88. }
  89. void pblk_rl_free_lines_inc(struct pblk_rl *rl, struct pblk_line *line)
  90. {
  91. struct pblk *pblk = container_of(rl, struct pblk, rl);
  92. int blk_in_line = atomic_read(&line->blk_in_line);
  93. int ret;
  94. atomic_add(blk_in_line, &rl->free_blocks);
  95. /* Rates will not change that often - no need to lock update */
  96. ret = pblk_rl_update_rates(rl, rl->rb_budget);
  97. if (ret == (PBLK_RL_MID | PBLK_RL_LOW))
  98. pblk_gc_should_start(pblk);
  99. else
  100. pblk_gc_should_stop(pblk);
  101. }
  102. void pblk_rl_free_lines_dec(struct pblk_rl *rl, struct pblk_line *line)
  103. {
  104. struct pblk *pblk = container_of(rl, struct pblk, rl);
  105. int blk_in_line = atomic_read(&line->blk_in_line);
  106. int ret;
  107. atomic_sub(blk_in_line, &rl->free_blocks);
  108. /* Rates will not change that often - no need to lock update */
  109. ret = pblk_rl_update_rates(rl, rl->rb_budget);
  110. if (ret == (PBLK_RL_MID | PBLK_RL_LOW))
  111. pblk_gc_should_start(pblk);
  112. else
  113. pblk_gc_should_stop(pblk);
  114. }
  115. int pblk_rl_gc_thrs(struct pblk_rl *rl)
  116. {
  117. return rl->high;
  118. }
  119. int pblk_rl_sysfs_rate_show(struct pblk_rl *rl)
  120. {
  121. return rl->rb_user_max;
  122. }
  123. static void pblk_rl_u_timer(unsigned long data)
  124. {
  125. struct pblk_rl *rl = (struct pblk_rl *)data;
  126. /* Release user I/O state. Protect from GC */
  127. smp_store_release(&rl->rb_user_active, 0);
  128. }
  129. void pblk_rl_free(struct pblk_rl *rl)
  130. {
  131. del_timer(&rl->u_timer);
  132. }
  133. void pblk_rl_init(struct pblk_rl *rl, int budget)
  134. {
  135. unsigned int rb_windows;
  136. rl->high = rl->total_blocks / PBLK_USER_HIGH_THRS;
  137. rl->low = rl->total_blocks / PBLK_USER_LOW_THRS;
  138. rl->high_pw = get_count_order(rl->high);
  139. /* This will always be a power-of-2 */
  140. rb_windows = budget / PBLK_MAX_REQ_ADDRS;
  141. rl->rb_windows_pw = get_count_order(rb_windows) + 1;
  142. /* To start with, all buffer is available to user I/O writers */
  143. rl->rb_budget = budget;
  144. rl->rb_user_max = budget;
  145. atomic_set(&rl->rb_user_cnt, 0);
  146. rl->rb_gc_max = 0;
  147. rl->rb_state = PBLK_RL_HIGH;
  148. atomic_set(&rl->rb_gc_cnt, 0);
  149. setup_timer(&rl->u_timer, pblk_rl_u_timer, (unsigned long)rl);
  150. rl->rb_user_active = 0;
  151. }