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
) {
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 blk_mq_timeout_data
{
408 struct blk_mq_hw_ctx
*hctx
;
410 unsigned int *next_set
;
413 static void blk_mq_timeout_check(void *__data
, unsigned long *free_tags
)
415 struct blk_mq_timeout_data
*data
= __data
;
416 struct blk_mq_hw_ctx
*hctx
= data
->hctx
;
419 /* It may not be in flight yet (this is where
420 * the REQ_ATOMIC_STARTED flag comes in). The requests are
421 * statically allocated, so we know it's always safe to access the
422 * memory associated with a bit offset into ->rqs[].
428 tag
= find_next_zero_bit(free_tags
, hctx
->queue_depth
, tag
);
429 if (tag
>= hctx
->queue_depth
)
432 rq
= hctx
->rqs
[tag
++];
434 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
437 blk_rq_check_expired(rq
, data
->next
, data
->next_set
);
441 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx
*hctx
,
443 unsigned int *next_set
)
445 struct blk_mq_timeout_data data
= {
448 .next_set
= next_set
,
452 * Ask the tagging code to iterate busy requests, so we can
453 * check them for timeout.
455 blk_mq_tag_busy_iter(hctx
->tags
, blk_mq_timeout_check
, &data
);
458 static void blk_mq_rq_timer(unsigned long data
)
460 struct request_queue
*q
= (struct request_queue
*) data
;
461 struct blk_mq_hw_ctx
*hctx
;
462 unsigned long next
= 0;
465 queue_for_each_hw_ctx(q
, hctx
, i
)
466 blk_mq_hw_ctx_check_timeout(hctx
, &next
, &next_set
);
469 mod_timer(&q
->timeout
, round_jiffies_up(next
));
473 * Reverse check our software queue for entries that we could potentially
474 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
475 * too much time checking for merges.
477 static bool blk_mq_attempt_merge(struct request_queue
*q
,
478 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
483 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
489 if (!blk_rq_merge_ok(rq
, bio
))
492 el_ret
= blk_try_merge(rq
, bio
);
493 if (el_ret
== ELEVATOR_BACK_MERGE
) {
494 if (bio_attempt_back_merge(q
, rq
, bio
)) {
499 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
500 if (bio_attempt_front_merge(q
, rq
, bio
)) {
511 void blk_mq_add_timer(struct request
*rq
)
513 __blk_add_timer(rq
, NULL
);
517 * Run this hardware queue, pulling any software queues mapped to it in.
518 * Note that this function currently has various problems around ordering
519 * of IO. In particular, we'd like FIFO behaviour on handling existing
520 * items on the hctx->dispatch list. Ignore that for now.
522 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
524 struct request_queue
*q
= hctx
->queue
;
525 struct blk_mq_ctx
*ctx
;
530 WARN_ON(!preempt_count());
532 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
538 * Touch any software queue that has pending entries.
540 for_each_set_bit(bit
, hctx
->ctx_map
, hctx
->nr_ctx
) {
541 clear_bit(bit
, hctx
->ctx_map
);
542 ctx
= hctx
->ctxs
[bit
];
543 BUG_ON(bit
!= ctx
->index_hw
);
545 spin_lock(&ctx
->lock
);
546 list_splice_tail_init(&ctx
->rq_list
, &rq_list
);
547 spin_unlock(&ctx
->lock
);
551 * If we have previous entries on our dispatch list, grab them
552 * and stuff them at the front for more fair dispatch.
554 if (!list_empty_careful(&hctx
->dispatch
)) {
555 spin_lock(&hctx
->lock
);
556 if (!list_empty(&hctx
->dispatch
))
557 list_splice_init(&hctx
->dispatch
, &rq_list
);
558 spin_unlock(&hctx
->lock
);
562 * Delete and return all entries from our dispatch list
567 * Now process all the entries, sending them to the driver.
569 while (!list_empty(&rq_list
)) {
572 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
573 list_del_init(&rq
->queuelist
);
575 blk_mq_start_request(rq
, list_empty(&rq_list
));
577 ret
= q
->mq_ops
->queue_rq(hctx
, rq
);
579 case BLK_MQ_RQ_QUEUE_OK
:
582 case BLK_MQ_RQ_QUEUE_BUSY
:
584 * FIXME: we should have a mechanism to stop the queue
585 * like blk_stop_queue, otherwise we will waste cpu
588 list_add(&rq
->queuelist
, &rq_list
);
589 blk_mq_requeue_request(rq
);
592 pr_err("blk-mq: bad return on queue: %d\n", ret
);
593 case BLK_MQ_RQ_QUEUE_ERROR
:
595 blk_mq_end_io(rq
, rq
->errors
);
599 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
604 hctx
->dispatched
[0]++;
605 else if (queued
< (1 << (BLK_MQ_MAX_DISPATCH_ORDER
- 1)))
606 hctx
->dispatched
[ilog2(queued
) + 1]++;
609 * Any items that need requeuing? Stuff them into hctx->dispatch,
610 * that is where we will continue on next queue run.
612 if (!list_empty(&rq_list
)) {
613 spin_lock(&hctx
->lock
);
614 list_splice(&rq_list
, &hctx
->dispatch
);
615 spin_unlock(&hctx
->lock
);
619 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
621 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
624 if (!async
&& cpumask_test_cpu(smp_processor_id(), hctx
->cpumask
))
625 __blk_mq_run_hw_queue(hctx
);
626 else if (hctx
->queue
->nr_hw_queues
== 1)
627 kblockd_schedule_delayed_work(&hctx
->delayed_work
, 0);
632 * It'd be great if the workqueue API had a way to pass
633 * in a mask and had some smarts for more clever placement
634 * than the first CPU. Or we could round-robin here. For now,
635 * just queue on the first CPU.
637 cpu
= cpumask_first(hctx
->cpumask
);
638 kblockd_schedule_delayed_work_on(cpu
, &hctx
->delayed_work
, 0);
642 void blk_mq_run_queues(struct request_queue
*q
, bool async
)
644 struct blk_mq_hw_ctx
*hctx
;
647 queue_for_each_hw_ctx(q
, hctx
, i
) {
648 if ((!blk_mq_hctx_has_pending(hctx
) &&
649 list_empty_careful(&hctx
->dispatch
)) ||
650 test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
654 blk_mq_run_hw_queue(hctx
, async
);
658 EXPORT_SYMBOL(blk_mq_run_queues
);
660 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
662 cancel_delayed_work(&hctx
->delayed_work
);
663 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
665 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
667 void blk_mq_stop_hw_queues(struct request_queue
*q
)
669 struct blk_mq_hw_ctx
*hctx
;
672 queue_for_each_hw_ctx(q
, hctx
, i
)
673 blk_mq_stop_hw_queue(hctx
);
675 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
677 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
679 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
682 __blk_mq_run_hw_queue(hctx
);
685 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
687 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
)
689 struct blk_mq_hw_ctx
*hctx
;
692 queue_for_each_hw_ctx(q
, hctx
, i
) {
693 if (!test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
696 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
698 blk_mq_run_hw_queue(hctx
, true);
702 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
704 static void blk_mq_work_fn(struct work_struct
*work
)
706 struct blk_mq_hw_ctx
*hctx
;
708 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delayed_work
.work
);
711 __blk_mq_run_hw_queue(hctx
);
715 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
716 struct request
*rq
, bool at_head
)
718 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
720 trace_block_rq_insert(hctx
->queue
, rq
);
723 list_add(&rq
->queuelist
, &ctx
->rq_list
);
725 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
726 blk_mq_hctx_mark_pending(hctx
, ctx
);
729 * We do this early, to ensure we are on the right CPU.
731 blk_mq_add_timer(rq
);
734 void blk_mq_insert_request(struct request
*rq
, bool at_head
, bool run_queue
,
737 struct request_queue
*q
= rq
->q
;
738 struct blk_mq_hw_ctx
*hctx
;
739 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
, *current_ctx
;
741 current_ctx
= blk_mq_get_ctx(q
);
742 if (!cpu_online(ctx
->cpu
))
743 rq
->mq_ctx
= ctx
= current_ctx
;
745 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
747 if (rq
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
) &&
748 !(rq
->cmd_flags
& (REQ_FLUSH_SEQ
))) {
749 blk_insert_flush(rq
);
751 spin_lock(&ctx
->lock
);
752 __blk_mq_insert_request(hctx
, rq
, at_head
);
753 spin_unlock(&ctx
->lock
);
757 blk_mq_run_hw_queue(hctx
, async
);
759 blk_mq_put_ctx(current_ctx
);
762 static void blk_mq_insert_requests(struct request_queue
*q
,
763 struct blk_mq_ctx
*ctx
,
764 struct list_head
*list
,
769 struct blk_mq_hw_ctx
*hctx
;
770 struct blk_mq_ctx
*current_ctx
;
772 trace_block_unplug(q
, depth
, !from_schedule
);
774 current_ctx
= blk_mq_get_ctx(q
);
776 if (!cpu_online(ctx
->cpu
))
778 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
781 * preemption doesn't flush plug list, so it's possible ctx->cpu is
784 spin_lock(&ctx
->lock
);
785 while (!list_empty(list
)) {
788 rq
= list_first_entry(list
, struct request
, queuelist
);
789 list_del_init(&rq
->queuelist
);
791 __blk_mq_insert_request(hctx
, rq
, false);
793 spin_unlock(&ctx
->lock
);
795 blk_mq_run_hw_queue(hctx
, from_schedule
);
796 blk_mq_put_ctx(current_ctx
);
799 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
801 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
802 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
804 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
805 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
806 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
809 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
811 struct blk_mq_ctx
*this_ctx
;
812 struct request_queue
*this_q
;
818 list_splice_init(&plug
->mq_list
, &list
);
820 list_sort(NULL
, &list
, plug_ctx_cmp
);
826 while (!list_empty(&list
)) {
827 rq
= list_entry_rq(list
.next
);
828 list_del_init(&rq
->queuelist
);
830 if (rq
->mq_ctx
!= this_ctx
) {
832 blk_mq_insert_requests(this_q
, this_ctx
,
837 this_ctx
= rq
->mq_ctx
;
843 list_add_tail(&rq
->queuelist
, &ctx_list
);
847 * If 'this_ctx' is set, we know we have entries to complete
848 * on 'ctx_list'. Do those.
851 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
856 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
858 init_request_from_bio(rq
, bio
);
859 blk_account_io_start(rq
, 1);
862 static void blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
864 struct blk_mq_hw_ctx
*hctx
;
865 struct blk_mq_ctx
*ctx
;
866 const int is_sync
= rw_is_sync(bio
->bi_rw
);
867 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
868 int rw
= bio_data_dir(bio
);
870 unsigned int use_plug
, request_count
= 0;
873 * If we have multiple hardware queues, just go directly to
874 * one of those for sync IO.
876 use_plug
= !is_flush_fua
&& ((q
->nr_hw_queues
== 1) || !is_sync
);
878 blk_queue_bounce(q
, &bio
);
880 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
881 bio_endio(bio
, -EIO
);
885 if (use_plug
&& blk_attempt_plug_merge(q
, bio
, &request_count
))
888 if (blk_mq_queue_enter(q
)) {
889 bio_endio(bio
, -EIO
);
893 ctx
= blk_mq_get_ctx(q
);
894 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
898 trace_block_getrq(q
, bio
, rw
);
899 rq
= __blk_mq_alloc_request(hctx
, GFP_ATOMIC
, false);
901 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
904 trace_block_sleeprq(q
, bio
, rw
);
905 rq
= blk_mq_alloc_request_pinned(q
, rw
, __GFP_WAIT
|GFP_ATOMIC
,
908 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
913 if (unlikely(is_flush_fua
)) {
914 blk_mq_bio_to_request(rq
, bio
);
915 blk_insert_flush(rq
);
920 * A task plug currently exists. Since this is completely lockless,
921 * utilize that to temporarily store requests until the task is
922 * either done or scheduled away.
925 struct blk_plug
*plug
= current
->plug
;
928 blk_mq_bio_to_request(rq
, bio
);
929 if (list_empty(&plug
->mq_list
))
931 else if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
932 blk_flush_plug_list(plug
, false);
935 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
941 spin_lock(&ctx
->lock
);
943 if ((hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
) &&
944 blk_mq_attempt_merge(q
, ctx
, bio
))
945 __blk_mq_free_request(hctx
, ctx
, rq
);
947 blk_mq_bio_to_request(rq
, bio
);
948 __blk_mq_insert_request(hctx
, rq
, false);
951 spin_unlock(&ctx
->lock
);
954 * For a SYNC request, send it to the hardware immediately. For an
955 * ASYNC request, just ensure that we run it later on. The latter
956 * allows for merging opportunities and more efficient dispatching.
959 blk_mq_run_hw_queue(hctx
, !is_sync
|| is_flush_fua
);
964 * Default mapping to a software queue, since we use one per CPU.
966 struct blk_mq_hw_ctx
*blk_mq_map_queue(struct request_queue
*q
, const int cpu
)
968 return q
->queue_hw_ctx
[q
->mq_map
[cpu
]];
970 EXPORT_SYMBOL(blk_mq_map_queue
);
972 struct blk_mq_hw_ctx
*blk_mq_alloc_single_hw_queue(struct blk_mq_reg
*reg
,
973 unsigned int hctx_index
)
975 return kmalloc_node(sizeof(struct blk_mq_hw_ctx
),
976 GFP_KERNEL
| __GFP_ZERO
, reg
->numa_node
);
978 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue
);
980 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx
*hctx
,
981 unsigned int hctx_index
)
985 EXPORT_SYMBOL(blk_mq_free_single_hw_queue
);
987 static void blk_mq_hctx_notify(void *data
, unsigned long action
,
990 struct blk_mq_hw_ctx
*hctx
= data
;
991 struct request_queue
*q
= hctx
->queue
;
992 struct blk_mq_ctx
*ctx
;
995 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
)
999 * Move ctx entries to new CPU, if this one is going away.
1001 ctx
= __blk_mq_get_ctx(q
, cpu
);
1003 spin_lock(&ctx
->lock
);
1004 if (!list_empty(&ctx
->rq_list
)) {
1005 list_splice_init(&ctx
->rq_list
, &tmp
);
1006 clear_bit(ctx
->index_hw
, hctx
->ctx_map
);
1008 spin_unlock(&ctx
->lock
);
1010 if (list_empty(&tmp
))
1013 ctx
= blk_mq_get_ctx(q
);
1014 spin_lock(&ctx
->lock
);
1016 while (!list_empty(&tmp
)) {
1019 rq
= list_first_entry(&tmp
, struct request
, queuelist
);
1021 list_move_tail(&rq
->queuelist
, &ctx
->rq_list
);
1024 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1025 blk_mq_hctx_mark_pending(hctx
, ctx
);
1027 spin_unlock(&ctx
->lock
);
1029 blk_mq_run_hw_queue(hctx
, true);
1030 blk_mq_put_ctx(ctx
);
1033 static void blk_mq_free_rq_map(struct blk_mq_hw_ctx
*hctx
, void *driver_data
)
1037 if (hctx
->rqs
&& hctx
->queue
->mq_ops
->exit_request
) {
1040 for (i
= 0; i
< hctx
->queue_depth
; i
++) {
1043 hctx
->queue
->mq_ops
->exit_request(driver_data
, hctx
,
1048 while (!list_empty(&hctx
->page_list
)) {
1049 page
= list_first_entry(&hctx
->page_list
, struct page
, lru
);
1050 list_del_init(&page
->lru
);
1051 __free_pages(page
, page
->private);
1057 blk_mq_free_tags(hctx
->tags
);
1060 static size_t order_to_size(unsigned int order
)
1062 size_t ret
= PAGE_SIZE
;
1070 static int blk_mq_init_rq_map(struct blk_mq_hw_ctx
*hctx
,
1071 struct blk_mq_reg
*reg
, void *driver_data
, int node
)
1073 unsigned int reserved_tags
= reg
->reserved_tags
;
1074 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1075 size_t rq_size
, left
;
1078 INIT_LIST_HEAD(&hctx
->page_list
);
1080 hctx
->rqs
= kmalloc_node(hctx
->queue_depth
* sizeof(struct request
*),
1086 * rq_size is the size of the request plus driver payload, rounded
1087 * to the cacheline size
1089 rq_size
= round_up(sizeof(struct request
) + hctx
->cmd_size
,
1091 left
= rq_size
* hctx
->queue_depth
;
1093 for (i
= 0; i
< hctx
->queue_depth
;) {
1094 int this_order
= max_order
;
1099 while (left
< order_to_size(this_order
- 1) && this_order
)
1103 page
= alloc_pages_node(node
, GFP_KERNEL
, this_order
);
1108 if (order_to_size(this_order
) < rq_size
)
1115 page
->private = this_order
;
1116 list_add_tail(&page
->lru
, &hctx
->page_list
);
1118 p
= page_address(page
);
1119 entries_per_page
= order_to_size(this_order
) / rq_size
;
1120 to_do
= min(entries_per_page
, hctx
->queue_depth
- i
);
1121 left
-= to_do
* rq_size
;
1122 for (j
= 0; j
< to_do
; j
++) {
1124 if (reg
->ops
->init_request
) {
1125 error
= reg
->ops
->init_request(driver_data
,
1126 hctx
, hctx
->rqs
[i
], i
);
1136 if (i
< (reserved_tags
+ BLK_MQ_TAG_MIN
)) {
1140 if (i
!= hctx
->queue_depth
) {
1141 hctx
->queue_depth
= i
;
1142 pr_warn("%s: queue depth set to %u because of low memory\n",
1146 hctx
->tags
= blk_mq_init_tags(hctx
->queue_depth
, reserved_tags
, node
);
1154 blk_mq_free_rq_map(hctx
, driver_data
);
1158 static int blk_mq_init_hw_queues(struct request_queue
*q
,
1159 struct blk_mq_reg
*reg
, void *driver_data
)
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
= reg
->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
= reg
->flags
;
1181 hctx
->queue_depth
= reg
->queue_depth
;
1182 hctx
->cmd_size
= reg
->cmd_size
;
1184 blk_mq_init_cpu_notifier(&hctx
->cpu_notifier
,
1185 blk_mq_hctx_notify
, hctx
);
1186 blk_mq_register_cpu_notifier(&hctx
->cpu_notifier
);
1188 if (blk_mq_init_rq_map(hctx
, reg
, driver_data
, node
))
1192 * Allocate space for all possible cpus to avoid allocation in
1195 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1200 num_maps
= ALIGN(nr_cpu_ids
, BITS_PER_LONG
) / BITS_PER_LONG
;
1201 hctx
->ctx_map
= kzalloc_node(num_maps
* sizeof(unsigned long),
1206 hctx
->nr_ctx_map
= num_maps
;
1209 if (reg
->ops
->init_hctx
&&
1210 reg
->ops
->init_hctx(hctx
, driver_data
, i
))
1214 if (i
== q
->nr_hw_queues
)
1220 queue_for_each_hw_ctx(q
, hctx
, j
) {
1224 if (reg
->ops
->exit_hctx
)
1225 reg
->ops
->exit_hctx(hctx
, j
);
1227 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1228 blk_mq_free_rq_map(hctx
, driver_data
);
1235 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1236 unsigned int nr_hw_queues
)
1240 for_each_possible_cpu(i
) {
1241 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1242 struct blk_mq_hw_ctx
*hctx
;
1244 memset(__ctx
, 0, sizeof(*__ctx
));
1246 spin_lock_init(&__ctx
->lock
);
1247 INIT_LIST_HEAD(&__ctx
->rq_list
);
1250 /* If the cpu isn't online, the cpu is mapped to first hctx */
1254 hctx
= q
->mq_ops
->map_queue(q
, i
);
1255 cpumask_set_cpu(i
, hctx
->cpumask
);
1259 * Set local node, IFF we have more than one hw queue. If
1260 * not, we remain on the home node of the device
1262 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1263 hctx
->numa_node
= cpu_to_node(i
);
1267 static void blk_mq_map_swqueue(struct request_queue
*q
)
1270 struct blk_mq_hw_ctx
*hctx
;
1271 struct blk_mq_ctx
*ctx
;
1273 queue_for_each_hw_ctx(q
, hctx
, i
) {
1274 cpumask_clear(hctx
->cpumask
);
1279 * Map software to hardware queues
1281 queue_for_each_ctx(q
, ctx
, i
) {
1282 /* If the cpu isn't online, the cpu is mapped to first hctx */
1286 hctx
= q
->mq_ops
->map_queue(q
, i
);
1287 cpumask_set_cpu(i
, hctx
->cpumask
);
1288 ctx
->index_hw
= hctx
->nr_ctx
;
1289 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1293 struct request_queue
*blk_mq_init_queue(struct blk_mq_reg
*reg
,
1296 struct blk_mq_hw_ctx
**hctxs
;
1297 struct blk_mq_ctx
*ctx
;
1298 struct request_queue
*q
;
1301 if (!reg
->nr_hw_queues
||
1302 !reg
->ops
->queue_rq
|| !reg
->ops
->map_queue
||
1303 !reg
->ops
->alloc_hctx
|| !reg
->ops
->free_hctx
)
1304 return ERR_PTR(-EINVAL
);
1306 if (!reg
->queue_depth
)
1307 reg
->queue_depth
= BLK_MQ_MAX_DEPTH
;
1308 else if (reg
->queue_depth
> BLK_MQ_MAX_DEPTH
) {
1309 pr_err("blk-mq: queuedepth too large (%u)\n", reg
->queue_depth
);
1310 reg
->queue_depth
= BLK_MQ_MAX_DEPTH
;
1313 if (reg
->queue_depth
< (reg
->reserved_tags
+ BLK_MQ_TAG_MIN
))
1314 return ERR_PTR(-EINVAL
);
1316 ctx
= alloc_percpu(struct blk_mq_ctx
);
1318 return ERR_PTR(-ENOMEM
);
1320 hctxs
= kmalloc_node(reg
->nr_hw_queues
* sizeof(*hctxs
), GFP_KERNEL
,
1326 for (i
= 0; i
< reg
->nr_hw_queues
; i
++) {
1327 hctxs
[i
] = reg
->ops
->alloc_hctx(reg
, i
);
1331 if (!zalloc_cpumask_var(&hctxs
[i
]->cpumask
, GFP_KERNEL
))
1334 hctxs
[i
]->numa_node
= NUMA_NO_NODE
;
1335 hctxs
[i
]->queue_num
= i
;
1338 q
= blk_alloc_queue_node(GFP_KERNEL
, reg
->numa_node
);
1342 q
->mq_map
= blk_mq_make_queue_map(reg
);
1346 setup_timer(&q
->timeout
, blk_mq_rq_timer
, (unsigned long) q
);
1347 blk_queue_rq_timeout(q
, 30000);
1349 q
->nr_queues
= nr_cpu_ids
;
1350 q
->nr_hw_queues
= reg
->nr_hw_queues
;
1353 q
->queue_hw_ctx
= hctxs
;
1355 q
->mq_ops
= reg
->ops
;
1356 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
1358 q
->sg_reserved_size
= INT_MAX
;
1360 blk_queue_make_request(q
, blk_mq_make_request
);
1361 blk_queue_rq_timed_out(q
, reg
->ops
->timeout
);
1363 blk_queue_rq_timeout(q
, reg
->timeout
);
1365 if (reg
->ops
->complete
)
1366 blk_queue_softirq_done(q
, reg
->ops
->complete
);
1368 blk_mq_init_flush(q
);
1369 blk_mq_init_cpu_queues(q
, reg
->nr_hw_queues
);
1371 q
->flush_rq
= kzalloc(round_up(sizeof(struct request
) + reg
->cmd_size
,
1372 cache_line_size()), GFP_KERNEL
);
1376 if (blk_mq_init_hw_queues(q
, reg
, driver_data
))
1379 blk_mq_map_swqueue(q
);
1381 mutex_lock(&all_q_mutex
);
1382 list_add_tail(&q
->all_q_node
, &all_q_list
);
1383 mutex_unlock(&all_q_mutex
);
1392 blk_cleanup_queue(q
);
1394 for (i
= 0; i
< reg
->nr_hw_queues
; i
++) {
1397 free_cpumask_var(hctxs
[i
]->cpumask
);
1398 reg
->ops
->free_hctx(hctxs
[i
], i
);
1403 return ERR_PTR(-ENOMEM
);
1405 EXPORT_SYMBOL(blk_mq_init_queue
);
1407 void blk_mq_free_queue(struct request_queue
*q
)
1409 struct blk_mq_hw_ctx
*hctx
;
1412 queue_for_each_hw_ctx(q
, hctx
, i
) {
1413 kfree(hctx
->ctx_map
);
1415 blk_mq_free_rq_map(hctx
, q
->queuedata
);
1416 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1417 if (q
->mq_ops
->exit_hctx
)
1418 q
->mq_ops
->exit_hctx(hctx
, i
);
1419 free_cpumask_var(hctx
->cpumask
);
1420 q
->mq_ops
->free_hctx(hctx
, i
);
1423 free_percpu(q
->queue_ctx
);
1424 kfree(q
->queue_hw_ctx
);
1427 q
->queue_ctx
= NULL
;
1428 q
->queue_hw_ctx
= NULL
;
1431 mutex_lock(&all_q_mutex
);
1432 list_del_init(&q
->all_q_node
);
1433 mutex_unlock(&all_q_mutex
);
1436 /* Basically redo blk_mq_init_queue with queue frozen */
1437 static void blk_mq_queue_reinit(struct request_queue
*q
)
1439 blk_mq_freeze_queue(q
);
1441 blk_mq_update_queue_map(q
->mq_map
, q
->nr_hw_queues
);
1444 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1445 * we should change hctx numa_node according to new topology (this
1446 * involves free and re-allocate memory, worthy doing?)
1449 blk_mq_map_swqueue(q
);
1451 blk_mq_unfreeze_queue(q
);
1454 static int blk_mq_queue_reinit_notify(struct notifier_block
*nb
,
1455 unsigned long action
, void *hcpu
)
1457 struct request_queue
*q
;
1460 * Before new mapping is established, hotadded cpu might already start
1461 * handling requests. This doesn't break anything as we map offline
1462 * CPUs to first hardware queue. We will re-init queue below to get
1465 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
&&
1466 action
!= CPU_ONLINE
&& action
!= CPU_ONLINE_FROZEN
)
1469 mutex_lock(&all_q_mutex
);
1470 list_for_each_entry(q
, &all_q_list
, all_q_node
)
1471 blk_mq_queue_reinit(q
);
1472 mutex_unlock(&all_q_mutex
);
1476 void blk_mq_disable_hotplug(void)
1478 mutex_lock(&all_q_mutex
);
1481 void blk_mq_enable_hotplug(void)
1483 mutex_unlock(&all_q_mutex
);
1486 static int __init
blk_mq_init(void)
1490 /* Must be called after percpu_counter_hotcpu_callback() */
1491 hotcpu_notifier(blk_mq_queue_reinit_notify
, -10);
1495 subsys_initcall(blk_mq_init
);