blob: d93e3bb9a769b46a29f6c1b4abbc15441341dd01 [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>
Christoph Hellwig82d981d2021-11-23 19:53:12 +010042#include <linux/part_stat.h>
Ming Lei71ac8602020-05-14 16:45:09 +080043#include <linux/sched/sysctl.h>
Satya Tangiralaa892c8d2020-05-14 00:37:18 +000044#include <linux/blk-crypto.h>
Li Zefan55782132009-06-09 13:43:05 +080045
46#define CREATE_TRACE_POINTS
47#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Jens Axboe8324aa92008-01-29 14:51:59 +010049#include "blk.h"
Christoph Hellwig2aa77452021-11-23 19:53:08 +010050#include "blk-mq-sched.h"
Bart Van Asschebca6b062018-09-26 14:01:03 -070051#include "blk-pm.h"
Jens Axboea7b36ee2021-10-05 09:11:56 -060052#include "blk-throttle.h"
Jens Axboe8324aa92008-01-29 14:51:59 +010053
Omar Sandoval18fbda92017-01-31 14:53:20 -080054struct dentry *blk_debugfs_root;
Omar Sandoval18fbda92017-01-31 14:53:20 -080055
Mike Snitzerd07335e2010-11-16 12:52:38 +010056EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020057EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -070058EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Keith Busch3291fa52014-04-28 12:30:52 -060059EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
NeilBrowncbae8d42012-12-14 20:49:27 +010060EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
Chaitanya Kulkarnib357e4a2021-02-21 21:29:59 -080061EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_insert);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010062
Tejun Heoa73f7302011-12-14 00:33:37 +010063DEFINE_IDA(blk_queue_ida);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * For queue allocation
67 */
Jens Axboe6728cb02008-01-31 13:03:55 +010068struct kmem_cache *blk_requestq_cachep;
Ming Lei704b9142021-12-03 21:15:32 +080069struct kmem_cache *blk_requestq_srcu_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * Controlling structure to kblockd
73 */
Jens Axboeff856ba2006-01-09 16:02:34 +010074static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Bart Van Assche8814ce82018-03-07 17:10:04 -080076/**
77 * blk_queue_flag_set - atomically set a queue flag
78 * @flag: flag to be set
79 * @q: request queue
80 */
81void blk_queue_flag_set(unsigned int flag, struct request_queue *q)
82{
Christoph Hellwig57d74df2018-11-14 17:02:07 +010083 set_bit(flag, &q->queue_flags);
Bart Van Assche8814ce82018-03-07 17:10:04 -080084}
85EXPORT_SYMBOL(blk_queue_flag_set);
86
87/**
88 * blk_queue_flag_clear - atomically clear a queue flag
89 * @flag: flag to be cleared
90 * @q: request queue
91 */
92void blk_queue_flag_clear(unsigned int flag, struct request_queue *q)
93{
Christoph Hellwig57d74df2018-11-14 17:02:07 +010094 clear_bit(flag, &q->queue_flags);
Bart Van Assche8814ce82018-03-07 17:10:04 -080095}
96EXPORT_SYMBOL(blk_queue_flag_clear);
97
98/**
99 * blk_queue_flag_test_and_set - atomically test and set a queue flag
100 * @flag: flag to be set
101 * @q: request queue
102 *
103 * Returns the previous value of @flag - 0 if the flag was not set and 1 if
104 * the flag was already set.
105 */
106bool blk_queue_flag_test_and_set(unsigned int flag, struct request_queue *q)
107{
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100108 return test_and_set_bit(flag, &q->queue_flags);
Bart Van Assche8814ce82018-03-07 17:10:04 -0800109}
110EXPORT_SYMBOL_GPL(blk_queue_flag_test_and_set);
111
Chaitanya Kulkarnie47bc4e2019-06-20 10:59:16 -0700112#define REQ_OP_NAME(name) [REQ_OP_##name] = #name
113static const char *const blk_op_name[] = {
114 REQ_OP_NAME(READ),
115 REQ_OP_NAME(WRITE),
116 REQ_OP_NAME(FLUSH),
117 REQ_OP_NAME(DISCARD),
118 REQ_OP_NAME(SECURE_ERASE),
119 REQ_OP_NAME(ZONE_RESET),
Chaitanya Kulkarni6e33dbf2019-08-01 10:26:36 -0700120 REQ_OP_NAME(ZONE_RESET_ALL),
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900121 REQ_OP_NAME(ZONE_OPEN),
122 REQ_OP_NAME(ZONE_CLOSE),
123 REQ_OP_NAME(ZONE_FINISH),
Keith Busch0512a752020-05-12 17:55:47 +0900124 REQ_OP_NAME(ZONE_APPEND),
Chaitanya Kulkarnie47bc4e2019-06-20 10:59:16 -0700125 REQ_OP_NAME(WRITE_SAME),
126 REQ_OP_NAME(WRITE_ZEROES),
Chaitanya Kulkarnie47bc4e2019-06-20 10:59:16 -0700127 REQ_OP_NAME(DRV_IN),
128 REQ_OP_NAME(DRV_OUT),
129};
130#undef REQ_OP_NAME
131
132/**
133 * blk_op_str - Return string XXX in the REQ_OP_XXX.
134 * @op: REQ_OP_XXX.
135 *
136 * Description: Centralize block layer function to convert REQ_OP_XXX into
137 * string format. Useful in the debugging and tracing bio or request. For
138 * invalid REQ_OP_XXX it returns string "UNKNOWN".
139 */
140inline const char *blk_op_str(unsigned int op)
141{
142 const char *op_str = "UNKNOWN";
143
144 if (op < ARRAY_SIZE(blk_op_name) && blk_op_name[op])
145 op_str = blk_op_name[op];
146
147 return op_str;
148}
149EXPORT_SYMBOL_GPL(blk_op_str);
150
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200151static const struct {
152 int errno;
153 const char *name;
154} blk_errors[] = {
155 [BLK_STS_OK] = { 0, "" },
156 [BLK_STS_NOTSUPP] = { -EOPNOTSUPP, "operation not supported" },
157 [BLK_STS_TIMEOUT] = { -ETIMEDOUT, "timeout" },
158 [BLK_STS_NOSPC] = { -ENOSPC, "critical space allocation" },
159 [BLK_STS_TRANSPORT] = { -ENOLINK, "recoverable transport" },
160 [BLK_STS_TARGET] = { -EREMOTEIO, "critical target" },
161 [BLK_STS_NEXUS] = { -EBADE, "critical nexus" },
162 [BLK_STS_MEDIUM] = { -ENODATA, "critical medium" },
163 [BLK_STS_PROTECTION] = { -EILSEQ, "protection" },
164 [BLK_STS_RESOURCE] = { -ENOMEM, "kernel resource" },
Ming Lei86ff7c22018-01-30 22:04:57 -0500165 [BLK_STS_DEV_RESOURCE] = { -EBUSY, "device resource" },
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500166 [BLK_STS_AGAIN] = { -EAGAIN, "nonblocking retry" },
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200167
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200168 /* device mapper special case, should not leak out: */
169 [BLK_STS_DM_REQUEUE] = { -EREMCHG, "dm internal retry" },
170
Keith Busch3b481d92020-09-24 13:53:28 -0700171 /* zone device specific errors */
172 [BLK_STS_ZONE_OPEN_RESOURCE] = { -ETOOMANYREFS, "open zones exceeded" },
173 [BLK_STS_ZONE_ACTIVE_RESOURCE] = { -EOVERFLOW, "active zones exceeded" },
174
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200175 /* everything else not covered above: */
176 [BLK_STS_IOERR] = { -EIO, "I/O" },
177};
178
179blk_status_t errno_to_blk_status(int errno)
180{
181 int i;
182
183 for (i = 0; i < ARRAY_SIZE(blk_errors); i++) {
184 if (blk_errors[i].errno == errno)
185 return (__force blk_status_t)i;
186 }
187
188 return BLK_STS_IOERR;
189}
190EXPORT_SYMBOL_GPL(errno_to_blk_status);
191
192int blk_status_to_errno(blk_status_t status)
193{
194 int idx = (__force int)status;
195
Bart Van Assche34bd9c12017-06-21 10:55:46 -0700196 if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200197 return -EIO;
198 return blk_errors[idx].errno;
199}
200EXPORT_SYMBOL_GPL(blk_status_to_errno);
201
Christoph Hellwig0d7a29a2021-11-17 07:14:03 +0100202const char *blk_status_to_str(blk_status_t status)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200203{
204 int idx = (__force int)status;
205
Bart Van Assche34bd9c12017-06-21 10:55:46 -0700206 if (WARN_ON_ONCE(idx >= ARRAY_SIZE(blk_errors)))
Christoph Hellwig0d7a29a2021-11-17 07:14:03 +0100207 return "<null>";
208 return blk_errors[idx].name;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/**
212 * blk_sync_queue - cancel any pending callbacks on a queue
213 * @q: the queue
214 *
215 * Description:
216 * The block layer may perform asynchronous callback activity
217 * on a queue, such as calling the unplug function after a timeout.
218 * A block device may call blk_sync_queue to ensure that any
219 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200220 * that the callbacks might use. The caller must already have made sure
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200221 * that its ->submit_bio will not re-add plugging prior to calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 * this function.
223 *
Vivek Goyalda527772011-03-02 19:05:33 -0500224 * This function does not cancel any asynchronous activity arising
Masanari Iidada3dae52014-09-09 01:27:23 +0900225 * out of elevator or throttling code. That would require elevator_exit()
Tejun Heo5efd6112012-03-05 13:15:12 -0800226 * and blkcg_exit_queue() to be called with queue lock initialized.
Vivek Goyalda527772011-03-02 19:05:33 -0500227 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
229void blk_sync_queue(struct request_queue *q)
230{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100231 del_timer_sync(&q->timeout);
Bart Van Assche4e9b6f22017-10-19 10:00:48 -0700232 cancel_work_sync(&q->timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234EXPORT_SYMBOL(blk_sync_queue);
235
236/**
Bart Van Asschecd84a622018-09-26 14:01:04 -0700237 * blk_set_pm_only - increment pm_only counter
Bart Van Asschec9254f22017-11-09 10:49:57 -0800238 * @q: request queue pointer
Bart Van Asschec9254f22017-11-09 10:49:57 -0800239 */
Bart Van Asschecd84a622018-09-26 14:01:04 -0700240void blk_set_pm_only(struct request_queue *q)
Bart Van Asschec9254f22017-11-09 10:49:57 -0800241{
Bart Van Asschecd84a622018-09-26 14:01:04 -0700242 atomic_inc(&q->pm_only);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800243}
Bart Van Asschecd84a622018-09-26 14:01:04 -0700244EXPORT_SYMBOL_GPL(blk_set_pm_only);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800245
Bart Van Asschecd84a622018-09-26 14:01:04 -0700246void blk_clear_pm_only(struct request_queue *q)
Bart Van Asschec9254f22017-11-09 10:49:57 -0800247{
Bart Van Asschecd84a622018-09-26 14:01:04 -0700248 int pm_only;
249
250 pm_only = atomic_dec_return(&q->pm_only);
251 WARN_ON_ONCE(pm_only < 0);
252 if (pm_only == 0)
253 wake_up_all(&q->mq_freeze_wq);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800254}
Bart Van Asschecd84a622018-09-26 14:01:04 -0700255EXPORT_SYMBOL_GPL(blk_clear_pm_only);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800256
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000257/**
258 * blk_put_queue - decrement the request_queue refcount
259 * @q: the request_queue structure to decrement the refcount for
260 *
261 * Decrements the refcount of the request_queue kobject. When this reaches 0
262 * we'll have blk_release_queue() called.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000263 *
264 * Context: Any context, but the last reference must not be dropped from
265 * atomic context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000266 */
Jens Axboe165125e2007-07-24 09:28:11 +0200267void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500268{
269 kobject_put(&q->kobj);
270}
Jens Axboed86e0e82011-05-27 07:44:43 +0200271EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500272
Christoph Hellwig8e141f92021-09-29 09:12:40 +0200273void blk_queue_start_drain(struct request_queue *q)
Jens Axboeaed3ea92014-12-22 14:04:42 -0700274{
Ming Leid3cfb2a2017-03-27 20:06:58 +0800275 /*
276 * When queue DYING flag is set, we need to block new req
277 * entering queue, so we call blk_freeze_queue_start() to
278 * prevent I/O from crossing blk_queue_enter().
279 */
280 blk_freeze_queue_start(q);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700281 if (queue_is_mq(q))
Jens Axboeaed3ea92014-12-22 14:04:42 -0700282 blk_mq_wake_waiters(q);
Ming Lei055f6e12017-11-09 10:49:53 -0800283 /* Make blk_queue_enter() reexamine the DYING flag. */
284 wake_up_all(&q->mq_freeze_wq);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700285}
Christoph Hellwig8e141f92021-09-29 09:12:40 +0200286
287void blk_set_queue_dying(struct request_queue *q)
288{
289 blk_queue_flag_set(QUEUE_FLAG_DYING, q);
290 blk_queue_start_drain(q);
291}
Jens Axboeaed3ea92014-12-22 14:04:42 -0700292EXPORT_SYMBOL_GPL(blk_set_queue_dying);
293
Tejun Heod7325802012-03-05 13:14:58 -0800294/**
Tejun Heoc9a929d2011-10-19 14:42:16 +0200295 * blk_cleanup_queue - shutdown a request queue
296 * @q: request queue to shutdown
297 *
Bart Van Asschec246e802012-12-06 14:32:01 +0100298 * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
299 * put it. All future requests will be failed immediately with -ENODEV.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000300 *
301 * Context: can sleep
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500302 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100303void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500304{
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000305 /* cannot be called from atomic context */
306 might_sleep();
307
Bart Van Asschebae85c12019-09-30 16:00:43 -0700308 WARN_ON_ONCE(blk_queue_registered(q));
309
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100310 /* mark @q DYING, no new request or merges will be allowed afterwards */
Jens Axboeaed3ea92014-12-22 14:04:42 -0700311 blk_set_queue_dying(q);
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800312
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100313 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q);
314 blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200315
Bart Van Asschec246e802012-12-06 14:32:01 +0100316 /*
317 * Drain all requests queued before DYING marking. Set DEAD flag to
Bart Van Assche67ed8b72019-08-01 15:39:55 -0700318 * prevent that blk_mq_run_hw_queues() accesses the hardware queues
319 * after draining finished.
Bart Van Asschec246e802012-12-06 14:32:01 +0100320 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400321 blk_freeze_queue(q);
Ming Leic57cdf72018-10-24 21:18:09 +0800322
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100323 blk_queue_flag_set(QUEUE_FLAG_DEAD, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200324
Tejun Heoc9a929d2011-10-19 14:42:16 +0200325 blk_sync_queue(q);
Ming Lei2a19b282021-11-16 09:43:43 +0800326 if (queue_is_mq(q)) {
327 blk_mq_cancel_work_sync(q);
Ming Leic7e2d942019-04-30 09:52:25 +0800328 blk_mq_exit_queue(q);
Ming Lei2a19b282021-11-16 09:43:43 +0800329 }
Jens Axboea1ce35f2018-10-29 10:23:51 -0600330
Ming Leic3e22192019-06-04 21:08:02 +0800331 /*
332 * In theory, request pool of sched_tags belongs to request queue.
333 * However, the current implementation requires tag_set for freeing
334 * requests, so free the pool now.
335 *
336 * Queue has become frozen, there can't be any in-queue requests, so
337 * it is safe to free requests now.
338 */
339 mutex_lock(&q->sysfs_lock);
340 if (q->elevator)
John Garry1820f4f2021-10-05 18:23:31 +0800341 blk_mq_sched_free_rqs(q);
Ming Leic3e22192019-06-04 21:08:02 +0800342 mutex_unlock(&q->sysfs_lock);
343
Dan Williams3ef28e82015-10-21 13:20:12 -0400344 percpu_ref_exit(&q->q_usage_counter);
Bart Van Assche45a9c9d2014-12-09 16:57:48 +0100345
Tejun Heoc9a929d2011-10-19 14:42:16 +0200346 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500347 blk_put_queue(q);
348}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349EXPORT_SYMBOL(blk_cleanup_queue);
350
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800351/**
352 * blk_queue_enter() - try to increase q->q_usage_counter
353 * @q: request queue pointer
Bart Van Asschea4d34da2020-12-08 21:29:50 -0800354 * @flags: BLK_MQ_REQ_NOWAIT and/or BLK_MQ_REQ_PM
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800355 */
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800356int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
Dan Williams3ef28e82015-10-21 13:20:12 -0400357{
Bart Van Asschea4d34da2020-12-08 21:29:50 -0800358 const bool pm = flags & BLK_MQ_REQ_PM;
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800359
Christoph Hellwig1f14a092021-09-29 09:12:38 +0200360 while (!blk_try_enter_queue(q, pm)) {
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800361 if (flags & BLK_MQ_REQ_NOWAIT)
Dan Williams3ef28e82015-10-21 13:20:12 -0400362 return -EBUSY;
363
Ming Lei5ed61d32017-03-27 20:06:56 +0800364 /*
Christoph Hellwig1f14a092021-09-29 09:12:38 +0200365 * read pair of barrier in blk_freeze_queue_start(), we need to
366 * order reading __PERCPU_REF_DEAD flag of .q_usage_counter and
367 * reading .mq_freeze_depth or queue dying flag, otherwise the
368 * following wait may never return if the two reads are
369 * reordered.
Ming Lei5ed61d32017-03-27 20:06:56 +0800370 */
371 smp_rmb();
Alan Jenkins1dc30392018-04-12 19:11:58 +0100372 wait_event(q->mq_freeze_wq,
Bob Liu7996a8b2019-05-21 11:25:55 +0800373 (!q->mq_freeze_depth &&
Alan Stern52abca62020-12-08 21:29:51 -0800374 blk_pm_resume_queue(pm, q)) ||
Alan Jenkins1dc30392018-04-12 19:11:58 +0100375 blk_queue_dying(q));
Dan Williams3ef28e82015-10-21 13:20:12 -0400376 if (blk_queue_dying(q))
377 return -ENODEV;
Dan Williams3ef28e82015-10-21 13:20:12 -0400378 }
Christoph Hellwig1f14a092021-09-29 09:12:38 +0200379
380 return 0;
Dan Williams3ef28e82015-10-21 13:20:12 -0400381}
382
Jens Axboec98cb5b2021-11-04 12:45:51 -0600383int __bio_queue_enter(struct request_queue *q, struct bio *bio)
Christoph Hellwigaccea322020-04-28 13:27:56 +0200384{
Christoph Hellwiga6741532021-09-29 09:12:39 +0200385 while (!blk_try_enter_queue(q, false)) {
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100386 struct gendisk *disk = bio->bi_bdev->bd_disk;
387
Christoph Hellwiga6741532021-09-29 09:12:39 +0200388 if (bio->bi_opf & REQ_NOWAIT) {
Christoph Hellwig8e141f92021-09-29 09:12:40 +0200389 if (test_bit(GD_DEAD, &disk->state))
Christoph Hellwiga6741532021-09-29 09:12:39 +0200390 goto dead;
Christoph Hellwigaccea322020-04-28 13:27:56 +0200391 bio_wouldblock_error(bio);
Christoph Hellwiga6741532021-09-29 09:12:39 +0200392 return -EBUSY;
393 }
394
395 /*
396 * read pair of barrier in blk_freeze_queue_start(), we need to
397 * order reading __PERCPU_REF_DEAD flag of .q_usage_counter and
398 * reading .mq_freeze_depth or queue dying flag, otherwise the
399 * following wait may never return if the two reads are
400 * reordered.
401 */
402 smp_rmb();
403 wait_event(q->mq_freeze_wq,
404 (!q->mq_freeze_depth &&
405 blk_pm_resume_queue(false, q)) ||
Christoph Hellwig8e141f92021-09-29 09:12:40 +0200406 test_bit(GD_DEAD, &disk->state));
407 if (test_bit(GD_DEAD, &disk->state))
Christoph Hellwiga6741532021-09-29 09:12:39 +0200408 goto dead;
Christoph Hellwigaccea322020-04-28 13:27:56 +0200409 }
410
Christoph Hellwiga6741532021-09-29 09:12:39 +0200411 return 0;
412dead:
413 bio_io_error(bio);
414 return -ENODEV;
Christoph Hellwigaccea322020-04-28 13:27:56 +0200415}
416
Dan Williams3ef28e82015-10-21 13:20:12 -0400417void blk_queue_exit(struct request_queue *q)
418{
419 percpu_ref_put(&q->q_usage_counter);
420}
421
422static void blk_queue_usage_counter_release(struct percpu_ref *ref)
423{
424 struct request_queue *q =
425 container_of(ref, struct request_queue, q_usage_counter);
426
427 wake_up_all(&q->mq_freeze_wq);
428}
429
Kees Cookbca237a2017-08-28 15:03:41 -0700430static void blk_rq_timed_out_timer(struct timer_list *t)
Christoph Hellwig287922e2015-10-30 20:57:30 +0800431{
Kees Cookbca237a2017-08-28 15:03:41 -0700432 struct request_queue *q = from_timer(q, t, timeout);
Christoph Hellwig287922e2015-10-30 20:57:30 +0800433
434 kblockd_schedule_work(&q->timeout_work);
435}
436
Tetsuo Handa2e3c18d2019-01-30 22:21:45 +0900437static void blk_timeout_work(struct work_struct *work)
438{
439}
440
Ming Lei704b9142021-12-03 21:15:32 +0800441struct request_queue *blk_alloc_queue(int node_id, bool alloc_srcu)
Christoph Lameter19460892005-06-23 00:08:19 -0700442{
Jens Axboe165125e2007-07-24 09:28:11 +0200443 struct request_queue *q;
Kent Overstreet338aa962018-05-20 18:25:47 -0400444 int ret;
Christoph Lameter19460892005-06-23 00:08:19 -0700445
Ming Lei704b9142021-12-03 21:15:32 +0800446 q = kmem_cache_alloc_node(blk_get_queue_kmem_cache(alloc_srcu),
447 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (!q)
449 return NULL;
450
Ming Lei704b9142021-12-03 21:15:32 +0800451 if (alloc_srcu) {
452 blk_queue_flag_set(QUEUE_FLAG_HAS_SRCU, q);
453 if (init_srcu_struct(q->srcu) != 0)
454 goto fail_q;
455 }
456
Christoph Hellwigcbf62af2018-05-31 19:11:36 +0200457 q->last_merge = NULL;
Christoph Hellwigcbf62af2018-05-31 19:11:36 +0200458
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100459 q->id = ida_simple_get(&blk_queue_ida, 0, 0, GFP_KERNEL);
Tejun Heoa73f7302011-12-14 00:33:37 +0100460 if (q->id < 0)
Ming Lei704b9142021-12-03 21:15:32 +0800461 goto fail_srcu;
Tejun Heoa73f7302011-12-14 00:33:37 +0100462
Ming Leic495a172021-01-11 11:05:53 +0800463 ret = bioset_init(&q->bio_split, BIO_POOL_SIZE, 0, 0);
Kent Overstreet338aa962018-05-20 18:25:47 -0400464 if (ret)
Kent Overstreet54efd502015-04-23 22:37:18 -0700465 goto fail_id;
466
Jens Axboea83b5762017-03-21 17:20:01 -0600467 q->stats = blk_alloc_queue_stats();
468 if (!q->stats)
Christoph Hellwigedb08722021-08-09 16:17:43 +0200469 goto fail_split;
Jens Axboea83b5762017-03-21 17:20:01 -0600470
Mike Snitzer51514122011-11-23 10:59:13 +0100471 q->node = node_id;
Jens Axboe0989a022009-06-12 14:42:56 +0200472
John Garry079a2e32021-10-05 18:23:39 +0800473 atomic_set(&q->nr_active_requests_shared_tags, 0);
John Garrybccf5e22020-08-19 23:20:26 +0800474
Kees Cookbca237a2017-08-28 15:03:41 -0700475 timer_setup(&q->timeout, blk_rq_timed_out_timer, 0);
Tetsuo Handa2e3c18d2019-01-30 22:21:45 +0900476 INIT_WORK(&q->timeout_work, blk_timeout_work);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100477 INIT_LIST_HEAD(&q->icq_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800478#ifdef CONFIG_BLK_CGROUP
Tejun Heoe8989fa2012-03-05 13:15:20 -0800479 INIT_LIST_HEAD(&q->blkg_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800480#endif
Al Viro483f4af2006-03-18 18:34:37 -0500481
Jens Axboe8324aa92008-01-29 14:51:59 +0100482 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000484 mutex_init(&q->debugfs_mutex);
Al Viro483f4af2006-03-18 18:34:37 -0500485 mutex_init(&q->sysfs_lock);
Ming Leicecf5d82019-08-27 19:01:48 +0800486 mutex_init(&q->sysfs_dir_lock);
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700487 spin_lock_init(&q->queue_lock);
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500488
Jens Axboe320ae512013-10-24 09:20:05 +0100489 init_waitqueue_head(&q->mq_freeze_wq);
Bob Liu7996a8b2019-05-21 11:25:55 +0800490 mutex_init(&q->mq_freeze_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100491
Dan Williams3ef28e82015-10-21 13:20:12 -0400492 /*
493 * Init percpu_ref in atomic mode so that it's faster to shutdown.
494 * See blk_register_queue() for details.
495 */
496 if (percpu_ref_init(&q->q_usage_counter,
497 blk_queue_usage_counter_release,
498 PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
Christoph Hellwigedb08722021-08-09 16:17:43 +0200499 goto fail_stats;
Tejun Heof51b8022012-03-05 13:15:05 -0800500
Dan Williams3ef28e82015-10-21 13:20:12 -0400501 if (blkcg_init_queue(q))
502 goto fail_ref;
503
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100504 blk_queue_dma_alignment(q, 511);
505 blk_set_default_limits(&q->limits);
John Garryd2a27962021-10-05 18:23:27 +0800506 q->nr_requests = BLKDEV_DEFAULT_RQ;
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100509
Dan Williams3ef28e82015-10-21 13:20:12 -0400510fail_ref:
511 percpu_ref_exit(&q->q_usage_counter);
Jens Axboea83b5762017-03-21 17:20:01 -0600512fail_stats:
Christoph Hellwigedb08722021-08-09 16:17:43 +0200513 blk_free_queue_stats(q->stats);
Kent Overstreet54efd502015-04-23 22:37:18 -0700514fail_split:
Kent Overstreet338aa962018-05-20 18:25:47 -0400515 bioset_exit(&q->bio_split);
Tejun Heoa73f7302011-12-14 00:33:37 +0100516fail_id:
517 ida_simple_remove(&blk_queue_ida, q->id);
Ming Lei704b9142021-12-03 21:15:32 +0800518fail_srcu:
519 if (alloc_srcu)
520 cleanup_srcu_struct(q->srcu);
Tejun Heoa73f7302011-12-14 00:33:37 +0100521fail_q:
Ming Lei704b9142021-12-03 21:15:32 +0800522 kmem_cache_free(blk_get_queue_kmem_cache(alloc_srcu), q);
Tejun Heoa73f7302011-12-14 00:33:37 +0100523 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000526/**
527 * blk_get_queue - increment the request_queue refcount
528 * @q: the request_queue structure to increment the refcount for
529 *
530 * Increment the refcount of the request_queue kobject.
Luis Chamberlain763b5892020-06-19 20:47:24 +0000531 *
532 * Context: Any context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000533 */
Tejun Heo09ac46c2011-12-14 00:33:38 +0100534bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100536 if (likely(!blk_queue_dying(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100537 __blk_get_queue(q);
538 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540
Tejun Heo09ac46c2011-12-14 00:33:38 +0100541 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
Jens Axboed86e0e82011-05-27 07:44:43 +0200543EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100545static void handle_bad_sector(struct bio *bio, sector_t maxsector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 char b[BDEVNAME_SIZE];
548
Christoph Hellwig8a3ee672021-09-28 07:27:55 +0200549 pr_info_ratelimited("%s: attempt to access beyond end of device\n"
Tetsuo Handaf4ac7122020-10-08 22:37:23 +0900550 "%s: rw=%d, want=%llu, limit=%llu\n",
Christoph Hellwig8a3ee672021-09-28 07:27:55 +0200551 current->comm,
Tetsuo Handaf4ac7122020-10-08 22:37:23 +0900552 bio_devname(bio, b), bio->bi_opf,
553 bio_end_sector(bio), maxsector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Akinobu Mitac17bb492006-12-08 02:39:46 -0800556#ifdef CONFIG_FAIL_MAKE_REQUEST
557
558static DECLARE_FAULT_ATTR(fail_make_request);
559
560static int __init setup_fail_make_request(char *str)
561{
562 return setup_fault_attr(&fail_make_request, str);
563}
564__setup("fail_make_request=", setup_fail_make_request);
565
Christoph Hellwig06c8c692021-11-17 07:13:58 +0100566bool should_fail_request(struct block_device *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -0800567{
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100568 return part->bd_make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800569}
570
571static int __init fail_make_request_debugfs(void)
572{
Akinobu Mitadd48c082011-08-03 16:21:01 -0700573 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
574 NULL, &fail_make_request);
575
Duan Jiong21f9fcd2014-04-11 15:58:56 +0800576 return PTR_ERR_OR_ZERO(dir);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800577}
578
579late_initcall(fail_make_request_debugfs);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800580#endif /* CONFIG_FAIL_MAKE_REQUEST */
581
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100582static inline bool bio_check_ro(struct bio *bio)
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100583{
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100584 if (op_is_write(bio_op(bio)) && bdev_read_only(bio->bi_bdev)) {
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100585 char b[BDEVNAME_SIZE];
586
Mikulas Patocka8b2ded12018-09-05 16:14:36 -0600587 if (op_is_flush(bio->bi_opf) && !bio_sectors(bio))
588 return false;
589
Linus Torvaldsa32e2362018-08-03 12:22:09 -0700590 WARN_ONCE(1,
Christoph Hellwigc8178672020-07-01 10:59:40 +0200591 "Trying to write to read-only block-device %s (partno %d)\n",
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100592 bio_devname(bio, b), bio->bi_bdev->bd_partno);
Linus Torvaldsa32e2362018-08-03 12:22:09 -0700593 /* Older lvm-tools actually trigger this */
594 return false;
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100595 }
596
597 return false;
598}
599
Howard McLauchlan30abb3a2018-02-06 14:05:39 -0800600static noinline int should_fail_bio(struct bio *bio)
601{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100602 if (should_fail_request(bdev_whole(bio->bi_bdev), bio->bi_iter.bi_size))
Howard McLauchlan30abb3a2018-02-06 14:05:39 -0800603 return -EIO;
604 return 0;
605}
606ALLOW_ERROR_INJECTION(should_fail_bio, ERRNO);
607
Jens Axboec07e2b42007-07-18 13:27:58 +0200608/*
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100609 * Check whether this bio extends beyond the end of the device or partition.
610 * This may well happen - the kernel calls bread() without checking the size of
611 * the device, e.g., when mounting a file system.
612 */
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100613static inline int bio_check_eod(struct bio *bio)
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100614{
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100615 sector_t maxsector = bdev_nr_sectors(bio->bi_bdev);
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100616 unsigned int nr_sectors = bio_sectors(bio);
617
618 if (nr_sectors && maxsector &&
619 (nr_sectors > maxsector ||
620 bio->bi_iter.bi_sector > maxsector - nr_sectors)) {
621 handle_bad_sector(bio, maxsector);
622 return -EIO;
623 }
624 return 0;
625}
626
627/*
Christoph Hellwig74d46992017-08-23 19:10:32 +0200628 * Remap block n of partition p to block n+start(p) of the disk.
629 */
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100630static int blk_partition_remap(struct bio *bio)
Christoph Hellwig74d46992017-08-23 19:10:32 +0200631{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100632 struct block_device *p = bio->bi_bdev;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200633
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100634 if (unlikely(should_fail_request(p, bio->bi_iter.bi_size)))
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100635 return -EIO;
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900636 if (bio_sectors(bio)) {
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100637 bio->bi_iter.bi_sector += p->bd_start_sect;
Christoph Hellwig1c02fca2020-12-03 17:21:38 +0100638 trace_block_bio_remap(bio, p->bd_dev,
Christoph Hellwig29ff57c2020-11-24 09:34:24 +0100639 bio->bi_iter.bi_sector -
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100640 p->bd_start_sect);
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100641 }
Christoph Hellwig30c5d342021-01-24 11:02:36 +0100642 bio_set_flag(bio, BIO_REMAPPED);
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100643 return 0;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200644}
645
Keith Busch0512a752020-05-12 17:55:47 +0900646/*
647 * Check write append to a zoned block device.
648 */
649static inline blk_status_t blk_check_zone_append(struct request_queue *q,
650 struct bio *bio)
651{
652 sector_t pos = bio->bi_iter.bi_sector;
653 int nr_sectors = bio_sectors(bio);
654
655 /* Only applicable to zoned block devices */
656 if (!blk_queue_is_zoned(q))
657 return BLK_STS_NOTSUPP;
658
659 /* The bio sector must point to the start of a sequential zone */
660 if (pos & (blk_queue_zone_sectors(q) - 1) ||
661 !blk_queue_zone_is_seq(q, pos))
662 return BLK_STS_IOERR;
663
664 /*
665 * Not allowed to cross zone boundaries. Otherwise, the BIO will be
666 * split and could result in non-contiguous sectors being written in
667 * different zones.
668 */
669 if (nr_sectors > q->limits.chunk_sectors)
670 return BLK_STS_IOERR;
671
672 /* Make sure the BIO is small enough and will not get split */
673 if (nr_sectors > q->limits.max_zone_append_sectors)
674 return BLK_STS_IOERR;
675
676 bio->bi_opf |= REQ_NOMERGE;
677
678 return BLK_STS_OK;
679}
680
Jens Axboe900e0802021-11-03 05:47:09 -0600681noinline_for_stack bool submit_bio_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100683 struct block_device *bdev = bio->bi_bdev;
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100684 struct request_queue *q = bdev_get_queue(bdev);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200685 blk_status_t status = BLK_STS_IOERR;
Jens Axboe5a473e82020-06-04 11:23:39 -0600686 struct blk_plug *plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Jens Axboe5a473e82020-06-04 11:23:39 -0600690 plug = blk_mq_plug(q, bio);
691 if (plug && plug->nowait)
692 bio->bi_opf |= REQ_NOWAIT;
693
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500694 /*
Jens Axboeb0beb282020-05-28 13:19:29 -0600695 * For a REQ_NOWAIT based request, return -EOPNOTSUPP
Mike Snitzer021a2442020-09-23 16:06:51 -0400696 * if queue does not support NOWAIT.
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500697 */
Mike Snitzer021a2442020-09-23 16:06:51 -0400698 if ((bio->bi_opf & REQ_NOWAIT) && !blk_queue_nowait(q))
Jens Axboeb0beb282020-05-28 13:19:29 -0600699 goto not_supported;
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500700
Howard McLauchlan30abb3a2018-02-06 14:05:39 -0800701 if (should_fail_bio(bio))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200702 goto end_io;
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100703 if (unlikely(bio_check_ro(bio)))
704 goto end_io;
Christoph Hellwig3a905c32021-01-25 19:39:57 +0100705 if (!bio_flagged(bio, BIO_REMAPPED)) {
706 if (unlikely(bio_check_eod(bio)))
707 goto end_io;
708 if (bdev->bd_partno && unlikely(blk_partition_remap(bio)))
709 goto end_io;
710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200712 /*
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200713 * Filter flush bio's early so that bio based drivers without flush
714 * support don't have to worry about them.
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200715 */
Jens Axboef3a8ab72017-01-27 09:08:23 -0700716 if (op_is_flush(bio->bi_opf) &&
Jens Axboec888a8f2016-04-13 13:33:19 -0600717 !test_bit(QUEUE_FLAG_WC, &q->queue_flags)) {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600718 bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
Christoph Hellwige439ab72020-07-01 10:59:42 +0200719 if (!bio_sectors(bio)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200720 status = BLK_STS_OK;
Tejun Heoa7384672008-11-28 13:32:03 +0900721 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Christoph Hellwigd04c4062018-12-14 17:21:22 +0100725 if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
Christoph Hellwig6ce913f2021-10-12 13:12:21 +0200726 bio_clear_polled(bio);
Christoph Hellwigd04c4062018-12-14 17:21:22 +0100727
Christoph Hellwig288dab82016-06-09 16:00:36 +0200728 switch (bio_op(bio)) {
729 case REQ_OP_DISCARD:
730 if (!blk_queue_discard(q))
731 goto not_supported;
732 break;
733 case REQ_OP_SECURE_ERASE:
734 if (!blk_queue_secure_erase(q))
735 goto not_supported;
736 break;
737 case REQ_OP_WRITE_SAME:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200738 if (!q->limits.max_write_same_sectors)
Christoph Hellwig288dab82016-06-09 16:00:36 +0200739 goto not_supported;
Nicolai Stange58886782016-12-04 14:56:39 +0100740 break;
Keith Busch0512a752020-05-12 17:55:47 +0900741 case REQ_OP_ZONE_APPEND:
742 status = blk_check_zone_append(q, bio);
743 if (status != BLK_STS_OK)
744 goto end_io;
745 break;
Shaun Tancheff2d253442016-10-18 15:40:32 +0900746 case REQ_OP_ZONE_RESET:
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900747 case REQ_OP_ZONE_OPEN:
748 case REQ_OP_ZONE_CLOSE:
749 case REQ_OP_ZONE_FINISH:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200750 if (!blk_queue_is_zoned(q))
Shaun Tancheff2d253442016-10-18 15:40:32 +0900751 goto not_supported;
Christoph Hellwig288dab82016-06-09 16:00:36 +0200752 break;
Chaitanya Kulkarni6e33dbf2019-08-01 10:26:36 -0700753 case REQ_OP_ZONE_RESET_ALL:
754 if (!blk_queue_is_zoned(q) || !blk_queue_zone_resetall(q))
755 goto not_supported;
756 break;
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800757 case REQ_OP_WRITE_ZEROES:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200758 if (!q->limits.max_write_zeroes_sectors)
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800759 goto not_supported;
760 break;
Christoph Hellwig288dab82016-06-09 16:00:36 +0200761 default:
762 break;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Laibin Qiub781d8db52021-11-12 17:33:54 +0800765 if (blk_throtl_bio(bio))
Tejun Heoae118892015-08-18 14:55:20 -0700766 return false;
Christoph Hellwigdb18a532020-06-27 09:31:58 +0200767
768 blk_cgroup_bio_start(bio);
769 blkcg_bio_issue_init(bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200770
NeilBrownfbbaf702017-04-07 09:40:52 -0600771 if (!bio_flagged(bio, BIO_TRACE_COMPLETION)) {
Christoph Hellwige8a676d2020-12-03 17:21:36 +0100772 trace_block_bio_queue(bio);
NeilBrownfbbaf702017-04-07 09:40:52 -0600773 /* Now that enqueuing has been traced, we need to trace
774 * completion as well.
775 */
776 bio_set_flag(bio, BIO_TRACE_COMPLETION);
777 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200778 return true;
Tejun Heoa7384672008-11-28 13:32:03 +0900779
Christoph Hellwig288dab82016-06-09 16:00:36 +0200780not_supported:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200781 status = BLK_STS_NOTSUPP;
Tejun Heoa7384672008-11-28 13:32:03 +0900782end_io:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200783 bio->bi_status = status;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200784 bio_endio(bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200785 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
Jens Axboe900e0802021-11-03 05:47:09 -0600788static void __submit_bio_fops(struct gendisk *disk, struct bio *bio)
789{
Ming Lei9d497e22022-01-04 21:42:23 +0800790 if (blk_crypto_bio_prep(&bio)) {
791 if (likely(bio_queue_enter(bio) == 0)) {
792 disk->fops->submit_bio(bio);
793 blk_queue_exit(disk->queue);
794 }
795 }
Jens Axboe900e0802021-11-03 05:47:09 -0600796}
797
Christoph Hellwig3e087732021-10-12 13:12:24 +0200798static void __submit_bio(struct bio *bio)
Christoph Hellwigac7c5672020-05-16 20:28:01 +0200799{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100800 struct gendisk *disk = bio->bi_bdev->bd_disk;
Christoph Hellwigac7c5672020-05-16 20:28:01 +0200801
Ming Lei9d497e22022-01-04 21:42:23 +0800802 if (unlikely(!submit_bio_checks(bio)))
803 return;
804
Jens Axboe900e0802021-11-03 05:47:09 -0600805 if (!disk->fops->submit_bio)
Christoph Hellwig3e087732021-10-12 13:12:24 +0200806 blk_mq_submit_bio(bio);
Jens Axboe900e0802021-11-03 05:47:09 -0600807 else
808 __submit_bio_fops(disk, bio);
Christoph Hellwigac7c5672020-05-16 20:28:01 +0200809}
810
Christoph Hellwig566acf22020-07-01 10:59:45 +0200811/*
812 * The loop in this function may be a bit non-obvious, and so deserves some
813 * explanation:
814 *
815 * - Before entering the loop, bio->bi_next is NULL (as all callers ensure
816 * that), so we have a list with a single bio.
817 * - We pretend that we have just taken it off a longer list, so we assign
818 * bio_list to a pointer to the bio_list_on_stack, thus initialising the
819 * bio_list of new bios to be added. ->submit_bio() may indeed add some more
820 * bios through a recursive call to submit_bio_noacct. If it did, we find a
821 * non-NULL value in bio_list and re-enter the loop from the top.
822 * - In this case we really did just take the bio of the top of the list (no
823 * pretending) and so remove it from bio_list, and call into ->submit_bio()
824 * again.
825 *
826 * bio_list_on_stack[0] contains bios submitted by the current ->submit_bio.
827 * bio_list_on_stack[1] contains bios that were submitted before the current
828 * ->submit_bio_bio, but that haven't been processed yet.
829 */
Christoph Hellwig3e087732021-10-12 13:12:24 +0200830static void __submit_bio_noacct(struct bio *bio)
Christoph Hellwig566acf22020-07-01 10:59:45 +0200831{
832 struct bio_list bio_list_on_stack[2];
Christoph Hellwig566acf22020-07-01 10:59:45 +0200833
834 BUG_ON(bio->bi_next);
835
836 bio_list_init(&bio_list_on_stack[0]);
837 current->bio_list = bio_list_on_stack;
838
839 do {
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100840 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
Christoph Hellwig566acf22020-07-01 10:59:45 +0200841 struct bio_list lower, same;
842
Christoph Hellwig566acf22020-07-01 10:59:45 +0200843 /*
844 * Create a fresh bio_list for all subordinate requests.
845 */
846 bio_list_on_stack[1] = bio_list_on_stack[0];
847 bio_list_init(&bio_list_on_stack[0]);
848
Christoph Hellwig3e087732021-10-12 13:12:24 +0200849 __submit_bio(bio);
Christoph Hellwig566acf22020-07-01 10:59:45 +0200850
851 /*
852 * Sort new bios into those for a lower level and those for the
853 * same level.
854 */
855 bio_list_init(&lower);
856 bio_list_init(&same);
857 while ((bio = bio_list_pop(&bio_list_on_stack[0])) != NULL)
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100858 if (q == bdev_get_queue(bio->bi_bdev))
Christoph Hellwig566acf22020-07-01 10:59:45 +0200859 bio_list_add(&same, bio);
860 else
861 bio_list_add(&lower, bio);
862
863 /*
864 * Now assemble so we handle the lowest level first.
865 */
866 bio_list_merge(&bio_list_on_stack[0], &lower);
867 bio_list_merge(&bio_list_on_stack[0], &same);
868 bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
869 } while ((bio = bio_list_pop(&bio_list_on_stack[0])));
870
871 current->bio_list = NULL;
Christoph Hellwig566acf22020-07-01 10:59:45 +0200872}
873
Christoph Hellwig3e087732021-10-12 13:12:24 +0200874static void __submit_bio_noacct_mq(struct bio *bio)
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200875{
Christoph Hellwig7c792f32020-07-02 21:21:25 +0200876 struct bio_list bio_list[2] = { };
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200877
Christoph Hellwig7c792f32020-07-02 21:21:25 +0200878 current->bio_list = bio_list;
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200879
880 do {
Christoph Hellwig3e087732021-10-12 13:12:24 +0200881 __submit_bio(bio);
Christoph Hellwig7c792f32020-07-02 21:21:25 +0200882 } while ((bio = bio_list_pop(&bio_list[0])));
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200883
884 current->bio_list = NULL;
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200885}
886
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200887/**
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200888 * submit_bio_noacct - re-submit a bio to the block device layer for I/O
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200889 * @bio: The bio describing the location in memory and on the device.
890 *
Christoph Hellwig3fdd4082020-04-28 13:27:53 +0200891 * This is a version of submit_bio() that shall only be used for I/O that is
892 * resubmitted to lower level drivers by stacking block drivers. All file
893 * systems and other upper level users of the block layer should use
894 * submit_bio() instead.
Neil Brownd89d8792007-05-01 09:53:42 +0200895 */
Christoph Hellwig3e087732021-10-12 13:12:24 +0200896void submit_bio_noacct(struct bio *bio)
Neil Brownd89d8792007-05-01 09:53:42 +0200897{
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200898 /*
Christoph Hellwig566acf22020-07-01 10:59:45 +0200899 * We only want one ->submit_bio to be active at a time, else stack
900 * usage with stacked devices could be a problem. Use current->bio_list
901 * to collect a list of requests submited by a ->submit_bio method while
902 * it is active, and then process them after it returned.
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200903 */
Christoph Hellwig3e087732021-10-12 13:12:24 +0200904 if (current->bio_list)
NeilBrownf5fe1b52017-03-10 17:00:47 +1100905 bio_list_add(&current->bio_list[0], bio);
Christoph Hellwig3e087732021-10-12 13:12:24 +0200906 else if (!bio->bi_bdev->bd_disk->fops->submit_bio)
907 __submit_bio_noacct_mq(bio);
908 else
909 __submit_bio_noacct(bio);
Neil Brownd89d8792007-05-01 09:53:42 +0200910}
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200911EXPORT_SYMBOL(submit_bio_noacct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913/**
Randy Dunlap710027a2008-08-19 20:13:11 +0200914 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * @bio: The &struct bio which describes the I/O
916 *
Christoph Hellwig3fdd4082020-04-28 13:27:53 +0200917 * submit_bio() is used to submit I/O requests to block devices. It is passed a
918 * fully set up &struct bio that describes the I/O that needs to be done. The
Christoph Hellwig309dca302021-01-24 11:02:34 +0100919 * bio will be send to the device described by the bi_bdev field.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 *
Christoph Hellwig3fdd4082020-04-28 13:27:53 +0200921 * The success/failure status of the request, along with notification of
922 * completion, is delivered asynchronously through the ->bi_end_io() callback
923 * in @bio. The bio must NOT be touched by thecaller until ->bi_end_io() has
924 * been called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 */
Christoph Hellwig3e087732021-10-12 13:12:24 +0200926void submit_bio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Tejun Heod3f77df2019-06-27 13:39:52 -0700928 if (blkcg_punt_bio_submit(bio))
Christoph Hellwig3e087732021-10-12 13:12:24 +0200929 return;
Tejun Heod3f77df2019-06-27 13:39:52 -0700930
Jens Axboebf2de6f2007-09-27 13:01:25 +0200931 /*
932 * If it's a regular read/write or a barrier with data attached,
933 * go through the normal accounting stuff before submission.
934 */
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400935 if (bio_has_data(bio)) {
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400936 unsigned int count;
937
Mike Christie95fe6c12016-06-05 14:31:48 -0500938 if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
Christoph Hellwig309dca302021-01-24 11:02:34 +0100939 count = queue_logical_block_size(
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100940 bdev_get_queue(bio->bi_bdev)) >> 9;
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400941 else
942 count = bio_sectors(bio);
943
Mike Christiea8ebb052016-06-05 14:31:45 -0500944 if (op_is_write(bio_op(bio))) {
Jens Axboebf2de6f2007-09-27 13:01:25 +0200945 count_vm_events(PGPGOUT, count);
946 } else {
Kent Overstreet4f024f32013-10-11 15:44:27 -0700947 task_io_account_read(bio->bi_iter.bi_size);
Jens Axboebf2de6f2007-09-27 13:01:25 +0200948 count_vm_events(PGPGIN, count);
949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951
Johannes Weinerb8e24a92019-08-08 15:03:00 -0400952 /*
Christoph Hellwig760f83e2020-04-28 13:27:54 +0200953 * If we're reading data that is part of the userspace workingset, count
954 * submission time as memory stall. When the device is congested, or
955 * the submitting cgroup IO-throttled, submission can be a significant
956 * part of overall IO time.
Johannes Weinerb8e24a92019-08-08 15:03:00 -0400957 */
Christoph Hellwig760f83e2020-04-28 13:27:54 +0200958 if (unlikely(bio_op(bio) == REQ_OP_READ &&
959 bio_flagged(bio, BIO_WORKINGSET))) {
960 unsigned long pflags;
Christoph Hellwig760f83e2020-04-28 13:27:54 +0200961
Johannes Weinerb8e24a92019-08-08 15:03:00 -0400962 psi_memstall_enter(&pflags);
Christoph Hellwig3e087732021-10-12 13:12:24 +0200963 submit_bio_noacct(bio);
Johannes Weinerb8e24a92019-08-08 15:03:00 -0400964 psi_memstall_leave(&pflags);
Christoph Hellwig3e087732021-10-12 13:12:24 +0200965 return;
Christoph Hellwig760f83e2020-04-28 13:27:54 +0200966 }
967
Christoph Hellwig3e087732021-10-12 13:12:24 +0200968 submit_bio_noacct(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970EXPORT_SYMBOL(submit_bio);
971
Jens Axboe1052b8a2018-11-26 08:21:49 -0700972/**
Christoph Hellwig3e087732021-10-12 13:12:24 +0200973 * bio_poll - poll for BIO completions
974 * @bio: bio to poll for
Yang Guange30028a2021-11-26 00:20:55 +0800975 * @iob: batches of IO
Christoph Hellwig3e087732021-10-12 13:12:24 +0200976 * @flags: BLK_POLL_* flags that control the behavior
977 *
978 * Poll for completions on queue associated with the bio. Returns number of
979 * completed entries found.
980 *
981 * Note: the caller must either be the context that submitted @bio, or
982 * be in a RCU critical section to prevent freeing of @bio.
983 */
Jens Axboe5a72e892021-10-12 09:24:29 -0600984int bio_poll(struct bio *bio, struct io_comp_batch *iob, unsigned int flags)
Christoph Hellwig3e087732021-10-12 13:12:24 +0200985{
Pavel Begunkov859897c2021-10-19 22:24:11 +0100986 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
Christoph Hellwig3e087732021-10-12 13:12:24 +0200987 blk_qc_t cookie = READ_ONCE(bio->bi_cookie);
988 int ret;
989
990 if (cookie == BLK_QC_T_NONE ||
991 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
992 return 0;
993
994 if (current->plug)
Christoph Hellwig008f75a2021-10-20 16:41:19 +0200995 blk_flush_plug(current->plug, false);
Christoph Hellwig3e087732021-10-12 13:12:24 +0200996
997 if (blk_queue_enter(q, BLK_MQ_REQ_NOWAIT))
998 return 0;
999 if (WARN_ON_ONCE(!queue_is_mq(q)))
1000 ret = 0; /* not yet implemented, should not happen */
1001 else
Jens Axboe5a72e892021-10-12 09:24:29 -06001002 ret = blk_mq_poll(q, cookie, iob, flags);
Christoph Hellwig3e087732021-10-12 13:12:24 +02001003 blk_queue_exit(q);
1004 return ret;
1005}
1006EXPORT_SYMBOL_GPL(bio_poll);
1007
1008/*
1009 * Helper to implement file_operations.iopoll. Requires the bio to be stored
1010 * in iocb->private, and cleared before freeing the bio.
1011 */
Jens Axboe5a72e892021-10-12 09:24:29 -06001012int iocb_bio_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
1013 unsigned int flags)
Christoph Hellwig3e087732021-10-12 13:12:24 +02001014{
1015 struct bio *bio;
1016 int ret = 0;
1017
1018 /*
1019 * Note: the bio cache only uses SLAB_TYPESAFE_BY_RCU, so bio can
1020 * point to a freshly allocated bio at this point. If that happens
1021 * we have a few cases to consider:
1022 *
1023 * 1) the bio is beeing initialized and bi_bdev is NULL. We can just
1024 * simply nothing in this case
1025 * 2) the bio points to a not poll enabled device. bio_poll will catch
1026 * this and return 0
1027 * 3) the bio points to a poll capable device, including but not
1028 * limited to the one that the original bio pointed to. In this
1029 * case we will call into the actual poll method and poll for I/O,
1030 * even if we don't need to, but it won't cause harm either.
1031 *
1032 * For cases 2) and 3) above the RCU grace period ensures that bi_bdev
1033 * is still allocated. Because partitions hold a reference to the whole
1034 * device bdev and thus disk, the disk is also still valid. Grabbing
1035 * a reference to the queue in bio_poll() ensures the hctxs and requests
1036 * are still valid as well.
1037 */
1038 rcu_read_lock();
1039 bio = READ_ONCE(kiocb->private);
1040 if (bio && bio->bi_bdev)
Jens Axboe5a72e892021-10-12 09:24:29 -06001041 ret = bio_poll(bio, iob, flags);
Christoph Hellwig3e087732021-10-12 13:12:24 +02001042 rcu_read_unlock();
1043
1044 return ret;
1045}
1046EXPORT_SYMBOL_GPL(iocb_bio_iopoll);
1047
Christoph Hellwig450b7872021-11-17 07:14:01 +01001048void update_io_ticks(struct block_device *part, unsigned long now, bool end)
Christoph Hellwig9123bf62020-05-27 07:24:13 +02001049{
1050 unsigned long stamp;
1051again:
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001052 stamp = READ_ONCE(part->bd_stamp);
Chunguang Xud80c2282021-07-06 05:47:26 +08001053 if (unlikely(time_after(now, stamp))) {
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001054 if (likely(cmpxchg(&part->bd_stamp, stamp, now) == stamp))
Christoph Hellwig9123bf62020-05-27 07:24:13 +02001055 __part_stat_add(part, io_ticks, end ? now - stamp : 1);
1056 }
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001057 if (part->bd_partno) {
1058 part = bdev_whole(part);
Christoph Hellwig9123bf62020-05-27 07:24:13 +02001059 goto again;
1060 }
1061}
1062
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001063static unsigned long __part_start_io_acct(struct block_device *part,
Mike Snitzere45c47d2022-01-28 10:58:39 -05001064 unsigned int sectors, unsigned int op,
1065 unsigned long start_time)
Christoph Hellwig956d5102020-05-27 07:24:04 +02001066{
Christoph Hellwig956d5102020-05-27 07:24:04 +02001067 const int sgrp = op_stat_group(op);
Christoph Hellwig956d5102020-05-27 07:24:04 +02001068
1069 part_stat_lock();
Mike Snitzere45c47d2022-01-28 10:58:39 -05001070 update_io_ticks(part, start_time, false);
Christoph Hellwig956d5102020-05-27 07:24:04 +02001071 part_stat_inc(part, ios[sgrp]);
1072 part_stat_add(part, sectors[sgrp], sectors);
1073 part_stat_local_inc(part, in_flight[op_is_write(op)]);
1074 part_stat_unlock();
1075
Mike Snitzere45c47d2022-01-28 10:58:39 -05001076 return start_time;
Christoph Hellwig956d5102020-05-27 07:24:04 +02001077}
Song Liu7b264102020-08-31 15:27:23 -07001078
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001079/**
Mike Snitzere45c47d2022-01-28 10:58:39 -05001080 * bio_start_io_acct_time - start I/O accounting for bio based drivers
1081 * @bio: bio to start account for
1082 * @start_time: start time that should be passed back to bio_end_io_acct().
1083 */
1084void bio_start_io_acct_time(struct bio *bio, unsigned long start_time)
1085{
1086 __part_start_io_acct(bio->bi_bdev, bio_sectors(bio),
1087 bio_op(bio), start_time);
1088}
1089EXPORT_SYMBOL_GPL(bio_start_io_acct_time);
1090
1091/**
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001092 * bio_start_io_acct - start I/O accounting for bio based drivers
1093 * @bio: bio to start account for
1094 *
1095 * Returns the start time that should be passed back to bio_end_io_acct().
1096 */
1097unsigned long bio_start_io_acct(struct bio *bio)
Song Liu7b264102020-08-31 15:27:23 -07001098{
Mike Snitzere45c47d2022-01-28 10:58:39 -05001099 return __part_start_io_acct(bio->bi_bdev, bio_sectors(bio),
1100 bio_op(bio), jiffies);
Song Liu7b264102020-08-31 15:27:23 -07001101}
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001102EXPORT_SYMBOL_GPL(bio_start_io_acct);
Song Liu7b264102020-08-31 15:27:23 -07001103
1104unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,
1105 unsigned int op)
1106{
Mike Snitzere45c47d2022-01-28 10:58:39 -05001107 return __part_start_io_acct(disk->part0, sectors, op, jiffies);
Song Liu7b264102020-08-31 15:27:23 -07001108}
Christoph Hellwig956d5102020-05-27 07:24:04 +02001109EXPORT_SYMBOL(disk_start_io_acct);
1110
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001111static void __part_end_io_acct(struct block_device *part, unsigned int op,
Song Liu7b264102020-08-31 15:27:23 -07001112 unsigned long start_time)
Christoph Hellwig956d5102020-05-27 07:24:04 +02001113{
Christoph Hellwig956d5102020-05-27 07:24:04 +02001114 const int sgrp = op_stat_group(op);
1115 unsigned long now = READ_ONCE(jiffies);
1116 unsigned long duration = now - start_time;
1117
1118 part_stat_lock();
1119 update_io_ticks(part, now, true);
1120 part_stat_add(part, nsecs[sgrp], jiffies_to_nsecs(duration));
1121 part_stat_local_dec(part, in_flight[op_is_write(op)]);
1122 part_stat_unlock();
1123}
Song Liu7b264102020-08-31 15:27:23 -07001124
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001125void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
1126 struct block_device *orig_bdev)
Song Liu7b264102020-08-31 15:27:23 -07001127{
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001128 __part_end_io_acct(orig_bdev, bio_op(bio), start_time);
Song Liu7b264102020-08-31 15:27:23 -07001129}
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001130EXPORT_SYMBOL_GPL(bio_end_io_acct_remapped);
Song Liu7b264102020-08-31 15:27:23 -07001131
1132void disk_end_io_acct(struct gendisk *disk, unsigned int op,
1133 unsigned long start_time)
1134{
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001135 __part_end_io_acct(disk->part0, op, start_time);
Song Liu7b264102020-08-31 15:27:23 -07001136}
Christoph Hellwig956d5102020-05-27 07:24:04 +02001137EXPORT_SYMBOL(disk_end_io_acct);
1138
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02001139/**
1140 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
1141 * @q : the queue of the device being checked
1142 *
1143 * Description:
1144 * Check if underlying low-level drivers of a device are busy.
1145 * If the drivers want to export their busy state, they must set own
1146 * exporting function using blk_queue_lld_busy() first.
1147 *
1148 * Basically, this function is used only by request stacking drivers
1149 * to stop dispatching requests to underlying devices when underlying
1150 * devices are busy. This behavior helps more I/O merging on the queue
1151 * of the request stacking driver and prevents I/O throughput regression
1152 * on burst I/O load.
1153 *
1154 * Return:
1155 * 0 - Not busy (The request stacking driver should dispatch request)
1156 * 1 - Busy (The request stacking driver should stop dispatching request)
1157 */
1158int blk_lld_busy(struct request_queue *q)
1159{
Jens Axboe344e9ff2018-11-15 12:22:51 -07001160 if (queue_is_mq(q) && q->mq_ops->busy)
Jens Axboe9ba20522018-10-29 10:15:10 -06001161 return q->mq_ops->busy(q);
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02001162
1163 return 0;
1164}
1165EXPORT_SYMBOL_GPL(blk_lld_busy);
1166
Jens Axboe59c3d452014-04-08 09:15:35 -06001167int kblockd_schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 return queue_work(kblockd_workqueue, work);
1170}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171EXPORT_SYMBOL(kblockd_schedule_work);
1172
Jens Axboe818cd1c2017-04-10 09:54:55 -06001173int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork,
1174 unsigned long delay)
1175{
1176 return mod_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
1177}
1178EXPORT_SYMBOL(kblockd_mod_delayed_work_on);
1179
Jens Axboe47c122e2021-10-06 06:34:11 -06001180void blk_start_plug_nr_ios(struct blk_plug *plug, unsigned short nr_ios)
1181{
1182 struct task_struct *tsk = current;
1183
1184 /*
1185 * If this is a nested plug, don't actually assign it.
1186 */
1187 if (tsk->plug)
1188 return;
1189
Jens Axboebc490f82021-10-18 10:12:12 -06001190 plug->mq_list = NULL;
Jens Axboe47c122e2021-10-06 06:34:11 -06001191 plug->cached_rq = NULL;
1192 plug->nr_ios = min_t(unsigned short, nr_ios, BLK_MAX_REQUEST_COUNT);
1193 plug->rq_count = 0;
1194 plug->multiple_queues = false;
Jens Axboedc5fc3612021-10-19 06:02:30 -06001195 plug->has_elevator = false;
Jens Axboe47c122e2021-10-06 06:34:11 -06001196 plug->nowait = false;
1197 INIT_LIST_HEAD(&plug->cb_list);
1198
1199 /*
1200 * Store ordering should not be needed here, since a potential
1201 * preempt will imply a full memory barrier
1202 */
1203 tsk->plug = plug;
1204}
1205
Suresh Jayaraman75df7132011-09-21 10:00:16 +02001206/**
1207 * blk_start_plug - initialize blk_plug and track it inside the task_struct
1208 * @plug: The &struct blk_plug that needs to be initialized
1209 *
1210 * Description:
Jeff Moyer40405852019-01-08 16:57:34 -05001211 * blk_start_plug() indicates to the block layer an intent by the caller
1212 * to submit multiple I/O requests in a batch. The block layer may use
1213 * this hint to defer submitting I/Os from the caller until blk_finish_plug()
1214 * is called. However, the block layer may choose to submit requests
1215 * before a call to blk_finish_plug() if the number of queued I/Os
1216 * exceeds %BLK_MAX_REQUEST_COUNT, or if the size of the I/O is larger than
1217 * %BLK_PLUG_FLUSH_SIZE. The queued I/Os may also be submitted early if
1218 * the task schedules (see below).
1219 *
Suresh Jayaraman75df7132011-09-21 10:00:16 +02001220 * Tracking blk_plug inside the task_struct will help with auto-flushing the
1221 * pending I/O should the task end up blocking between blk_start_plug() and
1222 * blk_finish_plug(). This is important from a performance perspective, but
1223 * also ensures that we don't deadlock. For instance, if the task is blocking
1224 * for a memory allocation, memory reclaim could end up wanting to free a
1225 * page belonging to that request that is currently residing in our private
1226 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
1227 * this kind of deadlock.
1228 */
Jens Axboe73c10102011-03-08 13:19:51 +01001229void blk_start_plug(struct blk_plug *plug)
1230{
Jens Axboe47c122e2021-10-06 06:34:11 -06001231 blk_start_plug_nr_ios(plug, 1);
Jens Axboe73c10102011-03-08 13:19:51 +01001232}
1233EXPORT_SYMBOL(blk_start_plug);
1234
NeilBrown74018dc2012-07-31 09:08:15 +02001235static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
NeilBrown048c9372011-04-18 09:52:22 +02001236{
1237 LIST_HEAD(callbacks);
1238
Shaohua Li2a7d5552012-07-31 09:08:15 +02001239 while (!list_empty(&plug->cb_list)) {
1240 list_splice_init(&plug->cb_list, &callbacks);
NeilBrown048c9372011-04-18 09:52:22 +02001241
Shaohua Li2a7d5552012-07-31 09:08:15 +02001242 while (!list_empty(&callbacks)) {
1243 struct blk_plug_cb *cb = list_first_entry(&callbacks,
NeilBrown048c9372011-04-18 09:52:22 +02001244 struct blk_plug_cb,
1245 list);
Shaohua Li2a7d5552012-07-31 09:08:15 +02001246 list_del(&cb->list);
NeilBrown74018dc2012-07-31 09:08:15 +02001247 cb->callback(cb, from_schedule);
Shaohua Li2a7d5552012-07-31 09:08:15 +02001248 }
NeilBrown048c9372011-04-18 09:52:22 +02001249 }
1250}
1251
NeilBrown9cbb1752012-07-31 09:08:14 +02001252struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
1253 int size)
1254{
1255 struct blk_plug *plug = current->plug;
1256 struct blk_plug_cb *cb;
1257
1258 if (!plug)
1259 return NULL;
1260
1261 list_for_each_entry(cb, &plug->cb_list, list)
1262 if (cb->callback == unplug && cb->data == data)
1263 return cb;
1264
1265 /* Not currently on the callback list */
1266 BUG_ON(size < sizeof(*cb));
1267 cb = kzalloc(size, GFP_ATOMIC);
1268 if (cb) {
1269 cb->data = data;
1270 cb->callback = unplug;
1271 list_add(&cb->list, &plug->cb_list);
1272 }
1273 return cb;
1274}
1275EXPORT_SYMBOL(blk_check_plugged);
1276
Christoph Hellwig008f75a2021-10-20 16:41:19 +02001277void blk_flush_plug(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01001278{
Pavel Begunkovb6004552021-10-20 16:41:18 +02001279 if (!list_empty(&plug->cb_list))
1280 flush_plug_callbacks(plug, from_schedule);
Jens Axboebc490f82021-10-18 10:12:12 -06001281 if (!rq_list_empty(plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +01001282 blk_mq_flush_plug_list(plug, from_schedule);
Jens Axboec5fc7b92021-11-03 05:49:07 -06001283 /*
1284 * Unconditionally flush out cached requests, even if the unplug
1285 * event came from schedule. Since we know hold references to the
1286 * queue for cached requests, we don't want a blocked task holding
1287 * up a queue freeze/quiesce event.
1288 */
1289 if (unlikely(!rq_list_empty(plug->cached_rq)))
Jens Axboe47c122e2021-10-06 06:34:11 -06001290 blk_mq_free_plug_rqs(plug);
Jens Axboe73c10102011-03-08 13:19:51 +01001291}
Jens Axboe73c10102011-03-08 13:19:51 +01001292
Jeff Moyer40405852019-01-08 16:57:34 -05001293/**
1294 * blk_finish_plug - mark the end of a batch of submitted I/O
1295 * @plug: The &struct blk_plug passed to blk_start_plug()
1296 *
1297 * Description:
1298 * Indicate that a batch of I/O submissions is complete. This function
1299 * must be paired with an initial call to blk_start_plug(). The intent
1300 * is to allow the block layer to optimize I/O submission. See the
1301 * documentation for blk_start_plug() for more information.
1302 */
Jens Axboe73c10102011-03-08 13:19:51 +01001303void blk_finish_plug(struct blk_plug *plug)
1304{
Christoph Hellwig008f75a2021-10-20 16:41:19 +02001305 if (plug == current->plug) {
1306 blk_flush_plug(plug, false);
1307 current->plug = NULL;
1308 }
Jens Axboe73c10102011-03-08 13:19:51 +01001309}
1310EXPORT_SYMBOL(blk_finish_plug);
1311
Ming Lei71ac8602020-05-14 16:45:09 +08001312void blk_io_schedule(void)
1313{
1314 /* Prevent hang_check timer from firing at us during very long I/O */
1315 unsigned long timeout = sysctl_hung_task_timeout_secs * HZ / 2;
1316
1317 if (timeout)
1318 io_schedule_timeout(timeout);
1319 else
1320 io_schedule();
1321}
1322EXPORT_SYMBOL_GPL(blk_io_schedule);
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324int __init blk_dev_init(void)
1325{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001326 BUILD_BUG_ON(REQ_OP_LAST >= (1 << REQ_OP_BITS));
1327 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001328 sizeof_field(struct request, cmd_flags));
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001329 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001330 sizeof_field(struct bio, bi_opf));
Ming Lei704b9142021-12-03 21:15:32 +08001331 BUILD_BUG_ON(ALIGN(offsetof(struct request_queue, srcu),
1332 __alignof__(struct request_queue)) !=
1333 sizeof(struct request_queue));
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02001334
Tejun Heo89b90be2011-01-03 15:01:47 +01001335 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
1336 kblockd_workqueue = alloc_workqueue("kblockd",
Matias Bjørling28747fc2014-06-11 23:43:54 +02001337 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (!kblockd_workqueue)
1339 panic("Failed to create kblockd\n");
1340
Ilya Dryomovc2789bd2015-11-20 22:16:46 +01001341 blk_requestq_cachep = kmem_cache_create("request_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02001342 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Ming Lei704b9142021-12-03 21:15:32 +08001344 blk_requestq_srcu_cachep = kmem_cache_create("request_queue_srcu",
1345 sizeof(struct request_queue) +
1346 sizeof(struct srcu_struct), 0, SLAB_PANIC, NULL);
1347
Omar Sandoval18fbda92017-01-31 14:53:20 -08001348 blk_debugfs_root = debugfs_create_dir("block", NULL);
Omar Sandoval18fbda92017-01-31 14:53:20 -08001349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return 0;
1351}