blk-mq.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. struct blk_mq_ctxs {
  8. struct kobject kobj;
  9. struct blk_mq_ctx __percpu *queue_ctx;
  10. };
  11. /**
  12. * struct blk_mq_ctx - State for a software queue facing the submitting CPUs
  13. */
  14. struct blk_mq_ctx {
  15. struct {
  16. spinlock_t lock;
  17. struct list_head rq_lists[HCTX_MAX_TYPES];
  18. } ____cacheline_aligned_in_smp;
  19. unsigned int cpu;
  20. unsigned short index_hw[HCTX_MAX_TYPES];
  21. struct blk_mq_hw_ctx *hctxs[HCTX_MAX_TYPES];
  22. /* incremented at dispatch time */
  23. unsigned long rq_dispatched[2];
  24. unsigned long rq_merged;
  25. /* incremented at completion time */
  26. unsigned long ____cacheline_aligned_in_smp rq_completed[2];
  27. struct request_queue *queue;
  28. struct blk_mq_ctxs *ctxs;
  29. struct kobject kobj;
  30. ANDROID_OEM_DATA_ARRAY(1, 2);
  31. } ____cacheline_aligned_in_smp;
  32. void blk_mq_exit_queue(struct request_queue *q);
  33. int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr);
  34. void blk_mq_wake_waiters(struct request_queue *q);
  35. bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *,
  36. unsigned int);
  37. void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
  38. bool kick_requeue_list);
  39. void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list);
  40. struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
  41. struct blk_mq_ctx *start);
  42. void blk_mq_put_rq_ref(struct request *rq);
  43. /*
  44. * Internal helpers for allocating/freeing the request map
  45. */
  46. void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
  47. unsigned int hctx_idx);
  48. void blk_mq_free_rq_map(struct blk_mq_tags *tags, unsigned int flags);
  49. struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
  50. unsigned int hctx_idx,
  51. unsigned int nr_tags,
  52. unsigned int reserved_tags,
  53. unsigned int flags);
  54. int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
  55. unsigned int hctx_idx, unsigned int depth);
  56. /*
  57. * Internal helpers for request insertion into sw queues
  58. */
  59. void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
  60. bool at_head);
  61. void blk_mq_request_bypass_insert(struct request *rq, bool at_head,
  62. bool run_queue);
  63. void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
  64. struct list_head *list);
  65. /* Used by blk_insert_cloned_request() to issue request directly */
  66. blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last);
  67. void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
  68. struct list_head *list);
  69. /*
  70. * CPU -> queue mappings
  71. */
  72. extern int blk_mq_hw_queue_to_node(struct blk_mq_queue_map *qmap, unsigned int);
  73. /*
  74. * blk_mq_map_queue_type() - map (hctx_type,cpu) to hardware queue
  75. * @q: request queue
  76. * @type: the hctx type index
  77. * @cpu: CPU
  78. */
  79. static inline struct blk_mq_hw_ctx *blk_mq_map_queue_type(struct request_queue *q,
  80. enum hctx_type type,
  81. unsigned int cpu)
  82. {
  83. return q->queue_hw_ctx[q->tag_set->map[type].mq_map[cpu]];
  84. }
  85. /*
  86. * blk_mq_map_queue() - map (cmd_flags,type) to hardware queue
  87. * @q: request queue
  88. * @flags: request command flags
  89. * @cpu: cpu ctx
  90. */
  91. static inline struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q,
  92. unsigned int flags,
  93. struct blk_mq_ctx *ctx)
  94. {
  95. enum hctx_type type = HCTX_TYPE_DEFAULT;
  96. /*
  97. * The caller ensure that if REQ_HIPRI, poll must be enabled.
  98. */
  99. if (flags & REQ_HIPRI)
  100. type = HCTX_TYPE_POLL;
  101. else if ((flags & REQ_OP_MASK) == REQ_OP_READ)
  102. type = HCTX_TYPE_READ;
  103. return ctx->hctxs[type];
  104. }
  105. /*
  106. * sysfs helpers
  107. */
  108. extern void blk_mq_sysfs_init(struct request_queue *q);
  109. extern void blk_mq_sysfs_deinit(struct request_queue *q);
  110. extern int __blk_mq_register_dev(struct device *dev, struct request_queue *q);
  111. extern int blk_mq_sysfs_register(struct request_queue *q);
  112. extern void blk_mq_sysfs_unregister(struct request_queue *q);
  113. extern void blk_mq_hctx_kobj_init(struct blk_mq_hw_ctx *hctx);
  114. void blk_mq_release(struct request_queue *q);
  115. static inline struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
  116. unsigned int cpu)
  117. {
  118. return per_cpu_ptr(q->queue_ctx, cpu);
  119. }
  120. /*
  121. * This assumes per-cpu software queueing queues. They could be per-node
  122. * as well, for instance. For now this is hardcoded as-is. Note that we don't
  123. * care about preemption, since we know the ctx's are persistent. This does
  124. * mean that we can't rely on ctx always matching the currently running CPU.
  125. */
  126. static inline struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
  127. {
  128. return __blk_mq_get_ctx(q, raw_smp_processor_id());
  129. }
  130. struct blk_mq_alloc_data {
  131. /* input parameter */
  132. struct request_queue *q;
  133. blk_mq_req_flags_t flags;
  134. unsigned int shallow_depth;
  135. unsigned int cmd_flags;
  136. /* input & output parameter */
  137. struct blk_mq_ctx *ctx;
  138. struct blk_mq_hw_ctx *hctx;
  139. };
  140. static inline bool blk_mq_is_sbitmap_shared(unsigned int flags)
  141. {
  142. return flags & BLK_MQ_F_TAG_HCTX_SHARED;
  143. }
  144. static inline struct blk_mq_tags *blk_mq_tags_from_data(struct blk_mq_alloc_data *data)
  145. {
  146. if (data->q->elevator)
  147. return data->hctx->sched_tags;
  148. return data->hctx->tags;
  149. }
  150. static inline bool blk_mq_hctx_stopped(struct blk_mq_hw_ctx *hctx)
  151. {
  152. return test_bit(BLK_MQ_S_STOPPED, &hctx->state);
  153. }
  154. static inline bool blk_mq_hw_queue_mapped(struct blk_mq_hw_ctx *hctx)
  155. {
  156. return hctx->nr_ctx && hctx->tags;
  157. }
  158. unsigned int blk_mq_in_flight(struct request_queue *q, struct hd_struct *part);
  159. void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,
  160. unsigned int inflight[2]);
  161. static inline void blk_mq_put_dispatch_budget(struct request_queue *q)
  162. {
  163. if (q->mq_ops->put_budget)
  164. q->mq_ops->put_budget(q);
  165. }
  166. static inline bool blk_mq_get_dispatch_budget(struct request_queue *q)
  167. {
  168. if (q->mq_ops->get_budget)
  169. return q->mq_ops->get_budget(q);
  170. return true;
  171. }
  172. static inline void __blk_mq_inc_active_requests(struct blk_mq_hw_ctx *hctx)
  173. {
  174. if (blk_mq_is_sbitmap_shared(hctx->flags))
  175. atomic_inc(&hctx->queue->nr_active_requests_shared_sbitmap);
  176. else
  177. atomic_inc(&hctx->nr_active);
  178. }
  179. static inline void __blk_mq_dec_active_requests(struct blk_mq_hw_ctx *hctx)
  180. {
  181. if (blk_mq_is_sbitmap_shared(hctx->flags))
  182. atomic_dec(&hctx->queue->nr_active_requests_shared_sbitmap);
  183. else
  184. atomic_dec(&hctx->nr_active);
  185. }
  186. static inline int __blk_mq_active_requests(struct blk_mq_hw_ctx *hctx)
  187. {
  188. if (blk_mq_is_sbitmap_shared(hctx->flags))
  189. return atomic_read(&hctx->queue->nr_active_requests_shared_sbitmap);
  190. return atomic_read(&hctx->nr_active);
  191. }
  192. static inline void __blk_mq_put_driver_tag(struct blk_mq_hw_ctx *hctx,
  193. struct request *rq)
  194. {
  195. blk_mq_put_tag(hctx->tags, rq->mq_ctx, rq->tag);
  196. rq->tag = BLK_MQ_NO_TAG;
  197. if (rq->rq_flags & RQF_MQ_INFLIGHT) {
  198. rq->rq_flags &= ~RQF_MQ_INFLIGHT;
  199. __blk_mq_dec_active_requests(hctx);
  200. }
  201. }
  202. static inline void blk_mq_put_driver_tag(struct request *rq)
  203. {
  204. if (rq->tag == BLK_MQ_NO_TAG || rq->internal_tag == BLK_MQ_NO_TAG)
  205. return;
  206. __blk_mq_put_driver_tag(rq->mq_hctx, rq);
  207. }
  208. static inline void blk_mq_clear_mq_map(struct blk_mq_queue_map *qmap)
  209. {
  210. int cpu;
  211. for_each_possible_cpu(cpu)
  212. qmap->mq_map[cpu] = 0;
  213. }
  214. /*
  215. * blk_mq_plug() - Get caller context plug
  216. * @q: request queue
  217. * @bio : the bio being submitted by the caller context
  218. *
  219. * Plugging, by design, may delay the insertion of BIOs into the elevator in
  220. * order to increase BIO merging opportunities. This however can cause BIO
  221. * insertion order to change from the order in which submit_bio() is being
  222. * executed in the case of multiple contexts concurrently issuing BIOs to a
  223. * device, even if these context are synchronized to tightly control BIO issuing
  224. * order. While this is not a problem with regular block devices, this ordering
  225. * change can cause write BIO failures with zoned block devices as these
  226. * require sequential write patterns to zones. Prevent this from happening by
  227. * ignoring the plug state of a BIO issuing context if the target request queue
  228. * is for a zoned block device and the BIO to plug is a write operation.
  229. *
  230. * Return current->plug if the bio can be plugged and NULL otherwise
  231. */
  232. static inline struct blk_plug *blk_mq_plug(struct request_queue *q,
  233. struct bio *bio)
  234. {
  235. /*
  236. * For regular block devices or read operations, use the context plug
  237. * which may be NULL if blk_start_plug() was not executed.
  238. */
  239. if (!blk_queue_is_zoned(q) || !op_is_write(bio_op(bio)))
  240. return current->plug;
  241. /* Zoned block device write operation case: do not plug the BIO */
  242. return NULL;
  243. }
  244. /*
  245. * For shared tag users, we track the number of currently active users
  246. * and attempt to provide a fair share of the tag depth for each of them.
  247. */
  248. static inline bool hctx_may_queue(struct blk_mq_hw_ctx *hctx,
  249. struct sbitmap_queue *bt)
  250. {
  251. unsigned int depth, users;
  252. if (!hctx || !(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED))
  253. return true;
  254. /*
  255. * Don't try dividing an ant
  256. */
  257. if (bt->sb.depth == 1)
  258. return true;
  259. if (blk_mq_is_sbitmap_shared(hctx->flags)) {
  260. struct request_queue *q = hctx->queue;
  261. struct blk_mq_tag_set *set = q->tag_set;
  262. if (!test_bit(QUEUE_FLAG_HCTX_ACTIVE, &q->queue_flags))
  263. return true;
  264. users = atomic_read(&set->active_queues_shared_sbitmap);
  265. } else {
  266. if (!test_bit(BLK_MQ_S_TAG_ACTIVE, &hctx->state))
  267. return true;
  268. users = atomic_read(&hctx->tags->active_queues);
  269. }
  270. if (!users)
  271. return true;
  272. /*
  273. * Allow at least some tags
  274. */
  275. depth = max((bt->sb.depth + users - 1) / users, 4U);
  276. return __blk_mq_active_requests(hctx) < depth;
  277. }
  278. #endif