2 * Block multiqueue core code
4 * Copyright (C) 2013-2014 Jens Axboe
5 * Copyright (C) 2013-2014 Christoph Hellwig
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/backing-dev.h>
10 #include <linux/bio.h>
11 #include <linux/blkdev.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
16 #include <linux/smp.h>
17 #include <linux/llist.h>
18 #include <linux/list_sort.h>
19 #include <linux/cpu.h>
20 #include <linux/cache.h>
21 #include <linux/sched/sysctl.h>
22 #include <linux/delay.h>
24 #include <trace/events/block.h>
26 #include <linux/blk-mq.h>
29 #include "blk-mq-tag.h"
31 static DEFINE_MUTEX(all_q_mutex
);
32 static LIST_HEAD(all_q_list
);
34 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
);
37 * Check if any of the ctx's have pending work in this hardware queue
39 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx
*hctx
)
43 for (i
= 0; i
< hctx
->ctx_map
.map_size
; i
++)
44 if (hctx
->ctx_map
.map
[i
].word
)
50 static inline struct blk_align_bitmap
*get_bm(struct blk_mq_hw_ctx
*hctx
,
51 struct blk_mq_ctx
*ctx
)
53 return &hctx
->ctx_map
.map
[ctx
->index_hw
/ hctx
->ctx_map
.bits_per_word
];
56 #define CTX_TO_BIT(hctx, ctx) \
57 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
60 * Mark this ctx as having pending work in this hardware queue
62 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx
*hctx
,
63 struct blk_mq_ctx
*ctx
)
65 struct blk_align_bitmap
*bm
= get_bm(hctx
, ctx
);
67 if (!test_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
))
68 set_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
);
71 static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx
*hctx
,
72 struct blk_mq_ctx
*ctx
)
74 struct blk_align_bitmap
*bm
= get_bm(hctx
, ctx
);
76 clear_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
);
79 static int blk_mq_queue_enter(struct request_queue
*q
)
84 if (percpu_ref_tryget_live(&q
->mq_usage_counter
))
87 ret
= wait_event_interruptible(q
->mq_freeze_wq
,
88 !q
->mq_freeze_depth
|| blk_queue_dying(q
));
89 if (blk_queue_dying(q
))
96 static void blk_mq_queue_exit(struct request_queue
*q
)
98 percpu_ref_put(&q
->mq_usage_counter
);
101 static void blk_mq_usage_counter_release(struct percpu_ref
*ref
)
103 struct request_queue
*q
=
104 container_of(ref
, struct request_queue
, mq_usage_counter
);
106 wake_up_all(&q
->mq_freeze_wq
);
110 * Guarantee no request is in use, so we can change any data structure of
111 * the queue afterward.
113 void blk_mq_freeze_queue(struct request_queue
*q
)
117 spin_lock_irq(q
->queue_lock
);
118 freeze
= !q
->mq_freeze_depth
++;
119 spin_unlock_irq(q
->queue_lock
);
123 * XXX: Temporary kludge to work around SCSI blk-mq stall.
124 * SCSI synchronously creates and destroys many queues
125 * back-to-back during probe leading to lengthy stalls.
126 * This will be fixed by keeping ->mq_usage_counter in
127 * atomic mode until genhd registration, but, for now,
128 * let's work around using expedited synchronization.
130 __percpu_ref_kill_expedited(&q
->mq_usage_counter
);
132 blk_mq_run_queues(q
, false);
134 wait_event(q
->mq_freeze_wq
, percpu_ref_is_zero(&q
->mq_usage_counter
));
137 static void blk_mq_unfreeze_queue(struct request_queue
*q
)
141 spin_lock_irq(q
->queue_lock
);
142 wake
= !--q
->mq_freeze_depth
;
143 WARN_ON_ONCE(q
->mq_freeze_depth
< 0);
144 spin_unlock_irq(q
->queue_lock
);
146 percpu_ref_reinit(&q
->mq_usage_counter
);
147 wake_up_all(&q
->mq_freeze_wq
);
151 bool blk_mq_can_queue(struct blk_mq_hw_ctx
*hctx
)
153 return blk_mq_has_free_tags(hctx
->tags
);
155 EXPORT_SYMBOL(blk_mq_can_queue
);
157 static void blk_mq_rq_ctx_init(struct request_queue
*q
, struct blk_mq_ctx
*ctx
,
158 struct request
*rq
, unsigned int rw_flags
)
160 if (blk_queue_io_stat(q
))
161 rw_flags
|= REQ_IO_STAT
;
163 INIT_LIST_HEAD(&rq
->queuelist
);
164 /* csd/requeue_work/fifo_time is initialized before use */
167 rq
->cmd_flags
|= rw_flags
;
168 /* do not touch atomic flags, it needs atomic ops against the timer */
170 INIT_HLIST_NODE(&rq
->hash
);
171 RB_CLEAR_NODE(&rq
->rb_node
);
174 rq
->start_time
= jiffies
;
175 #ifdef CONFIG_BLK_CGROUP
177 set_start_time_ns(rq
);
178 rq
->io_start_time_ns
= 0;
180 rq
->nr_phys_segments
= 0;
181 #if defined(CONFIG_BLK_DEV_INTEGRITY)
182 rq
->nr_integrity_segments
= 0;
185 /* tag was already set */
195 INIT_LIST_HEAD(&rq
->timeout_list
);
199 rq
->end_io_data
= NULL
;
202 ctx
->rq_dispatched
[rw_is_sync(rw_flags
)]++;
205 static struct request
*
206 __blk_mq_alloc_request(struct blk_mq_alloc_data
*data
, int rw
)
211 tag
= blk_mq_get_tag(data
);
212 if (tag
!= BLK_MQ_TAG_FAIL
) {
213 rq
= data
->hctx
->tags
->rqs
[tag
];
215 if (blk_mq_tag_busy(data
->hctx
)) {
216 rq
->cmd_flags
= REQ_MQ_INFLIGHT
;
217 atomic_inc(&data
->hctx
->nr_active
);
221 blk_mq_rq_ctx_init(data
->q
, data
->ctx
, rq
, rw
);
228 struct request
*blk_mq_alloc_request(struct request_queue
*q
, int rw
, gfp_t gfp
,
231 struct blk_mq_ctx
*ctx
;
232 struct blk_mq_hw_ctx
*hctx
;
234 struct blk_mq_alloc_data alloc_data
;
236 if (blk_mq_queue_enter(q
))
239 ctx
= blk_mq_get_ctx(q
);
240 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
241 blk_mq_set_alloc_data(&alloc_data
, q
, gfp
& ~__GFP_WAIT
,
242 reserved
, ctx
, hctx
);
244 rq
= __blk_mq_alloc_request(&alloc_data
, rw
);
245 if (!rq
&& (gfp
& __GFP_WAIT
)) {
246 __blk_mq_run_hw_queue(hctx
);
249 ctx
= blk_mq_get_ctx(q
);
250 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
251 blk_mq_set_alloc_data(&alloc_data
, q
, gfp
, reserved
, ctx
,
253 rq
= __blk_mq_alloc_request(&alloc_data
, rw
);
254 ctx
= alloc_data
.ctx
;
259 EXPORT_SYMBOL(blk_mq_alloc_request
);
261 static void __blk_mq_free_request(struct blk_mq_hw_ctx
*hctx
,
262 struct blk_mq_ctx
*ctx
, struct request
*rq
)
264 const int tag
= rq
->tag
;
265 struct request_queue
*q
= rq
->q
;
267 if (rq
->cmd_flags
& REQ_MQ_INFLIGHT
)
268 atomic_dec(&hctx
->nr_active
);
271 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
272 blk_mq_put_tag(hctx
, tag
, &ctx
->last_tag
);
273 blk_mq_queue_exit(q
);
276 void blk_mq_free_request(struct request
*rq
)
278 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
279 struct blk_mq_hw_ctx
*hctx
;
280 struct request_queue
*q
= rq
->q
;
282 ctx
->rq_completed
[rq_is_sync(rq
)]++;
284 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
285 __blk_mq_free_request(hctx
, ctx
, rq
);
289 * Clone all relevant state from a request that has been put on hold in
290 * the flush state machine into the preallocated flush request that hangs
291 * off the request queue.
293 * For a driver the flush request should be invisible, that's why we are
294 * impersonating the original request here.
296 void blk_mq_clone_flush_request(struct request
*flush_rq
,
297 struct request
*orig_rq
)
299 struct blk_mq_hw_ctx
*hctx
=
300 orig_rq
->q
->mq_ops
->map_queue(orig_rq
->q
, orig_rq
->mq_ctx
->cpu
);
302 flush_rq
->mq_ctx
= orig_rq
->mq_ctx
;
303 flush_rq
->tag
= orig_rq
->tag
;
304 memcpy(blk_mq_rq_to_pdu(flush_rq
), blk_mq_rq_to_pdu(orig_rq
),
308 inline void __blk_mq_end_io(struct request
*rq
, int error
)
310 blk_account_io_done(rq
);
313 rq
->end_io(rq
, error
);
315 if (unlikely(blk_bidi_rq(rq
)))
316 blk_mq_free_request(rq
->next_rq
);
317 blk_mq_free_request(rq
);
320 EXPORT_SYMBOL(__blk_mq_end_io
);
322 void blk_mq_end_io(struct request
*rq
, int error
)
324 if (blk_update_request(rq
, error
, blk_rq_bytes(rq
)))
326 __blk_mq_end_io(rq
, error
);
328 EXPORT_SYMBOL(blk_mq_end_io
);
330 static void __blk_mq_complete_request_remote(void *data
)
332 struct request
*rq
= data
;
334 rq
->q
->softirq_done_fn(rq
);
337 static void blk_mq_ipi_complete_request(struct request
*rq
)
339 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
343 if (!test_bit(QUEUE_FLAG_SAME_COMP
, &rq
->q
->queue_flags
)) {
344 rq
->q
->softirq_done_fn(rq
);
349 if (!test_bit(QUEUE_FLAG_SAME_FORCE
, &rq
->q
->queue_flags
))
350 shared
= cpus_share_cache(cpu
, ctx
->cpu
);
352 if (cpu
!= ctx
->cpu
&& !shared
&& cpu_online(ctx
->cpu
)) {
353 rq
->csd
.func
= __blk_mq_complete_request_remote
;
356 smp_call_function_single_async(ctx
->cpu
, &rq
->csd
);
358 rq
->q
->softirq_done_fn(rq
);
363 void __blk_mq_complete_request(struct request
*rq
)
365 struct request_queue
*q
= rq
->q
;
367 if (!q
->softirq_done_fn
)
368 blk_mq_end_io(rq
, rq
->errors
);
370 blk_mq_ipi_complete_request(rq
);
374 * blk_mq_complete_request - end I/O on a request
375 * @rq: the request being processed
378 * Ends all I/O on a request. It does not handle partial completions.
379 * The actual completion happens out-of-order, through a IPI handler.
381 void blk_mq_complete_request(struct request
*rq
)
383 struct request_queue
*q
= rq
->q
;
385 if (unlikely(blk_should_fake_timeout(q
)))
387 if (!blk_mark_rq_complete(rq
))
388 __blk_mq_complete_request(rq
);
390 EXPORT_SYMBOL(blk_mq_complete_request
);
392 static void blk_mq_start_request(struct request
*rq
, bool last
)
394 struct request_queue
*q
= rq
->q
;
396 trace_block_rq_issue(q
, rq
);
398 rq
->resid_len
= blk_rq_bytes(rq
);
399 if (unlikely(blk_bidi_rq(rq
)))
400 rq
->next_rq
->resid_len
= blk_rq_bytes(rq
->next_rq
);
405 * Ensure that ->deadline is visible before set the started
406 * flag and clear the completed flag.
408 smp_mb__before_atomic();
411 * Mark us as started and clear complete. Complete might have been
412 * set if requeue raced with timeout, which then marked it as
413 * complete. So be sure to clear complete again when we start
414 * the request, otherwise we'll ignore the completion event.
416 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
417 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
418 if (test_bit(REQ_ATOM_COMPLETE
, &rq
->atomic_flags
))
419 clear_bit(REQ_ATOM_COMPLETE
, &rq
->atomic_flags
);
421 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
423 * Make sure space for the drain appears. We know we can do
424 * this because max_hw_segments has been adjusted to be one
425 * fewer than the device can handle.
427 rq
->nr_phys_segments
++;
431 * Flag the last request in the series so that drivers know when IO
432 * should be kicked off, if they don't do it on a per-request basis.
434 * Note: the flag isn't the only condition drivers should do kick off.
435 * If drive is busy, the last request might not have the bit set.
438 rq
->cmd_flags
|= REQ_END
;
441 static void __blk_mq_requeue_request(struct request
*rq
)
443 struct request_queue
*q
= rq
->q
;
445 trace_block_rq_requeue(q
, rq
);
446 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
448 rq
->cmd_flags
&= ~REQ_END
;
450 if (q
->dma_drain_size
&& blk_rq_bytes(rq
))
451 rq
->nr_phys_segments
--;
454 void blk_mq_requeue_request(struct request
*rq
)
456 __blk_mq_requeue_request(rq
);
457 blk_clear_rq_complete(rq
);
459 BUG_ON(blk_queued_rq(rq
));
460 blk_mq_add_to_requeue_list(rq
, true);
462 EXPORT_SYMBOL(blk_mq_requeue_request
);
464 static void blk_mq_requeue_work(struct work_struct
*work
)
466 struct request_queue
*q
=
467 container_of(work
, struct request_queue
, requeue_work
);
469 struct request
*rq
, *next
;
472 spin_lock_irqsave(&q
->requeue_lock
, flags
);
473 list_splice_init(&q
->requeue_list
, &rq_list
);
474 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
476 list_for_each_entry_safe(rq
, next
, &rq_list
, queuelist
) {
477 if (!(rq
->cmd_flags
& REQ_SOFTBARRIER
))
480 rq
->cmd_flags
&= ~REQ_SOFTBARRIER
;
481 list_del_init(&rq
->queuelist
);
482 blk_mq_insert_request(rq
, true, false, false);
485 while (!list_empty(&rq_list
)) {
486 rq
= list_entry(rq_list
.next
, struct request
, queuelist
);
487 list_del_init(&rq
->queuelist
);
488 blk_mq_insert_request(rq
, false, false, false);
492 * Use the start variant of queue running here, so that running
493 * the requeue work will kick stopped queues.
495 blk_mq_start_hw_queues(q
);
498 void blk_mq_add_to_requeue_list(struct request
*rq
, bool at_head
)
500 struct request_queue
*q
= rq
->q
;
504 * We abuse this flag that is otherwise used by the I/O scheduler to
505 * request head insertation from the workqueue.
507 BUG_ON(rq
->cmd_flags
& REQ_SOFTBARRIER
);
509 spin_lock_irqsave(&q
->requeue_lock
, flags
);
511 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
512 list_add(&rq
->queuelist
, &q
->requeue_list
);
514 list_add_tail(&rq
->queuelist
, &q
->requeue_list
);
516 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
518 EXPORT_SYMBOL(blk_mq_add_to_requeue_list
);
520 void blk_mq_kick_requeue_list(struct request_queue
*q
)
522 kblockd_schedule_work(&q
->requeue_work
);
524 EXPORT_SYMBOL(blk_mq_kick_requeue_list
);
526 static inline bool is_flush_request(struct request
*rq
, unsigned int tag
)
528 return ((rq
->cmd_flags
& REQ_FLUSH_SEQ
) &&
529 rq
->q
->flush_rq
->tag
== tag
);
532 struct request
*blk_mq_tag_to_rq(struct blk_mq_tags
*tags
, unsigned int tag
)
534 struct request
*rq
= tags
->rqs
[tag
];
536 if (!is_flush_request(rq
, tag
))
539 return rq
->q
->flush_rq
;
541 EXPORT_SYMBOL(blk_mq_tag_to_rq
);
543 struct blk_mq_timeout_data
{
544 struct blk_mq_hw_ctx
*hctx
;
546 unsigned int *next_set
;
549 static void blk_mq_timeout_check(void *__data
, unsigned long *free_tags
)
551 struct blk_mq_timeout_data
*data
= __data
;
552 struct blk_mq_hw_ctx
*hctx
= data
->hctx
;
555 /* It may not be in flight yet (this is where
556 * the REQ_ATOMIC_STARTED flag comes in). The requests are
557 * statically allocated, so we know it's always safe to access the
558 * memory associated with a bit offset into ->rqs[].
564 tag
= find_next_zero_bit(free_tags
, hctx
->tags
->nr_tags
, tag
);
565 if (tag
>= hctx
->tags
->nr_tags
)
568 rq
= blk_mq_tag_to_rq(hctx
->tags
, tag
++);
569 if (rq
->q
!= hctx
->queue
)
571 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
574 blk_rq_check_expired(rq
, data
->next
, data
->next_set
);
578 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx
*hctx
,
580 unsigned int *next_set
)
582 struct blk_mq_timeout_data data
= {
585 .next_set
= next_set
,
589 * Ask the tagging code to iterate busy requests, so we can
590 * check them for timeout.
592 blk_mq_tag_busy_iter(hctx
->tags
, blk_mq_timeout_check
, &data
);
595 static enum blk_eh_timer_return
blk_mq_rq_timed_out(struct request
*rq
)
597 struct request_queue
*q
= rq
->q
;
600 * We know that complete is set at this point. If STARTED isn't set
601 * anymore, then the request isn't active and the "timeout" should
602 * just be ignored. This can happen due to the bitflag ordering.
603 * Timeout first checks if STARTED is set, and if it is, assumes
604 * the request is active. But if we race with completion, then
605 * we both flags will get cleared. So check here again, and ignore
606 * a timeout event with a request that isn't active.
608 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
609 return BLK_EH_NOT_HANDLED
;
611 if (!q
->mq_ops
->timeout
)
612 return BLK_EH_RESET_TIMER
;
614 return q
->mq_ops
->timeout(rq
);
617 static void blk_mq_rq_timer(unsigned long data
)
619 struct request_queue
*q
= (struct request_queue
*) data
;
620 struct blk_mq_hw_ctx
*hctx
;
621 unsigned long next
= 0;
624 queue_for_each_hw_ctx(q
, hctx
, i
) {
626 * If not software queues are currently mapped to this
627 * hardware queue, there's nothing to check
629 if (!hctx
->nr_ctx
|| !hctx
->tags
)
632 blk_mq_hw_ctx_check_timeout(hctx
, &next
, &next_set
);
636 next
= blk_rq_timeout(round_jiffies_up(next
));
637 mod_timer(&q
->timeout
, next
);
639 queue_for_each_hw_ctx(q
, hctx
, i
)
640 blk_mq_tag_idle(hctx
);
645 * Reverse check our software queue for entries that we could potentially
646 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
647 * too much time checking for merges.
649 static bool blk_mq_attempt_merge(struct request_queue
*q
,
650 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
655 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
661 if (!blk_rq_merge_ok(rq
, bio
))
664 el_ret
= blk_try_merge(rq
, bio
);
665 if (el_ret
== ELEVATOR_BACK_MERGE
) {
666 if (bio_attempt_back_merge(q
, rq
, bio
)) {
671 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
672 if (bio_attempt_front_merge(q
, rq
, bio
)) {
684 * Process software queues that have been marked busy, splicing them
685 * to the for-dispatch
687 static void flush_busy_ctxs(struct blk_mq_hw_ctx
*hctx
, struct list_head
*list
)
689 struct blk_mq_ctx
*ctx
;
692 for (i
= 0; i
< hctx
->ctx_map
.map_size
; i
++) {
693 struct blk_align_bitmap
*bm
= &hctx
->ctx_map
.map
[i
];
694 unsigned int off
, bit
;
700 off
= i
* hctx
->ctx_map
.bits_per_word
;
702 bit
= find_next_bit(&bm
->word
, bm
->depth
, bit
);
703 if (bit
>= bm
->depth
)
706 ctx
= hctx
->ctxs
[bit
+ off
];
707 clear_bit(bit
, &bm
->word
);
708 spin_lock(&ctx
->lock
);
709 list_splice_tail_init(&ctx
->rq_list
, list
);
710 spin_unlock(&ctx
->lock
);
718 * Run this hardware queue, pulling any software queues mapped to it in.
719 * Note that this function currently has various problems around ordering
720 * of IO. In particular, we'd like FIFO behaviour on handling existing
721 * items on the hctx->dispatch list. Ignore that for now.
723 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
725 struct request_queue
*q
= hctx
->queue
;
730 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx
->cpumask
));
732 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
738 * Touch any software queue that has pending entries.
740 flush_busy_ctxs(hctx
, &rq_list
);
743 * If we have previous entries on our dispatch list, grab them
744 * and stuff them at the front for more fair dispatch.
746 if (!list_empty_careful(&hctx
->dispatch
)) {
747 spin_lock(&hctx
->lock
);
748 if (!list_empty(&hctx
->dispatch
))
749 list_splice_init(&hctx
->dispatch
, &rq_list
);
750 spin_unlock(&hctx
->lock
);
754 * Now process all the entries, sending them to the driver.
757 while (!list_empty(&rq_list
)) {
760 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
761 list_del_init(&rq
->queuelist
);
763 blk_mq_start_request(rq
, list_empty(&rq_list
));
765 ret
= q
->mq_ops
->queue_rq(hctx
, rq
);
767 case BLK_MQ_RQ_QUEUE_OK
:
770 case BLK_MQ_RQ_QUEUE_BUSY
:
771 list_add(&rq
->queuelist
, &rq_list
);
772 __blk_mq_requeue_request(rq
);
775 pr_err("blk-mq: bad return on queue: %d\n", ret
);
776 case BLK_MQ_RQ_QUEUE_ERROR
:
778 blk_mq_end_io(rq
, rq
->errors
);
782 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
787 hctx
->dispatched
[0]++;
788 else if (queued
< (1 << (BLK_MQ_MAX_DISPATCH_ORDER
- 1)))
789 hctx
->dispatched
[ilog2(queued
) + 1]++;
792 * Any items that need requeuing? Stuff them into hctx->dispatch,
793 * that is where we will continue on next queue run.
795 if (!list_empty(&rq_list
)) {
796 spin_lock(&hctx
->lock
);
797 list_splice(&rq_list
, &hctx
->dispatch
);
798 spin_unlock(&hctx
->lock
);
803 * It'd be great if the workqueue API had a way to pass
804 * in a mask and had some smarts for more clever placement.
805 * For now we just round-robin here, switching for every
806 * BLK_MQ_CPU_WORK_BATCH queued items.
808 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx
*hctx
)
810 int cpu
= hctx
->next_cpu
;
812 if (--hctx
->next_cpu_batch
<= 0) {
815 next_cpu
= cpumask_next(hctx
->next_cpu
, hctx
->cpumask
);
816 if (next_cpu
>= nr_cpu_ids
)
817 next_cpu
= cpumask_first(hctx
->cpumask
);
819 hctx
->next_cpu
= next_cpu
;
820 hctx
->next_cpu_batch
= BLK_MQ_CPU_WORK_BATCH
;
826 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
828 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
831 if (!async
&& cpumask_test_cpu(smp_processor_id(), hctx
->cpumask
))
832 __blk_mq_run_hw_queue(hctx
);
833 else if (hctx
->queue
->nr_hw_queues
== 1)
834 kblockd_schedule_delayed_work(&hctx
->run_work
, 0);
838 cpu
= blk_mq_hctx_next_cpu(hctx
);
839 kblockd_schedule_delayed_work_on(cpu
, &hctx
->run_work
, 0);
843 void blk_mq_run_queues(struct request_queue
*q
, bool async
)
845 struct blk_mq_hw_ctx
*hctx
;
848 queue_for_each_hw_ctx(q
, hctx
, i
) {
849 if ((!blk_mq_hctx_has_pending(hctx
) &&
850 list_empty_careful(&hctx
->dispatch
)) ||
851 test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
855 blk_mq_run_hw_queue(hctx
, async
);
859 EXPORT_SYMBOL(blk_mq_run_queues
);
861 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
863 cancel_delayed_work(&hctx
->run_work
);
864 cancel_delayed_work(&hctx
->delay_work
);
865 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
867 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
869 void blk_mq_stop_hw_queues(struct request_queue
*q
)
871 struct blk_mq_hw_ctx
*hctx
;
874 queue_for_each_hw_ctx(q
, hctx
, i
)
875 blk_mq_stop_hw_queue(hctx
);
877 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
879 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
881 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
884 blk_mq_run_hw_queue(hctx
, false);
887 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
889 void blk_mq_start_hw_queues(struct request_queue
*q
)
891 struct blk_mq_hw_ctx
*hctx
;
894 queue_for_each_hw_ctx(q
, hctx
, i
)
895 blk_mq_start_hw_queue(hctx
);
897 EXPORT_SYMBOL(blk_mq_start_hw_queues
);
900 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
, bool async
)
902 struct blk_mq_hw_ctx
*hctx
;
905 queue_for_each_hw_ctx(q
, hctx
, i
) {
906 if (!test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
909 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
911 blk_mq_run_hw_queue(hctx
, async
);
915 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
917 static void blk_mq_run_work_fn(struct work_struct
*work
)
919 struct blk_mq_hw_ctx
*hctx
;
921 hctx
= container_of(work
, struct blk_mq_hw_ctx
, run_work
.work
);
923 __blk_mq_run_hw_queue(hctx
);
926 static void blk_mq_delay_work_fn(struct work_struct
*work
)
928 struct blk_mq_hw_ctx
*hctx
;
930 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delay_work
.work
);
932 if (test_and_clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
933 __blk_mq_run_hw_queue(hctx
);
936 void blk_mq_delay_queue(struct blk_mq_hw_ctx
*hctx
, unsigned long msecs
)
938 unsigned long tmo
= msecs_to_jiffies(msecs
);
940 if (hctx
->queue
->nr_hw_queues
== 1)
941 kblockd_schedule_delayed_work(&hctx
->delay_work
, tmo
);
945 cpu
= blk_mq_hctx_next_cpu(hctx
);
946 kblockd_schedule_delayed_work_on(cpu
, &hctx
->delay_work
, tmo
);
949 EXPORT_SYMBOL(blk_mq_delay_queue
);
951 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
952 struct request
*rq
, bool at_head
)
954 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
956 trace_block_rq_insert(hctx
->queue
, rq
);
959 list_add(&rq
->queuelist
, &ctx
->rq_list
);
961 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
963 blk_mq_hctx_mark_pending(hctx
, ctx
);
966 void blk_mq_insert_request(struct request
*rq
, bool at_head
, bool run_queue
,
969 struct request_queue
*q
= rq
->q
;
970 struct blk_mq_hw_ctx
*hctx
;
971 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
, *current_ctx
;
973 current_ctx
= blk_mq_get_ctx(q
);
974 if (!cpu_online(ctx
->cpu
))
975 rq
->mq_ctx
= ctx
= current_ctx
;
977 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
979 spin_lock(&ctx
->lock
);
980 __blk_mq_insert_request(hctx
, rq
, at_head
);
981 spin_unlock(&ctx
->lock
);
984 blk_mq_run_hw_queue(hctx
, async
);
986 blk_mq_put_ctx(current_ctx
);
989 static void blk_mq_insert_requests(struct request_queue
*q
,
990 struct blk_mq_ctx
*ctx
,
991 struct list_head
*list
,
996 struct blk_mq_hw_ctx
*hctx
;
997 struct blk_mq_ctx
*current_ctx
;
999 trace_block_unplug(q
, depth
, !from_schedule
);
1001 current_ctx
= blk_mq_get_ctx(q
);
1003 if (!cpu_online(ctx
->cpu
))
1005 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1008 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1011 spin_lock(&ctx
->lock
);
1012 while (!list_empty(list
)) {
1015 rq
= list_first_entry(list
, struct request
, queuelist
);
1016 list_del_init(&rq
->queuelist
);
1018 __blk_mq_insert_request(hctx
, rq
, false);
1020 spin_unlock(&ctx
->lock
);
1022 blk_mq_run_hw_queue(hctx
, from_schedule
);
1023 blk_mq_put_ctx(current_ctx
);
1026 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1028 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
1029 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
1031 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
1032 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
1033 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
1036 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
1038 struct blk_mq_ctx
*this_ctx
;
1039 struct request_queue
*this_q
;
1042 LIST_HEAD(ctx_list
);
1045 list_splice_init(&plug
->mq_list
, &list
);
1047 list_sort(NULL
, &list
, plug_ctx_cmp
);
1053 while (!list_empty(&list
)) {
1054 rq
= list_entry_rq(list
.next
);
1055 list_del_init(&rq
->queuelist
);
1057 if (rq
->mq_ctx
!= this_ctx
) {
1059 blk_mq_insert_requests(this_q
, this_ctx
,
1064 this_ctx
= rq
->mq_ctx
;
1070 list_add_tail(&rq
->queuelist
, &ctx_list
);
1074 * If 'this_ctx' is set, we know we have entries to complete
1075 * on 'ctx_list'. Do those.
1078 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
1083 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
1085 init_request_from_bio(rq
, bio
);
1087 if (blk_do_io_stat(rq
))
1088 blk_account_io_start(rq
, 1);
1091 static inline bool hctx_allow_merges(struct blk_mq_hw_ctx
*hctx
)
1093 return (hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
) &&
1094 !blk_queue_nomerges(hctx
->queue
);
1097 static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx
*hctx
,
1098 struct blk_mq_ctx
*ctx
,
1099 struct request
*rq
, struct bio
*bio
)
1101 if (!hctx_allow_merges(hctx
)) {
1102 blk_mq_bio_to_request(rq
, bio
);
1103 spin_lock(&ctx
->lock
);
1105 __blk_mq_insert_request(hctx
, rq
, false);
1106 spin_unlock(&ctx
->lock
);
1109 struct request_queue
*q
= hctx
->queue
;
1111 spin_lock(&ctx
->lock
);
1112 if (!blk_mq_attempt_merge(q
, ctx
, bio
)) {
1113 blk_mq_bio_to_request(rq
, bio
);
1117 spin_unlock(&ctx
->lock
);
1118 __blk_mq_free_request(hctx
, ctx
, rq
);
1123 struct blk_map_ctx
{
1124 struct blk_mq_hw_ctx
*hctx
;
1125 struct blk_mq_ctx
*ctx
;
1128 static struct request
*blk_mq_map_request(struct request_queue
*q
,
1130 struct blk_map_ctx
*data
)
1132 struct blk_mq_hw_ctx
*hctx
;
1133 struct blk_mq_ctx
*ctx
;
1135 int rw
= bio_data_dir(bio
);
1136 struct blk_mq_alloc_data alloc_data
;
1138 if (unlikely(blk_mq_queue_enter(q
))) {
1139 bio_endio(bio
, -EIO
);
1143 ctx
= blk_mq_get_ctx(q
);
1144 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1146 if (rw_is_sync(bio
->bi_rw
))
1149 trace_block_getrq(q
, bio
, rw
);
1150 blk_mq_set_alloc_data(&alloc_data
, q
, GFP_ATOMIC
, false, ctx
,
1152 rq
= __blk_mq_alloc_request(&alloc_data
, rw
);
1153 if (unlikely(!rq
)) {
1154 __blk_mq_run_hw_queue(hctx
);
1155 blk_mq_put_ctx(ctx
);
1156 trace_block_sleeprq(q
, bio
, rw
);
1158 ctx
= blk_mq_get_ctx(q
);
1159 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1160 blk_mq_set_alloc_data(&alloc_data
, q
,
1161 __GFP_WAIT
|GFP_ATOMIC
, false, ctx
, hctx
);
1162 rq
= __blk_mq_alloc_request(&alloc_data
, rw
);
1163 ctx
= alloc_data
.ctx
;
1164 hctx
= alloc_data
.hctx
;
1174 * Multiple hardware queue variant. This will not use per-process plugs,
1175 * but will attempt to bypass the hctx queueing if we can go straight to
1176 * hardware for SYNC IO.
1178 static void blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
1180 const int is_sync
= rw_is_sync(bio
->bi_rw
);
1181 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
1182 struct blk_map_ctx data
;
1185 blk_queue_bounce(q
, &bio
);
1187 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1188 bio_endio(bio
, -EIO
);
1192 rq
= blk_mq_map_request(q
, bio
, &data
);
1196 if (unlikely(is_flush_fua
)) {
1197 blk_mq_bio_to_request(rq
, bio
);
1198 blk_insert_flush(rq
);
1205 blk_mq_bio_to_request(rq
, bio
);
1206 blk_mq_start_request(rq
, true);
1209 * For OK queue, we are done. For error, kill it. Any other
1210 * error (busy), just add it to our list as we previously
1213 ret
= q
->mq_ops
->queue_rq(data
.hctx
, rq
);
1214 if (ret
== BLK_MQ_RQ_QUEUE_OK
)
1217 __blk_mq_requeue_request(rq
);
1219 if (ret
== BLK_MQ_RQ_QUEUE_ERROR
) {
1221 blk_mq_end_io(rq
, rq
->errors
);
1227 if (!blk_mq_merge_queue_io(data
.hctx
, data
.ctx
, rq
, bio
)) {
1229 * For a SYNC request, send it to the hardware immediately. For
1230 * an ASYNC request, just ensure that we run it later on. The
1231 * latter allows for merging opportunities and more efficient
1235 blk_mq_run_hw_queue(data
.hctx
, !is_sync
|| is_flush_fua
);
1238 blk_mq_put_ctx(data
.ctx
);
1242 * Single hardware queue variant. This will attempt to use any per-process
1243 * plug for merging and IO deferral.
1245 static void blk_sq_make_request(struct request_queue
*q
, struct bio
*bio
)
1247 const int is_sync
= rw_is_sync(bio
->bi_rw
);
1248 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
1249 unsigned int use_plug
, request_count
= 0;
1250 struct blk_map_ctx data
;
1254 * If we have multiple hardware queues, just go directly to
1255 * one of those for sync IO.
1257 use_plug
= !is_flush_fua
&& !is_sync
;
1259 blk_queue_bounce(q
, &bio
);
1261 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1262 bio_endio(bio
, -EIO
);
1266 if (use_plug
&& !blk_queue_nomerges(q
) &&
1267 blk_attempt_plug_merge(q
, bio
, &request_count
))
1270 rq
= blk_mq_map_request(q
, bio
, &data
);
1274 if (unlikely(is_flush_fua
)) {
1275 blk_mq_bio_to_request(rq
, bio
);
1276 blk_insert_flush(rq
);
1281 * A task plug currently exists. Since this is completely lockless,
1282 * utilize that to temporarily store requests until the task is
1283 * either done or scheduled away.
1286 struct blk_plug
*plug
= current
->plug
;
1289 blk_mq_bio_to_request(rq
, bio
);
1290 if (list_empty(&plug
->mq_list
))
1291 trace_block_plug(q
);
1292 else if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
1293 blk_flush_plug_list(plug
, false);
1294 trace_block_plug(q
);
1296 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
1297 blk_mq_put_ctx(data
.ctx
);
1302 if (!blk_mq_merge_queue_io(data
.hctx
, data
.ctx
, rq
, bio
)) {
1304 * For a SYNC request, send it to the hardware immediately. For
1305 * an ASYNC request, just ensure that we run it later on. The
1306 * latter allows for merging opportunities and more efficient
1310 blk_mq_run_hw_queue(data
.hctx
, !is_sync
|| is_flush_fua
);
1313 blk_mq_put_ctx(data
.ctx
);
1317 * Default mapping to a software queue, since we use one per CPU.
1319 struct blk_mq_hw_ctx
*blk_mq_map_queue(struct request_queue
*q
, const int cpu
)
1321 return q
->queue_hw_ctx
[q
->mq_map
[cpu
]];
1323 EXPORT_SYMBOL(blk_mq_map_queue
);
1325 static void blk_mq_free_rq_map(struct blk_mq_tag_set
*set
,
1326 struct blk_mq_tags
*tags
, unsigned int hctx_idx
)
1330 if (tags
->rqs
&& set
->ops
->exit_request
) {
1333 for (i
= 0; i
< tags
->nr_tags
; i
++) {
1336 set
->ops
->exit_request(set
->driver_data
, tags
->rqs
[i
],
1338 tags
->rqs
[i
] = NULL
;
1342 while (!list_empty(&tags
->page_list
)) {
1343 page
= list_first_entry(&tags
->page_list
, struct page
, lru
);
1344 list_del_init(&page
->lru
);
1345 __free_pages(page
, page
->private);
1350 blk_mq_free_tags(tags
);
1353 static size_t order_to_size(unsigned int order
)
1355 return (size_t)PAGE_SIZE
<< order
;
1358 static struct blk_mq_tags
*blk_mq_init_rq_map(struct blk_mq_tag_set
*set
,
1359 unsigned int hctx_idx
)
1361 struct blk_mq_tags
*tags
;
1362 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1363 size_t rq_size
, left
;
1365 tags
= blk_mq_init_tags(set
->queue_depth
, set
->reserved_tags
,
1370 INIT_LIST_HEAD(&tags
->page_list
);
1372 tags
->rqs
= kzalloc_node(set
->queue_depth
* sizeof(struct request
*),
1373 GFP_KERNEL
| __GFP_NOWARN
| __GFP_NORETRY
,
1376 blk_mq_free_tags(tags
);
1381 * rq_size is the size of the request plus driver payload, rounded
1382 * to the cacheline size
1384 rq_size
= round_up(sizeof(struct request
) + set
->cmd_size
,
1386 left
= rq_size
* set
->queue_depth
;
1388 for (i
= 0; i
< set
->queue_depth
; ) {
1389 int this_order
= max_order
;
1394 while (left
< order_to_size(this_order
- 1) && this_order
)
1398 page
= alloc_pages_node(set
->numa_node
,
1399 GFP_KERNEL
| __GFP_NOWARN
| __GFP_NORETRY
,
1405 if (order_to_size(this_order
) < rq_size
)
1412 page
->private = this_order
;
1413 list_add_tail(&page
->lru
, &tags
->page_list
);
1415 p
= page_address(page
);
1416 entries_per_page
= order_to_size(this_order
) / rq_size
;
1417 to_do
= min(entries_per_page
, set
->queue_depth
- i
);
1418 left
-= to_do
* rq_size
;
1419 for (j
= 0; j
< to_do
; j
++) {
1421 tags
->rqs
[i
]->atomic_flags
= 0;
1422 tags
->rqs
[i
]->cmd_flags
= 0;
1423 if (set
->ops
->init_request
) {
1424 if (set
->ops
->init_request(set
->driver_data
,
1425 tags
->rqs
[i
], hctx_idx
, i
,
1427 tags
->rqs
[i
] = NULL
;
1440 blk_mq_free_rq_map(set
, tags
, hctx_idx
);
1444 static void blk_mq_free_bitmap(struct blk_mq_ctxmap
*bitmap
)
1449 static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap
*bitmap
, int node
)
1451 unsigned int bpw
= 8, total
, num_maps
, i
;
1453 bitmap
->bits_per_word
= bpw
;
1455 num_maps
= ALIGN(nr_cpu_ids
, bpw
) / bpw
;
1456 bitmap
->map
= kzalloc_node(num_maps
* sizeof(struct blk_align_bitmap
),
1461 bitmap
->map_size
= num_maps
;
1464 for (i
= 0; i
< num_maps
; i
++) {
1465 bitmap
->map
[i
].depth
= min(total
, bitmap
->bits_per_word
);
1466 total
-= bitmap
->map
[i
].depth
;
1472 static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx
*hctx
, int cpu
)
1474 struct request_queue
*q
= hctx
->queue
;
1475 struct blk_mq_ctx
*ctx
;
1479 * Move ctx entries to new CPU, if this one is going away.
1481 ctx
= __blk_mq_get_ctx(q
, cpu
);
1483 spin_lock(&ctx
->lock
);
1484 if (!list_empty(&ctx
->rq_list
)) {
1485 list_splice_init(&ctx
->rq_list
, &tmp
);
1486 blk_mq_hctx_clear_pending(hctx
, ctx
);
1488 spin_unlock(&ctx
->lock
);
1490 if (list_empty(&tmp
))
1493 ctx
= blk_mq_get_ctx(q
);
1494 spin_lock(&ctx
->lock
);
1496 while (!list_empty(&tmp
)) {
1499 rq
= list_first_entry(&tmp
, struct request
, queuelist
);
1501 list_move_tail(&rq
->queuelist
, &ctx
->rq_list
);
1504 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1505 blk_mq_hctx_mark_pending(hctx
, ctx
);
1507 spin_unlock(&ctx
->lock
);
1509 blk_mq_run_hw_queue(hctx
, true);
1510 blk_mq_put_ctx(ctx
);
1514 static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx
*hctx
, int cpu
)
1516 struct request_queue
*q
= hctx
->queue
;
1517 struct blk_mq_tag_set
*set
= q
->tag_set
;
1519 if (set
->tags
[hctx
->queue_num
])
1522 set
->tags
[hctx
->queue_num
] = blk_mq_init_rq_map(set
, hctx
->queue_num
);
1523 if (!set
->tags
[hctx
->queue_num
])
1526 hctx
->tags
= set
->tags
[hctx
->queue_num
];
1530 static int blk_mq_hctx_notify(void *data
, unsigned long action
,
1533 struct blk_mq_hw_ctx
*hctx
= data
;
1535 if (action
== CPU_DEAD
|| action
== CPU_DEAD_FROZEN
)
1536 return blk_mq_hctx_cpu_offline(hctx
, cpu
);
1537 else if (action
== CPU_ONLINE
|| action
== CPU_ONLINE_FROZEN
)
1538 return blk_mq_hctx_cpu_online(hctx
, cpu
);
1543 static void blk_mq_exit_hw_queues(struct request_queue
*q
,
1544 struct blk_mq_tag_set
*set
, int nr_queue
)
1546 struct blk_mq_hw_ctx
*hctx
;
1549 queue_for_each_hw_ctx(q
, hctx
, i
) {
1553 blk_mq_tag_idle(hctx
);
1555 if (set
->ops
->exit_hctx
)
1556 set
->ops
->exit_hctx(hctx
, i
);
1558 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1560 blk_mq_free_bitmap(&hctx
->ctx_map
);
1565 static void blk_mq_free_hw_queues(struct request_queue
*q
,
1566 struct blk_mq_tag_set
*set
)
1568 struct blk_mq_hw_ctx
*hctx
;
1571 queue_for_each_hw_ctx(q
, hctx
, i
) {
1572 free_cpumask_var(hctx
->cpumask
);
1577 static int blk_mq_init_hw_queues(struct request_queue
*q
,
1578 struct blk_mq_tag_set
*set
)
1580 struct blk_mq_hw_ctx
*hctx
;
1584 * Initialize hardware queues
1586 queue_for_each_hw_ctx(q
, hctx
, i
) {
1589 node
= hctx
->numa_node
;
1590 if (node
== NUMA_NO_NODE
)
1591 node
= hctx
->numa_node
= set
->numa_node
;
1593 INIT_DELAYED_WORK(&hctx
->run_work
, blk_mq_run_work_fn
);
1594 INIT_DELAYED_WORK(&hctx
->delay_work
, blk_mq_delay_work_fn
);
1595 spin_lock_init(&hctx
->lock
);
1596 INIT_LIST_HEAD(&hctx
->dispatch
);
1598 hctx
->queue_num
= i
;
1599 hctx
->flags
= set
->flags
;
1600 hctx
->cmd_size
= set
->cmd_size
;
1602 blk_mq_init_cpu_notifier(&hctx
->cpu_notifier
,
1603 blk_mq_hctx_notify
, hctx
);
1604 blk_mq_register_cpu_notifier(&hctx
->cpu_notifier
);
1606 hctx
->tags
= set
->tags
[i
];
1609 * Allocate space for all possible cpus to avoid allocation at
1612 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1617 if (blk_mq_alloc_bitmap(&hctx
->ctx_map
, node
))
1622 if (set
->ops
->init_hctx
&&
1623 set
->ops
->init_hctx(hctx
, set
->driver_data
, i
))
1627 if (i
== q
->nr_hw_queues
)
1633 blk_mq_exit_hw_queues(q
, set
, i
);
1638 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1639 unsigned int nr_hw_queues
)
1643 for_each_possible_cpu(i
) {
1644 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1645 struct blk_mq_hw_ctx
*hctx
;
1647 memset(__ctx
, 0, sizeof(*__ctx
));
1649 spin_lock_init(&__ctx
->lock
);
1650 INIT_LIST_HEAD(&__ctx
->rq_list
);
1653 /* If the cpu isn't online, the cpu is mapped to first hctx */
1657 hctx
= q
->mq_ops
->map_queue(q
, i
);
1658 cpumask_set_cpu(i
, hctx
->cpumask
);
1662 * Set local node, IFF we have more than one hw queue. If
1663 * not, we remain on the home node of the device
1665 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1666 hctx
->numa_node
= cpu_to_node(i
);
1670 static void blk_mq_map_swqueue(struct request_queue
*q
)
1673 struct blk_mq_hw_ctx
*hctx
;
1674 struct blk_mq_ctx
*ctx
;
1676 queue_for_each_hw_ctx(q
, hctx
, i
) {
1677 cpumask_clear(hctx
->cpumask
);
1682 * Map software to hardware queues
1684 queue_for_each_ctx(q
, ctx
, i
) {
1685 /* If the cpu isn't online, the cpu is mapped to first hctx */
1689 hctx
= q
->mq_ops
->map_queue(q
, i
);
1690 cpumask_set_cpu(i
, hctx
->cpumask
);
1691 ctx
->index_hw
= hctx
->nr_ctx
;
1692 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1695 queue_for_each_hw_ctx(q
, hctx
, i
) {
1697 * If no software queues are mapped to this hardware queue,
1698 * disable it and free the request entries.
1700 if (!hctx
->nr_ctx
) {
1701 struct blk_mq_tag_set
*set
= q
->tag_set
;
1704 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
1705 set
->tags
[i
] = NULL
;
1712 * Initialize batch roundrobin counts
1714 hctx
->next_cpu
= cpumask_first(hctx
->cpumask
);
1715 hctx
->next_cpu_batch
= BLK_MQ_CPU_WORK_BATCH
;
1719 static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set
*set
)
1721 struct blk_mq_hw_ctx
*hctx
;
1722 struct request_queue
*q
;
1726 if (set
->tag_list
.next
== set
->tag_list
.prev
)
1731 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
) {
1732 blk_mq_freeze_queue(q
);
1734 queue_for_each_hw_ctx(q
, hctx
, i
) {
1736 hctx
->flags
|= BLK_MQ_F_TAG_SHARED
;
1738 hctx
->flags
&= ~BLK_MQ_F_TAG_SHARED
;
1740 blk_mq_unfreeze_queue(q
);
1744 static void blk_mq_del_queue_tag_set(struct request_queue
*q
)
1746 struct blk_mq_tag_set
*set
= q
->tag_set
;
1748 mutex_lock(&set
->tag_list_lock
);
1749 list_del_init(&q
->tag_set_list
);
1750 blk_mq_update_tag_set_depth(set
);
1751 mutex_unlock(&set
->tag_list_lock
);
1754 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set
*set
,
1755 struct request_queue
*q
)
1759 mutex_lock(&set
->tag_list_lock
);
1760 list_add_tail(&q
->tag_set_list
, &set
->tag_list
);
1761 blk_mq_update_tag_set_depth(set
);
1762 mutex_unlock(&set
->tag_list_lock
);
1765 struct request_queue
*blk_mq_init_queue(struct blk_mq_tag_set
*set
)
1767 struct blk_mq_hw_ctx
**hctxs
;
1768 struct blk_mq_ctx __percpu
*ctx
;
1769 struct request_queue
*q
;
1773 ctx
= alloc_percpu(struct blk_mq_ctx
);
1775 return ERR_PTR(-ENOMEM
);
1777 hctxs
= kmalloc_node(set
->nr_hw_queues
* sizeof(*hctxs
), GFP_KERNEL
,
1783 map
= blk_mq_make_queue_map(set
);
1787 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1788 int node
= blk_mq_hw_queue_to_node(map
, i
);
1790 hctxs
[i
] = kzalloc_node(sizeof(struct blk_mq_hw_ctx
),
1795 if (!zalloc_cpumask_var(&hctxs
[i
]->cpumask
, GFP_KERNEL
))
1798 atomic_set(&hctxs
[i
]->nr_active
, 0);
1799 hctxs
[i
]->numa_node
= node
;
1800 hctxs
[i
]->queue_num
= i
;
1803 q
= blk_alloc_queue_node(GFP_KERNEL
, set
->numa_node
);
1807 if (percpu_ref_init(&q
->mq_usage_counter
, blk_mq_usage_counter_release
))
1810 setup_timer(&q
->timeout
, blk_mq_rq_timer
, (unsigned long) q
);
1811 blk_queue_rq_timeout(q
, 30000);
1813 q
->nr_queues
= nr_cpu_ids
;
1814 q
->nr_hw_queues
= set
->nr_hw_queues
;
1818 q
->queue_hw_ctx
= hctxs
;
1820 q
->mq_ops
= set
->ops
;
1821 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
1823 if (!(set
->flags
& BLK_MQ_F_SG_MERGE
))
1824 q
->queue_flags
|= 1 << QUEUE_FLAG_NO_SG_MERGE
;
1826 q
->sg_reserved_size
= INT_MAX
;
1828 INIT_WORK(&q
->requeue_work
, blk_mq_requeue_work
);
1829 INIT_LIST_HEAD(&q
->requeue_list
);
1830 spin_lock_init(&q
->requeue_lock
);
1832 if (q
->nr_hw_queues
> 1)
1833 blk_queue_make_request(q
, blk_mq_make_request
);
1835 blk_queue_make_request(q
, blk_sq_make_request
);
1837 blk_queue_rq_timed_out(q
, blk_mq_rq_timed_out
);
1839 blk_queue_rq_timeout(q
, set
->timeout
);
1842 * Do this after blk_queue_make_request() overrides it...
1844 q
->nr_requests
= set
->queue_depth
;
1846 if (set
->ops
->complete
)
1847 blk_queue_softirq_done(q
, set
->ops
->complete
);
1849 blk_mq_init_flush(q
);
1850 blk_mq_init_cpu_queues(q
, set
->nr_hw_queues
);
1852 q
->flush_rq
= kzalloc(round_up(sizeof(struct request
) +
1853 set
->cmd_size
, cache_line_size()),
1858 if (blk_mq_init_hw_queues(q
, set
))
1861 mutex_lock(&all_q_mutex
);
1862 list_add_tail(&q
->all_q_node
, &all_q_list
);
1863 mutex_unlock(&all_q_mutex
);
1865 blk_mq_add_queue_tag_set(set
, q
);
1867 blk_mq_map_swqueue(q
);
1874 blk_cleanup_queue(q
);
1877 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1880 free_cpumask_var(hctxs
[i
]->cpumask
);
1887 return ERR_PTR(-ENOMEM
);
1889 EXPORT_SYMBOL(blk_mq_init_queue
);
1891 void blk_mq_free_queue(struct request_queue
*q
)
1893 struct blk_mq_tag_set
*set
= q
->tag_set
;
1895 blk_mq_del_queue_tag_set(q
);
1897 blk_mq_exit_hw_queues(q
, set
, set
->nr_hw_queues
);
1898 blk_mq_free_hw_queues(q
, set
);
1900 percpu_ref_exit(&q
->mq_usage_counter
);
1902 free_percpu(q
->queue_ctx
);
1903 kfree(q
->queue_hw_ctx
);
1906 q
->queue_ctx
= NULL
;
1907 q
->queue_hw_ctx
= NULL
;
1910 mutex_lock(&all_q_mutex
);
1911 list_del_init(&q
->all_q_node
);
1912 mutex_unlock(&all_q_mutex
);
1915 /* Basically redo blk_mq_init_queue with queue frozen */
1916 static void blk_mq_queue_reinit(struct request_queue
*q
)
1918 blk_mq_freeze_queue(q
);
1920 blk_mq_sysfs_unregister(q
);
1922 blk_mq_update_queue_map(q
->mq_map
, q
->nr_hw_queues
);
1925 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1926 * we should change hctx numa_node according to new topology (this
1927 * involves free and re-allocate memory, worthy doing?)
1930 blk_mq_map_swqueue(q
);
1932 blk_mq_sysfs_register(q
);
1934 blk_mq_unfreeze_queue(q
);
1937 static int blk_mq_queue_reinit_notify(struct notifier_block
*nb
,
1938 unsigned long action
, void *hcpu
)
1940 struct request_queue
*q
;
1943 * Before new mappings are established, hotadded cpu might already
1944 * start handling requests. This doesn't break anything as we map
1945 * offline CPUs to first hardware queue. We will re-init the queue
1946 * below to get optimal settings.
1948 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
&&
1949 action
!= CPU_ONLINE
&& action
!= CPU_ONLINE_FROZEN
)
1952 mutex_lock(&all_q_mutex
);
1953 list_for_each_entry(q
, &all_q_list
, all_q_node
)
1954 blk_mq_queue_reinit(q
);
1955 mutex_unlock(&all_q_mutex
);
1959 static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set
*set
)
1963 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1964 set
->tags
[i
] = blk_mq_init_rq_map(set
, i
);
1973 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
1979 * Allocate the request maps associated with this tag_set. Note that this
1980 * may reduce the depth asked for, if memory is tight. set->queue_depth
1981 * will be updated to reflect the allocated depth.
1983 static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set
*set
)
1988 depth
= set
->queue_depth
;
1990 err
= __blk_mq_alloc_rq_maps(set
);
1994 set
->queue_depth
>>= 1;
1995 if (set
->queue_depth
< set
->reserved_tags
+ BLK_MQ_TAG_MIN
) {
1999 } while (set
->queue_depth
);
2001 if (!set
->queue_depth
|| err
) {
2002 pr_err("blk-mq: failed to allocate request map\n");
2006 if (depth
!= set
->queue_depth
)
2007 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2008 depth
, set
->queue_depth
);
2014 * Alloc a tag set to be associated with one or more request queues.
2015 * May fail with EINVAL for various error conditions. May adjust the
2016 * requested depth down, if if it too large. In that case, the set
2017 * value will be stored in set->queue_depth.
2019 int blk_mq_alloc_tag_set(struct blk_mq_tag_set
*set
)
2021 if (!set
->nr_hw_queues
)
2023 if (!set
->queue_depth
)
2025 if (set
->queue_depth
< set
->reserved_tags
+ BLK_MQ_TAG_MIN
)
2028 if (!set
->nr_hw_queues
|| !set
->ops
->queue_rq
|| !set
->ops
->map_queue
)
2031 if (set
->queue_depth
> BLK_MQ_MAX_DEPTH
) {
2032 pr_info("blk-mq: reduced tag depth to %u\n",
2034 set
->queue_depth
= BLK_MQ_MAX_DEPTH
;
2037 set
->tags
= kmalloc_node(set
->nr_hw_queues
*
2038 sizeof(struct blk_mq_tags
*),
2039 GFP_KERNEL
, set
->numa_node
);
2043 if (blk_mq_alloc_rq_maps(set
))
2046 mutex_init(&set
->tag_list_lock
);
2047 INIT_LIST_HEAD(&set
->tag_list
);
2055 EXPORT_SYMBOL(blk_mq_alloc_tag_set
);
2057 void blk_mq_free_tag_set(struct blk_mq_tag_set
*set
)
2061 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
2063 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
2069 EXPORT_SYMBOL(blk_mq_free_tag_set
);
2071 int blk_mq_update_nr_requests(struct request_queue
*q
, unsigned int nr
)
2073 struct blk_mq_tag_set
*set
= q
->tag_set
;
2074 struct blk_mq_hw_ctx
*hctx
;
2077 if (!set
|| nr
> set
->queue_depth
)
2081 queue_for_each_hw_ctx(q
, hctx
, i
) {
2082 ret
= blk_mq_tag_update_depth(hctx
->tags
, nr
);
2088 q
->nr_requests
= nr
;
2093 void blk_mq_disable_hotplug(void)
2095 mutex_lock(&all_q_mutex
);
2098 void blk_mq_enable_hotplug(void)
2100 mutex_unlock(&all_q_mutex
);
2103 static int __init
blk_mq_init(void)
2107 hotcpu_notifier(blk_mq_queue_reinit_notify
, 0);
2111 subsys_initcall(blk_mq_init
);