2 * 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>
6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
8 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
12 * This handles all read/write requests to block devices
14 #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>
19 #include <linux/blk-mq.h>
20 #include <linux/highmem.h>
22 #include <linux/kernel_stat.h>
23 #include <linux/string.h>
24 #include <linux/init.h>
25 #include <linux/completion.h>
26 #include <linux/slab.h>
27 #include <linux/swap.h>
28 #include <linux/writeback.h>
29 #include <linux/task_io_accounting_ops.h>
30 #include <linux/fault-inject.h>
31 #include <linux/list_sort.h>
32 #include <linux/delay.h>
33 #include <linux/ratelimit.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/blk-cgroup.h>
36 #include <linux/debugfs.h>
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/block.h>
43 #include "blk-mq-sched.h"
46 #ifdef CONFIG_DEBUG_FS
47 struct dentry
*blk_debugfs_root
;
50 EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap
);
51 EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap
);
52 EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete
);
53 EXPORT_TRACEPOINT_SYMBOL_GPL(block_split
);
54 EXPORT_TRACEPOINT_SYMBOL_GPL(block_unplug
);
56 DEFINE_IDA(blk_queue_ida
);
59 * For the allocated request tables
61 struct kmem_cache
*request_cachep
;
64 * For queue allocation
66 struct kmem_cache
*blk_requestq_cachep
;
69 * Controlling structure to kblockd
71 static struct workqueue_struct
*kblockd_workqueue
;
73 static void blk_clear_congested(struct request_list
*rl
, int sync
)
75 #ifdef CONFIG_CGROUP_WRITEBACK
76 clear_wb_congested(rl
->blkg
->wb_congested
, sync
);
79 * If !CGROUP_WRITEBACK, all blkg's map to bdi->wb and we shouldn't
80 * flip its congestion state for events on other blkcgs.
82 if (rl
== &rl
->q
->root_rl
)
83 clear_wb_congested(rl
->q
->backing_dev_info
->wb
.congested
, sync
);
87 static void blk_set_congested(struct request_list
*rl
, int sync
)
89 #ifdef CONFIG_CGROUP_WRITEBACK
90 set_wb_congested(rl
->blkg
->wb_congested
, sync
);
92 /* see blk_clear_congested() */
93 if (rl
== &rl
->q
->root_rl
)
94 set_wb_congested(rl
->q
->backing_dev_info
->wb
.congested
, sync
);
98 void blk_queue_congestion_threshold(struct request_queue
*q
)
102 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) + 1;
103 if (nr
> q
->nr_requests
)
105 q
->nr_congestion_on
= nr
;
107 nr
= q
->nr_requests
- (q
->nr_requests
/ 8) - (q
->nr_requests
/ 16) - 1;
110 q
->nr_congestion_off
= nr
;
113 void blk_rq_init(struct request_queue
*q
, struct request
*rq
)
115 memset(rq
, 0, sizeof(*rq
));
117 INIT_LIST_HEAD(&rq
->queuelist
);
118 INIT_LIST_HEAD(&rq
->timeout_list
);
121 rq
->__sector
= (sector_t
) -1;
122 INIT_HLIST_NODE(&rq
->hash
);
123 RB_CLEAR_NODE(&rq
->rb_node
);
125 rq
->internal_tag
= -1;
126 rq
->start_time
= jiffies
;
127 set_start_time_ns(rq
);
130 EXPORT_SYMBOL(blk_rq_init
);
132 static void req_bio_endio(struct request
*rq
, struct bio
*bio
,
133 unsigned int nbytes
, int error
)
136 bio
->bi_error
= error
;
138 if (unlikely(rq
->rq_flags
& RQF_QUIET
))
139 bio_set_flag(bio
, BIO_QUIET
);
141 bio_advance(bio
, nbytes
);
143 /* don't actually finish bio if it's part of flush sequence */
144 if (bio
->bi_iter
.bi_size
== 0 && !(rq
->rq_flags
& RQF_FLUSH_SEQ
))
148 void blk_dump_rq_flags(struct request
*rq
, char *msg
)
150 printk(KERN_INFO
"%s: dev %s: flags=%llx\n", msg
,
151 rq
->rq_disk
? rq
->rq_disk
->disk_name
: "?",
152 (unsigned long long) rq
->cmd_flags
);
154 printk(KERN_INFO
" sector %llu, nr/cnr %u/%u\n",
155 (unsigned long long)blk_rq_pos(rq
),
156 blk_rq_sectors(rq
), blk_rq_cur_sectors(rq
));
157 printk(KERN_INFO
" bio %p, biotail %p, len %u\n",
158 rq
->bio
, rq
->biotail
, blk_rq_bytes(rq
));
160 EXPORT_SYMBOL(blk_dump_rq_flags
);
162 static void blk_delay_work(struct work_struct
*work
)
164 struct request_queue
*q
;
166 q
= container_of(work
, struct request_queue
, delay_work
.work
);
167 spin_lock_irq(q
->queue_lock
);
169 spin_unlock_irq(q
->queue_lock
);
173 * blk_delay_queue - restart queueing after defined interval
174 * @q: The &struct request_queue in question
175 * @msecs: Delay in msecs
178 * Sometimes queueing needs to be postponed for a little while, to allow
179 * resources to come back. This function will make sure that queueing is
180 * restarted around the specified time. Queue lock must be held.
182 void blk_delay_queue(struct request_queue
*q
, unsigned long msecs
)
184 if (likely(!blk_queue_dead(q
)))
185 queue_delayed_work(kblockd_workqueue
, &q
->delay_work
,
186 msecs_to_jiffies(msecs
));
188 EXPORT_SYMBOL(blk_delay_queue
);
191 * blk_start_queue_async - asynchronously restart a previously stopped queue
192 * @q: The &struct request_queue in question
195 * blk_start_queue_async() will clear the stop flag on the queue, and
196 * ensure that the request_fn for the queue is run from an async
199 void blk_start_queue_async(struct request_queue
*q
)
201 queue_flag_clear(QUEUE_FLAG_STOPPED
, q
);
202 blk_run_queue_async(q
);
204 EXPORT_SYMBOL(blk_start_queue_async
);
207 * blk_start_queue - restart a previously stopped queue
208 * @q: The &struct request_queue in question
211 * blk_start_queue() will clear the stop flag on the queue, and call
212 * the request_fn for the queue if it was in a stopped state when
213 * entered. Also see blk_stop_queue(). Queue lock must be held.
215 void blk_start_queue(struct request_queue
*q
)
217 WARN_ON(!irqs_disabled());
219 queue_flag_clear(QUEUE_FLAG_STOPPED
, q
);
222 EXPORT_SYMBOL(blk_start_queue
);
225 * blk_stop_queue - stop a queue
226 * @q: The &struct request_queue in question
229 * The Linux block layer assumes that a block driver will consume all
230 * entries on the request queue when the request_fn strategy is called.
231 * Often this will not happen, because of hardware limitations (queue
232 * depth settings). If a device driver gets a 'queue full' response,
233 * or if it simply chooses not to queue more I/O at one point, it can
234 * call this function to prevent the request_fn from being called until
235 * the driver has signalled it's ready to go again. This happens by calling
236 * blk_start_queue() to restart queue operations. Queue lock must be held.
238 void blk_stop_queue(struct request_queue
*q
)
240 cancel_delayed_work(&q
->delay_work
);
241 queue_flag_set(QUEUE_FLAG_STOPPED
, q
);
243 EXPORT_SYMBOL(blk_stop_queue
);
246 * blk_sync_queue - cancel any pending callbacks on a queue
250 * The block layer may perform asynchronous callback activity
251 * on a queue, such as calling the unplug function after a timeout.
252 * A block device may call blk_sync_queue to ensure that any
253 * such activity is cancelled, thus allowing it to release resources
254 * that the callbacks might use. The caller must already have made sure
255 * that its ->make_request_fn will not re-add plugging prior to calling
258 * This function does not cancel any asynchronous activity arising
259 * out of elevator or throttling code. That would require elevator_exit()
260 * and blkcg_exit_queue() to be called with queue lock initialized.
263 void blk_sync_queue(struct request_queue
*q
)
265 del_timer_sync(&q
->timeout
);
268 struct blk_mq_hw_ctx
*hctx
;
271 queue_for_each_hw_ctx(q
, hctx
, i
)
272 cancel_delayed_work_sync(&hctx
->run_work
);
274 cancel_delayed_work_sync(&q
->delay_work
);
277 EXPORT_SYMBOL(blk_sync_queue
);
280 * __blk_run_queue_uncond - run a queue whether or not it has been stopped
281 * @q: The queue to run
284 * Invoke request handling on a queue if there are any pending requests.
285 * May be used to restart request handling after a request has completed.
286 * This variant runs the queue whether or not the queue has been
287 * stopped. Must be called with the queue lock held and interrupts
288 * disabled. See also @blk_run_queue.
290 inline void __blk_run_queue_uncond(struct request_queue
*q
)
292 if (unlikely(blk_queue_dead(q
)))
296 * Some request_fn implementations, e.g. scsi_request_fn(), unlock
297 * the queue lock internally. As a result multiple threads may be
298 * running such a request function concurrently. Keep track of the
299 * number of active request_fn invocations such that blk_drain_queue()
300 * can wait until all these request_fn calls have finished.
302 q
->request_fn_active
++;
304 q
->request_fn_active
--;
306 EXPORT_SYMBOL_GPL(__blk_run_queue_uncond
);
309 * __blk_run_queue - run a single device queue
310 * @q: The queue to run
313 * See @blk_run_queue. This variant must be called with the queue lock
314 * held and interrupts disabled.
316 void __blk_run_queue(struct request_queue
*q
)
318 if (unlikely(blk_queue_stopped(q
)))
321 __blk_run_queue_uncond(q
);
323 EXPORT_SYMBOL(__blk_run_queue
);
326 * blk_run_queue_async - run a single device queue in workqueue context
327 * @q: The queue to run
330 * Tells kblockd to perform the equivalent of @blk_run_queue on behalf
331 * of us. The caller must hold the queue lock.
333 void blk_run_queue_async(struct request_queue
*q
)
335 if (likely(!blk_queue_stopped(q
) && !blk_queue_dead(q
)))
336 mod_delayed_work(kblockd_workqueue
, &q
->delay_work
, 0);
338 EXPORT_SYMBOL(blk_run_queue_async
);
341 * blk_run_queue - run a single device queue
342 * @q: The queue to run
345 * Invoke request handling on this queue, if it has pending work to do.
346 * May be used to restart queueing when a request has completed.
348 void blk_run_queue(struct request_queue
*q
)
352 spin_lock_irqsave(q
->queue_lock
, flags
);
354 spin_unlock_irqrestore(q
->queue_lock
, flags
);
356 EXPORT_SYMBOL(blk_run_queue
);
358 void blk_put_queue(struct request_queue
*q
)
360 kobject_put(&q
->kobj
);
362 EXPORT_SYMBOL(blk_put_queue
);
365 * __blk_drain_queue - drain requests from request_queue
367 * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV
369 * Drain requests from @q. If @drain_all is set, all requests are drained.
370 * If not, only ELVPRIV requests are drained. The caller is responsible
371 * for ensuring that no new requests which need to be drained are queued.
373 static void __blk_drain_queue(struct request_queue
*q
, bool drain_all
)
374 __releases(q
->queue_lock
)
375 __acquires(q
->queue_lock
)
379 lockdep_assert_held(q
->queue_lock
);
385 * The caller might be trying to drain @q before its
386 * elevator is initialized.
389 elv_drain_elevator(q
);
391 blkcg_drain_queue(q
);
394 * This function might be called on a queue which failed
395 * driver init after queue creation or is not yet fully
396 * active yet. Some drivers (e.g. fd and loop) get unhappy
397 * in such cases. Kick queue iff dispatch queue has
398 * something on it and @q has request_fn set.
400 if (!list_empty(&q
->queue_head
) && q
->request_fn
)
403 drain
|= q
->nr_rqs_elvpriv
;
404 drain
|= q
->request_fn_active
;
407 * Unfortunately, requests are queued at and tracked from
408 * multiple places and there's no single counter which can
409 * be drained. Check all the queues and counters.
412 struct blk_flush_queue
*fq
= blk_get_flush_queue(q
, NULL
);
413 drain
|= !list_empty(&q
->queue_head
);
414 for (i
= 0; i
< 2; i
++) {
415 drain
|= q
->nr_rqs
[i
];
416 drain
|= q
->in_flight
[i
];
418 drain
|= !list_empty(&fq
->flush_queue
[i
]);
425 spin_unlock_irq(q
->queue_lock
);
429 spin_lock_irq(q
->queue_lock
);
433 * With queue marked dead, any woken up waiter will fail the
434 * allocation path, so the wakeup chaining is lost and we're
435 * left with hung waiters. We need to wake up those waiters.
438 struct request_list
*rl
;
440 blk_queue_for_each_rl(rl
, q
)
441 for (i
= 0; i
< ARRAY_SIZE(rl
->wait
); i
++)
442 wake_up_all(&rl
->wait
[i
]);
447 * blk_queue_bypass_start - enter queue bypass mode
448 * @q: queue of interest
450 * In bypass mode, only the dispatch FIFO queue of @q is used. This
451 * function makes @q enter bypass mode and drains all requests which were
452 * throttled or issued before. On return, it's guaranteed that no request
453 * is being throttled or has ELVPRIV set and blk_queue_bypass() %true
454 * inside queue or RCU read lock.
456 void blk_queue_bypass_start(struct request_queue
*q
)
458 spin_lock_irq(q
->queue_lock
);
460 queue_flag_set(QUEUE_FLAG_BYPASS
, q
);
461 spin_unlock_irq(q
->queue_lock
);
464 * Queues start drained. Skip actual draining till init is
465 * complete. This avoids lenghty delays during queue init which
466 * can happen many times during boot.
468 if (blk_queue_init_done(q
)) {
469 spin_lock_irq(q
->queue_lock
);
470 __blk_drain_queue(q
, false);
471 spin_unlock_irq(q
->queue_lock
);
473 /* ensure blk_queue_bypass() is %true inside RCU read lock */
477 EXPORT_SYMBOL_GPL(blk_queue_bypass_start
);
480 * blk_queue_bypass_end - leave queue bypass mode
481 * @q: queue of interest
483 * Leave bypass mode and restore the normal queueing behavior.
485 void blk_queue_bypass_end(struct request_queue
*q
)
487 spin_lock_irq(q
->queue_lock
);
488 if (!--q
->bypass_depth
)
489 queue_flag_clear(QUEUE_FLAG_BYPASS
, q
);
490 WARN_ON_ONCE(q
->bypass_depth
< 0);
491 spin_unlock_irq(q
->queue_lock
);
493 EXPORT_SYMBOL_GPL(blk_queue_bypass_end
);
495 void blk_set_queue_dying(struct request_queue
*q
)
497 spin_lock_irq(q
->queue_lock
);
498 queue_flag_set(QUEUE_FLAG_DYING
, q
);
499 spin_unlock_irq(q
->queue_lock
);
502 * When queue DYING flag is set, we need to block new req
503 * entering queue, so we call blk_freeze_queue_start() to
504 * prevent I/O from crossing blk_queue_enter().
506 blk_freeze_queue_start(q
);
509 blk_mq_wake_waiters(q
);
511 struct request_list
*rl
;
513 spin_lock_irq(q
->queue_lock
);
514 blk_queue_for_each_rl(rl
, q
) {
516 wake_up(&rl
->wait
[BLK_RW_SYNC
]);
517 wake_up(&rl
->wait
[BLK_RW_ASYNC
]);
520 spin_unlock_irq(q
->queue_lock
);
523 EXPORT_SYMBOL_GPL(blk_set_queue_dying
);
526 * blk_cleanup_queue - shutdown a request queue
527 * @q: request queue to shutdown
529 * Mark @q DYING, drain all pending requests, mark @q DEAD, destroy and
530 * put it. All future requests will be failed immediately with -ENODEV.
532 void blk_cleanup_queue(struct request_queue
*q
)
534 spinlock_t
*lock
= q
->queue_lock
;
536 /* mark @q DYING, no new request or merges will be allowed afterwards */
537 mutex_lock(&q
->sysfs_lock
);
538 blk_set_queue_dying(q
);
542 * A dying queue is permanently in bypass mode till released. Note
543 * 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.
551 queue_flag_set(QUEUE_FLAG_BYPASS
, q
);
553 queue_flag_set(QUEUE_FLAG_NOMERGES
, q
);
554 queue_flag_set(QUEUE_FLAG_NOXMERGES
, q
);
555 queue_flag_set(QUEUE_FLAG_DYING
, q
);
556 spin_unlock_irq(lock
);
557 mutex_unlock(&q
->sysfs_lock
);
560 * Drain all requests queued before DYING marking. Set DEAD flag to
561 * prevent that q->request_fn() gets invoked after draining finished.
566 __blk_drain_queue(q
, true);
568 blk_mq_debugfs_unregister_mq(q
);
571 queue_flag_set(QUEUE_FLAG_DEAD
, q
);
572 spin_unlock_irq(lock
);
574 /* for synchronous bio-based driver finish in-flight integrity i/o */
575 blk_flush_integrity();
577 /* @q won't process any more request, flush async actions */
578 del_timer_sync(&q
->backing_dev_info
->laptop_mode_wb_timer
);
582 blk_mq_free_queue(q
);
583 percpu_ref_exit(&q
->q_usage_counter
);
586 if (q
->queue_lock
!= &q
->__queue_lock
)
587 q
->queue_lock
= &q
->__queue_lock
;
588 spin_unlock_irq(lock
);
590 /* @q is and will stay empty, shutdown and put */
593 EXPORT_SYMBOL(blk_cleanup_queue
);
595 /* Allocate memory local to the request queue */
596 static void *alloc_request_simple(gfp_t gfp_mask
, void *data
)
598 struct request_queue
*q
= data
;
600 return kmem_cache_alloc_node(request_cachep
, gfp_mask
, q
->node
);
603 static void free_request_simple(void *element
, void *data
)
605 kmem_cache_free(request_cachep
, element
);
608 static void *alloc_request_size(gfp_t gfp_mask
, void *data
)
610 struct request_queue
*q
= data
;
613 rq
= kmalloc_node(sizeof(struct request
) + q
->cmd_size
, gfp_mask
,
615 if (rq
&& q
->init_rq_fn
&& q
->init_rq_fn(q
, rq
, gfp_mask
) < 0) {
622 static void free_request_size(void *element
, void *data
)
624 struct request_queue
*q
= data
;
627 q
->exit_rq_fn(q
, element
);
631 int blk_init_rl(struct request_list
*rl
, struct request_queue
*q
,
634 if (unlikely(rl
->rq_pool
))
638 rl
->count
[BLK_RW_SYNC
] = rl
->count
[BLK_RW_ASYNC
] = 0;
639 rl
->starved
[BLK_RW_SYNC
] = rl
->starved
[BLK_RW_ASYNC
] = 0;
640 init_waitqueue_head(&rl
->wait
[BLK_RW_SYNC
]);
641 init_waitqueue_head(&rl
->wait
[BLK_RW_ASYNC
]);
644 rl
->rq_pool
= mempool_create_node(BLKDEV_MIN_RQ
,
645 alloc_request_size
, free_request_size
,
646 q
, gfp_mask
, q
->node
);
648 rl
->rq_pool
= mempool_create_node(BLKDEV_MIN_RQ
,
649 alloc_request_simple
, free_request_simple
,
650 q
, gfp_mask
, q
->node
);
658 void blk_exit_rl(struct request_list
*rl
)
661 mempool_destroy(rl
->rq_pool
);
664 struct request_queue
*blk_alloc_queue(gfp_t gfp_mask
)
666 return blk_alloc_queue_node(gfp_mask
, NUMA_NO_NODE
);
668 EXPORT_SYMBOL(blk_alloc_queue
);
670 int blk_queue_enter(struct request_queue
*q
, bool nowait
)
675 if (percpu_ref_tryget_live(&q
->q_usage_counter
))
682 * read pair of barrier in blk_freeze_queue_start(),
683 * we need to order reading __PERCPU_REF_DEAD flag of
684 * .q_usage_counter and reading .mq_freeze_depth or
685 * queue dying flag, otherwise the following wait may
686 * never return if the two reads are reordered.
690 ret
= wait_event_interruptible(q
->mq_freeze_wq
,
691 !atomic_read(&q
->mq_freeze_depth
) ||
693 if (blk_queue_dying(q
))
700 void blk_queue_exit(struct request_queue
*q
)
702 percpu_ref_put(&q
->q_usage_counter
);
705 static void blk_queue_usage_counter_release(struct percpu_ref
*ref
)
707 struct request_queue
*q
=
708 container_of(ref
, struct request_queue
, q_usage_counter
);
710 wake_up_all(&q
->mq_freeze_wq
);
713 static void blk_rq_timed_out_timer(unsigned long data
)
715 struct request_queue
*q
= (struct request_queue
*)data
;
717 kblockd_schedule_work(&q
->timeout_work
);
720 struct request_queue
*blk_alloc_queue_node(gfp_t gfp_mask
, int node_id
)
722 struct request_queue
*q
;
724 q
= kmem_cache_alloc_node(blk_requestq_cachep
,
725 gfp_mask
| __GFP_ZERO
, node_id
);
729 q
->id
= ida_simple_get(&blk_queue_ida
, 0, 0, gfp_mask
);
733 q
->bio_split
= bioset_create(BIO_POOL_SIZE
, 0);
737 q
->backing_dev_info
= bdi_alloc_node(gfp_mask
, node_id
);
738 if (!q
->backing_dev_info
)
741 q
->stats
= blk_alloc_queue_stats();
745 q
->backing_dev_info
->ra_pages
=
746 (VM_MAX_READAHEAD
* 1024) / PAGE_SIZE
;
747 q
->backing_dev_info
->capabilities
= BDI_CAP_CGROUP_WRITEBACK
;
748 q
->backing_dev_info
->name
= "block";
751 setup_timer(&q
->backing_dev_info
->laptop_mode_wb_timer
,
752 laptop_mode_timer_fn
, (unsigned long) q
);
753 setup_timer(&q
->timeout
, blk_rq_timed_out_timer
, (unsigned long) q
);
754 INIT_LIST_HEAD(&q
->queue_head
);
755 INIT_LIST_HEAD(&q
->timeout_list
);
756 INIT_LIST_HEAD(&q
->icq_list
);
757 #ifdef CONFIG_BLK_CGROUP
758 INIT_LIST_HEAD(&q
->blkg_list
);
760 INIT_DELAYED_WORK(&q
->delay_work
, blk_delay_work
);
762 kobject_init(&q
->kobj
, &blk_queue_ktype
);
764 mutex_init(&q
->sysfs_lock
);
765 spin_lock_init(&q
->__queue_lock
);
768 * By default initialize queue_lock to internal lock and driver can
769 * override it later if need be.
771 q
->queue_lock
= &q
->__queue_lock
;
774 * A queue starts its life with bypass turned on to avoid
775 * unnecessary bypass on/off overhead and nasty surprises during
776 * init. The initial bypass will be finished when the queue is
777 * registered by blk_register_queue().
780 __set_bit(QUEUE_FLAG_BYPASS
, &q
->queue_flags
);
782 init_waitqueue_head(&q
->mq_freeze_wq
);
785 * Init percpu_ref in atomic mode so that it's faster to shutdown.
786 * See blk_register_queue() for details.
788 if (percpu_ref_init(&q
->q_usage_counter
,
789 blk_queue_usage_counter_release
,
790 PERCPU_REF_INIT_ATOMIC
, GFP_KERNEL
))
793 if (blkcg_init_queue(q
))
799 percpu_ref_exit(&q
->q_usage_counter
);
801 blk_free_queue_stats(q
->stats
);
803 bdi_put(q
->backing_dev_info
);
805 bioset_free(q
->bio_split
);
807 ida_simple_remove(&blk_queue_ida
, q
->id
);
809 kmem_cache_free(blk_requestq_cachep
, q
);
812 EXPORT_SYMBOL(blk_alloc_queue_node
);
815 * blk_init_queue - prepare a request queue for use with a block device
816 * @rfn: The function to be called to process requests that have been
817 * placed on the queue.
818 * @lock: Request queue spin lock
821 * If a block device wishes to use the standard request handling procedures,
822 * which sorts requests and coalesces adjacent requests, then it must
823 * call blk_init_queue(). The function @rfn will be called when there
824 * are requests on the queue that need to be processed. If the device
825 * supports plugging, then @rfn may not be called immediately when requests
826 * are available on the queue, but may be called at some time later instead.
827 * Plugged queues are generally unplugged when a buffer belonging to one
828 * of the requests on the queue is needed, or due to memory pressure.
830 * @rfn is not required, or even expected, to remove all requests off the
831 * queue, but only as many as it can handle at a time. If it does leave
832 * requests on the queue, it is responsible for arranging that the requests
833 * get dealt with eventually.
835 * The queue spin lock must be held while manipulating the requests on the
836 * request queue; this lock will be taken also from interrupt context, so irq
837 * disabling is needed for it.
839 * Function returns a pointer to the initialized request queue, or %NULL if
843 * blk_init_queue() must be paired with a blk_cleanup_queue() call
844 * when the block device is deactivated (such as at module unload).
847 struct request_queue
*blk_init_queue(request_fn_proc
*rfn
, spinlock_t
*lock
)
849 return blk_init_queue_node(rfn
, lock
, NUMA_NO_NODE
);
851 EXPORT_SYMBOL(blk_init_queue
);
853 struct request_queue
*
854 blk_init_queue_node(request_fn_proc
*rfn
, spinlock_t
*lock
, int node_id
)
856 struct request_queue
*q
;
858 q
= blk_alloc_queue_node(GFP_KERNEL
, node_id
);
864 q
->queue_lock
= lock
;
865 if (blk_init_allocated_queue(q
) < 0) {
866 blk_cleanup_queue(q
);
872 EXPORT_SYMBOL(blk_init_queue_node
);
874 static blk_qc_t
blk_queue_bio(struct request_queue
*q
, struct bio
*bio
);
877 int blk_init_allocated_queue(struct request_queue
*q
)
879 q
->fq
= blk_alloc_flush_queue(q
, NUMA_NO_NODE
, q
->cmd_size
);
883 if (q
->init_rq_fn
&& q
->init_rq_fn(q
, q
->fq
->flush_rq
, GFP_KERNEL
))
884 goto out_free_flush_queue
;
886 if (blk_init_rl(&q
->root_rl
, q
, GFP_KERNEL
))
887 goto out_exit_flush_rq
;
889 INIT_WORK(&q
->timeout_work
, blk_timeout_work
);
890 q
->queue_flags
|= QUEUE_FLAG_DEFAULT
;
893 * This also sets hw/phys segments, boundary and size
895 blk_queue_make_request(q
, blk_queue_bio
);
897 q
->sg_reserved_size
= INT_MAX
;
899 /* Protect q->elevator from elevator_change */
900 mutex_lock(&q
->sysfs_lock
);
903 if (elevator_init(q
, NULL
)) {
904 mutex_unlock(&q
->sysfs_lock
);
905 goto out_exit_flush_rq
;
908 mutex_unlock(&q
->sysfs_lock
);
913 q
->exit_rq_fn(q
, q
->fq
->flush_rq
);
914 out_free_flush_queue
:
915 blk_free_flush_queue(q
->fq
);
918 EXPORT_SYMBOL(blk_init_allocated_queue
);
920 bool blk_get_queue(struct request_queue
*q
)
922 if (likely(!blk_queue_dying(q
))) {
929 EXPORT_SYMBOL(blk_get_queue
);
931 static inline void blk_free_request(struct request_list
*rl
, struct request
*rq
)
933 if (rq
->rq_flags
& RQF_ELVPRIV
) {
934 elv_put_request(rl
->q
, rq
);
936 put_io_context(rq
->elv
.icq
->ioc
);
939 mempool_free(rq
, rl
->rq_pool
);
943 * ioc_batching returns true if the ioc is a valid batching request and
944 * should be given priority access to a request.
946 static inline int ioc_batching(struct request_queue
*q
, struct io_context
*ioc
)
952 * Make sure the process is able to allocate at least 1 request
953 * even if the batch times out, otherwise we could theoretically
956 return ioc
->nr_batch_requests
== q
->nr_batching
||
957 (ioc
->nr_batch_requests
> 0
958 && time_before(jiffies
, ioc
->last_waited
+ BLK_BATCH_TIME
));
962 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
963 * will cause the process to be a "batcher" on all queues in the system. This
964 * is the behaviour we want though - once it gets a wakeup it should be given
967 static void ioc_set_batching(struct request_queue
*q
, struct io_context
*ioc
)
969 if (!ioc
|| ioc_batching(q
, ioc
))
972 ioc
->nr_batch_requests
= q
->nr_batching
;
973 ioc
->last_waited
= jiffies
;
976 static void __freed_request(struct request_list
*rl
, int sync
)
978 struct request_queue
*q
= rl
->q
;
980 if (rl
->count
[sync
] < queue_congestion_off_threshold(q
))
981 blk_clear_congested(rl
, sync
);
983 if (rl
->count
[sync
] + 1 <= q
->nr_requests
) {
984 if (waitqueue_active(&rl
->wait
[sync
]))
985 wake_up(&rl
->wait
[sync
]);
987 blk_clear_rl_full(rl
, sync
);
992 * A request has just been released. Account for it, update the full and
993 * congestion status, wake up any waiters. Called under q->queue_lock.
995 static void freed_request(struct request_list
*rl
, bool sync
,
996 req_flags_t rq_flags
)
998 struct request_queue
*q
= rl
->q
;
1002 if (rq_flags
& RQF_ELVPRIV
)
1003 q
->nr_rqs_elvpriv
--;
1005 __freed_request(rl
, sync
);
1007 if (unlikely(rl
->starved
[sync
^ 1]))
1008 __freed_request(rl
, sync
^ 1);
1011 int blk_update_nr_requests(struct request_queue
*q
, unsigned int nr
)
1013 struct request_list
*rl
;
1014 int on_thresh
, off_thresh
;
1016 spin_lock_irq(q
->queue_lock
);
1017 q
->nr_requests
= nr
;
1018 blk_queue_congestion_threshold(q
);
1019 on_thresh
= queue_congestion_on_threshold(q
);
1020 off_thresh
= queue_congestion_off_threshold(q
);
1022 blk_queue_for_each_rl(rl
, q
) {
1023 if (rl
->count
[BLK_RW_SYNC
] >= on_thresh
)
1024 blk_set_congested(rl
, BLK_RW_SYNC
);
1025 else if (rl
->count
[BLK_RW_SYNC
] < off_thresh
)
1026 blk_clear_congested(rl
, BLK_RW_SYNC
);
1028 if (rl
->count
[BLK_RW_ASYNC
] >= on_thresh
)
1029 blk_set_congested(rl
, BLK_RW_ASYNC
);
1030 else if (rl
->count
[BLK_RW_ASYNC
] < off_thresh
)
1031 blk_clear_congested(rl
, BLK_RW_ASYNC
);
1033 if (rl
->count
[BLK_RW_SYNC
] >= q
->nr_requests
) {
1034 blk_set_rl_full(rl
, BLK_RW_SYNC
);
1036 blk_clear_rl_full(rl
, BLK_RW_SYNC
);
1037 wake_up(&rl
->wait
[BLK_RW_SYNC
]);
1040 if (rl
->count
[BLK_RW_ASYNC
] >= q
->nr_requests
) {
1041 blk_set_rl_full(rl
, BLK_RW_ASYNC
);
1043 blk_clear_rl_full(rl
, BLK_RW_ASYNC
);
1044 wake_up(&rl
->wait
[BLK_RW_ASYNC
]);
1048 spin_unlock_irq(q
->queue_lock
);
1053 * __get_request - get a free request
1054 * @rl: request list to allocate from
1055 * @op: operation and flags
1056 * @bio: bio to allocate request for (can be %NULL)
1057 * @gfp_mask: allocation mask
1059 * Get a free request from @q. This function may fail under memory
1060 * pressure or if @q is dead.
1062 * Must be called with @q->queue_lock held and,
1063 * Returns ERR_PTR on failure, with @q->queue_lock held.
1064 * Returns request pointer on success, with @q->queue_lock *not held*.
1066 static struct request
*__get_request(struct request_list
*rl
, unsigned int op
,
1067 struct bio
*bio
, gfp_t gfp_mask
)
1069 struct request_queue
*q
= rl
->q
;
1071 struct elevator_type
*et
= q
->elevator
->type
;
1072 struct io_context
*ioc
= rq_ioc(bio
);
1073 struct io_cq
*icq
= NULL
;
1074 const bool is_sync
= op_is_sync(op
);
1076 req_flags_t rq_flags
= RQF_ALLOCED
;
1078 if (unlikely(blk_queue_dying(q
)))
1079 return ERR_PTR(-ENODEV
);
1081 may_queue
= elv_may_queue(q
, op
);
1082 if (may_queue
== ELV_MQUEUE_NO
)
1085 if (rl
->count
[is_sync
]+1 >= queue_congestion_on_threshold(q
)) {
1086 if (rl
->count
[is_sync
]+1 >= q
->nr_requests
) {
1088 * The queue will fill after this allocation, so set
1089 * it as full, and mark this process as "batching".
1090 * This process will be allowed to complete a batch of
1091 * requests, others will be blocked.
1093 if (!blk_rl_full(rl
, is_sync
)) {
1094 ioc_set_batching(q
, ioc
);
1095 blk_set_rl_full(rl
, is_sync
);
1097 if (may_queue
!= ELV_MQUEUE_MUST
1098 && !ioc_batching(q
, ioc
)) {
1100 * The queue is full and the allocating
1101 * process is not a "batcher", and not
1102 * exempted by the IO scheduler
1104 return ERR_PTR(-ENOMEM
);
1108 blk_set_congested(rl
, is_sync
);
1112 * Only allow batching queuers to allocate up to 50% over the defined
1113 * limit of requests, otherwise we could have thousands of requests
1114 * allocated with any setting of ->nr_requests
1116 if (rl
->count
[is_sync
] >= (3 * q
->nr_requests
/ 2))
1117 return ERR_PTR(-ENOMEM
);
1119 q
->nr_rqs
[is_sync
]++;
1120 rl
->count
[is_sync
]++;
1121 rl
->starved
[is_sync
] = 0;
1124 * Decide whether the new request will be managed by elevator. If
1125 * so, mark @rq_flags and increment elvpriv. Non-zero elvpriv will
1126 * prevent the current elevator from being destroyed until the new
1127 * request is freed. This guarantees icq's won't be destroyed and
1128 * makes creating new ones safe.
1130 * Flush requests do not use the elevator so skip initialization.
1131 * This allows a request to share the flush and elevator data.
1133 * Also, lookup icq while holding queue_lock. If it doesn't exist,
1134 * it will be created after releasing queue_lock.
1136 if (!op_is_flush(op
) && !blk_queue_bypass(q
)) {
1137 rq_flags
|= RQF_ELVPRIV
;
1138 q
->nr_rqs_elvpriv
++;
1139 if (et
->icq_cache
&& ioc
)
1140 icq
= ioc_lookup_icq(ioc
, q
);
1143 if (blk_queue_io_stat(q
))
1144 rq_flags
|= RQF_IO_STAT
;
1145 spin_unlock_irq(q
->queue_lock
);
1147 /* allocate and init request */
1148 rq
= mempool_alloc(rl
->rq_pool
, gfp_mask
);
1153 blk_rq_set_rl(rq
, rl
);
1155 rq
->rq_flags
= rq_flags
;
1158 if (rq_flags
& RQF_ELVPRIV
) {
1159 if (unlikely(et
->icq_cache
&& !icq
)) {
1161 icq
= ioc_create_icq(ioc
, q
, gfp_mask
);
1167 if (unlikely(elv_set_request(q
, rq
, bio
, gfp_mask
)))
1170 /* @rq->elv.icq holds io_context until @rq is freed */
1172 get_io_context(icq
->ioc
);
1176 * ioc may be NULL here, and ioc_batching will be false. That's
1177 * OK, if the queue is under the request limit then requests need
1178 * not count toward the nr_batch_requests limit. There will always
1179 * be some limit enforced by BLK_BATCH_TIME.
1181 if (ioc_batching(q
, ioc
))
1182 ioc
->nr_batch_requests
--;
1184 trace_block_getrq(q
, bio
, op
);
1189 * elvpriv init failed. ioc, icq and elvpriv aren't mempool backed
1190 * and may fail indefinitely under memory pressure and thus
1191 * shouldn't stall IO. Treat this request as !elvpriv. This will
1192 * disturb iosched and blkcg but weird is bettern than dead.
1194 printk_ratelimited(KERN_WARNING
"%s: dev %s: request aux data allocation failed, iosched may be disturbed\n",
1195 __func__
, dev_name(q
->backing_dev_info
->dev
));
1197 rq
->rq_flags
&= ~RQF_ELVPRIV
;
1200 spin_lock_irq(q
->queue_lock
);
1201 q
->nr_rqs_elvpriv
--;
1202 spin_unlock_irq(q
->queue_lock
);
1207 * Allocation failed presumably due to memory. Undo anything we
1208 * might have messed up.
1210 * Allocating task should really be put onto the front of the wait
1211 * queue, but this is pretty rare.
1213 spin_lock_irq(q
->queue_lock
);
1214 freed_request(rl
, is_sync
, rq_flags
);
1217 * in the very unlikely event that allocation failed and no
1218 * requests for this direction was pending, mark us starved so that
1219 * freeing of a request in the other direction will notice
1220 * us. another possible fix would be to split the rq mempool into
1224 if (unlikely(rl
->count
[is_sync
] == 0))
1225 rl
->starved
[is_sync
] = 1;
1226 return ERR_PTR(-ENOMEM
);
1230 * get_request - get a free request
1231 * @q: request_queue to allocate request from
1232 * @op: operation and flags
1233 * @bio: bio to allocate request for (can be %NULL)
1234 * @gfp_mask: allocation mask
1236 * Get a free request from @q. If %__GFP_DIRECT_RECLAIM is set in @gfp_mask,
1237 * this function keeps retrying under memory pressure and fails iff @q is dead.
1239 * Must be called with @q->queue_lock held and,
1240 * Returns ERR_PTR on failure, with @q->queue_lock held.
1241 * Returns request pointer on success, with @q->queue_lock *not held*.
1243 static struct request
*get_request(struct request_queue
*q
, unsigned int op
,
1244 struct bio
*bio
, gfp_t gfp_mask
)
1246 const bool is_sync
= op_is_sync(op
);
1248 struct request_list
*rl
;
1251 rl
= blk_get_rl(q
, bio
); /* transferred to @rq on success */
1253 rq
= __get_request(rl
, op
, bio
, gfp_mask
);
1257 if (!gfpflags_allow_blocking(gfp_mask
) || unlikely(blk_queue_dying(q
))) {
1262 /* wait on @rl and retry */
1263 prepare_to_wait_exclusive(&rl
->wait
[is_sync
], &wait
,
1264 TASK_UNINTERRUPTIBLE
);
1266 trace_block_sleeprq(q
, bio
, op
);
1268 spin_unlock_irq(q
->queue_lock
);
1272 * After sleeping, we become a "batching" process and will be able
1273 * to allocate at least one request, and up to a big batch of them
1274 * for a small period time. See ioc_batching, ioc_set_batching
1276 ioc_set_batching(q
, current
->io_context
);
1278 spin_lock_irq(q
->queue_lock
);
1279 finish_wait(&rl
->wait
[is_sync
], &wait
);
1284 static struct request
*blk_old_get_request(struct request_queue
*q
, int rw
,
1289 /* create ioc upfront */
1290 create_io_context(gfp_mask
, q
->node
);
1292 spin_lock_irq(q
->queue_lock
);
1293 rq
= get_request(q
, rw
, NULL
, gfp_mask
);
1295 spin_unlock_irq(q
->queue_lock
);
1299 /* q->queue_lock is unlocked at this point */
1301 rq
->__sector
= (sector_t
) -1;
1302 rq
->bio
= rq
->biotail
= NULL
;
1306 struct request
*blk_get_request(struct request_queue
*q
, int rw
, gfp_t gfp_mask
)
1309 return blk_mq_alloc_request(q
, rw
,
1310 (gfp_mask
& __GFP_DIRECT_RECLAIM
) ?
1311 0 : BLK_MQ_REQ_NOWAIT
);
1313 return blk_old_get_request(q
, rw
, gfp_mask
);
1315 EXPORT_SYMBOL(blk_get_request
);
1318 * blk_requeue_request - put a request back on queue
1319 * @q: request queue where request should be inserted
1320 * @rq: request to be inserted
1323 * Drivers often keep queueing requests until the hardware cannot accept
1324 * more, when that condition happens we need to put the request back
1325 * on the queue. Must be called with queue lock held.
1327 void blk_requeue_request(struct request_queue
*q
, struct request
*rq
)
1329 blk_delete_timer(rq
);
1330 blk_clear_rq_complete(rq
);
1331 trace_block_rq_requeue(q
, rq
);
1332 wbt_requeue(q
->rq_wb
, &rq
->issue_stat
);
1334 if (rq
->rq_flags
& RQF_QUEUED
)
1335 blk_queue_end_tag(q
, rq
);
1337 BUG_ON(blk_queued_rq(rq
));
1339 elv_requeue_request(q
, rq
);
1341 EXPORT_SYMBOL(blk_requeue_request
);
1343 static void add_acct_request(struct request_queue
*q
, struct request
*rq
,
1346 blk_account_io_start(rq
, true);
1347 __elv_add_request(q
, rq
, where
);
1350 static void part_round_stats_single(int cpu
, struct hd_struct
*part
,
1355 if (now
== part
->stamp
)
1358 inflight
= part_in_flight(part
);
1360 __part_stat_add(cpu
, part
, time_in_queue
,
1361 inflight
* (now
- part
->stamp
));
1362 __part_stat_add(cpu
, part
, io_ticks
, (now
- part
->stamp
));
1368 * part_round_stats() - Round off the performance stats on a struct disk_stats.
1369 * @cpu: cpu number for stats access
1370 * @part: target partition
1372 * The average IO queue length and utilisation statistics are maintained
1373 * by observing the current state of the queue length and the amount of
1374 * time it has been in this state for.
1376 * Normally, that accounting is done on IO completion, but that can result
1377 * in more than a second's worth of IO being accounted for within any one
1378 * second, leading to >100% utilisation. To deal with that, we call this
1379 * function to do a round-off before returning the results when reading
1380 * /proc/diskstats. This accounts immediately for all queue usage up to
1381 * the current jiffies and restarts the counters again.
1383 void part_round_stats(int cpu
, struct hd_struct
*part
)
1385 unsigned long now
= jiffies
;
1388 part_round_stats_single(cpu
, &part_to_disk(part
)->part0
, now
);
1389 part_round_stats_single(cpu
, part
, now
);
1391 EXPORT_SYMBOL_GPL(part_round_stats
);
1394 static void blk_pm_put_request(struct request
*rq
)
1396 if (rq
->q
->dev
&& !(rq
->rq_flags
& RQF_PM
) && !--rq
->q
->nr_pending
)
1397 pm_runtime_mark_last_busy(rq
->q
->dev
);
1400 static inline void blk_pm_put_request(struct request
*rq
) {}
1404 * queue lock must be held
1406 void __blk_put_request(struct request_queue
*q
, struct request
*req
)
1408 req_flags_t rq_flags
= req
->rq_flags
;
1414 blk_mq_free_request(req
);
1418 blk_pm_put_request(req
);
1420 elv_completed_request(q
, req
);
1422 /* this is a bio leak */
1423 WARN_ON(req
->bio
!= NULL
);
1425 wbt_done(q
->rq_wb
, &req
->issue_stat
);
1428 * Request may not have originated from ll_rw_blk. if not,
1429 * it didn't come out of our reserved rq pools
1431 if (rq_flags
& RQF_ALLOCED
) {
1432 struct request_list
*rl
= blk_rq_rl(req
);
1433 bool sync
= op_is_sync(req
->cmd_flags
);
1435 BUG_ON(!list_empty(&req
->queuelist
));
1436 BUG_ON(ELV_ON_HASH(req
));
1438 blk_free_request(rl
, req
);
1439 freed_request(rl
, sync
, rq_flags
);
1443 EXPORT_SYMBOL_GPL(__blk_put_request
);
1445 void blk_put_request(struct request
*req
)
1447 struct request_queue
*q
= req
->q
;
1450 blk_mq_free_request(req
);
1452 unsigned long flags
;
1454 spin_lock_irqsave(q
->queue_lock
, flags
);
1455 __blk_put_request(q
, req
);
1456 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1459 EXPORT_SYMBOL(blk_put_request
);
1461 bool bio_attempt_back_merge(struct request_queue
*q
, struct request
*req
,
1464 const int ff
= bio
->bi_opf
& REQ_FAILFAST_MASK
;
1466 if (!ll_back_merge_fn(q
, req
, bio
))
1469 trace_block_bio_backmerge(q
, req
, bio
);
1471 if ((req
->cmd_flags
& REQ_FAILFAST_MASK
) != ff
)
1472 blk_rq_set_mixed_merge(req
);
1474 req
->biotail
->bi_next
= bio
;
1476 req
->__data_len
+= bio
->bi_iter
.bi_size
;
1477 req
->ioprio
= ioprio_best(req
->ioprio
, bio_prio(bio
));
1479 blk_account_io_start(req
, false);
1483 bool bio_attempt_front_merge(struct request_queue
*q
, struct request
*req
,
1486 const int ff
= bio
->bi_opf
& REQ_FAILFAST_MASK
;
1488 if (!ll_front_merge_fn(q
, req
, bio
))
1491 trace_block_bio_frontmerge(q
, req
, bio
);
1493 if ((req
->cmd_flags
& REQ_FAILFAST_MASK
) != ff
)
1494 blk_rq_set_mixed_merge(req
);
1496 bio
->bi_next
= req
->bio
;
1499 req
->__sector
= bio
->bi_iter
.bi_sector
;
1500 req
->__data_len
+= bio
->bi_iter
.bi_size
;
1501 req
->ioprio
= ioprio_best(req
->ioprio
, bio_prio(bio
));
1503 blk_account_io_start(req
, false);
1507 bool bio_attempt_discard_merge(struct request_queue
*q
, struct request
*req
,
1510 unsigned short segments
= blk_rq_nr_discard_segments(req
);
1512 if (segments
>= queue_max_discard_segments(q
))
1514 if (blk_rq_sectors(req
) + bio_sectors(bio
) >
1515 blk_rq_get_max_sectors(req
, blk_rq_pos(req
)))
1518 req
->biotail
->bi_next
= bio
;
1520 req
->__data_len
+= bio
->bi_iter
.bi_size
;
1521 req
->ioprio
= ioprio_best(req
->ioprio
, bio_prio(bio
));
1522 req
->nr_phys_segments
= segments
+ 1;
1524 blk_account_io_start(req
, false);
1527 req_set_nomerge(q
, req
);
1532 * blk_attempt_plug_merge - try to merge with %current's plugged list
1533 * @q: request_queue new bio is being queued at
1534 * @bio: new bio being queued
1535 * @request_count: out parameter for number of traversed plugged requests
1536 * @same_queue_rq: pointer to &struct request that gets filled in when
1537 * another request associated with @q is found on the plug list
1538 * (optional, may be %NULL)
1540 * Determine whether @bio being queued on @q can be merged with a request
1541 * on %current's plugged list. Returns %true if merge was successful,
1544 * Plugging coalesces IOs from the same issuer for the same purpose without
1545 * going through @q->queue_lock. As such it's more of an issuing mechanism
1546 * than scheduling, and the request, while may have elvpriv data, is not
1547 * added on the elevator at this point. In addition, we don't have
1548 * reliable access to the elevator outside queue lock. Only check basic
1549 * merging parameters without querying the elevator.
1551 * Caller must ensure !blk_queue_nomerges(q) beforehand.
1553 bool blk_attempt_plug_merge(struct request_queue
*q
, struct bio
*bio
,
1554 unsigned int *request_count
,
1555 struct request
**same_queue_rq
)
1557 struct blk_plug
*plug
;
1559 struct list_head
*plug_list
;
1561 plug
= current
->plug
;
1567 plug_list
= &plug
->mq_list
;
1569 plug_list
= &plug
->list
;
1571 list_for_each_entry_reverse(rq
, plug_list
, queuelist
) {
1572 bool merged
= false;
1577 * Only blk-mq multiple hardware queues case checks the
1578 * rq in the same queue, there should be only one such
1582 *same_queue_rq
= rq
;
1585 if (rq
->q
!= q
|| !blk_rq_merge_ok(rq
, bio
))
1588 switch (blk_try_merge(rq
, bio
)) {
1589 case ELEVATOR_BACK_MERGE
:
1590 merged
= bio_attempt_back_merge(q
, rq
, bio
);
1592 case ELEVATOR_FRONT_MERGE
:
1593 merged
= bio_attempt_front_merge(q
, rq
, bio
);
1595 case ELEVATOR_DISCARD_MERGE
:
1596 merged
= bio_attempt_discard_merge(q
, rq
, bio
);
1609 unsigned int blk_plug_queued_count(struct request_queue
*q
)
1611 struct blk_plug
*plug
;
1613 struct list_head
*plug_list
;
1614 unsigned int ret
= 0;
1616 plug
= current
->plug
;
1621 plug_list
= &plug
->mq_list
;
1623 plug_list
= &plug
->list
;
1625 list_for_each_entry(rq
, plug_list
, queuelist
) {
1633 void blk_init_request_from_bio(struct request
*req
, struct bio
*bio
)
1635 struct io_context
*ioc
= rq_ioc(bio
);
1637 if (bio
->bi_opf
& REQ_RAHEAD
)
1638 req
->cmd_flags
|= REQ_FAILFAST_MASK
;
1640 req
->__sector
= bio
->bi_iter
.bi_sector
;
1641 if (ioprio_valid(bio_prio(bio
)))
1642 req
->ioprio
= bio_prio(bio
);
1644 req
->ioprio
= ioc
->ioprio
;
1646 req
->ioprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE
, 0);
1647 blk_rq_bio_prep(req
->q
, req
, bio
);
1649 EXPORT_SYMBOL_GPL(blk_init_request_from_bio
);
1651 static blk_qc_t
blk_queue_bio(struct request_queue
*q
, struct bio
*bio
)
1653 struct blk_plug
*plug
;
1654 int where
= ELEVATOR_INSERT_SORT
;
1655 struct request
*req
, *free
;
1656 unsigned int request_count
= 0;
1657 unsigned int wb_acct
;
1660 * low level driver can indicate that it wants pages above a
1661 * certain limit bounced to low memory (ie for highmem, or even
1662 * ISA dma in theory)
1664 blk_queue_bounce(q
, &bio
);
1666 blk_queue_split(q
, &bio
, q
->bio_split
);
1668 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1669 bio
->bi_error
= -EIO
;
1671 return BLK_QC_T_NONE
;
1674 if (op_is_flush(bio
->bi_opf
)) {
1675 spin_lock_irq(q
->queue_lock
);
1676 where
= ELEVATOR_INSERT_FLUSH
;
1681 * Check if we can merge with the plugged list before grabbing
1684 if (!blk_queue_nomerges(q
)) {
1685 if (blk_attempt_plug_merge(q
, bio
, &request_count
, NULL
))
1686 return BLK_QC_T_NONE
;
1688 request_count
= blk_plug_queued_count(q
);
1690 spin_lock_irq(q
->queue_lock
);
1692 switch (elv_merge(q
, &req
, bio
)) {
1693 case ELEVATOR_BACK_MERGE
:
1694 if (!bio_attempt_back_merge(q
, req
, bio
))
1696 elv_bio_merged(q
, req
, bio
);
1697 free
= attempt_back_merge(q
, req
);
1699 __blk_put_request(q
, free
);
1701 elv_merged_request(q
, req
, ELEVATOR_BACK_MERGE
);
1703 case ELEVATOR_FRONT_MERGE
:
1704 if (!bio_attempt_front_merge(q
, req
, bio
))
1706 elv_bio_merged(q
, req
, bio
);
1707 free
= attempt_front_merge(q
, req
);
1709 __blk_put_request(q
, free
);
1711 elv_merged_request(q
, req
, ELEVATOR_FRONT_MERGE
);
1718 wb_acct
= wbt_wait(q
->rq_wb
, bio
, q
->queue_lock
);
1721 * Grab a free request. This is might sleep but can not fail.
1722 * Returns with the queue unlocked.
1724 req
= get_request(q
, bio
->bi_opf
, bio
, GFP_NOIO
);
1726 __wbt_done(q
->rq_wb
, wb_acct
);
1727 bio
->bi_error
= PTR_ERR(req
);
1732 wbt_track(&req
->issue_stat
, wb_acct
);
1735 * After dropping the lock and possibly sleeping here, our request
1736 * may now be mergeable after it had proven unmergeable (above).
1737 * We don't worry about that case for efficiency. It won't happen
1738 * often, and the elevators are able to handle it.
1740 blk_init_request_from_bio(req
, bio
);
1742 if (test_bit(QUEUE_FLAG_SAME_COMP
, &q
->queue_flags
))
1743 req
->cpu
= raw_smp_processor_id();
1745 plug
= current
->plug
;
1748 * If this is the first request added after a plug, fire
1751 * @request_count may become stale because of schedule
1752 * out, so check plug list again.
1754 if (!request_count
|| list_empty(&plug
->list
))
1755 trace_block_plug(q
);
1757 struct request
*last
= list_entry_rq(plug
->list
.prev
);
1758 if (request_count
>= BLK_MAX_REQUEST_COUNT
||
1759 blk_rq_bytes(last
) >= BLK_PLUG_FLUSH_SIZE
) {
1760 blk_flush_plug_list(plug
, false);
1761 trace_block_plug(q
);
1764 list_add_tail(&req
->queuelist
, &plug
->list
);
1765 blk_account_io_start(req
, true);
1767 spin_lock_irq(q
->queue_lock
);
1768 add_acct_request(q
, req
, where
);
1771 spin_unlock_irq(q
->queue_lock
);
1774 return BLK_QC_T_NONE
;
1778 * If bio->bi_dev is a partition, remap the location
1780 static inline void blk_partition_remap(struct bio
*bio
)
1782 struct block_device
*bdev
= bio
->bi_bdev
;
1785 * Zone reset does not include bi_size so bio_sectors() is always 0.
1786 * Include a test for the reset op code and perform the remap if needed.
1788 if (bdev
!= bdev
->bd_contains
&&
1789 (bio_sectors(bio
) || bio_op(bio
) == REQ_OP_ZONE_RESET
)) {
1790 struct hd_struct
*p
= bdev
->bd_part
;
1792 bio
->bi_iter
.bi_sector
+= p
->start_sect
;
1793 bio
->bi_bdev
= bdev
->bd_contains
;
1795 trace_block_bio_remap(bdev_get_queue(bio
->bi_bdev
), bio
,
1797 bio
->bi_iter
.bi_sector
- p
->start_sect
);
1801 static void handle_bad_sector(struct bio
*bio
)
1803 char b
[BDEVNAME_SIZE
];
1805 printk(KERN_INFO
"attempt to access beyond end of device\n");
1806 printk(KERN_INFO
"%s: rw=%d, want=%Lu, limit=%Lu\n",
1807 bdevname(bio
->bi_bdev
, b
),
1809 (unsigned long long)bio_end_sector(bio
),
1810 (long long)(i_size_read(bio
->bi_bdev
->bd_inode
) >> 9));
1813 #ifdef CONFIG_FAIL_MAKE_REQUEST
1815 static DECLARE_FAULT_ATTR(fail_make_request
);
1817 static int __init
setup_fail_make_request(char *str
)
1819 return setup_fault_attr(&fail_make_request
, str
);
1821 __setup("fail_make_request=", setup_fail_make_request
);
1823 static bool should_fail_request(struct hd_struct
*part
, unsigned int bytes
)
1825 return part
->make_it_fail
&& should_fail(&fail_make_request
, bytes
);
1828 static int __init
fail_make_request_debugfs(void)
1830 struct dentry
*dir
= fault_create_debugfs_attr("fail_make_request",
1831 NULL
, &fail_make_request
);
1833 return PTR_ERR_OR_ZERO(dir
);
1836 late_initcall(fail_make_request_debugfs
);
1838 #else /* CONFIG_FAIL_MAKE_REQUEST */
1840 static inline bool should_fail_request(struct hd_struct
*part
,
1846 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1849 * Check whether this bio extends beyond the end of the device.
1851 static inline int bio_check_eod(struct bio
*bio
, unsigned int nr_sectors
)
1858 /* Test device or partition size, when known. */
1859 maxsector
= i_size_read(bio
->bi_bdev
->bd_inode
) >> 9;
1861 sector_t sector
= bio
->bi_iter
.bi_sector
;
1863 if (maxsector
< nr_sectors
|| maxsector
- nr_sectors
< sector
) {
1865 * This may well happen - the kernel calls bread()
1866 * without checking the size of the device, e.g., when
1867 * mounting a device.
1869 handle_bad_sector(bio
);
1877 static noinline_for_stack
bool
1878 generic_make_request_checks(struct bio
*bio
)
1880 struct request_queue
*q
;
1881 int nr_sectors
= bio_sectors(bio
);
1883 char b
[BDEVNAME_SIZE
];
1884 struct hd_struct
*part
;
1888 if (bio_check_eod(bio
, nr_sectors
))
1891 q
= bdev_get_queue(bio
->bi_bdev
);
1894 "generic_make_request: Trying to access "
1895 "nonexistent block-device %s (%Lu)\n",
1896 bdevname(bio
->bi_bdev
, b
),
1897 (long long) bio
->bi_iter
.bi_sector
);
1901 part
= bio
->bi_bdev
->bd_part
;
1902 if (should_fail_request(part
, bio
->bi_iter
.bi_size
) ||
1903 should_fail_request(&part_to_disk(part
)->part0
,
1904 bio
->bi_iter
.bi_size
))
1908 * If this device has partitions, remap block n
1909 * of partition p to block n+start(p) of the disk.
1911 blk_partition_remap(bio
);
1913 if (bio_check_eod(bio
, nr_sectors
))
1917 * Filter flush bio's early so that make_request based
1918 * drivers without flush support don't have to worry
1921 if (op_is_flush(bio
->bi_opf
) &&
1922 !test_bit(QUEUE_FLAG_WC
, &q
->queue_flags
)) {
1923 bio
->bi_opf
&= ~(REQ_PREFLUSH
| REQ_FUA
);
1930 switch (bio_op(bio
)) {
1931 case REQ_OP_DISCARD
:
1932 if (!blk_queue_discard(q
))
1935 case REQ_OP_SECURE_ERASE
:
1936 if (!blk_queue_secure_erase(q
))
1939 case REQ_OP_WRITE_SAME
:
1940 if (!bdev_write_same(bio
->bi_bdev
))
1943 case REQ_OP_ZONE_REPORT
:
1944 case REQ_OP_ZONE_RESET
:
1945 if (!bdev_is_zoned(bio
->bi_bdev
))
1948 case REQ_OP_WRITE_ZEROES
:
1949 if (!bdev_write_zeroes_sectors(bio
->bi_bdev
))
1957 * Various block parts want %current->io_context and lazy ioc
1958 * allocation ends up trading a lot of pain for a small amount of
1959 * memory. Just allocate it upfront. This may fail and block
1960 * layer knows how to live with it.
1962 create_io_context(GFP_ATOMIC
, q
->node
);
1964 if (!blkcg_bio_issue_check(q
, bio
))
1967 if (!bio_flagged(bio
, BIO_TRACE_COMPLETION
)) {
1968 trace_block_bio_queue(q
, bio
);
1969 /* Now that enqueuing has been traced, we need to trace
1970 * completion as well.
1972 bio_set_flag(bio
, BIO_TRACE_COMPLETION
);
1979 bio
->bi_error
= err
;
1985 * generic_make_request - hand a buffer to its device driver for I/O
1986 * @bio: The bio describing the location in memory and on the device.
1988 * generic_make_request() is used to make I/O requests of block
1989 * devices. It is passed a &struct bio, which describes the I/O that needs
1992 * generic_make_request() does not return any status. The
1993 * success/failure status of the request, along with notification of
1994 * completion, is delivered asynchronously through the bio->bi_end_io
1995 * function described (one day) else where.
1997 * The caller of generic_make_request must make sure that bi_io_vec
1998 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1999 * set to describe the device address, and the
2000 * bi_end_io and optionally bi_private are set to describe how
2001 * completion notification should be signaled.
2003 * generic_make_request and the drivers it calls may use bi_next if this
2004 * bio happens to be merged with someone else, and may resubmit the bio to
2005 * a lower device by calling into generic_make_request recursively, which
2006 * means the bio should NOT be touched after the call to ->make_request_fn.
2008 blk_qc_t
generic_make_request(struct bio
*bio
)
2011 * bio_list_on_stack[0] contains bios submitted by the current
2013 * bio_list_on_stack[1] contains bios that were submitted before
2014 * the current make_request_fn, but that haven't been processed
2017 struct bio_list bio_list_on_stack
[2];
2018 blk_qc_t ret
= BLK_QC_T_NONE
;
2020 if (!generic_make_request_checks(bio
))
2024 * We only want one ->make_request_fn to be active at a time, else
2025 * stack usage with stacked devices could be a problem. So use
2026 * current->bio_list to keep a list of requests submited by a
2027 * make_request_fn function. current->bio_list is also used as a
2028 * flag to say if generic_make_request is currently active in this
2029 * task or not. If it is NULL, then no make_request is active. If
2030 * it is non-NULL, then a make_request is active, and new requests
2031 * should be added at the tail
2033 if (current
->bio_list
) {
2034 bio_list_add(¤t
->bio_list
[0], bio
);
2038 /* following loop may be a bit non-obvious, and so deserves some
2040 * Before entering the loop, bio->bi_next is NULL (as all callers
2041 * ensure that) so we have a list with a single bio.
2042 * We pretend that we have just taken it off a longer list, so
2043 * we assign bio_list to a pointer to the bio_list_on_stack,
2044 * thus initialising the bio_list of new bios to be
2045 * added. ->make_request() may indeed add some more bios
2046 * through a recursive call to generic_make_request. If it
2047 * did, we find a non-NULL value in bio_list and re-enter the loop
2048 * from the top. In this case we really did just take the bio
2049 * of the top of the list (no pretending) and so remove it from
2050 * bio_list, and call into ->make_request() again.
2052 BUG_ON(bio
->bi_next
);
2053 bio_list_init(&bio_list_on_stack
[0]);
2054 current
->bio_list
= bio_list_on_stack
;
2056 struct request_queue
*q
= bdev_get_queue(bio
->bi_bdev
);
2058 if (likely(blk_queue_enter(q
, false) == 0)) {
2059 struct bio_list lower
, same
;
2061 /* Create a fresh bio_list for all subordinate requests */
2062 bio_list_on_stack
[1] = bio_list_on_stack
[0];
2063 bio_list_init(&bio_list_on_stack
[0]);
2064 ret
= q
->make_request_fn(q
, bio
);
2068 /* sort new bios into those for a lower level
2069 * and those for the same level
2071 bio_list_init(&lower
);
2072 bio_list_init(&same
);
2073 while ((bio
= bio_list_pop(&bio_list_on_stack
[0])) != NULL
)
2074 if (q
== bdev_get_queue(bio
->bi_bdev
))
2075 bio_list_add(&same
, bio
);
2077 bio_list_add(&lower
, bio
);
2078 /* now assemble so we handle the lowest level first */
2079 bio_list_merge(&bio_list_on_stack
[0], &lower
);
2080 bio_list_merge(&bio_list_on_stack
[0], &same
);
2081 bio_list_merge(&bio_list_on_stack
[0], &bio_list_on_stack
[1]);
2085 bio
= bio_list_pop(&bio_list_on_stack
[0]);
2087 current
->bio_list
= NULL
; /* deactivate */
2092 EXPORT_SYMBOL(generic_make_request
);
2095 * submit_bio - submit a bio to the block device layer for I/O
2096 * @bio: The &struct bio which describes the I/O
2098 * submit_bio() is very similar in purpose to generic_make_request(), and
2099 * uses that function to do most of the work. Both are fairly rough
2100 * interfaces; @bio must be presetup and ready for I/O.
2103 blk_qc_t
submit_bio(struct bio
*bio
)
2106 * If it's a regular read/write or a barrier with data attached,
2107 * go through the normal accounting stuff before submission.
2109 if (bio_has_data(bio
)) {
2112 if (unlikely(bio_op(bio
) == REQ_OP_WRITE_SAME
))
2113 count
= bdev_logical_block_size(bio
->bi_bdev
) >> 9;
2115 count
= bio_sectors(bio
);
2117 if (op_is_write(bio_op(bio
))) {
2118 count_vm_events(PGPGOUT
, count
);
2120 task_io_account_read(bio
->bi_iter
.bi_size
);
2121 count_vm_events(PGPGIN
, count
);
2124 if (unlikely(block_dump
)) {
2125 char b
[BDEVNAME_SIZE
];
2126 printk(KERN_DEBUG
"%s(%d): %s block %Lu on %s (%u sectors)\n",
2127 current
->comm
, task_pid_nr(current
),
2128 op_is_write(bio_op(bio
)) ? "WRITE" : "READ",
2129 (unsigned long long)bio
->bi_iter
.bi_sector
,
2130 bdevname(bio
->bi_bdev
, b
),
2135 return generic_make_request(bio
);
2137 EXPORT_SYMBOL(submit_bio
);
2140 * blk_cloned_rq_check_limits - Helper function to check a cloned request
2141 * for new the queue limits
2143 * @rq: the request being checked
2146 * @rq may have been made based on weaker limitations of upper-level queues
2147 * in request stacking drivers, and it may violate the limitation of @q.
2148 * Since the block layer and the underlying device driver trust @rq
2149 * after it is inserted to @q, it should be checked against @q before
2150 * the insertion using this generic function.
2152 * Request stacking drivers like request-based dm may change the queue
2153 * limits when retrying requests on other queues. Those requests need
2154 * to be checked against the new queue limits again during dispatch.
2156 static int blk_cloned_rq_check_limits(struct request_queue
*q
,
2159 if (blk_rq_sectors(rq
) > blk_queue_get_max_sectors(q
, req_op(rq
))) {
2160 printk(KERN_ERR
"%s: over max size limit.\n", __func__
);
2165 * queue's settings related to segment counting like q->bounce_pfn
2166 * may differ from that of other stacking queues.
2167 * Recalculate it to check the request correctly on this queue's
2170 blk_recalc_rq_segments(rq
);
2171 if (rq
->nr_phys_segments
> queue_max_segments(q
)) {
2172 printk(KERN_ERR
"%s: over max segments limit.\n", __func__
);
2180 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
2181 * @q: the queue to submit the request
2182 * @rq: the request being queued
2184 int blk_insert_cloned_request(struct request_queue
*q
, struct request
*rq
)
2186 unsigned long flags
;
2187 int where
= ELEVATOR_INSERT_BACK
;
2189 if (blk_cloned_rq_check_limits(q
, rq
))
2193 should_fail_request(&rq
->rq_disk
->part0
, blk_rq_bytes(rq
)))
2197 if (blk_queue_io_stat(q
))
2198 blk_account_io_start(rq
, true);
2199 blk_mq_sched_insert_request(rq
, false, true, false, false);
2203 spin_lock_irqsave(q
->queue_lock
, flags
);
2204 if (unlikely(blk_queue_dying(q
))) {
2205 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2210 * Submitting request must be dequeued before calling this function
2211 * because it will be linked to another request_queue
2213 BUG_ON(blk_queued_rq(rq
));
2215 if (op_is_flush(rq
->cmd_flags
))
2216 where
= ELEVATOR_INSERT_FLUSH
;
2218 add_acct_request(q
, rq
, where
);
2219 if (where
== ELEVATOR_INSERT_FLUSH
)
2221 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2225 EXPORT_SYMBOL_GPL(blk_insert_cloned_request
);
2228 * blk_rq_err_bytes - determine number of bytes till the next failure boundary
2229 * @rq: request to examine
2232 * A request could be merge of IOs which require different failure
2233 * handling. This function determines the number of bytes which
2234 * can be failed from the beginning of the request without
2235 * crossing into area which need to be retried further.
2238 * The number of bytes to fail.
2241 * queue_lock must be held.
2243 unsigned int blk_rq_err_bytes(const struct request
*rq
)
2245 unsigned int ff
= rq
->cmd_flags
& REQ_FAILFAST_MASK
;
2246 unsigned int bytes
= 0;
2249 if (!(rq
->rq_flags
& RQF_MIXED_MERGE
))
2250 return blk_rq_bytes(rq
);
2253 * Currently the only 'mixing' which can happen is between
2254 * different fastfail types. We can safely fail portions
2255 * which have all the failfast bits that the first one has -
2256 * the ones which are at least as eager to fail as the first
2259 for (bio
= rq
->bio
; bio
; bio
= bio
->bi_next
) {
2260 if ((bio
->bi_opf
& ff
) != ff
)
2262 bytes
+= bio
->bi_iter
.bi_size
;
2265 /* this could lead to infinite loop */
2266 BUG_ON(blk_rq_bytes(rq
) && !bytes
);
2269 EXPORT_SYMBOL_GPL(blk_rq_err_bytes
);
2271 void blk_account_io_completion(struct request
*req
, unsigned int bytes
)
2273 if (blk_do_io_stat(req
)) {
2274 const int rw
= rq_data_dir(req
);
2275 struct hd_struct
*part
;
2278 cpu
= part_stat_lock();
2280 part_stat_add(cpu
, part
, sectors
[rw
], bytes
>> 9);
2285 void blk_account_io_done(struct request
*req
)
2288 * Account IO completion. flush_rq isn't accounted as a
2289 * normal IO on queueing nor completion. Accounting the
2290 * containing request is enough.
2292 if (blk_do_io_stat(req
) && !(req
->rq_flags
& RQF_FLUSH_SEQ
)) {
2293 unsigned long duration
= jiffies
- req
->start_time
;
2294 const int rw
= rq_data_dir(req
);
2295 struct hd_struct
*part
;
2298 cpu
= part_stat_lock();
2301 part_stat_inc(cpu
, part
, ios
[rw
]);
2302 part_stat_add(cpu
, part
, ticks
[rw
], duration
);
2303 part_round_stats(cpu
, part
);
2304 part_dec_in_flight(part
, rw
);
2306 hd_struct_put(part
);
2313 * Don't process normal requests when queue is suspended
2314 * or in the process of suspending/resuming
2316 static struct request
*blk_pm_peek_request(struct request_queue
*q
,
2319 if (q
->dev
&& (q
->rpm_status
== RPM_SUSPENDED
||
2320 (q
->rpm_status
!= RPM_ACTIVE
&& !(rq
->rq_flags
& RQF_PM
))))
2326 static inline struct request
*blk_pm_peek_request(struct request_queue
*q
,
2333 void blk_account_io_start(struct request
*rq
, bool new_io
)
2335 struct hd_struct
*part
;
2336 int rw
= rq_data_dir(rq
);
2339 if (!blk_do_io_stat(rq
))
2342 cpu
= part_stat_lock();
2346 part_stat_inc(cpu
, part
, merges
[rw
]);
2348 part
= disk_map_sector_rcu(rq
->rq_disk
, blk_rq_pos(rq
));
2349 if (!hd_struct_try_get(part
)) {
2351 * The partition is already being removed,
2352 * the request will be accounted on the disk only
2354 * We take a reference on disk->part0 although that
2355 * partition will never be deleted, so we can treat
2356 * it as any other partition.
2358 part
= &rq
->rq_disk
->part0
;
2359 hd_struct_get(part
);
2361 part_round_stats(cpu
, part
);
2362 part_inc_in_flight(part
, rw
);
2370 * blk_peek_request - peek at the top of a request queue
2371 * @q: request queue to peek at
2374 * Return the request at the top of @q. The returned request
2375 * should be started using blk_start_request() before LLD starts
2379 * Pointer to the request at the top of @q if available. Null
2383 * queue_lock must be held.
2385 struct request
*blk_peek_request(struct request_queue
*q
)
2390 while ((rq
= __elv_next_request(q
)) != NULL
) {
2392 rq
= blk_pm_peek_request(q
, rq
);
2396 if (!(rq
->rq_flags
& RQF_STARTED
)) {
2398 * This is the first time the device driver
2399 * sees this request (possibly after
2400 * requeueing). Notify IO scheduler.
2402 if (rq
->rq_flags
& RQF_SORTED
)
2403 elv_activate_rq(q
, rq
);
2406 * just mark as started even if we don't start
2407 * it, a request that has been delayed should
2408 * not be passed by new incoming requests
2410 rq
->rq_flags
|= RQF_STARTED
;
2411 trace_block_rq_issue(q
, rq
);
2414 if (!q
->boundary_rq
|| q
->boundary_rq
== rq
) {
2415 q
->end_sector
= rq_end_sector(rq
);
2416 q
->boundary_rq
= NULL
;
2419 if (rq
->rq_flags
& RQF_DONTPREP
)
2422 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
2424 * make sure space for the drain appears we
2425 * know we can do this because max_hw_segments
2426 * has been adjusted to be one fewer than the
2429 rq
->nr_phys_segments
++;
2435 ret
= q
->prep_rq_fn(q
, rq
);
2436 if (ret
== BLKPREP_OK
) {
2438 } else if (ret
== BLKPREP_DEFER
) {
2440 * the request may have been (partially) prepped.
2441 * we need to keep this request in the front to
2442 * avoid resource deadlock. RQF_STARTED will
2443 * prevent other fs requests from passing this one.
2445 if (q
->dma_drain_size
&& blk_rq_bytes(rq
) &&
2446 !(rq
->rq_flags
& RQF_DONTPREP
)) {
2448 * remove the space for the drain we added
2449 * so that we don't add it again
2451 --rq
->nr_phys_segments
;
2456 } else if (ret
== BLKPREP_KILL
|| ret
== BLKPREP_INVALID
) {
2457 int err
= (ret
== BLKPREP_INVALID
) ? -EREMOTEIO
: -EIO
;
2459 rq
->rq_flags
|= RQF_QUIET
;
2461 * Mark this request as started so we don't trigger
2462 * any debug logic in the end I/O path.
2464 blk_start_request(rq
);
2465 __blk_end_request_all(rq
, err
);
2467 printk(KERN_ERR
"%s: bad return=%d\n", __func__
, ret
);
2474 EXPORT_SYMBOL(blk_peek_request
);
2476 void blk_dequeue_request(struct request
*rq
)
2478 struct request_queue
*q
= rq
->q
;
2480 BUG_ON(list_empty(&rq
->queuelist
));
2481 BUG_ON(ELV_ON_HASH(rq
));
2483 list_del_init(&rq
->queuelist
);
2486 * the time frame between a request being removed from the lists
2487 * and to it is freed is accounted as io that is in progress at
2490 if (blk_account_rq(rq
)) {
2491 q
->in_flight
[rq_is_sync(rq
)]++;
2492 set_io_start_time_ns(rq
);
2497 * blk_start_request - start request processing on the driver
2498 * @req: request to dequeue
2501 * Dequeue @req and start timeout timer on it. This hands off the
2502 * request to the driver.
2504 * Block internal functions which don't want to start timer should
2505 * call blk_dequeue_request().
2508 * queue_lock must be held.
2510 void blk_start_request(struct request
*req
)
2512 blk_dequeue_request(req
);
2514 if (test_bit(QUEUE_FLAG_STATS
, &req
->q
->queue_flags
)) {
2515 blk_stat_set_issue(&req
->issue_stat
, blk_rq_sectors(req
));
2516 req
->rq_flags
|= RQF_STATS
;
2517 wbt_issue(req
->q
->rq_wb
, &req
->issue_stat
);
2520 BUG_ON(test_bit(REQ_ATOM_COMPLETE
, &req
->atomic_flags
));
2523 EXPORT_SYMBOL(blk_start_request
);
2526 * blk_fetch_request - fetch a request from a request queue
2527 * @q: request queue to fetch a request from
2530 * Return the request at the top of @q. The request is started on
2531 * return and LLD can start processing it immediately.
2534 * Pointer to the request at the top of @q if available. Null
2538 * queue_lock must be held.
2540 struct request
*blk_fetch_request(struct request_queue
*q
)
2544 rq
= blk_peek_request(q
);
2546 blk_start_request(rq
);
2549 EXPORT_SYMBOL(blk_fetch_request
);
2552 * blk_update_request - Special helper function for request stacking drivers
2553 * @req: the request being processed
2554 * @error: %0 for success, < %0 for error
2555 * @nr_bytes: number of bytes to complete @req
2558 * Ends I/O on a number of bytes attached to @req, but doesn't complete
2559 * the request structure even if @req doesn't have leftover.
2560 * If @req has leftover, sets it up for the next range of segments.
2562 * This special helper function is only for request stacking drivers
2563 * (e.g. request-based dm) so that they can handle partial completion.
2564 * Actual device drivers should use blk_end_request instead.
2566 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
2567 * %false return from this function.
2570 * %false - this request doesn't have any more data
2571 * %true - this request has more data
2573 bool blk_update_request(struct request
*req
, int error
, unsigned int nr_bytes
)
2577 trace_block_rq_complete(req
, error
, nr_bytes
);
2582 if (error
&& !blk_rq_is_passthrough(req
) &&
2583 !(req
->rq_flags
& RQF_QUIET
)) {
2588 error_type
= "recoverable transport";
2591 error_type
= "critical target";
2594 error_type
= "critical nexus";
2597 error_type
= "timeout";
2600 error_type
= "critical space allocation";
2603 error_type
= "critical medium";
2610 printk_ratelimited(KERN_ERR
"%s: %s error, dev %s, sector %llu\n",
2611 __func__
, error_type
, req
->rq_disk
?
2612 req
->rq_disk
->disk_name
: "?",
2613 (unsigned long long)blk_rq_pos(req
));
2617 blk_account_io_completion(req
, nr_bytes
);
2621 struct bio
*bio
= req
->bio
;
2622 unsigned bio_bytes
= min(bio
->bi_iter
.bi_size
, nr_bytes
);
2624 if (bio_bytes
== bio
->bi_iter
.bi_size
)
2625 req
->bio
= bio
->bi_next
;
2627 /* Completion has already been traced */
2628 bio_clear_flag(bio
, BIO_TRACE_COMPLETION
);
2629 req_bio_endio(req
, bio
, bio_bytes
, error
);
2631 total_bytes
+= bio_bytes
;
2632 nr_bytes
-= bio_bytes
;
2643 * Reset counters so that the request stacking driver
2644 * can find how many bytes remain in the request
2647 req
->__data_len
= 0;
2651 WARN_ON_ONCE(req
->rq_flags
& RQF_SPECIAL_PAYLOAD
);
2653 req
->__data_len
-= total_bytes
;
2655 /* update sector only for requests with clear definition of sector */
2656 if (!blk_rq_is_passthrough(req
))
2657 req
->__sector
+= total_bytes
>> 9;
2659 /* mixed attributes always follow the first bio */
2660 if (req
->rq_flags
& RQF_MIXED_MERGE
) {
2661 req
->cmd_flags
&= ~REQ_FAILFAST_MASK
;
2662 req
->cmd_flags
|= req
->bio
->bi_opf
& REQ_FAILFAST_MASK
;
2666 * If total number of sectors is less than the first segment
2667 * size, something has gone terribly wrong.
2669 if (blk_rq_bytes(req
) < blk_rq_cur_bytes(req
)) {
2670 blk_dump_rq_flags(req
, "request botched");
2671 req
->__data_len
= blk_rq_cur_bytes(req
);
2674 /* recalculate the number of segments */
2675 blk_recalc_rq_segments(req
);
2679 EXPORT_SYMBOL_GPL(blk_update_request
);
2681 static bool blk_update_bidi_request(struct request
*rq
, int error
,
2682 unsigned int nr_bytes
,
2683 unsigned int bidi_bytes
)
2685 if (blk_update_request(rq
, error
, nr_bytes
))
2688 /* Bidi request must be completed as a whole */
2689 if (unlikely(blk_bidi_rq(rq
)) &&
2690 blk_update_request(rq
->next_rq
, error
, bidi_bytes
))
2693 if (blk_queue_add_random(rq
->q
))
2694 add_disk_randomness(rq
->rq_disk
);
2700 * blk_unprep_request - unprepare a request
2703 * This function makes a request ready for complete resubmission (or
2704 * completion). It happens only after all error handling is complete,
2705 * so represents the appropriate moment to deallocate any resources
2706 * that were allocated to the request in the prep_rq_fn. The queue
2707 * lock is held when calling this.
2709 void blk_unprep_request(struct request
*req
)
2711 struct request_queue
*q
= req
->q
;
2713 req
->rq_flags
&= ~RQF_DONTPREP
;
2714 if (q
->unprep_rq_fn
)
2715 q
->unprep_rq_fn(q
, req
);
2717 EXPORT_SYMBOL_GPL(blk_unprep_request
);
2720 * queue lock must be held
2722 void blk_finish_request(struct request
*req
, int error
)
2724 struct request_queue
*q
= req
->q
;
2726 if (req
->rq_flags
& RQF_STATS
)
2729 if (req
->rq_flags
& RQF_QUEUED
)
2730 blk_queue_end_tag(q
, req
);
2732 BUG_ON(blk_queued_rq(req
));
2734 if (unlikely(laptop_mode
) && !blk_rq_is_passthrough(req
))
2735 laptop_io_completion(req
->q
->backing_dev_info
);
2737 blk_delete_timer(req
);
2739 if (req
->rq_flags
& RQF_DONTPREP
)
2740 blk_unprep_request(req
);
2742 blk_account_io_done(req
);
2745 wbt_done(req
->q
->rq_wb
, &req
->issue_stat
);
2746 req
->end_io(req
, error
);
2748 if (blk_bidi_rq(req
))
2749 __blk_put_request(req
->next_rq
->q
, req
->next_rq
);
2751 __blk_put_request(q
, req
);
2754 EXPORT_SYMBOL(blk_finish_request
);
2757 * blk_end_bidi_request - Complete a bidi request
2758 * @rq: the request to complete
2759 * @error: %0 for success, < %0 for error
2760 * @nr_bytes: number of bytes to complete @rq
2761 * @bidi_bytes: number of bytes to complete @rq->next_rq
2764 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2765 * Drivers that supports bidi can safely call this member for any
2766 * type of request, bidi or uni. In the later case @bidi_bytes is
2770 * %false - we are done with this request
2771 * %true - still buffers pending for this request
2773 static bool blk_end_bidi_request(struct request
*rq
, int error
,
2774 unsigned int nr_bytes
, unsigned int bidi_bytes
)
2776 struct request_queue
*q
= rq
->q
;
2777 unsigned long flags
;
2779 if (blk_update_bidi_request(rq
, error
, nr_bytes
, bidi_bytes
))
2782 spin_lock_irqsave(q
->queue_lock
, flags
);
2783 blk_finish_request(rq
, error
);
2784 spin_unlock_irqrestore(q
->queue_lock
, flags
);
2790 * __blk_end_bidi_request - Complete a bidi request with queue lock held
2791 * @rq: the request to complete
2792 * @error: %0 for success, < %0 for error
2793 * @nr_bytes: number of bytes to complete @rq
2794 * @bidi_bytes: number of bytes to complete @rq->next_rq
2797 * Identical to blk_end_bidi_request() except that queue lock is
2798 * assumed to be locked on entry and remains so on return.
2801 * %false - we are done with this request
2802 * %true - still buffers pending for this request
2804 static bool __blk_end_bidi_request(struct request
*rq
, int error
,
2805 unsigned int nr_bytes
, unsigned int bidi_bytes
)
2807 if (blk_update_bidi_request(rq
, error
, nr_bytes
, bidi_bytes
))
2810 blk_finish_request(rq
, error
);
2816 * blk_end_request - Helper function for drivers to complete the request.
2817 * @rq: the request being processed
2818 * @error: %0 for success, < %0 for error
2819 * @nr_bytes: number of bytes to complete
2822 * Ends I/O on a number of bytes attached to @rq.
2823 * If @rq has leftover, sets it up for the next range of segments.
2826 * %false - we are done with this request
2827 * %true - still buffers pending for this request
2829 bool blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
2831 return blk_end_bidi_request(rq
, error
, nr_bytes
, 0);
2833 EXPORT_SYMBOL(blk_end_request
);
2836 * blk_end_request_all - Helper function for drives to finish the request.
2837 * @rq: the request to finish
2838 * @error: %0 for success, < %0 for error
2841 * Completely finish @rq.
2843 void blk_end_request_all(struct request
*rq
, int error
)
2846 unsigned int bidi_bytes
= 0;
2848 if (unlikely(blk_bidi_rq(rq
)))
2849 bidi_bytes
= blk_rq_bytes(rq
->next_rq
);
2851 pending
= blk_end_bidi_request(rq
, error
, blk_rq_bytes(rq
), bidi_bytes
);
2854 EXPORT_SYMBOL(blk_end_request_all
);
2857 * __blk_end_request - Helper function for drivers to complete the request.
2858 * @rq: the request being processed
2859 * @error: %0 for success, < %0 for error
2860 * @nr_bytes: number of bytes to complete
2863 * Must be called with queue lock held unlike blk_end_request().
2866 * %false - we are done with this request
2867 * %true - still buffers pending for this request
2869 bool __blk_end_request(struct request
*rq
, int error
, unsigned int nr_bytes
)
2871 return __blk_end_bidi_request(rq
, error
, nr_bytes
, 0);
2873 EXPORT_SYMBOL(__blk_end_request
);
2876 * __blk_end_request_all - Helper function for drives to finish the request.
2877 * @rq: the request to finish
2878 * @error: %0 for success, < %0 for error
2881 * Completely finish @rq. Must be called with queue lock held.
2883 void __blk_end_request_all(struct request
*rq
, int error
)
2886 unsigned int bidi_bytes
= 0;
2888 if (unlikely(blk_bidi_rq(rq
)))
2889 bidi_bytes
= blk_rq_bytes(rq
->next_rq
);
2891 pending
= __blk_end_bidi_request(rq
, error
, blk_rq_bytes(rq
), bidi_bytes
);
2894 EXPORT_SYMBOL(__blk_end_request_all
);
2897 * __blk_end_request_cur - Helper function to finish the current request chunk.
2898 * @rq: the request to finish the current chunk for
2899 * @error: %0 for success, < %0 for error
2902 * Complete the current consecutively mapped chunk from @rq. Must
2903 * be called with queue lock held.
2906 * %false - we are done with this request
2907 * %true - still buffers pending for this request
2909 bool __blk_end_request_cur(struct request
*rq
, int error
)
2911 return __blk_end_request(rq
, error
, blk_rq_cur_bytes(rq
));
2913 EXPORT_SYMBOL(__blk_end_request_cur
);
2915 void blk_rq_bio_prep(struct request_queue
*q
, struct request
*rq
,
2918 if (bio_has_data(bio
))
2919 rq
->nr_phys_segments
= bio_phys_segments(q
, bio
);
2921 rq
->__data_len
= bio
->bi_iter
.bi_size
;
2922 rq
->bio
= rq
->biotail
= bio
;
2925 rq
->rq_disk
= bio
->bi_bdev
->bd_disk
;
2928 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2930 * rq_flush_dcache_pages - Helper function to flush all pages in a request
2931 * @rq: the request to be flushed
2934 * Flush all pages in @rq.
2936 void rq_flush_dcache_pages(struct request
*rq
)
2938 struct req_iterator iter
;
2939 struct bio_vec bvec
;
2941 rq_for_each_segment(bvec
, rq
, iter
)
2942 flush_dcache_page(bvec
.bv_page
);
2944 EXPORT_SYMBOL_GPL(rq_flush_dcache_pages
);
2948 * blk_lld_busy - Check if underlying low-level drivers of a device are busy
2949 * @q : the queue of the device being checked
2952 * Check if underlying low-level drivers of a device are busy.
2953 * If the drivers want to export their busy state, they must set own
2954 * exporting function using blk_queue_lld_busy() first.
2956 * Basically, this function is used only by request stacking drivers
2957 * to stop dispatching requests to underlying devices when underlying
2958 * devices are busy. This behavior helps more I/O merging on the queue
2959 * of the request stacking driver and prevents I/O throughput regression
2960 * on burst I/O load.
2963 * 0 - Not busy (The request stacking driver should dispatch request)
2964 * 1 - Busy (The request stacking driver should stop dispatching request)
2966 int blk_lld_busy(struct request_queue
*q
)
2969 return q
->lld_busy_fn(q
);
2973 EXPORT_SYMBOL_GPL(blk_lld_busy
);
2976 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2977 * @rq: the clone request to be cleaned up
2980 * Free all bios in @rq for a cloned request.
2982 void blk_rq_unprep_clone(struct request
*rq
)
2986 while ((bio
= rq
->bio
) != NULL
) {
2987 rq
->bio
= bio
->bi_next
;
2992 EXPORT_SYMBOL_GPL(blk_rq_unprep_clone
);
2995 * Copy attributes of the original request to the clone request.
2996 * The actual data parts (e.g. ->cmd, ->sense) are not copied.
2998 static void __blk_rq_prep_clone(struct request
*dst
, struct request
*src
)
3000 dst
->cpu
= src
->cpu
;
3001 dst
->__sector
= blk_rq_pos(src
);
3002 dst
->__data_len
= blk_rq_bytes(src
);
3003 dst
->nr_phys_segments
= src
->nr_phys_segments
;
3004 dst
->ioprio
= src
->ioprio
;
3005 dst
->extra_len
= src
->extra_len
;
3009 * blk_rq_prep_clone - Helper function to setup clone request
3010 * @rq: the request to be setup
3011 * @rq_src: original request to be cloned
3012 * @bs: bio_set that bios for clone are allocated from
3013 * @gfp_mask: memory allocation mask for bio
3014 * @bio_ctr: setup function to be called for each clone bio.
3015 * Returns %0 for success, non %0 for failure.
3016 * @data: private data to be passed to @bio_ctr
3019 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
3020 * The actual data parts of @rq_src (e.g. ->cmd, ->sense)
3021 * are not copied, and copying such parts is the caller's responsibility.
3022 * Also, pages which the original bios are pointing to are not copied
3023 * and the cloned bios just point same pages.
3024 * So cloned bios must be completed before original bios, which means
3025 * the caller must complete @rq before @rq_src.
3027 int blk_rq_prep_clone(struct request
*rq
, struct request
*rq_src
,
3028 struct bio_set
*bs
, gfp_t gfp_mask
,
3029 int (*bio_ctr
)(struct bio
*, struct bio
*, void *),
3032 struct bio
*bio
, *bio_src
;
3037 __rq_for_each_bio(bio_src
, rq_src
) {
3038 bio
= bio_clone_fast(bio_src
, gfp_mask
, bs
);
3042 if (bio_ctr
&& bio_ctr(bio
, bio_src
, data
))
3046 rq
->biotail
->bi_next
= bio
;
3049 rq
->bio
= rq
->biotail
= bio
;
3052 __blk_rq_prep_clone(rq
, rq_src
);
3059 blk_rq_unprep_clone(rq
);
3063 EXPORT_SYMBOL_GPL(blk_rq_prep_clone
);
3065 int kblockd_schedule_work(struct work_struct
*work
)
3067 return queue_work(kblockd_workqueue
, work
);
3069 EXPORT_SYMBOL(kblockd_schedule_work
);
3071 int kblockd_schedule_work_on(int cpu
, struct work_struct
*work
)
3073 return queue_work_on(cpu
, kblockd_workqueue
, work
);
3075 EXPORT_SYMBOL(kblockd_schedule_work_on
);
3077 int kblockd_mod_delayed_work_on(int cpu
, struct delayed_work
*dwork
,
3078 unsigned long delay
)
3080 return mod_delayed_work_on(cpu
, kblockd_workqueue
, dwork
, delay
);
3082 EXPORT_SYMBOL(kblockd_mod_delayed_work_on
);
3084 int kblockd_schedule_delayed_work(struct delayed_work
*dwork
,
3085 unsigned long delay
)
3087 return queue_delayed_work(kblockd_workqueue
, dwork
, delay
);
3089 EXPORT_SYMBOL(kblockd_schedule_delayed_work
);
3091 int kblockd_schedule_delayed_work_on(int cpu
, struct delayed_work
*dwork
,
3092 unsigned long delay
)
3094 return queue_delayed_work_on(cpu
, kblockd_workqueue
, dwork
, delay
);
3096 EXPORT_SYMBOL(kblockd_schedule_delayed_work_on
);
3099 * blk_start_plug - initialize blk_plug and track it inside the task_struct
3100 * @plug: The &struct blk_plug that needs to be initialized
3103 * Tracking blk_plug inside the task_struct will help with auto-flushing the
3104 * pending I/O should the task end up blocking between blk_start_plug() and
3105 * blk_finish_plug(). This is important from a performance perspective, but
3106 * also ensures that we don't deadlock. For instance, if the task is blocking
3107 * for a memory allocation, memory reclaim could end up wanting to free a
3108 * page belonging to that request that is currently residing in our private
3109 * plug. By flushing the pending I/O when the process goes to sleep, we avoid
3110 * this kind of deadlock.
3112 void blk_start_plug(struct blk_plug
*plug
)
3114 struct task_struct
*tsk
= current
;
3117 * If this is a nested plug, don't actually assign it.
3122 INIT_LIST_HEAD(&plug
->list
);
3123 INIT_LIST_HEAD(&plug
->mq_list
);
3124 INIT_LIST_HEAD(&plug
->cb_list
);
3126 * Store ordering should not be needed here, since a potential
3127 * preempt will imply a full memory barrier
3131 EXPORT_SYMBOL(blk_start_plug
);
3133 static int plug_rq_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
3135 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
3136 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
3138 return !(rqa
->q
< rqb
->q
||
3139 (rqa
->q
== rqb
->q
&& blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
3143 * If 'from_schedule' is true, then postpone the dispatch of requests
3144 * until a safe kblockd context. We due this to avoid accidental big
3145 * additional stack usage in driver dispatch, in places where the originally
3146 * plugger did not intend it.
3148 static void queue_unplugged(struct request_queue
*q
, unsigned int depth
,
3150 __releases(q
->queue_lock
)
3152 trace_block_unplug(q
, depth
, !from_schedule
);
3155 blk_run_queue_async(q
);
3158 spin_unlock(q
->queue_lock
);
3161 static void flush_plug_callbacks(struct blk_plug
*plug
, bool from_schedule
)
3163 LIST_HEAD(callbacks
);
3165 while (!list_empty(&plug
->cb_list
)) {
3166 list_splice_init(&plug
->cb_list
, &callbacks
);
3168 while (!list_empty(&callbacks
)) {
3169 struct blk_plug_cb
*cb
= list_first_entry(&callbacks
,
3172 list_del(&cb
->list
);
3173 cb
->callback(cb
, from_schedule
);
3178 struct blk_plug_cb
*blk_check_plugged(blk_plug_cb_fn unplug
, void *data
,
3181 struct blk_plug
*plug
= current
->plug
;
3182 struct blk_plug_cb
*cb
;
3187 list_for_each_entry(cb
, &plug
->cb_list
, list
)
3188 if (cb
->callback
== unplug
&& cb
->data
== data
)
3191 /* Not currently on the callback list */
3192 BUG_ON(size
< sizeof(*cb
));
3193 cb
= kzalloc(size
, GFP_ATOMIC
);
3196 cb
->callback
= unplug
;
3197 list_add(&cb
->list
, &plug
->cb_list
);
3201 EXPORT_SYMBOL(blk_check_plugged
);
3203 void blk_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
3205 struct request_queue
*q
;
3206 unsigned long flags
;
3211 flush_plug_callbacks(plug
, from_schedule
);
3213 if (!list_empty(&plug
->mq_list
))
3214 blk_mq_flush_plug_list(plug
, from_schedule
);
3216 if (list_empty(&plug
->list
))
3219 list_splice_init(&plug
->list
, &list
);
3221 list_sort(NULL
, &list
, plug_rq_cmp
);
3227 * Save and disable interrupts here, to avoid doing it for every
3228 * queue lock we have to take.
3230 local_irq_save(flags
);
3231 while (!list_empty(&list
)) {
3232 rq
= list_entry_rq(list
.next
);
3233 list_del_init(&rq
->queuelist
);
3237 * This drops the queue lock
3240 queue_unplugged(q
, depth
, from_schedule
);
3243 spin_lock(q
->queue_lock
);
3247 * Short-circuit if @q is dead
3249 if (unlikely(blk_queue_dying(q
))) {
3250 __blk_end_request_all(rq
, -ENODEV
);
3255 * rq is already accounted, so use raw insert
3257 if (op_is_flush(rq
->cmd_flags
))
3258 __elv_add_request(q
, rq
, ELEVATOR_INSERT_FLUSH
);
3260 __elv_add_request(q
, rq
, ELEVATOR_INSERT_SORT_MERGE
);
3266 * This drops the queue lock
3269 queue_unplugged(q
, depth
, from_schedule
);
3271 local_irq_restore(flags
);
3274 void blk_finish_plug(struct blk_plug
*plug
)
3276 if (plug
!= current
->plug
)
3278 blk_flush_plug_list(plug
, false);
3280 current
->plug
= NULL
;
3282 EXPORT_SYMBOL(blk_finish_plug
);
3286 * blk_pm_runtime_init - Block layer runtime PM initialization routine
3287 * @q: the queue of the device
3288 * @dev: the device the queue belongs to
3291 * Initialize runtime-PM-related fields for @q and start auto suspend for
3292 * @dev. Drivers that want to take advantage of request-based runtime PM
3293 * should call this function after @dev has been initialized, and its
3294 * request queue @q has been allocated, and runtime PM for it can not happen
3295 * yet(either due to disabled/forbidden or its usage_count > 0). In most
3296 * cases, driver should call this function before any I/O has taken place.
3298 * This function takes care of setting up using auto suspend for the device,
3299 * the autosuspend delay is set to -1 to make runtime suspend impossible
3300 * until an updated value is either set by user or by driver. Drivers do
3301 * not need to touch other autosuspend settings.
3303 * The block layer runtime PM is request based, so only works for drivers
3304 * that use request as their IO unit instead of those directly use bio's.
3306 void blk_pm_runtime_init(struct request_queue
*q
, struct device
*dev
)
3309 q
->rpm_status
= RPM_ACTIVE
;
3310 pm_runtime_set_autosuspend_delay(q
->dev
, -1);
3311 pm_runtime_use_autosuspend(q
->dev
);
3313 EXPORT_SYMBOL(blk_pm_runtime_init
);
3316 * blk_pre_runtime_suspend - Pre runtime suspend check
3317 * @q: the queue of the device
3320 * This function will check if runtime suspend is allowed for the device
3321 * by examining if there are any requests pending in the queue. If there
3322 * are requests pending, the device can not be runtime suspended; otherwise,
3323 * the queue's status will be updated to SUSPENDING and the driver can
3324 * proceed to suspend the device.
3326 * For the not allowed case, we mark last busy for the device so that
3327 * runtime PM core will try to autosuspend it some time later.
3329 * This function should be called near the start of the device's
3330 * runtime_suspend callback.
3333 * 0 - OK to runtime suspend the device
3334 * -EBUSY - Device should not be runtime suspended
3336 int blk_pre_runtime_suspend(struct request_queue
*q
)
3343 spin_lock_irq(q
->queue_lock
);
3344 if (q
->nr_pending
) {
3346 pm_runtime_mark_last_busy(q
->dev
);
3348 q
->rpm_status
= RPM_SUSPENDING
;
3350 spin_unlock_irq(q
->queue_lock
);
3353 EXPORT_SYMBOL(blk_pre_runtime_suspend
);
3356 * blk_post_runtime_suspend - Post runtime suspend processing
3357 * @q: the queue of the device
3358 * @err: return value of the device's runtime_suspend function
3361 * Update the queue's runtime status according to the return value of the
3362 * device's runtime suspend function and mark last busy for the device so
3363 * that PM core will try to auto suspend the device at a later time.
3365 * This function should be called near the end of the device's
3366 * runtime_suspend callback.
3368 void blk_post_runtime_suspend(struct request_queue
*q
, int err
)
3373 spin_lock_irq(q
->queue_lock
);
3375 q
->rpm_status
= RPM_SUSPENDED
;
3377 q
->rpm_status
= RPM_ACTIVE
;
3378 pm_runtime_mark_last_busy(q
->dev
);
3380 spin_unlock_irq(q
->queue_lock
);
3382 EXPORT_SYMBOL(blk_post_runtime_suspend
);
3385 * blk_pre_runtime_resume - Pre runtime resume processing
3386 * @q: the queue of the device
3389 * Update the queue's runtime status to RESUMING in preparation for the
3390 * runtime resume of the device.
3392 * This function should be called near the start of the device's
3393 * runtime_resume callback.
3395 void blk_pre_runtime_resume(struct request_queue
*q
)
3400 spin_lock_irq(q
->queue_lock
);
3401 q
->rpm_status
= RPM_RESUMING
;
3402 spin_unlock_irq(q
->queue_lock
);
3404 EXPORT_SYMBOL(blk_pre_runtime_resume
);
3407 * blk_post_runtime_resume - Post runtime resume processing
3408 * @q: the queue of the device
3409 * @err: return value of the device's runtime_resume function
3412 * Update the queue's runtime status according to the return value of the
3413 * device's runtime_resume function. If it is successfully resumed, process
3414 * the requests that are queued into the device's queue when it is resuming
3415 * and then mark last busy and initiate autosuspend for it.
3417 * This function should be called near the end of the device's
3418 * runtime_resume callback.
3420 void blk_post_runtime_resume(struct request_queue
*q
, int err
)
3425 spin_lock_irq(q
->queue_lock
);
3427 q
->rpm_status
= RPM_ACTIVE
;
3429 pm_runtime_mark_last_busy(q
->dev
);
3430 pm_request_autosuspend(q
->dev
);
3432 q
->rpm_status
= RPM_SUSPENDED
;
3434 spin_unlock_irq(q
->queue_lock
);
3436 EXPORT_SYMBOL(blk_post_runtime_resume
);
3439 * blk_set_runtime_active - Force runtime status of the queue to be active
3440 * @q: the queue of the device
3442 * If the device is left runtime suspended during system suspend the resume
3443 * hook typically resumes the device and corrects runtime status
3444 * accordingly. However, that does not affect the queue runtime PM status
3445 * which is still "suspended". This prevents processing requests from the
3448 * This function can be used in driver's resume hook to correct queue
3449 * runtime PM status and re-enable peeking requests from the queue. It
3450 * should be called before first request is added to the queue.
3452 void blk_set_runtime_active(struct request_queue
*q
)
3454 spin_lock_irq(q
->queue_lock
);
3455 q
->rpm_status
= RPM_ACTIVE
;
3456 pm_runtime_mark_last_busy(q
->dev
);
3457 pm_request_autosuspend(q
->dev
);
3458 spin_unlock_irq(q
->queue_lock
);
3460 EXPORT_SYMBOL(blk_set_runtime_active
);
3463 int __init
blk_dev_init(void)
3465 BUILD_BUG_ON(REQ_OP_LAST
>= (1 << REQ_OP_BITS
));
3466 BUILD_BUG_ON(REQ_OP_BITS
+ REQ_FLAG_BITS
> 8 *
3467 FIELD_SIZEOF(struct request
, cmd_flags
));
3468 BUILD_BUG_ON(REQ_OP_BITS
+ REQ_FLAG_BITS
> 8 *
3469 FIELD_SIZEOF(struct bio
, bi_opf
));
3471 /* used for unplugging and affects IO latency/throughput - HIGHPRI */
3472 kblockd_workqueue
= alloc_workqueue("kblockd",
3473 WQ_MEM_RECLAIM
| WQ_HIGHPRI
, 0);
3474 if (!kblockd_workqueue
)
3475 panic("Failed to create kblockd\n");
3477 request_cachep
= kmem_cache_create("blkdev_requests",
3478 sizeof(struct request
), 0, SLAB_PANIC
, NULL
);
3480 blk_requestq_cachep
= kmem_cache_create("request_queue",
3481 sizeof(struct request_queue
), 0, SLAB_PANIC
, NULL
);
3483 #ifdef CONFIG_DEBUG_FS
3484 blk_debugfs_root
= debugfs_create_dir("block", NULL
);