blk-rq-qos.c 7.0 KB

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