blk.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BLK_INTERNAL_H
  3. #define BLK_INTERNAL_H
  4. #include <linux/idr.h>
  5. #include <linux/blk-mq.h>
  6. #include "blk-mq.h"
  7. /* Amount of time in which a process may batch requests */
  8. #define BLK_BATCH_TIME (HZ/50UL)
  9. /* Number of requests a "batching" process may submit */
  10. #define BLK_BATCH_REQ 32
  11. /* Max future timer expiry for timeouts */
  12. #define BLK_MAX_TIMEOUT (5 * HZ)
  13. #ifdef CONFIG_DEBUG_FS
  14. extern struct dentry *blk_debugfs_root;
  15. #endif
  16. struct blk_flush_queue {
  17. unsigned int flush_queue_delayed:1;
  18. unsigned int flush_pending_idx:1;
  19. unsigned int flush_running_idx:1;
  20. blk_status_t rq_status;
  21. unsigned long flush_pending_since;
  22. struct list_head flush_queue[2];
  23. struct list_head flush_data_in_flight;
  24. struct request *flush_rq;
  25. /*
  26. * flush_rq shares tag with this rq, both can't be active
  27. * at the same time
  28. */
  29. struct request *orig_rq;
  30. spinlock_t mq_flush_lock;
  31. };
  32. extern struct kmem_cache *blk_requestq_cachep;
  33. extern struct kmem_cache *request_cachep;
  34. extern struct kobj_type blk_queue_ktype;
  35. extern struct ida blk_queue_ida;
  36. /*
  37. * @q->queue_lock is set while a queue is being initialized. Since we know
  38. * that no other threads access the queue object before @q->queue_lock has
  39. * been set, it is safe to manipulate queue flags without holding the
  40. * queue_lock if @q->queue_lock == NULL. See also blk_alloc_queue_node() and
  41. * blk_init_allocated_queue().
  42. */
  43. static inline void queue_lockdep_assert_held(struct request_queue *q)
  44. {
  45. if (q->queue_lock)
  46. lockdep_assert_held(q->queue_lock);
  47. }
  48. static inline void queue_flag_set_unlocked(unsigned int flag,
  49. struct request_queue *q)
  50. {
  51. if (test_bit(QUEUE_FLAG_INIT_DONE, &q->queue_flags) &&
  52. kref_read(&q->kobj.kref))
  53. lockdep_assert_held(q->queue_lock);
  54. __set_bit(flag, &q->queue_flags);
  55. }
  56. static inline void queue_flag_clear_unlocked(unsigned int flag,
  57. struct request_queue *q)
  58. {
  59. if (test_bit(QUEUE_FLAG_INIT_DONE, &q->queue_flags) &&
  60. kref_read(&q->kobj.kref))
  61. lockdep_assert_held(q->queue_lock);
  62. __clear_bit(flag, &q->queue_flags);
  63. }
  64. static inline int queue_flag_test_and_clear(unsigned int flag,
  65. struct request_queue *q)
  66. {
  67. queue_lockdep_assert_held(q);
  68. if (test_bit(flag, &q->queue_flags)) {
  69. __clear_bit(flag, &q->queue_flags);
  70. return 1;
  71. }
  72. return 0;
  73. }
  74. static inline int queue_flag_test_and_set(unsigned int flag,
  75. struct request_queue *q)
  76. {
  77. queue_lockdep_assert_held(q);
  78. if (!test_bit(flag, &q->queue_flags)) {
  79. __set_bit(flag, &q->queue_flags);
  80. return 0;
  81. }
  82. return 1;
  83. }
  84. static inline void queue_flag_set(unsigned int flag, struct request_queue *q)
  85. {
  86. queue_lockdep_assert_held(q);
  87. __set_bit(flag, &q->queue_flags);
  88. }
  89. static inline void queue_flag_clear(unsigned int flag, struct request_queue *q)
  90. {
  91. queue_lockdep_assert_held(q);
  92. __clear_bit(flag, &q->queue_flags);
  93. }
  94. static inline struct blk_flush_queue *blk_get_flush_queue(
  95. struct request_queue *q, struct blk_mq_ctx *ctx)
  96. {
  97. if (q->mq_ops)
  98. return blk_mq_map_queue(q, ctx->cpu)->fq;
  99. return q->fq;
  100. }
  101. static inline void __blk_get_queue(struct request_queue *q)
  102. {
  103. kobject_get(&q->kobj);
  104. }
  105. static inline bool
  106. is_flush_rq(struct request *req, struct blk_mq_hw_ctx *hctx)
  107. {
  108. return hctx->fq->flush_rq == req;
  109. }
  110. struct blk_flush_queue *blk_alloc_flush_queue(struct request_queue *q,
  111. int node, int cmd_size, gfp_t flags);
  112. void blk_free_flush_queue(struct blk_flush_queue *q);
  113. int blk_init_rl(struct request_list *rl, struct request_queue *q,
  114. gfp_t gfp_mask);
  115. void blk_exit_rl(struct request_queue *q, struct request_list *rl);
  116. void blk_exit_queue(struct request_queue *q);
  117. void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
  118. struct bio *bio);
  119. void blk_queue_bypass_start(struct request_queue *q);
  120. void blk_queue_bypass_end(struct request_queue *q);
  121. void __blk_queue_free_tags(struct request_queue *q);
  122. void blk_freeze_queue(struct request_queue *q);
  123. static inline void blk_queue_enter_live(struct request_queue *q)
  124. {
  125. /*
  126. * Given that running in generic_make_request() context
  127. * guarantees that a live reference against q_usage_counter has
  128. * been established, further references under that same context
  129. * need not check that the queue has been frozen (marked dead).
  130. */
  131. percpu_ref_get(&q->q_usage_counter);
  132. }
  133. #ifdef CONFIG_BLK_DEV_INTEGRITY
  134. void blk_flush_integrity(void);
  135. bool __bio_integrity_endio(struct bio *);
  136. static inline bool bio_integrity_endio(struct bio *bio)
  137. {
  138. if (bio_integrity(bio))
  139. return __bio_integrity_endio(bio);
  140. return true;
  141. }
  142. #else
  143. static inline void blk_flush_integrity(void)
  144. {
  145. }
  146. static inline bool bio_integrity_endio(struct bio *bio)
  147. {
  148. return true;
  149. }
  150. #endif
  151. void blk_timeout_work(struct work_struct *work);
  152. unsigned long blk_rq_timeout(unsigned long timeout);
  153. void blk_add_timer(struct request *req);
  154. void blk_delete_timer(struct request *);
  155. bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
  156. struct bio *bio);
  157. bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
  158. struct bio *bio);
  159. bool bio_attempt_discard_merge(struct request_queue *q, struct request *req,
  160. struct bio *bio);
  161. bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
  162. unsigned int *request_count,
  163. struct request **same_queue_rq);
  164. unsigned int blk_plug_queued_count(struct request_queue *q);
  165. void blk_account_io_start(struct request *req, bool new_io);
  166. void blk_account_io_completion(struct request *req, unsigned int bytes);
  167. void blk_account_io_done(struct request *req, u64 now);
  168. /*
  169. * EH timer and IO completion will both attempt to 'grab' the request, make
  170. * sure that only one of them succeeds. Steal the bottom bit of the
  171. * __deadline field for this.
  172. */
  173. static inline int blk_mark_rq_complete(struct request *rq)
  174. {
  175. return test_and_set_bit(0, &rq->__deadline);
  176. }
  177. static inline void blk_clear_rq_complete(struct request *rq)
  178. {
  179. clear_bit(0, &rq->__deadline);
  180. }
  181. static inline bool blk_rq_is_complete(struct request *rq)
  182. {
  183. return test_bit(0, &rq->__deadline);
  184. }
  185. /*
  186. * Internal elevator interface
  187. */
  188. #define ELV_ON_HASH(rq) ((rq)->rq_flags & RQF_HASHED)
  189. void blk_insert_flush(struct request *rq);
  190. static inline void elv_activate_rq(struct request_queue *q, struct request *rq)
  191. {
  192. struct elevator_queue *e = q->elevator;
  193. if (e->type->ops.sq.elevator_activate_req_fn)
  194. e->type->ops.sq.elevator_activate_req_fn(q, rq);
  195. }
  196. static inline void elv_deactivate_rq(struct request_queue *q, struct request *rq)
  197. {
  198. struct elevator_queue *e = q->elevator;
  199. if (e->type->ops.sq.elevator_deactivate_req_fn)
  200. e->type->ops.sq.elevator_deactivate_req_fn(q, rq);
  201. }
  202. int elevator_init(struct request_queue *);
  203. int elevator_init_mq(struct request_queue *q);
  204. int elevator_switch_mq(struct request_queue *q,
  205. struct elevator_type *new_e);
  206. void elevator_exit(struct request_queue *, struct elevator_queue *);
  207. int elv_register_queue(struct request_queue *q);
  208. void elv_unregister_queue(struct request_queue *q);
  209. struct hd_struct *__disk_get_part(struct gendisk *disk, int partno);
  210. #ifdef CONFIG_FAIL_IO_TIMEOUT
  211. int blk_should_fake_timeout(struct request_queue *);
  212. ssize_t part_timeout_show(struct device *, struct device_attribute *, char *);
  213. ssize_t part_timeout_store(struct device *, struct device_attribute *,
  214. const char *, size_t);
  215. #else
  216. static inline int blk_should_fake_timeout(struct request_queue *q)
  217. {
  218. return 0;
  219. }
  220. #endif
  221. int ll_back_merge_fn(struct request_queue *q, struct request *req,
  222. struct bio *bio);
  223. int ll_front_merge_fn(struct request_queue *q, struct request *req,
  224. struct bio *bio);
  225. struct request *attempt_back_merge(struct request_queue *q, struct request *rq);
  226. struct request *attempt_front_merge(struct request_queue *q, struct request *rq);
  227. int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
  228. struct request *next);
  229. void blk_recalc_rq_segments(struct request *rq);
  230. void blk_rq_set_mixed_merge(struct request *rq);
  231. bool blk_rq_merge_ok(struct request *rq, struct bio *bio);
  232. enum elv_merge blk_try_merge(struct request *rq, struct bio *bio);
  233. void blk_queue_congestion_threshold(struct request_queue *q);
  234. int blk_dev_init(void);
  235. /*
  236. * Return the threshold (number of used requests) at which the queue is
  237. * considered to be congested. It include a little hysteresis to keep the
  238. * context switch rate down.
  239. */
  240. static inline int queue_congestion_on_threshold(struct request_queue *q)
  241. {
  242. return q->nr_congestion_on;
  243. }
  244. /*
  245. * The threshold at which a queue is considered to be uncongested
  246. */
  247. static inline int queue_congestion_off_threshold(struct request_queue *q)
  248. {
  249. return q->nr_congestion_off;
  250. }
  251. extern int blk_update_nr_requests(struct request_queue *, unsigned int);
  252. /*
  253. * Contribute to IO statistics IFF:
  254. *
  255. * a) it's attached to a gendisk, and
  256. * b) the queue had IO stats enabled when this request was started, and
  257. * c) it's a file system request
  258. */
  259. static inline bool blk_do_io_stat(struct request *rq)
  260. {
  261. return rq->rq_disk &&
  262. (rq->rq_flags & RQF_IO_STAT) &&
  263. !blk_rq_is_passthrough(rq);
  264. }
  265. static inline void req_set_nomerge(struct request_queue *q, struct request *req)
  266. {
  267. req->cmd_flags |= REQ_NOMERGE;
  268. if (req == q->last_merge)
  269. q->last_merge = NULL;
  270. }
  271. /*
  272. * Steal a bit from this field for legacy IO path atomic IO marking. Note that
  273. * setting the deadline clears the bottom bit, potentially clearing the
  274. * completed bit. The user has to be OK with this (current ones are fine).
  275. */
  276. static inline void blk_rq_set_deadline(struct request *rq, unsigned long time)
  277. {
  278. rq->__deadline = time & ~0x1UL;
  279. }
  280. static inline unsigned long blk_rq_deadline(struct request *rq)
  281. {
  282. return rq->__deadline & ~0x1UL;
  283. }
  284. /*
  285. * The max size one bio can handle is UINT_MAX becasue bvec_iter.bi_size
  286. * is defined as 'unsigned int', meantime it has to aligned to with logical
  287. * block size which is the minimum accepted unit by hardware.
  288. */
  289. static inline unsigned int bio_allowed_max_sectors(struct request_queue *q)
  290. {
  291. return round_down(UINT_MAX, queue_logical_block_size(q)) >> 9;
  292. }
  293. /*
  294. * Internal io_context interface
  295. */
  296. void get_io_context(struct io_context *ioc);
  297. struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q);
  298. struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,
  299. gfp_t gfp_mask);
  300. void ioc_clear_queue(struct request_queue *q);
  301. int create_task_io_context(struct task_struct *task, gfp_t gfp_mask, int node);
  302. /**
  303. * rq_ioc - determine io_context for request allocation
  304. * @bio: request being allocated is for this bio (can be %NULL)
  305. *
  306. * Determine io_context to use for request allocation for @bio. May return
  307. * %NULL if %current->io_context doesn't exist.
  308. */
  309. static inline struct io_context *rq_ioc(struct bio *bio)
  310. {
  311. #ifdef CONFIG_BLK_CGROUP
  312. if (bio && bio->bi_ioc)
  313. return bio->bi_ioc;
  314. #endif
  315. return current->io_context;
  316. }
  317. /**
  318. * create_io_context - try to create task->io_context
  319. * @gfp_mask: allocation mask
  320. * @node: allocation node
  321. *
  322. * If %current->io_context is %NULL, allocate a new io_context and install
  323. * it. Returns the current %current->io_context which may be %NULL if
  324. * allocation failed.
  325. *
  326. * Note that this function can't be called with IRQ disabled because
  327. * task_lock which protects %current->io_context is IRQ-unsafe.
  328. */
  329. static inline struct io_context *create_io_context(gfp_t gfp_mask, int node)
  330. {
  331. WARN_ON_ONCE(irqs_disabled());
  332. if (unlikely(!current->io_context))
  333. create_task_io_context(current, gfp_mask, node);
  334. return current->io_context;
  335. }
  336. /*
  337. * Internal throttling interface
  338. */
  339. #ifdef CONFIG_BLK_DEV_THROTTLING
  340. extern void blk_throtl_drain(struct request_queue *q);
  341. extern int blk_throtl_init(struct request_queue *q);
  342. extern void blk_throtl_exit(struct request_queue *q);
  343. extern void blk_throtl_register_queue(struct request_queue *q);
  344. #else /* CONFIG_BLK_DEV_THROTTLING */
  345. static inline void blk_throtl_drain(struct request_queue *q) { }
  346. static inline int blk_throtl_init(struct request_queue *q) { return 0; }
  347. static inline void blk_throtl_exit(struct request_queue *q) { }
  348. static inline void blk_throtl_register_queue(struct request_queue *q) { }
  349. #endif /* CONFIG_BLK_DEV_THROTTLING */
  350. #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
  351. extern ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page);
  352. extern ssize_t blk_throtl_sample_time_store(struct request_queue *q,
  353. const char *page, size_t count);
  354. extern void blk_throtl_bio_endio(struct bio *bio);
  355. extern void blk_throtl_stat_add(struct request *rq, u64 time);
  356. #else
  357. static inline void blk_throtl_bio_endio(struct bio *bio) { }
  358. static inline void blk_throtl_stat_add(struct request *rq, u64 time) { }
  359. #endif
  360. #ifdef CONFIG_BOUNCE
  361. extern int init_emergency_isa_pool(void);
  362. extern void blk_queue_bounce(struct request_queue *q, struct bio **bio);
  363. #else
  364. static inline int init_emergency_isa_pool(void)
  365. {
  366. return 0;
  367. }
  368. static inline void blk_queue_bounce(struct request_queue *q, struct bio **bio)
  369. {
  370. }
  371. #endif /* CONFIG_BOUNCE */
  372. extern void blk_drain_queue(struct request_queue *q);
  373. #ifdef CONFIG_BLK_CGROUP_IOLATENCY
  374. extern int blk_iolatency_init(struct request_queue *q);
  375. #else
  376. static inline int blk_iolatency_init(struct request_queue *q) { return 0; }
  377. #endif
  378. #endif /* BLK_INTERNAL_H */