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
];
93 static int blk_mq_queue_enter(struct request_queue
*q
)
97 __percpu_counter_add(&q
->mq_usage_counter
, 1, 1000000);
99 /* we have problems to freeze the queue if it's initializing */
100 if (!blk_queue_bypass(q
) || !blk_queue_init_done(q
))
103 __percpu_counter_add(&q
->mq_usage_counter
, -1, 1000000);
105 spin_lock_irq(q
->queue_lock
);
106 ret
= wait_event_interruptible_lock_irq(q
->mq_freeze_wq
,
107 !blk_queue_bypass(q
) || blk_queue_dying(q
),
109 /* inc usage with lock hold to avoid freeze_queue runs here */
110 if (!ret
&& !blk_queue_dying(q
))
111 __percpu_counter_add(&q
->mq_usage_counter
, 1, 1000000);
112 else if (blk_queue_dying(q
))
114 spin_unlock_irq(q
->queue_lock
);
119 static void blk_mq_queue_exit(struct request_queue
*q
)
121 __percpu_counter_add(&q
->mq_usage_counter
, -1, 1000000);
124 static void __blk_mq_drain_queue(struct request_queue
*q
)
129 spin_lock_irq(q
->queue_lock
);
130 count
= percpu_counter_sum(&q
->mq_usage_counter
);
131 spin_unlock_irq(q
->queue_lock
);
135 blk_mq_run_queues(q
, false);
141 * Guarantee no request is in use, so we can change any data structure of
142 * the queue afterward.
144 static void blk_mq_freeze_queue(struct request_queue
*q
)
148 spin_lock_irq(q
->queue_lock
);
149 drain
= !q
->bypass_depth
++;
150 queue_flag_set(QUEUE_FLAG_BYPASS
, q
);
151 spin_unlock_irq(q
->queue_lock
);
154 __blk_mq_drain_queue(q
);
157 void blk_mq_drain_queue(struct request_queue
*q
)
159 __blk_mq_drain_queue(q
);
162 static void blk_mq_unfreeze_queue(struct request_queue
*q
)
166 spin_lock_irq(q
->queue_lock
);
167 if (!--q
->bypass_depth
) {
168 queue_flag_clear(QUEUE_FLAG_BYPASS
, q
);
171 WARN_ON_ONCE(q
->bypass_depth
< 0);
172 spin_unlock_irq(q
->queue_lock
);
174 wake_up_all(&q
->mq_freeze_wq
);
177 bool blk_mq_can_queue(struct blk_mq_hw_ctx
*hctx
)
179 return blk_mq_has_free_tags(hctx
->tags
);
181 EXPORT_SYMBOL(blk_mq_can_queue
);
183 static void blk_mq_rq_ctx_init(struct request_queue
*q
, struct blk_mq_ctx
*ctx
,
184 struct request
*rq
, unsigned int rw_flags
)
186 if (blk_queue_io_stat(q
))
187 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_rq_init(hctx
->queue
, rq
);
262 blk_mq_put_tag(hctx
->tags
, tag
);
263 blk_mq_queue_exit(q
);
266 void blk_mq_free_request(struct request
*rq
)
268 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
269 struct blk_mq_hw_ctx
*hctx
;
270 struct request_queue
*q
= rq
->q
;
272 ctx
->rq_completed
[rq_is_sync(rq
)]++;
274 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
275 __blk_mq_free_request(hctx
, ctx
, rq
);
279 * Clone all relevant state from a request that has been put on hold in
280 * the flush state machine into the preallocated flush request that hangs
281 * off the request queue.
283 * For a driver the flush request should be invisible, that's why we are
284 * impersonating the original request here.
286 void blk_mq_clone_flush_request(struct request
*flush_rq
,
287 struct request
*orig_rq
)
289 struct blk_mq_hw_ctx
*hctx
=
290 orig_rq
->q
->mq_ops
->map_queue(orig_rq
->q
, orig_rq
->mq_ctx
->cpu
);
292 flush_rq
->mq_ctx
= orig_rq
->mq_ctx
;
293 flush_rq
->tag
= orig_rq
->tag
;
294 memcpy(blk_mq_rq_to_pdu(flush_rq
), blk_mq_rq_to_pdu(orig_rq
),
298 inline void __blk_mq_end_io(struct request
*rq
, int error
)
300 blk_account_io_done(rq
);
303 rq
->end_io(rq
, error
);
305 if (unlikely(blk_bidi_rq(rq
)))
306 blk_mq_free_request(rq
->next_rq
);
307 blk_mq_free_request(rq
);
310 EXPORT_SYMBOL(__blk_mq_end_io
);
312 void blk_mq_end_io(struct request
*rq
, int error
)
314 if (blk_update_request(rq
, error
, blk_rq_bytes(rq
)))
316 __blk_mq_end_io(rq
, error
);
318 EXPORT_SYMBOL(blk_mq_end_io
);
320 static void __blk_mq_complete_request_remote(void *data
)
322 struct request
*rq
= data
;
324 rq
->q
->softirq_done_fn(rq
);
327 void __blk_mq_complete_request(struct request
*rq
)
329 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
332 if (!ctx
->ipi_redirect
) {
333 rq
->q
->softirq_done_fn(rq
);
338 if (cpu
!= ctx
->cpu
&& cpu_online(ctx
->cpu
)) {
339 rq
->csd
.func
= __blk_mq_complete_request_remote
;
342 smp_call_function_single_async(ctx
->cpu
, &rq
->csd
);
344 rq
->q
->softirq_done_fn(rq
);
350 * blk_mq_complete_request - end I/O on a request
351 * @rq: the request being processed
354 * Ends all I/O on a request. It does not handle partial completions.
355 * The actual completion happens out-of-order, through a IPI handler.
357 void blk_mq_complete_request(struct request
*rq
)
359 if (unlikely(blk_should_fake_timeout(rq
->q
)))
361 if (!blk_mark_rq_complete(rq
))
362 __blk_mq_complete_request(rq
);
364 EXPORT_SYMBOL(blk_mq_complete_request
);
366 static void blk_mq_start_request(struct request
*rq
, bool last
)
368 struct request_queue
*q
= rq
->q
;
370 trace_block_rq_issue(q
, rq
);
372 rq
->resid_len
= blk_rq_bytes(rq
);
373 if (unlikely(blk_bidi_rq(rq
)))
374 rq
->next_rq
->resid_len
= blk_rq_bytes(rq
->next_rq
);
377 * Just mark start time and set the started bit. Due to memory
378 * ordering, we know we'll see the correct deadline as long as
379 * REQ_ATOMIC_STARTED is seen.
381 rq
->deadline
= jiffies
+ q
->rq_timeout
;
382 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
384 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
386 * Make sure space for the drain appears. We know we can do
387 * this because max_hw_segments has been adjusted to be one
388 * fewer than the device can handle.
390 rq
->nr_phys_segments
++;
394 * Flag the last request in the series so that drivers know when IO
395 * should be kicked off, if they don't do it on a per-request basis.
397 * Note: the flag isn't the only condition drivers should do kick off.
398 * If drive is busy, the last request might not have the bit set.
401 rq
->cmd_flags
|= REQ_END
;
404 static void __blk_mq_requeue_request(struct request
*rq
)
406 struct request_queue
*q
= rq
->q
;
408 trace_block_rq_requeue(q
, rq
);
409 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
411 rq
->cmd_flags
&= ~REQ_END
;
413 if (q
->dma_drain_size
&& blk_rq_bytes(rq
))
414 rq
->nr_phys_segments
--;
417 void blk_mq_requeue_request(struct request
*rq
)
419 struct request_queue
*q
= rq
->q
;
421 __blk_mq_requeue_request(rq
);
422 blk_clear_rq_complete(rq
);
424 trace_block_rq_requeue(q
, rq
);
426 BUG_ON(blk_queued_rq(rq
));
427 blk_mq_insert_request(rq
, true, true, false);
429 EXPORT_SYMBOL(blk_mq_requeue_request
);
431 struct request
*blk_mq_tag_to_rq(struct blk_mq_tags
*tags
, unsigned int tag
)
433 return tags
->rqs
[tag
];
435 EXPORT_SYMBOL(blk_mq_tag_to_rq
);
437 struct blk_mq_timeout_data
{
438 struct blk_mq_hw_ctx
*hctx
;
440 unsigned int *next_set
;
443 static void blk_mq_timeout_check(void *__data
, unsigned long *free_tags
)
445 struct blk_mq_timeout_data
*data
= __data
;
446 struct blk_mq_hw_ctx
*hctx
= data
->hctx
;
449 /* It may not be in flight yet (this is where
450 * the REQ_ATOMIC_STARTED flag comes in). The requests are
451 * statically allocated, so we know it's always safe to access the
452 * memory associated with a bit offset into ->rqs[].
458 tag
= find_next_zero_bit(free_tags
, hctx
->tags
->nr_tags
, tag
);
459 if (tag
>= hctx
->tags
->nr_tags
)
462 rq
= blk_mq_tag_to_rq(hctx
->tags
, tag
++);
463 if (rq
->q
!= hctx
->queue
)
465 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
468 blk_rq_check_expired(rq
, data
->next
, data
->next_set
);
472 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx
*hctx
,
474 unsigned int *next_set
)
476 struct blk_mq_timeout_data data
= {
479 .next_set
= next_set
,
483 * Ask the tagging code to iterate busy requests, so we can
484 * check them for timeout.
486 blk_mq_tag_busy_iter(hctx
->tags
, blk_mq_timeout_check
, &data
);
489 static void blk_mq_rq_timer(unsigned long data
)
491 struct request_queue
*q
= (struct request_queue
*) data
;
492 struct blk_mq_hw_ctx
*hctx
;
493 unsigned long next
= 0;
496 queue_for_each_hw_ctx(q
, hctx
, i
)
497 blk_mq_hw_ctx_check_timeout(hctx
, &next
, &next_set
);
500 mod_timer(&q
->timeout
, round_jiffies_up(next
));
504 * Reverse check our software queue for entries that we could potentially
505 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
506 * too much time checking for merges.
508 static bool blk_mq_attempt_merge(struct request_queue
*q
,
509 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
514 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
520 if (!blk_rq_merge_ok(rq
, bio
))
523 el_ret
= blk_try_merge(rq
, bio
);
524 if (el_ret
== ELEVATOR_BACK_MERGE
) {
525 if (bio_attempt_back_merge(q
, rq
, bio
)) {
530 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
531 if (bio_attempt_front_merge(q
, rq
, bio
)) {
542 void blk_mq_add_timer(struct request
*rq
)
544 __blk_add_timer(rq
, NULL
);
548 * Run this hardware queue, pulling any software queues mapped to it in.
549 * Note that this function currently has various problems around ordering
550 * of IO. In particular, we'd like FIFO behaviour on handling existing
551 * items on the hctx->dispatch list. Ignore that for now.
553 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
555 struct request_queue
*q
= hctx
->queue
;
556 struct blk_mq_ctx
*ctx
;
561 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx
->cpumask
));
563 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
569 * Touch any software queue that has pending entries.
571 for_each_set_bit(bit
, hctx
->ctx_map
, hctx
->nr_ctx
) {
572 clear_bit(bit
, hctx
->ctx_map
);
573 ctx
= hctx
->ctxs
[bit
];
574 BUG_ON(bit
!= ctx
->index_hw
);
576 spin_lock(&ctx
->lock
);
577 list_splice_tail_init(&ctx
->rq_list
, &rq_list
);
578 spin_unlock(&ctx
->lock
);
582 * If we have previous entries on our dispatch list, grab them
583 * and stuff them at the front for more fair dispatch.
585 if (!list_empty_careful(&hctx
->dispatch
)) {
586 spin_lock(&hctx
->lock
);
587 if (!list_empty(&hctx
->dispatch
))
588 list_splice_init(&hctx
->dispatch
, &rq_list
);
589 spin_unlock(&hctx
->lock
);
593 * Delete and return all entries from our dispatch list
598 * Now process all the entries, sending them to the driver.
600 while (!list_empty(&rq_list
)) {
603 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
604 list_del_init(&rq
->queuelist
);
606 blk_mq_start_request(rq
, list_empty(&rq_list
));
608 ret
= q
->mq_ops
->queue_rq(hctx
, rq
);
610 case BLK_MQ_RQ_QUEUE_OK
:
613 case BLK_MQ_RQ_QUEUE_BUSY
:
615 * FIXME: we should have a mechanism to stop the queue
616 * like blk_stop_queue, otherwise we will waste cpu
619 list_add(&rq
->queuelist
, &rq_list
);
620 __blk_mq_requeue_request(rq
);
623 pr_err("blk-mq: bad return on queue: %d\n", ret
);
624 case BLK_MQ_RQ_QUEUE_ERROR
:
626 blk_mq_end_io(rq
, rq
->errors
);
630 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
635 hctx
->dispatched
[0]++;
636 else if (queued
< (1 << (BLK_MQ_MAX_DISPATCH_ORDER
- 1)))
637 hctx
->dispatched
[ilog2(queued
) + 1]++;
640 * Any items that need requeuing? Stuff them into hctx->dispatch,
641 * that is where we will continue on next queue run.
643 if (!list_empty(&rq_list
)) {
644 spin_lock(&hctx
->lock
);
645 list_splice(&rq_list
, &hctx
->dispatch
);
646 spin_unlock(&hctx
->lock
);
650 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
652 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
655 if (!async
&& cpumask_test_cpu(smp_processor_id(), hctx
->cpumask
))
656 __blk_mq_run_hw_queue(hctx
);
657 else if (hctx
->queue
->nr_hw_queues
== 1)
658 kblockd_schedule_delayed_work(&hctx
->run_work
, 0);
663 * It'd be great if the workqueue API had a way to pass
664 * in a mask and had some smarts for more clever placement
665 * than the first CPU. Or we could round-robin here. For now,
666 * just queue on the first CPU.
668 cpu
= cpumask_first(hctx
->cpumask
);
669 kblockd_schedule_delayed_work_on(cpu
, &hctx
->run_work
, 0);
673 void blk_mq_run_queues(struct request_queue
*q
, bool async
)
675 struct blk_mq_hw_ctx
*hctx
;
678 queue_for_each_hw_ctx(q
, hctx
, i
) {
679 if ((!blk_mq_hctx_has_pending(hctx
) &&
680 list_empty_careful(&hctx
->dispatch
)) ||
681 test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
685 blk_mq_run_hw_queue(hctx
, async
);
689 EXPORT_SYMBOL(blk_mq_run_queues
);
691 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
693 cancel_delayed_work(&hctx
->run_work
);
694 cancel_delayed_work(&hctx
->delay_work
);
695 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
697 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
699 void blk_mq_stop_hw_queues(struct request_queue
*q
)
701 struct blk_mq_hw_ctx
*hctx
;
704 queue_for_each_hw_ctx(q
, hctx
, i
)
705 blk_mq_stop_hw_queue(hctx
);
707 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
709 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
711 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
714 __blk_mq_run_hw_queue(hctx
);
717 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
719 void blk_mq_start_hw_queues(struct request_queue
*q
)
721 struct blk_mq_hw_ctx
*hctx
;
724 queue_for_each_hw_ctx(q
, hctx
, i
)
725 blk_mq_start_hw_queue(hctx
);
727 EXPORT_SYMBOL(blk_mq_start_hw_queues
);
730 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
, bool async
)
732 struct blk_mq_hw_ctx
*hctx
;
735 queue_for_each_hw_ctx(q
, hctx
, i
) {
736 if (!test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
739 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
741 blk_mq_run_hw_queue(hctx
, async
);
745 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
747 static void blk_mq_run_work_fn(struct work_struct
*work
)
749 struct blk_mq_hw_ctx
*hctx
;
751 hctx
= container_of(work
, struct blk_mq_hw_ctx
, run_work
.work
);
753 __blk_mq_run_hw_queue(hctx
);
756 static void blk_mq_delay_work_fn(struct work_struct
*work
)
758 struct blk_mq_hw_ctx
*hctx
;
760 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delay_work
.work
);
762 if (test_and_clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
763 __blk_mq_run_hw_queue(hctx
);
766 void blk_mq_delay_queue(struct blk_mq_hw_ctx
*hctx
, unsigned long msecs
)
768 unsigned long tmo
= msecs_to_jiffies(msecs
);
770 if (hctx
->queue
->nr_hw_queues
== 1)
771 kblockd_schedule_delayed_work(&hctx
->delay_work
, tmo
);
776 * It'd be great if the workqueue API had a way to pass
777 * in a mask and had some smarts for more clever placement
778 * than the first CPU. Or we could round-robin here. For now,
779 * just queue on the first CPU.
781 cpu
= cpumask_first(hctx
->cpumask
);
782 kblockd_schedule_delayed_work_on(cpu
, &hctx
->delay_work
, tmo
);
785 EXPORT_SYMBOL(blk_mq_delay_queue
);
787 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
788 struct request
*rq
, bool at_head
)
790 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
792 trace_block_rq_insert(hctx
->queue
, rq
);
795 list_add(&rq
->queuelist
, &ctx
->rq_list
);
797 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
798 blk_mq_hctx_mark_pending(hctx
, ctx
);
801 * We do this early, to ensure we are on the right CPU.
803 blk_mq_add_timer(rq
);
806 void blk_mq_insert_request(struct request
*rq
, bool at_head
, bool run_queue
,
809 struct request_queue
*q
= rq
->q
;
810 struct blk_mq_hw_ctx
*hctx
;
811 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
, *current_ctx
;
813 current_ctx
= blk_mq_get_ctx(q
);
814 if (!cpu_online(ctx
->cpu
))
815 rq
->mq_ctx
= ctx
= current_ctx
;
817 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
819 if (rq
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
) &&
820 !(rq
->cmd_flags
& (REQ_FLUSH_SEQ
))) {
821 blk_insert_flush(rq
);
823 spin_lock(&ctx
->lock
);
824 __blk_mq_insert_request(hctx
, rq
, at_head
);
825 spin_unlock(&ctx
->lock
);
829 blk_mq_run_hw_queue(hctx
, async
);
831 blk_mq_put_ctx(current_ctx
);
834 static void blk_mq_insert_requests(struct request_queue
*q
,
835 struct blk_mq_ctx
*ctx
,
836 struct list_head
*list
,
841 struct blk_mq_hw_ctx
*hctx
;
842 struct blk_mq_ctx
*current_ctx
;
844 trace_block_unplug(q
, depth
, !from_schedule
);
846 current_ctx
= blk_mq_get_ctx(q
);
848 if (!cpu_online(ctx
->cpu
))
850 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
853 * preemption doesn't flush plug list, so it's possible ctx->cpu is
856 spin_lock(&ctx
->lock
);
857 while (!list_empty(list
)) {
860 rq
= list_first_entry(list
, struct request
, queuelist
);
861 list_del_init(&rq
->queuelist
);
863 __blk_mq_insert_request(hctx
, rq
, false);
865 spin_unlock(&ctx
->lock
);
867 blk_mq_run_hw_queue(hctx
, from_schedule
);
868 blk_mq_put_ctx(current_ctx
);
871 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
873 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
874 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
876 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
877 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
878 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
881 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
883 struct blk_mq_ctx
*this_ctx
;
884 struct request_queue
*this_q
;
890 list_splice_init(&plug
->mq_list
, &list
);
892 list_sort(NULL
, &list
, plug_ctx_cmp
);
898 while (!list_empty(&list
)) {
899 rq
= list_entry_rq(list
.next
);
900 list_del_init(&rq
->queuelist
);
902 if (rq
->mq_ctx
!= this_ctx
) {
904 blk_mq_insert_requests(this_q
, this_ctx
,
909 this_ctx
= rq
->mq_ctx
;
915 list_add_tail(&rq
->queuelist
, &ctx_list
);
919 * If 'this_ctx' is set, we know we have entries to complete
920 * on 'ctx_list'. Do those.
923 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
928 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
930 init_request_from_bio(rq
, bio
);
931 blk_account_io_start(rq
, 1);
934 static void blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
936 struct blk_mq_hw_ctx
*hctx
;
937 struct blk_mq_ctx
*ctx
;
938 const int is_sync
= rw_is_sync(bio
->bi_rw
);
939 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
940 int rw
= bio_data_dir(bio
);
942 unsigned int use_plug
, request_count
= 0;
945 * If we have multiple hardware queues, just go directly to
946 * one of those for sync IO.
948 use_plug
= !is_flush_fua
&& ((q
->nr_hw_queues
== 1) || !is_sync
);
950 blk_queue_bounce(q
, &bio
);
952 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
953 bio_endio(bio
, -EIO
);
957 if (use_plug
&& blk_attempt_plug_merge(q
, bio
, &request_count
))
960 if (blk_mq_queue_enter(q
)) {
961 bio_endio(bio
, -EIO
);
965 ctx
= blk_mq_get_ctx(q
);
966 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
970 trace_block_getrq(q
, bio
, rw
);
971 rq
= __blk_mq_alloc_request(hctx
, GFP_ATOMIC
, false);
973 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
976 trace_block_sleeprq(q
, bio
, rw
);
977 rq
= blk_mq_alloc_request_pinned(q
, rw
, __GFP_WAIT
|GFP_ATOMIC
,
980 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
985 if (unlikely(is_flush_fua
)) {
986 blk_mq_bio_to_request(rq
, bio
);
987 blk_insert_flush(rq
);
992 * A task plug currently exists. Since this is completely lockless,
993 * utilize that to temporarily store requests until the task is
994 * either done or scheduled away.
997 struct blk_plug
*plug
= current
->plug
;
1000 blk_mq_bio_to_request(rq
, bio
);
1001 if (list_empty(&plug
->mq_list
))
1002 trace_block_plug(q
);
1003 else if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
1004 blk_flush_plug_list(plug
, false);
1005 trace_block_plug(q
);
1007 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
1008 blk_mq_put_ctx(ctx
);
1013 spin_lock(&ctx
->lock
);
1015 if ((hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
) &&
1016 blk_mq_attempt_merge(q
, ctx
, bio
))
1017 __blk_mq_free_request(hctx
, ctx
, rq
);
1019 blk_mq_bio_to_request(rq
, bio
);
1020 __blk_mq_insert_request(hctx
, rq
, false);
1023 spin_unlock(&ctx
->lock
);
1026 * For a SYNC request, send it to the hardware immediately. For an
1027 * ASYNC request, just ensure that we run it later on. The latter
1028 * allows for merging opportunities and more efficient dispatching.
1031 blk_mq_run_hw_queue(hctx
, !is_sync
|| is_flush_fua
);
1032 blk_mq_put_ctx(ctx
);
1036 * Default mapping to a software queue, since we use one per CPU.
1038 struct blk_mq_hw_ctx
*blk_mq_map_queue(struct request_queue
*q
, const int cpu
)
1040 return q
->queue_hw_ctx
[q
->mq_map
[cpu
]];
1042 EXPORT_SYMBOL(blk_mq_map_queue
);
1044 struct blk_mq_hw_ctx
*blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set
*set
,
1045 unsigned int hctx_index
)
1047 return kmalloc_node(sizeof(struct blk_mq_hw_ctx
),
1048 GFP_KERNEL
| __GFP_ZERO
, set
->numa_node
);
1050 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue
);
1052 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx
*hctx
,
1053 unsigned int hctx_index
)
1057 EXPORT_SYMBOL(blk_mq_free_single_hw_queue
);
1059 static void blk_mq_hctx_notify(void *data
, unsigned long action
,
1062 struct blk_mq_hw_ctx
*hctx
= data
;
1063 struct request_queue
*q
= hctx
->queue
;
1064 struct blk_mq_ctx
*ctx
;
1067 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
)
1071 * Move ctx entries to new CPU, if this one is going away.
1073 ctx
= __blk_mq_get_ctx(q
, cpu
);
1075 spin_lock(&ctx
->lock
);
1076 if (!list_empty(&ctx
->rq_list
)) {
1077 list_splice_init(&ctx
->rq_list
, &tmp
);
1078 clear_bit(ctx
->index_hw
, hctx
->ctx_map
);
1080 spin_unlock(&ctx
->lock
);
1082 if (list_empty(&tmp
))
1085 ctx
= blk_mq_get_ctx(q
);
1086 spin_lock(&ctx
->lock
);
1088 while (!list_empty(&tmp
)) {
1091 rq
= list_first_entry(&tmp
, struct request
, queuelist
);
1093 list_move_tail(&rq
->queuelist
, &ctx
->rq_list
);
1096 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1097 blk_mq_hctx_mark_pending(hctx
, ctx
);
1099 spin_unlock(&ctx
->lock
);
1101 blk_mq_run_hw_queue(hctx
, true);
1102 blk_mq_put_ctx(ctx
);
1105 static void blk_mq_free_rq_map(struct blk_mq_tag_set
*set
,
1106 struct blk_mq_tags
*tags
, unsigned int hctx_idx
)
1110 if (tags
->rqs
&& set
->ops
->exit_request
) {
1113 for (i
= 0; i
< tags
->nr_tags
; i
++) {
1116 set
->ops
->exit_request(set
->driver_data
, tags
->rqs
[i
],
1121 while (!list_empty(&tags
->page_list
)) {
1122 page
= list_first_entry(&tags
->page_list
, struct page
, lru
);
1123 list_del_init(&page
->lru
);
1124 __free_pages(page
, page
->private);
1129 blk_mq_free_tags(tags
);
1132 static size_t order_to_size(unsigned int order
)
1134 return (size_t)PAGE_SIZE
<< order
;
1137 static struct blk_mq_tags
*blk_mq_init_rq_map(struct blk_mq_tag_set
*set
,
1138 unsigned int hctx_idx
)
1140 struct blk_mq_tags
*tags
;
1141 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1142 size_t rq_size
, left
;
1144 tags
= blk_mq_init_tags(set
->queue_depth
, set
->reserved_tags
,
1149 INIT_LIST_HEAD(&tags
->page_list
);
1151 tags
->rqs
= kmalloc_node(set
->queue_depth
* sizeof(struct request
*),
1152 GFP_KERNEL
, set
->numa_node
);
1154 blk_mq_free_tags(tags
);
1159 * rq_size is the size of the request plus driver payload, rounded
1160 * to the cacheline size
1162 rq_size
= round_up(sizeof(struct request
) + set
->cmd_size
,
1164 left
= rq_size
* set
->queue_depth
;
1166 for (i
= 0; i
< set
->queue_depth
; ) {
1167 int this_order
= max_order
;
1172 while (left
< order_to_size(this_order
- 1) && this_order
)
1176 page
= alloc_pages_node(set
->numa_node
, GFP_KERNEL
,
1182 if (order_to_size(this_order
) < rq_size
)
1189 page
->private = this_order
;
1190 list_add_tail(&page
->lru
, &tags
->page_list
);
1192 p
= page_address(page
);
1193 entries_per_page
= order_to_size(this_order
) / rq_size
;
1194 to_do
= min(entries_per_page
, set
->queue_depth
- i
);
1195 left
-= to_do
* rq_size
;
1196 for (j
= 0; j
< to_do
; j
++) {
1198 blk_rq_init(NULL
, tags
->rqs
[i
]);
1199 if (set
->ops
->init_request
) {
1200 if (set
->ops
->init_request(set
->driver_data
,
1201 tags
->rqs
[i
], hctx_idx
, i
,
1214 pr_warn("%s: failed to allocate requests\n", __func__
);
1215 blk_mq_free_rq_map(set
, tags
, hctx_idx
);
1219 static int blk_mq_init_hw_queues(struct request_queue
*q
,
1220 struct blk_mq_tag_set
*set
)
1222 struct blk_mq_hw_ctx
*hctx
;
1226 * Initialize hardware queues
1228 queue_for_each_hw_ctx(q
, hctx
, i
) {
1229 unsigned int num_maps
;
1232 node
= hctx
->numa_node
;
1233 if (node
== NUMA_NO_NODE
)
1234 node
= hctx
->numa_node
= set
->numa_node
;
1236 INIT_DELAYED_WORK(&hctx
->run_work
, blk_mq_run_work_fn
);
1237 INIT_DELAYED_WORK(&hctx
->delay_work
, blk_mq_delay_work_fn
);
1238 spin_lock_init(&hctx
->lock
);
1239 INIT_LIST_HEAD(&hctx
->dispatch
);
1241 hctx
->queue_num
= i
;
1242 hctx
->flags
= set
->flags
;
1243 hctx
->cmd_size
= set
->cmd_size
;
1245 blk_mq_init_cpu_notifier(&hctx
->cpu_notifier
,
1246 blk_mq_hctx_notify
, hctx
);
1247 blk_mq_register_cpu_notifier(&hctx
->cpu_notifier
);
1249 hctx
->tags
= set
->tags
[i
];
1252 * Allocate space for all possible cpus to avoid allocation in
1255 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1260 num_maps
= ALIGN(nr_cpu_ids
, BITS_PER_LONG
) / BITS_PER_LONG
;
1261 hctx
->ctx_map
= kzalloc_node(num_maps
* sizeof(unsigned long),
1266 hctx
->nr_ctx_map
= num_maps
;
1269 if (set
->ops
->init_hctx
&&
1270 set
->ops
->init_hctx(hctx
, set
->driver_data
, i
))
1274 if (i
== q
->nr_hw_queues
)
1280 queue_for_each_hw_ctx(q
, hctx
, j
) {
1284 if (set
->ops
->exit_hctx
)
1285 set
->ops
->exit_hctx(hctx
, j
);
1287 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1289 kfree(hctx
->ctx_map
);
1295 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1296 unsigned int nr_hw_queues
)
1300 for_each_possible_cpu(i
) {
1301 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1302 struct blk_mq_hw_ctx
*hctx
;
1304 memset(__ctx
, 0, sizeof(*__ctx
));
1306 spin_lock_init(&__ctx
->lock
);
1307 INIT_LIST_HEAD(&__ctx
->rq_list
);
1310 /* If the cpu isn't online, the cpu is mapped to first hctx */
1314 hctx
= q
->mq_ops
->map_queue(q
, i
);
1315 cpumask_set_cpu(i
, hctx
->cpumask
);
1319 * Set local node, IFF we have more than one hw queue. If
1320 * not, we remain on the home node of the device
1322 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1323 hctx
->numa_node
= cpu_to_node(i
);
1327 static void blk_mq_map_swqueue(struct request_queue
*q
)
1330 struct blk_mq_hw_ctx
*hctx
;
1331 struct blk_mq_ctx
*ctx
;
1333 queue_for_each_hw_ctx(q
, hctx
, i
) {
1334 cpumask_clear(hctx
->cpumask
);
1339 * Map software to hardware queues
1341 queue_for_each_ctx(q
, ctx
, i
) {
1342 /* If the cpu isn't online, the cpu is mapped to first hctx */
1346 hctx
= q
->mq_ops
->map_queue(q
, i
);
1347 cpumask_set_cpu(i
, hctx
->cpumask
);
1348 ctx
->index_hw
= hctx
->nr_ctx
;
1349 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1353 struct request_queue
*blk_mq_init_queue(struct blk_mq_tag_set
*set
)
1355 struct blk_mq_hw_ctx
**hctxs
;
1356 struct blk_mq_ctx
*ctx
;
1357 struct request_queue
*q
;
1360 ctx
= alloc_percpu(struct blk_mq_ctx
);
1362 return ERR_PTR(-ENOMEM
);
1364 hctxs
= kmalloc_node(set
->nr_hw_queues
* sizeof(*hctxs
), GFP_KERNEL
,
1370 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1371 hctxs
[i
] = set
->ops
->alloc_hctx(set
, i
);
1375 if (!zalloc_cpumask_var(&hctxs
[i
]->cpumask
, GFP_KERNEL
))
1378 hctxs
[i
]->numa_node
= NUMA_NO_NODE
;
1379 hctxs
[i
]->queue_num
= i
;
1382 q
= blk_alloc_queue_node(GFP_KERNEL
, set
->numa_node
);
1386 q
->mq_map
= blk_mq_make_queue_map(set
);
1390 setup_timer(&q
->timeout
, blk_mq_rq_timer
, (unsigned long) q
);
1391 blk_queue_rq_timeout(q
, 30000);
1393 q
->nr_queues
= nr_cpu_ids
;
1394 q
->nr_hw_queues
= set
->nr_hw_queues
;
1397 q
->queue_hw_ctx
= hctxs
;
1399 q
->mq_ops
= set
->ops
;
1400 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
1402 q
->sg_reserved_size
= INT_MAX
;
1404 blk_queue_make_request(q
, blk_mq_make_request
);
1405 blk_queue_rq_timed_out(q
, set
->ops
->timeout
);
1407 blk_queue_rq_timeout(q
, set
->timeout
);
1409 if (set
->ops
->complete
)
1410 blk_queue_softirq_done(q
, set
->ops
->complete
);
1412 blk_mq_init_flush(q
);
1413 blk_mq_init_cpu_queues(q
, set
->nr_hw_queues
);
1415 q
->flush_rq
= kzalloc(round_up(sizeof(struct request
) +
1416 set
->cmd_size
, cache_line_size()),
1421 if (blk_mq_init_hw_queues(q
, set
))
1424 blk_mq_map_swqueue(q
);
1426 mutex_lock(&all_q_mutex
);
1427 list_add_tail(&q
->all_q_node
, &all_q_list
);
1428 mutex_unlock(&all_q_mutex
);
1437 blk_cleanup_queue(q
);
1439 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1442 free_cpumask_var(hctxs
[i
]->cpumask
);
1443 set
->ops
->free_hctx(hctxs
[i
], i
);
1448 return ERR_PTR(-ENOMEM
);
1450 EXPORT_SYMBOL(blk_mq_init_queue
);
1452 void blk_mq_free_queue(struct request_queue
*q
)
1454 struct blk_mq_hw_ctx
*hctx
;
1457 queue_for_each_hw_ctx(q
, hctx
, i
) {
1458 kfree(hctx
->ctx_map
);
1460 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1461 if (q
->mq_ops
->exit_hctx
)
1462 q
->mq_ops
->exit_hctx(hctx
, i
);
1463 free_cpumask_var(hctx
->cpumask
);
1464 q
->mq_ops
->free_hctx(hctx
, i
);
1467 free_percpu(q
->queue_ctx
);
1468 kfree(q
->queue_hw_ctx
);
1471 q
->queue_ctx
= NULL
;
1472 q
->queue_hw_ctx
= NULL
;
1475 mutex_lock(&all_q_mutex
);
1476 list_del_init(&q
->all_q_node
);
1477 mutex_unlock(&all_q_mutex
);
1480 /* Basically redo blk_mq_init_queue with queue frozen */
1481 static void blk_mq_queue_reinit(struct request_queue
*q
)
1483 blk_mq_freeze_queue(q
);
1485 blk_mq_update_queue_map(q
->mq_map
, q
->nr_hw_queues
);
1488 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1489 * we should change hctx numa_node according to new topology (this
1490 * involves free and re-allocate memory, worthy doing?)
1493 blk_mq_map_swqueue(q
);
1495 blk_mq_unfreeze_queue(q
);
1498 static int blk_mq_queue_reinit_notify(struct notifier_block
*nb
,
1499 unsigned long action
, void *hcpu
)
1501 struct request_queue
*q
;
1504 * Before new mapping is established, hotadded cpu might already start
1505 * handling requests. This doesn't break anything as we map offline
1506 * CPUs to first hardware queue. We will re-init queue below to get
1509 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
&&
1510 action
!= CPU_ONLINE
&& action
!= CPU_ONLINE_FROZEN
)
1513 mutex_lock(&all_q_mutex
);
1514 list_for_each_entry(q
, &all_q_list
, all_q_node
)
1515 blk_mq_queue_reinit(q
);
1516 mutex_unlock(&all_q_mutex
);
1520 int blk_mq_alloc_tag_set(struct blk_mq_tag_set
*set
)
1524 if (!set
->nr_hw_queues
)
1526 if (!set
->queue_depth
|| set
->queue_depth
> BLK_MQ_MAX_DEPTH
)
1528 if (set
->queue_depth
< set
->reserved_tags
+ BLK_MQ_TAG_MIN
)
1531 if (!set
->nr_hw_queues
||
1532 !set
->ops
->queue_rq
|| !set
->ops
->map_queue
||
1533 !set
->ops
->alloc_hctx
|| !set
->ops
->free_hctx
)
1537 set
->tags
= kmalloc_node(set
->nr_hw_queues
*
1538 sizeof(struct blk_mq_tags
*),
1539 GFP_KERNEL
, set
->numa_node
);
1543 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1544 set
->tags
[i
] = blk_mq_init_rq_map(set
, i
);
1553 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
1557 EXPORT_SYMBOL(blk_mq_alloc_tag_set
);
1559 void blk_mq_free_tag_set(struct blk_mq_tag_set
*set
)
1563 for (i
= 0; i
< set
->nr_hw_queues
; i
++)
1564 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
1566 EXPORT_SYMBOL(blk_mq_free_tag_set
);
1568 void blk_mq_disable_hotplug(void)
1570 mutex_lock(&all_q_mutex
);
1573 void blk_mq_enable_hotplug(void)
1575 mutex_unlock(&all_q_mutex
);
1578 static int __init
blk_mq_init(void)
1582 /* Must be called after percpu_counter_hotcpu_callback() */
1583 hotcpu_notifier(blk_mq_queue_reinit_notify
, -10);
1587 subsys_initcall(blk_mq_init
);