blk.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 <xen/xen.h>
  7. #include "blk-mq.h"
  8. /* Amount of time in which a process may batch requests */
  9. #define BLK_BATCH_TIME (HZ/50UL)
  10. /* Number of requests a "batching" process may submit */
  11. #define BLK_BATCH_REQ 32
  12. /* Max future timer expiry for timeouts */
  13. #define BLK_MAX_TIMEOUT (5 * HZ)
  14. #ifdef CONFIG_DEBUG_FS
  15. extern struct dentry *blk_debugfs_root;
  16. #endif
  17. struct blk_flush_queue {
  18. unsigned int flush_queue_delayed:1;
  19. unsigned int flush_pending_idx:1;
  20. unsigned int flush_running_idx:1;
  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. struct blk_flush_queue *blk_alloc_flush_queue(struct request_queue *q,
  106. int node, int cmd_size, gfp_t flags);
  107. void blk_free_flush_queue(struct blk_flush_queue *q);
  108. int blk_init_rl(struct request_list *rl, struct request_queue *q,
  109. gfp_t gfp_mask);
  110. void blk_exit_rl(struct request_queue *q, struct request_list *rl);
  111. void blk_exit_queue(struct request_queue *q);
  112. void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
  113. struct bio *bio);
  114. void blk_queue_bypass_start(struct request_queue *q);
  115. void blk_queue_bypass_end(struct request_queue *q);
  116. void __blk_queue_free_tags(struct request_queue *q);
  117. void blk_freeze_queue(struct request_queue *q);
  118. static inline void blk_queue_enter_live(struct request_queue *q)
  119. {
  120. /*
  121. * Given that running in generic_make_request() context
  122. * guarantees that a live reference against q_usage_counter has
  123. * been established, further references under that same context
  124. * need not check that the queue has been frozen (marked dead).
  125. */
  126. percpu_ref_get(&q->q_usage_counter);
  127. }
  128. static inline bool biovec_phys_mergeable(struct request_queue *q,
  129. struct bio_vec *vec1, struct bio_vec *vec2)
  130. {
  131. unsigned long mask = queue_segment_boundary(q);
  132. phys_addr_t addr1 = page_to_phys(vec1->bv_page) + vec1->bv_offset;
  133. phys_addr_t addr2 = page_to_phys(vec2->bv_page) + vec2->bv_offset;
  134. if (addr1 + vec1->bv_len != addr2)
  135. return false;
  136. if (xen_domain() && !xen_biovec_phys_mergeable(vec1, vec2))
  137. return false;
  138. if ((addr1 | mask) != ((addr2 + vec2->bv_len - 1) | mask))
  139. return false;
  140. return true;
  141. }
  142. static inline bool __bvec_gap_to_prev(struct request_queue *q,
  143. struct bio_vec *bprv, unsigned int offset)
  144. {
  145. return (offset & queue_virt_boundary(q)) ||
  146. ((bprv->bv_offset + bprv->bv_len) & queue_virt_boundary(q));
  147. }
  148. /*
  149. * Check if adding a bio_vec after bprv with offset would create a gap in
  150. * the SG list. Most drivers don't care about this, but some do.
  151. */
  152. static inline bool bvec_gap_to_prev(struct request_queue *q,
  153. struct bio_vec *bprv, unsigned int offset)
  154. {
  155. if (!queue_virt_boundary(q))
  156. return false;
  157. return __bvec_gap_to_prev(q, bprv, offset);
  158. }
  159. #ifdef CONFIG_BLK_DEV_INTEGRITY
  160. void blk_flush_integrity(void);
  161. bool __bio_integrity_endio(struct bio *);
  162. static inline bool bio_integrity_endio(struct bio *bio)
  163. {
  164. if (bio_integrity(bio))
  165. return __bio_integrity_endio(bio);
  166. return true;
  167. }
  168. static inline bool integrity_req_gap_back_merge(struct request *req,
  169. struct bio *next)
  170. {
  171. struct bio_integrity_payload *bip = bio_integrity(req->bio);
  172. struct bio_integrity_payload *bip_next = bio_integrity(next);
  173. return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1],
  174. bip_next->bip_vec[0].bv_offset);
  175. }
  176. static inline bool integrity_req_gap_front_merge(struct request *req,
  177. struct bio *bio)
  178. {
  179. struct bio_integrity_payload *bip = bio_integrity(bio);
  180. struct bio_integrity_payload *bip_next = bio_integrity(req->bio);
  181. return bvec_gap_to_prev(req->q, &bip->bip_vec[bip->bip_vcnt - 1],
  182. bip_next->bip_vec[0].bv_offset);
  183. }
  184. #else /* CONFIG_BLK_DEV_INTEGRITY */
  185. static inline bool integrity_req_gap_back_merge(struct request *req,
  186. struct bio *next)
  187. {
  188. return false;
  189. }
  190. static inline bool integrity_req_gap_front_merge(struct request *req,
  191. struct bio *bio)
  192. {
  193. return false;
  194. }
  195. static inline void blk_flush_integrity(void)
  196. {
  197. }
  198. static inline bool bio_integrity_endio(struct bio *bio)
  199. {
  200. return true;
  201. }
  202. #endif /* CONFIG_BLK_DEV_INTEGRITY */
  203. void blk_timeout_work(struct work_struct *work);
  204. unsigned long blk_rq_timeout(unsigned long timeout);
  205. void blk_add_timer(struct request *req);
  206. void blk_delete_timer(struct request *);
  207. bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
  208. struct bio *bio);
  209. bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
  210. struct bio *bio);
  211. bool bio_attempt_discard_merge(struct request_queue *q, struct request *req,
  212. struct bio *bio);
  213. bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
  214. unsigned int *request_count,
  215. struct request **same_queue_rq);
  216. unsigned int blk_plug_queued_count(struct request_queue *q);
  217. void blk_account_io_start(struct request *req, bool new_io);
  218. void blk_account_io_completion(struct request *req, unsigned int bytes);
  219. void blk_account_io_done(struct request *req, u64 now);
  220. /*
  221. * EH timer and IO completion will both attempt to 'grab' the request, make
  222. * sure that only one of them succeeds. Steal the bottom bit of the
  223. * __deadline field for this.
  224. */
  225. static inline int blk_mark_rq_complete(struct request *rq)
  226. {
  227. return test_and_set_bit(0, &rq->__deadline);
  228. }
  229. static inline void blk_clear_rq_complete(struct request *rq)
  230. {
  231. clear_bit(0, &rq->__deadline);
  232. }
  233. static inline bool blk_rq_is_complete(struct request *rq)
  234. {
  235. return test_bit(0, &rq->__deadline);
  236. }
  237. /*
  238. * Internal elevator interface
  239. */
  240. #define ELV_ON_HASH(rq) ((rq)->rq_flags & RQF_HASHED)
  241. void blk_insert_flush(struct request *rq);
  242. static inline void elv_activate_rq(struct request_queue *q, struct request *rq)
  243. {
  244. struct elevator_queue *e = q->elevator;
  245. if (e->type->ops.sq.elevator_activate_req_fn)
  246. e->type->ops.sq.elevator_activate_req_fn(q, rq);
  247. }
  248. static inline void elv_deactivate_rq(struct request_queue *q, struct request *rq)
  249. {
  250. struct elevator_queue *e = q->elevator;
  251. if (e->type->ops.sq.elevator_deactivate_req_fn)
  252. e->type->ops.sq.elevator_deactivate_req_fn(q, rq);
  253. }
  254. int elevator_init(struct request_queue *);
  255. int elevator_init_mq(struct request_queue *q);
  256. int elevator_switch_mq(struct request_queue *q,
  257. struct elevator_type *new_e);
  258. void elevator_exit(struct request_queue *, struct elevator_queue *);
  259. int elv_register_queue(struct request_queue *q);
  260. void elv_unregister_queue(struct request_queue *q);
  261. struct hd_struct *__disk_get_part(struct gendisk *disk, int partno);
  262. #ifdef CONFIG_FAIL_IO_TIMEOUT
  263. int blk_should_fake_timeout(struct request_queue *);
  264. ssize_t part_timeout_show(struct device *, struct device_attribute *, char *);
  265. ssize_t part_timeout_store(struct device *, struct device_attribute *,
  266. const char *, size_t);
  267. #else
  268. static inline int blk_should_fake_timeout(struct request_queue *q)
  269. {
  270. return 0;
  271. }
  272. #endif
  273. int ll_back_merge_fn(struct request_queue *q, struct request *req,
  274. struct bio *bio);
  275. int ll_front_merge_fn(struct request_queue *q, struct request *req,
  276. struct bio *bio);
  277. struct request *attempt_back_merge(struct request_queue *q, struct request *rq);
  278. struct request *attempt_front_merge(struct request_queue *q, struct request *rq);
  279. int blk_attempt_req_merge(struct request_queue *q, struct request *rq,
  280. struct request *next);
  281. void blk_recalc_rq_segments(struct request *rq);
  282. void blk_rq_set_mixed_merge(struct request *rq);
  283. bool blk_rq_merge_ok(struct request *rq, struct bio *bio);
  284. enum elv_merge blk_try_merge(struct request *rq, struct bio *bio);
  285. void blk_queue_congestion_threshold(struct request_queue *q);
  286. int blk_dev_init(void);
  287. /*
  288. * Return the threshold (number of used requests) at which the queue is
  289. * considered to be congested. It include a little hysteresis to keep the
  290. * context switch rate down.
  291. */
  292. static inline int queue_congestion_on_threshold(struct request_queue *q)
  293. {
  294. return q->nr_congestion_on;
  295. }
  296. /*
  297. * The threshold at which a queue is considered to be uncongested
  298. */
  299. static inline int queue_congestion_off_threshold(struct request_queue *q)
  300. {
  301. return q->nr_congestion_off;
  302. }
  303. extern int blk_update_nr_requests(struct request_queue *, unsigned int);
  304. /*
  305. * Contribute to IO statistics IFF:
  306. *
  307. * a) it's attached to a gendisk, and
  308. * b) the queue had IO stats enabled when this request was started, and
  309. * c) it's a file system request
  310. */
  311. static inline bool blk_do_io_stat(struct request *rq)
  312. {
  313. return rq->rq_disk &&
  314. (rq->rq_flags & RQF_IO_STAT) &&
  315. !blk_rq_is_passthrough(rq);
  316. }
  317. static inline void req_set_nomerge(struct request_queue *q, struct request *req)
  318. {
  319. req->cmd_flags |= REQ_NOMERGE;
  320. if (req == q->last_merge)
  321. q->last_merge = NULL;
  322. }
  323. /*
  324. * Steal a bit from this field for legacy IO path atomic IO marking. Note that
  325. * setting the deadline clears the bottom bit, potentially clearing the
  326. * completed bit. The user has to be OK with this (current ones are fine).
  327. */
  328. static inline void blk_rq_set_deadline(struct request *rq, unsigned long time)
  329. {
  330. rq->__deadline = time & ~0x1UL;
  331. }
  332. static inline unsigned long blk_rq_deadline(struct request *rq)
  333. {
  334. return rq->__deadline & ~0x1UL;
  335. }
  336. /*
  337. * The max size one bio can handle is UINT_MAX becasue bvec_iter.bi_size
  338. * is defined as 'unsigned int', meantime it has to aligned to with logical
  339. * block size which is the minimum accepted unit by hardware.
  340. */
  341. static inline unsigned int bio_allowed_max_sectors(struct request_queue *q)
  342. {
  343. return round_down(UINT_MAX, queue_logical_block_size(q)) >> 9;
  344. }
  345. /*
  346. * Internal io_context interface
  347. */
  348. void get_io_context(struct io_context *ioc);
  349. struct io_cq *ioc_lookup_icq(struct io_context *ioc, struct request_queue *q);
  350. struct io_cq *ioc_create_icq(struct io_context *ioc, struct request_queue *q,
  351. gfp_t gfp_mask);
  352. void ioc_clear_queue(struct request_queue *q);
  353. int create_task_io_context(struct task_struct *task, gfp_t gfp_mask, int node);
  354. /**
  355. * rq_ioc - determine io_context for request allocation
  356. * @bio: request being allocated is for this bio (can be %NULL)
  357. *
  358. * Determine io_context to use for request allocation for @bio. May return
  359. * %NULL if %current->io_context doesn't exist.
  360. */
  361. static inline struct io_context *rq_ioc(struct bio *bio)
  362. {
  363. #ifdef CONFIG_BLK_CGROUP
  364. if (bio && bio->bi_ioc)
  365. return bio->bi_ioc;
  366. #endif
  367. return current->io_context;
  368. }
  369. /**
  370. * create_io_context - try to create task->io_context
  371. * @gfp_mask: allocation mask
  372. * @node: allocation node
  373. *
  374. * If %current->io_context is %NULL, allocate a new io_context and install
  375. * it. Returns the current %current->io_context which may be %NULL if
  376. * allocation failed.
  377. *
  378. * Note that this function can't be called with IRQ disabled because
  379. * task_lock which protects %current->io_context is IRQ-unsafe.
  380. */
  381. static inline struct io_context *create_io_context(gfp_t gfp_mask, int node)
  382. {
  383. WARN_ON_ONCE(irqs_disabled());
  384. if (unlikely(!current->io_context))
  385. create_task_io_context(current, gfp_mask, node);
  386. return current->io_context;
  387. }
  388. /*
  389. * Internal throttling interface
  390. */
  391. #ifdef CONFIG_BLK_DEV_THROTTLING
  392. extern void blk_throtl_drain(struct request_queue *q);
  393. extern int blk_throtl_init(struct request_queue *q);
  394. extern void blk_throtl_exit(struct request_queue *q);
  395. extern void blk_throtl_register_queue(struct request_queue *q);
  396. #else /* CONFIG_BLK_DEV_THROTTLING */
  397. static inline void blk_throtl_drain(struct request_queue *q) { }
  398. static inline int blk_throtl_init(struct request_queue *q) { return 0; }
  399. static inline void blk_throtl_exit(struct request_queue *q) { }
  400. static inline void blk_throtl_register_queue(struct request_queue *q) { }
  401. #endif /* CONFIG_BLK_DEV_THROTTLING */
  402. #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
  403. extern ssize_t blk_throtl_sample_time_show(struct request_queue *q, char *page);
  404. extern ssize_t blk_throtl_sample_time_store(struct request_queue *q,
  405. const char *page, size_t count);
  406. extern void blk_throtl_bio_endio(struct bio *bio);
  407. extern void blk_throtl_stat_add(struct request *rq, u64 time);
  408. #else
  409. static inline void blk_throtl_bio_endio(struct bio *bio) { }
  410. static inline void blk_throtl_stat_add(struct request *rq, u64 time) { }
  411. #endif
  412. #ifdef CONFIG_BOUNCE
  413. extern int init_emergency_isa_pool(void);
  414. extern void blk_queue_bounce(struct request_queue *q, struct bio **bio);
  415. #else
  416. static inline int init_emergency_isa_pool(void)
  417. {
  418. return 0;
  419. }
  420. static inline void blk_queue_bounce(struct request_queue *q, struct bio **bio)
  421. {
  422. }
  423. #endif /* CONFIG_BOUNCE */
  424. extern void blk_drain_queue(struct request_queue *q);
  425. #ifdef CONFIG_BLK_CGROUP_IOLATENCY
  426. extern int blk_iolatency_init(struct request_queue *q);
  427. #else
  428. static inline int blk_iolatency_init(struct request_queue *q) { return 0; }
  429. #endif
  430. struct bio *blk_next_bio(struct bio *bio, unsigned int nr_pages, gfp_t gfp);
  431. #ifdef CONFIG_BLK_DEV_ZONED
  432. void blk_queue_free_zone_bitmaps(struct request_queue *q);
  433. #else
  434. static inline void blk_queue_free_zone_bitmaps(struct request_queue *q) {}
  435. #endif
  436. #endif /* BLK_INTERNAL_H */