blk-mq-sched.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BLK_MQ_SCHED_H
  3. #define BLK_MQ_SCHED_H
  4. #include "elevator.h"
  5. #include "blk-mq.h"
  6. #define MAX_SCHED_RQ (16 * BLKDEV_DEFAULT_RQ)
  7. bool blk_mq_sched_try_merge(struct request_queue *q, struct bio *bio,
  8. unsigned int nr_segs, struct request **merged_request);
  9. bool blk_mq_sched_bio_merge(struct request_queue *q, struct bio *bio,
  10. unsigned int nr_segs);
  11. bool blk_mq_sched_try_insert_merge(struct request_queue *q, struct request *rq,
  12. struct list_head *free);
  13. void blk_mq_sched_mark_restart_hctx(struct blk_mq_hw_ctx *hctx);
  14. void __blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx);
  15. void blk_mq_sched_dispatch_requests(struct blk_mq_hw_ctx *hctx);
  16. int blk_mq_init_sched(struct request_queue *q, struct elevator_type *e,
  17. struct elevator_resources *res);
  18. void blk_mq_exit_sched(struct request_queue *q, struct elevator_queue *e);
  19. void blk_mq_sched_free_rqs(struct request_queue *q);
  20. struct elevator_tags *blk_mq_alloc_sched_tags(struct blk_mq_tag_set *set,
  21. unsigned int nr_hw_queues, unsigned int nr_requests);
  22. int blk_mq_alloc_sched_res(struct request_queue *q,
  23. struct elevator_type *type,
  24. struct elevator_resources *res,
  25. unsigned int nr_hw_queues);
  26. int blk_mq_alloc_sched_res_batch(struct xarray *elv_tbl,
  27. struct blk_mq_tag_set *set, unsigned int nr_hw_queues);
  28. int blk_mq_alloc_sched_ctx_batch(struct xarray *elv_tbl,
  29. struct blk_mq_tag_set *set);
  30. void blk_mq_free_sched_ctx_batch(struct xarray *elv_tbl);
  31. void blk_mq_free_sched_tags(struct elevator_tags *et,
  32. struct blk_mq_tag_set *set);
  33. void blk_mq_free_sched_res(struct elevator_resources *res,
  34. struct elevator_type *type,
  35. struct blk_mq_tag_set *set);
  36. void blk_mq_free_sched_res_batch(struct xarray *et_table,
  37. struct blk_mq_tag_set *set);
  38. /*
  39. * blk_mq_alloc_sched_data() - Allocates scheduler specific data
  40. * Returns:
  41. * - Pointer to allocated data on success
  42. * - NULL if no allocation needed
  43. * - ERR_PTR(-ENOMEM) in case of failure
  44. */
  45. static inline void *blk_mq_alloc_sched_data(struct request_queue *q,
  46. struct elevator_type *e)
  47. {
  48. void *sched_data;
  49. if (!e || !e->ops.alloc_sched_data)
  50. return NULL;
  51. sched_data = e->ops.alloc_sched_data(q);
  52. return (sched_data) ?: ERR_PTR(-ENOMEM);
  53. }
  54. static inline void blk_mq_free_sched_data(struct elevator_type *e, void *data)
  55. {
  56. if (e && e->ops.free_sched_data)
  57. e->ops.free_sched_data(data);
  58. }
  59. static inline void blk_mq_sched_restart(struct blk_mq_hw_ctx *hctx)
  60. {
  61. if (test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
  62. __blk_mq_sched_restart(hctx);
  63. }
  64. static inline bool bio_mergeable(struct bio *bio)
  65. {
  66. return !(bio->bi_opf & REQ_NOMERGE_FLAGS);
  67. }
  68. static inline bool
  69. blk_mq_sched_allow_merge(struct request_queue *q, struct request *rq,
  70. struct bio *bio)
  71. {
  72. if (rq->rq_flags & RQF_USE_SCHED) {
  73. struct elevator_queue *e = q->elevator;
  74. if (e->type->ops.allow_merge)
  75. return e->type->ops.allow_merge(q, rq, bio);
  76. }
  77. return true;
  78. }
  79. static inline void blk_mq_sched_completed_request(struct request *rq, u64 now)
  80. {
  81. if (rq->rq_flags & RQF_USE_SCHED) {
  82. struct elevator_queue *e = rq->q->elevator;
  83. if (e->type->ops.completed_request)
  84. e->type->ops.completed_request(rq, now);
  85. }
  86. }
  87. static inline void blk_mq_sched_requeue_request(struct request *rq)
  88. {
  89. if (rq->rq_flags & RQF_USE_SCHED) {
  90. struct request_queue *q = rq->q;
  91. struct elevator_queue *e = q->elevator;
  92. if (e->type->ops.requeue_request)
  93. e->type->ops.requeue_request(rq);
  94. }
  95. }
  96. static inline bool blk_mq_sched_has_work(struct blk_mq_hw_ctx *hctx)
  97. {
  98. struct elevator_queue *e = hctx->queue->elevator;
  99. if (e && e->type->ops.has_work)
  100. return e->type->ops.has_work(hctx);
  101. return false;
  102. }
  103. static inline bool blk_mq_sched_needs_restart(struct blk_mq_hw_ctx *hctx)
  104. {
  105. return test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
  106. }
  107. static inline void blk_mq_set_min_shallow_depth(struct request_queue *q,
  108. unsigned int depth)
  109. {
  110. struct blk_mq_hw_ctx *hctx;
  111. unsigned long i;
  112. queue_for_each_hw_ctx(q, hctx, i)
  113. sbitmap_queue_min_shallow_depth(&hctx->sched_tags->bitmap_tags,
  114. depth);
  115. }
  116. static inline bool blk_mq_is_sync_read(blk_opf_t opf)
  117. {
  118. return op_is_sync(opf) && !op_is_write(opf);
  119. }
  120. #endif