blk-rq-qos.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "blk-rq-qos.h"
  3. /*
  4. * Increment 'v', if 'v' is below 'below'. Returns true if we succeeded,
  5. * false if 'v' + 1 would be bigger than 'below'.
  6. */
  7. static bool atomic_inc_below(atomic_t *v, unsigned int below)
  8. {
  9. unsigned int cur = atomic_read(v);
  10. do {
  11. if (cur >= below)
  12. return false;
  13. } while (!atomic_try_cmpxchg(v, &cur, cur + 1));
  14. return true;
  15. }
  16. bool rq_wait_inc_below(struct rq_wait *rq_wait, unsigned int limit)
  17. {
  18. return atomic_inc_below(&rq_wait->inflight, limit);
  19. }
  20. void __rq_qos_cleanup(struct rq_qos *rqos, struct bio *bio)
  21. {
  22. do {
  23. if (rqos->ops->cleanup)
  24. rqos->ops->cleanup(rqos, bio);
  25. rqos = rqos->next;
  26. } while (rqos);
  27. }
  28. void __rq_qos_done(struct rq_qos *rqos, struct request *rq)
  29. {
  30. do {
  31. if (rqos->ops->done)
  32. rqos->ops->done(rqos, rq);
  33. rqos = rqos->next;
  34. } while (rqos);
  35. }
  36. void __rq_qos_issue(struct rq_qos *rqos, struct request *rq)
  37. {
  38. do {
  39. if (rqos->ops->issue)
  40. rqos->ops->issue(rqos, rq);
  41. rqos = rqos->next;
  42. } while (rqos);
  43. }
  44. void __rq_qos_requeue(struct rq_qos *rqos, struct request *rq)
  45. {
  46. do {
  47. if (rqos->ops->requeue)
  48. rqos->ops->requeue(rqos, rq);
  49. rqos = rqos->next;
  50. } while (rqos);
  51. }
  52. void __rq_qos_throttle(struct rq_qos *rqos, struct bio *bio)
  53. {
  54. do {
  55. if (rqos->ops->throttle)
  56. rqos->ops->throttle(rqos, bio);
  57. rqos = rqos->next;
  58. } while (rqos);
  59. }
  60. void __rq_qos_track(struct rq_qos *rqos, struct request *rq, struct bio *bio)
  61. {
  62. do {
  63. if (rqos->ops->track)
  64. rqos->ops->track(rqos, rq, bio);
  65. rqos = rqos->next;
  66. } while (rqos);
  67. }
  68. void __rq_qos_merge(struct rq_qos *rqos, struct request *rq, struct bio *bio)
  69. {
  70. do {
  71. if (rqos->ops->merge)
  72. rqos->ops->merge(rqos, rq, bio);
  73. rqos = rqos->next;
  74. } while (rqos);
  75. }
  76. void __rq_qos_done_bio(struct rq_qos *rqos, struct bio *bio)
  77. {
  78. do {
  79. if (rqos->ops->done_bio)
  80. rqos->ops->done_bio(rqos, bio);
  81. rqos = rqos->next;
  82. } while (rqos);
  83. }
  84. void __rq_qos_queue_depth_changed(struct rq_qos *rqos)
  85. {
  86. do {
  87. if (rqos->ops->queue_depth_changed)
  88. rqos->ops->queue_depth_changed(rqos);
  89. rqos = rqos->next;
  90. } while (rqos);
  91. }
  92. /*
  93. * Return true, if we can't increase the depth further by scaling
  94. */
  95. bool rq_depth_calc_max_depth(struct rq_depth *rqd)
  96. {
  97. unsigned int depth;
  98. bool ret = false;
  99. /*
  100. * For QD=1 devices, this is a special case. It's important for those
  101. * to have one request ready when one completes, so force a depth of
  102. * 2 for those devices. On the backend, it'll be a depth of 1 anyway,
  103. * since the device can't have more than that in flight. If we're
  104. * scaling down, then keep a setting of 1/1/1.
  105. */
  106. if (rqd->queue_depth == 1) {
  107. if (rqd->scale_step > 0)
  108. rqd->max_depth = 1;
  109. else {
  110. rqd->max_depth = 2;
  111. ret = true;
  112. }
  113. } else {
  114. /*
  115. * scale_step == 0 is our default state. If we have suffered
  116. * latency spikes, step will be > 0, and we shrink the
  117. * allowed write depths. If step is < 0, we're only doing
  118. * writes, and we allow a temporarily higher depth to
  119. * increase performance.
  120. */
  121. depth = min_t(unsigned int, rqd->default_depth,
  122. rqd->queue_depth);
  123. if (rqd->scale_step > 0)
  124. depth = 1 + ((depth - 1) >> min(31, rqd->scale_step));
  125. else if (rqd->scale_step < 0) {
  126. unsigned int maxd = 3 * rqd->queue_depth / 4;
  127. depth = 1 + ((depth - 1) << -rqd->scale_step);
  128. if (depth > maxd) {
  129. depth = maxd;
  130. ret = true;
  131. }
  132. }
  133. rqd->max_depth = depth;
  134. }
  135. return ret;
  136. }
  137. /* Returns true on success and false if scaling up wasn't possible */
  138. bool rq_depth_scale_up(struct rq_depth *rqd)
  139. {
  140. /*
  141. * Hit max in previous round, stop here
  142. */
  143. if (rqd->scaled_max)
  144. return false;
  145. rqd->scale_step--;
  146. rqd->scaled_max = rq_depth_calc_max_depth(rqd);
  147. return true;
  148. }
  149. /*
  150. * Scale rwb down. If 'hard_throttle' is set, do it quicker, since we
  151. * had a latency violation. Returns true on success and returns false if
  152. * scaling down wasn't possible.
  153. */
  154. bool rq_depth_scale_down(struct rq_depth *rqd, bool hard_throttle)
  155. {
  156. /*
  157. * Stop scaling down when we've hit the limit. This also prevents
  158. * ->scale_step from going to crazy values, if the device can't
  159. * keep up.
  160. */
  161. if (rqd->max_depth == 1)
  162. return false;
  163. if (rqd->scale_step < 0 && hard_throttle)
  164. rqd->scale_step = 0;
  165. else
  166. rqd->scale_step++;
  167. rqd->scaled_max = false;
  168. rq_depth_calc_max_depth(rqd);
  169. return true;
  170. }
  171. struct rq_qos_wait_data {
  172. struct wait_queue_entry wq;
  173. struct task_struct *task;
  174. struct rq_wait *rqw;
  175. acquire_inflight_cb_t *cb;
  176. void *private_data;
  177. bool got_token;
  178. };
  179. static int rq_qos_wake_function(struct wait_queue_entry *curr,
  180. unsigned int mode, int wake_flags, void *key)
  181. {
  182. struct rq_qos_wait_data *data = container_of(curr,
  183. struct rq_qos_wait_data,
  184. wq);
  185. /*
  186. * If we fail to get a budget, return -1 to interrupt the wake up loop
  187. * in __wake_up_common.
  188. */
  189. if (!data->cb(data->rqw, data->private_data))
  190. return -1;
  191. data->got_token = true;
  192. smp_wmb();
  193. list_del_init(&curr->entry);
  194. wake_up_process(data->task);
  195. return 1;
  196. }
  197. /**
  198. * rq_qos_wait - throttle on a rqw if we need to
  199. * @rqw: rqw to throttle on
  200. * @private_data: caller provided specific data
  201. * @acquire_inflight_cb: inc the rqw->inflight counter if we can
  202. * @cleanup_cb: the callback to cleanup in case we race with a waker
  203. *
  204. * This provides a uniform place for the rq_qos users to do their throttling.
  205. * Since you can end up with a lot of things sleeping at once, this manages the
  206. * waking up based on the resources available. The acquire_inflight_cb should
  207. * inc the rqw->inflight if we have the ability to do so, or return false if not
  208. * and then we will sleep until the room becomes available.
  209. *
  210. * cleanup_cb is in case that we race with a waker and need to cleanup the
  211. * inflight count accordingly.
  212. */
  213. void rq_qos_wait(struct rq_wait *rqw, void *private_data,
  214. acquire_inflight_cb_t *acquire_inflight_cb,
  215. cleanup_cb_t *cleanup_cb)
  216. {
  217. struct rq_qos_wait_data data = {
  218. .wq = {
  219. .func = rq_qos_wake_function,
  220. .entry = LIST_HEAD_INIT(data.wq.entry),
  221. },
  222. .task = current,
  223. .rqw = rqw,
  224. .cb = acquire_inflight_cb,
  225. .private_data = private_data,
  226. };
  227. bool has_sleeper;
  228. has_sleeper = wq_has_sleeper(&rqw->wait);
  229. if (!has_sleeper && acquire_inflight_cb(rqw, private_data))
  230. return;
  231. has_sleeper = !prepare_to_wait_exclusive(&rqw->wait, &data.wq,
  232. TASK_UNINTERRUPTIBLE);
  233. do {
  234. /* The memory barrier in set_task_state saves us here. */
  235. if (data.got_token)
  236. break;
  237. if (!has_sleeper && acquire_inflight_cb(rqw, private_data)) {
  238. finish_wait(&rqw->wait, &data.wq);
  239. /*
  240. * We raced with wbt_wake_function() getting a token,
  241. * which means we now have two. Put our local token
  242. * and wake anyone else potentially waiting for one.
  243. */
  244. smp_rmb();
  245. if (data.got_token)
  246. cleanup_cb(rqw, private_data);
  247. break;
  248. }
  249. io_schedule();
  250. has_sleeper = true;
  251. set_current_state(TASK_UNINTERRUPTIBLE);
  252. } while (1);
  253. finish_wait(&rqw->wait, &data.wq);
  254. }
  255. void rq_qos_exit(struct request_queue *q)
  256. {
  257. while (q->rq_qos) {
  258. struct rq_qos *rqos = q->rq_qos;
  259. q->rq_qos = rqos->next;
  260. rqos->ops->exit(rqos);
  261. }
  262. }