2 * Block multiqueue core code
4 * Copyright (C) 2013-2014 Jens Axboe
5 * Copyright (C) 2013-2014 Christoph Hellwig
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/backing-dev.h>
10 #include <linux/bio.h>
11 #include <linux/blkdev.h>
12 #include <linux/kmemleak.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/workqueue.h>
17 #include <linux/smp.h>
18 #include <linux/llist.h>
19 #include <linux/list_sort.h>
20 #include <linux/cpu.h>
21 #include <linux/cache.h>
22 #include <linux/sched/sysctl.h>
23 #include <linux/delay.h>
24 #include <linux/crash_dump.h>
26 #include <trace/events/block.h>
28 #include <linux/blk-mq.h>
31 #include "blk-mq-tag.h"
33 static DEFINE_MUTEX(all_q_mutex
);
34 static LIST_HEAD(all_q_list
);
36 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
);
39 * Check if any of the ctx's have pending work in this hardware queue
41 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx
*hctx
)
45 for (i
= 0; i
< hctx
->ctx_map
.size
; i
++)
46 if (hctx
->ctx_map
.map
[i
].word
)
52 static inline struct blk_align_bitmap
*get_bm(struct blk_mq_hw_ctx
*hctx
,
53 struct blk_mq_ctx
*ctx
)
55 return &hctx
->ctx_map
.map
[ctx
->index_hw
/ hctx
->ctx_map
.bits_per_word
];
58 #define CTX_TO_BIT(hctx, ctx) \
59 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
62 * Mark this ctx as having pending work in this hardware queue
64 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx
*hctx
,
65 struct blk_mq_ctx
*ctx
)
67 struct blk_align_bitmap
*bm
= get_bm(hctx
, ctx
);
69 if (!test_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
))
70 set_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
);
73 static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx
*hctx
,
74 struct blk_mq_ctx
*ctx
)
76 struct blk_align_bitmap
*bm
= get_bm(hctx
, ctx
);
78 clear_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
);
81 static int blk_mq_queue_enter(struct request_queue
*q
, gfp_t gfp
)
86 if (percpu_ref_tryget_live(&q
->mq_usage_counter
))
89 if (!(gfp
& __GFP_WAIT
))
92 ret
= wait_event_interruptible(q
->mq_freeze_wq
,
93 !atomic_read(&q
->mq_freeze_depth
) ||
95 if (blk_queue_dying(q
))
102 static void blk_mq_queue_exit(struct request_queue
*q
)
104 percpu_ref_put(&q
->mq_usage_counter
);
107 static void blk_mq_usage_counter_release(struct percpu_ref
*ref
)
109 struct request_queue
*q
=
110 container_of(ref
, struct request_queue
, mq_usage_counter
);
112 wake_up_all(&q
->mq_freeze_wq
);
115 void blk_mq_freeze_queue_start(struct request_queue
*q
)
119 freeze_depth
= atomic_inc_return(&q
->mq_freeze_depth
);
120 if (freeze_depth
== 1) {
121 percpu_ref_kill(&q
->mq_usage_counter
);
122 blk_mq_run_hw_queues(q
, false);
125 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start
);
127 static void blk_mq_freeze_queue_wait(struct request_queue
*q
)
129 wait_event(q
->mq_freeze_wq
, percpu_ref_is_zero(&q
->mq_usage_counter
));
133 * Guarantee no request is in use, so we can change any data structure of
134 * the queue afterward.
136 void blk_mq_freeze_queue(struct request_queue
*q
)
138 blk_mq_freeze_queue_start(q
);
139 blk_mq_freeze_queue_wait(q
);
141 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue
);
143 void blk_mq_unfreeze_queue(struct request_queue
*q
)
147 freeze_depth
= atomic_dec_return(&q
->mq_freeze_depth
);
148 WARN_ON_ONCE(freeze_depth
< 0);
150 percpu_ref_reinit(&q
->mq_usage_counter
);
151 wake_up_all(&q
->mq_freeze_wq
);
154 EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue
);
156 void blk_mq_wake_waiters(struct request_queue
*q
)
158 struct blk_mq_hw_ctx
*hctx
;
161 queue_for_each_hw_ctx(q
, hctx
, i
)
162 if (blk_mq_hw_queue_mapped(hctx
))
163 blk_mq_tag_wakeup_all(hctx
->tags
, true);
166 * If we are called because the queue has now been marked as
167 * dying, we need to ensure that processes currently waiting on
168 * the queue are notified as well.
170 wake_up_all(&q
->mq_freeze_wq
);
173 bool blk_mq_can_queue(struct blk_mq_hw_ctx
*hctx
)
175 return blk_mq_has_free_tags(hctx
->tags
);
177 EXPORT_SYMBOL(blk_mq_can_queue
);
179 static void blk_mq_rq_ctx_init(struct request_queue
*q
, struct blk_mq_ctx
*ctx
,
180 struct request
*rq
, unsigned int rw_flags
)
182 if (blk_queue_io_stat(q
))
183 rw_flags
|= REQ_IO_STAT
;
185 INIT_LIST_HEAD(&rq
->queuelist
);
186 /* csd/requeue_work/fifo_time is initialized before use */
189 rq
->cmd_flags
|= rw_flags
;
190 /* do not touch atomic flags, it needs atomic ops against the timer */
192 INIT_HLIST_NODE(&rq
->hash
);
193 RB_CLEAR_NODE(&rq
->rb_node
);
196 rq
->start_time
= jiffies
;
197 #ifdef CONFIG_BLK_CGROUP
199 set_start_time_ns(rq
);
200 rq
->io_start_time_ns
= 0;
202 rq
->nr_phys_segments
= 0;
203 #if defined(CONFIG_BLK_DEV_INTEGRITY)
204 rq
->nr_integrity_segments
= 0;
207 /* tag was already set */
217 INIT_LIST_HEAD(&rq
->timeout_list
);
221 rq
->end_io_data
= NULL
;
224 ctx
->rq_dispatched
[rw_is_sync(rw_flags
)]++;
227 static struct request
*
228 __blk_mq_alloc_request(struct blk_mq_alloc_data
*data
, int rw
)
233 tag
= blk_mq_get_tag(data
);
234 if (tag
!= BLK_MQ_TAG_FAIL
) {
235 rq
= data
->hctx
->tags
->rqs
[tag
];
237 if (blk_mq_tag_busy(data
->hctx
)) {
238 rq
->cmd_flags
= REQ_MQ_INFLIGHT
;
239 atomic_inc(&data
->hctx
->nr_active
);
243 blk_mq_rq_ctx_init(data
->q
, data
->ctx
, rq
, rw
);
250 struct request
*blk_mq_alloc_request(struct request_queue
*q
, int rw
, gfp_t gfp
,
253 struct blk_mq_ctx
*ctx
;
254 struct blk_mq_hw_ctx
*hctx
;
256 struct blk_mq_alloc_data alloc_data
;
259 ret
= blk_mq_queue_enter(q
, gfp
);
263 ctx
= blk_mq_get_ctx(q
);
264 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
265 blk_mq_set_alloc_data(&alloc_data
, q
, gfp
& ~__GFP_WAIT
,
266 reserved
, ctx
, hctx
);
268 rq
= __blk_mq_alloc_request(&alloc_data
, rw
);
269 if (!rq
&& (gfp
& __GFP_WAIT
)) {
270 __blk_mq_run_hw_queue(hctx
);
273 ctx
= blk_mq_get_ctx(q
);
274 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
275 blk_mq_set_alloc_data(&alloc_data
, q
, gfp
, reserved
, ctx
,
277 rq
= __blk_mq_alloc_request(&alloc_data
, rw
);
278 ctx
= alloc_data
.ctx
;
282 blk_mq_queue_exit(q
);
283 return ERR_PTR(-EWOULDBLOCK
);
287 EXPORT_SYMBOL(blk_mq_alloc_request
);
289 static void __blk_mq_free_request(struct blk_mq_hw_ctx
*hctx
,
290 struct blk_mq_ctx
*ctx
, struct request
*rq
)
292 const int tag
= rq
->tag
;
293 struct request_queue
*q
= rq
->q
;
295 if (rq
->cmd_flags
& REQ_MQ_INFLIGHT
)
296 atomic_dec(&hctx
->nr_active
);
299 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
300 blk_mq_put_tag(hctx
, tag
, &ctx
->last_tag
);
301 blk_mq_queue_exit(q
);
304 void blk_mq_free_hctx_request(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
)
306 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
308 ctx
->rq_completed
[rq_is_sync(rq
)]++;
309 __blk_mq_free_request(hctx
, ctx
, rq
);
312 EXPORT_SYMBOL_GPL(blk_mq_free_hctx_request
);
314 void blk_mq_free_request(struct request
*rq
)
316 struct blk_mq_hw_ctx
*hctx
;
317 struct request_queue
*q
= rq
->q
;
319 hctx
= q
->mq_ops
->map_queue(q
, rq
->mq_ctx
->cpu
);
320 blk_mq_free_hctx_request(hctx
, rq
);
322 EXPORT_SYMBOL_GPL(blk_mq_free_request
);
324 inline void __blk_mq_end_request(struct request
*rq
, int error
)
326 blk_account_io_done(rq
);
329 rq
->end_io(rq
, error
);
331 if (unlikely(blk_bidi_rq(rq
)))
332 blk_mq_free_request(rq
->next_rq
);
333 blk_mq_free_request(rq
);
336 EXPORT_SYMBOL(__blk_mq_end_request
);
338 void blk_mq_end_request(struct request
*rq
, int error
)
340 if (blk_update_request(rq
, error
, blk_rq_bytes(rq
)))
342 __blk_mq_end_request(rq
, error
);
344 EXPORT_SYMBOL(blk_mq_end_request
);
346 static void __blk_mq_complete_request_remote(void *data
)
348 struct request
*rq
= data
;
350 rq
->q
->softirq_done_fn(rq
);
353 static void blk_mq_ipi_complete_request(struct request
*rq
)
355 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
359 if (!test_bit(QUEUE_FLAG_SAME_COMP
, &rq
->q
->queue_flags
)) {
360 rq
->q
->softirq_done_fn(rq
);
365 if (!test_bit(QUEUE_FLAG_SAME_FORCE
, &rq
->q
->queue_flags
))
366 shared
= cpus_share_cache(cpu
, ctx
->cpu
);
368 if (cpu
!= ctx
->cpu
&& !shared
&& cpu_online(ctx
->cpu
)) {
369 rq
->csd
.func
= __blk_mq_complete_request_remote
;
372 smp_call_function_single_async(ctx
->cpu
, &rq
->csd
);
374 rq
->q
->softirq_done_fn(rq
);
379 void __blk_mq_complete_request(struct request
*rq
)
381 struct request_queue
*q
= rq
->q
;
383 if (!q
->softirq_done_fn
)
384 blk_mq_end_request(rq
, rq
->errors
);
386 blk_mq_ipi_complete_request(rq
);
390 * blk_mq_complete_request - end I/O on a request
391 * @rq: the request being processed
394 * Ends all I/O on a request. It does not handle partial completions.
395 * The actual completion happens out-of-order, through a IPI handler.
397 void blk_mq_complete_request(struct request
*rq
, int error
)
399 struct request_queue
*q
= rq
->q
;
401 if (unlikely(blk_should_fake_timeout(q
)))
403 if (!blk_mark_rq_complete(rq
)) {
405 __blk_mq_complete_request(rq
);
408 EXPORT_SYMBOL(blk_mq_complete_request
);
410 int blk_mq_request_started(struct request
*rq
)
412 return test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
414 EXPORT_SYMBOL_GPL(blk_mq_request_started
);
416 void blk_mq_start_request(struct request
*rq
)
418 struct request_queue
*q
= rq
->q
;
420 trace_block_rq_issue(q
, rq
);
422 rq
->resid_len
= blk_rq_bytes(rq
);
423 if (unlikely(blk_bidi_rq(rq
)))
424 rq
->next_rq
->resid_len
= blk_rq_bytes(rq
->next_rq
);
429 * Ensure that ->deadline is visible before set the started
430 * flag and clear the completed flag.
432 smp_mb__before_atomic();
435 * Mark us as started and clear complete. Complete might have been
436 * set if requeue raced with timeout, which then marked it as
437 * complete. So be sure to clear complete again when we start
438 * the request, otherwise we'll ignore the completion event.
440 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
441 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
442 if (test_bit(REQ_ATOM_COMPLETE
, &rq
->atomic_flags
))
443 clear_bit(REQ_ATOM_COMPLETE
, &rq
->atomic_flags
);
445 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
447 * Make sure space for the drain appears. We know we can do
448 * this because max_hw_segments has been adjusted to be one
449 * fewer than the device can handle.
451 rq
->nr_phys_segments
++;
454 EXPORT_SYMBOL(blk_mq_start_request
);
456 static void __blk_mq_requeue_request(struct request
*rq
)
458 struct request_queue
*q
= rq
->q
;
460 trace_block_rq_requeue(q
, rq
);
462 if (test_and_clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
)) {
463 if (q
->dma_drain_size
&& blk_rq_bytes(rq
))
464 rq
->nr_phys_segments
--;
468 void blk_mq_requeue_request(struct request
*rq
)
470 __blk_mq_requeue_request(rq
);
472 BUG_ON(blk_queued_rq(rq
));
473 blk_mq_add_to_requeue_list(rq
, true);
475 EXPORT_SYMBOL(blk_mq_requeue_request
);
477 static void blk_mq_requeue_work(struct work_struct
*work
)
479 struct request_queue
*q
=
480 container_of(work
, struct request_queue
, requeue_work
);
482 struct request
*rq
, *next
;
485 spin_lock_irqsave(&q
->requeue_lock
, flags
);
486 list_splice_init(&q
->requeue_list
, &rq_list
);
487 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
489 list_for_each_entry_safe(rq
, next
, &rq_list
, queuelist
) {
490 if (!(rq
->cmd_flags
& REQ_SOFTBARRIER
))
493 rq
->cmd_flags
&= ~REQ_SOFTBARRIER
;
494 list_del_init(&rq
->queuelist
);
495 blk_mq_insert_request(rq
, true, false, false);
498 while (!list_empty(&rq_list
)) {
499 rq
= list_entry(rq_list
.next
, struct request
, queuelist
);
500 list_del_init(&rq
->queuelist
);
501 blk_mq_insert_request(rq
, false, false, false);
505 * Use the start variant of queue running here, so that running
506 * the requeue work will kick stopped queues.
508 blk_mq_start_hw_queues(q
);
511 void blk_mq_add_to_requeue_list(struct request
*rq
, bool at_head
)
513 struct request_queue
*q
= rq
->q
;
517 * We abuse this flag that is otherwise used by the I/O scheduler to
518 * request head insertation from the workqueue.
520 BUG_ON(rq
->cmd_flags
& REQ_SOFTBARRIER
);
522 spin_lock_irqsave(&q
->requeue_lock
, flags
);
524 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
525 list_add(&rq
->queuelist
, &q
->requeue_list
);
527 list_add_tail(&rq
->queuelist
, &q
->requeue_list
);
529 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
531 EXPORT_SYMBOL(blk_mq_add_to_requeue_list
);
533 void blk_mq_cancel_requeue_work(struct request_queue
*q
)
535 cancel_work_sync(&q
->requeue_work
);
537 EXPORT_SYMBOL_GPL(blk_mq_cancel_requeue_work
);
539 void blk_mq_kick_requeue_list(struct request_queue
*q
)
541 kblockd_schedule_work(&q
->requeue_work
);
543 EXPORT_SYMBOL(blk_mq_kick_requeue_list
);
545 void blk_mq_abort_requeue_list(struct request_queue
*q
)
550 spin_lock_irqsave(&q
->requeue_lock
, flags
);
551 list_splice_init(&q
->requeue_list
, &rq_list
);
552 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
554 while (!list_empty(&rq_list
)) {
557 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
558 list_del_init(&rq
->queuelist
);
560 blk_mq_end_request(rq
, rq
->errors
);
563 EXPORT_SYMBOL(blk_mq_abort_requeue_list
);
565 struct request
*blk_mq_tag_to_rq(struct blk_mq_tags
*tags
, unsigned int tag
)
567 return tags
->rqs
[tag
];
569 EXPORT_SYMBOL(blk_mq_tag_to_rq
);
571 struct blk_mq_timeout_data
{
573 unsigned int next_set
;
576 void blk_mq_rq_timed_out(struct request
*req
, bool reserved
)
578 struct blk_mq_ops
*ops
= req
->q
->mq_ops
;
579 enum blk_eh_timer_return ret
= BLK_EH_RESET_TIMER
;
582 * We know that complete is set at this point. If STARTED isn't set
583 * anymore, then the request isn't active and the "timeout" should
584 * just be ignored. This can happen due to the bitflag ordering.
585 * Timeout first checks if STARTED is set, and if it is, assumes
586 * the request is active. But if we race with completion, then
587 * we both flags will get cleared. So check here again, and ignore
588 * a timeout event with a request that isn't active.
590 if (!test_bit(REQ_ATOM_STARTED
, &req
->atomic_flags
))
594 ret
= ops
->timeout(req
, reserved
);
598 __blk_mq_complete_request(req
);
600 case BLK_EH_RESET_TIMER
:
602 blk_clear_rq_complete(req
);
604 case BLK_EH_NOT_HANDLED
:
607 printk(KERN_ERR
"block: bad eh return: %d\n", ret
);
612 static void blk_mq_check_expired(struct blk_mq_hw_ctx
*hctx
,
613 struct request
*rq
, void *priv
, bool reserved
)
615 struct blk_mq_timeout_data
*data
= priv
;
617 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
)) {
619 * If a request wasn't started before the queue was
620 * marked dying, kill it here or it'll go unnoticed.
622 if (unlikely(blk_queue_dying(rq
->q
)))
623 blk_mq_complete_request(rq
, -EIO
);
626 if (rq
->cmd_flags
& REQ_NO_TIMEOUT
)
629 if (time_after_eq(jiffies
, rq
->deadline
)) {
630 if (!blk_mark_rq_complete(rq
))
631 blk_mq_rq_timed_out(rq
, reserved
);
632 } else if (!data
->next_set
|| time_after(data
->next
, rq
->deadline
)) {
633 data
->next
= rq
->deadline
;
638 static void blk_mq_rq_timer(unsigned long priv
)
640 struct request_queue
*q
= (struct request_queue
*)priv
;
641 struct blk_mq_timeout_data data
= {
647 blk_mq_queue_tag_busy_iter(q
, blk_mq_check_expired
, &data
);
650 data
.next
= blk_rq_timeout(round_jiffies_up(data
.next
));
651 mod_timer(&q
->timeout
, data
.next
);
653 struct blk_mq_hw_ctx
*hctx
;
655 queue_for_each_hw_ctx(q
, hctx
, i
) {
656 /* the hctx may be unmapped, so check it here */
657 if (blk_mq_hw_queue_mapped(hctx
))
658 blk_mq_tag_idle(hctx
);
664 * Reverse check our software queue for entries that we could potentially
665 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
666 * too much time checking for merges.
668 static bool blk_mq_attempt_merge(struct request_queue
*q
,
669 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
674 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
680 if (!blk_rq_merge_ok(rq
, bio
))
683 el_ret
= blk_try_merge(rq
, bio
);
684 if (el_ret
== ELEVATOR_BACK_MERGE
) {
685 if (bio_attempt_back_merge(q
, rq
, bio
)) {
690 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
691 if (bio_attempt_front_merge(q
, rq
, bio
)) {
703 * Process software queues that have been marked busy, splicing them
704 * to the for-dispatch
706 static void flush_busy_ctxs(struct blk_mq_hw_ctx
*hctx
, struct list_head
*list
)
708 struct blk_mq_ctx
*ctx
;
711 for (i
= 0; i
< hctx
->ctx_map
.size
; i
++) {
712 struct blk_align_bitmap
*bm
= &hctx
->ctx_map
.map
[i
];
713 unsigned int off
, bit
;
719 off
= i
* hctx
->ctx_map
.bits_per_word
;
721 bit
= find_next_bit(&bm
->word
, bm
->depth
, bit
);
722 if (bit
>= bm
->depth
)
725 ctx
= hctx
->ctxs
[bit
+ off
];
726 clear_bit(bit
, &bm
->word
);
727 spin_lock(&ctx
->lock
);
728 list_splice_tail_init(&ctx
->rq_list
, list
);
729 spin_unlock(&ctx
->lock
);
737 * Run this hardware queue, pulling any software queues mapped to it in.
738 * Note that this function currently has various problems around ordering
739 * of IO. In particular, we'd like FIFO behaviour on handling existing
740 * items on the hctx->dispatch list. Ignore that for now.
742 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
744 struct request_queue
*q
= hctx
->queue
;
747 LIST_HEAD(driver_list
);
748 struct list_head
*dptr
;
751 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx
->cpumask
));
753 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
759 * Touch any software queue that has pending entries.
761 flush_busy_ctxs(hctx
, &rq_list
);
764 * If we have previous entries on our dispatch list, grab them
765 * and stuff them at the front for more fair dispatch.
767 if (!list_empty_careful(&hctx
->dispatch
)) {
768 spin_lock(&hctx
->lock
);
769 if (!list_empty(&hctx
->dispatch
))
770 list_splice_init(&hctx
->dispatch
, &rq_list
);
771 spin_unlock(&hctx
->lock
);
775 * Start off with dptr being NULL, so we start the first request
776 * immediately, even if we have more pending.
781 * Now process all the entries, sending them to the driver.
784 while (!list_empty(&rq_list
)) {
785 struct blk_mq_queue_data bd
;
788 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
789 list_del_init(&rq
->queuelist
);
793 bd
.last
= list_empty(&rq_list
);
795 ret
= q
->mq_ops
->queue_rq(hctx
, &bd
);
797 case BLK_MQ_RQ_QUEUE_OK
:
800 case BLK_MQ_RQ_QUEUE_BUSY
:
801 list_add(&rq
->queuelist
, &rq_list
);
802 __blk_mq_requeue_request(rq
);
805 pr_err("blk-mq: bad return on queue: %d\n", ret
);
806 case BLK_MQ_RQ_QUEUE_ERROR
:
808 blk_mq_end_request(rq
, rq
->errors
);
812 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
816 * We've done the first request. If we have more than 1
817 * left in the list, set dptr to defer issue.
819 if (!dptr
&& rq_list
.next
!= rq_list
.prev
)
824 hctx
->dispatched
[0]++;
825 else if (queued
< (1 << (BLK_MQ_MAX_DISPATCH_ORDER
- 1)))
826 hctx
->dispatched
[ilog2(queued
) + 1]++;
829 * Any items that need requeuing? Stuff them into hctx->dispatch,
830 * that is where we will continue on next queue run.
832 if (!list_empty(&rq_list
)) {
833 spin_lock(&hctx
->lock
);
834 list_splice(&rq_list
, &hctx
->dispatch
);
835 spin_unlock(&hctx
->lock
);
837 * the queue is expected stopped with BLK_MQ_RQ_QUEUE_BUSY, but
838 * it's possible the queue is stopped and restarted again
839 * before this. Queue restart will dispatch requests. And since
840 * requests in rq_list aren't added into hctx->dispatch yet,
841 * the requests in rq_list might get lost.
843 * blk_mq_run_hw_queue() already checks the STOPPED bit
845 blk_mq_run_hw_queue(hctx
, true);
850 * It'd be great if the workqueue API had a way to pass
851 * in a mask and had some smarts for more clever placement.
852 * For now we just round-robin here, switching for every
853 * BLK_MQ_CPU_WORK_BATCH queued items.
855 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx
*hctx
)
857 if (hctx
->queue
->nr_hw_queues
== 1)
858 return WORK_CPU_UNBOUND
;
860 if (--hctx
->next_cpu_batch
<= 0) {
861 int cpu
= hctx
->next_cpu
, next_cpu
;
863 next_cpu
= cpumask_next(hctx
->next_cpu
, hctx
->cpumask
);
864 if (next_cpu
>= nr_cpu_ids
)
865 next_cpu
= cpumask_first(hctx
->cpumask
);
867 hctx
->next_cpu
= next_cpu
;
868 hctx
->next_cpu_batch
= BLK_MQ_CPU_WORK_BATCH
;
873 return hctx
->next_cpu
;
876 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
878 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
) ||
879 !blk_mq_hw_queue_mapped(hctx
)))
884 if (cpumask_test_cpu(cpu
, hctx
->cpumask
)) {
885 __blk_mq_run_hw_queue(hctx
);
893 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx
),
897 void blk_mq_run_hw_queues(struct request_queue
*q
, bool async
)
899 struct blk_mq_hw_ctx
*hctx
;
902 queue_for_each_hw_ctx(q
, hctx
, i
) {
903 if ((!blk_mq_hctx_has_pending(hctx
) &&
904 list_empty_careful(&hctx
->dispatch
)) ||
905 test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
908 blk_mq_run_hw_queue(hctx
, async
);
911 EXPORT_SYMBOL(blk_mq_run_hw_queues
);
913 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
915 cancel_delayed_work(&hctx
->run_work
);
916 cancel_delayed_work(&hctx
->delay_work
);
917 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
919 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
921 void blk_mq_stop_hw_queues(struct request_queue
*q
)
923 struct blk_mq_hw_ctx
*hctx
;
926 queue_for_each_hw_ctx(q
, hctx
, i
)
927 blk_mq_stop_hw_queue(hctx
);
929 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
931 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
933 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
935 blk_mq_run_hw_queue(hctx
, false);
937 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
939 void blk_mq_start_hw_queues(struct request_queue
*q
)
941 struct blk_mq_hw_ctx
*hctx
;
944 queue_for_each_hw_ctx(q
, hctx
, i
)
945 blk_mq_start_hw_queue(hctx
);
947 EXPORT_SYMBOL(blk_mq_start_hw_queues
);
949 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
, bool async
)
951 struct blk_mq_hw_ctx
*hctx
;
954 queue_for_each_hw_ctx(q
, hctx
, i
) {
955 if (!test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
958 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
959 blk_mq_run_hw_queue(hctx
, async
);
962 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
964 static void blk_mq_run_work_fn(struct work_struct
*work
)
966 struct blk_mq_hw_ctx
*hctx
;
968 hctx
= container_of(work
, struct blk_mq_hw_ctx
, run_work
.work
);
970 __blk_mq_run_hw_queue(hctx
);
973 static void blk_mq_delay_work_fn(struct work_struct
*work
)
975 struct blk_mq_hw_ctx
*hctx
;
977 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delay_work
.work
);
979 if (test_and_clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
980 __blk_mq_run_hw_queue(hctx
);
983 void blk_mq_delay_queue(struct blk_mq_hw_ctx
*hctx
, unsigned long msecs
)
985 if (unlikely(!blk_mq_hw_queue_mapped(hctx
)))
988 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx
),
989 &hctx
->delay_work
, msecs_to_jiffies(msecs
));
991 EXPORT_SYMBOL(blk_mq_delay_queue
);
993 static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx
*hctx
,
994 struct blk_mq_ctx
*ctx
,
998 trace_block_rq_insert(hctx
->queue
, rq
);
1001 list_add(&rq
->queuelist
, &ctx
->rq_list
);
1003 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
1006 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
1007 struct request
*rq
, bool at_head
)
1009 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
1011 __blk_mq_insert_req_list(hctx
, ctx
, rq
, at_head
);
1012 blk_mq_hctx_mark_pending(hctx
, ctx
);
1015 void blk_mq_insert_request(struct request
*rq
, bool at_head
, bool run_queue
,
1018 struct request_queue
*q
= rq
->q
;
1019 struct blk_mq_hw_ctx
*hctx
;
1020 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
, *current_ctx
;
1022 current_ctx
= blk_mq_get_ctx(q
);
1023 if (!cpu_online(ctx
->cpu
))
1024 rq
->mq_ctx
= ctx
= current_ctx
;
1026 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1028 spin_lock(&ctx
->lock
);
1029 __blk_mq_insert_request(hctx
, rq
, at_head
);
1030 spin_unlock(&ctx
->lock
);
1033 blk_mq_run_hw_queue(hctx
, async
);
1035 blk_mq_put_ctx(current_ctx
);
1038 static void blk_mq_insert_requests(struct request_queue
*q
,
1039 struct blk_mq_ctx
*ctx
,
1040 struct list_head
*list
,
1045 struct blk_mq_hw_ctx
*hctx
;
1046 struct blk_mq_ctx
*current_ctx
;
1048 trace_block_unplug(q
, depth
, !from_schedule
);
1050 current_ctx
= blk_mq_get_ctx(q
);
1052 if (!cpu_online(ctx
->cpu
))
1054 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1057 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1060 spin_lock(&ctx
->lock
);
1061 while (!list_empty(list
)) {
1064 rq
= list_first_entry(list
, struct request
, queuelist
);
1065 list_del_init(&rq
->queuelist
);
1067 __blk_mq_insert_req_list(hctx
, ctx
, rq
, false);
1069 blk_mq_hctx_mark_pending(hctx
, ctx
);
1070 spin_unlock(&ctx
->lock
);
1072 blk_mq_run_hw_queue(hctx
, from_schedule
);
1073 blk_mq_put_ctx(current_ctx
);
1076 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1078 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
1079 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
1081 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
1082 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
1083 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
1086 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
1088 struct blk_mq_ctx
*this_ctx
;
1089 struct request_queue
*this_q
;
1092 LIST_HEAD(ctx_list
);
1095 list_splice_init(&plug
->mq_list
, &list
);
1097 list_sort(NULL
, &list
, plug_ctx_cmp
);
1103 while (!list_empty(&list
)) {
1104 rq
= list_entry_rq(list
.next
);
1105 list_del_init(&rq
->queuelist
);
1107 if (rq
->mq_ctx
!= this_ctx
) {
1109 blk_mq_insert_requests(this_q
, this_ctx
,
1114 this_ctx
= rq
->mq_ctx
;
1120 list_add_tail(&rq
->queuelist
, &ctx_list
);
1124 * If 'this_ctx' is set, we know we have entries to complete
1125 * on 'ctx_list'. Do those.
1128 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
1133 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
1135 init_request_from_bio(rq
, bio
);
1137 if (blk_do_io_stat(rq
))
1138 blk_account_io_start(rq
, 1);
1141 static inline bool hctx_allow_merges(struct blk_mq_hw_ctx
*hctx
)
1143 return (hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
) &&
1144 !blk_queue_nomerges(hctx
->queue
);
1147 static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx
*hctx
,
1148 struct blk_mq_ctx
*ctx
,
1149 struct request
*rq
, struct bio
*bio
)
1151 if (!hctx_allow_merges(hctx
) || !bio_mergeable(bio
)) {
1152 blk_mq_bio_to_request(rq
, bio
);
1153 spin_lock(&ctx
->lock
);
1155 __blk_mq_insert_request(hctx
, rq
, false);
1156 spin_unlock(&ctx
->lock
);
1159 struct request_queue
*q
= hctx
->queue
;
1161 spin_lock(&ctx
->lock
);
1162 if (!blk_mq_attempt_merge(q
, ctx
, bio
)) {
1163 blk_mq_bio_to_request(rq
, bio
);
1167 spin_unlock(&ctx
->lock
);
1168 __blk_mq_free_request(hctx
, ctx
, rq
);
1173 struct blk_map_ctx
{
1174 struct blk_mq_hw_ctx
*hctx
;
1175 struct blk_mq_ctx
*ctx
;
1178 static struct request
*blk_mq_map_request(struct request_queue
*q
,
1180 struct blk_map_ctx
*data
)
1182 struct blk_mq_hw_ctx
*hctx
;
1183 struct blk_mq_ctx
*ctx
;
1185 int rw
= bio_data_dir(bio
);
1186 struct blk_mq_alloc_data alloc_data
;
1188 if (unlikely(blk_mq_queue_enter(q
, GFP_KERNEL
))) {
1193 ctx
= blk_mq_get_ctx(q
);
1194 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1196 if (rw_is_sync(bio
->bi_rw
))
1199 trace_block_getrq(q
, bio
, rw
);
1200 blk_mq_set_alloc_data(&alloc_data
, q
, GFP_ATOMIC
, false, ctx
,
1202 rq
= __blk_mq_alloc_request(&alloc_data
, rw
);
1203 if (unlikely(!rq
)) {
1204 __blk_mq_run_hw_queue(hctx
);
1205 blk_mq_put_ctx(ctx
);
1206 trace_block_sleeprq(q
, bio
, rw
);
1208 ctx
= blk_mq_get_ctx(q
);
1209 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1210 blk_mq_set_alloc_data(&alloc_data
, q
,
1211 __GFP_WAIT
|GFP_ATOMIC
, false, ctx
, hctx
);
1212 rq
= __blk_mq_alloc_request(&alloc_data
, rw
);
1213 ctx
= alloc_data
.ctx
;
1214 hctx
= alloc_data
.hctx
;
1223 static int blk_mq_direct_issue_request(struct request
*rq
)
1226 struct request_queue
*q
= rq
->q
;
1227 struct blk_mq_hw_ctx
*hctx
= q
->mq_ops
->map_queue(q
,
1229 struct blk_mq_queue_data bd
= {
1236 * For OK queue, we are done. For error, kill it. Any other
1237 * error (busy), just add it to our list as we previously
1240 ret
= q
->mq_ops
->queue_rq(hctx
, &bd
);
1241 if (ret
== BLK_MQ_RQ_QUEUE_OK
)
1244 __blk_mq_requeue_request(rq
);
1246 if (ret
== BLK_MQ_RQ_QUEUE_ERROR
) {
1248 blk_mq_end_request(rq
, rq
->errors
);
1256 * Multiple hardware queue variant. This will not use per-process plugs,
1257 * but will attempt to bypass the hctx queueing if we can go straight to
1258 * hardware for SYNC IO.
1260 static void blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
1262 const int is_sync
= rw_is_sync(bio
->bi_rw
);
1263 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
1264 struct blk_map_ctx data
;
1266 unsigned int request_count
= 0;
1267 struct blk_plug
*plug
;
1268 struct request
*same_queue_rq
= NULL
;
1270 blk_queue_bounce(q
, &bio
);
1272 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1277 blk_queue_split(q
, &bio
, q
->bio_split
);
1279 if (!is_flush_fua
&& !blk_queue_nomerges(q
)) {
1280 if (blk_attempt_plug_merge(q
, bio
, &request_count
,
1284 request_count
= blk_plug_queued_count(q
);
1286 rq
= blk_mq_map_request(q
, bio
, &data
);
1290 if (unlikely(is_flush_fua
)) {
1291 blk_mq_bio_to_request(rq
, bio
);
1292 blk_insert_flush(rq
);
1296 plug
= current
->plug
;
1298 * If the driver supports defer issued based on 'last', then
1299 * queue it up like normal since we can potentially save some
1302 if (((plug
&& !blk_queue_nomerges(q
)) || is_sync
) &&
1303 !(data
.hctx
->flags
& BLK_MQ_F_DEFER_ISSUE
)) {
1304 struct request
*old_rq
= NULL
;
1306 blk_mq_bio_to_request(rq
, bio
);
1309 * we do limited pluging. If bio can be merged, do merge.
1310 * Otherwise the existing request in the plug list will be
1311 * issued. So the plug list will have one request at most
1315 * The plug list might get flushed before this. If that
1316 * happens, same_queue_rq is invalid and plug list is empty
1318 if (same_queue_rq
&& !list_empty(&plug
->mq_list
)) {
1319 old_rq
= same_queue_rq
;
1320 list_del_init(&old_rq
->queuelist
);
1322 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
1323 } else /* is_sync */
1325 blk_mq_put_ctx(data
.ctx
);
1328 if (!blk_mq_direct_issue_request(old_rq
))
1330 blk_mq_insert_request(old_rq
, false, true, true);
1334 if (!blk_mq_merge_queue_io(data
.hctx
, data
.ctx
, rq
, bio
)) {
1336 * For a SYNC request, send it to the hardware immediately. For
1337 * an ASYNC request, just ensure that we run it later on. The
1338 * latter allows for merging opportunities and more efficient
1342 blk_mq_run_hw_queue(data
.hctx
, !is_sync
|| is_flush_fua
);
1344 blk_mq_put_ctx(data
.ctx
);
1348 * Single hardware queue variant. This will attempt to use any per-process
1349 * plug for merging and IO deferral.
1351 static void blk_sq_make_request(struct request_queue
*q
, struct bio
*bio
)
1353 const int is_sync
= rw_is_sync(bio
->bi_rw
);
1354 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
1355 struct blk_plug
*plug
;
1356 unsigned int request_count
= 0;
1357 struct blk_map_ctx data
;
1360 blk_queue_bounce(q
, &bio
);
1362 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1367 blk_queue_split(q
, &bio
, q
->bio_split
);
1369 if (!is_flush_fua
&& !blk_queue_nomerges(q
) &&
1370 blk_attempt_plug_merge(q
, bio
, &request_count
, NULL
))
1373 rq
= blk_mq_map_request(q
, bio
, &data
);
1377 if (unlikely(is_flush_fua
)) {
1378 blk_mq_bio_to_request(rq
, bio
);
1379 blk_insert_flush(rq
);
1384 * A task plug currently exists. Since this is completely lockless,
1385 * utilize that to temporarily store requests until the task is
1386 * either done or scheduled away.
1388 plug
= current
->plug
;
1390 blk_mq_bio_to_request(rq
, bio
);
1392 trace_block_plug(q
);
1393 else if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
1394 blk_flush_plug_list(plug
, false);
1395 trace_block_plug(q
);
1397 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
1398 blk_mq_put_ctx(data
.ctx
);
1402 if (!blk_mq_merge_queue_io(data
.hctx
, data
.ctx
, rq
, bio
)) {
1404 * For a SYNC request, send it to the hardware immediately. For
1405 * an ASYNC request, just ensure that we run it later on. The
1406 * latter allows for merging opportunities and more efficient
1410 blk_mq_run_hw_queue(data
.hctx
, !is_sync
|| is_flush_fua
);
1413 blk_mq_put_ctx(data
.ctx
);
1417 * Default mapping to a software queue, since we use one per CPU.
1419 struct blk_mq_hw_ctx
*blk_mq_map_queue(struct request_queue
*q
, const int cpu
)
1421 return q
->queue_hw_ctx
[q
->mq_map
[cpu
]];
1423 EXPORT_SYMBOL(blk_mq_map_queue
);
1425 static void blk_mq_free_rq_map(struct blk_mq_tag_set
*set
,
1426 struct blk_mq_tags
*tags
, unsigned int hctx_idx
)
1430 if (tags
->rqs
&& set
->ops
->exit_request
) {
1433 for (i
= 0; i
< tags
->nr_tags
; i
++) {
1436 set
->ops
->exit_request(set
->driver_data
, tags
->rqs
[i
],
1438 tags
->rqs
[i
] = NULL
;
1442 while (!list_empty(&tags
->page_list
)) {
1443 page
= list_first_entry(&tags
->page_list
, struct page
, lru
);
1444 list_del_init(&page
->lru
);
1446 * Remove kmemleak object previously allocated in
1447 * blk_mq_init_rq_map().
1449 kmemleak_free(page_address(page
));
1450 __free_pages(page
, page
->private);
1455 blk_mq_free_tags(tags
);
1458 static size_t order_to_size(unsigned int order
)
1460 return (size_t)PAGE_SIZE
<< order
;
1463 static struct blk_mq_tags
*blk_mq_init_rq_map(struct blk_mq_tag_set
*set
,
1464 unsigned int hctx_idx
)
1466 struct blk_mq_tags
*tags
;
1467 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1468 size_t rq_size
, left
;
1470 tags
= blk_mq_init_tags(set
->queue_depth
, set
->reserved_tags
,
1472 BLK_MQ_FLAG_TO_ALLOC_POLICY(set
->flags
));
1476 INIT_LIST_HEAD(&tags
->page_list
);
1478 tags
->rqs
= kzalloc_node(set
->queue_depth
* sizeof(struct request
*),
1479 GFP_KERNEL
| __GFP_NOWARN
| __GFP_NORETRY
,
1482 blk_mq_free_tags(tags
);
1487 * rq_size is the size of the request plus driver payload, rounded
1488 * to the cacheline size
1490 rq_size
= round_up(sizeof(struct request
) + set
->cmd_size
,
1492 left
= rq_size
* set
->queue_depth
;
1494 for (i
= 0; i
< set
->queue_depth
; ) {
1495 int this_order
= max_order
;
1500 while (left
< order_to_size(this_order
- 1) && this_order
)
1504 page
= alloc_pages_node(set
->numa_node
,
1505 GFP_KERNEL
| __GFP_NOWARN
| __GFP_NORETRY
| __GFP_ZERO
,
1511 if (order_to_size(this_order
) < rq_size
)
1518 page
->private = this_order
;
1519 list_add_tail(&page
->lru
, &tags
->page_list
);
1521 p
= page_address(page
);
1523 * Allow kmemleak to scan these pages as they contain pointers
1524 * to additional allocations like via ops->init_request().
1526 kmemleak_alloc(p
, order_to_size(this_order
), 1, GFP_KERNEL
);
1527 entries_per_page
= order_to_size(this_order
) / rq_size
;
1528 to_do
= min(entries_per_page
, set
->queue_depth
- i
);
1529 left
-= to_do
* rq_size
;
1530 for (j
= 0; j
< to_do
; j
++) {
1532 if (set
->ops
->init_request
) {
1533 if (set
->ops
->init_request(set
->driver_data
,
1534 tags
->rqs
[i
], hctx_idx
, i
,
1536 tags
->rqs
[i
] = NULL
;
1548 blk_mq_free_rq_map(set
, tags
, hctx_idx
);
1552 static void blk_mq_free_bitmap(struct blk_mq_ctxmap
*bitmap
)
1557 static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap
*bitmap
, int node
)
1559 unsigned int bpw
= 8, total
, num_maps
, i
;
1561 bitmap
->bits_per_word
= bpw
;
1563 num_maps
= ALIGN(nr_cpu_ids
, bpw
) / bpw
;
1564 bitmap
->map
= kzalloc_node(num_maps
* sizeof(struct blk_align_bitmap
),
1570 for (i
= 0; i
< num_maps
; i
++) {
1571 bitmap
->map
[i
].depth
= min(total
, bitmap
->bits_per_word
);
1572 total
-= bitmap
->map
[i
].depth
;
1578 static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx
*hctx
, int cpu
)
1580 struct request_queue
*q
= hctx
->queue
;
1581 struct blk_mq_ctx
*ctx
;
1585 * Move ctx entries to new CPU, if this one is going away.
1587 ctx
= __blk_mq_get_ctx(q
, cpu
);
1589 spin_lock(&ctx
->lock
);
1590 if (!list_empty(&ctx
->rq_list
)) {
1591 list_splice_init(&ctx
->rq_list
, &tmp
);
1592 blk_mq_hctx_clear_pending(hctx
, ctx
);
1594 spin_unlock(&ctx
->lock
);
1596 if (list_empty(&tmp
))
1599 ctx
= blk_mq_get_ctx(q
);
1600 spin_lock(&ctx
->lock
);
1602 while (!list_empty(&tmp
)) {
1605 rq
= list_first_entry(&tmp
, struct request
, queuelist
);
1607 list_move_tail(&rq
->queuelist
, &ctx
->rq_list
);
1610 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1611 blk_mq_hctx_mark_pending(hctx
, ctx
);
1613 spin_unlock(&ctx
->lock
);
1615 blk_mq_run_hw_queue(hctx
, true);
1616 blk_mq_put_ctx(ctx
);
1620 static int blk_mq_hctx_notify(void *data
, unsigned long action
,
1623 struct blk_mq_hw_ctx
*hctx
= data
;
1625 if (action
== CPU_DEAD
|| action
== CPU_DEAD_FROZEN
)
1626 return blk_mq_hctx_cpu_offline(hctx
, cpu
);
1629 * In case of CPU online, tags may be reallocated
1630 * in blk_mq_map_swqueue() after mapping is updated.
1636 /* hctx->ctxs will be freed in queue's release handler */
1637 static void blk_mq_exit_hctx(struct request_queue
*q
,
1638 struct blk_mq_tag_set
*set
,
1639 struct blk_mq_hw_ctx
*hctx
, unsigned int hctx_idx
)
1641 unsigned flush_start_tag
= set
->queue_depth
;
1643 blk_mq_tag_idle(hctx
);
1645 if (set
->ops
->exit_request
)
1646 set
->ops
->exit_request(set
->driver_data
,
1647 hctx
->fq
->flush_rq
, hctx_idx
,
1648 flush_start_tag
+ hctx_idx
);
1650 if (set
->ops
->exit_hctx
)
1651 set
->ops
->exit_hctx(hctx
, hctx_idx
);
1653 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1654 blk_free_flush_queue(hctx
->fq
);
1655 blk_mq_free_bitmap(&hctx
->ctx_map
);
1658 static void blk_mq_exit_hw_queues(struct request_queue
*q
,
1659 struct blk_mq_tag_set
*set
, int nr_queue
)
1661 struct blk_mq_hw_ctx
*hctx
;
1664 queue_for_each_hw_ctx(q
, hctx
, i
) {
1667 blk_mq_exit_hctx(q
, set
, hctx
, i
);
1671 static void blk_mq_free_hw_queues(struct request_queue
*q
,
1672 struct blk_mq_tag_set
*set
)
1674 struct blk_mq_hw_ctx
*hctx
;
1677 queue_for_each_hw_ctx(q
, hctx
, i
)
1678 free_cpumask_var(hctx
->cpumask
);
1681 static int blk_mq_init_hctx(struct request_queue
*q
,
1682 struct blk_mq_tag_set
*set
,
1683 struct blk_mq_hw_ctx
*hctx
, unsigned hctx_idx
)
1686 unsigned flush_start_tag
= set
->queue_depth
;
1688 node
= hctx
->numa_node
;
1689 if (node
== NUMA_NO_NODE
)
1690 node
= hctx
->numa_node
= set
->numa_node
;
1692 INIT_DELAYED_WORK(&hctx
->run_work
, blk_mq_run_work_fn
);
1693 INIT_DELAYED_WORK(&hctx
->delay_work
, blk_mq_delay_work_fn
);
1694 spin_lock_init(&hctx
->lock
);
1695 INIT_LIST_HEAD(&hctx
->dispatch
);
1697 hctx
->queue_num
= hctx_idx
;
1698 hctx
->flags
= set
->flags
& ~BLK_MQ_F_TAG_SHARED
;
1700 blk_mq_init_cpu_notifier(&hctx
->cpu_notifier
,
1701 blk_mq_hctx_notify
, hctx
);
1702 blk_mq_register_cpu_notifier(&hctx
->cpu_notifier
);
1704 hctx
->tags
= set
->tags
[hctx_idx
];
1707 * Allocate space for all possible cpus to avoid allocation at
1710 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1713 goto unregister_cpu_notifier
;
1715 if (blk_mq_alloc_bitmap(&hctx
->ctx_map
, node
))
1720 if (set
->ops
->init_hctx
&&
1721 set
->ops
->init_hctx(hctx
, set
->driver_data
, hctx_idx
))
1724 hctx
->fq
= blk_alloc_flush_queue(q
, hctx
->numa_node
, set
->cmd_size
);
1728 if (set
->ops
->init_request
&&
1729 set
->ops
->init_request(set
->driver_data
,
1730 hctx
->fq
->flush_rq
, hctx_idx
,
1731 flush_start_tag
+ hctx_idx
, node
))
1739 if (set
->ops
->exit_hctx
)
1740 set
->ops
->exit_hctx(hctx
, hctx_idx
);
1742 blk_mq_free_bitmap(&hctx
->ctx_map
);
1745 unregister_cpu_notifier
:
1746 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1751 static int blk_mq_init_hw_queues(struct request_queue
*q
,
1752 struct blk_mq_tag_set
*set
)
1754 struct blk_mq_hw_ctx
*hctx
;
1758 * Initialize hardware queues
1760 queue_for_each_hw_ctx(q
, hctx
, i
) {
1761 if (blk_mq_init_hctx(q
, set
, hctx
, i
))
1765 if (i
== q
->nr_hw_queues
)
1771 blk_mq_exit_hw_queues(q
, set
, i
);
1776 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1777 unsigned int nr_hw_queues
)
1781 for_each_possible_cpu(i
) {
1782 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1783 struct blk_mq_hw_ctx
*hctx
;
1785 memset(__ctx
, 0, sizeof(*__ctx
));
1787 spin_lock_init(&__ctx
->lock
);
1788 INIT_LIST_HEAD(&__ctx
->rq_list
);
1791 /* If the cpu isn't online, the cpu is mapped to first hctx */
1795 hctx
= q
->mq_ops
->map_queue(q
, i
);
1798 * Set local node, IFF we have more than one hw queue. If
1799 * not, we remain on the home node of the device
1801 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1802 hctx
->numa_node
= cpu_to_node(i
);
1806 static void blk_mq_map_swqueue(struct request_queue
*q
,
1807 const struct cpumask
*online_mask
)
1810 struct blk_mq_hw_ctx
*hctx
;
1811 struct blk_mq_ctx
*ctx
;
1812 struct blk_mq_tag_set
*set
= q
->tag_set
;
1815 * Avoid others reading imcomplete hctx->cpumask through sysfs
1817 mutex_lock(&q
->sysfs_lock
);
1819 queue_for_each_hw_ctx(q
, hctx
, i
) {
1820 cpumask_clear(hctx
->cpumask
);
1825 * Map software to hardware queues
1827 queue_for_each_ctx(q
, ctx
, i
) {
1828 /* If the cpu isn't online, the cpu is mapped to first hctx */
1829 if (!cpumask_test_cpu(i
, online_mask
))
1832 hctx
= q
->mq_ops
->map_queue(q
, i
);
1833 cpumask_set_cpu(i
, hctx
->cpumask
);
1834 ctx
->index_hw
= hctx
->nr_ctx
;
1835 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1838 mutex_unlock(&q
->sysfs_lock
);
1840 queue_for_each_hw_ctx(q
, hctx
, i
) {
1841 struct blk_mq_ctxmap
*map
= &hctx
->ctx_map
;
1844 * If no software queues are mapped to this hardware queue,
1845 * disable it and free the request entries.
1847 if (!hctx
->nr_ctx
) {
1849 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
1850 set
->tags
[i
] = NULL
;
1856 /* unmapped hw queue can be remapped after CPU topo changed */
1858 set
->tags
[i
] = blk_mq_init_rq_map(set
, i
);
1859 hctx
->tags
= set
->tags
[i
];
1860 WARN_ON(!hctx
->tags
);
1863 * Set the map size to the number of mapped software queues.
1864 * This is more accurate and more efficient than looping
1865 * over all possibly mapped software queues.
1867 map
->size
= DIV_ROUND_UP(hctx
->nr_ctx
, map
->bits_per_word
);
1870 * Initialize batch roundrobin counts
1872 hctx
->next_cpu
= cpumask_first(hctx
->cpumask
);
1873 hctx
->next_cpu_batch
= BLK_MQ_CPU_WORK_BATCH
;
1876 queue_for_each_ctx(q
, ctx
, i
) {
1877 if (!cpumask_test_cpu(i
, online_mask
))
1880 hctx
= q
->mq_ops
->map_queue(q
, i
);
1881 cpumask_set_cpu(i
, hctx
->tags
->cpumask
);
1885 static void queue_set_hctx_shared(struct request_queue
*q
, bool shared
)
1887 struct blk_mq_hw_ctx
*hctx
;
1890 queue_for_each_hw_ctx(q
, hctx
, i
) {
1892 hctx
->flags
|= BLK_MQ_F_TAG_SHARED
;
1894 hctx
->flags
&= ~BLK_MQ_F_TAG_SHARED
;
1898 static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set
*set
, bool shared
)
1900 struct request_queue
*q
;
1902 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
) {
1903 blk_mq_freeze_queue(q
);
1904 queue_set_hctx_shared(q
, shared
);
1905 blk_mq_unfreeze_queue(q
);
1909 static void blk_mq_del_queue_tag_set(struct request_queue
*q
)
1911 struct blk_mq_tag_set
*set
= q
->tag_set
;
1913 mutex_lock(&set
->tag_list_lock
);
1914 list_del_init(&q
->tag_set_list
);
1915 if (list_is_singular(&set
->tag_list
)) {
1916 /* just transitioned to unshared */
1917 set
->flags
&= ~BLK_MQ_F_TAG_SHARED
;
1918 /* update existing queue */
1919 blk_mq_update_tag_set_depth(set
, false);
1921 mutex_unlock(&set
->tag_list_lock
);
1924 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set
*set
,
1925 struct request_queue
*q
)
1929 mutex_lock(&set
->tag_list_lock
);
1931 /* Check to see if we're transitioning to shared (from 1 to 2 queues). */
1932 if (!list_empty(&set
->tag_list
) && !(set
->flags
& BLK_MQ_F_TAG_SHARED
)) {
1933 set
->flags
|= BLK_MQ_F_TAG_SHARED
;
1934 /* update existing queue */
1935 blk_mq_update_tag_set_depth(set
, true);
1937 if (set
->flags
& BLK_MQ_F_TAG_SHARED
)
1938 queue_set_hctx_shared(q
, true);
1939 list_add_tail(&q
->tag_set_list
, &set
->tag_list
);
1941 mutex_unlock(&set
->tag_list_lock
);
1945 * It is the actual release handler for mq, but we do it from
1946 * request queue's release handler for avoiding use-after-free
1947 * and headache because q->mq_kobj shouldn't have been introduced,
1948 * but we can't group ctx/kctx kobj without it.
1950 void blk_mq_release(struct request_queue
*q
)
1952 struct blk_mq_hw_ctx
*hctx
;
1955 /* hctx kobj stays in hctx */
1956 queue_for_each_hw_ctx(q
, hctx
, i
) {
1966 kfree(q
->queue_hw_ctx
);
1968 /* ctx kobj stays in queue_ctx */
1969 free_percpu(q
->queue_ctx
);
1972 struct request_queue
*blk_mq_init_queue(struct blk_mq_tag_set
*set
)
1974 struct request_queue
*uninit_q
, *q
;
1976 uninit_q
= blk_alloc_queue_node(GFP_KERNEL
, set
->numa_node
);
1978 return ERR_PTR(-ENOMEM
);
1980 q
= blk_mq_init_allocated_queue(set
, uninit_q
);
1982 blk_cleanup_queue(uninit_q
);
1986 EXPORT_SYMBOL(blk_mq_init_queue
);
1988 struct request_queue
*blk_mq_init_allocated_queue(struct blk_mq_tag_set
*set
,
1989 struct request_queue
*q
)
1991 struct blk_mq_hw_ctx
**hctxs
;
1992 struct blk_mq_ctx __percpu
*ctx
;
1996 ctx
= alloc_percpu(struct blk_mq_ctx
);
1998 return ERR_PTR(-ENOMEM
);
2000 hctxs
= kmalloc_node(set
->nr_hw_queues
* sizeof(*hctxs
), GFP_KERNEL
,
2006 map
= blk_mq_make_queue_map(set
);
2010 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
2011 int node
= blk_mq_hw_queue_to_node(map
, i
);
2013 hctxs
[i
] = kzalloc_node(sizeof(struct blk_mq_hw_ctx
),
2018 if (!zalloc_cpumask_var_node(&hctxs
[i
]->cpumask
, GFP_KERNEL
,
2022 atomic_set(&hctxs
[i
]->nr_active
, 0);
2023 hctxs
[i
]->numa_node
= node
;
2024 hctxs
[i
]->queue_num
= i
;
2028 * Init percpu_ref in atomic mode so that it's faster to shutdown.
2029 * See blk_register_queue() for details.
2031 if (percpu_ref_init(&q
->mq_usage_counter
, blk_mq_usage_counter_release
,
2032 PERCPU_REF_INIT_ATOMIC
, GFP_KERNEL
))
2035 setup_timer(&q
->timeout
, blk_mq_rq_timer
, (unsigned long) q
);
2036 blk_queue_rq_timeout(q
, set
->timeout
? set
->timeout
: 30 * HZ
);
2038 q
->nr_queues
= nr_cpu_ids
;
2039 q
->nr_hw_queues
= set
->nr_hw_queues
;
2043 q
->queue_hw_ctx
= hctxs
;
2045 q
->mq_ops
= set
->ops
;
2046 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
2048 if (!(set
->flags
& BLK_MQ_F_SG_MERGE
))
2049 q
->queue_flags
|= 1 << QUEUE_FLAG_NO_SG_MERGE
;
2051 q
->sg_reserved_size
= INT_MAX
;
2053 INIT_WORK(&q
->requeue_work
, blk_mq_requeue_work
);
2054 INIT_LIST_HEAD(&q
->requeue_list
);
2055 spin_lock_init(&q
->requeue_lock
);
2057 if (q
->nr_hw_queues
> 1)
2058 blk_queue_make_request(q
, blk_mq_make_request
);
2060 blk_queue_make_request(q
, blk_sq_make_request
);
2063 * Do this after blk_queue_make_request() overrides it...
2065 q
->nr_requests
= set
->queue_depth
;
2067 if (set
->ops
->complete
)
2068 blk_queue_softirq_done(q
, set
->ops
->complete
);
2070 blk_mq_init_cpu_queues(q
, set
->nr_hw_queues
);
2072 if (blk_mq_init_hw_queues(q
, set
))
2076 mutex_lock(&all_q_mutex
);
2078 list_add_tail(&q
->all_q_node
, &all_q_list
);
2079 blk_mq_add_queue_tag_set(set
, q
);
2080 blk_mq_map_swqueue(q
, cpu_online_mask
);
2082 mutex_unlock(&all_q_mutex
);
2089 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
2092 free_cpumask_var(hctxs
[i
]->cpumask
);
2099 return ERR_PTR(-ENOMEM
);
2101 EXPORT_SYMBOL(blk_mq_init_allocated_queue
);
2103 void blk_mq_free_queue(struct request_queue
*q
)
2105 struct blk_mq_tag_set
*set
= q
->tag_set
;
2107 mutex_lock(&all_q_mutex
);
2108 list_del_init(&q
->all_q_node
);
2109 mutex_unlock(&all_q_mutex
);
2111 blk_mq_del_queue_tag_set(q
);
2113 blk_mq_exit_hw_queues(q
, set
, set
->nr_hw_queues
);
2114 blk_mq_free_hw_queues(q
, set
);
2116 percpu_ref_exit(&q
->mq_usage_counter
);
2119 /* Basically redo blk_mq_init_queue with queue frozen */
2120 static void blk_mq_queue_reinit(struct request_queue
*q
,
2121 const struct cpumask
*online_mask
)
2123 WARN_ON_ONCE(!atomic_read(&q
->mq_freeze_depth
));
2125 blk_mq_sysfs_unregister(q
);
2127 blk_mq_update_queue_map(q
->mq_map
, q
->nr_hw_queues
, online_mask
);
2130 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
2131 * we should change hctx numa_node according to new topology (this
2132 * involves free and re-allocate memory, worthy doing?)
2135 blk_mq_map_swqueue(q
, online_mask
);
2137 blk_mq_sysfs_register(q
);
2140 static int blk_mq_queue_reinit_notify(struct notifier_block
*nb
,
2141 unsigned long action
, void *hcpu
)
2143 struct request_queue
*q
;
2144 int cpu
= (unsigned long)hcpu
;
2146 * New online cpumask which is going to be set in this hotplug event.
2147 * Declare this cpumasks as global as cpu-hotplug operation is invoked
2148 * one-by-one and dynamically allocating this could result in a failure.
2150 static struct cpumask online_new
;
2153 * Before hotadded cpu starts handling requests, new mappings must
2154 * be established. Otherwise, these requests in hw queue might
2155 * never be dispatched.
2157 * For example, there is a single hw queue (hctx) and two CPU queues
2158 * (ctx0 for CPU0, and ctx1 for CPU1).
2160 * Now CPU1 is just onlined and a request is inserted into
2161 * ctx1->rq_list and set bit0 in pending bitmap as ctx1->index_hw is
2164 * And then while running hw queue, flush_busy_ctxs() finds bit0 is
2165 * set in pending bitmap and tries to retrieve requests in
2166 * hctx->ctxs[0]->rq_list. But htx->ctxs[0] is a pointer to ctx0,
2167 * so the request in ctx1->rq_list is ignored.
2169 switch (action
& ~CPU_TASKS_FROZEN
) {
2171 case CPU_UP_CANCELED
:
2172 cpumask_copy(&online_new
, cpu_online_mask
);
2174 case CPU_UP_PREPARE
:
2175 cpumask_copy(&online_new
, cpu_online_mask
);
2176 cpumask_set_cpu(cpu
, &online_new
);
2182 mutex_lock(&all_q_mutex
);
2185 * We need to freeze and reinit all existing queues. Freezing
2186 * involves synchronous wait for an RCU grace period and doing it
2187 * one by one may take a long time. Start freezing all queues in
2188 * one swoop and then wait for the completions so that freezing can
2189 * take place in parallel.
2191 list_for_each_entry(q
, &all_q_list
, all_q_node
)
2192 blk_mq_freeze_queue_start(q
);
2193 list_for_each_entry(q
, &all_q_list
, all_q_node
) {
2194 blk_mq_freeze_queue_wait(q
);
2197 * timeout handler can't touch hw queue during the
2200 del_timer_sync(&q
->timeout
);
2203 list_for_each_entry(q
, &all_q_list
, all_q_node
)
2204 blk_mq_queue_reinit(q
, &online_new
);
2206 list_for_each_entry(q
, &all_q_list
, all_q_node
)
2207 blk_mq_unfreeze_queue(q
);
2209 mutex_unlock(&all_q_mutex
);
2213 static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set
*set
)
2217 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
2218 set
->tags
[i
] = blk_mq_init_rq_map(set
, i
);
2227 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
2233 * Allocate the request maps associated with this tag_set. Note that this
2234 * may reduce the depth asked for, if memory is tight. set->queue_depth
2235 * will be updated to reflect the allocated depth.
2237 static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set
*set
)
2242 depth
= set
->queue_depth
;
2244 err
= __blk_mq_alloc_rq_maps(set
);
2248 set
->queue_depth
>>= 1;
2249 if (set
->queue_depth
< set
->reserved_tags
+ BLK_MQ_TAG_MIN
) {
2253 } while (set
->queue_depth
);
2255 if (!set
->queue_depth
|| err
) {
2256 pr_err("blk-mq: failed to allocate request map\n");
2260 if (depth
!= set
->queue_depth
)
2261 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2262 depth
, set
->queue_depth
);
2267 struct cpumask
*blk_mq_tags_cpumask(struct blk_mq_tags
*tags
)
2269 return tags
->cpumask
;
2271 EXPORT_SYMBOL_GPL(blk_mq_tags_cpumask
);
2274 * Alloc a tag set to be associated with one or more request queues.
2275 * May fail with EINVAL for various error conditions. May adjust the
2276 * requested depth down, if if it too large. In that case, the set
2277 * value will be stored in set->queue_depth.
2279 int blk_mq_alloc_tag_set(struct blk_mq_tag_set
*set
)
2281 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH
> 1 << BLK_MQ_UNIQUE_TAG_BITS
);
2283 if (!set
->nr_hw_queues
)
2285 if (!set
->queue_depth
)
2287 if (set
->queue_depth
< set
->reserved_tags
+ BLK_MQ_TAG_MIN
)
2290 if (!set
->ops
->queue_rq
|| !set
->ops
->map_queue
)
2293 if (set
->queue_depth
> BLK_MQ_MAX_DEPTH
) {
2294 pr_info("blk-mq: reduced tag depth to %u\n",
2296 set
->queue_depth
= BLK_MQ_MAX_DEPTH
;
2300 * If a crashdump is active, then we are potentially in a very
2301 * memory constrained environment. Limit us to 1 queue and
2302 * 64 tags to prevent using too much memory.
2304 if (is_kdump_kernel()) {
2305 set
->nr_hw_queues
= 1;
2306 set
->queue_depth
= min(64U, set
->queue_depth
);
2309 set
->tags
= kmalloc_node(set
->nr_hw_queues
*
2310 sizeof(struct blk_mq_tags
*),
2311 GFP_KERNEL
, set
->numa_node
);
2315 if (blk_mq_alloc_rq_maps(set
))
2318 mutex_init(&set
->tag_list_lock
);
2319 INIT_LIST_HEAD(&set
->tag_list
);
2327 EXPORT_SYMBOL(blk_mq_alloc_tag_set
);
2329 void blk_mq_free_tag_set(struct blk_mq_tag_set
*set
)
2333 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
2335 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
2336 free_cpumask_var(set
->tags
[i
]->cpumask
);
2343 EXPORT_SYMBOL(blk_mq_free_tag_set
);
2345 int blk_mq_update_nr_requests(struct request_queue
*q
, unsigned int nr
)
2347 struct blk_mq_tag_set
*set
= q
->tag_set
;
2348 struct blk_mq_hw_ctx
*hctx
;
2351 if (!set
|| nr
> set
->queue_depth
)
2355 queue_for_each_hw_ctx(q
, hctx
, i
) {
2356 ret
= blk_mq_tag_update_depth(hctx
->tags
, nr
);
2362 q
->nr_requests
= nr
;
2367 void blk_mq_disable_hotplug(void)
2369 mutex_lock(&all_q_mutex
);
2372 void blk_mq_enable_hotplug(void)
2374 mutex_unlock(&all_q_mutex
);
2377 static int __init
blk_mq_init(void)
2381 hotcpu_notifier(blk_mq_queue_reinit_notify
, 0);
2385 subsys_initcall(blk_mq_init
);