blk-rq-qos.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 rq_wait *rqw;
  174. acquire_inflight_cb_t *cb;
  175. void *private_data;
  176. bool got_token;
  177. };
  178. static int rq_qos_wake_function(struct wait_queue_entry *curr,
  179. unsigned int mode, int wake_flags, void *key)
  180. {
  181. struct rq_qos_wait_data *data = container_of(curr,
  182. struct rq_qos_wait_data,
  183. wq);
  184. /*
  185. * If we fail to get a budget, return -1 to interrupt the wake up loop
  186. * in __wake_up_common.
  187. */
  188. if (!data->cb(data->rqw, data->private_data))
  189. return -1;
  190. data->got_token = true;
  191. /*
  192. * autoremove_wake_function() removes the wait entry only when it
  193. * actually changed the task state. We want the wait always removed.
  194. * Remove explicitly and use default_wake_function().
  195. */
  196. default_wake_function(curr, mode, wake_flags, key);
  197. /*
  198. * Note that the order of operations is important as finish_wait()
  199. * tests whether @curr is removed without grabbing the lock. This
  200. * should be the last thing to do to make sure we will not have a
  201. * UAF access to @data. And the semantics of memory barrier in it
  202. * also make sure the waiter will see the latest @data->got_token
  203. * once list_empty_careful() in finish_wait() returns true.
  204. */
  205. list_del_init_careful(&curr->entry);
  206. return 1;
  207. }
  208. /**
  209. * rq_qos_wait - throttle on a rqw if we need to
  210. * @rqw: rqw to throttle on
  211. * @private_data: caller provided specific data
  212. * @acquire_inflight_cb: inc the rqw->inflight counter if we can
  213. * @cleanup_cb: the callback to cleanup in case we race with a waker
  214. *
  215. * This provides a uniform place for the rq_qos users to do their throttling.
  216. * Since you can end up with a lot of things sleeping at once, this manages the
  217. * waking up based on the resources available. The acquire_inflight_cb should
  218. * inc the rqw->inflight if we have the ability to do so, or return false if not
  219. * and then we will sleep until the room becomes available.
  220. *
  221. * cleanup_cb is in case that we race with a waker and need to cleanup the
  222. * inflight count accordingly.
  223. */
  224. void rq_qos_wait(struct rq_wait *rqw, void *private_data,
  225. acquire_inflight_cb_t *acquire_inflight_cb,
  226. cleanup_cb_t *cleanup_cb)
  227. {
  228. struct rq_qos_wait_data data = {
  229. .rqw = rqw,
  230. .cb = acquire_inflight_cb,
  231. .private_data = private_data,
  232. .got_token = false,
  233. };
  234. bool first_waiter;
  235. /*
  236. * If there are no waiters in the waiting queue, try to increase the
  237. * inflight counter if we can. Otherwise, prepare for adding ourselves
  238. * to the waiting queue.
  239. */
  240. if (!waitqueue_active(&rqw->wait) && acquire_inflight_cb(rqw, private_data))
  241. return;
  242. init_wait_func(&data.wq, rq_qos_wake_function);
  243. first_waiter = prepare_to_wait_exclusive(&rqw->wait, &data.wq,
  244. TASK_UNINTERRUPTIBLE);
  245. /*
  246. * Make sure there is at least one inflight process; otherwise, waiters
  247. * will never be woken up. Since there may be no inflight process before
  248. * adding ourselves to the waiting queue above, we need to try to
  249. * increase the inflight counter for ourselves. And it is sufficient to
  250. * guarantee that at least the first waiter to enter the waiting queue
  251. * will re-check the waiting condition before going to sleep, thus
  252. * ensuring forward progress.
  253. */
  254. if (!data.got_token && first_waiter && acquire_inflight_cb(rqw, private_data)) {
  255. finish_wait(&rqw->wait, &data.wq);
  256. /*
  257. * We raced with rq_qos_wake_function() getting a token,
  258. * which means we now have two. Put our local token
  259. * and wake anyone else potentially waiting for one.
  260. *
  261. * Enough memory barrier in list_empty_careful() in
  262. * finish_wait() is paired with list_del_init_careful()
  263. * in rq_qos_wake_function() to make sure we will see
  264. * the latest @data->got_token.
  265. */
  266. if (data.got_token)
  267. cleanup_cb(rqw, private_data);
  268. return;
  269. }
  270. /* we are now relying on the waker to increase our inflight counter. */
  271. do {
  272. if (data.got_token)
  273. break;
  274. io_schedule();
  275. set_current_state(TASK_UNINTERRUPTIBLE);
  276. } while (1);
  277. finish_wait(&rqw->wait, &data.wq);
  278. }
  279. void rq_qos_exit(struct request_queue *q)
  280. {
  281. mutex_lock(&q->rq_qos_mutex);
  282. while (q->rq_qos) {
  283. struct rq_qos *rqos = q->rq_qos;
  284. q->rq_qos = rqos->next;
  285. rqos->ops->exit(rqos);
  286. }
  287. blk_queue_flag_clear(QUEUE_FLAG_QOS_ENABLED, q);
  288. mutex_unlock(&q->rq_qos_mutex);
  289. }
  290. int rq_qos_add(struct rq_qos *rqos, struct gendisk *disk, enum rq_qos_id id,
  291. const struct rq_qos_ops *ops)
  292. {
  293. struct request_queue *q = disk->queue;
  294. unsigned int memflags;
  295. lockdep_assert_held(&q->rq_qos_mutex);
  296. rqos->disk = disk;
  297. rqos->id = id;
  298. rqos->ops = ops;
  299. /*
  300. * No IO can be in-flight when adding rqos, so freeze queue, which
  301. * is fine since we only support rq_qos for blk-mq queue.
  302. */
  303. memflags = blk_mq_freeze_queue(q);
  304. if (rq_qos_id(q, rqos->id))
  305. goto ebusy;
  306. rqos->next = q->rq_qos;
  307. q->rq_qos = rqos;
  308. blk_queue_flag_set(QUEUE_FLAG_QOS_ENABLED, q);
  309. blk_mq_unfreeze_queue(q, memflags);
  310. return 0;
  311. ebusy:
  312. blk_mq_unfreeze_queue(q, memflags);
  313. return -EBUSY;
  314. }
  315. void rq_qos_del(struct rq_qos *rqos)
  316. {
  317. struct request_queue *q = rqos->disk->queue;
  318. struct rq_qos **cur;
  319. unsigned int memflags;
  320. lockdep_assert_held(&q->rq_qos_mutex);
  321. memflags = blk_mq_freeze_queue(q);
  322. for (cur = &q->rq_qos; *cur; cur = &(*cur)->next) {
  323. if (*cur == rqos) {
  324. *cur = rqos->next;
  325. break;
  326. }
  327. }
  328. if (!q->rq_qos)
  329. blk_queue_flag_clear(QUEUE_FLAG_QOS_ENABLED, q);
  330. blk_mq_unfreeze_queue(q, memflags);
  331. }