blob: 24886b69690f08ffd832a694c74accf139ac8906 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
Jens Axboe6728cb02008-01-31 13:03:55 +01006 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
9 */
10
11/*
12 * This handles all read/write requests to block devices
13 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/bio.h>
18#include <linux/blkdev.h>
Jens Axboe320ae512013-10-24 09:20:05 +010019#include <linux/blk-mq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/highmem.h>
21#include <linux/mm.h>
22#include <linux/kernel_stat.h>
23#include <linux/string.h>
24#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/completion.h>
26#include <linux/slab.h>
27#include <linux/swap.h>
28#include <linux/writeback.h>
Andrew Mortonfaccbd4b2006-12-10 02:19:35 -080029#include <linux/task_io_accounting_ops.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080030#include <linux/fault-inject.h>
Jens Axboe73c10102011-03-08 13:19:51 +010031#include <linux/list_sort.h>
Tejun Heoe3c78ca2011-10-19 14:32:38 +020032#include <linux/delay.h>
Tejun Heoaaf7c682012-04-19 16:29:22 -070033#include <linux/ratelimit.h>
Lin Ming6c954662013-03-23 11:42:26 +080034#include <linux/pm_runtime.h>
Tejun Heoeea8f412015-05-22 17:13:17 -040035#include <linux/blk-cgroup.h>
Omar Sandoval18fbda92017-01-31 14:53:20 -080036#include <linux/debugfs.h>
Li Zefan55782132009-06-09 13:43:05 +080037
38#define CREATE_TRACE_POINTS
39#include <trace/events/block.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Jens Axboe8324aa92008-01-29 14:51:59 +010041#include "blk.h"
Ming Lei43a5e4e2013-12-26 21:31:35 +080042#include "blk-mq.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070043#include "blk-mq-sched.h"
Jens Axboe87760e52016-11-09 12:38:14 -070044#include "blk-wbt.h"
Jens Axboe8324aa92008-01-29 14:51:59 +010045
Omar Sandoval18fbda92017-01-31 14:53:20 -080046#ifdef CONFIG_DEBUG_FS
47struct dentry *blk_debugfs_root;
48#endif
49
Mike Snitzerd07335e2010-11-16 12:52:38 +010050EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020051EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -070052EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Keith Busch3291fa52014-04-28 12:30:52 -060053EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
NeilBrowncbae8d42012-12-14 20:49:27 +010054EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010055
Tejun Heoa73f7302011-12-14 00:33:37 +010056DEFINE_IDA(blk_queue_ida);
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
59 * For the allocated request tables
60 */
Wei Tangd674d412015-11-24 09:58:45 +080061struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/*
64 * For queue allocation
65 */
Jens Axboe6728cb02008-01-31 13:03:55 +010066struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * Controlling structure to kblockd
70 */
Jens Axboeff856ba2006-01-09 16:02:34 +010071static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Tejun Heod40f75a2015-05-22 17:13:42 -040073static void blk_clear_congested(struct request_list *rl, int sync)
74{
Tejun Heod40f75a2015-05-22 17:13:42 -040075#ifdef CONFIG_CGROUP_WRITEBACK
76 clear_wb_congested(rl->blkg->wb_congested, sync);
77#else
Tejun Heo482cf792015-05-22 17:13:43 -040078 /*
79 * If !CGROUP_WRITEBACK, all blkg's map to bdi->wb and we shouldn't
80 * flip its congestion state for events on other blkcgs.
81 */
82 if (rl == &rl->q->root_rl)
Jan Karadc3b17c2017-02-02 15:56:50 +010083 clear_wb_congested(rl->q->backing_dev_info->wb.congested, sync);
Tejun Heod40f75a2015-05-22 17:13:42 -040084#endif
85}
86
87static void blk_set_congested(struct request_list *rl, int sync)
88{
Tejun Heod40f75a2015-05-22 17:13:42 -040089#ifdef CONFIG_CGROUP_WRITEBACK
90 set_wb_congested(rl->blkg->wb_congested, sync);
91#else
Tejun Heo482cf792015-05-22 17:13:43 -040092 /* see blk_clear_congested() */
93 if (rl == &rl->q->root_rl)
Jan Karadc3b17c2017-02-02 15:56:50 +010094 set_wb_congested(rl->q->backing_dev_info->wb.congested, sync);
Tejun Heod40f75a2015-05-22 17:13:42 -040095#endif
96}
97
Jens Axboe8324aa92008-01-29 14:51:59 +010098void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 int nr;
101
102 nr = q->nr_requests - (q->nr_requests / 8) + 1;
103 if (nr > q->nr_requests)
104 nr = q->nr_requests;
105 q->nr_congestion_on = nr;
106
107 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
108 if (nr < 1)
109 nr = 1;
110 q->nr_congestion_off = nr;
111}
112
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200113void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200115 memset(rq, 0, sizeof(*rq));
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700118 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200119 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100120 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900121 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200122 INIT_HLIST_NODE(&rq->hash);
123 RB_CLEAR_NODE(&rq->rb_node);
Jens Axboe63a71382008-02-08 12:41:03 +0100124 rq->tag = -1;
Jens Axboebd166ef2017-01-17 06:03:22 -0700125 rq->internal_tag = -1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900126 rq->start_time = jiffies;
Divyesh Shah91952912010-04-01 15:01:41 -0700127 set_start_time_ns(rq);
Jerome Marchand09e099d2011-01-05 16:57:38 +0100128 rq->part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200130EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
NeilBrown5bb23a62007-09-27 12:46:13 +0200132static void req_bio_endio(struct request *rq, struct bio *bio,
133 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100134{
Mike Snitzer78d8e582015-06-26 10:01:13 -0400135 if (error)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200136 bio->bi_error = error;
Tejun Heo797e7db2006-01-06 09:51:03 +0100137
Christoph Hellwige8064022016-10-20 15:12:13 +0200138 if (unlikely(rq->rq_flags & RQF_QUIET))
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600139 bio_set_flag(bio, BIO_QUIET);
Tejun Heo143a87f2011-01-25 12:43:52 +0100140
Kent Overstreetf79ea412012-09-20 16:38:30 -0700141 bio_advance(bio, nbytes);
Tejun Heo143a87f2011-01-25 12:43:52 +0100142
143 /* don't actually finish bio if it's part of flush sequence */
Christoph Hellwige8064022016-10-20 15:12:13 +0200144 if (bio->bi_iter.bi_size == 0 && !(rq->rq_flags & RQF_FLUSH_SEQ))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200145 bio_endio(bio);
Tejun Heo797e7db2006-01-06 09:51:03 +0100146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148void blk_dump_rq_flags(struct request *rq, char *msg)
149{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100150 printk(KERN_INFO "%s: dev %s: flags=%llx\n", msg,
151 rq->rq_disk ? rq->rq_disk->disk_name : "?",
Jens Axboe59533162013-05-23 12:25:08 +0200152 (unsigned long long) rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Tejun Heo83096eb2009-05-07 22:24:39 +0900154 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
155 (unsigned long long)blk_rq_pos(rq),
156 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Jens Axboeb4f42e22014-04-10 09:46:28 -0600157 printk(KERN_INFO " bio %p, biotail %p, len %u\n",
158 rq->bio, rq->biotail, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160EXPORT_SYMBOL(blk_dump_rq_flags);
161
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500162static void blk_delay_work(struct work_struct *work)
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200163{
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500164 struct request_queue *q;
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200165
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500166 q = container_of(work, struct request_queue, delay_work.work);
167 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200168 __blk_run_queue(q);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500169 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/**
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500173 * blk_delay_queue - restart queueing after defined interval
174 * @q: The &struct request_queue in question
175 * @msecs: Delay in msecs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 *
177 * Description:
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500178 * Sometimes queueing needs to be postponed for a little while, to allow
179 * resources to come back. This function will make sure that queueing is
Bart Van Assche70460572012-11-28 13:45:56 +0100180 * restarted around the specified time. Queue lock must be held.
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500181 */
182void blk_delay_queue(struct request_queue *q, unsigned long msecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Bart Van Assche70460572012-11-28 13:45:56 +0100184 if (likely(!blk_queue_dead(q)))
185 queue_delayed_work(kblockd_workqueue, &q->delay_work,
186 msecs_to_jiffies(msecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500188EXPORT_SYMBOL(blk_delay_queue);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/**
Jens Axboe21491412015-12-28 13:01:22 -0700191 * blk_start_queue_async - asynchronously restart a previously stopped queue
192 * @q: The &struct request_queue in question
193 *
194 * Description:
195 * blk_start_queue_async() will clear the stop flag on the queue, and
196 * ensure that the request_fn for the queue is run from an async
197 * context.
198 **/
199void blk_start_queue_async(struct request_queue *q)
200{
201 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
202 blk_run_queue_async(q);
203}
204EXPORT_SYMBOL(blk_start_queue_async);
205
206/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200208 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 *
210 * Description:
211 * blk_start_queue() will clear the stop flag on the queue, and call
212 * the request_fn for the queue if it was in a stopped state when
213 * entered. Also see blk_stop_queue(). Queue lock must be held.
214 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200215void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200217 WARN_ON(!irqs_disabled());
218
Nick Piggin75ad23b2008-04-29 14:48:33 +0200219 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200220 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222EXPORT_SYMBOL(blk_start_queue);
223
224/**
225 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200226 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 *
228 * Description:
229 * The Linux block layer assumes that a block driver will consume all
230 * entries on the request queue when the request_fn strategy is called.
231 * Often this will not happen, because of hardware limitations (queue
232 * depth settings). If a device driver gets a 'queue full' response,
233 * or if it simply chooses not to queue more I/O at one point, it can
234 * call this function to prevent the request_fn from being called until
235 * the driver has signalled it's ready to go again. This happens by calling
236 * blk_start_queue() to restart queue operations. Queue lock must be held.
237 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200238void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Tejun Heo136b5722012-08-21 13:18:24 -0700240 cancel_delayed_work(&q->delay_work);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200241 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243EXPORT_SYMBOL(blk_stop_queue);
244
245/**
246 * blk_sync_queue - cancel any pending callbacks on a queue
247 * @q: the queue
248 *
249 * Description:
250 * The block layer may perform asynchronous callback activity
251 * on a queue, such as calling the unplug function after a timeout.
252 * A block device may call blk_sync_queue to ensure that any
253 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200254 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * that its ->make_request_fn will not re-add plugging prior to calling
256 * this function.
257 *
Vivek Goyalda527772011-03-02 19:05:33 -0500258 * This function does not cancel any asynchronous activity arising
Masanari Iidada3dae52014-09-09 01:27:23 +0900259 * out of elevator or throttling code. That would require elevator_exit()
Tejun Heo5efd6112012-03-05 13:15:12 -0800260 * and blkcg_exit_queue() to be called with queue lock initialized.
Vivek Goyalda527772011-03-02 19:05:33 -0500261 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
263void blk_sync_queue(struct request_queue *q)
264{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100265 del_timer_sync(&q->timeout);
Ming Leif04c1fe2013-12-26 21:31:36 +0800266
267 if (q->mq_ops) {
268 struct blk_mq_hw_ctx *hctx;
269 int i;
270
Jens Axboe21c6e932017-04-10 09:54:56 -0600271 queue_for_each_hw_ctx(q, hctx, i)
Jens Axboe9f993732017-04-10 09:54:54 -0600272 cancel_delayed_work_sync(&hctx->run_work);
Ming Leif04c1fe2013-12-26 21:31:36 +0800273 } else {
274 cancel_delayed_work_sync(&q->delay_work);
275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277EXPORT_SYMBOL(blk_sync_queue);
278
279/**
Bart Van Asschec246e802012-12-06 14:32:01 +0100280 * __blk_run_queue_uncond - run a queue whether or not it has been stopped
281 * @q: The queue to run
282 *
283 * Description:
284 * Invoke request handling on a queue if there are any pending requests.
285 * May be used to restart request handling after a request has completed.
286 * This variant runs the queue whether or not the queue has been
287 * stopped. Must be called with the queue lock held and interrupts
288 * disabled. See also @blk_run_queue.
289 */
290inline void __blk_run_queue_uncond(struct request_queue *q)
291{
292 if (unlikely(blk_queue_dead(q)))
293 return;
294
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100295 /*
296 * Some request_fn implementations, e.g. scsi_request_fn(), unlock
297 * the queue lock internally. As a result multiple threads may be
298 * running such a request function concurrently. Keep track of the
299 * number of active request_fn invocations such that blk_drain_queue()
300 * can wait until all these request_fn calls have finished.
301 */
302 q->request_fn_active++;
Bart Van Asschec246e802012-12-06 14:32:01 +0100303 q->request_fn(q);
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100304 q->request_fn_active--;
Bart Van Asschec246e802012-12-06 14:32:01 +0100305}
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200306EXPORT_SYMBOL_GPL(__blk_run_queue_uncond);
Bart Van Asschec246e802012-12-06 14:32:01 +0100307
308/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200309 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200311 *
312 * Description:
313 * See @blk_run_queue. This variant must be called with the queue lock
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200314 * held and interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200316void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Tejun Heoa538cd02009-04-23 11:05:17 +0900318 if (unlikely(blk_queue_stopped(q)))
319 return;
320
Bart Van Asschec246e802012-12-06 14:32:01 +0100321 __blk_run_queue_uncond(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200322}
323EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200324
Nick Piggin75ad23b2008-04-29 14:48:33 +0200325/**
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200326 * blk_run_queue_async - run a single device queue in workqueue context
327 * @q: The queue to run
328 *
329 * Description:
330 * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
Bart Van Assche70460572012-11-28 13:45:56 +0100331 * of us. The caller must hold the queue lock.
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200332 */
333void blk_run_queue_async(struct request_queue *q)
334{
Bart Van Assche70460572012-11-28 13:45:56 +0100335 if (likely(!blk_queue_stopped(q) && !blk_queue_dead(q)))
Tejun Heoe7c2f962012-08-21 13:18:24 -0700336 mod_delayed_work(kblockd_workqueue, &q->delay_work, 0);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200337}
Jens Axboec21e6be2011-04-19 13:32:46 +0200338EXPORT_SYMBOL(blk_run_queue_async);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200339
340/**
Nick Piggin75ad23b2008-04-29 14:48:33 +0200341 * blk_run_queue - run a single device queue
342 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200343 *
344 * Description:
345 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900346 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200347 */
348void blk_run_queue(struct request_queue *q)
349{
350 unsigned long flags;
351
352 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200353 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 spin_unlock_irqrestore(q->queue_lock, flags);
355}
356EXPORT_SYMBOL(blk_run_queue);
357
Jens Axboe165125e2007-07-24 09:28:11 +0200358void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500359{
360 kobject_put(&q->kobj);
361}
Jens Axboed86e0e82011-05-27 07:44:43 +0200362EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500363
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200364/**
Bart Van Assche807592a2012-11-28 13:43:38 +0100365 * __blk_drain_queue - drain requests from request_queue
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200366 * @q: queue to drain
Tejun Heoc9a929d2011-10-19 14:42:16 +0200367 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200368 *
Tejun Heoc9a929d2011-10-19 14:42:16 +0200369 * Drain requests from @q. If @drain_all is set, all requests are drained.
370 * If not, only ELVPRIV requests are drained. The caller is responsible
371 * for ensuring that no new requests which need to be drained are queued.
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200372 */
Bart Van Assche807592a2012-11-28 13:43:38 +0100373static void __blk_drain_queue(struct request_queue *q, bool drain_all)
374 __releases(q->queue_lock)
375 __acquires(q->queue_lock)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200376{
Asias He458f27a2012-06-15 08:45:25 +0200377 int i;
378
Bart Van Assche807592a2012-11-28 13:43:38 +0100379 lockdep_assert_held(q->queue_lock);
380
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200381 while (true) {
Tejun Heo481a7d62011-12-14 00:33:37 +0100382 bool drain = false;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200383
Tejun Heob855b042012-03-06 21:24:55 +0100384 /*
385 * The caller might be trying to drain @q before its
386 * elevator is initialized.
387 */
388 if (q->elevator)
389 elv_drain_elevator(q);
390
Tejun Heo5efd6112012-03-05 13:15:12 -0800391 blkcg_drain_queue(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200392
Tejun Heo4eabc942011-12-15 20:03:04 +0100393 /*
394 * This function might be called on a queue which failed
Tejun Heob855b042012-03-06 21:24:55 +0100395 * driver init after queue creation or is not yet fully
396 * active yet. Some drivers (e.g. fd and loop) get unhappy
397 * in such cases. Kick queue iff dispatch queue has
398 * something on it and @q has request_fn set.
Tejun Heo4eabc942011-12-15 20:03:04 +0100399 */
Tejun Heob855b042012-03-06 21:24:55 +0100400 if (!list_empty(&q->queue_head) && q->request_fn)
Tejun Heo4eabc942011-12-15 20:03:04 +0100401 __blk_run_queue(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200402
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700403 drain |= q->nr_rqs_elvpriv;
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100404 drain |= q->request_fn_active;
Tejun Heo481a7d62011-12-14 00:33:37 +0100405
406 /*
407 * Unfortunately, requests are queued at and tracked from
408 * multiple places and there's no single counter which can
409 * be drained. Check all the queues and counters.
410 */
411 if (drain_all) {
Ming Leie97c2932014-09-25 23:23:46 +0800412 struct blk_flush_queue *fq = blk_get_flush_queue(q, NULL);
Tejun Heo481a7d62011-12-14 00:33:37 +0100413 drain |= !list_empty(&q->queue_head);
414 for (i = 0; i < 2; i++) {
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700415 drain |= q->nr_rqs[i];
Tejun Heo481a7d62011-12-14 00:33:37 +0100416 drain |= q->in_flight[i];
Ming Lei7c94e1c2014-09-25 23:23:43 +0800417 if (fq)
418 drain |= !list_empty(&fq->flush_queue[i]);
Tejun Heo481a7d62011-12-14 00:33:37 +0100419 }
420 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200421
Tejun Heo481a7d62011-12-14 00:33:37 +0100422 if (!drain)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200423 break;
Bart Van Assche807592a2012-11-28 13:43:38 +0100424
425 spin_unlock_irq(q->queue_lock);
426
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200427 msleep(10);
Bart Van Assche807592a2012-11-28 13:43:38 +0100428
429 spin_lock_irq(q->queue_lock);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200430 }
Asias He458f27a2012-06-15 08:45:25 +0200431
432 /*
433 * With queue marked dead, any woken up waiter will fail the
434 * allocation path, so the wakeup chaining is lost and we're
435 * left with hung waiters. We need to wake up those waiters.
436 */
437 if (q->request_fn) {
Tejun Heoa0516612012-06-26 15:05:44 -0700438 struct request_list *rl;
439
Tejun Heoa0516612012-06-26 15:05:44 -0700440 blk_queue_for_each_rl(rl, q)
441 for (i = 0; i < ARRAY_SIZE(rl->wait); i++)
442 wake_up_all(&rl->wait[i]);
Asias He458f27a2012-06-15 08:45:25 +0200443 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200444}
445
Tejun Heoc9a929d2011-10-19 14:42:16 +0200446/**
Tejun Heod7325802012-03-05 13:14:58 -0800447 * blk_queue_bypass_start - enter queue bypass mode
448 * @q: queue of interest
449 *
450 * In bypass mode, only the dispatch FIFO queue of @q is used. This
451 * function makes @q enter bypass mode and drains all requests which were
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800452 * throttled or issued before. On return, it's guaranteed that no request
Tejun Heo80fd9972012-04-13 14:50:53 -0700453 * is being throttled or has ELVPRIV set and blk_queue_bypass() %true
454 * inside queue or RCU read lock.
Tejun Heod7325802012-03-05 13:14:58 -0800455 */
456void blk_queue_bypass_start(struct request_queue *q)
457{
458 spin_lock_irq(q->queue_lock);
Tejun Heo776687b2014-07-01 10:29:17 -0600459 q->bypass_depth++;
Tejun Heod7325802012-03-05 13:14:58 -0800460 queue_flag_set(QUEUE_FLAG_BYPASS, q);
461 spin_unlock_irq(q->queue_lock);
462
Tejun Heo776687b2014-07-01 10:29:17 -0600463 /*
464 * Queues start drained. Skip actual draining till init is
465 * complete. This avoids lenghty delays during queue init which
466 * can happen many times during boot.
467 */
468 if (blk_queue_init_done(q)) {
Bart Van Assche807592a2012-11-28 13:43:38 +0100469 spin_lock_irq(q->queue_lock);
470 __blk_drain_queue(q, false);
471 spin_unlock_irq(q->queue_lock);
472
Tejun Heob82d4b12012-04-13 13:11:31 -0700473 /* ensure blk_queue_bypass() is %true inside RCU read lock */
474 synchronize_rcu();
475 }
Tejun Heod7325802012-03-05 13:14:58 -0800476}
477EXPORT_SYMBOL_GPL(blk_queue_bypass_start);
478
479/**
480 * blk_queue_bypass_end - leave queue bypass mode
481 * @q: queue of interest
482 *
483 * Leave bypass mode and restore the normal queueing behavior.
484 */
485void blk_queue_bypass_end(struct request_queue *q)
486{
487 spin_lock_irq(q->queue_lock);
488 if (!--q->bypass_depth)
489 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
490 WARN_ON_ONCE(q->bypass_depth < 0);
491 spin_unlock_irq(q->queue_lock);
492}
493EXPORT_SYMBOL_GPL(blk_queue_bypass_end);
494
Jens Axboeaed3ea92014-12-22 14:04:42 -0700495void blk_set_queue_dying(struct request_queue *q)
496{
Bart Van Assche1b856082016-08-16 16:48:36 -0700497 spin_lock_irq(q->queue_lock);
498 queue_flag_set(QUEUE_FLAG_DYING, q);
499 spin_unlock_irq(q->queue_lock);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700500
Ming Leid3cfb2a2017-03-27 20:06:58 +0800501 /*
502 * When queue DYING flag is set, we need to block new req
503 * entering queue, so we call blk_freeze_queue_start() to
504 * prevent I/O from crossing blk_queue_enter().
505 */
506 blk_freeze_queue_start(q);
507
Jens Axboeaed3ea92014-12-22 14:04:42 -0700508 if (q->mq_ops)
509 blk_mq_wake_waiters(q);
510 else {
511 struct request_list *rl;
512
Tahsin Erdoganbbfc3c52017-01-31 22:36:50 -0800513 spin_lock_irq(q->queue_lock);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700514 blk_queue_for_each_rl(rl, q) {
515 if (rl->rq_pool) {
516 wake_up(&rl->wait[BLK_RW_SYNC]);
517 wake_up(&rl->wait[BLK_RW_ASYNC]);
518 }
519 }
Tahsin Erdoganbbfc3c52017-01-31 22:36:50 -0800520 spin_unlock_irq(q->queue_lock);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700521 }
522}
523EXPORT_SYMBOL_GPL(blk_set_queue_dying);
524
Tejun Heod7325802012-03-05 13:14:58 -0800525/**
Tejun Heoc9a929d2011-10-19 14:42:16 +0200526 * blk_cleanup_queue - shutdown a request queue
527 * @q: request queue to shutdown
528 *
Bart Van Asschec246e802012-12-06 14:32:01 +0100529 * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
530 * put it. All future requests will be failed immediately with -ENODEV.
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500531 */
Jens Axboe6728cb02008-01-31 13:03:55 +0100532void blk_cleanup_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500533{
Tejun Heoc9a929d2011-10-19 14:42:16 +0200534 spinlock_t *lock = q->queue_lock;
Jens Axboee3335de2008-09-18 09:22:54 -0700535
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100536 /* mark @q DYING, no new request or merges will be allowed afterwards */
Al Viro483f4af2006-03-18 18:34:37 -0500537 mutex_lock(&q->sysfs_lock);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700538 blk_set_queue_dying(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200539 spin_lock_irq(lock);
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800540
Tejun Heo80fd9972012-04-13 14:50:53 -0700541 /*
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100542 * A dying queue is permanently in bypass mode till released. Note
Tejun Heo80fd9972012-04-13 14:50:53 -0700543 * that, unlike blk_queue_bypass_start(), we aren't performing
544 * synchronize_rcu() after entering bypass mode to avoid the delay
545 * as some drivers create and destroy a lot of queues while
546 * probing. This is still safe because blk_release_queue() will be
547 * called only after the queue refcnt drops to zero and nothing,
548 * RCU or not, would be traversing the queue by then.
549 */
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800550 q->bypass_depth++;
551 queue_flag_set(QUEUE_FLAG_BYPASS, q);
552
Tejun Heoc9a929d2011-10-19 14:42:16 +0200553 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
554 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100555 queue_flag_set(QUEUE_FLAG_DYING, q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200556 spin_unlock_irq(lock);
557 mutex_unlock(&q->sysfs_lock);
558
Bart Van Asschec246e802012-12-06 14:32:01 +0100559 /*
560 * Drain all requests queued before DYING marking. Set DEAD flag to
561 * prevent that q->request_fn() gets invoked after draining finished.
562 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400563 blk_freeze_queue(q);
Bart Van Asschee869b5462017-04-26 13:47:52 -0700564 if (!q->mq_ops) {
565 spin_lock_irq(lock);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800566 __blk_drain_queue(q, true);
Bart Van Asschee869b5462017-04-26 13:47:52 -0700567 } else {
568 blk_mq_debugfs_unregister_mq(q);
569 spin_lock_irq(lock);
570 }
Bart Van Asschec246e802012-12-06 14:32:01 +0100571 queue_flag_set(QUEUE_FLAG_DEAD, q);
Bart Van Assche807592a2012-11-28 13:43:38 +0100572 spin_unlock_irq(lock);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200573
Dan Williams5a48fc12015-10-21 13:20:23 -0400574 /* for synchronous bio-based driver finish in-flight integrity i/o */
575 blk_flush_integrity();
576
Tejun Heoc9a929d2011-10-19 14:42:16 +0200577 /* @q won't process any more request, flush async actions */
Jan Karadc3b17c2017-02-02 15:56:50 +0100578 del_timer_sync(&q->backing_dev_info->laptop_mode_wb_timer);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200579 blk_sync_queue(q);
580
Bart Van Assche45a9c9d2014-12-09 16:57:48 +0100581 if (q->mq_ops)
582 blk_mq_free_queue(q);
Dan Williams3ef28e82015-10-21 13:20:12 -0400583 percpu_ref_exit(&q->q_usage_counter);
Bart Van Assche45a9c9d2014-12-09 16:57:48 +0100584
Asias He5e5cfac2012-05-24 23:28:52 +0800585 spin_lock_irq(lock);
586 if (q->queue_lock != &q->__queue_lock)
587 q->queue_lock = &q->__queue_lock;
588 spin_unlock_irq(lock);
589
Tejun Heoc9a929d2011-10-19 14:42:16 +0200590 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500591 blk_put_queue(q);
592}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593EXPORT_SYMBOL(blk_cleanup_queue);
594
David Rientjes271508d2015-03-24 16:21:16 -0700595/* Allocate memory local to the request queue */
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700596static void *alloc_request_simple(gfp_t gfp_mask, void *data)
David Rientjes271508d2015-03-24 16:21:16 -0700597{
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700598 struct request_queue *q = data;
599
600 return kmem_cache_alloc_node(request_cachep, gfp_mask, q->node);
David Rientjes271508d2015-03-24 16:21:16 -0700601}
602
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700603static void free_request_simple(void *element, void *data)
David Rientjes271508d2015-03-24 16:21:16 -0700604{
605 kmem_cache_free(request_cachep, element);
606}
607
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700608static void *alloc_request_size(gfp_t gfp_mask, void *data)
609{
610 struct request_queue *q = data;
611 struct request *rq;
612
613 rq = kmalloc_node(sizeof(struct request) + q->cmd_size, gfp_mask,
614 q->node);
615 if (rq && q->init_rq_fn && q->init_rq_fn(q, rq, gfp_mask) < 0) {
616 kfree(rq);
617 rq = NULL;
618 }
619 return rq;
620}
621
622static void free_request_size(void *element, void *data)
623{
624 struct request_queue *q = data;
625
626 if (q->exit_rq_fn)
627 q->exit_rq_fn(q, element);
628 kfree(element);
629}
630
Tejun Heo5b788ce2012-06-04 20:40:59 -0700631int blk_init_rl(struct request_list *rl, struct request_queue *q,
632 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400634 if (unlikely(rl->rq_pool))
635 return 0;
636
Tejun Heo5b788ce2012-06-04 20:40:59 -0700637 rl->q = q;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200638 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
639 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200640 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
641 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700643 if (q->cmd_size) {
644 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ,
645 alloc_request_size, free_request_size,
646 q, gfp_mask, q->node);
647 } else {
648 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ,
649 alloc_request_simple, free_request_simple,
650 q, gfp_mask, q->node);
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (!rl->rq_pool)
653 return -ENOMEM;
654
655 return 0;
656}
657
Tejun Heo5b788ce2012-06-04 20:40:59 -0700658void blk_exit_rl(struct request_list *rl)
659{
660 if (rl->rq_pool)
661 mempool_destroy(rl->rq_pool);
662}
663
Jens Axboe165125e2007-07-24 09:28:11 +0200664struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100666 return blk_alloc_queue_node(gfp_mask, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700667}
668EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100670int blk_queue_enter(struct request_queue *q, bool nowait)
Dan Williams3ef28e82015-10-21 13:20:12 -0400671{
672 while (true) {
673 int ret;
674
675 if (percpu_ref_tryget_live(&q->q_usage_counter))
676 return 0;
677
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100678 if (nowait)
Dan Williams3ef28e82015-10-21 13:20:12 -0400679 return -EBUSY;
680
Ming Lei5ed61d32017-03-27 20:06:56 +0800681 /*
Ming Lei1671d522017-03-27 20:06:57 +0800682 * read pair of barrier in blk_freeze_queue_start(),
Ming Lei5ed61d32017-03-27 20:06:56 +0800683 * we need to order reading __PERCPU_REF_DEAD flag of
Ming Leid3cfb2a2017-03-27 20:06:58 +0800684 * .q_usage_counter and reading .mq_freeze_depth or
685 * queue dying flag, otherwise the following wait may
686 * never return if the two reads are reordered.
Ming Lei5ed61d32017-03-27 20:06:56 +0800687 */
688 smp_rmb();
689
Dan Williams3ef28e82015-10-21 13:20:12 -0400690 ret = wait_event_interruptible(q->mq_freeze_wq,
691 !atomic_read(&q->mq_freeze_depth) ||
692 blk_queue_dying(q));
693 if (blk_queue_dying(q))
694 return -ENODEV;
695 if (ret)
696 return ret;
697 }
698}
699
700void blk_queue_exit(struct request_queue *q)
701{
702 percpu_ref_put(&q->q_usage_counter);
703}
704
705static void blk_queue_usage_counter_release(struct percpu_ref *ref)
706{
707 struct request_queue *q =
708 container_of(ref, struct request_queue, q_usage_counter);
709
710 wake_up_all(&q->mq_freeze_wq);
711}
712
Christoph Hellwig287922eb2015-10-30 20:57:30 +0800713static void blk_rq_timed_out_timer(unsigned long data)
714{
715 struct request_queue *q = (struct request_queue *)data;
716
717 kblockd_schedule_work(&q->timeout_work);
718}
719
Jens Axboe165125e2007-07-24 09:28:11 +0200720struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700721{
Jens Axboe165125e2007-07-24 09:28:11 +0200722 struct request_queue *q;
Christoph Lameter19460892005-06-23 00:08:19 -0700723
Jens Axboe8324aa92008-01-29 14:51:59 +0100724 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700725 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!q)
727 return NULL;
728
Dan Carpenter00380a42012-03-23 09:58:54 +0100729 q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
Tejun Heoa73f7302011-12-14 00:33:37 +0100730 if (q->id < 0)
Ming Lei3d2936f2014-05-27 23:35:14 +0800731 goto fail_q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100732
Kent Overstreet54efd502015-04-23 22:37:18 -0700733 q->bio_split = bioset_create(BIO_POOL_SIZE, 0);
734 if (!q->bio_split)
735 goto fail_id;
736
Jan Karad03f6cd2017-02-02 15:56:51 +0100737 q->backing_dev_info = bdi_alloc_node(gfp_mask, node_id);
738 if (!q->backing_dev_info)
Kent Overstreet54efd502015-04-23 22:37:18 -0700739 goto fail_split;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700740
Jens Axboea83b5762017-03-21 17:20:01 -0600741 q->stats = blk_alloc_queue_stats();
742 if (!q->stats)
743 goto fail_stats;
744
Jan Karadc3b17c2017-02-02 15:56:50 +0100745 q->backing_dev_info->ra_pages =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 (VM_MAX_READAHEAD * 1024) / PAGE_SIZE;
Jan Karadc3b17c2017-02-02 15:56:50 +0100747 q->backing_dev_info->capabilities = BDI_CAP_CGROUP_WRITEBACK;
748 q->backing_dev_info->name = "block";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 q->node = node_id;
750
Jan Karadc3b17c2017-02-02 15:56:50 +0100751 setup_timer(&q->backing_dev_info->laptop_mode_wb_timer,
Matthew Garrett31373d02010-04-06 14:25:14 +0200752 laptop_mode_timer_fn, (unsigned long) q);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700753 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
Tejun Heob855b042012-03-06 21:24:55 +0100754 INIT_LIST_HEAD(&q->queue_head);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700755 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100756 INIT_LIST_HEAD(&q->icq_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800757#ifdef CONFIG_BLK_CGROUP
Tejun Heoe8989fa2012-03-05 13:15:20 -0800758 INIT_LIST_HEAD(&q->blkg_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800759#endif
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500760 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
Al Viro483f4af2006-03-18 18:34:37 -0500761
Jens Axboe8324aa92008-01-29 14:51:59 +0100762 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Al Viro483f4af2006-03-18 18:34:37 -0500764 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700765 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500766
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500767 /*
768 * By default initialize queue_lock to internal lock and driver can
769 * override it later if need be.
770 */
771 q->queue_lock = &q->__queue_lock;
772
Tejun Heob82d4b12012-04-13 13:11:31 -0700773 /*
774 * A queue starts its life with bypass turned on to avoid
775 * unnecessary bypass on/off overhead and nasty surprises during
Tejun Heo749fefe2012-09-20 14:08:52 -0700776 * init. The initial bypass will be finished when the queue is
777 * registered by blk_register_queue().
Tejun Heob82d4b12012-04-13 13:11:31 -0700778 */
779 q->bypass_depth = 1;
780 __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
781
Jens Axboe320ae512013-10-24 09:20:05 +0100782 init_waitqueue_head(&q->mq_freeze_wq);
783
Dan Williams3ef28e82015-10-21 13:20:12 -0400784 /*
785 * Init percpu_ref in atomic mode so that it's faster to shutdown.
786 * See blk_register_queue() for details.
787 */
788 if (percpu_ref_init(&q->q_usage_counter,
789 blk_queue_usage_counter_release,
790 PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
Mikulas Patockafff49962013-10-14 12:11:36 -0400791 goto fail_bdi;
Tejun Heof51b8022012-03-05 13:15:05 -0800792
Dan Williams3ef28e82015-10-21 13:20:12 -0400793 if (blkcg_init_queue(q))
794 goto fail_ref;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100797
Dan Williams3ef28e82015-10-21 13:20:12 -0400798fail_ref:
799 percpu_ref_exit(&q->q_usage_counter);
Mikulas Patockafff49962013-10-14 12:11:36 -0400800fail_bdi:
Jens Axboea83b5762017-03-21 17:20:01 -0600801 blk_free_queue_stats(q->stats);
802fail_stats:
Jan Karad03f6cd2017-02-02 15:56:51 +0100803 bdi_put(q->backing_dev_info);
Kent Overstreet54efd502015-04-23 22:37:18 -0700804fail_split:
805 bioset_free(q->bio_split);
Tejun Heoa73f7302011-12-14 00:33:37 +0100806fail_id:
807 ida_simple_remove(&blk_queue_ida, q->id);
808fail_q:
809 kmem_cache_free(blk_requestq_cachep, q);
810 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
Christoph Lameter19460892005-06-23 00:08:19 -0700812EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814/**
815 * blk_init_queue - prepare a request queue for use with a block device
816 * @rfn: The function to be called to process requests that have been
817 * placed on the queue.
818 * @lock: Request queue spin lock
819 *
820 * Description:
821 * If a block device wishes to use the standard request handling procedures,
822 * which sorts requests and coalesces adjacent requests, then it must
823 * call blk_init_queue(). The function @rfn will be called when there
824 * are requests on the queue that need to be processed. If the device
825 * supports plugging, then @rfn may not be called immediately when requests
826 * are available on the queue, but may be called at some time later instead.
827 * Plugged queues are generally unplugged when a buffer belonging to one
828 * of the requests on the queue is needed, or due to memory pressure.
829 *
830 * @rfn is not required, or even expected, to remove all requests off the
831 * queue, but only as many as it can handle at a time. If it does leave
832 * requests on the queue, it is responsible for arranging that the requests
833 * get dealt with eventually.
834 *
835 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200836 * request queue; this lock will be taken also from interrupt context, so irq
837 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200839 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 * it didn't succeed.
841 *
842 * Note:
843 * blk_init_queue() must be paired with a blk_cleanup_queue() call
844 * when the block device is deactivated (such as at module unload).
845 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700846
Jens Axboe165125e2007-07-24 09:28:11 +0200847struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100849 return blk_init_queue_node(rfn, lock, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700850}
851EXPORT_SYMBOL(blk_init_queue);
852
Jens Axboe165125e2007-07-24 09:28:11 +0200853struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700854blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
855{
Christoph Hellwig5ea708d2017-01-03 14:52:44 +0300856 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Christoph Hellwig5ea708d2017-01-03 14:52:44 +0300858 q = blk_alloc_queue_node(GFP_KERNEL, node_id);
859 if (!q)
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600860 return NULL;
861
Christoph Hellwig5ea708d2017-01-03 14:52:44 +0300862 q->request_fn = rfn;
863 if (lock)
864 q->queue_lock = lock;
865 if (blk_init_allocated_queue(q) < 0) {
866 blk_cleanup_queue(q);
867 return NULL;
868 }
Christoph Hellwig18741982014-02-10 09:29:00 -0700869
Mike Snitzer7982e902014-03-08 17:20:01 -0700870 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200871}
872EXPORT_SYMBOL(blk_init_queue_node);
873
Jens Axboedece1632015-11-05 10:41:16 -0700874static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio);
Mike Snitzer336b7e12015-05-11 14:06:32 -0400875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Christoph Hellwig5ea708d2017-01-03 14:52:44 +0300877int blk_init_allocated_queue(struct request_queue *q)
878{
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700879 q->fq = blk_alloc_flush_queue(q, NUMA_NO_NODE, q->cmd_size);
Ming Leiba483382014-09-25 23:23:44 +0800880 if (!q->fq)
Christoph Hellwig5ea708d2017-01-03 14:52:44 +0300881 return -ENOMEM;
Mike Snitzer7982e902014-03-08 17:20:01 -0700882
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700883 if (q->init_rq_fn && q->init_rq_fn(q, q->fq->flush_rq, GFP_KERNEL))
884 goto out_free_flush_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 if (blk_init_rl(&q->root_rl, q, GFP_KERNEL))
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700887 goto out_exit_flush_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Christoph Hellwig287922eb2015-10-30 20:57:30 +0800889 INIT_WORK(&q->timeout_work, blk_timeout_work);
Tejun Heo60ea8222012-09-20 14:09:30 -0700890 q->queue_flags |= QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500891
Jens Axboef3b144a2009-03-06 08:48:33 +0100892 /*
893 * This also sets hw/phys segments, boundary and size
894 */
Jens Axboec20e8de2011-09-12 12:03:37 +0200895 blk_queue_make_request(q, blk_queue_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Alan Stern44ec9542007-02-20 11:01:57 -0500897 q->sg_reserved_size = INT_MAX;
898
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600899 /* Protect q->elevator from elevator_change */
900 mutex_lock(&q->sysfs_lock);
901
Tejun Heob82d4b12012-04-13 13:11:31 -0700902 /* init elevator */
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600903 if (elevator_init(q, NULL)) {
904 mutex_unlock(&q->sysfs_lock);
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700905 goto out_exit_flush_rq;
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600906 }
907
908 mutex_unlock(&q->sysfs_lock);
Christoph Hellwig5ea708d2017-01-03 14:52:44 +0300909 return 0;
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600910
Christoph Hellwig6d247d72017-01-27 09:51:45 -0700911out_exit_flush_rq:
912 if (q->exit_rq_fn)
913 q->exit_rq_fn(q, q->fq->flush_rq);
914out_free_flush_queue:
Ming Leiba483382014-09-25 23:23:44 +0800915 blk_free_flush_queue(q->fq);
Christoph Hellwig5ea708d2017-01-03 14:52:44 +0300916 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
Mike Snitzer51514122011-11-23 10:59:13 +0100918EXPORT_SYMBOL(blk_init_allocated_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Tejun Heo09ac46c2011-12-14 00:33:38 +0100920bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100922 if (likely(!blk_queue_dying(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100923 __blk_get_queue(q);
924 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
Tejun Heo09ac46c2011-12-14 00:33:38 +0100927 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
Jens Axboed86e0e82011-05-27 07:44:43 +0200929EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Tejun Heo5b788ce2012-06-04 20:40:59 -0700931static inline void blk_free_request(struct request_list *rl, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Christoph Hellwige8064022016-10-20 15:12:13 +0200933 if (rq->rq_flags & RQF_ELVPRIV) {
Tejun Heo5b788ce2012-06-04 20:40:59 -0700934 elv_put_request(rl->q, rq);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100935 if (rq->elv.icq)
Tejun Heo11a31222012-02-07 07:51:30 +0100936 put_io_context(rq->elv.icq->ioc);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100937 }
938
Tejun Heo5b788ce2012-06-04 20:40:59 -0700939 mempool_free(rq, rl->rq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942/*
943 * ioc_batching returns true if the ioc is a valid batching request and
944 * should be given priority access to a request.
945 */
Jens Axboe165125e2007-07-24 09:28:11 +0200946static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 if (!ioc)
949 return 0;
950
951 /*
952 * Make sure the process is able to allocate at least 1 request
953 * even if the batch times out, otherwise we could theoretically
954 * lose wakeups.
955 */
956 return ioc->nr_batch_requests == q->nr_batching ||
957 (ioc->nr_batch_requests > 0
958 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
959}
960
961/*
962 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
963 * will cause the process to be a "batcher" on all queues in the system. This
964 * is the behaviour we want though - once it gets a wakeup it should be given
965 * a nice run.
966 */
Jens Axboe165125e2007-07-24 09:28:11 +0200967static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 if (!ioc || ioc_batching(q, ioc))
970 return;
971
972 ioc->nr_batch_requests = q->nr_batching;
973 ioc->last_waited = jiffies;
974}
975
Tejun Heo5b788ce2012-06-04 20:40:59 -0700976static void __freed_request(struct request_list *rl, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700978 struct request_queue *q = rl->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Tejun Heod40f75a2015-05-22 17:13:42 -0400980 if (rl->count[sync] < queue_congestion_off_threshold(q))
981 blk_clear_congested(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Jens Axboe1faa16d2009-04-06 14:48:01 +0200983 if (rl->count[sync] + 1 <= q->nr_requests) {
984 if (waitqueue_active(&rl->wait[sync]))
985 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Tejun Heo5b788ce2012-06-04 20:40:59 -0700987 blk_clear_rl_full(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989}
990
991/*
992 * A request has just been released. Account for it, update the full and
993 * congestion status, wake up any waiters. Called under q->queue_lock.
994 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200995static void freed_request(struct request_list *rl, bool sync,
996 req_flags_t rq_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700998 struct request_queue *q = rl->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001000 q->nr_rqs[sync]--;
Jens Axboe1faa16d2009-04-06 14:48:01 +02001001 rl->count[sync]--;
Christoph Hellwige8064022016-10-20 15:12:13 +02001002 if (rq_flags & RQF_ELVPRIV)
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001003 q->nr_rqs_elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Tejun Heo5b788ce2012-06-04 20:40:59 -07001005 __freed_request(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Jens Axboe1faa16d2009-04-06 14:48:01 +02001007 if (unlikely(rl->starved[sync ^ 1]))
Tejun Heo5b788ce2012-06-04 20:40:59 -07001008 __freed_request(rl, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Jens Axboee3a2b3f2014-05-20 11:49:02 -06001011int blk_update_nr_requests(struct request_queue *q, unsigned int nr)
1012{
1013 struct request_list *rl;
Tejun Heod40f75a2015-05-22 17:13:42 -04001014 int on_thresh, off_thresh;
Jens Axboee3a2b3f2014-05-20 11:49:02 -06001015
1016 spin_lock_irq(q->queue_lock);
1017 q->nr_requests = nr;
1018 blk_queue_congestion_threshold(q);
Tejun Heod40f75a2015-05-22 17:13:42 -04001019 on_thresh = queue_congestion_on_threshold(q);
1020 off_thresh = queue_congestion_off_threshold(q);
Jens Axboee3a2b3f2014-05-20 11:49:02 -06001021
1022 blk_queue_for_each_rl(rl, q) {
Tejun Heod40f75a2015-05-22 17:13:42 -04001023 if (rl->count[BLK_RW_SYNC] >= on_thresh)
1024 blk_set_congested(rl, BLK_RW_SYNC);
1025 else if (rl->count[BLK_RW_SYNC] < off_thresh)
1026 blk_clear_congested(rl, BLK_RW_SYNC);
1027
1028 if (rl->count[BLK_RW_ASYNC] >= on_thresh)
1029 blk_set_congested(rl, BLK_RW_ASYNC);
1030 else if (rl->count[BLK_RW_ASYNC] < off_thresh)
1031 blk_clear_congested(rl, BLK_RW_ASYNC);
1032
Jens Axboee3a2b3f2014-05-20 11:49:02 -06001033 if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
1034 blk_set_rl_full(rl, BLK_RW_SYNC);
1035 } else {
1036 blk_clear_rl_full(rl, BLK_RW_SYNC);
1037 wake_up(&rl->wait[BLK_RW_SYNC]);
1038 }
1039
1040 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
1041 blk_set_rl_full(rl, BLK_RW_ASYNC);
1042 } else {
1043 blk_clear_rl_full(rl, BLK_RW_ASYNC);
1044 wake_up(&rl->wait[BLK_RW_ASYNC]);
1045 }
1046 }
1047
1048 spin_unlock_irq(q->queue_lock);
1049 return 0;
1050}
1051
Tejun Heoda8303c2011-10-19 14:33:05 +02001052/**
Tejun Heoa06e05e2012-06-04 20:40:55 -07001053 * __get_request - get a free request
Tejun Heo5b788ce2012-06-04 20:40:59 -07001054 * @rl: request list to allocate from
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001055 * @op: operation and flags
Tejun Heoda8303c2011-10-19 14:33:05 +02001056 * @bio: bio to allocate request for (can be %NULL)
1057 * @gfp_mask: allocation mask
1058 *
1059 * Get a free request from @q. This function may fail under memory
1060 * pressure or if @q is dead.
1061 *
Masanari Iidada3dae52014-09-09 01:27:23 +09001062 * Must be called with @q->queue_lock held and,
Joe Lawrencea492f072014-08-28 08:15:21 -06001063 * Returns ERR_PTR on failure, with @q->queue_lock held.
1064 * Returns request pointer on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 */
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001066static struct request *__get_request(struct request_list *rl, unsigned int op,
1067 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Tejun Heo5b788ce2012-06-04 20:40:59 -07001069 struct request_queue *q = rl->q;
Tejun Heob6792812012-03-05 13:15:23 -08001070 struct request *rq;
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001071 struct elevator_type *et = q->elevator->type;
1072 struct io_context *ioc = rq_ioc(bio);
Tejun Heof1f8cc92011-12-14 00:33:42 +01001073 struct io_cq *icq = NULL;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001074 const bool is_sync = op_is_sync(op);
Tejun Heo75eb6c32011-10-19 14:31:22 +02001075 int may_queue;
Christoph Hellwige8064022016-10-20 15:12:13 +02001076 req_flags_t rq_flags = RQF_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001078 if (unlikely(blk_queue_dying(q)))
Joe Lawrencea492f072014-08-28 08:15:21 -06001079 return ERR_PTR(-ENODEV);
Tejun Heoda8303c2011-10-19 14:33:05 +02001080
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001081 may_queue = elv_may_queue(q, op);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001082 if (may_queue == ELV_MQUEUE_NO)
1083 goto rq_starved;
1084
Jens Axboe1faa16d2009-04-06 14:48:01 +02001085 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
1086 if (rl->count[is_sync]+1 >= q->nr_requests) {
Tejun Heof2dbd762011-12-14 00:33:40 +01001087 /*
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001088 * The queue will fill after this allocation, so set
1089 * it as full, and mark this process as "batching".
1090 * This process will be allowed to complete a batch of
1091 * requests, others will be blocked.
1092 */
Tejun Heo5b788ce2012-06-04 20:40:59 -07001093 if (!blk_rl_full(rl, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001094 ioc_set_batching(q, ioc);
Tejun Heo5b788ce2012-06-04 20:40:59 -07001095 blk_set_rl_full(rl, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001096 } else {
1097 if (may_queue != ELV_MQUEUE_MUST
1098 && !ioc_batching(q, ioc)) {
1099 /*
1100 * The queue is full and the allocating
1101 * process is not a "batcher", and not
1102 * exempted by the IO scheduler
1103 */
Joe Lawrencea492f072014-08-28 08:15:21 -06001104 return ERR_PTR(-ENOMEM);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001105 }
1106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
Tejun Heod40f75a2015-05-22 17:13:42 -04001108 blk_set_congested(rl, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
Jens Axboe082cf692005-06-28 16:35:11 +02001111 /*
1112 * Only allow batching queuers to allocate up to 50% over the defined
1113 * limit of requests, otherwise we could have thousands of requests
1114 * allocated with any setting of ->nr_requests
1115 */
Jens Axboe1faa16d2009-04-06 14:48:01 +02001116 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Joe Lawrencea492f072014-08-28 08:15:21 -06001117 return ERR_PTR(-ENOMEM);
Hugh Dickinsfd782a42005-06-29 15:15:40 +01001118
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001119 q->nr_rqs[is_sync]++;
Jens Axboe1faa16d2009-04-06 14:48:01 +02001120 rl->count[is_sync]++;
1121 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02001122
Tejun Heof1f8cc92011-12-14 00:33:42 +01001123 /*
1124 * Decide whether the new request will be managed by elevator. If
Christoph Hellwige8064022016-10-20 15:12:13 +02001125 * so, mark @rq_flags and increment elvpriv. Non-zero elvpriv will
Tejun Heof1f8cc92011-12-14 00:33:42 +01001126 * prevent the current elevator from being destroyed until the new
1127 * request is freed. This guarantees icq's won't be destroyed and
1128 * makes creating new ones safe.
1129 *
Christoph Hellwige6f7f932017-01-25 11:17:11 +01001130 * Flush requests do not use the elevator so skip initialization.
1131 * This allows a request to share the flush and elevator data.
1132 *
Tejun Heof1f8cc92011-12-14 00:33:42 +01001133 * Also, lookup icq while holding queue_lock. If it doesn't exist,
1134 * it will be created after releasing queue_lock.
1135 */
Christoph Hellwige6f7f932017-01-25 11:17:11 +01001136 if (!op_is_flush(op) && !blk_queue_bypass(q)) {
Christoph Hellwige8064022016-10-20 15:12:13 +02001137 rq_flags |= RQF_ELVPRIV;
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001138 q->nr_rqs_elvpriv++;
Tejun Heof1f8cc92011-12-14 00:33:42 +01001139 if (et->icq_cache && ioc)
1140 icq = ioc_lookup_icq(ioc, q);
Mike Snitzer9d5a4e92011-02-11 11:05:46 +01001141 }
Tejun Heocb98fc82005-10-28 08:29:39 +02001142
Jens Axboef253b862010-10-24 22:06:02 +02001143 if (blk_queue_io_stat(q))
Christoph Hellwige8064022016-10-20 15:12:13 +02001144 rq_flags |= RQF_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 spin_unlock_irq(q->queue_lock);
1146
Tejun Heo29e2b092012-04-19 16:29:21 -07001147 /* allocate and init request */
Tejun Heo5b788ce2012-06-04 20:40:59 -07001148 rq = mempool_alloc(rl->rq_pool, gfp_mask);
Tejun Heo29e2b092012-04-19 16:29:21 -07001149 if (!rq)
Tejun Heob6792812012-03-05 13:15:23 -08001150 goto fail_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Tejun Heo29e2b092012-04-19 16:29:21 -07001152 blk_rq_init(q, rq);
Tejun Heoa0516612012-06-26 15:05:44 -07001153 blk_rq_set_rl(rq, rl);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001154 rq->cmd_flags = op;
Christoph Hellwige8064022016-10-20 15:12:13 +02001155 rq->rq_flags = rq_flags;
Tejun Heo29e2b092012-04-19 16:29:21 -07001156
Tejun Heoaaf7c682012-04-19 16:29:22 -07001157 /* init elvpriv */
Christoph Hellwige8064022016-10-20 15:12:13 +02001158 if (rq_flags & RQF_ELVPRIV) {
Tejun Heoaaf7c682012-04-19 16:29:22 -07001159 if (unlikely(et->icq_cache && !icq)) {
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001160 if (ioc)
1161 icq = ioc_create_icq(ioc, q, gfp_mask);
Tejun Heoaaf7c682012-04-19 16:29:22 -07001162 if (!icq)
1163 goto fail_elvpriv;
Tejun Heo29e2b092012-04-19 16:29:21 -07001164 }
Tejun Heoaaf7c682012-04-19 16:29:22 -07001165
1166 rq->elv.icq = icq;
1167 if (unlikely(elv_set_request(q, rq, bio, gfp_mask)))
1168 goto fail_elvpriv;
1169
1170 /* @rq->elv.icq holds io_context until @rq is freed */
Tejun Heo29e2b092012-04-19 16:29:21 -07001171 if (icq)
1172 get_io_context(icq->ioc);
1173 }
Tejun Heoaaf7c682012-04-19 16:29:22 -07001174out:
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001175 /*
1176 * ioc may be NULL here, and ioc_batching will be false. That's
1177 * OK, if the queue is under the request limit then requests need
1178 * not count toward the nr_batch_requests limit. There will always
1179 * be some limit enforced by BLK_BATCH_TIME.
1180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (ioc_batching(q, ioc))
1182 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +01001183
Mike Christiee6a40b02016-06-05 14:32:11 -05001184 trace_block_getrq(q, bio, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return rq;
Tejun Heob6792812012-03-05 13:15:23 -08001186
Tejun Heoaaf7c682012-04-19 16:29:22 -07001187fail_elvpriv:
1188 /*
1189 * elvpriv init failed. ioc, icq and elvpriv aren't mempool backed
1190 * and may fail indefinitely under memory pressure and thus
1191 * shouldn't stall IO. Treat this request as !elvpriv. This will
1192 * disturb iosched and blkcg but weird is bettern than dead.
1193 */
Robert Elliott7b2b10e2014-08-27 10:50:36 -05001194 printk_ratelimited(KERN_WARNING "%s: dev %s: request aux data allocation failed, iosched may be disturbed\n",
Jan Karadc3b17c2017-02-02 15:56:50 +01001195 __func__, dev_name(q->backing_dev_info->dev));
Tejun Heoaaf7c682012-04-19 16:29:22 -07001196
Christoph Hellwige8064022016-10-20 15:12:13 +02001197 rq->rq_flags &= ~RQF_ELVPRIV;
Tejun Heoaaf7c682012-04-19 16:29:22 -07001198 rq->elv.icq = NULL;
1199
1200 spin_lock_irq(q->queue_lock);
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001201 q->nr_rqs_elvpriv--;
Tejun Heoaaf7c682012-04-19 16:29:22 -07001202 spin_unlock_irq(q->queue_lock);
1203 goto out;
1204
Tejun Heob6792812012-03-05 13:15:23 -08001205fail_alloc:
1206 /*
1207 * Allocation failed presumably due to memory. Undo anything we
1208 * might have messed up.
1209 *
1210 * Allocating task should really be put onto the front of the wait
1211 * queue, but this is pretty rare.
1212 */
1213 spin_lock_irq(q->queue_lock);
Christoph Hellwige8064022016-10-20 15:12:13 +02001214 freed_request(rl, is_sync, rq_flags);
Tejun Heob6792812012-03-05 13:15:23 -08001215
1216 /*
1217 * in the very unlikely event that allocation failed and no
1218 * requests for this direction was pending, mark us starved so that
1219 * freeing of a request in the other direction will notice
1220 * us. another possible fix would be to split the rq mempool into
1221 * READ and WRITE
1222 */
1223rq_starved:
1224 if (unlikely(rl->count[is_sync] == 0))
1225 rl->starved[is_sync] = 1;
Joe Lawrencea492f072014-08-28 08:15:21 -06001226 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Tejun Heoda8303c2011-10-19 14:33:05 +02001229/**
Tejun Heoa06e05e2012-06-04 20:40:55 -07001230 * get_request - get a free request
Tejun Heoda8303c2011-10-19 14:33:05 +02001231 * @q: request_queue to allocate request from
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001232 * @op: operation and flags
Tejun Heoda8303c2011-10-19 14:33:05 +02001233 * @bio: bio to allocate request for (can be %NULL)
Tejun Heoa06e05e2012-06-04 20:40:55 -07001234 * @gfp_mask: allocation mask
Nick Piggind6344532005-06-28 20:45:14 -07001235 *
Mel Gormand0164ad2015-11-06 16:28:21 -08001236 * Get a free request from @q. If %__GFP_DIRECT_RECLAIM is set in @gfp_mask,
1237 * this function keeps retrying under memory pressure and fails iff @q is dead.
Tejun Heoda8303c2011-10-19 14:33:05 +02001238 *
Masanari Iidada3dae52014-09-09 01:27:23 +09001239 * Must be called with @q->queue_lock held and,
Joe Lawrencea492f072014-08-28 08:15:21 -06001240 * Returns ERR_PTR on failure, with @q->queue_lock held.
1241 * Returns request pointer on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001243static struct request *get_request(struct request_queue *q, unsigned int op,
1244 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001246 const bool is_sync = op_is_sync(op);
Tejun Heoa06e05e2012-06-04 20:40:55 -07001247 DEFINE_WAIT(wait);
Tejun Heoa0516612012-06-26 15:05:44 -07001248 struct request_list *rl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct request *rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001250
1251 rl = blk_get_rl(q, bio); /* transferred to @rq on success */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001252retry:
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001253 rq = __get_request(rl, op, bio, gfp_mask);
Joe Lawrencea492f072014-08-28 08:15:21 -06001254 if (!IS_ERR(rq))
Tejun Heoa06e05e2012-06-04 20:40:55 -07001255 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Mel Gormand0164ad2015-11-06 16:28:21 -08001257 if (!gfpflags_allow_blocking(gfp_mask) || unlikely(blk_queue_dying(q))) {
Tejun Heoa0516612012-06-26 15:05:44 -07001258 blk_put_rl(rl);
Joe Lawrencea492f072014-08-28 08:15:21 -06001259 return rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Tejun Heoa06e05e2012-06-04 20:40:55 -07001262 /* wait on @rl and retry */
1263 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
1264 TASK_UNINTERRUPTIBLE);
Tejun Heoda8303c2011-10-19 14:33:05 +02001265
Mike Christiee6a40b02016-06-05 14:32:11 -05001266 trace_block_sleeprq(q, bio, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Tejun Heoa06e05e2012-06-04 20:40:55 -07001268 spin_unlock_irq(q->queue_lock);
1269 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Tejun Heoa06e05e2012-06-04 20:40:55 -07001271 /*
1272 * After sleeping, we become a "batching" process and will be able
1273 * to allocate at least one request, and up to a big batch of them
1274 * for a small period time. See ioc_batching, ioc_set_batching
1275 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001276 ioc_set_batching(q, current->io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Tejun Heoa06e05e2012-06-04 20:40:55 -07001278 spin_lock_irq(q->queue_lock);
1279 finish_wait(&rl->wait[is_sync], &wait);
Jens Axboe2056a782006-03-23 20:00:26 +01001280
Tejun Heoa06e05e2012-06-04 20:40:55 -07001281 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Jens Axboe320ae512013-10-24 09:20:05 +01001284static struct request *blk_old_get_request(struct request_queue *q, int rw,
1285 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 struct request *rq;
1288
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001289 /* create ioc upfront */
1290 create_io_context(gfp_mask, q->node);
1291
Nick Piggind6344532005-06-28 20:45:14 -07001292 spin_lock_irq(q->queue_lock);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001293 rq = get_request(q, rw, NULL, gfp_mask);
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +02001294 if (IS_ERR(rq)) {
Tejun Heoda8303c2011-10-19 14:33:05 +02001295 spin_unlock_irq(q->queue_lock);
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +02001296 return rq;
1297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +02001299 /* q->queue_lock is unlocked at this point */
1300 rq->__data_len = 0;
1301 rq->__sector = (sector_t) -1;
1302 rq->bio = rq->biotail = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return rq;
1304}
Jens Axboe320ae512013-10-24 09:20:05 +01001305
1306struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
1307{
1308 if (q->mq_ops)
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001309 return blk_mq_alloc_request(q, rw,
1310 (gfp_mask & __GFP_DIRECT_RECLAIM) ?
1311 0 : BLK_MQ_REQ_NOWAIT);
Jens Axboe320ae512013-10-24 09:20:05 +01001312 else
1313 return blk_old_get_request(q, rw, gfp_mask);
1314}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315EXPORT_SYMBOL(blk_get_request);
1316
1317/**
1318 * blk_requeue_request - put a request back on queue
1319 * @q: request queue where request should be inserted
1320 * @rq: request to be inserted
1321 *
1322 * Description:
1323 * Drivers often keep queueing requests until the hardware cannot accept
1324 * more, when that condition happens we need to put the request back
1325 * on the queue. Must be called with queue lock held.
1326 */
Jens Axboe165125e2007-07-24 09:28:11 +02001327void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001329 blk_delete_timer(rq);
1330 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001331 trace_block_rq_requeue(q, rq);
Jens Axboe87760e52016-11-09 12:38:14 -07001332 wbt_requeue(q->rq_wb, &rq->issue_stat);
Jens Axboe2056a782006-03-23 20:00:26 +01001333
Christoph Hellwige8064022016-10-20 15:12:13 +02001334 if (rq->rq_flags & RQF_QUEUED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 blk_queue_end_tag(q, rq);
1336
James Bottomleyba396a62009-05-27 14:17:08 +02001337 BUG_ON(blk_queued_rq(rq));
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 elv_requeue_request(q, rq);
1340}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341EXPORT_SYMBOL(blk_requeue_request);
1342
Jens Axboe73c10102011-03-08 13:19:51 +01001343static void add_acct_request(struct request_queue *q, struct request *rq,
1344 int where)
1345{
Jens Axboe320ae512013-10-24 09:20:05 +01001346 blk_account_io_start(rq, true);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001347 __elv_add_request(q, rq, where);
Jens Axboe73c10102011-03-08 13:19:51 +01001348}
1349
Tejun Heo074a7ac2008-08-25 19:56:14 +09001350static void part_round_stats_single(int cpu, struct hd_struct *part,
1351 unsigned long now)
1352{
Jens Axboe7276d022014-05-09 15:48:23 -06001353 int inflight;
1354
Tejun Heo074a7ac2008-08-25 19:56:14 +09001355 if (now == part->stamp)
1356 return;
1357
Jens Axboe7276d022014-05-09 15:48:23 -06001358 inflight = part_in_flight(part);
1359 if (inflight) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001360 __part_stat_add(cpu, part, time_in_queue,
Jens Axboe7276d022014-05-09 15:48:23 -06001361 inflight * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001362 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1363 }
1364 part->stamp = now;
1365}
1366
1367/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001368 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1369 * @cpu: cpu number for stats access
1370 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 *
1372 * The average IO queue length and utilisation statistics are maintained
1373 * by observing the current state of the queue length and the amount of
1374 * time it has been in this state for.
1375 *
1376 * Normally, that accounting is done on IO completion, but that can result
1377 * in more than a second's worth of IO being accounted for within any one
1378 * second, leading to >100% utilisation. To deal with that, we call this
1379 * function to do a round-off before returning the results when reading
1380 * /proc/diskstats. This accounts immediately for all queue usage up to
1381 * the current jiffies and restarts the counters again.
1382 */
Tejun Heoc9959052008-08-25 19:47:21 +09001383void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001384{
1385 unsigned long now = jiffies;
1386
Tejun Heo074a7ac2008-08-25 19:56:14 +09001387 if (part->partno)
1388 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1389 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001390}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001391EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001392
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01001393#ifdef CONFIG_PM
Lin Mingc8158812013-03-23 11:42:27 +08001394static void blk_pm_put_request(struct request *rq)
1395{
Christoph Hellwige8064022016-10-20 15:12:13 +02001396 if (rq->q->dev && !(rq->rq_flags & RQF_PM) && !--rq->q->nr_pending)
Lin Mingc8158812013-03-23 11:42:27 +08001397 pm_runtime_mark_last_busy(rq->q->dev);
1398}
1399#else
1400static inline void blk_pm_put_request(struct request *rq) {}
1401#endif
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403/*
1404 * queue lock must be held
1405 */
Jens Axboe165125e2007-07-24 09:28:11 +02001406void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Christoph Hellwige8064022016-10-20 15:12:13 +02001408 req_flags_t rq_flags = req->rq_flags;
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (unlikely(!q))
1411 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Christoph Hellwig6f5ba582014-02-07 10:22:37 -08001413 if (q->mq_ops) {
1414 blk_mq_free_request(req);
1415 return;
1416 }
1417
Lin Mingc8158812013-03-23 11:42:27 +08001418 blk_pm_put_request(req);
1419
Tejun Heo8922e162005-10-20 16:23:44 +02001420 elv_completed_request(q, req);
1421
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001422 /* this is a bio leak */
1423 WARN_ON(req->bio != NULL);
1424
Jens Axboe87760e52016-11-09 12:38:14 -07001425 wbt_done(q->rq_wb, &req->issue_stat);
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /*
1428 * Request may not have originated from ll_rw_blk. if not,
1429 * it didn't come out of our reserved rq pools
1430 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001431 if (rq_flags & RQF_ALLOCED) {
Tejun Heoa0516612012-06-26 15:05:44 -07001432 struct request_list *rl = blk_rq_rl(req);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001433 bool sync = op_is_sync(req->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe360f92c2014-04-09 20:27:01 -06001436 BUG_ON(ELV_ON_HASH(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Tejun Heoa0516612012-06-26 15:05:44 -07001438 blk_free_request(rl, req);
Christoph Hellwige8064022016-10-20 15:12:13 +02001439 freed_request(rl, sync, rq_flags);
Tejun Heoa0516612012-06-26 15:05:44 -07001440 blk_put_rl(rl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442}
Mike Christie6e39b692005-11-11 05:30:24 -06001443EXPORT_SYMBOL_GPL(__blk_put_request);
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445void blk_put_request(struct request *req)
1446{
Jens Axboe165125e2007-07-24 09:28:11 +02001447 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Jens Axboe320ae512013-10-24 09:20:05 +01001449 if (q->mq_ops)
1450 blk_mq_free_request(req);
1451 else {
1452 unsigned long flags;
1453
1454 spin_lock_irqsave(q->queue_lock, flags);
1455 __blk_put_request(q, req);
1456 spin_unlock_irqrestore(q->queue_lock, flags);
1457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459EXPORT_SYMBOL(blk_put_request);
1460
Jens Axboe320ae512013-10-24 09:20:05 +01001461bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1462 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001463{
Jens Axboe1eff9d32016-08-05 15:35:16 -06001464 const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001465
Jens Axboe73c10102011-03-08 13:19:51 +01001466 if (!ll_back_merge_fn(q, req, bio))
1467 return false;
1468
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001469 trace_block_bio_backmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001470
1471 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1472 blk_rq_set_mixed_merge(req);
1473
1474 req->biotail->bi_next = bio;
1475 req->biotail = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001476 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001477 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1478
Jens Axboe320ae512013-10-24 09:20:05 +01001479 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001480 return true;
1481}
1482
Jens Axboe320ae512013-10-24 09:20:05 +01001483bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
1484 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001485{
Jens Axboe1eff9d32016-08-05 15:35:16 -06001486 const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001487
Jens Axboe73c10102011-03-08 13:19:51 +01001488 if (!ll_front_merge_fn(q, req, bio))
1489 return false;
1490
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001491 trace_block_bio_frontmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001492
1493 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1494 blk_rq_set_mixed_merge(req);
1495
Jens Axboe73c10102011-03-08 13:19:51 +01001496 bio->bi_next = req->bio;
1497 req->bio = bio;
1498
Kent Overstreet4f024f32013-10-11 15:44:27 -07001499 req->__sector = bio->bi_iter.bi_sector;
1500 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001501 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1502
Jens Axboe320ae512013-10-24 09:20:05 +01001503 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001504 return true;
1505}
1506
Christoph Hellwig1e739732017-02-08 14:46:49 +01001507bool bio_attempt_discard_merge(struct request_queue *q, struct request *req,
1508 struct bio *bio)
1509{
1510 unsigned short segments = blk_rq_nr_discard_segments(req);
1511
1512 if (segments >= queue_max_discard_segments(q))
1513 goto no_merge;
1514 if (blk_rq_sectors(req) + bio_sectors(bio) >
1515 blk_rq_get_max_sectors(req, blk_rq_pos(req)))
1516 goto no_merge;
1517
1518 req->biotail->bi_next = bio;
1519 req->biotail = bio;
1520 req->__data_len += bio->bi_iter.bi_size;
1521 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1522 req->nr_phys_segments = segments + 1;
1523
1524 blk_account_io_start(req, false);
1525 return true;
1526no_merge:
1527 req_set_nomerge(q, req);
1528 return false;
1529}
1530
Tejun Heobd87b582011-10-19 14:33:08 +02001531/**
Jens Axboe320ae512013-10-24 09:20:05 +01001532 * blk_attempt_plug_merge - try to merge with %current's plugged list
Tejun Heobd87b582011-10-19 14:33:08 +02001533 * @q: request_queue new bio is being queued at
1534 * @bio: new bio being queued
1535 * @request_count: out parameter for number of traversed plugged requests
Randy Dunlapccc26002015-10-30 18:36:16 -07001536 * @same_queue_rq: pointer to &struct request that gets filled in when
1537 * another request associated with @q is found on the plug list
1538 * (optional, may be %NULL)
Tejun Heobd87b582011-10-19 14:33:08 +02001539 *
1540 * Determine whether @bio being queued on @q can be merged with a request
1541 * on %current's plugged list. Returns %true if merge was successful,
1542 * otherwise %false.
1543 *
Tejun Heo07c2bd32012-02-08 09:19:42 +01001544 * Plugging coalesces IOs from the same issuer for the same purpose without
1545 * going through @q->queue_lock. As such it's more of an issuing mechanism
1546 * than scheduling, and the request, while may have elvpriv data, is not
1547 * added on the elevator at this point. In addition, we don't have
1548 * reliable access to the elevator outside queue lock. Only check basic
1549 * merging parameters without querying the elevator.
Robert Elliottda41a582014-05-20 16:46:26 -05001550 *
1551 * Caller must ensure !blk_queue_nomerges(q) beforehand.
Jens Axboe73c10102011-03-08 13:19:51 +01001552 */
Jens Axboe320ae512013-10-24 09:20:05 +01001553bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
Shaohua Li5b3f3412015-05-08 10:51:33 -07001554 unsigned int *request_count,
1555 struct request **same_queue_rq)
Jens Axboe73c10102011-03-08 13:19:51 +01001556{
1557 struct blk_plug *plug;
1558 struct request *rq;
Shaohua Li92f399c2013-10-29 12:01:03 -06001559 struct list_head *plug_list;
Jens Axboe73c10102011-03-08 13:19:51 +01001560
Tejun Heobd87b582011-10-19 14:33:08 +02001561 plug = current->plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001562 if (!plug)
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001563 return false;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001564 *request_count = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01001565
Shaohua Li92f399c2013-10-29 12:01:03 -06001566 if (q->mq_ops)
1567 plug_list = &plug->mq_list;
1568 else
1569 plug_list = &plug->list;
1570
1571 list_for_each_entry_reverse(rq, plug_list, queuelist) {
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001572 bool merged = false;
Jens Axboe73c10102011-03-08 13:19:51 +01001573
Shaohua Li5b3f3412015-05-08 10:51:33 -07001574 if (rq->q == q) {
Shaohua Li1b2e19f2012-04-06 11:37:47 -06001575 (*request_count)++;
Shaohua Li5b3f3412015-05-08 10:51:33 -07001576 /*
1577 * Only blk-mq multiple hardware queues case checks the
1578 * rq in the same queue, there should be only one such
1579 * rq in a queue
1580 **/
1581 if (same_queue_rq)
1582 *same_queue_rq = rq;
1583 }
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001584
Tejun Heo07c2bd32012-02-08 09:19:42 +01001585 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
Jens Axboe73c10102011-03-08 13:19:51 +01001586 continue;
1587
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001588 switch (blk_try_merge(rq, bio)) {
1589 case ELEVATOR_BACK_MERGE:
1590 merged = bio_attempt_back_merge(q, rq, bio);
1591 break;
1592 case ELEVATOR_FRONT_MERGE:
1593 merged = bio_attempt_front_merge(q, rq, bio);
1594 break;
Christoph Hellwig1e739732017-02-08 14:46:49 +01001595 case ELEVATOR_DISCARD_MERGE:
1596 merged = bio_attempt_discard_merge(q, rq, bio);
1597 break;
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001598 default:
1599 break;
Jens Axboe73c10102011-03-08 13:19:51 +01001600 }
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001601
1602 if (merged)
1603 return true;
Jens Axboe73c10102011-03-08 13:19:51 +01001604 }
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001605
1606 return false;
Jens Axboe73c10102011-03-08 13:19:51 +01001607}
1608
Jeff Moyer0809e3a2015-10-20 23:13:51 +08001609unsigned int blk_plug_queued_count(struct request_queue *q)
1610{
1611 struct blk_plug *plug;
1612 struct request *rq;
1613 struct list_head *plug_list;
1614 unsigned int ret = 0;
1615
1616 plug = current->plug;
1617 if (!plug)
1618 goto out;
1619
1620 if (q->mq_ops)
1621 plug_list = &plug->mq_list;
1622 else
1623 plug_list = &plug->list;
1624
1625 list_for_each_entry(rq, plug_list, queuelist) {
1626 if (rq->q == q)
1627 ret++;
1628 }
1629out:
1630 return ret;
1631}
1632
Bart Van Asscheda8d7f02017-04-19 14:01:24 -07001633void blk_init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001634{
Bart Van Assche0be0dee2017-04-19 14:01:27 -07001635 struct io_context *ioc = rq_ioc(bio);
1636
Jens Axboe1eff9d32016-08-05 15:35:16 -06001637 if (bio->bi_opf & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001638 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001639
Kent Overstreet4f024f32013-10-11 15:44:27 -07001640 req->__sector = bio->bi_iter.bi_sector;
Adam Manzanares5dc8b362016-10-17 11:27:28 -07001641 if (ioprio_valid(bio_prio(bio)))
1642 req->ioprio = bio_prio(bio);
Bart Van Assche0be0dee2017-04-19 14:01:27 -07001643 else if (ioc)
1644 req->ioprio = ioc->ioprio;
1645 else
1646 req->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE, 0);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001647 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001648}
Bart Van Asscheda8d7f02017-04-19 14:01:24 -07001649EXPORT_SYMBOL_GPL(blk_init_request_from_bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001650
Jens Axboedece1632015-11-05 10:41:16 -07001651static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Jens Axboe73c10102011-03-08 13:19:51 +01001653 struct blk_plug *plug;
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001654 int where = ELEVATOR_INSERT_SORT;
Jens Axboee4d750c2017-02-03 09:48:28 -07001655 struct request *req, *free;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001656 unsigned int request_count = 0;
Jens Axboe87760e52016-11-09 12:38:14 -07001657 unsigned int wb_acct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 /*
1660 * low level driver can indicate that it wants pages above a
1661 * certain limit bounced to low memory (ie for highmem, or even
1662 * ISA dma in theory)
1663 */
1664 blk_queue_bounce(q, &bio);
1665
Junichi Nomura23688bf2015-12-22 10:23:44 -07001666 blk_queue_split(q, &bio, q->bio_split);
1667
Darrick J. Wongffecfd12013-02-21 16:42:55 -08001668 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001669 bio->bi_error = -EIO;
1670 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001671 return BLK_QC_T_NONE;
Darrick J. Wongffecfd12013-02-21 16:42:55 -08001672 }
1673
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07001674 if (op_is_flush(bio->bi_opf)) {
Jens Axboe73c10102011-03-08 13:19:51 +01001675 spin_lock_irq(q->queue_lock);
Tejun Heoae1b1532011-01-25 12:43:54 +01001676 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001677 goto get_rq;
1678 }
1679
Jens Axboe73c10102011-03-08 13:19:51 +01001680 /*
1681 * Check if we can merge with the plugged list before grabbing
1682 * any locks.
1683 */
Jeff Moyer0809e3a2015-10-20 23:13:51 +08001684 if (!blk_queue_nomerges(q)) {
1685 if (blk_attempt_plug_merge(q, bio, &request_count, NULL))
Jens Axboedece1632015-11-05 10:41:16 -07001686 return BLK_QC_T_NONE;
Jeff Moyer0809e3a2015-10-20 23:13:51 +08001687 } else
1688 request_count = blk_plug_queued_count(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001689
1690 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Christoph Hellwig34fe7c02017-02-08 14:46:48 +01001692 switch (elv_merge(q, &req, bio)) {
1693 case ELEVATOR_BACK_MERGE:
1694 if (!bio_attempt_back_merge(q, req, bio))
1695 break;
1696 elv_bio_merged(q, req, bio);
1697 free = attempt_back_merge(q, req);
1698 if (free)
1699 __blk_put_request(q, free);
1700 else
1701 elv_merged_request(q, req, ELEVATOR_BACK_MERGE);
1702 goto out_unlock;
1703 case ELEVATOR_FRONT_MERGE:
1704 if (!bio_attempt_front_merge(q, req, bio))
1705 break;
1706 elv_bio_merged(q, req, bio);
1707 free = attempt_front_merge(q, req);
1708 if (free)
1709 __blk_put_request(q, free);
1710 else
1711 elv_merged_request(q, req, ELEVATOR_FRONT_MERGE);
1712 goto out_unlock;
1713 default:
1714 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717get_rq:
Jens Axboe87760e52016-11-09 12:38:14 -07001718 wb_acct = wbt_wait(q->rq_wb, bio, q->queue_lock);
1719
Nick Piggin450991b2005-06-28 20:45:13 -07001720 /*
1721 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001722 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001723 */
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001724 req = get_request(q, bio->bi_opf, bio, GFP_NOIO);
Joe Lawrencea492f072014-08-28 08:15:21 -06001725 if (IS_ERR(req)) {
Jens Axboe87760e52016-11-09 12:38:14 -07001726 __wbt_done(q->rq_wb, wb_acct);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001727 bio->bi_error = PTR_ERR(req);
1728 bio_endio(bio);
Tejun Heoda8303c2011-10-19 14:33:05 +02001729 goto out_unlock;
1730 }
Nick Piggind6344532005-06-28 20:45:14 -07001731
Jens Axboe87760e52016-11-09 12:38:14 -07001732 wbt_track(&req->issue_stat, wb_acct);
1733
Nick Piggin450991b2005-06-28 20:45:13 -07001734 /*
1735 * After dropping the lock and possibly sleeping here, our request
1736 * may now be mergeable after it had proven unmergeable (above).
1737 * We don't worry about that case for efficiency. It won't happen
1738 * often, and the elevators are able to handle it.
1739 */
Bart Van Asscheda8d7f02017-04-19 14:01:24 -07001740 blk_init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Tao Ma9562ad92011-10-24 16:11:30 +02001742 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
Jens Axboe11ccf112011-07-26 15:01:15 +02001743 req->cpu = raw_smp_processor_id();
Tejun Heodd831002010-09-03 11:56:16 +02001744
Jens Axboe73c10102011-03-08 13:19:51 +01001745 plug = current->plug;
Jens Axboe721a9602011-03-09 11:56:30 +01001746 if (plug) {
Jens Axboedc6d36c2011-04-12 10:28:28 +02001747 /*
1748 * If this is the first request added after a plug, fire
Jianpeng Ma7aef2e72013-09-11 13:21:07 -06001749 * of a plug trace.
Ming Lei0a6219a2016-11-16 18:07:05 +08001750 *
1751 * @request_count may become stale because of schedule
1752 * out, so check plug list again.
Jens Axboedc6d36c2011-04-12 10:28:28 +02001753 */
Ming Lei0a6219a2016-11-16 18:07:05 +08001754 if (!request_count || list_empty(&plug->list))
Jens Axboedc6d36c2011-04-12 10:28:28 +02001755 trace_block_plug(q);
Shaohua Li3540d5e2011-11-16 09:21:50 +01001756 else {
Shaohua Li50d24c32016-11-03 17:03:53 -07001757 struct request *last = list_entry_rq(plug->list.prev);
1758 if (request_count >= BLK_MAX_REQUEST_COUNT ||
1759 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE) {
Shaohua Li3540d5e2011-11-16 09:21:50 +01001760 blk_flush_plug_list(plug, false);
Shaohua Li019ceb72011-11-16 09:21:50 +01001761 trace_block_plug(q);
1762 }
Jens Axboe73c10102011-03-08 13:19:51 +01001763 }
Shaohua Lia6327162011-08-24 16:04:32 +02001764 list_add_tail(&req->queuelist, &plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01001765 blk_account_io_start(req, true);
Jens Axboe73c10102011-03-08 13:19:51 +01001766 } else {
1767 spin_lock_irq(q->queue_lock);
1768 add_acct_request(q, req, where);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02001769 __blk_run_queue(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001770out_unlock:
1771 spin_unlock_irq(q->queue_lock);
1772 }
Jens Axboedece1632015-11-05 10:41:16 -07001773
1774 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
1777/*
1778 * If bio->bi_dev is a partition, remap the location
1779 */
1780static inline void blk_partition_remap(struct bio *bio)
1781{
1782 struct block_device *bdev = bio->bi_bdev;
1783
Shaun Tancheff778889d2016-11-21 15:52:23 -06001784 /*
1785 * Zone reset does not include bi_size so bio_sectors() is always 0.
1786 * Include a test for the reset op code and perform the remap if needed.
1787 */
1788 if (bdev != bdev->bd_contains &&
1789 (bio_sectors(bio) || bio_op(bio) == REQ_OP_ZONE_RESET)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001791
Kent Overstreet4f024f32013-10-11 15:44:27 -07001792 bio->bi_iter.bi_sector += p->start_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001794
Mike Snitzerd07335e2010-11-16 12:52:38 +01001795 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1796 bdev->bd_dev,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001797 bio->bi_iter.bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799}
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801static void handle_bad_sector(struct bio *bio)
1802{
1803 char b[BDEVNAME_SIZE];
1804
1805 printk(KERN_INFO "attempt to access beyond end of device\n");
Mike Christie6296b962016-06-05 14:32:21 -05001806 printk(KERN_INFO "%s: rw=%d, want=%Lu, limit=%Lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 bdevname(bio->bi_bdev, b),
Jens Axboe1eff9d32016-08-05 15:35:16 -06001808 bio->bi_opf,
Kent Overstreetf73a1c72012-09-25 15:05:12 -07001809 (unsigned long long)bio_end_sector(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001810 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Akinobu Mitac17bb492006-12-08 02:39:46 -08001813#ifdef CONFIG_FAIL_MAKE_REQUEST
1814
1815static DECLARE_FAULT_ATTR(fail_make_request);
1816
1817static int __init setup_fail_make_request(char *str)
1818{
1819 return setup_fault_attr(&fail_make_request, str);
1820}
1821__setup("fail_make_request=", setup_fail_make_request);
1822
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001823static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001824{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001825 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001826}
1827
1828static int __init fail_make_request_debugfs(void)
1829{
Akinobu Mitadd48c082011-08-03 16:21:01 -07001830 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1831 NULL, &fail_make_request);
1832
Duan Jiong21f9fcd2014-04-11 15:58:56 +08001833 return PTR_ERR_OR_ZERO(dir);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001834}
1835
1836late_initcall(fail_make_request_debugfs);
1837
1838#else /* CONFIG_FAIL_MAKE_REQUEST */
1839
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001840static inline bool should_fail_request(struct hd_struct *part,
1841 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001842{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001843 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001844}
1845
1846#endif /* CONFIG_FAIL_MAKE_REQUEST */
1847
Jens Axboec07e2b42007-07-18 13:27:58 +02001848/*
1849 * Check whether this bio extends beyond the end of the device.
1850 */
1851static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1852{
1853 sector_t maxsector;
1854
1855 if (!nr_sectors)
1856 return 0;
1857
1858 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001859 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001860 if (maxsector) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07001861 sector_t sector = bio->bi_iter.bi_sector;
Jens Axboec07e2b42007-07-18 13:27:58 +02001862
1863 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1864 /*
1865 * This may well happen - the kernel calls bread()
1866 * without checking the size of the device, e.g., when
1867 * mounting a device.
1868 */
1869 handle_bad_sector(bio);
1870 return 1;
1871 }
1872 }
1873
1874 return 0;
1875}
1876
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001877static noinline_for_stack bool
1878generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Jens Axboe165125e2007-07-24 09:28:11 +02001880 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001881 int nr_sectors = bio_sectors(bio);
Jens Axboe51fd77b2007-11-02 08:49:08 +01001882 int err = -EIO;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001883 char b[BDEVNAME_SIZE];
1884 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Jens Axboec07e2b42007-07-18 13:27:58 +02001888 if (bio_check_eod(bio, nr_sectors))
1889 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001891 q = bdev_get_queue(bio->bi_bdev);
1892 if (unlikely(!q)) {
1893 printk(KERN_ERR
1894 "generic_make_request: Trying to access "
1895 "nonexistent block-device %s (%Lu)\n",
1896 bdevname(bio->bi_bdev, b),
Kent Overstreet4f024f32013-10-11 15:44:27 -07001897 (long long) bio->bi_iter.bi_sector);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001898 goto end_io;
1899 }
1900
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001901 part = bio->bi_bdev->bd_part;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001902 if (should_fail_request(part, bio->bi_iter.bi_size) ||
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001903 should_fail_request(&part_to_disk(part)->part0,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001904 bio->bi_iter.bi_size))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001905 goto end_io;
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /*
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001908 * If this device has partitions, remap block n
1909 * of partition p to block n+start(p) of the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001911 blk_partition_remap(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001913 if (bio_check_eod(bio, nr_sectors))
1914 goto end_io;
1915
1916 /*
1917 * Filter flush bio's early so that make_request based
1918 * drivers without flush support don't have to worry
1919 * about them.
1920 */
Jens Axboef3a8ab72017-01-27 09:08:23 -07001921 if (op_is_flush(bio->bi_opf) &&
Jens Axboec888a8f2016-04-13 13:33:19 -06001922 !test_bit(QUEUE_FLAG_WC, &q->queue_flags)) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001923 bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001924 if (!nr_sectors) {
1925 err = 0;
Tejun Heoa7384672008-11-28 13:32:03 +09001926 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Christoph Hellwig288dab82016-06-09 16:00:36 +02001930 switch (bio_op(bio)) {
1931 case REQ_OP_DISCARD:
1932 if (!blk_queue_discard(q))
1933 goto not_supported;
1934 break;
1935 case REQ_OP_SECURE_ERASE:
1936 if (!blk_queue_secure_erase(q))
1937 goto not_supported;
1938 break;
1939 case REQ_OP_WRITE_SAME:
1940 if (!bdev_write_same(bio->bi_bdev))
1941 goto not_supported;
Nicolai Stange58886782016-12-04 14:56:39 +01001942 break;
Shaun Tancheff2d253442016-10-18 15:40:32 +09001943 case REQ_OP_ZONE_REPORT:
1944 case REQ_OP_ZONE_RESET:
1945 if (!bdev_is_zoned(bio->bi_bdev))
1946 goto not_supported;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001947 break;
Chaitanya Kulkarnia6f07882016-11-30 12:28:59 -08001948 case REQ_OP_WRITE_ZEROES:
1949 if (!bdev_write_zeroes_sectors(bio->bi_bdev))
1950 goto not_supported;
1951 break;
Christoph Hellwig288dab82016-06-09 16:00:36 +02001952 default:
1953 break;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001956 /*
1957 * Various block parts want %current->io_context and lazy ioc
1958 * allocation ends up trading a lot of pain for a small amount of
1959 * memory. Just allocate it upfront. This may fail and block
1960 * layer knows how to live with it.
1961 */
1962 create_io_context(GFP_ATOMIC, q->node);
1963
Tejun Heoae118892015-08-18 14:55:20 -07001964 if (!blkcg_bio_issue_check(q, bio))
1965 return false;
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001966
NeilBrownfbbaf702017-04-07 09:40:52 -06001967 if (!bio_flagged(bio, BIO_TRACE_COMPLETION)) {
1968 trace_block_bio_queue(q, bio);
1969 /* Now that enqueuing has been traced, we need to trace
1970 * completion as well.
1971 */
1972 bio_set_flag(bio, BIO_TRACE_COMPLETION);
1973 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001974 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001975
Christoph Hellwig288dab82016-06-09 16:00:36 +02001976not_supported:
1977 err = -EOPNOTSUPP;
Tejun Heoa7384672008-11-28 13:32:03 +09001978end_io:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001979 bio->bi_error = err;
1980 bio_endio(bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001981 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001984/**
1985 * generic_make_request - hand a buffer to its device driver for I/O
1986 * @bio: The bio describing the location in memory and on the device.
1987 *
1988 * generic_make_request() is used to make I/O requests of block
1989 * devices. It is passed a &struct bio, which describes the I/O that needs
1990 * to be done.
1991 *
1992 * generic_make_request() does not return any status. The
1993 * success/failure status of the request, along with notification of
1994 * completion, is delivered asynchronously through the bio->bi_end_io
1995 * function described (one day) else where.
1996 *
1997 * The caller of generic_make_request must make sure that bi_io_vec
1998 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1999 * set to describe the device address, and the
2000 * bi_end_io and optionally bi_private are set to describe how
2001 * completion notification should be signaled.
2002 *
2003 * generic_make_request and the drivers it calls may use bi_next if this
2004 * bio happens to be merged with someone else, and may resubmit the bio to
2005 * a lower device by calling into generic_make_request recursively, which
2006 * means the bio should NOT be touched after the call to ->make_request_fn.
Neil Brownd89d8792007-05-01 09:53:42 +02002007 */
Jens Axboedece1632015-11-05 10:41:16 -07002008blk_qc_t generic_make_request(struct bio *bio)
Neil Brownd89d8792007-05-01 09:53:42 +02002009{
NeilBrownf5fe1b52017-03-10 17:00:47 +11002010 /*
2011 * bio_list_on_stack[0] contains bios submitted by the current
2012 * make_request_fn.
2013 * bio_list_on_stack[1] contains bios that were submitted before
2014 * the current make_request_fn, but that haven't been processed
2015 * yet.
2016 */
2017 struct bio_list bio_list_on_stack[2];
Jens Axboedece1632015-11-05 10:41:16 -07002018 blk_qc_t ret = BLK_QC_T_NONE;
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002019
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002020 if (!generic_make_request_checks(bio))
Jens Axboedece1632015-11-05 10:41:16 -07002021 goto out;
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002022
2023 /*
2024 * We only want one ->make_request_fn to be active at a time, else
2025 * stack usage with stacked devices could be a problem. So use
2026 * current->bio_list to keep a list of requests submited by a
2027 * make_request_fn function. current->bio_list is also used as a
2028 * flag to say if generic_make_request is currently active in this
2029 * task or not. If it is NULL, then no make_request is active. If
2030 * it is non-NULL, then a make_request is active, and new requests
2031 * should be added at the tail
2032 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002033 if (current->bio_list) {
NeilBrownf5fe1b52017-03-10 17:00:47 +11002034 bio_list_add(&current->bio_list[0], bio);
Jens Axboedece1632015-11-05 10:41:16 -07002035 goto out;
Neil Brownd89d8792007-05-01 09:53:42 +02002036 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002037
Neil Brownd89d8792007-05-01 09:53:42 +02002038 /* following loop may be a bit non-obvious, and so deserves some
2039 * explanation.
2040 * Before entering the loop, bio->bi_next is NULL (as all callers
2041 * ensure that) so we have a list with a single bio.
2042 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002043 * we assign bio_list to a pointer to the bio_list_on_stack,
2044 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002045 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02002046 * through a recursive call to generic_make_request. If it
2047 * did, we find a non-NULL value in bio_list and re-enter the loop
2048 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002049 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002050 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02002051 */
2052 BUG_ON(bio->bi_next);
NeilBrownf5fe1b52017-03-10 17:00:47 +11002053 bio_list_init(&bio_list_on_stack[0]);
2054 current->bio_list = bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02002055 do {
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002056 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
2057
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01002058 if (likely(blk_queue_enter(q, false) == 0)) {
NeilBrown79bd9952017-03-08 07:38:05 +11002059 struct bio_list lower, same;
2060
2061 /* Create a fresh bio_list for all subordinate requests */
NeilBrownf5fe1b52017-03-10 17:00:47 +11002062 bio_list_on_stack[1] = bio_list_on_stack[0];
2063 bio_list_init(&bio_list_on_stack[0]);
Jens Axboedece1632015-11-05 10:41:16 -07002064 ret = q->make_request_fn(q, bio);
Dan Williams3ef28e82015-10-21 13:20:12 -04002065
2066 blk_queue_exit(q);
2067
NeilBrown79bd9952017-03-08 07:38:05 +11002068 /* sort new bios into those for a lower level
2069 * and those for the same level
2070 */
2071 bio_list_init(&lower);
2072 bio_list_init(&same);
NeilBrownf5fe1b52017-03-10 17:00:47 +11002073 while ((bio = bio_list_pop(&bio_list_on_stack[0])) != NULL)
NeilBrown79bd9952017-03-08 07:38:05 +11002074 if (q == bdev_get_queue(bio->bi_bdev))
2075 bio_list_add(&same, bio);
2076 else
2077 bio_list_add(&lower, bio);
2078 /* now assemble so we handle the lowest level first */
NeilBrownf5fe1b52017-03-10 17:00:47 +11002079 bio_list_merge(&bio_list_on_stack[0], &lower);
2080 bio_list_merge(&bio_list_on_stack[0], &same);
2081 bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
Dan Williams3ef28e82015-10-21 13:20:12 -04002082 } else {
Dan Williams3ef28e82015-10-21 13:20:12 -04002083 bio_io_error(bio);
Dan Williams3ef28e82015-10-21 13:20:12 -04002084 }
NeilBrownf5fe1b52017-03-10 17:00:47 +11002085 bio = bio_list_pop(&bio_list_on_stack[0]);
Neil Brownd89d8792007-05-01 09:53:42 +02002086 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002087 current->bio_list = NULL; /* deactivate */
Jens Axboedece1632015-11-05 10:41:16 -07002088
2089out:
2090 return ret;
Neil Brownd89d8792007-05-01 09:53:42 +02002091}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092EXPORT_SYMBOL(generic_make_request);
2093
2094/**
Randy Dunlap710027a2008-08-19 20:13:11 +02002095 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 * @bio: The &struct bio which describes the I/O
2097 *
2098 * submit_bio() is very similar in purpose to generic_make_request(), and
2099 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02002100 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 *
2102 */
Mike Christie4e49ea42016-06-05 14:31:41 -05002103blk_qc_t submit_bio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Jens Axboebf2de6f2007-09-27 13:01:25 +02002105 /*
2106 * If it's a regular read/write or a barrier with data attached,
2107 * go through the normal accounting stuff before submission.
2108 */
Martin K. Petersene2a60da2012-09-18 12:19:25 -04002109 if (bio_has_data(bio)) {
Martin K. Petersen4363ac72012-09-18 12:19:27 -04002110 unsigned int count;
2111
Mike Christie95fe6c12016-06-05 14:31:48 -05002112 if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
Martin K. Petersen4363ac72012-09-18 12:19:27 -04002113 count = bdev_logical_block_size(bio->bi_bdev) >> 9;
2114 else
2115 count = bio_sectors(bio);
2116
Mike Christiea8ebb052016-06-05 14:31:45 -05002117 if (op_is_write(bio_op(bio))) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02002118 count_vm_events(PGPGOUT, count);
2119 } else {
Kent Overstreet4f024f32013-10-11 15:44:27 -07002120 task_io_account_read(bio->bi_iter.bi_size);
Jens Axboebf2de6f2007-09-27 13:01:25 +02002121 count_vm_events(PGPGIN, count);
2122 }
2123
2124 if (unlikely(block_dump)) {
2125 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02002126 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002127 current->comm, task_pid_nr(current),
Mike Christiea8ebb052016-06-05 14:31:45 -05002128 op_is_write(bio_op(bio)) ? "WRITE" : "READ",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002129 (unsigned long long)bio->bi_iter.bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02002130 bdevname(bio->bi_bdev, b),
2131 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02002132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134
Jens Axboedece1632015-11-05 10:41:16 -07002135 return generic_make_request(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137EXPORT_SYMBOL(submit_bio);
2138
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002139/**
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01002140 * blk_cloned_rq_check_limits - Helper function to check a cloned request
2141 * for new the queue limits
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002142 * @q: the queue
2143 * @rq: the request being checked
2144 *
2145 * Description:
2146 * @rq may have been made based on weaker limitations of upper-level queues
2147 * in request stacking drivers, and it may violate the limitation of @q.
2148 * Since the block layer and the underlying device driver trust @rq
2149 * after it is inserted to @q, it should be checked against @q before
2150 * the insertion using this generic function.
2151 *
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002152 * Request stacking drivers like request-based dm may change the queue
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01002153 * limits when retrying requests on other queues. Those requests need
2154 * to be checked against the new queue limits again during dispatch.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002155 */
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01002156static int blk_cloned_rq_check_limits(struct request_queue *q,
2157 struct request *rq)
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002158{
Mike Christie8fe0d472016-06-05 14:32:15 -05002159 if (blk_rq_sectors(rq) > blk_queue_get_max_sectors(q, req_op(rq))) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002160 printk(KERN_ERR "%s: over max size limit.\n", __func__);
2161 return -EIO;
2162 }
2163
2164 /*
2165 * queue's settings related to segment counting like q->bounce_pfn
2166 * may differ from that of other stacking queues.
2167 * Recalculate it to check the request correctly on this queue's
2168 * limitation.
2169 */
2170 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05002171 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002172 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
2173 return -EIO;
2174 }
2175
2176 return 0;
2177}
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002178
2179/**
2180 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
2181 * @q: the queue to submit the request
2182 * @rq: the request being queued
2183 */
2184int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
2185{
2186 unsigned long flags;
Jeff Moyer4853aba2011-08-15 21:37:25 +02002187 int where = ELEVATOR_INSERT_BACK;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002188
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01002189 if (blk_cloned_rq_check_limits(q, rq))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002190 return -EIO;
2191
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07002192 if (rq->rq_disk &&
2193 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002194 return -EIO;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002195
Keith Busch7fb48982014-10-17 17:46:38 -06002196 if (q->mq_ops) {
2197 if (blk_queue_io_stat(q))
2198 blk_account_io_start(rq, true);
Jens Axboebd6737f2017-01-27 01:00:47 -07002199 blk_mq_sched_insert_request(rq, false, true, false, false);
Keith Busch7fb48982014-10-17 17:46:38 -06002200 return 0;
2201 }
2202
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002203 spin_lock_irqsave(q->queue_lock, flags);
Bart Van Assche3f3299d2012-11-28 13:42:38 +01002204 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01002205 spin_unlock_irqrestore(q->queue_lock, flags);
2206 return -ENODEV;
2207 }
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002208
2209 /*
2210 * Submitting request must be dequeued before calling this function
2211 * because it will be linked to another request_queue
2212 */
2213 BUG_ON(blk_queued_rq(rq));
2214
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07002215 if (op_is_flush(rq->cmd_flags))
Jeff Moyer4853aba2011-08-15 21:37:25 +02002216 where = ELEVATOR_INSERT_FLUSH;
2217
2218 add_acct_request(q, rq, where);
Jeff Moyere67b77c2011-10-17 12:57:23 +02002219 if (where == ELEVATOR_INSERT_FLUSH)
2220 __blk_run_queue(q);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002221 spin_unlock_irqrestore(q->queue_lock, flags);
2222
2223 return 0;
2224}
2225EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
2226
Tejun Heo80a761f2009-07-03 17:48:17 +09002227/**
2228 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
2229 * @rq: request to examine
2230 *
2231 * Description:
2232 * A request could be merge of IOs which require different failure
2233 * handling. This function determines the number of bytes which
2234 * can be failed from the beginning of the request without
2235 * crossing into area which need to be retried further.
2236 *
2237 * Return:
2238 * The number of bytes to fail.
2239 *
2240 * Context:
2241 * queue_lock must be held.
2242 */
2243unsigned int blk_rq_err_bytes(const struct request *rq)
2244{
2245 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
2246 unsigned int bytes = 0;
2247 struct bio *bio;
2248
Christoph Hellwige8064022016-10-20 15:12:13 +02002249 if (!(rq->rq_flags & RQF_MIXED_MERGE))
Tejun Heo80a761f2009-07-03 17:48:17 +09002250 return blk_rq_bytes(rq);
2251
2252 /*
2253 * Currently the only 'mixing' which can happen is between
2254 * different fastfail types. We can safely fail portions
2255 * which have all the failfast bits that the first one has -
2256 * the ones which are at least as eager to fail as the first
2257 * one.
2258 */
2259 for (bio = rq->bio; bio; bio = bio->bi_next) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06002260 if ((bio->bi_opf & ff) != ff)
Tejun Heo80a761f2009-07-03 17:48:17 +09002261 break;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002262 bytes += bio->bi_iter.bi_size;
Tejun Heo80a761f2009-07-03 17:48:17 +09002263 }
2264
2265 /* this could lead to infinite loop */
2266 BUG_ON(blk_rq_bytes(rq) && !bytes);
2267 return bytes;
2268}
2269EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
2270
Jens Axboe320ae512013-10-24 09:20:05 +01002271void blk_account_io_completion(struct request *req, unsigned int bytes)
Jens Axboebc58ba92009-01-23 10:54:44 +01002272{
Jens Axboec2553b52009-04-24 08:10:11 +02002273 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002274 const int rw = rq_data_dir(req);
2275 struct hd_struct *part;
2276 int cpu;
2277
2278 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002279 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002280 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
2281 part_stat_unlock();
2282 }
2283}
2284
Jens Axboe320ae512013-10-24 09:20:05 +01002285void blk_account_io_done(struct request *req)
Jens Axboebc58ba92009-01-23 10:54:44 +01002286{
Jens Axboebc58ba92009-01-23 10:54:44 +01002287 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02002288 * Account IO completion. flush_rq isn't accounted as a
2289 * normal IO on queueing nor completion. Accounting the
2290 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01002291 */
Christoph Hellwige8064022016-10-20 15:12:13 +02002292 if (blk_do_io_stat(req) && !(req->rq_flags & RQF_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002293 unsigned long duration = jiffies - req->start_time;
2294 const int rw = rq_data_dir(req);
2295 struct hd_struct *part;
2296 int cpu;
2297
2298 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002299 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002300
2301 part_stat_inc(cpu, part, ios[rw]);
2302 part_stat_add(cpu, part, ticks[rw], duration);
2303 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002304 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01002305
Jens Axboe6c23a962011-01-07 08:43:37 +01002306 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01002307 part_stat_unlock();
2308 }
2309}
2310
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01002311#ifdef CONFIG_PM
Lin Mingc8158812013-03-23 11:42:27 +08002312/*
2313 * Don't process normal requests when queue is suspended
2314 * or in the process of suspending/resuming
2315 */
2316static struct request *blk_pm_peek_request(struct request_queue *q,
2317 struct request *rq)
2318{
2319 if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
Christoph Hellwige8064022016-10-20 15:12:13 +02002320 (q->rpm_status != RPM_ACTIVE && !(rq->rq_flags & RQF_PM))))
Lin Mingc8158812013-03-23 11:42:27 +08002321 return NULL;
2322 else
2323 return rq;
2324}
2325#else
2326static inline struct request *blk_pm_peek_request(struct request_queue *q,
2327 struct request *rq)
2328{
2329 return rq;
2330}
2331#endif
2332
Jens Axboe320ae512013-10-24 09:20:05 +01002333void blk_account_io_start(struct request *rq, bool new_io)
2334{
2335 struct hd_struct *part;
2336 int rw = rq_data_dir(rq);
2337 int cpu;
2338
2339 if (!blk_do_io_stat(rq))
2340 return;
2341
2342 cpu = part_stat_lock();
2343
2344 if (!new_io) {
2345 part = rq->part;
2346 part_stat_inc(cpu, part, merges[rw]);
2347 } else {
2348 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
2349 if (!hd_struct_try_get(part)) {
2350 /*
2351 * The partition is already being removed,
2352 * the request will be accounted on the disk only
2353 *
2354 * We take a reference on disk->part0 although that
2355 * partition will never be deleted, so we can treat
2356 * it as any other partition.
2357 */
2358 part = &rq->rq_disk->part0;
2359 hd_struct_get(part);
2360 }
2361 part_round_stats(cpu, part);
2362 part_inc_in_flight(part, rw);
2363 rq->part = part;
2364 }
2365
2366 part_stat_unlock();
2367}
2368
Tejun Heo53a08802008-12-03 12:41:26 +01002369/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002370 * blk_peek_request - peek at the top of a request queue
2371 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002372 *
2373 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002374 * Return the request at the top of @q. The returned request
2375 * should be started using blk_start_request() before LLD starts
2376 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002377 *
2378 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002379 * Pointer to the request at the top of @q if available. Null
2380 * otherwise.
2381 *
2382 * Context:
2383 * queue_lock must be held.
2384 */
2385struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09002386{
2387 struct request *rq;
2388 int ret;
2389
2390 while ((rq = __elv_next_request(q)) != NULL) {
Lin Mingc8158812013-03-23 11:42:27 +08002391
2392 rq = blk_pm_peek_request(q, rq);
2393 if (!rq)
2394 break;
2395
Christoph Hellwige8064022016-10-20 15:12:13 +02002396 if (!(rq->rq_flags & RQF_STARTED)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002397 /*
2398 * This is the first time the device driver
2399 * sees this request (possibly after
2400 * requeueing). Notify IO scheduler.
2401 */
Christoph Hellwige8064022016-10-20 15:12:13 +02002402 if (rq->rq_flags & RQF_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09002403 elv_activate_rq(q, rq);
2404
2405 /*
2406 * just mark as started even if we don't start
2407 * it, a request that has been delayed should
2408 * not be passed by new incoming requests
2409 */
Christoph Hellwige8064022016-10-20 15:12:13 +02002410 rq->rq_flags |= RQF_STARTED;
Tejun Heo158dbda2009-04-23 11:05:18 +09002411 trace_block_rq_issue(q, rq);
2412 }
2413
2414 if (!q->boundary_rq || q->boundary_rq == rq) {
2415 q->end_sector = rq_end_sector(rq);
2416 q->boundary_rq = NULL;
2417 }
2418
Christoph Hellwige8064022016-10-20 15:12:13 +02002419 if (rq->rq_flags & RQF_DONTPREP)
Tejun Heo158dbda2009-04-23 11:05:18 +09002420 break;
2421
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002422 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002423 /*
2424 * make sure space for the drain appears we
2425 * know we can do this because max_hw_segments
2426 * has been adjusted to be one fewer than the
2427 * device can handle
2428 */
2429 rq->nr_phys_segments++;
2430 }
2431
2432 if (!q->prep_rq_fn)
2433 break;
2434
2435 ret = q->prep_rq_fn(q, rq);
2436 if (ret == BLKPREP_OK) {
2437 break;
2438 } else if (ret == BLKPREP_DEFER) {
2439 /*
2440 * the request may have been (partially) prepped.
2441 * we need to keep this request in the front to
Christoph Hellwige8064022016-10-20 15:12:13 +02002442 * avoid resource deadlock. RQF_STARTED will
Tejun Heo158dbda2009-04-23 11:05:18 +09002443 * prevent other fs requests from passing this one.
2444 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002445 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Christoph Hellwige8064022016-10-20 15:12:13 +02002446 !(rq->rq_flags & RQF_DONTPREP)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002447 /*
2448 * remove the space for the drain we added
2449 * so that we don't add it again
2450 */
2451 --rq->nr_phys_segments;
2452 }
2453
2454 rq = NULL;
2455 break;
Martin K. Petersen0fb5b1f2016-02-04 00:52:12 -05002456 } else if (ret == BLKPREP_KILL || ret == BLKPREP_INVALID) {
2457 int err = (ret == BLKPREP_INVALID) ? -EREMOTEIO : -EIO;
2458
Christoph Hellwige8064022016-10-20 15:12:13 +02002459 rq->rq_flags |= RQF_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02002460 /*
2461 * Mark this request as started so we don't trigger
2462 * any debug logic in the end I/O path.
2463 */
2464 blk_start_request(rq);
Martin K. Petersen0fb5b1f2016-02-04 00:52:12 -05002465 __blk_end_request_all(rq, err);
Tejun Heo158dbda2009-04-23 11:05:18 +09002466 } else {
2467 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
2468 break;
2469 }
2470 }
2471
2472 return rq;
2473}
Tejun Heo9934c8c2009-05-08 11:54:16 +09002474EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09002475
Tejun Heo9934c8c2009-05-08 11:54:16 +09002476void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09002477{
Tejun Heo9934c8c2009-05-08 11:54:16 +09002478 struct request_queue *q = rq->q;
2479
Tejun Heo158dbda2009-04-23 11:05:18 +09002480 BUG_ON(list_empty(&rq->queuelist));
2481 BUG_ON(ELV_ON_HASH(rq));
2482
2483 list_del_init(&rq->queuelist);
2484
2485 /*
2486 * the time frame between a request being removed from the lists
2487 * and to it is freed is accounted as io that is in progress at
2488 * the driver side.
2489 */
Divyesh Shah91952912010-04-01 15:01:41 -07002490 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02002491 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07002492 set_io_start_time_ns(rq);
2493 }
Tejun Heo158dbda2009-04-23 11:05:18 +09002494}
2495
Tejun Heo5efccd12009-04-23 11:05:18 +09002496/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002497 * blk_start_request - start request processing on the driver
2498 * @req: request to dequeue
2499 *
2500 * Description:
2501 * Dequeue @req and start timeout timer on it. This hands off the
2502 * request to the driver.
2503 *
2504 * Block internal functions which don't want to start timer should
2505 * call blk_dequeue_request().
2506 *
2507 * Context:
2508 * queue_lock must be held.
2509 */
2510void blk_start_request(struct request *req)
2511{
2512 blk_dequeue_request(req);
2513
Jens Axboecf43e6b2016-11-07 21:32:37 -07002514 if (test_bit(QUEUE_FLAG_STATS, &req->q->queue_flags)) {
Shaohua Li88eeca42017-03-27 15:19:41 -07002515 blk_stat_set_issue(&req->issue_stat, blk_rq_sectors(req));
Jens Axboecf43e6b2016-11-07 21:32:37 -07002516 req->rq_flags |= RQF_STATS;
Jens Axboe87760e52016-11-09 12:38:14 -07002517 wbt_issue(req->q->rq_wb, &req->issue_stat);
Jens Axboecf43e6b2016-11-07 21:32:37 -07002518 }
2519
Jeff Moyer4912aa62013-10-08 14:36:41 -04002520 BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags));
Tejun Heo9934c8c2009-05-08 11:54:16 +09002521 blk_add_timer(req);
2522}
2523EXPORT_SYMBOL(blk_start_request);
2524
2525/**
2526 * blk_fetch_request - fetch a request from a request queue
2527 * @q: request queue to fetch a request from
2528 *
2529 * Description:
2530 * Return the request at the top of @q. The request is started on
2531 * return and LLD can start processing it immediately.
2532 *
2533 * Return:
2534 * Pointer to the request at the top of @q if available. Null
2535 * otherwise.
2536 *
2537 * Context:
2538 * queue_lock must be held.
2539 */
2540struct request *blk_fetch_request(struct request_queue *q)
2541{
2542 struct request *rq;
2543
2544 rq = blk_peek_request(q);
2545 if (rq)
2546 blk_start_request(rq);
2547 return rq;
2548}
2549EXPORT_SYMBOL(blk_fetch_request);
2550
2551/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002552 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002553 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002554 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002555 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002556 *
2557 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002558 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2559 * the request structure even if @req doesn't have leftover.
2560 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002561 *
2562 * This special helper function is only for request stacking drivers
2563 * (e.g. request-based dm) so that they can handle partial completion.
2564 * Actual device drivers should use blk_end_request instead.
2565 *
2566 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2567 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002568 *
2569 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002570 * %false - this request doesn't have any more data
2571 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002572 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002573bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574{
Kent Overstreetf79ea412012-09-20 16:38:30 -07002575 int total_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Christoph Hellwigcaf7df12017-04-20 16:03:16 +02002577 trace_block_rq_complete(req, error, nr_bytes);
Hannes Reinecke4a0efdc2014-10-01 14:32:31 +02002578
Tejun Heo2e60e022009-04-23 11:05:18 +09002579 if (!req->bio)
2580 return false;
2581
Christoph Hellwig57292b52017-01-31 16:57:29 +01002582 if (error && !blk_rq_is_passthrough(req) &&
Christoph Hellwige8064022016-10-20 15:12:13 +02002583 !(req->rq_flags & RQF_QUIET)) {
Hannes Reinecke79775562011-01-18 10:13:13 +01002584 char *error_type;
2585
2586 switch (error) {
2587 case -ENOLINK:
2588 error_type = "recoverable transport";
2589 break;
2590 case -EREMOTEIO:
2591 error_type = "critical target";
2592 break;
2593 case -EBADE:
2594 error_type = "critical nexus";
2595 break;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002596 case -ETIMEDOUT:
2597 error_type = "timeout";
2598 break;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +02002599 case -ENOSPC:
2600 error_type = "critical space allocation";
2601 break;
Hannes Reinecke7e782af2013-07-01 15:16:26 +02002602 case -ENODATA:
2603 error_type = "critical medium";
2604 break;
Hannes Reinecke79775562011-01-18 10:13:13 +01002605 case -EIO:
2606 default:
2607 error_type = "I/O";
2608 break;
2609 }
Robert Elliottef3ecb62014-08-27 10:50:31 -05002610 printk_ratelimited(KERN_ERR "%s: %s error, dev %s, sector %llu\n",
2611 __func__, error_type, req->rq_disk ?
Yi Zou37d7b342012-08-30 16:26:25 -07002612 req->rq_disk->disk_name : "?",
2613 (unsigned long long)blk_rq_pos(req));
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
2616
Jens Axboebc58ba92009-01-23 10:54:44 +01002617 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002618
Kent Overstreetf79ea412012-09-20 16:38:30 -07002619 total_bytes = 0;
2620 while (req->bio) {
2621 struct bio *bio = req->bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002622 unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Kent Overstreet4f024f32013-10-11 15:44:27 -07002624 if (bio_bytes == bio->bi_iter.bi_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 req->bio = bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
NeilBrownfbbaf702017-04-07 09:40:52 -06002627 /* Completion has already been traced */
2628 bio_clear_flag(bio, BIO_TRACE_COMPLETION);
Kent Overstreetf79ea412012-09-20 16:38:30 -07002629 req_bio_endio(req, bio, bio_bytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Kent Overstreetf79ea412012-09-20 16:38:30 -07002631 total_bytes += bio_bytes;
2632 nr_bytes -= bio_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Kent Overstreetf79ea412012-09-20 16:38:30 -07002634 if (!nr_bytes)
2635 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
2637
2638 /*
2639 * completely done
2640 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002641 if (!req->bio) {
2642 /*
2643 * Reset counters so that the request stacking driver
2644 * can find how many bytes remain in the request
2645 * later.
2646 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002647 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002648 return false;
2649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Christoph Hellwigf9d03f92016-12-08 15:20:32 -07002651 WARN_ON_ONCE(req->rq_flags & RQF_SPECIAL_PAYLOAD);
2652
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002653 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002654
2655 /* update sector only for requests with clear definition of sector */
Christoph Hellwig57292b52017-01-31 16:57:29 +01002656 if (!blk_rq_is_passthrough(req))
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002657 req->__sector += total_bytes >> 9;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002658
Tejun Heo80a761f2009-07-03 17:48:17 +09002659 /* mixed attributes always follow the first bio */
Christoph Hellwige8064022016-10-20 15:12:13 +02002660 if (req->rq_flags & RQF_MIXED_MERGE) {
Tejun Heo80a761f2009-07-03 17:48:17 +09002661 req->cmd_flags &= ~REQ_FAILFAST_MASK;
Jens Axboe1eff9d32016-08-05 15:35:16 -06002662 req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK;
Tejun Heo80a761f2009-07-03 17:48:17 +09002663 }
2664
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002665 /*
2666 * If total number of sectors is less than the first segment
2667 * size, something has gone terribly wrong.
2668 */
2669 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
Jens Axboe81829242011-03-30 09:51:33 +02002670 blk_dump_rq_flags(req, "request botched");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002671 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002672 }
2673
2674 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002676
Tejun Heo2e60e022009-04-23 11:05:18 +09002677 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678}
Tejun Heo2e60e022009-04-23 11:05:18 +09002679EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Tejun Heo2e60e022009-04-23 11:05:18 +09002681static bool blk_update_bidi_request(struct request *rq, int error,
2682 unsigned int nr_bytes,
2683 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002684{
Tejun Heo2e60e022009-04-23 11:05:18 +09002685 if (blk_update_request(rq, error, nr_bytes))
2686 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002687
Tejun Heo2e60e022009-04-23 11:05:18 +09002688 /* Bidi request must be completed as a whole */
2689 if (unlikely(blk_bidi_rq(rq)) &&
2690 blk_update_request(rq->next_rq, error, bidi_bytes))
2691 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002692
Jens Axboee2e1a142010-06-09 10:42:09 +02002693 if (blk_queue_add_random(rq->q))
2694 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002695
2696 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
James Bottomley28018c22010-07-01 19:49:17 +09002699/**
2700 * blk_unprep_request - unprepare a request
2701 * @req: the request
2702 *
2703 * This function makes a request ready for complete resubmission (or
2704 * completion). It happens only after all error handling is complete,
2705 * so represents the appropriate moment to deallocate any resources
2706 * that were allocated to the request in the prep_rq_fn. The queue
2707 * lock is held when calling this.
2708 */
2709void blk_unprep_request(struct request *req)
2710{
2711 struct request_queue *q = req->q;
2712
Christoph Hellwige8064022016-10-20 15:12:13 +02002713 req->rq_flags &= ~RQF_DONTPREP;
James Bottomley28018c22010-07-01 19:49:17 +09002714 if (q->unprep_rq_fn)
2715 q->unprep_rq_fn(q, req);
2716}
2717EXPORT_SYMBOL_GPL(blk_unprep_request);
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719/*
2720 * queue lock must be held
2721 */
Christoph Hellwig12120072014-04-16 09:44:59 +02002722void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
Jens Axboecf43e6b2016-11-07 21:32:37 -07002724 struct request_queue *q = req->q;
2725
2726 if (req->rq_flags & RQF_STATS)
Omar Sandoval34dbad52017-03-21 08:56:08 -07002727 blk_stat_add(req);
Jens Axboecf43e6b2016-11-07 21:32:37 -07002728
Christoph Hellwige8064022016-10-20 15:12:13 +02002729 if (req->rq_flags & RQF_QUEUED)
Jens Axboecf43e6b2016-11-07 21:32:37 -07002730 blk_queue_end_tag(q, req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002731
James Bottomleyba396a62009-05-27 14:17:08 +02002732 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Christoph Hellwig57292b52017-01-31 16:57:29 +01002734 if (unlikely(laptop_mode) && !blk_rq_is_passthrough(req))
Jan Karadc3b17c2017-02-02 15:56:50 +01002735 laptop_io_completion(req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Mike Andersone78042e2008-10-30 02:16:20 -07002737 blk_delete_timer(req);
2738
Christoph Hellwige8064022016-10-20 15:12:13 +02002739 if (req->rq_flags & RQF_DONTPREP)
James Bottomley28018c22010-07-01 19:49:17 +09002740 blk_unprep_request(req);
2741
Jens Axboebc58ba92009-01-23 10:54:44 +01002742 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002743
Jens Axboe87760e52016-11-09 12:38:14 -07002744 if (req->end_io) {
2745 wbt_done(req->q->rq_wb, &req->issue_stat);
Tejun Heo8ffdc652006-01-06 09:49:03 +01002746 req->end_io(req, error);
Jens Axboe87760e52016-11-09 12:38:14 -07002747 } else {
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002748 if (blk_bidi_rq(req))
2749 __blk_put_request(req->next_rq->q, req->next_rq);
2750
Jens Axboecf43e6b2016-11-07 21:32:37 -07002751 __blk_put_request(q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753}
Christoph Hellwig12120072014-04-16 09:44:59 +02002754EXPORT_SYMBOL(blk_finish_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002756/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002757 * blk_end_bidi_request - Complete a bidi request
2758 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002759 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002760 * @nr_bytes: number of bytes to complete @rq
2761 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002762 *
2763 * Description:
2764 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002765 * Drivers that supports bidi can safely call this member for any
2766 * type of request, bidi or uni. In the later case @bidi_bytes is
2767 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002768 *
2769 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002770 * %false - we are done with this request
2771 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002772 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002773static bool blk_end_bidi_request(struct request *rq, int error,
2774 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002775{
2776 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002777 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002778
Tejun Heo2e60e022009-04-23 11:05:18 +09002779 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2780 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002781
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002782 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002783 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002784 spin_unlock_irqrestore(q->queue_lock, flags);
2785
Tejun Heo2e60e022009-04-23 11:05:18 +09002786 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002787}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002788
2789/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002790 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2791 * @rq: the request to complete
2792 * @error: %0 for success, < %0 for error
2793 * @nr_bytes: number of bytes to complete @rq
2794 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002795 *
2796 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002797 * Identical to blk_end_bidi_request() except that queue lock is
2798 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002799 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002800 * Return:
2801 * %false - we are done with this request
2802 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002803 **/
Christoph Hellwigd0fac022017-04-12 12:13:59 +02002804static bool __blk_end_bidi_request(struct request *rq, int error,
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002805 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002806{
Tejun Heo2e60e022009-04-23 11:05:18 +09002807 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2808 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002809
Tejun Heo2e60e022009-04-23 11:05:18 +09002810 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002811
Tejun Heo2e60e022009-04-23 11:05:18 +09002812 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002813}
2814
2815/**
2816 * blk_end_request - Helper function for drivers to complete the request.
2817 * @rq: the request being processed
2818 * @error: %0 for success, < %0 for error
2819 * @nr_bytes: number of bytes to complete
2820 *
2821 * Description:
2822 * Ends I/O on a number of bytes attached to @rq.
2823 * If @rq has leftover, sets it up for the next range of segments.
2824 *
2825 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002826 * %false - we are done with this request
2827 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002828 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002829bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002830{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002831 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002832}
Jens Axboe56ad1742009-07-28 22:11:24 +02002833EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002834
2835/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002836 * blk_end_request_all - Helper function for drives to finish the request.
2837 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002838 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002839 *
2840 * Description:
2841 * Completely finish @rq.
2842 */
2843void blk_end_request_all(struct request *rq, int error)
2844{
2845 bool pending;
2846 unsigned int bidi_bytes = 0;
2847
2848 if (unlikely(blk_bidi_rq(rq)))
2849 bidi_bytes = blk_rq_bytes(rq->next_rq);
2850
2851 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2852 BUG_ON(pending);
2853}
Jens Axboe56ad1742009-07-28 22:11:24 +02002854EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002855
2856/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002857 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002858 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002859 * @error: %0 for success, < %0 for error
2860 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002861 *
2862 * Description:
2863 * Must be called with queue lock held unlike blk_end_request().
2864 *
2865 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002866 * %false - we are done with this request
2867 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002868 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002869bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002870{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002871 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002872}
Jens Axboe56ad1742009-07-28 22:11:24 +02002873EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002874
2875/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002876 * __blk_end_request_all - Helper function for drives to finish the request.
2877 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002878 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002879 *
2880 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002881 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002882 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002883void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002884{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002885 bool pending;
2886 unsigned int bidi_bytes = 0;
2887
2888 if (unlikely(blk_bidi_rq(rq)))
2889 bidi_bytes = blk_rq_bytes(rq->next_rq);
2890
2891 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2892 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002893}
Jens Axboe56ad1742009-07-28 22:11:24 +02002894EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002895
2896/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002897 * __blk_end_request_cur - Helper function to finish the current request chunk.
2898 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002899 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002900 *
2901 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002902 * Complete the current consecutively mapped chunk from @rq. Must
2903 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002904 *
2905 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002906 * %false - we are done with this request
2907 * %true - still buffers pending for this request
2908 */
2909bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002910{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002911 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002912}
Jens Axboe56ad1742009-07-28 22:11:24 +02002913EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002914
Jens Axboe86db1e22008-01-29 14:53:40 +01002915void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2916 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
Jens Axboeb4f42e22014-04-10 09:46:28 -06002918 if (bio_has_data(bio))
David Woodhousefb2dce82008-08-05 18:01:53 +01002919 rq->nr_phys_segments = bio_phys_segments(q, bio);
Jens Axboeb4f42e22014-04-10 09:46:28 -06002920
Kent Overstreet4f024f32013-10-11 15:44:27 -07002921 rq->__data_len = bio->bi_iter.bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
NeilBrown66846572007-08-16 13:31:28 +02002924 if (bio->bi_bdev)
2925 rq->rq_disk = bio->bi_bdev->bd_disk;
2926}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002928#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2929/**
2930 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2931 * @rq: the request to be flushed
2932 *
2933 * Description:
2934 * Flush all pages in @rq.
2935 */
2936void rq_flush_dcache_pages(struct request *rq)
2937{
2938 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -08002939 struct bio_vec bvec;
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002940
2941 rq_for_each_segment(bvec, rq, iter)
Kent Overstreet79886132013-11-23 17:19:00 -08002942 flush_dcache_page(bvec.bv_page);
Ilya Loginov2d4dc892009-11-26 09:16:19 +01002943}
2944EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2945#endif
2946
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02002947/**
2948 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2949 * @q : the queue of the device being checked
2950 *
2951 * Description:
2952 * Check if underlying low-level drivers of a device are busy.
2953 * If the drivers want to export their busy state, they must set own
2954 * exporting function using blk_queue_lld_busy() first.
2955 *
2956 * Basically, this function is used only by request stacking drivers
2957 * to stop dispatching requests to underlying devices when underlying
2958 * devices are busy. This behavior helps more I/O merging on the queue
2959 * of the request stacking driver and prevents I/O throughput regression
2960 * on burst I/O load.
2961 *
2962 * Return:
2963 * 0 - Not busy (The request stacking driver should dispatch request)
2964 * 1 - Busy (The request stacking driver should stop dispatching request)
2965 */
2966int blk_lld_busy(struct request_queue *q)
2967{
2968 if (q->lld_busy_fn)
2969 return q->lld_busy_fn(q);
2970
2971 return 0;
2972}
2973EXPORT_SYMBOL_GPL(blk_lld_busy);
2974
Mike Snitzer78d8e582015-06-26 10:01:13 -04002975/**
2976 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2977 * @rq: the clone request to be cleaned up
2978 *
2979 * Description:
2980 * Free all bios in @rq for a cloned request.
2981 */
2982void blk_rq_unprep_clone(struct request *rq)
2983{
2984 struct bio *bio;
2985
2986 while ((bio = rq->bio) != NULL) {
2987 rq->bio = bio->bi_next;
2988
2989 bio_put(bio);
2990 }
2991}
2992EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2993
2994/*
2995 * Copy attributes of the original request to the clone request.
2996 * The actual data parts (e.g. ->cmd, ->sense) are not copied.
2997 */
2998static void __blk_rq_prep_clone(struct request *dst, struct request *src)
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02002999{
3000 dst->cpu = src->cpu;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003001 dst->__sector = blk_rq_pos(src);
3002 dst->__data_len = blk_rq_bytes(src);
3003 dst->nr_phys_segments = src->nr_phys_segments;
3004 dst->ioprio = src->ioprio;
3005 dst->extra_len = src->extra_len;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003006}
3007
3008/**
3009 * blk_rq_prep_clone - Helper function to setup clone request
3010 * @rq: the request to be setup
3011 * @rq_src: original request to be cloned
3012 * @bs: bio_set that bios for clone are allocated from
3013 * @gfp_mask: memory allocation mask for bio
3014 * @bio_ctr: setup function to be called for each clone bio.
3015 * Returns %0 for success, non %0 for failure.
3016 * @data: private data to be passed to @bio_ctr
3017 *
3018 * Description:
3019 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
3020 * The actual data parts of @rq_src (e.g. ->cmd, ->sense)
3021 * are not copied, and copying such parts is the caller's responsibility.
3022 * Also, pages which the original bios are pointing to are not copied
3023 * and the cloned bios just point same pages.
3024 * So cloned bios must be completed before original bios, which means
3025 * the caller must complete @rq before @rq_src.
3026 */
3027int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
3028 struct bio_set *bs, gfp_t gfp_mask,
3029 int (*bio_ctr)(struct bio *, struct bio *, void *),
3030 void *data)
3031{
3032 struct bio *bio, *bio_src;
3033
3034 if (!bs)
3035 bs = fs_bio_set;
3036
3037 __rq_for_each_bio(bio_src, rq_src) {
3038 bio = bio_clone_fast(bio_src, gfp_mask, bs);
3039 if (!bio)
3040 goto free_and_out;
3041
3042 if (bio_ctr && bio_ctr(bio, bio_src, data))
3043 goto free_and_out;
3044
3045 if (rq->bio) {
3046 rq->biotail->bi_next = bio;
3047 rq->biotail = bio;
3048 } else
3049 rq->bio = rq->biotail = bio;
3050 }
3051
3052 __blk_rq_prep_clone(rq, rq_src);
3053
3054 return 0;
3055
3056free_and_out:
3057 if (bio)
3058 bio_put(bio);
3059 blk_rq_unprep_clone(rq);
3060
3061 return -ENOMEM;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003062}
3063EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
3064
Jens Axboe59c3d452014-04-08 09:15:35 -06003065int kblockd_schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
3067 return queue_work(kblockd_workqueue, work);
3068}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069EXPORT_SYMBOL(kblockd_schedule_work);
3070
Jens Axboeee63cfa2016-08-24 15:52:48 -06003071int kblockd_schedule_work_on(int cpu, struct work_struct *work)
3072{
3073 return queue_work_on(cpu, kblockd_workqueue, work);
3074}
3075EXPORT_SYMBOL(kblockd_schedule_work_on);
3076
Jens Axboe818cd1c2017-04-10 09:54:55 -06003077int kblockd_mod_delayed_work_on(int cpu, struct delayed_work *dwork,
3078 unsigned long delay)
3079{
3080 return mod_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
3081}
3082EXPORT_SYMBOL(kblockd_mod_delayed_work_on);
3083
Jens Axboe59c3d452014-04-08 09:15:35 -06003084int kblockd_schedule_delayed_work(struct delayed_work *dwork,
3085 unsigned long delay)
Vivek Goyale43473b2010-09-15 17:06:35 -04003086{
3087 return queue_delayed_work(kblockd_workqueue, dwork, delay);
3088}
3089EXPORT_SYMBOL(kblockd_schedule_delayed_work);
3090
Jens Axboe8ab14592014-04-08 09:17:40 -06003091int kblockd_schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3092 unsigned long delay)
3093{
3094 return queue_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
3095}
3096EXPORT_SYMBOL(kblockd_schedule_delayed_work_on);
3097
Suresh Jayaraman75df7132011-09-21 10:00:16 +02003098/**
3099 * blk_start_plug - initialize blk_plug and track it inside the task_struct
3100 * @plug: The &struct blk_plug that needs to be initialized
3101 *
3102 * Description:
3103 * Tracking blk_plug inside the task_struct will help with auto-flushing the
3104 * pending I/O should the task end up blocking between blk_start_plug() and
3105 * blk_finish_plug(). This is important from a performance perspective, but
3106 * also ensures that we don't deadlock. For instance, if the task is blocking
3107 * for a memory allocation, memory reclaim could end up wanting to free a
3108 * page belonging to that request that is currently residing in our private
3109 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
3110 * this kind of deadlock.
3111 */
Jens Axboe73c10102011-03-08 13:19:51 +01003112void blk_start_plug(struct blk_plug *plug)
3113{
3114 struct task_struct *tsk = current;
3115
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003116 /*
3117 * If this is a nested plug, don't actually assign it.
3118 */
3119 if (tsk->plug)
3120 return;
3121
Jens Axboe73c10102011-03-08 13:19:51 +01003122 INIT_LIST_HEAD(&plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01003123 INIT_LIST_HEAD(&plug->mq_list);
NeilBrown048c9372011-04-18 09:52:22 +02003124 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe73c10102011-03-08 13:19:51 +01003125 /*
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003126 * Store ordering should not be needed here, since a potential
3127 * preempt will imply a full memory barrier
Jens Axboe73c10102011-03-08 13:19:51 +01003128 */
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003129 tsk->plug = plug;
Jens Axboe73c10102011-03-08 13:19:51 +01003130}
3131EXPORT_SYMBOL(blk_start_plug);
3132
3133static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
3134{
3135 struct request *rqa = container_of(a, struct request, queuelist);
3136 struct request *rqb = container_of(b, struct request, queuelist);
3137
Jianpeng Ma975927b2012-10-25 21:58:17 +02003138 return !(rqa->q < rqb->q ||
3139 (rqa->q == rqb->q && blk_rq_pos(rqa) < blk_rq_pos(rqb)));
Jens Axboe73c10102011-03-08 13:19:51 +01003140}
3141
Jens Axboe49cac012011-04-16 13:51:05 +02003142/*
3143 * If 'from_schedule' is true, then postpone the dispatch of requests
3144 * until a safe kblockd context. We due this to avoid accidental big
3145 * additional stack usage in driver dispatch, in places where the originally
3146 * plugger did not intend it.
3147 */
Jens Axboef6603782011-04-15 15:49:07 +02003148static void queue_unplugged(struct request_queue *q, unsigned int depth,
Jens Axboe49cac012011-04-16 13:51:05 +02003149 bool from_schedule)
Jens Axboe99e22592011-04-18 09:59:55 +02003150 __releases(q->queue_lock)
Jens Axboe94b5eb22011-04-12 10:12:19 +02003151{
Jens Axboe49cac012011-04-16 13:51:05 +02003152 trace_block_unplug(q, depth, !from_schedule);
Jens Axboe99e22592011-04-18 09:59:55 +02003153
Bart Van Assche70460572012-11-28 13:45:56 +01003154 if (from_schedule)
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003155 blk_run_queue_async(q);
Bart Van Assche70460572012-11-28 13:45:56 +01003156 else
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003157 __blk_run_queue(q);
Bart Van Assche70460572012-11-28 13:45:56 +01003158 spin_unlock(q->queue_lock);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003159}
3160
NeilBrown74018dc2012-07-31 09:08:15 +02003161static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
NeilBrown048c9372011-04-18 09:52:22 +02003162{
3163 LIST_HEAD(callbacks);
3164
Shaohua Li2a7d5552012-07-31 09:08:15 +02003165 while (!list_empty(&plug->cb_list)) {
3166 list_splice_init(&plug->cb_list, &callbacks);
NeilBrown048c9372011-04-18 09:52:22 +02003167
Shaohua Li2a7d5552012-07-31 09:08:15 +02003168 while (!list_empty(&callbacks)) {
3169 struct blk_plug_cb *cb = list_first_entry(&callbacks,
NeilBrown048c9372011-04-18 09:52:22 +02003170 struct blk_plug_cb,
3171 list);
Shaohua Li2a7d5552012-07-31 09:08:15 +02003172 list_del(&cb->list);
NeilBrown74018dc2012-07-31 09:08:15 +02003173 cb->callback(cb, from_schedule);
Shaohua Li2a7d5552012-07-31 09:08:15 +02003174 }
NeilBrown048c9372011-04-18 09:52:22 +02003175 }
3176}
3177
NeilBrown9cbb1752012-07-31 09:08:14 +02003178struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
3179 int size)
3180{
3181 struct blk_plug *plug = current->plug;
3182 struct blk_plug_cb *cb;
3183
3184 if (!plug)
3185 return NULL;
3186
3187 list_for_each_entry(cb, &plug->cb_list, list)
3188 if (cb->callback == unplug && cb->data == data)
3189 return cb;
3190
3191 /* Not currently on the callback list */
3192 BUG_ON(size < sizeof(*cb));
3193 cb = kzalloc(size, GFP_ATOMIC);
3194 if (cb) {
3195 cb->data = data;
3196 cb->callback = unplug;
3197 list_add(&cb->list, &plug->cb_list);
3198 }
3199 return cb;
3200}
3201EXPORT_SYMBOL(blk_check_plugged);
3202
Jens Axboe49cac012011-04-16 13:51:05 +02003203void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01003204{
3205 struct request_queue *q;
3206 unsigned long flags;
3207 struct request *rq;
NeilBrown109b8122011-04-11 14:13:10 +02003208 LIST_HEAD(list);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003209 unsigned int depth;
Jens Axboe73c10102011-03-08 13:19:51 +01003210
NeilBrown74018dc2012-07-31 09:08:15 +02003211 flush_plug_callbacks(plug, from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01003212
3213 if (!list_empty(&plug->mq_list))
3214 blk_mq_flush_plug_list(plug, from_schedule);
3215
Jens Axboe73c10102011-03-08 13:19:51 +01003216 if (list_empty(&plug->list))
3217 return;
3218
NeilBrown109b8122011-04-11 14:13:10 +02003219 list_splice_init(&plug->list, &list);
3220
Jianpeng Ma422765c2013-01-11 14:46:09 +01003221 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe73c10102011-03-08 13:19:51 +01003222
3223 q = NULL;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003224 depth = 0;
Jens Axboe18811272011-04-12 10:11:24 +02003225
3226 /*
3227 * Save and disable interrupts here, to avoid doing it for every
3228 * queue lock we have to take.
3229 */
Jens Axboe73c10102011-03-08 13:19:51 +01003230 local_irq_save(flags);
NeilBrown109b8122011-04-11 14:13:10 +02003231 while (!list_empty(&list)) {
3232 rq = list_entry_rq(list.next);
Jens Axboe73c10102011-03-08 13:19:51 +01003233 list_del_init(&rq->queuelist);
Jens Axboe73c10102011-03-08 13:19:51 +01003234 BUG_ON(!rq->q);
3235 if (rq->q != q) {
Jens Axboe99e22592011-04-18 09:59:55 +02003236 /*
3237 * This drops the queue lock
3238 */
3239 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003240 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003241 q = rq->q;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003242 depth = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01003243 spin_lock(q->queue_lock);
3244 }
Tejun Heo8ba61432011-12-14 00:33:37 +01003245
3246 /*
3247 * Short-circuit if @q is dead
3248 */
Bart Van Assche3f3299d2012-11-28 13:42:38 +01003249 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01003250 __blk_end_request_all(rq, -ENODEV);
3251 continue;
3252 }
3253
Jens Axboe73c10102011-03-08 13:19:51 +01003254 /*
3255 * rq is already accounted, so use raw insert
3256 */
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07003257 if (op_is_flush(rq->cmd_flags))
Jens Axboe401a18e2011-03-25 16:57:52 +01003258 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
3259 else
3260 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003261
3262 depth++;
Jens Axboe73c10102011-03-08 13:19:51 +01003263 }
3264
Jens Axboe99e22592011-04-18 09:59:55 +02003265 /*
3266 * This drops the queue lock
3267 */
3268 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003269 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003270
Jens Axboe73c10102011-03-08 13:19:51 +01003271 local_irq_restore(flags);
3272}
Jens Axboe73c10102011-03-08 13:19:51 +01003273
3274void blk_finish_plug(struct blk_plug *plug)
3275{
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003276 if (plug != current->plug)
3277 return;
Jens Axboef6603782011-04-15 15:49:07 +02003278 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02003279
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003280 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01003281}
3282EXPORT_SYMBOL(blk_finish_plug);
3283
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01003284#ifdef CONFIG_PM
Lin Ming6c954662013-03-23 11:42:26 +08003285/**
3286 * blk_pm_runtime_init - Block layer runtime PM initialization routine
3287 * @q: the queue of the device
3288 * @dev: the device the queue belongs to
3289 *
3290 * Description:
3291 * Initialize runtime-PM-related fields for @q and start auto suspend for
3292 * @dev. Drivers that want to take advantage of request-based runtime PM
3293 * should call this function after @dev has been initialized, and its
3294 * request queue @q has been allocated, and runtime PM for it can not happen
3295 * yet(either due to disabled/forbidden or its usage_count > 0). In most
3296 * cases, driver should call this function before any I/O has taken place.
3297 *
3298 * This function takes care of setting up using auto suspend for the device,
3299 * the autosuspend delay is set to -1 to make runtime suspend impossible
3300 * until an updated value is either set by user or by driver. Drivers do
3301 * not need to touch other autosuspend settings.
3302 *
3303 * The block layer runtime PM is request based, so only works for drivers
3304 * that use request as their IO unit instead of those directly use bio's.
3305 */
3306void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
3307{
3308 q->dev = dev;
3309 q->rpm_status = RPM_ACTIVE;
3310 pm_runtime_set_autosuspend_delay(q->dev, -1);
3311 pm_runtime_use_autosuspend(q->dev);
3312}
3313EXPORT_SYMBOL(blk_pm_runtime_init);
3314
3315/**
3316 * blk_pre_runtime_suspend - Pre runtime suspend check
3317 * @q: the queue of the device
3318 *
3319 * Description:
3320 * This function will check if runtime suspend is allowed for the device
3321 * by examining if there are any requests pending in the queue. If there
3322 * are requests pending, the device can not be runtime suspended; otherwise,
3323 * the queue's status will be updated to SUSPENDING and the driver can
3324 * proceed to suspend the device.
3325 *
3326 * For the not allowed case, we mark last busy for the device so that
3327 * runtime PM core will try to autosuspend it some time later.
3328 *
3329 * This function should be called near the start of the device's
3330 * runtime_suspend callback.
3331 *
3332 * Return:
3333 * 0 - OK to runtime suspend the device
3334 * -EBUSY - Device should not be runtime suspended
3335 */
3336int blk_pre_runtime_suspend(struct request_queue *q)
3337{
3338 int ret = 0;
3339
Ken Xue4fd41a852015-12-01 14:45:46 +08003340 if (!q->dev)
3341 return ret;
3342
Lin Ming6c954662013-03-23 11:42:26 +08003343 spin_lock_irq(q->queue_lock);
3344 if (q->nr_pending) {
3345 ret = -EBUSY;
3346 pm_runtime_mark_last_busy(q->dev);
3347 } else {
3348 q->rpm_status = RPM_SUSPENDING;
3349 }
3350 spin_unlock_irq(q->queue_lock);
3351 return ret;
3352}
3353EXPORT_SYMBOL(blk_pre_runtime_suspend);
3354
3355/**
3356 * blk_post_runtime_suspend - Post runtime suspend processing
3357 * @q: the queue of the device
3358 * @err: return value of the device's runtime_suspend function
3359 *
3360 * Description:
3361 * Update the queue's runtime status according to the return value of the
3362 * device's runtime suspend function and mark last busy for the device so
3363 * that PM core will try to auto suspend the device at a later time.
3364 *
3365 * This function should be called near the end of the device's
3366 * runtime_suspend callback.
3367 */
3368void blk_post_runtime_suspend(struct request_queue *q, int err)
3369{
Ken Xue4fd41a852015-12-01 14:45:46 +08003370 if (!q->dev)
3371 return;
3372
Lin Ming6c954662013-03-23 11:42:26 +08003373 spin_lock_irq(q->queue_lock);
3374 if (!err) {
3375 q->rpm_status = RPM_SUSPENDED;
3376 } else {
3377 q->rpm_status = RPM_ACTIVE;
3378 pm_runtime_mark_last_busy(q->dev);
3379 }
3380 spin_unlock_irq(q->queue_lock);
3381}
3382EXPORT_SYMBOL(blk_post_runtime_suspend);
3383
3384/**
3385 * blk_pre_runtime_resume - Pre runtime resume processing
3386 * @q: the queue of the device
3387 *
3388 * Description:
3389 * Update the queue's runtime status to RESUMING in preparation for the
3390 * runtime resume of the device.
3391 *
3392 * This function should be called near the start of the device's
3393 * runtime_resume callback.
3394 */
3395void blk_pre_runtime_resume(struct request_queue *q)
3396{
Ken Xue4fd41a852015-12-01 14:45:46 +08003397 if (!q->dev)
3398 return;
3399
Lin Ming6c954662013-03-23 11:42:26 +08003400 spin_lock_irq(q->queue_lock);
3401 q->rpm_status = RPM_RESUMING;
3402 spin_unlock_irq(q->queue_lock);
3403}
3404EXPORT_SYMBOL(blk_pre_runtime_resume);
3405
3406/**
3407 * blk_post_runtime_resume - Post runtime resume processing
3408 * @q: the queue of the device
3409 * @err: return value of the device's runtime_resume function
3410 *
3411 * Description:
3412 * Update the queue's runtime status according to the return value of the
3413 * device's runtime_resume function. If it is successfully resumed, process
3414 * the requests that are queued into the device's queue when it is resuming
3415 * and then mark last busy and initiate autosuspend for it.
3416 *
3417 * This function should be called near the end of the device's
3418 * runtime_resume callback.
3419 */
3420void blk_post_runtime_resume(struct request_queue *q, int err)
3421{
Ken Xue4fd41a852015-12-01 14:45:46 +08003422 if (!q->dev)
3423 return;
3424
Lin Ming6c954662013-03-23 11:42:26 +08003425 spin_lock_irq(q->queue_lock);
3426 if (!err) {
3427 q->rpm_status = RPM_ACTIVE;
3428 __blk_run_queue(q);
3429 pm_runtime_mark_last_busy(q->dev);
Aaron Luc60855c2013-05-17 15:47:20 +08003430 pm_request_autosuspend(q->dev);
Lin Ming6c954662013-03-23 11:42:26 +08003431 } else {
3432 q->rpm_status = RPM_SUSPENDED;
3433 }
3434 spin_unlock_irq(q->queue_lock);
3435}
3436EXPORT_SYMBOL(blk_post_runtime_resume);
Mika Westerbergd07ab6d2016-02-18 10:54:11 +02003437
3438/**
3439 * blk_set_runtime_active - Force runtime status of the queue to be active
3440 * @q: the queue of the device
3441 *
3442 * If the device is left runtime suspended during system suspend the resume
3443 * hook typically resumes the device and corrects runtime status
3444 * accordingly. However, that does not affect the queue runtime PM status
3445 * which is still "suspended". This prevents processing requests from the
3446 * queue.
3447 *
3448 * This function can be used in driver's resume hook to correct queue
3449 * runtime PM status and re-enable peeking requests from the queue. It
3450 * should be called before first request is added to the queue.
3451 */
3452void blk_set_runtime_active(struct request_queue *q)
3453{
3454 spin_lock_irq(q->queue_lock);
3455 q->rpm_status = RPM_ACTIVE;
3456 pm_runtime_mark_last_busy(q->dev);
3457 pm_request_autosuspend(q->dev);
3458 spin_unlock_irq(q->queue_lock);
3459}
3460EXPORT_SYMBOL(blk_set_runtime_active);
Lin Ming6c954662013-03-23 11:42:26 +08003461#endif
3462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463int __init blk_dev_init(void)
3464{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06003465 BUILD_BUG_ON(REQ_OP_LAST >= (1 << REQ_OP_BITS));
3466 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
Maninder Singh0762b232015-07-07 12:41:07 +05303467 FIELD_SIZEOF(struct request, cmd_flags));
Christoph Hellwigef295ec2016-10-28 08:48:16 -06003468 BUILD_BUG_ON(REQ_OP_BITS + REQ_FLAG_BITS > 8 *
3469 FIELD_SIZEOF(struct bio, bi_opf));
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02003470
Tejun Heo89b90be2011-01-03 15:01:47 +01003471 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
3472 kblockd_workqueue = alloc_workqueue("kblockd",
Matias Bjørling28747fc2014-06-11 23:43:54 +02003473 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 if (!kblockd_workqueue)
3475 panic("Failed to create kblockd\n");
3476
3477 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09003478 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Ilya Dryomovc2789bd2015-11-20 22:16:46 +01003480 blk_requestq_cachep = kmem_cache_create("request_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02003481 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Omar Sandoval18fbda92017-01-31 14:53:20 -08003483#ifdef CONFIG_DEBUG_FS
3484 blk_debugfs_root = debugfs_create_dir("block", NULL);
3485#endif
3486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 return 0;
3488}