blk-rq-qos.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef RQ_QOS_H
  3. #define RQ_QOS_H
  4. #include <linux/kernel.h>
  5. #include <linux/blkdev.h>
  6. #include <linux/blk_types.h>
  7. #include <linux/atomic.h>
  8. #include <linux/wait.h>
  9. #include <linux/blk-mq.h>
  10. #include "blk-mq-debugfs.h"
  11. struct blk_mq_debugfs_attr;
  12. enum rq_qos_id {
  13. RQ_QOS_WBT,
  14. RQ_QOS_LATENCY,
  15. RQ_QOS_COST,
  16. };
  17. struct rq_wait {
  18. wait_queue_head_t wait;
  19. atomic_t inflight;
  20. };
  21. struct rq_qos {
  22. const struct rq_qos_ops *ops;
  23. struct gendisk *disk;
  24. enum rq_qos_id id;
  25. struct rq_qos *next;
  26. #ifdef CONFIG_BLK_DEBUG_FS
  27. struct dentry *debugfs_dir;
  28. #endif
  29. };
  30. struct rq_qos_ops {
  31. void (*throttle)(struct rq_qos *, struct bio *);
  32. void (*track)(struct rq_qos *, struct request *, struct bio *);
  33. void (*merge)(struct rq_qos *, struct request *, struct bio *);
  34. void (*issue)(struct rq_qos *, struct request *);
  35. void (*requeue)(struct rq_qos *, struct request *);
  36. void (*done)(struct rq_qos *, struct request *);
  37. void (*done_bio)(struct rq_qos *, struct bio *);
  38. void (*cleanup)(struct rq_qos *, struct bio *);
  39. void (*queue_depth_changed)(struct rq_qos *);
  40. void (*exit)(struct rq_qos *);
  41. const struct blk_mq_debugfs_attr *debugfs_attrs;
  42. };
  43. struct rq_depth {
  44. unsigned int max_depth;
  45. int scale_step;
  46. bool scaled_max;
  47. unsigned int queue_depth;
  48. unsigned int default_depth;
  49. };
  50. static inline struct rq_qos *rq_qos_id(struct request_queue *q,
  51. enum rq_qos_id id)
  52. {
  53. struct rq_qos *rqos;
  54. for (rqos = q->rq_qos; rqos; rqos = rqos->next) {
  55. if (rqos->id == id)
  56. break;
  57. }
  58. return rqos;
  59. }
  60. static inline struct rq_qos *wbt_rq_qos(struct request_queue *q)
  61. {
  62. return rq_qos_id(q, RQ_QOS_WBT);
  63. }
  64. static inline struct rq_qos *iolat_rq_qos(struct request_queue *q)
  65. {
  66. return rq_qos_id(q, RQ_QOS_LATENCY);
  67. }
  68. static inline void rq_wait_init(struct rq_wait *rq_wait)
  69. {
  70. atomic_set(&rq_wait->inflight, 0);
  71. init_waitqueue_head(&rq_wait->wait);
  72. }
  73. int rq_qos_add(struct rq_qos *rqos, struct gendisk *disk, enum rq_qos_id id,
  74. const struct rq_qos_ops *ops);
  75. void rq_qos_del(struct rq_qos *rqos);
  76. typedef bool (acquire_inflight_cb_t)(struct rq_wait *rqw, void *private_data);
  77. typedef void (cleanup_cb_t)(struct rq_wait *rqw, void *private_data);
  78. void rq_qos_wait(struct rq_wait *rqw, void *private_data,
  79. acquire_inflight_cb_t *acquire_inflight_cb,
  80. cleanup_cb_t *cleanup_cb);
  81. bool rq_wait_inc_below(struct rq_wait *rq_wait, unsigned int limit);
  82. bool rq_depth_scale_up(struct rq_depth *rqd);
  83. bool rq_depth_scale_down(struct rq_depth *rqd, bool hard_throttle);
  84. bool rq_depth_calc_max_depth(struct rq_depth *rqd);
  85. void __rq_qos_cleanup(struct rq_qos *rqos, struct bio *bio);
  86. void __rq_qos_done(struct rq_qos *rqos, struct request *rq);
  87. void __rq_qos_issue(struct rq_qos *rqos, struct request *rq);
  88. void __rq_qos_requeue(struct rq_qos *rqos, struct request *rq);
  89. void __rq_qos_throttle(struct rq_qos *rqos, struct bio *bio);
  90. void __rq_qos_track(struct rq_qos *rqos, struct request *rq, struct bio *bio);
  91. void __rq_qos_merge(struct rq_qos *rqos, struct request *rq, struct bio *bio);
  92. void __rq_qos_done_bio(struct rq_qos *rqos, struct bio *bio);
  93. void __rq_qos_queue_depth_changed(struct rq_qos *rqos);
  94. static inline void rq_qos_cleanup(struct request_queue *q, struct bio *bio)
  95. {
  96. if (test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags) && q->rq_qos)
  97. __rq_qos_cleanup(q->rq_qos, bio);
  98. }
  99. static inline void rq_qos_done(struct request_queue *q, struct request *rq)
  100. {
  101. if (test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags) &&
  102. q->rq_qos && !blk_rq_is_passthrough(rq))
  103. __rq_qos_done(q->rq_qos, rq);
  104. }
  105. static inline void rq_qos_issue(struct request_queue *q, struct request *rq)
  106. {
  107. if (test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags) && q->rq_qos)
  108. __rq_qos_issue(q->rq_qos, rq);
  109. }
  110. static inline void rq_qos_requeue(struct request_queue *q, struct request *rq)
  111. {
  112. if (test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags) && q->rq_qos)
  113. __rq_qos_requeue(q->rq_qos, rq);
  114. }
  115. static inline void rq_qos_done_bio(struct bio *bio)
  116. {
  117. struct request_queue *q;
  118. if (!bio->bi_bdev || (!bio_flagged(bio, BIO_QOS_THROTTLED) &&
  119. !bio_flagged(bio, BIO_QOS_MERGED)))
  120. return;
  121. q = bdev_get_queue(bio->bi_bdev);
  122. /*
  123. * A BIO may carry BIO_QOS_* flags even if the associated request_queue
  124. * does not have rq_qos enabled. This can happen with stacked block
  125. * devices — for example, NVMe multipath, where it's possible that the
  126. * bottom device has QoS enabled but the top device does not. Therefore,
  127. * always verify that q->rq_qos is present and QoS is enabled before
  128. * calling __rq_qos_done_bio().
  129. */
  130. if (test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags) && q->rq_qos)
  131. __rq_qos_done_bio(q->rq_qos, bio);
  132. }
  133. static inline void rq_qos_throttle(struct request_queue *q, struct bio *bio)
  134. {
  135. if (test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags) && q->rq_qos) {
  136. bio_set_flag(bio, BIO_QOS_THROTTLED);
  137. __rq_qos_throttle(q->rq_qos, bio);
  138. }
  139. }
  140. static inline void rq_qos_track(struct request_queue *q, struct request *rq,
  141. struct bio *bio)
  142. {
  143. if (test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags) && q->rq_qos)
  144. __rq_qos_track(q->rq_qos, rq, bio);
  145. }
  146. static inline void rq_qos_merge(struct request_queue *q, struct request *rq,
  147. struct bio *bio)
  148. {
  149. if (test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags) && q->rq_qos) {
  150. bio_set_flag(bio, BIO_QOS_MERGED);
  151. __rq_qos_merge(q->rq_qos, rq, bio);
  152. }
  153. }
  154. static inline void rq_qos_queue_depth_changed(struct request_queue *q)
  155. {
  156. if (test_bit(QUEUE_FLAG_QOS_ENABLED, &q->queue_flags) && q->rq_qos)
  157. __rq_qos_queue_depth_changed(q->rq_qos);
  158. }
  159. void rq_qos_exit(struct request_queue *);
  160. #endif