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>
25 #include <linux/prefetch.h>
27 #include <trace/events/block.h>
29 #include <linux/blk-mq.h>
32 #include "blk-mq-tag.h"
34 static DEFINE_MUTEX(all_q_mutex
);
35 static LIST_HEAD(all_q_list
);
38 * Check if any of the ctx's have pending work in this hardware queue
40 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx
*hctx
)
42 return sbitmap_any_bit_set(&hctx
->ctx_map
);
46 * Mark this ctx as having pending work in this hardware queue
48 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx
*hctx
,
49 struct blk_mq_ctx
*ctx
)
51 if (!sbitmap_test_bit(&hctx
->ctx_map
, ctx
->index_hw
))
52 sbitmap_set_bit(&hctx
->ctx_map
, ctx
->index_hw
);
55 static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx
*hctx
,
56 struct blk_mq_ctx
*ctx
)
58 sbitmap_clear_bit(&hctx
->ctx_map
, ctx
->index_hw
);
61 void blk_mq_freeze_queue_start(struct request_queue
*q
)
65 freeze_depth
= atomic_inc_return(&q
->mq_freeze_depth
);
66 if (freeze_depth
== 1) {
67 percpu_ref_kill(&q
->q_usage_counter
);
68 blk_mq_run_hw_queues(q
, false);
71 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start
);
73 static void blk_mq_freeze_queue_wait(struct request_queue
*q
)
75 wait_event(q
->mq_freeze_wq
, percpu_ref_is_zero(&q
->q_usage_counter
));
79 * Guarantee no request is in use, so we can change any data structure of
80 * the queue afterward.
82 void blk_freeze_queue(struct request_queue
*q
)
85 * In the !blk_mq case we are only calling this to kill the
86 * q_usage_counter, otherwise this increases the freeze depth
87 * and waits for it to return to zero. For this reason there is
88 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
89 * exported to drivers as the only user for unfreeze is blk_mq.
91 blk_mq_freeze_queue_start(q
);
92 blk_mq_freeze_queue_wait(q
);
95 void blk_mq_freeze_queue(struct request_queue
*q
)
98 * ...just an alias to keep freeze and unfreeze actions balanced
99 * in the blk_mq_* namespace
103 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue
);
105 void blk_mq_unfreeze_queue(struct request_queue
*q
)
109 freeze_depth
= atomic_dec_return(&q
->mq_freeze_depth
);
110 WARN_ON_ONCE(freeze_depth
< 0);
112 percpu_ref_reinit(&q
->q_usage_counter
);
113 wake_up_all(&q
->mq_freeze_wq
);
116 EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue
);
119 * blk_mq_quiesce_queue() - wait until all ongoing queue_rq calls have finished
122 * Note: this function does not prevent that the struct request end_io()
123 * callback function is invoked. Additionally, it is not prevented that
124 * new queue_rq() calls occur unless the queue has been stopped first.
126 void blk_mq_quiesce_queue(struct request_queue
*q
)
128 struct blk_mq_hw_ctx
*hctx
;
132 blk_mq_stop_hw_queues(q
);
134 queue_for_each_hw_ctx(q
, hctx
, i
) {
135 if (hctx
->flags
& BLK_MQ_F_BLOCKING
)
136 synchronize_srcu(&hctx
->queue_rq_srcu
);
143 EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue
);
145 void blk_mq_wake_waiters(struct request_queue
*q
)
147 struct blk_mq_hw_ctx
*hctx
;
150 queue_for_each_hw_ctx(q
, hctx
, i
)
151 if (blk_mq_hw_queue_mapped(hctx
))
152 blk_mq_tag_wakeup_all(hctx
->tags
, true);
155 * If we are called because the queue has now been marked as
156 * dying, we need to ensure that processes currently waiting on
157 * the queue are notified as well.
159 wake_up_all(&q
->mq_freeze_wq
);
162 bool blk_mq_can_queue(struct blk_mq_hw_ctx
*hctx
)
164 return blk_mq_has_free_tags(hctx
->tags
);
166 EXPORT_SYMBOL(blk_mq_can_queue
);
168 static void blk_mq_rq_ctx_init(struct request_queue
*q
, struct blk_mq_ctx
*ctx
,
169 struct request
*rq
, unsigned int op
)
171 INIT_LIST_HEAD(&rq
->queuelist
);
172 /* csd/requeue_work/fifo_time is initialized before use */
176 if (blk_queue_io_stat(q
))
177 rq
->rq_flags
|= RQF_IO_STAT
;
178 /* do not touch atomic flags, it needs atomic ops against the timer */
180 INIT_HLIST_NODE(&rq
->hash
);
181 RB_CLEAR_NODE(&rq
->rb_node
);
184 rq
->start_time
= jiffies
;
185 #ifdef CONFIG_BLK_CGROUP
187 set_start_time_ns(rq
);
188 rq
->io_start_time_ns
= 0;
190 rq
->nr_phys_segments
= 0;
191 #if defined(CONFIG_BLK_DEV_INTEGRITY)
192 rq
->nr_integrity_segments
= 0;
195 /* tag was already set */
205 INIT_LIST_HEAD(&rq
->timeout_list
);
209 rq
->end_io_data
= NULL
;
212 ctx
->rq_dispatched
[op_is_sync(op
)]++;
215 static struct request
*
216 __blk_mq_alloc_request(struct blk_mq_alloc_data
*data
, unsigned int op
)
221 tag
= blk_mq_get_tag(data
);
222 if (tag
!= BLK_MQ_TAG_FAIL
) {
223 rq
= data
->hctx
->tags
->rqs
[tag
];
225 if (blk_mq_tag_busy(data
->hctx
)) {
226 rq
->rq_flags
= RQF_MQ_INFLIGHT
;
227 atomic_inc(&data
->hctx
->nr_active
);
231 blk_mq_rq_ctx_init(data
->q
, data
->ctx
, rq
, op
);
238 struct request
*blk_mq_alloc_request(struct request_queue
*q
, int rw
,
241 struct blk_mq_ctx
*ctx
;
242 struct blk_mq_hw_ctx
*hctx
;
244 struct blk_mq_alloc_data alloc_data
;
247 ret
= blk_queue_enter(q
, flags
& BLK_MQ_REQ_NOWAIT
);
251 ctx
= blk_mq_get_ctx(q
);
252 hctx
= blk_mq_map_queue(q
, ctx
->cpu
);
253 blk_mq_set_alloc_data(&alloc_data
, q
, flags
, ctx
, hctx
);
254 rq
= __blk_mq_alloc_request(&alloc_data
, rw
);
259 return ERR_PTR(-EWOULDBLOCK
);
263 rq
->__sector
= (sector_t
) -1;
264 rq
->bio
= rq
->biotail
= NULL
;
267 EXPORT_SYMBOL(blk_mq_alloc_request
);
269 struct request
*blk_mq_alloc_request_hctx(struct request_queue
*q
, int rw
,
270 unsigned int flags
, unsigned int hctx_idx
)
272 struct blk_mq_hw_ctx
*hctx
;
273 struct blk_mq_ctx
*ctx
;
275 struct blk_mq_alloc_data alloc_data
;
279 * If the tag allocator sleeps we could get an allocation for a
280 * different hardware context. No need to complicate the low level
281 * allocator for this for the rare use case of a command tied to
284 if (WARN_ON_ONCE(!(flags
& BLK_MQ_REQ_NOWAIT
)))
285 return ERR_PTR(-EINVAL
);
287 if (hctx_idx
>= q
->nr_hw_queues
)
288 return ERR_PTR(-EIO
);
290 ret
= blk_queue_enter(q
, true);
295 * Check if the hardware context is actually mapped to anything.
296 * If not tell the caller that it should skip this queue.
298 hctx
= q
->queue_hw_ctx
[hctx_idx
];
299 if (!blk_mq_hw_queue_mapped(hctx
)) {
303 ctx
= __blk_mq_get_ctx(q
, cpumask_first(hctx
->cpumask
));
305 blk_mq_set_alloc_data(&alloc_data
, q
, flags
, ctx
, hctx
);
306 rq
= __blk_mq_alloc_request(&alloc_data
, rw
);
318 EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx
);
320 static void __blk_mq_free_request(struct blk_mq_hw_ctx
*hctx
,
321 struct blk_mq_ctx
*ctx
, struct request
*rq
)
323 const int tag
= rq
->tag
;
324 struct request_queue
*q
= rq
->q
;
326 if (rq
->rq_flags
& RQF_MQ_INFLIGHT
)
327 atomic_dec(&hctx
->nr_active
);
330 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
331 blk_mq_put_tag(hctx
, ctx
, tag
);
335 void blk_mq_free_hctx_request(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
)
337 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
339 ctx
->rq_completed
[rq_is_sync(rq
)]++;
340 __blk_mq_free_request(hctx
, ctx
, rq
);
343 EXPORT_SYMBOL_GPL(blk_mq_free_hctx_request
);
345 void blk_mq_free_request(struct request
*rq
)
347 blk_mq_free_hctx_request(blk_mq_map_queue(rq
->q
, rq
->mq_ctx
->cpu
), rq
);
349 EXPORT_SYMBOL_GPL(blk_mq_free_request
);
351 inline void __blk_mq_end_request(struct request
*rq
, int error
)
353 blk_account_io_done(rq
);
356 rq
->end_io(rq
, error
);
358 if (unlikely(blk_bidi_rq(rq
)))
359 blk_mq_free_request(rq
->next_rq
);
360 blk_mq_free_request(rq
);
363 EXPORT_SYMBOL(__blk_mq_end_request
);
365 void blk_mq_end_request(struct request
*rq
, int error
)
367 if (blk_update_request(rq
, error
, blk_rq_bytes(rq
)))
369 __blk_mq_end_request(rq
, error
);
371 EXPORT_SYMBOL(blk_mq_end_request
);
373 static void __blk_mq_complete_request_remote(void *data
)
375 struct request
*rq
= data
;
377 rq
->q
->softirq_done_fn(rq
);
380 static void blk_mq_ipi_complete_request(struct request
*rq
)
382 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
386 if (!test_bit(QUEUE_FLAG_SAME_COMP
, &rq
->q
->queue_flags
)) {
387 rq
->q
->softirq_done_fn(rq
);
392 if (!test_bit(QUEUE_FLAG_SAME_FORCE
, &rq
->q
->queue_flags
))
393 shared
= cpus_share_cache(cpu
, ctx
->cpu
);
395 if (cpu
!= ctx
->cpu
&& !shared
&& cpu_online(ctx
->cpu
)) {
396 rq
->csd
.func
= __blk_mq_complete_request_remote
;
399 smp_call_function_single_async(ctx
->cpu
, &rq
->csd
);
401 rq
->q
->softirq_done_fn(rq
);
406 static void __blk_mq_complete_request(struct request
*rq
)
408 struct request_queue
*q
= rq
->q
;
410 if (!q
->softirq_done_fn
)
411 blk_mq_end_request(rq
, rq
->errors
);
413 blk_mq_ipi_complete_request(rq
);
417 * blk_mq_complete_request - end I/O on a request
418 * @rq: the request being processed
421 * Ends all I/O on a request. It does not handle partial completions.
422 * The actual completion happens out-of-order, through a IPI handler.
424 void blk_mq_complete_request(struct request
*rq
, int error
)
426 struct request_queue
*q
= rq
->q
;
428 if (unlikely(blk_should_fake_timeout(q
)))
430 if (!blk_mark_rq_complete(rq
)) {
432 __blk_mq_complete_request(rq
);
435 EXPORT_SYMBOL(blk_mq_complete_request
);
437 int blk_mq_request_started(struct request
*rq
)
439 return test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
441 EXPORT_SYMBOL_GPL(blk_mq_request_started
);
443 void blk_mq_start_request(struct request
*rq
)
445 struct request_queue
*q
= rq
->q
;
447 trace_block_rq_issue(q
, rq
);
449 rq
->resid_len
= blk_rq_bytes(rq
);
450 if (unlikely(blk_bidi_rq(rq
)))
451 rq
->next_rq
->resid_len
= blk_rq_bytes(rq
->next_rq
);
456 * Ensure that ->deadline is visible before set the started
457 * flag and clear the completed flag.
459 smp_mb__before_atomic();
462 * Mark us as started and clear complete. Complete might have been
463 * set if requeue raced with timeout, which then marked it as
464 * complete. So be sure to clear complete again when we start
465 * the request, otherwise we'll ignore the completion event.
467 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
468 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
469 if (test_bit(REQ_ATOM_COMPLETE
, &rq
->atomic_flags
))
470 clear_bit(REQ_ATOM_COMPLETE
, &rq
->atomic_flags
);
472 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
474 * Make sure space for the drain appears. We know we can do
475 * this because max_hw_segments has been adjusted to be one
476 * fewer than the device can handle.
478 rq
->nr_phys_segments
++;
481 EXPORT_SYMBOL(blk_mq_start_request
);
483 static void __blk_mq_requeue_request(struct request
*rq
)
485 struct request_queue
*q
= rq
->q
;
487 trace_block_rq_requeue(q
, rq
);
489 if (test_and_clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
)) {
490 if (q
->dma_drain_size
&& blk_rq_bytes(rq
))
491 rq
->nr_phys_segments
--;
495 void blk_mq_requeue_request(struct request
*rq
, bool kick_requeue_list
)
497 __blk_mq_requeue_request(rq
);
499 BUG_ON(blk_queued_rq(rq
));
500 blk_mq_add_to_requeue_list(rq
, true, kick_requeue_list
);
502 EXPORT_SYMBOL(blk_mq_requeue_request
);
504 static void blk_mq_requeue_work(struct work_struct
*work
)
506 struct request_queue
*q
=
507 container_of(work
, struct request_queue
, requeue_work
.work
);
509 struct request
*rq
, *next
;
512 spin_lock_irqsave(&q
->requeue_lock
, flags
);
513 list_splice_init(&q
->requeue_list
, &rq_list
);
514 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
516 list_for_each_entry_safe(rq
, next
, &rq_list
, queuelist
) {
517 if (!(rq
->rq_flags
& RQF_SOFTBARRIER
))
520 rq
->rq_flags
&= ~RQF_SOFTBARRIER
;
521 list_del_init(&rq
->queuelist
);
522 blk_mq_insert_request(rq
, true, false, false);
525 while (!list_empty(&rq_list
)) {
526 rq
= list_entry(rq_list
.next
, struct request
, queuelist
);
527 list_del_init(&rq
->queuelist
);
528 blk_mq_insert_request(rq
, false, false, false);
531 blk_mq_run_hw_queues(q
, false);
534 void blk_mq_add_to_requeue_list(struct request
*rq
, bool at_head
,
535 bool kick_requeue_list
)
537 struct request_queue
*q
= rq
->q
;
541 * We abuse this flag that is otherwise used by the I/O scheduler to
542 * request head insertation from the workqueue.
544 BUG_ON(rq
->rq_flags
& RQF_SOFTBARRIER
);
546 spin_lock_irqsave(&q
->requeue_lock
, flags
);
548 rq
->rq_flags
|= RQF_SOFTBARRIER
;
549 list_add(&rq
->queuelist
, &q
->requeue_list
);
551 list_add_tail(&rq
->queuelist
, &q
->requeue_list
);
553 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
555 if (kick_requeue_list
)
556 blk_mq_kick_requeue_list(q
);
558 EXPORT_SYMBOL(blk_mq_add_to_requeue_list
);
560 void blk_mq_kick_requeue_list(struct request_queue
*q
)
562 kblockd_schedule_delayed_work(&q
->requeue_work
, 0);
564 EXPORT_SYMBOL(blk_mq_kick_requeue_list
);
566 void blk_mq_delay_kick_requeue_list(struct request_queue
*q
,
569 kblockd_schedule_delayed_work(&q
->requeue_work
,
570 msecs_to_jiffies(msecs
));
572 EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list
);
574 void blk_mq_abort_requeue_list(struct request_queue
*q
)
579 spin_lock_irqsave(&q
->requeue_lock
, flags
);
580 list_splice_init(&q
->requeue_list
, &rq_list
);
581 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
583 while (!list_empty(&rq_list
)) {
586 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
587 list_del_init(&rq
->queuelist
);
589 blk_mq_end_request(rq
, rq
->errors
);
592 EXPORT_SYMBOL(blk_mq_abort_requeue_list
);
594 struct request
*blk_mq_tag_to_rq(struct blk_mq_tags
*tags
, unsigned int tag
)
596 if (tag
< tags
->nr_tags
) {
597 prefetch(tags
->rqs
[tag
]);
598 return tags
->rqs
[tag
];
603 EXPORT_SYMBOL(blk_mq_tag_to_rq
);
605 struct blk_mq_timeout_data
{
607 unsigned int next_set
;
610 void blk_mq_rq_timed_out(struct request
*req
, bool reserved
)
612 struct blk_mq_ops
*ops
= req
->q
->mq_ops
;
613 enum blk_eh_timer_return ret
= BLK_EH_RESET_TIMER
;
616 * We know that complete is set at this point. If STARTED isn't set
617 * anymore, then the request isn't active and the "timeout" should
618 * just be ignored. This can happen due to the bitflag ordering.
619 * Timeout first checks if STARTED is set, and if it is, assumes
620 * the request is active. But if we race with completion, then
621 * we both flags will get cleared. So check here again, and ignore
622 * a timeout event with a request that isn't active.
624 if (!test_bit(REQ_ATOM_STARTED
, &req
->atomic_flags
))
628 ret
= ops
->timeout(req
, reserved
);
632 __blk_mq_complete_request(req
);
634 case BLK_EH_RESET_TIMER
:
636 blk_clear_rq_complete(req
);
638 case BLK_EH_NOT_HANDLED
:
641 printk(KERN_ERR
"block: bad eh return: %d\n", ret
);
646 static void blk_mq_check_expired(struct blk_mq_hw_ctx
*hctx
,
647 struct request
*rq
, void *priv
, bool reserved
)
649 struct blk_mq_timeout_data
*data
= priv
;
651 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
)) {
653 * If a request wasn't started before the queue was
654 * marked dying, kill it here or it'll go unnoticed.
656 if (unlikely(blk_queue_dying(rq
->q
))) {
658 blk_mq_end_request(rq
, rq
->errors
);
663 if (time_after_eq(jiffies
, rq
->deadline
)) {
664 if (!blk_mark_rq_complete(rq
))
665 blk_mq_rq_timed_out(rq
, reserved
);
666 } else if (!data
->next_set
|| time_after(data
->next
, rq
->deadline
)) {
667 data
->next
= rq
->deadline
;
672 static void blk_mq_timeout_work(struct work_struct
*work
)
674 struct request_queue
*q
=
675 container_of(work
, struct request_queue
, timeout_work
);
676 struct blk_mq_timeout_data data
= {
682 /* A deadlock might occur if a request is stuck requiring a
683 * timeout at the same time a queue freeze is waiting
684 * completion, since the timeout code would not be able to
685 * acquire the queue reference here.
687 * That's why we don't use blk_queue_enter here; instead, we use
688 * percpu_ref_tryget directly, because we need to be able to
689 * obtain a reference even in the short window between the queue
690 * starting to freeze, by dropping the first reference in
691 * blk_mq_freeze_queue_start, and the moment the last request is
692 * consumed, marked by the instant q_usage_counter reaches
695 if (!percpu_ref_tryget(&q
->q_usage_counter
))
698 blk_mq_queue_tag_busy_iter(q
, blk_mq_check_expired
, &data
);
701 data
.next
= blk_rq_timeout(round_jiffies_up(data
.next
));
702 mod_timer(&q
->timeout
, data
.next
);
704 struct blk_mq_hw_ctx
*hctx
;
706 queue_for_each_hw_ctx(q
, hctx
, i
) {
707 /* the hctx may be unmapped, so check it here */
708 if (blk_mq_hw_queue_mapped(hctx
))
709 blk_mq_tag_idle(hctx
);
716 * Reverse check our software queue for entries that we could potentially
717 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
718 * too much time checking for merges.
720 static bool blk_mq_attempt_merge(struct request_queue
*q
,
721 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
726 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
732 if (!blk_rq_merge_ok(rq
, bio
))
735 el_ret
= blk_try_merge(rq
, bio
);
736 if (el_ret
== ELEVATOR_BACK_MERGE
) {
737 if (bio_attempt_back_merge(q
, rq
, bio
)) {
742 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
743 if (bio_attempt_front_merge(q
, rq
, bio
)) {
754 struct flush_busy_ctx_data
{
755 struct blk_mq_hw_ctx
*hctx
;
756 struct list_head
*list
;
759 static bool flush_busy_ctx(struct sbitmap
*sb
, unsigned int bitnr
, void *data
)
761 struct flush_busy_ctx_data
*flush_data
= data
;
762 struct blk_mq_hw_ctx
*hctx
= flush_data
->hctx
;
763 struct blk_mq_ctx
*ctx
= hctx
->ctxs
[bitnr
];
765 sbitmap_clear_bit(sb
, bitnr
);
766 spin_lock(&ctx
->lock
);
767 list_splice_tail_init(&ctx
->rq_list
, flush_data
->list
);
768 spin_unlock(&ctx
->lock
);
773 * Process software queues that have been marked busy, splicing them
774 * to the for-dispatch
776 static void flush_busy_ctxs(struct blk_mq_hw_ctx
*hctx
, struct list_head
*list
)
778 struct flush_busy_ctx_data data
= {
783 sbitmap_for_each_set(&hctx
->ctx_map
, flush_busy_ctx
, &data
);
786 static inline unsigned int queued_to_index(unsigned int queued
)
791 return min(BLK_MQ_MAX_DISPATCH_ORDER
- 1, ilog2(queued
) + 1);
795 * Run this hardware queue, pulling any software queues mapped to it in.
796 * Note that this function currently has various problems around ordering
797 * of IO. In particular, we'd like FIFO behaviour on handling existing
798 * items on the hctx->dispatch list. Ignore that for now.
800 static void blk_mq_process_rq_list(struct blk_mq_hw_ctx
*hctx
)
802 struct request_queue
*q
= hctx
->queue
;
805 LIST_HEAD(driver_list
);
806 struct list_head
*dptr
;
809 if (unlikely(blk_mq_hctx_stopped(hctx
)))
815 * Touch any software queue that has pending entries.
817 flush_busy_ctxs(hctx
, &rq_list
);
820 * If we have previous entries on our dispatch list, grab them
821 * and stuff them at the front for more fair dispatch.
823 if (!list_empty_careful(&hctx
->dispatch
)) {
824 spin_lock(&hctx
->lock
);
825 if (!list_empty(&hctx
->dispatch
))
826 list_splice_init(&hctx
->dispatch
, &rq_list
);
827 spin_unlock(&hctx
->lock
);
831 * Start off with dptr being NULL, so we start the first request
832 * immediately, even if we have more pending.
837 * Now process all the entries, sending them to the driver.
840 while (!list_empty(&rq_list
)) {
841 struct blk_mq_queue_data bd
;
844 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
845 list_del_init(&rq
->queuelist
);
849 bd
.last
= list_empty(&rq_list
);
851 ret
= q
->mq_ops
->queue_rq(hctx
, &bd
);
853 case BLK_MQ_RQ_QUEUE_OK
:
856 case BLK_MQ_RQ_QUEUE_BUSY
:
857 list_add(&rq
->queuelist
, &rq_list
);
858 __blk_mq_requeue_request(rq
);
861 pr_err("blk-mq: bad return on queue: %d\n", ret
);
862 case BLK_MQ_RQ_QUEUE_ERROR
:
864 blk_mq_end_request(rq
, rq
->errors
);
868 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
872 * We've done the first request. If we have more than 1
873 * left in the list, set dptr to defer issue.
875 if (!dptr
&& rq_list
.next
!= rq_list
.prev
)
879 hctx
->dispatched
[queued_to_index(queued
)]++;
882 * Any items that need requeuing? Stuff them into hctx->dispatch,
883 * that is where we will continue on next queue run.
885 if (!list_empty(&rq_list
)) {
886 spin_lock(&hctx
->lock
);
887 list_splice(&rq_list
, &hctx
->dispatch
);
888 spin_unlock(&hctx
->lock
);
890 * the queue is expected stopped with BLK_MQ_RQ_QUEUE_BUSY, but
891 * it's possible the queue is stopped and restarted again
892 * before this. Queue restart will dispatch requests. And since
893 * requests in rq_list aren't added into hctx->dispatch yet,
894 * the requests in rq_list might get lost.
896 * blk_mq_run_hw_queue() already checks the STOPPED bit
898 blk_mq_run_hw_queue(hctx
, true);
902 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
906 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx
->cpumask
) &&
907 cpu_online(hctx
->next_cpu
));
909 if (!(hctx
->flags
& BLK_MQ_F_BLOCKING
)) {
911 blk_mq_process_rq_list(hctx
);
914 srcu_idx
= srcu_read_lock(&hctx
->queue_rq_srcu
);
915 blk_mq_process_rq_list(hctx
);
916 srcu_read_unlock(&hctx
->queue_rq_srcu
, srcu_idx
);
921 * It'd be great if the workqueue API had a way to pass
922 * in a mask and had some smarts for more clever placement.
923 * For now we just round-robin here, switching for every
924 * BLK_MQ_CPU_WORK_BATCH queued items.
926 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx
*hctx
)
928 if (hctx
->queue
->nr_hw_queues
== 1)
929 return WORK_CPU_UNBOUND
;
931 if (--hctx
->next_cpu_batch
<= 0) {
932 int cpu
= hctx
->next_cpu
, next_cpu
;
934 next_cpu
= cpumask_next(hctx
->next_cpu
, hctx
->cpumask
);
935 if (next_cpu
>= nr_cpu_ids
)
936 next_cpu
= cpumask_first(hctx
->cpumask
);
938 hctx
->next_cpu
= next_cpu
;
939 hctx
->next_cpu_batch
= BLK_MQ_CPU_WORK_BATCH
;
944 return hctx
->next_cpu
;
947 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
949 if (unlikely(blk_mq_hctx_stopped(hctx
) ||
950 !blk_mq_hw_queue_mapped(hctx
)))
953 if (!async
&& !(hctx
->flags
& BLK_MQ_F_BLOCKING
)) {
955 if (cpumask_test_cpu(cpu
, hctx
->cpumask
)) {
956 __blk_mq_run_hw_queue(hctx
);
964 kblockd_schedule_work_on(blk_mq_hctx_next_cpu(hctx
), &hctx
->run_work
);
967 void blk_mq_run_hw_queues(struct request_queue
*q
, bool async
)
969 struct blk_mq_hw_ctx
*hctx
;
972 queue_for_each_hw_ctx(q
, hctx
, i
) {
973 if ((!blk_mq_hctx_has_pending(hctx
) &&
974 list_empty_careful(&hctx
->dispatch
)) ||
975 blk_mq_hctx_stopped(hctx
))
978 blk_mq_run_hw_queue(hctx
, async
);
981 EXPORT_SYMBOL(blk_mq_run_hw_queues
);
984 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
987 * The caller is responsible for serializing this function against
988 * blk_mq_{start,stop}_hw_queue().
990 bool blk_mq_queue_stopped(struct request_queue
*q
)
992 struct blk_mq_hw_ctx
*hctx
;
995 queue_for_each_hw_ctx(q
, hctx
, i
)
996 if (blk_mq_hctx_stopped(hctx
))
1001 EXPORT_SYMBOL(blk_mq_queue_stopped
);
1003 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
1005 cancel_work(&hctx
->run_work
);
1006 cancel_delayed_work(&hctx
->delay_work
);
1007 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
1009 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
1011 void blk_mq_stop_hw_queues(struct request_queue
*q
)
1013 struct blk_mq_hw_ctx
*hctx
;
1016 queue_for_each_hw_ctx(q
, hctx
, i
)
1017 blk_mq_stop_hw_queue(hctx
);
1019 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
1021 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
1023 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
1025 blk_mq_run_hw_queue(hctx
, false);
1027 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
1029 void blk_mq_start_hw_queues(struct request_queue
*q
)
1031 struct blk_mq_hw_ctx
*hctx
;
1034 queue_for_each_hw_ctx(q
, hctx
, i
)
1035 blk_mq_start_hw_queue(hctx
);
1037 EXPORT_SYMBOL(blk_mq_start_hw_queues
);
1039 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
, bool async
)
1041 struct blk_mq_hw_ctx
*hctx
;
1044 queue_for_each_hw_ctx(q
, hctx
, i
) {
1045 if (!blk_mq_hctx_stopped(hctx
))
1048 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
1049 blk_mq_run_hw_queue(hctx
, async
);
1052 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
1054 static void blk_mq_run_work_fn(struct work_struct
*work
)
1056 struct blk_mq_hw_ctx
*hctx
;
1058 hctx
= container_of(work
, struct blk_mq_hw_ctx
, run_work
);
1060 __blk_mq_run_hw_queue(hctx
);
1063 static void blk_mq_delay_work_fn(struct work_struct
*work
)
1065 struct blk_mq_hw_ctx
*hctx
;
1067 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delay_work
.work
);
1069 if (test_and_clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
1070 __blk_mq_run_hw_queue(hctx
);
1073 void blk_mq_delay_queue(struct blk_mq_hw_ctx
*hctx
, unsigned long msecs
)
1075 if (unlikely(!blk_mq_hw_queue_mapped(hctx
)))
1078 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx
),
1079 &hctx
->delay_work
, msecs_to_jiffies(msecs
));
1081 EXPORT_SYMBOL(blk_mq_delay_queue
);
1083 static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx
*hctx
,
1087 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
1089 trace_block_rq_insert(hctx
->queue
, rq
);
1092 list_add(&rq
->queuelist
, &ctx
->rq_list
);
1094 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
1097 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
1098 struct request
*rq
, bool at_head
)
1100 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
1102 __blk_mq_insert_req_list(hctx
, rq
, at_head
);
1103 blk_mq_hctx_mark_pending(hctx
, ctx
);
1106 void blk_mq_insert_request(struct request
*rq
, bool at_head
, bool run_queue
,
1109 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
1110 struct request_queue
*q
= rq
->q
;
1111 struct blk_mq_hw_ctx
*hctx
= blk_mq_map_queue(q
, ctx
->cpu
);
1113 spin_lock(&ctx
->lock
);
1114 __blk_mq_insert_request(hctx
, rq
, at_head
);
1115 spin_unlock(&ctx
->lock
);
1118 blk_mq_run_hw_queue(hctx
, async
);
1121 static void blk_mq_insert_requests(struct request_queue
*q
,
1122 struct blk_mq_ctx
*ctx
,
1123 struct list_head
*list
,
1128 struct blk_mq_hw_ctx
*hctx
= blk_mq_map_queue(q
, ctx
->cpu
);
1130 trace_block_unplug(q
, depth
, !from_schedule
);
1133 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1136 spin_lock(&ctx
->lock
);
1137 while (!list_empty(list
)) {
1140 rq
= list_first_entry(list
, struct request
, queuelist
);
1141 BUG_ON(rq
->mq_ctx
!= ctx
);
1142 list_del_init(&rq
->queuelist
);
1143 __blk_mq_insert_req_list(hctx
, rq
, false);
1145 blk_mq_hctx_mark_pending(hctx
, ctx
);
1146 spin_unlock(&ctx
->lock
);
1148 blk_mq_run_hw_queue(hctx
, from_schedule
);
1151 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1153 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
1154 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
1156 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
1157 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
1158 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
1161 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
1163 struct blk_mq_ctx
*this_ctx
;
1164 struct request_queue
*this_q
;
1167 LIST_HEAD(ctx_list
);
1170 list_splice_init(&plug
->mq_list
, &list
);
1172 list_sort(NULL
, &list
, plug_ctx_cmp
);
1178 while (!list_empty(&list
)) {
1179 rq
= list_entry_rq(list
.next
);
1180 list_del_init(&rq
->queuelist
);
1182 if (rq
->mq_ctx
!= this_ctx
) {
1184 blk_mq_insert_requests(this_q
, this_ctx
,
1189 this_ctx
= rq
->mq_ctx
;
1195 list_add_tail(&rq
->queuelist
, &ctx_list
);
1199 * If 'this_ctx' is set, we know we have entries to complete
1200 * on 'ctx_list'. Do those.
1203 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
1208 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
1210 init_request_from_bio(rq
, bio
);
1212 blk_account_io_start(rq
, 1);
1215 static inline bool hctx_allow_merges(struct blk_mq_hw_ctx
*hctx
)
1217 return (hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
) &&
1218 !blk_queue_nomerges(hctx
->queue
);
1221 static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx
*hctx
,
1222 struct blk_mq_ctx
*ctx
,
1223 struct request
*rq
, struct bio
*bio
)
1225 if (!hctx_allow_merges(hctx
) || !bio_mergeable(bio
)) {
1226 blk_mq_bio_to_request(rq
, bio
);
1227 spin_lock(&ctx
->lock
);
1229 __blk_mq_insert_request(hctx
, rq
, false);
1230 spin_unlock(&ctx
->lock
);
1233 struct request_queue
*q
= hctx
->queue
;
1235 spin_lock(&ctx
->lock
);
1236 if (!blk_mq_attempt_merge(q
, ctx
, bio
)) {
1237 blk_mq_bio_to_request(rq
, bio
);
1241 spin_unlock(&ctx
->lock
);
1242 __blk_mq_free_request(hctx
, ctx
, rq
);
1247 static struct request
*blk_mq_map_request(struct request_queue
*q
,
1249 struct blk_mq_alloc_data
*data
)
1251 struct blk_mq_hw_ctx
*hctx
;
1252 struct blk_mq_ctx
*ctx
;
1255 blk_queue_enter_live(q
);
1256 ctx
= blk_mq_get_ctx(q
);
1257 hctx
= blk_mq_map_queue(q
, ctx
->cpu
);
1259 trace_block_getrq(q
, bio
, bio
->bi_opf
);
1260 blk_mq_set_alloc_data(data
, q
, 0, ctx
, hctx
);
1261 rq
= __blk_mq_alloc_request(data
, bio
->bi_opf
);
1263 data
->hctx
->queued
++;
1267 static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx
*hctx
,
1268 struct request
*rq
, blk_qc_t
*cookie
)
1271 struct request_queue
*q
= rq
->q
;
1272 struct blk_mq_queue_data bd
= {
1277 blk_qc_t new_cookie
= blk_tag_to_qc_t(rq
->tag
, hctx
->queue_num
);
1279 if (blk_mq_hctx_stopped(hctx
))
1283 * For OK queue, we are done. For error, kill it. Any other
1284 * error (busy), just add it to our list as we previously
1287 ret
= q
->mq_ops
->queue_rq(hctx
, &bd
);
1288 if (ret
== BLK_MQ_RQ_QUEUE_OK
) {
1289 *cookie
= new_cookie
;
1293 __blk_mq_requeue_request(rq
);
1295 if (ret
== BLK_MQ_RQ_QUEUE_ERROR
) {
1296 *cookie
= BLK_QC_T_NONE
;
1298 blk_mq_end_request(rq
, rq
->errors
);
1303 blk_mq_insert_request(rq
, false, true, true);
1307 * Multiple hardware queue variant. This will not use per-process plugs,
1308 * but will attempt to bypass the hctx queueing if we can go straight to
1309 * hardware for SYNC IO.
1311 static blk_qc_t
blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
1313 const int is_sync
= op_is_sync(bio
->bi_opf
);
1314 const int is_flush_fua
= bio
->bi_opf
& (REQ_PREFLUSH
| REQ_FUA
);
1315 struct blk_mq_alloc_data data
;
1317 unsigned int request_count
= 0, srcu_idx
;
1318 struct blk_plug
*plug
;
1319 struct request
*same_queue_rq
= NULL
;
1322 blk_queue_bounce(q
, &bio
);
1324 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1326 return BLK_QC_T_NONE
;
1329 blk_queue_split(q
, &bio
, q
->bio_split
);
1331 if (!is_flush_fua
&& !blk_queue_nomerges(q
) &&
1332 blk_attempt_plug_merge(q
, bio
, &request_count
, &same_queue_rq
))
1333 return BLK_QC_T_NONE
;
1335 rq
= blk_mq_map_request(q
, bio
, &data
);
1337 return BLK_QC_T_NONE
;
1339 cookie
= blk_tag_to_qc_t(rq
->tag
, data
.hctx
->queue_num
);
1341 if (unlikely(is_flush_fua
)) {
1342 blk_mq_bio_to_request(rq
, bio
);
1343 blk_insert_flush(rq
);
1347 plug
= current
->plug
;
1349 * If the driver supports defer issued based on 'last', then
1350 * queue it up like normal since we can potentially save some
1353 if (((plug
&& !blk_queue_nomerges(q
)) || is_sync
) &&
1354 !(data
.hctx
->flags
& BLK_MQ_F_DEFER_ISSUE
)) {
1355 struct request
*old_rq
= NULL
;
1357 blk_mq_bio_to_request(rq
, bio
);
1360 * We do limited plugging. If the bio can be merged, do that.
1361 * Otherwise the existing request in the plug list will be
1362 * issued. So the plug list will have one request at most
1366 * The plug list might get flushed before this. If that
1367 * happens, same_queue_rq is invalid and plug list is
1370 if (same_queue_rq
&& !list_empty(&plug
->mq_list
)) {
1371 old_rq
= same_queue_rq
;
1372 list_del_init(&old_rq
->queuelist
);
1374 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
1375 } else /* is_sync */
1377 blk_mq_put_ctx(data
.ctx
);
1381 if (!(data
.hctx
->flags
& BLK_MQ_F_BLOCKING
)) {
1383 blk_mq_try_issue_directly(data
.hctx
, old_rq
, &cookie
);
1386 srcu_idx
= srcu_read_lock(&data
.hctx
->queue_rq_srcu
);
1387 blk_mq_try_issue_directly(data
.hctx
, old_rq
, &cookie
);
1388 srcu_read_unlock(&data
.hctx
->queue_rq_srcu
, srcu_idx
);
1393 if (!blk_mq_merge_queue_io(data
.hctx
, data
.ctx
, rq
, bio
)) {
1395 * For a SYNC request, send it to the hardware immediately. For
1396 * an ASYNC request, just ensure that we run it later on. The
1397 * latter allows for merging opportunities and more efficient
1401 blk_mq_run_hw_queue(data
.hctx
, !is_sync
|| is_flush_fua
);
1403 blk_mq_put_ctx(data
.ctx
);
1409 * Single hardware queue variant. This will attempt to use any per-process
1410 * plug for merging and IO deferral.
1412 static blk_qc_t
blk_sq_make_request(struct request_queue
*q
, struct bio
*bio
)
1414 const int is_sync
= op_is_sync(bio
->bi_opf
);
1415 const int is_flush_fua
= bio
->bi_opf
& (REQ_PREFLUSH
| REQ_FUA
);
1416 struct blk_plug
*plug
;
1417 unsigned int request_count
= 0;
1418 struct blk_mq_alloc_data data
;
1422 blk_queue_bounce(q
, &bio
);
1424 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1426 return BLK_QC_T_NONE
;
1429 blk_queue_split(q
, &bio
, q
->bio_split
);
1431 if (!is_flush_fua
&& !blk_queue_nomerges(q
)) {
1432 if (blk_attempt_plug_merge(q
, bio
, &request_count
, NULL
))
1433 return BLK_QC_T_NONE
;
1435 request_count
= blk_plug_queued_count(q
);
1437 rq
= blk_mq_map_request(q
, bio
, &data
);
1439 return BLK_QC_T_NONE
;
1441 cookie
= blk_tag_to_qc_t(rq
->tag
, data
.hctx
->queue_num
);
1443 if (unlikely(is_flush_fua
)) {
1444 blk_mq_bio_to_request(rq
, bio
);
1445 blk_insert_flush(rq
);
1450 * A task plug currently exists. Since this is completely lockless,
1451 * utilize that to temporarily store requests until the task is
1452 * either done or scheduled away.
1454 plug
= current
->plug
;
1456 blk_mq_bio_to_request(rq
, bio
);
1458 trace_block_plug(q
);
1460 blk_mq_put_ctx(data
.ctx
);
1462 if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
1463 blk_flush_plug_list(plug
, false);
1464 trace_block_plug(q
);
1467 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
1471 if (!blk_mq_merge_queue_io(data
.hctx
, data
.ctx
, rq
, bio
)) {
1473 * For a SYNC request, send it to the hardware immediately. For
1474 * an ASYNC request, just ensure that we run it later on. The
1475 * latter allows for merging opportunities and more efficient
1479 blk_mq_run_hw_queue(data
.hctx
, !is_sync
|| is_flush_fua
);
1482 blk_mq_put_ctx(data
.ctx
);
1486 static void blk_mq_free_rq_map(struct blk_mq_tag_set
*set
,
1487 struct blk_mq_tags
*tags
, unsigned int hctx_idx
)
1491 if (tags
->rqs
&& set
->ops
->exit_request
) {
1494 for (i
= 0; i
< tags
->nr_tags
; i
++) {
1497 set
->ops
->exit_request(set
->driver_data
, tags
->rqs
[i
],
1499 tags
->rqs
[i
] = NULL
;
1503 while (!list_empty(&tags
->page_list
)) {
1504 page
= list_first_entry(&tags
->page_list
, struct page
, lru
);
1505 list_del_init(&page
->lru
);
1507 * Remove kmemleak object previously allocated in
1508 * blk_mq_init_rq_map().
1510 kmemleak_free(page_address(page
));
1511 __free_pages(page
, page
->private);
1516 blk_mq_free_tags(tags
);
1519 static size_t order_to_size(unsigned int order
)
1521 return (size_t)PAGE_SIZE
<< order
;
1524 static struct blk_mq_tags
*blk_mq_init_rq_map(struct blk_mq_tag_set
*set
,
1525 unsigned int hctx_idx
)
1527 struct blk_mq_tags
*tags
;
1528 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1529 size_t rq_size
, left
;
1531 tags
= blk_mq_init_tags(set
->queue_depth
, set
->reserved_tags
,
1533 BLK_MQ_FLAG_TO_ALLOC_POLICY(set
->flags
));
1537 INIT_LIST_HEAD(&tags
->page_list
);
1539 tags
->rqs
= kzalloc_node(set
->queue_depth
* sizeof(struct request
*),
1540 GFP_KERNEL
| __GFP_NOWARN
| __GFP_NORETRY
,
1543 blk_mq_free_tags(tags
);
1548 * rq_size is the size of the request plus driver payload, rounded
1549 * to the cacheline size
1551 rq_size
= round_up(sizeof(struct request
) + set
->cmd_size
,
1553 left
= rq_size
* set
->queue_depth
;
1555 for (i
= 0; i
< set
->queue_depth
; ) {
1556 int this_order
= max_order
;
1561 while (this_order
&& left
< order_to_size(this_order
- 1))
1565 page
= alloc_pages_node(set
->numa_node
,
1566 GFP_KERNEL
| __GFP_NOWARN
| __GFP_NORETRY
| __GFP_ZERO
,
1572 if (order_to_size(this_order
) < rq_size
)
1579 page
->private = this_order
;
1580 list_add_tail(&page
->lru
, &tags
->page_list
);
1582 p
= page_address(page
);
1584 * Allow kmemleak to scan these pages as they contain pointers
1585 * to additional allocations like via ops->init_request().
1587 kmemleak_alloc(p
, order_to_size(this_order
), 1, GFP_KERNEL
);
1588 entries_per_page
= order_to_size(this_order
) / rq_size
;
1589 to_do
= min(entries_per_page
, set
->queue_depth
- i
);
1590 left
-= to_do
* rq_size
;
1591 for (j
= 0; j
< to_do
; j
++) {
1593 if (set
->ops
->init_request
) {
1594 if (set
->ops
->init_request(set
->driver_data
,
1595 tags
->rqs
[i
], hctx_idx
, i
,
1597 tags
->rqs
[i
] = NULL
;
1609 blk_mq_free_rq_map(set
, tags
, hctx_idx
);
1614 * 'cpu' is going away. splice any existing rq_list entries from this
1615 * software queue to the hw queue dispatch list, and ensure that it
1618 static int blk_mq_hctx_notify_dead(unsigned int cpu
, struct hlist_node
*node
)
1620 struct blk_mq_hw_ctx
*hctx
;
1621 struct blk_mq_ctx
*ctx
;
1624 hctx
= hlist_entry_safe(node
, struct blk_mq_hw_ctx
, cpuhp_dead
);
1625 ctx
= __blk_mq_get_ctx(hctx
->queue
, cpu
);
1627 spin_lock(&ctx
->lock
);
1628 if (!list_empty(&ctx
->rq_list
)) {
1629 list_splice_init(&ctx
->rq_list
, &tmp
);
1630 blk_mq_hctx_clear_pending(hctx
, ctx
);
1632 spin_unlock(&ctx
->lock
);
1634 if (list_empty(&tmp
))
1637 spin_lock(&hctx
->lock
);
1638 list_splice_tail_init(&tmp
, &hctx
->dispatch
);
1639 spin_unlock(&hctx
->lock
);
1641 blk_mq_run_hw_queue(hctx
, true);
1645 static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx
*hctx
)
1647 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD
,
1651 /* hctx->ctxs will be freed in queue's release handler */
1652 static void blk_mq_exit_hctx(struct request_queue
*q
,
1653 struct blk_mq_tag_set
*set
,
1654 struct blk_mq_hw_ctx
*hctx
, unsigned int hctx_idx
)
1656 unsigned flush_start_tag
= set
->queue_depth
;
1658 blk_mq_tag_idle(hctx
);
1660 if (set
->ops
->exit_request
)
1661 set
->ops
->exit_request(set
->driver_data
,
1662 hctx
->fq
->flush_rq
, hctx_idx
,
1663 flush_start_tag
+ hctx_idx
);
1665 if (set
->ops
->exit_hctx
)
1666 set
->ops
->exit_hctx(hctx
, hctx_idx
);
1668 if (hctx
->flags
& BLK_MQ_F_BLOCKING
)
1669 cleanup_srcu_struct(&hctx
->queue_rq_srcu
);
1671 blk_mq_remove_cpuhp(hctx
);
1672 blk_free_flush_queue(hctx
->fq
);
1673 sbitmap_free(&hctx
->ctx_map
);
1676 static void blk_mq_exit_hw_queues(struct request_queue
*q
,
1677 struct blk_mq_tag_set
*set
, int nr_queue
)
1679 struct blk_mq_hw_ctx
*hctx
;
1682 queue_for_each_hw_ctx(q
, hctx
, i
) {
1685 blk_mq_exit_hctx(q
, set
, hctx
, i
);
1689 static void blk_mq_free_hw_queues(struct request_queue
*q
,
1690 struct blk_mq_tag_set
*set
)
1692 struct blk_mq_hw_ctx
*hctx
;
1695 queue_for_each_hw_ctx(q
, hctx
, i
)
1696 free_cpumask_var(hctx
->cpumask
);
1699 static int blk_mq_init_hctx(struct request_queue
*q
,
1700 struct blk_mq_tag_set
*set
,
1701 struct blk_mq_hw_ctx
*hctx
, unsigned hctx_idx
)
1704 unsigned flush_start_tag
= set
->queue_depth
;
1706 node
= hctx
->numa_node
;
1707 if (node
== NUMA_NO_NODE
)
1708 node
= hctx
->numa_node
= set
->numa_node
;
1710 INIT_WORK(&hctx
->run_work
, blk_mq_run_work_fn
);
1711 INIT_DELAYED_WORK(&hctx
->delay_work
, blk_mq_delay_work_fn
);
1712 spin_lock_init(&hctx
->lock
);
1713 INIT_LIST_HEAD(&hctx
->dispatch
);
1715 hctx
->queue_num
= hctx_idx
;
1716 hctx
->flags
= set
->flags
& ~BLK_MQ_F_TAG_SHARED
;
1718 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD
, &hctx
->cpuhp_dead
);
1720 hctx
->tags
= set
->tags
[hctx_idx
];
1723 * Allocate space for all possible cpus to avoid allocation at
1726 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1729 goto unregister_cpu_notifier
;
1731 if (sbitmap_init_node(&hctx
->ctx_map
, nr_cpu_ids
, ilog2(8), GFP_KERNEL
,
1737 if (set
->ops
->init_hctx
&&
1738 set
->ops
->init_hctx(hctx
, set
->driver_data
, hctx_idx
))
1741 hctx
->fq
= blk_alloc_flush_queue(q
, hctx
->numa_node
, set
->cmd_size
);
1745 if (set
->ops
->init_request
&&
1746 set
->ops
->init_request(set
->driver_data
,
1747 hctx
->fq
->flush_rq
, hctx_idx
,
1748 flush_start_tag
+ hctx_idx
, node
))
1751 if (hctx
->flags
& BLK_MQ_F_BLOCKING
)
1752 init_srcu_struct(&hctx
->queue_rq_srcu
);
1759 if (set
->ops
->exit_hctx
)
1760 set
->ops
->exit_hctx(hctx
, hctx_idx
);
1762 sbitmap_free(&hctx
->ctx_map
);
1765 unregister_cpu_notifier
:
1766 blk_mq_remove_cpuhp(hctx
);
1770 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1771 unsigned int nr_hw_queues
)
1775 for_each_possible_cpu(i
) {
1776 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1777 struct blk_mq_hw_ctx
*hctx
;
1779 memset(__ctx
, 0, sizeof(*__ctx
));
1781 spin_lock_init(&__ctx
->lock
);
1782 INIT_LIST_HEAD(&__ctx
->rq_list
);
1785 /* If the cpu isn't online, the cpu is mapped to first hctx */
1789 hctx
= blk_mq_map_queue(q
, i
);
1792 * Set local node, IFF we have more than one hw queue. If
1793 * not, we remain on the home node of the device
1795 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1796 hctx
->numa_node
= local_memory_node(cpu_to_node(i
));
1800 static void blk_mq_map_swqueue(struct request_queue
*q
,
1801 const struct cpumask
*online_mask
)
1804 struct blk_mq_hw_ctx
*hctx
;
1805 struct blk_mq_ctx
*ctx
;
1806 struct blk_mq_tag_set
*set
= q
->tag_set
;
1809 * Avoid others reading imcomplete hctx->cpumask through sysfs
1811 mutex_lock(&q
->sysfs_lock
);
1813 queue_for_each_hw_ctx(q
, hctx
, i
) {
1814 cpumask_clear(hctx
->cpumask
);
1819 * Map software to hardware queues
1821 for_each_possible_cpu(i
) {
1822 /* If the cpu isn't online, the cpu is mapped to first hctx */
1823 if (!cpumask_test_cpu(i
, online_mask
))
1826 ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1827 hctx
= blk_mq_map_queue(q
, i
);
1829 cpumask_set_cpu(i
, hctx
->cpumask
);
1830 ctx
->index_hw
= hctx
->nr_ctx
;
1831 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1834 mutex_unlock(&q
->sysfs_lock
);
1836 queue_for_each_hw_ctx(q
, hctx
, i
) {
1838 * If no software queues are mapped to this hardware queue,
1839 * disable it and free the request entries.
1841 if (!hctx
->nr_ctx
) {
1843 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
1844 set
->tags
[i
] = NULL
;
1850 /* unmapped hw queue can be remapped after CPU topo changed */
1852 set
->tags
[i
] = blk_mq_init_rq_map(set
, i
);
1853 hctx
->tags
= set
->tags
[i
];
1854 WARN_ON(!hctx
->tags
);
1857 * Set the map size to the number of mapped software queues.
1858 * This is more accurate and more efficient than looping
1859 * over all possibly mapped software queues.
1861 sbitmap_resize(&hctx
->ctx_map
, hctx
->nr_ctx
);
1864 * Initialize batch roundrobin counts
1866 hctx
->next_cpu
= cpumask_first(hctx
->cpumask
);
1867 hctx
->next_cpu_batch
= BLK_MQ_CPU_WORK_BATCH
;
1871 static void queue_set_hctx_shared(struct request_queue
*q
, bool shared
)
1873 struct blk_mq_hw_ctx
*hctx
;
1876 queue_for_each_hw_ctx(q
, hctx
, i
) {
1878 hctx
->flags
|= BLK_MQ_F_TAG_SHARED
;
1880 hctx
->flags
&= ~BLK_MQ_F_TAG_SHARED
;
1884 static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set
*set
, bool shared
)
1886 struct request_queue
*q
;
1888 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
) {
1889 blk_mq_freeze_queue(q
);
1890 queue_set_hctx_shared(q
, shared
);
1891 blk_mq_unfreeze_queue(q
);
1895 static void blk_mq_del_queue_tag_set(struct request_queue
*q
)
1897 struct blk_mq_tag_set
*set
= q
->tag_set
;
1899 mutex_lock(&set
->tag_list_lock
);
1900 list_del_init(&q
->tag_set_list
);
1901 if (list_is_singular(&set
->tag_list
)) {
1902 /* just transitioned to unshared */
1903 set
->flags
&= ~BLK_MQ_F_TAG_SHARED
;
1904 /* update existing queue */
1905 blk_mq_update_tag_set_depth(set
, false);
1907 mutex_unlock(&set
->tag_list_lock
);
1910 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set
*set
,
1911 struct request_queue
*q
)
1915 mutex_lock(&set
->tag_list_lock
);
1917 /* Check to see if we're transitioning to shared (from 1 to 2 queues). */
1918 if (!list_empty(&set
->tag_list
) && !(set
->flags
& BLK_MQ_F_TAG_SHARED
)) {
1919 set
->flags
|= BLK_MQ_F_TAG_SHARED
;
1920 /* update existing queue */
1921 blk_mq_update_tag_set_depth(set
, true);
1923 if (set
->flags
& BLK_MQ_F_TAG_SHARED
)
1924 queue_set_hctx_shared(q
, true);
1925 list_add_tail(&q
->tag_set_list
, &set
->tag_list
);
1927 mutex_unlock(&set
->tag_list_lock
);
1931 * It is the actual release handler for mq, but we do it from
1932 * request queue's release handler for avoiding use-after-free
1933 * and headache because q->mq_kobj shouldn't have been introduced,
1934 * but we can't group ctx/kctx kobj without it.
1936 void blk_mq_release(struct request_queue
*q
)
1938 struct blk_mq_hw_ctx
*hctx
;
1941 /* hctx kobj stays in hctx */
1942 queue_for_each_hw_ctx(q
, hctx
, i
) {
1951 kfree(q
->queue_hw_ctx
);
1953 /* ctx kobj stays in queue_ctx */
1954 free_percpu(q
->queue_ctx
);
1957 struct request_queue
*blk_mq_init_queue(struct blk_mq_tag_set
*set
)
1959 struct request_queue
*uninit_q
, *q
;
1961 uninit_q
= blk_alloc_queue_node(GFP_KERNEL
, set
->numa_node
);
1963 return ERR_PTR(-ENOMEM
);
1965 q
= blk_mq_init_allocated_queue(set
, uninit_q
);
1967 blk_cleanup_queue(uninit_q
);
1971 EXPORT_SYMBOL(blk_mq_init_queue
);
1973 static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set
*set
,
1974 struct request_queue
*q
)
1977 struct blk_mq_hw_ctx
**hctxs
= q
->queue_hw_ctx
;
1979 blk_mq_sysfs_unregister(q
);
1980 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1986 node
= blk_mq_hw_queue_to_node(q
->mq_map
, i
);
1987 hctxs
[i
] = kzalloc_node(sizeof(struct blk_mq_hw_ctx
),
1992 if (!zalloc_cpumask_var_node(&hctxs
[i
]->cpumask
, GFP_KERNEL
,
1999 atomic_set(&hctxs
[i
]->nr_active
, 0);
2000 hctxs
[i
]->numa_node
= node
;
2001 hctxs
[i
]->queue_num
= i
;
2003 if (blk_mq_init_hctx(q
, set
, hctxs
[i
], i
)) {
2004 free_cpumask_var(hctxs
[i
]->cpumask
);
2009 blk_mq_hctx_kobj_init(hctxs
[i
]);
2011 for (j
= i
; j
< q
->nr_hw_queues
; j
++) {
2012 struct blk_mq_hw_ctx
*hctx
= hctxs
[j
];
2016 blk_mq_free_rq_map(set
, hctx
->tags
, j
);
2017 set
->tags
[j
] = NULL
;
2019 blk_mq_exit_hctx(q
, set
, hctx
, j
);
2020 free_cpumask_var(hctx
->cpumask
);
2021 kobject_put(&hctx
->kobj
);
2028 q
->nr_hw_queues
= i
;
2029 blk_mq_sysfs_register(q
);
2032 struct request_queue
*blk_mq_init_allocated_queue(struct blk_mq_tag_set
*set
,
2033 struct request_queue
*q
)
2035 /* mark the queue as mq asap */
2036 q
->mq_ops
= set
->ops
;
2038 q
->queue_ctx
= alloc_percpu(struct blk_mq_ctx
);
2042 q
->queue_hw_ctx
= kzalloc_node(nr_cpu_ids
* sizeof(*(q
->queue_hw_ctx
)),
2043 GFP_KERNEL
, set
->numa_node
);
2044 if (!q
->queue_hw_ctx
)
2047 q
->mq_map
= set
->mq_map
;
2049 blk_mq_realloc_hw_ctxs(set
, q
);
2050 if (!q
->nr_hw_queues
)
2053 INIT_WORK(&q
->timeout_work
, blk_mq_timeout_work
);
2054 blk_queue_rq_timeout(q
, set
->timeout
? set
->timeout
: 30 * HZ
);
2056 q
->nr_queues
= nr_cpu_ids
;
2058 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
2060 if (!(set
->flags
& BLK_MQ_F_SG_MERGE
))
2061 q
->queue_flags
|= 1 << QUEUE_FLAG_NO_SG_MERGE
;
2063 q
->sg_reserved_size
= INT_MAX
;
2065 INIT_DELAYED_WORK(&q
->requeue_work
, blk_mq_requeue_work
);
2066 INIT_LIST_HEAD(&q
->requeue_list
);
2067 spin_lock_init(&q
->requeue_lock
);
2069 if (q
->nr_hw_queues
> 1)
2070 blk_queue_make_request(q
, blk_mq_make_request
);
2072 blk_queue_make_request(q
, blk_sq_make_request
);
2075 * Do this after blk_queue_make_request() overrides it...
2077 q
->nr_requests
= set
->queue_depth
;
2079 if (set
->ops
->complete
)
2080 blk_queue_softirq_done(q
, set
->ops
->complete
);
2082 blk_mq_init_cpu_queues(q
, set
->nr_hw_queues
);
2085 mutex_lock(&all_q_mutex
);
2087 list_add_tail(&q
->all_q_node
, &all_q_list
);
2088 blk_mq_add_queue_tag_set(set
, q
);
2089 blk_mq_map_swqueue(q
, cpu_online_mask
);
2091 mutex_unlock(&all_q_mutex
);
2097 kfree(q
->queue_hw_ctx
);
2099 free_percpu(q
->queue_ctx
);
2102 return ERR_PTR(-ENOMEM
);
2104 EXPORT_SYMBOL(blk_mq_init_allocated_queue
);
2106 void blk_mq_free_queue(struct request_queue
*q
)
2108 struct blk_mq_tag_set
*set
= q
->tag_set
;
2110 mutex_lock(&all_q_mutex
);
2111 list_del_init(&q
->all_q_node
);
2112 mutex_unlock(&all_q_mutex
);
2114 blk_mq_del_queue_tag_set(q
);
2116 blk_mq_exit_hw_queues(q
, set
, set
->nr_hw_queues
);
2117 blk_mq_free_hw_queues(q
, set
);
2120 /* Basically redo blk_mq_init_queue with queue frozen */
2121 static void blk_mq_queue_reinit(struct request_queue
*q
,
2122 const struct cpumask
*online_mask
)
2124 WARN_ON_ONCE(!atomic_read(&q
->mq_freeze_depth
));
2126 blk_mq_sysfs_unregister(q
);
2129 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
2130 * we should change hctx numa_node according to new topology (this
2131 * involves free and re-allocate memory, worthy doing?)
2134 blk_mq_map_swqueue(q
, online_mask
);
2136 blk_mq_sysfs_register(q
);
2140 * New online cpumask which is going to be set in this hotplug event.
2141 * Declare this cpumasks as global as cpu-hotplug operation is invoked
2142 * one-by-one and dynamically allocating this could result in a failure.
2144 static struct cpumask cpuhp_online_new
;
2146 static void blk_mq_queue_reinit_work(void)
2148 struct request_queue
*q
;
2150 mutex_lock(&all_q_mutex
);
2152 * We need to freeze and reinit all existing queues. Freezing
2153 * involves synchronous wait for an RCU grace period and doing it
2154 * one by one may take a long time. Start freezing all queues in
2155 * one swoop and then wait for the completions so that freezing can
2156 * take place in parallel.
2158 list_for_each_entry(q
, &all_q_list
, all_q_node
)
2159 blk_mq_freeze_queue_start(q
);
2160 list_for_each_entry(q
, &all_q_list
, all_q_node
) {
2161 blk_mq_freeze_queue_wait(q
);
2164 * timeout handler can't touch hw queue during the
2167 del_timer_sync(&q
->timeout
);
2170 list_for_each_entry(q
, &all_q_list
, all_q_node
)
2171 blk_mq_queue_reinit(q
, &cpuhp_online_new
);
2173 list_for_each_entry(q
, &all_q_list
, all_q_node
)
2174 blk_mq_unfreeze_queue(q
);
2176 mutex_unlock(&all_q_mutex
);
2179 static int blk_mq_queue_reinit_dead(unsigned int cpu
)
2181 cpumask_copy(&cpuhp_online_new
, cpu_online_mask
);
2182 blk_mq_queue_reinit_work();
2187 * Before hotadded cpu starts handling requests, new mappings must be
2188 * established. Otherwise, these requests in hw queue might never be
2191 * For example, there is a single hw queue (hctx) and two CPU queues (ctx0
2192 * for CPU0, and ctx1 for CPU1).
2194 * Now CPU1 is just onlined and a request is inserted into ctx1->rq_list
2195 * and set bit0 in pending bitmap as ctx1->index_hw is still zero.
2197 * And then while running hw queue, flush_busy_ctxs() finds bit0 is set in
2198 * pending bitmap and tries to retrieve requests in hctx->ctxs[0]->rq_list.
2199 * But htx->ctxs[0] is a pointer to ctx0, so the request in ctx1->rq_list
2202 static int blk_mq_queue_reinit_prepare(unsigned int cpu
)
2204 cpumask_copy(&cpuhp_online_new
, cpu_online_mask
);
2205 cpumask_set_cpu(cpu
, &cpuhp_online_new
);
2206 blk_mq_queue_reinit_work();
2210 static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set
*set
)
2214 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
2215 set
->tags
[i
] = blk_mq_init_rq_map(set
, i
);
2224 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
2230 * Allocate the request maps associated with this tag_set. Note that this
2231 * may reduce the depth asked for, if memory is tight. set->queue_depth
2232 * will be updated to reflect the allocated depth.
2234 static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set
*set
)
2239 depth
= set
->queue_depth
;
2241 err
= __blk_mq_alloc_rq_maps(set
);
2245 set
->queue_depth
>>= 1;
2246 if (set
->queue_depth
< set
->reserved_tags
+ BLK_MQ_TAG_MIN
) {
2250 } while (set
->queue_depth
);
2252 if (!set
->queue_depth
|| err
) {
2253 pr_err("blk-mq: failed to allocate request map\n");
2257 if (depth
!= set
->queue_depth
)
2258 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2259 depth
, set
->queue_depth
);
2265 * Alloc a tag set to be associated with one or more request queues.
2266 * May fail with EINVAL for various error conditions. May adjust the
2267 * requested depth down, if if it too large. In that case, the set
2268 * value will be stored in set->queue_depth.
2270 int blk_mq_alloc_tag_set(struct blk_mq_tag_set
*set
)
2274 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH
> 1 << BLK_MQ_UNIQUE_TAG_BITS
);
2276 if (!set
->nr_hw_queues
)
2278 if (!set
->queue_depth
)
2280 if (set
->queue_depth
< set
->reserved_tags
+ BLK_MQ_TAG_MIN
)
2283 if (!set
->ops
->queue_rq
)
2286 if (set
->queue_depth
> BLK_MQ_MAX_DEPTH
) {
2287 pr_info("blk-mq: reduced tag depth to %u\n",
2289 set
->queue_depth
= BLK_MQ_MAX_DEPTH
;
2293 * If a crashdump is active, then we are potentially in a very
2294 * memory constrained environment. Limit us to 1 queue and
2295 * 64 tags to prevent using too much memory.
2297 if (is_kdump_kernel()) {
2298 set
->nr_hw_queues
= 1;
2299 set
->queue_depth
= min(64U, set
->queue_depth
);
2302 * There is no use for more h/w queues than cpus.
2304 if (set
->nr_hw_queues
> nr_cpu_ids
)
2305 set
->nr_hw_queues
= nr_cpu_ids
;
2307 set
->tags
= kzalloc_node(nr_cpu_ids
* sizeof(struct blk_mq_tags
*),
2308 GFP_KERNEL
, set
->numa_node
);
2313 set
->mq_map
= kzalloc_node(sizeof(*set
->mq_map
) * nr_cpu_ids
,
2314 GFP_KERNEL
, set
->numa_node
);
2318 if (set
->ops
->map_queues
)
2319 ret
= set
->ops
->map_queues(set
);
2321 ret
= blk_mq_map_queues(set
);
2323 goto out_free_mq_map
;
2325 ret
= blk_mq_alloc_rq_maps(set
);
2327 goto out_free_mq_map
;
2329 mutex_init(&set
->tag_list_lock
);
2330 INIT_LIST_HEAD(&set
->tag_list
);
2342 EXPORT_SYMBOL(blk_mq_alloc_tag_set
);
2344 void blk_mq_free_tag_set(struct blk_mq_tag_set
*set
)
2348 for (i
= 0; i
< nr_cpu_ids
; i
++) {
2350 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
2359 EXPORT_SYMBOL(blk_mq_free_tag_set
);
2361 int blk_mq_update_nr_requests(struct request_queue
*q
, unsigned int nr
)
2363 struct blk_mq_tag_set
*set
= q
->tag_set
;
2364 struct blk_mq_hw_ctx
*hctx
;
2367 if (!set
|| nr
> set
->queue_depth
)
2371 queue_for_each_hw_ctx(q
, hctx
, i
) {
2374 ret
= blk_mq_tag_update_depth(hctx
->tags
, nr
);
2380 q
->nr_requests
= nr
;
2385 void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set
*set
, int nr_hw_queues
)
2387 struct request_queue
*q
;
2389 if (nr_hw_queues
> nr_cpu_ids
)
2390 nr_hw_queues
= nr_cpu_ids
;
2391 if (nr_hw_queues
< 1 || nr_hw_queues
== set
->nr_hw_queues
)
2394 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
)
2395 blk_mq_freeze_queue(q
);
2397 set
->nr_hw_queues
= nr_hw_queues
;
2398 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
) {
2399 blk_mq_realloc_hw_ctxs(set
, q
);
2401 if (q
->nr_hw_queues
> 1)
2402 blk_queue_make_request(q
, blk_mq_make_request
);
2404 blk_queue_make_request(q
, blk_sq_make_request
);
2406 blk_mq_queue_reinit(q
, cpu_online_mask
);
2409 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
)
2410 blk_mq_unfreeze_queue(q
);
2412 EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues
);
2414 void blk_mq_disable_hotplug(void)
2416 mutex_lock(&all_q_mutex
);
2419 void blk_mq_enable_hotplug(void)
2421 mutex_unlock(&all_q_mutex
);
2424 static int __init
blk_mq_init(void)
2426 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD
, "block/mq:dead", NULL
,
2427 blk_mq_hctx_notify_dead
);
2429 cpuhp_setup_state_nocalls(CPUHP_BLK_MQ_PREPARE
, "block/mq:prepare",
2430 blk_mq_queue_reinit_prepare
,
2431 blk_mq_queue_reinit_dead
);
2434 subsys_initcall(blk_mq_init
);