blob: 1acd94ba10c7504aa411c7b72d6abe3f70667da6 [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
5 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
6 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
Jens Axboe6728cb02008-01-31 13:03:55 +01007 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
8 * - July2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
10 */
11
12/*
13 * This handles all read/write requests to block devices
14 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/bio.h>
18#include <linux/blkdev.h>
Alan Stern52abca62020-12-08 21:29:51 -080019#include <linux/blk-pm.h>
Christoph Hellwigfe45e632021-09-20 14:33:27 +020020#include <linux/blk-integrity.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/highmem.h>
22#include <linux/mm.h>
Matthew Wilcox (Oracle)cee9a0c2020-06-01 21:46:07 -070023#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel_stat.h>
25#include <linux/string.h>
26#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/completion.h>
28#include <linux/slab.h>
29#include <linux/swap.h>
30#include <linux/writeback.h>
Andrew Mortonfaccbd4b2006-12-10 02:19:35 -080031#include <linux/task_io_accounting_ops.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080032#include <linux/fault-inject.h>
Jens Axboe73c10102011-03-08 13:19:51 +010033#include <linux/list_sort.h>
Tejun Heoe3c78ca2011-10-19 14:32:38 +020034#include <linux/delay.h>
Tejun Heoaaf7c682012-04-19 16:29:22 -070035#include <linux/ratelimit.h>
Lin Ming6c954662013-03-23 11:42:26 +080036#include <linux/pm_runtime.h>
Tejun Heoeea8f412015-05-22 17:13:17 -040037#include <linux/blk-cgroup.h>
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +030038#include <linux/t10-pi.h>
Omar Sandoval18fbda92017-01-31 14:53:20 -080039#include <linux/debugfs.h>
Howard McLauchlan30abb3a2018-02-06 14:05:39 -080040#include <linux/bpf.h>
Johannes Weinerb8e24a92019-08-08 15:03:00 -040041#include <linux/psi.h>
Ming Lei71ac8602020-05-14 16:45:09 +080042#include <linux/sched/sysctl.h>
Satya Tangiralaa892c8d2020-05-14 00:37:18 +000043#include <linux/blk-crypto.h>
Li Zefan55782132009-06-09 13:43:05 +080044
45#define CREATE_TRACE_POINTS
46#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Jens Axboe8324aa92008-01-29 14:51:59 +010048#include "blk.h"
Christoph Hellwig2aa77452021-11-23 19:53:08 +010049#include "blk-mq-sched.h"
Bart Van Asschebca6b062018-09-26 14:01:03 -070050#include "blk-pm.h"
Jens Axboea7b36ee2021-10-05 09:11:56 -060051#include "blk-throttle.h"
Jens Axboe8324aa92008-01-29 14:51:59 +010052
Omar Sandoval18fbda92017-01-31 14:53:20 -080053struct dentry *blk_debugfs_root;
Omar Sandoval18fbda92017-01-31 14:53:20 -080054
Mike Snitzerd07335e2010-11-16 12:52:38 +010055EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020056EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -070057EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Keith Busch3291fa52014-04-28 12:30:52 -060058EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
NeilBrowncbae8d42012-12-14 20:49:27 +010059EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
Chaitanya Kulkarnib357e4a2021-02-21 21:29:59 -080060EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_insert);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010061
Tejun Heoa73f7302011-12-14 00:33:37 +010062DEFINE_IDA(blk_queue_ida);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * For queue allocation
66 */
Jens Axboe6728cb02008-01-31 13:03:55 +010067struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * Controlling structure to kblockd
71 */
Jens Axboeff856ba2006-01-09 16:02:34 +010072static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Bart Van Assche8814ce82018-03-07 17:10:04 -080074/**
75 * blk_queue_flag_set - atomically set a queue flag
76 * @flag: flag to be set
77 * @q: request queue
78 */
79void blk_queue_flag_set(unsigned int flag, struct request_queue *q)
80{
Christoph Hellwig57d74df2018-11-14 17:02:07 +010081 set_bit(flag, &q->queue_flags);
Bart Van Assche8814ce82018-03-07 17:10:04 -080082}
83EXPORT_SYMBOL(blk_queue_flag_set);
84
85/**
86 * blk_queue_flag_clear - atomically clear a queue flag
87 * @flag: flag to be cleared
88 * @q: request queue
89 */
90void blk_queue_flag_clear(unsigned int flag, struct request_queue *q)
91{
Christoph Hellwig57d74df2018-11-14 17:02:07 +010092 clear_bit(flag, &q->queue_flags);
Bart Van Assche8814ce82018-03-07 17:10:04 -080093}
94EXPORT_SYMBOL(blk_queue_flag_clear);
95
96/**
97 * blk_queue_flag_test_and_set - atomically test and set a queue flag
98 * @flag: flag to be set
99 * @q: request queue
100 *
101 * Returns the previous value of @flag - 0 if the flag was not set and 1 if
102 * the flag was already set.
103 */
104bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q)
105{
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100106 return test_and_set_bit(flag, &q->queue_flags);
Bart Van Assche8814ce82018-03-07 17:10:04 -0800107}
108EXPORT_SYMBOL_GPL(blk_queue_flag_test_and_set);
109
Chaitanya Kulkarnie47bc4e2019-06-20 10:59:16 -0700110#define REQ_OP_NAME(name) [REQ_OP_##name] = #name
111static const char *const blk_op_name[] = {
112 REQ_OP_NAME(READ),
113 REQ_OP_NAME(WRITE),
114 REQ_OP_NAME(FLUSH),
115 REQ_OP_NAME(DISCARD),
116 REQ_OP_NAME(SECURE_ERASE),
117 REQ_OP_NAME(ZONE_RESET),
Chaitanya Kulkarni6e33dbf2019-08-01 10:26:36 -0700118 REQ_OP_NAME(ZONE_RESET_ALL),
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900119 REQ_OP_NAME(ZONE_OPEN),
120 REQ_OP_NAME(ZONE_CLOSE),
121 REQ_OP_NAME(ZONE_FINISH),
Keith Busch0512a752020-05-12 17:55:47 +0900122 REQ_OP_NAME(ZONE_APPEND),
Chaitanya Kulkarnie47bc4e2019-06-20 10:59:16 -0700123 REQ_OP_NAME(WRITE_SAME),
124 REQ_OP_NAME(WRITE_ZEROES),
Chaitanya Kulkarnie47bc4e2019-06-20 10:59:16 -0700125 REQ_OP_NAME(DRV_IN),
126 REQ_OP_NAME(DRV_OUT),
127};
128#undef REQ_OP_NAME
129
130/**
131 * blk_op_str - Return string XXX in the REQ_OP_XXX.
132 * @op: REQ_OP_XXX.
133 *
134 * Description: Centralize block layer function to convert REQ_OP_XXX into
135 * string format. Useful in the debugging and tracing bio or request. For
136 * invalid REQ_OP_XXX it returns string "UNKNOWN".
137 */
138inline const char *blk_op_str(unsigned int op)
139{
140 const char *op_str = "UNKNOWN";
141
142 if (op < ARRAY_SIZE(blk_op_name) && blk_op_name[op])
143 op_str = blk_op_name[op];
144
145 return op_str;
146}
147EXPORT_SYMBOL_GPL(blk_op_str);
148
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200149static const struct {
150 int errno;
151 const char *name;
152} blk_errors[] = {
153 [BLK_STS_OK] = { 0, "" },
154 [BLK_STS_NOTSUPP] = { -EOPNOTSUPP, "operation not supported" },
155 [BLK_STS_TIMEOUT] = { -ETIMEDOUT, "timeout" },
156 [BLK_STS_NOSPC] = { -ENOSPC, "critical space allocation" },
157 [BLK_STS_TRANSPORT] = { -ENOLINK, "recoverable transport" },
158 [BLK_STS_TARGET] = { -EREMOTEIO, "critical target" },
159 [BLK_STS_NEXUS] = { -EBADE, "critical nexus" },
160 [BLK_STS_MEDIUM] = { -ENODATA, "critical medium" },
161 [BLK_STS_PROTECTION] = { -EILSEQ, "protection" },
162 [BLK_STS_RESOURCE] = { -ENOMEM, "kernel resource" },
Ming Lei86ff7c22018-01-30 22:04:57 -0500163 [BLK_STS_DEV_RESOURCE] = { -EBUSY, "device resource" },
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500164 [BLK_STS_AGAIN] = { -EAGAIN, "nonblocking retry" },
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200165
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200166 /* device mapper special case, should not leak out: */
167 [BLK_STS_DM_REQUEUE] = { -EREMCHG, "dm internal retry" },
168
Keith Busch3b481d92020-09-24 13:53:28 -0700169 /* zone device specific errors */
170 [BLK_STS_ZONE_OPEN_RESOURCE] = { -ETOOMANYREFS, "open zones exceeded" },
171 [BLK_STS_ZONE_ACTIVE_RESOURCE] = { -EOVERFLOW, "active zones exceeded" },
172
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200173 /* everything else not covered above: */
174 [BLK_STS_IOERR] = { -EIO, "I/O" },
175};
176
177blk_status_t errno_to_blk_status(int errno)
178{
179 int i;
180
181 for (i = 0; i < ARRAY_SIZE(blk_errors); i++) {
182 if (blk_errors[i].errno == errno)
183 return (__force blk_status_t)i;
184 }
185
186 return BLK_STS_IOERR;
187}
188EXPORT_SYMBOL_GPL(errno_to_blk_status);
189
190int blk_status_to_errno(blk_status_t status)
191{
192 int idx = (__force int)status;
193
Bart Van Assche34bd9c12017-06-21 10:55:46 -0700194 if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200195 return -EIO;
196 return blk_errors[idx].errno;
197}
198EXPORT_SYMBOL_GPL(blk_status_to_errno);
199
Christoph Hellwig0d7a29a2021-11-17 07:14:03 +0100200const char *blk_status_to_str(blk_status_t status)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200201{
202 int idx = (__force int)status;
203
Bart Van Assche34bd9c12017-06-21 10:55:46 -0700204 if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
Christoph Hellwig0d7a29a2021-11-17 07:14:03 +0100205 return "<null>";
206 return blk_errors[idx].name;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/**
210 * blk_sync_queue - cancel any pending callbacks on a queue
211 * @q: the queue
212 *
213 * Description:
214 * The block layer may perform asynchronous callback activity
215 * on a queue, such as calling the unplug function after a timeout.
216 * A block device may call blk_sync_queue to ensure that any
217 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200218 * that the callbacks might use. The caller must already have made sure
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200219 * that its ->submit_bio will not re-add plugging prior to calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * this function.
221 *
Vivek Goyalda527772011-03-02 19:05:33 -0500222 * This function does not cancel any asynchronous activity arising
Masanari Iidada3dae52014-09-09 01:27:23 +0900223 * out of elevator or throttling code. That would require elevator_exit()
Tejun Heo5efd6112012-03-05 13:15:12 -0800224 * and blkcg_exit_queue() to be called with queue lock initialized.
Vivek Goyalda527772011-03-02 19:05:33 -0500225 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
227void blk_sync_queue(struct request_queue *q)
228{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100229 del_timer_sync(&q->timeout);
Bart Van Assche4e9b6f22017-10-19 10:00:48 -0700230 cancel_work_sync(&q->timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232EXPORT_SYMBOL(blk_sync_queue);
233
234/**
Bart Van Asschecd84a622018-09-26 14:01:04 -0700235 * blk_set_pm_only - increment pm_only counter
Bart Van Asschec9254f22017-11-09 10:49:57 -0800236 * @q: request queue pointer
Bart Van Asschec9254f22017-11-09 10:49:57 -0800237 */
Bart Van Asschecd84a622018-09-26 14:01:04 -0700238void blk_set_pm_only(struct request_queue *q)
Bart Van Asschec9254f22017-11-09 10:49:57 -0800239{
Bart Van Asschecd84a622018-09-26 14:01:04 -0700240 atomic_inc(&q->pm_only);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800241}
Bart Van Asschecd84a622018-09-26 14:01:04 -0700242EXPORT_SYMBOL_GPL(blk_set_pm_only);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800243
Bart Van Asschecd84a622018-09-26 14:01:04 -0700244void blk_clear_pm_only(struct request_queue *q)
Bart Van Asschec9254f22017-11-09 10:49:57 -0800245{
Bart Van Asschecd84a622018-09-26 14:01:04 -0700246 int pm_only;
247
248 pm_only = atomic_dec_return(&q->pm_only);
249 WARN_ON_ONCE(pm_only < 0);
250 if (pm_only == 0)
251 wake_up_all(&q->mq_freeze_wq);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800252}
Bart Van Asschecd84a622018-09-26 14:01:04 -0700253EXPORT_SYMBOL_GPL(blk_clear_pm_only);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800254
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000255/**
256 * blk_put_queue - decrement the request_queue refcount
257 * @q: the request_queue structure to decrement the refcount for
258 *
259 * Decrements the refcount of the request_queue kobject. When this reaches 0
260 * we'll have blk_release_queue() called.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000261 *
262 * Context: Any context, but the last reference must not be dropped from
263 * atomic context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000264 */
Jens Axboe165125e2007-07-24 09:28:11 +0200265void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500266{
267 kobject_put(&q->kobj);
268}
Jens Axboed86e0e82011-05-27 07:44:43 +0200269EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500270
Christoph Hellwig8e141f92021-09-29 09:12:40 +0200271void blk_queue_start_drain(struct request_queue *q)
Jens Axboeaed3ea92014-12-22 14:04:42 -0700272{
Ming Leid3cfb2a2017-03-27 20:06:58 +0800273 /*
274 * When queue DYING flag is set, we need to block new req
275 * entering queue, so we call blk_freeze_queue_start() to
276 * prevent I/O from crossing blk_queue_enter().
277 */
278 blk_freeze_queue_start(q);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700279 if (queue_is_mq(q))
Jens Axboeaed3ea92014-12-22 14:04:42 -0700280 blk_mq_wake_waiters(q);
Ming Lei055f6e12017-11-09 10:49:53 -0800281 /* Make blk_queue_enter() reexamine the DYING flag. */
282 wake_up_all(&q->mq_freeze_wq);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700283}
Christoph Hellwig8e141f92021-09-29 09:12:40 +0200284
285void blk_set_queue_dying(struct request_queue *q)
286{
287 blk_queue_flag_set(QUEUE_FLAG_DYING, q);
288 blk_queue_start_drain(q);
289}
Jens Axboeaed3ea92014-12-22 14:04:42 -0700290EXPORT_SYMBOL_GPL(blk_set_queue_dying);
291
Tejun Heod7325802012-03-05 13:14:58 -0800292/**
Tejun Heoc9a929d2011-10-19 14:42:16 +0200293 * blk_cleanup_queue - shutdown a request queue
294 * @q: request queue to shutdown
295 *
Bart Van Asschec246e802012-12-06 14:32:01 +0100296 * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
297 * put it. All future requests will be failed immediately with -ENODEV.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000298 *
299 * Context: can sleep
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500300 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100301void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500302{
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000303 /* cannot be called from atomic context */
304 might_sleep();
305
Bart Van Asschebae85c12019-09-30 16:00:43 -0700306 WARN_ON_ONCE(blk_queue_registered(q));
307
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100308 /* mark @q DYING, no new request or merges will be allowed afterwards */
Jens Axboeaed3ea92014-12-22 14:04:42 -0700309 blk_set_queue_dying(q);
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800310
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100311 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q);
312 blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200313
Bart Van Asschec246e802012-12-06 14:32:01 +0100314 /*
315 * Drain all requests queued before DYING marking. Set DEAD flag to
Bart Van Assche67ed8b72019-08-01 15:39:55 -0700316 * prevent that blk_mq_run_hw_queues() accesses the hardware queues
317 * after draining finished.
Bart Van Asschec246e802012-12-06 14:32:01 +0100318 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400319 blk_freeze_queue(q);
Ming Leic57cdf72018-10-24 21:18:09 +0800320
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100321 blk_queue_flag_set(QUEUE_FLAG_DEAD, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200322
Tejun Heoc9a929d2011-10-19 14:42:16 +0200323 blk_sync_queue(q);
Ming Lei2a19b282021-11-16 09:43:43 +0800324 if (queue_is_mq(q)) {
325 blk_mq_cancel_work_sync(q);
Ming Leic7e2d942019-04-30 09:52:25 +0800326 blk_mq_exit_queue(q);
Ming Lei2a19b282021-11-16 09:43:43 +0800327 }
Jens Axboea1ce35f2018-10-29 10:23:51 -0600328
Ming Leic3e22192019-06-04 21:08:02 +0800329 /*
330 * In theory, request pool of sched_tags belongs to request queue.
331 * However, the current implementation requires tag_set for freeing
332 * requests, so free the pool now.
333 *
334 * Queue has become frozen, there can't be any in-queue requests, so
335 * it is safe to free requests now.
336 */
337 mutex_lock(&q->sysfs_lock);
338 if (q->elevator)
John Garry1820f4f2021-10-05 18:23:31 +0800339 blk_mq_sched_free_rqs(q);
Ming Leic3e22192019-06-04 21:08:02 +0800340 mutex_unlock(&q->sysfs_lock);
341
Dan Williams3ef28e82015-10-21 13:20:12 -0400342 percpu_ref_exit(&q->q_usage_counter);
Bart Van Assche45a9c9d2014-12-09 16:57:48 +0100343
Tejun Heoc9a929d2011-10-19 14:42:16 +0200344 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500345 blk_put_queue(q);
346}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347EXPORT_SYMBOL(blk_cleanup_queue);
348
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800349/**
350 * blk_queue_enter() - try to increase q->q_usage_counter
351 * @q: request queue pointer
Bart Van Asschea4d34da2020-12-08 21:29:50 -0800352 * @flags: BLK_MQ_REQ_NOWAIT and/or BLK_MQ_REQ_PM
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800353 */
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800354int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
Dan Williams3ef28e82015-10-21 13:20:12 -0400355{
Bart Van Asschea4d34da2020-12-08 21:29:50 -0800356 const bool pm = flags & BLK_MQ_REQ_PM;
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800357
Christoph Hellwig1f14a092021-09-29 09:12:38 +0200358 while (!blk_try_enter_queue(q, pm)) {
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800359 if (flags & BLK_MQ_REQ_NOWAIT)
Dan Williams3ef28e82015-10-21 13:20:12 -0400360 return -EBUSY;
361
Ming Lei5ed61d32017-03-27 20:06:56 +0800362 /*
Christoph Hellwig1f14a092021-09-29 09:12:38 +0200363 * read pair of barrier in blk_freeze_queue_start(), we need to
364 * order reading __PERCPU_REF_DEAD flag of .q_usage_counter and
365 * reading .mq_freeze_depth or queue dying flag, otherwise the
366 * following wait may never return if the two reads are
367 * reordered.
Ming Lei5ed61d32017-03-27 20:06:56 +0800368 */
369 smp_rmb();
Alan Jenkins1dc30392018-04-12 19:11:58 +0100370 wait_event(q->mq_freeze_wq,
Bob Liu7996a8b2019-05-21 11:25:55 +0800371 (!q->mq_freeze_depth &&
Alan Stern52abca62020-12-08 21:29:51 -0800372 blk_pm_resume_queue(pm, q)) ||
Alan Jenkins1dc30392018-04-12 19:11:58 +0100373 blk_queue_dying(q));
Dan Williams3ef28e82015-10-21 13:20:12 -0400374 if (blk_queue_dying(q))
375 return -ENODEV;
Dan Williams3ef28e82015-10-21 13:20:12 -0400376 }
Christoph Hellwig1f14a092021-09-29 09:12:38 +0200377
378 return 0;
Dan Williams3ef28e82015-10-21 13:20:12 -0400379}
380
Jens Axboec98cb5b2021-11-04 12:45:51 -0600381int __bio_queue_enter(struct request_queue *q, struct bio *bio)
Christoph Hellwigaccea322020-04-28 13:27:56 +0200382{
Christoph Hellwiga6741532021-09-29 09:12:39 +0200383 while (!blk_try_enter_queue(q, false)) {
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100384 struct gendisk *disk = bio->bi_bdev->bd_disk;
385
Christoph Hellwiga6741532021-09-29 09:12:39 +0200386 if (bio->bi_opf & REQ_NOWAIT) {
Christoph Hellwig8e141f92021-09-29 09:12:40 +0200387 if (test_bit(GD_DEAD, &disk->state))
Christoph Hellwiga6741532021-09-29 09:12:39 +0200388 goto dead;
Christoph Hellwigaccea322020-04-28 13:27:56 +0200389 bio_wouldblock_error(bio);
Christoph Hellwiga6741532021-09-29 09:12:39 +0200390 return -EBUSY;
391 }
392
393 /*
394 * read pair of barrier in blk_freeze_queue_start(), we need to
395 * order reading __PERCPU_REF_DEAD flag of .q_usage_counter and
396 * reading .mq_freeze_depth or queue dying flag, otherwise the
397 * following wait may never return if the two reads are
398 * reordered.
399 */
400 smp_rmb();
401 wait_event(q->mq_freeze_wq,
402 (!q->mq_freeze_depth &&
403 blk_pm_resume_queue(false, q)) ||
Christoph Hellwig8e141f92021-09-29 09:12:40 +0200404 test_bit(GD_DEAD, &disk->state));
405 if (test_bit(GD_DEAD, &disk->state))
Christoph Hellwiga6741532021-09-29 09:12:39 +0200406 goto dead;
Christoph Hellwigaccea322020-04-28 13:27:56 +0200407 }
408
Christoph Hellwiga6741532021-09-29 09:12:39 +0200409 return 0;
410dead:
411 bio_io_error(bio);
412 return -ENODEV;
Christoph Hellwigaccea322020-04-28 13:27:56 +0200413}
414
Dan Williams3ef28e82015-10-21 13:20:12 -0400415void blk_queue_exit(struct request_queue *q)
416{
417 percpu_ref_put(&q->q_usage_counter);
418}
419
420static void blk_queue_usage_counter_release(struct percpu_ref *ref)
421{
422 struct request_queue *q =
423 container_of(ref, struct request_queue, q_usage_counter);
424
425 wake_up_all(&q->mq_freeze_wq);
426}
427
Kees Cookbca237a2017-08-28 15:03:41 -0700428static void blk_rq_timed_out_timer(struct timer_list *t)
Christoph Hellwig287922e2015-10-30 20:57:30 +0800429{
Kees Cookbca237a2017-08-28 15:03:41 -0700430 struct request_queue *q = from_timer(q, t, timeout);
Christoph Hellwig287922e2015-10-30 20:57:30 +0800431
432 kblockd_schedule_work(&q->timeout_work);
433}
434
Tetsuo Handa2e3c18d2019-01-30 22:21:45 +0900435static void blk_timeout_work(struct work_struct *work)
436{
437}
438
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200439struct request_queue *blk_alloc_queue(int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700440{
Jens Axboe165125e2007-07-24 09:28:11 +0200441 struct request_queue *q;
Kent Overstreet338aa962018-05-20 18:25:47 -0400442 int ret;
Christoph Lameter19460892005-06-23 00:08:19 -0700443
Jens Axboe8324aa92008-01-29 14:51:59 +0100444 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100445 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (!q)
447 return NULL;
448
Christoph Hellwigcbf62af2018-05-31 19:11:36 +0200449 q->last_merge = NULL;
Christoph Hellwigcbf62af2018-05-31 19:11:36 +0200450
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100451 q->id = ida_simple_get(&blk_queue_ida, 0, 0, GFP_KERNEL);
Tejun Heoa73f7302011-12-14 00:33:37 +0100452 if (q->id < 0)
Ming Lei3d2936f2014-05-27 23:35:14 +0800453 goto fail_q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100454
Ming Leic495a172021-01-11 11:05:53 +0800455 ret = bioset_init(&q->bio_split, BIO_POOL_SIZE, 0, 0);
Kent Overstreet338aa962018-05-20 18:25:47 -0400456 if (ret)
Kent Overstreet54efd502015-04-23 22:37:18 -0700457 goto fail_id;
458
Jens Axboea83b5762017-03-21 17:20:01 -0600459 q->stats = blk_alloc_queue_stats();
460 if (!q->stats)
Christoph Hellwigedb08722021-08-09 16:17:43 +0200461 goto fail_split;
Jens Axboea83b5762017-03-21 17:20:01 -0600462
Mike Snitzer51514122011-11-23 10:59:13 +0100463 q->node = node_id;
Jens Axboe0989a022009-06-12 14:42:56 +0200464
John Garry079a2e32021-10-05 18:23:39 +0800465 atomic_set(&q->nr_active_requests_shared_tags, 0);
John Garrybccf5e22020-08-19 23:20:26 +0800466
Kees Cookbca237a2017-08-28 15:03:41 -0700467 timer_setup(&q->timeout, blk_rq_timed_out_timer, 0);
Tetsuo Handa2e3c18d2019-01-30 22:21:45 +0900468 INIT_WORK(&q->timeout_work, blk_timeout_work);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100469 INIT_LIST_HEAD(&q->icq_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800470#ifdef CONFIG_BLK_CGROUP
Tejun Heoe8989fa2012-03-05 13:15:20 -0800471 INIT_LIST_HEAD(&q->blkg_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800472#endif
Al Viro483f4af2006-03-18 18:34:37 -0500473
Jens Axboe8324aa92008-01-29 14:51:59 +0100474 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000476 mutex_init(&q->debugfs_mutex);
Al Viro483f4af2006-03-18 18:34:37 -0500477 mutex_init(&q->sysfs_lock);
Ming Leicecf5d82019-08-27 19:01:48 +0800478 mutex_init(&q->sysfs_dir_lock);
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700479 spin_lock_init(&q->queue_lock);
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500480
Jens Axboe320ae512013-10-24 09:20:05 +0100481 init_waitqueue_head(&q->mq_freeze_wq);
Bob Liu7996a8b2019-05-21 11:25:55 +0800482 mutex_init(&q->mq_freeze_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100483
Dan Williams3ef28e82015-10-21 13:20:12 -0400484 /*
485 * Init percpu_ref in atomic mode so that it's faster to shutdown.
486 * See blk_register_queue() for details.
487 */
488 if (percpu_ref_init(&q->q_usage_counter,
489 blk_queue_usage_counter_release,
490 PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
Christoph Hellwigedb08722021-08-09 16:17:43 +0200491 goto fail_stats;
Tejun Heof51b8022012-03-05 13:15:05 -0800492
Dan Williams3ef28e82015-10-21 13:20:12 -0400493 if (blkcg_init_queue(q))
494 goto fail_ref;
495
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100496 blk_queue_dma_alignment(q, 511);
497 blk_set_default_limits(&q->limits);
John Garryd2a27962021-10-05 18:23:27 +0800498 q->nr_requests = BLKDEV_DEFAULT_RQ;
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100501
Dan Williams3ef28e82015-10-21 13:20:12 -0400502fail_ref:
503 percpu_ref_exit(&q->q_usage_counter);
Jens Axboea83b5762017-03-21 17:20:01 -0600504fail_stats:
Christoph Hellwigedb08722021-08-09 16:17:43 +0200505 blk_free_queue_stats(q->stats);
Kent Overstreet54efd502015-04-23 22:37:18 -0700506fail_split:
Kent Overstreet338aa962018-05-20 18:25:47 -0400507 bioset_exit(&q->bio_split);
Tejun Heoa73f7302011-12-14 00:33:37 +0100508fail_id:
509 ida_simple_remove(&blk_queue_ida, q->id);
510fail_q:
511 kmem_cache_free(blk_requestq_cachep, q);
512 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000515/**
516 * blk_get_queue - increment the request_queue refcount
517 * @q: the request_queue structure to increment the refcount for
518 *
519 * Increment the refcount of the request_queue kobject.
Luis Chamberlain763b5892020-06-19 20:47:24 +0000520 *
521 * Context: Any context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000522 */
Tejun Heo09ac46c2011-12-14 00:33:38 +0100523bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100525 if (likely(!blk_queue_dying(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100526 __blk_get_queue(q);
527 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529
Tejun Heo09ac46c2011-12-14 00:33:38 +0100530 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
Jens Axboed86e0e82011-05-27 07:44:43 +0200532EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100534static void handle_bad_sector(struct bio *bio, sector_t maxsector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 char b[BDEVNAME_SIZE];
537
Christoph Hellwig8a3ee672021-09-28 07:27:55 +0200538 pr_info_ratelimited("%s: attempt to access beyond end of device\n"
Tetsuo Handaf4ac7122020-10-08 22:37:23 +0900539 "%s: rw=%d, want=%llu, limit=%llu\n",
Christoph Hellwig8a3ee672021-09-28 07:27:55 +0200540 current->comm,
Tetsuo Handaf4ac7122020-10-08 22:37:23 +0900541 bio_devname(bio, b), bio->bi_opf,
542 bio_end_sector(bio), maxsector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Akinobu Mitac17bb492006-12-08 02:39:46 -0800545#ifdef CONFIG_FAIL_MAKE_REQUEST
546
547static DECLARE_FAULT_ATTR(fail_make_request);
548
549static int __init setup_fail_make_request(char *str)
550{
551 return setup_fault_attr(&fail_make_request, str);
552}
553__setup("fail_make_request=", setup_fail_make_request);
554
Christoph Hellwig06c8c692021-11-17 07:13:58 +0100555bool should_fail_request(struct block_device *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -0800556{
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100557 return part->bd_make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800558}
559
560static int __init fail_make_request_debugfs(void)
561{
Akinobu Mitadd48c082011-08-03 16:21:01 -0700562 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
563 NULL, &fail_make_request);
564
Duan Jiong21f9fcd2014-04-11 15:58:56 +0800565 return PTR_ERR_OR_ZERO(dir);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800566}
567
568late_initcall(fail_make_request_debugfs);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800569#endif /* CONFIG_FAIL_MAKE_REQUEST */
570
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100571static inline bool bio_check_ro(struct bio *bio)
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100572{
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100573 if (op_is_write(bio_op(bio)) && bdev_read_only(bio->bi_bdev)) {
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100574 char b[BDEVNAME_SIZE];
575
Mikulas Patocka8b2ded12018-09-05 16:14:36 -0600576 if (op_is_flush(bio->bi_opf) && !bio_sectors(bio))
577 return false;
578
Linus Torvaldsa32e2362018-08-03 12:22:09 -0700579 WARN_ONCE(1,
Christoph Hellwigc8178672020-07-01 10:59:40 +0200580 "Trying to write to read-only block-device %s (partno %d)\n",
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100581 bio_devname(bio, b), bio->bi_bdev->bd_partno);
Linus Torvaldsa32e2362018-08-03 12:22:09 -0700582 /* Older lvm-tools actually trigger this */
583 return false;
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100584 }
585
586 return false;
587}
588
Howard McLauchlan30abb3a2018-02-06 14:05:39 -0800589static noinline int should_fail_bio(struct bio *bio)
590{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100591 if (should_fail_request(bdev_whole(bio->bi_bdev), bio->bi_iter.bi_size))
Howard McLauchlan30abb3a2018-02-06 14:05:39 -0800592 return -EIO;
593 return 0;
594}
595ALLOW_ERROR_INJECTION(should_fail_bio, ERRNO);
596
Jens Axboec07e2b42007-07-18 13:27:58 +0200597/*
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100598 * Check whether this bio extends beyond the end of the device or partition.
599 * This may well happen - the kernel calls bread() without checking the size of
600 * the device, e.g., when mounting a file system.
601 */
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100602static inline int bio_check_eod(struct bio *bio)
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100603{
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100604 sector_t maxsector = bdev_nr_sectors(bio->bi_bdev);
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100605 unsigned int nr_sectors = bio_sectors(bio);
606
607 if (nr_sectors && maxsector &&
608 (nr_sectors > maxsector ||
609 bio->bi_iter.bi_sector > maxsector - nr_sectors)) {
610 handle_bad_sector(bio, maxsector);
611 return -EIO;
612 }
613 return 0;
614}
615
616/*
Christoph Hellwig74d46992017-08-23 19:10:32 +0200617 * Remap block n of partition p to block n+start(p) of the disk.
618 */
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100619static int blk_partition_remap(struct bio *bio)
Christoph Hellwig74d46992017-08-23 19:10:32 +0200620{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100621 struct block_device *p = bio->bi_bdev;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200622
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100623 if (unlikely(should_fail_request(p, bio->bi_iter.bi_size)))
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100624 return -EIO;
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900625 if (bio_sectors(bio)) {
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100626 bio->bi_iter.bi_sector += p->bd_start_sect;
Christoph Hellwig1c02fca2020-12-03 17:21:38 +0100627 trace_block_bio_remap(bio, p->bd_dev,
Christoph Hellwig29ff57c2020-11-24 09:34:24 +0100628 bio->bi_iter.bi_sector -
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100629 p->bd_start_sect);
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100630 }
Christoph Hellwig30c5d342021-01-24 11:02:36 +0100631 bio_set_flag(bio, BIO_REMAPPED);
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100632 return 0;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200633}
634
Keith Busch0512a752020-05-12 17:55:47 +0900635/*
636 * Check write append to a zoned block device.
637 */
638static inline blk_status_t blk_check_zone_append(struct request_queue *q,
639 struct bio *bio)
640{
641 sector_t pos = bio->bi_iter.bi_sector;
642 int nr_sectors = bio_sectors(bio);
643
644 /* Only applicable to zoned block devices */
645 if (!blk_queue_is_zoned(q))
646 return BLK_STS_NOTSUPP;
647
648 /* The bio sector must point to the start of a sequential zone */
649 if (pos & (blk_queue_zone_sectors(q) - 1) ||
650 !blk_queue_zone_is_seq(q, pos))
651 return BLK_STS_IOERR;
652
653 /*
654 * Not allowed to cross zone boundaries. Otherwise, the BIO will be
655 * split and could result in non-contiguous sectors being written in
656 * different zones.
657 */
658 if (nr_sectors > q->limits.chunk_sectors)
659 return BLK_STS_IOERR;
660
661 /* Make sure the BIO is small enough and will not get split */
662 if (nr_sectors > q->limits.max_zone_append_sectors)
663 return BLK_STS_IOERR;
664
665 bio->bi_opf |= REQ_NOMERGE;
666
667 return BLK_STS_OK;
668}
669
Jens Axboe900e0802021-11-03 05:47:09 -0600670noinline_for_stack bool submit_bio_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100672 struct block_device *bdev = bio->bi_bdev;
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100673 struct request_queue *q = bdev_get_queue(bdev);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200674 blk_status_t status = BLK_STS_IOERR;
Jens Axboe5a473e82020-06-04 11:23:39 -0600675 struct blk_plug *plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Jens Axboe5a473e82020-06-04 11:23:39 -0600679 plug = blk_mq_plug(q, bio);
680 if (plug && plug->nowait)
681 bio->bi_opf |= REQ_NOWAIT;
682
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500683 /*
Jens Axboeb0beb282020-05-28 13:19:29 -0600684 * For a REQ_NOWAIT based request, return -EOPNOTSUPP
Mike Snitzer021a2442020-09-23 16:06:51 -0400685 * if queue does not support NOWAIT.
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500686 */
Mike Snitzer021a2442020-09-23 16:06:51 -0400687 if ((bio->bi_opf & REQ_NOWAIT) && !blk_queue_nowait(q))
Jens Axboeb0beb282020-05-28 13:19:29 -0600688 goto not_supported;
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500689
Howard McLauchlan30abb3a2018-02-06 14:05:39 -0800690 if (should_fail_bio(bio))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200691 goto end_io;
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100692 if (unlikely(bio_check_ro(bio)))
693 goto end_io;
Christoph Hellwig3a905c32021-01-25 19:39:57 +0100694 if (!bio_flagged(bio, BIO_REMAPPED)) {
695 if (unlikely(bio_check_eod(bio)))
696 goto end_io;
697 if (bdev->bd_partno && unlikely(blk_partition_remap(bio)))
698 goto end_io;
699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200701 /*
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200702 * Filter flush bio's early so that bio based drivers without flush
703 * support don't have to worry about them.
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200704 */
Jens Axboef3a8ab72017-01-27 09:08:23 -0700705 if (op_is_flush(bio->bi_opf) &&
Jens Axboec888a8f2016-04-13 13:33:19 -0600706 !test_bit(QUEUE_FLAG_WC, &q->queue_flags)) {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600707 bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
Christoph Hellwige439ab72020-07-01 10:59:42 +0200708 if (!bio_sectors(bio)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200709 status = BLK_STS_OK;
Tejun Heoa7384672008-11-28 13:32:03 +0900710 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Christoph Hellwigd04c4062018-12-14 17:21:22 +0100714 if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
Christoph Hellwig6ce913f2021-10-12 13:12:21 +0200715 bio_clear_polled(bio);
Christoph Hellwigd04c4062018-12-14 17:21:22 +0100716
Christoph Hellwig288dab82016-06-09 16:00:36 +0200717 switch (bio_op(bio)) {
718 case REQ_OP_DISCARD:
719 if (!blk_queue_discard(q))
720 goto not_supported;
721 break;
722 case REQ_OP_SECURE_ERASE:
723 if (!blk_queue_secure_erase(q))
724 goto not_supported;
725 break;
726 case REQ_OP_WRITE_SAME:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200727 if (!q->limits.max_write_same_sectors)
Christoph Hellwig288dab82016-06-09 16:00:36 +0200728 goto not_supported;
Nicolai Stange58886782016-12-04 14:56:39 +0100729 break;
Keith Busch0512a752020-05-12 17:55:47 +0900730 case REQ_OP_ZONE_APPEND:
731 status = blk_check_zone_append(q, bio);
732 if (status != BLK_STS_OK)
733 goto end_io;
734 break;
Shaun Tancheff2d253442016-10-18 15:40:32 +0900735 case REQ_OP_ZONE_RESET:
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900736 case REQ_OP_ZONE_OPEN:
737 case REQ_OP_ZONE_CLOSE:
738 case REQ_OP_ZONE_FINISH:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200739 if (!blk_queue_is_zoned(q))
Shaun Tancheff2d253442016-10-18 15:40:32 +0900740 goto not_supported;
Christoph Hellwig288dab82016-06-09 16:00:36 +0200741 break;
Chaitanya Kulkarni6e33dbf2019-08-01 10:26:36 -0700742 case REQ_OP_ZONE_RESET_ALL:
743 if (!blk_queue_is_zoned(q) || !blk_queue_zone_resetall(q))
744 goto not_supported;
745 break;
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800746 case REQ_OP_WRITE_ZEROES:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200747 if (!q->limits.max_write_zeroes_sectors)
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800748 goto not_supported;
749 break;
Christoph Hellwig288dab82016-06-09 16:00:36 +0200750 default:
751 break;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Laibin Qiub781d8db52021-11-12 17:33:54 +0800754 if (blk_throtl_bio(bio))
Tejun Heoae118892015-08-18 14:55:20 -0700755 return false;
Christoph Hellwigdb18a532020-06-27 09:31:58 +0200756
757 blk_cgroup_bio_start(bio);
758 blkcg_bio_issue_init(bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200759
NeilBrownfbbaf702017-04-07 09:40:52 -0600760 if (!bio_flagged(bio, BIO_TRACE_COMPLETION)) {
Christoph Hellwige8a676d2020-12-03 17:21:36 +0100761 trace_block_bio_queue(bio);
NeilBrownfbbaf702017-04-07 09:40:52 -0600762 /* Now that enqueuing has been traced, we need to trace
763 * completion as well.
764 */
765 bio_set_flag(bio, BIO_TRACE_COMPLETION);
766 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200767 return true;
Tejun Heoa7384672008-11-28 13:32:03 +0900768
Christoph Hellwig288dab82016-06-09 16:00:36 +0200769not_supported:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200770 status = BLK_STS_NOTSUPP;
Tejun Heoa7384672008-11-28 13:32:03 +0900771end_io:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200772 bio->bi_status = status;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200773 bio_endio(bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200774 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Jens Axboe900e0802021-11-03 05:47:09 -0600777static void __submit_bio_fops(struct gendisk *disk, struct bio *bio)
778{
779 if (unlikely(bio_queue_enter(bio) != 0))
780 return;
781 if (submit_bio_checks(bio) && blk_crypto_bio_prep(&bio))
782 disk->fops->submit_bio(bio);
783 blk_queue_exit(disk->queue);
784}
785
Christoph Hellwig3e087732021-10-12 13:12:24 +0200786static void __submit_bio(struct bio *bio)
Christoph Hellwigac7c5672020-05-16 20:28:01 +0200787{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100788 struct gendisk *disk = bio->bi_bdev->bd_disk;
Christoph Hellwigac7c5672020-05-16 20:28:01 +0200789
Jens Axboe900e0802021-11-03 05:47:09 -0600790 if (!disk->fops->submit_bio)
Christoph Hellwig3e087732021-10-12 13:12:24 +0200791 blk_mq_submit_bio(bio);
Jens Axboe900e0802021-11-03 05:47:09 -0600792 else
793 __submit_bio_fops(disk, bio);
Christoph Hellwigac7c5672020-05-16 20:28:01 +0200794}
795
Christoph Hellwig566acf22020-07-01 10:59:45 +0200796/*
797 * The loop in this function may be a bit non-obvious, and so deserves some
798 * explanation:
799 *
800 * - Before entering the loop, bio->bi_next is NULL (as all callers ensure
801 * that), so we have a list with a single bio.
802 * - We pretend that we have just taken it off a longer list, so we assign
803 * bio_list to a pointer to the bio_list_on_stack, thus initialising the
804 * bio_list of new bios to be added. ->submit_bio() may indeed add some more
805 * bios through a recursive call to submit_bio_noacct. If it did, we find a
806 * non-NULL value in bio_list and re-enter the loop from the top.
807 * - In this case we really did just take the bio of the top of the list (no
808 * pretending) and so remove it from bio_list, and call into ->submit_bio()
809 * again.
810 *
811 * bio_list_on_stack[0] contains bios submitted by the current ->submit_bio.
812 * bio_list_on_stack[1] contains bios that were submitted before the current
813 * ->submit_bio_bio, but that haven't been processed yet.
814 */
Christoph Hellwig3e087732021-10-12 13:12:24 +0200815static void __submit_bio_noacct(struct bio *bio)
Christoph Hellwig566acf22020-07-01 10:59:45 +0200816{
817 struct bio_list bio_list_on_stack[2];
Christoph Hellwig566acf22020-07-01 10:59:45 +0200818
819 BUG_ON(bio->bi_next);
820
821 bio_list_init(&bio_list_on_stack[0]);
822 current->bio_list = bio_list_on_stack;
823
824 do {
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100825 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
Christoph Hellwig566acf22020-07-01 10:59:45 +0200826 struct bio_list lower, same;
827
Christoph Hellwig566acf22020-07-01 10:59:45 +0200828 /*
829 * Create a fresh bio_list for all subordinate requests.
830 */
831 bio_list_on_stack[1] = bio_list_on_stack[0];
832 bio_list_init(&bio_list_on_stack[0]);
833
Christoph Hellwig3e087732021-10-12 13:12:24 +0200834 __submit_bio(bio);
Christoph Hellwig566acf22020-07-01 10:59:45 +0200835
836 /*
837 * Sort new bios into those for a lower level and those for the
838 * same level.
839 */
840 bio_list_init(&lower);
841 bio_list_init(&same);
842 while ((bio = bio_list_pop(&bio_list_on_stack[0])) != NULL)
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100843 if (q == bdev_get_queue(bio->bi_bdev))
Christoph Hellwig566acf22020-07-01 10:59:45 +0200844 bio_list_add(&same, bio);
845 else
846 bio_list_add(&lower, bio);
847
848 /*
849 * Now assemble so we handle the lowest level first.
850 */
851 bio_list_merge(&bio_list_on_stack[0], &lower);
852 bio_list_merge(&bio_list_on_stack[0], &same);
853 bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
854 } while ((bio = bio_list_pop(&bio_list_on_stack[0])));
855
856 current->bio_list = NULL;
Christoph Hellwig566acf22020-07-01 10:59:45 +0200857}
858
Christoph Hellwig3e087732021-10-12 13:12:24 +0200859static void __submit_bio_noacct_mq(struct bio *bio)
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200860{
Christoph Hellwig7c792f32020-07-02 21:21:25 +0200861 struct bio_list bio_list[2] = { };
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200862
Christoph Hellwig7c792f32020-07-02 21:21:25 +0200863 current->bio_list = bio_list;
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200864
865 do {
Christoph Hellwig3e087732021-10-12 13:12:24 +0200866 __submit_bio(bio);
Christoph Hellwig7c792f32020-07-02 21:21:25 +0200867 } while ((bio = bio_list_pop(&bio_list[0])));
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200868
869 current->bio_list = NULL;
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200870}
871
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200872/**
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200873 * submit_bio_noacct - re-submit a bio to the block device layer for I/O
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200874 * @bio: The bio describing the location in memory and on the device.
875 *
Christoph Hellwig3fdd4082020-04-28 13:27:53 +0200876 * This is a version of submit_bio() that shall only be used for I/O that is
877 * resubmitted to lower level drivers by stacking block drivers. All file
878 * systems and other upper level users of the block layer should use
879 * submit_bio() instead.
Neil Brownd89d8792007-05-01 09:53:42 +0200880 */
Christoph Hellwig3e087732021-10-12 13:12:24 +0200881void submit_bio_noacct(struct bio *bio)
Neil Brownd89d8792007-05-01 09:53:42 +0200882{
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200883 /*
Christoph Hellwig566acf22020-07-01 10:59:45 +0200884 * We only want one ->submit_bio to be active at a time, else stack
885 * usage with stacked devices could be a problem. Use current->bio_list
886 * to collect a list of requests submited by a ->submit_bio method while
887 * it is active, and then process them after it returned.
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200888 */
Christoph Hellwig3e087732021-10-12 13:12:24 +0200889 if (current->bio_list)
NeilBrownf5fe1b52017-03-10 17:00:47 +1100890 bio_list_add(&current->bio_list[0], bio);
Christoph Hellwig3e087732021-10-12 13:12:24 +0200891 else if (!bio->bi_bdev->bd_disk->fops->submit_bio)
892 __submit_bio_noacct_mq(bio);
893 else
894 __submit_bio_noacct(bio);
Neil Brownd89d8792007-05-01 09:53:42 +0200895}
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200896EXPORT_SYMBOL(submit_bio_noacct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898/**
Randy Dunlap710027a2008-08-19 20:13:11 +0200899 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 * @bio: The &struct bio which describes the I/O
901 *
Christoph Hellwig3fdd4082020-04-28 13:27:53 +0200902 * submit_bio() is used to submit I/O requests to block devices. It is passed a
903 * fully set up &struct bio that describes the I/O that needs to be done. The
Christoph Hellwig309dca302021-01-24 11:02:34 +0100904 * bio will be send to the device described by the bi_bdev field.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 *
Christoph Hellwig3fdd4082020-04-28 13:27:53 +0200906 * The success/failure status of the request, along with notification of
907 * completion, is delivered asynchronously through the ->bi_end_io() callback
908 * in @bio. The bio must NOT be touched by thecaller until ->bi_end_io() has
909 * been called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 */
Christoph Hellwig3e087732021-10-12 13:12:24 +0200911void submit_bio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Tejun Heod3f77df2019-06-27 13:39:52 -0700913 if (blkcg_punt_bio_submit(bio))
Christoph Hellwig3e087732021-10-12 13:12:24 +0200914 return;
Tejun Heod3f77df2019-06-27 13:39:52 -0700915
Jens Axboebf2de6f2007-09-27 13:01:25 +0200916 /*
917 * If it's a regular read/write or a barrier with data attached,
918 * go through the normal accounting stuff before submission.
919 */
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400920 if (bio_has_data(bio)) {
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400921 unsigned int count;
922
Mike Christie95fe6c12016-06-05 14:31:48 -0500923 if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
Christoph Hellwig309dca302021-01-24 11:02:34 +0100924 count = queue_logical_block_size(
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100925 bdev_get_queue(bio->bi_bdev)) >> 9;
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400926 else
927 count = bio_sectors(bio);
928
Mike Christiea8ebb052016-06-05 14:31:45 -0500929 if (op_is_write(bio_op(bio))) {
Jens Axboebf2de6f2007-09-27 13:01:25 +0200930 count_vm_events(PGPGOUT, count);
931 } else {
Kent Overstreet4f024f32013-10-11 15:44:27 -0700932 task_io_account_read(bio->bi_iter.bi_size);
Jens Axboebf2de6f2007-09-27 13:01:25 +0200933 count_vm_events(PGPGIN, count);
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
Johannes Weinerb8e24a92019-08-08 15:03:00 -0400937 /*
Christoph Hellwig760f83e2020-04-28 13:27:54 +0200938 * If we're reading data that is part of the userspace workingset, count
939 * submission time as memory stall. When the device is congested, or
940 * the submitting cgroup IO-throttled, submission can be a significant
941 * part of overall IO time.
Johannes Weinerb8e24a92019-08-08 15:03:00 -0400942 */
Christoph Hellwig760f83e2020-04-28 13:27:54 +0200943 if (unlikely(bio_op(bio) == REQ_OP_READ &&
944 bio_flagged(bio, BIO_WORKINGSET))) {
945 unsigned long pflags;
Christoph Hellwig760f83e2020-04-28 13:27:54 +0200946
Johannes Weinerb8e24a92019-08-08 15:03:00 -0400947 psi_memstall_enter(&pflags);
Christoph Hellwig3e087732021-10-12 13:12:24 +0200948 submit_bio_noacct(bio);
Johannes Weinerb8e24a92019-08-08 15:03:00 -0400949 psi_memstall_leave(&pflags);
Christoph Hellwig3e087732021-10-12 13:12:24 +0200950 return;
Christoph Hellwig760f83e2020-04-28 13:27:54 +0200951 }
952
Christoph Hellwig3e087732021-10-12 13:12:24 +0200953 submit_bio_noacct(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955EXPORT_SYMBOL(submit_bio);
956
Jens Axboe1052b8a2018-11-26 08:21:49 -0700957/**
Christoph Hellwig3e087732021-10-12 13:12:24 +0200958 * bio_poll - poll for BIO completions
959 * @bio: bio to poll for
Yang Guange30028a2021-11-26 00:20:55 +0800960 * @iob: batches of IO
Christoph Hellwig3e087732021-10-12 13:12:24 +0200961 * @flags: BLK_POLL_* flags that control the behavior
962 *
963 * Poll for completions on queue associated with the bio. Returns number of
964 * completed entries found.
965 *
966 * Note: the caller must either be the context that submitted @bio, or
967 * be in a RCU critical section to prevent freeing of @bio.
968 */
Jens Axboe5a72e892021-10-12 09:24:29 -0600969int bio_poll(struct bio *bio, struct io_comp_batch *iob, unsigned int flags)
Christoph Hellwig3e087732021-10-12 13:12:24 +0200970{
Pavel Begunkov859897c2021-10-19 22:24:11 +0100971 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
Christoph Hellwig3e087732021-10-12 13:12:24 +0200972 blk_qc_t cookie = READ_ONCE(bio->bi_cookie);
973 int ret;
974
975 if (cookie == BLK_QC_T_NONE ||
976 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
977 return 0;
978
979 if (current->plug)
Christoph Hellwig008f75a2021-10-20 16:41:19 +0200980 blk_flush_plug(current->plug, false);
Christoph Hellwig3e087732021-10-12 13:12:24 +0200981
982 if (blk_queue_enter(q, BLK_MQ_REQ_NOWAIT))
983 return 0;
984 if (WARN_ON_ONCE(!queue_is_mq(q)))
985 ret = 0; /* not yet implemented, should not happen */
986 else
Jens Axboe5a72e892021-10-12 09:24:29 -0600987 ret = blk_mq_poll(q, cookie, iob, flags);
Christoph Hellwig3e087732021-10-12 13:12:24 +0200988 blk_queue_exit(q);
989 return ret;
990}
991EXPORT_SYMBOL_GPL(bio_poll);
992
993/*
994 * Helper to implement file_operations.iopoll. Requires the bio to be stored
995 * in iocb->private, and cleared before freeing the bio.
996 */
Jens Axboe5a72e892021-10-12 09:24:29 -0600997int iocb_bio_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
998 unsigned int flags)
Christoph Hellwig3e087732021-10-12 13:12:24 +0200999{
1000 struct bio *bio;
1001 int ret = 0;
1002
1003 /*
1004 * Note: the bio cache only uses SLAB_TYPESAFE_BY_RCU, so bio can
1005 * point to a freshly allocated bio at this point. If that happens
1006 * we have a few cases to consider:
1007 *
1008 * 1) the bio is beeing initialized and bi_bdev is NULL. We can just
1009 * simply nothing in this case
1010 * 2) the bio points to a not poll enabled device. bio_poll will catch
1011 * this and return 0
1012 * 3) the bio points to a poll capable device, including but not
1013 * limited to the one that the original bio pointed to. In this
1014 * case we will call into the actual poll method and poll for I/O,
1015 * even if we don't need to, but it won't cause harm either.
1016 *
1017 * For cases 2) and 3) above the RCU grace period ensures that bi_bdev
1018 * is still allocated. Because partitions hold a reference to the whole
1019 * device bdev and thus disk, the disk is also still valid. Grabbing
1020 * a reference to the queue in bio_poll() ensures the hctxs and requests
1021 * are still valid as well.
1022 */
1023 rcu_read_lock();
1024 bio = READ_ONCE(kiocb->private);
1025 if (bio && bio->bi_bdev)
Jens Axboe5a72e892021-10-12 09:24:29 -06001026 ret = bio_poll(bio, iob, flags);
Christoph Hellwig3e087732021-10-12 13:12:24 +02001027 rcu_read_unlock();
1028
1029 return ret;
1030}
1031EXPORT_SYMBOL_GPL(iocb_bio_iopoll);
1032
Christoph Hellwig450b7872021-11-17 07:14:01 +01001033void update_io_ticks(struct block_device *part, unsigned long now, bool end)
Christoph Hellwig9123bf62020-05-27 07:24:13 +02001034{
1035 unsigned long stamp;
1036again:
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001037 stamp = READ_ONCE(part->bd_stamp);
Chunguang Xud80c2282021-07-06 05:47:26 +08001038 if (unlikely(time_after(now, stamp))) {
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001039 if (likely(cmpxchg(&part->bd_stamp, stamp, now) == stamp))
Christoph Hellwig9123bf62020-05-27 07:24:13 +02001040 __part_stat_add(part, io_ticks, end ? now - stamp : 1);
1041 }
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001042 if (part->bd_partno) {
1043 part = bdev_whole(part);
Christoph Hellwig9123bf62020-05-27 07:24:13 +02001044 goto again;
1045 }
1046}
1047
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001048static unsigned long __part_start_io_acct(struct block_device *part,
Song Liu7b264102020-08-31 15:27:23 -07001049 unsigned int sectors, unsigned int op)
Christoph Hellwig956d5102020-05-27 07:24:04 +02001050{
Christoph Hellwig956d5102020-05-27 07:24:04 +02001051 const int sgrp = op_stat_group(op);
1052 unsigned long now = READ_ONCE(jiffies);
1053
1054 part_stat_lock();
1055 update_io_ticks(part, now, false);
1056 part_stat_inc(part, ios[sgrp]);
1057 part_stat_add(part, sectors[sgrp], sectors);
1058 part_stat_local_inc(part, in_flight[op_is_write(op)]);
1059 part_stat_unlock();
1060
1061 return now;
1062}
Song Liu7b264102020-08-31 15:27:23 -07001063
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001064/**
1065 * bio_start_io_acct - start I/O accounting for bio based drivers
1066 * @bio: bio to start account for
1067 *
1068 * Returns the start time that should be passed back to bio_end_io_acct().
1069 */
1070unsigned long bio_start_io_acct(struct bio *bio)
Song Liu7b264102020-08-31 15:27:23 -07001071{
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001072 return __part_start_io_acct(bio->bi_bdev, bio_sectors(bio), bio_op(bio));
Song Liu7b264102020-08-31 15:27:23 -07001073}
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001074EXPORT_SYMBOL_GPL(bio_start_io_acct);
Song Liu7b264102020-08-31 15:27:23 -07001075
1076unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,
1077 unsigned int op)
1078{
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001079 return __part_start_io_acct(disk->part0, sectors, op);
Song Liu7b264102020-08-31 15:27:23 -07001080}
Christoph Hellwig956d5102020-05-27 07:24:04 +02001081EXPORT_SYMBOL(disk_start_io_acct);
1082
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001083static void __part_end_io_acct(struct block_device *part, unsigned int op,
Song Liu7b264102020-08-31 15:27:23 -07001084 unsigned long start_time)
Christoph Hellwig956d5102020-05-27 07:24:04 +02001085{
Christoph Hellwig956d5102020-05-27 07:24:04 +02001086 const int sgrp = op_stat_group(op);
1087 unsigned long now = READ_ONCE(jiffies);
1088 unsigned long duration = now - start_time;
1089
1090 part_stat_lock();
1091 update_io_ticks(part, now, true);
1092 part_stat_add(part, nsecs[sgrp], jiffies_to_nsecs(duration));
1093 part_stat_local_dec(part, in_flight[op_is_write(op)]);
1094 part_stat_unlock();
1095}
Song Liu7b264102020-08-31 15:27:23 -07001096
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001097void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
1098 struct block_device *orig_bdev)
Song Liu7b264102020-08-31 15:27:23 -07001099{
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001100 __part_end_io_acct(orig_bdev, bio_op(bio), start_time);
Song Liu7b264102020-08-31 15:27:23 -07001101}
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001102EXPORT_SYMBOL_GPL(bio_end_io_acct_remapped);
Song Liu7b264102020-08-31 15:27:23 -07001103
1104void disk_end_io_acct(struct gendisk *disk, unsigned int op,
1105 unsigned long start_time)
1106{
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001107 __part_end_io_acct(disk->part0, op, start_time);
Song Liu7b264102020-08-31 15:27:23 -07001108}
Christoph Hellwig956d5102020-05-27 07:24:04 +02001109EXPORT_SYMBOL(disk_end_io_acct);
1110
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02001111/**
1112 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
1113 * @q : the queue of the device being checked
1114 *
1115 * Description:
1116 * Check if underlying low-level drivers of a device are busy.
1117 * If the drivers want to export their busy state, they must set own
1118 * exporting function using blk_queue_lld_busy() first.
1119 *
1120 * Basically, this function is used only by request stacking drivers
1121 * to stop dispatching requests to underlying devices when underlying
1122 * devices are busy. This behavior helps more I/O merging on the queue
1123 * of the request stacking driver and prevents I/O throughput regression
1124 * on burst I/O load.
1125 *
1126 * Return:
1127 * 0 - Not busy (The request stacking driver should dispatch request)
1128 * 1 - Busy (The request stacking driver should stop dispatching request)
1129 */
1130int blk_lld_busy(struct request_queue *q)
1131{
Jens Axboe344e9ff2018-11-15 12:22:51 -07001132 if (queue_is_mq(q) && q->mq_ops->busy)
Jens Axboe9ba20522018-10-29 10:15:10 -06001133 return q->mq_ops->busy(q);
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02001134
1135 return 0;
1136}
1137EXPORT_SYMBOL_GPL(blk_lld_busy);
1138
Jens Axboe59c3d452014-04-08 09:15:35 -06001139int kblockd_schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 return queue_work(kblockd_workqueue, work);
1142}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143EXPORT_SYMBOL(kblockd_schedule_work);
1144
Jens Axboe818cd1c2017-04-10 09:54:55 -06001145int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork,
1146 unsigned long delay)
1147{
1148 return mod_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
1149}
1150EXPORT_SYMBOL(kblockd_mod_delayed_work_on);
1151
Jens Axboe47c122e2021-10-06 06:34:11 -06001152void blk_start_plug_nr_ios(struct blk_plug *plug, unsigned short nr_ios)
1153{
1154 struct task_struct *tsk = current;
1155
1156 /*
1157 * If this is a nested plug, don't actually assign it.
1158 */
1159 if (tsk->plug)
1160 return;
1161
Jens Axboebc490f82021-10-18 10:12:12 -06001162 plug->mq_list = NULL;
Jens Axboe47c122e2021-10-06 06:34:11 -06001163 plug->cached_rq = NULL;
1164 plug->nr_ios = min_t(unsigned short, nr_ios, BLK_MAX_REQUEST_COUNT);
1165 plug->rq_count = 0;
1166 plug->multiple_queues = false;
Jens Axboedc5fc3612021-10-19 06:02:30 -06001167 plug->has_elevator = false;
Jens Axboe47c122e2021-10-06 06:34:11 -06001168 plug->nowait = false;
1169 INIT_LIST_HEAD(&plug->cb_list);
1170
1171 /*
1172 * Store ordering should not be needed here, since a potential
1173 * preempt will imply a full memory barrier
1174 */
1175 tsk->plug = plug;
1176}
1177
Suresh Jayaraman75df7132011-09-21 10:00:16 +02001178/**
1179 * blk_start_plug - initialize blk_plug and track it inside the task_struct
1180 * @plug: The &struct blk_plug that needs to be initialized
1181 *
1182 * Description:
Jeff Moyer40405852019-01-08 16:57:34 -05001183 * blk_start_plug() indicates to the block layer an intent by the caller
1184 * to submit multiple I/O requests in a batch. The block layer may use
1185 * this hint to defer submitting I/Os from the caller until blk_finish_plug()
1186 * is called. However, the block layer may choose to submit requests
1187 * before a call to blk_finish_plug() if the number of queued I/Os
1188 * exceeds %BLK_MAX_REQUEST_COUNT, or if the size of the I/O is larger than
1189 * %BLK_PLUG_FLUSH_SIZE. The queued I/Os may also be submitted early if
1190 * the task schedules (see below).
1191 *
Suresh Jayaraman75df7132011-09-21 10:00:16 +02001192 * Tracking blk_plug inside the task_struct will help with auto-flushing the
1193 * pending I/O should the task end up blocking between blk_start_plug() and
1194 * blk_finish_plug(). This is important from a performance perspective, but
1195 * also ensures that we don't deadlock. For instance, if the task is blocking
1196 * for a memory allocation, memory reclaim could end up wanting to free a
1197 * page belonging to that request that is currently residing in our private
1198 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
1199 * this kind of deadlock.
1200 */
Jens Axboe73c10102011-03-08 13:19:51 +01001201void blk_start_plug(struct blk_plug *plug)
1202{
Jens Axboe47c122e2021-10-06 06:34:11 -06001203 blk_start_plug_nr_ios(plug, 1);
Jens Axboe73c10102011-03-08 13:19:51 +01001204}
1205EXPORT_SYMBOL(blk_start_plug);
1206
NeilBrown74018dc2012-07-31 09:08:15 +02001207static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
NeilBrown048c9372011-04-18 09:52:22 +02001208{
1209 LIST_HEAD(callbacks);
1210
Shaohua Li2a7d5552012-07-31 09:08:15 +02001211 while (!list_empty(&plug->cb_list)) {
1212 list_splice_init(&plug->cb_list, &callbacks);
NeilBrown048c9372011-04-18 09:52:22 +02001213
Shaohua Li2a7d5552012-07-31 09:08:15 +02001214 while (!list_empty(&callbacks)) {
1215 struct blk_plug_cb *cb = list_first_entry(&callbacks,
NeilBrown048c9372011-04-18 09:52:22 +02001216 struct blk_plug_cb,
1217 list);
Shaohua Li2a7d5552012-07-31 09:08:15 +02001218 list_del(&cb->list);
NeilBrown74018dc2012-07-31 09:08:15 +02001219 cb->callback(cb, from_schedule);
Shaohua Li2a7d5552012-07-31 09:08:15 +02001220 }
NeilBrown048c9372011-04-18 09:52:22 +02001221 }
1222}
1223
NeilBrown9cbb1752012-07-31 09:08:14 +02001224struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
1225 int size)
1226{
1227 struct blk_plug *plug = current->plug;
1228 struct blk_plug_cb *cb;
1229
1230 if (!plug)
1231 return NULL;
1232
1233 list_for_each_entry(cb, &plug->cb_list, list)
1234 if (cb->callback == unplug && cb->data == data)
1235 return cb;
1236
1237 /* Not currently on the callback list */
1238 BUG_ON(size < sizeof(*cb));
1239 cb = kzalloc(size, GFP_ATOMIC);
1240 if (cb) {
1241 cb->data = data;
1242 cb->callback = unplug;
1243 list_add(&cb->list, &plug->cb_list);
1244 }
1245 return cb;
1246}
1247EXPORT_SYMBOL(blk_check_plugged);
1248
Christoph Hellwig008f75a2021-10-20 16:41:19 +02001249void blk_flush_plug(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01001250{
Pavel Begunkovb6004552021-10-20 16:41:18 +02001251 if (!list_empty(&plug->cb_list))
1252 flush_plug_callbacks(plug, from_schedule);
Jens Axboebc490f82021-10-18 10:12:12 -06001253 if (!rq_list_empty(plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +01001254 blk_mq_flush_plug_list(plug, from_schedule);
Jens Axboec5fc7b92021-11-03 05:49:07 -06001255 /*
1256 * Unconditionally flush out cached requests, even if the unplug
1257 * event came from schedule. Since we know hold references to the
1258 * queue for cached requests, we don't want a blocked task holding
1259 * up a queue freeze/quiesce event.
1260 */
1261 if (unlikely(!rq_list_empty(plug->cached_rq)))
Jens Axboe47c122e2021-10-06 06:34:11 -06001262 blk_mq_free_plug_rqs(plug);
Jens Axboe73c10102011-03-08 13:19:51 +01001263}
Jens Axboe73c10102011-03-08 13:19:51 +01001264
Jeff Moyer40405852019-01-08 16:57:34 -05001265/**
1266 * blk_finish_plug - mark the end of a batch of submitted I/O
1267 * @plug: The &struct blk_plug passed to blk_start_plug()
1268 *
1269 * Description:
1270 * Indicate that a batch of I/O submissions is complete. This function
1271 * must be paired with an initial call to blk_start_plug(). The intent
1272 * is to allow the block layer to optimize I/O submission. See the
1273 * documentation for blk_start_plug() for more information.
1274 */
Jens Axboe73c10102011-03-08 13:19:51 +01001275void blk_finish_plug(struct blk_plug *plug)
1276{
Christoph Hellwig008f75a2021-10-20 16:41:19 +02001277 if (plug == current->plug) {
1278 blk_flush_plug(plug, false);
1279 current->plug = NULL;
1280 }
Jens Axboe73c10102011-03-08 13:19:51 +01001281}
1282EXPORT_SYMBOL(blk_finish_plug);
1283
Ming Lei71ac8602020-05-14 16:45:09 +08001284void blk_io_schedule(void)
1285{
1286 /* Prevent hang_check timer from firing at us during very long I/O */
1287 unsigned long timeout = sysctl_hung_task_timeout_secs * HZ / 2;
1288
1289 if (timeout)
1290 io_schedule_timeout(timeout);
1291 else
1292 io_schedule();
1293}
1294EXPORT_SYMBOL_GPL(blk_io_schedule);
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296int __init blk_dev_init(void)
1297{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001298 BUILD_BUG_ON(REQ_OP_LAST >= (1 << REQ_OP_BITS));
1299 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001300 sizeof_field(struct request, cmd_flags));
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001301 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001302 sizeof_field(struct bio, bi_opf));
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02001303
Tejun Heo89b90be2011-01-03 15:01:47 +01001304 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
1305 kblockd_workqueue = alloc_workqueue("kblockd",
Matias Bjørling28747fc2014-06-11 23:43:54 +02001306 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (!kblockd_workqueue)
1308 panic("Failed to create kblockd\n");
1309
Ilya Dryomovc2789bd2015-11-20 22:16:46 +01001310 blk_requestq_cachep = kmem_cache_create("request_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02001311 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Omar Sandoval18fbda92017-01-31 14:53:20 -08001313 blk_debugfs_root = debugfs_create_dir("block", NULL);
Omar Sandoval18fbda92017-01-31 14:53:20 -08001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return 0;
1316}