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
);
36 static struct blk_mq_ctx
*__blk_mq_get_ctx(struct request_queue
*q
,
39 return per_cpu_ptr(q
->queue_ctx
, cpu
);
43 * This assumes per-cpu software queueing queues. They could be per-node
44 * as well, for instance. For now this is hardcoded as-is. Note that we don't
45 * care about preemption, since we know the ctx's are persistent. This does
46 * mean that we can't rely on ctx always matching the currently running CPU.
48 static struct blk_mq_ctx
*blk_mq_get_ctx(struct request_queue
*q
)
50 return __blk_mq_get_ctx(q
, get_cpu());
53 static void blk_mq_put_ctx(struct blk_mq_ctx
*ctx
)
59 * Check if any of the ctx's have pending work in this hardware queue
61 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx
*hctx
)
65 for (i
= 0; i
< hctx
->ctx_map
.map_size
; i
++)
66 if (hctx
->ctx_map
.map
[i
].word
)
72 static inline struct blk_align_bitmap
*get_bm(struct blk_mq_hw_ctx
*hctx
,
73 struct blk_mq_ctx
*ctx
)
75 return &hctx
->ctx_map
.map
[ctx
->index_hw
/ hctx
->ctx_map
.bits_per_word
];
78 #define CTX_TO_BIT(hctx, ctx) \
79 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
82 * Mark this ctx as having pending work in this hardware queue
84 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx
*hctx
,
85 struct blk_mq_ctx
*ctx
)
87 struct blk_align_bitmap
*bm
= get_bm(hctx
, ctx
);
89 if (!test_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
))
90 set_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
);
93 static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx
*hctx
,
94 struct blk_mq_ctx
*ctx
)
96 struct blk_align_bitmap
*bm
= get_bm(hctx
, ctx
);
98 clear_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
);
101 static int blk_mq_queue_enter(struct request_queue
*q
)
105 __percpu_counter_add(&q
->mq_usage_counter
, 1, 1000000);
107 /* we have problems to freeze the queue if it's initializing */
108 if (!blk_queue_bypass(q
) || !blk_queue_init_done(q
))
111 __percpu_counter_add(&q
->mq_usage_counter
, -1, 1000000);
113 spin_lock_irq(q
->queue_lock
);
114 ret
= wait_event_interruptible_lock_irq(q
->mq_freeze_wq
,
115 !blk_queue_bypass(q
) || blk_queue_dying(q
),
117 /* inc usage with lock hold to avoid freeze_queue runs here */
118 if (!ret
&& !blk_queue_dying(q
))
119 __percpu_counter_add(&q
->mq_usage_counter
, 1, 1000000);
120 else if (blk_queue_dying(q
))
122 spin_unlock_irq(q
->queue_lock
);
127 static void blk_mq_queue_exit(struct request_queue
*q
)
129 __percpu_counter_add(&q
->mq_usage_counter
, -1, 1000000);
132 static void __blk_mq_drain_queue(struct request_queue
*q
)
137 spin_lock_irq(q
->queue_lock
);
138 count
= percpu_counter_sum(&q
->mq_usage_counter
);
139 spin_unlock_irq(q
->queue_lock
);
143 blk_mq_run_queues(q
, false);
149 * Guarantee no request is in use, so we can change any data structure of
150 * the queue afterward.
152 static void blk_mq_freeze_queue(struct request_queue
*q
)
156 spin_lock_irq(q
->queue_lock
);
157 drain
= !q
->bypass_depth
++;
158 queue_flag_set(QUEUE_FLAG_BYPASS
, q
);
159 spin_unlock_irq(q
->queue_lock
);
162 __blk_mq_drain_queue(q
);
165 void blk_mq_drain_queue(struct request_queue
*q
)
167 __blk_mq_drain_queue(q
);
170 static void blk_mq_unfreeze_queue(struct request_queue
*q
)
174 spin_lock_irq(q
->queue_lock
);
175 if (!--q
->bypass_depth
) {
176 queue_flag_clear(QUEUE_FLAG_BYPASS
, q
);
179 WARN_ON_ONCE(q
->bypass_depth
< 0);
180 spin_unlock_irq(q
->queue_lock
);
182 wake_up_all(&q
->mq_freeze_wq
);
185 bool blk_mq_can_queue(struct blk_mq_hw_ctx
*hctx
)
187 return blk_mq_has_free_tags(hctx
->tags
);
189 EXPORT_SYMBOL(blk_mq_can_queue
);
191 static void blk_mq_rq_ctx_init(struct request_queue
*q
, struct blk_mq_ctx
*ctx
,
192 struct request
*rq
, unsigned int rw_flags
)
194 if (blk_queue_io_stat(q
))
195 rw_flags
|= REQ_IO_STAT
;
197 INIT_LIST_HEAD(&rq
->queuelist
);
198 /* csd/requeue_work/fifo_time is initialized before use */
201 rq
->cmd_flags
|= rw_flags
;
202 /* do not touch atomic flags, it needs atomic ops against the timer */
204 INIT_HLIST_NODE(&rq
->hash
);
205 RB_CLEAR_NODE(&rq
->rb_node
);
208 #ifdef CONFIG_BLK_CGROUP
210 set_start_time_ns(rq
);
211 rq
->io_start_time_ns
= 0;
213 rq
->nr_phys_segments
= 0;
214 #if defined(CONFIG_BLK_DEV_INTEGRITY)
215 rq
->nr_integrity_segments
= 0;
218 /* tag was already set */
226 INIT_LIST_HEAD(&rq
->timeout_list
);
228 rq
->end_io_data
= NULL
;
231 ctx
->rq_dispatched
[rw_is_sync(rw_flags
)]++;
234 static struct request
*
235 __blk_mq_alloc_request(struct request_queue
*q
, struct blk_mq_hw_ctx
*hctx
,
236 struct blk_mq_ctx
*ctx
, int rw
, gfp_t gfp
, bool reserved
)
241 tag
= blk_mq_get_tag(hctx
, &ctx
->last_tag
, gfp
, reserved
);
242 if (tag
!= BLK_MQ_TAG_FAIL
) {
243 rq
= hctx
->tags
->rqs
[tag
];
246 if (blk_mq_tag_busy(hctx
)) {
247 rq
->cmd_flags
= REQ_MQ_INFLIGHT
;
248 atomic_inc(&hctx
->nr_active
);
252 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
259 struct request
*blk_mq_alloc_request(struct request_queue
*q
, int rw
, gfp_t gfp
,
262 struct blk_mq_ctx
*ctx
;
263 struct blk_mq_hw_ctx
*hctx
;
266 if (blk_mq_queue_enter(q
))
269 ctx
= blk_mq_get_ctx(q
);
270 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
272 rq
= __blk_mq_alloc_request(q
, hctx
, ctx
, rw
, gfp
& ~__GFP_WAIT
,
274 if (!rq
&& (gfp
& __GFP_WAIT
)) {
275 __blk_mq_run_hw_queue(hctx
);
278 ctx
= blk_mq_get_ctx(q
);
279 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
280 rq
= __blk_mq_alloc_request(q
, hctx
, ctx
, rw
, gfp
, reserved
);
285 EXPORT_SYMBOL(blk_mq_alloc_request
);
287 static void __blk_mq_free_request(struct blk_mq_hw_ctx
*hctx
,
288 struct blk_mq_ctx
*ctx
, struct request
*rq
)
290 const int tag
= rq
->tag
;
291 struct request_queue
*q
= rq
->q
;
293 if (rq
->cmd_flags
& REQ_MQ_INFLIGHT
)
294 atomic_dec(&hctx
->nr_active
);
296 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
297 blk_mq_put_tag(hctx
, tag
, &ctx
->last_tag
);
298 blk_mq_queue_exit(q
);
301 void blk_mq_free_request(struct request
*rq
)
303 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
304 struct blk_mq_hw_ctx
*hctx
;
305 struct request_queue
*q
= rq
->q
;
307 ctx
->rq_completed
[rq_is_sync(rq
)]++;
309 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
310 __blk_mq_free_request(hctx
, ctx
, rq
);
314 * Clone all relevant state from a request that has been put on hold in
315 * the flush state machine into the preallocated flush request that hangs
316 * off the request queue.
318 * For a driver the flush request should be invisible, that's why we are
319 * impersonating the original request here.
321 void blk_mq_clone_flush_request(struct request
*flush_rq
,
322 struct request
*orig_rq
)
324 struct blk_mq_hw_ctx
*hctx
=
325 orig_rq
->q
->mq_ops
->map_queue(orig_rq
->q
, orig_rq
->mq_ctx
->cpu
);
327 flush_rq
->mq_ctx
= orig_rq
->mq_ctx
;
328 flush_rq
->tag
= orig_rq
->tag
;
329 memcpy(blk_mq_rq_to_pdu(flush_rq
), blk_mq_rq_to_pdu(orig_rq
),
333 inline void __blk_mq_end_io(struct request
*rq
, int error
)
335 blk_account_io_done(rq
);
338 rq
->end_io(rq
, error
);
340 if (unlikely(blk_bidi_rq(rq
)))
341 blk_mq_free_request(rq
->next_rq
);
342 blk_mq_free_request(rq
);
345 EXPORT_SYMBOL(__blk_mq_end_io
);
347 void blk_mq_end_io(struct request
*rq
, int error
)
349 if (blk_update_request(rq
, error
, blk_rq_bytes(rq
)))
351 __blk_mq_end_io(rq
, error
);
353 EXPORT_SYMBOL(blk_mq_end_io
);
355 static void __blk_mq_complete_request_remote(void *data
)
357 struct request
*rq
= data
;
359 rq
->q
->softirq_done_fn(rq
);
362 static void blk_mq_ipi_complete_request(struct request
*rq
)
364 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
368 if (!test_bit(QUEUE_FLAG_SAME_COMP
, &rq
->q
->queue_flags
)) {
369 rq
->q
->softirq_done_fn(rq
);
374 if (!test_bit(QUEUE_FLAG_SAME_FORCE
, &rq
->q
->queue_flags
))
375 shared
= cpus_share_cache(cpu
, ctx
->cpu
);
377 if (cpu
!= ctx
->cpu
&& !shared
&& cpu_online(ctx
->cpu
)) {
378 rq
->csd
.func
= __blk_mq_complete_request_remote
;
381 smp_call_function_single_async(ctx
->cpu
, &rq
->csd
);
383 rq
->q
->softirq_done_fn(rq
);
388 void __blk_mq_complete_request(struct request
*rq
)
390 struct request_queue
*q
= rq
->q
;
392 if (!q
->softirq_done_fn
)
393 blk_mq_end_io(rq
, rq
->errors
);
395 blk_mq_ipi_complete_request(rq
);
399 * blk_mq_complete_request - end I/O on a request
400 * @rq: the request being processed
403 * Ends all I/O on a request. It does not handle partial completions.
404 * The actual completion happens out-of-order, through a IPI handler.
406 void blk_mq_complete_request(struct request
*rq
)
408 struct request_queue
*q
= rq
->q
;
410 if (unlikely(blk_should_fake_timeout(q
)))
412 if (!blk_mark_rq_complete(rq
))
413 __blk_mq_complete_request(rq
);
415 EXPORT_SYMBOL(blk_mq_complete_request
);
417 static void blk_mq_start_request(struct request
*rq
, bool last
)
419 struct request_queue
*q
= rq
->q
;
421 trace_block_rq_issue(q
, rq
);
423 rq
->resid_len
= blk_rq_bytes(rq
);
424 if (unlikely(blk_bidi_rq(rq
)))
425 rq
->next_rq
->resid_len
= blk_rq_bytes(rq
->next_rq
);
428 * Just mark start time and set the started bit. Due to memory
429 * ordering, we know we'll see the correct deadline as long as
430 * REQ_ATOMIC_STARTED is seen. Use the default queue timeout,
431 * unless one has been set in the request.
434 rq
->deadline
= jiffies
+ q
->rq_timeout
;
436 rq
->deadline
= jiffies
+ rq
->timeout
;
439 * Mark us as started and clear complete. Complete might have been
440 * set if requeue raced with timeout, which then marked it as
441 * complete. So be sure to clear complete again when we start
442 * the request, otherwise we'll ignore the completion event.
444 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
445 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
446 if (test_bit(REQ_ATOM_COMPLETE
, &rq
->atomic_flags
))
447 clear_bit(REQ_ATOM_COMPLETE
, &rq
->atomic_flags
);
449 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
451 * Make sure space for the drain appears. We know we can do
452 * this because max_hw_segments has been adjusted to be one
453 * fewer than the device can handle.
455 rq
->nr_phys_segments
++;
459 * Flag the last request in the series so that drivers know when IO
460 * should be kicked off, if they don't do it on a per-request basis.
462 * Note: the flag isn't the only condition drivers should do kick off.
463 * If drive is busy, the last request might not have the bit set.
466 rq
->cmd_flags
|= REQ_END
;
469 static void __blk_mq_requeue_request(struct request
*rq
)
471 struct request_queue
*q
= rq
->q
;
473 trace_block_rq_requeue(q
, rq
);
474 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
476 rq
->cmd_flags
&= ~REQ_END
;
478 if (q
->dma_drain_size
&& blk_rq_bytes(rq
))
479 rq
->nr_phys_segments
--;
482 void blk_mq_requeue_request(struct request
*rq
)
484 __blk_mq_requeue_request(rq
);
485 blk_clear_rq_complete(rq
);
487 BUG_ON(blk_queued_rq(rq
));
488 blk_mq_add_to_requeue_list(rq
, true);
490 EXPORT_SYMBOL(blk_mq_requeue_request
);
492 static void blk_mq_requeue_work(struct work_struct
*work
)
494 struct request_queue
*q
=
495 container_of(work
, struct request_queue
, requeue_work
);
497 struct request
*rq
, *next
;
500 spin_lock_irqsave(&q
->requeue_lock
, flags
);
501 list_splice_init(&q
->requeue_list
, &rq_list
);
502 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
504 list_for_each_entry_safe(rq
, next
, &rq_list
, queuelist
) {
505 if (!(rq
->cmd_flags
& REQ_SOFTBARRIER
))
508 rq
->cmd_flags
&= ~REQ_SOFTBARRIER
;
509 list_del_init(&rq
->queuelist
);
510 blk_mq_insert_request(rq
, true, false, false);
513 while (!list_empty(&rq_list
)) {
514 rq
= list_entry(rq_list
.next
, struct request
, queuelist
);
515 list_del_init(&rq
->queuelist
);
516 blk_mq_insert_request(rq
, false, false, false);
519 blk_mq_run_queues(q
, false);
522 void blk_mq_add_to_requeue_list(struct request
*rq
, bool at_head
)
524 struct request_queue
*q
= rq
->q
;
528 * We abuse this flag that is otherwise used by the I/O scheduler to
529 * request head insertation from the workqueue.
531 BUG_ON(rq
->cmd_flags
& REQ_SOFTBARRIER
);
533 spin_lock_irqsave(&q
->requeue_lock
, flags
);
535 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
536 list_add(&rq
->queuelist
, &q
->requeue_list
);
538 list_add_tail(&rq
->queuelist
, &q
->requeue_list
);
540 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
542 EXPORT_SYMBOL(blk_mq_add_to_requeue_list
);
544 void blk_mq_kick_requeue_list(struct request_queue
*q
)
546 kblockd_schedule_work(&q
->requeue_work
);
548 EXPORT_SYMBOL(blk_mq_kick_requeue_list
);
550 struct request
*blk_mq_tag_to_rq(struct blk_mq_hw_ctx
*hctx
, unsigned int tag
)
552 struct request_queue
*q
= hctx
->queue
;
554 if ((q
->flush_rq
->cmd_flags
& REQ_FLUSH_SEQ
) &&
555 q
->flush_rq
->tag
== tag
)
558 return hctx
->tags
->rqs
[tag
];
560 EXPORT_SYMBOL(blk_mq_tag_to_rq
);
562 struct blk_mq_timeout_data
{
563 struct blk_mq_hw_ctx
*hctx
;
565 unsigned int *next_set
;
568 static void blk_mq_timeout_check(void *__data
, unsigned long *free_tags
)
570 struct blk_mq_timeout_data
*data
= __data
;
571 struct blk_mq_hw_ctx
*hctx
= data
->hctx
;
574 /* It may not be in flight yet (this is where
575 * the REQ_ATOMIC_STARTED flag comes in). The requests are
576 * statically allocated, so we know it's always safe to access the
577 * memory associated with a bit offset into ->rqs[].
583 tag
= find_next_zero_bit(free_tags
, hctx
->tags
->nr_tags
, tag
);
584 if (tag
>= hctx
->tags
->nr_tags
)
587 rq
= blk_mq_tag_to_rq(hctx
, tag
++);
588 if (rq
->q
!= hctx
->queue
)
590 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
593 blk_rq_check_expired(rq
, data
->next
, data
->next_set
);
597 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx
*hctx
,
599 unsigned int *next_set
)
601 struct blk_mq_timeout_data data
= {
604 .next_set
= next_set
,
608 * Ask the tagging code to iterate busy requests, so we can
609 * check them for timeout.
611 blk_mq_tag_busy_iter(hctx
->tags
, blk_mq_timeout_check
, &data
);
614 static enum blk_eh_timer_return
blk_mq_rq_timed_out(struct request
*rq
)
616 struct request_queue
*q
= rq
->q
;
619 * We know that complete is set at this point. If STARTED isn't set
620 * anymore, then the request isn't active and the "timeout" should
621 * just be ignored. This can happen due to the bitflag ordering.
622 * Timeout first checks if STARTED is set, and if it is, assumes
623 * the request is active. But if we race with completion, then
624 * we both flags will get cleared. So check here again, and ignore
625 * a timeout event with a request that isn't active.
627 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
628 return BLK_EH_NOT_HANDLED
;
630 if (!q
->mq_ops
->timeout
)
631 return BLK_EH_RESET_TIMER
;
633 return q
->mq_ops
->timeout(rq
);
636 static void blk_mq_rq_timer(unsigned long data
)
638 struct request_queue
*q
= (struct request_queue
*) data
;
639 struct blk_mq_hw_ctx
*hctx
;
640 unsigned long next
= 0;
643 queue_for_each_hw_ctx(q
, hctx
, i
) {
645 * If not software queues are currently mapped to this
646 * hardware queue, there's nothing to check
648 if (!hctx
->nr_ctx
|| !hctx
->tags
)
651 blk_mq_hw_ctx_check_timeout(hctx
, &next
, &next_set
);
655 next
= blk_rq_timeout(round_jiffies_up(next
));
656 mod_timer(&q
->timeout
, next
);
658 queue_for_each_hw_ctx(q
, hctx
, i
)
659 blk_mq_tag_idle(hctx
);
664 * Reverse check our software queue for entries that we could potentially
665 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
666 * too much time checking for merges.
668 static bool blk_mq_attempt_merge(struct request_queue
*q
,
669 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
674 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
680 if (!blk_rq_merge_ok(rq
, bio
))
683 el_ret
= blk_try_merge(rq
, bio
);
684 if (el_ret
== ELEVATOR_BACK_MERGE
) {
685 if (bio_attempt_back_merge(q
, rq
, bio
)) {
690 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
691 if (bio_attempt_front_merge(q
, rq
, bio
)) {
703 * Process software queues that have been marked busy, splicing them
704 * to the for-dispatch
706 static void flush_busy_ctxs(struct blk_mq_hw_ctx
*hctx
, struct list_head
*list
)
708 struct blk_mq_ctx
*ctx
;
711 for (i
= 0; i
< hctx
->ctx_map
.map_size
; i
++) {
712 struct blk_align_bitmap
*bm
= &hctx
->ctx_map
.map
[i
];
713 unsigned int off
, bit
;
719 off
= i
* hctx
->ctx_map
.bits_per_word
;
721 bit
= find_next_bit(&bm
->word
, bm
->depth
, bit
);
722 if (bit
>= bm
->depth
)
725 ctx
= hctx
->ctxs
[bit
+ off
];
726 clear_bit(bit
, &bm
->word
);
727 spin_lock(&ctx
->lock
);
728 list_splice_tail_init(&ctx
->rq_list
, list
);
729 spin_unlock(&ctx
->lock
);
737 * Run this hardware queue, pulling any software queues mapped to it in.
738 * Note that this function currently has various problems around ordering
739 * of IO. In particular, we'd like FIFO behaviour on handling existing
740 * items on the hctx->dispatch list. Ignore that for now.
742 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
744 struct request_queue
*q
= hctx
->queue
;
749 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx
->cpumask
));
751 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
757 * Touch any software queue that has pending entries.
759 flush_busy_ctxs(hctx
, &rq_list
);
762 * If we have previous entries on our dispatch list, grab them
763 * and stuff them at the front for more fair dispatch.
765 if (!list_empty_careful(&hctx
->dispatch
)) {
766 spin_lock(&hctx
->lock
);
767 if (!list_empty(&hctx
->dispatch
))
768 list_splice_init(&hctx
->dispatch
, &rq_list
);
769 spin_unlock(&hctx
->lock
);
773 * Now process all the entries, sending them to the driver.
776 while (!list_empty(&rq_list
)) {
779 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
780 list_del_init(&rq
->queuelist
);
782 blk_mq_start_request(rq
, list_empty(&rq_list
));
784 ret
= q
->mq_ops
->queue_rq(hctx
, rq
);
786 case BLK_MQ_RQ_QUEUE_OK
:
789 case BLK_MQ_RQ_QUEUE_BUSY
:
790 list_add(&rq
->queuelist
, &rq_list
);
791 __blk_mq_requeue_request(rq
);
794 pr_err("blk-mq: bad return on queue: %d\n", ret
);
795 case BLK_MQ_RQ_QUEUE_ERROR
:
797 blk_mq_end_io(rq
, rq
->errors
);
801 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
806 hctx
->dispatched
[0]++;
807 else if (queued
< (1 << (BLK_MQ_MAX_DISPATCH_ORDER
- 1)))
808 hctx
->dispatched
[ilog2(queued
) + 1]++;
811 * Any items that need requeuing? Stuff them into hctx->dispatch,
812 * that is where we will continue on next queue run.
814 if (!list_empty(&rq_list
)) {
815 spin_lock(&hctx
->lock
);
816 list_splice(&rq_list
, &hctx
->dispatch
);
817 spin_unlock(&hctx
->lock
);
822 * It'd be great if the workqueue API had a way to pass
823 * in a mask and had some smarts for more clever placement.
824 * For now we just round-robin here, switching for every
825 * BLK_MQ_CPU_WORK_BATCH queued items.
827 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx
*hctx
)
829 int cpu
= hctx
->next_cpu
;
831 if (--hctx
->next_cpu_batch
<= 0) {
834 next_cpu
= cpumask_next(hctx
->next_cpu
, hctx
->cpumask
);
835 if (next_cpu
>= nr_cpu_ids
)
836 next_cpu
= cpumask_first(hctx
->cpumask
);
838 hctx
->next_cpu
= next_cpu
;
839 hctx
->next_cpu_batch
= BLK_MQ_CPU_WORK_BATCH
;
845 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
847 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
850 if (!async
&& cpumask_test_cpu(smp_processor_id(), hctx
->cpumask
))
851 __blk_mq_run_hw_queue(hctx
);
852 else if (hctx
->queue
->nr_hw_queues
== 1)
853 kblockd_schedule_delayed_work(&hctx
->run_work
, 0);
857 cpu
= blk_mq_hctx_next_cpu(hctx
);
858 kblockd_schedule_delayed_work_on(cpu
, &hctx
->run_work
, 0);
862 void blk_mq_run_queues(struct request_queue
*q
, bool async
)
864 struct blk_mq_hw_ctx
*hctx
;
867 queue_for_each_hw_ctx(q
, hctx
, i
) {
868 if ((!blk_mq_hctx_has_pending(hctx
) &&
869 list_empty_careful(&hctx
->dispatch
)) ||
870 test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
874 blk_mq_run_hw_queue(hctx
, async
);
878 EXPORT_SYMBOL(blk_mq_run_queues
);
880 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
882 cancel_delayed_work(&hctx
->run_work
);
883 cancel_delayed_work(&hctx
->delay_work
);
884 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
886 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
888 void blk_mq_stop_hw_queues(struct request_queue
*q
)
890 struct blk_mq_hw_ctx
*hctx
;
893 queue_for_each_hw_ctx(q
, hctx
, i
)
894 blk_mq_stop_hw_queue(hctx
);
896 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
898 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
900 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
903 __blk_mq_run_hw_queue(hctx
);
906 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
908 void blk_mq_start_hw_queues(struct request_queue
*q
)
910 struct blk_mq_hw_ctx
*hctx
;
913 queue_for_each_hw_ctx(q
, hctx
, i
)
914 blk_mq_start_hw_queue(hctx
);
916 EXPORT_SYMBOL(blk_mq_start_hw_queues
);
919 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
, bool async
)
921 struct blk_mq_hw_ctx
*hctx
;
924 queue_for_each_hw_ctx(q
, hctx
, i
) {
925 if (!test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
928 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
930 blk_mq_run_hw_queue(hctx
, async
);
934 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
936 static void blk_mq_run_work_fn(struct work_struct
*work
)
938 struct blk_mq_hw_ctx
*hctx
;
940 hctx
= container_of(work
, struct blk_mq_hw_ctx
, run_work
.work
);
942 __blk_mq_run_hw_queue(hctx
);
945 static void blk_mq_delay_work_fn(struct work_struct
*work
)
947 struct blk_mq_hw_ctx
*hctx
;
949 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delay_work
.work
);
951 if (test_and_clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
952 __blk_mq_run_hw_queue(hctx
);
955 void blk_mq_delay_queue(struct blk_mq_hw_ctx
*hctx
, unsigned long msecs
)
957 unsigned long tmo
= msecs_to_jiffies(msecs
);
959 if (hctx
->queue
->nr_hw_queues
== 1)
960 kblockd_schedule_delayed_work(&hctx
->delay_work
, tmo
);
964 cpu
= blk_mq_hctx_next_cpu(hctx
);
965 kblockd_schedule_delayed_work_on(cpu
, &hctx
->delay_work
, tmo
);
968 EXPORT_SYMBOL(blk_mq_delay_queue
);
970 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
971 struct request
*rq
, bool at_head
)
973 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
975 trace_block_rq_insert(hctx
->queue
, rq
);
978 list_add(&rq
->queuelist
, &ctx
->rq_list
);
980 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
982 blk_mq_hctx_mark_pending(hctx
, ctx
);
985 * We do this early, to ensure we are on the right CPU.
990 void blk_mq_insert_request(struct request
*rq
, bool at_head
, bool run_queue
,
993 struct request_queue
*q
= rq
->q
;
994 struct blk_mq_hw_ctx
*hctx
;
995 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
, *current_ctx
;
997 current_ctx
= blk_mq_get_ctx(q
);
998 if (!cpu_online(ctx
->cpu
))
999 rq
->mq_ctx
= ctx
= current_ctx
;
1001 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1003 if (rq
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
) &&
1004 !(rq
->cmd_flags
& (REQ_FLUSH_SEQ
))) {
1005 blk_insert_flush(rq
);
1007 spin_lock(&ctx
->lock
);
1008 __blk_mq_insert_request(hctx
, rq
, at_head
);
1009 spin_unlock(&ctx
->lock
);
1013 blk_mq_run_hw_queue(hctx
, async
);
1015 blk_mq_put_ctx(current_ctx
);
1018 static void blk_mq_insert_requests(struct request_queue
*q
,
1019 struct blk_mq_ctx
*ctx
,
1020 struct list_head
*list
,
1025 struct blk_mq_hw_ctx
*hctx
;
1026 struct blk_mq_ctx
*current_ctx
;
1028 trace_block_unplug(q
, depth
, !from_schedule
);
1030 current_ctx
= blk_mq_get_ctx(q
);
1032 if (!cpu_online(ctx
->cpu
))
1034 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1037 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1040 spin_lock(&ctx
->lock
);
1041 while (!list_empty(list
)) {
1044 rq
= list_first_entry(list
, struct request
, queuelist
);
1045 list_del_init(&rq
->queuelist
);
1047 __blk_mq_insert_request(hctx
, rq
, false);
1049 spin_unlock(&ctx
->lock
);
1051 blk_mq_run_hw_queue(hctx
, from_schedule
);
1052 blk_mq_put_ctx(current_ctx
);
1055 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1057 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
1058 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
1060 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
1061 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
1062 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
1065 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
1067 struct blk_mq_ctx
*this_ctx
;
1068 struct request_queue
*this_q
;
1071 LIST_HEAD(ctx_list
);
1074 list_splice_init(&plug
->mq_list
, &list
);
1076 list_sort(NULL
, &list
, plug_ctx_cmp
);
1082 while (!list_empty(&list
)) {
1083 rq
= list_entry_rq(list
.next
);
1084 list_del_init(&rq
->queuelist
);
1086 if (rq
->mq_ctx
!= this_ctx
) {
1088 blk_mq_insert_requests(this_q
, this_ctx
,
1093 this_ctx
= rq
->mq_ctx
;
1099 list_add_tail(&rq
->queuelist
, &ctx_list
);
1103 * If 'this_ctx' is set, we know we have entries to complete
1104 * on 'ctx_list'. Do those.
1107 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
1112 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
1114 init_request_from_bio(rq
, bio
);
1116 if (blk_do_io_stat(rq
)) {
1117 rq
->start_time
= jiffies
;
1118 blk_account_io_start(rq
, 1);
1122 static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx
*hctx
,
1123 struct blk_mq_ctx
*ctx
,
1124 struct request
*rq
, struct bio
*bio
)
1126 struct request_queue
*q
= hctx
->queue
;
1128 if (!(hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
)) {
1129 blk_mq_bio_to_request(rq
, bio
);
1130 spin_lock(&ctx
->lock
);
1132 __blk_mq_insert_request(hctx
, rq
, false);
1133 spin_unlock(&ctx
->lock
);
1136 spin_lock(&ctx
->lock
);
1137 if (!blk_mq_attempt_merge(q
, ctx
, bio
)) {
1138 blk_mq_bio_to_request(rq
, bio
);
1142 spin_unlock(&ctx
->lock
);
1143 __blk_mq_free_request(hctx
, ctx
, rq
);
1148 struct blk_map_ctx
{
1149 struct blk_mq_hw_ctx
*hctx
;
1150 struct blk_mq_ctx
*ctx
;
1153 static struct request
*blk_mq_map_request(struct request_queue
*q
,
1155 struct blk_map_ctx
*data
)
1157 struct blk_mq_hw_ctx
*hctx
;
1158 struct blk_mq_ctx
*ctx
;
1160 int rw
= bio_data_dir(bio
);
1162 if (unlikely(blk_mq_queue_enter(q
))) {
1163 bio_endio(bio
, -EIO
);
1167 ctx
= blk_mq_get_ctx(q
);
1168 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1170 if (rw_is_sync(bio
->bi_rw
))
1173 trace_block_getrq(q
, bio
, rw
);
1174 rq
= __blk_mq_alloc_request(q
, hctx
, ctx
, rw
, GFP_ATOMIC
, false);
1175 if (unlikely(!rq
)) {
1176 __blk_mq_run_hw_queue(hctx
);
1177 blk_mq_put_ctx(ctx
);
1178 trace_block_sleeprq(q
, bio
, rw
);
1180 ctx
= blk_mq_get_ctx(q
);
1181 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1182 rq
= __blk_mq_alloc_request(q
, hctx
, ctx
, rw
,
1183 __GFP_WAIT
|GFP_ATOMIC
, false);
1193 * Multiple hardware queue variant. This will not use per-process plugs,
1194 * but will attempt to bypass the hctx queueing if we can go straight to
1195 * hardware for SYNC IO.
1197 static void blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
1199 const int is_sync
= rw_is_sync(bio
->bi_rw
);
1200 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
1201 struct blk_map_ctx data
;
1204 blk_queue_bounce(q
, &bio
);
1206 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1207 bio_endio(bio
, -EIO
);
1211 rq
= blk_mq_map_request(q
, bio
, &data
);
1215 if (unlikely(is_flush_fua
)) {
1216 blk_mq_bio_to_request(rq
, bio
);
1217 blk_insert_flush(rq
);
1224 blk_mq_bio_to_request(rq
, bio
);
1225 blk_mq_start_request(rq
, true);
1229 * For OK queue, we are done. For error, kill it. Any other
1230 * error (busy), just add it to our list as we previously
1233 ret
= q
->mq_ops
->queue_rq(data
.hctx
, rq
);
1234 if (ret
== BLK_MQ_RQ_QUEUE_OK
)
1237 __blk_mq_requeue_request(rq
);
1239 if (ret
== BLK_MQ_RQ_QUEUE_ERROR
) {
1241 blk_mq_end_io(rq
, rq
->errors
);
1247 if (!blk_mq_merge_queue_io(data
.hctx
, data
.ctx
, rq
, bio
)) {
1249 * For a SYNC request, send it to the hardware immediately. For
1250 * an ASYNC request, just ensure that we run it later on. The
1251 * latter allows for merging opportunities and more efficient
1255 blk_mq_run_hw_queue(data
.hctx
, !is_sync
|| is_flush_fua
);
1258 blk_mq_put_ctx(data
.ctx
);
1262 * Single hardware queue variant. This will attempt to use any per-process
1263 * plug for merging and IO deferral.
1265 static void blk_sq_make_request(struct request_queue
*q
, struct bio
*bio
)
1267 const int is_sync
= rw_is_sync(bio
->bi_rw
);
1268 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
1269 unsigned int use_plug
, request_count
= 0;
1270 struct blk_map_ctx data
;
1274 * If we have multiple hardware queues, just go directly to
1275 * one of those for sync IO.
1277 use_plug
= !is_flush_fua
&& !is_sync
;
1279 blk_queue_bounce(q
, &bio
);
1281 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1282 bio_endio(bio
, -EIO
);
1286 if (use_plug
&& !blk_queue_nomerges(q
) &&
1287 blk_attempt_plug_merge(q
, bio
, &request_count
))
1290 rq
= blk_mq_map_request(q
, bio
, &data
);
1292 if (unlikely(is_flush_fua
)) {
1293 blk_mq_bio_to_request(rq
, bio
);
1294 blk_insert_flush(rq
);
1299 * A task plug currently exists. Since this is completely lockless,
1300 * utilize that to temporarily store requests until the task is
1301 * either done or scheduled away.
1304 struct blk_plug
*plug
= current
->plug
;
1307 blk_mq_bio_to_request(rq
, bio
);
1308 if (list_empty(&plug
->mq_list
))
1309 trace_block_plug(q
);
1310 else if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
1311 blk_flush_plug_list(plug
, false);
1312 trace_block_plug(q
);
1314 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
1315 blk_mq_put_ctx(data
.ctx
);
1320 if (!blk_mq_merge_queue_io(data
.hctx
, data
.ctx
, rq
, bio
)) {
1322 * For a SYNC request, send it to the hardware immediately. For
1323 * an ASYNC request, just ensure that we run it later on. The
1324 * latter allows for merging opportunities and more efficient
1328 blk_mq_run_hw_queue(data
.hctx
, !is_sync
|| is_flush_fua
);
1331 blk_mq_put_ctx(data
.ctx
);
1335 * Default mapping to a software queue, since we use one per CPU.
1337 struct blk_mq_hw_ctx
*blk_mq_map_queue(struct request_queue
*q
, const int cpu
)
1339 return q
->queue_hw_ctx
[q
->mq_map
[cpu
]];
1341 EXPORT_SYMBOL(blk_mq_map_queue
);
1343 static void blk_mq_free_rq_map(struct blk_mq_tag_set
*set
,
1344 struct blk_mq_tags
*tags
, unsigned int hctx_idx
)
1348 if (tags
->rqs
&& set
->ops
->exit_request
) {
1351 for (i
= 0; i
< tags
->nr_tags
; i
++) {
1354 set
->ops
->exit_request(set
->driver_data
, tags
->rqs
[i
],
1359 while (!list_empty(&tags
->page_list
)) {
1360 page
= list_first_entry(&tags
->page_list
, struct page
, lru
);
1361 list_del_init(&page
->lru
);
1362 __free_pages(page
, page
->private);
1367 blk_mq_free_tags(tags
);
1370 static size_t order_to_size(unsigned int order
)
1372 return (size_t)PAGE_SIZE
<< order
;
1375 static struct blk_mq_tags
*blk_mq_init_rq_map(struct blk_mq_tag_set
*set
,
1376 unsigned int hctx_idx
)
1378 struct blk_mq_tags
*tags
;
1379 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1380 size_t rq_size
, left
;
1382 tags
= blk_mq_init_tags(set
->queue_depth
, set
->reserved_tags
,
1387 INIT_LIST_HEAD(&tags
->page_list
);
1389 tags
->rqs
= kmalloc_node(set
->queue_depth
* sizeof(struct request
*),
1390 GFP_KERNEL
, set
->numa_node
);
1392 blk_mq_free_tags(tags
);
1397 * rq_size is the size of the request plus driver payload, rounded
1398 * to the cacheline size
1400 rq_size
= round_up(sizeof(struct request
) + set
->cmd_size
,
1402 left
= rq_size
* set
->queue_depth
;
1404 for (i
= 0; i
< set
->queue_depth
; ) {
1405 int this_order
= max_order
;
1410 while (left
< order_to_size(this_order
- 1) && this_order
)
1414 page
= alloc_pages_node(set
->numa_node
, GFP_KERNEL
,
1420 if (order_to_size(this_order
) < rq_size
)
1427 page
->private = this_order
;
1428 list_add_tail(&page
->lru
, &tags
->page_list
);
1430 p
= page_address(page
);
1431 entries_per_page
= order_to_size(this_order
) / rq_size
;
1432 to_do
= min(entries_per_page
, set
->queue_depth
- i
);
1433 left
-= to_do
* rq_size
;
1434 for (j
= 0; j
< to_do
; j
++) {
1436 if (set
->ops
->init_request
) {
1437 if (set
->ops
->init_request(set
->driver_data
,
1438 tags
->rqs
[i
], hctx_idx
, i
,
1451 pr_warn("%s: failed to allocate requests\n", __func__
);
1452 blk_mq_free_rq_map(set
, tags
, hctx_idx
);
1456 static void blk_mq_free_bitmap(struct blk_mq_ctxmap
*bitmap
)
1461 static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap
*bitmap
, int node
)
1463 unsigned int bpw
= 8, total
, num_maps
, i
;
1465 bitmap
->bits_per_word
= bpw
;
1467 num_maps
= ALIGN(nr_cpu_ids
, bpw
) / bpw
;
1468 bitmap
->map
= kzalloc_node(num_maps
* sizeof(struct blk_align_bitmap
),
1473 bitmap
->map_size
= num_maps
;
1476 for (i
= 0; i
< num_maps
; i
++) {
1477 bitmap
->map
[i
].depth
= min(total
, bitmap
->bits_per_word
);
1478 total
-= bitmap
->map
[i
].depth
;
1484 static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx
*hctx
, int cpu
)
1486 struct request_queue
*q
= hctx
->queue
;
1487 struct blk_mq_ctx
*ctx
;
1491 * Move ctx entries to new CPU, if this one is going away.
1493 ctx
= __blk_mq_get_ctx(q
, cpu
);
1495 spin_lock(&ctx
->lock
);
1496 if (!list_empty(&ctx
->rq_list
)) {
1497 list_splice_init(&ctx
->rq_list
, &tmp
);
1498 blk_mq_hctx_clear_pending(hctx
, ctx
);
1500 spin_unlock(&ctx
->lock
);
1502 if (list_empty(&tmp
))
1505 ctx
= blk_mq_get_ctx(q
);
1506 spin_lock(&ctx
->lock
);
1508 while (!list_empty(&tmp
)) {
1511 rq
= list_first_entry(&tmp
, struct request
, queuelist
);
1513 list_move_tail(&rq
->queuelist
, &ctx
->rq_list
);
1516 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1517 blk_mq_hctx_mark_pending(hctx
, ctx
);
1519 spin_unlock(&ctx
->lock
);
1521 blk_mq_run_hw_queue(hctx
, true);
1522 blk_mq_put_ctx(ctx
);
1526 static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx
*hctx
, int cpu
)
1528 struct request_queue
*q
= hctx
->queue
;
1529 struct blk_mq_tag_set
*set
= q
->tag_set
;
1531 if (set
->tags
[hctx
->queue_num
])
1534 set
->tags
[hctx
->queue_num
] = blk_mq_init_rq_map(set
, hctx
->queue_num
);
1535 if (!set
->tags
[hctx
->queue_num
])
1538 hctx
->tags
= set
->tags
[hctx
->queue_num
];
1542 static int blk_mq_hctx_notify(void *data
, unsigned long action
,
1545 struct blk_mq_hw_ctx
*hctx
= data
;
1547 if (action
== CPU_DEAD
|| action
== CPU_DEAD_FROZEN
)
1548 return blk_mq_hctx_cpu_offline(hctx
, cpu
);
1549 else if (action
== CPU_ONLINE
|| action
== CPU_ONLINE_FROZEN
)
1550 return blk_mq_hctx_cpu_online(hctx
, cpu
);
1555 static void blk_mq_exit_hw_queues(struct request_queue
*q
,
1556 struct blk_mq_tag_set
*set
, int nr_queue
)
1558 struct blk_mq_hw_ctx
*hctx
;
1561 queue_for_each_hw_ctx(q
, hctx
, i
) {
1565 if (set
->ops
->exit_hctx
)
1566 set
->ops
->exit_hctx(hctx
, i
);
1568 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1570 blk_mq_free_bitmap(&hctx
->ctx_map
);
1575 static void blk_mq_free_hw_queues(struct request_queue
*q
,
1576 struct blk_mq_tag_set
*set
)
1578 struct blk_mq_hw_ctx
*hctx
;
1581 queue_for_each_hw_ctx(q
, hctx
, i
) {
1582 free_cpumask_var(hctx
->cpumask
);
1587 static int blk_mq_init_hw_queues(struct request_queue
*q
,
1588 struct blk_mq_tag_set
*set
)
1590 struct blk_mq_hw_ctx
*hctx
;
1594 * Initialize hardware queues
1596 queue_for_each_hw_ctx(q
, hctx
, i
) {
1599 node
= hctx
->numa_node
;
1600 if (node
== NUMA_NO_NODE
)
1601 node
= hctx
->numa_node
= set
->numa_node
;
1603 INIT_DELAYED_WORK(&hctx
->run_work
, blk_mq_run_work_fn
);
1604 INIT_DELAYED_WORK(&hctx
->delay_work
, blk_mq_delay_work_fn
);
1605 spin_lock_init(&hctx
->lock
);
1606 INIT_LIST_HEAD(&hctx
->dispatch
);
1608 hctx
->queue_num
= i
;
1609 hctx
->flags
= set
->flags
;
1610 hctx
->cmd_size
= set
->cmd_size
;
1612 blk_mq_init_cpu_notifier(&hctx
->cpu_notifier
,
1613 blk_mq_hctx_notify
, hctx
);
1614 blk_mq_register_cpu_notifier(&hctx
->cpu_notifier
);
1616 hctx
->tags
= set
->tags
[i
];
1619 * Allocate space for all possible cpus to avoid allocation in
1622 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1627 if (blk_mq_alloc_bitmap(&hctx
->ctx_map
, node
))
1632 if (set
->ops
->init_hctx
&&
1633 set
->ops
->init_hctx(hctx
, set
->driver_data
, i
))
1637 if (i
== q
->nr_hw_queues
)
1643 blk_mq_exit_hw_queues(q
, set
, i
);
1648 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1649 unsigned int nr_hw_queues
)
1653 for_each_possible_cpu(i
) {
1654 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1655 struct blk_mq_hw_ctx
*hctx
;
1657 memset(__ctx
, 0, sizeof(*__ctx
));
1659 spin_lock_init(&__ctx
->lock
);
1660 INIT_LIST_HEAD(&__ctx
->rq_list
);
1663 /* If the cpu isn't online, the cpu is mapped to first hctx */
1667 hctx
= q
->mq_ops
->map_queue(q
, i
);
1668 cpumask_set_cpu(i
, hctx
->cpumask
);
1672 * Set local node, IFF we have more than one hw queue. If
1673 * not, we remain on the home node of the device
1675 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1676 hctx
->numa_node
= cpu_to_node(i
);
1680 static void blk_mq_map_swqueue(struct request_queue
*q
)
1683 struct blk_mq_hw_ctx
*hctx
;
1684 struct blk_mq_ctx
*ctx
;
1686 queue_for_each_hw_ctx(q
, hctx
, i
) {
1687 cpumask_clear(hctx
->cpumask
);
1692 * Map software to hardware queues
1694 queue_for_each_ctx(q
, ctx
, i
) {
1695 /* If the cpu isn't online, the cpu is mapped to first hctx */
1699 hctx
= q
->mq_ops
->map_queue(q
, i
);
1700 cpumask_set_cpu(i
, hctx
->cpumask
);
1701 ctx
->index_hw
= hctx
->nr_ctx
;
1702 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1705 queue_for_each_hw_ctx(q
, hctx
, i
) {
1707 * If not software queues are mapped to this hardware queue,
1708 * disable it and free the request entries
1710 if (!hctx
->nr_ctx
) {
1711 struct blk_mq_tag_set
*set
= q
->tag_set
;
1714 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
1715 set
->tags
[i
] = NULL
;
1722 * Initialize batch roundrobin counts
1724 hctx
->next_cpu
= cpumask_first(hctx
->cpumask
);
1725 hctx
->next_cpu_batch
= BLK_MQ_CPU_WORK_BATCH
;
1729 static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set
*set
)
1731 struct blk_mq_hw_ctx
*hctx
;
1732 struct request_queue
*q
;
1736 if (set
->tag_list
.next
== set
->tag_list
.prev
)
1741 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
) {
1742 blk_mq_freeze_queue(q
);
1744 queue_for_each_hw_ctx(q
, hctx
, i
) {
1746 hctx
->flags
|= BLK_MQ_F_TAG_SHARED
;
1748 hctx
->flags
&= ~BLK_MQ_F_TAG_SHARED
;
1750 blk_mq_unfreeze_queue(q
);
1754 static void blk_mq_del_queue_tag_set(struct request_queue
*q
)
1756 struct blk_mq_tag_set
*set
= q
->tag_set
;
1758 blk_mq_freeze_queue(q
);
1760 mutex_lock(&set
->tag_list_lock
);
1761 list_del_init(&q
->tag_set_list
);
1762 blk_mq_update_tag_set_depth(set
);
1763 mutex_unlock(&set
->tag_list_lock
);
1765 blk_mq_unfreeze_queue(q
);
1768 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set
*set
,
1769 struct request_queue
*q
)
1773 mutex_lock(&set
->tag_list_lock
);
1774 list_add_tail(&q
->tag_set_list
, &set
->tag_list
);
1775 blk_mq_update_tag_set_depth(set
);
1776 mutex_unlock(&set
->tag_list_lock
);
1779 struct request_queue
*blk_mq_init_queue(struct blk_mq_tag_set
*set
)
1781 struct blk_mq_hw_ctx
**hctxs
;
1782 struct blk_mq_ctx
*ctx
;
1783 struct request_queue
*q
;
1787 ctx
= alloc_percpu(struct blk_mq_ctx
);
1789 return ERR_PTR(-ENOMEM
);
1791 hctxs
= kmalloc_node(set
->nr_hw_queues
* sizeof(*hctxs
), GFP_KERNEL
,
1797 map
= blk_mq_make_queue_map(set
);
1801 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1802 int node
= blk_mq_hw_queue_to_node(map
, i
);
1804 hctxs
[i
] = kzalloc_node(sizeof(struct blk_mq_hw_ctx
),
1809 if (!zalloc_cpumask_var(&hctxs
[i
]->cpumask
, GFP_KERNEL
))
1812 atomic_set(&hctxs
[i
]->nr_active
, 0);
1813 hctxs
[i
]->numa_node
= node
;
1814 hctxs
[i
]->queue_num
= i
;
1817 q
= blk_alloc_queue_node(GFP_KERNEL
, set
->numa_node
);
1821 if (percpu_counter_init(&q
->mq_usage_counter
, 0))
1824 setup_timer(&q
->timeout
, blk_mq_rq_timer
, (unsigned long) q
);
1825 blk_queue_rq_timeout(q
, 30000);
1827 q
->nr_queues
= nr_cpu_ids
;
1828 q
->nr_hw_queues
= set
->nr_hw_queues
;
1832 q
->queue_hw_ctx
= hctxs
;
1834 q
->mq_ops
= set
->ops
;
1835 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
1837 if (!(set
->flags
& BLK_MQ_F_SG_MERGE
))
1838 q
->queue_flags
|= 1 << QUEUE_FLAG_NO_SG_MERGE
;
1840 q
->sg_reserved_size
= INT_MAX
;
1842 INIT_WORK(&q
->requeue_work
, blk_mq_requeue_work
);
1843 INIT_LIST_HEAD(&q
->requeue_list
);
1844 spin_lock_init(&q
->requeue_lock
);
1846 if (q
->nr_hw_queues
> 1)
1847 blk_queue_make_request(q
, blk_mq_make_request
);
1849 blk_queue_make_request(q
, blk_sq_make_request
);
1851 blk_queue_rq_timed_out(q
, blk_mq_rq_timed_out
);
1853 blk_queue_rq_timeout(q
, set
->timeout
);
1856 * Do this after blk_queue_make_request() overrides it...
1858 q
->nr_requests
= set
->queue_depth
;
1860 if (set
->ops
->complete
)
1861 blk_queue_softirq_done(q
, set
->ops
->complete
);
1863 blk_mq_init_flush(q
);
1864 blk_mq_init_cpu_queues(q
, set
->nr_hw_queues
);
1866 q
->flush_rq
= kzalloc(round_up(sizeof(struct request
) +
1867 set
->cmd_size
, cache_line_size()),
1872 if (blk_mq_init_hw_queues(q
, set
))
1875 mutex_lock(&all_q_mutex
);
1876 list_add_tail(&q
->all_q_node
, &all_q_list
);
1877 mutex_unlock(&all_q_mutex
);
1879 blk_mq_add_queue_tag_set(set
, q
);
1881 blk_mq_map_swqueue(q
);
1888 blk_cleanup_queue(q
);
1891 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1894 free_cpumask_var(hctxs
[i
]->cpumask
);
1901 return ERR_PTR(-ENOMEM
);
1903 EXPORT_SYMBOL(blk_mq_init_queue
);
1905 void blk_mq_free_queue(struct request_queue
*q
)
1907 struct blk_mq_tag_set
*set
= q
->tag_set
;
1909 blk_mq_del_queue_tag_set(q
);
1911 blk_mq_exit_hw_queues(q
, set
, set
->nr_hw_queues
);
1912 blk_mq_free_hw_queues(q
, set
);
1914 percpu_counter_destroy(&q
->mq_usage_counter
);
1916 free_percpu(q
->queue_ctx
);
1917 kfree(q
->queue_hw_ctx
);
1920 q
->queue_ctx
= NULL
;
1921 q
->queue_hw_ctx
= NULL
;
1924 mutex_lock(&all_q_mutex
);
1925 list_del_init(&q
->all_q_node
);
1926 mutex_unlock(&all_q_mutex
);
1929 /* Basically redo blk_mq_init_queue with queue frozen */
1930 static void blk_mq_queue_reinit(struct request_queue
*q
)
1932 blk_mq_freeze_queue(q
);
1934 blk_mq_sysfs_unregister(q
);
1936 blk_mq_update_queue_map(q
->mq_map
, q
->nr_hw_queues
);
1939 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1940 * we should change hctx numa_node according to new topology (this
1941 * involves free and re-allocate memory, worthy doing?)
1944 blk_mq_map_swqueue(q
);
1946 blk_mq_sysfs_register(q
);
1948 blk_mq_unfreeze_queue(q
);
1951 static int blk_mq_queue_reinit_notify(struct notifier_block
*nb
,
1952 unsigned long action
, void *hcpu
)
1954 struct request_queue
*q
;
1957 * Before new mappings are established, hotadded cpu might already
1958 * start handling requests. This doesn't break anything as we map
1959 * offline CPUs to first hardware queue. We will re-init the queue
1960 * below to get optimal settings.
1962 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
&&
1963 action
!= CPU_ONLINE
&& action
!= CPU_ONLINE_FROZEN
)
1966 mutex_lock(&all_q_mutex
);
1967 list_for_each_entry(q
, &all_q_list
, all_q_node
)
1968 blk_mq_queue_reinit(q
);
1969 mutex_unlock(&all_q_mutex
);
1973 int blk_mq_alloc_tag_set(struct blk_mq_tag_set
*set
)
1977 if (!set
->nr_hw_queues
)
1979 if (!set
->queue_depth
|| set
->queue_depth
> BLK_MQ_MAX_DEPTH
)
1981 if (set
->queue_depth
< set
->reserved_tags
+ BLK_MQ_TAG_MIN
)
1984 if (!set
->nr_hw_queues
|| !set
->ops
->queue_rq
|| !set
->ops
->map_queue
)
1988 set
->tags
= kmalloc_node(set
->nr_hw_queues
*
1989 sizeof(struct blk_mq_tags
*),
1990 GFP_KERNEL
, set
->numa_node
);
1994 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1995 set
->tags
[i
] = blk_mq_init_rq_map(set
, i
);
2000 mutex_init(&set
->tag_list_lock
);
2001 INIT_LIST_HEAD(&set
->tag_list
);
2007 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
2011 EXPORT_SYMBOL(blk_mq_alloc_tag_set
);
2013 void blk_mq_free_tag_set(struct blk_mq_tag_set
*set
)
2017 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
2019 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
2024 EXPORT_SYMBOL(blk_mq_free_tag_set
);
2026 int blk_mq_update_nr_requests(struct request_queue
*q
, unsigned int nr
)
2028 struct blk_mq_tag_set
*set
= q
->tag_set
;
2029 struct blk_mq_hw_ctx
*hctx
;
2032 if (!set
|| nr
> set
->queue_depth
)
2036 queue_for_each_hw_ctx(q
, hctx
, i
) {
2037 ret
= blk_mq_tag_update_depth(hctx
->tags
, nr
);
2043 q
->nr_requests
= nr
;
2048 void blk_mq_disable_hotplug(void)
2050 mutex_lock(&all_q_mutex
);
2053 void blk_mq_enable_hotplug(void)
2055 mutex_unlock(&all_q_mutex
);
2058 static int __init
blk_mq_init(void)
2062 /* Must be called after percpu_counter_hotcpu_callback() */
2063 hotcpu_notifier(blk_mq_queue_reinit_notify
, -10);
2067 subsys_initcall(blk_mq_init
);