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
->ctx_map
.map_size
; i
++)
60 if (hctx
->ctx_map
.map
[i
].word
)
66 static inline struct blk_align_bitmap
*get_bm(struct blk_mq_hw_ctx
*hctx
,
67 struct blk_mq_ctx
*ctx
)
69 return &hctx
->ctx_map
.map
[ctx
->index_hw
/ hctx
->ctx_map
.bits_per_word
];
72 #define CTX_TO_BIT(hctx, ctx) \
73 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
76 * Mark this ctx as having pending work in this hardware queue
78 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx
*hctx
,
79 struct blk_mq_ctx
*ctx
)
81 struct blk_align_bitmap
*bm
= get_bm(hctx
, ctx
);
83 if (!test_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
))
84 set_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
);
87 static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx
*hctx
,
88 struct blk_mq_ctx
*ctx
)
90 struct blk_align_bitmap
*bm
= get_bm(hctx
, ctx
);
92 clear_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
);
95 static int blk_mq_queue_enter(struct request_queue
*q
)
99 __percpu_counter_add(&q
->mq_usage_counter
, 1, 1000000);
101 /* we have problems to freeze the queue if it's initializing */
102 if (!blk_queue_bypass(q
) || !blk_queue_init_done(q
))
105 __percpu_counter_add(&q
->mq_usage_counter
, -1, 1000000);
107 spin_lock_irq(q
->queue_lock
);
108 ret
= wait_event_interruptible_lock_irq(q
->mq_freeze_wq
,
109 !blk_queue_bypass(q
) || blk_queue_dying(q
),
111 /* inc usage with lock hold to avoid freeze_queue runs here */
112 if (!ret
&& !blk_queue_dying(q
))
113 __percpu_counter_add(&q
->mq_usage_counter
, 1, 1000000);
114 else if (blk_queue_dying(q
))
116 spin_unlock_irq(q
->queue_lock
);
121 static void blk_mq_queue_exit(struct request_queue
*q
)
123 __percpu_counter_add(&q
->mq_usage_counter
, -1, 1000000);
126 static void __blk_mq_drain_queue(struct request_queue
*q
)
131 spin_lock_irq(q
->queue_lock
);
132 count
= percpu_counter_sum(&q
->mq_usage_counter
);
133 spin_unlock_irq(q
->queue_lock
);
137 blk_mq_run_queues(q
, false);
143 * Guarantee no request is in use, so we can change any data structure of
144 * the queue afterward.
146 static void blk_mq_freeze_queue(struct request_queue
*q
)
150 spin_lock_irq(q
->queue_lock
);
151 drain
= !q
->bypass_depth
++;
152 queue_flag_set(QUEUE_FLAG_BYPASS
, q
);
153 spin_unlock_irq(q
->queue_lock
);
156 __blk_mq_drain_queue(q
);
159 void blk_mq_drain_queue(struct request_queue
*q
)
161 __blk_mq_drain_queue(q
);
164 static void blk_mq_unfreeze_queue(struct request_queue
*q
)
168 spin_lock_irq(q
->queue_lock
);
169 if (!--q
->bypass_depth
) {
170 queue_flag_clear(QUEUE_FLAG_BYPASS
, q
);
173 WARN_ON_ONCE(q
->bypass_depth
< 0);
174 spin_unlock_irq(q
->queue_lock
);
176 wake_up_all(&q
->mq_freeze_wq
);
179 bool blk_mq_can_queue(struct blk_mq_hw_ctx
*hctx
)
181 return blk_mq_has_free_tags(hctx
->tags
);
183 EXPORT_SYMBOL(blk_mq_can_queue
);
185 static void blk_mq_rq_ctx_init(struct request_queue
*q
, struct blk_mq_ctx
*ctx
,
186 struct request
*rq
, unsigned int rw_flags
)
188 if (blk_queue_io_stat(q
))
189 rw_flags
|= REQ_IO_STAT
;
191 INIT_LIST_HEAD(&rq
->queuelist
);
192 /* csd/requeue_work/fifo_time is initialized before use */
195 rq
->cmd_flags
|= rw_flags
;
197 /* do not touch atomic flags, it needs atomic ops against the timer */
200 rq
->__sector
= (sector_t
) -1;
203 INIT_HLIST_NODE(&rq
->hash
);
204 RB_CLEAR_NODE(&rq
->rb_node
);
205 memset(&rq
->flush
, 0, max(sizeof(rq
->flush
), sizeof(rq
->elv
)));
208 rq
->start_time
= jiffies
;
209 #ifdef CONFIG_BLK_CGROUP
211 set_start_time_ns(rq
);
212 rq
->io_start_time_ns
= 0;
214 rq
->nr_phys_segments
= 0;
215 #if defined(CONFIG_BLK_DEV_INTEGRITY)
216 rq
->nr_integrity_segments
= 0;
220 /* tag was already set */
222 memset(rq
->__cmd
, 0, sizeof(rq
->__cmd
));
224 rq
->cmd_len
= BLK_MAX_CDB
;
232 INIT_LIST_HEAD(&rq
->timeout_list
);
236 rq
->end_io_data
= NULL
;
239 ctx
->rq_dispatched
[rw_is_sync(rw_flags
)]++;
242 static struct request
*
243 __blk_mq_alloc_request(struct request_queue
*q
, struct blk_mq_hw_ctx
*hctx
,
244 struct blk_mq_ctx
*ctx
, int rw
, gfp_t gfp
, bool reserved
)
249 tag
= blk_mq_get_tag(hctx
, &ctx
->last_tag
, gfp
, reserved
);
250 if (tag
!= BLK_MQ_TAG_FAIL
) {
251 rq
= hctx
->tags
->rqs
[tag
];
254 if (blk_mq_tag_busy(hctx
)) {
255 rq
->cmd_flags
= REQ_MQ_INFLIGHT
;
256 atomic_inc(&hctx
->nr_active
);
260 blk_mq_rq_ctx_init(q
, ctx
, rq
, rw
);
267 static struct request
*blk_mq_alloc_request_pinned(struct request_queue
*q
,
271 bool gfp_mask
= gfp
& ~__GFP_WAIT
;
275 struct blk_mq_ctx
*ctx
= blk_mq_get_ctx(q
);
276 struct blk_mq_hw_ctx
*hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
278 rq
= __blk_mq_alloc_request(q
, hctx
, ctx
, rw
, gfp_mask
,
283 if (!(gfp
& __GFP_WAIT
)) {
288 __blk_mq_run_hw_queue(hctx
);
296 struct request
*blk_mq_alloc_request(struct request_queue
*q
, int rw
, gfp_t gfp
,
301 if (blk_mq_queue_enter(q
))
304 rq
= blk_mq_alloc_request_pinned(q
, rw
, gfp
, reserved
);
306 blk_mq_put_ctx(rq
->mq_ctx
);
309 EXPORT_SYMBOL(blk_mq_alloc_request
);
311 static void __blk_mq_free_request(struct blk_mq_hw_ctx
*hctx
,
312 struct blk_mq_ctx
*ctx
, struct request
*rq
)
314 const int tag
= rq
->tag
;
315 struct request_queue
*q
= rq
->q
;
317 if (rq
->cmd_flags
& REQ_MQ_INFLIGHT
)
318 atomic_dec(&hctx
->nr_active
);
320 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
321 blk_mq_put_tag(hctx
, tag
, &ctx
->last_tag
);
322 blk_mq_queue_exit(q
);
325 void blk_mq_free_request(struct request
*rq
)
327 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
328 struct blk_mq_hw_ctx
*hctx
;
329 struct request_queue
*q
= rq
->q
;
331 ctx
->rq_completed
[rq_is_sync(rq
)]++;
333 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
334 __blk_mq_free_request(hctx
, ctx
, rq
);
338 * Clone all relevant state from a request that has been put on hold in
339 * the flush state machine into the preallocated flush request that hangs
340 * off the request queue.
342 * For a driver the flush request should be invisible, that's why we are
343 * impersonating the original request here.
345 void blk_mq_clone_flush_request(struct request
*flush_rq
,
346 struct request
*orig_rq
)
348 struct blk_mq_hw_ctx
*hctx
=
349 orig_rq
->q
->mq_ops
->map_queue(orig_rq
->q
, orig_rq
->mq_ctx
->cpu
);
351 flush_rq
->mq_ctx
= orig_rq
->mq_ctx
;
352 flush_rq
->tag
= orig_rq
->tag
;
353 memcpy(blk_mq_rq_to_pdu(flush_rq
), blk_mq_rq_to_pdu(orig_rq
),
357 inline void __blk_mq_end_io(struct request
*rq
, int error
)
359 blk_account_io_done(rq
);
362 rq
->end_io(rq
, error
);
364 if (unlikely(blk_bidi_rq(rq
)))
365 blk_mq_free_request(rq
->next_rq
);
366 blk_mq_free_request(rq
);
369 EXPORT_SYMBOL(__blk_mq_end_io
);
371 void blk_mq_end_io(struct request
*rq
, int error
)
373 if (blk_update_request(rq
, error
, blk_rq_bytes(rq
)))
375 __blk_mq_end_io(rq
, error
);
377 EXPORT_SYMBOL(blk_mq_end_io
);
379 static void __blk_mq_complete_request_remote(void *data
)
381 struct request
*rq
= data
;
383 rq
->q
->softirq_done_fn(rq
);
386 void __blk_mq_complete_request(struct request
*rq
)
388 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
392 if (!test_bit(QUEUE_FLAG_SAME_COMP
, &rq
->q
->queue_flags
)) {
393 rq
->q
->softirq_done_fn(rq
);
398 if (!test_bit(QUEUE_FLAG_SAME_FORCE
, &rq
->q
->queue_flags
))
399 shared
= cpus_share_cache(cpu
, ctx
->cpu
);
401 if (cpu
!= ctx
->cpu
&& !shared
&& cpu_online(ctx
->cpu
)) {
402 rq
->csd
.func
= __blk_mq_complete_request_remote
;
405 smp_call_function_single_async(ctx
->cpu
, &rq
->csd
);
407 rq
->q
->softirq_done_fn(rq
);
413 * blk_mq_complete_request - end I/O on a request
414 * @rq: the request being processed
417 * Ends all I/O on a request. It does not handle partial completions.
418 * The actual completion happens out-of-order, through a IPI handler.
420 void blk_mq_complete_request(struct request
*rq
)
422 struct request_queue
*q
= rq
->q
;
424 if (unlikely(blk_should_fake_timeout(q
)))
426 if (!blk_mark_rq_complete(rq
)) {
427 if (q
->softirq_done_fn
)
428 __blk_mq_complete_request(rq
);
430 blk_mq_end_io(rq
, rq
->errors
);
433 EXPORT_SYMBOL(blk_mq_complete_request
);
435 static void blk_mq_start_request(struct request
*rq
, bool last
)
437 struct request_queue
*q
= rq
->q
;
439 trace_block_rq_issue(q
, rq
);
441 rq
->resid_len
= blk_rq_bytes(rq
);
442 if (unlikely(blk_bidi_rq(rq
)))
443 rq
->next_rq
->resid_len
= blk_rq_bytes(rq
->next_rq
);
446 * Just mark start time and set the started bit. Due to memory
447 * ordering, we know we'll see the correct deadline as long as
448 * REQ_ATOMIC_STARTED is seen. Use the default queue timeout,
449 * unless one has been set in the request.
452 rq
->deadline
= jiffies
+ q
->rq_timeout
;
454 rq
->deadline
= jiffies
+ rq
->timeout
;
457 * Mark us as started and clear complete. Complete might have been
458 * set if requeue raced with timeout, which then marked it as
459 * complete. So be sure to clear complete again when we start
460 * the request, otherwise we'll ignore the completion event.
462 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
463 clear_bit(REQ_ATOM_COMPLETE
, &rq
->atomic_flags
);
465 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
467 * Make sure space for the drain appears. We know we can do
468 * this because max_hw_segments has been adjusted to be one
469 * fewer than the device can handle.
471 rq
->nr_phys_segments
++;
475 * Flag the last request in the series so that drivers know when IO
476 * should be kicked off, if they don't do it on a per-request basis.
478 * Note: the flag isn't the only condition drivers should do kick off.
479 * If drive is busy, the last request might not have the bit set.
482 rq
->cmd_flags
|= REQ_END
;
485 static void __blk_mq_requeue_request(struct request
*rq
)
487 struct request_queue
*q
= rq
->q
;
489 trace_block_rq_requeue(q
, rq
);
490 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
492 rq
->cmd_flags
&= ~REQ_END
;
494 if (q
->dma_drain_size
&& blk_rq_bytes(rq
))
495 rq
->nr_phys_segments
--;
498 void blk_mq_requeue_request(struct request
*rq
)
500 __blk_mq_requeue_request(rq
);
501 blk_clear_rq_complete(rq
);
503 BUG_ON(blk_queued_rq(rq
));
504 blk_mq_add_to_requeue_list(rq
, true);
506 EXPORT_SYMBOL(blk_mq_requeue_request
);
508 static void blk_mq_requeue_work(struct work_struct
*work
)
510 struct request_queue
*q
=
511 container_of(work
, struct request_queue
, requeue_work
);
513 struct request
*rq
, *next
;
516 spin_lock_irqsave(&q
->requeue_lock
, flags
);
517 list_splice_init(&q
->requeue_list
, &rq_list
);
518 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
520 list_for_each_entry_safe(rq
, next
, &rq_list
, queuelist
) {
521 if (!(rq
->cmd_flags
& REQ_SOFTBARRIER
))
524 rq
->cmd_flags
&= ~REQ_SOFTBARRIER
;
525 list_del_init(&rq
->queuelist
);
526 blk_mq_insert_request(rq
, true, false, false);
529 while (!list_empty(&rq_list
)) {
530 rq
= list_entry(rq_list
.next
, struct request
, queuelist
);
531 list_del_init(&rq
->queuelist
);
532 blk_mq_insert_request(rq
, false, false, false);
535 blk_mq_run_queues(q
, false);
538 void blk_mq_add_to_requeue_list(struct request
*rq
, bool at_head
)
540 struct request_queue
*q
= rq
->q
;
544 * We abuse this flag that is otherwise used by the I/O scheduler to
545 * request head insertation from the workqueue.
547 BUG_ON(rq
->cmd_flags
& REQ_SOFTBARRIER
);
549 spin_lock_irqsave(&q
->requeue_lock
, flags
);
551 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
552 list_add(&rq
->queuelist
, &q
->requeue_list
);
554 list_add_tail(&rq
->queuelist
, &q
->requeue_list
);
556 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
558 EXPORT_SYMBOL(blk_mq_add_to_requeue_list
);
560 void blk_mq_kick_requeue_list(struct request_queue
*q
)
562 kblockd_schedule_work(&q
->requeue_work
);
564 EXPORT_SYMBOL(blk_mq_kick_requeue_list
);
566 struct request
*blk_mq_tag_to_rq(struct blk_mq_tags
*tags
, unsigned int tag
)
568 return tags
->rqs
[tag
];
570 EXPORT_SYMBOL(blk_mq_tag_to_rq
);
572 struct blk_mq_timeout_data
{
573 struct blk_mq_hw_ctx
*hctx
;
575 unsigned int *next_set
;
578 static void blk_mq_timeout_check(void *__data
, unsigned long *free_tags
)
580 struct blk_mq_timeout_data
*data
= __data
;
581 struct blk_mq_hw_ctx
*hctx
= data
->hctx
;
584 /* It may not be in flight yet (this is where
585 * the REQ_ATOMIC_STARTED flag comes in). The requests are
586 * statically allocated, so we know it's always safe to access the
587 * memory associated with a bit offset into ->rqs[].
593 tag
= find_next_zero_bit(free_tags
, hctx
->tags
->nr_tags
, tag
);
594 if (tag
>= hctx
->tags
->nr_tags
)
597 rq
= blk_mq_tag_to_rq(hctx
->tags
, tag
++);
598 if (rq
->q
!= hctx
->queue
)
600 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
603 blk_rq_check_expired(rq
, data
->next
, data
->next_set
);
607 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx
*hctx
,
609 unsigned int *next_set
)
611 struct blk_mq_timeout_data data
= {
614 .next_set
= next_set
,
618 * Ask the tagging code to iterate busy requests, so we can
619 * check them for timeout.
621 blk_mq_tag_busy_iter(hctx
->tags
, blk_mq_timeout_check
, &data
);
624 static enum blk_eh_timer_return
blk_mq_rq_timed_out(struct request
*rq
)
626 struct request_queue
*q
= rq
->q
;
629 * We know that complete is set at this point. If STARTED isn't set
630 * anymore, then the request isn't active and the "timeout" should
631 * just be ignored. This can happen due to the bitflag ordering.
632 * Timeout first checks if STARTED is set, and if it is, assumes
633 * the request is active. But if we race with completion, then
634 * we both flags will get cleared. So check here again, and ignore
635 * a timeout event with a request that isn't active.
637 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
638 return BLK_EH_NOT_HANDLED
;
640 if (!q
->mq_ops
->timeout
)
641 return BLK_EH_RESET_TIMER
;
643 return q
->mq_ops
->timeout(rq
);
646 static void blk_mq_rq_timer(unsigned long data
)
648 struct request_queue
*q
= (struct request_queue
*) data
;
649 struct blk_mq_hw_ctx
*hctx
;
650 unsigned long next
= 0;
653 queue_for_each_hw_ctx(q
, hctx
, i
) {
655 * If not software queues are currently mapped to this
656 * hardware queue, there's nothing to check
658 if (!hctx
->nr_ctx
|| !hctx
->tags
)
661 blk_mq_hw_ctx_check_timeout(hctx
, &next
, &next_set
);
665 next
= blk_rq_timeout(round_jiffies_up(next
));
666 mod_timer(&q
->timeout
, next
);
668 queue_for_each_hw_ctx(q
, hctx
, i
)
669 blk_mq_tag_idle(hctx
);
674 * Reverse check our software queue for entries that we could potentially
675 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
676 * too much time checking for merges.
678 static bool blk_mq_attempt_merge(struct request_queue
*q
,
679 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
684 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
690 if (!blk_rq_merge_ok(rq
, bio
))
693 el_ret
= blk_try_merge(rq
, bio
);
694 if (el_ret
== ELEVATOR_BACK_MERGE
) {
695 if (bio_attempt_back_merge(q
, rq
, bio
)) {
700 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
701 if (bio_attempt_front_merge(q
, rq
, bio
)) {
713 * Process software queues that have been marked busy, splicing them
714 * to the for-dispatch
716 static void flush_busy_ctxs(struct blk_mq_hw_ctx
*hctx
, struct list_head
*list
)
718 struct blk_mq_ctx
*ctx
;
721 for (i
= 0; i
< hctx
->ctx_map
.map_size
; i
++) {
722 struct blk_align_bitmap
*bm
= &hctx
->ctx_map
.map
[i
];
723 unsigned int off
, bit
;
729 off
= i
* hctx
->ctx_map
.bits_per_word
;
731 bit
= find_next_bit(&bm
->word
, bm
->depth
, bit
);
732 if (bit
>= bm
->depth
)
735 ctx
= hctx
->ctxs
[bit
+ off
];
736 clear_bit(bit
, &bm
->word
);
737 spin_lock(&ctx
->lock
);
738 list_splice_tail_init(&ctx
->rq_list
, list
);
739 spin_unlock(&ctx
->lock
);
747 * Run this hardware queue, pulling any software queues mapped to it in.
748 * Note that this function currently has various problems around ordering
749 * of IO. In particular, we'd like FIFO behaviour on handling existing
750 * items on the hctx->dispatch list. Ignore that for now.
752 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
754 struct request_queue
*q
= hctx
->queue
;
759 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx
->cpumask
));
761 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
767 * Touch any software queue that has pending entries.
769 flush_busy_ctxs(hctx
, &rq_list
);
772 * If we have previous entries on our dispatch list, grab them
773 * and stuff them at the front for more fair dispatch.
775 if (!list_empty_careful(&hctx
->dispatch
)) {
776 spin_lock(&hctx
->lock
);
777 if (!list_empty(&hctx
->dispatch
))
778 list_splice_init(&hctx
->dispatch
, &rq_list
);
779 spin_unlock(&hctx
->lock
);
783 * Now process all the entries, sending them to the driver.
786 while (!list_empty(&rq_list
)) {
789 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
790 list_del_init(&rq
->queuelist
);
792 blk_mq_start_request(rq
, list_empty(&rq_list
));
794 ret
= q
->mq_ops
->queue_rq(hctx
, rq
);
796 case BLK_MQ_RQ_QUEUE_OK
:
799 case BLK_MQ_RQ_QUEUE_BUSY
:
800 list_add(&rq
->queuelist
, &rq_list
);
801 __blk_mq_requeue_request(rq
);
804 pr_err("blk-mq: bad return on queue: %d\n", ret
);
805 case BLK_MQ_RQ_QUEUE_ERROR
:
807 blk_mq_end_io(rq
, rq
->errors
);
811 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
816 hctx
->dispatched
[0]++;
817 else if (queued
< (1 << (BLK_MQ_MAX_DISPATCH_ORDER
- 1)))
818 hctx
->dispatched
[ilog2(queued
) + 1]++;
821 * Any items that need requeuing? Stuff them into hctx->dispatch,
822 * that is where we will continue on next queue run.
824 if (!list_empty(&rq_list
)) {
825 spin_lock(&hctx
->lock
);
826 list_splice(&rq_list
, &hctx
->dispatch
);
827 spin_unlock(&hctx
->lock
);
832 * It'd be great if the workqueue API had a way to pass
833 * in a mask and had some smarts for more clever placement.
834 * For now we just round-robin here, switching for every
835 * BLK_MQ_CPU_WORK_BATCH queued items.
837 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx
*hctx
)
839 int cpu
= hctx
->next_cpu
;
841 if (--hctx
->next_cpu_batch
<= 0) {
844 next_cpu
= cpumask_next(hctx
->next_cpu
, hctx
->cpumask
);
845 if (next_cpu
>= nr_cpu_ids
)
846 next_cpu
= cpumask_first(hctx
->cpumask
);
848 hctx
->next_cpu
= next_cpu
;
849 hctx
->next_cpu_batch
= BLK_MQ_CPU_WORK_BATCH
;
855 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
857 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
860 if (!async
&& cpumask_test_cpu(smp_processor_id(), hctx
->cpumask
))
861 __blk_mq_run_hw_queue(hctx
);
862 else if (hctx
->queue
->nr_hw_queues
== 1)
863 kblockd_schedule_delayed_work(&hctx
->run_work
, 0);
867 cpu
= blk_mq_hctx_next_cpu(hctx
);
868 kblockd_schedule_delayed_work_on(cpu
, &hctx
->run_work
, 0);
872 void blk_mq_run_queues(struct request_queue
*q
, bool async
)
874 struct blk_mq_hw_ctx
*hctx
;
877 queue_for_each_hw_ctx(q
, hctx
, i
) {
878 if ((!blk_mq_hctx_has_pending(hctx
) &&
879 list_empty_careful(&hctx
->dispatch
)) ||
880 test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
884 blk_mq_run_hw_queue(hctx
, async
);
888 EXPORT_SYMBOL(blk_mq_run_queues
);
890 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
892 cancel_delayed_work(&hctx
->run_work
);
893 cancel_delayed_work(&hctx
->delay_work
);
894 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
896 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
898 void blk_mq_stop_hw_queues(struct request_queue
*q
)
900 struct blk_mq_hw_ctx
*hctx
;
903 queue_for_each_hw_ctx(q
, hctx
, i
)
904 blk_mq_stop_hw_queue(hctx
);
906 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
908 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
910 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
913 __blk_mq_run_hw_queue(hctx
);
916 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
918 void blk_mq_start_hw_queues(struct request_queue
*q
)
920 struct blk_mq_hw_ctx
*hctx
;
923 queue_for_each_hw_ctx(q
, hctx
, i
)
924 blk_mq_start_hw_queue(hctx
);
926 EXPORT_SYMBOL(blk_mq_start_hw_queues
);
929 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
, bool async
)
931 struct blk_mq_hw_ctx
*hctx
;
934 queue_for_each_hw_ctx(q
, hctx
, i
) {
935 if (!test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
938 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
940 blk_mq_run_hw_queue(hctx
, async
);
944 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
946 static void blk_mq_run_work_fn(struct work_struct
*work
)
948 struct blk_mq_hw_ctx
*hctx
;
950 hctx
= container_of(work
, struct blk_mq_hw_ctx
, run_work
.work
);
952 __blk_mq_run_hw_queue(hctx
);
955 static void blk_mq_delay_work_fn(struct work_struct
*work
)
957 struct blk_mq_hw_ctx
*hctx
;
959 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delay_work
.work
);
961 if (test_and_clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
962 __blk_mq_run_hw_queue(hctx
);
965 void blk_mq_delay_queue(struct blk_mq_hw_ctx
*hctx
, unsigned long msecs
)
967 unsigned long tmo
= msecs_to_jiffies(msecs
);
969 if (hctx
->queue
->nr_hw_queues
== 1)
970 kblockd_schedule_delayed_work(&hctx
->delay_work
, tmo
);
974 cpu
= blk_mq_hctx_next_cpu(hctx
);
975 kblockd_schedule_delayed_work_on(cpu
, &hctx
->delay_work
, tmo
);
978 EXPORT_SYMBOL(blk_mq_delay_queue
);
980 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
981 struct request
*rq
, bool at_head
)
983 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
985 trace_block_rq_insert(hctx
->queue
, rq
);
988 list_add(&rq
->queuelist
, &ctx
->rq_list
);
990 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
992 blk_mq_hctx_mark_pending(hctx
, ctx
);
995 * We do this early, to ensure we are on the right CPU.
1000 void blk_mq_insert_request(struct request
*rq
, bool at_head
, bool run_queue
,
1003 struct request_queue
*q
= rq
->q
;
1004 struct blk_mq_hw_ctx
*hctx
;
1005 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
, *current_ctx
;
1007 current_ctx
= blk_mq_get_ctx(q
);
1008 if (!cpu_online(ctx
->cpu
))
1009 rq
->mq_ctx
= ctx
= current_ctx
;
1011 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1013 if (rq
->cmd_flags
& (REQ_FLUSH
| REQ_FUA
) &&
1014 !(rq
->cmd_flags
& (REQ_FLUSH_SEQ
))) {
1015 blk_insert_flush(rq
);
1017 spin_lock(&ctx
->lock
);
1018 __blk_mq_insert_request(hctx
, rq
, at_head
);
1019 spin_unlock(&ctx
->lock
);
1023 blk_mq_run_hw_queue(hctx
, async
);
1025 blk_mq_put_ctx(current_ctx
);
1028 static void blk_mq_insert_requests(struct request_queue
*q
,
1029 struct blk_mq_ctx
*ctx
,
1030 struct list_head
*list
,
1035 struct blk_mq_hw_ctx
*hctx
;
1036 struct blk_mq_ctx
*current_ctx
;
1038 trace_block_unplug(q
, depth
, !from_schedule
);
1040 current_ctx
= blk_mq_get_ctx(q
);
1042 if (!cpu_online(ctx
->cpu
))
1044 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1047 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1050 spin_lock(&ctx
->lock
);
1051 while (!list_empty(list
)) {
1054 rq
= list_first_entry(list
, struct request
, queuelist
);
1055 list_del_init(&rq
->queuelist
);
1057 __blk_mq_insert_request(hctx
, rq
, false);
1059 spin_unlock(&ctx
->lock
);
1061 blk_mq_run_hw_queue(hctx
, from_schedule
);
1062 blk_mq_put_ctx(current_ctx
);
1065 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1067 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
1068 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
1070 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
1071 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
1072 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
1075 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
1077 struct blk_mq_ctx
*this_ctx
;
1078 struct request_queue
*this_q
;
1081 LIST_HEAD(ctx_list
);
1084 list_splice_init(&plug
->mq_list
, &list
);
1086 list_sort(NULL
, &list
, plug_ctx_cmp
);
1092 while (!list_empty(&list
)) {
1093 rq
= list_entry_rq(list
.next
);
1094 list_del_init(&rq
->queuelist
);
1096 if (rq
->mq_ctx
!= this_ctx
) {
1098 blk_mq_insert_requests(this_q
, this_ctx
,
1103 this_ctx
= rq
->mq_ctx
;
1109 list_add_tail(&rq
->queuelist
, &ctx_list
);
1113 * If 'this_ctx' is set, we know we have entries to complete
1114 * on 'ctx_list'. Do those.
1117 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
1122 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
1124 init_request_from_bio(rq
, bio
);
1125 blk_account_io_start(rq
, 1);
1128 static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx
*hctx
,
1129 struct blk_mq_ctx
*ctx
,
1130 struct request
*rq
, struct bio
*bio
)
1132 struct request_queue
*q
= hctx
->queue
;
1134 if (!(hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
)) {
1135 blk_mq_bio_to_request(rq
, bio
);
1136 spin_lock(&ctx
->lock
);
1138 __blk_mq_insert_request(hctx
, rq
, false);
1139 spin_unlock(&ctx
->lock
);
1142 spin_lock(&ctx
->lock
);
1143 if (!blk_mq_attempt_merge(q
, ctx
, bio
)) {
1144 blk_mq_bio_to_request(rq
, bio
);
1148 spin_unlock(&ctx
->lock
);
1149 __blk_mq_free_request(hctx
, ctx
, rq
);
1154 struct blk_map_ctx
{
1155 struct blk_mq_hw_ctx
*hctx
;
1156 struct blk_mq_ctx
*ctx
;
1159 static struct request
*blk_mq_map_request(struct request_queue
*q
,
1161 struct blk_map_ctx
*data
)
1163 struct blk_mq_hw_ctx
*hctx
;
1164 struct blk_mq_ctx
*ctx
;
1166 int rw
= bio_data_dir(bio
);
1168 if (unlikely(blk_mq_queue_enter(q
))) {
1169 bio_endio(bio
, -EIO
);
1173 ctx
= blk_mq_get_ctx(q
);
1174 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1176 if (rw_is_sync(bio
->bi_rw
))
1179 trace_block_getrq(q
, bio
, rw
);
1180 rq
= __blk_mq_alloc_request(q
, hctx
, ctx
, rw
, GFP_ATOMIC
, false);
1181 if (unlikely(!rq
)) {
1182 __blk_mq_run_hw_queue(hctx
);
1183 blk_mq_put_ctx(ctx
);
1184 trace_block_sleeprq(q
, bio
, rw
);
1186 ctx
= blk_mq_get_ctx(q
);
1187 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1188 rq
= __blk_mq_alloc_request(q
, hctx
, ctx
, rw
,
1189 __GFP_WAIT
|GFP_ATOMIC
, false);
1199 * Multiple hardware queue variant. This will not use per-process plugs,
1200 * but will attempt to bypass the hctx queueing if we can go straight to
1201 * hardware for SYNC IO.
1203 static void blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
1205 const int is_sync
= rw_is_sync(bio
->bi_rw
);
1206 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
1207 struct blk_map_ctx data
;
1210 blk_queue_bounce(q
, &bio
);
1212 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1213 bio_endio(bio
, -EIO
);
1217 rq
= blk_mq_map_request(q
, bio
, &data
);
1221 if (unlikely(is_flush_fua
)) {
1222 blk_mq_bio_to_request(rq
, bio
);
1223 blk_insert_flush(rq
);
1230 blk_mq_bio_to_request(rq
, bio
);
1231 blk_mq_start_request(rq
, true);
1234 * For OK queue, we are done. For error, kill it. Any other
1235 * error (busy), just add it to our list as we previously
1238 ret
= q
->mq_ops
->queue_rq(data
.hctx
, rq
);
1239 if (ret
== BLK_MQ_RQ_QUEUE_OK
)
1242 __blk_mq_requeue_request(rq
);
1244 if (ret
== BLK_MQ_RQ_QUEUE_ERROR
) {
1246 blk_mq_end_io(rq
, rq
->errors
);
1252 if (!blk_mq_merge_queue_io(data
.hctx
, data
.ctx
, rq
, bio
)) {
1254 * For a SYNC request, send it to the hardware immediately. For
1255 * an ASYNC request, just ensure that we run it later on. The
1256 * latter allows for merging opportunities and more efficient
1260 blk_mq_run_hw_queue(data
.hctx
, !is_sync
|| is_flush_fua
);
1263 blk_mq_put_ctx(data
.ctx
);
1267 * Single hardware queue variant. This will attempt to use any per-process
1268 * plug for merging and IO deferral.
1270 static void blk_sq_make_request(struct request_queue
*q
, struct bio
*bio
)
1272 const int is_sync
= rw_is_sync(bio
->bi_rw
);
1273 const int is_flush_fua
= bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
);
1274 unsigned int use_plug
, request_count
= 0;
1275 struct blk_map_ctx data
;
1279 * If we have multiple hardware queues, just go directly to
1280 * one of those for sync IO.
1282 use_plug
= !is_flush_fua
&& !is_sync
;
1284 blk_queue_bounce(q
, &bio
);
1286 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1287 bio_endio(bio
, -EIO
);
1291 if (use_plug
&& !blk_queue_nomerges(q
) &&
1292 blk_attempt_plug_merge(q
, bio
, &request_count
))
1295 rq
= blk_mq_map_request(q
, bio
, &data
);
1297 if (unlikely(is_flush_fua
)) {
1298 blk_mq_bio_to_request(rq
, bio
);
1299 blk_insert_flush(rq
);
1304 * A task plug currently exists. Since this is completely lockless,
1305 * utilize that to temporarily store requests until the task is
1306 * either done or scheduled away.
1309 struct blk_plug
*plug
= current
->plug
;
1312 blk_mq_bio_to_request(rq
, bio
);
1313 if (list_empty(&plug
->mq_list
))
1314 trace_block_plug(q
);
1315 else if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
1316 blk_flush_plug_list(plug
, false);
1317 trace_block_plug(q
);
1319 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
1320 blk_mq_put_ctx(data
.ctx
);
1325 if (!blk_mq_merge_queue_io(data
.hctx
, data
.ctx
, rq
, bio
)) {
1327 * For a SYNC request, send it to the hardware immediately. For
1328 * an ASYNC request, just ensure that we run it later on. The
1329 * latter allows for merging opportunities and more efficient
1333 blk_mq_run_hw_queue(data
.hctx
, !is_sync
|| is_flush_fua
);
1336 blk_mq_put_ctx(data
.ctx
);
1340 * Default mapping to a software queue, since we use one per CPU.
1342 struct blk_mq_hw_ctx
*blk_mq_map_queue(struct request_queue
*q
, const int cpu
)
1344 return q
->queue_hw_ctx
[q
->mq_map
[cpu
]];
1346 EXPORT_SYMBOL(blk_mq_map_queue
);
1348 struct blk_mq_hw_ctx
*blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set
*set
,
1349 unsigned int hctx_index
,
1352 return kzalloc_node(sizeof(struct blk_mq_hw_ctx
), GFP_KERNEL
, node
);
1354 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue
);
1356 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx
*hctx
,
1357 unsigned int hctx_index
)
1361 EXPORT_SYMBOL(blk_mq_free_single_hw_queue
);
1363 static void blk_mq_free_rq_map(struct blk_mq_tag_set
*set
,
1364 struct blk_mq_tags
*tags
, unsigned int hctx_idx
)
1368 if (tags
->rqs
&& set
->ops
->exit_request
) {
1371 for (i
= 0; i
< tags
->nr_tags
; i
++) {
1374 set
->ops
->exit_request(set
->driver_data
, tags
->rqs
[i
],
1379 while (!list_empty(&tags
->page_list
)) {
1380 page
= list_first_entry(&tags
->page_list
, struct page
, lru
);
1381 list_del_init(&page
->lru
);
1382 __free_pages(page
, page
->private);
1387 blk_mq_free_tags(tags
);
1390 static size_t order_to_size(unsigned int order
)
1392 return (size_t)PAGE_SIZE
<< order
;
1395 static struct blk_mq_tags
*blk_mq_init_rq_map(struct blk_mq_tag_set
*set
,
1396 unsigned int hctx_idx
)
1398 struct blk_mq_tags
*tags
;
1399 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1400 size_t rq_size
, left
;
1402 tags
= blk_mq_init_tags(set
->queue_depth
, set
->reserved_tags
,
1407 INIT_LIST_HEAD(&tags
->page_list
);
1409 tags
->rqs
= kmalloc_node(set
->queue_depth
* sizeof(struct request
*),
1410 GFP_KERNEL
, set
->numa_node
);
1412 blk_mq_free_tags(tags
);
1417 * rq_size is the size of the request plus driver payload, rounded
1418 * to the cacheline size
1420 rq_size
= round_up(sizeof(struct request
) + set
->cmd_size
,
1422 left
= rq_size
* set
->queue_depth
;
1424 for (i
= 0; i
< set
->queue_depth
; ) {
1425 int this_order
= max_order
;
1430 while (left
< order_to_size(this_order
- 1) && this_order
)
1434 page
= alloc_pages_node(set
->numa_node
, GFP_KERNEL
,
1440 if (order_to_size(this_order
) < rq_size
)
1447 page
->private = this_order
;
1448 list_add_tail(&page
->lru
, &tags
->page_list
);
1450 p
= page_address(page
);
1451 entries_per_page
= order_to_size(this_order
) / rq_size
;
1452 to_do
= min(entries_per_page
, set
->queue_depth
- i
);
1453 left
-= to_do
* rq_size
;
1454 for (j
= 0; j
< to_do
; j
++) {
1456 if (set
->ops
->init_request
) {
1457 if (set
->ops
->init_request(set
->driver_data
,
1458 tags
->rqs
[i
], hctx_idx
, i
,
1471 pr_warn("%s: failed to allocate requests\n", __func__
);
1472 blk_mq_free_rq_map(set
, tags
, hctx_idx
);
1476 static void blk_mq_free_bitmap(struct blk_mq_ctxmap
*bitmap
)
1481 static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap
*bitmap
, int node
)
1483 unsigned int bpw
= 8, total
, num_maps
, i
;
1485 bitmap
->bits_per_word
= bpw
;
1487 num_maps
= ALIGN(nr_cpu_ids
, bpw
) / bpw
;
1488 bitmap
->map
= kzalloc_node(num_maps
* sizeof(struct blk_align_bitmap
),
1493 bitmap
->map_size
= num_maps
;
1496 for (i
= 0; i
< num_maps
; i
++) {
1497 bitmap
->map
[i
].depth
= min(total
, bitmap
->bits_per_word
);
1498 total
-= bitmap
->map
[i
].depth
;
1504 static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx
*hctx
, int cpu
)
1506 struct request_queue
*q
= hctx
->queue
;
1507 struct blk_mq_ctx
*ctx
;
1511 * Move ctx entries to new CPU, if this one is going away.
1513 ctx
= __blk_mq_get_ctx(q
, cpu
);
1515 spin_lock(&ctx
->lock
);
1516 if (!list_empty(&ctx
->rq_list
)) {
1517 list_splice_init(&ctx
->rq_list
, &tmp
);
1518 blk_mq_hctx_clear_pending(hctx
, ctx
);
1520 spin_unlock(&ctx
->lock
);
1522 if (list_empty(&tmp
))
1525 ctx
= blk_mq_get_ctx(q
);
1526 spin_lock(&ctx
->lock
);
1528 while (!list_empty(&tmp
)) {
1531 rq
= list_first_entry(&tmp
, struct request
, queuelist
);
1533 list_move_tail(&rq
->queuelist
, &ctx
->rq_list
);
1536 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1537 blk_mq_hctx_mark_pending(hctx
, ctx
);
1539 spin_unlock(&ctx
->lock
);
1541 blk_mq_run_hw_queue(hctx
, true);
1542 blk_mq_put_ctx(ctx
);
1546 static int blk_mq_hctx_cpu_online(struct blk_mq_hw_ctx
*hctx
, int cpu
)
1548 struct request_queue
*q
= hctx
->queue
;
1549 struct blk_mq_tag_set
*set
= q
->tag_set
;
1551 if (set
->tags
[hctx
->queue_num
])
1554 set
->tags
[hctx
->queue_num
] = blk_mq_init_rq_map(set
, hctx
->queue_num
);
1555 if (!set
->tags
[hctx
->queue_num
])
1558 hctx
->tags
= set
->tags
[hctx
->queue_num
];
1562 static int blk_mq_hctx_notify(void *data
, unsigned long action
,
1565 struct blk_mq_hw_ctx
*hctx
= data
;
1567 if (action
== CPU_DEAD
|| action
== CPU_DEAD_FROZEN
)
1568 return blk_mq_hctx_cpu_offline(hctx
, cpu
);
1569 else if (action
== CPU_ONLINE
|| action
== CPU_ONLINE_FROZEN
)
1570 return blk_mq_hctx_cpu_online(hctx
, cpu
);
1575 static void blk_mq_exit_hw_queues(struct request_queue
*q
,
1576 struct blk_mq_tag_set
*set
, int nr_queue
)
1578 struct blk_mq_hw_ctx
*hctx
;
1581 queue_for_each_hw_ctx(q
, hctx
, i
) {
1585 if (set
->ops
->exit_hctx
)
1586 set
->ops
->exit_hctx(hctx
, i
);
1588 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1590 blk_mq_free_bitmap(&hctx
->ctx_map
);
1595 static void blk_mq_free_hw_queues(struct request_queue
*q
,
1596 struct blk_mq_tag_set
*set
)
1598 struct blk_mq_hw_ctx
*hctx
;
1601 queue_for_each_hw_ctx(q
, hctx
, i
) {
1602 free_cpumask_var(hctx
->cpumask
);
1603 set
->ops
->free_hctx(hctx
, i
);
1607 static int blk_mq_init_hw_queues(struct request_queue
*q
,
1608 struct blk_mq_tag_set
*set
)
1610 struct blk_mq_hw_ctx
*hctx
;
1614 * Initialize hardware queues
1616 queue_for_each_hw_ctx(q
, hctx
, i
) {
1619 node
= hctx
->numa_node
;
1620 if (node
== NUMA_NO_NODE
)
1621 node
= hctx
->numa_node
= set
->numa_node
;
1623 INIT_DELAYED_WORK(&hctx
->run_work
, blk_mq_run_work_fn
);
1624 INIT_DELAYED_WORK(&hctx
->delay_work
, blk_mq_delay_work_fn
);
1625 spin_lock_init(&hctx
->lock
);
1626 INIT_LIST_HEAD(&hctx
->dispatch
);
1628 hctx
->queue_num
= i
;
1629 hctx
->flags
= set
->flags
;
1630 hctx
->cmd_size
= set
->cmd_size
;
1632 blk_mq_init_cpu_notifier(&hctx
->cpu_notifier
,
1633 blk_mq_hctx_notify
, hctx
);
1634 blk_mq_register_cpu_notifier(&hctx
->cpu_notifier
);
1636 hctx
->tags
= set
->tags
[i
];
1639 * Allocate space for all possible cpus to avoid allocation in
1642 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1647 if (blk_mq_alloc_bitmap(&hctx
->ctx_map
, node
))
1652 if (set
->ops
->init_hctx
&&
1653 set
->ops
->init_hctx(hctx
, set
->driver_data
, i
))
1657 if (i
== q
->nr_hw_queues
)
1663 blk_mq_exit_hw_queues(q
, set
, i
);
1668 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1669 unsigned int nr_hw_queues
)
1673 for_each_possible_cpu(i
) {
1674 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1675 struct blk_mq_hw_ctx
*hctx
;
1677 memset(__ctx
, 0, sizeof(*__ctx
));
1679 spin_lock_init(&__ctx
->lock
);
1680 INIT_LIST_HEAD(&__ctx
->rq_list
);
1683 /* If the cpu isn't online, the cpu is mapped to first hctx */
1687 hctx
= q
->mq_ops
->map_queue(q
, i
);
1688 cpumask_set_cpu(i
, hctx
->cpumask
);
1692 * Set local node, IFF we have more than one hw queue. If
1693 * not, we remain on the home node of the device
1695 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1696 hctx
->numa_node
= cpu_to_node(i
);
1700 static void blk_mq_map_swqueue(struct request_queue
*q
)
1703 struct blk_mq_hw_ctx
*hctx
;
1704 struct blk_mq_ctx
*ctx
;
1706 queue_for_each_hw_ctx(q
, hctx
, i
) {
1707 cpumask_clear(hctx
->cpumask
);
1712 * Map software to hardware queues
1714 queue_for_each_ctx(q
, ctx
, i
) {
1715 /* If the cpu isn't online, the cpu is mapped to first hctx */
1719 hctx
= q
->mq_ops
->map_queue(q
, i
);
1720 cpumask_set_cpu(i
, hctx
->cpumask
);
1721 ctx
->index_hw
= hctx
->nr_ctx
;
1722 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1725 queue_for_each_hw_ctx(q
, hctx
, i
) {
1727 * If not software queues are mapped to this hardware queue,
1728 * disable it and free the request entries
1730 if (!hctx
->nr_ctx
) {
1731 struct blk_mq_tag_set
*set
= q
->tag_set
;
1734 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
1735 set
->tags
[i
] = NULL
;
1742 * Initialize batch roundrobin counts
1744 hctx
->next_cpu
= cpumask_first(hctx
->cpumask
);
1745 hctx
->next_cpu_batch
= BLK_MQ_CPU_WORK_BATCH
;
1749 static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set
*set
)
1751 struct blk_mq_hw_ctx
*hctx
;
1752 struct request_queue
*q
;
1756 if (set
->tag_list
.next
== set
->tag_list
.prev
)
1761 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
) {
1762 blk_mq_freeze_queue(q
);
1764 queue_for_each_hw_ctx(q
, hctx
, i
) {
1766 hctx
->flags
|= BLK_MQ_F_TAG_SHARED
;
1768 hctx
->flags
&= ~BLK_MQ_F_TAG_SHARED
;
1770 blk_mq_unfreeze_queue(q
);
1774 static void blk_mq_del_queue_tag_set(struct request_queue
*q
)
1776 struct blk_mq_tag_set
*set
= q
->tag_set
;
1778 blk_mq_freeze_queue(q
);
1780 mutex_lock(&set
->tag_list_lock
);
1781 list_del_init(&q
->tag_set_list
);
1782 blk_mq_update_tag_set_depth(set
);
1783 mutex_unlock(&set
->tag_list_lock
);
1785 blk_mq_unfreeze_queue(q
);
1788 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set
*set
,
1789 struct request_queue
*q
)
1793 mutex_lock(&set
->tag_list_lock
);
1794 list_add_tail(&q
->tag_set_list
, &set
->tag_list
);
1795 blk_mq_update_tag_set_depth(set
);
1796 mutex_unlock(&set
->tag_list_lock
);
1799 struct request_queue
*blk_mq_init_queue(struct blk_mq_tag_set
*set
)
1801 struct blk_mq_hw_ctx
**hctxs
;
1802 struct blk_mq_ctx
*ctx
;
1803 struct request_queue
*q
;
1807 ctx
= alloc_percpu(struct blk_mq_ctx
);
1809 return ERR_PTR(-ENOMEM
);
1811 hctxs
= kmalloc_node(set
->nr_hw_queues
* sizeof(*hctxs
), GFP_KERNEL
,
1817 map
= blk_mq_make_queue_map(set
);
1821 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1822 int node
= blk_mq_hw_queue_to_node(map
, i
);
1824 hctxs
[i
] = set
->ops
->alloc_hctx(set
, i
, node
);
1828 if (!zalloc_cpumask_var(&hctxs
[i
]->cpumask
, GFP_KERNEL
))
1831 atomic_set(&hctxs
[i
]->nr_active
, 0);
1832 hctxs
[i
]->numa_node
= node
;
1833 hctxs
[i
]->queue_num
= i
;
1836 q
= blk_alloc_queue_node(GFP_KERNEL
, set
->numa_node
);
1840 if (percpu_counter_init(&q
->mq_usage_counter
, 0))
1843 setup_timer(&q
->timeout
, blk_mq_rq_timer
, (unsigned long) q
);
1844 blk_queue_rq_timeout(q
, 30000);
1846 q
->nr_queues
= nr_cpu_ids
;
1847 q
->nr_hw_queues
= set
->nr_hw_queues
;
1851 q
->queue_hw_ctx
= hctxs
;
1853 q
->mq_ops
= set
->ops
;
1854 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
1856 q
->sg_reserved_size
= INT_MAX
;
1858 INIT_WORK(&q
->requeue_work
, blk_mq_requeue_work
);
1859 INIT_LIST_HEAD(&q
->requeue_list
);
1860 spin_lock_init(&q
->requeue_lock
);
1862 if (q
->nr_hw_queues
> 1)
1863 blk_queue_make_request(q
, blk_mq_make_request
);
1865 blk_queue_make_request(q
, blk_sq_make_request
);
1867 blk_queue_rq_timed_out(q
, blk_mq_rq_timed_out
);
1869 blk_queue_rq_timeout(q
, set
->timeout
);
1872 * Do this after blk_queue_make_request() overrides it...
1874 q
->nr_requests
= set
->queue_depth
;
1876 if (set
->ops
->complete
)
1877 blk_queue_softirq_done(q
, set
->ops
->complete
);
1879 blk_mq_init_flush(q
);
1880 blk_mq_init_cpu_queues(q
, set
->nr_hw_queues
);
1882 q
->flush_rq
= kzalloc(round_up(sizeof(struct request
) +
1883 set
->cmd_size
, cache_line_size()),
1888 if (blk_mq_init_hw_queues(q
, set
))
1891 mutex_lock(&all_q_mutex
);
1892 list_add_tail(&q
->all_q_node
, &all_q_list
);
1893 mutex_unlock(&all_q_mutex
);
1895 blk_mq_add_queue_tag_set(set
, q
);
1897 blk_mq_map_swqueue(q
);
1904 blk_cleanup_queue(q
);
1907 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
1910 free_cpumask_var(hctxs
[i
]->cpumask
);
1911 set
->ops
->free_hctx(hctxs
[i
], i
);
1917 return ERR_PTR(-ENOMEM
);
1919 EXPORT_SYMBOL(blk_mq_init_queue
);
1921 void blk_mq_free_queue(struct request_queue
*q
)
1923 struct blk_mq_tag_set
*set
= q
->tag_set
;
1925 blk_mq_del_queue_tag_set(q
);
1927 blk_mq_exit_hw_queues(q
, set
, set
->nr_hw_queues
);
1928 blk_mq_free_hw_queues(q
, set
);
1930 percpu_counter_destroy(&q
->mq_usage_counter
);
1932 free_percpu(q
->queue_ctx
);
1933 kfree(q
->queue_hw_ctx
);
1936 q
->queue_ctx
= NULL
;
1937 q
->queue_hw_ctx
= NULL
;
1940 mutex_lock(&all_q_mutex
);
1941 list_del_init(&q
->all_q_node
);
1942 mutex_unlock(&all_q_mutex
);
1945 /* Basically redo blk_mq_init_queue with queue frozen */
1946 static void blk_mq_queue_reinit(struct request_queue
*q
)
1948 blk_mq_freeze_queue(q
);
1950 blk_mq_update_queue_map(q
->mq_map
, q
->nr_hw_queues
);
1953 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1954 * we should change hctx numa_node according to new topology (this
1955 * involves free and re-allocate memory, worthy doing?)
1958 blk_mq_map_swqueue(q
);
1960 blk_mq_unfreeze_queue(q
);
1963 static int blk_mq_queue_reinit_notify(struct notifier_block
*nb
,
1964 unsigned long action
, void *hcpu
)
1966 struct request_queue
*q
;
1969 * Before new mappings are established, hotadded cpu might already
1970 * start handling requests. This doesn't break anything as we map
1971 * offline CPUs to first hardware queue. We will re-init the queue
1972 * below to get optimal settings.
1974 if (action
!= CPU_DEAD
&& action
!= CPU_DEAD_FROZEN
&&
1975 action
!= CPU_ONLINE
&& action
!= CPU_ONLINE_FROZEN
)
1978 mutex_lock(&all_q_mutex
);
1979 list_for_each_entry(q
, &all_q_list
, all_q_node
)
1980 blk_mq_queue_reinit(q
);
1981 mutex_unlock(&all_q_mutex
);
1985 int blk_mq_alloc_tag_set(struct blk_mq_tag_set
*set
)
1989 if (!set
->nr_hw_queues
)
1991 if (!set
->queue_depth
|| set
->queue_depth
> BLK_MQ_MAX_DEPTH
)
1993 if (set
->queue_depth
< set
->reserved_tags
+ BLK_MQ_TAG_MIN
)
1996 if (!set
->nr_hw_queues
||
1997 !set
->ops
->queue_rq
|| !set
->ops
->map_queue
||
1998 !set
->ops
->alloc_hctx
|| !set
->ops
->free_hctx
)
2002 set
->tags
= kmalloc_node(set
->nr_hw_queues
*
2003 sizeof(struct blk_mq_tags
*),
2004 GFP_KERNEL
, set
->numa_node
);
2008 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
2009 set
->tags
[i
] = blk_mq_init_rq_map(set
, i
);
2014 mutex_init(&set
->tag_list_lock
);
2015 INIT_LIST_HEAD(&set
->tag_list
);
2021 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
2025 EXPORT_SYMBOL(blk_mq_alloc_tag_set
);
2027 void blk_mq_free_tag_set(struct blk_mq_tag_set
*set
)
2031 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
2033 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
2038 EXPORT_SYMBOL(blk_mq_free_tag_set
);
2040 int blk_mq_update_nr_requests(struct request_queue
*q
, unsigned int nr
)
2042 struct blk_mq_tag_set
*set
= q
->tag_set
;
2043 struct blk_mq_hw_ctx
*hctx
;
2046 if (!set
|| nr
> set
->queue_depth
)
2050 queue_for_each_hw_ctx(q
, hctx
, i
) {
2051 ret
= blk_mq_tag_update_depth(hctx
->tags
, nr
);
2057 q
->nr_requests
= nr
;
2062 void blk_mq_disable_hotplug(void)
2064 mutex_lock(&all_q_mutex
);
2067 void blk_mq_enable_hotplug(void)
2069 mutex_unlock(&all_q_mutex
);
2072 static int __init
blk_mq_init(void)
2076 /* Must be called after percpu_counter_hotcpu_callback() */
2077 hotcpu_notifier(blk_mq_queue_reinit_notify
, -10);
2081 subsys_initcall(blk_mq_init
);