blob: 0d5edebde8eb4a145eed11de32fc70e86553ac6f [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
Waiman Longb4880922017-09-20 13:12:20 -0600716#ifdef CONFIG_BLK_DEV_IO_TRACE
717 mutex_init(&q->blk_trace_mutex);
718#endif
Al Viro483f4af2006-03-18 18:34:37 -0500719 mutex_init(&q->sysfs_lock);
Neil Browne7e72bf2008-05-14 16:05:54 -0700720 spin_lock_init(&q->__queue_lock);
Al Viro483f4af2006-03-18 18:34:37 -0500721
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500722 /*
723 * By default initialize queue_lock to internal lock and driver can
724 * override it later if need be.
725 */
726 q->queue_lock = &q->__queue_lock;
727
Tejun Heob82d4b12012-04-13 13:11:31 -0700728 /*
729 * A queue starts its life with bypass turned on to avoid
730 * unnecessary bypass on/off overhead and nasty surprises during
Tejun Heo749fefe2012-09-20 14:08:52 -0700731 * init. The initial bypass will be finished when the queue is
732 * registered by blk_register_queue().
Tejun Heob82d4b12012-04-13 13:11:31 -0700733 */
734 q->bypass_depth = 1;
735 __set_bit(QUEUE_FLAG_BYPASS, &q->queue_flags);
736
Jens Axboe320ae512013-10-24 09:20:05 +0100737 init_waitqueue_head(&q->mq_freeze_wq);
738
Dan Williams3ef28e82015-10-21 13:20:12 -0400739 /*
740 * Init percpu_ref in atomic mode so that it's faster to shutdown.
741 * See blk_register_queue() for details.
742 */
743 if (percpu_ref_init(&q->q_usage_counter,
744 blk_queue_usage_counter_release,
745 PERCPU_REF_INIT_ATOMIC, GFP_KERNEL))
Mikulas Patockafff49962013-10-14 12:11:36 -0400746 goto fail_bdi;
Tejun Heof51b8022012-03-05 13:15:05 -0800747
Dan Williams3ef28e82015-10-21 13:20:12 -0400748 if (blkcg_init_queue(q))
749 goto fail_ref;
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return q;
Tejun Heoa73f7302011-12-14 00:33:37 +0100752
Dan Williams3ef28e82015-10-21 13:20:12 -0400753fail_ref:
754 percpu_ref_exit(&q->q_usage_counter);
Mikulas Patockafff49962013-10-14 12:11:36 -0400755fail_bdi:
Jan Kara33c17052017-02-02 15:56:51 +0100756 bdi_put(q->backing_dev_info);
Kent Overstreet54efd502015-04-23 22:37:18 -0700757fail_split:
758 bioset_free(q->bio_split);
Tejun Heoa73f7302011-12-14 00:33:37 +0100759fail_id:
760 ida_simple_remove(&blk_queue_ida, q->id);
761fail_q:
762 kmem_cache_free(blk_requestq_cachep, q);
763 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
Christoph Lameter19460892005-06-23 00:08:19 -0700765EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767/**
768 * blk_init_queue - prepare a request queue for use with a block device
769 * @rfn: The function to be called to process requests that have been
770 * placed on the queue.
771 * @lock: Request queue spin lock
772 *
773 * Description:
774 * If a block device wishes to use the standard request handling procedures,
775 * which sorts requests and coalesces adjacent requests, then it must
776 * call blk_init_queue(). The function @rfn will be called when there
777 * are requests on the queue that need to be processed. If the device
778 * supports plugging, then @rfn may not be called immediately when requests
779 * are available on the queue, but may be called at some time later instead.
780 * Plugged queues are generally unplugged when a buffer belonging to one
781 * of the requests on the queue is needed, or due to memory pressure.
782 *
783 * @rfn is not required, or even expected, to remove all requests off the
784 * queue, but only as many as it can handle at a time. If it does leave
785 * requests on the queue, it is responsible for arranging that the requests
786 * get dealt with eventually.
787 *
788 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200789 * request queue; this lock will be taken also from interrupt context, so irq
790 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 *
Randy Dunlap710027a2008-08-19 20:13:11 +0200792 * Function returns a pointer to the initialized request queue, or %NULL if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 * it didn't succeed.
794 *
795 * Note:
796 * blk_init_queue() must be paired with a blk_cleanup_queue() call
797 * when the block device is deactivated (such as at module unload).
798 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700799
Jens Axboe165125e2007-07-24 09:28:11 +0200800struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Ezequiel Garciac304a512012-11-10 10:39:44 +0100802 return blk_init_queue_node(rfn, lock, NUMA_NO_NODE);
Christoph Lameter19460892005-06-23 00:08:19 -0700803}
804EXPORT_SYMBOL(blk_init_queue);
805
Jens Axboe165125e2007-07-24 09:28:11 +0200806struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700807blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
808{
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600809 struct request_queue *uninit_q, *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600811 uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id);
812 if (!uninit_q)
813 return NULL;
814
Mike Snitzer51514122011-11-23 10:59:13 +0100815 q = blk_init_allocated_queue(uninit_q, rfn, lock);
Mike Snitzerc86d1b82010-06-03 11:34:52 -0600816 if (!q)
Mike Snitzer7982e902014-03-08 17:20:01 -0700817 blk_cleanup_queue(uninit_q);
Christoph Hellwig18741982014-02-10 09:29:00 -0700818
Mike Snitzer7982e902014-03-08 17:20:01 -0700819 return q;
Mike Snitzer01effb02010-05-11 08:57:42 +0200820}
821EXPORT_SYMBOL(blk_init_queue_node);
822
Jens Axboedece1632015-11-05 10:41:16 -0700823static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio);
Mike Snitzer336b7e12015-05-11 14:06:32 -0400824
Mike Snitzer01effb02010-05-11 08:57:42 +0200825struct request_queue *
826blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
827 spinlock_t *lock)
828{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (!q)
830 return NULL;
831
Ming Leif70ced02014-09-25 23:23:47 +0800832 q->fq = blk_alloc_flush_queue(q, NUMA_NO_NODE, 0);
Ming Leiba483382014-09-25 23:23:44 +0800833 if (!q->fq)
Mike Snitzer7982e902014-03-08 17:20:01 -0700834 return NULL;
835
Tejun Heoa0516612012-06-26 15:05:44 -0700836 if (blk_init_rl(&q->root_rl, q, GFP_KERNEL))
Dave Jones708f04d2014-03-20 15:03:58 -0600837 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Christoph Hellwig287922e2015-10-30 20:57:30 +0800839 INIT_WORK(&q->timeout_work, blk_timeout_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 q->prep_rq_fn = NULL;
James Bottomley28018c22010-07-01 19:49:17 +0900842 q->unprep_rq_fn = NULL;
Tejun Heo60ea8222012-09-20 14:09:30 -0700843 q->queue_flags |= QUEUE_FLAG_DEFAULT;
Vivek Goyalc94a96a2011-03-02 19:04:42 -0500844
845 /* Override internal queue lock with supplied lock pointer */
846 if (lock)
847 q->queue_lock = lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Jens Axboef3b144a2009-03-06 08:48:33 +0100849 /*
850 * This also sets hw/phys segments, boundary and size
851 */
Jens Axboec20e8de2011-09-12 12:03:37 +0200852 blk_queue_make_request(q, blk_queue_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Alan Stern44ec9542007-02-20 11:01:57 -0500854 q->sg_reserved_size = INT_MAX;
855
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600856 /* Protect q->elevator from elevator_change */
857 mutex_lock(&q->sysfs_lock);
858
Tejun Heob82d4b12012-04-13 13:11:31 -0700859 /* init elevator */
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600860 if (elevator_init(q, NULL)) {
861 mutex_unlock(&q->sysfs_lock);
Dave Jones708f04d2014-03-20 15:03:58 -0600862 goto fail;
Tomoki Sekiyamaeb1c1602013-10-15 16:42:16 -0600863 }
864
865 mutex_unlock(&q->sysfs_lock);
866
Tejun Heob82d4b12012-04-13 13:11:31 -0700867 return q;
Dave Jones708f04d2014-03-20 15:03:58 -0600868
869fail:
Ming Leiba483382014-09-25 23:23:44 +0800870 blk_free_flush_queue(q->fq);
xiao jinc1919912018-07-30 14:11:12 +0800871 q->fq = NULL;
Dave Jones708f04d2014-03-20 15:03:58 -0600872 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
Mike Snitzer51514122011-11-23 10:59:13 +0100874EXPORT_SYMBOL(blk_init_allocated_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Tejun Heo09ac46c2011-12-14 00:33:38 +0100876bool blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Bart Van Assche3f3299d2012-11-28 13:42:38 +0100878 if (likely(!blk_queue_dying(q))) {
Tejun Heo09ac46c2011-12-14 00:33:38 +0100879 __blk_get_queue(q);
880 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882
Tejun Heo09ac46c2011-12-14 00:33:38 +0100883 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
Jens Axboed86e0e82011-05-27 07:44:43 +0200885EXPORT_SYMBOL(blk_get_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Tejun Heo5b788ce2012-06-04 20:40:59 -0700887static inline void blk_free_request(struct request_list *rl, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Tejun Heof1f8cc92011-12-14 00:33:42 +0100889 if (rq->cmd_flags & REQ_ELVPRIV) {
Tejun Heo5b788ce2012-06-04 20:40:59 -0700890 elv_put_request(rl->q, rq);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100891 if (rq->elv.icq)
Tejun Heo11a31222012-02-07 07:51:30 +0100892 put_io_context(rq->elv.icq->ioc);
Tejun Heof1f8cc92011-12-14 00:33:42 +0100893 }
894
Tejun Heo5b788ce2012-06-04 20:40:59 -0700895 mempool_free(rq, rl->rq_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898/*
899 * ioc_batching returns true if the ioc is a valid batching request and
900 * should be given priority access to a request.
901 */
Jens Axboe165125e2007-07-24 09:28:11 +0200902static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 if (!ioc)
905 return 0;
906
907 /*
908 * Make sure the process is able to allocate at least 1 request
909 * even if the batch times out, otherwise we could theoretically
910 * lose wakeups.
911 */
912 return ioc->nr_batch_requests == q->nr_batching ||
913 (ioc->nr_batch_requests > 0
914 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
915}
916
917/*
918 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
919 * will cause the process to be a "batcher" on all queues in the system. This
920 * is the behaviour we want though - once it gets a wakeup it should be given
921 * a nice run.
922 */
Jens Axboe165125e2007-07-24 09:28:11 +0200923static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 if (!ioc || ioc_batching(q, ioc))
926 return;
927
928 ioc->nr_batch_requests = q->nr_batching;
929 ioc->last_waited = jiffies;
930}
931
Tejun Heo5b788ce2012-06-04 20:40:59 -0700932static void __freed_request(struct request_list *rl, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700934 struct request_queue *q = rl->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Tejun Heod40f75a2015-05-22 17:13:42 -0400936 if (rl->count[sync] < queue_congestion_off_threshold(q))
937 blk_clear_congested(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Jens Axboe1faa16d2009-04-06 14:48:01 +0200939 if (rl->count[sync] + 1 <= q->nr_requests) {
940 if (waitqueue_active(&rl->wait[sync]))
941 wake_up(&rl->wait[sync]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Tejun Heo5b788ce2012-06-04 20:40:59 -0700943 blk_clear_rl_full(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945}
946
947/*
948 * A request has just been released. Account for it, update the full and
949 * congestion status, wake up any waiters. Called under q->queue_lock.
950 */
Mike Christiee6a40b02016-06-05 14:32:11 -0500951static void freed_request(struct request_list *rl, int op, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Tejun Heo5b788ce2012-06-04 20:40:59 -0700953 struct request_queue *q = rl->q;
Mike Christied9d8c5c2016-06-05 14:32:16 -0500954 int sync = rw_is_sync(op, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700956 q->nr_rqs[sync]--;
Jens Axboe1faa16d2009-04-06 14:48:01 +0200957 rl->count[sync]--;
Tejun Heo75eb6c32011-10-19 14:31:22 +0200958 if (flags & REQ_ELVPRIV)
Tejun Heo8a5ecdd2012-06-04 20:40:58 -0700959 q->nr_rqs_elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Tejun Heo5b788ce2012-06-04 20:40:59 -0700961 __freed_request(rl, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Jens Axboe1faa16d2009-04-06 14:48:01 +0200963 if (unlikely(rl->starved[sync ^ 1]))
Tejun Heo5b788ce2012-06-04 20:40:59 -0700964 __freed_request(rl, sync ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600967int blk_update_nr_requests(struct request_queue *q, unsigned int nr)
968{
969 struct request_list *rl;
Tejun Heod40f75a2015-05-22 17:13:42 -0400970 int on_thresh, off_thresh;
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600971
972 spin_lock_irq(q->queue_lock);
973 q->nr_requests = nr;
974 blk_queue_congestion_threshold(q);
Tejun Heod40f75a2015-05-22 17:13:42 -0400975 on_thresh = queue_congestion_on_threshold(q);
976 off_thresh = queue_congestion_off_threshold(q);
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600977
978 blk_queue_for_each_rl(rl, q) {
Tejun Heod40f75a2015-05-22 17:13:42 -0400979 if (rl->count[BLK_RW_SYNC] >= on_thresh)
980 blk_set_congested(rl, BLK_RW_SYNC);
981 else if (rl->count[BLK_RW_SYNC] < off_thresh)
982 blk_clear_congested(rl, BLK_RW_SYNC);
983
984 if (rl->count[BLK_RW_ASYNC] >= on_thresh)
985 blk_set_congested(rl, BLK_RW_ASYNC);
986 else if (rl->count[BLK_RW_ASYNC] < off_thresh)
987 blk_clear_congested(rl, BLK_RW_ASYNC);
988
Jens Axboee3a2b3f2014-05-20 11:49:02 -0600989 if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
990 blk_set_rl_full(rl, BLK_RW_SYNC);
991 } else {
992 blk_clear_rl_full(rl, BLK_RW_SYNC);
993 wake_up(&rl->wait[BLK_RW_SYNC]);
994 }
995
996 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
997 blk_set_rl_full(rl, BLK_RW_ASYNC);
998 } else {
999 blk_clear_rl_full(rl, BLK_RW_ASYNC);
1000 wake_up(&rl->wait[BLK_RW_ASYNC]);
1001 }
1002 }
1003
1004 spin_unlock_irq(q->queue_lock);
1005 return 0;
1006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/*
Mike Snitzer9d5a4e92011-02-11 11:05:46 +01001009 * Determine if elevator data should be initialized when allocating the
1010 * request associated with @bio.
1011 */
1012static bool blk_rq_should_init_elevator(struct bio *bio)
1013{
1014 if (!bio)
1015 return true;
1016
1017 /*
1018 * Flush requests do not use the elevator so skip initialization.
1019 * This allows a request to share the flush and elevator data.
1020 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06001021 if (bio->bi_opf & (REQ_PREFLUSH | REQ_FUA))
Mike Snitzer9d5a4e92011-02-11 11:05:46 +01001022 return false;
1023
1024 return true;
1025}
1026
Tejun Heoda8303c2011-10-19 14:33:05 +02001027/**
Tejun Heo852c7882012-03-05 13:15:27 -08001028 * rq_ioc - determine io_context for request allocation
1029 * @bio: request being allocated is for this bio (can be %NULL)
1030 *
1031 * Determine io_context to use for request allocation for @bio. May return
1032 * %NULL if %current->io_context doesn't exist.
1033 */
1034static struct io_context *rq_ioc(struct bio *bio)
1035{
1036#ifdef CONFIG_BLK_CGROUP
1037 if (bio && bio->bi_ioc)
1038 return bio->bi_ioc;
1039#endif
1040 return current->io_context;
1041}
1042
1043/**
Tejun Heoa06e05e2012-06-04 20:40:55 -07001044 * __get_request - get a free request
Tejun Heo5b788ce2012-06-04 20:40:59 -07001045 * @rl: request list to allocate from
Mike Christiee6a40b02016-06-05 14:32:11 -05001046 * @op: REQ_OP_READ/REQ_OP_WRITE
1047 * @op_flags: rq_flag_bits
Tejun Heoda8303c2011-10-19 14:33:05 +02001048 * @bio: bio to allocate request for (can be %NULL)
1049 * @gfp_mask: allocation mask
1050 *
1051 * Get a free request from @q. This function may fail under memory
1052 * pressure or if @q is dead.
1053 *
Masanari Iidada3dae52014-09-09 01:27:23 +09001054 * Must be called with @q->queue_lock held and,
Joe Lawrencea492f072014-08-28 08:15:21 -06001055 * Returns ERR_PTR on failure, with @q->queue_lock held.
1056 * Returns request pointer on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
Mike Christiee6a40b02016-06-05 14:32:11 -05001058static struct request *__get_request(struct request_list *rl, int op,
1059 int op_flags, struct bio *bio,
1060 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Tejun Heo5b788ce2012-06-04 20:40:59 -07001062 struct request_queue *q = rl->q;
Tejun Heob6792812012-03-05 13:15:23 -08001063 struct request *rq;
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001064 struct elevator_type *et = q->elevator->type;
1065 struct io_context *ioc = rq_ioc(bio);
Tejun Heof1f8cc92011-12-14 00:33:42 +01001066 struct io_cq *icq = NULL;
Mike Christied9d8c5c2016-06-05 14:32:16 -05001067 const bool is_sync = rw_is_sync(op, op_flags) != 0;
Tejun Heo75eb6c32011-10-19 14:31:22 +02001068 int may_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Bart Van Assche3f3299d2012-11-28 13:42:38 +01001070 if (unlikely(blk_queue_dying(q)))
Joe Lawrencea492f072014-08-28 08:15:21 -06001071 return ERR_PTR(-ENODEV);
Tejun Heoda8303c2011-10-19 14:33:05 +02001072
Mike Christieba568ea2016-06-05 14:32:13 -05001073 may_queue = elv_may_queue(q, op, op_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001074 if (may_queue == ELV_MQUEUE_NO)
1075 goto rq_starved;
1076
Jens Axboe1faa16d2009-04-06 14:48:01 +02001077 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
1078 if (rl->count[is_sync]+1 >= q->nr_requests) {
Tejun Heof2dbd762011-12-14 00:33:40 +01001079 /*
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001080 * The queue will fill after this allocation, so set
1081 * it as full, and mark this process as "batching".
1082 * This process will be allowed to complete a batch of
1083 * requests, others will be blocked.
1084 */
Tejun Heo5b788ce2012-06-04 20:40:59 -07001085 if (!blk_rl_full(rl, is_sync)) {
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001086 ioc_set_batching(q, ioc);
Tejun Heo5b788ce2012-06-04 20:40:59 -07001087 blk_set_rl_full(rl, is_sync);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001088 } else {
1089 if (may_queue != ELV_MQUEUE_MUST
1090 && !ioc_batching(q, ioc)) {
1091 /*
1092 * The queue is full and the allocating
1093 * process is not a "batcher", and not
1094 * exempted by the IO scheduler
1095 */
Joe Lawrencea492f072014-08-28 08:15:21 -06001096 return ERR_PTR(-ENOMEM);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001097 }
1098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Tejun Heod40f75a2015-05-22 17:13:42 -04001100 blk_set_congested(rl, is_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
Jens Axboe082cf692005-06-28 16:35:11 +02001103 /*
1104 * Only allow batching queuers to allocate up to 50% over the defined
1105 * limit of requests, otherwise we could have thousands of requests
1106 * allocated with any setting of ->nr_requests
1107 */
Jens Axboe1faa16d2009-04-06 14:48:01 +02001108 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
Joe Lawrencea492f072014-08-28 08:15:21 -06001109 return ERR_PTR(-ENOMEM);
Hugh Dickinsfd782a42005-06-29 15:15:40 +01001110
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001111 q->nr_rqs[is_sync]++;
Jens Axboe1faa16d2009-04-06 14:48:01 +02001112 rl->count[is_sync]++;
1113 rl->starved[is_sync] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02001114
Tejun Heof1f8cc92011-12-14 00:33:42 +01001115 /*
1116 * Decide whether the new request will be managed by elevator. If
Mike Christiee6a40b02016-06-05 14:32:11 -05001117 * so, mark @op_flags and increment elvpriv. Non-zero elvpriv will
Tejun Heof1f8cc92011-12-14 00:33:42 +01001118 * prevent the current elevator from being destroyed until the new
1119 * request is freed. This guarantees icq's won't be destroyed and
1120 * makes creating new ones safe.
1121 *
1122 * Also, lookup icq while holding queue_lock. If it doesn't exist,
1123 * it will be created after releasing queue_lock.
1124 */
Tejun Heod7325802012-03-05 13:14:58 -08001125 if (blk_rq_should_init_elevator(bio) && !blk_queue_bypass(q)) {
Mike Christiee6a40b02016-06-05 14:32:11 -05001126 op_flags |= REQ_ELVPRIV;
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001127 q->nr_rqs_elvpriv++;
Tejun Heof1f8cc92011-12-14 00:33:42 +01001128 if (et->icq_cache && ioc)
1129 icq = ioc_lookup_icq(ioc, q);
Mike Snitzer9d5a4e92011-02-11 11:05:46 +01001130 }
Tejun Heocb98fc82005-10-28 08:29:39 +02001131
Jens Axboef253b862010-10-24 22:06:02 +02001132 if (blk_queue_io_stat(q))
Mike Christiee6a40b02016-06-05 14:32:11 -05001133 op_flags |= REQ_IO_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 spin_unlock_irq(q->queue_lock);
1135
Tejun Heo29e2b092012-04-19 16:29:21 -07001136 /* allocate and init request */
Tejun Heo5b788ce2012-06-04 20:40:59 -07001137 rq = mempool_alloc(rl->rq_pool, gfp_mask);
Tejun Heo29e2b092012-04-19 16:29:21 -07001138 if (!rq)
Tejun Heob6792812012-03-05 13:15:23 -08001139 goto fail_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Tejun Heo29e2b092012-04-19 16:29:21 -07001141 blk_rq_init(q, rq);
Tejun Heoa0516612012-06-26 15:05:44 -07001142 blk_rq_set_rl(rq, rl);
Mike Christiee6a40b02016-06-05 14:32:11 -05001143 req_set_op_attrs(rq, op, op_flags | REQ_ALLOCED);
Tejun Heo29e2b092012-04-19 16:29:21 -07001144
Tejun Heoaaf7c682012-04-19 16:29:22 -07001145 /* init elvpriv */
Mike Christiee6a40b02016-06-05 14:32:11 -05001146 if (op_flags & REQ_ELVPRIV) {
Tejun Heoaaf7c682012-04-19 16:29:22 -07001147 if (unlikely(et->icq_cache && !icq)) {
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001148 if (ioc)
1149 icq = ioc_create_icq(ioc, q, gfp_mask);
Tejun Heoaaf7c682012-04-19 16:29:22 -07001150 if (!icq)
1151 goto fail_elvpriv;
Tejun Heo29e2b092012-04-19 16:29:21 -07001152 }
Tejun Heoaaf7c682012-04-19 16:29:22 -07001153
1154 rq->elv.icq = icq;
1155 if (unlikely(elv_set_request(q, rq, bio, gfp_mask)))
1156 goto fail_elvpriv;
1157
1158 /* @rq->elv.icq holds io_context until @rq is freed */
Tejun Heo29e2b092012-04-19 16:29:21 -07001159 if (icq)
1160 get_io_context(icq->ioc);
1161 }
Tejun Heoaaf7c682012-04-19 16:29:22 -07001162out:
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001163 /*
1164 * ioc may be NULL here, and ioc_batching will be false. That's
1165 * OK, if the queue is under the request limit then requests need
1166 * not count toward the nr_batch_requests limit. There will always
1167 * be some limit enforced by BLK_BATCH_TIME.
1168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (ioc_batching(q, ioc))
1170 ioc->nr_batch_requests--;
Jens Axboe6728cb02008-01-31 13:03:55 +01001171
Mike Christiee6a40b02016-06-05 14:32:11 -05001172 trace_block_getrq(q, bio, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return rq;
Tejun Heob6792812012-03-05 13:15:23 -08001174
Tejun Heoaaf7c682012-04-19 16:29:22 -07001175fail_elvpriv:
1176 /*
1177 * elvpriv init failed. ioc, icq and elvpriv aren't mempool backed
1178 * and may fail indefinitely under memory pressure and thus
1179 * shouldn't stall IO. Treat this request as !elvpriv. This will
1180 * disturb iosched and blkcg but weird is bettern than dead.
1181 */
Robert Elliott7b2b10e2014-08-27 10:50:36 -05001182 printk_ratelimited(KERN_WARNING "%s: dev %s: request aux data allocation failed, iosched may be disturbed\n",
Jan Kara1cdb7f92017-02-02 15:56:50 +01001183 __func__, dev_name(q->backing_dev_info->dev));
Tejun Heoaaf7c682012-04-19 16:29:22 -07001184
1185 rq->cmd_flags &= ~REQ_ELVPRIV;
1186 rq->elv.icq = NULL;
1187
1188 spin_lock_irq(q->queue_lock);
Tejun Heo8a5ecdd2012-06-04 20:40:58 -07001189 q->nr_rqs_elvpriv--;
Tejun Heoaaf7c682012-04-19 16:29:22 -07001190 spin_unlock_irq(q->queue_lock);
1191 goto out;
1192
Tejun Heob6792812012-03-05 13:15:23 -08001193fail_alloc:
1194 /*
1195 * Allocation failed presumably due to memory. Undo anything we
1196 * might have messed up.
1197 *
1198 * Allocating task should really be put onto the front of the wait
1199 * queue, but this is pretty rare.
1200 */
1201 spin_lock_irq(q->queue_lock);
Mike Christiee6a40b02016-06-05 14:32:11 -05001202 freed_request(rl, op, op_flags);
Tejun Heob6792812012-03-05 13:15:23 -08001203
1204 /*
1205 * in the very unlikely event that allocation failed and no
1206 * requests for this direction was pending, mark us starved so that
1207 * freeing of a request in the other direction will notice
1208 * us. another possible fix would be to split the rq mempool into
1209 * READ and WRITE
1210 */
1211rq_starved:
1212 if (unlikely(rl->count[is_sync] == 0))
1213 rl->starved[is_sync] = 1;
Joe Lawrencea492f072014-08-28 08:15:21 -06001214 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Tejun Heoda8303c2011-10-19 14:33:05 +02001217/**
Tejun Heoa06e05e2012-06-04 20:40:55 -07001218 * get_request - get a free request
Tejun Heoda8303c2011-10-19 14:33:05 +02001219 * @q: request_queue to allocate request from
Mike Christiee6a40b02016-06-05 14:32:11 -05001220 * @op: REQ_OP_READ/REQ_OP_WRITE
1221 * @op_flags: rq_flag_bits
Tejun Heoda8303c2011-10-19 14:33:05 +02001222 * @bio: bio to allocate request for (can be %NULL)
Tejun Heoa06e05e2012-06-04 20:40:55 -07001223 * @gfp_mask: allocation mask
Nick Piggind6344532005-06-28 20:45:14 -07001224 *
Mel Gormand0164ad2015-11-06 16:28:21 -08001225 * Get a free request from @q. If %__GFP_DIRECT_RECLAIM is set in @gfp_mask,
1226 * this function keeps retrying under memory pressure and fails iff @q is dead.
Tejun Heoda8303c2011-10-19 14:33:05 +02001227 *
Masanari Iidada3dae52014-09-09 01:27:23 +09001228 * Must be called with @q->queue_lock held and,
Joe Lawrencea492f072014-08-28 08:15:21 -06001229 * Returns ERR_PTR on failure, with @q->queue_lock held.
1230 * Returns request pointer on success, with @q->queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
Mike Christiee6a40b02016-06-05 14:32:11 -05001232static struct request *get_request(struct request_queue *q, int op,
1233 int op_flags, struct bio *bio,
1234 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Mike Christied9d8c5c2016-06-05 14:32:16 -05001236 const bool is_sync = rw_is_sync(op, op_flags) != 0;
Tejun Heoa06e05e2012-06-04 20:40:55 -07001237 DEFINE_WAIT(wait);
Tejun Heoa0516612012-06-26 15:05:44 -07001238 struct request_list *rl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 struct request *rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001240
1241 rl = blk_get_rl(q, bio); /* transferred to @rq on success */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001242retry:
Mike Christiee6a40b02016-06-05 14:32:11 -05001243 rq = __get_request(rl, op, op_flags, bio, gfp_mask);
Joe Lawrencea492f072014-08-28 08:15:21 -06001244 if (!IS_ERR(rq))
Tejun Heoa06e05e2012-06-04 20:40:55 -07001245 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Mel Gormand0164ad2015-11-06 16:28:21 -08001247 if (!gfpflags_allow_blocking(gfp_mask) || unlikely(blk_queue_dying(q))) {
Tejun Heoa0516612012-06-26 15:05:44 -07001248 blk_put_rl(rl);
Joe Lawrencea492f072014-08-28 08:15:21 -06001249 return rq;
Tejun Heoa0516612012-06-26 15:05:44 -07001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Tejun Heoa06e05e2012-06-04 20:40:55 -07001252 /* wait on @rl and retry */
1253 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
1254 TASK_UNINTERRUPTIBLE);
Tejun Heoda8303c2011-10-19 14:33:05 +02001255
Mike Christiee6a40b02016-06-05 14:32:11 -05001256 trace_block_sleeprq(q, bio, op);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Tejun Heoa06e05e2012-06-04 20:40:55 -07001258 spin_unlock_irq(q->queue_lock);
1259 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Tejun Heoa06e05e2012-06-04 20:40:55 -07001261 /*
1262 * After sleeping, we become a "batching" process and will be able
1263 * to allocate at least one request, and up to a big batch of them
1264 * for a small period time. See ioc_batching, ioc_set_batching
1265 */
Tejun Heoa06e05e2012-06-04 20:40:55 -07001266 ioc_set_batching(q, current->io_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Tejun Heoa06e05e2012-06-04 20:40:55 -07001268 spin_lock_irq(q->queue_lock);
1269 finish_wait(&rl->wait[is_sync], &wait);
Jens Axboe2056a782006-03-23 20:00:26 +01001270
Tejun Heoa06e05e2012-06-04 20:40:55 -07001271 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Jens Axboe320ae512013-10-24 09:20:05 +01001274static struct request *blk_old_get_request(struct request_queue *q, int rw,
1275 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 struct request *rq;
1278
1279 BUG_ON(rw != READ && rw != WRITE);
1280
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001281 /* create ioc upfront */
1282 create_io_context(gfp_mask, q->node);
1283
Nick Piggind6344532005-06-28 20:45:14 -07001284 spin_lock_irq(q->queue_lock);
Mike Christiee6a40b02016-06-05 14:32:11 -05001285 rq = get_request(q, rw, 0, NULL, gfp_mask);
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +02001286 if (IS_ERR(rq)) {
Tejun Heoda8303c2011-10-19 14:33:05 +02001287 spin_unlock_irq(q->queue_lock);
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +02001288 return rq;
1289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +02001291 /* q->queue_lock is unlocked at this point */
1292 rq->__data_len = 0;
1293 rq->__sector = (sector_t) -1;
1294 rq->bio = rq->biotail = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return rq;
1296}
Jens Axboe320ae512013-10-24 09:20:05 +01001297
1298struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
1299{
1300 if (q->mq_ops)
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01001301 return blk_mq_alloc_request(q, rw,
1302 (gfp_mask & __GFP_DIRECT_RECLAIM) ?
1303 0 : BLK_MQ_REQ_NOWAIT);
Jens Axboe320ae512013-10-24 09:20:05 +01001304 else
1305 return blk_old_get_request(q, rw, gfp_mask);
1306}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307EXPORT_SYMBOL(blk_get_request);
1308
1309/**
Masanari Iidada3dae52014-09-09 01:27:23 +09001310 * blk_rq_set_block_pc - initialize a request to type BLOCK_PC
Jens Axboef27b0872014-06-06 07:57:37 -06001311 * @rq: request to be initialized
1312 *
1313 */
1314void blk_rq_set_block_pc(struct request *rq)
1315{
1316 rq->cmd_type = REQ_TYPE_BLOCK_PC;
Jens Axboef27b0872014-06-06 07:57:37 -06001317 memset(rq->__cmd, 0, sizeof(rq->__cmd));
Jens Axboef27b0872014-06-06 07:57:37 -06001318}
1319EXPORT_SYMBOL(blk_rq_set_block_pc);
1320
1321/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 * blk_requeue_request - put a request back on queue
1323 * @q: request queue where request should be inserted
1324 * @rq: request to be inserted
1325 *
1326 * Description:
1327 * Drivers often keep queueing requests until the hardware cannot accept
1328 * more, when that condition happens we need to put the request back
1329 * on the queue. Must be called with queue lock held.
1330 */
Jens Axboe165125e2007-07-24 09:28:11 +02001331void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Jens Axboe242f9dc2008-09-14 05:55:09 -07001333 blk_delete_timer(rq);
1334 blk_clear_rq_complete(rq);
Arnaldo Carvalho de Melo5f3ea372008-10-30 08:34:33 +01001335 trace_block_rq_requeue(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001336
Christoph Hellwig125c99b2014-11-03 12:47:47 +01001337 if (rq->cmd_flags & REQ_QUEUED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 blk_queue_end_tag(q, rq);
1339
James Bottomleyba396a62009-05-27 14:17:08 +02001340 BUG_ON(blk_queued_rq(rq));
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 elv_requeue_request(q, rq);
1343}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344EXPORT_SYMBOL(blk_requeue_request);
1345
Jens Axboe73c10102011-03-08 13:19:51 +01001346static void add_acct_request(struct request_queue *q, struct request *rq,
1347 int where)
1348{
Jens Axboe320ae512013-10-24 09:20:05 +01001349 blk_account_io_start(rq, true);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001350 __elv_add_request(q, rq, where);
Jens Axboe73c10102011-03-08 13:19:51 +01001351}
1352
Tejun Heo074a7ac2008-08-25 19:56:14 +09001353static void part_round_stats_single(int cpu, struct hd_struct *part,
1354 unsigned long now)
1355{
Jens Axboe7276d022014-05-09 15:48:23 -06001356 int inflight;
1357
Tejun Heo074a7ac2008-08-25 19:56:14 +09001358 if (now == part->stamp)
1359 return;
1360
Jens Axboe7276d022014-05-09 15:48:23 -06001361 inflight = part_in_flight(part);
1362 if (inflight) {
Tejun Heo074a7ac2008-08-25 19:56:14 +09001363 __part_stat_add(cpu, part, time_in_queue,
Jens Axboe7276d022014-05-09 15:48:23 -06001364 inflight * (now - part->stamp));
Tejun Heo074a7ac2008-08-25 19:56:14 +09001365 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1366 }
1367 part->stamp = now;
1368}
1369
1370/**
Randy Dunlap496aa8a2008-10-16 07:46:23 +02001371 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1372 * @cpu: cpu number for stats access
1373 * @part: target partition
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 *
1375 * The average IO queue length and utilisation statistics are maintained
1376 * by observing the current state of the queue length and the amount of
1377 * time it has been in this state for.
1378 *
1379 * Normally, that accounting is done on IO completion, but that can result
1380 * in more than a second's worth of IO being accounted for within any one
1381 * second, leading to >100% utilisation. To deal with that, we call this
1382 * function to do a round-off before returning the results when reading
1383 * /proc/diskstats. This accounts immediately for all queue usage up to
1384 * the current jiffies and restarts the counters again.
1385 */
Tejun Heoc9959052008-08-25 19:47:21 +09001386void part_round_stats(int cpu, struct hd_struct *part)
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001387{
1388 unsigned long now = jiffies;
1389
Tejun Heo074a7ac2008-08-25 19:56:14 +09001390 if (part->partno)
1391 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1392 part_round_stats_single(cpu, part, now);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001393}
Tejun Heo074a7ac2008-08-25 19:56:14 +09001394EXPORT_SYMBOL_GPL(part_round_stats);
Jerome Marchand6f2576a2008-02-08 11:04:35 +01001395
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01001396#ifdef CONFIG_PM
Lin Mingc8158812013-03-23 11:42:27 +08001397static void blk_pm_put_request(struct request *rq)
1398{
1399 if (rq->q->dev && !(rq->cmd_flags & REQ_PM) && !--rq->q->nr_pending)
1400 pm_runtime_mark_last_busy(rq->q->dev);
1401}
1402#else
1403static inline void blk_pm_put_request(struct request *rq) {}
1404#endif
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406/*
1407 * queue lock must be held
1408 */
Jens Axboe165125e2007-07-24 09:28:11 +02001409void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (unlikely(!q))
1412 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Christoph Hellwig6f5ba582014-02-07 10:22:37 -08001414 if (q->mq_ops) {
1415 blk_mq_free_request(req);
1416 return;
1417 }
1418
Lin Mingc8158812013-03-23 11:42:27 +08001419 blk_pm_put_request(req);
1420
Tejun Heo8922e162005-10-20 16:23:44 +02001421 elv_completed_request(q, req);
1422
Boaz Harrosh1cd96c22009-03-24 12:35:07 +01001423 /* this is a bio leak */
1424 WARN_ON(req->bio != NULL);
1425
AnilKumar Chimata7214d7e2017-06-23 03:09:59 -07001426 /* this is a bio leak if the bio is not tagged with BIO_DONTFREE */
1427 WARN_ON(req->bio && !bio_flagged(req->bio, BIO_DONTFREE));
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /*
1430 * Request may not have originated from ll_rw_blk. if not,
1431 * it didn't come out of our reserved rq pools
1432 */
Jens Axboe49171e52006-08-10 08:59:11 +02001433 if (req->cmd_flags & REQ_ALLOCED) {
Tejun Heo75eb6c32011-10-19 14:31:22 +02001434 unsigned int flags = req->cmd_flags;
Mike Christiee6a40b02016-06-05 14:32:11 -05001435 int op = req_op(req);
Tejun Heoa0516612012-06-26 15:05:44 -07001436 struct request_list *rl = blk_rq_rl(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe360f92c2014-04-09 20:27:01 -06001439 BUG_ON(ELV_ON_HASH(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Tejun Heoa0516612012-06-26 15:05:44 -07001441 blk_free_request(rl, req);
Mike Christiee6a40b02016-06-05 14:32:11 -05001442 freed_request(rl, op, flags);
Tejun Heoa0516612012-06-26 15:05:44 -07001443 blk_put_rl(rl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445}
Mike Christie6e39b69e2005-11-11 05:30:24 -06001446EXPORT_SYMBOL_GPL(__blk_put_request);
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448void blk_put_request(struct request *req)
1449{
Jens Axboe165125e2007-07-24 09:28:11 +02001450 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Jens Axboe320ae512013-10-24 09:20:05 +01001452 if (q->mq_ops)
1453 blk_mq_free_request(req);
1454 else {
1455 unsigned long flags;
1456
1457 spin_lock_irqsave(q->queue_lock, flags);
1458 __blk_put_request(q, req);
1459 spin_unlock_irqrestore(q->queue_lock, flags);
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462EXPORT_SYMBOL(blk_put_request);
1463
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001464/**
1465 * blk_add_request_payload - add a payload to a request
1466 * @rq: request to update
1467 * @page: page backing the payload
Ming Lin37e58232016-03-22 00:24:44 -07001468 * @offset: offset in page
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001469 * @len: length of the payload.
1470 *
1471 * This allows to later add a payload to an already submitted request by
1472 * a block driver. The driver needs to take care of freeing the payload
1473 * itself.
1474 *
1475 * Note that this is a quite horrible hack and nothing but handling of
1476 * discard requests should ever use it.
1477 */
1478void blk_add_request_payload(struct request *rq, struct page *page,
Ming Lin37e58232016-03-22 00:24:44 -07001479 int offset, unsigned int len)
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001480{
1481 struct bio *bio = rq->bio;
1482
1483 bio->bi_io_vec->bv_page = page;
Ming Lin37e58232016-03-22 00:24:44 -07001484 bio->bi_io_vec->bv_offset = offset;
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001485 bio->bi_io_vec->bv_len = len;
1486
Kent Overstreet4f024f32013-10-11 15:44:27 -07001487 bio->bi_iter.bi_size = len;
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001488 bio->bi_vcnt = 1;
1489 bio->bi_phys_segments = 1;
1490
1491 rq->__data_len = rq->resid_len = len;
1492 rq->nr_phys_segments = 1;
Christoph Hellwig66ac0282010-06-18 16:59:42 +02001493}
1494EXPORT_SYMBOL_GPL(blk_add_request_payload);
1495
Jens Axboe320ae512013-10-24 09:20:05 +01001496bool bio_attempt_back_merge(struct request_queue *q, struct request *req,
1497 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001498{
Jens Axboe1eff9d32016-08-05 15:35:16 -06001499 const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001500
Jens Axboe73c10102011-03-08 13:19:51 +01001501 if (!ll_back_merge_fn(q, req, bio))
1502 return false;
1503
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001504 trace_block_bio_backmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001505
1506 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1507 blk_rq_set_mixed_merge(req);
1508
1509 req->biotail->bi_next = bio;
1510 req->biotail = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001511 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001512 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1513
Jens Axboe320ae512013-10-24 09:20:05 +01001514 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001515 return true;
1516}
1517
Jens Axboe320ae512013-10-24 09:20:05 +01001518bool bio_attempt_front_merge(struct request_queue *q, struct request *req,
1519 struct bio *bio)
Jens Axboe73c10102011-03-08 13:19:51 +01001520{
Jens Axboe1eff9d32016-08-05 15:35:16 -06001521 const int ff = bio->bi_opf & REQ_FAILFAST_MASK;
Jens Axboe73c10102011-03-08 13:19:51 +01001522
Jens Axboe73c10102011-03-08 13:19:51 +01001523 if (!ll_front_merge_fn(q, req, bio))
1524 return false;
1525
Tejun Heo8c1cf6b2013-01-11 13:06:34 -08001526 trace_block_bio_frontmerge(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001527
1528 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1529 blk_rq_set_mixed_merge(req);
1530
Jens Axboe73c10102011-03-08 13:19:51 +01001531 bio->bi_next = req->bio;
1532 req->bio = bio;
1533
Jaegeuk Kim5190c812018-04-22 16:05:02 -07001534#ifdef CONFIG_PFK
Jaegeuk Kim84680312018-01-03 21:22:07 -08001535 WARN_ON(req->__dun || bio->bi_iter.bi_dun);
Jaegeuk Kim5190c812018-04-22 16:05:02 -07001536#endif
Kent Overstreet4f024f32013-10-11 15:44:27 -07001537 req->__sector = bio->bi_iter.bi_sector;
1538 req->__data_len += bio->bi_iter.bi_size;
Jens Axboe73c10102011-03-08 13:19:51 +01001539 req->ioprio = ioprio_best(req->ioprio, bio_prio(bio));
1540
Jens Axboe320ae512013-10-24 09:20:05 +01001541 blk_account_io_start(req, false);
Jens Axboe73c10102011-03-08 13:19:51 +01001542 return true;
1543}
1544
Tejun Heobd87b582011-10-19 14:33:08 +02001545/**
Jens Axboe320ae512013-10-24 09:20:05 +01001546 * blk_attempt_plug_merge - try to merge with %current's plugged list
Tejun Heobd87b582011-10-19 14:33:08 +02001547 * @q: request_queue new bio is being queued at
1548 * @bio: new bio being queued
1549 * @request_count: out parameter for number of traversed plugged requests
Randy Dunlapccc26002015-10-30 18:36:16 -07001550 * @same_queue_rq: pointer to &struct request that gets filled in when
1551 * another request associated with @q is found on the plug list
1552 * (optional, may be %NULL)
Tejun Heobd87b582011-10-19 14:33:08 +02001553 *
1554 * Determine whether @bio being queued on @q can be merged with a request
1555 * on %current's plugged list. Returns %true if merge was successful,
1556 * otherwise %false.
1557 *
Tejun Heo07c2bd32012-02-08 09:19:42 +01001558 * Plugging coalesces IOs from the same issuer for the same purpose without
1559 * going through @q->queue_lock. As such it's more of an issuing mechanism
1560 * than scheduling, and the request, while may have elvpriv data, is not
1561 * added on the elevator at this point. In addition, we don't have
1562 * reliable access to the elevator outside queue lock. Only check basic
1563 * merging parameters without querying the elevator.
Robert Elliottda41a582014-05-20 16:46:26 -05001564 *
1565 * Caller must ensure !blk_queue_nomerges(q) beforehand.
Jens Axboe73c10102011-03-08 13:19:51 +01001566 */
Jens Axboe320ae512013-10-24 09:20:05 +01001567bool blk_attempt_plug_merge(struct request_queue *q, struct bio *bio,
Shaohua Li5b3f3412015-05-08 10:51:33 -07001568 unsigned int *request_count,
1569 struct request **same_queue_rq)
Jens Axboe73c10102011-03-08 13:19:51 +01001570{
1571 struct blk_plug *plug;
1572 struct request *rq;
1573 bool ret = false;
Shaohua Li92f399c2013-10-29 12:01:03 -06001574 struct list_head *plug_list;
Jens Axboe73c10102011-03-08 13:19:51 +01001575
Tejun Heobd87b582011-10-19 14:33:08 +02001576 plug = current->plug;
Jens Axboe73c10102011-03-08 13:19:51 +01001577 if (!plug)
1578 goto out;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001579 *request_count = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01001580
Shaohua Li92f399c2013-10-29 12:01:03 -06001581 if (q->mq_ops)
1582 plug_list = &plug->mq_list;
1583 else
1584 plug_list = &plug->list;
1585
1586 list_for_each_entry_reverse(rq, plug_list, queuelist) {
Jens Axboe73c10102011-03-08 13:19:51 +01001587 int el_ret;
1588
Shaohua Li5b3f3412015-05-08 10:51:33 -07001589 if (rq->q == q) {
Shaohua Li1b2e19f2012-04-06 11:37:47 -06001590 (*request_count)++;
Shaohua Li5b3f3412015-05-08 10:51:33 -07001591 /*
1592 * Only blk-mq multiple hardware queues case checks the
1593 * rq in the same queue, there should be only one such
1594 * rq in a queue
1595 **/
1596 if (same_queue_rq)
1597 *same_queue_rq = rq;
1598 }
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001599
Tejun Heo07c2bd32012-02-08 09:19:42 +01001600 if (rq->q != q || !blk_rq_merge_ok(rq, bio))
Jens Axboe73c10102011-03-08 13:19:51 +01001601 continue;
1602
Tejun Heo050c8ea2012-02-08 09:19:38 +01001603 el_ret = blk_try_merge(rq, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001604 if (el_ret == ELEVATOR_BACK_MERGE) {
1605 ret = bio_attempt_back_merge(q, rq, bio);
1606 if (ret)
1607 break;
1608 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
1609 ret = bio_attempt_front_merge(q, rq, bio);
1610 if (ret)
1611 break;
1612 }
1613 }
1614out:
1615 return ret;
1616}
1617
Jeff Moyer0809e3a2015-10-20 23:13:51 +08001618unsigned int blk_plug_queued_count(struct request_queue *q)
1619{
1620 struct blk_plug *plug;
1621 struct request *rq;
1622 struct list_head *plug_list;
1623 unsigned int ret = 0;
1624
1625 plug = current->plug;
1626 if (!plug)
1627 goto out;
1628
1629 if (q->mq_ops)
1630 plug_list = &plug->mq_list;
1631 else
1632 plug_list = &plug->list;
1633
1634 list_for_each_entry(rq, plug_list, queuelist) {
1635 if (rq->q == q)
1636 ret++;
1637 }
1638out:
1639 return ret;
1640}
1641
Jens Axboe86db1e22008-01-29 14:53:40 +01001642void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001643{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001644 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001645
Jens Axboe1eff9d32016-08-05 15:35:16 -06001646 req->cmd_flags |= bio->bi_opf & REQ_COMMON_MASK;
1647 if (bio->bi_opf & REQ_RAHEAD)
Tejun Heoa82afdf2009-07-03 17:48:16 +09001648 req->cmd_flags |= REQ_FAILFAST_MASK;
Jens Axboeb31dc662006-06-13 08:26:10 +02001649
Tejun Heo52d9e672006-01-06 09:49:58 +01001650 req->errors = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001651 req->__sector = bio->bi_iter.bi_sector;
Jaegeuk Kim5190c812018-04-22 16:05:02 -07001652#ifdef CONFIG_PFK
Jaegeuk Kim84680312018-01-03 21:22:07 -08001653 req->__dun = bio->bi_iter.bi_dun;
Jaegeuk Kim5190c812018-04-22 16:05:02 -07001654#endif
Tejun Heo52d9e672006-01-06 09:49:58 +01001655 req->ioprio = bio_prio(bio);
NeilBrownbc1c56f2007-08-16 13:31:30 +02001656 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001657}
1658
Jens Axboedece1632015-11-05 10:41:16 -07001659static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Jens Axboe1eff9d32016-08-05 15:35:16 -06001661 const bool sync = !!(bio->bi_opf & REQ_SYNC);
Jens Axboe73c10102011-03-08 13:19:51 +01001662 struct blk_plug *plug;
Mike Christiee6a40b02016-06-05 14:32:11 -05001663 int el_ret, rw_flags = 0, where = ELEVATOR_INSERT_SORT;
Jens Axboe73c10102011-03-08 13:19:51 +01001664 struct request *req;
Shaohua Li56ebdaf2011-08-24 16:04:34 +02001665 unsigned int request_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 /*
1668 * low level driver can indicate that it wants pages above a
1669 * certain limit bounced to low memory (ie for highmem, or even
1670 * ISA dma in theory)
1671 */
1672 blk_queue_bounce(q, &bio);
1673
Junichi Nomura23688bf2015-12-22 10:23:44 -07001674 blk_queue_split(q, &bio, q->bio_split);
1675
Darrick J. Wongffecfd12013-02-21 16:42:55 -08001676 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001677 bio->bi_error = -EIO;
1678 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001679 return BLK_QC_T_NONE;
Darrick J. Wongffecfd12013-02-21 16:42:55 -08001680 }
1681
Jens Axboe1eff9d32016-08-05 15:35:16 -06001682 if (bio->bi_opf & (REQ_PREFLUSH | REQ_FUA)) {
Jens Axboe73c10102011-03-08 13:19:51 +01001683 spin_lock_irq(q->queue_lock);
Tejun Heoae1b1532011-01-25 12:43:54 +01001684 where = ELEVATOR_INSERT_FLUSH;
Tejun Heo28e7d182010-09-03 11:56:16 +02001685 goto get_rq;
1686 }
1687
Jens Axboe73c10102011-03-08 13:19:51 +01001688 /*
1689 * Check if we can merge with the plugged list before grabbing
1690 * any locks.
1691 */
Jeff Moyer0809e3a2015-10-20 23:13:51 +08001692 if (!blk_queue_nomerges(q)) {
1693 if (blk_attempt_plug_merge(q, bio, &request_count, NULL))
Jens Axboedece1632015-11-05 10:41:16 -07001694 return BLK_QC_T_NONE;
Jeff Moyer0809e3a2015-10-20 23:13:51 +08001695 } else
1696 request_count = blk_plug_queued_count(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001697
1698 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 el_ret = elv_merge(q, &req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001701 if (el_ret == ELEVATOR_BACK_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001702 if (bio_attempt_back_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001703 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001704 if (!attempt_back_merge(q, req))
1705 elv_merged_request(q, req, el_ret);
1706 goto out_unlock;
Tejun Heo80a761f2009-07-03 17:48:17 +09001707 }
Jens Axboe73c10102011-03-08 13:19:51 +01001708 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
Jens Axboe73c10102011-03-08 13:19:51 +01001709 if (bio_attempt_front_merge(q, req, bio)) {
Tejun Heo07c2bd32012-02-08 09:19:42 +01001710 elv_bio_merged(q, req, bio);
Jens Axboe73c10102011-03-08 13:19:51 +01001711 if (!attempt_front_merge(q, req))
1712 elv_merged_request(q, req, el_ret);
1713 goto out_unlock;
1714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001718 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001719 * This sync check and mask will be re-done in init_request_from_bio(),
1720 * but we need to set it earlier to expose the sync flag to the
1721 * rq allocator and io schedulers.
1722 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001723 if (sync)
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001724 rw_flags |= REQ_SYNC;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001725
1726 /*
Jens Axboeb8269db2016-06-09 15:47:29 -06001727 * Add in META/PRIO flags, if set, before we get to the IO scheduler
1728 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06001729 rw_flags |= (bio->bi_opf & (REQ_META | REQ_PRIO));
Jens Axboeb8269db2016-06-09 15:47:29 -06001730
1731 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001732 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001733 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001734 */
Mike Christiee6a40b02016-06-05 14:32:11 -05001735 req = get_request(q, bio_data_dir(bio), rw_flags, bio, GFP_NOIO);
Joe Lawrencea492f072014-08-28 08:15:21 -06001736 if (IS_ERR(req)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001737 bio->bi_error = PTR_ERR(req);
1738 bio_endio(bio);
Tejun Heoda8303c2011-10-19 14:33:05 +02001739 goto out_unlock;
1740 }
Nick Piggind6344532005-06-28 20:45:14 -07001741
Nick Piggin450991b2005-06-28 20:45:13 -07001742 /*
1743 * After dropping the lock and possibly sleeping here, our request
1744 * may now be mergeable after it had proven unmergeable (above).
1745 * We don't worry about that case for efficiency. It won't happen
1746 * often, and the elevators are able to handle it.
1747 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001748 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Tao Ma9562ad92011-10-24 16:11:30 +02001750 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags))
Jens Axboe11ccf112011-07-26 15:01:15 +02001751 req->cpu = raw_smp_processor_id();
Tejun Heodd831002010-09-03 11:56:16 +02001752
Jens Axboe73c10102011-03-08 13:19:51 +01001753 plug = current->plug;
Jens Axboe721a9602011-03-09 11:56:30 +01001754 if (plug) {
Jens Axboedc6d36c2011-04-12 10:28:28 +02001755 /*
1756 * If this is the first request added after a plug, fire
Jianpeng Ma7aef2e72013-09-11 13:21:07 -06001757 * of a plug trace.
Jens Axboedc6d36c2011-04-12 10:28:28 +02001758 */
Jianpeng Ma7aef2e72013-09-11 13:21:07 -06001759 if (!request_count)
Jens Axboedc6d36c2011-04-12 10:28:28 +02001760 trace_block_plug(q);
Shaohua Li3540d5e2011-11-16 09:21:50 +01001761 else {
Shaohua Li019ceb72011-11-16 09:21:50 +01001762 if (request_count >= BLK_MAX_REQUEST_COUNT) {
Shaohua Li3540d5e2011-11-16 09:21:50 +01001763 blk_flush_plug_list(plug, false);
Shaohua Li019ceb72011-11-16 09:21:50 +01001764 trace_block_plug(q);
1765 }
Jens Axboe73c10102011-03-08 13:19:51 +01001766 }
Shaohua Lia6327162011-08-24 16:04:32 +02001767 list_add_tail(&req->queuelist, &plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01001768 blk_account_io_start(req, true);
Jens Axboe73c10102011-03-08 13:19:51 +01001769 } else {
1770 spin_lock_irq(q->queue_lock);
1771 add_acct_request(q, req, where);
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02001772 __blk_run_queue(q);
Jens Axboe73c10102011-03-08 13:19:51 +01001773out_unlock:
1774 spin_unlock_irq(q->queue_lock);
1775 }
Jens Axboedece1632015-11-05 10:41:16 -07001776
1777 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
1780/*
1781 * If bio->bi_dev is a partition, remap the location
1782 */
1783static inline void blk_partition_remap(struct bio *bio)
1784{
1785 struct block_device *bdev = bio->bi_bdev;
1786
Jens Axboebf2de6f2007-09-27 13:01:25 +02001787 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001789
Kent Overstreet4f024f32013-10-11 15:44:27 -07001790 bio->bi_iter.bi_sector += p->start_sect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001792
Mike Snitzerd07335e2010-11-16 12:52:38 +01001793 trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio,
1794 bdev->bd_dev,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001795 bio->bi_iter.bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797}
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799static void handle_bad_sector(struct bio *bio)
1800{
1801 char b[BDEVNAME_SIZE];
1802
1803 printk(KERN_INFO "attempt to access beyond end of device\n");
Mike Christie6296b962016-06-05 14:32:21 -05001804 printk(KERN_INFO "%s: rw=%d, want=%Lu, limit=%Lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 bdevname(bio->bi_bdev, b),
Jens Axboe1eff9d32016-08-05 15:35:16 -06001806 bio->bi_opf,
Kent Overstreetf73a1c72012-09-25 15:05:12 -07001807 (unsigned long long)bio_end_sector(bio),
Mike Snitzer77304d22010-11-08 14:39:12 +01001808 (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
Akinobu Mitac17bb492006-12-08 02:39:46 -08001811#ifdef CONFIG_FAIL_MAKE_REQUEST
1812
1813static DECLARE_FAULT_ATTR(fail_make_request);
1814
1815static int __init setup_fail_make_request(char *str)
1816{
1817 return setup_fault_attr(&fail_make_request, str);
1818}
1819__setup("fail_make_request=", setup_fail_make_request);
1820
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001821static bool should_fail_request(struct hd_struct *part, unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001822{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001823 return part->make_it_fail && should_fail(&fail_make_request, bytes);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001824}
1825
1826static int __init fail_make_request_debugfs(void)
1827{
Akinobu Mitadd48c082011-08-03 16:21:01 -07001828 struct dentry *dir = fault_create_debugfs_attr("fail_make_request",
1829 NULL, &fail_make_request);
1830
Duan Jiong21f9fcd2014-04-11 15:58:56 +08001831 return PTR_ERR_OR_ZERO(dir);
Akinobu Mitac17bb492006-12-08 02:39:46 -08001832}
1833
1834late_initcall(fail_make_request_debugfs);
1835
1836#else /* CONFIG_FAIL_MAKE_REQUEST */
1837
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001838static inline bool should_fail_request(struct hd_struct *part,
1839 unsigned int bytes)
Akinobu Mitac17bb492006-12-08 02:39:46 -08001840{
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07001841 return false;
Akinobu Mitac17bb492006-12-08 02:39:46 -08001842}
1843
1844#endif /* CONFIG_FAIL_MAKE_REQUEST */
1845
Jens Axboec07e2b42007-07-18 13:27:58 +02001846/*
1847 * Check whether this bio extends beyond the end of the device.
1848 */
1849static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1850{
1851 sector_t maxsector;
1852
1853 if (!nr_sectors)
1854 return 0;
1855
1856 /* Test device or partition size, when known. */
Mike Snitzer77304d22010-11-08 14:39:12 +01001857 maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9;
Jens Axboec07e2b42007-07-18 13:27:58 +02001858 if (maxsector) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07001859 sector_t sector = bio->bi_iter.bi_sector;
Jens Axboec07e2b42007-07-18 13:27:58 +02001860
1861 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1862 /*
1863 * This may well happen - the kernel calls bread()
1864 * without checking the size of the device, e.g., when
1865 * mounting a device.
1866 */
1867 handle_bad_sector(bio);
1868 return 1;
1869 }
1870 }
1871
1872 return 0;
1873}
1874
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001875static noinline_for_stack bool
1876generic_make_request_checks(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Jens Axboe165125e2007-07-24 09:28:11 +02001878 struct request_queue *q;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001879 int nr_sectors = bio_sectors(bio);
Jens Axboe51fd77b2007-11-02 08:49:08 +01001880 int err = -EIO;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001881 char b[BDEVNAME_SIZE];
1882 struct hd_struct *part;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Jens Axboec07e2b42007-07-18 13:27:58 +02001886 if (bio_check_eod(bio, nr_sectors))
1887 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001889 q = bdev_get_queue(bio->bi_bdev);
1890 if (unlikely(!q)) {
1891 printk(KERN_ERR
1892 "generic_make_request: Trying to access "
1893 "nonexistent block-device %s (%Lu)\n",
1894 bdevname(bio->bi_bdev, b),
Kent Overstreet4f024f32013-10-11 15:44:27 -07001895 (long long) bio->bi_iter.bi_sector);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001896 goto end_io;
1897 }
1898
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001899 part = bio->bi_bdev->bd_part;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001900 if (should_fail_request(part, bio->bi_iter.bi_size) ||
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001901 should_fail_request(&part_to_disk(part)->part0,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001902 bio->bi_iter.bi_size))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001903 goto end_io;
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 /*
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001906 * If this device has partitions, remap block n
1907 * of partition p to block n+start(p) of the disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 */
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001909 blk_partition_remap(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001911 if (bio_check_eod(bio, nr_sectors))
1912 goto end_io;
1913
1914 /*
1915 * Filter flush bio's early so that make_request based
1916 * drivers without flush support don't have to worry
1917 * about them.
1918 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06001919 if ((bio->bi_opf & (REQ_PREFLUSH | REQ_FUA)) &&
Jens Axboec888a8f2016-04-13 13:33:19 -06001920 !test_bit(QUEUE_FLAG_WC, &q->queue_flags)) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001921 bio->bi_opf &= ~(REQ_PREFLUSH | REQ_FUA);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001922 if (!nr_sectors) {
1923 err = 0;
Tejun Heoa7384672008-11-28 13:32:03 +09001924 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Christoph Hellwig288dab82016-06-09 16:00:36 +02001928 switch (bio_op(bio)) {
1929 case REQ_OP_DISCARD:
1930 if (!blk_queue_discard(q))
1931 goto not_supported;
1932 break;
1933 case REQ_OP_SECURE_ERASE:
1934 if (!blk_queue_secure_erase(q))
1935 goto not_supported;
1936 break;
1937 case REQ_OP_WRITE_SAME:
1938 if (!bdev_write_same(bio->bi_bdev))
1939 goto not_supported;
1940 break;
1941 default:
1942 break;
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Tejun Heo7f4b35d2012-06-04 20:40:56 -07001945 /*
1946 * Various block parts want %current->io_context and lazy ioc
1947 * allocation ends up trading a lot of pain for a small amount of
1948 * memory. Just allocate it upfront. This may fail and block
1949 * layer knows how to live with it.
1950 */
1951 create_io_context(GFP_ATOMIC, q->node);
1952
Tejun Heoae118892015-08-18 14:55:20 -07001953 if (!blkcg_bio_issue_check(q, bio))
1954 return false;
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001955
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02001956 trace_block_bio_queue(q, bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001957 return true;
Tejun Heoa7384672008-11-28 13:32:03 +09001958
Christoph Hellwig288dab82016-06-09 16:00:36 +02001959not_supported:
1960 err = -EOPNOTSUPP;
Tejun Heoa7384672008-11-28 13:32:03 +09001961end_io:
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001962 bio->bi_error = err;
1963 bio_endio(bio);
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001964 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
Christoph Hellwig27a84d52011-09-15 14:01:40 +02001967/**
1968 * generic_make_request - hand a buffer to its device driver for I/O
1969 * @bio: The bio describing the location in memory and on the device.
1970 *
1971 * generic_make_request() is used to make I/O requests of block
1972 * devices. It is passed a &struct bio, which describes the I/O that needs
1973 * to be done.
1974 *
1975 * generic_make_request() does not return any status. The
1976 * success/failure status of the request, along with notification of
1977 * completion, is delivered asynchronously through the bio->bi_end_io
1978 * function described (one day) else where.
1979 *
1980 * The caller of generic_make_request must make sure that bi_io_vec
1981 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1982 * set to describe the device address, and the
1983 * bi_end_io and optionally bi_private are set to describe how
1984 * completion notification should be signaled.
1985 *
1986 * generic_make_request and the drivers it calls may use bi_next if this
1987 * bio happens to be merged with someone else, and may resubmit the bio to
1988 * a lower device by calling into generic_make_request recursively, which
1989 * means the bio should NOT be touched after the call to ->make_request_fn.
Neil Brownd89d8792007-05-01 09:53:42 +02001990 */
Jens Axboedece1632015-11-05 10:41:16 -07001991blk_qc_t generic_make_request(struct bio *bio)
Neil Brownd89d8792007-05-01 09:53:42 +02001992{
NeilBrown5959cde2017-03-10 17:00:47 +11001993 /*
1994 * bio_list_on_stack[0] contains bios submitted by the current
1995 * make_request_fn.
1996 * bio_list_on_stack[1] contains bios that were submitted before
1997 * the current make_request_fn, but that haven't been processed
1998 * yet.
1999 */
2000 struct bio_list bio_list_on_stack[2];
Jens Axboedece1632015-11-05 10:41:16 -07002001 blk_qc_t ret = BLK_QC_T_NONE;
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002002
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002003 if (!generic_make_request_checks(bio))
Jens Axboedece1632015-11-05 10:41:16 -07002004 goto out;
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002005
2006 /*
2007 * We only want one ->make_request_fn to be active at a time, else
2008 * stack usage with stacked devices could be a problem. So use
2009 * current->bio_list to keep a list of requests submited by a
2010 * make_request_fn function. current->bio_list is also used as a
2011 * flag to say if generic_make_request is currently active in this
2012 * task or not. If it is NULL, then no make_request is active. If
2013 * it is non-NULL, then a make_request is active, and new requests
2014 * should be added at the tail
2015 */
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002016 if (current->bio_list) {
NeilBrown5959cde2017-03-10 17:00:47 +11002017 bio_list_add(&current->bio_list[0], bio);
Jens Axboedece1632015-11-05 10:41:16 -07002018 goto out;
Neil Brownd89d8792007-05-01 09:53:42 +02002019 }
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002020
Neil Brownd89d8792007-05-01 09:53:42 +02002021 /* following loop may be a bit non-obvious, and so deserves some
2022 * explanation.
2023 * Before entering the loop, bio->bi_next is NULL (as all callers
2024 * ensure that) so we have a list with a single bio.
2025 * We pretend that we have just taken it off a longer list, so
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002026 * we assign bio_list to a pointer to the bio_list_on_stack,
2027 * thus initialising the bio_list of new bios to be
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002028 * added. ->make_request() may indeed add some more bios
Neil Brownd89d8792007-05-01 09:53:42 +02002029 * through a recursive call to generic_make_request. If it
2030 * did, we find a non-NULL value in bio_list and re-enter the loop
2031 * from the top. In this case we really did just take the bio
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002032 * of the top of the list (no pretending) and so remove it from
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002033 * bio_list, and call into ->make_request() again.
Neil Brownd89d8792007-05-01 09:53:42 +02002034 */
2035 BUG_ON(bio->bi_next);
NeilBrown5959cde2017-03-10 17:00:47 +11002036 bio_list_init(&bio_list_on_stack[0]);
2037 current->bio_list = bio_list_on_stack;
Neil Brownd89d8792007-05-01 09:53:42 +02002038 do {
Christoph Hellwig27a84d52011-09-15 14:01:40 +02002039 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
2040
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +01002041 if (likely(blk_queue_enter(q, false) == 0)) {
NeilBrownd5986e02017-03-08 07:38:05 +11002042 struct bio_list lower, same;
2043
2044 /* Create a fresh bio_list for all subordinate requests */
NeilBrown5959cde2017-03-10 17:00:47 +11002045 bio_list_on_stack[1] = bio_list_on_stack[0];
2046 bio_list_init(&bio_list_on_stack[0]);
Jens Axboedece1632015-11-05 10:41:16 -07002047 ret = q->make_request_fn(q, bio);
Dan Williams3ef28e82015-10-21 13:20:12 -04002048
2049 blk_queue_exit(q);
2050
NeilBrownd5986e02017-03-08 07:38:05 +11002051 /* sort new bios into those for a lower level
2052 * and those for the same level
2053 */
2054 bio_list_init(&lower);
2055 bio_list_init(&same);
NeilBrown5959cde2017-03-10 17:00:47 +11002056 while ((bio = bio_list_pop(&bio_list_on_stack[0])) != NULL)
NeilBrownd5986e02017-03-08 07:38:05 +11002057 if (q == bdev_get_queue(bio->bi_bdev))
2058 bio_list_add(&same, bio);
2059 else
2060 bio_list_add(&lower, bio);
2061 /* now assemble so we handle the lowest level first */
NeilBrown5959cde2017-03-10 17:00:47 +11002062 bio_list_merge(&bio_list_on_stack[0], &lower);
2063 bio_list_merge(&bio_list_on_stack[0], &same);
2064 bio_list_merge(&bio_list_on_stack[0], &bio_list_on_stack[1]);
Dan Williams3ef28e82015-10-21 13:20:12 -04002065 } else {
Dan Williams3ef28e82015-10-21 13:20:12 -04002066 bio_io_error(bio);
Dan Williams3ef28e82015-10-21 13:20:12 -04002067 }
NeilBrown5959cde2017-03-10 17:00:47 +11002068 bio = bio_list_pop(&bio_list_on_stack[0]);
Neil Brownd89d8792007-05-01 09:53:42 +02002069 } while (bio);
Akinobu Mitabddd87c2010-02-23 08:55:42 +01002070 current->bio_list = NULL; /* deactivate */
Jens Axboedece1632015-11-05 10:41:16 -07002071
2072out:
2073 return ret;
Neil Brownd89d8792007-05-01 09:53:42 +02002074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075EXPORT_SYMBOL(generic_make_request);
2076
2077/**
Randy Dunlap710027a2008-08-19 20:13:11 +02002078 * submit_bio - submit a bio to the block device layer for I/O
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 * @bio: The &struct bio which describes the I/O
2080 *
2081 * submit_bio() is very similar in purpose to generic_make_request(), and
2082 * uses that function to do most of the work. Both are fairly rough
Randy Dunlap710027a2008-08-19 20:13:11 +02002083 * interfaces; @bio must be presetup and ready for I/O.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 *
2085 */
Mike Christie4e49ea42016-06-05 14:31:41 -05002086blk_qc_t submit_bio(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
Johannes Weiner0b0803a2019-08-08 15:03:00 -04002088 bool workingset_read = false;
2089 unsigned long pflags;
2090 blk_qc_t ret;
2091
Jens Axboebf2de6f2007-09-27 13:01:25 +02002092 /*
2093 * If it's a regular read/write or a barrier with data attached,
2094 * go through the normal accounting stuff before submission.
2095 */
Martin K. Petersene2a60da2012-09-18 12:19:25 -04002096 if (bio_has_data(bio)) {
Martin K. Petersen4363ac72012-09-18 12:19:27 -04002097 unsigned int count;
2098
Mike Christie95fe6c12016-06-05 14:31:48 -05002099 if (unlikely(bio_op(bio) == REQ_OP_WRITE_SAME))
Martin K. Petersen4363ac72012-09-18 12:19:27 -04002100 count = bdev_logical_block_size(bio->bi_bdev) >> 9;
2101 else
2102 count = bio_sectors(bio);
2103
Mike Christiea8ebb052016-06-05 14:31:45 -05002104 if (op_is_write(bio_op(bio))) {
Jens Axboebf2de6f2007-09-27 13:01:25 +02002105 count_vm_events(PGPGOUT, count);
2106 } else {
Johannes Weiner0b0803a2019-08-08 15:03:00 -04002107 if (bio_flagged(bio, BIO_WORKINGSET))
2108 workingset_read = true;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002109 task_io_account_read(bio->bi_iter.bi_size);
Jens Axboebf2de6f2007-09-27 13:01:25 +02002110 count_vm_events(PGPGIN, count);
2111 }
2112
2113 if (unlikely(block_dump)) {
2114 char b[BDEVNAME_SIZE];
San Mehat8dcbdc72010-09-14 08:48:01 +02002115 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002116 current->comm, task_pid_nr(current),
Mike Christiea8ebb052016-06-05 14:31:45 -05002117 op_is_write(bio_op(bio)) ? "WRITE" : "READ",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002118 (unsigned long long)bio->bi_iter.bi_sector,
San Mehat8dcbdc72010-09-14 08:48:01 +02002119 bdevname(bio->bi_bdev, b),
2120 count);
Jens Axboebf2de6f2007-09-27 13:01:25 +02002121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
2123
Johannes Weiner0b0803a2019-08-08 15:03:00 -04002124 /*
2125 * If we're reading data that is part of the userspace
2126 * workingset, count submission time as memory stall. When the
2127 * device is congested, or the submitting cgroup IO-throttled,
2128 * submission can be a significant part of overall IO time.
2129 */
2130 if (workingset_read)
2131 psi_memstall_enter(&pflags);
2132
2133 ret = generic_make_request(bio);
2134
2135 if (workingset_read)
2136 psi_memstall_leave(&pflags);
2137
2138 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140EXPORT_SYMBOL(submit_bio);
2141
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002142/**
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01002143 * blk_cloned_rq_check_limits - Helper function to check a cloned request
2144 * for new the queue limits
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002145 * @q: the queue
2146 * @rq: the request being checked
2147 *
2148 * Description:
2149 * @rq may have been made based on weaker limitations of upper-level queues
2150 * in request stacking drivers, and it may violate the limitation of @q.
2151 * Since the block layer and the underlying device driver trust @rq
2152 * after it is inserted to @q, it should be checked against @q before
2153 * the insertion using this generic function.
2154 *
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002155 * Request stacking drivers like request-based dm may change the queue
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01002156 * limits when retrying requests on other queues. Those requests need
2157 * to be checked against the new queue limits again during dispatch.
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002158 */
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01002159static int blk_cloned_rq_check_limits(struct request_queue *q,
2160 struct request *rq)
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002161{
Mike Christie8fe0d472016-06-05 14:32:15 -05002162 if (blk_rq_sectors(rq) > blk_queue_get_max_sectors(q, req_op(rq))) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002163 printk(KERN_ERR "%s: over max size limit.\n", __func__);
2164 return -EIO;
2165 }
2166
2167 /*
2168 * queue's settings related to segment counting like q->bounce_pfn
2169 * may differ from that of other stacking queues.
2170 * Recalculate it to check the request correctly on this queue's
2171 * limitation.
2172 */
2173 blk_recalc_rq_segments(rq);
Martin K. Petersen8a783622010-02-26 00:20:39 -05002174 if (rq->nr_phys_segments > queue_max_segments(q)) {
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002175 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
2176 return -EIO;
2177 }
2178
2179 return 0;
2180}
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002181
2182/**
2183 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
2184 * @q: the queue to submit the request
2185 * @rq: the request being queued
2186 */
2187int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
2188{
2189 unsigned long flags;
Jeff Moyer4853aba2011-08-15 21:37:25 +02002190 int where = ELEVATOR_INSERT_BACK;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002191
Hannes Reineckebf4e6b42015-11-26 08:46:57 +01002192 if (blk_cloned_rq_check_limits(q, rq))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002193 return -EIO;
2194
Akinobu Mitab2c9cd32011-07-26 16:09:03 -07002195 if (rq->rq_disk &&
2196 should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq)))
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002197 return -EIO;
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002198
Keith Busch7fb48982014-10-17 17:46:38 -06002199 if (q->mq_ops) {
2200 if (blk_queue_io_stat(q))
2201 blk_account_io_start(rq, true);
Mike Snitzer6acfe682016-02-05 08:49:01 -05002202 blk_mq_insert_request(rq, false, true, false);
Keith Busch7fb48982014-10-17 17:46:38 -06002203 return 0;
2204 }
2205
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002206 spin_lock_irqsave(q->queue_lock, flags);
Bart Van Assche3f3299d2012-11-28 13:42:38 +01002207 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01002208 spin_unlock_irqrestore(q->queue_lock, flags);
2209 return -ENODEV;
2210 }
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002211
2212 /*
2213 * Submitting request must be dequeued before calling this function
2214 * because it will be linked to another request_queue
2215 */
2216 BUG_ON(blk_queued_rq(rq));
2217
Mike Christie28a8f0d2016-06-05 14:32:25 -05002218 if (rq->cmd_flags & (REQ_PREFLUSH | REQ_FUA))
Jeff Moyer4853aba2011-08-15 21:37:25 +02002219 where = ELEVATOR_INSERT_FLUSH;
2220
2221 add_acct_request(q, rq, where);
Jeff Moyere67b77c2011-10-17 12:57:23 +02002222 if (where == ELEVATOR_INSERT_FLUSH)
2223 __blk_run_queue(q);
Kiyoshi Ueda82124d62008-09-18 10:45:38 -04002224 spin_unlock_irqrestore(q->queue_lock, flags);
2225
2226 return 0;
2227}
2228EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
2229
Tejun Heo80a761f2009-07-03 17:48:17 +09002230/**
2231 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
2232 * @rq: request to examine
2233 *
2234 * Description:
2235 * A request could be merge of IOs which require different failure
2236 * handling. This function determines the number of bytes which
2237 * can be failed from the beginning of the request without
2238 * crossing into area which need to be retried further.
2239 *
2240 * Return:
2241 * The number of bytes to fail.
2242 *
2243 * Context:
2244 * queue_lock must be held.
2245 */
2246unsigned int blk_rq_err_bytes(const struct request *rq)
2247{
2248 unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK;
2249 unsigned int bytes = 0;
2250 struct bio *bio;
2251
2252 if (!(rq->cmd_flags & REQ_MIXED_MERGE))
2253 return blk_rq_bytes(rq);
2254
2255 /*
2256 * Currently the only 'mixing' which can happen is between
2257 * different fastfail types. We can safely fail portions
2258 * which have all the failfast bits that the first one has -
2259 * the ones which are at least as eager to fail as the first
2260 * one.
2261 */
2262 for (bio = rq->bio; bio; bio = bio->bi_next) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06002263 if ((bio->bi_opf & ff) != ff)
Tejun Heo80a761f2009-07-03 17:48:17 +09002264 break;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002265 bytes += bio->bi_iter.bi_size;
Tejun Heo80a761f2009-07-03 17:48:17 +09002266 }
2267
2268 /* this could lead to infinite loop */
2269 BUG_ON(blk_rq_bytes(rq) && !bytes);
2270 return bytes;
2271}
2272EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
2273
Jens Axboe320ae512013-10-24 09:20:05 +01002274void blk_account_io_completion(struct request *req, unsigned int bytes)
Jens Axboebc58ba92009-01-23 10:54:44 +01002275{
Jens Axboec2553b52009-04-24 08:10:11 +02002276 if (blk_do_io_stat(req)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002277 const int rw = rq_data_dir(req);
2278 struct hd_struct *part;
2279 int cpu;
2280
2281 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002282 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002283 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
2284 part_stat_unlock();
2285 }
2286}
2287
Jens Axboe320ae512013-10-24 09:20:05 +01002288void blk_account_io_done(struct request *req)
Jens Axboebc58ba92009-01-23 10:54:44 +01002289{
Jens Axboebc58ba92009-01-23 10:54:44 +01002290 /*
Tejun Heodd4c1332010-09-03 11:56:16 +02002291 * Account IO completion. flush_rq isn't accounted as a
2292 * normal IO on queueing nor completion. Accounting the
2293 * containing request is enough.
Jens Axboebc58ba92009-01-23 10:54:44 +01002294 */
Tejun Heo414b4ff2011-01-25 12:43:49 +01002295 if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) {
Jens Axboebc58ba92009-01-23 10:54:44 +01002296 unsigned long duration = jiffies - req->start_time;
2297 const int rw = rq_data_dir(req);
2298 struct hd_struct *part;
2299 int cpu;
2300
2301 cpu = part_stat_lock();
Jerome Marchand09e099d2011-01-05 16:57:38 +01002302 part = req->part;
Jens Axboebc58ba92009-01-23 10:54:44 +01002303
2304 part_stat_inc(cpu, part, ios[rw]);
2305 part_stat_add(cpu, part, ticks[rw], duration);
2306 part_round_stats(cpu, part);
Nikanth Karthikesan316d3152009-10-06 20:16:55 +02002307 part_dec_in_flight(part, rw);
Jens Axboebc58ba92009-01-23 10:54:44 +01002308
Jens Axboe6c23a962011-01-07 08:43:37 +01002309 hd_struct_put(part);
Jens Axboebc58ba92009-01-23 10:54:44 +01002310 part_stat_unlock();
2311 }
2312}
2313
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01002314#ifdef CONFIG_PM
Lin Mingc8158812013-03-23 11:42:27 +08002315/*
2316 * Don't process normal requests when queue is suspended
2317 * or in the process of suspending/resuming
2318 */
2319static struct request *blk_pm_peek_request(struct request_queue *q,
2320 struct request *rq)
2321{
2322 if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
2323 (q->rpm_status != RPM_ACTIVE && !(rq->cmd_flags & REQ_PM))))
2324 return NULL;
2325 else
2326 return rq;
2327}
2328#else
2329static inline struct request *blk_pm_peek_request(struct request_queue *q,
2330 struct request *rq)
2331{
2332 return rq;
2333}
2334#endif
2335
Jens Axboe320ae512013-10-24 09:20:05 +01002336void blk_account_io_start(struct request *rq, bool new_io)
2337{
2338 struct hd_struct *part;
2339 int rw = rq_data_dir(rq);
2340 int cpu;
2341
2342 if (!blk_do_io_stat(rq))
2343 return;
2344
2345 cpu = part_stat_lock();
2346
2347 if (!new_io) {
2348 part = rq->part;
2349 part_stat_inc(cpu, part, merges[rw]);
2350 } else {
2351 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
2352 if (!hd_struct_try_get(part)) {
2353 /*
2354 * The partition is already being removed,
2355 * the request will be accounted on the disk only
2356 *
2357 * We take a reference on disk->part0 although that
2358 * partition will never be deleted, so we can treat
2359 * it as any other partition.
2360 */
2361 part = &rq->rq_disk->part0;
2362 hd_struct_get(part);
2363 }
2364 part_round_stats(cpu, part);
2365 part_inc_in_flight(part, rw);
2366 rq->part = part;
2367 }
2368
2369 part_stat_unlock();
2370}
2371
Tejun Heo53a08802008-12-03 12:41:26 +01002372/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002373 * blk_peek_request - peek at the top of a request queue
2374 * @q: request queue to peek at
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002375 *
2376 * Description:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002377 * Return the request at the top of @q. The returned request
2378 * should be started using blk_start_request() before LLD starts
2379 * processing it.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002380 *
2381 * Return:
Tejun Heo9934c8c2009-05-08 11:54:16 +09002382 * Pointer to the request at the top of @q if available. Null
2383 * otherwise.
2384 *
2385 * Context:
2386 * queue_lock must be held.
2387 */
2388struct request *blk_peek_request(struct request_queue *q)
Tejun Heo158dbda2009-04-23 11:05:18 +09002389{
2390 struct request *rq;
2391 int ret;
2392
2393 while ((rq = __elv_next_request(q)) != NULL) {
Lin Mingc8158812013-03-23 11:42:27 +08002394
2395 rq = blk_pm_peek_request(q, rq);
2396 if (!rq)
2397 break;
2398
Tejun Heo158dbda2009-04-23 11:05:18 +09002399 if (!(rq->cmd_flags & REQ_STARTED)) {
2400 /*
2401 * This is the first time the device driver
2402 * sees this request (possibly after
2403 * requeueing). Notify IO scheduler.
2404 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002405 if (rq->cmd_flags & REQ_SORTED)
Tejun Heo158dbda2009-04-23 11:05:18 +09002406 elv_activate_rq(q, rq);
2407
2408 /*
2409 * just mark as started even if we don't start
2410 * it, a request that has been delayed should
2411 * not be passed by new incoming requests
2412 */
2413 rq->cmd_flags |= REQ_STARTED;
2414 trace_block_rq_issue(q, rq);
2415 }
2416
2417 if (!q->boundary_rq || q->boundary_rq == rq) {
2418 q->end_sector = rq_end_sector(rq);
2419 q->boundary_rq = NULL;
2420 }
2421
2422 if (rq->cmd_flags & REQ_DONTPREP)
2423 break;
2424
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002425 if (q->dma_drain_size && blk_rq_bytes(rq)) {
Tejun Heo158dbda2009-04-23 11:05:18 +09002426 /*
2427 * make sure space for the drain appears we
2428 * know we can do this because max_hw_segments
2429 * has been adjusted to be one fewer than the
2430 * device can handle
2431 */
2432 rq->nr_phys_segments++;
2433 }
2434
2435 if (!q->prep_rq_fn)
2436 break;
2437
2438 ret = q->prep_rq_fn(q, rq);
2439 if (ret == BLKPREP_OK) {
2440 break;
2441 } else if (ret == BLKPREP_DEFER) {
2442 /*
2443 * the request may have been (partially) prepped.
2444 * we need to keep this request in the front to
2445 * avoid resource deadlock. REQ_STARTED will
2446 * prevent other fs requests from passing this one.
2447 */
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002448 if (q->dma_drain_size && blk_rq_bytes(rq) &&
Tejun Heo158dbda2009-04-23 11:05:18 +09002449 !(rq->cmd_flags & REQ_DONTPREP)) {
2450 /*
2451 * remove the space for the drain we added
2452 * so that we don't add it again
2453 */
2454 --rq->nr_phys_segments;
2455 }
2456
2457 rq = NULL;
2458 break;
Martin K. Petersen0fb5b1f2016-02-04 00:52:12 -05002459 } else if (ret == BLKPREP_KILL || ret == BLKPREP_INVALID) {
2460 int err = (ret == BLKPREP_INVALID) ? -EREMOTEIO : -EIO;
2461
Tejun Heo158dbda2009-04-23 11:05:18 +09002462 rq->cmd_flags |= REQ_QUIET;
James Bottomleyc143dc92009-05-30 06:43:49 +02002463 /*
2464 * Mark this request as started so we don't trigger
2465 * any debug logic in the end I/O path.
2466 */
2467 blk_start_request(rq);
Martin K. Petersen0fb5b1f2016-02-04 00:52:12 -05002468 __blk_end_request_all(rq, err);
Tejun Heo158dbda2009-04-23 11:05:18 +09002469 } else {
2470 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
2471 break;
2472 }
2473 }
2474
2475 return rq;
2476}
Tejun Heo9934c8c2009-05-08 11:54:16 +09002477EXPORT_SYMBOL(blk_peek_request);
Tejun Heo158dbda2009-04-23 11:05:18 +09002478
Tejun Heo9934c8c2009-05-08 11:54:16 +09002479void blk_dequeue_request(struct request *rq)
Tejun Heo158dbda2009-04-23 11:05:18 +09002480{
Tejun Heo9934c8c2009-05-08 11:54:16 +09002481 struct request_queue *q = rq->q;
2482
Tejun Heo158dbda2009-04-23 11:05:18 +09002483 BUG_ON(list_empty(&rq->queuelist));
2484 BUG_ON(ELV_ON_HASH(rq));
2485
2486 list_del_init(&rq->queuelist);
2487
2488 /*
2489 * the time frame between a request being removed from the lists
2490 * and to it is freed is accounted as io that is in progress at
2491 * the driver side.
2492 */
Divyesh Shah91952912010-04-01 15:01:41 -07002493 if (blk_account_rq(rq)) {
Jens Axboe0a7ae2f2009-05-20 08:54:31 +02002494 q->in_flight[rq_is_sync(rq)]++;
Divyesh Shah91952912010-04-01 15:01:41 -07002495 set_io_start_time_ns(rq);
2496 }
Tejun Heo158dbda2009-04-23 11:05:18 +09002497}
2498
Tejun Heo5efccd12009-04-23 11:05:18 +09002499/**
Tejun Heo9934c8c2009-05-08 11:54:16 +09002500 * blk_start_request - start request processing on the driver
2501 * @req: request to dequeue
2502 *
2503 * Description:
2504 * Dequeue @req and start timeout timer on it. This hands off the
2505 * request to the driver.
2506 *
2507 * Block internal functions which don't want to start timer should
2508 * call blk_dequeue_request().
2509 *
2510 * Context:
2511 * queue_lock must be held.
2512 */
2513void blk_start_request(struct request *req)
2514{
2515 blk_dequeue_request(req);
2516
2517 /*
Tejun Heo5f49f632009-05-19 18:33:05 +09002518 * We are now handing the request to the hardware, initialize
2519 * resid_len to full count and add the timeout handler.
Tejun Heo9934c8c2009-05-08 11:54:16 +09002520 */
Tejun Heo5f49f632009-05-19 18:33:05 +09002521 req->resid_len = blk_rq_bytes(req);
FUJITA Tomonoridbb66c42009-06-09 05:47:10 +02002522 if (unlikely(blk_bidi_rq(req)))
2523 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
2524
Jeff Moyer4912aa62013-10-08 14:36:41 -04002525 BUG_ON(test_bit(REQ_ATOM_COMPLETE, &req->atomic_flags));
Tejun Heo9934c8c2009-05-08 11:54:16 +09002526 blk_add_timer(req);
2527}
2528EXPORT_SYMBOL(blk_start_request);
2529
2530/**
2531 * blk_fetch_request - fetch a request from a request queue
2532 * @q: request queue to fetch a request from
2533 *
2534 * Description:
2535 * Return the request at the top of @q. The request is started on
2536 * return and LLD can start processing it immediately.
2537 *
2538 * Return:
2539 * Pointer to the request at the top of @q if available. Null
2540 * otherwise.
2541 *
2542 * Context:
2543 * queue_lock must be held.
2544 */
2545struct request *blk_fetch_request(struct request_queue *q)
2546{
2547 struct request *rq;
2548
2549 rq = blk_peek_request(q);
2550 if (rq)
2551 blk_start_request(rq);
2552 return rq;
2553}
2554EXPORT_SYMBOL(blk_fetch_request);
2555
2556/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002557 * blk_update_request - Special helper function for request stacking drivers
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002558 * @req: the request being processed
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002559 * @error: %0 for success, < %0 for error
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002560 * @nr_bytes: number of bytes to complete @req
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002561 *
2562 * Description:
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002563 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2564 * the request structure even if @req doesn't have leftover.
2565 * If @req has leftover, sets it up for the next range of segments.
Tejun Heo2e60e022009-04-23 11:05:18 +09002566 *
2567 * This special helper function is only for request stacking drivers
2568 * (e.g. request-based dm) so that they can handle partial completion.
2569 * Actual device drivers should use blk_end_request instead.
2570 *
2571 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2572 * %false return from this function.
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002573 *
2574 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002575 * %false - this request doesn't have any more data
2576 * %true - this request has more data
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05002577 **/
Tejun Heo2e60e022009-04-23 11:05:18 +09002578bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
Kent Overstreetf79ea412012-09-20 16:38:30 -07002580 int total_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Hannes Reinecke4a0efdc2014-10-01 14:32:31 +02002582 trace_block_rq_complete(req->q, req, nr_bytes);
2583
Tejun Heo2e60e022009-04-23 11:05:18 +09002584 if (!req->bio)
2585 return false;
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 /*
Tejun Heo6f414692009-04-19 07:00:41 +09002588 * For fs requests, rq is just carrier of independent bio's
2589 * and each partial completion should be handled separately.
2590 * Reset per-request error on each partial completion.
2591 *
2592 * TODO: tj: This is too subtle. It would be better to let
2593 * low level drivers do what they see fit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 */
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002595 if (req->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 req->errors = 0;
2597
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002598 if (error && req->cmd_type == REQ_TYPE_FS &&
2599 !(req->cmd_flags & REQ_QUIET)) {
Hannes Reinecke79775562011-01-18 10:13:13 +01002600 char *error_type;
2601
2602 switch (error) {
2603 case -ENOLINK:
2604 error_type = "recoverable transport";
2605 break;
2606 case -EREMOTEIO:
2607 error_type = "critical target";
2608 break;
2609 case -EBADE:
2610 error_type = "critical nexus";
2611 break;
Hannes Reinecked1ffc1f2013-01-30 09:26:16 +00002612 case -ETIMEDOUT:
2613 error_type = "timeout";
2614 break;
Hannes Reineckea9d6ceb82013-07-01 15:16:25 +02002615 case -ENOSPC:
2616 error_type = "critical space allocation";
2617 break;
Hannes Reinecke7e782af2013-07-01 15:16:26 +02002618 case -ENODATA:
2619 error_type = "critical medium";
2620 break;
Hannes Reinecke79775562011-01-18 10:13:13 +01002621 case -EIO:
2622 default:
2623 error_type = "I/O";
2624 break;
2625 }
Robert Elliottef3ecb62014-08-27 10:50:31 -05002626 printk_ratelimited(KERN_ERR "%s: %s error, dev %s, sector %llu\n",
2627 __func__, error_type, req->rq_disk ?
Yi Zou37d7b342012-08-30 16:26:25 -07002628 req->rq_disk->disk_name : "?",
2629 (unsigned long long)blk_rq_pos(req));
2630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
2632
Jens Axboebc58ba92009-01-23 10:54:44 +01002633 blk_account_io_completion(req, nr_bytes);
Jens Axboed72d9042005-11-01 08:35:42 +01002634
Kent Overstreetf79ea412012-09-20 16:38:30 -07002635 total_bytes = 0;
AnilKumar Chimata7214d7e2017-06-23 03:09:59 -07002636
2637 /*
2638 * Check for this if flagged, Req based dm needs to perform
2639 * post processing, hence dont end bios or request.DM
2640 * layer takes care.
2641 */
2642 if (bio_flagged(req->bio, BIO_DONTFREE))
2643 return false;
2644
Kent Overstreetf79ea412012-09-20 16:38:30 -07002645 while (req->bio) {
2646 struct bio *bio = req->bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002647 unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Kent Overstreet4f024f32013-10-11 15:44:27 -07002649 if (bio_bytes == bio->bi_iter.bi_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 req->bio = bio->bi_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Kent Overstreetf79ea412012-09-20 16:38:30 -07002652 req_bio_endio(req, bio, bio_bytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Kent Overstreetf79ea412012-09-20 16:38:30 -07002654 total_bytes += bio_bytes;
2655 nr_bytes -= bio_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Kent Overstreetf79ea412012-09-20 16:38:30 -07002657 if (!nr_bytes)
2658 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
2660
2661 /*
2662 * completely done
2663 */
Tejun Heo2e60e022009-04-23 11:05:18 +09002664 if (!req->bio) {
2665 /*
2666 * Reset counters so that the request stacking driver
2667 * can find how many bytes remain in the request
2668 * later.
2669 */
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002670 req->__data_len = 0;
Tejun Heo2e60e022009-04-23 11:05:18 +09002671 return false;
2672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002674 req->__data_len -= total_bytes;
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002675
2676 /* update sector only for requests with clear definition of sector */
Jaegeuk Kim84680312018-01-03 21:22:07 -08002677 if (req->cmd_type == REQ_TYPE_FS) {
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002678 req->__sector += total_bytes >> 9;
Jaegeuk Kim5190c812018-04-22 16:05:02 -07002679#ifdef CONFIG_PFK
Jaegeuk Kim84680312018-01-03 21:22:07 -08002680 if (req->__dun)
2681 req->__dun += total_bytes >> 12;
Jaegeuk Kim5190c812018-04-22 16:05:02 -07002682#endif
Jaegeuk Kim84680312018-01-03 21:22:07 -08002683 }
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002684
Tejun Heo80a761f2009-07-03 17:48:17 +09002685 /* mixed attributes always follow the first bio */
2686 if (req->cmd_flags & REQ_MIXED_MERGE) {
2687 req->cmd_flags &= ~REQ_FAILFAST_MASK;
Jens Axboe1eff9d32016-08-05 15:35:16 -06002688 req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK;
Tejun Heo80a761f2009-07-03 17:48:17 +09002689 }
2690
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002691 /*
2692 * If total number of sectors is less than the first segment
2693 * size, something has gone terribly wrong.
2694 */
2695 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
Jens Axboe81829242011-03-30 09:51:33 +02002696 blk_dump_rq_flags(req, "request botched");
Tejun Heoa2dec7b2009-05-07 22:24:44 +09002697 req->__data_len = blk_rq_cur_bytes(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002698 }
2699
2700 /* recalculate the number of segments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 blk_recalc_rq_segments(req);
Tejun Heo2e46e8b2009-05-07 22:24:41 +09002702
Tejun Heo2e60e022009-04-23 11:05:18 +09002703 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704}
Tejun Heo2e60e022009-04-23 11:05:18 +09002705EXPORT_SYMBOL_GPL(blk_update_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Tejun Heo2e60e022009-04-23 11:05:18 +09002707static bool blk_update_bidi_request(struct request *rq, int error,
2708 unsigned int nr_bytes,
2709 unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002710{
Tejun Heo2e60e022009-04-23 11:05:18 +09002711 if (blk_update_request(rq, error, nr_bytes))
2712 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002713
Tejun Heo2e60e022009-04-23 11:05:18 +09002714 /* Bidi request must be completed as a whole */
2715 if (unlikely(blk_bidi_rq(rq)) &&
2716 blk_update_request(rq->next_rq, error, bidi_bytes))
2717 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002718
Jens Axboee2e1a142010-06-09 10:42:09 +02002719 if (blk_queue_add_random(rq->q))
2720 add_disk_randomness(rq->rq_disk);
Tejun Heo2e60e022009-04-23 11:05:18 +09002721
2722 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
2724
James Bottomley28018c22010-07-01 19:49:17 +09002725/**
2726 * blk_unprep_request - unprepare a request
2727 * @req: the request
2728 *
2729 * This function makes a request ready for complete resubmission (or
2730 * completion). It happens only after all error handling is complete,
2731 * so represents the appropriate moment to deallocate any resources
2732 * that were allocated to the request in the prep_rq_fn. The queue
2733 * lock is held when calling this.
2734 */
2735void blk_unprep_request(struct request *req)
2736{
2737 struct request_queue *q = req->q;
2738
2739 req->cmd_flags &= ~REQ_DONTPREP;
2740 if (q->unprep_rq_fn)
2741 q->unprep_rq_fn(q, req);
2742}
2743EXPORT_SYMBOL_GPL(blk_unprep_request);
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745/*
2746 * queue lock must be held
2747 */
Christoph Hellwig12120072014-04-16 09:44:59 +02002748void blk_finish_request(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
Christoph Hellwig125c99b2014-11-03 12:47:47 +01002750 if (req->cmd_flags & REQ_QUEUED)
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002751 blk_queue_end_tag(req->q, req);
2752
James Bottomleyba396a62009-05-27 14:17:08 +02002753 BUG_ON(blk_queued_rq(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Christoph Hellwig33659eb2010-08-07 18:17:56 +02002755 if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS)
Jan Kara1cdb7f92017-02-02 15:56:50 +01002756 laptop_io_completion(req->q->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Mike Andersone78042e2008-10-30 02:16:20 -07002758 blk_delete_timer(req);
2759
James Bottomley28018c22010-07-01 19:49:17 +09002760 if (req->cmd_flags & REQ_DONTPREP)
2761 blk_unprep_request(req);
2762
Jens Axboebc58ba92009-01-23 10:54:44 +01002763 blk_account_io_done(req);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002766 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002767 else {
2768 if (blk_bidi_rq(req))
2769 __blk_put_request(req->next_rq->q, req->next_rq);
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 }
2773}
Christoph Hellwig12120072014-04-16 09:44:59 +02002774EXPORT_SYMBOL(blk_finish_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002776/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002777 * blk_end_bidi_request - Complete a bidi request
2778 * @rq: the request to complete
Randy Dunlap710027a2008-08-19 20:13:11 +02002779 * @error: %0 for success, < %0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002780 * @nr_bytes: number of bytes to complete @rq
2781 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002782 *
2783 * Description:
2784 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Tejun Heo2e60e022009-04-23 11:05:18 +09002785 * Drivers that supports bidi can safely call this member for any
2786 * type of request, bidi or uni. In the later case @bidi_bytes is
2787 * just ignored.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002788 *
2789 * Return:
Tejun Heo2e60e022009-04-23 11:05:18 +09002790 * %false - we are done with this request
2791 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002792 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002793static bool blk_end_bidi_request(struct request *rq, int error,
2794 unsigned int nr_bytes, unsigned int bidi_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002795{
2796 struct request_queue *q = rq->q;
Tejun Heo2e60e022009-04-23 11:05:18 +09002797 unsigned long flags;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002798
Tejun Heo2e60e022009-04-23 11:05:18 +09002799 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2800 return true;
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002801
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002802 spin_lock_irqsave(q->queue_lock, flags);
Tejun Heo2e60e022009-04-23 11:05:18 +09002803 blk_finish_request(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002804 spin_unlock_irqrestore(q->queue_lock, flags);
2805
Tejun Heo2e60e022009-04-23 11:05:18 +09002806 return false;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002807}
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002808
2809/**
Tejun Heo2e60e022009-04-23 11:05:18 +09002810 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2811 * @rq: the request to complete
2812 * @error: %0 for success, < %0 for error
2813 * @nr_bytes: number of bytes to complete @rq
2814 * @bidi_bytes: number of bytes to complete @rq->next_rq
Tejun Heo5efccd12009-04-23 11:05:18 +09002815 *
2816 * Description:
Tejun Heo2e60e022009-04-23 11:05:18 +09002817 * Identical to blk_end_bidi_request() except that queue lock is
2818 * assumed to be locked on entry and remains so on return.
Tejun Heo5efccd12009-04-23 11:05:18 +09002819 *
Tejun Heo2e60e022009-04-23 11:05:18 +09002820 * Return:
2821 * %false - we are done with this request
2822 * %true - still buffers pending for this request
Tejun Heo5efccd12009-04-23 11:05:18 +09002823 **/
Jeff Moyer4853aba2011-08-15 21:37:25 +02002824bool __blk_end_bidi_request(struct request *rq, int error,
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002825 unsigned int nr_bytes, unsigned int bidi_bytes)
Tejun Heo5efccd12009-04-23 11:05:18 +09002826{
Tejun Heo2e60e022009-04-23 11:05:18 +09002827 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2828 return true;
Tejun Heo5efccd12009-04-23 11:05:18 +09002829
Tejun Heo2e60e022009-04-23 11:05:18 +09002830 blk_finish_request(rq, error);
Tejun Heo5efccd12009-04-23 11:05:18 +09002831
Tejun Heo2e60e022009-04-23 11:05:18 +09002832 return false;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002833}
2834
2835/**
2836 * blk_end_request - Helper function for drivers to complete the request.
2837 * @rq: the request being processed
2838 * @error: %0 for success, < %0 for error
2839 * @nr_bytes: number of bytes to complete
2840 *
2841 * Description:
2842 * Ends I/O on a number of bytes attached to @rq.
2843 * If @rq has leftover, sets it up for the next range of segments.
2844 *
2845 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002846 * %false - we are done with this request
2847 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002848 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002849bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002850{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002851 return blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002852}
Jens Axboe56ad1742009-07-28 22:11:24 +02002853EXPORT_SYMBOL(blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002854
2855/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002856 * blk_end_request_all - Helper function for drives to finish the request.
2857 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002858 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002859 *
2860 * Description:
2861 * Completely finish @rq.
2862 */
2863void blk_end_request_all(struct request *rq, int error)
2864{
2865 bool pending;
2866 unsigned int bidi_bytes = 0;
2867
2868 if (unlikely(blk_bidi_rq(rq)))
2869 bidi_bytes = blk_rq_bytes(rq->next_rq);
2870
2871 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2872 BUG_ON(pending);
2873}
Jens Axboe56ad1742009-07-28 22:11:24 +02002874EXPORT_SYMBOL(blk_end_request_all);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002875
2876/**
2877 * blk_end_request_cur - Helper function to finish the current request chunk.
2878 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002879 * @error: %0 for success, < %0 for error
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002880 *
2881 * Description:
2882 * Complete the current consecutively mapped chunk from @rq.
2883 *
2884 * Return:
2885 * %false - we are done with this request
2886 * %true - still buffers pending for this request
2887 */
2888bool blk_end_request_cur(struct request *rq, int error)
2889{
2890 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2891}
Jens Axboe56ad1742009-07-28 22:11:24 +02002892EXPORT_SYMBOL(blk_end_request_cur);
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002893
2894/**
Tejun Heo80a761f2009-07-03 17:48:17 +09002895 * blk_end_request_err - Finish a request till the next failure boundary.
2896 * @rq: the request to finish till the next failure boundary for
2897 * @error: must be negative errno
2898 *
2899 * Description:
2900 * Complete @rq till the next failure boundary.
2901 *
2902 * Return:
2903 * %false - we are done with this request
2904 * %true - still buffers pending for this request
2905 */
2906bool blk_end_request_err(struct request *rq, int error)
2907{
2908 WARN_ON(error >= 0);
2909 return blk_end_request(rq, error, blk_rq_err_bytes(rq));
2910}
2911EXPORT_SYMBOL_GPL(blk_end_request_err);
2912
2913/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002914 * __blk_end_request - Helper function for drivers to complete the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002915 * @rq: the request being processed
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002916 * @error: %0 for success, < %0 for error
2917 * @nr_bytes: number of bytes to complete
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002918 *
2919 * Description:
2920 * Must be called with queue lock held unlike blk_end_request().
2921 *
2922 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002923 * %false - we are done with this request
2924 * %true - still buffers pending for this request
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002925 **/
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002926bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002927{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002928 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002929}
Jens Axboe56ad1742009-07-28 22:11:24 +02002930EXPORT_SYMBOL(__blk_end_request);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002931
2932/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002933 * __blk_end_request_all - Helper function for drives to finish the request.
2934 * @rq: the request to finish
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002935 * @error: %0 for success, < %0 for error
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002936 *
2937 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002938 * Completely finish @rq. Must be called with queue lock held.
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002939 */
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002940void __blk_end_request_all(struct request *rq, int error)
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002941{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002942 bool pending;
2943 unsigned int bidi_bytes = 0;
2944
2945 if (unlikely(blk_bidi_rq(rq)))
2946 bidi_bytes = blk_rq_bytes(rq->next_rq);
2947
2948 pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2949 BUG_ON(pending);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002950}
Jens Axboe56ad1742009-07-28 22:11:24 +02002951EXPORT_SYMBOL(__blk_end_request_all);
Kiyoshi Ueda32fab442008-09-18 10:45:09 -04002952
2953/**
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002954 * __blk_end_request_cur - Helper function to finish the current request chunk.
2955 * @rq: the request to finish the current chunk for
Randy Dunlap8ebf9752009-06-11 20:00:41 -07002956 * @error: %0 for success, < %0 for error
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002957 *
2958 * Description:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002959 * Complete the current consecutively mapped chunk from @rq. Must
2960 * be called with queue lock held.
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002961 *
2962 * Return:
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002963 * %false - we are done with this request
2964 * %true - still buffers pending for this request
2965 */
2966bool __blk_end_request_cur(struct request *rq, int error)
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002967{
FUJITA Tomonorib1f74492009-05-11 17:56:09 +09002968 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002969}
Jens Axboe56ad1742009-07-28 22:11:24 +02002970EXPORT_SYMBOL(__blk_end_request_cur);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002971
Tejun Heo80a761f2009-07-03 17:48:17 +09002972/**
2973 * __blk_end_request_err - Finish a request till the next failure boundary.
2974 * @rq: the request to finish till the next failure boundary for
2975 * @error: must be negative errno
2976 *
2977 * Description:
2978 * Complete @rq till the next failure boundary. Must be called
2979 * with queue lock held.
2980 *
2981 * Return:
2982 * %false - we are done with this request
2983 * %true - still buffers pending for this request
2984 */
2985bool __blk_end_request_err(struct request *rq, int error)
2986{
2987 WARN_ON(error >= 0);
2988 return __blk_end_request(rq, error, blk_rq_err_bytes(rq));
2989}
2990EXPORT_SYMBOL_GPL(__blk_end_request_err);
2991
Jens Axboe86db1e22008-01-29 14:53:40 +01002992void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2993 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
Mike Christie4993b772016-06-05 14:32:10 -05002995 req_set_op(rq, bio_op(bio));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Jens Axboeb4f42e22014-04-10 09:46:28 -06002997 if (bio_has_data(bio))
David Woodhousefb2dce82008-08-05 18:01:53 +01002998 rq->nr_phys_segments = bio_phys_segments(q, bio);
Jens Axboeb4f42e22014-04-10 09:46:28 -06002999
Kent Overstreet4f024f32013-10-11 15:44:27 -07003000 rq->__data_len = bio->bi_iter.bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
NeilBrown66846572007-08-16 13:31:28 +02003003 if (bio->bi_bdev)
3004 rq->rq_disk = bio->bi_bdev->bd_disk;
3005}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Ilya Loginov2d4dc892009-11-26 09:16:19 +01003007#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
3008/**
3009 * rq_flush_dcache_pages - Helper function to flush all pages in a request
3010 * @rq: the request to be flushed
3011 *
3012 * Description:
3013 * Flush all pages in @rq.
3014 */
3015void rq_flush_dcache_pages(struct request *rq)
3016{
3017 struct req_iterator iter;
Kent Overstreet79886132013-11-23 17:19:00 -08003018 struct bio_vec bvec;
Ilya Loginov2d4dc892009-11-26 09:16:19 +01003019
3020 rq_for_each_segment(bvec, rq, iter)
Kent Overstreet79886132013-11-23 17:19:00 -08003021 flush_dcache_page(bvec.bv_page);
Ilya Loginov2d4dc892009-11-26 09:16:19 +01003022}
3023EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
3024#endif
3025
Kiyoshi Uedaef9e3fa2008-10-01 16:12:15 +02003026/**
3027 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
3028 * @q : the queue of the device being checked
3029 *
3030 * Description:
3031 * Check if underlying low-level drivers of a device are busy.
3032 * If the drivers want to export their busy state, they must set own
3033 * exporting function using blk_queue_lld_busy() first.
3034 *
3035 * Basically, this function is used only by request stacking drivers
3036 * to stop dispatching requests to underlying devices when underlying
3037 * devices are busy. This behavior helps more I/O merging on the queue
3038 * of the request stacking driver and prevents I/O throughput regression
3039 * on burst I/O load.
3040 *
3041 * Return:
3042 * 0 - Not busy (The request stacking driver should dispatch request)
3043 * 1 - Busy (The request stacking driver should stop dispatching request)
3044 */
3045int blk_lld_busy(struct request_queue *q)
3046{
3047 if (q->lld_busy_fn)
3048 return q->lld_busy_fn(q);
3049
3050 return 0;
3051}
3052EXPORT_SYMBOL_GPL(blk_lld_busy);
3053
Mike Snitzer78d8e582015-06-26 10:01:13 -04003054/**
3055 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
3056 * @rq: the clone request to be cleaned up
3057 *
3058 * Description:
3059 * Free all bios in @rq for a cloned request.
3060 */
3061void blk_rq_unprep_clone(struct request *rq)
3062{
3063 struct bio *bio;
3064
3065 while ((bio = rq->bio) != NULL) {
3066 rq->bio = bio->bi_next;
3067
3068 bio_put(bio);
3069 }
3070}
3071EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
3072
3073/*
3074 * Copy attributes of the original request to the clone request.
3075 * The actual data parts (e.g. ->cmd, ->sense) are not copied.
3076 */
3077static void __blk_rq_prep_clone(struct request *dst, struct request *src)
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003078{
3079 dst->cpu = src->cpu;
Mike Christie4993b772016-06-05 14:32:10 -05003080 req_set_op_attrs(dst, req_op(src),
3081 (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE);
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003082 dst->cmd_type = src->cmd_type;
3083 dst->__sector = blk_rq_pos(src);
Jaegeuk Kim5190c812018-04-22 16:05:02 -07003084#ifdef CONFIG_PFK
Jaegeuk Kim84680312018-01-03 21:22:07 -08003085 dst->__dun = blk_rq_dun(src);
Jaegeuk Kim5190c812018-04-22 16:05:02 -07003086#endif
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003087 dst->__data_len = blk_rq_bytes(src);
3088 dst->nr_phys_segments = src->nr_phys_segments;
3089 dst->ioprio = src->ioprio;
3090 dst->extra_len = src->extra_len;
Mike Snitzer78d8e582015-06-26 10:01:13 -04003091}
3092
3093/**
3094 * blk_rq_prep_clone - Helper function to setup clone request
3095 * @rq: the request to be setup
3096 * @rq_src: original request to be cloned
3097 * @bs: bio_set that bios for clone are allocated from
3098 * @gfp_mask: memory allocation mask for bio
3099 * @bio_ctr: setup function to be called for each clone bio.
3100 * Returns %0 for success, non %0 for failure.
3101 * @data: private data to be passed to @bio_ctr
3102 *
3103 * Description:
3104 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
3105 * The actual data parts of @rq_src (e.g. ->cmd, ->sense)
3106 * are not copied, and copying such parts is the caller's responsibility.
3107 * Also, pages which the original bios are pointing to are not copied
3108 * and the cloned bios just point same pages.
3109 * So cloned bios must be completed before original bios, which means
3110 * the caller must complete @rq before @rq_src.
3111 */
3112int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
3113 struct bio_set *bs, gfp_t gfp_mask,
3114 int (*bio_ctr)(struct bio *, struct bio *, void *),
3115 void *data)
3116{
3117 struct bio *bio, *bio_src;
3118
3119 if (!bs)
3120 bs = fs_bio_set;
3121
3122 __rq_for_each_bio(bio_src, rq_src) {
3123 bio = bio_clone_fast(bio_src, gfp_mask, bs);
3124 if (!bio)
3125 goto free_and_out;
3126
3127 if (bio_ctr && bio_ctr(bio, bio_src, data))
3128 goto free_and_out;
3129
3130 if (rq->bio) {
3131 rq->biotail->bi_next = bio;
3132 rq->biotail = bio;
3133 } else
3134 rq->bio = rq->biotail = bio;
3135 }
3136
3137 __blk_rq_prep_clone(rq, rq_src);
3138
3139 return 0;
3140
3141free_and_out:
3142 if (bio)
3143 bio_put(bio);
3144 blk_rq_unprep_clone(rq);
3145
3146 return -ENOMEM;
Kiyoshi Uedab0fd2712009-06-11 13:10:16 +02003147}
3148EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
3149
Jens Axboe59c3d452014-04-08 09:15:35 -06003150int kblockd_schedule_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
3152 return queue_work(kblockd_workqueue, work);
3153}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154EXPORT_SYMBOL(kblockd_schedule_work);
3155
Jens Axboeee63cfa2016-08-24 15:52:48 -06003156int kblockd_schedule_work_on(int cpu, struct work_struct *work)
3157{
3158 return queue_work_on(cpu, kblockd_workqueue, work);
3159}
3160EXPORT_SYMBOL(kblockd_schedule_work_on);
3161
Jens Axboe59c3d452014-04-08 09:15:35 -06003162int kblockd_schedule_delayed_work(struct delayed_work *dwork,
3163 unsigned long delay)
Vivek Goyale43473b2010-09-15 17:06:35 -04003164{
3165 return queue_delayed_work(kblockd_workqueue, dwork, delay);
3166}
3167EXPORT_SYMBOL(kblockd_schedule_delayed_work);
3168
Jens Axboe8ab14592014-04-08 09:17:40 -06003169int kblockd_schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
3170 unsigned long delay)
3171{
3172 return queue_delayed_work_on(cpu, kblockd_workqueue, dwork, delay);
3173}
3174EXPORT_SYMBOL(kblockd_schedule_delayed_work_on);
3175
Suresh Jayaraman75df7132011-09-21 10:00:16 +02003176/**
3177 * blk_start_plug - initialize blk_plug and track it inside the task_struct
3178 * @plug: The &struct blk_plug that needs to be initialized
3179 *
3180 * Description:
3181 * Tracking blk_plug inside the task_struct will help with auto-flushing the
3182 * pending I/O should the task end up blocking between blk_start_plug() and
3183 * blk_finish_plug(). This is important from a performance perspective, but
3184 * also ensures that we don't deadlock. For instance, if the task is blocking
3185 * for a memory allocation, memory reclaim could end up wanting to free a
3186 * page belonging to that request that is currently residing in our private
3187 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
3188 * this kind of deadlock.
3189 */
Jens Axboe73c10102011-03-08 13:19:51 +01003190void blk_start_plug(struct blk_plug *plug)
3191{
3192 struct task_struct *tsk = current;
3193
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003194 /*
3195 * If this is a nested plug, don't actually assign it.
3196 */
3197 if (tsk->plug)
3198 return;
3199
Jens Axboe73c10102011-03-08 13:19:51 +01003200 INIT_LIST_HEAD(&plug->list);
Jens Axboe320ae512013-10-24 09:20:05 +01003201 INIT_LIST_HEAD(&plug->mq_list);
NeilBrown048c9372011-04-18 09:52:22 +02003202 INIT_LIST_HEAD(&plug->cb_list);
Jens Axboe73c10102011-03-08 13:19:51 +01003203 /*
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003204 * Store ordering should not be needed here, since a potential
3205 * preempt will imply a full memory barrier
Jens Axboe73c10102011-03-08 13:19:51 +01003206 */
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003207 tsk->plug = plug;
Jens Axboe73c10102011-03-08 13:19:51 +01003208}
3209EXPORT_SYMBOL(blk_start_plug);
3210
3211static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
3212{
3213 struct request *rqa = container_of(a, struct request, queuelist);
3214 struct request *rqb = container_of(b, struct request, queuelist);
3215
Jianpeng Ma975927b2012-10-25 21:58:17 +02003216 return !(rqa->q < rqb->q ||
3217 (rqa->q == rqb->q && blk_rq_pos(rqa) < blk_rq_pos(rqb)));
Jens Axboe73c10102011-03-08 13:19:51 +01003218}
3219
Jens Axboe49cac012011-04-16 13:51:05 +02003220/*
3221 * If 'from_schedule' is true, then postpone the dispatch of requests
3222 * until a safe kblockd context. We due this to avoid accidental big
3223 * additional stack usage in driver dispatch, in places where the originally
3224 * plugger did not intend it.
3225 */
Jens Axboef6603782011-04-15 15:49:07 +02003226static void queue_unplugged(struct request_queue *q, unsigned int depth,
Jens Axboe49cac012011-04-16 13:51:05 +02003227 bool from_schedule)
Jens Axboe99e22592011-04-18 09:59:55 +02003228 __releases(q->queue_lock)
Jens Axboe94b5eb22011-04-12 10:12:19 +02003229{
Jens Axboe49cac012011-04-16 13:51:05 +02003230 trace_block_unplug(q, depth, !from_schedule);
Jens Axboe99e22592011-04-18 09:59:55 +02003231
Bart Van Assche70460572012-11-28 13:45:56 +01003232 if (from_schedule)
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003233 blk_run_queue_async(q);
Bart Van Assche70460572012-11-28 13:45:56 +01003234 else
Christoph Hellwig24ecfbe2011-04-18 11:41:33 +02003235 __blk_run_queue(q);
Bart Van Assche70460572012-11-28 13:45:56 +01003236 spin_unlock(q->queue_lock);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003237}
3238
NeilBrown74018dc2012-07-31 09:08:15 +02003239static void flush_plug_callbacks(struct blk_plug *plug, bool from_schedule)
NeilBrown048c9372011-04-18 09:52:22 +02003240{
3241 LIST_HEAD(callbacks);
3242
Shaohua Li2a7d5552012-07-31 09:08:15 +02003243 while (!list_empty(&plug->cb_list)) {
3244 list_splice_init(&plug->cb_list, &callbacks);
NeilBrown048c9372011-04-18 09:52:22 +02003245
Shaohua Li2a7d5552012-07-31 09:08:15 +02003246 while (!list_empty(&callbacks)) {
3247 struct blk_plug_cb *cb = list_first_entry(&callbacks,
NeilBrown048c9372011-04-18 09:52:22 +02003248 struct blk_plug_cb,
3249 list);
Shaohua Li2a7d5552012-07-31 09:08:15 +02003250 list_del(&cb->list);
NeilBrown74018dc2012-07-31 09:08:15 +02003251 cb->callback(cb, from_schedule);
Shaohua Li2a7d5552012-07-31 09:08:15 +02003252 }
NeilBrown048c9372011-04-18 09:52:22 +02003253 }
3254}
3255
NeilBrown9cbb1752012-07-31 09:08:14 +02003256struct blk_plug_cb *blk_check_plugged(blk_plug_cb_fn unplug, void *data,
3257 int size)
3258{
3259 struct blk_plug *plug = current->plug;
3260 struct blk_plug_cb *cb;
3261
3262 if (!plug)
3263 return NULL;
3264
3265 list_for_each_entry(cb, &plug->cb_list, list)
3266 if (cb->callback == unplug && cb->data == data)
3267 return cb;
3268
3269 /* Not currently on the callback list */
3270 BUG_ON(size < sizeof(*cb));
3271 cb = kzalloc(size, GFP_ATOMIC);
3272 if (cb) {
3273 cb->data = data;
3274 cb->callback = unplug;
3275 list_add(&cb->list, &plug->cb_list);
3276 }
3277 return cb;
3278}
3279EXPORT_SYMBOL(blk_check_plugged);
3280
Jens Axboe49cac012011-04-16 13:51:05 +02003281void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule)
Jens Axboe73c10102011-03-08 13:19:51 +01003282{
3283 struct request_queue *q;
3284 unsigned long flags;
3285 struct request *rq;
NeilBrown109b8122011-04-11 14:13:10 +02003286 LIST_HEAD(list);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003287 unsigned int depth;
Jens Axboe73c10102011-03-08 13:19:51 +01003288
NeilBrown74018dc2012-07-31 09:08:15 +02003289 flush_plug_callbacks(plug, from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01003290
3291 if (!list_empty(&plug->mq_list))
3292 blk_mq_flush_plug_list(plug, from_schedule);
3293
Jens Axboe73c10102011-03-08 13:19:51 +01003294 if (list_empty(&plug->list))
3295 return;
3296
NeilBrown109b8122011-04-11 14:13:10 +02003297 list_splice_init(&plug->list, &list);
3298
Jianpeng Ma422765c2013-01-11 14:46:09 +01003299 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe73c10102011-03-08 13:19:51 +01003300
3301 q = NULL;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003302 depth = 0;
Jens Axboe18811272011-04-12 10:11:24 +02003303
3304 /*
3305 * Save and disable interrupts here, to avoid doing it for every
3306 * queue lock we have to take.
3307 */
Jens Axboe73c10102011-03-08 13:19:51 +01003308 local_irq_save(flags);
NeilBrown109b8122011-04-11 14:13:10 +02003309 while (!list_empty(&list)) {
3310 rq = list_entry_rq(list.next);
Jens Axboe73c10102011-03-08 13:19:51 +01003311 list_del_init(&rq->queuelist);
Jens Axboe73c10102011-03-08 13:19:51 +01003312 BUG_ON(!rq->q);
3313 if (rq->q != q) {
Jens Axboe99e22592011-04-18 09:59:55 +02003314 /*
3315 * This drops the queue lock
3316 */
3317 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003318 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003319 q = rq->q;
Jens Axboe94b5eb22011-04-12 10:12:19 +02003320 depth = 0;
Jens Axboe73c10102011-03-08 13:19:51 +01003321 spin_lock(q->queue_lock);
3322 }
Tejun Heo8ba61432011-12-14 00:33:37 +01003323
3324 /*
3325 * Short-circuit if @q is dead
3326 */
Bart Van Assche3f3299d2012-11-28 13:42:38 +01003327 if (unlikely(blk_queue_dying(q))) {
Tejun Heo8ba61432011-12-14 00:33:37 +01003328 __blk_end_request_all(rq, -ENODEV);
3329 continue;
3330 }
3331
Jens Axboe73c10102011-03-08 13:19:51 +01003332 /*
3333 * rq is already accounted, so use raw insert
3334 */
Mike Christie28a8f0d2016-06-05 14:32:25 -05003335 if (rq->cmd_flags & (REQ_PREFLUSH | REQ_FUA))
Jens Axboe401a18e2011-03-25 16:57:52 +01003336 __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH);
3337 else
3338 __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE);
Jens Axboe94b5eb22011-04-12 10:12:19 +02003339
3340 depth++;
Jens Axboe73c10102011-03-08 13:19:51 +01003341 }
3342
Jens Axboe99e22592011-04-18 09:59:55 +02003343 /*
3344 * This drops the queue lock
3345 */
3346 if (q)
Jens Axboe49cac012011-04-16 13:51:05 +02003347 queue_unplugged(q, depth, from_schedule);
Jens Axboe73c10102011-03-08 13:19:51 +01003348
Jens Axboe73c10102011-03-08 13:19:51 +01003349 local_irq_restore(flags);
3350}
Jens Axboe73c10102011-03-08 13:19:51 +01003351
3352void blk_finish_plug(struct blk_plug *plug)
3353{
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003354 if (plug != current->plug)
3355 return;
Jens Axboef6603782011-04-15 15:49:07 +02003356 blk_flush_plug_list(plug, false);
Christoph Hellwig88b996c2011-04-15 15:20:10 +02003357
Shaohua Lidd6cf3e2015-05-08 10:51:28 -07003358 current->plug = NULL;
Jens Axboe73c10102011-03-08 13:19:51 +01003359}
3360EXPORT_SYMBOL(blk_finish_plug);
3361
Jens Axboe05229be2015-11-05 10:44:55 -07003362bool blk_poll(struct request_queue *q, blk_qc_t cookie)
3363{
3364 struct blk_plug *plug;
3365 long state;
Stephen Bates6e219352016-09-13 12:23:15 -06003366 unsigned int queue_num;
3367 struct blk_mq_hw_ctx *hctx;
Jens Axboe05229be2015-11-05 10:44:55 -07003368
3369 if (!q->mq_ops || !q->mq_ops->poll || !blk_qc_t_valid(cookie) ||
3370 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
3371 return false;
3372
Stephen Bates6e219352016-09-13 12:23:15 -06003373 queue_num = blk_qc_t_to_queue_num(cookie);
3374 hctx = q->queue_hw_ctx[queue_num];
3375 hctx->poll_considered++;
3376
Jens Axboe05229be2015-11-05 10:44:55 -07003377 plug = current->plug;
3378 if (plug)
3379 blk_flush_plug_list(plug, false);
3380
3381 state = current->state;
3382 while (!need_resched()) {
Jens Axboe05229be2015-11-05 10:44:55 -07003383 int ret;
3384
3385 hctx->poll_invoked++;
3386
3387 ret = q->mq_ops->poll(hctx, blk_qc_t_to_tag(cookie));
3388 if (ret > 0) {
3389 hctx->poll_success++;
3390 set_current_state(TASK_RUNNING);
3391 return true;
3392 }
3393
3394 if (signal_pending_state(state, current))
3395 set_current_state(TASK_RUNNING);
3396
3397 if (current->state == TASK_RUNNING)
3398 return true;
3399 if (ret < 0)
3400 break;
3401 cpu_relax();
3402 }
3403
3404 return false;
3405}
Sagi Grimberg9645c1a2016-06-21 18:04:19 +02003406EXPORT_SYMBOL_GPL(blk_poll);
Jens Axboe05229be2015-11-05 10:44:55 -07003407
Rafael J. Wysocki47fafbc2014-12-04 01:00:23 +01003408#ifdef CONFIG_PM
Lin Ming6c954662013-03-23 11:42:26 +08003409/**
3410 * blk_pm_runtime_init - Block layer runtime PM initialization routine
3411 * @q: the queue of the device
3412 * @dev: the device the queue belongs to
3413 *
3414 * Description:
3415 * Initialize runtime-PM-related fields for @q and start auto suspend for
3416 * @dev. Drivers that want to take advantage of request-based runtime PM
3417 * should call this function after @dev has been initialized, and its
3418 * request queue @q has been allocated, and runtime PM for it can not happen
3419 * yet(either due to disabled/forbidden or its usage_count > 0). In most
3420 * cases, driver should call this function before any I/O has taken place.
3421 *
3422 * This function takes care of setting up using auto suspend for the device,
3423 * the autosuspend delay is set to -1 to make runtime suspend impossible
3424 * until an updated value is either set by user or by driver. Drivers do
3425 * not need to touch other autosuspend settings.
3426 *
3427 * The block layer runtime PM is request based, so only works for drivers
3428 * that use request as their IO unit instead of those directly use bio's.
3429 */
3430void blk_pm_runtime_init(struct request_queue *q, struct device *dev)
3431{
3432 q->dev = dev;
3433 q->rpm_status = RPM_ACTIVE;
3434 pm_runtime_set_autosuspend_delay(q->dev, -1);
3435 pm_runtime_use_autosuspend(q->dev);
3436}
3437EXPORT_SYMBOL(blk_pm_runtime_init);
3438
3439/**
3440 * blk_pre_runtime_suspend - Pre runtime suspend check
3441 * @q: the queue of the device
3442 *
3443 * Description:
3444 * This function will check if runtime suspend is allowed for the device
3445 * by examining if there are any requests pending in the queue. If there
3446 * are requests pending, the device can not be runtime suspended; otherwise,
3447 * the queue's status will be updated to SUSPENDING and the driver can
3448 * proceed to suspend the device.
3449 *
3450 * For the not allowed case, we mark last busy for the device so that
3451 * runtime PM core will try to autosuspend it some time later.
3452 *
3453 * This function should be called near the start of the device's
3454 * runtime_suspend callback.
3455 *
3456 * Return:
3457 * 0 - OK to runtime suspend the device
3458 * -EBUSY - Device should not be runtime suspended
3459 */
3460int blk_pre_runtime_suspend(struct request_queue *q)
3461{
3462 int ret = 0;
3463
Ken Xue4fd41a852015-12-01 14:45:46 +08003464 if (!q->dev)
3465 return ret;
3466
Lin Ming6c954662013-03-23 11:42:26 +08003467 spin_lock_irq(q->queue_lock);
3468 if (q->nr_pending) {
3469 ret = -EBUSY;
3470 pm_runtime_mark_last_busy(q->dev);
3471 } else {
3472 q->rpm_status = RPM_SUSPENDING;
3473 }
3474 spin_unlock_irq(q->queue_lock);
3475 return ret;
3476}
3477EXPORT_SYMBOL(blk_pre_runtime_suspend);
3478
3479/**
3480 * blk_post_runtime_suspend - Post runtime suspend processing
3481 * @q: the queue of the device
3482 * @err: return value of the device's runtime_suspend function
3483 *
3484 * Description:
3485 * Update the queue's runtime status according to the return value of the
3486 * device's runtime suspend function and mark last busy for the device so
3487 * that PM core will try to auto suspend the device at a later time.
3488 *
3489 * This function should be called near the end of the device's
3490 * runtime_suspend callback.
3491 */
3492void blk_post_runtime_suspend(struct request_queue *q, int err)
3493{
Ken Xue4fd41a852015-12-01 14:45:46 +08003494 if (!q->dev)
3495 return;
3496
Lin Ming6c954662013-03-23 11:42:26 +08003497 spin_lock_irq(q->queue_lock);
3498 if (!err) {
3499 q->rpm_status = RPM_SUSPENDED;
3500 } else {
3501 q->rpm_status = RPM_ACTIVE;
3502 pm_runtime_mark_last_busy(q->dev);
3503 }
3504 spin_unlock_irq(q->queue_lock);
3505}
3506EXPORT_SYMBOL(blk_post_runtime_suspend);
3507
3508/**
3509 * blk_pre_runtime_resume - Pre runtime resume processing
3510 * @q: the queue of the device
3511 *
3512 * Description:
3513 * Update the queue's runtime status to RESUMING in preparation for the
3514 * runtime resume of the device.
3515 *
3516 * This function should be called near the start of the device's
3517 * runtime_resume callback.
3518 */
3519void blk_pre_runtime_resume(struct request_queue *q)
3520{
Ken Xue4fd41a852015-12-01 14:45:46 +08003521 if (!q->dev)
3522 return;
3523
Lin Ming6c954662013-03-23 11:42:26 +08003524 spin_lock_irq(q->queue_lock);
3525 q->rpm_status = RPM_RESUMING;
3526 spin_unlock_irq(q->queue_lock);
3527}
3528EXPORT_SYMBOL(blk_pre_runtime_resume);
3529
3530/**
3531 * blk_post_runtime_resume - Post runtime resume processing
3532 * @q: the queue of the device
3533 * @err: return value of the device's runtime_resume function
3534 *
3535 * Description:
3536 * Update the queue's runtime status according to the return value of the
3537 * device's runtime_resume function. If it is successfully resumed, process
3538 * the requests that are queued into the device's queue when it is resuming
3539 * and then mark last busy and initiate autosuspend for it.
3540 *
3541 * This function should be called near the end of the device's
3542 * runtime_resume callback.
3543 */
3544void blk_post_runtime_resume(struct request_queue *q, int err)
3545{
Ken Xue4fd41a852015-12-01 14:45:46 +08003546 if (!q->dev)
3547 return;
3548
Lin Ming6c954662013-03-23 11:42:26 +08003549 spin_lock_irq(q->queue_lock);
3550 if (!err) {
3551 q->rpm_status = RPM_ACTIVE;
3552 __blk_run_queue(q);
3553 pm_runtime_mark_last_busy(q->dev);
Aaron Luc60855c2013-05-17 15:47:20 +08003554 pm_request_autosuspend(q->dev);
Lin Ming6c954662013-03-23 11:42:26 +08003555 } else {
3556 q->rpm_status = RPM_SUSPENDED;
3557 }
3558 spin_unlock_irq(q->queue_lock);
3559}
3560EXPORT_SYMBOL(blk_post_runtime_resume);
Mika Westerbergd07ab6d2016-02-18 10:54:11 +02003561
3562/**
3563 * blk_set_runtime_active - Force runtime status of the queue to be active
3564 * @q: the queue of the device
3565 *
3566 * If the device is left runtime suspended during system suspend the resume
3567 * hook typically resumes the device and corrects runtime status
3568 * accordingly. However, that does not affect the queue runtime PM status
3569 * which is still "suspended". This prevents processing requests from the
3570 * queue.
3571 *
3572 * This function can be used in driver's resume hook to correct queue
3573 * runtime PM status and re-enable peeking requests from the queue. It
3574 * should be called before first request is added to the queue.
3575 */
3576void blk_set_runtime_active(struct request_queue *q)
3577{
3578 spin_lock_irq(q->queue_lock);
3579 q->rpm_status = RPM_ACTIVE;
3580 pm_runtime_mark_last_busy(q->dev);
3581 pm_request_autosuspend(q->dev);
3582 spin_unlock_irq(q->queue_lock);
3583}
3584EXPORT_SYMBOL(blk_set_runtime_active);
Lin Ming6c954662013-03-23 11:42:26 +08003585#endif
3586
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587int __init blk_dev_init(void)
3588{
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02003589 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
Maninder Singh0762b232015-07-07 12:41:07 +05303590 FIELD_SIZEOF(struct request, cmd_flags));
Nikanth Karthikesan9eb55b02009-04-27 14:53:54 +02003591
Tejun Heo89b90be2011-01-03 15:01:47 +01003592 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
3593 kblockd_workqueue = alloc_workqueue("kblockd",
Matias Bjørling28747fc2014-06-11 23:43:54 +02003594 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 if (!kblockd_workqueue)
3596 panic("Failed to create kblockd\n");
3597
3598 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09003599 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
Ilya Dryomovc2789bd2015-11-20 22:16:46 +01003601 blk_requestq_cachep = kmem_cache_create("request_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02003602 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 return 0;
3605}
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003606
3607/*
3608 * Blk IO latency support. We want this to be as cheap as possible, so doing
3609 * this lockless (and avoiding atomics), a few off by a few errors in this
3610 * code is not harmful, and we don't want to do anything that is
3611 * perf-impactful.
3612 * TODO : If necessary, we can make the histograms per-cpu and aggregate
3613 * them when printing them out.
3614 */
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003615ssize_t
Hyojun Kim11537d52017-12-21 09:57:41 -08003616blk_latency_hist_show(char* name, struct io_latency_state *s, char *buf,
3617 int buf_size)
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003618{
3619 int i;
3620 int bytes_written = 0;
3621 u_int64_t num_elem, elem;
3622 int pct;
Hyojun Kim11537d52017-12-21 09:57:41 -08003623 u_int64_t average;
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003624
Hyojun Kim11537d52017-12-21 09:57:41 -08003625 num_elem = s->latency_elems;
3626 if (num_elem > 0) {
3627 average = div64_u64(s->latency_sum, s->latency_elems);
3628 bytes_written += scnprintf(buf + bytes_written,
3629 buf_size - bytes_written,
3630 "IO svc_time %s Latency Histogram (n = %llu,"
3631 " average = %llu):\n", name, num_elem, average);
3632 for (i = 0;
3633 i < ARRAY_SIZE(latency_x_axis_us);
3634 i++) {
3635 elem = s->latency_y_axis[i];
3636 pct = div64_u64(elem * 100, num_elem);
3637 bytes_written += scnprintf(buf + bytes_written,
3638 PAGE_SIZE - bytes_written,
3639 "\t< %6lluus%15llu%15d%%\n",
3640 latency_x_axis_us[i],
3641 elem, pct);
3642 }
3643 /* Last element in y-axis table is overflow */
3644 elem = s->latency_y_axis[i];
3645 pct = div64_u64(elem * 100, num_elem);
3646 bytes_written += scnprintf(buf + bytes_written,
3647 PAGE_SIZE - bytes_written,
3648 "\t>=%6lluus%15llu%15d%%\n",
3649 latency_x_axis_us[i - 1], elem, pct);
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003650 }
Hyojun Kim11537d52017-12-21 09:57:41 -08003651
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003652 return bytes_written;
3653}
3654EXPORT_SYMBOL(blk_latency_hist_show);