13dcf60bcSChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
2a168ee84SJens Axboe /*
3a168ee84SJens Axboe * Copyright (C) 1991, 1992 Linus Torvalds
4a168ee84SJens Axboe * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
5a168ee84SJens Axboe * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
6a168ee84SJens Axboe * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
76728cb0eSJens Axboe * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
86728cb0eSJens Axboe * - July2000
9a168ee84SJens Axboe * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
10a168ee84SJens Axboe */
11a168ee84SJens Axboe
12a168ee84SJens Axboe /*
13a168ee84SJens Axboe * This handles all read/write requests to block devices
14a168ee84SJens Axboe */
15a168ee84SJens Axboe #include <linux/kernel.h>
16a168ee84SJens Axboe #include <linux/module.h>
17a168ee84SJens Axboe #include <linux/bio.h>
18a168ee84SJens Axboe #include <linux/blkdev.h>
1952abca64SAlan Stern #include <linux/blk-pm.h>
20fe45e630SChristoph Hellwig #include <linux/blk-integrity.h>
21a168ee84SJens Axboe #include <linux/highmem.h>
22a168ee84SJens Axboe #include <linux/mm.h>
23cee9a0c4SMatthew Wilcox (Oracle) #include <linux/pagemap.h>
24a168ee84SJens Axboe #include <linux/kernel_stat.h>
25a168ee84SJens Axboe #include <linux/string.h>
26a168ee84SJens Axboe #include <linux/init.h>
27a168ee84SJens Axboe #include <linux/completion.h>
28a168ee84SJens Axboe #include <linux/slab.h>
29a168ee84SJens Axboe #include <linux/swap.h>
30a168ee84SJens Axboe #include <linux/writeback.h>
31a168ee84SJens Axboe #include <linux/task_io_accounting_ops.h>
32a168ee84SJens Axboe #include <linux/fault-inject.h>
3373c10101SJens Axboe #include <linux/list_sort.h>
34e3c78ca5STejun Heo #include <linux/delay.h>
35aaf7c680STejun Heo #include <linux/ratelimit.h>
366c954667SLin Ming #include <linux/pm_runtime.h>
3754d4e6abSMax Gurtovoy #include <linux/t10-pi.h>
3818fbda91SOmar Sandoval #include <linux/debugfs.h>
3930abb3a6SHoward McLauchlan #include <linux/bpf.h>
4082d981d4SChristoph Hellwig #include <linux/part_stat.h>
4171ac860aSMing Lei #include <linux/sched/sysctl.h>
42a892c8d5SSatya Tangirala #include <linux/blk-crypto.h>
4355782138SLi Zefan
4455782138SLi Zefan #define CREATE_TRACE_POINTS
4555782138SLi Zefan #include <trace/events/block.h>
46a168ee84SJens Axboe
478324aa91SJens Axboe #include "blk.h"
482aa7745bSChristoph Hellwig #include "blk-mq-sched.h"
49bca6b067SBart Van Assche #include "blk-pm.h"
50672fdcf0SMing Lei #include "blk-cgroup.h"
51a7b36ee6SJens Axboe #include "blk-throttle.h"
52*c20e89c9SHongyu Jin #include "blk-ioprio.h"
538324aa91SJens Axboe
5418fbda91SOmar Sandoval struct dentry *blk_debugfs_root;
5518fbda91SOmar Sandoval
56d07335e5SMike Snitzer EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
57b0da3f0dSJun'ichi Nomura EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
580a82a8d1SLinus Torvalds EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
593291fa57SKeith Busch EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
60cbae8d45SNeilBrown EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
61b357e4a6SChaitanya Kulkarni EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_insert);
620bfc2455SIngo Molnar
632bd85221SChristoph Hellwig static DEFINE_IDA(blk_queue_ida);
64a73f730dSTejun Heo
65a168ee84SJens Axboe /*
66a168ee84SJens Axboe * For queue allocation
67a168ee84SJens Axboe */
682bd85221SChristoph Hellwig static struct kmem_cache *blk_requestq_cachep;
69a168ee84SJens Axboe
70a168ee84SJens Axboe /*
71a168ee84SJens Axboe * Controlling structure to kblockd
72a168ee84SJens Axboe */
73a168ee84SJens Axboe static struct workqueue_struct *kblockd_workqueue;
74a168ee84SJens Axboe
758814ce8aSBart Van Assche /**
768814ce8aSBart Van Assche * blk_queue_flag_set - atomically set a queue flag
778814ce8aSBart Van Assche * @flag: flag to be set
788814ce8aSBart Van Assche * @q: request queue
798814ce8aSBart Van Assche */
blk_queue_flag_set(unsigned int flag,struct request_queue * q)808814ce8aSBart Van Assche void blk_queue_flag_set(unsigned int flag, struct request_queue *q)
818814ce8aSBart Van Assche {
8257d74df9SChristoph Hellwig set_bit(flag, &q->queue_flags);
838814ce8aSBart Van Assche }
848814ce8aSBart Van Assche EXPORT_SYMBOL(blk_queue_flag_set);
858814ce8aSBart Van Assche
868814ce8aSBart Van Assche /**
878814ce8aSBart Van Assche * blk_queue_flag_clear - atomically clear a queue flag
888814ce8aSBart Van Assche * @flag: flag to be cleared
898814ce8aSBart Van Assche * @q: request queue
908814ce8aSBart Van Assche */
blk_queue_flag_clear(unsigned int flag,struct request_queue * q)918814ce8aSBart Van Assche void blk_queue_flag_clear(unsigned int flag, struct request_queue *q)
928814ce8aSBart Van Assche {
9357d74df9SChristoph Hellwig clear_bit(flag, &q->queue_flags);
948814ce8aSBart Van Assche }
958814ce8aSBart Van Assche EXPORT_SYMBOL(blk_queue_flag_clear);
968814ce8aSBart Van Assche
978814ce8aSBart Van Assche /**
988814ce8aSBart Van Assche * blk_queue_flag_test_and_set - atomically test and set a queue flag
998814ce8aSBart Van Assche * @flag: flag to be set
1008814ce8aSBart Van Assche * @q: request queue
1018814ce8aSBart Van Assche *
1028814ce8aSBart Van Assche * Returns the previous value of @flag - 0 if the flag was not set and 1 if
1038814ce8aSBart Van Assche * the flag was already set.
1048814ce8aSBart Van Assche */
blk_queue_flag_test_and_set(unsigned int flag,struct request_queue * q)1058814ce8aSBart Van Assche bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q)
1068814ce8aSBart Van Assche {
10757d74df9SChristoph Hellwig return test_and_set_bit(flag, &q->queue_flags);
1088814ce8aSBart Van Assche }
1098814ce8aSBart Van Assche EXPORT_SYMBOL_GPL(blk_queue_flag_test_and_set);
1108814ce8aSBart Van Assche
111e47bc4edSChaitanya Kulkarni #define REQ_OP_NAME(name) [REQ_OP_##name] = #name
112e47bc4edSChaitanya Kulkarni static const char *const blk_op_name[] = {
113e47bc4edSChaitanya Kulkarni REQ_OP_NAME(READ),
114e47bc4edSChaitanya Kulkarni REQ_OP_NAME(WRITE),
115e47bc4edSChaitanya Kulkarni REQ_OP_NAME(FLUSH),
116e47bc4edSChaitanya Kulkarni REQ_OP_NAME(DISCARD),
117e47bc4edSChaitanya Kulkarni REQ_OP_NAME(SECURE_ERASE),
118e47bc4edSChaitanya Kulkarni REQ_OP_NAME(ZONE_RESET),
1196e33dbf2SChaitanya Kulkarni REQ_OP_NAME(ZONE_RESET_ALL),
1206c1b1da5SAjay Joshi REQ_OP_NAME(ZONE_OPEN),
1216c1b1da5SAjay Joshi REQ_OP_NAME(ZONE_CLOSE),
1226c1b1da5SAjay Joshi REQ_OP_NAME(ZONE_FINISH),
1230512a75bSKeith Busch REQ_OP_NAME(ZONE_APPEND),
124e47bc4edSChaitanya Kulkarni REQ_OP_NAME(WRITE_ZEROES),
125e47bc4edSChaitanya Kulkarni REQ_OP_NAME(DRV_IN),
126e47bc4edSChaitanya Kulkarni REQ_OP_NAME(DRV_OUT),
127e47bc4edSChaitanya Kulkarni };
128e47bc4edSChaitanya Kulkarni #undef REQ_OP_NAME
129e47bc4edSChaitanya Kulkarni
130e47bc4edSChaitanya Kulkarni /**
131e47bc4edSChaitanya Kulkarni * blk_op_str - Return string XXX in the REQ_OP_XXX.
132e47bc4edSChaitanya Kulkarni * @op: REQ_OP_XXX.
133e47bc4edSChaitanya Kulkarni *
134e47bc4edSChaitanya Kulkarni * Description: Centralize block layer function to convert REQ_OP_XXX into
135e47bc4edSChaitanya Kulkarni * string format. Useful in the debugging and tracing bio or request. For
136e47bc4edSChaitanya Kulkarni * invalid REQ_OP_XXX it returns string "UNKNOWN".
137e47bc4edSChaitanya Kulkarni */
blk_op_str(enum req_op op)13877e7ffd7SBart Van Assche inline const char *blk_op_str(enum req_op op)
139e47bc4edSChaitanya Kulkarni {
140e47bc4edSChaitanya Kulkarni const char *op_str = "UNKNOWN";
141e47bc4edSChaitanya Kulkarni
142e47bc4edSChaitanya Kulkarni if (op < ARRAY_SIZE(blk_op_name) && blk_op_name[op])
143e47bc4edSChaitanya Kulkarni op_str = blk_op_name[op];
144e47bc4edSChaitanya Kulkarni
145e47bc4edSChaitanya Kulkarni return op_str;
146e47bc4edSChaitanya Kulkarni }
147e47bc4edSChaitanya Kulkarni EXPORT_SYMBOL_GPL(blk_op_str);
148e47bc4edSChaitanya Kulkarni
1492a842acaSChristoph Hellwig static const struct {
1502a842acaSChristoph Hellwig int errno;
1512a842acaSChristoph Hellwig const char *name;
1522a842acaSChristoph Hellwig } blk_errors[] = {
1532a842acaSChristoph Hellwig [BLK_STS_OK] = { 0, "" },
1542a842acaSChristoph Hellwig [BLK_STS_NOTSUPP] = { -EOPNOTSUPP, "operation not supported" },
1552a842acaSChristoph Hellwig [BLK_STS_TIMEOUT] = { -ETIMEDOUT, "timeout" },
1562a842acaSChristoph Hellwig [BLK_STS_NOSPC] = { -ENOSPC, "critical space allocation" },
1572a842acaSChristoph Hellwig [BLK_STS_TRANSPORT] = { -ENOLINK, "recoverable transport" },
1582a842acaSChristoph Hellwig [BLK_STS_TARGET] = { -EREMOTEIO, "critical target" },
1597ba15083SMike Christie [BLK_STS_RESV_CONFLICT] = { -EBADE, "reservation conflict" },
1602a842acaSChristoph Hellwig [BLK_STS_MEDIUM] = { -ENODATA, "critical medium" },
1612a842acaSChristoph Hellwig [BLK_STS_PROTECTION] = { -EILSEQ, "protection" },
1622a842acaSChristoph Hellwig [BLK_STS_RESOURCE] = { -ENOMEM, "kernel resource" },
16386ff7c2aSMing Lei [BLK_STS_DEV_RESOURCE] = { -EBUSY, "device resource" },
16403a07c92SGoldwyn Rodrigues [BLK_STS_AGAIN] = { -EAGAIN, "nonblocking retry" },
1657d32c027SSong Liu [BLK_STS_OFFLINE] = { -ENODEV, "device offline" },
1662a842acaSChristoph Hellwig
1674e4cbee9SChristoph Hellwig /* device mapper special case, should not leak out: */
1684e4cbee9SChristoph Hellwig [BLK_STS_DM_REQUEUE] = { -EREMCHG, "dm internal retry" },
1694e4cbee9SChristoph Hellwig
1703b481d91SKeith Busch /* zone device specific errors */
1713b481d91SKeith Busch [BLK_STS_ZONE_OPEN_RESOURCE] = { -ETOOMANYREFS, "open zones exceeded" },
1723b481d91SKeith Busch [BLK_STS_ZONE_ACTIVE_RESOURCE] = { -EOVERFLOW, "active zones exceeded" },
1733b481d91SKeith Busch
174dffc480dSDamien Le Moal /* Command duration limit device-side timeout */
175dffc480dSDamien Le Moal [BLK_STS_DURATION_LIMIT] = { -ETIME, "duration limit exceeded" },
176dffc480dSDamien Le Moal
1772a842acaSChristoph Hellwig /* everything else not covered above: */
1782a842acaSChristoph Hellwig [BLK_STS_IOERR] = { -EIO, "I/O" },
1792a842acaSChristoph Hellwig };
1802a842acaSChristoph Hellwig
errno_to_blk_status(int errno)1812a842acaSChristoph Hellwig blk_status_t errno_to_blk_status(int errno)
1822a842acaSChristoph Hellwig {
1832a842acaSChristoph Hellwig int i;
1842a842acaSChristoph Hellwig
1852a842acaSChristoph Hellwig for (i = 0; i < ARRAY_SIZE(blk_errors); i++) {
1862a842acaSChristoph Hellwig if (blk_errors[i].errno == errno)
1872a842acaSChristoph Hellwig return (__force blk_status_t)i;
1882a842acaSChristoph Hellwig }
1892a842acaSChristoph Hellwig
1902a842acaSChristoph Hellwig return BLK_STS_IOERR;
1912a842acaSChristoph Hellwig }
1922a842acaSChristoph Hellwig EXPORT_SYMBOL_GPL(errno_to_blk_status);
1932a842acaSChristoph Hellwig
blk_status_to_errno(blk_status_t status)1942a842acaSChristoph Hellwig int blk_status_to_errno(blk_status_t status)
1952a842acaSChristoph Hellwig {
1962a842acaSChristoph Hellwig int idx = (__force int)status;
1972a842acaSChristoph Hellwig
19834bd9c1cSBart Van Assche if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
1992a842acaSChristoph Hellwig return -EIO;
2002a842acaSChristoph Hellwig return blk_errors[idx].errno;
2012a842acaSChristoph Hellwig }
2022a842acaSChristoph Hellwig EXPORT_SYMBOL_GPL(blk_status_to_errno);
2032a842acaSChristoph Hellwig
blk_status_to_str(blk_status_t status)2040d7a29a2SChristoph Hellwig const char *blk_status_to_str(blk_status_t status)
2052a842acaSChristoph Hellwig {
2062a842acaSChristoph Hellwig int idx = (__force int)status;
2072a842acaSChristoph Hellwig
20834bd9c1cSBart Van Assche if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
2090d7a29a2SChristoph Hellwig return "<null>";
2100d7a29a2SChristoph Hellwig return blk_errors[idx].name;
2112a842acaSChristoph Hellwig }
2127ba37927SKent Overstreet EXPORT_SYMBOL_GPL(blk_status_to_str);
2132a842acaSChristoph Hellwig
214a168ee84SJens Axboe /**
215a168ee84SJens Axboe * blk_sync_queue - cancel any pending callbacks on a queue
216a168ee84SJens Axboe * @q: the queue
217a168ee84SJens Axboe *
218a168ee84SJens Axboe * Description:
219a168ee84SJens Axboe * The block layer may perform asynchronous callback activity
220a168ee84SJens Axboe * on a queue, such as calling the unplug function after a timeout.
221a168ee84SJens Axboe * A block device may call blk_sync_queue to ensure that any
222a168ee84SJens Axboe * such activity is cancelled, thus allowing it to release resources
223a168ee84SJens Axboe * that the callbacks might use. The caller must already have made sure
224c62b37d9SChristoph Hellwig * that its ->submit_bio will not re-add plugging prior to calling
225a168ee84SJens Axboe * this function.
226a168ee84SJens Axboe *
227da527770SVivek Goyal * This function does not cancel any asynchronous activity arising
228da3dae54SMasanari Iida * out of elevator or throttling code. That would require elevator_exit()
2295efd6113STejun Heo * and blkcg_exit_queue() to be called with queue lock initialized.
230da527770SVivek Goyal *
231a168ee84SJens Axboe */
blk_sync_queue(struct request_queue * q)232a168ee84SJens Axboe void blk_sync_queue(struct request_queue *q)
233a168ee84SJens Axboe {
23470ed28b9SJens Axboe del_timer_sync(&q->timeout);
2354e9b6f20SBart Van Assche cancel_work_sync(&q->timeout_work);
236f04c1fe7SMing Lei }
237a168ee84SJens Axboe EXPORT_SYMBOL(blk_sync_queue);
238a168ee84SJens Axboe
239a168ee84SJens Axboe /**
240cd84a62eSBart Van Assche * blk_set_pm_only - increment pm_only counter
241c9254f2dSBart Van Assche * @q: request queue pointer
242c9254f2dSBart Van Assche */
blk_set_pm_only(struct request_queue * q)243cd84a62eSBart Van Assche void blk_set_pm_only(struct request_queue *q)
244c9254f2dSBart Van Assche {
245cd84a62eSBart Van Assche atomic_inc(&q->pm_only);
246c9254f2dSBart Van Assche }
247cd84a62eSBart Van Assche EXPORT_SYMBOL_GPL(blk_set_pm_only);
248c9254f2dSBart Van Assche
blk_clear_pm_only(struct request_queue * q)249cd84a62eSBart Van Assche void blk_clear_pm_only(struct request_queue *q)
250c9254f2dSBart Van Assche {
251cd84a62eSBart Van Assche int pm_only;
252cd84a62eSBart Van Assche
253cd84a62eSBart Van Assche pm_only = atomic_dec_return(&q->pm_only);
254cd84a62eSBart Van Assche WARN_ON_ONCE(pm_only < 0);
255cd84a62eSBart Van Assche if (pm_only == 0)
2563a0a5299SBart Van Assche wake_up_all(&q->mq_freeze_wq);
257c9254f2dSBart Van Assche }
258cd84a62eSBart Van Assche EXPORT_SYMBOL_GPL(blk_clear_pm_only);
259c9254f2dSBart Van Assche
blk_free_queue_rcu(struct rcu_head * rcu_head)2602bd85221SChristoph Hellwig static void blk_free_queue_rcu(struct rcu_head *rcu_head)
2612bd85221SChristoph Hellwig {
262d36a9ea5SMing Lei struct request_queue *q = container_of(rcu_head,
263d36a9ea5SMing Lei struct request_queue, rcu_head);
264d36a9ea5SMing Lei
265d36a9ea5SMing Lei percpu_ref_exit(&q->q_usage_counter);
266d36a9ea5SMing Lei kmem_cache_free(blk_requestq_cachep, q);
2672bd85221SChristoph Hellwig }
2682bd85221SChristoph Hellwig
blk_free_queue(struct request_queue * q)2692bd85221SChristoph Hellwig static void blk_free_queue(struct request_queue *q)
2702bd85221SChristoph Hellwig {
2712bd85221SChristoph Hellwig blk_free_queue_stats(q->stats);
2722bd85221SChristoph Hellwig if (queue_is_mq(q))
2732bd85221SChristoph Hellwig blk_mq_release(q);
2742bd85221SChristoph Hellwig
2752bd85221SChristoph Hellwig ida_free(&blk_queue_ida, q->id);
2762bd85221SChristoph Hellwig call_rcu(&q->rcu_head, blk_free_queue_rcu);
2772bd85221SChristoph Hellwig }
2782bd85221SChristoph Hellwig
279b5bd357cSLuis Chamberlain /**
280b5bd357cSLuis Chamberlain * blk_put_queue - decrement the request_queue refcount
281b5bd357cSLuis Chamberlain * @q: the request_queue structure to decrement the refcount for
282b5bd357cSLuis Chamberlain *
2832bd85221SChristoph Hellwig * Decrements the refcount of the request_queue and free it when the refcount
2842bd85221SChristoph Hellwig * reaches 0.
285b5bd357cSLuis Chamberlain */
blk_put_queue(struct request_queue * q)286a168ee84SJens Axboe void blk_put_queue(struct request_queue *q)
287a168ee84SJens Axboe {
2882bd85221SChristoph Hellwig if (refcount_dec_and_test(&q->refs))
2892bd85221SChristoph Hellwig blk_free_queue(q);
290a168ee84SJens Axboe }
291d86e0e83SJens Axboe EXPORT_SYMBOL(blk_put_queue);
292a168ee84SJens Axboe
blk_queue_start_drain(struct request_queue * q)2938e141f9eSChristoph Hellwig void blk_queue_start_drain(struct request_queue *q)
294aed3ea94SJens Axboe {
295d3cfb2a0SMing Lei /*
296d3cfb2a0SMing Lei * When queue DYING flag is set, we need to block new req
297d3cfb2a0SMing Lei * entering queue, so we call blk_freeze_queue_start() to
298d3cfb2a0SMing Lei * prevent I/O from crossing blk_queue_enter().
299d3cfb2a0SMing Lei */
300d3cfb2a0SMing Lei blk_freeze_queue_start(q);
301344e9ffcSJens Axboe if (queue_is_mq(q))
302aed3ea94SJens Axboe blk_mq_wake_waiters(q);
303055f6e18SMing Lei /* Make blk_queue_enter() reexamine the DYING flag. */
304055f6e18SMing Lei wake_up_all(&q->mq_freeze_wq);
305aed3ea94SJens Axboe }
3068e141f9eSChristoph Hellwig
307d732580bSTejun Heo /**
3083a0a5299SBart Van Assche * blk_queue_enter() - try to increase q->q_usage_counter
3093a0a5299SBart Van Assche * @q: request queue pointer
310a4d34da7SBart Van Assche * @flags: BLK_MQ_REQ_NOWAIT and/or BLK_MQ_REQ_PM
3113a0a5299SBart Van Assche */
blk_queue_enter(struct request_queue * q,blk_mq_req_flags_t flags)3129a95e4efSBart Van Assche int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
3133ef28e83SDan Williams {
314a4d34da7SBart Van Assche const bool pm = flags & BLK_MQ_REQ_PM;
3153a0a5299SBart Van Assche
3161f14a098SChristoph Hellwig while (!blk_try_enter_queue(q, pm)) {
3173a0a5299SBart Van Assche if (flags & BLK_MQ_REQ_NOWAIT)
31856f99b8dSStefan Roesch return -EAGAIN;
3193ef28e83SDan Williams
3205ed61d3fSMing Lei /*
3211f14a098SChristoph Hellwig * read pair of barrier in blk_freeze_queue_start(), we need to
3221f14a098SChristoph Hellwig * order reading __PERCPU_REF_DEAD flag of .q_usage_counter and
3231f14a098SChristoph Hellwig * reading .mq_freeze_depth or queue dying flag, otherwise the
3241f14a098SChristoph Hellwig * following wait may never return if the two reads are
3251f14a098SChristoph Hellwig * reordered.
3265ed61d3fSMing Lei */
3275ed61d3fSMing Lei smp_rmb();
3281dc3039bSAlan Jenkins wait_event(q->mq_freeze_wq,
3297996a8b5SBob Liu (!q->mq_freeze_depth &&
33052abca64SAlan Stern blk_pm_resume_queue(pm, q)) ||
3313ef28e83SDan Williams blk_queue_dying(q));
3323ef28e83SDan Williams if (blk_queue_dying(q))
3333ef28e83SDan Williams return -ENODEV;
3343ef28e83SDan Williams }
3351f14a098SChristoph Hellwig
3361f14a098SChristoph Hellwig return 0;
3373ef28e83SDan Williams }
3383ef28e83SDan Williams
__bio_queue_enter(struct request_queue * q,struct bio * bio)339c98cb5bbSJens Axboe int __bio_queue_enter(struct request_queue *q, struct bio *bio)
340accea322SChristoph Hellwig {
341a6741536SChristoph Hellwig while (!blk_try_enter_queue(q, false)) {
342eab4e027SPavel Begunkov struct gendisk *disk = bio->bi_bdev->bd_disk;
343eab4e027SPavel Begunkov
344a6741536SChristoph Hellwig if (bio->bi_opf & REQ_NOWAIT) {
3458e141f9eSChristoph Hellwig if (test_bit(GD_DEAD, &disk->state))
346a6741536SChristoph Hellwig goto dead;
347accea322SChristoph Hellwig bio_wouldblock_error(bio);
34856f99b8dSStefan Roesch return -EAGAIN;
349accea322SChristoph Hellwig }
350accea322SChristoph Hellwig
351a6741536SChristoph Hellwig /*
352a6741536SChristoph Hellwig * read pair of barrier in blk_freeze_queue_start(), we need to
353a6741536SChristoph Hellwig * order reading __PERCPU_REF_DEAD flag of .q_usage_counter and
354a6741536SChristoph Hellwig * reading .mq_freeze_depth or queue dying flag, otherwise the
355a6741536SChristoph Hellwig * following wait may never return if the two reads are
356a6741536SChristoph Hellwig * reordered.
357a6741536SChristoph Hellwig */
358a6741536SChristoph Hellwig smp_rmb();
359a6741536SChristoph Hellwig wait_event(q->mq_freeze_wq,
360a6741536SChristoph Hellwig (!q->mq_freeze_depth &&
361a6741536SChristoph Hellwig blk_pm_resume_queue(false, q)) ||
3628e141f9eSChristoph Hellwig test_bit(GD_DEAD, &disk->state));
3638e141f9eSChristoph Hellwig if (test_bit(GD_DEAD, &disk->state))
364a6741536SChristoph Hellwig goto dead;
365a6741536SChristoph Hellwig }
366a6741536SChristoph Hellwig
367a6741536SChristoph Hellwig return 0;
368a6741536SChristoph Hellwig dead:
369a6741536SChristoph Hellwig bio_io_error(bio);
370a6741536SChristoph Hellwig return -ENODEV;
371accea322SChristoph Hellwig }
372accea322SChristoph Hellwig
blk_queue_exit(struct request_queue * q)3733ef28e83SDan Williams void blk_queue_exit(struct request_queue *q)
3743ef28e83SDan Williams {
3753ef28e83SDan Williams percpu_ref_put(&q->q_usage_counter);
3763ef28e83SDan Williams }
3773ef28e83SDan Williams
blk_queue_usage_counter_release(struct percpu_ref * ref)3783ef28e83SDan Williams static void blk_queue_usage_counter_release(struct percpu_ref *ref)
3793ef28e83SDan Williams {
3803ef28e83SDan Williams struct request_queue *q =
3813ef28e83SDan Williams container_of(ref, struct request_queue, q_usage_counter);
3823ef28e83SDan Williams
3833ef28e83SDan Williams wake_up_all(&q->mq_freeze_wq);
3843ef28e83SDan Williams }
3853ef28e83SDan Williams
blk_rq_timed_out_timer(struct timer_list * t)386bca237a5SKees Cook static void blk_rq_timed_out_timer(struct timer_list *t)
387287922ebSChristoph Hellwig {
388bca237a5SKees Cook struct request_queue *q = from_timer(q, t, timeout);
389287922ebSChristoph Hellwig
390287922ebSChristoph Hellwig kblockd_schedule_work(&q->timeout_work);
391287922ebSChristoph Hellwig }
392287922ebSChristoph Hellwig
blk_timeout_work(struct work_struct * work)3932e3c18d0STetsuo Handa static void blk_timeout_work(struct work_struct *work)
3942e3c18d0STetsuo Handa {
3952e3c18d0STetsuo Handa }
3962e3c18d0STetsuo Handa
blk_alloc_queue(int node_id)39780bd4a7aSChristoph Hellwig struct request_queue *blk_alloc_queue(int node_id)
398a168ee84SJens Axboe {
399a168ee84SJens Axboe struct request_queue *q;
400a168ee84SJens Axboe
40180bd4a7aSChristoph Hellwig q = kmem_cache_alloc_node(blk_requestq_cachep, GFP_KERNEL | __GFP_ZERO,
40280bd4a7aSChristoph Hellwig node_id);
403a168ee84SJens Axboe if (!q)
404a168ee84SJens Axboe return NULL;
405a168ee84SJens Axboe
406cbf62af3SChristoph Hellwig q->last_merge = NULL;
407cbf62af3SChristoph Hellwig
408798f2a6fSBo Liu q->id = ida_alloc(&blk_queue_ida, GFP_KERNEL);
409a73f730dSTejun Heo if (q->id < 0)
41080bd4a7aSChristoph Hellwig goto fail_q;
411a73f730dSTejun Heo
412a83b576cSJens Axboe q->stats = blk_alloc_queue_stats();
413a83b576cSJens Axboe if (!q->stats)
41446754bd0SChristoph Hellwig goto fail_id;
415a83b576cSJens Axboe
4165151412dSMike Snitzer q->node = node_id;
4170989a025SJens Axboe
418079a2e3eSJohn Garry atomic_set(&q->nr_active_requests_shared_tags, 0);
419bccf5e26SJohn Garry
420bca237a5SKees Cook timer_setup(&q->timeout, blk_rq_timed_out_timer, 0);
4212e3c18d0STetsuo Handa INIT_WORK(&q->timeout_work, blk_timeout_work);
422a612fddfSTejun Heo INIT_LIST_HEAD(&q->icq_list);
423a168ee84SJens Axboe
4242bd85221SChristoph Hellwig refcount_set(&q->refs, 1);
42585e0cbbbSLuis Chamberlain mutex_init(&q->debugfs_mutex);
426a168ee84SJens Axboe mutex_init(&q->sysfs_lock);
427cecf5d87SMing Lei mutex_init(&q->sysfs_dir_lock);
428a13bd91bSYu Kuai mutex_init(&q->rq_qos_mutex);
4290d945c1fSChristoph Hellwig spin_lock_init(&q->queue_lock);
430c94a96acSVivek Goyal
431320ae51fSJens Axboe init_waitqueue_head(&q->mq_freeze_wq);
4327996a8b5SBob Liu mutex_init(&q->mq_freeze_lock);
433320ae51fSJens Axboe
434740ffad9SMing Lei blkg_init_queue(q);
435740ffad9SMing Lei
4363ef28e83SDan Williams /*
4373ef28e83SDan Williams * Init percpu_ref in atomic mode so that it's faster to shutdown.
4383ef28e83SDan Williams * See blk_register_queue() for details.
4393ef28e83SDan Williams */
4403ef28e83SDan Williams if (percpu_ref_init(&q->q_usage_counter,
4413ef28e83SDan Williams blk_queue_usage_counter_release,
4423ef28e83SDan Williams PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
443edb0872fSChristoph Hellwig goto fail_stats;
444f51b802cSTejun Heo
4453d745ea5SChristoph Hellwig blk_set_default_limits(&q->limits);
446d2a27964SJohn Garry q->nr_requests = BLKDEV_DEFAULT_RQ;
4473d745ea5SChristoph Hellwig
448a168ee84SJens Axboe return q;
449a73f730dSTejun Heo
450a83b576cSJens Axboe fail_stats:
451edb0872fSChristoph Hellwig blk_free_queue_stats(q->stats);
452a73f730dSTejun Heo fail_id:
453798f2a6fSBo Liu ida_free(&blk_queue_ida, q->id);
454a73f730dSTejun Heo fail_q:
45580bd4a7aSChristoph Hellwig kmem_cache_free(blk_requestq_cachep, q);
456a73f730dSTejun Heo return NULL;
457a168ee84SJens Axboe }
458a168ee84SJens Axboe
459b5bd357cSLuis Chamberlain /**
460b5bd357cSLuis Chamberlain * blk_get_queue - increment the request_queue refcount
461b5bd357cSLuis Chamberlain * @q: the request_queue structure to increment the refcount for
462b5bd357cSLuis Chamberlain *
463b5bd357cSLuis Chamberlain * Increment the refcount of the request_queue kobject.
464763b5892SLuis Chamberlain *
465763b5892SLuis Chamberlain * Context: Any context.
466b5bd357cSLuis Chamberlain */
blk_get_queue(struct request_queue * q)46709ac46c4STejun Heo bool blk_get_queue(struct request_queue *q)
468a168ee84SJens Axboe {
469828b5f01SChristoph Hellwig if (unlikely(blk_queue_dying(q)))
47009ac46c4STejun Heo return false;
4712bd85221SChristoph Hellwig refcount_inc(&q->refs);
472828b5f01SChristoph Hellwig return true;
473a168ee84SJens Axboe }
474d86e0e83SJens Axboe EXPORT_SYMBOL(blk_get_queue);
475a168ee84SJens Axboe
476a168ee84SJens Axboe #ifdef CONFIG_FAIL_MAKE_REQUEST
477a168ee84SJens Axboe
478a168ee84SJens Axboe static DECLARE_FAULT_ATTR(fail_make_request);
479a168ee84SJens Axboe
setup_fail_make_request(char * str)480a168ee84SJens Axboe static int __init setup_fail_make_request(char *str)
481a168ee84SJens Axboe {
482a168ee84SJens Axboe return setup_fault_attr(&fail_make_request, str);
483a168ee84SJens Axboe }
484a168ee84SJens Axboe __setup("fail_make_request=", setup_fail_make_request);
485a168ee84SJens Axboe
should_fail_request(struct block_device * part,unsigned int bytes)48606c8c691SChristoph Hellwig bool should_fail_request(struct block_device *part, unsigned int bytes)
487a168ee84SJens Axboe {
4888446fe92SChristoph Hellwig return part->bd_make_it_fail && should_fail(&fail_make_request, bytes);
489a168ee84SJens Axboe }
490a168ee84SJens Axboe
fail_make_request_debugfs(void)491a168ee84SJens Axboe static int __init fail_make_request_debugfs(void)
492a168ee84SJens Axboe {
493dd48c085SAkinobu Mita struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
494dd48c085SAkinobu Mita NULL, &fail_make_request);
495dd48c085SAkinobu Mita
49621f9fcd8SDuan Jiong return PTR_ERR_OR_ZERO(dir);
497a168ee84SJens Axboe }
498a168ee84SJens Axboe
499a168ee84SJens Axboe late_initcall(fail_make_request_debugfs);
500a168ee84SJens Axboe #endif /* CONFIG_FAIL_MAKE_REQUEST */
501a168ee84SJens Axboe
bio_check_ro(struct bio * bio)502bdb7d420SMiaohe Lin static inline void bio_check_ro(struct bio *bio)
503721c7fc7SIlya Dryomov {
5042f9f6221SChristoph Hellwig if (op_is_write(bio_op(bio)) && bdev_read_only(bio->bi_bdev)) {
5058b2ded1cSMikulas Patocka if (op_is_flush(bio->bi_opf) && !bio_sectors(bio))
506bdb7d420SMiaohe Lin return;
507da29e401SYu Kuai
508da29e401SYu Kuai if (bio->bi_bdev->bd_ro_warned)
509da29e401SYu Kuai return;
510da29e401SYu Kuai
511da29e401SYu Kuai bio->bi_bdev->bd_ro_warned = true;
512da29e401SYu Kuai /*
513da29e401SYu Kuai * Use ioctl to set underlying disk of raid/dm to read-only
514da29e401SYu Kuai * will trigger this.
515da29e401SYu Kuai */
516da29e401SYu Kuai pr_warn("Trying to write to read-only block-device %pg\n",
51757e95e46SChristoph Hellwig bio->bi_bdev);
518721c7fc7SIlya Dryomov }
519721c7fc7SIlya Dryomov }
520721c7fc7SIlya Dryomov
should_fail_bio(struct bio * bio)52130abb3a6SHoward McLauchlan static noinline int should_fail_bio(struct bio *bio)
52230abb3a6SHoward McLauchlan {
523309dca30SChristoph Hellwig if (should_fail_request(bdev_whole(bio->bi_bdev), bio->bi_iter.bi_size))
52430abb3a6SHoward McLauchlan return -EIO;
52530abb3a6SHoward McLauchlan return 0;
52630abb3a6SHoward McLauchlan }
52730abb3a6SHoward McLauchlan ALLOW_ERROR_INJECTION(should_fail_bio, ERRNO);
52830abb3a6SHoward McLauchlan
529a168ee84SJens Axboe /*
53052c5e62dSChristoph Hellwig * Check whether this bio extends beyond the end of the device or partition.
53152c5e62dSChristoph Hellwig * This may well happen - the kernel calls bread() without checking the size of
53252c5e62dSChristoph Hellwig * the device, e.g., when mounting a file system.
53352c5e62dSChristoph Hellwig */
bio_check_eod(struct bio * bio)5342f9f6221SChristoph Hellwig static inline int bio_check_eod(struct bio *bio)
53552c5e62dSChristoph Hellwig {
5362f9f6221SChristoph Hellwig sector_t maxsector = bdev_nr_sectors(bio->bi_bdev);
53752c5e62dSChristoph Hellwig unsigned int nr_sectors = bio_sectors(bio);
53852c5e62dSChristoph Hellwig
5393eb96946SChristoph Hellwig if (nr_sectors &&
54052c5e62dSChristoph Hellwig (nr_sectors > maxsector ||
54152c5e62dSChristoph Hellwig bio->bi_iter.bi_sector > maxsector - nr_sectors)) {
542ad740780SChristoph Hellwig pr_info_ratelimited("%s: attempt to access beyond end of device\n"
543069adbacSChristoph Hellwig "%pg: rw=%d, sector=%llu, nr_sectors = %u limit=%llu\n",
544069adbacSChristoph Hellwig current->comm, bio->bi_bdev, bio->bi_opf,
545069adbacSChristoph Hellwig bio->bi_iter.bi_sector, nr_sectors, maxsector);
54652c5e62dSChristoph Hellwig return -EIO;
54752c5e62dSChristoph Hellwig }
54852c5e62dSChristoph Hellwig return 0;
54952c5e62dSChristoph Hellwig }
55052c5e62dSChristoph Hellwig
55152c5e62dSChristoph Hellwig /*
55274d46992SChristoph Hellwig * Remap block n of partition p to block n+start(p) of the disk.
55374d46992SChristoph Hellwig */
blk_partition_remap(struct bio * bio)5542f9f6221SChristoph Hellwig static int blk_partition_remap(struct bio *bio)
55574d46992SChristoph Hellwig {
556309dca30SChristoph Hellwig struct block_device *p = bio->bi_bdev;
55774d46992SChristoph Hellwig
55852c5e62dSChristoph Hellwig if (unlikely(should_fail_request(p, bio->bi_iter.bi_size)))
5592f9f6221SChristoph Hellwig return -EIO;
5605eac3eb3SDamien Le Moal if (bio_sectors(bio)) {
5618446fe92SChristoph Hellwig bio->bi_iter.bi_sector += p->bd_start_sect;
5621c02fca6SChristoph Hellwig trace_block_bio_remap(bio, p->bd_dev,
56329ff57c6SChristoph Hellwig bio->bi_iter.bi_sector -
5648446fe92SChristoph Hellwig p->bd_start_sect);
56552c5e62dSChristoph Hellwig }
56630c5d345SChristoph Hellwig bio_set_flag(bio, BIO_REMAPPED);
5672f9f6221SChristoph Hellwig return 0;
56874d46992SChristoph Hellwig }
56974d46992SChristoph Hellwig
5700512a75bSKeith Busch /*
5710512a75bSKeith Busch * Check write append to a zoned block device.
5720512a75bSKeith Busch */
blk_check_zone_append(struct request_queue * q,struct bio * bio)5730512a75bSKeith Busch static inline blk_status_t blk_check_zone_append(struct request_queue *q,
5740512a75bSKeith Busch struct bio *bio)
5750512a75bSKeith Busch {
5760512a75bSKeith Busch int nr_sectors = bio_sectors(bio);
5770512a75bSKeith Busch
5780512a75bSKeith Busch /* Only applicable to zoned block devices */
579edd1dbc8SChristoph Hellwig if (!bdev_is_zoned(bio->bi_bdev))
5800512a75bSKeith Busch return BLK_STS_NOTSUPP;
5810512a75bSKeith Busch
5820512a75bSKeith Busch /* The bio sector must point to the start of a sequential zone */
583e29b2100SPankaj Raghav if (!bdev_is_zone_start(bio->bi_bdev, bio->bi_iter.bi_sector) ||
584052e545cSChristoph Hellwig !bio_zone_is_seq(bio))
5850512a75bSKeith Busch return BLK_STS_IOERR;
5860512a75bSKeith Busch
5870512a75bSKeith Busch /*
5880512a75bSKeith Busch * Not allowed to cross zone boundaries. Otherwise, the BIO will be
5890512a75bSKeith Busch * split and could result in non-contiguous sectors being written in
5900512a75bSKeith Busch * different zones.
5910512a75bSKeith Busch */
5920512a75bSKeith Busch if (nr_sectors > q->limits.chunk_sectors)
5930512a75bSKeith Busch return BLK_STS_IOERR;
5940512a75bSKeith Busch
5950512a75bSKeith Busch /* Make sure the BIO is small enough and will not get split */
5960512a75bSKeith Busch if (nr_sectors > q->limits.max_zone_append_sectors)
5970512a75bSKeith Busch return BLK_STS_IOERR;
5980512a75bSKeith Busch
5990512a75bSKeith Busch bio->bi_opf |= REQ_NOMERGE;
6000512a75bSKeith Busch
6010512a75bSKeith Busch return BLK_STS_OK;
6020512a75bSKeith Busch }
6030512a75bSKeith Busch
__submit_bio(struct bio * bio)6043e08773cSChristoph Hellwig static void __submit_bio(struct bio *bio)
605ac7c5675SChristoph Hellwig {
6067f36b7d0SMing Lei if (unlikely(!blk_crypto_bio_prep(&bio)))
6077f36b7d0SMing Lei return;
6087f36b7d0SMing Lei
6099f4107b0SJens Axboe if (!bio->bi_bdev->bd_has_submit_bio) {
6103e08773cSChristoph Hellwig blk_mq_submit_bio(bio);
6117f36b7d0SMing Lei } else if (likely(bio_queue_enter(bio) == 0)) {
6129f4107b0SJens Axboe struct gendisk *disk = bio->bi_bdev->bd_disk;
6139f4107b0SJens Axboe
6147f36b7d0SMing Lei disk->fops->submit_bio(bio);
6157f36b7d0SMing Lei blk_queue_exit(disk->queue);
6167f36b7d0SMing Lei }
617ac7c5675SChristoph Hellwig }
618ac7c5675SChristoph Hellwig
619566acf2dSChristoph Hellwig /*
620566acf2dSChristoph Hellwig * The loop in this function may be a bit non-obvious, and so deserves some
621566acf2dSChristoph Hellwig * explanation:
622566acf2dSChristoph Hellwig *
623566acf2dSChristoph Hellwig * - Before entering the loop, bio->bi_next is NULL (as all callers ensure
624566acf2dSChristoph Hellwig * that), so we have a list with a single bio.
625566acf2dSChristoph Hellwig * - We pretend that we have just taken it off a longer list, so we assign
626566acf2dSChristoph Hellwig * bio_list to a pointer to the bio_list_on_stack, thus initialising the
627566acf2dSChristoph Hellwig * bio_list of new bios to be added. ->submit_bio() may indeed add some more
628566acf2dSChristoph Hellwig * bios through a recursive call to submit_bio_noacct. If it did, we find a
629566acf2dSChristoph Hellwig * non-NULL value in bio_list and re-enter the loop from the top.
630566acf2dSChristoph Hellwig * - In this case we really did just take the bio of the top of the list (no
631566acf2dSChristoph Hellwig * pretending) and so remove it from bio_list, and call into ->submit_bio()
632566acf2dSChristoph Hellwig * again.
633566acf2dSChristoph Hellwig *
634566acf2dSChristoph Hellwig * bio_list_on_stack[0] contains bios submitted by the current ->submit_bio.
635566acf2dSChristoph Hellwig * bio_list_on_stack[1] contains bios that were submitted before the current
63669fe0f29SMing Lei * ->submit_bio, but that haven't been processed yet.
637566acf2dSChristoph Hellwig */
__submit_bio_noacct(struct bio * bio)6383e08773cSChristoph Hellwig static void __submit_bio_noacct(struct bio *bio)
639566acf2dSChristoph Hellwig {
640566acf2dSChristoph Hellwig struct bio_list bio_list_on_stack[2];
641566acf2dSChristoph Hellwig
642566acf2dSChristoph Hellwig BUG_ON(bio->bi_next);
643566acf2dSChristoph Hellwig
644566acf2dSChristoph Hellwig bio_list_init(&bio_list_on_stack[0]);
645566acf2dSChristoph Hellwig current->bio_list = bio_list_on_stack;
646566acf2dSChristoph Hellwig
647566acf2dSChristoph Hellwig do {
648eab4e027SPavel Begunkov struct request_queue *q = bdev_get_queue(bio->bi_bdev);
649566acf2dSChristoph Hellwig struct bio_list lower, same;
650566acf2dSChristoph Hellwig
651566acf2dSChristoph Hellwig /*
652566acf2dSChristoph Hellwig * Create a fresh bio_list for all subordinate requests.
653566acf2dSChristoph Hellwig */
654566acf2dSChristoph Hellwig bio_list_on_stack[1] = bio_list_on_stack[0];
655566acf2dSChristoph Hellwig bio_list_init(&bio_list_on_stack[0]);
656566acf2dSChristoph Hellwig
6573e08773cSChristoph Hellwig __submit_bio(bio);
658566acf2dSChristoph Hellwig
659566acf2dSChristoph Hellwig /*
660566acf2dSChristoph Hellwig * Sort new bios into those for a lower level and those for the
661566acf2dSChristoph Hellwig * same level.
662566acf2dSChristoph Hellwig */
663566acf2dSChristoph Hellwig bio_list_init(&lower);
664566acf2dSChristoph Hellwig bio_list_init(&same);
665566acf2dSChristoph Hellwig while ((bio = bio_list_pop(&bio_list_on_stack[0])) != NULL)
666eab4e027SPavel Begunkov if (q == bdev_get_queue(bio->bi_bdev))
667566acf2dSChristoph Hellwig bio_list_add(&same, bio);
668566acf2dSChristoph Hellwig else
669566acf2dSChristoph Hellwig bio_list_add(&lower, bio);
670566acf2dSChristoph Hellwig
671566acf2dSChristoph Hellwig /*
672566acf2dSChristoph Hellwig * Now assemble so we handle the lowest level first.
673566acf2dSChristoph Hellwig */
674566acf2dSChristoph Hellwig bio_list_merge(&bio_list_on_stack[0], &lower);
675566acf2dSChristoph Hellwig bio_list_merge(&bio_list_on_stack[0], &same);
676566acf2dSChristoph Hellwig bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
677566acf2dSChristoph Hellwig } while ((bio = bio_list_pop(&bio_list_on_stack[0])));
678566acf2dSChristoph Hellwig
679566acf2dSChristoph Hellwig current->bio_list = NULL;
680566acf2dSChristoph Hellwig }
681566acf2dSChristoph Hellwig
__submit_bio_noacct_mq(struct bio * bio)6823e08773cSChristoph Hellwig static void __submit_bio_noacct_mq(struct bio *bio)
683ff93ea0cSChristoph Hellwig {
6847c792f33SChristoph Hellwig struct bio_list bio_list[2] = { };
685ff93ea0cSChristoph Hellwig
6867c792f33SChristoph Hellwig current->bio_list = bio_list;
687ff93ea0cSChristoph Hellwig
688ff93ea0cSChristoph Hellwig do {
6893e08773cSChristoph Hellwig __submit_bio(bio);
6907c792f33SChristoph Hellwig } while ((bio = bio_list_pop(&bio_list[0])));
691ff93ea0cSChristoph Hellwig
692ff93ea0cSChristoph Hellwig current->bio_list = NULL;
693ff93ea0cSChristoph Hellwig }
694ff93ea0cSChristoph Hellwig
submit_bio_noacct_nocheck(struct bio * bio)6953f98c753SMing Lei void submit_bio_noacct_nocheck(struct bio *bio)
6963f98c753SMing Lei {
6970f7c8f0fSJinke Han blk_cgroup_bio_start(bio);
6980f7c8f0fSJinke Han blkcg_bio_issue_init(bio);
6990f7c8f0fSJinke Han
7000f7c8f0fSJinke Han if (!bio_flagged(bio, BIO_TRACE_COMPLETION)) {
7010f7c8f0fSJinke Han trace_block_bio_queue(bio);
7020f7c8f0fSJinke Han /*
7030f7c8f0fSJinke Han * Now that enqueuing has been traced, we need to trace
7040f7c8f0fSJinke Han * completion as well.
7050f7c8f0fSJinke Han */
7060f7c8f0fSJinke Han bio_set_flag(bio, BIO_TRACE_COMPLETION);
7070f7c8f0fSJinke Han }
7080f7c8f0fSJinke Han
7093f98c753SMing Lei /*
7103f98c753SMing Lei * We only want one ->submit_bio to be active at a time, else stack
7113f98c753SMing Lei * usage with stacked devices could be a problem. Use current->bio_list
7123f98c753SMing Lei * to collect a list of requests submited by a ->submit_bio method while
7133f98c753SMing Lei * it is active, and then process them after it returned.
7143f98c753SMing Lei */
7153f98c753SMing Lei if (current->bio_list)
7163f98c753SMing Lei bio_list_add(¤t->bio_list[0], bio);
7179f4107b0SJens Axboe else if (!bio->bi_bdev->bd_has_submit_bio)
7183f98c753SMing Lei __submit_bio_noacct_mq(bio);
7193f98c753SMing Lei else
7203f98c753SMing Lei __submit_bio_noacct(bio);
7213f98c753SMing Lei }
7223f98c753SMing Lei
72327a84d54SChristoph Hellwig /**
724ed00aabdSChristoph Hellwig * submit_bio_noacct - re-submit a bio to the block device layer for I/O
72527a84d54SChristoph Hellwig * @bio: The bio describing the location in memory and on the device.
72627a84d54SChristoph Hellwig *
7273fdd4086SChristoph Hellwig * This is a version of submit_bio() that shall only be used for I/O that is
7283fdd4086SChristoph Hellwig * resubmitted to lower level drivers by stacking block drivers. All file
7293fdd4086SChristoph Hellwig * systems and other upper level users of the block layer should use
7303fdd4086SChristoph Hellwig * submit_bio() instead.
731a168ee84SJens Axboe */
submit_bio_noacct(struct bio * bio)7323e08773cSChristoph Hellwig void submit_bio_noacct(struct bio *bio)
733a168ee84SJens Axboe {
734a168ee84SJens Axboe struct block_device *bdev = bio->bi_bdev;
735a168ee84SJens Axboe struct request_queue *q = bdev_get_queue(bdev);
736a168ee84SJens Axboe blk_status_t status = BLK_STS_IOERR;
737a168ee84SJens Axboe
738a168ee84SJens Axboe might_sleep();
739a168ee84SJens Axboe
740a168ee84SJens Axboe /*
741a168ee84SJens Axboe * For a REQ_NOWAIT based request, return -EOPNOTSUPP
742a168ee84SJens Axboe * if queue does not support NOWAIT.
743a168ee84SJens Axboe */
744568ec936SChristoph Hellwig if ((bio->bi_opf & REQ_NOWAIT) && !bdev_nowait(bdev))
745a168ee84SJens Axboe goto not_supported;
746a168ee84SJens Axboe
747a168ee84SJens Axboe if (should_fail_bio(bio))
748a168ee84SJens Axboe goto end_io;
749bdb7d420SMiaohe Lin bio_check_ro(bio);
750a168ee84SJens Axboe if (!bio_flagged(bio, BIO_REMAPPED)) {
751a168ee84SJens Axboe if (unlikely(bio_check_eod(bio)))
752a168ee84SJens Axboe goto end_io;
753a168ee84SJens Axboe if (bdev->bd_partno && unlikely(blk_partition_remap(bio)))
754a168ee84SJens Axboe goto end_io;
755a168ee84SJens Axboe }
756a168ee84SJens Axboe
757a168ee84SJens Axboe /*
758a168ee84SJens Axboe * Filter flush bio's early so that bio based drivers without flush
759a168ee84SJens Axboe * support don't have to worry about them.
760a168ee84SJens Axboe */
761b4a6bb3aSChristoph Hellwig if (op_is_flush(bio->bi_opf)) {
762b4a6bb3aSChristoph Hellwig if (WARN_ON_ONCE(bio_op(bio) != REQ_OP_WRITE &&
763b4a6bb3aSChristoph Hellwig bio_op(bio) != REQ_OP_ZONE_APPEND))
764b4a6bb3aSChristoph Hellwig goto end_io;
765b4a6bb3aSChristoph Hellwig if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags)) {
766a168ee84SJens Axboe bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
767a168ee84SJens Axboe if (!bio_sectors(bio)) {
768a168ee84SJens Axboe status = BLK_STS_OK;
769a168ee84SJens Axboe goto end_io;
770a168ee84SJens Axboe }
771a168ee84SJens Axboe }
772b4a6bb3aSChristoph Hellwig }
773a168ee84SJens Axboe
774a168ee84SJens Axboe if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
775a168ee84SJens Axboe bio_clear_polled(bio);
776a168ee84SJens Axboe
777a168ee84SJens Axboe switch (bio_op(bio)) {
778a168ee84SJens Axboe case REQ_OP_DISCARD:
77970200574SChristoph Hellwig if (!bdev_max_discard_sectors(bdev))
780a168ee84SJens Axboe goto not_supported;
781a168ee84SJens Axboe break;
782a168ee84SJens Axboe case REQ_OP_SECURE_ERASE:
78344abff2cSChristoph Hellwig if (!bdev_max_secure_erase_sectors(bdev))
784a168ee84SJens Axboe goto not_supported;
785a168ee84SJens Axboe break;
786a168ee84SJens Axboe case REQ_OP_ZONE_APPEND:
787a168ee84SJens Axboe status = blk_check_zone_append(q, bio);
788a168ee84SJens Axboe if (status != BLK_STS_OK)
789a168ee84SJens Axboe goto end_io;
790a168ee84SJens Axboe break;
791a168ee84SJens Axboe case REQ_OP_ZONE_RESET:
792a168ee84SJens Axboe case REQ_OP_ZONE_OPEN:
793a168ee84SJens Axboe case REQ_OP_ZONE_CLOSE:
794a168ee84SJens Axboe case REQ_OP_ZONE_FINISH:
795edd1dbc8SChristoph Hellwig if (!bdev_is_zoned(bio->bi_bdev))
796a168ee84SJens Axboe goto not_supported;
797a168ee84SJens Axboe break;
798a168ee84SJens Axboe case REQ_OP_ZONE_RESET_ALL:
799edd1dbc8SChristoph Hellwig if (!bdev_is_zoned(bio->bi_bdev) || !blk_queue_zone_resetall(q))
800a168ee84SJens Axboe goto not_supported;
801a168ee84SJens Axboe break;
802a168ee84SJens Axboe case REQ_OP_WRITE_ZEROES:
803a168ee84SJens Axboe if (!q->limits.max_write_zeroes_sectors)
804a168ee84SJens Axboe goto not_supported;
805a168ee84SJens Axboe break;
806a168ee84SJens Axboe default:
807a168ee84SJens Axboe break;
808a168ee84SJens Axboe }
809a168ee84SJens Axboe
810a168ee84SJens Axboe if (blk_throtl_bio(bio))
811a650628bSMing Lei return;
8123f98c753SMing Lei submit_bio_noacct_nocheck(bio);
813d24c670eSMing Lei return;
814a168ee84SJens Axboe
815a168ee84SJens Axboe not_supported:
816a168ee84SJens Axboe status = BLK_STS_NOTSUPP;
817a168ee84SJens Axboe end_io:
818a168ee84SJens Axboe bio->bi_status = status;
819a168ee84SJens Axboe bio_endio(bio);
820a168ee84SJens Axboe }
821ed00aabdSChristoph Hellwig EXPORT_SYMBOL(submit_bio_noacct);
822a168ee84SJens Axboe
bio_set_ioprio(struct bio * bio)823*c20e89c9SHongyu Jin static void bio_set_ioprio(struct bio *bio)
824*c20e89c9SHongyu Jin {
825*c20e89c9SHongyu Jin /* Nobody set ioprio so far? Initialize it based on task's nice value */
826*c20e89c9SHongyu Jin if (IOPRIO_PRIO_CLASS(bio->bi_ioprio) == IOPRIO_CLASS_NONE)
827*c20e89c9SHongyu Jin bio->bi_ioprio = get_current_ioprio();
828*c20e89c9SHongyu Jin blkcg_set_ioprio(bio);
829*c20e89c9SHongyu Jin }
830*c20e89c9SHongyu Jin
831a168ee84SJens Axboe /**
832710027a4SRandy Dunlap * submit_bio - submit a bio to the block device layer for I/O
833a168ee84SJens Axboe * @bio: The &struct bio which describes the I/O
834a168ee84SJens Axboe *
8353fdd4086SChristoph Hellwig * submit_bio() is used to submit I/O requests to block devices. It is passed a
8363fdd4086SChristoph Hellwig * fully set up &struct bio that describes the I/O that needs to be done. The
837309dca30SChristoph Hellwig * bio will be send to the device described by the bi_bdev field.
838a168ee84SJens Axboe *
8393fdd4086SChristoph Hellwig * The success/failure status of the request, along with notification of
8403fdd4086SChristoph Hellwig * completion, is delivered asynchronously through the ->bi_end_io() callback
8413fdd4086SChristoph Hellwig * in @bio. The bio must NOT be touched by the caller until ->bi_end_io() has
8423fdd4086SChristoph Hellwig * been called.
843a168ee84SJens Axboe */
submit_bio(struct bio * bio)8443e08773cSChristoph Hellwig void submit_bio(struct bio *bio)
845a168ee84SJens Axboe {
846a3e7689bSChristoph Hellwig if (bio_op(bio) == REQ_OP_READ) {
8474f024f37SKent Overstreet task_io_account_read(bio->bi_iter.bi_size);
848a3e7689bSChristoph Hellwig count_vm_events(PGPGIN, bio_sectors(bio));
849a3e7689bSChristoph Hellwig } else if (bio_op(bio) == REQ_OP_WRITE) {
850a3e7689bSChristoph Hellwig count_vm_events(PGPGOUT, bio_sectors(bio));
851a168ee84SJens Axboe }
852a168ee84SJens Axboe
853*c20e89c9SHongyu Jin bio_set_ioprio(bio);
8543e08773cSChristoph Hellwig submit_bio_noacct(bio);
8553e08773cSChristoph Hellwig }
8563e08773cSChristoph Hellwig EXPORT_SYMBOL(submit_bio);
8573e08773cSChristoph Hellwig
8583e08773cSChristoph Hellwig /**
8593e08773cSChristoph Hellwig * bio_poll - poll for BIO completions
8603e08773cSChristoph Hellwig * @bio: bio to poll for
861e30028acSYang Guang * @iob: batches of IO
8623e08773cSChristoph Hellwig * @flags: BLK_POLL_* flags that control the behavior
8633e08773cSChristoph Hellwig *
8643e08773cSChristoph Hellwig * Poll for completions on queue associated with the bio. Returns number of
8653e08773cSChristoph Hellwig * completed entries found.
8663e08773cSChristoph Hellwig *
8673e08773cSChristoph Hellwig * Note: the caller must either be the context that submitted @bio, or
8683e08773cSChristoph Hellwig * be in a RCU critical section to prevent freeing of @bio.
8693e08773cSChristoph Hellwig */
bio_poll(struct bio * bio,struct io_comp_batch * iob,unsigned int flags)8705a72e899SJens Axboe int bio_poll(struct bio *bio, struct io_comp_batch *iob, unsigned int flags)
8713e08773cSChristoph Hellwig {
8723e08773cSChristoph Hellwig blk_qc_t cookie = READ_ONCE(bio->bi_cookie);
873310726c3SJens Axboe struct block_device *bdev;
874310726c3SJens Axboe struct request_queue *q;
87569fe0f29SMing Lei int ret = 0;
8763e08773cSChristoph Hellwig
877310726c3SJens Axboe bdev = READ_ONCE(bio->bi_bdev);
878310726c3SJens Axboe if (!bdev)
879310726c3SJens Axboe return 0;
880310726c3SJens Axboe
881310726c3SJens Axboe q = bdev_get_queue(bdev);
8823e08773cSChristoph Hellwig if (cookie == BLK_QC_T_NONE ||
8833e08773cSChristoph Hellwig !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
8843e08773cSChristoph Hellwig return 0;
8853e08773cSChristoph Hellwig
886110fdb44SPankaj Raghav /*
887110fdb44SPankaj Raghav * As the requests that require a zone lock are not plugged in the
888110fdb44SPankaj Raghav * first place, directly accessing the plug instead of using
889110fdb44SPankaj Raghav * blk_mq_plug() should not have any consequences during flushing for
890110fdb44SPankaj Raghav * zoned devices.
891110fdb44SPankaj Raghav */
892008f75a2SChristoph Hellwig blk_flush_plug(current->plug, false);
8933e08773cSChristoph Hellwig
89433391eecSJens Axboe /*
89533391eecSJens Axboe * We need to be able to enter a frozen queue, similar to how
89633391eecSJens Axboe * timeouts also need to do that. If that is blocked, then we can
89733391eecSJens Axboe * have pending IO when a queue freeze is started, and then the
89833391eecSJens Axboe * wait for the freeze to finish will wait for polled requests to
89933391eecSJens Axboe * timeout as the poller is preventer from entering the queue and
90033391eecSJens Axboe * completing them. As long as we prevent new IO from being queued,
90133391eecSJens Axboe * that should be all that matters.
90233391eecSJens Axboe */
90333391eecSJens Axboe if (!percpu_ref_tryget(&q->q_usage_counter))
9043e08773cSChristoph Hellwig return 0;
90569fe0f29SMing Lei if (queue_is_mq(q)) {
9065a72e899SJens Axboe ret = blk_mq_poll(q, cookie, iob, flags);
90769fe0f29SMing Lei } else {
90869fe0f29SMing Lei struct gendisk *disk = q->disk;
90969fe0f29SMing Lei
91069fe0f29SMing Lei if (disk && disk->fops->poll_bio)
91169fe0f29SMing Lei ret = disk->fops->poll_bio(bio, iob, flags);
91269fe0f29SMing Lei }
9133e08773cSChristoph Hellwig blk_queue_exit(q);
9143e08773cSChristoph Hellwig return ret;
9153e08773cSChristoph Hellwig }
9163e08773cSChristoph Hellwig EXPORT_SYMBOL_GPL(bio_poll);
9173e08773cSChristoph Hellwig
9183e08773cSChristoph Hellwig /*
9193e08773cSChristoph Hellwig * Helper to implement file_operations.iopoll. Requires the bio to be stored
9203e08773cSChristoph Hellwig * in iocb->private, and cleared before freeing the bio.
9213e08773cSChristoph Hellwig */
iocb_bio_iopoll(struct kiocb * kiocb,struct io_comp_batch * iob,unsigned int flags)9225a72e899SJens Axboe int iocb_bio_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
9235a72e899SJens Axboe unsigned int flags)
9243e08773cSChristoph Hellwig {
9253e08773cSChristoph Hellwig struct bio *bio;
9263e08773cSChristoph Hellwig int ret = 0;
9273e08773cSChristoph Hellwig
9283e08773cSChristoph Hellwig /*
9293e08773cSChristoph Hellwig * Note: the bio cache only uses SLAB_TYPESAFE_BY_RCU, so bio can
9303e08773cSChristoph Hellwig * point to a freshly allocated bio at this point. If that happens
9313e08773cSChristoph Hellwig * we have a few cases to consider:
9323e08773cSChristoph Hellwig *
9333e08773cSChristoph Hellwig * 1) the bio is beeing initialized and bi_bdev is NULL. We can just
9343e08773cSChristoph Hellwig * simply nothing in this case
9353e08773cSChristoph Hellwig * 2) the bio points to a not poll enabled device. bio_poll will catch
9363e08773cSChristoph Hellwig * this and return 0
9373e08773cSChristoph Hellwig * 3) the bio points to a poll capable device, including but not
9383e08773cSChristoph Hellwig * limited to the one that the original bio pointed to. In this
9393e08773cSChristoph Hellwig * case we will call into the actual poll method and poll for I/O,
9403e08773cSChristoph Hellwig * even if we don't need to, but it won't cause harm either.
9413e08773cSChristoph Hellwig *
9423e08773cSChristoph Hellwig * For cases 2) and 3) above the RCU grace period ensures that bi_bdev
9433e08773cSChristoph Hellwig * is still allocated. Because partitions hold a reference to the whole
9443e08773cSChristoph Hellwig * device bdev and thus disk, the disk is also still valid. Grabbing
9453e08773cSChristoph Hellwig * a reference to the queue in bio_poll() ensures the hctxs and requests
9463e08773cSChristoph Hellwig * are still valid as well.
9473e08773cSChristoph Hellwig */
9483e08773cSChristoph Hellwig rcu_read_lock();
9493e08773cSChristoph Hellwig bio = READ_ONCE(kiocb->private);
950310726c3SJens Axboe if (bio)
9515a72e899SJens Axboe ret = bio_poll(bio, iob, flags);
9523e08773cSChristoph Hellwig rcu_read_unlock();
953b8e24a93SJohannes Weiner
954b8e24a93SJohannes Weiner return ret;
955a168ee84SJens Axboe }
9563e08773cSChristoph Hellwig EXPORT_SYMBOL_GPL(iocb_bio_iopoll);
957a168ee84SJens Axboe
update_io_ticks(struct block_device * part,unsigned long now,bool end)958450b7879SChristoph Hellwig void update_io_ticks(struct block_device *part, unsigned long now, bool end)
9599123bf6fSChristoph Hellwig {
9609123bf6fSChristoph Hellwig unsigned long stamp;
9619123bf6fSChristoph Hellwig again:
9628446fe92SChristoph Hellwig stamp = READ_ONCE(part->bd_stamp);
963e5d98cc3SYu Kuai if (unlikely(time_after(now, stamp)) &&
964e5d98cc3SYu Kuai likely(try_cmpxchg(&part->bd_stamp, &stamp, now)) &&
965e5d98cc3SYu Kuai (end || part_in_flight(part)))
966e5d98cc3SYu Kuai __part_stat_add(part, io_ticks, now - stamp);
967e5d98cc3SYu Kuai
9688446fe92SChristoph Hellwig if (part->bd_partno) {
9698446fe92SChristoph Hellwig part = bdev_whole(part);
9709123bf6fSChristoph Hellwig goto again;
9719123bf6fSChristoph Hellwig }
9729123bf6fSChristoph Hellwig }
9739123bf6fSChristoph Hellwig
bdev_start_io_acct(struct block_device * bdev,enum req_op op,unsigned long start_time)9745f275713SYu Kuai unsigned long bdev_start_io_acct(struct block_device *bdev, enum req_op op,
975e45c47d1SMike Snitzer unsigned long start_time)
976956d510eSChristoph Hellwig {
977956d510eSChristoph Hellwig part_stat_lock();
9785f0614a5SMing Lei update_io_ticks(bdev, start_time, false);
9795f0614a5SMing Lei part_stat_local_inc(bdev, in_flight[op_is_write(op)]);
980956d510eSChristoph Hellwig part_stat_unlock();
981956d510eSChristoph Hellwig
982e45c47d1SMike Snitzer return start_time;
983956d510eSChristoph Hellwig }
9845f0614a5SMing Lei EXPORT_SYMBOL(bdev_start_io_acct);
9857b26410bSSong Liu
98699dfc43eSChristoph Hellwig /**
98799dfc43eSChristoph Hellwig * bio_start_io_acct - start I/O accounting for bio based drivers
98899dfc43eSChristoph Hellwig * @bio: bio to start account for
98999dfc43eSChristoph Hellwig *
99099dfc43eSChristoph Hellwig * Returns the start time that should be passed back to bio_end_io_acct().
99199dfc43eSChristoph Hellwig */
bio_start_io_acct(struct bio * bio)99299dfc43eSChristoph Hellwig unsigned long bio_start_io_acct(struct bio *bio)
9937b26410bSSong Liu {
9945f275713SYu Kuai return bdev_start_io_acct(bio->bi_bdev, bio_op(bio), jiffies);
9957b26410bSSong Liu }
99699dfc43eSChristoph Hellwig EXPORT_SYMBOL_GPL(bio_start_io_acct);
9977b26410bSSong Liu
bdev_end_io_acct(struct block_device * bdev,enum req_op op,unsigned int sectors,unsigned long start_time)99877e7ffd7SBart Van Assche void bdev_end_io_acct(struct block_device *bdev, enum req_op op,
9995f275713SYu Kuai unsigned int sectors, unsigned long start_time)
1000956d510eSChristoph Hellwig {
1001956d510eSChristoph Hellwig const int sgrp = op_stat_group(op);
1002956d510eSChristoph Hellwig unsigned long now = READ_ONCE(jiffies);
1003956d510eSChristoph Hellwig unsigned long duration = now - start_time;
1004956d510eSChristoph Hellwig
1005956d510eSChristoph Hellwig part_stat_lock();
10065f0614a5SMing Lei update_io_ticks(bdev, now, true);
10075f275713SYu Kuai part_stat_inc(bdev, ios[sgrp]);
10085f275713SYu Kuai part_stat_add(bdev, sectors[sgrp], sectors);
10095f0614a5SMing Lei part_stat_add(bdev, nsecs[sgrp], jiffies_to_nsecs(duration));
10105f0614a5SMing Lei part_stat_local_dec(bdev, in_flight[op_is_write(op)]);
1011956d510eSChristoph Hellwig part_stat_unlock();
1012956d510eSChristoph Hellwig }
10135f0614a5SMing Lei EXPORT_SYMBOL(bdev_end_io_acct);
10147b26410bSSong Liu
bio_end_io_acct_remapped(struct bio * bio,unsigned long start_time,struct block_device * orig_bdev)101599dfc43eSChristoph Hellwig void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
101699dfc43eSChristoph Hellwig struct block_device *orig_bdev)
10177b26410bSSong Liu {
10185f275713SYu Kuai bdev_end_io_acct(orig_bdev, bio_op(bio), bio_sectors(bio), start_time);
10197b26410bSSong Liu }
102099dfc43eSChristoph Hellwig EXPORT_SYMBOL_GPL(bio_end_io_acct_remapped);
10217b26410bSSong Liu
1022ef9e3facSKiyoshi Ueda /**
1023ef9e3facSKiyoshi Ueda * blk_lld_busy - Check if underlying low-level drivers of a device are busy
1024ef9e3facSKiyoshi Ueda * @q : the queue of the device being checked
1025ef9e3facSKiyoshi Ueda *
1026ef9e3facSKiyoshi Ueda * Description:
1027ef9e3facSKiyoshi Ueda * Check if underlying low-level drivers of a device are busy.
1028ef9e3facSKiyoshi Ueda * If the drivers want to export their busy state, they must set own
1029ef9e3facSKiyoshi Ueda * exporting function using blk_queue_lld_busy() first.
1030ef9e3facSKiyoshi Ueda *
1031ef9e3facSKiyoshi Ueda * Basically, this function is used only by request stacking drivers
1032ef9e3facSKiyoshi Ueda * to stop dispatching requests to underlying devices when underlying
1033ef9e3facSKiyoshi Ueda * devices are busy. This behavior helps more I/O merging on the queue
1034ef9e3facSKiyoshi Ueda * of the request stacking driver and prevents I/O throughput regression
1035ef9e3facSKiyoshi Ueda * on burst I/O load.
1036ef9e3facSKiyoshi Ueda *
1037ef9e3facSKiyoshi Ueda * Return:
1038ef9e3facSKiyoshi Ueda * 0 - Not busy (The request stacking driver should dispatch request)
1039ef9e3facSKiyoshi Ueda * 1 - Busy (The request stacking driver should stop dispatching request)
1040ef9e3facSKiyoshi Ueda */
blk_lld_busy(struct request_queue * q)1041ef9e3facSKiyoshi Ueda int blk_lld_busy(struct request_queue *q)
1042ef9e3facSKiyoshi Ueda {
1043344e9ffcSJens Axboe if (queue_is_mq(q) && q->mq_ops->busy)
10449ba20527SJens Axboe return q->mq_ops->busy(q);
1045ef9e3facSKiyoshi Ueda
1046ef9e3facSKiyoshi Ueda return 0;
1047ef9e3facSKiyoshi Ueda }
1048ef9e3facSKiyoshi Ueda EXPORT_SYMBOL_GPL(blk_lld_busy);
1049ef9e3facSKiyoshi Ueda
kblockd_schedule_work(struct work_struct * work)105059c3d45eSJens Axboe int kblockd_schedule_work(struct work_struct *work)
1051a168ee84SJens Axboe {
1052a168ee84SJens Axboe return queue_work(kblockd_workqueue, work);
1053a168ee84SJens Axboe }
1054a168ee84SJens Axboe EXPORT_SYMBOL(kblockd_schedule_work);
1055a168ee84SJens Axboe
kblockd_mod_delayed_work_on(int cpu,struct delayed_work * dwork,unsigned long delay)1056818cd1cbSJens Axboe int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork,
1057818cd1cbSJens Axboe unsigned long delay)
1058818cd1cbSJens Axboe {
1059818cd1cbSJens Axboe return mod_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
1060818cd1cbSJens Axboe }
1061818cd1cbSJens Axboe EXPORT_SYMBOL(kblockd_mod_delayed_work_on);
1062818cd1cbSJens Axboe
blk_start_plug_nr_ios(struct blk_plug * plug,unsigned short nr_ios)106347c122e3SJens Axboe void blk_start_plug_nr_ios(struct blk_plug *plug, unsigned short nr_ios)
106447c122e3SJens Axboe {
106547c122e3SJens Axboe struct task_struct *tsk = current;
106647c122e3SJens Axboe
106747c122e3SJens Axboe /*
106847c122e3SJens Axboe * If this is a nested plug, don't actually assign it.
106947c122e3SJens Axboe */
107047c122e3SJens Axboe if (tsk->plug)
107147c122e3SJens Axboe return;
107247c122e3SJens Axboe
1073bc490f81SJens Axboe plug->mq_list = NULL;
107447c122e3SJens Axboe plug->cached_rq = NULL;
107547c122e3SJens Axboe plug->nr_ios = min_t(unsigned short, nr_ios, BLK_MAX_REQUEST_COUNT);
107647c122e3SJens Axboe plug->rq_count = 0;
107747c122e3SJens Axboe plug->multiple_queues = false;
1078dc5fc361SJens Axboe plug->has_elevator = false;
107947c122e3SJens Axboe INIT_LIST_HEAD(&plug->cb_list);
108047c122e3SJens Axboe
108147c122e3SJens Axboe /*
108247c122e3SJens Axboe * Store ordering should not be needed here, since a potential
108347c122e3SJens Axboe * preempt will imply a full memory barrier
108447c122e3SJens Axboe */
108547c122e3SJens Axboe tsk->plug = plug;
108647c122e3SJens Axboe }
108747c122e3SJens Axboe
108875df7136SSuresh Jayaraman /**
108975df7136SSuresh Jayaraman * blk_start_plug - initialize blk_plug and track it inside the task_struct
109075df7136SSuresh Jayaraman * @plug: The &struct blk_plug that needs to be initialized
109175df7136SSuresh Jayaraman *
109275df7136SSuresh Jayaraman * Description:
109340405851SJeff Moyer * blk_start_plug() indicates to the block layer an intent by the caller
109440405851SJeff Moyer * to submit multiple I/O requests in a batch. The block layer may use
109540405851SJeff Moyer * this hint to defer submitting I/Os from the caller until blk_finish_plug()
109640405851SJeff Moyer * is called. However, the block layer may choose to submit requests
109740405851SJeff Moyer * before a call to blk_finish_plug() if the number of queued I/Os
109840405851SJeff Moyer * exceeds %BLK_MAX_REQUEST_COUNT, or if the size of the I/O is larger than
109940405851SJeff Moyer * %BLK_PLUG_FLUSH_SIZE. The queued I/Os may also be submitted early if
110040405851SJeff Moyer * the task schedules (see below).
110140405851SJeff Moyer *
110275df7136SSuresh Jayaraman * Tracking blk_plug inside the task_struct will help with auto-flushing the
110375df7136SSuresh Jayaraman * pending I/O should the task end up blocking between blk_start_plug() and
110475df7136SSuresh Jayaraman * blk_finish_plug(). This is important from a performance perspective, but
110575df7136SSuresh Jayaraman * also ensures that we don't deadlock. For instance, if the task is blocking
110675df7136SSuresh Jayaraman * for a memory allocation, memory reclaim could end up wanting to free a
110775df7136SSuresh Jayaraman * page belonging to that request that is currently residing in our private
110875df7136SSuresh Jayaraman * plug. By flushing the pending I/O when the process goes to sleep, we avoid
110975df7136SSuresh Jayaraman * this kind of deadlock.
111075df7136SSuresh Jayaraman */
blk_start_plug(struct blk_plug * plug)111173c10101SJens Axboe void blk_start_plug(struct blk_plug *plug)
111273c10101SJens Axboe {
111347c122e3SJens Axboe blk_start_plug_nr_ios(plug, 1);
111473c10101SJens Axboe }
111573c10101SJens Axboe EXPORT_SYMBOL(blk_start_plug);
111673c10101SJens Axboe
flush_plug_callbacks(struct blk_plug * plug,bool from_schedule)111774018dc3SNeilBrown static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
1118048c9374SNeilBrown {
1119048c9374SNeilBrown LIST_HEAD(callbacks);
1120048c9374SNeilBrown
11212a7d5559SShaohua Li while (!list_empty(&plug->cb_list)) {
1122048c9374SNeilBrown list_splice_init(&plug->cb_list, &callbacks);
1123048c9374SNeilBrown
1124048c9374SNeilBrown while (!list_empty(&callbacks)) {
1125048c9374SNeilBrown struct blk_plug_cb *cb = list_first_entry(&callbacks,
1126048c9374SNeilBrown struct blk_plug_cb,
1127048c9374SNeilBrown list);
1128048c9374SNeilBrown list_del(&cb->list);
112974018dc3SNeilBrown cb->callback(cb, from_schedule);
1130048c9374SNeilBrown }
1131048c9374SNeilBrown }
11322a7d5559SShaohua Li }
1133048c9374SNeilBrown
blk_check_plugged(blk_plug_cb_fn unplug,void * data,int size)11349cbb1750SNeilBrown struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
11359cbb1750SNeilBrown int size)
11369cbb1750SNeilBrown {
11379cbb1750SNeilBrown struct blk_plug *plug = current->plug;
11389cbb1750SNeilBrown struct blk_plug_cb *cb;
11399cbb1750SNeilBrown
11409cbb1750SNeilBrown if (!plug)
11419cbb1750SNeilBrown return NULL;
11429cbb1750SNeilBrown
11439cbb1750SNeilBrown list_for_each_entry(cb, &plug->cb_list, list)
11449cbb1750SNeilBrown if (cb->callback == unplug && cb->data == data)
11459cbb1750SNeilBrown return cb;
11469cbb1750SNeilBrown
11479cbb1750SNeilBrown /* Not currently on the callback list */
11489cbb1750SNeilBrown BUG_ON(size < sizeof(*cb));
11499cbb1750SNeilBrown cb = kzalloc(size, GFP_ATOMIC);
11509cbb1750SNeilBrown if (cb) {
11519cbb1750SNeilBrown cb->data = data;
11529cbb1750SNeilBrown cb->callback = unplug;
11539cbb1750SNeilBrown list_add(&cb->list, &plug->cb_list);
11549cbb1750SNeilBrown }
11559cbb1750SNeilBrown return cb;
11569cbb1750SNeilBrown }
11579cbb1750SNeilBrown EXPORT_SYMBOL(blk_check_plugged);
11589cbb1750SNeilBrown
__blk_flush_plug(struct blk_plug * plug,bool from_schedule)1159aa8dcccaSChristoph Hellwig void __blk_flush_plug(struct blk_plug *plug, bool from_schedule)
116073c10101SJens Axboe {
1161b600455dSPavel Begunkov if (!list_empty(&plug->cb_list))
116274018dc3SNeilBrown flush_plug_callbacks(plug, from_schedule);
1163320ae51fSJens Axboe blk_mq_flush_plug_list(plug, from_schedule);
1164c5fc7b93SJens Axboe /*
1165c5fc7b93SJens Axboe * Unconditionally flush out cached requests, even if the unplug
1166c5fc7b93SJens Axboe * event came from schedule. Since we know hold references to the
1167c5fc7b93SJens Axboe * queue for cached requests, we don't want a blocked task holding
1168c5fc7b93SJens Axboe * up a queue freeze/quiesce event.
1169c5fc7b93SJens Axboe */
1170c5fc7b93SJens Axboe if (unlikely(!rq_list_empty(plug->cached_rq)))
117147c122e3SJens Axboe blk_mq_free_plug_rqs(plug);
117273c10101SJens Axboe }
117373c10101SJens Axboe
117440405851SJeff Moyer /**
117540405851SJeff Moyer * blk_finish_plug - mark the end of a batch of submitted I/O
117640405851SJeff Moyer * @plug: The &struct blk_plug passed to blk_start_plug()
117740405851SJeff Moyer *
117840405851SJeff Moyer * Description:
117940405851SJeff Moyer * Indicate that a batch of I/O submissions is complete. This function
118040405851SJeff Moyer * must be paired with an initial call to blk_start_plug(). The intent
118140405851SJeff Moyer * is to allow the block layer to optimize I/O submission. See the
118240405851SJeff Moyer * documentation for blk_start_plug() for more information.
118340405851SJeff Moyer */
blk_finish_plug(struct blk_plug * plug)118473c10101SJens Axboe void blk_finish_plug(struct blk_plug *plug)
118573c10101SJens Axboe {
1186008f75a2SChristoph Hellwig if (plug == current->plug) {
1187aa8dcccaSChristoph Hellwig __blk_flush_plug(plug, false);
118888b996cdSChristoph Hellwig current->plug = NULL;
118973c10101SJens Axboe }
1190008f75a2SChristoph Hellwig }
119173c10101SJens Axboe EXPORT_SYMBOL(blk_finish_plug);
119273c10101SJens Axboe
blk_io_schedule(void)119371ac860aSMing Lei void blk_io_schedule(void)
119471ac860aSMing Lei {
119571ac860aSMing Lei /* Prevent hang_check timer from firing at us during very long I/O */
119671ac860aSMing Lei unsigned long timeout = sysctl_hung_task_timeout_secs * HZ / 2;
119771ac860aSMing Lei
119871ac860aSMing Lei if (timeout)
119971ac860aSMing Lei io_schedule_timeout(timeout);
120071ac860aSMing Lei else
120171ac860aSMing Lei io_schedule();
120271ac860aSMing Lei }
120371ac860aSMing Lei EXPORT_SYMBOL_GPL(blk_io_schedule);
120471ac860aSMing Lei
blk_dev_init(void)1205a168ee84SJens Axboe int __init blk_dev_init(void)
1206a168ee84SJens Axboe {
120716458cf3SBart Van Assche BUILD_BUG_ON((__force u32)REQ_OP_LAST >= (1 << REQ_OP_BITS));
1208ef295ecfSChristoph Hellwig BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
1209c593642cSPankaj Bharadiya sizeof_field(struct request, cmd_flags));
1210ef295ecfSChristoph Hellwig BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
1211c593642cSPankaj Bharadiya sizeof_field(struct bio, bi_opf));
12129eb55b03SNikanth Karthikesan
121389b90be2STejun Heo /* used for unplugging and affects IO latency/throughput - HIGHPRI */
121489b90be2STejun Heo kblockd_workqueue = alloc_workqueue("kblockd",
121528747fcdSMatias Bjørling WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
1216a168ee84SJens Axboe if (!kblockd_workqueue)
1217a168ee84SJens Axboe panic("Failed to create kblockd\n");
1218a168ee84SJens Axboe
1219c2789bd4SIlya Dryomov blk_requestq_cachep = kmem_cache_create("request_queue",
1220a168ee84SJens Axboe sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
1221a168ee84SJens Axboe
122218fbda91SOmar Sandoval blk_debugfs_root = debugfs_create_dir("block", NULL);
122318fbda91SOmar Sandoval
1224a168ee84SJens Axboe return 0;
1225a168ee84SJens Axboe }
1226