blk-mq.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef INT_BLK_MQ_H
  3. #define INT_BLK_MQ_H
  4. #include "blk-stat.h"
  5. #include "blk-mq-tag.h"
  6. struct blk_mq_tag_set;
  7. /**
  8. * struct blk_mq_ctx - State for a software queue facing the submitting CPUs
  9. */
  10. struct blk_mq_ctx {
  11. struct {
  12. spinlock_t lock;
  13. struct list_head rq_list;
  14. } ____cacheline_aligned_in_smp;
  15. unsigned int cpu;
  16. unsigned int index_hw;
  17. /* incremented at dispatch time */
  18. unsigned long rq_dispatched[2];
  19. unsigned long rq_merged;
  20. /* incremented at completion time */
  21. unsigned long ____cacheline_aligned_in_smp rq_completed[2];
  22. struct request_queue *queue;
  23. struct kobject kobj;
  24. } ____cacheline_aligned_in_smp;
  25. void blk_mq_freeze_queue(struct request_queue *q);
  26. void blk_mq_exit_queue(struct request_queue *q);
  27. int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr);
  28. void blk_mq_wake_waiters(struct request_queue *q);
  29. bool blk_mq_dispatch_rq_list(struct request_queue *, struct list_head *, bool);
  30. void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list);
  31. bool blk_mq_get_driver_tag(struct request *rq);
  32. struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
  33. struct blk_mq_ctx *start);
  34. /*
  35. * Internal helpers for allocating/freeing the request map
  36. */
  37. void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
  38. unsigned int hctx_idx);
  39. void blk_mq_free_rq_map(struct blk_mq_tags *tags);
  40. struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
  41. unsigned int hctx_idx,
  42. unsigned int nr_tags,
  43. unsigned int reserved_tags);
  44. int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
  45. unsigned int hctx_idx, unsigned int depth);
  46. /*
  47. * Internal helpers for request insertion into sw queues
  48. */
  49. void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
  50. bool at_head);
  51. void blk_mq_request_bypass_insert(struct request *rq, bool run_queue);
  52. void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
  53. struct list_head *list);
  54. /* Used by blk_insert_cloned_request() to issue request directly */
  55. blk_status_t blk_mq_request_issue_directly(struct request *rq);
  56. void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
  57. struct list_head *list);
  58. /*
  59. * CPU -> queue mappings
  60. */
  61. extern int blk_mq_hw_queue_to_node(unsigned int *map, unsigned int);
  62. static inline struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q,
  63. int cpu)
  64. {
  65. return q->queue_hw_ctx[q->mq_map[cpu]];
  66. }
  67. /*
  68. * sysfs helpers
  69. */
  70. extern void blk_mq_sysfs_init(struct request_queue *q);
  71. extern void blk_mq_sysfs_deinit(struct request_queue *q);
  72. extern int __blk_mq_register_dev(struct device *dev, struct request_queue *q);
  73. extern int blk_mq_sysfs_register(struct request_queue *q);
  74. extern void blk_mq_sysfs_unregister(struct request_queue *q);
  75. extern void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx);
  76. void blk_mq_release(struct request_queue *q);
  77. /**
  78. * blk_mq_rq_state() - read the current MQ_RQ_* state of a request
  79. * @rq: target request.
  80. */
  81. static inline enum mq_rq_state blk_mq_rq_state(struct request *rq)
  82. {
  83. return READ_ONCE(rq->state);
  84. }
  85. static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  86. unsigned int cpu)
  87. {
  88. return per_cpu_ptr(q->queue_ctx, cpu);
  89. }
  90. /*
  91. * This assumes per-cpu software queueing queues. They could be per-node
  92. * as well, for instance. For now this is hardcoded as-is. Note that we don't
  93. * care about preemption, since we know the ctx's are persistent. This does
  94. * mean that we can't rely on ctx always matching the currently running CPU.
  95. */
  96. static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
  97. {
  98. return __blk_mq_get_ctx(q, get_cpu());
  99. }
  100. static inline void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
  101. {
  102. put_cpu();
  103. }
  104. struct blk_mq_alloc_data {
  105. /* input parameter */
  106. struct request_queue *q;
  107. blk_mq_req_flags_t flags;
  108. unsigned int shallow_depth;
  109. /* input & output parameter */
  110. struct blk_mq_ctx *ctx;
  111. struct blk_mq_hw_ctx *hctx;
  112. };
  113. static inline struct blk_mq_tags *blk_mq_tags_from_data(struct blk_mq_alloc_data *data)
  114. {
  115. if (data->flags & BLK_MQ_REQ_INTERNAL)
  116. return data->hctx->sched_tags;
  117. return data->hctx->tags;
  118. }
  119. static inline bool blk_mq_hctx_stopped(struct blk_mq_hw_ctx *hctx)
  120. {
  121. return test_bit(BLK_MQ_S_STOPPED, &hctx->state);
  122. }
  123. static inline bool blk_mq_hw_queue_mapped(struct blk_mq_hw_ctx *hctx)
  124. {
  125. return hctx->nr_ctx && hctx->tags;
  126. }
  127. void blk_mq_in_flight(struct request_queue *q, struct hd_struct *part,
  128. unsigned int inflight[2]);
  129. void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,
  130. unsigned int inflight[2]);
  131. static inline void blk_mq_put_dispatch_budget(struct blk_mq_hw_ctx *hctx)
  132. {
  133. struct request_queue *q = hctx->queue;
  134. if (q->mq_ops->put_budget)
  135. q->mq_ops->put_budget(hctx);
  136. }
  137. static inline bool blk_mq_get_dispatch_budget(struct blk_mq_hw_ctx *hctx)
  138. {
  139. struct request_queue *q = hctx->queue;
  140. if (q->mq_ops->get_budget)
  141. return q->mq_ops->get_budget(hctx);
  142. return true;
  143. }
  144. static inline void __blk_mq_put_driver_tag(struct blk_mq_hw_ctx *hctx,
  145. struct request *rq)
  146. {
  147. blk_mq_put_tag(hctx, hctx->tags, rq->mq_ctx, rq->tag);
  148. rq->tag = -1;
  149. if (rq->rq_flags & RQF_MQ_INFLIGHT) {
  150. rq->rq_flags &= ~RQF_MQ_INFLIGHT;
  151. atomic_dec(&hctx->nr_active);
  152. }
  153. }
  154. static inline void blk_mq_put_driver_tag_hctx(struct blk_mq_hw_ctx *hctx,
  155. struct request *rq)
  156. {
  157. if (rq->tag == -1 || rq->internal_tag == -1)
  158. return;
  159. __blk_mq_put_driver_tag(hctx, rq);
  160. }
  161. static inline void blk_mq_put_driver_tag(struct request *rq)
  162. {
  163. struct blk_mq_hw_ctx *hctx;
  164. if (rq->tag == -1 || rq->internal_tag == -1)
  165. return;
  166. hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu);
  167. __blk_mq_put_driver_tag(hctx, rq);
  168. }
  169. static inline void blk_mq_clear_mq_map(struct blk_mq_tag_set *set)
  170. {
  171. int cpu;
  172. for_each_possible_cpu(cpu)
  173. set->mq_map[cpu] = 0;
  174. }
  175. #endif