blk.h 12 KB

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