1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/backing-dev.h>
5 #include <linux/blkdev.h>
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/workqueue.h>
10 #include <linux/smp.h>
11 #include <linux/llist.h>
12 #include <linux/list_sort.h>
13 #include <linux/cpu.h>
14 #include <linux/cache.h>
15 #include <linux/sched/sysctl.h>
16 #include <linux/delay.h>
18 #include <trace/events/block.h>
20 #include <linux/blk-mq.h>
23 #include "blk-mq-tag.h"
25 static DEFINE_MUTEX(all_q_mutex
);
26 static LIST_HEAD(all_q_list
);
28 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
);
30 static struct blk_mq_ctx
*__blk_mq_get_ctx(struct request_queue
*q
,
33 return per_cpu_ptr(q
->queue_ctx
, cpu
);
37 * This assumes per-cpu software queueing queues. They could be per-node
38 * as well, for instance. For now this is hardcoded as-is. Note that we don't
39 * care about preemption, since we know the ctx's are persistent. This does
40 * mean that we can't rely on ctx always matching the currently running CPU.
42 static struct blk_mq_ctx
*blk_mq_get_ctx(struct request_queue
*q
)
44 return __blk_mq_get_ctx(q
, get_cpu());
47 static void blk_mq_put_ctx(struct blk_mq_ctx
*ctx
)
53 * Check if any of the ctx's have pending work in this hardware queue
55 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx
*hctx
)
59 for (i
= 0; i
< hctx
->nr_ctx_map
; i
++)
67 * Mark this ctx as having pending work in this hardware queue
69 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx
*hctx
,
70 struct blk_mq_ctx
*ctx
)
72 if (!test_bit(ctx
->index_hw
, hctx
->ctx_map
))
73 set_bit(ctx
->index_hw
, hctx
->ctx_map
);
76 static struct request
*__blk_mq_alloc_request(struct blk_mq_hw_ctx
*hctx
,
77 gfp_t gfp
, bool reserved
)
82 tag
= blk_mq_get_tag(hctx
->tags
, gfp
, reserved
);
83 if (tag
!= BLK_MQ_TAG_FAIL
) {
84 rq
= hctx
->tags
->rqs
[tag
];
85 blk_rq_init(hctx
->queue
, rq
);
94 static int blk_mq_queue_enter(struct request_queue
*q
)
98 __percpu_counter_add(&q
->mq_usage_counter
, 1, 1000000);
100 /* we have problems to freeze the queue if it's initializing */
101 if (!blk_queue_bypass(q
) || !blk_queue_init_done(q
))
104 __percpu_counter_add(&q
->mq_usage_counter
, -1, 1000000);
106 spin_lock_irq(q
->queue_lock
);
107 ret
= wait_event_interruptible_lock_irq(q
->mq_freeze_wq
,
108 !blk_queue_bypass(q
) || blk_queue_dying(q
),
110 /* inc usage with lock hold to avoid freeze_queue runs here */
111 if (!ret
&& !blk_queue_dying(q
))
112 __percpu_counter_add(&q
->mq_usage_counter
, 1, 1000000);
113 else if (blk_queue_dying(q
))
115 spin_unlock_irq(q
->queue_lock
);
120 static void blk_mq_queue_exit(struct request_queue
*q
)
122 __percpu_counter_add(&q
->mq_usage_counter
, -1, 1000000);
125 static void __blk_mq_drain_queue(struct request_queue
*q
)
130 spin_lock_irq(q
->queue_lock
);
131 count
= percpu_counter_sum(&q
->mq_usage_counter
);
132 spin_unlock_irq(q
->queue_lock
);
136 blk_mq_run_queues(q
, false);
142 * Guarantee no request is in use, so we can change any data structure of
143 * the queue afterward.
145 static void blk_mq_freeze_queue(struct request_queue
*q
)
149 spin_lock_irq(q
->queue_lock
);
150 drain
= !q
->bypass_depth
++;
151 queue_flag_set(QUEUE_FLAG_BYPASS
, q
);
152 spin_unlock_irq(q
->queue_lock
);
155 __blk_mq_drain_queue(q
);
158 void blk_mq_drain_queue(struct request_queue
*q
)
160 __blk_mq_drain_queue(q
);
163 static void blk_mq_unfreeze_queue(struct request_queue
*q
)
167 spin_lock_irq(q
->queue_lock
);
168 if (!--q
->bypass_depth
) {
169 queue_flag_clear(QUEUE_FLAG_BYPASS
, q
);
172 WARN_ON_ONCE(q
->bypass_depth
< 0);
173 spin_unlock_irq(q
->queue_lock
);
175 wake_up_all(&q
->mq_freeze_wq
);
178 bool blk_mq_can_queue(struct blk_mq_hw_ctx
*hctx
)
180 return blk_mq_has_free_tags(hctx
->tags
);
182 EXPORT_SYMBOL(blk_mq_can_queue
);
184 static void blk_mq_rq_ctx_init(struct request_queue
*q
, struct blk_mq_ctx
*ctx
,
185 struct request
*rq
, unsigned int rw_flags
)
187 if (blk_queue_io_stat(q
))
188 rw_flags
|= REQ_IO_STAT
;
191 rq
->cmd_flags
= rw_flags
;
192 rq
->start_time
= jiffies
;
193 set_start_time_ns(rq
);
194 ctx
->rq_dispatched
[rw_is_sync(rw_flags
)]++;
197 static struct request
*blk_mq_alloc_request_pinned(struct request_queue
*q
,
204 struct blk_mq_ctx
*ctx
= blk_mq_get_ctx(q
);
205 struct blk_mq_hw_ctx
*hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
207 rq
= __blk_mq_alloc_request(hctx
, gfp
& ~__GFP_WAIT
, reserved
);
209 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
213 if (gfp
& __GFP_WAIT
) {
214 __blk_mq_run_hw_queue(hctx
);
221 blk_mq_wait_for_tags(hctx
->tags
);
227 struct request
*blk_mq_alloc_request(struct request_queue
*q
, int rw
, gfp_t gfp
)
231 if (blk_mq_queue_enter(q
))
234 rq
= blk_mq_alloc_request_pinned(q
, rw
, gfp
, false);
236 blk_mq_put_ctx(rq
->mq_ctx
);
240 struct request
*blk_mq_alloc_reserved_request(struct request_queue
*q
, int rw
,
245 if (blk_mq_queue_enter(q
))
248 rq
= blk_mq_alloc_request_pinned(q
, rw
, gfp
, true);
250 blk_mq_put_ctx(rq
->mq_ctx
);
253 EXPORT_SYMBOL(blk_mq_alloc_reserved_request
);
255 static void __blk_mq_free_request(struct blk_mq_hw_ctx
*hctx
,
256 struct blk_mq_ctx
*ctx
, struct request
*rq
)
258 const int tag
= rq
->tag
;
259 struct request_queue
*q
= rq
->q
;
261 blk_mq_put_tag(hctx
->tags
, tag
);
262 blk_mq_queue_exit(q
);
265 void blk_mq_free_request(struct request
*rq
)
267 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
268 struct blk_mq_hw_ctx
*hctx
;
269 struct request_queue
*q
= rq
->q
;
271 ctx
->rq_completed
[rq_is_sync(rq
)]++;
273 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
274 __blk_mq_free_request(hctx
, ctx
, rq
);
278 * Clone all relevant state from a request that has been put on hold in
279 * the flush state machine into the preallocated flush request that hangs
280 * off the request queue.
282 * For a driver the flush request should be invisible, that's why we are
283 * impersonating the original request here.
285 void blk_mq_clone_flush_request(struct request
*flush_rq
,
286 struct request
*orig_rq
)
288 struct blk_mq_hw_ctx
*hctx
=
289 orig_rq
->q
->mq_ops
->map_queue(orig_rq
->q
, orig_rq
->mq_ctx
->cpu
);
291 flush_rq
->mq_ctx
= orig_rq
->mq_ctx
;
292 flush_rq
->tag
= orig_rq
->tag
;
293 memcpy(blk_mq_rq_to_pdu(flush_rq
), blk_mq_rq_to_pdu(orig_rq
),
297 bool blk_mq_end_io_partial(struct request
*rq
, int error
, unsigned int nr_bytes
)
299 if (blk_update_request(rq
, error
, blk_rq_bytes(rq
)))
302 blk_account_io_done(rq
);
305 rq
->end_io(rq
, error
);
307 blk_mq_free_request(rq
);
310 EXPORT_SYMBOL(blk_mq_end_io_partial
);
312 static void __blk_mq_complete_request_remote(void *data
)
314 struct request
*rq
= data
;
316 rq
->q
->softirq_done_fn(rq
);
319 void __blk_mq_complete_request(struct request
*rq
)
321 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
324 if (!ctx
->ipi_redirect
) {
325 rq
->q
->softirq_done_fn(rq
);
330 if (cpu
!= ctx
->cpu
&& cpu_online(ctx
->cpu
)) {
331 rq
->csd
.func
= __blk_mq_complete_request_remote
;
334 smp_call_function_single_async(ctx
->cpu
, &rq
->csd
);
336 rq
->q
->softirq_done_fn(rq
);
342 * blk_mq_complete_request - end I/O on a request
343 * @rq: the request being processed
346 * Ends all I/O on a request. It does not handle partial completions.
347 * The actual completion happens out-of-order, through a IPI handler.
349 void blk_mq_complete_request(struct request
*rq
)
351 if (unlikely(blk_should_fake_timeout(rq
->q
)))
353 if (!blk_mark_rq_complete(rq
))
354 __blk_mq_complete_request(rq
);
356 EXPORT_SYMBOL(blk_mq_complete_request
);
358 static void blk_mq_start_request(struct request
*rq
, bool last
)
360 struct request_queue
*q
= rq
->q
;
362 trace_block_rq_issue(q
, rq
);
364 rq
->resid_len
= blk_rq_bytes(rq
);
367 * Just mark start time and set the started bit. Due to memory
368 * ordering, we know we'll see the correct deadline as long as
369 * REQ_ATOMIC_STARTED is seen.
371 rq
->deadline
= jiffies
+ q
->rq_timeout
;
372 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
374 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
376 * Make sure space for the drain appears. We know we can do
377 * this because max_hw_segments has been adjusted to be one
378 * fewer than the device can handle.
380 rq
->nr_phys_segments
++;
384 * Flag the last request in the series so that drivers know when IO
385 * should be kicked off, if they don't do it on a per-request basis.
387 * Note: the flag isn't the only condition drivers should do kick off.
388 * If drive is busy, the last request might not have the bit set.
391 rq
->cmd_flags
|= REQ_END
;
394 static void blk_mq_requeue_request(struct request
*rq
)
396 struct request_queue
*q
= rq
->q
;
398 trace_block_rq_requeue(q
, rq
);
399 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
401 rq
->cmd_flags
&= ~REQ_END
;
403 if (q
->dma_drain_size
&& blk_rq_bytes(rq
))
404 rq
->nr_phys_segments
--;
407 struct request
*blk_mq_tag_to_rq(struct blk_mq_tags
*tags
, unsigned int tag
)
409 return tags
->rqs
[tag
];
411 EXPORT_SYMBOL(blk_mq_tag_to_rq
);
413 struct blk_mq_timeout_data
{
414 struct blk_mq_hw_ctx
*hctx
;
416 unsigned int *next_set
;
419 static void blk_mq_timeout_check(void *__data
, unsigned long *free_tags
)
421 struct blk_mq_timeout_data
*data
= __data
;
422 struct blk_mq_hw_ctx
*hctx
= data
->hctx
;
425 /* It may not be in flight yet (this is where
426 * the REQ_ATOMIC_STARTED flag comes in). The requests are
427 * statically allocated, so we know it's always safe to access the
428 * memory associated with a bit offset into ->rqs[].
434 tag
= find_next_zero_bit(free_tags
, hctx
->tags
->nr_tags
, tag
);
435 if (tag
>= hctx
->tags
->nr_tags
)
438 rq
= blk_mq_tag_to_rq(hctx
->tags
, tag
++);
439 if (rq
->q
!= hctx
->queue
)
441 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
444 blk_rq_check_expired(rq
, data
->next
, data
->next_set
);
448 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx
*hctx
,
450 unsigned int *next_set
)
452 struct blk_mq_timeout_data data
= {
455 .next_set
= next_set
,
459 * Ask the tagging code to iterate busy requests, so we can
460 * check them for timeout.
462 blk_mq_tag_busy_iter(hctx
->tags
, blk_mq_timeout_check
, &data
);
465 static void blk_mq_rq_timer(unsigned long data
)
467 struct request_queue
*q
= (struct request_queue
*) data
;
468 struct blk_mq_hw_ctx
*hctx
;
469 unsigned long next
= 0;
472 queue_for_each_hw_ctx(q
, hctx
, i
)
473 blk_mq_hw_ctx_check_timeout(hctx
, &next
, &next_set
);
476 mod_timer(&q
->timeout
, round_jiffies_up(next
));
480 * Reverse check our software queue for entries that we could potentially
481 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
482 * too much time checking for merges.
484 static bool blk_mq_attempt_merge(struct request_queue
*q
,
485 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
490 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
496 if (!blk_rq_merge_ok(rq
, bio
))
499 el_ret
= blk_try_merge(rq
, bio
);
500 if (el_ret
== ELEVATOR_BACK_MERGE
) {
501 if (bio_attempt_back_merge(q
, rq
, bio
)) {
506 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
507 if (bio_attempt_front_merge(q
, rq
, bio
)) {
518 void blk_mq_add_timer(struct request
*rq
)
520 __blk_add_timer(rq
, NULL
);
524 * Run this hardware queue, pulling any software queues mapped to it in.
525 * Note that this function currently has various problems around ordering
526 * of IO. In particular, we'd like FIFO behaviour on handling existing
527 * items on the hctx->dispatch list. Ignore that for now.
529 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
531 struct request_queue
*q
= hctx
->queue
;
532 struct blk_mq_ctx
*ctx
;
537 WARN_ON(!preempt_count());
539 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
545 * Touch any software queue that has pending entries.
547 for_each_set_bit(bit
, hctx
->ctx_map
, hctx
->nr_ctx
) {
548 clear_bit(bit
, hctx
->ctx_map
);
549 ctx
= hctx
->ctxs
[bit
];
550 BUG_ON(bit
!= ctx
->index_hw
);
552 spin_lock(&ctx
->lock
);
553 list_splice_tail_init(&ctx
->rq_list
, &rq_list
);
554 spin_unlock(&ctx
->lock
);
558 * If we have previous entries on our dispatch list, grab them
559 * and stuff them at the front for more fair dispatch.
561 if (!list_empty_careful(&hctx
->dispatch
)) {
562 spin_lock(&hctx
->lock
);
563 if (!list_empty(&hctx
->dispatch
))
564 list_splice_init(&hctx
->dispatch
, &rq_list
);
565 spin_unlock(&hctx
->lock
);
569 * Delete and return all entries from our dispatch list
574 * Now process all the entries, sending them to the driver.
576 while (!list_empty(&rq_list
)) {
579 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
580 list_del_init(&rq
->queuelist
);
582 blk_mq_start_request(rq
, list_empty(&rq_list
));
584 ret
= q
->mq_ops
->queue_rq(hctx
, rq
);
586 case BLK_MQ_RQ_QUEUE_OK
:
589 case BLK_MQ_RQ_QUEUE_BUSY
:
591 * FIXME: we should have a mechanism to stop the queue
592 * like blk_stop_queue, otherwise we will waste cpu
595 list_add(&rq
->queuelist
, &rq_list
);
596 blk_mq_requeue_request(rq
);
599 pr_err("blk-mq: bad return on queue: %d\n", ret
);
600 case BLK_MQ_RQ_QUEUE_ERROR
:
602 blk_mq_end_io(rq
, rq
->errors
);
606 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
611 hctx
->dispatched
[0]++;
612 else if (queued
< (1 << (BLK_MQ_MAX_DISPATCH_ORDER
- 1)))
613 hctx
->dispatched
[ilog2(queued
) + 1]++;
616 * Any items that need requeuing? Stuff them into hctx->dispatch,
617 * that is where we will continue on next queue run.
619 if (!list_empty(&rq_list
)) {
620 spin_lock(&hctx
->lock
);
621 list_splice(&rq_list
, &hctx
->dispatch
);
622 spin_unlock(&hctx
->lock
);
626 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
628 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
631 if (!async
&& cpumask_test_cpu(smp_processor_id(), hctx
->cpumask
))
632 __blk_mq_run_hw_queue(hctx
);
633 else if (hctx
->queue
->nr_hw_queues
== 1)
634 kblockd_schedule_delayed_work(&hctx
->delayed_work
, 0);
639 * It'd be great if the workqueue API had a way to pass
640 * in a mask and had some smarts for more clever placement
641 * than the first CPU. Or we could round-robin here. For now,
642 * just queue on the first CPU.
644 cpu
= cpumask_first(hctx
->cpumask
);
645 kblockd_schedule_delayed_work_on(cpu
, &hctx
->delayed_work
, 0);
649 void blk_mq_run_queues(struct request_queue
*q
, bool async
)
651 struct blk_mq_hw_ctx
*hctx
;
654 queue_for_each_hw_ctx(q
, hctx
, i
) {
655 if ((!blk_mq_hctx_has_pending(hctx
) &&
656 list_empty_careful(&hctx
->dispatch
)) ||
657 test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
661 blk_mq_run_hw_queue(hctx
, async
);
665 EXPORT_SYMBOL(blk_mq_run_queues
);
667 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
669 cancel_delayed_work(&hctx
->delayed_work
);
670 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
672 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
674 void blk_mq_stop_hw_queues(struct request_queue
*q
)
676 struct blk_mq_hw_ctx
*hctx
;
679 queue_for_each_hw_ctx(q
, hctx
, i
)
680 blk_mq_stop_hw_queue(hctx
);
682 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
684 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
686 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
689 __blk_mq_run_hw_queue(hctx
);
692 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
694 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
)
696 struct blk_mq_hw_ctx
*hctx
;
699 queue_for_each_hw_ctx(q
, hctx
, i
) {
700 if (!test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
703 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
705 blk_mq_run_hw_queue(hctx
, true);
709 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
711 static void blk_mq_work_fn(struct work_struct
*work
)
713 struct blk_mq_hw_ctx
*hctx
;
715 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delayed_work
.work
);
718 __blk_mq_run_hw_queue(hctx
);
722 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
723 struct request
*rq
, bool at_head
)
725 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
727 trace_block_rq_insert(hctx
->queue
, rq
);
730 list_add(&rq
->queuelist
, &ctx
->rq_list
);
732 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
733 blk_mq_hctx_mark_pending(hctx
, ctx
);
736 * We do this early, to ensure we are on the right CPU.
738 blk_mq_add_timer(rq
);
741 void blk_mq_insert_request(struct request
*rq
, bool at_head
, bool run_queue
,
744 struct request_queue
*q
= rq
->q
;
745 struct blk_mq_hw_ctx
*hctx
;
746 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
, *current_ctx
;
748 current_ctx
= blk_mq_get_ctx(q
);
749 if (!cpu_online(ctx
->cpu
))
750 rq
->mq_ctx
= ctx
= current_ctx
;
752 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
754 if (rq
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
) &&
755 !(rq
->cmd_flags
& (REQ_FLUSH_SEQ
))) {
756 blk_insert_flush(rq
);
758 spin_lock(&ctx
->lock
);
759 __blk_mq_insert_request(hctx
, rq
, at_head
);
760 spin_unlock(&ctx
->lock
);
764 blk_mq_run_hw_queue(hctx
, async
);
766 blk_mq_put_ctx(current_ctx
);
769 static void blk_mq_insert_requests(struct request_queue
*q
,
770 struct blk_mq_ctx
*ctx
,
771 struct list_head
*list
,
776 struct blk_mq_hw_ctx
*hctx
;
777 struct blk_mq_ctx
*current_ctx
;
779 trace_block_unplug(q
, depth
, !from_schedule
);
781 current_ctx
= blk_mq_get_ctx(q
);
783 if (!cpu_online(ctx
->cpu
))
785 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
788 * preemption doesn't flush plug list, so it's possible ctx->cpu is
791 spin_lock(&ctx
->lock
);
792 while (!list_empty(list
)) {
795 rq
= list_first_entry(list
, struct request
, queuelist
);
796 list_del_init(&rq
->queuelist
);
798 __blk_mq_insert_request(hctx
, rq
, false);
800 spin_unlock(&ctx
->lock
);
802 blk_mq_run_hw_queue(hctx
, from_schedule
);
803 blk_mq_put_ctx(current_ctx
);
806 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
808 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
809 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
811 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
812 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
813 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
816 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
818 struct blk_mq_ctx
*this_ctx
;
819 struct request_queue
*this_q
;
825 list_splice_init(&plug
->mq_list
, &list
);
827 list_sort(NULL
, &list
, plug_ctx_cmp
);
833 while (!list_empty(&list
)) {
834 rq
= list_entry_rq(list
.next
);
835 list_del_init(&rq
->queuelist
);
837 if (rq
->mq_ctx
!= this_ctx
) {
839 blk_mq_insert_requests(this_q
, this_ctx
,
844 this_ctx
= rq
->mq_ctx
;
850 list_add_tail(&rq
->queuelist
, &ctx_list
);
854 * If 'this_ctx' is set, we know we have entries to complete
855 * on 'ctx_list'. Do those.
858 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
863 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
865 init_request_from_bio(rq
, bio
);
866 blk_account_io_start(rq
, 1);
869 static void blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
871 struct blk_mq_hw_ctx
*hctx
;
872 struct blk_mq_ctx
*ctx
;
873 const int is_sync
= rw_is_sync(bio
->bi_rw
);
874 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
875 int rw
= bio_data_dir(bio
);
877 unsigned int use_plug
, request_count
= 0;
880 * If we have multiple hardware queues, just go directly to
881 * one of those for sync IO.
883 use_plug
= !is_flush_fua
&& ((q
->nr_hw_queues
== 1) || !is_sync
);
885 blk_queue_bounce(q
, &bio
);
887 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
888 bio_endio(bio
, -EIO
);
892 if (use_plug
&& blk_attempt_plug_merge(q
, bio
, &request_count
))
895 if (blk_mq_queue_enter(q
)) {
896 bio_endio(bio
, -EIO
);
900 ctx
= blk_mq_get_ctx(q
);
901 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
905 trace_block_getrq(q
, bio
, rw
);
906 rq
= __blk_mq_alloc_request(hctx
, GFP_ATOMIC
, false);
908 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
911 trace_block_sleeprq(q
, bio
, rw
);
912 rq
= blk_mq_alloc_request_pinned(q
, rw
, __GFP_WAIT
|GFP_ATOMIC
,
915 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
920 if (unlikely(is_flush_fua
)) {
921 blk_mq_bio_to_request(rq
, bio
);
922 blk_insert_flush(rq
);
927 * A task plug currently exists. Since this is completely lockless,
928 * utilize that to temporarily store requests until the task is
929 * either done or scheduled away.
932 struct blk_plug
*plug
= current
->plug
;
935 blk_mq_bio_to_request(rq
, bio
);
936 if (list_empty(&plug
->mq_list
))
938 else if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
939 blk_flush_plug_list(plug
, false);
942 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
948 spin_lock(&ctx
->lock
);
950 if ((hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
) &&
951 blk_mq_attempt_merge(q
, ctx
, bio
))
952 __blk_mq_free_request(hctx
, ctx
, rq
);
954 blk_mq_bio_to_request(rq
, bio
);
955 __blk_mq_insert_request(hctx
, rq
, false);
958 spin_unlock(&ctx
->lock
);
961 * For a SYNC request, send it to the hardware immediately. For an
962 * ASYNC request, just ensure that we run it later on. The latter
963 * allows for merging opportunities and more efficient dispatching.
966 blk_mq_run_hw_queue(hctx
, !is_sync
|| is_flush_fua
);
971 * Default mapping to a software queue, since we use one per CPU.
973 struct blk_mq_hw_ctx
*blk_mq_map_queue(struct request_queue
*q
, const int cpu
)
975 return q
->queue_hw_ctx
[q
->mq_map
[cpu
]];
977 EXPORT_SYMBOL(blk_mq_map_queue
);
979 struct blk_mq_hw_ctx
*blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set
*set
,
980 unsigned int hctx_index
)
982 return kmalloc_node(sizeof(struct blk_mq_hw_ctx
),
983 GFP_KERNEL
| __GFP_ZERO
, set
->numa_node
);
985 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue
);
987 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx
*hctx
,
988 unsigned int hctx_index
)
992 EXPORT_SYMBOL(blk_mq_free_single_hw_queue
);
994 static void blk_mq_hctx_notify(void *data
, unsigned long action
,
997 struct blk_mq_hw_ctx
*hctx
= data
;
998 struct request_queue
*q
= hctx
->queue
;
999 struct blk_mq_ctx
*ctx
;
1002 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
)
1006 * Move ctx entries to new CPU, if this one is going away.
1008 ctx
= __blk_mq_get_ctx(q
, cpu
);
1010 spin_lock(&ctx
->lock
);
1011 if (!list_empty(&ctx
->rq_list
)) {
1012 list_splice_init(&ctx
->rq_list
, &tmp
);
1013 clear_bit(ctx
->index_hw
, hctx
->ctx_map
);
1015 spin_unlock(&ctx
->lock
);
1017 if (list_empty(&tmp
))
1020 ctx
= blk_mq_get_ctx(q
);
1021 spin_lock(&ctx
->lock
);
1023 while (!list_empty(&tmp
)) {
1026 rq
= list_first_entry(&tmp
, struct request
, queuelist
);
1028 list_move_tail(&rq
->queuelist
, &ctx
->rq_list
);
1031 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1032 blk_mq_hctx_mark_pending(hctx
, ctx
);
1034 spin_unlock(&ctx
->lock
);
1036 blk_mq_run_hw_queue(hctx
, true);
1037 blk_mq_put_ctx(ctx
);
1040 static void blk_mq_free_rq_map(struct blk_mq_tag_set
*set
,
1041 struct blk_mq_tags
*tags
, unsigned int hctx_idx
)
1045 if (tags
->rqs
&& set
->ops
->exit_request
) {
1048 for (i
= 0; i
< tags
->nr_tags
; i
++) {
1051 set
->ops
->exit_request(set
->driver_data
, tags
->rqs
[i
],
1056 while (!list_empty(&tags
->page_list
)) {
1057 page
= list_first_entry(&tags
->page_list
, struct page
, lru
);
1058 list_del_init(&page
->lru
);
1059 __free_pages(page
, page
->private);
1064 blk_mq_free_tags(tags
);
1067 static size_t order_to_size(unsigned int order
)
1069 size_t ret
= PAGE_SIZE
;
1077 static struct blk_mq_tags
*blk_mq_init_rq_map(struct blk_mq_tag_set
*set
,
1078 unsigned int hctx_idx
)
1080 struct blk_mq_tags
*tags
;
1081 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1082 size_t rq_size
, left
;
1084 tags
= blk_mq_init_tags(set
->queue_depth
, set
->reserved_tags
,
1089 INIT_LIST_HEAD(&tags
->page_list
);
1091 tags
->rqs
= kmalloc_node(set
->queue_depth
* sizeof(struct request
*),
1092 GFP_KERNEL
, set
->numa_node
);
1094 blk_mq_free_tags(tags
);
1099 * rq_size is the size of the request plus driver payload, rounded
1100 * to the cacheline size
1102 rq_size
= round_up(sizeof(struct request
) + set
->cmd_size
,
1104 left
= rq_size
* set
->queue_depth
;
1106 for (i
= 0; i
< set
->queue_depth
; ) {
1107 int this_order
= max_order
;
1112 while (left
< order_to_size(this_order
- 1) && this_order
)
1116 page
= alloc_pages_node(set
->numa_node
, GFP_KERNEL
,
1122 if (order_to_size(this_order
) < rq_size
)
1129 page
->private = this_order
;
1130 list_add_tail(&page
->lru
, &tags
->page_list
);
1132 p
= page_address(page
);
1133 entries_per_page
= order_to_size(this_order
) / rq_size
;
1134 to_do
= min(entries_per_page
, set
->queue_depth
- i
);
1135 left
-= to_do
* rq_size
;
1136 for (j
= 0; j
< to_do
; j
++) {
1138 if (set
->ops
->init_request
) {
1139 if (set
->ops
->init_request(set
->driver_data
,
1140 tags
->rqs
[i
], hctx_idx
, i
,
1153 pr_warn("%s: failed to allocate requests\n", __func__
);
1154 blk_mq_free_rq_map(set
, tags
, hctx_idx
);
1158 static int blk_mq_init_hw_queues(struct request_queue
*q
,
1159 struct blk_mq_tag_set
*set
)
1161 struct blk_mq_hw_ctx
*hctx
;
1165 * Initialize hardware queues
1167 queue_for_each_hw_ctx(q
, hctx
, i
) {
1168 unsigned int num_maps
;
1171 node
= hctx
->numa_node
;
1172 if (node
== NUMA_NO_NODE
)
1173 node
= hctx
->numa_node
= set
->numa_node
;
1175 INIT_DELAYED_WORK(&hctx
->delayed_work
, blk_mq_work_fn
);
1176 spin_lock_init(&hctx
->lock
);
1177 INIT_LIST_HEAD(&hctx
->dispatch
);
1179 hctx
->queue_num
= i
;
1180 hctx
->flags
= set
->flags
;
1181 hctx
->cmd_size
= set
->cmd_size
;
1183 blk_mq_init_cpu_notifier(&hctx
->cpu_notifier
,
1184 blk_mq_hctx_notify
, hctx
);
1185 blk_mq_register_cpu_notifier(&hctx
->cpu_notifier
);
1187 hctx
->tags
= set
->tags
[i
];
1190 * Allocate space for all possible cpus to avoid allocation in
1193 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1198 num_maps
= ALIGN(nr_cpu_ids
, BITS_PER_LONG
) / BITS_PER_LONG
;
1199 hctx
->ctx_map
= kzalloc_node(num_maps
* sizeof(unsigned long),
1204 hctx
->nr_ctx_map
= num_maps
;
1207 if (set
->ops
->init_hctx
&&
1208 set
->ops
->init_hctx(hctx
, set
->driver_data
, i
))
1212 if (i
== q
->nr_hw_queues
)
1218 queue_for_each_hw_ctx(q
, hctx
, j
) {
1222 if (set
->ops
->exit_hctx
)
1223 set
->ops
->exit_hctx(hctx
, j
);
1225 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1232 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1233 unsigned int nr_hw_queues
)
1237 for_each_possible_cpu(i
) {
1238 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1239 struct blk_mq_hw_ctx
*hctx
;
1241 memset(__ctx
, 0, sizeof(*__ctx
));
1243 spin_lock_init(&__ctx
->lock
);
1244 INIT_LIST_HEAD(&__ctx
->rq_list
);
1247 /* If the cpu isn't online, the cpu is mapped to first hctx */
1251 hctx
= q
->mq_ops
->map_queue(q
, i
);
1252 cpumask_set_cpu(i
, hctx
->cpumask
);
1256 * Set local node, IFF we have more than one hw queue. If
1257 * not, we remain on the home node of the device
1259 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1260 hctx
->numa_node
= cpu_to_node(i
);
1264 static void blk_mq_map_swqueue(struct request_queue
*q
)
1267 struct blk_mq_hw_ctx
*hctx
;
1268 struct blk_mq_ctx
*ctx
;
1270 queue_for_each_hw_ctx(q
, hctx
, i
) {
1271 cpumask_clear(hctx
->cpumask
);
1276 * Map software to hardware queues
1278 queue_for_each_ctx(q
, ctx
, i
) {
1279 /* If the cpu isn't online, the cpu is mapped to first hctx */
1283 hctx
= q
->mq_ops
->map_queue(q
, i
);
1284 cpumask_set_cpu(i
, hctx
->cpumask
);
1285 ctx
->index_hw
= hctx
->nr_ctx
;
1286 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1290 struct request_queue
*blk_mq_init_queue(struct blk_mq_tag_set
*set
)
1292 struct blk_mq_hw_ctx
**hctxs
;
1293 struct blk_mq_ctx
*ctx
;
1294 struct request_queue
*q
;
1297 ctx
= alloc_percpu(struct blk_mq_ctx
);
1299 return ERR_PTR(-ENOMEM
);
1301 hctxs
= kmalloc_node(set
->nr_hw_queues
* sizeof(*hctxs
), GFP_KERNEL
,
1307 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1308 hctxs
[i
] = set
->ops
->alloc_hctx(set
, i
);
1312 if (!zalloc_cpumask_var(&hctxs
[i
]->cpumask
, GFP_KERNEL
))
1315 hctxs
[i
]->numa_node
= NUMA_NO_NODE
;
1316 hctxs
[i
]->queue_num
= i
;
1319 q
= blk_alloc_queue_node(GFP_KERNEL
, set
->numa_node
);
1323 q
->mq_map
= blk_mq_make_queue_map(set
);
1327 setup_timer(&q
->timeout
, blk_mq_rq_timer
, (unsigned long) q
);
1328 blk_queue_rq_timeout(q
, 30000);
1330 q
->nr_queues
= nr_cpu_ids
;
1331 q
->nr_hw_queues
= set
->nr_hw_queues
;
1334 q
->queue_hw_ctx
= hctxs
;
1336 q
->mq_ops
= set
->ops
;
1337 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
1339 q
->sg_reserved_size
= INT_MAX
;
1341 blk_queue_make_request(q
, blk_mq_make_request
);
1342 blk_queue_rq_timed_out(q
, set
->ops
->timeout
);
1344 blk_queue_rq_timeout(q
, set
->timeout
);
1346 if (set
->ops
->complete
)
1347 blk_queue_softirq_done(q
, set
->ops
->complete
);
1349 blk_mq_init_flush(q
);
1350 blk_mq_init_cpu_queues(q
, set
->nr_hw_queues
);
1352 q
->flush_rq
= kzalloc(round_up(sizeof(struct request
) +
1353 set
->cmd_size
, cache_line_size()),
1358 if (blk_mq_init_hw_queues(q
, set
))
1361 blk_mq_map_swqueue(q
);
1363 mutex_lock(&all_q_mutex
);
1364 list_add_tail(&q
->all_q_node
, &all_q_list
);
1365 mutex_unlock(&all_q_mutex
);
1374 blk_cleanup_queue(q
);
1376 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1379 free_cpumask_var(hctxs
[i
]->cpumask
);
1380 set
->ops
->free_hctx(hctxs
[i
], i
);
1385 return ERR_PTR(-ENOMEM
);
1387 EXPORT_SYMBOL(blk_mq_init_queue
);
1389 void blk_mq_free_queue(struct request_queue
*q
)
1391 struct blk_mq_hw_ctx
*hctx
;
1394 queue_for_each_hw_ctx(q
, hctx
, i
) {
1395 kfree(hctx
->ctx_map
);
1397 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1398 if (q
->mq_ops
->exit_hctx
)
1399 q
->mq_ops
->exit_hctx(hctx
, i
);
1400 free_cpumask_var(hctx
->cpumask
);
1401 q
->mq_ops
->free_hctx(hctx
, i
);
1404 free_percpu(q
->queue_ctx
);
1405 kfree(q
->queue_hw_ctx
);
1408 q
->queue_ctx
= NULL
;
1409 q
->queue_hw_ctx
= NULL
;
1412 mutex_lock(&all_q_mutex
);
1413 list_del_init(&q
->all_q_node
);
1414 mutex_unlock(&all_q_mutex
);
1417 /* Basically redo blk_mq_init_queue with queue frozen */
1418 static void blk_mq_queue_reinit(struct request_queue
*q
)
1420 blk_mq_freeze_queue(q
);
1422 blk_mq_update_queue_map(q
->mq_map
, q
->nr_hw_queues
);
1425 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1426 * we should change hctx numa_node according to new topology (this
1427 * involves free and re-allocate memory, worthy doing?)
1430 blk_mq_map_swqueue(q
);
1432 blk_mq_unfreeze_queue(q
);
1435 static int blk_mq_queue_reinit_notify(struct notifier_block
*nb
,
1436 unsigned long action
, void *hcpu
)
1438 struct request_queue
*q
;
1441 * Before new mapping is established, hotadded cpu might already start
1442 * handling requests. This doesn't break anything as we map offline
1443 * CPUs to first hardware queue. We will re-init queue below to get
1446 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
&&
1447 action
!= CPU_ONLINE
&& action
!= CPU_ONLINE_FROZEN
)
1450 mutex_lock(&all_q_mutex
);
1451 list_for_each_entry(q
, &all_q_list
, all_q_node
)
1452 blk_mq_queue_reinit(q
);
1453 mutex_unlock(&all_q_mutex
);
1457 int blk_mq_alloc_tag_set(struct blk_mq_tag_set
*set
)
1461 if (!set
->nr_hw_queues
)
1463 if (!set
->queue_depth
|| set
->queue_depth
> BLK_MQ_MAX_DEPTH
)
1465 if (set
->queue_depth
< set
->reserved_tags
+ BLK_MQ_TAG_MIN
)
1468 if (!set
->nr_hw_queues
||
1469 !set
->ops
->queue_rq
|| !set
->ops
->map_queue
||
1470 !set
->ops
->alloc_hctx
|| !set
->ops
->free_hctx
)
1474 set
->tags
= kmalloc_node(set
->nr_hw_queues
* sizeof(struct blk_mq_tags
),
1475 GFP_KERNEL
, set
->numa_node
);
1479 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1480 set
->tags
[i
] = blk_mq_init_rq_map(set
, i
);
1489 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
1493 EXPORT_SYMBOL(blk_mq_alloc_tag_set
);
1495 void blk_mq_free_tag_set(struct blk_mq_tag_set
*set
)
1499 for (i
= 0; i
< set
->nr_hw_queues
; i
++)
1500 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
1502 EXPORT_SYMBOL(blk_mq_free_tag_set
);
1504 void blk_mq_disable_hotplug(void)
1506 mutex_lock(&all_q_mutex
);
1509 void blk_mq_enable_hotplug(void)
1511 mutex_unlock(&all_q_mutex
);
1514 static int __init
blk_mq_init(void)
1518 /* Must be called after percpu_counter_hotcpu_callback() */
1519 hotcpu_notifier(blk_mq_queue_reinit_notify
, -10);
1523 subsys_initcall(blk_mq_init
);