blob: f05e9bbf2ddc0f1f64847272a7d1a5d0fa77fe30 [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>
Johannes Weiner0b0803a2019-08-08 15:03:00 -040036#include <linux/psi.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 Axboe8324aa92008-01-29 14:51:59 +010043
Mohan Srinivasane2d88782016-12-14 15:55:36 -080044#include <linux/math64.h>
45
Mike Snitzerd07335e2010-11-16 12:52:38 +010046EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
Jun'ichi Nomurab0da3f02009-10-01 21:16:13 +020047EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -070048EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
Keith Busch3291fa52014-04-28 12:30:52 -060049EXPORT_TRACEPOINT_SYMBOL_GPL(block_split);
NeilBrowncbae8d42012-12-14 20:49:27 +010050EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug);
Ingo Molnar0bfc2452008-11-26 11:59:56 +010051
Tejun Heoa73f7302011-12-14 00:33:37 +010052DEFINE_IDA(blk_queue_ida);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
55 * For the allocated request tables
56 */
Wei Tangd674d412015-11-24 09:58:45 +080057struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/*
60 * For queue allocation
61 */
Jens Axboe6728cb02008-01-31 13:03:55 +010062struct kmem_cache *blk_requestq_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * Controlling structure to kblockd
66 */
Jens Axboeff856ba2006-01-09 16:02:34 +010067static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Tejun Heod40f75a2015-05-22 17:13:42 -040069static void blk_clear_congested(struct request_list *rl, int sync)
70{
Tejun Heod40f75a2015-05-22 17:13:42 -040071#ifdef CONFIG_CGROUP_WRITEBACK
72 clear_wb_congested(rl->blkg->wb_congested, sync);
73#else
Tejun Heo482cf792015-05-22 17:13:43 -040074 /*
75 * If !CGROUP_WRITEBACK, all blkg's map to bdi->wb and we shouldn't
76 * flip its congestion state for events on other blkcgs.
77 */
78 if (rl == &rl->q->root_rl)
Jan Kara1cdb7f92017-02-02 15:56:50 +010079 clear_wb_congested(rl->q->backing_dev_info->wb.congested, sync);
Tejun Heod40f75a2015-05-22 17:13:42 -040080#endif
81}
82
83static void blk_set_congested(struct request_list *rl, int sync)
84{
Tejun Heod40f75a2015-05-22 17:13:42 -040085#ifdef CONFIG_CGROUP_WRITEBACK
86 set_wb_congested(rl->blkg->wb_congested, sync);
87#else
Tejun Heo482cf792015-05-22 17:13:43 -040088 /* see blk_clear_congested() */
89 if (rl == &rl->q->root_rl)
Jan Kara1cdb7f92017-02-02 15:56:50 +010090 set_wb_congested(rl->q->backing_dev_info->wb.congested, sync);
Tejun Heod40f75a2015-05-22 17:13:42 -040091#endif
92}
93
Jens Axboe8324aa92008-01-29 14:51:59 +010094void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 int nr;
97
98 nr = q->nr_requests - (q->nr_requests / 8) + 1;
99 if (nr > q->nr_requests)
100 nr = q->nr_requests;
101 q->nr_congestion_on = nr;
102
103 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
104 if (nr < 1)
105 nr = 1;
106 q->nr_congestion_off = nr;
107}
108
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200109void blk_rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
FUJITA Tomonori1afb20f2008-04-25 12:26:28 +0200111 memset(rq, 0, sizeof(*rq));
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700114 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboec7c22e42008-09-13 20:26:01 +0200115 rq->cpu = -1;
Jens Axboe63a71382008-02-08 12:41:03 +0100116 rq->q = q;
Tejun Heoa2dec7b2009-05-07 22:24:44 +0900117 rq->__sector = (sector_t) -1;
Jens Axboe2e662b62006-07-13 11:55:04 +0200118 INIT_HLIST_NODE(&rq->hash);
119 RB_CLEAR_NODE(&rq->rb_node);
FUJITA Tomonorid7e3c322008-04-29 09:54:39 +0200120 rq->cmd = rq->__cmd;
Li Zefane2494e12009-04-02 13:43:26 +0800121 rq->cmd_len = BLK_MAX_CDB;
Jens Axboe63a71382008-02-08 12:41:03 +0100122 rq->tag = -1;
Tejun Heob243ddc2009-04-23 11:05:18 +0900123 rq->start_time = jiffies;
Divyesh Shah91952912010-04-01 15:01:41 -0700124 set_start_time_ns(rq);
Jerome Marchand09e099d2011-01-05 16:57:38 +0100125 rq->part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
FUJITA Tomonori2a4aa302008-04-29 09:54:36 +0200127EXPORT_SYMBOL(blk_rq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
NeilBrown5bb23a62007-09-27 12:46:13 +0200129static void req_bio_endio(struct request *rq, struct bio *bio,
130 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100131{
Mike Snitzer78d8e582015-06-26 10:01:13 -0400132 if (error)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200133 bio->bi_error = error;
Tejun Heo797e7db2006-01-06 09:51:03 +0100134
Tejun Heo143a87f2011-01-25 12:43:52 +0100135 if (unlikely(rq->cmd_flags & REQ_QUIET))
Jens Axboeb7c44ed2015-07-24 12:37:59 -0600136 bio_set_flag(bio, BIO_QUIET);
Tejun Heo143a87f2011-01-25 12:43:52 +0100137
Kent Overstreetf79ea412012-09-20 16:38:30 -0700138 bio_advance(bio, nbytes);
Tejun Heo143a87f2011-01-25 12:43:52 +0100139
140 /* don't actually finish bio if it's part of flush sequence */
Mike Snitzer78d8e582015-06-26 10:01:13 -0400141 if (bio->bi_iter.bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ))
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200142 bio_endio(bio);
Tejun Heo797e7db2006-01-06 09:51:03 +0100143}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145void blk_dump_rq_flags(struct request *rq, char *msg)
146{
147 int bit;
148
Jens Axboe59533162013-05-23 12:25:08 +0200149 printk(KERN_INFO "%s: dev %s: type=%x, flags=%llx\n", msg,
Jens Axboe4aff5e22006-08-10 08:44:47 +0200150 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
Jens Axboe59533162013-05-23 12:25:08 +0200151 (unsigned long long) rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Tejun Heo83096eb2009-05-07 22:24:39 +0900153 printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n",
154 (unsigned long long)blk_rq_pos(rq),
155 blk_rq_sectors(rq), blk_rq_cur_sectors(rq));
Jens Axboeb4f42e22014-04-10 09:46:28 -0600156 printk(KERN_INFO " bio %p, biotail %p, len %u\n",
157 rq->bio, rq->biotail, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200159 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Jens Axboe6728cb02008-01-31 13:03:55 +0100160 printk(KERN_INFO " cdb: ");
FUJITA Tomonorid34c87e2008-04-29 14:37:52 +0200161 for (bit = 0; bit < BLK_MAX_CDB; bit++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 printk("%02x ", rq->cmd[bit]);
163 printk("\n");
164 }
165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166EXPORT_SYMBOL(blk_dump_rq_flags);
167
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500168static void blk_delay_work(struct work_struct *work)
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200169{
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500170 struct request_queue *q;
Jens Axboe6c5e0c42008-08-01 20:31:32 +0200171
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500172 q = container_of(work, struct request_queue, delay_work.work);
173 spin_lock_irq(q->queue_lock);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200174 __blk_run_queue(q);
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500175 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/**
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500179 * blk_delay_queue - restart queueing after defined interval
180 * @q: The &struct request_queue in question
181 * @msecs: Delay in msecs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 *
183 * Description:
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500184 * Sometimes queueing needs to be postponed for a little while, to allow
185 * resources to come back. This function will make sure that queueing is
Bart Van Assche70460572012-11-28 13:45:56 +0100186 * restarted around the specified time. Queue lock must be held.
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500187 */
188void blk_delay_queue(struct request_queue *q, unsigned long msecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Bart Van Assche70460572012-11-28 13:45:56 +0100190 if (likely(!blk_queue_dead(q)))
191 queue_delayed_work(kblockd_workqueue, &q->delay_work,
192 msecs_to_jiffies(msecs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500194EXPORT_SYMBOL(blk_delay_queue);
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/**
Jens Axboe21491412015-12-28 13:01:22 -0700197 * blk_start_queue_async - asynchronously restart a previously stopped queue
198 * @q: The &struct request_queue in question
199 *
200 * Description:
201 * blk_start_queue_async() will clear the stop flag on the queue, and
202 * ensure that the request_fn for the queue is run from an async
203 * context.
204 **/
205void blk_start_queue_async(struct request_queue *q)
206{
207 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
208 blk_run_queue_async(q);
209}
210EXPORT_SYMBOL(blk_start_queue_async);
211
212/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200214 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 *
216 * Description:
217 * blk_start_queue() will clear the stop flag on the queue, and call
218 * the request_fn for the queue if it was in a stopped state when
219 * entered. Also see blk_stop_queue(). Queue lock must be held.
220 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200221void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Bart Van Assche120ec1e2017-08-17 13:12:44 -0700223 WARN_ON(!in_interrupt() && !irqs_disabled());
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200224
Nick Piggin75ad23b2008-04-29 14:48:33 +0200225 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200226 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228EXPORT_SYMBOL(blk_start_queue);
229
230/**
231 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200232 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 *
234 * Description:
235 * The Linux block layer assumes that a block driver will consume all
236 * entries on the request queue when the request_fn strategy is called.
237 * Often this will not happen, because of hardware limitations (queue
238 * depth settings). If a device driver gets a 'queue full' response,
239 * or if it simply chooses not to queue more I/O at one point, it can
240 * call this function to prevent the request_fn from being called until
241 * the driver has signalled it's ready to go again. This happens by calling
242 * blk_start_queue() to restart queue operations. Queue lock must be held.
243 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200244void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Tejun Heo136b5722012-08-21 13:18:24 -0700246 cancel_delayed_work(&q->delay_work);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200247 queue_flag_set(QUEUE_FLAG_STOPPED, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249EXPORT_SYMBOL(blk_stop_queue);
250
251/**
252 * blk_sync_queue - cancel any pending callbacks on a queue
253 * @q: the queue
254 *
255 * Description:
256 * The block layer may perform asynchronous callback activity
257 * on a queue, such as calling the unplug function after a timeout.
258 * A block device may call blk_sync_queue to ensure that any
259 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200260 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * that its ->make_request_fn will not re-add plugging prior to calling
262 * this function.
263 *
Vivek Goyalda527772011-03-02 19:05:33 -0500264 * This function does not cancel any asynchronous activity arising
Masanari Iidada3dae52014-09-09 01:27:23 +0900265 * out of elevator or throttling code. That would require elevator_exit()
Tejun Heo5efd6112012-03-05 13:15:12 -0800266 * and blkcg_exit_queue() to be called with queue lock initialized.
Vivek Goyalda527772011-03-02 19:05:33 -0500267 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
269void blk_sync_queue(struct request_queue *q)
270{
Jens Axboe70ed28b2008-11-19 14:38:39 +0100271 del_timer_sync(&q->timeout);
Bart Van Asschee5386fc2017-10-19 10:00:48 -0700272 cancel_work_sync(&q->timeout_work);
Ming Leif04c1fe2013-12-26 21:31:36 +0800273
274 if (q->mq_ops) {
275 struct blk_mq_hw_ctx *hctx;
276 int i;
277
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600278 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe27489a32016-08-24 15:54:25 -0600279 cancel_work_sync(&hctx->run_work);
Christoph Hellwig70f4db62014-04-16 10:48:08 -0600280 cancel_delayed_work_sync(&hctx->delay_work);
281 }
Ming Leif04c1fe2013-12-26 21:31:36 +0800282 } else {
283 cancel_delayed_work_sync(&q->delay_work);
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286EXPORT_SYMBOL(blk_sync_queue);
287
288/**
Bart Van Asschec246e802012-12-06 14:32:01 +0100289 * __blk_run_queue_uncond - run a queue whether or not it has been stopped
290 * @q: The queue to run
291 *
292 * Description:
293 * Invoke request handling on a queue if there are any pending requests.
294 * May be used to restart request handling after a request has completed.
295 * This variant runs the queue whether or not the queue has been
296 * stopped. Must be called with the queue lock held and interrupts
297 * disabled. See also @blk_run_queue.
298 */
299inline void __blk_run_queue_uncond(struct request_queue *q)
300{
301 if (unlikely(blk_queue_dead(q)))
302 return;
303
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100304 /*
305 * Some request_fn implementations, e.g. scsi_request_fn(), unlock
306 * the queue lock internally. As a result multiple threads may be
307 * running such a request function concurrently. Keep track of the
308 * number of active request_fn invocations such that blk_drain_queue()
309 * can wait until all these request_fn calls have finished.
310 */
311 q->request_fn_active++;
Bart Van Asschec246e802012-12-06 14:32:01 +0100312 q->request_fn(q);
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100313 q->request_fn_active--;
Bart Van Asschec246e802012-12-06 14:32:01 +0100314}
Christoph Hellwiga7928c12015-04-17 22:37:20 +0200315EXPORT_SYMBOL_GPL(__blk_run_queue_uncond);
Bart Van Asschec246e802012-12-06 14:32:01 +0100316
317/**
Jens Axboe80a4b582008-10-14 09:51:06 +0200318 * __blk_run_queue - run a single device queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200320 *
321 * Description:
322 * See @blk_run_queue. This variant must be called with the queue lock
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200323 * held and interrupts disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 */
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200325void __blk_run_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Tejun Heoa538cd02009-04-23 11:05:17 +0900327 if (unlikely(blk_queue_stopped(q)))
328 return;
329
Bart Van Asschec246e802012-12-06 14:32:01 +0100330 __blk_run_queue_uncond(q);
Nick Piggin75ad23b2008-04-29 14:48:33 +0200331}
332EXPORT_SYMBOL(__blk_run_queue);
Jens Axboedac07ec2006-05-11 08:20:16 +0200333
Nick Piggin75ad23b2008-04-29 14:48:33 +0200334/**
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200335 * blk_run_queue_async - run a single device queue in workqueue context
336 * @q: The queue to run
337 *
338 * Description:
339 * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
Bart Van Assche70460572012-11-28 13:45:56 +0100340 * of us. The caller must hold the queue lock.
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200341 */
342void blk_run_queue_async(struct request_queue *q)
343{
Bart Van Assche70460572012-11-28 13:45:56 +0100344 if (likely(!blk_queue_stopped(q) && !blk_queue_dead(q)))
Tejun Heoe7c2f962012-08-21 13:18:24 -0700345 mod_delayed_work(kblockd_workqueue, &q->delay_work, 0);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200346}
Jens Axboec21e6be2011-04-19 13:32:46 +0200347EXPORT_SYMBOL(blk_run_queue_async);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200348
349/**
Nick Piggin75ad23b2008-04-29 14:48:33 +0200350 * blk_run_queue - run a single device queue
351 * @q: The queue to run
Jens Axboe80a4b582008-10-14 09:51:06 +0200352 *
353 * Description:
354 * Invoke request handling on this queue, if it has pending work to do.
Tejun Heoa7f55792009-04-23 11:05:17 +0900355 * May be used to restart queueing when a request has completed.
Nick Piggin75ad23b2008-04-29 14:48:33 +0200356 */
357void blk_run_queue(struct request_queue *q)
358{
359 unsigned long flags;
360
361 spin_lock_irqsave(q->queue_lock, flags);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +0200362 __blk_run_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 spin_unlock_irqrestore(q->queue_lock, flags);
364}
365EXPORT_SYMBOL(blk_run_queue);
366
Jens Axboe165125e2007-07-24 09:28:11 +0200367void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500368{
369 kobject_put(&q->kobj);
370}
Jens Axboed86e0e82011-05-27 07:44:43 +0200371EXPORT_SYMBOL(blk_put_queue);
Al Viro483f4af2006-03-18 18:34:37 -0500372
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200373/**
Bart Van Assche807592a2012-11-28 13:43:38 +0100374 * __blk_drain_queue - drain requests from request_queue
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200375 * @q: queue to drain
Tejun Heoc9a929d2011-10-19 14:42:16 +0200376 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200377 *
Tejun Heoc9a929d2011-10-19 14:42:16 +0200378 * Drain requests from @q. If @drain_all is set, all requests are drained.
379 * If not, only ELVPRIV requests are drained. The caller is responsible
380 * for ensuring that no new requests which need to be drained are queued.
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200381 */
Bart Van Assche807592a2012-11-28 13:43:38 +0100382static void __blk_drain_queue(struct request_queue *q, bool drain_all)
383 __releases(q->queue_lock)
384 __acquires(q->queue_lock)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200385{
Asias He458f27a2012-06-15 08:45:25 +0200386 int i;
387
Bart Van Assche807592a2012-11-28 13:43:38 +0100388 lockdep_assert_held(q->queue_lock);
389
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200390 while (true) {
Tejun Heo481a7d62011-12-14 00:33:37 +0100391 bool drain = false;
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200392
Tejun Heob855b042012-03-06 21:24:55 +0100393 /*
394 * The caller might be trying to drain @q before its
395 * elevator is initialized.
396 */
397 if (q->elevator)
398 elv_drain_elevator(q);
399
Tejun Heo5efd6112012-03-05 13:15:12 -0800400 blkcg_drain_queue(q);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200401
Tejun Heo4eabc942011-12-15 20:03:04 +0100402 /*
403 * This function might be called on a queue which failed
Tejun Heob855b042012-03-06 21:24:55 +0100404 * driver init after queue creation or is not yet fully
405 * active yet. Some drivers (e.g. fd and loop) get unhappy
406 * in such cases. Kick queue iff dispatch queue has
407 * something on it and @q has request_fn set.
Tejun Heo4eabc942011-12-15 20:03:04 +0100408 */
Tejun Heob855b042012-03-06 21:24:55 +0100409 if (!list_empty(&q->queue_head) && q->request_fn)
Tejun Heo4eabc942011-12-15 20:03:04 +0100410 __blk_run_queue(q);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200411
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700412 drain |= q->nr_rqs_elvpriv;
Bart Van Assche24faf6f2012-11-28 13:46:45 +0100413 drain |= q->request_fn_active;
Tejun Heo481a7d62011-12-14 00:33:37 +0100414
415 /*
416 * Unfortunately, requests are queued at and tracked from
417 * multiple places and there's no single counter which can
418 * be drained. Check all the queues and counters.
419 */
420 if (drain_all) {
Ming Leie97c2932014-09-25 23:23:46 +0800421 struct blk_flush_queue *fq = blk_get_flush_queue(q, NULL);
Tejun Heo481a7d62011-12-14 00:33:37 +0100422 drain |= !list_empty(&q->queue_head);
423 for (i = 0; i < 2; i++) {
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700424 drain |= q->nr_rqs[i];
Tejun Heo481a7d62011-12-14 00:33:37 +0100425 drain |= q->in_flight[i];
Ming Lei7c94e1c2014-09-25 23:23:43 +0800426 if (fq)
427 drain |= !list_empty(&fq->flush_queue[i]);
Tejun Heo481a7d62011-12-14 00:33:37 +0100428 }
429 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200430
Tejun Heo481a7d62011-12-14 00:33:37 +0100431 if (!drain)
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200432 break;
Bart Van Assche807592a2012-11-28 13:43:38 +0100433
434 spin_unlock_irq(q->queue_lock);
435
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200436 msleep(10);
Bart Van Assche807592a2012-11-28 13:43:38 +0100437
438 spin_lock_irq(q->queue_lock);
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200439 }
Asias He458f27a2012-06-15 08:45:25 +0200440
441 /*
442 * With queue marked dead, any woken up waiter will fail the
443 * allocation path, so the wakeup chaining is lost and we're
444 * left with hung waiters. We need to wake up those waiters.
445 */
446 if (q->request_fn) {
Tejun Heoa0516612012-06-26 15:05:44 -0700447 struct request_list *rl;
448
Tejun Heoa0516612012-06-26 15:05:44 -0700449 blk_queue_for_each_rl(rl, q)
450 for (i = 0; i < ARRAY_SIZE(rl->wait); i++)
451 wake_up_all(&rl->wait[i]);
Asias He458f27a2012-06-15 08:45:25 +0200452 }
Tejun Heoe3c78ca2011-10-19 14:32:38 +0200453}
454
Tejun Heoc9a929d2011-10-19 14:42:16 +0200455/**
Tejun Heod7325802012-03-05 13:14:58 -0800456 * blk_queue_bypass_start - enter queue bypass mode
457 * @q: queue of interest
458 *
459 * In bypass mode, only the dispatch FIFO queue of @q is used. This
460 * function makes @q enter bypass mode and drains all requests which were
Tejun Heo6ecf23a2012-03-05 13:14:59 -0800461 * throttled or issued before. On return, it's guaranteed that no request
Tejun Heo80fd9972012-04-13 14:50:53 -0700462 * is being throttled or has ELVPRIV set and blk_queue_bypass() %true
463 * inside queue or RCU read lock.
Tejun Heod7325802012-03-05 13:14:58 -0800464 */
465void blk_queue_bypass_start(struct request_queue *q)
466{
467 spin_lock_irq(q->queue_lock);
Tejun Heo776687b2014-07-01 10:29:17 -0600468 q->bypass_depth++;
Tejun Heod7325802012-03-05 13:14:58 -0800469 queue_flag_set(QUEUE_FLAG_BYPASS, q);
470 spin_unlock_irq(q->queue_lock);
471
Tejun Heo776687b2014-07-01 10:29:17 -0600472 /*
473 * Queues start drained. Skip actual draining till init is
474 * complete. This avoids lenghty delays during queue init which
475 * can happen many times during boot.
476 */
477 if (blk_queue_init_done(q)) {
Bart Van Assche807592a2012-11-28 13:43:38 +0100478 spin_lock_irq(q->queue_lock);
479 __blk_drain_queue(q, false);
480 spin_unlock_irq(q->queue_lock);
481
Tejun Heob82d4b12012-04-13 13:11:31 -0700482 /* ensure blk_queue_bypass() is %true inside RCU read lock */
483 synchronize_rcu();
484 }
Tejun Heod7325802012-03-05 13:14:58 -0800485}
486EXPORT_SYMBOL_GPL(blk_queue_bypass_start);
487
488/**
489 * blk_queue_bypass_end - leave queue bypass mode
490 * @q: queue of interest
491 *
492 * Leave bypass mode and restore the normal queueing behavior.
493 */
494void blk_queue_bypass_end(struct request_queue *q)
495{
496 spin_lock_irq(q->queue_lock);
497 if (!--q->bypass_depth)
498 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
499 WARN_ON_ONCE(q->bypass_depth < 0);
500 spin_unlock_irq(q->queue_lock);
501}
502EXPORT_SYMBOL_GPL(blk_queue_bypass_end);
503
Jens Axboeaed3ea92014-12-22 14:04:42 -0700504void blk_set_queue_dying(struct request_queue *q)
505{
Bart Van Assche1b856082016-08-16 16:48:36 -0700506 spin_lock_irq(q->queue_lock);
507 queue_flag_set(QUEUE_FLAG_DYING, q);
508 spin_unlock_irq(q->queue_lock);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700509
510 if (q->mq_ops)
511 blk_mq_wake_waiters(q);
512 else {
513 struct request_list *rl;
514
515 blk_queue_for_each_rl(rl, q) {
516 if (rl->rq_pool) {
Ming Lei1a5a4c62017-11-16 08:08:44 +0800517 wake_up_all(&rl->wait[BLK_RW_SYNC]);
518 wake_up_all(&rl->wait[BLK_RW_ASYNC]);
Jens Axboeaed3ea92014-12-22 14:04:42 -0700519 }
520 }
521 }
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 Axboee3335de92008-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);
564 spin_lock_irq(lock);
565 if (!q->mq_ops)
Ming Lei43a5e4e2013-12-26 21:31:35 +0800566 __blk_drain_queue(q, true);
Bart Van Asschec246e802012-12-06 14:32:01 +0100567 queue_flag_set(QUEUE_FLAG_DEAD, q);
Bart Van Assche807592a2012-11-28 13:43:38 +0100568 spin_unlock_irq(lock);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200569
Dan Williams5a48fc12015-10-21 13:20:23 -0400570 /* for synchronous bio-based driver finish in-flight integrity i/o */
571 blk_flush_integrity();
572
Tejun Heoc9a929d2011-10-19 14:42:16 +0200573 /* @q won't process any more request, flush async actions */
Jan Kara1cdb7f92017-02-02 15:56:50 +0100574 del_timer_sync(&q->backing_dev_info->laptop_mode_wb_timer);
Tejun Heoc9a929d2011-10-19 14:42:16 +0200575 blk_sync_queue(q);
576
Bart Van Assche45a9c9d2014-12-09 16:57:48 +0100577 if (q->mq_ops)
578 blk_mq_free_queue(q);
Dan Williams3ef28e82015-10-21 13:20:12 -0400579 percpu_ref_exit(&q->q_usage_counter);
Bart Van Assche45a9c9d2014-12-09 16:57:48 +0100580
Asias He5e5cfac2012-05-24 23:28:52 +0800581 spin_lock_irq(lock);
582 if (q->queue_lock != &q->__queue_lock)
583 q->queue_lock = &q->__queue_lock;
584 spin_unlock_irq(lock);
585
Tejun Heoc9a929d2011-10-19 14:42:16 +0200586 /* @q is and will stay empty, shutdown and put */
Al Viro483f4af2006-03-18 18:34:37 -0500587 blk_put_queue(q);
588}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589EXPORT_SYMBOL(blk_cleanup_queue);
590
David Rientjes271508d2015-03-24 16:21:16 -0700591/* Allocate memory local to the request queue */
592static void *alloc_request_struct(gfp_t gfp_mask, void *data)
593{
594 int nid = (int)(long)data;
595 return kmem_cache_alloc_node(request_cachep, gfp_mask, nid);
596}
597
598static void free_request_struct(void *element, void *unused)
599{
600 kmem_cache_free(request_cachep, element);
601}
602
Tejun Heo5b788ce2012-06-04 20:40:59 -0700603int blk_init_rl(struct request_list *rl, struct request_queue *q,
604 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Mike Snitzer1abec4f2010-05-25 13:15:15 -0400606 if (unlikely(rl->rq_pool))
607 return 0;
608
Tejun Heo5b788ce2012-06-04 20:40:59 -0700609 rl->q = q;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200610 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
611 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200612 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
613 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
David Rientjes271508d2015-03-24 16:21:16 -0700615 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, alloc_request_struct,
616 free_request_struct,
617 (void *)(long)q->node, gfp_mask,
618 q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (!rl->rq_pool)
620 return -ENOMEM;
621
622 return 0;
623}
624
Tejun Heo5b788ce2012-06-04 20:40:59 -0700625void blk_exit_rl(struct request_list *rl)
626{
627 if (rl->rq_pool)
628 mempool_destroy(rl->rq_pool);
629}
630
Jens Axboe165125e2007-07-24 09:28:11 +0200631struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100633 return blk_alloc_queue_node(gfp_mask, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700634}
635EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100637int blk_queue_enter(struct request_queue *q, bool nowait)
Dan Williams3ef28e82015-10-21 13:20:12 -0400638{
639 while (true) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400640
641 if (percpu_ref_tryget_live(&q->q_usage_counter))
642 return 0;
643
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100644 if (nowait)
Dan Williams3ef28e82015-10-21 13:20:12 -0400645 return -EBUSY;
646
Alan Jenkins3118ceb2018-04-12 19:11:58 +0100647 wait_event(q->mq_freeze_wq,
648 !atomic_read(&q->mq_freeze_depth) ||
649 blk_queue_dying(q));
Dan Williams3ef28e82015-10-21 13:20:12 -0400650 if (blk_queue_dying(q))
651 return -ENODEV;
Dan Williams3ef28e82015-10-21 13:20:12 -0400652 }
653}
654
655void blk_queue_exit(struct request_queue *q)
656{
657 percpu_ref_put(&q->q_usage_counter);
658}
659
660static void blk_queue_usage_counter_release(struct percpu_ref *ref)
661{
662 struct request_queue *q =
663 container_of(ref, struct request_queue, q_usage_counter);
664
665 wake_up_all(&q->mq_freeze_wq);
666}
667
Christoph Hellwig287922e2015-10-30 20:57:30 +0800668static void blk_rq_timed_out_timer(unsigned long data)
669{
670 struct request_queue *q = (struct request_queue *)data;
671
672 kblockd_schedule_work(&q->timeout_work);
673}
674
Jens Axboe165125e2007-07-24 09:28:11 +0200675struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700676{
Jens Axboe165125e2007-07-24 09:28:11 +0200677 struct request_queue *q;
Christoph Lameter19460892005-06-23 00:08:19 -0700678
Jens Axboe8324aa92008-01-29 14:51:59 +0100679 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700680 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (!q)
682 return NULL;
683
Dan Carpenter00380a42012-03-23 09:58:54 +0100684 q->id = ida_simple_get(&blk_queue_ida, 0, 0, gfp_mask);
Tejun Heoa73f7302011-12-14 00:33:37 +0100685 if (q->id < 0)
Ming Lei3d2936f2014-05-27 23:35:14 +0800686 goto fail_q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100687
Kent Overstreet54efd502015-04-23 22:37:18 -0700688 q->bio_split = bioset_create(BIO_POOL_SIZE, 0);
689 if (!q->bio_split)
690 goto fail_id;
691
Jan Kara33c17052017-02-02 15:56:51 +0100692 q->backing_dev_info = bdi_alloc_node(gfp_mask, node_id);
693 if (!q->backing_dev_info)
694 goto fail_split;
695
Jan Kara1cdb7f92017-02-02 15:56:50 +0100696 q->backing_dev_info->ra_pages =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300697 (VM_MAX_READAHEAD * 1024) / PAGE_SIZE;
Jan Kara1cdb7f92017-02-02 15:56:50 +0100698 q->backing_dev_info->capabilities = BDI_CAP_CGROUP_WRITEBACK;
699 q->backing_dev_info->name = "block";
Mike Snitzer51514122011-11-23 10:59:13 +0100700 q->node = node_id;
Jens Axboe0989a022009-06-12 14:42:56 +0200701
Jan Kara1cdb7f92017-02-02 15:56:50 +0100702 setup_timer(&q->backing_dev_info->laptop_mode_wb_timer,
Matthew Garrett31373d02010-04-06 14:25:14 +0200703 laptop_mode_timer_fn, (unsigned long) q);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700704 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
Bart Van Asschee5386fc2017-10-19 10:00:48 -0700705 INIT_WORK(&q->timeout_work, NULL);
Tejun Heob855b042012-03-06 21:24:55 +0100706 INIT_LIST_HEAD(&q->queue_head);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700707 INIT_LIST_HEAD(&q->timeout_list);
Tejun Heoa612fdd2011-12-14 00:33:41 +0100708 INIT_LIST_HEAD(&q->icq_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800709#ifdef CONFIG_BLK_CGROUP
Tejun Heoe8989fa2012-03-05 13:15:20 -0800710 INIT_LIST_HEAD(&q->blkg_list);
Tejun Heo4eef3042012-03-05 13:15:18 -0800711#endif
Jens Axboe3cca6dc2011-03-02 11:08:00 -0500712 INIT_DELAYED_WORK(&q->delay_work, blk_delay_work);
Al Viro483f4af2006-03-18 18:34:37 -0500713
Jens Axboe8324aa92008-01-29 14:51:59 +0100714 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Al Viro483f4af2006-03-18 18:34:37 -0500716 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700717 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500718
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500719 /*
720 * By default initialize queue_lock to internal lock and driver can
721 * override it later if need be.
722 */
723 q->queue_lock = &q->__queue_lock;
724
Tejun Heob82d4b12012-04-13 13:11:31 -0700725 /*
726 * A queue starts its life with bypass turned on to avoid
727 * unnecessary bypass on/off overhead and nasty surprises during
Tejun Heo749fefe2012-09-20 14:08:52 -0700728 * init. The initial bypass will be finished when the queue is
729 * registered by blk_register_queue().
Tejun Heob82d4b12012-04-13 13:11:31 -0700730 */
731 q->bypass_depth = 1;
732 __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
733
Jens Axboe320ae512013-10-24 09:20:05 +0100734 init_waitqueue_head(&q->mq_freeze_wq);
735
Dan Williams3ef28e82015-10-21 13:20:12 -0400736 /*
737 * Init percpu_ref in atomic mode so that it's faster to shutdown.
738 * See blk_register_queue() for details.
739 */
740 if (percpu_ref_init(&q->q_usage_counter,
741 blk_queue_usage_counter_release,
742 PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
Mikulas Patockafff49962013-10-14 12:11:36 -0400743 goto fail_bdi;
Tejun Heof51b8022012-03-05 13:15:05 -0800744
Dan Williams3ef28e82015-10-21 13:20:12 -0400745 if (blkcg_init_queue(q))
746 goto fail_ref;
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100749
Dan Williams3ef28e82015-10-21 13:20:12 -0400750fail_ref:
751 percpu_ref_exit(&q->q_usage_counter);
Mikulas Patockafff49962013-10-14 12:11:36 -0400752fail_bdi:
Jan Kara33c17052017-02-02 15:56:51 +0100753 bdi_put(q->backing_dev_info);
Kent Overstreet54efd502015-04-23 22:37:18 -0700754fail_split:
755 bioset_free(q->bio_split);
Tejun Heoa73f7302011-12-14 00:33:37 +0100756fail_id:
757 ida_simple_remove(&blk_queue_ida, q->id);
758fail_q:
759 kmem_cache_free(blk_requestq_cachep, q);
760 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
Christoph Lameter19460892005-06-23 00:08:19 -0700762EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764/**
765 * blk_init_queue - prepare a request queue for use with a block device
766 * @rfn: The function to be called to process requests that have been
767 * placed on the queue.
768 * @lock: Request queue spin lock
769 *
770 * Description:
771 * If a block device wishes to use the standard request handling procedures,
772 * which sorts requests and coalesces adjacent requests, then it must
773 * call blk_init_queue(). The function @rfn will be called when there
774 * are requests on the queue that need to be processed. If the device
775 * supports plugging, then @rfn may not be called immediately when requests
776 * are available on the queue, but may be called at some time later instead.
777 * Plugged queues are generally unplugged when a buffer belonging to one
778 * of the requests on the queue is needed, or due to memory pressure.
779 *
780 * @rfn is not required, or even expected, to remove all requests off the
781 * queue, but only as many as it can handle at a time. If it does leave
782 * requests on the queue, it is responsible for arranging that the requests
783 * get dealt with eventually.
784 *
785 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200786 * request queue; this lock will be taken also from interrupt context, so irq
787 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200789 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 * it didn't succeed.
791 *
792 * Note:
793 * blk_init_queue() must be paired with a blk_cleanup_queue() call
794 * when the block device is deactivated (such as at module unload).
795 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700796
Jens Axboe165125e2007-07-24 09:28:11 +0200797struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100799 return blk_init_queue_node(rfn, lock, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700800}
801EXPORT_SYMBOL(blk_init_queue);
802
Jens Axboe165125e2007-07-24 09:28:11 +0200803struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700804blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
805{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600806 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600808 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
809 if (!uninit_q)
810 return NULL;
811
Mike Snitzer51514122011-11-23 10:59:13 +0100812 q = blk_init_allocated_queue(uninit_q, rfn, lock);
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600813 if (!q)
Mike Snitzer7982e902014-03-08 17:20:01 -0700814 blk_cleanup_queue(uninit_q);
Christoph Hellwig18741982014-02-10 09:29:00 -0700815
Mike Snitzer7982e902014-03-08 17:20:01 -0700816 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200817}
818EXPORT_SYMBOL(blk_init_queue_node);
819
Jens Axboedece1632015-11-05 10:41:16 -0700820static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio);
Mike Snitzer336b7e12015-05-11 14:06:32 -0400821
Mike Snitzer01effb02010-05-11 08:57:42 +0200822struct request_queue *
823blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
824 spinlock_t *lock)
825{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (!q)
827 return NULL;
828
Ming Leif70ced02014-09-25 23:23:47 +0800829 q->fq = blk_alloc_flush_queue(q, NUMA_NO_NODE, 0);
Ming Leiba483382014-09-25 23:23:44 +0800830 if (!q->fq)
Mike Snitzer7982e902014-03-08 17:20:01 -0700831 return NULL;
832
Tejun Heoa0516612012-06-26 15:05:44 -0700833 if (blk_init_rl(&q->root_rl, q, GFP_KERNEL))
Dave Jones708f04d2014-03-20 15:03:58 -0600834 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Christoph Hellwig287922e2015-10-30 20:57:30 +0800836 INIT_WORK(&q->timeout_work, blk_timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900839 q->unprep_rq_fn = NULL;
Tejun Heo60ea8222012-09-20 14:09:30 -0700840 q->queue_flags |= QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500841
842 /* Override internal queue lock with supplied lock pointer */
843 if (lock)
844 q->queue_lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Jens Axboef3b144a2009-03-06 08:48:33 +0100846 /*
847 * This also sets hw/phys segments, boundary and size
848 */
Jens Axboec20e8de2011-09-12 12:03:37 +0200849 blk_queue_make_request(q, blk_queue_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Alan Stern44ec9542007-02-20 11:01:57 -0500851 q->sg_reserved_size = INT_MAX;
852
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600853 /* Protect q->elevator from elevator_change */
854 mutex_lock(&q->sysfs_lock);
855
Tejun Heob82d4b12012-04-13 13:11:31 -0700856 /* init elevator */
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600857 if (elevator_init(q, NULL)) {
858 mutex_unlock(&q->sysfs_lock);
Dave Jones708f04d2014-03-20 15:03:58 -0600859 goto fail;
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600860 }
861
862 mutex_unlock(&q->sysfs_lock);
863
Tejun Heob82d4b12012-04-13 13:11:31 -0700864 return q;
Dave Jones708f04d2014-03-20 15:03:58 -0600865
866fail:
Ming Leiba483382014-09-25 23:23:44 +0800867 blk_free_flush_queue(q->fq);
xiao jinc1919912018-07-30 14:11:12 +0800868 q->fq = NULL;
Dave Jones708f04d2014-03-20 15:03:58 -0600869 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
Mike Snitzer51514122011-11-23 10:59:13 +0100871EXPORT_SYMBOL(blk_init_allocated_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Tejun Heo09ac46c2011-12-14 00:33:38 +0100873bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100875 if (likely(!blk_queue_dying(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100876 __blk_get_queue(q);
877 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
Tejun Heo09ac46c2011-12-14 00:33:38 +0100880 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
Jens Axboed86e0e82011-05-27 07:44:43 +0200882EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Tejun Heo5b788ce2012-06-04 20:40:59 -0700884static inline void blk_free_request(struct request_list *rl, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Tejun Heof1f8cc92011-12-14 00:33:42 +0100886 if (rq->cmd_flags & REQ_ELVPRIV) {
Tejun Heo5b788ce2012-06-04 20:40:59 -0700887 elv_put_request(rl->q, rq);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100888 if (rq->elv.icq)
Tejun Heo11a31222012-02-07 07:51:30 +0100889 put_io_context(rq->elv.icq->ioc);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100890 }
891
Tejun Heo5b788ce2012-06-04 20:40:59 -0700892 mempool_free(rq, rl->rq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/*
896 * ioc_batching returns true if the ioc is a valid batching request and
897 * should be given priority access to a request.
898 */
Jens Axboe165125e2007-07-24 09:28:11 +0200899static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 if (!ioc)
902 return 0;
903
904 /*
905 * Make sure the process is able to allocate at least 1 request
906 * even if the batch times out, otherwise we could theoretically
907 * lose wakeups.
908 */
909 return ioc->nr_batch_requests == q->nr_batching ||
910 (ioc->nr_batch_requests > 0
911 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
912}
913
914/*
915 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
916 * will cause the process to be a "batcher" on all queues in the system. This
917 * is the behaviour we want though - once it gets a wakeup it should be given
918 * a nice run.
919 */
Jens Axboe165125e2007-07-24 09:28:11 +0200920static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 if (!ioc || ioc_batching(q, ioc))
923 return;
924
925 ioc->nr_batch_requests = q->nr_batching;
926 ioc->last_waited = jiffies;
927}
928
Tejun Heo5b788ce2012-06-04 20:40:59 -0700929static void __freed_request(struct request_list *rl, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700931 struct request_queue *q = rl->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Tejun Heod40f75a2015-05-22 17:13:42 -0400933 if (rl->count[sync] < queue_congestion_off_threshold(q))
934 blk_clear_congested(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Jens Axboe1faa16d2009-04-06 14:48:01 +0200936 if (rl->count[sync] + 1 <= q->nr_requests) {
937 if (waitqueue_active(&rl->wait[sync]))
938 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Tejun Heo5b788ce2012-06-04 20:40:59 -0700940 blk_clear_rl_full(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942}
943
944/*
945 * A request has just been released. Account for it, update the full and
946 * congestion status, wake up any waiters. Called under q->queue_lock.
947 */
Mike Christiee6a40b02016-06-05 14:32:11 -0500948static void freed_request(struct request_list *rl, int op, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700950 struct request_queue *q = rl->q;
Mike Christied9d8c5c2016-06-05 14:32:16 -0500951 int sync = rw_is_sync(op, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700953 q->nr_rqs[sync]--;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200954 rl->count[sync]--;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200955 if (flags & REQ_ELVPRIV)
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700956 q->nr_rqs_elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Tejun Heo5b788ce2012-06-04 20:40:59 -0700958 __freed_request(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Jens Axboe1faa16d2009-04-06 14:48:01 +0200960 if (unlikely(rl->starved[sync ^ 1]))
Tejun Heo5b788ce2012-06-04 20:40:59 -0700961 __freed_request(rl, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600964int blk_update_nr_requests(struct request_queue *q, unsigned int nr)
965{
966 struct request_list *rl;
Tejun Heod40f75a2015-05-22 17:13:42 -0400967 int on_thresh, off_thresh;
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600968
969 spin_lock_irq(q->queue_lock);
970 q->nr_requests = nr;
971 blk_queue_congestion_threshold(q);
Tejun Heod40f75a2015-05-22 17:13:42 -0400972 on_thresh = queue_congestion_on_threshold(q);
973 off_thresh = queue_congestion_off_threshold(q);
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600974
975 blk_queue_for_each_rl(rl, q) {
Tejun Heod40f75a2015-05-22 17:13:42 -0400976 if (rl->count[BLK_RW_SYNC] >= on_thresh)
977 blk_set_congested(rl, BLK_RW_SYNC);
978 else if (rl->count[BLK_RW_SYNC] < off_thresh)
979 blk_clear_congested(rl, BLK_RW_SYNC);
980
981 if (rl->count[BLK_RW_ASYNC] >= on_thresh)
982 blk_set_congested(rl, BLK_RW_ASYNC);
983 else if (rl->count[BLK_RW_ASYNC] < off_thresh)
984 blk_clear_congested(rl, BLK_RW_ASYNC);
985
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600986 if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
987 blk_set_rl_full(rl, BLK_RW_SYNC);
988 } else {
989 blk_clear_rl_full(rl, BLK_RW_SYNC);
990 wake_up(&rl->wait[BLK_RW_SYNC]);
991 }
992
993 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
994 blk_set_rl_full(rl, BLK_RW_ASYNC);
995 } else {
996 blk_clear_rl_full(rl, BLK_RW_ASYNC);
997 wake_up(&rl->wait[BLK_RW_ASYNC]);
998 }
999 }
1000
1001 spin_unlock_irq(q->queue_lock);
1002 return 0;
1003}
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +01001006 * Determine if elevator data should be initialized when allocating the
1007 * request associated with @bio.
1008 */
1009static bool blk_rq_should_init_elevator(struct bio *bio)
1010{
1011 if (!bio)
1012 return true;
1013
1014 /*
1015 * Flush requests do not use the elevator so skip initialization.
1016 * This allows a request to share the flush and elevator data.
1017 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06001018 if (bio->bi_opf & (REQ_PREFLUSH | REQ_FUA))
Mike Snitzer9d5a4e92011-02-11 11:05:46 +01001019 return false;
1020
1021 return true;
1022}
1023
Tejun Heoda8303c2011-10-19 14:33:05 +02001024/**
Tejun Heo852c7882012-03-05 13:15:27 -08001025 * rq_ioc - determine io_context for request allocation
1026 * @bio: request being allocated is for this bio (can be %NULL)
1027 *
1028 * Determine io_context to use for request allocation for @bio. May return
1029 * %NULL if %current->io_context doesn't exist.
1030 */
1031static struct io_context *rq_ioc(struct bio *bio)
1032{
1033#ifdef CONFIG_BLK_CGROUP
1034 if (bio && bio->bi_ioc)
1035 return bio->bi_ioc;
1036#endif
1037 return current->io_context;
1038}
1039
1040/**
Tejun Heoa06e05e2012-06-04 20:40:55 -07001041 * __get_request - get a free request
Tejun Heo5b788ce2012-06-04 20:40:59 -07001042 * @rl: request list to allocate from
Mike Christiee6a40b02016-06-05 14:32:11 -05001043 * @op: REQ_OP_READ/REQ_OP_WRITE
1044 * @op_flags: rq_flag_bits
Tejun Heoda8303c2011-10-19 14:33:05 +02001045 * @bio: bio to allocate request for (can be %NULL)
1046 * @gfp_mask: allocation mask
1047 *
1048 * Get a free request from @q. This function may fail under memory
1049 * pressure or if @q is dead.
1050 *
Masanari Iidada3dae52014-09-09 01:27:23 +09001051 * Must be called with @q->queue_lock held and,
Joe Lawrencea492f072014-08-28 08:15:21 -06001052 * Returns ERR_PTR on failure, with @q->queue_lock held.
1053 * Returns request pointer on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
Mike Christiee6a40b02016-06-05 14:32:11 -05001055static struct request *__get_request(struct request_list *rl, int op,
1056 int op_flags, struct bio *bio,
1057 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Tejun Heo5b788ce2012-06-04 20:40:59 -07001059 struct request_queue *q = rl->q;
Tejun Heob6792812012-03-05 13:15:23 -08001060 struct request *rq;
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001061 struct elevator_type *et = q->elevator->type;
1062 struct io_context *ioc = rq_ioc(bio);
Tejun Heof1f8cc92011-12-14 00:33:42 +01001063 struct io_cq *icq = NULL;
Mike Christied9d8c5c2016-06-05 14:32:16 -05001064 const bool is_sync = rw_is_sync(op, op_flags) != 0;
Tejun Heo75eb6c32011-10-19 14:31:22 +02001065 int may_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001067 if (unlikely(blk_queue_dying(q)))
Joe Lawrencea492f072014-08-28 08:15:21 -06001068 return ERR_PTR(-ENODEV);
Tejun Heoda8303c2011-10-19 14:33:05 +02001069
Mike Christieba568ea2016-06-05 14:32:13 -05001070 may_queue = elv_may_queue(q, op, op_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001071 if (may_queue == ELV_MQUEUE_NO)
1072 goto rq_starved;
1073
Jens Axboe1faa16d2009-04-06 14:48:01 +02001074 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
1075 if (rl->count[is_sync]+1 >= q->nr_requests) {
Tejun Heof2dbd762011-12-14 00:33:40 +01001076 /*
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001077 * The queue will fill after this allocation, so set
1078 * it as full, and mark this process as "batching".
1079 * This process will be allowed to complete a batch of
1080 * requests, others will be blocked.
1081 */
Tejun Heo5b788ce2012-06-04 20:40:59 -07001082 if (!blk_rl_full(rl, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001083 ioc_set_batching(q, ioc);
Tejun Heo5b788ce2012-06-04 20:40:59 -07001084 blk_set_rl_full(rl, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001085 } else {
1086 if (may_queue != ELV_MQUEUE_MUST
1087 && !ioc_batching(q, ioc)) {
1088 /*
1089 * The queue is full and the allocating
1090 * process is not a "batcher", and not
1091 * exempted by the IO scheduler
1092 */
Joe Lawrencea492f072014-08-28 08:15:21 -06001093 return ERR_PTR(-ENOMEM);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001094 }
1095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
Tejun Heod40f75a2015-05-22 17:13:42 -04001097 blk_set_congested(rl, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099
Jens Axboe082cf692005-06-28 16:35:11 +02001100 /*
1101 * Only allow batching queuers to allocate up to 50% over the defined
1102 * limit of requests, otherwise we could have thousands of requests
1103 * allocated with any setting of ->nr_requests
1104 */
Jens Axboe1faa16d2009-04-06 14:48:01 +02001105 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Joe Lawrencea492f072014-08-28 08:15:21 -06001106 return ERR_PTR(-ENOMEM);
Hugh Dickinsfd782a42005-06-29 15:15:40 +01001107
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001108 q->nr_rqs[is_sync]++;
Jens Axboe1faa16d2009-04-06 14:48:01 +02001109 rl->count[is_sync]++;
1110 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02001111
Tejun Heof1f8cc92011-12-14 00:33:42 +01001112 /*
1113 * Decide whether the new request will be managed by elevator. If
Mike Christiee6a40b02016-06-05 14:32:11 -05001114 * so, mark @op_flags and increment elvpriv. Non-zero elvpriv will
Tejun Heof1f8cc92011-12-14 00:33:42 +01001115 * prevent the current elevator from being destroyed until the new
1116 * request is freed. This guarantees icq's won't be destroyed and
1117 * makes creating new ones safe.
1118 *
1119 * Also, lookup icq while holding queue_lock. If it doesn't exist,
1120 * it will be created after releasing queue_lock.
1121 */
Tejun Heod7325802012-03-05 13:14:58 -08001122 if (blk_rq_should_init_elevator(bio) && !blk_queue_bypass(q)) {
Mike Christiee6a40b02016-06-05 14:32:11 -05001123 op_flags |= REQ_ELVPRIV;
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001124 q->nr_rqs_elvpriv++;
Tejun Heof1f8cc92011-12-14 00:33:42 +01001125 if (et->icq_cache && ioc)
1126 icq = ioc_lookup_icq(ioc, q);
Mike Snitzer9d5a4e92011-02-11 11:05:46 +01001127 }
Tejun Heocb98fc82005-10-28 08:29:39 +02001128
Jens Axboef253b862010-10-24 22:06:02 +02001129 if (blk_queue_io_stat(q))
Mike Christiee6a40b02016-06-05 14:32:11 -05001130 op_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 spin_unlock_irq(q->queue_lock);
1132
Tejun Heo29e2b092012-04-19 16:29:21 -07001133 /* allocate and init request */
Tejun Heo5b788ce2012-06-04 20:40:59 -07001134 rq = mempool_alloc(rl->rq_pool, gfp_mask);
Tejun Heo29e2b092012-04-19 16:29:21 -07001135 if (!rq)
Tejun Heob6792812012-03-05 13:15:23 -08001136 goto fail_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Tejun Heo29e2b092012-04-19 16:29:21 -07001138 blk_rq_init(q, rq);
Tejun Heoa0516612012-06-26 15:05:44 -07001139 blk_rq_set_rl(rq, rl);
Mike Christiee6a40b02016-06-05 14:32:11 -05001140 req_set_op_attrs(rq, op, op_flags | REQ_ALLOCED);
Tejun Heo29e2b092012-04-19 16:29:21 -07001141
Tejun Heoaaf7c682012-04-19 16:29:22 -07001142 /* init elvpriv */
Mike Christiee6a40b02016-06-05 14:32:11 -05001143 if (op_flags & REQ_ELVPRIV) {
Tejun Heoaaf7c682012-04-19 16:29:22 -07001144 if (unlikely(et->icq_cache && !icq)) {
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001145 if (ioc)
1146 icq = ioc_create_icq(ioc, q, gfp_mask);
Tejun Heoaaf7c682012-04-19 16:29:22 -07001147 if (!icq)
1148 goto fail_elvpriv;
Tejun Heo29e2b092012-04-19 16:29:21 -07001149 }
Tejun Heoaaf7c682012-04-19 16:29:22 -07001150
1151 rq->elv.icq = icq;
1152 if (unlikely(elv_set_request(q, rq, bio, gfp_mask)))
1153 goto fail_elvpriv;
1154
1155 /* @rq->elv.icq holds io_context until @rq is freed */
Tejun Heo29e2b092012-04-19 16:29:21 -07001156 if (icq)
1157 get_io_context(icq->ioc);
1158 }
Tejun Heoaaf7c682012-04-19 16:29:22 -07001159out:
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001160 /*
1161 * ioc may be NULL here, and ioc_batching will be false. That's
1162 * OK, if the queue is under the request limit then requests need
1163 * not count toward the nr_batch_requests limit. There will always
1164 * be some limit enforced by BLK_BATCH_TIME.
1165 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (ioc_batching(q, ioc))
1167 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +01001168
Mike Christiee6a40b02016-06-05 14:32:11 -05001169 trace_block_getrq(q, bio, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return rq;
Tejun Heob6792812012-03-05 13:15:23 -08001171
Tejun Heoaaf7c682012-04-19 16:29:22 -07001172fail_elvpriv:
1173 /*
1174 * elvpriv init failed. ioc, icq and elvpriv aren't mempool backed
1175 * and may fail indefinitely under memory pressure and thus
1176 * shouldn't stall IO. Treat this request as !elvpriv. This will
1177 * disturb iosched and blkcg but weird is bettern than dead.
1178 */
Robert Elliott7b2b10e2014-08-27 10:50:36 -05001179 printk_ratelimited(KERN_WARNING "%s: dev %s: request aux data allocation failed, iosched may be disturbed\n",
Jan Kara1cdb7f92017-02-02 15:56:50 +01001180 __func__, dev_name(q->backing_dev_info->dev));
Tejun Heoaaf7c682012-04-19 16:29:22 -07001181
1182 rq->cmd_flags &= ~REQ_ELVPRIV;
1183 rq->elv.icq = NULL;
1184
1185 spin_lock_irq(q->queue_lock);
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001186 q->nr_rqs_elvpriv--;
Tejun Heoaaf7c682012-04-19 16:29:22 -07001187 spin_unlock_irq(q->queue_lock);
1188 goto out;
1189
Tejun Heob6792812012-03-05 13:15:23 -08001190fail_alloc:
1191 /*
1192 * Allocation failed presumably due to memory. Undo anything we
1193 * might have messed up.
1194 *
1195 * Allocating task should really be put onto the front of the wait
1196 * queue, but this is pretty rare.
1197 */
1198 spin_lock_irq(q->queue_lock);
Mike Christiee6a40b02016-06-05 14:32:11 -05001199 freed_request(rl, op, op_flags);
Tejun Heob6792812012-03-05 13:15:23 -08001200
1201 /*
1202 * in the very unlikely event that allocation failed and no
1203 * requests for this direction was pending, mark us starved so that
1204 * freeing of a request in the other direction will notice
1205 * us. another possible fix would be to split the rq mempool into
1206 * READ and WRITE
1207 */
1208rq_starved:
1209 if (unlikely(rl->count[is_sync] == 0))
1210 rl->starved[is_sync] = 1;
Joe Lawrencea492f072014-08-28 08:15:21 -06001211 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Tejun Heoda8303c2011-10-19 14:33:05 +02001214/**
Tejun Heoa06e05e2012-06-04 20:40:55 -07001215 * get_request - get a free request
Tejun Heoda8303c2011-10-19 14:33:05 +02001216 * @q: request_queue to allocate request from
Mike Christiee6a40b02016-06-05 14:32:11 -05001217 * @op: REQ_OP_READ/REQ_OP_WRITE
1218 * @op_flags: rq_flag_bits
Tejun Heoda8303c2011-10-19 14:33:05 +02001219 * @bio: bio to allocate request for (can be %NULL)
Tejun Heoa06e05e2012-06-04 20:40:55 -07001220 * @gfp_mask: allocation mask
Nick Piggind6344532005-06-28 20:45:14 -07001221 *
Mel Gormand0164ad2015-11-06 16:28:21 -08001222 * Get a free request from @q. If %__GFP_DIRECT_RECLAIM is set in @gfp_mask,
1223 * this function keeps retrying under memory pressure and fails iff @q is dead.
Tejun Heoda8303c2011-10-19 14:33:05 +02001224 *
Masanari Iidada3dae52014-09-09 01:27:23 +09001225 * Must be called with @q->queue_lock held and,
Joe Lawrencea492f072014-08-28 08:15:21 -06001226 * Returns ERR_PTR on failure, with @q->queue_lock held.
1227 * Returns request pointer on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 */
Mike Christiee6a40b02016-06-05 14:32:11 -05001229static struct request *get_request(struct request_queue *q, int op,
1230 int op_flags, struct bio *bio,
1231 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Mike Christied9d8c5c2016-06-05 14:32:16 -05001233 const bool is_sync = rw_is_sync(op, op_flags) != 0;
Tejun Heoa06e05e2012-06-04 20:40:55 -07001234 DEFINE_WAIT(wait);
Tejun Heoa0516612012-06-26 15:05:44 -07001235 struct request_list *rl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 struct request *rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001237
1238 rl = blk_get_rl(q, bio); /* transferred to @rq on success */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001239retry:
Mike Christiee6a40b02016-06-05 14:32:11 -05001240 rq = __get_request(rl, op, op_flags, bio, gfp_mask);
Joe Lawrencea492f072014-08-28 08:15:21 -06001241 if (!IS_ERR(rq))
Tejun Heoa06e05e2012-06-04 20:40:55 -07001242 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Mel Gormand0164ad2015-11-06 16:28:21 -08001244 if (!gfpflags_allow_blocking(gfp_mask) || unlikely(blk_queue_dying(q))) {
Tejun Heoa0516612012-06-26 15:05:44 -07001245 blk_put_rl(rl);
Joe Lawrencea492f072014-08-28 08:15:21 -06001246 return rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Tejun Heoa06e05e2012-06-04 20:40:55 -07001249 /* wait on @rl and retry */
1250 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
1251 TASK_UNINTERRUPTIBLE);
Tejun Heoda8303c2011-10-19 14:33:05 +02001252
Mike Christiee6a40b02016-06-05 14:32:11 -05001253 trace_block_sleeprq(q, bio, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Tejun Heoa06e05e2012-06-04 20:40:55 -07001255 spin_unlock_irq(q->queue_lock);
1256 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Tejun Heoa06e05e2012-06-04 20:40:55 -07001258 /*
1259 * After sleeping, we become a "batching" process and will be able
1260 * to allocate at least one request, and up to a big batch of them
1261 * for a small period time. See ioc_batching, ioc_set_batching
1262 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001263 ioc_set_batching(q, current->io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Tejun Heoa06e05e2012-06-04 20:40:55 -07001265 spin_lock_irq(q->queue_lock);
1266 finish_wait(&rl->wait[is_sync], &wait);
Jens Axboe2056a782006-03-23 20:00:26 +01001267
Tejun Heoa06e05e2012-06-04 20:40:55 -07001268 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Jens Axboe320ae512013-10-24 09:20:05 +01001271static struct request *blk_old_get_request(struct request_queue *q, int rw,
1272 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 struct request *rq;
1275
1276 BUG_ON(rw != READ && rw != WRITE);
1277
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001278 /* create ioc upfront */
1279 create_io_context(gfp_mask, q->node);
1280
Nick Piggind6344532005-06-28 20:45:14 -07001281 spin_lock_irq(q->queue_lock);
Mike Christiee6a40b02016-06-05 14:32:11 -05001282 rq = get_request(q, rw, 0, NULL, gfp_mask);
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +02001283 if (IS_ERR(rq)) {
Tejun Heoda8303c2011-10-19 14:33:05 +02001284 spin_unlock_irq(q->queue_lock);
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +02001285 return rq;
1286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +02001288 /* q->queue_lock is unlocked at this point */
1289 rq->__data_len = 0;
1290 rq->__sector = (sector_t) -1;
1291 rq->bio = rq->biotail = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return rq;
1293}
Jens Axboe320ae512013-10-24 09:20:05 +01001294
1295struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
1296{
1297 if (q->mq_ops)
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001298 return blk_mq_alloc_request(q, rw,
1299 (gfp_mask & __GFP_DIRECT_RECLAIM) ?
1300 0 : BLK_MQ_REQ_NOWAIT);
Jens Axboe320ae512013-10-24 09:20:05 +01001301 else
1302 return blk_old_get_request(q, rw, gfp_mask);
1303}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304EXPORT_SYMBOL(blk_get_request);
1305
1306/**
Masanari Iidada3dae52014-09-09 01:27:23 +09001307 * blk_rq_set_block_pc - initialize a request to type BLOCK_PC
Jens Axboef27b0872014-06-06 07:57:37 -06001308 * @rq: request to be initialized
1309 *
1310 */
1311void blk_rq_set_block_pc(struct request *rq)
1312{
1313 rq->cmd_type = REQ_TYPE_BLOCK_PC;
Jens Axboef27b0872014-06-06 07:57:37 -06001314 memset(rq->__cmd, 0, sizeof(rq->__cmd));
Jens Axboef27b0872014-06-06 07:57:37 -06001315}
1316EXPORT_SYMBOL(blk_rq_set_block_pc);
1317
1318/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 * blk_requeue_request - put a request back on queue
1320 * @q: request queue where request should be inserted
1321 * @rq: request to be inserted
1322 *
1323 * Description:
1324 * Drivers often keep queueing requests until the hardware cannot accept
1325 * more, when that condition happens we need to put the request back
1326 * on the queue. Must be called with queue lock held.
1327 */
Jens Axboe165125e2007-07-24 09:28:11 +02001328void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001330 blk_delete_timer(rq);
1331 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001332 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001333
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001334 if (rq->cmd_flags & REQ_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{
1396 if (rq->q->dev && !(rq->cmd_flags & REQ_PM) && !--rq->q->nr_pending)
1397 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{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (unlikely(!q))
1409 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Christoph Hellwig6f5ba582014-02-07 10:22:37 -08001411 if (q->mq_ops) {
1412 blk_mq_free_request(req);
1413 return;
1414 }
1415
Lin Mingc8158812013-03-23 11:42:27 +08001416 blk_pm_put_request(req);
1417
Tejun Heo8922e162005-10-20 16:23:44 +02001418 elv_completed_request(q, req);
1419
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001420 /* this is a bio leak */
1421 WARN_ON(req->bio != NULL);
1422
AnilKumar Chimata7214d7e2017-06-23 03:09:59 -07001423 /* this is a bio leak if the bio is not tagged with BIO_DONTFREE */
1424 WARN_ON(req->bio && !bio_flagged(req->bio, BIO_DONTFREE));
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 /*
1427 * Request may not have originated from ll_rw_blk. if not,
1428 * it didn't come out of our reserved rq pools
1429 */
Jens Axboe49171e52006-08-10 08:59:11 +02001430 if (req->cmd_flags & REQ_ALLOCED) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001431 unsigned int flags = req->cmd_flags;
Mike Christiee6a40b02016-06-05 14:32:11 -05001432 int op = req_op(req);
Tejun Heoa0516612012-06-26 15:05:44 -07001433 struct request_list *rl = blk_rq_rl(req);
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);
Mike Christiee6a40b02016-06-05 14:32:11 -05001439 freed_request(rl, op, flags);
Tejun Heoa0516612012-06-26 15:05:44 -07001440 blk_put_rl(rl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 }
1442}
Mike Christie6e39b69e2005-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
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001461/**
1462 * blk_add_request_payload - add a payload to a request
1463 * @rq: request to update
1464 * @page: page backing the payload
Ming Lin37e58232016-03-22 00:24:44 -07001465 * @offset: offset in page
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001466 * @len: length of the payload.
1467 *
1468 * This allows to later add a payload to an already submitted request by
1469 * a block driver. The driver needs to take care of freeing the payload
1470 * itself.
1471 *
1472 * Note that this is a quite horrible hack and nothing but handling of
1473 * discard requests should ever use it.
1474 */
1475void blk_add_request_payload(struct request *rq, struct page *page,
Ming Lin37e58232016-03-22 00:24:44 -07001476 int offset, unsigned int len)
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001477{
1478 struct bio *bio = rq->bio;
1479
1480 bio->bi_io_vec->bv_page = page;
Ming Lin37e58232016-03-22 00:24:44 -07001481 bio->bi_io_vec->bv_offset = offset;
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001482 bio->bi_io_vec->bv_len = len;
1483
Kent Overstreet4f024f32013-10-11 15:44:27 -07001484 bio->bi_iter.bi_size = len;
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001485 bio->bi_vcnt = 1;
1486 bio->bi_phys_segments = 1;
1487
1488 rq->__data_len = rq->resid_len = len;
1489 rq->nr_phys_segments = 1;
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001490}
1491EXPORT_SYMBOL_GPL(blk_add_request_payload);
1492
Jens Axboe320ae512013-10-24 09:20:05 +01001493bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1494 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001495{
Jens Axboe1eff9d32016-08-05 15:35:16 -06001496 const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001497
Jens Axboe73c10102011-03-08 13:19:51 +01001498 if (!ll_back_merge_fn(q, req, bio))
1499 return false;
1500
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001501 trace_block_bio_backmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001502
1503 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1504 blk_rq_set_mixed_merge(req);
1505
1506 req->biotail->bi_next = bio;
1507 req->biotail = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001508 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001509 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1510
Jens Axboe320ae512013-10-24 09:20:05 +01001511 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001512 return true;
1513}
1514
Jens Axboe320ae512013-10-24 09:20:05 +01001515bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
1516 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001517{
Jens Axboe1eff9d32016-08-05 15:35:16 -06001518 const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001519
Jens Axboe73c10102011-03-08 13:19:51 +01001520 if (!ll_front_merge_fn(q, req, bio))
1521 return false;
1522
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001523 trace_block_bio_frontmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001524
1525 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1526 blk_rq_set_mixed_merge(req);
1527
Jens Axboe73c10102011-03-08 13:19:51 +01001528 bio->bi_next = req->bio;
1529 req->bio = bio;
1530
Jaegeuk Kim5190c812018-04-22 16:05:02 -07001531#ifdef CONFIG_PFK
Jaegeuk Kim84680312018-01-03 21:22:07 -08001532 WARN_ON(req->__dun || bio->bi_iter.bi_dun);
Jaegeuk Kim5190c812018-04-22 16:05:02 -07001533#endif
Kent Overstreet4f024f32013-10-11 15:44:27 -07001534 req->__sector = bio->bi_iter.bi_sector;
1535 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001536 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1537
Jens Axboe320ae512013-10-24 09:20:05 +01001538 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001539 return true;
1540}
1541
Tejun Heobd87b582011-10-19 14:33:08 +02001542/**
Jens Axboe320ae512013-10-24 09:20:05 +01001543 * blk_attempt_plug_merge - try to merge with %current's plugged list
Tejun Heobd87b582011-10-19 14:33:08 +02001544 * @q: request_queue new bio is being queued at
1545 * @bio: new bio being queued
1546 * @request_count: out parameter for number of traversed plugged requests
Randy Dunlapccc26002015-10-30 18:36:16 -07001547 * @same_queue_rq: pointer to &struct request that gets filled in when
1548 * another request associated with @q is found on the plug list
1549 * (optional, may be %NULL)
Tejun Heobd87b582011-10-19 14:33:08 +02001550 *
1551 * Determine whether @bio being queued on @q can be merged with a request
1552 * on %current's plugged list. Returns %true if merge was successful,
1553 * otherwise %false.
1554 *
Tejun Heo07c2bd32012-02-08 09:19:42 +01001555 * Plugging coalesces IOs from the same issuer for the same purpose without
1556 * going through @q->queue_lock. As such it's more of an issuing mechanism
1557 * than scheduling, and the request, while may have elvpriv data, is not
1558 * added on the elevator at this point. In addition, we don't have
1559 * reliable access to the elevator outside queue lock. Only check basic
1560 * merging parameters without querying the elevator.
Robert Elliottda41a582014-05-20 16:46:26 -05001561 *
1562 * Caller must ensure !blk_queue_nomerges(q) beforehand.
Jens Axboe73c10102011-03-08 13:19:51 +01001563 */
Jens Axboe320ae512013-10-24 09:20:05 +01001564bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
Shaohua Li5b3f3412015-05-08 10:51:33 -07001565 unsigned int *request_count,
1566 struct request **same_queue_rq)
Jens Axboe73c10102011-03-08 13:19:51 +01001567{
1568 struct blk_plug *plug;
1569 struct request *rq;
1570 bool ret = false;
Shaohua Li92f399c2013-10-29 12:01:03 -06001571 struct list_head *plug_list;
Jens Axboe73c10102011-03-08 13:19:51 +01001572
Tejun Heobd87b582011-10-19 14:33:08 +02001573 plug = current->plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001574 if (!plug)
1575 goto out;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001576 *request_count = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01001577
Shaohua Li92f399c2013-10-29 12:01:03 -06001578 if (q->mq_ops)
1579 plug_list = &plug->mq_list;
1580 else
1581 plug_list = &plug->list;
1582
1583 list_for_each_entry_reverse(rq, plug_list, queuelist) {
Jens Axboe73c10102011-03-08 13:19:51 +01001584 int el_ret;
1585
Shaohua Li5b3f3412015-05-08 10:51:33 -07001586 if (rq->q == q) {
Shaohua Li1b2e19f2012-04-06 11:37:47 -06001587 (*request_count)++;
Shaohua Li5b3f3412015-05-08 10:51:33 -07001588 /*
1589 * Only blk-mq multiple hardware queues case checks the
1590 * rq in the same queue, there should be only one such
1591 * rq in a queue
1592 **/
1593 if (same_queue_rq)
1594 *same_queue_rq = rq;
1595 }
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001596
Tejun Heo07c2bd32012-02-08 09:19:42 +01001597 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
Jens Axboe73c10102011-03-08 13:19:51 +01001598 continue;
1599
Tejun Heo050c8ea2012-02-08 09:19:38 +01001600 el_ret = blk_try_merge(rq, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001601 if (el_ret == ELEVATOR_BACK_MERGE) {
1602 ret = bio_attempt_back_merge(q, rq, bio);
1603 if (ret)
1604 break;
1605 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
1606 ret = bio_attempt_front_merge(q, rq, bio);
1607 if (ret)
1608 break;
1609 }
1610 }
1611out:
1612 return ret;
1613}
1614
Jeff Moyer0809e3a2015-10-20 23:13:51 +08001615unsigned int blk_plug_queued_count(struct request_queue *q)
1616{
1617 struct blk_plug *plug;
1618 struct request *rq;
1619 struct list_head *plug_list;
1620 unsigned int ret = 0;
1621
1622 plug = current->plug;
1623 if (!plug)
1624 goto out;
1625
1626 if (q->mq_ops)
1627 plug_list = &plug->mq_list;
1628 else
1629 plug_list = &plug->list;
1630
1631 list_for_each_entry(rq, plug_list, queuelist) {
1632 if (rq->q == q)
1633 ret++;
1634 }
1635out:
1636 return ret;
1637}
1638
Jens Axboe86db1e22008-01-29 14:53:40 +01001639void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001640{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001641 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001642
Jens Axboe1eff9d32016-08-05 15:35:16 -06001643 req->cmd_flags |= bio->bi_opf & REQ_COMMON_MASK;
1644 if (bio->bi_opf & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001645 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001646
Tejun Heo52d9e672006-01-06 09:49:58 +01001647 req->errors = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001648 req->__sector = bio->bi_iter.bi_sector;
Jaegeuk Kim5190c812018-04-22 16:05:02 -07001649#ifdef CONFIG_PFK
Jaegeuk Kim84680312018-01-03 21:22:07 -08001650 req->__dun = bio->bi_iter.bi_dun;
Jaegeuk Kim5190c812018-04-22 16:05:02 -07001651#endif
Tejun Heo52d9e672006-01-06 09:49:58 +01001652 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001653 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001654}
1655
Jens Axboedece1632015-11-05 10:41:16 -07001656static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Jens Axboe1eff9d32016-08-05 15:35:16 -06001658 const bool sync = !!(bio->bi_opf & REQ_SYNC);
Jens Axboe73c10102011-03-08 13:19:51 +01001659 struct blk_plug *plug;
Mike Christiee6a40b02016-06-05 14:32:11 -05001660 int el_ret, rw_flags = 0, where = ELEVATOR_INSERT_SORT;
Jens Axboe73c10102011-03-08 13:19:51 +01001661 struct request *req;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001662 unsigned int request_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /*
1665 * low level driver can indicate that it wants pages above a
1666 * certain limit bounced to low memory (ie for highmem, or even
1667 * ISA dma in theory)
1668 */
1669 blk_queue_bounce(q, &bio);
1670
Junichi Nomura23688bf2015-12-22 10:23:44 -07001671 blk_queue_split(q, &bio, q->bio_split);
1672
Darrick J. Wongffecfd12013-02-21 16:42:55 -08001673 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001674 bio->bi_error = -EIO;
1675 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001676 return BLK_QC_T_NONE;
Darrick J. Wongffecfd12013-02-21 16:42:55 -08001677 }
1678
Jens Axboe1eff9d32016-08-05 15:35:16 -06001679 if (bio->bi_opf & (REQ_PREFLUSH | REQ_FUA)) {
Jens Axboe73c10102011-03-08 13:19:51 +01001680 spin_lock_irq(q->queue_lock);
Tejun Heoae1b1532011-01-25 12:43:54 +01001681 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001682 goto get_rq;
1683 }
1684
Jens Axboe73c10102011-03-08 13:19:51 +01001685 /*
1686 * Check if we can merge with the plugged list before grabbing
1687 * any locks.
1688 */
Jeff Moyer0809e3a2015-10-20 23:13:51 +08001689 if (!blk_queue_nomerges(q)) {
1690 if (blk_attempt_plug_merge(q, bio, &request_count, NULL))
Jens Axboedece1632015-11-05 10:41:16 -07001691 return BLK_QC_T_NONE;
Jeff Moyer0809e3a2015-10-20 23:13:51 +08001692 } else
1693 request_count = blk_plug_queued_count(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001694
1695 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 el_ret = elv_merge(q, &req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001698 if (el_ret == ELEVATOR_BACK_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001699 if (bio_attempt_back_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001700 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001701 if (!attempt_back_merge(q, req))
1702 elv_merged_request(q, req, el_ret);
1703 goto out_unlock;
Tejun Heo80a761f2009-07-03 17:48:17 +09001704 }
Jens Axboe73c10102011-03-08 13:19:51 +01001705 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001706 if (bio_attempt_front_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001707 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001708 if (!attempt_front_merge(q, req))
1709 elv_merged_request(q, req, el_ret);
1710 goto out_unlock;
1711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001715 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001716 * This sync check and mask will be re-done in init_request_from_bio(),
1717 * but we need to set it earlier to expose the sync flag to the
1718 * rq allocator and io schedulers.
1719 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001720 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001721 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001722
1723 /*
Jens Axboeb8269db2016-06-09 15:47:29 -06001724 * Add in META/PRIO flags, if set, before we get to the IO scheduler
1725 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06001726 rw_flags |= (bio->bi_opf & (REQ_META | REQ_PRIO));
Jens Axboeb8269db2016-06-09 15:47:29 -06001727
1728 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001729 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001730 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001731 */
Mike Christiee6a40b02016-06-05 14:32:11 -05001732 req = get_request(q, bio_data_dir(bio), rw_flags, bio, GFP_NOIO);
Joe Lawrencea492f072014-08-28 08:15:21 -06001733 if (IS_ERR(req)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001734 bio->bi_error = PTR_ERR(req);
1735 bio_endio(bio);
Tejun Heoda8303c2011-10-19 14:33:05 +02001736 goto out_unlock;
1737 }
Nick Piggind6344532005-06-28 20:45:14 -07001738
Nick Piggin450991b2005-06-28 20:45:13 -07001739 /*
1740 * After dropping the lock and possibly sleeping here, our request
1741 * may now be mergeable after it had proven unmergeable (above).
1742 * We don't worry about that case for efficiency. It won't happen
1743 * often, and the elevators are able to handle it.
1744 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001745 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Tao Ma9562ad92011-10-24 16:11:30 +02001747 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
Jens Axboe11ccf112011-07-26 15:01:15 +02001748 req->cpu = raw_smp_processor_id();
Tejun Heodd831002010-09-03 11:56:16 +02001749
Jens Axboe73c10102011-03-08 13:19:51 +01001750 plug = current->plug;
Jens Axboe721a9602011-03-09 11:56:30 +01001751 if (plug) {
Jens Axboedc6d36c2011-04-12 10:28:28 +02001752 /*
1753 * If this is the first request added after a plug, fire
Jianpeng Ma7aef2e72013-09-11 13:21:07 -06001754 * of a plug trace.
Jens Axboedc6d36c2011-04-12 10:28:28 +02001755 */
Jianpeng Ma7aef2e72013-09-11 13:21:07 -06001756 if (!request_count)
Jens Axboedc6d36c2011-04-12 10:28:28 +02001757 trace_block_plug(q);
Shaohua Li3540d5e2011-11-16 09:21:50 +01001758 else {
Shaohua Li019ceb72011-11-16 09:21:50 +01001759 if (request_count >= BLK_MAX_REQUEST_COUNT) {
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
Jens Axboebf2de6f2007-09-27 13:01:25 +02001784 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001786
Kent Overstreet4f024f32013-10-11 15:44:27 -07001787 bio->bi_iter.bi_sector += p->start_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001789
Mike Snitzerd07335e2010-11-16 12:52:38 +01001790 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1791 bdev->bd_dev,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001792 bio->bi_iter.bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794}
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796static void handle_bad_sector(struct bio *bio)
1797{
1798 char b[BDEVNAME_SIZE];
1799
1800 printk(KERN_INFO "attempt to access beyond end of device\n");
Mike Christie6296b962016-06-05 14:32:21 -05001801 printk(KERN_INFO "%s: rw=%d, want=%Lu, limit=%Lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 bdevname(bio->bi_bdev, b),
Jens Axboe1eff9d32016-08-05 15:35:16 -06001803 bio->bi_opf,
Kent Overstreetf73a1c72012-09-25 15:05:12 -07001804 (unsigned long long)bio_end_sector(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001805 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
Akinobu Mitac17bb492006-12-08 02:39:46 -08001808#ifdef CONFIG_FAIL_MAKE_REQUEST
1809
1810static DECLARE_FAULT_ATTR(fail_make_request);
1811
1812static int __init setup_fail_make_request(char *str)
1813{
1814 return setup_fault_attr(&fail_make_request, str);
1815}
1816__setup("fail_make_request=", setup_fail_make_request);
1817
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001818static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001819{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001820 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001821}
1822
1823static int __init fail_make_request_debugfs(void)
1824{
Akinobu Mitadd48c082011-08-03 16:21:01 -07001825 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1826 NULL, &fail_make_request);
1827
Duan Jiong21f9fcd2014-04-11 15:58:56 +08001828 return PTR_ERR_OR_ZERO(dir);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001829}
1830
1831late_initcall(fail_make_request_debugfs);
1832
1833#else /* CONFIG_FAIL_MAKE_REQUEST */
1834
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001835static inline bool should_fail_request(struct hd_struct *part,
1836 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001837{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001838 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001839}
1840
1841#endif /* CONFIG_FAIL_MAKE_REQUEST */
1842
Jens Axboec07e2b42007-07-18 13:27:58 +02001843/*
1844 * Check whether this bio extends beyond the end of the device.
1845 */
1846static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1847{
1848 sector_t maxsector;
1849
1850 if (!nr_sectors)
1851 return 0;
1852
1853 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001854 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001855 if (maxsector) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07001856 sector_t sector = bio->bi_iter.bi_sector;
Jens Axboec07e2b42007-07-18 13:27:58 +02001857
1858 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1859 /*
1860 * This may well happen - the kernel calls bread()
1861 * without checking the size of the device, e.g., when
1862 * mounting a device.
1863 */
1864 handle_bad_sector(bio);
1865 return 1;
1866 }
1867 }
1868
1869 return 0;
1870}
1871
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001872static noinline_for_stack bool
1873generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Jens Axboe165125e2007-07-24 09:28:11 +02001875 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001876 int nr_sectors = bio_sectors(bio);
Jens Axboe51fd77b2007-11-02 08:49:08 +01001877 int err = -EIO;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001878 char b[BDEVNAME_SIZE];
1879 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Jens Axboec07e2b42007-07-18 13:27:58 +02001883 if (bio_check_eod(bio, nr_sectors))
1884 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001886 q = bdev_get_queue(bio->bi_bdev);
1887 if (unlikely(!q)) {
1888 printk(KERN_ERR
1889 "generic_make_request: Trying to access "
1890 "nonexistent block-device %s (%Lu)\n",
1891 bdevname(bio->bi_bdev, b),
Kent Overstreet4f024f32013-10-11 15:44:27 -07001892 (long long) bio->bi_iter.bi_sector);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001893 goto end_io;
1894 }
1895
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001896 part = bio->bi_bdev->bd_part;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001897 if (should_fail_request(part, bio->bi_iter.bi_size) ||
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001898 should_fail_request(&part_to_disk(part)->part0,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001899 bio->bi_iter.bi_size))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001900 goto end_io;
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /*
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001903 * If this device has partitions, remap block n
1904 * of partition p to block n+start(p) of the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001906 blk_partition_remap(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001908 if (bio_check_eod(bio, nr_sectors))
1909 goto end_io;
1910
1911 /*
1912 * Filter flush bio's early so that make_request based
1913 * drivers without flush support don't have to worry
1914 * about them.
1915 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06001916 if ((bio->bi_opf & (REQ_PREFLUSH | REQ_FUA)) &&
Jens Axboec888a8f2016-04-13 13:33:19 -06001917 !test_bit(QUEUE_FLAG_WC, &q->queue_flags)) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001918 bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001919 if (!nr_sectors) {
1920 err = 0;
Tejun Heoa7384672008-11-28 13:32:03 +09001921 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Christoph Hellwig288dab82016-06-09 16:00:36 +02001925 switch (bio_op(bio)) {
1926 case REQ_OP_DISCARD:
1927 if (!blk_queue_discard(q))
1928 goto not_supported;
1929 break;
1930 case REQ_OP_SECURE_ERASE:
1931 if (!blk_queue_secure_erase(q))
1932 goto not_supported;
1933 break;
1934 case REQ_OP_WRITE_SAME:
1935 if (!bdev_write_same(bio->bi_bdev))
1936 goto not_supported;
1937 break;
1938 default:
1939 break;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001942 /*
1943 * Various block parts want %current->io_context and lazy ioc
1944 * allocation ends up trading a lot of pain for a small amount of
1945 * memory. Just allocate it upfront. This may fail and block
1946 * layer knows how to live with it.
1947 */
1948 create_io_context(GFP_ATOMIC, q->node);
1949
Tejun Heoae118892015-08-18 14:55:20 -07001950 if (!blkcg_bio_issue_check(q, bio))
1951 return false;
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001952
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001953 trace_block_bio_queue(q, bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001954 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001955
Christoph Hellwig288dab82016-06-09 16:00:36 +02001956not_supported:
1957 err = -EOPNOTSUPP;
Tejun Heoa7384672008-11-28 13:32:03 +09001958end_io:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001959 bio->bi_error = err;
1960 bio_endio(bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001961 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001964/**
1965 * generic_make_request - hand a buffer to its device driver for I/O
1966 * @bio: The bio describing the location in memory and on the device.
1967 *
1968 * generic_make_request() is used to make I/O requests of block
1969 * devices. It is passed a &struct bio, which describes the I/O that needs
1970 * to be done.
1971 *
1972 * generic_make_request() does not return any status. The
1973 * success/failure status of the request, along with notification of
1974 * completion, is delivered asynchronously through the bio->bi_end_io
1975 * function described (one day) else where.
1976 *
1977 * The caller of generic_make_request must make sure that bi_io_vec
1978 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1979 * set to describe the device address, and the
1980 * bi_end_io and optionally bi_private are set to describe how
1981 * completion notification should be signaled.
1982 *
1983 * generic_make_request and the drivers it calls may use bi_next if this
1984 * bio happens to be merged with someone else, and may resubmit the bio to
1985 * a lower device by calling into generic_make_request recursively, which
1986 * means the bio should NOT be touched after the call to ->make_request_fn.
Neil Brownd89d8792007-05-01 09:53:42 +02001987 */
Jens Axboedece1632015-11-05 10:41:16 -07001988blk_qc_t generic_make_request(struct bio *bio)
Neil Brownd89d8792007-05-01 09:53:42 +02001989{
NeilBrown5959cde2017-03-10 17:00:47 +11001990 /*
1991 * bio_list_on_stack[0] contains bios submitted by the current
1992 * make_request_fn.
1993 * bio_list_on_stack[1] contains bios that were submitted before
1994 * the current make_request_fn, but that haven't been processed
1995 * yet.
1996 */
1997 struct bio_list bio_list_on_stack[2];
Jens Axboedece1632015-11-05 10:41:16 -07001998 blk_qc_t ret = BLK_QC_T_NONE;
Akinobu Mitabddd87c2010-02-23 08:55:42 +01001999
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002000 if (!generic_make_request_checks(bio))
Jens Axboedece1632015-11-05 10:41:16 -07002001 goto out;
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002002
2003 /*
2004 * We only want one ->make_request_fn to be active at a time, else
2005 * stack usage with stacked devices could be a problem. So use
2006 * current->bio_list to keep a list of requests submited by a
2007 * make_request_fn function. current->bio_list is also used as a
2008 * flag to say if generic_make_request is currently active in this
2009 * task or not. If it is NULL, then no make_request is active. If
2010 * it is non-NULL, then a make_request is active, and new requests
2011 * should be added at the tail
2012 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002013 if (current->bio_list) {
NeilBrown5959cde2017-03-10 17:00:47 +11002014 bio_list_add(&current->bio_list[0], bio);
Jens Axboedece1632015-11-05 10:41:16 -07002015 goto out;
Neil Brownd89d8792007-05-01 09:53:42 +02002016 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002017
Neil Brownd89d8792007-05-01 09:53:42 +02002018 /* following loop may be a bit non-obvious, and so deserves some
2019 * explanation.
2020 * Before entering the loop, bio->bi_next is NULL (as all callers
2021 * ensure that) so we have a list with a single bio.
2022 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002023 * we assign bio_list to a pointer to the bio_list_on_stack,
2024 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002025 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02002026 * through a recursive call to generic_make_request. If it
2027 * did, we find a non-NULL value in bio_list and re-enter the loop
2028 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002029 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002030 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02002031 */
2032 BUG_ON(bio->bi_next);
NeilBrown5959cde2017-03-10 17:00:47 +11002033 bio_list_init(&bio_list_on_stack[0]);
2034 current->bio_list = bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02002035 do {
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002036 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
2037
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01002038 if (likely(blk_queue_enter(q, false) == 0)) {
NeilBrownd5986e02017-03-08 07:38:05 +11002039 struct bio_list lower, same;
2040
2041 /* Create a fresh bio_list for all subordinate requests */
NeilBrown5959cde2017-03-10 17:00:47 +11002042 bio_list_on_stack[1] = bio_list_on_stack[0];
2043 bio_list_init(&bio_list_on_stack[0]);
Jens Axboedece1632015-11-05 10:41:16 -07002044 ret = q->make_request_fn(q, bio);
Dan Williams3ef28e82015-10-21 13:20:12 -04002045
2046 blk_queue_exit(q);
2047
NeilBrownd5986e02017-03-08 07:38:05 +11002048 /* sort new bios into those for a lower level
2049 * and those for the same level
2050 */
2051 bio_list_init(&lower);
2052 bio_list_init(&same);
NeilBrown5959cde2017-03-10 17:00:47 +11002053 while ((bio = bio_list_pop(&bio_list_on_stack[0])) != NULL)
NeilBrownd5986e02017-03-08 07:38:05 +11002054 if (q == bdev_get_queue(bio->bi_bdev))
2055 bio_list_add(&same, bio);
2056 else
2057 bio_list_add(&lower, bio);
2058 /* now assemble so we handle the lowest level first */
NeilBrown5959cde2017-03-10 17:00:47 +11002059 bio_list_merge(&bio_list_on_stack[0], &lower);
2060 bio_list_merge(&bio_list_on_stack[0], &same);
2061 bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
Dan Williams3ef28e82015-10-21 13:20:12 -04002062 } else {
Dan Williams3ef28e82015-10-21 13:20:12 -04002063 bio_io_error(bio);
Dan Williams3ef28e82015-10-21 13:20:12 -04002064 }
NeilBrown5959cde2017-03-10 17:00:47 +11002065 bio = bio_list_pop(&bio_list_on_stack[0]);
Neil Brownd89d8792007-05-01 09:53:42 +02002066 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002067 current->bio_list = NULL; /* deactivate */
Jens Axboedece1632015-11-05 10:41:16 -07002068
2069out:
2070 return ret;
Neil Brownd89d8792007-05-01 09:53:42 +02002071}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072EXPORT_SYMBOL(generic_make_request);
2073
2074/**
Randy Dunlap710027a2008-08-19 20:13:11 +02002075 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 * @bio: The &struct bio which describes the I/O
2077 *
2078 * submit_bio() is very similar in purpose to generic_make_request(), and
2079 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02002080 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 *
2082 */
Mike Christie4e49ea42016-06-05 14:31:41 -05002083blk_qc_t submit_bio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Johannes Weiner0b0803a2019-08-08 15:03:00 -04002085 bool workingset_read = false;
2086 unsigned long pflags;
2087 blk_qc_t ret;
2088
Jens Axboebf2de6f2007-09-27 13:01:25 +02002089 /*
2090 * If it's a regular read/write or a barrier with data attached,
2091 * go through the normal accounting stuff before submission.
2092 */
Martin K. Petersene2a60da2012-09-18 12:19:25 -04002093 if (bio_has_data(bio)) {
Martin K. Petersen4363ac72012-09-18 12:19:27 -04002094 unsigned int count;
2095
Mike Christie95fe6c12016-06-05 14:31:48 -05002096 if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
Martin K. Petersen4363ac72012-09-18 12:19:27 -04002097 count = bdev_logical_block_size(bio->bi_bdev) >> 9;
2098 else
2099 count = bio_sectors(bio);
2100
Mike Christiea8ebb052016-06-05 14:31:45 -05002101 if (op_is_write(bio_op(bio))) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02002102 count_vm_events(PGPGOUT, count);
2103 } else {
Johannes Weiner0b0803a2019-08-08 15:03:00 -04002104 if (bio_flagged(bio, BIO_WORKINGSET))
2105 workingset_read = true;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002106 task_io_account_read(bio->bi_iter.bi_size);
Jens Axboebf2de6f2007-09-27 13:01:25 +02002107 count_vm_events(PGPGIN, count);
2108 }
2109
2110 if (unlikely(block_dump)) {
2111 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02002112 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002113 current->comm, task_pid_nr(current),
Mike Christiea8ebb052016-06-05 14:31:45 -05002114 op_is_write(bio_op(bio)) ? "WRITE" : "READ",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002115 (unsigned long long)bio->bi_iter.bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02002116 bdevname(bio->bi_bdev, b),
2117 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02002118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
2120
Johannes Weiner0b0803a2019-08-08 15:03:00 -04002121 /*
2122 * If we're reading data that is part of the userspace
2123 * workingset, count submission time as memory stall. When the
2124 * device is congested, or the submitting cgroup IO-throttled,
2125 * submission can be a significant part of overall IO time.
2126 */
2127 if (workingset_read)
2128 psi_memstall_enter(&pflags);
2129
2130 ret = generic_make_request(bio);
2131
2132 if (workingset_read)
2133 psi_memstall_leave(&pflags);
2134
2135 return ret;
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);
Mike Snitzer6acfe682016-02-05 08:49:01 -05002199 blk_mq_insert_request(rq, false, true, 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
Mike Christie28a8f0d2016-06-05 14:32:25 -05002215 if (rq->cmd_flags & (REQ_PREFLUSH | REQ_FUA))
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
2249 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
2250 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 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01002292 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_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 ||
2320 (q->rpm_status != RPM_ACTIVE && !(rq->cmd_flags & REQ_PM))))
2321 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
Tejun Heo158dbda2009-04-23 11:05:18 +09002396 if (!(rq->cmd_flags & REQ_STARTED)) {
2397 /*
2398 * This is the first time the device driver
2399 * sees this request (possibly after
2400 * requeueing). Notify IO scheduler.
2401 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002402 if (rq->cmd_flags & REQ_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 */
2410 rq->cmd_flags |= REQ_STARTED;
2411 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
2419 if (rq->cmd_flags & REQ_DONTPREP)
2420 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
2442 * avoid resource deadlock. REQ_STARTED will
2443 * 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) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09002446 !(rq->cmd_flags & REQ_DONTPREP)) {
2447 /*
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
Tejun Heo158dbda2009-04-23 11:05:18 +09002459 rq->cmd_flags |= REQ_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
2514 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09002515 * We are now handing the request to the hardware, initialize
2516 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09002517 */
Tejun Heo5f49f632009-05-19 18:33:05 +09002518 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02002519 if (unlikely(blk_bidi_rq(req)))
2520 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
2521
Jeff Moyer4912aa62013-10-08 14:36:41 -04002522 BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags));
Tejun Heo9934c8c2009-05-08 11:54:16 +09002523 blk_add_timer(req);
2524}
2525EXPORT_SYMBOL(blk_start_request);
2526
2527/**
2528 * blk_fetch_request - fetch a request from a request queue
2529 * @q: request queue to fetch a request from
2530 *
2531 * Description:
2532 * Return the request at the top of @q. The request is started on
2533 * return and LLD can start processing it immediately.
2534 *
2535 * Return:
2536 * Pointer to the request at the top of @q if available. Null
2537 * otherwise.
2538 *
2539 * Context:
2540 * queue_lock must be held.
2541 */
2542struct request *blk_fetch_request(struct request_queue *q)
2543{
2544 struct request *rq;
2545
2546 rq = blk_peek_request(q);
2547 if (rq)
2548 blk_start_request(rq);
2549 return rq;
2550}
2551EXPORT_SYMBOL(blk_fetch_request);
2552
2553/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002554 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002555 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002556 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002557 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002558 *
2559 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002560 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2561 * the request structure even if @req doesn't have leftover.
2562 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002563 *
2564 * This special helper function is only for request stacking drivers
2565 * (e.g. request-based dm) so that they can handle partial completion.
2566 * Actual device drivers should use blk_end_request instead.
2567 *
2568 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2569 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002570 *
2571 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002572 * %false - this request doesn't have any more data
2573 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002574 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002575bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Kent Overstreetf79ea412012-09-20 16:38:30 -07002577 int total_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Hannes Reinecke4a0efdc2014-10-01 14:32:31 +02002579 trace_block_rq_complete(req->q, req, nr_bytes);
2580
Tejun Heo2e60e022009-04-23 11:05:18 +09002581 if (!req->bio)
2582 return false;
2583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002585 * For fs requests, rq is just carrier of independent bio's
2586 * and each partial completion should be handled separately.
2587 * Reset per-request error on each partial completion.
2588 *
2589 * TODO: tj: This is too subtle. It would be better to let
2590 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002592 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 req->errors = 0;
2594
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002595 if (error && req->cmd_type == REQ_TYPE_FS &&
2596 !(req->cmd_flags & REQ_QUIET)) {
Hannes Reinecke79775562011-01-18 10:13:13 +01002597 char *error_type;
2598
2599 switch (error) {
2600 case -ENOLINK:
2601 error_type = "recoverable transport";
2602 break;
2603 case -EREMOTEIO:
2604 error_type = "critical target";
2605 break;
2606 case -EBADE:
2607 error_type = "critical nexus";
2608 break;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002609 case -ETIMEDOUT:
2610 error_type = "timeout";
2611 break;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +02002612 case -ENOSPC:
2613 error_type = "critical space allocation";
2614 break;
Hannes Reinecke7e782af2013-07-01 15:16:26 +02002615 case -ENODATA:
2616 error_type = "critical medium";
2617 break;
Hannes Reinecke79775562011-01-18 10:13:13 +01002618 case -EIO:
2619 default:
2620 error_type = "I/O";
2621 break;
2622 }
Robert Elliottef3ecb62014-08-27 10:50:31 -05002623 printk_ratelimited(KERN_ERR "%s: %s error, dev %s, sector %llu\n",
2624 __func__, error_type, req->rq_disk ?
Yi Zou37d7b342012-08-30 16:26:25 -07002625 req->rq_disk->disk_name : "?",
2626 (unsigned long long)blk_rq_pos(req));
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 }
2629
Jens Axboebc58ba92009-01-23 10:54:44 +01002630 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002631
Kent Overstreetf79ea412012-09-20 16:38:30 -07002632 total_bytes = 0;
AnilKumar Chimata7214d7e2017-06-23 03:09:59 -07002633
2634 /*
2635 * Check for this if flagged, Req based dm needs to perform
2636 * post processing, hence dont end bios or request.DM
2637 * layer takes care.
2638 */
2639 if (bio_flagged(req->bio, BIO_DONTFREE))
2640 return false;
2641
Kent Overstreetf79ea412012-09-20 16:38:30 -07002642 while (req->bio) {
2643 struct bio *bio = req->bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002644 unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
Kent Overstreet4f024f32013-10-11 15:44:27 -07002646 if (bio_bytes == bio->bi_iter.bi_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 req->bio = bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Kent Overstreetf79ea412012-09-20 16:38:30 -07002649 req_bio_endio(req, bio, bio_bytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Kent Overstreetf79ea412012-09-20 16:38:30 -07002651 total_bytes += bio_bytes;
2652 nr_bytes -= bio_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Kent Overstreetf79ea412012-09-20 16:38:30 -07002654 if (!nr_bytes)
2655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 }
2657
2658 /*
2659 * completely done
2660 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002661 if (!req->bio) {
2662 /*
2663 * Reset counters so that the request stacking driver
2664 * can find how many bytes remain in the request
2665 * later.
2666 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002667 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002668 return false;
2669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002671 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002672
2673 /* update sector only for requests with clear definition of sector */
Jaegeuk Kim84680312018-01-03 21:22:07 -08002674 if (req->cmd_type == REQ_TYPE_FS) {
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002675 req->__sector += total_bytes >> 9;
Jaegeuk Kim5190c812018-04-22 16:05:02 -07002676#ifdef CONFIG_PFK
Jaegeuk Kim84680312018-01-03 21:22:07 -08002677 if (req->__dun)
2678 req->__dun += total_bytes >> 12;
Jaegeuk Kim5190c812018-04-22 16:05:02 -07002679#endif
Jaegeuk Kim84680312018-01-03 21:22:07 -08002680 }
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002681
Tejun Heo80a761f2009-07-03 17:48:17 +09002682 /* mixed attributes always follow the first bio */
2683 if (req->cmd_flags & REQ_MIXED_MERGE) {
2684 req->cmd_flags &= ~REQ_FAILFAST_MASK;
Jens Axboe1eff9d32016-08-05 15:35:16 -06002685 req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK;
Tejun Heo80a761f2009-07-03 17:48:17 +09002686 }
2687
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002688 /*
2689 * If total number of sectors is less than the first segment
2690 * size, something has gone terribly wrong.
2691 */
2692 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
Jens Axboe81829242011-03-30 09:51:33 +02002693 blk_dump_rq_flags(req, "request botched");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002694 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002695 }
2696
2697 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002699
Tejun Heo2e60e022009-04-23 11:05:18 +09002700 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
Tejun Heo2e60e022009-04-23 11:05:18 +09002702EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Tejun Heo2e60e022009-04-23 11:05:18 +09002704static bool blk_update_bidi_request(struct request *rq, int error,
2705 unsigned int nr_bytes,
2706 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002707{
Tejun Heo2e60e022009-04-23 11:05:18 +09002708 if (blk_update_request(rq, error, nr_bytes))
2709 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002710
Tejun Heo2e60e022009-04-23 11:05:18 +09002711 /* Bidi request must be completed as a whole */
2712 if (unlikely(blk_bidi_rq(rq)) &&
2713 blk_update_request(rq->next_rq, error, bidi_bytes))
2714 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002715
Jens Axboee2e1a142010-06-09 10:42:09 +02002716 if (blk_queue_add_random(rq->q))
2717 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002718
2719 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720}
2721
James Bottomley28018c22010-07-01 19:49:17 +09002722/**
2723 * blk_unprep_request - unprepare a request
2724 * @req: the request
2725 *
2726 * This function makes a request ready for complete resubmission (or
2727 * completion). It happens only after all error handling is complete,
2728 * so represents the appropriate moment to deallocate any resources
2729 * that were allocated to the request in the prep_rq_fn. The queue
2730 * lock is held when calling this.
2731 */
2732void blk_unprep_request(struct request *req)
2733{
2734 struct request_queue *q = req->q;
2735
2736 req->cmd_flags &= ~REQ_DONTPREP;
2737 if (q->unprep_rq_fn)
2738 q->unprep_rq_fn(q, req);
2739}
2740EXPORT_SYMBOL_GPL(blk_unprep_request);
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742/*
2743 * queue lock must be held
2744 */
Christoph Hellwig12120072014-04-16 09:44:59 +02002745void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
Christoph Hellwig125c99b2014-11-03 12:47:47 +01002747 if (req->cmd_flags & REQ_QUEUED)
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002748 blk_queue_end_tag(req->q, req);
2749
James Bottomleyba396a62009-05-27 14:17:08 +02002750 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002752 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Jan Kara1cdb7f92017-02-02 15:56:50 +01002753 laptop_io_completion(req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Mike Andersone78042e2008-10-30 02:16:20 -07002755 blk_delete_timer(req);
2756
James Bottomley28018c22010-07-01 19:49:17 +09002757 if (req->cmd_flags & REQ_DONTPREP)
2758 blk_unprep_request(req);
2759
Jens Axboebc58ba92009-01-23 10:54:44 +01002760 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002763 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002764 else {
2765 if (blk_bidi_rq(req))
2766 __blk_put_request(req->next_rq->q, req->next_rq);
2767
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 }
2770}
Christoph Hellwig12120072014-04-16 09:44:59 +02002771EXPORT_SYMBOL(blk_finish_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002773/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002774 * blk_end_bidi_request - Complete a bidi request
2775 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002776 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002777 * @nr_bytes: number of bytes to complete @rq
2778 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002779 *
2780 * Description:
2781 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002782 * Drivers that supports bidi can safely call this member for any
2783 * type of request, bidi or uni. In the later case @bidi_bytes is
2784 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002785 *
2786 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002787 * %false - we are done with this request
2788 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002789 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002790static bool blk_end_bidi_request(struct request *rq, int error,
2791 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002792{
2793 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002794 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002795
Tejun Heo2e60e022009-04-23 11:05:18 +09002796 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2797 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002798
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002799 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002800 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002801 spin_unlock_irqrestore(q->queue_lock, flags);
2802
Tejun Heo2e60e022009-04-23 11:05:18 +09002803 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002804}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002805
2806/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002807 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2808 * @rq: the request to complete
2809 * @error: %0 for success, < %0 for error
2810 * @nr_bytes: number of bytes to complete @rq
2811 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002812 *
2813 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002814 * Identical to blk_end_bidi_request() except that queue lock is
2815 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002816 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002817 * Return:
2818 * %false - we are done with this request
2819 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002820 **/
Jeff Moyer4853aba2011-08-15 21:37:25 +02002821bool __blk_end_bidi_request(struct request *rq, int error,
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002822 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002823{
Tejun Heo2e60e022009-04-23 11:05:18 +09002824 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2825 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002826
Tejun Heo2e60e022009-04-23 11:05:18 +09002827 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002828
Tejun Heo2e60e022009-04-23 11:05:18 +09002829 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002830}
2831
2832/**
2833 * blk_end_request - Helper function for drivers to complete the request.
2834 * @rq: the request being processed
2835 * @error: %0 for success, < %0 for error
2836 * @nr_bytes: number of bytes to complete
2837 *
2838 * Description:
2839 * Ends I/O on a number of bytes attached to @rq.
2840 * If @rq has leftover, sets it up for the next range of segments.
2841 *
2842 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002843 * %false - we are done with this request
2844 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002845 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002846bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002847{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002848 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002849}
Jens Axboe56ad1742009-07-28 22:11:24 +02002850EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002851
2852/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002853 * blk_end_request_all - Helper function for drives to finish the request.
2854 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002855 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002856 *
2857 * Description:
2858 * Completely finish @rq.
2859 */
2860void blk_end_request_all(struct request *rq, int error)
2861{
2862 bool pending;
2863 unsigned int bidi_bytes = 0;
2864
2865 if (unlikely(blk_bidi_rq(rq)))
2866 bidi_bytes = blk_rq_bytes(rq->next_rq);
2867
2868 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2869 BUG_ON(pending);
2870}
Jens Axboe56ad1742009-07-28 22:11:24 +02002871EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002872
2873/**
2874 * blk_end_request_cur - Helper function to finish the current request chunk.
2875 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002876 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002877 *
2878 * Description:
2879 * Complete the current consecutively mapped chunk from @rq.
2880 *
2881 * Return:
2882 * %false - we are done with this request
2883 * %true - still buffers pending for this request
2884 */
2885bool blk_end_request_cur(struct request *rq, int error)
2886{
2887 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2888}
Jens Axboe56ad1742009-07-28 22:11:24 +02002889EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002890
2891/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002892 * blk_end_request_err - Finish a request till the next failure boundary.
2893 * @rq: the request to finish till the next failure boundary for
2894 * @error: must be negative errno
2895 *
2896 * Description:
2897 * Complete @rq till the next failure boundary.
2898 *
2899 * Return:
2900 * %false - we are done with this request
2901 * %true - still buffers pending for this request
2902 */
2903bool blk_end_request_err(struct request *rq, int error)
2904{
2905 WARN_ON(error >= 0);
2906 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2907}
2908EXPORT_SYMBOL_GPL(blk_end_request_err);
2909
2910/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002911 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002912 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002913 * @error: %0 for success, < %0 for error
2914 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002915 *
2916 * Description:
2917 * Must be called with queue lock held unlike blk_end_request().
2918 *
2919 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002920 * %false - we are done with this request
2921 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002922 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002923bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002924{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002925 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002926}
Jens Axboe56ad1742009-07-28 22:11:24 +02002927EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002928
2929/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002930 * __blk_end_request_all - Helper function for drives to finish the request.
2931 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002932 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002933 *
2934 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002935 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002936 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002937void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002938{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002939 bool pending;
2940 unsigned int bidi_bytes = 0;
2941
2942 if (unlikely(blk_bidi_rq(rq)))
2943 bidi_bytes = blk_rq_bytes(rq->next_rq);
2944
2945 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2946 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002947}
Jens Axboe56ad1742009-07-28 22:11:24 +02002948EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002949
2950/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002951 * __blk_end_request_cur - Helper function to finish the current request chunk.
2952 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002953 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002954 *
2955 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002956 * Complete the current consecutively mapped chunk from @rq. Must
2957 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002958 *
2959 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002960 * %false - we are done with this request
2961 * %true - still buffers pending for this request
2962 */
2963bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002964{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002965 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002966}
Jens Axboe56ad1742009-07-28 22:11:24 +02002967EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002968
Tejun Heo80a761f2009-07-03 17:48:17 +09002969/**
2970 * __blk_end_request_err - Finish a request till the next failure boundary.
2971 * @rq: the request to finish till the next failure boundary for
2972 * @error: must be negative errno
2973 *
2974 * Description:
2975 * Complete @rq till the next failure boundary. Must be called
2976 * with queue lock held.
2977 *
2978 * Return:
2979 * %false - we are done with this request
2980 * %true - still buffers pending for this request
2981 */
2982bool __blk_end_request_err(struct request *rq, int error)
2983{
2984 WARN_ON(error >= 0);
2985 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2986}
2987EXPORT_SYMBOL_GPL(__blk_end_request_err);
2988
Jens Axboe86db1e22008-01-29 14:53:40 +01002989void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2990 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
Mike Christie4993b772016-06-05 14:32:10 -05002992 req_set_op(rq, bio_op(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Jens Axboeb4f42e22014-04-10 09:46:28 -06002994 if (bio_has_data(bio))
David Woodhousefb2dce82008-08-05 18:01:53 +01002995 rq->nr_phys_segments = bio_phys_segments(q, bio);
Jens Axboeb4f42e22014-04-10 09:46:28 -06002996
Kent Overstreet4f024f32013-10-11 15:44:27 -07002997 rq->__data_len = bio->bi_iter.bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
NeilBrown66846572007-08-16 13:31:28 +02003000 if (bio->bi_bdev)
3001 rq->rq_disk = bio->bi_bdev->bd_disk;
3002}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Ilya Loginov2d4dc892009-11-26 09:16:19 +01003004#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
3005/**
3006 * rq_flush_dcache_pages - Helper function to flush all pages in a request
3007 * @rq: the request to be flushed
3008 *
3009 * Description:
3010 * Flush all pages in @rq.
3011 */
3012void rq_flush_dcache_pages(struct request *rq)
3013{
3014 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -08003015 struct bio_vec bvec;
Ilya Loginov2d4dc892009-11-26 09:16:19 +01003016
3017 rq_for_each_segment(bvec, rq, iter)
Kent Overstreet79886132013-11-23 17:19:00 -08003018 flush_dcache_page(bvec.bv_page);
Ilya Loginov2d4dc892009-11-26 09:16:19 +01003019}
3020EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
3021#endif
3022
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02003023/**
3024 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
3025 * @q : the queue of the device being checked
3026 *
3027 * Description:
3028 * Check if underlying low-level drivers of a device are busy.
3029 * If the drivers want to export their busy state, they must set own
3030 * exporting function using blk_queue_lld_busy() first.
3031 *
3032 * Basically, this function is used only by request stacking drivers
3033 * to stop dispatching requests to underlying devices when underlying
3034 * devices are busy. This behavior helps more I/O merging on the queue
3035 * of the request stacking driver and prevents I/O throughput regression
3036 * on burst I/O load.
3037 *
3038 * Return:
3039 * 0 - Not busy (The request stacking driver should dispatch request)
3040 * 1 - Busy (The request stacking driver should stop dispatching request)
3041 */
3042int blk_lld_busy(struct request_queue *q)
3043{
3044 if (q->lld_busy_fn)
3045 return q->lld_busy_fn(q);
3046
3047 return 0;
3048}
3049EXPORT_SYMBOL_GPL(blk_lld_busy);
3050
Mike Snitzer78d8e582015-06-26 10:01:13 -04003051/**
3052 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
3053 * @rq: the clone request to be cleaned up
3054 *
3055 * Description:
3056 * Free all bios in @rq for a cloned request.
3057 */
3058void blk_rq_unprep_clone(struct request *rq)
3059{
3060 struct bio *bio;
3061
3062 while ((bio = rq->bio) != NULL) {
3063 rq->bio = bio->bi_next;
3064
3065 bio_put(bio);
3066 }
3067}
3068EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
3069
3070/*
3071 * Copy attributes of the original request to the clone request.
3072 * The actual data parts (e.g. ->cmd, ->sense) are not copied.
3073 */
3074static void __blk_rq_prep_clone(struct request *dst, struct request *src)
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003075{
3076 dst->cpu = src->cpu;
Mike Christie4993b772016-06-05 14:32:10 -05003077 req_set_op_attrs(dst, req_op(src),
3078 (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE);
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003079 dst->cmd_type = src->cmd_type;
3080 dst->__sector = blk_rq_pos(src);
Jaegeuk Kim5190c812018-04-22 16:05:02 -07003081#ifdef CONFIG_PFK
Jaegeuk Kim84680312018-01-03 21:22:07 -08003082 dst->__dun = blk_rq_dun(src);
Jaegeuk Kim5190c812018-04-22 16:05:02 -07003083#endif
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003084 dst->__data_len = blk_rq_bytes(src);
3085 dst->nr_phys_segments = src->nr_phys_segments;
3086 dst->ioprio = src->ioprio;
3087 dst->extra_len = src->extra_len;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003088}
3089
3090/**
3091 * blk_rq_prep_clone - Helper function to setup clone request
3092 * @rq: the request to be setup
3093 * @rq_src: original request to be cloned
3094 * @bs: bio_set that bios for clone are allocated from
3095 * @gfp_mask: memory allocation mask for bio
3096 * @bio_ctr: setup function to be called for each clone bio.
3097 * Returns %0 for success, non %0 for failure.
3098 * @data: private data to be passed to @bio_ctr
3099 *
3100 * Description:
3101 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
3102 * The actual data parts of @rq_src (e.g. ->cmd, ->sense)
3103 * are not copied, and copying such parts is the caller's responsibility.
3104 * Also, pages which the original bios are pointing to are not copied
3105 * and the cloned bios just point same pages.
3106 * So cloned bios must be completed before original bios, which means
3107 * the caller must complete @rq before @rq_src.
3108 */
3109int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
3110 struct bio_set *bs, gfp_t gfp_mask,
3111 int (*bio_ctr)(struct bio *, struct bio *, void *),
3112 void *data)
3113{
3114 struct bio *bio, *bio_src;
3115
3116 if (!bs)
3117 bs = fs_bio_set;
3118
3119 __rq_for_each_bio(bio_src, rq_src) {
3120 bio = bio_clone_fast(bio_src, gfp_mask, bs);
3121 if (!bio)
3122 goto free_and_out;
3123
3124 if (bio_ctr && bio_ctr(bio, bio_src, data))
3125 goto free_and_out;
3126
3127 if (rq->bio) {
3128 rq->biotail->bi_next = bio;
3129 rq->biotail = bio;
3130 } else
3131 rq->bio = rq->biotail = bio;
3132 }
3133
3134 __blk_rq_prep_clone(rq, rq_src);
3135
3136 return 0;
3137
3138free_and_out:
3139 if (bio)
3140 bio_put(bio);
3141 blk_rq_unprep_clone(rq);
3142
3143 return -ENOMEM;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003144}
3145EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
3146
Jens Axboe59c3d452014-04-08 09:15:35 -06003147int kblockd_schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
3149 return queue_work(kblockd_workqueue, work);
3150}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151EXPORT_SYMBOL(kblockd_schedule_work);
3152
Jens Axboeee63cfa2016-08-24 15:52:48 -06003153int kblockd_schedule_work_on(int cpu, struct work_struct *work)
3154{
3155 return queue_work_on(cpu, kblockd_workqueue, work);
3156}
3157EXPORT_SYMBOL(kblockd_schedule_work_on);
3158
Jens Axboe59c3d452014-04-08 09:15:35 -06003159int kblockd_schedule_delayed_work(struct delayed_work *dwork,
3160 unsigned long delay)
Vivek Goyale43473b2010-09-15 17:06:35 -04003161{
3162 return queue_delayed_work(kblockd_workqueue, dwork, delay);
3163}
3164EXPORT_SYMBOL(kblockd_schedule_delayed_work);
3165
Jens Axboe8ab14592014-04-08 09:17:40 -06003166int kblockd_schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3167 unsigned long delay)
3168{
3169 return queue_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
3170}
3171EXPORT_SYMBOL(kblockd_schedule_delayed_work_on);
3172
Suresh Jayaraman75df7132011-09-21 10:00:16 +02003173/**
3174 * blk_start_plug - initialize blk_plug and track it inside the task_struct
3175 * @plug: The &struct blk_plug that needs to be initialized
3176 *
3177 * Description:
3178 * Tracking blk_plug inside the task_struct will help with auto-flushing the
3179 * pending I/O should the task end up blocking between blk_start_plug() and
3180 * blk_finish_plug(). This is important from a performance perspective, but
3181 * also ensures that we don't deadlock. For instance, if the task is blocking
3182 * for a memory allocation, memory reclaim could end up wanting to free a
3183 * page belonging to that request that is currently residing in our private
3184 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
3185 * this kind of deadlock.
3186 */
Jens Axboe73c10102011-03-08 13:19:51 +01003187void blk_start_plug(struct blk_plug *plug)
3188{
3189 struct task_struct *tsk = current;
3190
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003191 /*
3192 * If this is a nested plug, don't actually assign it.
3193 */
3194 if (tsk->plug)
3195 return;
3196
Jens Axboe73c10102011-03-08 13:19:51 +01003197 INIT_LIST_HEAD(&plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01003198 INIT_LIST_HEAD(&plug->mq_list);
NeilBrown048c9372011-04-18 09:52:22 +02003199 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe73c10102011-03-08 13:19:51 +01003200 /*
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003201 * Store ordering should not be needed here, since a potential
3202 * preempt will imply a full memory barrier
Jens Axboe73c10102011-03-08 13:19:51 +01003203 */
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003204 tsk->plug = plug;
Jens Axboe73c10102011-03-08 13:19:51 +01003205}
3206EXPORT_SYMBOL(blk_start_plug);
3207
3208static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
3209{
3210 struct request *rqa = container_of(a, struct request, queuelist);
3211 struct request *rqb = container_of(b, struct request, queuelist);
3212
Jianpeng Ma975927b2012-10-25 21:58:17 +02003213 return !(rqa->q < rqb->q ||
3214 (rqa->q == rqb->q && blk_rq_pos(rqa) < blk_rq_pos(rqb)));
Jens Axboe73c10102011-03-08 13:19:51 +01003215}
3216
Jens Axboe49cac012011-04-16 13:51:05 +02003217/*
3218 * If 'from_schedule' is true, then postpone the dispatch of requests
3219 * until a safe kblockd context. We due this to avoid accidental big
3220 * additional stack usage in driver dispatch, in places where the originally
3221 * plugger did not intend it.
3222 */
Jens Axboef6603782011-04-15 15:49:07 +02003223static void queue_unplugged(struct request_queue *q, unsigned int depth,
Jens Axboe49cac012011-04-16 13:51:05 +02003224 bool from_schedule)
Jens Axboe99e22592011-04-18 09:59:55 +02003225 __releases(q->queue_lock)
Jens Axboe94b5eb22011-04-12 10:12:19 +02003226{
Jens Axboe49cac012011-04-16 13:51:05 +02003227 trace_block_unplug(q, depth, !from_schedule);
Jens Axboe99e22592011-04-18 09:59:55 +02003228
Bart Van Assche70460572012-11-28 13:45:56 +01003229 if (from_schedule)
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003230 blk_run_queue_async(q);
Bart Van Assche70460572012-11-28 13:45:56 +01003231 else
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003232 __blk_run_queue(q);
Bart Van Assche70460572012-11-28 13:45:56 +01003233 spin_unlock(q->queue_lock);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003234}
3235
NeilBrown74018dc2012-07-31 09:08:15 +02003236static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
NeilBrown048c9372011-04-18 09:52:22 +02003237{
3238 LIST_HEAD(callbacks);
3239
Shaohua Li2a7d5552012-07-31 09:08:15 +02003240 while (!list_empty(&plug->cb_list)) {
3241 list_splice_init(&plug->cb_list, &callbacks);
NeilBrown048c9372011-04-18 09:52:22 +02003242
Shaohua Li2a7d5552012-07-31 09:08:15 +02003243 while (!list_empty(&callbacks)) {
3244 struct blk_plug_cb *cb = list_first_entry(&callbacks,
NeilBrown048c9372011-04-18 09:52:22 +02003245 struct blk_plug_cb,
3246 list);
Shaohua Li2a7d5552012-07-31 09:08:15 +02003247 list_del(&cb->list);
NeilBrown74018dc2012-07-31 09:08:15 +02003248 cb->callback(cb, from_schedule);
Shaohua Li2a7d5552012-07-31 09:08:15 +02003249 }
NeilBrown048c9372011-04-18 09:52:22 +02003250 }
3251}
3252
NeilBrown9cbb1752012-07-31 09:08:14 +02003253struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
3254 int size)
3255{
3256 struct blk_plug *plug = current->plug;
3257 struct blk_plug_cb *cb;
3258
3259 if (!plug)
3260 return NULL;
3261
3262 list_for_each_entry(cb, &plug->cb_list, list)
3263 if (cb->callback == unplug && cb->data == data)
3264 return cb;
3265
3266 /* Not currently on the callback list */
3267 BUG_ON(size < sizeof(*cb));
3268 cb = kzalloc(size, GFP_ATOMIC);
3269 if (cb) {
3270 cb->data = data;
3271 cb->callback = unplug;
3272 list_add(&cb->list, &plug->cb_list);
3273 }
3274 return cb;
3275}
3276EXPORT_SYMBOL(blk_check_plugged);
3277
Jens Axboe49cac012011-04-16 13:51:05 +02003278void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01003279{
3280 struct request_queue *q;
3281 unsigned long flags;
3282 struct request *rq;
NeilBrown109b8122011-04-11 14:13:10 +02003283 LIST_HEAD(list);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003284 unsigned int depth;
Jens Axboe73c10102011-03-08 13:19:51 +01003285
NeilBrown74018dc2012-07-31 09:08:15 +02003286 flush_plug_callbacks(plug, from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01003287
3288 if (!list_empty(&plug->mq_list))
3289 blk_mq_flush_plug_list(plug, from_schedule);
3290
Jens Axboe73c10102011-03-08 13:19:51 +01003291 if (list_empty(&plug->list))
3292 return;
3293
NeilBrown109b8122011-04-11 14:13:10 +02003294 list_splice_init(&plug->list, &list);
3295
Jianpeng Ma422765c2013-01-11 14:46:09 +01003296 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe73c10102011-03-08 13:19:51 +01003297
3298 q = NULL;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003299 depth = 0;
Jens Axboe18811272011-04-12 10:11:24 +02003300
3301 /*
3302 * Save and disable interrupts here, to avoid doing it for every
3303 * queue lock we have to take.
3304 */
Jens Axboe73c10102011-03-08 13:19:51 +01003305 local_irq_save(flags);
NeilBrown109b8122011-04-11 14:13:10 +02003306 while (!list_empty(&list)) {
3307 rq = list_entry_rq(list.next);
Jens Axboe73c10102011-03-08 13:19:51 +01003308 list_del_init(&rq->queuelist);
Jens Axboe73c10102011-03-08 13:19:51 +01003309 BUG_ON(!rq->q);
3310 if (rq->q != q) {
Jens Axboe99e22592011-04-18 09:59:55 +02003311 /*
3312 * This drops the queue lock
3313 */
3314 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003315 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003316 q = rq->q;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003317 depth = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01003318 spin_lock(q->queue_lock);
3319 }
Tejun Heo8ba61432011-12-14 00:33:37 +01003320
3321 /*
3322 * Short-circuit if @q is dead
3323 */
Bart Van Assche3f3299d2012-11-28 13:42:38 +01003324 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01003325 __blk_end_request_all(rq, -ENODEV);
3326 continue;
3327 }
3328
Jens Axboe73c10102011-03-08 13:19:51 +01003329 /*
3330 * rq is already accounted, so use raw insert
3331 */
Mike Christie28a8f0d2016-06-05 14:32:25 -05003332 if (rq->cmd_flags & (REQ_PREFLUSH | REQ_FUA))
Jens Axboe401a18e2011-03-25 16:57:52 +01003333 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
3334 else
3335 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003336
3337 depth++;
Jens Axboe73c10102011-03-08 13:19:51 +01003338 }
3339
Jens Axboe99e22592011-04-18 09:59:55 +02003340 /*
3341 * This drops the queue lock
3342 */
3343 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003344 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003345
Jens Axboe73c10102011-03-08 13:19:51 +01003346 local_irq_restore(flags);
3347}
Jens Axboe73c10102011-03-08 13:19:51 +01003348
3349void blk_finish_plug(struct blk_plug *plug)
3350{
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003351 if (plug != current->plug)
3352 return;
Jens Axboef6603782011-04-15 15:49:07 +02003353 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02003354
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003355 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01003356}
3357EXPORT_SYMBOL(blk_finish_plug);
3358
Jens Axboe05229be2015-11-05 10:44:55 -07003359bool blk_poll(struct request_queue *q, blk_qc_t cookie)
3360{
3361 struct blk_plug *plug;
3362 long state;
Stephen Bates6e219352016-09-13 12:23:15 -06003363 unsigned int queue_num;
3364 struct blk_mq_hw_ctx *hctx;
Jens Axboe05229be2015-11-05 10:44:55 -07003365
3366 if (!q->mq_ops || !q->mq_ops->poll || !blk_qc_t_valid(cookie) ||
3367 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
3368 return false;
3369
Stephen Bates6e219352016-09-13 12:23:15 -06003370 queue_num = blk_qc_t_to_queue_num(cookie);
3371 hctx = q->queue_hw_ctx[queue_num];
3372 hctx->poll_considered++;
3373
Jens Axboe05229be2015-11-05 10:44:55 -07003374 plug = current->plug;
3375 if (plug)
3376 blk_flush_plug_list(plug, false);
3377
3378 state = current->state;
3379 while (!need_resched()) {
Jens Axboe05229be2015-11-05 10:44:55 -07003380 int ret;
3381
3382 hctx->poll_invoked++;
3383
3384 ret = q->mq_ops->poll(hctx, blk_qc_t_to_tag(cookie));
3385 if (ret > 0) {
3386 hctx->poll_success++;
3387 set_current_state(TASK_RUNNING);
3388 return true;
3389 }
3390
3391 if (signal_pending_state(state, current))
3392 set_current_state(TASK_RUNNING);
3393
3394 if (current->state == TASK_RUNNING)
3395 return true;
3396 if (ret < 0)
3397 break;
3398 cpu_relax();
3399 }
3400
3401 return false;
3402}
Sagi Grimberg9645c1a2016-06-21 18:04:19 +02003403EXPORT_SYMBOL_GPL(blk_poll);
Jens Axboe05229be2015-11-05 10:44:55 -07003404
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01003405#ifdef CONFIG_PM
Lin Ming6c954662013-03-23 11:42:26 +08003406/**
3407 * blk_pm_runtime_init - Block layer runtime PM initialization routine
3408 * @q: the queue of the device
3409 * @dev: the device the queue belongs to
3410 *
3411 * Description:
3412 * Initialize runtime-PM-related fields for @q and start auto suspend for
3413 * @dev. Drivers that want to take advantage of request-based runtime PM
3414 * should call this function after @dev has been initialized, and its
3415 * request queue @q has been allocated, and runtime PM for it can not happen
3416 * yet(either due to disabled/forbidden or its usage_count > 0). In most
3417 * cases, driver should call this function before any I/O has taken place.
3418 *
3419 * This function takes care of setting up using auto suspend for the device,
3420 * the autosuspend delay is set to -1 to make runtime suspend impossible
3421 * until an updated value is either set by user or by driver. Drivers do
3422 * not need to touch other autosuspend settings.
3423 *
3424 * The block layer runtime PM is request based, so only works for drivers
3425 * that use request as their IO unit instead of those directly use bio's.
3426 */
3427void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
3428{
3429 q->dev = dev;
3430 q->rpm_status = RPM_ACTIVE;
3431 pm_runtime_set_autosuspend_delay(q->dev, -1);
3432 pm_runtime_use_autosuspend(q->dev);
3433}
3434EXPORT_SYMBOL(blk_pm_runtime_init);
3435
3436/**
3437 * blk_pre_runtime_suspend - Pre runtime suspend check
3438 * @q: the queue of the device
3439 *
3440 * Description:
3441 * This function will check if runtime suspend is allowed for the device
3442 * by examining if there are any requests pending in the queue. If there
3443 * are requests pending, the device can not be runtime suspended; otherwise,
3444 * the queue's status will be updated to SUSPENDING and the driver can
3445 * proceed to suspend the device.
3446 *
3447 * For the not allowed case, we mark last busy for the device so that
3448 * runtime PM core will try to autosuspend it some time later.
3449 *
3450 * This function should be called near the start of the device's
3451 * runtime_suspend callback.
3452 *
3453 * Return:
3454 * 0 - OK to runtime suspend the device
3455 * -EBUSY - Device should not be runtime suspended
3456 */
3457int blk_pre_runtime_suspend(struct request_queue *q)
3458{
3459 int ret = 0;
3460
Ken Xue4fd41a852015-12-01 14:45:46 +08003461 if (!q->dev)
3462 return ret;
3463
Lin Ming6c954662013-03-23 11:42:26 +08003464 spin_lock_irq(q->queue_lock);
3465 if (q->nr_pending) {
3466 ret = -EBUSY;
3467 pm_runtime_mark_last_busy(q->dev);
3468 } else {
3469 q->rpm_status = RPM_SUSPENDING;
3470 }
3471 spin_unlock_irq(q->queue_lock);
3472 return ret;
3473}
3474EXPORT_SYMBOL(blk_pre_runtime_suspend);
3475
3476/**
3477 * blk_post_runtime_suspend - Post runtime suspend processing
3478 * @q: the queue of the device
3479 * @err: return value of the device's runtime_suspend function
3480 *
3481 * Description:
3482 * Update the queue's runtime status according to the return value of the
3483 * device's runtime suspend function and mark last busy for the device so
3484 * that PM core will try to auto suspend the device at a later time.
3485 *
3486 * This function should be called near the end of the device's
3487 * runtime_suspend callback.
3488 */
3489void blk_post_runtime_suspend(struct request_queue *q, int err)
3490{
Ken Xue4fd41a852015-12-01 14:45:46 +08003491 if (!q->dev)
3492 return;
3493
Lin Ming6c954662013-03-23 11:42:26 +08003494 spin_lock_irq(q->queue_lock);
3495 if (!err) {
3496 q->rpm_status = RPM_SUSPENDED;
3497 } else {
3498 q->rpm_status = RPM_ACTIVE;
3499 pm_runtime_mark_last_busy(q->dev);
3500 }
3501 spin_unlock_irq(q->queue_lock);
3502}
3503EXPORT_SYMBOL(blk_post_runtime_suspend);
3504
3505/**
3506 * blk_pre_runtime_resume - Pre runtime resume processing
3507 * @q: the queue of the device
3508 *
3509 * Description:
3510 * Update the queue's runtime status to RESUMING in preparation for the
3511 * runtime resume of the device.
3512 *
3513 * This function should be called near the start of the device's
3514 * runtime_resume callback.
3515 */
3516void blk_pre_runtime_resume(struct request_queue *q)
3517{
Ken Xue4fd41a852015-12-01 14:45:46 +08003518 if (!q->dev)
3519 return;
3520
Lin Ming6c954662013-03-23 11:42:26 +08003521 spin_lock_irq(q->queue_lock);
3522 q->rpm_status = RPM_RESUMING;
3523 spin_unlock_irq(q->queue_lock);
3524}
3525EXPORT_SYMBOL(blk_pre_runtime_resume);
3526
3527/**
3528 * blk_post_runtime_resume - Post runtime resume processing
3529 * @q: the queue of the device
3530 * @err: return value of the device's runtime_resume function
3531 *
3532 * Description:
3533 * Update the queue's runtime status according to the return value of the
3534 * device's runtime_resume function. If it is successfully resumed, process
3535 * the requests that are queued into the device's queue when it is resuming
3536 * and then mark last busy and initiate autosuspend for it.
3537 *
3538 * This function should be called near the end of the device's
3539 * runtime_resume callback.
3540 */
3541void blk_post_runtime_resume(struct request_queue *q, int err)
3542{
Ken Xue4fd41a852015-12-01 14:45:46 +08003543 if (!q->dev)
3544 return;
3545
Lin Ming6c954662013-03-23 11:42:26 +08003546 spin_lock_irq(q->queue_lock);
3547 if (!err) {
3548 q->rpm_status = RPM_ACTIVE;
3549 __blk_run_queue(q);
3550 pm_runtime_mark_last_busy(q->dev);
Aaron Luc60855c2013-05-17 15:47:20 +08003551 pm_request_autosuspend(q->dev);
Lin Ming6c954662013-03-23 11:42:26 +08003552 } else {
3553 q->rpm_status = RPM_SUSPENDED;
3554 }
3555 spin_unlock_irq(q->queue_lock);
3556}
3557EXPORT_SYMBOL(blk_post_runtime_resume);
Mika Westerbergd07ab6d2016-02-18 10:54:11 +02003558
3559/**
3560 * blk_set_runtime_active - Force runtime status of the queue to be active
3561 * @q: the queue of the device
3562 *
3563 * If the device is left runtime suspended during system suspend the resume
3564 * hook typically resumes the device and corrects runtime status
3565 * accordingly. However, that does not affect the queue runtime PM status
3566 * which is still "suspended". This prevents processing requests from the
3567 * queue.
3568 *
3569 * This function can be used in driver's resume hook to correct queue
3570 * runtime PM status and re-enable peeking requests from the queue. It
3571 * should be called before first request is added to the queue.
3572 */
3573void blk_set_runtime_active(struct request_queue *q)
3574{
3575 spin_lock_irq(q->queue_lock);
3576 q->rpm_status = RPM_ACTIVE;
3577 pm_runtime_mark_last_busy(q->dev);
3578 pm_request_autosuspend(q->dev);
3579 spin_unlock_irq(q->queue_lock);
3580}
3581EXPORT_SYMBOL(blk_set_runtime_active);
Lin Ming6c954662013-03-23 11:42:26 +08003582#endif
3583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584int __init blk_dev_init(void)
3585{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02003586 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
Maninder Singh0762b232015-07-07 12:41:07 +05303587 FIELD_SIZEOF(struct request, cmd_flags));
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02003588
Tejun Heo89b90be2011-01-03 15:01:47 +01003589 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
3590 kblockd_workqueue = alloc_workqueue("kblockd",
Matias Bjørling28747fc2014-06-11 23:43:54 +02003591 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 if (!kblockd_workqueue)
3593 panic("Failed to create kblockd\n");
3594
3595 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09003596 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Ilya Dryomovc2789bd2015-11-20 22:16:46 +01003598 blk_requestq_cachep = kmem_cache_create("request_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02003599 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 return 0;
3602}
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003603
3604/*
3605 * Blk IO latency support. We want this to be as cheap as possible, so doing
3606 * this lockless (and avoiding atomics), a few off by a few errors in this
3607 * code is not harmful, and we don't want to do anything that is
3608 * perf-impactful.
3609 * TODO : If necessary, we can make the histograms per-cpu and aggregate
3610 * them when printing them out.
3611 */
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003612ssize_t
Hyojun Kim11537d52017-12-21 09:57:41 -08003613blk_latency_hist_show(char* name, struct io_latency_state *s, char *buf,
3614 int buf_size)
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003615{
3616 int i;
3617 int bytes_written = 0;
3618 u_int64_t num_elem, elem;
3619 int pct;
Hyojun Kim11537d52017-12-21 09:57:41 -08003620 u_int64_t average;
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003621
Hyojun Kim11537d52017-12-21 09:57:41 -08003622 num_elem = s->latency_elems;
3623 if (num_elem > 0) {
3624 average = div64_u64(s->latency_sum, s->latency_elems);
3625 bytes_written += scnprintf(buf + bytes_written,
3626 buf_size - bytes_written,
3627 "IO svc_time %s Latency Histogram (n = %llu,"
3628 " average = %llu):\n", name, num_elem, average);
3629 for (i = 0;
3630 i < ARRAY_SIZE(latency_x_axis_us);
3631 i++) {
3632 elem = s->latency_y_axis[i];
3633 pct = div64_u64(elem * 100, num_elem);
3634 bytes_written += scnprintf(buf + bytes_written,
3635 PAGE_SIZE - bytes_written,
3636 "\t< %6lluus%15llu%15d%%\n",
3637 latency_x_axis_us[i],
3638 elem, pct);
3639 }
3640 /* Last element in y-axis table is overflow */
3641 elem = s->latency_y_axis[i];
3642 pct = div64_u64(elem * 100, num_elem);
3643 bytes_written += scnprintf(buf + bytes_written,
3644 PAGE_SIZE - bytes_written,
3645 "\t>=%6lluus%15llu%15d%%\n",
3646 latency_x_axis_us[i - 1], elem, pct);
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003647 }
Hyojun Kim11537d52017-12-21 09:57:41 -08003648
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003649 return bytes_written;
3650}
3651EXPORT_SYMBOL(blk_latency_hist_show);