blob: 29c4db03742b425af138d7598ed69fe3b9bbf084 [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>
Jens Axboe320ae512013-10-24 09:20:05 +010019#include <linux/blk-mq.h>
Alan Stern52abca62020-12-08 21:29:51 -080020#include <linux/blk-pm.h>
Christoph Hellwigfe45e632021-09-20 14:33:27 +020021#include <linux/blk-integrity.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/highmem.h>
23#include <linux/mm.h>
Matthew Wilcox (Oracle)cee9a0c2020-06-01 21:46:07 -070024#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kernel_stat.h>
26#include <linux/string.h>
27#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/completion.h>
29#include <linux/slab.h>
30#include <linux/swap.h>
31#include <linux/writeback.h>
Andrew Mortonfaccbd4b2006-12-10 02:19:35 -080032#include <linux/task_io_accounting_ops.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080033#include <linux/fault-inject.h>
Jens Axboe73c10102011-03-08 13:19:51 +010034#include <linux/list_sort.h>
Tejun Heoe3c78ca2011-10-19 14:32:38 +020035#include <linux/delay.h>
Tejun Heoaaf7c682012-04-19 16:29:22 -070036#include <linux/ratelimit.h>
Lin Ming6c954662013-03-23 11:42:26 +080037#include <linux/pm_runtime.h>
Tejun Heoeea8f412015-05-22 17:13:17 -040038#include <linux/blk-cgroup.h>
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +030039#include <linux/t10-pi.h>
Omar Sandoval18fbda92017-01-31 14:53:20 -080040#include <linux/debugfs.h>
Howard McLauchlan30abb3a2018-02-06 14:05:39 -080041#include <linux/bpf.h>
Johannes Weinerb8e24a92019-08-08 15:03:00 -040042#include <linux/psi.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"
Ming Lei43a5e4e2013-12-26 21:31:35 +080050#include "blk-mq.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070051#include "blk-mq-sched.h"
Bart Van Asschebca6b062018-09-26 14:01:03 -070052#include "blk-pm.h"
Jens Axboea7b36ee2021-10-05 09:11:56 -060053#include "blk-throttle.h"
Jens Axboe8324aa92008-01-29 14:51:59 +010054
Omar Sandoval18fbda92017-01-31 14:53:20 -080055struct dentry *blk_debugfs_root;
Omar Sandoval18fbda92017-01-31 14:53:20 -080056
Mike Snitzerd07335e2010-11-16 12:52:38 +010057EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020058EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -070059EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Keith Busch3291fa52014-04-28 12:30:52 -060060EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
NeilBrowncbae8d42012-12-14 20:49:27 +010061EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
Chaitanya Kulkarnib357e4a2021-02-21 21:29:59 -080062EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_insert);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010063
Tejun Heoa73f7302011-12-14 00:33:37 +010064DEFINE_IDA(blk_queue_ida);
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * For queue allocation
68 */
Jens Axboe6728cb02008-01-31 13:03:55 +010069struct kmem_cache *blk_requestq_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
Jens Axboe9be3e062021-10-14 09:17:01 -0600202void blk_print_req_error(struct request *req, 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 Hellwig2a842ac2017-06-03 09:38:04 +0200207 return;
208
Christoph Hellwig178cc592019-06-20 10:59:15 -0700209 printk_ratelimited(KERN_ERR
Jens Axboec477b792021-10-14 09:15:40 -0600210 "%s error, dev %s, sector %llu op 0x%x:(%s) flags 0x%x "
Chaitanya Kulkarnib0e51682019-06-20 10:59:18 -0700211 "phys_seg %u prio class %u\n",
Jens Axboec477b792021-10-14 09:15:40 -0600212 blk_errors[idx].name,
Chaitanya Kulkarnib0e51682019-06-20 10:59:18 -0700213 req->rq_disk ? req->rq_disk->disk_name : "?",
214 blk_rq_pos(req), req_op(req), blk_op_str(req_op(req)),
215 req->cmd_flags & ~REQ_OP_MASK,
216 req->nr_phys_segments,
217 IOPRIO_PRIO_CLASS(req->ioprio));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220void blk_dump_rq_flags(struct request *rq, char *msg)
221{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100222 printk(KERN_INFO "%s: dev %s: flags=%llx\n", msg,
223 rq->rq_disk ? rq->rq_disk->disk_name : "?",
Jens Axboe59533162013-05-23 12:25:08 +0200224 (unsigned long long) rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Tejun Heo83096eb2009-05-07 22:24:39 +0900226 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
227 (unsigned long long)blk_rq_pos(rq),
228 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Jens Axboeb4f42e22014-04-10 09:46:28 -0600229 printk(KERN_INFO " bio %p, biotail %p, len %u\n",
230 rq->bio, rq->biotail, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232EXPORT_SYMBOL(blk_dump_rq_flags);
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/**
235 * blk_sync_queue - cancel any pending callbacks on a queue
236 * @q: the queue
237 *
238 * Description:
239 * The block layer may perform asynchronous callback activity
240 * on a queue, such as calling the unplug function after a timeout.
241 * A block device may call blk_sync_queue to ensure that any
242 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200243 * that the callbacks might use. The caller must already have made sure
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200244 * that its ->submit_bio will not re-add plugging prior to calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 * this function.
246 *
Vivek Goyalda527772011-03-02 19:05:33 -0500247 * This function does not cancel any asynchronous activity arising
Masanari Iidada3dae52014-09-09 01:27:23 +0900248 * out of elevator or throttling code. That would require elevator_exit()
Tejun Heo5efd6112012-03-05 13:15:12 -0800249 * and blkcg_exit_queue() to be called with queue lock initialized.
Vivek Goyalda527772011-03-02 19:05:33 -0500250 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 */
252void blk_sync_queue(struct request_queue *q)
253{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100254 del_timer_sync(&q->timeout);
Bart Van Assche4e9b6f22017-10-19 10:00:48 -0700255 cancel_work_sync(&q->timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257EXPORT_SYMBOL(blk_sync_queue);
258
259/**
Bart Van Asschecd84a622018-09-26 14:01:04 -0700260 * blk_set_pm_only - increment pm_only counter
Bart Van Asschec9254f22017-11-09 10:49:57 -0800261 * @q: request queue pointer
Bart Van Asschec9254f22017-11-09 10:49:57 -0800262 */
Bart Van Asschecd84a622018-09-26 14:01:04 -0700263void blk_set_pm_only(struct request_queue *q)
Bart Van Asschec9254f22017-11-09 10:49:57 -0800264{
Bart Van Asschecd84a622018-09-26 14:01:04 -0700265 atomic_inc(&q->pm_only);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800266}
Bart Van Asschecd84a622018-09-26 14:01:04 -0700267EXPORT_SYMBOL_GPL(blk_set_pm_only);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800268
Bart Van Asschecd84a622018-09-26 14:01:04 -0700269void blk_clear_pm_only(struct request_queue *q)
Bart Van Asschec9254f22017-11-09 10:49:57 -0800270{
Bart Van Asschecd84a622018-09-26 14:01:04 -0700271 int pm_only;
272
273 pm_only = atomic_dec_return(&q->pm_only);
274 WARN_ON_ONCE(pm_only < 0);
275 if (pm_only == 0)
276 wake_up_all(&q->mq_freeze_wq);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800277}
Bart Van Asschecd84a622018-09-26 14:01:04 -0700278EXPORT_SYMBOL_GPL(blk_clear_pm_only);
Bart Van Asschec9254f22017-11-09 10:49:57 -0800279
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000280/**
281 * blk_put_queue - decrement the request_queue refcount
282 * @q: the request_queue structure to decrement the refcount for
283 *
284 * Decrements the refcount of the request_queue kobject. When this reaches 0
285 * we'll have blk_release_queue() called.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000286 *
287 * Context: Any context, but the last reference must not be dropped from
288 * atomic context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000289 */
Jens Axboe165125e2007-07-24 09:28:11 +0200290void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500291{
292 kobject_put(&q->kobj);
293}
Jens Axboed86e0e82011-05-27 07:44:43 +0200294EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500295
Christoph Hellwig8e141f92021-09-29 09:12:40 +0200296void blk_queue_start_drain(struct request_queue *q)
Jens Axboeaed3ea92014-12-22 14:04:42 -0700297{
Ming Leid3cfb2a2017-03-27 20:06:58 +0800298 /*
299 * When queue DYING flag is set, we need to block new req
300 * entering queue, so we call blk_freeze_queue_start() to
301 * prevent I/O from crossing blk_queue_enter().
302 */
303 blk_freeze_queue_start(q);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700304 if (queue_is_mq(q))
Jens Axboeaed3ea92014-12-22 14:04:42 -0700305 blk_mq_wake_waiters(q);
Ming Lei055f6e12017-11-09 10:49:53 -0800306 /* Make blk_queue_enter() reexamine the DYING flag. */
307 wake_up_all(&q->mq_freeze_wq);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700308}
Christoph Hellwig8e141f92021-09-29 09:12:40 +0200309
310void blk_set_queue_dying(struct request_queue *q)
311{
312 blk_queue_flag_set(QUEUE_FLAG_DYING, q);
313 blk_queue_start_drain(q);
314}
Jens Axboeaed3ea92014-12-22 14:04:42 -0700315EXPORT_SYMBOL_GPL(blk_set_queue_dying);
316
Tejun Heod7325802012-03-05 13:14:58 -0800317/**
Tejun Heoc9a929d2011-10-19 14:42:16 +0200318 * blk_cleanup_queue - shutdown a request queue
319 * @q: request queue to shutdown
320 *
Bart Van Asschec246e802012-12-06 14:32:01 +0100321 * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
322 * put it. All future requests will be failed immediately with -ENODEV.
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000323 *
324 * Context: can sleep
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500325 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100326void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500327{
Luis Chamberlaine8c7d142020-06-19 20:47:25 +0000328 /* cannot be called from atomic context */
329 might_sleep();
330
Bart Van Asschebae85c12019-09-30 16:00:43 -0700331 WARN_ON_ONCE(blk_queue_registered(q));
332
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100333 /* mark @q DYING, no new request or merges will be allowed afterwards */
Jens Axboeaed3ea92014-12-22 14:04:42 -0700334 blk_set_queue_dying(q);
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800335
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100336 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, q);
337 blk_queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200338
Bart Van Asschec246e802012-12-06 14:32:01 +0100339 /*
340 * Drain all requests queued before DYING marking. Set DEAD flag to
Bart Van Assche67ed8b72019-08-01 15:39:55 -0700341 * prevent that blk_mq_run_hw_queues() accesses the hardware queues
342 * after draining finished.
Bart Van Asschec246e802012-12-06 14:32:01 +0100343 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400344 blk_freeze_queue(q);
Ming Leic57cdf72018-10-24 21:18:09 +0800345
Christoph Hellwig57d74df2018-11-14 17:02:07 +0100346 blk_queue_flag_set(QUEUE_FLAG_DEAD, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200347
Tejun Heoc9a929d2011-10-19 14:42:16 +0200348 blk_sync_queue(q);
Ming Lei2a19b282021-11-16 09:43:43 +0800349 if (queue_is_mq(q)) {
350 blk_mq_cancel_work_sync(q);
Ming Leic7e2d942019-04-30 09:52:25 +0800351 blk_mq_exit_queue(q);
Ming Lei2a19b282021-11-16 09:43:43 +0800352 }
Jens Axboea1ce35f2018-10-29 10:23:51 -0600353
Ming Leic3e22192019-06-04 21:08:02 +0800354 /*
355 * In theory, request pool of sched_tags belongs to request queue.
356 * However, the current implementation requires tag_set for freeing
357 * requests, so free the pool now.
358 *
359 * Queue has become frozen, there can't be any in-queue requests, so
360 * it is safe to free requests now.
361 */
362 mutex_lock(&q->sysfs_lock);
363 if (q->elevator)
John Garry1820f4f2021-10-05 18:23:31 +0800364 blk_mq_sched_free_rqs(q);
Ming Leic3e22192019-06-04 21:08:02 +0800365 mutex_unlock(&q->sysfs_lock);
366
Dan Williams3ef28e82015-10-21 13:20:12 -0400367 percpu_ref_exit(&q->q_usage_counter);
Bart Van Assche45a9c9d2014-12-09 16:57:48 +0100368
Tejun Heoc9a929d2011-10-19 14:42:16 +0200369 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500370 blk_put_queue(q);
371}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372EXPORT_SYMBOL(blk_cleanup_queue);
373
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800374/**
375 * blk_queue_enter() - try to increase q->q_usage_counter
376 * @q: request queue pointer
Bart Van Asschea4d34da2020-12-08 21:29:50 -0800377 * @flags: BLK_MQ_REQ_NOWAIT and/or BLK_MQ_REQ_PM
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800378 */
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800379int blk_queue_enter(struct request_queue *q, blk_mq_req_flags_t flags)
Dan Williams3ef28e82015-10-21 13:20:12 -0400380{
Bart Van Asschea4d34da2020-12-08 21:29:50 -0800381 const bool pm = flags & BLK_MQ_REQ_PM;
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800382
Christoph Hellwig1f14a092021-09-29 09:12:38 +0200383 while (!blk_try_enter_queue(q, pm)) {
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800384 if (flags & BLK_MQ_REQ_NOWAIT)
Dan Williams3ef28e82015-10-21 13:20:12 -0400385 return -EBUSY;
386
Ming Lei5ed61d32017-03-27 20:06:56 +0800387 /*
Christoph Hellwig1f14a092021-09-29 09:12:38 +0200388 * read pair of barrier in blk_freeze_queue_start(), we need to
389 * order reading __PERCPU_REF_DEAD flag of .q_usage_counter and
390 * reading .mq_freeze_depth or queue dying flag, otherwise the
391 * following wait may never return if the two reads are
392 * reordered.
Ming Lei5ed61d32017-03-27 20:06:56 +0800393 */
394 smp_rmb();
Alan Jenkins1dc30392018-04-12 19:11:58 +0100395 wait_event(q->mq_freeze_wq,
Bob Liu7996a8b2019-05-21 11:25:55 +0800396 (!q->mq_freeze_depth &&
Alan Stern52abca62020-12-08 21:29:51 -0800397 blk_pm_resume_queue(pm, q)) ||
Alan Jenkins1dc30392018-04-12 19:11:58 +0100398 blk_queue_dying(q));
Dan Williams3ef28e82015-10-21 13:20:12 -0400399 if (blk_queue_dying(q))
400 return -ENODEV;
Dan Williams3ef28e82015-10-21 13:20:12 -0400401 }
Christoph Hellwig1f14a092021-09-29 09:12:38 +0200402
403 return 0;
Dan Williams3ef28e82015-10-21 13:20:12 -0400404}
405
Jens Axboec98cb5b2021-11-04 12:45:51 -0600406int __bio_queue_enter(struct request_queue *q, struct bio *bio)
Christoph Hellwigaccea322020-04-28 13:27:56 +0200407{
Christoph Hellwiga6741532021-09-29 09:12:39 +0200408 while (!blk_try_enter_queue(q, false)) {
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100409 struct gendisk *disk = bio->bi_bdev->bd_disk;
410
Christoph Hellwiga6741532021-09-29 09:12:39 +0200411 if (bio->bi_opf & REQ_NOWAIT) {
Christoph Hellwig8e141f92021-09-29 09:12:40 +0200412 if (test_bit(GD_DEAD, &disk->state))
Christoph Hellwiga6741532021-09-29 09:12:39 +0200413 goto dead;
Christoph Hellwigaccea322020-04-28 13:27:56 +0200414 bio_wouldblock_error(bio);
Christoph Hellwiga6741532021-09-29 09:12:39 +0200415 return -EBUSY;
416 }
417
418 /*
419 * read pair of barrier in blk_freeze_queue_start(), we need to
420 * order reading __PERCPU_REF_DEAD flag of .q_usage_counter and
421 * reading .mq_freeze_depth or queue dying flag, otherwise the
422 * following wait may never return if the two reads are
423 * reordered.
424 */
425 smp_rmb();
426 wait_event(q->mq_freeze_wq,
427 (!q->mq_freeze_depth &&
428 blk_pm_resume_queue(false, q)) ||
Christoph Hellwig8e141f92021-09-29 09:12:40 +0200429 test_bit(GD_DEAD, &disk->state));
430 if (test_bit(GD_DEAD, &disk->state))
Christoph Hellwiga6741532021-09-29 09:12:39 +0200431 goto dead;
Christoph Hellwigaccea322020-04-28 13:27:56 +0200432 }
433
Christoph Hellwiga6741532021-09-29 09:12:39 +0200434 return 0;
435dead:
436 bio_io_error(bio);
437 return -ENODEV;
Christoph Hellwigaccea322020-04-28 13:27:56 +0200438}
439
Dan Williams3ef28e82015-10-21 13:20:12 -0400440void blk_queue_exit(struct request_queue *q)
441{
442 percpu_ref_put(&q->q_usage_counter);
443}
444
445static void blk_queue_usage_counter_release(struct percpu_ref *ref)
446{
447 struct request_queue *q =
448 container_of(ref, struct request_queue, q_usage_counter);
449
450 wake_up_all(&q->mq_freeze_wq);
451}
452
Kees Cookbca237a2017-08-28 15:03:41 -0700453static void blk_rq_timed_out_timer(struct timer_list *t)
Christoph Hellwig287922e2015-10-30 20:57:30 +0800454{
Kees Cookbca237a2017-08-28 15:03:41 -0700455 struct request_queue *q = from_timer(q, t, timeout);
Christoph Hellwig287922e2015-10-30 20:57:30 +0800456
457 kblockd_schedule_work(&q->timeout_work);
458}
459
Tetsuo Handa2e3c18d2019-01-30 22:21:45 +0900460static void blk_timeout_work(struct work_struct *work)
461{
462}
463
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200464struct request_queue *blk_alloc_queue(int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700465{
Jens Axboe165125e2007-07-24 09:28:11 +0200466 struct request_queue *q;
Kent Overstreet338aa962018-05-20 18:25:47 -0400467 int ret;
Christoph Lameter19460892005-06-23 00:08:19 -0700468
Jens Axboe8324aa92008-01-29 14:51:59 +0100469 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100470 GFP_KERNEL | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (!q)
472 return NULL;
473
Christoph Hellwigcbf62af2018-05-31 19:11:36 +0200474 q->last_merge = NULL;
Christoph Hellwigcbf62af2018-05-31 19:11:36 +0200475
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100476 q->id = ida_simple_get(&blk_queue_ida, 0, 0, GFP_KERNEL);
Tejun Heoa73f7302011-12-14 00:33:37 +0100477 if (q->id < 0)
Ming Lei3d2936f2014-05-27 23:35:14 +0800478 goto fail_q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100479
Ming Leic495a172021-01-11 11:05:53 +0800480 ret = bioset_init(&q->bio_split, BIO_POOL_SIZE, 0, 0);
Kent Overstreet338aa962018-05-20 18:25:47 -0400481 if (ret)
Kent Overstreet54efd502015-04-23 22:37:18 -0700482 goto fail_id;
483
Jens Axboea83b5762017-03-21 17:20:01 -0600484 q->stats = blk_alloc_queue_stats();
485 if (!q->stats)
Christoph Hellwigedb08722021-08-09 16:17:43 +0200486 goto fail_split;
Jens Axboea83b5762017-03-21 17:20:01 -0600487
Mike Snitzer51514122011-11-23 10:59:13 +0100488 q->node = node_id;
Jens Axboe0989a022009-06-12 14:42:56 +0200489
John Garry079a2e32021-10-05 18:23:39 +0800490 atomic_set(&q->nr_active_requests_shared_tags, 0);
John Garrybccf5e22020-08-19 23:20:26 +0800491
Kees Cookbca237a2017-08-28 15:03:41 -0700492 timer_setup(&q->timeout, blk_rq_timed_out_timer, 0);
Tetsuo Handa2e3c18d2019-01-30 22:21:45 +0900493 INIT_WORK(&q->timeout_work, blk_timeout_work);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100494 INIT_LIST_HEAD(&q->icq_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800495#ifdef CONFIG_BLK_CGROUP
Tejun Heoe8989fa2012-03-05 13:15:20 -0800496 INIT_LIST_HEAD(&q->blkg_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800497#endif
Al Viro483f4af2006-03-18 18:34:37 -0500498
Jens Axboe8324aa92008-01-29 14:51:59 +0100499 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Luis Chamberlain85e0cbb2020-06-19 20:47:30 +0000501 mutex_init(&q->debugfs_mutex);
Al Viro483f4af2006-03-18 18:34:37 -0500502 mutex_init(&q->sysfs_lock);
Ming Leicecf5d82019-08-27 19:01:48 +0800503 mutex_init(&q->sysfs_dir_lock);
Christoph Hellwig0d945c12018-11-15 12:17:28 -0700504 spin_lock_init(&q->queue_lock);
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500505
Jens Axboe320ae512013-10-24 09:20:05 +0100506 init_waitqueue_head(&q->mq_freeze_wq);
Bob Liu7996a8b2019-05-21 11:25:55 +0800507 mutex_init(&q->mq_freeze_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100508
Dan Williams3ef28e82015-10-21 13:20:12 -0400509 /*
510 * Init percpu_ref in atomic mode so that it's faster to shutdown.
511 * See blk_register_queue() for details.
512 */
513 if (percpu_ref_init(&q->q_usage_counter,
514 blk_queue_usage_counter_release,
515 PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
Christoph Hellwigedb08722021-08-09 16:17:43 +0200516 goto fail_stats;
Tejun Heof51b8022012-03-05 13:15:05 -0800517
Dan Williams3ef28e82015-10-21 13:20:12 -0400518 if (blkcg_init_queue(q))
519 goto fail_ref;
520
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100521 blk_queue_dma_alignment(q, 511);
522 blk_set_default_limits(&q->limits);
John Garryd2a27962021-10-05 18:23:27 +0800523 q->nr_requests = BLKDEV_DEFAULT_RQ;
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100526
Dan Williams3ef28e82015-10-21 13:20:12 -0400527fail_ref:
528 percpu_ref_exit(&q->q_usage_counter);
Jens Axboea83b5762017-03-21 17:20:01 -0600529fail_stats:
Christoph Hellwigedb08722021-08-09 16:17:43 +0200530 blk_free_queue_stats(q->stats);
Kent Overstreet54efd502015-04-23 22:37:18 -0700531fail_split:
Kent Overstreet338aa962018-05-20 18:25:47 -0400532 bioset_exit(&q->bio_split);
Tejun Heoa73f7302011-12-14 00:33:37 +0100533fail_id:
534 ida_simple_remove(&blk_queue_ida, q->id);
535fail_q:
536 kmem_cache_free(blk_requestq_cachep, q);
537 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000540/**
541 * blk_get_queue - increment the request_queue refcount
542 * @q: the request_queue structure to increment the refcount for
543 *
544 * Increment the refcount of the request_queue kobject.
Luis Chamberlain763b5892020-06-19 20:47:24 +0000545 *
546 * Context: Any context.
Luis Chamberlainb5bd3572020-06-19 20:47:23 +0000547 */
Tejun Heo09ac46c2011-12-14 00:33:38 +0100548bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100550 if (likely(!blk_queue_dying(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100551 __blk_get_queue(q);
552 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
Tejun Heo09ac46c2011-12-14 00:33:38 +0100555 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
Jens Axboed86e0e82011-05-27 07:44:43 +0200557EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100559static void handle_bad_sector(struct bio *bio, sector_t maxsector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 char b[BDEVNAME_SIZE];
562
Christoph Hellwig8a3ee672021-09-28 07:27:55 +0200563 pr_info_ratelimited("%s: attempt to access beyond end of device\n"
Tetsuo Handaf4ac7122020-10-08 22:37:23 +0900564 "%s: rw=%d, want=%llu, limit=%llu\n",
Christoph Hellwig8a3ee672021-09-28 07:27:55 +0200565 current->comm,
Tetsuo Handaf4ac7122020-10-08 22:37:23 +0900566 bio_devname(bio, b), bio->bi_opf,
567 bio_end_sector(bio), maxsector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Akinobu Mitac17bb492006-12-08 02:39:46 -0800570#ifdef CONFIG_FAIL_MAKE_REQUEST
571
572static DECLARE_FAULT_ATTR(fail_make_request);
573
574static int __init setup_fail_make_request(char *str)
575{
576 return setup_fault_attr(&fail_make_request, str);
577}
578__setup("fail_make_request=", setup_fail_make_request);
579
Christoph Hellwig06c8c692021-11-17 07:13:58 +0100580bool should_fail_request(struct block_device *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -0800581{
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100582 return part->bd_make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800583}
584
585static int __init fail_make_request_debugfs(void)
586{
Akinobu Mitadd48c082011-08-03 16:21:01 -0700587 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
588 NULL, &fail_make_request);
589
Duan Jiong21f9fcd2014-04-11 15:58:56 +0800590 return PTR_ERR_OR_ZERO(dir);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800591}
592
593late_initcall(fail_make_request_debugfs);
Akinobu Mitac17bb492006-12-08 02:39:46 -0800594#endif /* CONFIG_FAIL_MAKE_REQUEST */
595
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100596static inline bool bio_check_ro(struct bio *bio)
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100597{
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100598 if (op_is_write(bio_op(bio)) && bdev_read_only(bio->bi_bdev)) {
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100599 char b[BDEVNAME_SIZE];
600
Mikulas Patocka8b2ded12018-09-05 16:14:36 -0600601 if (op_is_flush(bio->bi_opf) && !bio_sectors(bio))
602 return false;
603
Linus Torvaldsa32e2362018-08-03 12:22:09 -0700604 WARN_ONCE(1,
Christoph Hellwigc8178672020-07-01 10:59:40 +0200605 "Trying to write to read-only block-device %s (partno %d)\n",
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100606 bio_devname(bio, b), bio->bi_bdev->bd_partno);
Linus Torvaldsa32e2362018-08-03 12:22:09 -0700607 /* Older lvm-tools actually trigger this */
608 return false;
Ilya Dryomov721c7fc2018-01-11 14:09:11 +0100609 }
610
611 return false;
612}
613
Howard McLauchlan30abb3a2018-02-06 14:05:39 -0800614static noinline int should_fail_bio(struct bio *bio)
615{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100616 if (should_fail_request(bdev_whole(bio->bi_bdev), bio->bi_iter.bi_size))
Howard McLauchlan30abb3a2018-02-06 14:05:39 -0800617 return -EIO;
618 return 0;
619}
620ALLOW_ERROR_INJECTION(should_fail_bio, ERRNO);
621
Jens Axboec07e2b42007-07-18 13:27:58 +0200622/*
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100623 * Check whether this bio extends beyond the end of the device or partition.
624 * This may well happen - the kernel calls bread() without checking the size of
625 * the device, e.g., when mounting a file system.
626 */
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100627static inline int bio_check_eod(struct bio *bio)
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100628{
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100629 sector_t maxsector = bdev_nr_sectors(bio->bi_bdev);
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100630 unsigned int nr_sectors = bio_sectors(bio);
631
632 if (nr_sectors && maxsector &&
633 (nr_sectors > maxsector ||
634 bio->bi_iter.bi_sector > maxsector - nr_sectors)) {
635 handle_bad_sector(bio, maxsector);
636 return -EIO;
637 }
638 return 0;
639}
640
641/*
Christoph Hellwig74d46992017-08-23 19:10:32 +0200642 * Remap block n of partition p to block n+start(p) of the disk.
643 */
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100644static int blk_partition_remap(struct bio *bio)
Christoph Hellwig74d46992017-08-23 19:10:32 +0200645{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100646 struct block_device *p = bio->bi_bdev;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200647
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100648 if (unlikely(should_fail_request(p, bio->bi_iter.bi_size)))
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100649 return -EIO;
Damien Le Moal5eac3eb2019-11-11 11:39:25 +0900650 if (bio_sectors(bio)) {
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100651 bio->bi_iter.bi_sector += p->bd_start_sect;
Christoph Hellwig1c02fca2020-12-03 17:21:38 +0100652 trace_block_bio_remap(bio, p->bd_dev,
Christoph Hellwig29ff57c2020-11-24 09:34:24 +0100653 bio->bi_iter.bi_sector -
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100654 p->bd_start_sect);
Christoph Hellwig52c5e622018-03-14 16:56:53 +0100655 }
Christoph Hellwig30c5d342021-01-24 11:02:36 +0100656 bio_set_flag(bio, BIO_REMAPPED);
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100657 return 0;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200658}
659
Keith Busch0512a752020-05-12 17:55:47 +0900660/*
661 * Check write append to a zoned block device.
662 */
663static inline blk_status_t blk_check_zone_append(struct request_queue *q,
664 struct bio *bio)
665{
666 sector_t pos = bio->bi_iter.bi_sector;
667 int nr_sectors = bio_sectors(bio);
668
669 /* Only applicable to zoned block devices */
670 if (!blk_queue_is_zoned(q))
671 return BLK_STS_NOTSUPP;
672
673 /* The bio sector must point to the start of a sequential zone */
674 if (pos & (blk_queue_zone_sectors(q) - 1) ||
675 !blk_queue_zone_is_seq(q, pos))
676 return BLK_STS_IOERR;
677
678 /*
679 * Not allowed to cross zone boundaries. Otherwise, the BIO will be
680 * split and could result in non-contiguous sectors being written in
681 * different zones.
682 */
683 if (nr_sectors > q->limits.chunk_sectors)
684 return BLK_STS_IOERR;
685
686 /* Make sure the BIO is small enough and will not get split */
687 if (nr_sectors > q->limits.max_zone_append_sectors)
688 return BLK_STS_IOERR;
689
690 bio->bi_opf |= REQ_NOMERGE;
691
692 return BLK_STS_OK;
693}
694
Jens Axboe900e0802021-11-03 05:47:09 -0600695noinline_for_stack bool submit_bio_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100697 struct block_device *bdev = bio->bi_bdev;
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100698 struct request_queue *q = bdev_get_queue(bdev);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200699 blk_status_t status = BLK_STS_IOERR;
Jens Axboe5a473e82020-06-04 11:23:39 -0600700 struct blk_plug *plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Jens Axboe5a473e82020-06-04 11:23:39 -0600704 plug = blk_mq_plug(q, bio);
705 if (plug && plug->nowait)
706 bio->bi_opf |= REQ_NOWAIT;
707
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500708 /*
Jens Axboeb0beb282020-05-28 13:19:29 -0600709 * For a REQ_NOWAIT based request, return -EOPNOTSUPP
Mike Snitzer021a2442020-09-23 16:06:51 -0400710 * if queue does not support NOWAIT.
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500711 */
Mike Snitzer021a2442020-09-23 16:06:51 -0400712 if ((bio->bi_opf & REQ_NOWAIT) && !blk_queue_nowait(q))
Jens Axboeb0beb282020-05-28 13:19:29 -0600713 goto not_supported;
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500714
Howard McLauchlan30abb3a2018-02-06 14:05:39 -0800715 if (should_fail_bio(bio))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200716 goto end_io;
Christoph Hellwig2f9f6222021-01-24 11:02:35 +0100717 if (unlikely(bio_check_ro(bio)))
718 goto end_io;
Christoph Hellwig3a905c32021-01-25 19:39:57 +0100719 if (!bio_flagged(bio, BIO_REMAPPED)) {
720 if (unlikely(bio_check_eod(bio)))
721 goto end_io;
722 if (bdev->bd_partno && unlikely(blk_partition_remap(bio)))
723 goto end_io;
724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200726 /*
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200727 * Filter flush bio's early so that bio based drivers without flush
728 * support don't have to worry about them.
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200729 */
Jens Axboef3a8ab72017-01-27 09:08:23 -0700730 if (op_is_flush(bio->bi_opf) &&
Jens Axboec888a8f2016-04-13 13:33:19 -0600731 !test_bit(QUEUE_FLAG_WC, &q->queue_flags)) {
Jens Axboe1eff9d32016-08-05 15:35:16 -0600732 bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
Christoph Hellwige439ab72020-07-01 10:59:42 +0200733 if (!bio_sectors(bio)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200734 status = BLK_STS_OK;
Tejun Heoa7384672008-11-28 13:32:03 +0900735 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Christoph Hellwigd04c4062018-12-14 17:21:22 +0100739 if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
Christoph Hellwig6ce913f2021-10-12 13:12:21 +0200740 bio_clear_polled(bio);
Christoph Hellwigd04c4062018-12-14 17:21:22 +0100741
Christoph Hellwig288dab82016-06-09 16:00:36 +0200742 switch (bio_op(bio)) {
743 case REQ_OP_DISCARD:
744 if (!blk_queue_discard(q))
745 goto not_supported;
746 break;
747 case REQ_OP_SECURE_ERASE:
748 if (!blk_queue_secure_erase(q))
749 goto not_supported;
750 break;
751 case REQ_OP_WRITE_SAME:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200752 if (!q->limits.max_write_same_sectors)
Christoph Hellwig288dab82016-06-09 16:00:36 +0200753 goto not_supported;
Nicolai Stange58886782016-12-04 14:56:39 +0100754 break;
Keith Busch0512a752020-05-12 17:55:47 +0900755 case REQ_OP_ZONE_APPEND:
756 status = blk_check_zone_append(q, bio);
757 if (status != BLK_STS_OK)
758 goto end_io;
759 break;
Shaun Tancheff2d253442016-10-18 15:40:32 +0900760 case REQ_OP_ZONE_RESET:
Ajay Joshi6c1b1da2019-10-27 23:05:45 +0900761 case REQ_OP_ZONE_OPEN:
762 case REQ_OP_ZONE_CLOSE:
763 case REQ_OP_ZONE_FINISH:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200764 if (!blk_queue_is_zoned(q))
Shaun Tancheff2d253442016-10-18 15:40:32 +0900765 goto not_supported;
Christoph Hellwig288dab82016-06-09 16:00:36 +0200766 break;
Chaitanya Kulkarni6e33dbf2019-08-01 10:26:36 -0700767 case REQ_OP_ZONE_RESET_ALL:
768 if (!blk_queue_is_zoned(q) || !blk_queue_zone_resetall(q))
769 goto not_supported;
770 break;
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800771 case REQ_OP_WRITE_ZEROES:
Christoph Hellwig74d46992017-08-23 19:10:32 +0200772 if (!q->limits.max_write_zeroes_sectors)
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -0800773 goto not_supported;
774 break;
Christoph Hellwig288dab82016-06-09 16:00:36 +0200775 default:
776 break;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +0200777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Tejun Heo7f4b35d2012-06-04 20:40:56 -0700779 /*
Christoph Hellwig3e82c342020-04-25 09:55:51 +0200780 * Various block parts want %current->io_context, so allocate it up
781 * front rather than dealing with lots of pain to allocate it only
782 * where needed. This may fail and the block layer knows how to live
783 * with it.
Tejun Heo7f4b35d2012-06-04 20:40:56 -0700784 */
Christoph Hellwig3e82c342020-04-25 09:55:51 +0200785 if (unlikely(!current->io_context))
786 create_task_io_context(current, GFP_ATOMIC, q->node);
Tejun Heo7f4b35d2012-06-04 20:40:56 -0700787
Laibin Qiub781d8db52021-11-12 17:33:54 +0800788 if (blk_throtl_bio(bio))
Tejun Heoae118892015-08-18 14:55:20 -0700789 return false;
Christoph Hellwigdb18a532020-06-27 09:31:58 +0200790
791 blk_cgroup_bio_start(bio);
792 blkcg_bio_issue_init(bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200793
NeilBrownfbbaf702017-04-07 09:40:52 -0600794 if (!bio_flagged(bio, BIO_TRACE_COMPLETION)) {
Christoph Hellwige8a676d2020-12-03 17:21:36 +0100795 trace_block_bio_queue(bio);
NeilBrownfbbaf702017-04-07 09:40:52 -0600796 /* Now that enqueuing has been traced, we need to trace
797 * completion as well.
798 */
799 bio_set_flag(bio, BIO_TRACE_COMPLETION);
800 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200801 return true;
Tejun Heoa7384672008-11-28 13:32:03 +0900802
Christoph Hellwig288dab82016-06-09 16:00:36 +0200803not_supported:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200804 status = BLK_STS_NOTSUPP;
Tejun Heoa7384672008-11-28 13:32:03 +0900805end_io:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200806 bio->bi_status = status;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200807 bio_endio(bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200808 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Jens Axboe900e0802021-11-03 05:47:09 -0600811static void __submit_bio_fops(struct gendisk *disk, struct bio *bio)
812{
813 if (unlikely(bio_queue_enter(bio) != 0))
814 return;
815 if (submit_bio_checks(bio) && blk_crypto_bio_prep(&bio))
816 disk->fops->submit_bio(bio);
817 blk_queue_exit(disk->queue);
818}
819
Christoph Hellwig3e087732021-10-12 13:12:24 +0200820static void __submit_bio(struct bio *bio)
Christoph Hellwigac7c5672020-05-16 20:28:01 +0200821{
Christoph Hellwig309dca302021-01-24 11:02:34 +0100822 struct gendisk *disk = bio->bi_bdev->bd_disk;
Christoph Hellwigac7c5672020-05-16 20:28:01 +0200823
Jens Axboe900e0802021-11-03 05:47:09 -0600824 if (!disk->fops->submit_bio)
Christoph Hellwig3e087732021-10-12 13:12:24 +0200825 blk_mq_submit_bio(bio);
Jens Axboe900e0802021-11-03 05:47:09 -0600826 else
827 __submit_bio_fops(disk, bio);
Christoph Hellwigac7c5672020-05-16 20:28:01 +0200828}
829
Christoph Hellwig566acf22020-07-01 10:59:45 +0200830/*
831 * The loop in this function may be a bit non-obvious, and so deserves some
832 * explanation:
833 *
834 * - Before entering the loop, bio->bi_next is NULL (as all callers ensure
835 * that), so we have a list with a single bio.
836 * - We pretend that we have just taken it off a longer list, so we assign
837 * bio_list to a pointer to the bio_list_on_stack, thus initialising the
838 * bio_list of new bios to be added. ->submit_bio() may indeed add some more
839 * bios through a recursive call to submit_bio_noacct. If it did, we find a
840 * non-NULL value in bio_list and re-enter the loop from the top.
841 * - In this case we really did just take the bio of the top of the list (no
842 * pretending) and so remove it from bio_list, and call into ->submit_bio()
843 * again.
844 *
845 * bio_list_on_stack[0] contains bios submitted by the current ->submit_bio.
846 * bio_list_on_stack[1] contains bios that were submitted before the current
847 * ->submit_bio_bio, but that haven't been processed yet.
848 */
Christoph Hellwig3e087732021-10-12 13:12:24 +0200849static void __submit_bio_noacct(struct bio *bio)
Christoph Hellwig566acf22020-07-01 10:59:45 +0200850{
851 struct bio_list bio_list_on_stack[2];
Christoph Hellwig566acf22020-07-01 10:59:45 +0200852
853 BUG_ON(bio->bi_next);
854
855 bio_list_init(&bio_list_on_stack[0]);
856 current->bio_list = bio_list_on_stack;
857
858 do {
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100859 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
Christoph Hellwig566acf22020-07-01 10:59:45 +0200860 struct bio_list lower, same;
861
Christoph Hellwig566acf22020-07-01 10:59:45 +0200862 /*
863 * Create a fresh bio_list for all subordinate requests.
864 */
865 bio_list_on_stack[1] = bio_list_on_stack[0];
866 bio_list_init(&bio_list_on_stack[0]);
867
Christoph Hellwig3e087732021-10-12 13:12:24 +0200868 __submit_bio(bio);
Christoph Hellwig566acf22020-07-01 10:59:45 +0200869
870 /*
871 * Sort new bios into those for a lower level and those for the
872 * same level.
873 */
874 bio_list_init(&lower);
875 bio_list_init(&same);
876 while ((bio = bio_list_pop(&bio_list_on_stack[0])) != NULL)
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100877 if (q == bdev_get_queue(bio->bi_bdev))
Christoph Hellwig566acf22020-07-01 10:59:45 +0200878 bio_list_add(&same, bio);
879 else
880 bio_list_add(&lower, bio);
881
882 /*
883 * Now assemble so we handle the lowest level first.
884 */
885 bio_list_merge(&bio_list_on_stack[0], &lower);
886 bio_list_merge(&bio_list_on_stack[0], &same);
887 bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
888 } while ((bio = bio_list_pop(&bio_list_on_stack[0])));
889
890 current->bio_list = NULL;
Christoph Hellwig566acf22020-07-01 10:59:45 +0200891}
892
Christoph Hellwig3e087732021-10-12 13:12:24 +0200893static void __submit_bio_noacct_mq(struct bio *bio)
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200894{
Christoph Hellwig7c792f32020-07-02 21:21:25 +0200895 struct bio_list bio_list[2] = { };
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200896
Christoph Hellwig7c792f32020-07-02 21:21:25 +0200897 current->bio_list = bio_list;
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200898
899 do {
Christoph Hellwig3e087732021-10-12 13:12:24 +0200900 __submit_bio(bio);
Christoph Hellwig7c792f32020-07-02 21:21:25 +0200901 } while ((bio = bio_list_pop(&bio_list[0])));
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200902
903 current->bio_list = NULL;
Christoph Hellwigff93ea02020-07-01 10:59:46 +0200904}
905
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200906/**
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200907 * submit_bio_noacct - re-submit a bio to the block device layer for I/O
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200908 * @bio: The bio describing the location in memory and on the device.
909 *
Christoph Hellwig3fdd4082020-04-28 13:27:53 +0200910 * This is a version of submit_bio() that shall only be used for I/O that is
911 * resubmitted to lower level drivers by stacking block drivers. All file
912 * systems and other upper level users of the block layer should use
913 * submit_bio() instead.
Neil Brownd89d8792007-05-01 09:53:42 +0200914 */
Christoph Hellwig3e087732021-10-12 13:12:24 +0200915void submit_bio_noacct(struct bio *bio)
Neil Brownd89d8792007-05-01 09:53:42 +0200916{
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200917 /*
Christoph Hellwig566acf22020-07-01 10:59:45 +0200918 * We only want one ->submit_bio to be active at a time, else stack
919 * usage with stacked devices could be a problem. Use current->bio_list
920 * to collect a list of requests submited by a ->submit_bio method while
921 * it is active, and then process them after it returned.
Christoph Hellwig27a84d52011-09-15 14:01:40 +0200922 */
Christoph Hellwig3e087732021-10-12 13:12:24 +0200923 if (current->bio_list)
NeilBrownf5fe1b52017-03-10 17:00:47 +1100924 bio_list_add(&current->bio_list[0], bio);
Christoph Hellwig3e087732021-10-12 13:12:24 +0200925 else if (!bio->bi_bdev->bd_disk->fops->submit_bio)
926 __submit_bio_noacct_mq(bio);
927 else
928 __submit_bio_noacct(bio);
Neil Brownd89d8792007-05-01 09:53:42 +0200929}
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200930EXPORT_SYMBOL(submit_bio_noacct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932/**
Randy Dunlap710027a2008-08-19 20:13:11 +0200933 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 * @bio: The &struct bio which describes the I/O
935 *
Christoph Hellwig3fdd4082020-04-28 13:27:53 +0200936 * submit_bio() is used to submit I/O requests to block devices. It is passed a
937 * fully set up &struct bio that describes the I/O that needs to be done. The
Christoph Hellwig309dca302021-01-24 11:02:34 +0100938 * bio will be send to the device described by the bi_bdev field.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 *
Christoph Hellwig3fdd4082020-04-28 13:27:53 +0200940 * The success/failure status of the request, along with notification of
941 * completion, is delivered asynchronously through the ->bi_end_io() callback
942 * in @bio. The bio must NOT be touched by thecaller until ->bi_end_io() has
943 * been called.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 */
Christoph Hellwig3e087732021-10-12 13:12:24 +0200945void submit_bio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Tejun Heod3f77df2019-06-27 13:39:52 -0700947 if (blkcg_punt_bio_submit(bio))
Christoph Hellwig3e087732021-10-12 13:12:24 +0200948 return;
Tejun Heod3f77df2019-06-27 13:39:52 -0700949
Jens Axboebf2de6f2007-09-27 13:01:25 +0200950 /*
951 * If it's a regular read/write or a barrier with data attached,
952 * go through the normal accounting stuff before submission.
953 */
Martin K. Petersene2a60da2012-09-18 12:19:25 -0400954 if (bio_has_data(bio)) {
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400955 unsigned int count;
956
Mike Christie95fe6c12016-06-05 14:31:48 -0500957 if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
Christoph Hellwig309dca302021-01-24 11:02:34 +0100958 count = queue_logical_block_size(
Pavel Begunkoveab4e022021-10-14 15:03:29 +0100959 bdev_get_queue(bio->bi_bdev)) >> 9;
Martin K. Petersen4363ac72012-09-18 12:19:27 -0400960 else
961 count = bio_sectors(bio);
962
Mike Christiea8ebb052016-06-05 14:31:45 -0500963 if (op_is_write(bio_op(bio))) {
Jens Axboebf2de6f2007-09-27 13:01:25 +0200964 count_vm_events(PGPGOUT, count);
965 } else {
Kent Overstreet4f024f32013-10-11 15:44:27 -0700966 task_io_account_read(bio->bi_iter.bi_size);
Jens Axboebf2de6f2007-09-27 13:01:25 +0200967 count_vm_events(PGPGIN, count);
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970
Johannes Weinerb8e24a92019-08-08 15:03:00 -0400971 /*
Christoph Hellwig760f83e2020-04-28 13:27:54 +0200972 * If we're reading data that is part of the userspace workingset, count
973 * submission time as memory stall. When the device is congested, or
974 * the submitting cgroup IO-throttled, submission can be a significant
975 * part of overall IO time.
Johannes Weinerb8e24a92019-08-08 15:03:00 -0400976 */
Christoph Hellwig760f83e2020-04-28 13:27:54 +0200977 if (unlikely(bio_op(bio) == REQ_OP_READ &&
978 bio_flagged(bio, BIO_WORKINGSET))) {
979 unsigned long pflags;
Christoph Hellwig760f83e2020-04-28 13:27:54 +0200980
Johannes Weinerb8e24a92019-08-08 15:03:00 -0400981 psi_memstall_enter(&pflags);
Christoph Hellwig3e087732021-10-12 13:12:24 +0200982 submit_bio_noacct(bio);
Johannes Weinerb8e24a92019-08-08 15:03:00 -0400983 psi_memstall_leave(&pflags);
Christoph Hellwig3e087732021-10-12 13:12:24 +0200984 return;
Christoph Hellwig760f83e2020-04-28 13:27:54 +0200985 }
986
Christoph Hellwig3e087732021-10-12 13:12:24 +0200987 submit_bio_noacct(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989EXPORT_SYMBOL(submit_bio);
990
Jens Axboe1052b8a2018-11-26 08:21:49 -0700991/**
Christoph Hellwig3e087732021-10-12 13:12:24 +0200992 * bio_poll - poll for BIO completions
993 * @bio: bio to poll for
Yang Guange30028a2021-11-26 00:20:55 +0800994 * @iob: batches of IO
Christoph Hellwig3e087732021-10-12 13:12:24 +0200995 * @flags: BLK_POLL_* flags that control the behavior
996 *
997 * Poll for completions on queue associated with the bio. Returns number of
998 * completed entries found.
999 *
1000 * Note: the caller must either be the context that submitted @bio, or
1001 * be in a RCU critical section to prevent freeing of @bio.
1002 */
Jens Axboe5a72e892021-10-12 09:24:29 -06001003int bio_poll(struct bio *bio, struct io_comp_batch *iob, unsigned int flags)
Christoph Hellwig3e087732021-10-12 13:12:24 +02001004{
Pavel Begunkov859897c2021-10-19 22:24:11 +01001005 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
Christoph Hellwig3e087732021-10-12 13:12:24 +02001006 blk_qc_t cookie = READ_ONCE(bio->bi_cookie);
1007 int ret;
1008
1009 if (cookie == BLK_QC_T_NONE ||
1010 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
1011 return 0;
1012
1013 if (current->plug)
Christoph Hellwig008f75a2021-10-20 16:41:19 +02001014 blk_flush_plug(current->plug, false);
Christoph Hellwig3e087732021-10-12 13:12:24 +02001015
1016 if (blk_queue_enter(q, BLK_MQ_REQ_NOWAIT))
1017 return 0;
1018 if (WARN_ON_ONCE(!queue_is_mq(q)))
1019 ret = 0; /* not yet implemented, should not happen */
1020 else
Jens Axboe5a72e892021-10-12 09:24:29 -06001021 ret = blk_mq_poll(q, cookie, iob, flags);
Christoph Hellwig3e087732021-10-12 13:12:24 +02001022 blk_queue_exit(q);
1023 return ret;
1024}
1025EXPORT_SYMBOL_GPL(bio_poll);
1026
1027/*
1028 * Helper to implement file_operations.iopoll. Requires the bio to be stored
1029 * in iocb->private, and cleared before freeing the bio.
1030 */
Jens Axboe5a72e892021-10-12 09:24:29 -06001031int iocb_bio_iopoll(struct kiocb *kiocb, struct io_comp_batch *iob,
1032 unsigned int flags)
Christoph Hellwig3e087732021-10-12 13:12:24 +02001033{
1034 struct bio *bio;
1035 int ret = 0;
1036
1037 /*
1038 * Note: the bio cache only uses SLAB_TYPESAFE_BY_RCU, so bio can
1039 * point to a freshly allocated bio at this point. If that happens
1040 * we have a few cases to consider:
1041 *
1042 * 1) the bio is beeing initialized and bi_bdev is NULL. We can just
1043 * simply nothing in this case
1044 * 2) the bio points to a not poll enabled device. bio_poll will catch
1045 * this and return 0
1046 * 3) the bio points to a poll capable device, including but not
1047 * limited to the one that the original bio pointed to. In this
1048 * case we will call into the actual poll method and poll for I/O,
1049 * even if we don't need to, but it won't cause harm either.
1050 *
1051 * For cases 2) and 3) above the RCU grace period ensures that bi_bdev
1052 * is still allocated. Because partitions hold a reference to the whole
1053 * device bdev and thus disk, the disk is also still valid. Grabbing
1054 * a reference to the queue in bio_poll() ensures the hctxs and requests
1055 * are still valid as well.
1056 */
1057 rcu_read_lock();
1058 bio = READ_ONCE(kiocb->private);
1059 if (bio && bio->bi_bdev)
Jens Axboe5a72e892021-10-12 09:24:29 -06001060 ret = bio_poll(bio, iob, flags);
Christoph Hellwig3e087732021-10-12 13:12:24 +02001061 rcu_read_unlock();
1062
1063 return ret;
1064}
1065EXPORT_SYMBOL_GPL(iocb_bio_iopoll);
1066
Christoph Hellwig450b7872021-11-17 07:14:01 +01001067void update_io_ticks(struct block_device *part, unsigned long now, bool end)
Christoph Hellwig9123bf62020-05-27 07:24:13 +02001068{
1069 unsigned long stamp;
1070again:
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001071 stamp = READ_ONCE(part->bd_stamp);
Chunguang Xud80c2282021-07-06 05:47:26 +08001072 if (unlikely(time_after(now, stamp))) {
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001073 if (likely(cmpxchg(&part->bd_stamp, stamp, now) == stamp))
Christoph Hellwig9123bf62020-05-27 07:24:13 +02001074 __part_stat_add(part, io_ticks, end ? now - stamp : 1);
1075 }
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001076 if (part->bd_partno) {
1077 part = bdev_whole(part);
Christoph Hellwig9123bf62020-05-27 07:24:13 +02001078 goto again;
1079 }
1080}
1081
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001082static unsigned long __part_start_io_acct(struct block_device *part,
Song Liu7b264102020-08-31 15:27:23 -07001083 unsigned int sectors, unsigned int op)
Christoph Hellwig956d5102020-05-27 07:24:04 +02001084{
Christoph Hellwig956d5102020-05-27 07:24:04 +02001085 const int sgrp = op_stat_group(op);
1086 unsigned long now = READ_ONCE(jiffies);
1087
1088 part_stat_lock();
1089 update_io_ticks(part, now, false);
1090 part_stat_inc(part, ios[sgrp]);
1091 part_stat_add(part, sectors[sgrp], sectors);
1092 part_stat_local_inc(part, in_flight[op_is_write(op)]);
1093 part_stat_unlock();
1094
1095 return now;
1096}
Song Liu7b264102020-08-31 15:27:23 -07001097
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001098/**
1099 * bio_start_io_acct - start I/O accounting for bio based drivers
1100 * @bio: bio to start account for
1101 *
1102 * Returns the start time that should be passed back to bio_end_io_acct().
1103 */
1104unsigned long bio_start_io_acct(struct bio *bio)
Song Liu7b264102020-08-31 15:27:23 -07001105{
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001106 return __part_start_io_acct(bio->bi_bdev, bio_sectors(bio), bio_op(bio));
Song Liu7b264102020-08-31 15:27:23 -07001107}
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001108EXPORT_SYMBOL_GPL(bio_start_io_acct);
Song Liu7b264102020-08-31 15:27:23 -07001109
1110unsigned long disk_start_io_acct(struct gendisk *disk, unsigned int sectors,
1111 unsigned int op)
1112{
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001113 return __part_start_io_acct(disk->part0, sectors, op);
Song Liu7b264102020-08-31 15:27:23 -07001114}
Christoph Hellwig956d5102020-05-27 07:24:04 +02001115EXPORT_SYMBOL(disk_start_io_acct);
1116
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001117static void __part_end_io_acct(struct block_device *part, unsigned int op,
Song Liu7b264102020-08-31 15:27:23 -07001118 unsigned long start_time)
Christoph Hellwig956d5102020-05-27 07:24:04 +02001119{
Christoph Hellwig956d5102020-05-27 07:24:04 +02001120 const int sgrp = op_stat_group(op);
1121 unsigned long now = READ_ONCE(jiffies);
1122 unsigned long duration = now - start_time;
1123
1124 part_stat_lock();
1125 update_io_ticks(part, now, true);
1126 part_stat_add(part, nsecs[sgrp], jiffies_to_nsecs(duration));
1127 part_stat_local_dec(part, in_flight[op_is_write(op)]);
1128 part_stat_unlock();
1129}
Song Liu7b264102020-08-31 15:27:23 -07001130
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001131void bio_end_io_acct_remapped(struct bio *bio, unsigned long start_time,
1132 struct block_device *orig_bdev)
Song Liu7b264102020-08-31 15:27:23 -07001133{
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001134 __part_end_io_acct(orig_bdev, bio_op(bio), start_time);
Song Liu7b264102020-08-31 15:27:23 -07001135}
Christoph Hellwig99dfc432021-01-24 11:02:37 +01001136EXPORT_SYMBOL_GPL(bio_end_io_acct_remapped);
Song Liu7b264102020-08-31 15:27:23 -07001137
1138void disk_end_io_acct(struct gendisk *disk, unsigned int op,
1139 unsigned long start_time)
1140{
Christoph Hellwig8446fe92020-11-24 09:36:54 +01001141 __part_end_io_acct(disk->part0, op, start_time);
Song Liu7b264102020-08-31 15:27:23 -07001142}
Christoph Hellwig956d5102020-05-27 07:24:04 +02001143EXPORT_SYMBOL(disk_end_io_acct);
1144
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02001145/**
1146 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
1147 * @q : the queue of the device being checked
1148 *
1149 * Description:
1150 * Check if underlying low-level drivers of a device are busy.
1151 * If the drivers want to export their busy state, they must set own
1152 * exporting function using blk_queue_lld_busy() first.
1153 *
1154 * Basically, this function is used only by request stacking drivers
1155 * to stop dispatching requests to underlying devices when underlying
1156 * devices are busy. This behavior helps more I/O merging on the queue
1157 * of the request stacking driver and prevents I/O throughput regression
1158 * on burst I/O load.
1159 *
1160 * Return:
1161 * 0 - Not busy (The request stacking driver should dispatch request)
1162 * 1 - Busy (The request stacking driver should stop dispatching request)
1163 */
1164int blk_lld_busy(struct request_queue *q)
1165{
Jens Axboe344e9ff2018-11-15 12:22:51 -07001166 if (queue_is_mq(q) && q->mq_ops->busy)
Jens Axboe9ba20522018-10-29 10:15:10 -06001167 return q->mq_ops->busy(q);
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02001168
1169 return 0;
1170}
1171EXPORT_SYMBOL_GPL(blk_lld_busy);
1172
Jens Axboe59c3d452014-04-08 09:15:35 -06001173int kblockd_schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 return queue_work(kblockd_workqueue, work);
1176}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177EXPORT_SYMBOL(kblockd_schedule_work);
1178
Jens Axboe818cd1c2017-04-10 09:54:55 -06001179int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork,
1180 unsigned long delay)
1181{
1182 return mod_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
1183}
1184EXPORT_SYMBOL(kblockd_mod_delayed_work_on);
1185
Jens Axboe47c122e2021-10-06 06:34:11 -06001186void blk_start_plug_nr_ios(struct blk_plug *plug, unsigned short nr_ios)
1187{
1188 struct task_struct *tsk = current;
1189
1190 /*
1191 * If this is a nested plug, don't actually assign it.
1192 */
1193 if (tsk->plug)
1194 return;
1195
Jens Axboebc490f82021-10-18 10:12:12 -06001196 plug->mq_list = NULL;
Jens Axboe47c122e2021-10-06 06:34:11 -06001197 plug->cached_rq = NULL;
1198 plug->nr_ios = min_t(unsigned short, nr_ios, BLK_MAX_REQUEST_COUNT);
1199 plug->rq_count = 0;
1200 plug->multiple_queues = false;
Jens Axboedc5fc3612021-10-19 06:02:30 -06001201 plug->has_elevator = false;
Jens Axboe47c122e2021-10-06 06:34:11 -06001202 plug->nowait = false;
1203 INIT_LIST_HEAD(&plug->cb_list);
1204
1205 /*
1206 * Store ordering should not be needed here, since a potential
1207 * preempt will imply a full memory barrier
1208 */
1209 tsk->plug = plug;
1210}
1211
Suresh Jayaraman75df7132011-09-21 10:00:16 +02001212/**
1213 * blk_start_plug - initialize blk_plug and track it inside the task_struct
1214 * @plug: The &struct blk_plug that needs to be initialized
1215 *
1216 * Description:
Jeff Moyer40405852019-01-08 16:57:34 -05001217 * blk_start_plug() indicates to the block layer an intent by the caller
1218 * to submit multiple I/O requests in a batch. The block layer may use
1219 * this hint to defer submitting I/Os from the caller until blk_finish_plug()
1220 * is called. However, the block layer may choose to submit requests
1221 * before a call to blk_finish_plug() if the number of queued I/Os
1222 * exceeds %BLK_MAX_REQUEST_COUNT, or if the size of the I/O is larger than
1223 * %BLK_PLUG_FLUSH_SIZE. The queued I/Os may also be submitted early if
1224 * the task schedules (see below).
1225 *
Suresh Jayaraman75df7132011-09-21 10:00:16 +02001226 * Tracking blk_plug inside the task_struct will help with auto-flushing the
1227 * pending I/O should the task end up blocking between blk_start_plug() and
1228 * blk_finish_plug(). This is important from a performance perspective, but
1229 * also ensures that we don't deadlock. For instance, if the task is blocking
1230 * for a memory allocation, memory reclaim could end up wanting to free a
1231 * page belonging to that request that is currently residing in our private
1232 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
1233 * this kind of deadlock.
1234 */
Jens Axboe73c10102011-03-08 13:19:51 +01001235void blk_start_plug(struct blk_plug *plug)
1236{
Jens Axboe47c122e2021-10-06 06:34:11 -06001237 blk_start_plug_nr_ios(plug, 1);
Jens Axboe73c10102011-03-08 13:19:51 +01001238}
1239EXPORT_SYMBOL(blk_start_plug);
1240
NeilBrown74018dc2012-07-31 09:08:15 +02001241static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
NeilBrown048c9372011-04-18 09:52:22 +02001242{
1243 LIST_HEAD(callbacks);
1244
Shaohua Li2a7d5552012-07-31 09:08:15 +02001245 while (!list_empty(&plug->cb_list)) {
1246 list_splice_init(&plug->cb_list, &callbacks);
NeilBrown048c9372011-04-18 09:52:22 +02001247
Shaohua Li2a7d5552012-07-31 09:08:15 +02001248 while (!list_empty(&callbacks)) {
1249 struct blk_plug_cb *cb = list_first_entry(&callbacks,
NeilBrown048c9372011-04-18 09:52:22 +02001250 struct blk_plug_cb,
1251 list);
Shaohua Li2a7d5552012-07-31 09:08:15 +02001252 list_del(&cb->list);
NeilBrown74018dc2012-07-31 09:08:15 +02001253 cb->callback(cb, from_schedule);
Shaohua Li2a7d5552012-07-31 09:08:15 +02001254 }
NeilBrown048c9372011-04-18 09:52:22 +02001255 }
1256}
1257
NeilBrown9cbb1752012-07-31 09:08:14 +02001258struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
1259 int size)
1260{
1261 struct blk_plug *plug = current->plug;
1262 struct blk_plug_cb *cb;
1263
1264 if (!plug)
1265 return NULL;
1266
1267 list_for_each_entry(cb, &plug->cb_list, list)
1268 if (cb->callback == unplug && cb->data == data)
1269 return cb;
1270
1271 /* Not currently on the callback list */
1272 BUG_ON(size < sizeof(*cb));
1273 cb = kzalloc(size, GFP_ATOMIC);
1274 if (cb) {
1275 cb->data = data;
1276 cb->callback = unplug;
1277 list_add(&cb->list, &plug->cb_list);
1278 }
1279 return cb;
1280}
1281EXPORT_SYMBOL(blk_check_plugged);
1282
Christoph Hellwig008f75a2021-10-20 16:41:19 +02001283void blk_flush_plug(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01001284{
Pavel Begunkovb6004552021-10-20 16:41:18 +02001285 if (!list_empty(&plug->cb_list))
1286 flush_plug_callbacks(plug, from_schedule);
Jens Axboebc490f82021-10-18 10:12:12 -06001287 if (!rq_list_empty(plug->mq_list))
Jens Axboe320ae512013-10-24 09:20:05 +01001288 blk_mq_flush_plug_list(plug, from_schedule);
Jens Axboec5fc7b92021-11-03 05:49:07 -06001289 /*
1290 * Unconditionally flush out cached requests, even if the unplug
1291 * event came from schedule. Since we know hold references to the
1292 * queue for cached requests, we don't want a blocked task holding
1293 * up a queue freeze/quiesce event.
1294 */
1295 if (unlikely(!rq_list_empty(plug->cached_rq)))
Jens Axboe47c122e2021-10-06 06:34:11 -06001296 blk_mq_free_plug_rqs(plug);
Jens Axboe73c10102011-03-08 13:19:51 +01001297}
Jens Axboe73c10102011-03-08 13:19:51 +01001298
Jeff Moyer40405852019-01-08 16:57:34 -05001299/**
1300 * blk_finish_plug - mark the end of a batch of submitted I/O
1301 * @plug: The &struct blk_plug passed to blk_start_plug()
1302 *
1303 * Description:
1304 * Indicate that a batch of I/O submissions is complete. This function
1305 * must be paired with an initial call to blk_start_plug(). The intent
1306 * is to allow the block layer to optimize I/O submission. See the
1307 * documentation for blk_start_plug() for more information.
1308 */
Jens Axboe73c10102011-03-08 13:19:51 +01001309void blk_finish_plug(struct blk_plug *plug)
1310{
Christoph Hellwig008f75a2021-10-20 16:41:19 +02001311 if (plug == current->plug) {
1312 blk_flush_plug(plug, false);
1313 current->plug = NULL;
1314 }
Jens Axboe73c10102011-03-08 13:19:51 +01001315}
1316EXPORT_SYMBOL(blk_finish_plug);
1317
Ming Lei71ac8602020-05-14 16:45:09 +08001318void blk_io_schedule(void)
1319{
1320 /* Prevent hang_check timer from firing at us during very long I/O */
1321 unsigned long timeout = sysctl_hung_task_timeout_secs * HZ / 2;
1322
1323 if (timeout)
1324 io_schedule_timeout(timeout);
1325 else
1326 io_schedule();
1327}
1328EXPORT_SYMBOL_GPL(blk_io_schedule);
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330int __init blk_dev_init(void)
1331{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001332 BUILD_BUG_ON(REQ_OP_LAST >= (1 << REQ_OP_BITS));
1333 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001334 sizeof_field(struct request, cmd_flags));
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001335 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001336 sizeof_field(struct bio, bi_opf));
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02001337
Tejun Heo89b90be2011-01-03 15:01:47 +01001338 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
1339 kblockd_workqueue = alloc_workqueue("kblockd",
Matias Bjørling28747fc2014-06-11 23:43:54 +02001340 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (!kblockd_workqueue)
1342 panic("Failed to create kblockd\n");
1343
Ilya Dryomovc2789bd2015-11-20 22:16:46 +01001344 blk_requestq_cachep = kmem_cache_create("request_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02001345 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Omar Sandoval18fbda92017-01-31 14:53:20 -08001347 blk_debugfs_root = debugfs_create_dir("block", NULL);
Omar Sandoval18fbda92017-01-31 14:53:20 -08001348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return 0;
1350}