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"
35 static DEFINE_MUTEX(all_q_mutex
);
36 static LIST_HEAD(all_q_list
);
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
)
43 return sbitmap_any_bit_set(&hctx
->ctx_map
);
47 * Mark this ctx as having pending work in this hardware queue
49 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx
*hctx
,
50 struct blk_mq_ctx
*ctx
)
52 if (!sbitmap_test_bit(&hctx
->ctx_map
, ctx
->index_hw
))
53 sbitmap_set_bit(&hctx
->ctx_map
, ctx
->index_hw
);
56 static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx
*hctx
,
57 struct blk_mq_ctx
*ctx
)
59 sbitmap_clear_bit(&hctx
->ctx_map
, ctx
->index_hw
);
62 void blk_mq_freeze_queue_start(struct request_queue
*q
)
66 freeze_depth
= atomic_inc_return(&q
->mq_freeze_depth
);
67 if (freeze_depth
== 1) {
68 percpu_ref_kill(&q
->q_usage_counter
);
69 blk_mq_run_hw_queues(q
, false);
72 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start
);
74 static void blk_mq_freeze_queue_wait(struct request_queue
*q
)
76 wait_event(q
->mq_freeze_wq
, percpu_ref_is_zero(&q
->q_usage_counter
));
80 * Guarantee no request is in use, so we can change any data structure of
81 * the queue afterward.
83 void blk_freeze_queue(struct request_queue
*q
)
86 * In the !blk_mq case we are only calling this to kill the
87 * q_usage_counter, otherwise this increases the freeze depth
88 * and waits for it to return to zero. For this reason there is
89 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
90 * exported to drivers as the only user for unfreeze is blk_mq.
92 blk_mq_freeze_queue_start(q
);
93 blk_mq_freeze_queue_wait(q
);
96 void blk_mq_freeze_queue(struct request_queue
*q
)
99 * ...just an alias to keep freeze and unfreeze actions balanced
100 * in the blk_mq_* namespace
104 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue
);
106 void blk_mq_unfreeze_queue(struct request_queue
*q
)
110 freeze_depth
= atomic_dec_return(&q
->mq_freeze_depth
);
111 WARN_ON_ONCE(freeze_depth
< 0);
113 percpu_ref_reinit(&q
->q_usage_counter
);
114 wake_up_all(&q
->mq_freeze_wq
);
117 EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue
);
120 * blk_mq_quiesce_queue() - wait until all ongoing queue_rq calls have finished
123 * Note: this function does not prevent that the struct request end_io()
124 * callback function is invoked. Additionally, it is not prevented that
125 * new queue_rq() calls occur unless the queue has been stopped first.
127 void blk_mq_quiesce_queue(struct request_queue
*q
)
129 struct blk_mq_hw_ctx
*hctx
;
133 blk_mq_stop_hw_queues(q
);
135 queue_for_each_hw_ctx(q
, hctx
, i
) {
136 if (hctx
->flags
& BLK_MQ_F_BLOCKING
)
137 synchronize_srcu(&hctx
->queue_rq_srcu
);
144 EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue
);
146 void blk_mq_wake_waiters(struct request_queue
*q
)
148 struct blk_mq_hw_ctx
*hctx
;
151 queue_for_each_hw_ctx(q
, hctx
, i
)
152 if (blk_mq_hw_queue_mapped(hctx
))
153 blk_mq_tag_wakeup_all(hctx
->tags
, true);
156 * If we are called because the queue has now been marked as
157 * dying, we need to ensure that processes currently waiting on
158 * the queue are notified as well.
160 wake_up_all(&q
->mq_freeze_wq
);
163 bool blk_mq_can_queue(struct blk_mq_hw_ctx
*hctx
)
165 return blk_mq_has_free_tags(hctx
->tags
);
167 EXPORT_SYMBOL(blk_mq_can_queue
);
169 static void blk_mq_rq_ctx_init(struct request_queue
*q
, struct blk_mq_ctx
*ctx
,
170 struct request
*rq
, unsigned int op
)
172 INIT_LIST_HEAD(&rq
->queuelist
);
173 /* csd/requeue_work/fifo_time is initialized before use */
177 if (blk_queue_io_stat(q
))
178 rq
->rq_flags
|= RQF_IO_STAT
;
179 /* do not touch atomic flags, it needs atomic ops against the timer */
181 INIT_HLIST_NODE(&rq
->hash
);
182 RB_CLEAR_NODE(&rq
->rb_node
);
185 rq
->start_time
= jiffies
;
186 #ifdef CONFIG_BLK_CGROUP
188 set_start_time_ns(rq
);
189 rq
->io_start_time_ns
= 0;
191 rq
->nr_phys_segments
= 0;
192 #if defined(CONFIG_BLK_DEV_INTEGRITY)
193 rq
->nr_integrity_segments
= 0;
196 /* tag was already set */
206 INIT_LIST_HEAD(&rq
->timeout_list
);
210 rq
->end_io_data
= NULL
;
213 ctx
->rq_dispatched
[op_is_sync(op
)]++;
216 static struct request
*
217 __blk_mq_alloc_request(struct blk_mq_alloc_data
*data
, unsigned int op
)
222 tag
= blk_mq_get_tag(data
);
223 if (tag
!= BLK_MQ_TAG_FAIL
) {
224 rq
= data
->hctx
->tags
->rqs
[tag
];
226 if (blk_mq_tag_busy(data
->hctx
)) {
227 rq
->rq_flags
= RQF_MQ_INFLIGHT
;
228 atomic_inc(&data
->hctx
->nr_active
);
232 blk_mq_rq_ctx_init(data
->q
, data
->ctx
, rq
, op
);
239 struct request
*blk_mq_alloc_request(struct request_queue
*q
, int rw
,
242 struct blk_mq_ctx
*ctx
;
243 struct blk_mq_hw_ctx
*hctx
;
245 struct blk_mq_alloc_data alloc_data
;
248 ret
= blk_queue_enter(q
, flags
& BLK_MQ_REQ_NOWAIT
);
252 ctx
= blk_mq_get_ctx(q
);
253 hctx
= blk_mq_map_queue(q
, ctx
->cpu
);
254 blk_mq_set_alloc_data(&alloc_data
, q
, flags
, ctx
, hctx
);
255 rq
= __blk_mq_alloc_request(&alloc_data
, rw
);
260 return ERR_PTR(-EWOULDBLOCK
);
264 rq
->__sector
= (sector_t
) -1;
265 rq
->bio
= rq
->biotail
= NULL
;
268 EXPORT_SYMBOL(blk_mq_alloc_request
);
270 struct request
*blk_mq_alloc_request_hctx(struct request_queue
*q
, int rw
,
271 unsigned int flags
, unsigned int hctx_idx
)
273 struct blk_mq_hw_ctx
*hctx
;
274 struct blk_mq_ctx
*ctx
;
276 struct blk_mq_alloc_data alloc_data
;
280 * If the tag allocator sleeps we could get an allocation for a
281 * different hardware context. No need to complicate the low level
282 * allocator for this for the rare use case of a command tied to
285 if (WARN_ON_ONCE(!(flags
& BLK_MQ_REQ_NOWAIT
)))
286 return ERR_PTR(-EINVAL
);
288 if (hctx_idx
>= q
->nr_hw_queues
)
289 return ERR_PTR(-EIO
);
291 ret
= blk_queue_enter(q
, true);
296 * Check if the hardware context is actually mapped to anything.
297 * If not tell the caller that it should skip this queue.
299 hctx
= q
->queue_hw_ctx
[hctx_idx
];
300 if (!blk_mq_hw_queue_mapped(hctx
)) {
304 ctx
= __blk_mq_get_ctx(q
, cpumask_first(hctx
->cpumask
));
306 blk_mq_set_alloc_data(&alloc_data
, q
, flags
, ctx
, hctx
);
307 rq
= __blk_mq_alloc_request(&alloc_data
, rw
);
319 EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx
);
321 static void __blk_mq_free_request(struct blk_mq_hw_ctx
*hctx
,
322 struct blk_mq_ctx
*ctx
, struct request
*rq
)
324 const int tag
= rq
->tag
;
325 struct request_queue
*q
= rq
->q
;
327 if (rq
->rq_flags
& RQF_MQ_INFLIGHT
)
328 atomic_dec(&hctx
->nr_active
);
331 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
332 blk_mq_put_tag(hctx
, ctx
, tag
);
336 void blk_mq_free_hctx_request(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
)
338 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
340 ctx
->rq_completed
[rq_is_sync(rq
)]++;
341 __blk_mq_free_request(hctx
, ctx
, rq
);
344 EXPORT_SYMBOL_GPL(blk_mq_free_hctx_request
);
346 void blk_mq_free_request(struct request
*rq
)
348 blk_mq_free_hctx_request(blk_mq_map_queue(rq
->q
, rq
->mq_ctx
->cpu
), rq
);
350 EXPORT_SYMBOL_GPL(blk_mq_free_request
);
352 inline void __blk_mq_end_request(struct request
*rq
, int error
)
354 blk_account_io_done(rq
);
357 rq
->end_io(rq
, error
);
359 if (unlikely(blk_bidi_rq(rq
)))
360 blk_mq_free_request(rq
->next_rq
);
361 blk_mq_free_request(rq
);
364 EXPORT_SYMBOL(__blk_mq_end_request
);
366 void blk_mq_end_request(struct request
*rq
, int error
)
368 if (blk_update_request(rq
, error
, blk_rq_bytes(rq
)))
370 __blk_mq_end_request(rq
, error
);
372 EXPORT_SYMBOL(blk_mq_end_request
);
374 static void __blk_mq_complete_request_remote(void *data
)
376 struct request
*rq
= data
;
378 rq
->q
->softirq_done_fn(rq
);
381 static void blk_mq_ipi_complete_request(struct request
*rq
)
383 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
387 if (!test_bit(QUEUE_FLAG_SAME_COMP
, &rq
->q
->queue_flags
)) {
388 rq
->q
->softirq_done_fn(rq
);
393 if (!test_bit(QUEUE_FLAG_SAME_FORCE
, &rq
->q
->queue_flags
))
394 shared
= cpus_share_cache(cpu
, ctx
->cpu
);
396 if (cpu
!= ctx
->cpu
&& !shared
&& cpu_online(ctx
->cpu
)) {
397 rq
->csd
.func
= __blk_mq_complete_request_remote
;
400 smp_call_function_single_async(ctx
->cpu
, &rq
->csd
);
402 rq
->q
->softirq_done_fn(rq
);
407 static void blk_mq_stat_add(struct request
*rq
)
409 if (rq
->rq_flags
& RQF_STATS
) {
411 * We could rq->mq_ctx here, but there's less of a risk
412 * of races if we have the completion event add the stats
413 * to the local software queue.
415 struct blk_mq_ctx
*ctx
;
417 ctx
= __blk_mq_get_ctx(rq
->q
, raw_smp_processor_id());
418 blk_stat_add(&ctx
->stat
[rq_data_dir(rq
)], rq
);
422 static void __blk_mq_complete_request(struct request
*rq
)
424 struct request_queue
*q
= rq
->q
;
428 if (!q
->softirq_done_fn
)
429 blk_mq_end_request(rq
, rq
->errors
);
431 blk_mq_ipi_complete_request(rq
);
435 * blk_mq_complete_request - end I/O on a request
436 * @rq: the request being processed
439 * Ends all I/O on a request. It does not handle partial completions.
440 * The actual completion happens out-of-order, through a IPI handler.
442 void blk_mq_complete_request(struct request
*rq
, int error
)
444 struct request_queue
*q
= rq
->q
;
446 if (unlikely(blk_should_fake_timeout(q
)))
448 if (!blk_mark_rq_complete(rq
)) {
450 __blk_mq_complete_request(rq
);
453 EXPORT_SYMBOL(blk_mq_complete_request
);
455 int blk_mq_request_started(struct request
*rq
)
457 return test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
459 EXPORT_SYMBOL_GPL(blk_mq_request_started
);
461 void blk_mq_start_request(struct request
*rq
)
463 struct request_queue
*q
= rq
->q
;
465 trace_block_rq_issue(q
, rq
);
467 rq
->resid_len
= blk_rq_bytes(rq
);
468 if (unlikely(blk_bidi_rq(rq
)))
469 rq
->next_rq
->resid_len
= blk_rq_bytes(rq
->next_rq
);
471 if (test_bit(QUEUE_FLAG_STATS
, &q
->queue_flags
)) {
472 blk_stat_set_issue_time(&rq
->issue_stat
);
473 rq
->rq_flags
|= RQF_STATS
;
479 * Ensure that ->deadline is visible before set the started
480 * flag and clear the completed flag.
482 smp_mb__before_atomic();
485 * Mark us as started and clear complete. Complete might have been
486 * set if requeue raced with timeout, which then marked it as
487 * complete. So be sure to clear complete again when we start
488 * the request, otherwise we'll ignore the completion event.
490 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
491 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
492 if (test_bit(REQ_ATOM_COMPLETE
, &rq
->atomic_flags
))
493 clear_bit(REQ_ATOM_COMPLETE
, &rq
->atomic_flags
);
495 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
497 * Make sure space for the drain appears. We know we can do
498 * this because max_hw_segments has been adjusted to be one
499 * fewer than the device can handle.
501 rq
->nr_phys_segments
++;
504 EXPORT_SYMBOL(blk_mq_start_request
);
506 static void __blk_mq_requeue_request(struct request
*rq
)
508 struct request_queue
*q
= rq
->q
;
510 trace_block_rq_requeue(q
, rq
);
512 if (test_and_clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
)) {
513 if (q
->dma_drain_size
&& blk_rq_bytes(rq
))
514 rq
->nr_phys_segments
--;
518 void blk_mq_requeue_request(struct request
*rq
, bool kick_requeue_list
)
520 __blk_mq_requeue_request(rq
);
522 BUG_ON(blk_queued_rq(rq
));
523 blk_mq_add_to_requeue_list(rq
, true, kick_requeue_list
);
525 EXPORT_SYMBOL(blk_mq_requeue_request
);
527 static void blk_mq_requeue_work(struct work_struct
*work
)
529 struct request_queue
*q
=
530 container_of(work
, struct request_queue
, requeue_work
.work
);
532 struct request
*rq
, *next
;
535 spin_lock_irqsave(&q
->requeue_lock
, flags
);
536 list_splice_init(&q
->requeue_list
, &rq_list
);
537 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
539 list_for_each_entry_safe(rq
, next
, &rq_list
, queuelist
) {
540 if (!(rq
->rq_flags
& RQF_SOFTBARRIER
))
543 rq
->rq_flags
&= ~RQF_SOFTBARRIER
;
544 list_del_init(&rq
->queuelist
);
545 blk_mq_insert_request(rq
, true, false, false);
548 while (!list_empty(&rq_list
)) {
549 rq
= list_entry(rq_list
.next
, struct request
, queuelist
);
550 list_del_init(&rq
->queuelist
);
551 blk_mq_insert_request(rq
, false, false, false);
554 blk_mq_run_hw_queues(q
, false);
557 void blk_mq_add_to_requeue_list(struct request
*rq
, bool at_head
,
558 bool kick_requeue_list
)
560 struct request_queue
*q
= rq
->q
;
564 * We abuse this flag that is otherwise used by the I/O scheduler to
565 * request head insertation from the workqueue.
567 BUG_ON(rq
->rq_flags
& RQF_SOFTBARRIER
);
569 spin_lock_irqsave(&q
->requeue_lock
, flags
);
571 rq
->rq_flags
|= RQF_SOFTBARRIER
;
572 list_add(&rq
->queuelist
, &q
->requeue_list
);
574 list_add_tail(&rq
->queuelist
, &q
->requeue_list
);
576 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
578 if (kick_requeue_list
)
579 blk_mq_kick_requeue_list(q
);
581 EXPORT_SYMBOL(blk_mq_add_to_requeue_list
);
583 void blk_mq_kick_requeue_list(struct request_queue
*q
)
585 kblockd_schedule_delayed_work(&q
->requeue_work
, 0);
587 EXPORT_SYMBOL(blk_mq_kick_requeue_list
);
589 void blk_mq_delay_kick_requeue_list(struct request_queue
*q
,
592 kblockd_schedule_delayed_work(&q
->requeue_work
,
593 msecs_to_jiffies(msecs
));
595 EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list
);
597 void blk_mq_abort_requeue_list(struct request_queue
*q
)
602 spin_lock_irqsave(&q
->requeue_lock
, flags
);
603 list_splice_init(&q
->requeue_list
, &rq_list
);
604 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
606 while (!list_empty(&rq_list
)) {
609 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
610 list_del_init(&rq
->queuelist
);
612 blk_mq_end_request(rq
, rq
->errors
);
615 EXPORT_SYMBOL(blk_mq_abort_requeue_list
);
617 struct request
*blk_mq_tag_to_rq(struct blk_mq_tags
*tags
, unsigned int tag
)
619 if (tag
< tags
->nr_tags
) {
620 prefetch(tags
->rqs
[tag
]);
621 return tags
->rqs
[tag
];
626 EXPORT_SYMBOL(blk_mq_tag_to_rq
);
628 struct blk_mq_timeout_data
{
630 unsigned int next_set
;
633 void blk_mq_rq_timed_out(struct request
*req
, bool reserved
)
635 struct blk_mq_ops
*ops
= req
->q
->mq_ops
;
636 enum blk_eh_timer_return ret
= BLK_EH_RESET_TIMER
;
639 * We know that complete is set at this point. If STARTED isn't set
640 * anymore, then the request isn't active and the "timeout" should
641 * just be ignored. This can happen due to the bitflag ordering.
642 * Timeout first checks if STARTED is set, and if it is, assumes
643 * the request is active. But if we race with completion, then
644 * we both flags will get cleared. So check here again, and ignore
645 * a timeout event with a request that isn't active.
647 if (!test_bit(REQ_ATOM_STARTED
, &req
->atomic_flags
))
651 ret
= ops
->timeout(req
, reserved
);
655 __blk_mq_complete_request(req
);
657 case BLK_EH_RESET_TIMER
:
659 blk_clear_rq_complete(req
);
661 case BLK_EH_NOT_HANDLED
:
664 printk(KERN_ERR
"block: bad eh return: %d\n", ret
);
669 static void blk_mq_check_expired(struct blk_mq_hw_ctx
*hctx
,
670 struct request
*rq
, void *priv
, bool reserved
)
672 struct blk_mq_timeout_data
*data
= priv
;
674 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
)) {
676 * If a request wasn't started before the queue was
677 * marked dying, kill it here or it'll go unnoticed.
679 if (unlikely(blk_queue_dying(rq
->q
))) {
681 blk_mq_end_request(rq
, rq
->errors
);
686 if (time_after_eq(jiffies
, rq
->deadline
)) {
687 if (!blk_mark_rq_complete(rq
))
688 blk_mq_rq_timed_out(rq
, reserved
);
689 } else if (!data
->next_set
|| time_after(data
->next
, rq
->deadline
)) {
690 data
->next
= rq
->deadline
;
695 static void blk_mq_timeout_work(struct work_struct
*work
)
697 struct request_queue
*q
=
698 container_of(work
, struct request_queue
, timeout_work
);
699 struct blk_mq_timeout_data data
= {
705 /* A deadlock might occur if a request is stuck requiring a
706 * timeout at the same time a queue freeze is waiting
707 * completion, since the timeout code would not be able to
708 * acquire the queue reference here.
710 * That's why we don't use blk_queue_enter here; instead, we use
711 * percpu_ref_tryget directly, because we need to be able to
712 * obtain a reference even in the short window between the queue
713 * starting to freeze, by dropping the first reference in
714 * blk_mq_freeze_queue_start, and the moment the last request is
715 * consumed, marked by the instant q_usage_counter reaches
718 if (!percpu_ref_tryget(&q
->q_usage_counter
))
721 blk_mq_queue_tag_busy_iter(q
, blk_mq_check_expired
, &data
);
724 data
.next
= blk_rq_timeout(round_jiffies_up(data
.next
));
725 mod_timer(&q
->timeout
, data
.next
);
727 struct blk_mq_hw_ctx
*hctx
;
729 queue_for_each_hw_ctx(q
, hctx
, i
) {
730 /* the hctx may be unmapped, so check it here */
731 if (blk_mq_hw_queue_mapped(hctx
))
732 blk_mq_tag_idle(hctx
);
739 * Reverse check our software queue for entries that we could potentially
740 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
741 * too much time checking for merges.
743 static bool blk_mq_attempt_merge(struct request_queue
*q
,
744 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
749 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
755 if (!blk_rq_merge_ok(rq
, bio
))
758 el_ret
= blk_try_merge(rq
, bio
);
759 if (el_ret
== ELEVATOR_BACK_MERGE
) {
760 if (bio_attempt_back_merge(q
, rq
, bio
)) {
765 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
766 if (bio_attempt_front_merge(q
, rq
, bio
)) {
777 struct flush_busy_ctx_data
{
778 struct blk_mq_hw_ctx
*hctx
;
779 struct list_head
*list
;
782 static bool flush_busy_ctx(struct sbitmap
*sb
, unsigned int bitnr
, void *data
)
784 struct flush_busy_ctx_data
*flush_data
= data
;
785 struct blk_mq_hw_ctx
*hctx
= flush_data
->hctx
;
786 struct blk_mq_ctx
*ctx
= hctx
->ctxs
[bitnr
];
788 sbitmap_clear_bit(sb
, bitnr
);
789 spin_lock(&ctx
->lock
);
790 list_splice_tail_init(&ctx
->rq_list
, flush_data
->list
);
791 spin_unlock(&ctx
->lock
);
796 * Process software queues that have been marked busy, splicing them
797 * to the for-dispatch
799 static void flush_busy_ctxs(struct blk_mq_hw_ctx
*hctx
, struct list_head
*list
)
801 struct flush_busy_ctx_data data
= {
806 sbitmap_for_each_set(&hctx
->ctx_map
, flush_busy_ctx
, &data
);
809 static inline unsigned int queued_to_index(unsigned int queued
)
814 return min(BLK_MQ_MAX_DISPATCH_ORDER
- 1, ilog2(queued
) + 1);
818 * Run this hardware queue, pulling any software queues mapped to it in.
819 * Note that this function currently has various problems around ordering
820 * of IO. In particular, we'd like FIFO behaviour on handling existing
821 * items on the hctx->dispatch list. Ignore that for now.
823 static void blk_mq_process_rq_list(struct blk_mq_hw_ctx
*hctx
)
825 struct request_queue
*q
= hctx
->queue
;
828 LIST_HEAD(driver_list
);
829 struct list_head
*dptr
;
832 if (unlikely(blk_mq_hctx_stopped(hctx
)))
838 * Touch any software queue that has pending entries.
840 flush_busy_ctxs(hctx
, &rq_list
);
843 * If we have previous entries on our dispatch list, grab them
844 * and stuff them at the front for more fair dispatch.
846 if (!list_empty_careful(&hctx
->dispatch
)) {
847 spin_lock(&hctx
->lock
);
848 if (!list_empty(&hctx
->dispatch
))
849 list_splice_init(&hctx
->dispatch
, &rq_list
);
850 spin_unlock(&hctx
->lock
);
854 * Start off with dptr being NULL, so we start the first request
855 * immediately, even if we have more pending.
860 * Now process all the entries, sending them to the driver.
863 while (!list_empty(&rq_list
)) {
864 struct blk_mq_queue_data bd
;
867 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
868 list_del_init(&rq
->queuelist
);
872 bd
.last
= list_empty(&rq_list
);
874 ret
= q
->mq_ops
->queue_rq(hctx
, &bd
);
876 case BLK_MQ_RQ_QUEUE_OK
:
879 case BLK_MQ_RQ_QUEUE_BUSY
:
880 list_add(&rq
->queuelist
, &rq_list
);
881 __blk_mq_requeue_request(rq
);
884 pr_err("blk-mq: bad return on queue: %d\n", ret
);
885 case BLK_MQ_RQ_QUEUE_ERROR
:
887 blk_mq_end_request(rq
, rq
->errors
);
891 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
895 * We've done the first request. If we have more than 1
896 * left in the list, set dptr to defer issue.
898 if (!dptr
&& rq_list
.next
!= rq_list
.prev
)
902 hctx
->dispatched
[queued_to_index(queued
)]++;
905 * Any items that need requeuing? Stuff them into hctx->dispatch,
906 * that is where we will continue on next queue run.
908 if (!list_empty(&rq_list
)) {
909 spin_lock(&hctx
->lock
);
910 list_splice(&rq_list
, &hctx
->dispatch
);
911 spin_unlock(&hctx
->lock
);
913 * the queue is expected stopped with BLK_MQ_RQ_QUEUE_BUSY, but
914 * it's possible the queue is stopped and restarted again
915 * before this. Queue restart will dispatch requests. And since
916 * requests in rq_list aren't added into hctx->dispatch yet,
917 * the requests in rq_list might get lost.
919 * blk_mq_run_hw_queue() already checks the STOPPED bit
921 blk_mq_run_hw_queue(hctx
, true);
925 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
929 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx
->cpumask
) &&
930 cpu_online(hctx
->next_cpu
));
932 if (!(hctx
->flags
& BLK_MQ_F_BLOCKING
)) {
934 blk_mq_process_rq_list(hctx
);
937 srcu_idx
= srcu_read_lock(&hctx
->queue_rq_srcu
);
938 blk_mq_process_rq_list(hctx
);
939 srcu_read_unlock(&hctx
->queue_rq_srcu
, srcu_idx
);
944 * It'd be great if the workqueue API had a way to pass
945 * in a mask and had some smarts for more clever placement.
946 * For now we just round-robin here, switching for every
947 * BLK_MQ_CPU_WORK_BATCH queued items.
949 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx
*hctx
)
951 if (hctx
->queue
->nr_hw_queues
== 1)
952 return WORK_CPU_UNBOUND
;
954 if (--hctx
->next_cpu_batch
<= 0) {
957 next_cpu
= cpumask_next(hctx
->next_cpu
, hctx
->cpumask
);
958 if (next_cpu
>= nr_cpu_ids
)
959 next_cpu
= cpumask_first(hctx
->cpumask
);
961 hctx
->next_cpu
= next_cpu
;
962 hctx
->next_cpu_batch
= BLK_MQ_CPU_WORK_BATCH
;
965 return hctx
->next_cpu
;
968 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
970 if (unlikely(blk_mq_hctx_stopped(hctx
) ||
971 !blk_mq_hw_queue_mapped(hctx
)))
974 if (!async
&& !(hctx
->flags
& BLK_MQ_F_BLOCKING
)) {
976 if (cpumask_test_cpu(cpu
, hctx
->cpumask
)) {
977 __blk_mq_run_hw_queue(hctx
);
985 kblockd_schedule_work_on(blk_mq_hctx_next_cpu(hctx
), &hctx
->run_work
);
988 void blk_mq_run_hw_queues(struct request_queue
*q
, bool async
)
990 struct blk_mq_hw_ctx
*hctx
;
993 queue_for_each_hw_ctx(q
, hctx
, i
) {
994 if ((!blk_mq_hctx_has_pending(hctx
) &&
995 list_empty_careful(&hctx
->dispatch
)) ||
996 blk_mq_hctx_stopped(hctx
))
999 blk_mq_run_hw_queue(hctx
, async
);
1002 EXPORT_SYMBOL(blk_mq_run_hw_queues
);
1005 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1006 * @q: request queue.
1008 * The caller is responsible for serializing this function against
1009 * blk_mq_{start,stop}_hw_queue().
1011 bool blk_mq_queue_stopped(struct request_queue
*q
)
1013 struct blk_mq_hw_ctx
*hctx
;
1016 queue_for_each_hw_ctx(q
, hctx
, i
)
1017 if (blk_mq_hctx_stopped(hctx
))
1022 EXPORT_SYMBOL(blk_mq_queue_stopped
);
1024 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
1026 cancel_work(&hctx
->run_work
);
1027 cancel_delayed_work(&hctx
->delay_work
);
1028 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
1030 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
1032 void blk_mq_stop_hw_queues(struct request_queue
*q
)
1034 struct blk_mq_hw_ctx
*hctx
;
1037 queue_for_each_hw_ctx(q
, hctx
, i
)
1038 blk_mq_stop_hw_queue(hctx
);
1040 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
1042 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
1044 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
1046 blk_mq_run_hw_queue(hctx
, false);
1048 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
1050 void blk_mq_start_hw_queues(struct request_queue
*q
)
1052 struct blk_mq_hw_ctx
*hctx
;
1055 queue_for_each_hw_ctx(q
, hctx
, i
)
1056 blk_mq_start_hw_queue(hctx
);
1058 EXPORT_SYMBOL(blk_mq_start_hw_queues
);
1060 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
, bool async
)
1062 struct blk_mq_hw_ctx
*hctx
;
1065 queue_for_each_hw_ctx(q
, hctx
, i
) {
1066 if (!blk_mq_hctx_stopped(hctx
))
1069 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
1070 blk_mq_run_hw_queue(hctx
, async
);
1073 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
1075 static void blk_mq_run_work_fn(struct work_struct
*work
)
1077 struct blk_mq_hw_ctx
*hctx
;
1079 hctx
= container_of(work
, struct blk_mq_hw_ctx
, run_work
);
1081 __blk_mq_run_hw_queue(hctx
);
1084 static void blk_mq_delay_work_fn(struct work_struct
*work
)
1086 struct blk_mq_hw_ctx
*hctx
;
1088 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delay_work
.work
);
1090 if (test_and_clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
1091 __blk_mq_run_hw_queue(hctx
);
1094 void blk_mq_delay_queue(struct blk_mq_hw_ctx
*hctx
, unsigned long msecs
)
1096 if (unlikely(!blk_mq_hw_queue_mapped(hctx
)))
1099 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx
),
1100 &hctx
->delay_work
, msecs_to_jiffies(msecs
));
1102 EXPORT_SYMBOL(blk_mq_delay_queue
);
1104 static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx
*hctx
,
1108 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
1110 trace_block_rq_insert(hctx
->queue
, rq
);
1113 list_add(&rq
->queuelist
, &ctx
->rq_list
);
1115 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
1118 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
1119 struct request
*rq
, bool at_head
)
1121 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
1123 __blk_mq_insert_req_list(hctx
, rq
, at_head
);
1124 blk_mq_hctx_mark_pending(hctx
, ctx
);
1127 void blk_mq_insert_request(struct request
*rq
, bool at_head
, bool run_queue
,
1130 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
1131 struct request_queue
*q
= rq
->q
;
1132 struct blk_mq_hw_ctx
*hctx
= blk_mq_map_queue(q
, ctx
->cpu
);
1134 spin_lock(&ctx
->lock
);
1135 __blk_mq_insert_request(hctx
, rq
, at_head
);
1136 spin_unlock(&ctx
->lock
);
1139 blk_mq_run_hw_queue(hctx
, async
);
1142 static void blk_mq_insert_requests(struct request_queue
*q
,
1143 struct blk_mq_ctx
*ctx
,
1144 struct list_head
*list
,
1149 struct blk_mq_hw_ctx
*hctx
= blk_mq_map_queue(q
, ctx
->cpu
);
1151 trace_block_unplug(q
, depth
, !from_schedule
);
1154 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1157 spin_lock(&ctx
->lock
);
1158 while (!list_empty(list
)) {
1161 rq
= list_first_entry(list
, struct request
, queuelist
);
1162 BUG_ON(rq
->mq_ctx
!= ctx
);
1163 list_del_init(&rq
->queuelist
);
1164 __blk_mq_insert_req_list(hctx
, rq
, false);
1166 blk_mq_hctx_mark_pending(hctx
, ctx
);
1167 spin_unlock(&ctx
->lock
);
1169 blk_mq_run_hw_queue(hctx
, from_schedule
);
1172 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1174 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
1175 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
1177 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
1178 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
1179 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
1182 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
1184 struct blk_mq_ctx
*this_ctx
;
1185 struct request_queue
*this_q
;
1188 LIST_HEAD(ctx_list
);
1191 list_splice_init(&plug
->mq_list
, &list
);
1193 list_sort(NULL
, &list
, plug_ctx_cmp
);
1199 while (!list_empty(&list
)) {
1200 rq
= list_entry_rq(list
.next
);
1201 list_del_init(&rq
->queuelist
);
1203 if (rq
->mq_ctx
!= this_ctx
) {
1205 blk_mq_insert_requests(this_q
, this_ctx
,
1210 this_ctx
= rq
->mq_ctx
;
1216 list_add_tail(&rq
->queuelist
, &ctx_list
);
1220 * If 'this_ctx' is set, we know we have entries to complete
1221 * on 'ctx_list'. Do those.
1224 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
1229 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
1231 init_request_from_bio(rq
, bio
);
1233 blk_account_io_start(rq
, 1);
1236 static inline bool hctx_allow_merges(struct blk_mq_hw_ctx
*hctx
)
1238 return (hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
) &&
1239 !blk_queue_nomerges(hctx
->queue
);
1242 static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx
*hctx
,
1243 struct blk_mq_ctx
*ctx
,
1244 struct request
*rq
, struct bio
*bio
)
1246 if (!hctx_allow_merges(hctx
) || !bio_mergeable(bio
)) {
1247 blk_mq_bio_to_request(rq
, bio
);
1248 spin_lock(&ctx
->lock
);
1250 __blk_mq_insert_request(hctx
, rq
, false);
1251 spin_unlock(&ctx
->lock
);
1254 struct request_queue
*q
= hctx
->queue
;
1256 spin_lock(&ctx
->lock
);
1257 if (!blk_mq_attempt_merge(q
, ctx
, bio
)) {
1258 blk_mq_bio_to_request(rq
, bio
);
1262 spin_unlock(&ctx
->lock
);
1263 __blk_mq_free_request(hctx
, ctx
, rq
);
1268 static struct request
*blk_mq_map_request(struct request_queue
*q
,
1270 struct blk_mq_alloc_data
*data
)
1272 struct blk_mq_hw_ctx
*hctx
;
1273 struct blk_mq_ctx
*ctx
;
1276 blk_queue_enter_live(q
);
1277 ctx
= blk_mq_get_ctx(q
);
1278 hctx
= blk_mq_map_queue(q
, ctx
->cpu
);
1280 trace_block_getrq(q
, bio
, bio
->bi_opf
);
1281 blk_mq_set_alloc_data(data
, q
, 0, ctx
, hctx
);
1282 rq
= __blk_mq_alloc_request(data
, bio
->bi_opf
);
1284 data
->hctx
->queued
++;
1288 static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx
*hctx
,
1289 struct request
*rq
, blk_qc_t
*cookie
)
1292 struct request_queue
*q
= rq
->q
;
1293 struct blk_mq_queue_data bd
= {
1298 blk_qc_t new_cookie
= blk_tag_to_qc_t(rq
->tag
, hctx
->queue_num
);
1300 if (blk_mq_hctx_stopped(hctx
))
1304 * For OK queue, we are done. For error, kill it. Any other
1305 * error (busy), just add it to our list as we previously
1308 ret
= q
->mq_ops
->queue_rq(hctx
, &bd
);
1309 if (ret
== BLK_MQ_RQ_QUEUE_OK
) {
1310 *cookie
= new_cookie
;
1314 __blk_mq_requeue_request(rq
);
1316 if (ret
== BLK_MQ_RQ_QUEUE_ERROR
) {
1317 *cookie
= BLK_QC_T_NONE
;
1319 blk_mq_end_request(rq
, rq
->errors
);
1324 blk_mq_insert_request(rq
, false, true, true);
1328 * Multiple hardware queue variant. This will not use per-process plugs,
1329 * but will attempt to bypass the hctx queueing if we can go straight to
1330 * hardware for SYNC IO.
1332 static blk_qc_t
blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
1334 const int is_sync
= op_is_sync(bio
->bi_opf
);
1335 const int is_flush_fua
= bio
->bi_opf
& (REQ_PREFLUSH
| REQ_FUA
);
1336 struct blk_mq_alloc_data data
;
1338 unsigned int request_count
= 0, srcu_idx
;
1339 struct blk_plug
*plug
;
1340 struct request
*same_queue_rq
= NULL
;
1343 blk_queue_bounce(q
, &bio
);
1345 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1347 return BLK_QC_T_NONE
;
1350 blk_queue_split(q
, &bio
, q
->bio_split
);
1352 if (!is_flush_fua
&& !blk_queue_nomerges(q
) &&
1353 blk_attempt_plug_merge(q
, bio
, &request_count
, &same_queue_rq
))
1354 return BLK_QC_T_NONE
;
1356 rq
= blk_mq_map_request(q
, bio
, &data
);
1358 return BLK_QC_T_NONE
;
1360 cookie
= blk_tag_to_qc_t(rq
->tag
, data
.hctx
->queue_num
);
1362 if (unlikely(is_flush_fua
)) {
1363 blk_mq_bio_to_request(rq
, bio
);
1364 blk_insert_flush(rq
);
1368 plug
= current
->plug
;
1370 * If the driver supports defer issued based on 'last', then
1371 * queue it up like normal since we can potentially save some
1374 if (((plug
&& !blk_queue_nomerges(q
)) || is_sync
) &&
1375 !(data
.hctx
->flags
& BLK_MQ_F_DEFER_ISSUE
)) {
1376 struct request
*old_rq
= NULL
;
1378 blk_mq_bio_to_request(rq
, bio
);
1381 * We do limited plugging. If the bio can be merged, do that.
1382 * Otherwise the existing request in the plug list will be
1383 * issued. So the plug list will have one request at most
1387 * The plug list might get flushed before this. If that
1388 * happens, same_queue_rq is invalid and plug list is
1391 if (same_queue_rq
&& !list_empty(&plug
->mq_list
)) {
1392 old_rq
= same_queue_rq
;
1393 list_del_init(&old_rq
->queuelist
);
1395 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
1396 } else /* is_sync */
1398 blk_mq_put_ctx(data
.ctx
);
1402 if (!(data
.hctx
->flags
& BLK_MQ_F_BLOCKING
)) {
1404 blk_mq_try_issue_directly(data
.hctx
, old_rq
, &cookie
);
1407 srcu_idx
= srcu_read_lock(&data
.hctx
->queue_rq_srcu
);
1408 blk_mq_try_issue_directly(data
.hctx
, old_rq
, &cookie
);
1409 srcu_read_unlock(&data
.hctx
->queue_rq_srcu
, srcu_idx
);
1414 if (!blk_mq_merge_queue_io(data
.hctx
, data
.ctx
, rq
, bio
)) {
1416 * For a SYNC request, send it to the hardware immediately. For
1417 * an ASYNC request, just ensure that we run it later on. The
1418 * latter allows for merging opportunities and more efficient
1422 blk_mq_run_hw_queue(data
.hctx
, !is_sync
|| is_flush_fua
);
1424 blk_mq_put_ctx(data
.ctx
);
1430 * Single hardware queue variant. This will attempt to use any per-process
1431 * plug for merging and IO deferral.
1433 static blk_qc_t
blk_sq_make_request(struct request_queue
*q
, struct bio
*bio
)
1435 const int is_sync
= op_is_sync(bio
->bi_opf
);
1436 const int is_flush_fua
= bio
->bi_opf
& (REQ_PREFLUSH
| REQ_FUA
);
1437 struct blk_plug
*plug
;
1438 unsigned int request_count
= 0;
1439 struct blk_mq_alloc_data data
;
1443 blk_queue_bounce(q
, &bio
);
1445 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1447 return BLK_QC_T_NONE
;
1450 blk_queue_split(q
, &bio
, q
->bio_split
);
1452 if (!is_flush_fua
&& !blk_queue_nomerges(q
)) {
1453 if (blk_attempt_plug_merge(q
, bio
, &request_count
, NULL
))
1454 return BLK_QC_T_NONE
;
1456 request_count
= blk_plug_queued_count(q
);
1458 rq
= blk_mq_map_request(q
, bio
, &data
);
1460 return BLK_QC_T_NONE
;
1462 cookie
= blk_tag_to_qc_t(rq
->tag
, data
.hctx
->queue_num
);
1464 if (unlikely(is_flush_fua
)) {
1465 blk_mq_bio_to_request(rq
, bio
);
1466 blk_insert_flush(rq
);
1471 * A task plug currently exists. Since this is completely lockless,
1472 * utilize that to temporarily store requests until the task is
1473 * either done or scheduled away.
1475 plug
= current
->plug
;
1477 struct request
*last
= NULL
;
1479 blk_mq_bio_to_request(rq
, bio
);
1481 trace_block_plug(q
);
1483 last
= list_entry_rq(plug
->mq_list
.prev
);
1485 blk_mq_put_ctx(data
.ctx
);
1487 if (request_count
>= BLK_MAX_REQUEST_COUNT
|| (last
&&
1488 blk_rq_bytes(last
) >= BLK_PLUG_FLUSH_SIZE
)) {
1489 blk_flush_plug_list(plug
, false);
1490 trace_block_plug(q
);
1493 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
1497 if (!blk_mq_merge_queue_io(data
.hctx
, data
.ctx
, rq
, bio
)) {
1499 * For a SYNC request, send it to the hardware immediately. For
1500 * an ASYNC request, just ensure that we run it later on. The
1501 * latter allows for merging opportunities and more efficient
1505 blk_mq_run_hw_queue(data
.hctx
, !is_sync
|| is_flush_fua
);
1508 blk_mq_put_ctx(data
.ctx
);
1512 static void blk_mq_free_rq_map(struct blk_mq_tag_set
*set
,
1513 struct blk_mq_tags
*tags
, unsigned int hctx_idx
)
1517 if (tags
->rqs
&& set
->ops
->exit_request
) {
1520 for (i
= 0; i
< tags
->nr_tags
; i
++) {
1523 set
->ops
->exit_request(set
->driver_data
, tags
->rqs
[i
],
1525 tags
->rqs
[i
] = NULL
;
1529 while (!list_empty(&tags
->page_list
)) {
1530 page
= list_first_entry(&tags
->page_list
, struct page
, lru
);
1531 list_del_init(&page
->lru
);
1533 * Remove kmemleak object previously allocated in
1534 * blk_mq_init_rq_map().
1536 kmemleak_free(page_address(page
));
1537 __free_pages(page
, page
->private);
1542 blk_mq_free_tags(tags
);
1545 static size_t order_to_size(unsigned int order
)
1547 return (size_t)PAGE_SIZE
<< order
;
1550 static struct blk_mq_tags
*blk_mq_init_rq_map(struct blk_mq_tag_set
*set
,
1551 unsigned int hctx_idx
)
1553 struct blk_mq_tags
*tags
;
1554 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1555 size_t rq_size
, left
;
1557 tags
= blk_mq_init_tags(set
->queue_depth
, set
->reserved_tags
,
1559 BLK_MQ_FLAG_TO_ALLOC_POLICY(set
->flags
));
1563 INIT_LIST_HEAD(&tags
->page_list
);
1565 tags
->rqs
= kzalloc_node(set
->queue_depth
* sizeof(struct request
*),
1566 GFP_KERNEL
| __GFP_NOWARN
| __GFP_NORETRY
,
1569 blk_mq_free_tags(tags
);
1574 * rq_size is the size of the request plus driver payload, rounded
1575 * to the cacheline size
1577 rq_size
= round_up(sizeof(struct request
) + set
->cmd_size
,
1579 left
= rq_size
* set
->queue_depth
;
1581 for (i
= 0; i
< set
->queue_depth
; ) {
1582 int this_order
= max_order
;
1587 while (this_order
&& left
< order_to_size(this_order
- 1))
1591 page
= alloc_pages_node(set
->numa_node
,
1592 GFP_KERNEL
| __GFP_NOWARN
| __GFP_NORETRY
| __GFP_ZERO
,
1598 if (order_to_size(this_order
) < rq_size
)
1605 page
->private = this_order
;
1606 list_add_tail(&page
->lru
, &tags
->page_list
);
1608 p
= page_address(page
);
1610 * Allow kmemleak to scan these pages as they contain pointers
1611 * to additional allocations like via ops->init_request().
1613 kmemleak_alloc(p
, order_to_size(this_order
), 1, GFP_KERNEL
);
1614 entries_per_page
= order_to_size(this_order
) / rq_size
;
1615 to_do
= min(entries_per_page
, set
->queue_depth
- i
);
1616 left
-= to_do
* rq_size
;
1617 for (j
= 0; j
< to_do
; j
++) {
1619 if (set
->ops
->init_request
) {
1620 if (set
->ops
->init_request(set
->driver_data
,
1621 tags
->rqs
[i
], hctx_idx
, i
,
1623 tags
->rqs
[i
] = NULL
;
1635 blk_mq_free_rq_map(set
, tags
, hctx_idx
);
1640 * 'cpu' is going away. splice any existing rq_list entries from this
1641 * software queue to the hw queue dispatch list, and ensure that it
1644 static int blk_mq_hctx_notify_dead(unsigned int cpu
, struct hlist_node
*node
)
1646 struct blk_mq_hw_ctx
*hctx
;
1647 struct blk_mq_ctx
*ctx
;
1650 hctx
= hlist_entry_safe(node
, struct blk_mq_hw_ctx
, cpuhp_dead
);
1651 ctx
= __blk_mq_get_ctx(hctx
->queue
, cpu
);
1653 spin_lock(&ctx
->lock
);
1654 if (!list_empty(&ctx
->rq_list
)) {
1655 list_splice_init(&ctx
->rq_list
, &tmp
);
1656 blk_mq_hctx_clear_pending(hctx
, ctx
);
1658 spin_unlock(&ctx
->lock
);
1660 if (list_empty(&tmp
))
1663 spin_lock(&hctx
->lock
);
1664 list_splice_tail_init(&tmp
, &hctx
->dispatch
);
1665 spin_unlock(&hctx
->lock
);
1667 blk_mq_run_hw_queue(hctx
, true);
1671 static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx
*hctx
)
1673 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD
,
1677 /* hctx->ctxs will be freed in queue's release handler */
1678 static void blk_mq_exit_hctx(struct request_queue
*q
,
1679 struct blk_mq_tag_set
*set
,
1680 struct blk_mq_hw_ctx
*hctx
, unsigned int hctx_idx
)
1682 unsigned flush_start_tag
= set
->queue_depth
;
1684 blk_mq_tag_idle(hctx
);
1686 if (set
->ops
->exit_request
)
1687 set
->ops
->exit_request(set
->driver_data
,
1688 hctx
->fq
->flush_rq
, hctx_idx
,
1689 flush_start_tag
+ hctx_idx
);
1691 if (set
->ops
->exit_hctx
)
1692 set
->ops
->exit_hctx(hctx
, hctx_idx
);
1694 if (hctx
->flags
& BLK_MQ_F_BLOCKING
)
1695 cleanup_srcu_struct(&hctx
->queue_rq_srcu
);
1697 blk_mq_remove_cpuhp(hctx
);
1698 blk_free_flush_queue(hctx
->fq
);
1699 sbitmap_free(&hctx
->ctx_map
);
1702 static void blk_mq_exit_hw_queues(struct request_queue
*q
,
1703 struct blk_mq_tag_set
*set
, int nr_queue
)
1705 struct blk_mq_hw_ctx
*hctx
;
1708 queue_for_each_hw_ctx(q
, hctx
, i
) {
1711 blk_mq_exit_hctx(q
, set
, hctx
, i
);
1715 static void blk_mq_free_hw_queues(struct request_queue
*q
,
1716 struct blk_mq_tag_set
*set
)
1718 struct blk_mq_hw_ctx
*hctx
;
1721 queue_for_each_hw_ctx(q
, hctx
, i
)
1722 free_cpumask_var(hctx
->cpumask
);
1725 static int blk_mq_init_hctx(struct request_queue
*q
,
1726 struct blk_mq_tag_set
*set
,
1727 struct blk_mq_hw_ctx
*hctx
, unsigned hctx_idx
)
1730 unsigned flush_start_tag
= set
->queue_depth
;
1732 node
= hctx
->numa_node
;
1733 if (node
== NUMA_NO_NODE
)
1734 node
= hctx
->numa_node
= set
->numa_node
;
1736 INIT_WORK(&hctx
->run_work
, blk_mq_run_work_fn
);
1737 INIT_DELAYED_WORK(&hctx
->delay_work
, blk_mq_delay_work_fn
);
1738 spin_lock_init(&hctx
->lock
);
1739 INIT_LIST_HEAD(&hctx
->dispatch
);
1741 hctx
->queue_num
= hctx_idx
;
1742 hctx
->flags
= set
->flags
& ~BLK_MQ_F_TAG_SHARED
;
1744 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD
, &hctx
->cpuhp_dead
);
1746 hctx
->tags
= set
->tags
[hctx_idx
];
1749 * Allocate space for all possible cpus to avoid allocation at
1752 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1755 goto unregister_cpu_notifier
;
1757 if (sbitmap_init_node(&hctx
->ctx_map
, nr_cpu_ids
, ilog2(8), GFP_KERNEL
,
1763 if (set
->ops
->init_hctx
&&
1764 set
->ops
->init_hctx(hctx
, set
->driver_data
, hctx_idx
))
1767 hctx
->fq
= blk_alloc_flush_queue(q
, hctx
->numa_node
, set
->cmd_size
);
1771 if (set
->ops
->init_request
&&
1772 set
->ops
->init_request(set
->driver_data
,
1773 hctx
->fq
->flush_rq
, hctx_idx
,
1774 flush_start_tag
+ hctx_idx
, node
))
1777 if (hctx
->flags
& BLK_MQ_F_BLOCKING
)
1778 init_srcu_struct(&hctx
->queue_rq_srcu
);
1785 if (set
->ops
->exit_hctx
)
1786 set
->ops
->exit_hctx(hctx
, hctx_idx
);
1788 sbitmap_free(&hctx
->ctx_map
);
1791 unregister_cpu_notifier
:
1792 blk_mq_remove_cpuhp(hctx
);
1796 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1797 unsigned int nr_hw_queues
)
1801 for_each_possible_cpu(i
) {
1802 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1803 struct blk_mq_hw_ctx
*hctx
;
1805 memset(__ctx
, 0, sizeof(*__ctx
));
1807 spin_lock_init(&__ctx
->lock
);
1808 INIT_LIST_HEAD(&__ctx
->rq_list
);
1810 blk_stat_init(&__ctx
->stat
[BLK_STAT_READ
]);
1811 blk_stat_init(&__ctx
->stat
[BLK_STAT_WRITE
]);
1813 /* If the cpu isn't online, the cpu is mapped to first hctx */
1817 hctx
= blk_mq_map_queue(q
, i
);
1820 * Set local node, IFF we have more than one hw queue. If
1821 * not, we remain on the home node of the device
1823 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1824 hctx
->numa_node
= local_memory_node(cpu_to_node(i
));
1828 static void blk_mq_map_swqueue(struct request_queue
*q
,
1829 const struct cpumask
*online_mask
)
1832 struct blk_mq_hw_ctx
*hctx
;
1833 struct blk_mq_ctx
*ctx
;
1834 struct blk_mq_tag_set
*set
= q
->tag_set
;
1837 * Avoid others reading imcomplete hctx->cpumask through sysfs
1839 mutex_lock(&q
->sysfs_lock
);
1841 queue_for_each_hw_ctx(q
, hctx
, i
) {
1842 cpumask_clear(hctx
->cpumask
);
1847 * Map software to hardware queues
1849 for_each_possible_cpu(i
) {
1850 /* If the cpu isn't online, the cpu is mapped to first hctx */
1851 if (!cpumask_test_cpu(i
, online_mask
))
1854 ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1855 hctx
= blk_mq_map_queue(q
, i
);
1857 cpumask_set_cpu(i
, hctx
->cpumask
);
1858 ctx
->index_hw
= hctx
->nr_ctx
;
1859 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1862 mutex_unlock(&q
->sysfs_lock
);
1864 queue_for_each_hw_ctx(q
, hctx
, i
) {
1866 * If no software queues are mapped to this hardware queue,
1867 * disable it and free the request entries.
1869 if (!hctx
->nr_ctx
) {
1871 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
1872 set
->tags
[i
] = NULL
;
1878 /* unmapped hw queue can be remapped after CPU topo changed */
1880 set
->tags
[i
] = blk_mq_init_rq_map(set
, i
);
1881 hctx
->tags
= set
->tags
[i
];
1882 WARN_ON(!hctx
->tags
);
1885 * Set the map size to the number of mapped software queues.
1886 * This is more accurate and more efficient than looping
1887 * over all possibly mapped software queues.
1889 sbitmap_resize(&hctx
->ctx_map
, hctx
->nr_ctx
);
1892 * Initialize batch roundrobin counts
1894 hctx
->next_cpu
= cpumask_first(hctx
->cpumask
);
1895 hctx
->next_cpu_batch
= BLK_MQ_CPU_WORK_BATCH
;
1899 static void queue_set_hctx_shared(struct request_queue
*q
, bool shared
)
1901 struct blk_mq_hw_ctx
*hctx
;
1904 queue_for_each_hw_ctx(q
, hctx
, i
) {
1906 hctx
->flags
|= BLK_MQ_F_TAG_SHARED
;
1908 hctx
->flags
&= ~BLK_MQ_F_TAG_SHARED
;
1912 static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set
*set
, bool shared
)
1914 struct request_queue
*q
;
1916 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
) {
1917 blk_mq_freeze_queue(q
);
1918 queue_set_hctx_shared(q
, shared
);
1919 blk_mq_unfreeze_queue(q
);
1923 static void blk_mq_del_queue_tag_set(struct request_queue
*q
)
1925 struct blk_mq_tag_set
*set
= q
->tag_set
;
1927 mutex_lock(&set
->tag_list_lock
);
1928 list_del_init(&q
->tag_set_list
);
1929 if (list_is_singular(&set
->tag_list
)) {
1930 /* just transitioned to unshared */
1931 set
->flags
&= ~BLK_MQ_F_TAG_SHARED
;
1932 /* update existing queue */
1933 blk_mq_update_tag_set_depth(set
, false);
1935 mutex_unlock(&set
->tag_list_lock
);
1938 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set
*set
,
1939 struct request_queue
*q
)
1943 mutex_lock(&set
->tag_list_lock
);
1945 /* Check to see if we're transitioning to shared (from 1 to 2 queues). */
1946 if (!list_empty(&set
->tag_list
) && !(set
->flags
& BLK_MQ_F_TAG_SHARED
)) {
1947 set
->flags
|= BLK_MQ_F_TAG_SHARED
;
1948 /* update existing queue */
1949 blk_mq_update_tag_set_depth(set
, true);
1951 if (set
->flags
& BLK_MQ_F_TAG_SHARED
)
1952 queue_set_hctx_shared(q
, true);
1953 list_add_tail(&q
->tag_set_list
, &set
->tag_list
);
1955 mutex_unlock(&set
->tag_list_lock
);
1959 * It is the actual release handler for mq, but we do it from
1960 * request queue's release handler for avoiding use-after-free
1961 * and headache because q->mq_kobj shouldn't have been introduced,
1962 * but we can't group ctx/kctx kobj without it.
1964 void blk_mq_release(struct request_queue
*q
)
1966 struct blk_mq_hw_ctx
*hctx
;
1969 /* hctx kobj stays in hctx */
1970 queue_for_each_hw_ctx(q
, hctx
, i
) {
1979 kfree(q
->queue_hw_ctx
);
1981 /* ctx kobj stays in queue_ctx */
1982 free_percpu(q
->queue_ctx
);
1985 struct request_queue
*blk_mq_init_queue(struct blk_mq_tag_set
*set
)
1987 struct request_queue
*uninit_q
, *q
;
1989 uninit_q
= blk_alloc_queue_node(GFP_KERNEL
, set
->numa_node
);
1991 return ERR_PTR(-ENOMEM
);
1993 q
= blk_mq_init_allocated_queue(set
, uninit_q
);
1995 blk_cleanup_queue(uninit_q
);
1999 EXPORT_SYMBOL(blk_mq_init_queue
);
2001 static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set
*set
,
2002 struct request_queue
*q
)
2005 struct blk_mq_hw_ctx
**hctxs
= q
->queue_hw_ctx
;
2007 blk_mq_sysfs_unregister(q
);
2008 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
2014 node
= blk_mq_hw_queue_to_node(q
->mq_map
, i
);
2015 hctxs
[i
] = kzalloc_node(sizeof(struct blk_mq_hw_ctx
),
2020 if (!zalloc_cpumask_var_node(&hctxs
[i
]->cpumask
, GFP_KERNEL
,
2027 atomic_set(&hctxs
[i
]->nr_active
, 0);
2028 hctxs
[i
]->numa_node
= node
;
2029 hctxs
[i
]->queue_num
= i
;
2031 if (blk_mq_init_hctx(q
, set
, hctxs
[i
], i
)) {
2032 free_cpumask_var(hctxs
[i
]->cpumask
);
2037 blk_mq_hctx_kobj_init(hctxs
[i
]);
2039 for (j
= i
; j
< q
->nr_hw_queues
; j
++) {
2040 struct blk_mq_hw_ctx
*hctx
= hctxs
[j
];
2044 blk_mq_free_rq_map(set
, hctx
->tags
, j
);
2045 set
->tags
[j
] = NULL
;
2047 blk_mq_exit_hctx(q
, set
, hctx
, j
);
2048 free_cpumask_var(hctx
->cpumask
);
2049 kobject_put(&hctx
->kobj
);
2056 q
->nr_hw_queues
= i
;
2057 blk_mq_sysfs_register(q
);
2060 struct request_queue
*blk_mq_init_allocated_queue(struct blk_mq_tag_set
*set
,
2061 struct request_queue
*q
)
2063 /* mark the queue as mq asap */
2064 q
->mq_ops
= set
->ops
;
2066 q
->queue_ctx
= alloc_percpu(struct blk_mq_ctx
);
2070 q
->queue_hw_ctx
= kzalloc_node(nr_cpu_ids
* sizeof(*(q
->queue_hw_ctx
)),
2071 GFP_KERNEL
, set
->numa_node
);
2072 if (!q
->queue_hw_ctx
)
2075 q
->mq_map
= set
->mq_map
;
2077 blk_mq_realloc_hw_ctxs(set
, q
);
2078 if (!q
->nr_hw_queues
)
2081 INIT_WORK(&q
->timeout_work
, blk_mq_timeout_work
);
2082 blk_queue_rq_timeout(q
, set
->timeout
? set
->timeout
: 30 * HZ
);
2084 q
->nr_queues
= nr_cpu_ids
;
2086 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
2088 if (!(set
->flags
& BLK_MQ_F_SG_MERGE
))
2089 q
->queue_flags
|= 1 << QUEUE_FLAG_NO_SG_MERGE
;
2091 q
->sg_reserved_size
= INT_MAX
;
2093 INIT_DELAYED_WORK(&q
->requeue_work
, blk_mq_requeue_work
);
2094 INIT_LIST_HEAD(&q
->requeue_list
);
2095 spin_lock_init(&q
->requeue_lock
);
2097 if (q
->nr_hw_queues
> 1)
2098 blk_queue_make_request(q
, blk_mq_make_request
);
2100 blk_queue_make_request(q
, blk_sq_make_request
);
2103 * Do this after blk_queue_make_request() overrides it...
2105 q
->nr_requests
= set
->queue_depth
;
2107 if (set
->ops
->complete
)
2108 blk_queue_softirq_done(q
, set
->ops
->complete
);
2110 blk_mq_init_cpu_queues(q
, set
->nr_hw_queues
);
2113 mutex_lock(&all_q_mutex
);
2115 list_add_tail(&q
->all_q_node
, &all_q_list
);
2116 blk_mq_add_queue_tag_set(set
, q
);
2117 blk_mq_map_swqueue(q
, cpu_online_mask
);
2119 mutex_unlock(&all_q_mutex
);
2125 kfree(q
->queue_hw_ctx
);
2127 free_percpu(q
->queue_ctx
);
2130 return ERR_PTR(-ENOMEM
);
2132 EXPORT_SYMBOL(blk_mq_init_allocated_queue
);
2134 void blk_mq_free_queue(struct request_queue
*q
)
2136 struct blk_mq_tag_set
*set
= q
->tag_set
;
2138 mutex_lock(&all_q_mutex
);
2139 list_del_init(&q
->all_q_node
);
2140 mutex_unlock(&all_q_mutex
);
2142 blk_mq_del_queue_tag_set(q
);
2144 blk_mq_exit_hw_queues(q
, set
, set
->nr_hw_queues
);
2145 blk_mq_free_hw_queues(q
, set
);
2148 /* Basically redo blk_mq_init_queue with queue frozen */
2149 static void blk_mq_queue_reinit(struct request_queue
*q
,
2150 const struct cpumask
*online_mask
)
2152 WARN_ON_ONCE(!atomic_read(&q
->mq_freeze_depth
));
2154 blk_mq_sysfs_unregister(q
);
2157 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
2158 * we should change hctx numa_node according to new topology (this
2159 * involves free and re-allocate memory, worthy doing?)
2162 blk_mq_map_swqueue(q
, online_mask
);
2164 blk_mq_sysfs_register(q
);
2168 * New online cpumask which is going to be set in this hotplug event.
2169 * Declare this cpumasks as global as cpu-hotplug operation is invoked
2170 * one-by-one and dynamically allocating this could result in a failure.
2172 static struct cpumask cpuhp_online_new
;
2174 static void blk_mq_queue_reinit_work(void)
2176 struct request_queue
*q
;
2178 mutex_lock(&all_q_mutex
);
2180 * We need to freeze and reinit all existing queues. Freezing
2181 * involves synchronous wait for an RCU grace period and doing it
2182 * one by one may take a long time. Start freezing all queues in
2183 * one swoop and then wait for the completions so that freezing can
2184 * take place in parallel.
2186 list_for_each_entry(q
, &all_q_list
, all_q_node
)
2187 blk_mq_freeze_queue_start(q
);
2188 list_for_each_entry(q
, &all_q_list
, all_q_node
) {
2189 blk_mq_freeze_queue_wait(q
);
2192 * timeout handler can't touch hw queue during the
2195 del_timer_sync(&q
->timeout
);
2198 list_for_each_entry(q
, &all_q_list
, all_q_node
)
2199 blk_mq_queue_reinit(q
, &cpuhp_online_new
);
2201 list_for_each_entry(q
, &all_q_list
, all_q_node
)
2202 blk_mq_unfreeze_queue(q
);
2204 mutex_unlock(&all_q_mutex
);
2207 static int blk_mq_queue_reinit_dead(unsigned int cpu
)
2209 cpumask_copy(&cpuhp_online_new
, cpu_online_mask
);
2210 blk_mq_queue_reinit_work();
2215 * Before hotadded cpu starts handling requests, new mappings must be
2216 * established. Otherwise, these requests in hw queue might never be
2219 * For example, there is a single hw queue (hctx) and two CPU queues (ctx0
2220 * for CPU0, and ctx1 for CPU1).
2222 * Now CPU1 is just onlined and a request is inserted into ctx1->rq_list
2223 * and set bit0 in pending bitmap as ctx1->index_hw is still zero.
2225 * And then while running hw queue, flush_busy_ctxs() finds bit0 is set in
2226 * pending bitmap and tries to retrieve requests in hctx->ctxs[0]->rq_list.
2227 * But htx->ctxs[0] is a pointer to ctx0, so the request in ctx1->rq_list
2230 static int blk_mq_queue_reinit_prepare(unsigned int cpu
)
2232 cpumask_copy(&cpuhp_online_new
, cpu_online_mask
);
2233 cpumask_set_cpu(cpu
, &cpuhp_online_new
);
2234 blk_mq_queue_reinit_work();
2238 static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set
*set
)
2242 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
2243 set
->tags
[i
] = blk_mq_init_rq_map(set
, i
);
2252 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
2258 * Allocate the request maps associated with this tag_set. Note that this
2259 * may reduce the depth asked for, if memory is tight. set->queue_depth
2260 * will be updated to reflect the allocated depth.
2262 static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set
*set
)
2267 depth
= set
->queue_depth
;
2269 err
= __blk_mq_alloc_rq_maps(set
);
2273 set
->queue_depth
>>= 1;
2274 if (set
->queue_depth
< set
->reserved_tags
+ BLK_MQ_TAG_MIN
) {
2278 } while (set
->queue_depth
);
2280 if (!set
->queue_depth
|| err
) {
2281 pr_err("blk-mq: failed to allocate request map\n");
2285 if (depth
!= set
->queue_depth
)
2286 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2287 depth
, set
->queue_depth
);
2293 * Alloc a tag set to be associated with one or more request queues.
2294 * May fail with EINVAL for various error conditions. May adjust the
2295 * requested depth down, if if it too large. In that case, the set
2296 * value will be stored in set->queue_depth.
2298 int blk_mq_alloc_tag_set(struct blk_mq_tag_set
*set
)
2302 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH
> 1 << BLK_MQ_UNIQUE_TAG_BITS
);
2304 if (!set
->nr_hw_queues
)
2306 if (!set
->queue_depth
)
2308 if (set
->queue_depth
< set
->reserved_tags
+ BLK_MQ_TAG_MIN
)
2311 if (!set
->ops
->queue_rq
)
2314 if (set
->queue_depth
> BLK_MQ_MAX_DEPTH
) {
2315 pr_info("blk-mq: reduced tag depth to %u\n",
2317 set
->queue_depth
= BLK_MQ_MAX_DEPTH
;
2321 * If a crashdump is active, then we are potentially in a very
2322 * memory constrained environment. Limit us to 1 queue and
2323 * 64 tags to prevent using too much memory.
2325 if (is_kdump_kernel()) {
2326 set
->nr_hw_queues
= 1;
2327 set
->queue_depth
= min(64U, set
->queue_depth
);
2330 * There is no use for more h/w queues than cpus.
2332 if (set
->nr_hw_queues
> nr_cpu_ids
)
2333 set
->nr_hw_queues
= nr_cpu_ids
;
2335 set
->tags
= kzalloc_node(nr_cpu_ids
* sizeof(struct blk_mq_tags
*),
2336 GFP_KERNEL
, set
->numa_node
);
2341 set
->mq_map
= kzalloc_node(sizeof(*set
->mq_map
) * nr_cpu_ids
,
2342 GFP_KERNEL
, set
->numa_node
);
2346 if (set
->ops
->map_queues
)
2347 ret
= set
->ops
->map_queues(set
);
2349 ret
= blk_mq_map_queues(set
);
2351 goto out_free_mq_map
;
2353 ret
= blk_mq_alloc_rq_maps(set
);
2355 goto out_free_mq_map
;
2357 mutex_init(&set
->tag_list_lock
);
2358 INIT_LIST_HEAD(&set
->tag_list
);
2370 EXPORT_SYMBOL(blk_mq_alloc_tag_set
);
2372 void blk_mq_free_tag_set(struct blk_mq_tag_set
*set
)
2376 for (i
= 0; i
< nr_cpu_ids
; i
++) {
2378 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
2387 EXPORT_SYMBOL(blk_mq_free_tag_set
);
2389 int blk_mq_update_nr_requests(struct request_queue
*q
, unsigned int nr
)
2391 struct blk_mq_tag_set
*set
= q
->tag_set
;
2392 struct blk_mq_hw_ctx
*hctx
;
2395 if (!set
|| nr
> set
->queue_depth
)
2399 queue_for_each_hw_ctx(q
, hctx
, i
) {
2402 ret
= blk_mq_tag_update_depth(hctx
->tags
, nr
);
2408 q
->nr_requests
= nr
;
2413 void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set
*set
, int nr_hw_queues
)
2415 struct request_queue
*q
;
2417 if (nr_hw_queues
> nr_cpu_ids
)
2418 nr_hw_queues
= nr_cpu_ids
;
2419 if (nr_hw_queues
< 1 || nr_hw_queues
== set
->nr_hw_queues
)
2422 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
)
2423 blk_mq_freeze_queue(q
);
2425 set
->nr_hw_queues
= nr_hw_queues
;
2426 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
) {
2427 blk_mq_realloc_hw_ctxs(set
, q
);
2429 if (q
->nr_hw_queues
> 1)
2430 blk_queue_make_request(q
, blk_mq_make_request
);
2432 blk_queue_make_request(q
, blk_sq_make_request
);
2434 blk_mq_queue_reinit(q
, cpu_online_mask
);
2437 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
)
2438 blk_mq_unfreeze_queue(q
);
2440 EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues
);
2442 void blk_mq_disable_hotplug(void)
2444 mutex_lock(&all_q_mutex
);
2447 void blk_mq_enable_hotplug(void)
2449 mutex_unlock(&all_q_mutex
);
2452 static int __init
blk_mq_init(void)
2454 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD
, "block/mq:dead", NULL
,
2455 blk_mq_hctx_notify_dead
);
2457 cpuhp_setup_state_nocalls(CPUHP_BLK_MQ_PREPARE
, "block/mq:prepare",
2458 blk_mq_queue_reinit_prepare
,
2459 blk_mq_queue_reinit_dead
);
2462 subsys_initcall(blk_mq_init
);