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>
12 #include <linux/kmemleak.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/workqueue.h>
17 #include <linux/smp.h>
18 #include <linux/llist.h>
19 #include <linux/list_sort.h>
20 #include <linux/cpu.h>
21 #include <linux/cache.h>
22 #include <linux/sched/sysctl.h>
23 #include <linux/delay.h>
24 #include <linux/crash_dump.h>
25 #include <linux/prefetch.h>
27 #include <trace/events/block.h>
29 #include <linux/blk-mq.h>
32 #include "blk-mq-tag.h"
34 static DEFINE_MUTEX(all_q_mutex
);
35 static LIST_HEAD(all_q_list
);
37 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
);
40 * Check if any of the ctx's have pending work in this hardware queue
42 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx
*hctx
)
46 for (i
= 0; i
< hctx
->ctx_map
.size
; i
++)
47 if (hctx
->ctx_map
.map
[i
].word
)
53 static inline struct blk_align_bitmap
*get_bm(struct blk_mq_hw_ctx
*hctx
,
54 struct blk_mq_ctx
*ctx
)
56 return &hctx
->ctx_map
.map
[ctx
->index_hw
/ hctx
->ctx_map
.bits_per_word
];
59 #define CTX_TO_BIT(hctx, ctx) \
60 ((ctx)->index_hw & ((hctx)->ctx_map.bits_per_word - 1))
63 * Mark this ctx as having pending work in this hardware queue
65 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx
*hctx
,
66 struct blk_mq_ctx
*ctx
)
68 struct blk_align_bitmap
*bm
= get_bm(hctx
, ctx
);
70 if (!test_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
))
71 set_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
);
74 static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx
*hctx
,
75 struct blk_mq_ctx
*ctx
)
77 struct blk_align_bitmap
*bm
= get_bm(hctx
, ctx
);
79 clear_bit(CTX_TO_BIT(hctx
, ctx
), &bm
->word
);
82 void blk_mq_freeze_queue_start(struct request_queue
*q
)
86 freeze_depth
= atomic_inc_return(&q
->mq_freeze_depth
);
87 if (freeze_depth
== 1) {
88 percpu_ref_kill(&q
->q_usage_counter
);
89 blk_mq_run_hw_queues(q
, false);
92 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_start
);
94 static void blk_mq_freeze_queue_wait(struct request_queue
*q
)
96 wait_event(q
->mq_freeze_wq
, percpu_ref_is_zero(&q
->q_usage_counter
));
100 * Guarantee no request is in use, so we can change any data structure of
101 * the queue afterward.
103 void blk_freeze_queue(struct request_queue
*q
)
106 * In the !blk_mq case we are only calling this to kill the
107 * q_usage_counter, otherwise this increases the freeze depth
108 * and waits for it to return to zero. For this reason there is
109 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
110 * exported to drivers as the only user for unfreeze is blk_mq.
112 blk_mq_freeze_queue_start(q
);
113 blk_mq_freeze_queue_wait(q
);
116 void blk_mq_freeze_queue(struct request_queue
*q
)
119 * ...just an alias to keep freeze and unfreeze actions balanced
120 * in the blk_mq_* namespace
124 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue
);
126 void blk_mq_unfreeze_queue(struct request_queue
*q
)
130 freeze_depth
= atomic_dec_return(&q
->mq_freeze_depth
);
131 WARN_ON_ONCE(freeze_depth
< 0);
133 percpu_ref_reinit(&q
->q_usage_counter
);
134 wake_up_all(&q
->mq_freeze_wq
);
137 EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue
);
139 void blk_mq_wake_waiters(struct request_queue
*q
)
141 struct blk_mq_hw_ctx
*hctx
;
144 queue_for_each_hw_ctx(q
, hctx
, i
)
145 if (blk_mq_hw_queue_mapped(hctx
))
146 blk_mq_tag_wakeup_all(hctx
->tags
, true);
149 * If we are called because the queue has now been marked as
150 * dying, we need to ensure that processes currently waiting on
151 * the queue are notified as well.
153 wake_up_all(&q
->mq_freeze_wq
);
156 bool blk_mq_can_queue(struct blk_mq_hw_ctx
*hctx
)
158 return blk_mq_has_free_tags(hctx
->tags
);
160 EXPORT_SYMBOL(blk_mq_can_queue
);
162 static void blk_mq_rq_ctx_init(struct request_queue
*q
, struct blk_mq_ctx
*ctx
,
163 struct request
*rq
, int op
,
164 unsigned int op_flags
)
166 if (blk_queue_io_stat(q
))
167 op_flags
|= REQ_IO_STAT
;
169 INIT_LIST_HEAD(&rq
->queuelist
);
170 /* csd/requeue_work/fifo_time is initialized before use */
173 req_set_op_attrs(rq
, op
, op_flags
);
174 /* do not touch atomic flags, it needs atomic ops against the timer */
176 INIT_HLIST_NODE(&rq
->hash
);
177 RB_CLEAR_NODE(&rq
->rb_node
);
180 rq
->start_time
= jiffies
;
181 #ifdef CONFIG_BLK_CGROUP
183 set_start_time_ns(rq
);
184 rq
->io_start_time_ns
= 0;
186 rq
->nr_phys_segments
= 0;
187 #if defined(CONFIG_BLK_DEV_INTEGRITY)
188 rq
->nr_integrity_segments
= 0;
191 /* tag was already set */
201 INIT_LIST_HEAD(&rq
->timeout_list
);
205 rq
->end_io_data
= NULL
;
208 ctx
->rq_dispatched
[rw_is_sync(op
, op_flags
)]++;
211 static struct request
*
212 __blk_mq_alloc_request(struct blk_mq_alloc_data
*data
, int op
, int op_flags
)
217 tag
= blk_mq_get_tag(data
);
218 if (tag
!= BLK_MQ_TAG_FAIL
) {
219 rq
= data
->hctx
->tags
->rqs
[tag
];
221 if (blk_mq_tag_busy(data
->hctx
)) {
222 rq
->cmd_flags
= REQ_MQ_INFLIGHT
;
223 atomic_inc(&data
->hctx
->nr_active
);
227 blk_mq_rq_ctx_init(data
->q
, data
->ctx
, rq
, op
, op_flags
);
234 struct request
*blk_mq_alloc_request(struct request_queue
*q
, int rw
,
237 struct blk_mq_ctx
*ctx
;
238 struct blk_mq_hw_ctx
*hctx
;
240 struct blk_mq_alloc_data alloc_data
;
243 ret
= blk_queue_enter(q
, flags
& BLK_MQ_REQ_NOWAIT
);
247 ctx
= blk_mq_get_ctx(q
);
248 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
249 blk_mq_set_alloc_data(&alloc_data
, q
, flags
, ctx
, hctx
);
251 rq
= __blk_mq_alloc_request(&alloc_data
, rw
, 0);
252 if (!rq
&& !(flags
& BLK_MQ_REQ_NOWAIT
)) {
253 __blk_mq_run_hw_queue(hctx
);
256 ctx
= blk_mq_get_ctx(q
);
257 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
258 blk_mq_set_alloc_data(&alloc_data
, q
, flags
, ctx
, hctx
);
259 rq
= __blk_mq_alloc_request(&alloc_data
, rw
, 0);
260 ctx
= alloc_data
.ctx
;
265 return ERR_PTR(-EWOULDBLOCK
);
269 rq
->__sector
= (sector_t
) -1;
270 rq
->bio
= rq
->biotail
= NULL
;
273 EXPORT_SYMBOL(blk_mq_alloc_request
);
275 struct request
*blk_mq_alloc_request_hctx(struct request_queue
*q
, int rw
,
276 unsigned int flags
, unsigned int hctx_idx
)
278 struct blk_mq_hw_ctx
*hctx
;
279 struct blk_mq_ctx
*ctx
;
281 struct blk_mq_alloc_data alloc_data
;
285 * If the tag allocator sleeps we could get an allocation for a
286 * different hardware context. No need to complicate the low level
287 * allocator for this for the rare use case of a command tied to
290 if (WARN_ON_ONCE(!(flags
& BLK_MQ_REQ_NOWAIT
)))
291 return ERR_PTR(-EINVAL
);
293 if (hctx_idx
>= q
->nr_hw_queues
)
294 return ERR_PTR(-EIO
);
296 ret
= blk_queue_enter(q
, true);
300 hctx
= q
->queue_hw_ctx
[hctx_idx
];
301 ctx
= __blk_mq_get_ctx(q
, cpumask_first(hctx
->cpumask
));
303 blk_mq_set_alloc_data(&alloc_data
, q
, flags
, ctx
, hctx
);
304 rq
= __blk_mq_alloc_request(&alloc_data
, rw
, 0);
307 return ERR_PTR(-EWOULDBLOCK
);
312 EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx
);
314 static void __blk_mq_free_request(struct blk_mq_hw_ctx
*hctx
,
315 struct blk_mq_ctx
*ctx
, struct request
*rq
)
317 const int tag
= rq
->tag
;
318 struct request_queue
*q
= rq
->q
;
320 if (rq
->cmd_flags
& REQ_MQ_INFLIGHT
)
321 atomic_dec(&hctx
->nr_active
);
324 clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
325 blk_mq_put_tag(hctx
, tag
, &ctx
->last_tag
);
329 void blk_mq_free_hctx_request(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
)
331 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
333 ctx
->rq_completed
[rq_is_sync(rq
)]++;
334 __blk_mq_free_request(hctx
, ctx
, rq
);
337 EXPORT_SYMBOL_GPL(blk_mq_free_hctx_request
);
339 void blk_mq_free_request(struct request
*rq
)
341 struct blk_mq_hw_ctx
*hctx
;
342 struct request_queue
*q
= rq
->q
;
344 hctx
= q
->mq_ops
->map_queue(q
, rq
->mq_ctx
->cpu
);
345 blk_mq_free_hctx_request(hctx
, rq
);
347 EXPORT_SYMBOL_GPL(blk_mq_free_request
);
349 inline void __blk_mq_end_request(struct request
*rq
, int error
)
351 blk_account_io_done(rq
);
354 rq
->end_io(rq
, error
);
356 if (unlikely(blk_bidi_rq(rq
)))
357 blk_mq_free_request(rq
->next_rq
);
358 blk_mq_free_request(rq
);
361 EXPORT_SYMBOL(__blk_mq_end_request
);
363 void blk_mq_end_request(struct request
*rq
, int error
)
365 if (blk_update_request(rq
, error
, blk_rq_bytes(rq
)))
367 __blk_mq_end_request(rq
, error
);
369 EXPORT_SYMBOL(blk_mq_end_request
);
371 static void __blk_mq_complete_request_remote(void *data
)
373 struct request
*rq
= data
;
375 rq
->q
->softirq_done_fn(rq
);
378 static void blk_mq_ipi_complete_request(struct request
*rq
)
380 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
384 if (!test_bit(QUEUE_FLAG_SAME_COMP
, &rq
->q
->queue_flags
)) {
385 rq
->q
->softirq_done_fn(rq
);
390 if (!test_bit(QUEUE_FLAG_SAME_FORCE
, &rq
->q
->queue_flags
))
391 shared
= cpus_share_cache(cpu
, ctx
->cpu
);
393 if (cpu
!= ctx
->cpu
&& !shared
&& cpu_online(ctx
->cpu
)) {
394 rq
->csd
.func
= __blk_mq_complete_request_remote
;
397 smp_call_function_single_async(ctx
->cpu
, &rq
->csd
);
399 rq
->q
->softirq_done_fn(rq
);
404 static void __blk_mq_complete_request(struct request
*rq
)
406 struct request_queue
*q
= rq
->q
;
408 if (!q
->softirq_done_fn
)
409 blk_mq_end_request(rq
, rq
->errors
);
411 blk_mq_ipi_complete_request(rq
);
415 * blk_mq_complete_request - end I/O on a request
416 * @rq: the request being processed
419 * Ends all I/O on a request. It does not handle partial completions.
420 * The actual completion happens out-of-order, through a IPI handler.
422 void blk_mq_complete_request(struct request
*rq
, int error
)
424 struct request_queue
*q
= rq
->q
;
426 if (unlikely(blk_should_fake_timeout(q
)))
428 if (!blk_mark_rq_complete(rq
)) {
430 __blk_mq_complete_request(rq
);
433 EXPORT_SYMBOL(blk_mq_complete_request
);
435 int blk_mq_request_started(struct request
*rq
)
437 return test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
439 EXPORT_SYMBOL_GPL(blk_mq_request_started
);
441 void blk_mq_start_request(struct request
*rq
)
443 struct request_queue
*q
= rq
->q
;
445 trace_block_rq_issue(q
, rq
);
447 rq
->resid_len
= blk_rq_bytes(rq
);
448 if (unlikely(blk_bidi_rq(rq
)))
449 rq
->next_rq
->resid_len
= blk_rq_bytes(rq
->next_rq
);
454 * Ensure that ->deadline is visible before set the started
455 * flag and clear the completed flag.
457 smp_mb__before_atomic();
460 * Mark us as started and clear complete. Complete might have been
461 * set if requeue raced with timeout, which then marked it as
462 * complete. So be sure to clear complete again when we start
463 * the request, otherwise we'll ignore the completion event.
465 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
))
466 set_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
);
467 if (test_bit(REQ_ATOM_COMPLETE
, &rq
->atomic_flags
))
468 clear_bit(REQ_ATOM_COMPLETE
, &rq
->atomic_flags
);
470 if (q
->dma_drain_size
&& blk_rq_bytes(rq
)) {
472 * Make sure space for the drain appears. We know we can do
473 * this because max_hw_segments has been adjusted to be one
474 * fewer than the device can handle.
476 rq
->nr_phys_segments
++;
479 EXPORT_SYMBOL(blk_mq_start_request
);
481 static void __blk_mq_requeue_request(struct request
*rq
)
483 struct request_queue
*q
= rq
->q
;
485 trace_block_rq_requeue(q
, rq
);
487 if (test_and_clear_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
)) {
488 if (q
->dma_drain_size
&& blk_rq_bytes(rq
))
489 rq
->nr_phys_segments
--;
493 void blk_mq_requeue_request(struct request
*rq
)
495 __blk_mq_requeue_request(rq
);
497 BUG_ON(blk_queued_rq(rq
));
498 blk_mq_add_to_requeue_list(rq
, true);
500 EXPORT_SYMBOL(blk_mq_requeue_request
);
502 static void blk_mq_requeue_work(struct work_struct
*work
)
504 struct request_queue
*q
=
505 container_of(work
, struct request_queue
, requeue_work
.work
);
507 struct request
*rq
, *next
;
510 spin_lock_irqsave(&q
->requeue_lock
, flags
);
511 list_splice_init(&q
->requeue_list
, &rq_list
);
512 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
514 list_for_each_entry_safe(rq
, next
, &rq_list
, queuelist
) {
515 if (!(rq
->cmd_flags
& REQ_SOFTBARRIER
))
518 rq
->cmd_flags
&= ~REQ_SOFTBARRIER
;
519 list_del_init(&rq
->queuelist
);
520 blk_mq_insert_request(rq
, true, false, false);
523 while (!list_empty(&rq_list
)) {
524 rq
= list_entry(rq_list
.next
, struct request
, queuelist
);
525 list_del_init(&rq
->queuelist
);
526 blk_mq_insert_request(rq
, false, false, false);
530 * Use the start variant of queue running here, so that running
531 * the requeue work will kick stopped queues.
533 blk_mq_start_hw_queues(q
);
536 void blk_mq_add_to_requeue_list(struct request
*rq
, bool at_head
)
538 struct request_queue
*q
= rq
->q
;
542 * We abuse this flag that is otherwise used by the I/O scheduler to
543 * request head insertation from the workqueue.
545 BUG_ON(rq
->cmd_flags
& REQ_SOFTBARRIER
);
547 spin_lock_irqsave(&q
->requeue_lock
, flags
);
549 rq
->cmd_flags
|= REQ_SOFTBARRIER
;
550 list_add(&rq
->queuelist
, &q
->requeue_list
);
552 list_add_tail(&rq
->queuelist
, &q
->requeue_list
);
554 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
556 EXPORT_SYMBOL(blk_mq_add_to_requeue_list
);
558 void blk_mq_cancel_requeue_work(struct request_queue
*q
)
560 cancel_delayed_work_sync(&q
->requeue_work
);
562 EXPORT_SYMBOL_GPL(blk_mq_cancel_requeue_work
);
564 void blk_mq_kick_requeue_list(struct request_queue
*q
)
566 kblockd_schedule_delayed_work(&q
->requeue_work
, 0);
568 EXPORT_SYMBOL(blk_mq_kick_requeue_list
);
570 void blk_mq_delay_kick_requeue_list(struct request_queue
*q
,
573 kblockd_schedule_delayed_work(&q
->requeue_work
,
574 msecs_to_jiffies(msecs
));
576 EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list
);
578 void blk_mq_abort_requeue_list(struct request_queue
*q
)
583 spin_lock_irqsave(&q
->requeue_lock
, flags
);
584 list_splice_init(&q
->requeue_list
, &rq_list
);
585 spin_unlock_irqrestore(&q
->requeue_lock
, flags
);
587 while (!list_empty(&rq_list
)) {
590 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
591 list_del_init(&rq
->queuelist
);
593 blk_mq_end_request(rq
, rq
->errors
);
596 EXPORT_SYMBOL(blk_mq_abort_requeue_list
);
598 struct request
*blk_mq_tag_to_rq(struct blk_mq_tags
*tags
, unsigned int tag
)
600 if (tag
< tags
->nr_tags
) {
601 prefetch(tags
->rqs
[tag
]);
602 return tags
->rqs
[tag
];
607 EXPORT_SYMBOL(blk_mq_tag_to_rq
);
609 struct blk_mq_timeout_data
{
611 unsigned int next_set
;
614 void blk_mq_rq_timed_out(struct request
*req
, bool reserved
)
616 struct blk_mq_ops
*ops
= req
->q
->mq_ops
;
617 enum blk_eh_timer_return ret
= BLK_EH_RESET_TIMER
;
620 * We know that complete is set at this point. If STARTED isn't set
621 * anymore, then the request isn't active and the "timeout" should
622 * just be ignored. This can happen due to the bitflag ordering.
623 * Timeout first checks if STARTED is set, and if it is, assumes
624 * the request is active. But if we race with completion, then
625 * we both flags will get cleared. So check here again, and ignore
626 * a timeout event with a request that isn't active.
628 if (!test_bit(REQ_ATOM_STARTED
, &req
->atomic_flags
))
632 ret
= ops
->timeout(req
, reserved
);
636 __blk_mq_complete_request(req
);
638 case BLK_EH_RESET_TIMER
:
640 blk_clear_rq_complete(req
);
642 case BLK_EH_NOT_HANDLED
:
645 printk(KERN_ERR
"block: bad eh return: %d\n", ret
);
650 static void blk_mq_check_expired(struct blk_mq_hw_ctx
*hctx
,
651 struct request
*rq
, void *priv
, bool reserved
)
653 struct blk_mq_timeout_data
*data
= priv
;
655 if (!test_bit(REQ_ATOM_STARTED
, &rq
->atomic_flags
)) {
657 * If a request wasn't started before the queue was
658 * marked dying, kill it here or it'll go unnoticed.
660 if (unlikely(blk_queue_dying(rq
->q
))) {
662 blk_mq_end_request(rq
, rq
->errors
);
667 if (time_after_eq(jiffies
, rq
->deadline
)) {
668 if (!blk_mark_rq_complete(rq
))
669 blk_mq_rq_timed_out(rq
, reserved
);
670 } else if (!data
->next_set
|| time_after(data
->next
, rq
->deadline
)) {
671 data
->next
= rq
->deadline
;
676 static void blk_mq_timeout_work(struct work_struct
*work
)
678 struct request_queue
*q
=
679 container_of(work
, struct request_queue
, timeout_work
);
680 struct blk_mq_timeout_data data
= {
686 /* A deadlock might occur if a request is stuck requiring a
687 * timeout at the same time a queue freeze is waiting
688 * completion, since the timeout code would not be able to
689 * acquire the queue reference here.
691 * That's why we don't use blk_queue_enter here; instead, we use
692 * percpu_ref_tryget directly, because we need to be able to
693 * obtain a reference even in the short window between the queue
694 * starting to freeze, by dropping the first reference in
695 * blk_mq_freeze_queue_start, and the moment the last request is
696 * consumed, marked by the instant q_usage_counter reaches
699 if (!percpu_ref_tryget(&q
->q_usage_counter
))
702 blk_mq_queue_tag_busy_iter(q
, blk_mq_check_expired
, &data
);
705 data
.next
= blk_rq_timeout(round_jiffies_up(data
.next
));
706 mod_timer(&q
->timeout
, data
.next
);
708 struct blk_mq_hw_ctx
*hctx
;
710 queue_for_each_hw_ctx(q
, hctx
, i
) {
711 /* the hctx may be unmapped, so check it here */
712 if (blk_mq_hw_queue_mapped(hctx
))
713 blk_mq_tag_idle(hctx
);
720 * Reverse check our software queue for entries that we could potentially
721 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
722 * too much time checking for merges.
724 static bool blk_mq_attempt_merge(struct request_queue
*q
,
725 struct blk_mq_ctx
*ctx
, struct bio
*bio
)
730 list_for_each_entry_reverse(rq
, &ctx
->rq_list
, queuelist
) {
736 if (!blk_rq_merge_ok(rq
, bio
))
739 el_ret
= blk_try_merge(rq
, bio
);
740 if (el_ret
== ELEVATOR_BACK_MERGE
) {
741 if (bio_attempt_back_merge(q
, rq
, bio
)) {
746 } else if (el_ret
== ELEVATOR_FRONT_MERGE
) {
747 if (bio_attempt_front_merge(q
, rq
, bio
)) {
759 * Process software queues that have been marked busy, splicing them
760 * to the for-dispatch
762 static void flush_busy_ctxs(struct blk_mq_hw_ctx
*hctx
, struct list_head
*list
)
764 struct blk_mq_ctx
*ctx
;
767 for (i
= 0; i
< hctx
->ctx_map
.size
; i
++) {
768 struct blk_align_bitmap
*bm
= &hctx
->ctx_map
.map
[i
];
769 unsigned int off
, bit
;
775 off
= i
* hctx
->ctx_map
.bits_per_word
;
777 bit
= find_next_bit(&bm
->word
, bm
->depth
, bit
);
778 if (bit
>= bm
->depth
)
781 ctx
= hctx
->ctxs
[bit
+ off
];
782 clear_bit(bit
, &bm
->word
);
783 spin_lock(&ctx
->lock
);
784 list_splice_tail_init(&ctx
->rq_list
, list
);
785 spin_unlock(&ctx
->lock
);
793 * Run this hardware queue, pulling any software queues mapped to it in.
794 * Note that this function currently has various problems around ordering
795 * of IO. In particular, we'd like FIFO behaviour on handling existing
796 * items on the hctx->dispatch list. Ignore that for now.
798 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
)
800 struct request_queue
*q
= hctx
->queue
;
803 LIST_HEAD(driver_list
);
804 struct list_head
*dptr
;
807 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
)))
810 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx
->cpumask
) &&
811 cpu_online(hctx
->next_cpu
));
816 * Touch any software queue that has pending entries.
818 flush_busy_ctxs(hctx
, &rq_list
);
821 * If we have previous entries on our dispatch list, grab them
822 * and stuff them at the front for more fair dispatch.
824 if (!list_empty_careful(&hctx
->dispatch
)) {
825 spin_lock(&hctx
->lock
);
826 if (!list_empty(&hctx
->dispatch
))
827 list_splice_init(&hctx
->dispatch
, &rq_list
);
828 spin_unlock(&hctx
->lock
);
832 * Start off with dptr being NULL, so we start the first request
833 * immediately, even if we have more pending.
838 * Now process all the entries, sending them to the driver.
841 while (!list_empty(&rq_list
)) {
842 struct blk_mq_queue_data bd
;
845 rq
= list_first_entry(&rq_list
, struct request
, queuelist
);
846 list_del_init(&rq
->queuelist
);
850 bd
.last
= list_empty(&rq_list
);
852 ret
= q
->mq_ops
->queue_rq(hctx
, &bd
);
854 case BLK_MQ_RQ_QUEUE_OK
:
857 case BLK_MQ_RQ_QUEUE_BUSY
:
858 list_add(&rq
->queuelist
, &rq_list
);
859 __blk_mq_requeue_request(rq
);
862 pr_err("blk-mq: bad return on queue: %d\n", ret
);
863 case BLK_MQ_RQ_QUEUE_ERROR
:
865 blk_mq_end_request(rq
, rq
->errors
);
869 if (ret
== BLK_MQ_RQ_QUEUE_BUSY
)
873 * We've done the first request. If we have more than 1
874 * left in the list, set dptr to defer issue.
876 if (!dptr
&& rq_list
.next
!= rq_list
.prev
)
881 hctx
->dispatched
[0]++;
882 else if (queued
< (1 << (BLK_MQ_MAX_DISPATCH_ORDER
- 1)))
883 hctx
->dispatched
[ilog2(queued
) + 1]++;
886 * Any items that need requeuing? Stuff them into hctx->dispatch,
887 * that is where we will continue on next queue run.
889 if (!list_empty(&rq_list
)) {
890 spin_lock(&hctx
->lock
);
891 list_splice(&rq_list
, &hctx
->dispatch
);
892 spin_unlock(&hctx
->lock
);
894 * the queue is expected stopped with BLK_MQ_RQ_QUEUE_BUSY, but
895 * it's possible the queue is stopped and restarted again
896 * before this. Queue restart will dispatch requests. And since
897 * requests in rq_list aren't added into hctx->dispatch yet,
898 * the requests in rq_list might get lost.
900 * blk_mq_run_hw_queue() already checks the STOPPED bit
902 blk_mq_run_hw_queue(hctx
, true);
907 * It'd be great if the workqueue API had a way to pass
908 * in a mask and had some smarts for more clever placement.
909 * For now we just round-robin here, switching for every
910 * BLK_MQ_CPU_WORK_BATCH queued items.
912 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx
*hctx
)
914 if (hctx
->queue
->nr_hw_queues
== 1)
915 return WORK_CPU_UNBOUND
;
917 if (--hctx
->next_cpu_batch
<= 0) {
918 int cpu
= hctx
->next_cpu
, next_cpu
;
920 next_cpu
= cpumask_next(hctx
->next_cpu
, hctx
->cpumask
);
921 if (next_cpu
>= nr_cpu_ids
)
922 next_cpu
= cpumask_first(hctx
->cpumask
);
924 hctx
->next_cpu
= next_cpu
;
925 hctx
->next_cpu_batch
= BLK_MQ_CPU_WORK_BATCH
;
930 return hctx
->next_cpu
;
933 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx
*hctx
, bool async
)
935 if (unlikely(test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
) ||
936 !blk_mq_hw_queue_mapped(hctx
)))
941 if (cpumask_test_cpu(cpu
, hctx
->cpumask
)) {
942 __blk_mq_run_hw_queue(hctx
);
950 kblockd_schedule_work_on(blk_mq_hctx_next_cpu(hctx
), &hctx
->run_work
);
953 void blk_mq_run_hw_queues(struct request_queue
*q
, bool async
)
955 struct blk_mq_hw_ctx
*hctx
;
958 queue_for_each_hw_ctx(q
, hctx
, i
) {
959 if ((!blk_mq_hctx_has_pending(hctx
) &&
960 list_empty_careful(&hctx
->dispatch
)) ||
961 test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
964 blk_mq_run_hw_queue(hctx
, async
);
967 EXPORT_SYMBOL(blk_mq_run_hw_queues
);
969 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx
*hctx
)
971 cancel_work(&hctx
->run_work
);
972 cancel_delayed_work(&hctx
->delay_work
);
973 set_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
975 EXPORT_SYMBOL(blk_mq_stop_hw_queue
);
977 void blk_mq_stop_hw_queues(struct request_queue
*q
)
979 struct blk_mq_hw_ctx
*hctx
;
982 queue_for_each_hw_ctx(q
, hctx
, i
)
983 blk_mq_stop_hw_queue(hctx
);
985 EXPORT_SYMBOL(blk_mq_stop_hw_queues
);
987 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx
*hctx
)
989 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
991 blk_mq_run_hw_queue(hctx
, false);
993 EXPORT_SYMBOL(blk_mq_start_hw_queue
);
995 void blk_mq_start_hw_queues(struct request_queue
*q
)
997 struct blk_mq_hw_ctx
*hctx
;
1000 queue_for_each_hw_ctx(q
, hctx
, i
)
1001 blk_mq_start_hw_queue(hctx
);
1003 EXPORT_SYMBOL(blk_mq_start_hw_queues
);
1005 void blk_mq_start_stopped_hw_queues(struct request_queue
*q
, bool async
)
1007 struct blk_mq_hw_ctx
*hctx
;
1010 queue_for_each_hw_ctx(q
, hctx
, i
) {
1011 if (!test_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
1014 clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
);
1015 blk_mq_run_hw_queue(hctx
, async
);
1018 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues
);
1020 static void blk_mq_run_work_fn(struct work_struct
*work
)
1022 struct blk_mq_hw_ctx
*hctx
;
1024 hctx
= container_of(work
, struct blk_mq_hw_ctx
, run_work
);
1026 __blk_mq_run_hw_queue(hctx
);
1029 static void blk_mq_delay_work_fn(struct work_struct
*work
)
1031 struct blk_mq_hw_ctx
*hctx
;
1033 hctx
= container_of(work
, struct blk_mq_hw_ctx
, delay_work
.work
);
1035 if (test_and_clear_bit(BLK_MQ_S_STOPPED
, &hctx
->state
))
1036 __blk_mq_run_hw_queue(hctx
);
1039 void blk_mq_delay_queue(struct blk_mq_hw_ctx
*hctx
, unsigned long msecs
)
1041 if (unlikely(!blk_mq_hw_queue_mapped(hctx
)))
1044 kblockd_schedule_delayed_work_on(blk_mq_hctx_next_cpu(hctx
),
1045 &hctx
->delay_work
, msecs_to_jiffies(msecs
));
1047 EXPORT_SYMBOL(blk_mq_delay_queue
);
1049 static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx
*hctx
,
1053 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
1055 trace_block_rq_insert(hctx
->queue
, rq
);
1058 list_add(&rq
->queuelist
, &ctx
->rq_list
);
1060 list_add_tail(&rq
->queuelist
, &ctx
->rq_list
);
1063 static void __blk_mq_insert_request(struct blk_mq_hw_ctx
*hctx
,
1064 struct request
*rq
, bool at_head
)
1066 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
1068 __blk_mq_insert_req_list(hctx
, rq
, at_head
);
1069 blk_mq_hctx_mark_pending(hctx
, ctx
);
1072 void blk_mq_insert_request(struct request
*rq
, bool at_head
, bool run_queue
,
1075 struct blk_mq_ctx
*ctx
= rq
->mq_ctx
;
1076 struct request_queue
*q
= rq
->q
;
1077 struct blk_mq_hw_ctx
*hctx
;
1079 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1081 spin_lock(&ctx
->lock
);
1082 __blk_mq_insert_request(hctx
, rq
, at_head
);
1083 spin_unlock(&ctx
->lock
);
1086 blk_mq_run_hw_queue(hctx
, async
);
1089 static void blk_mq_insert_requests(struct request_queue
*q
,
1090 struct blk_mq_ctx
*ctx
,
1091 struct list_head
*list
,
1096 struct blk_mq_hw_ctx
*hctx
;
1098 trace_block_unplug(q
, depth
, !from_schedule
);
1100 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1103 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1106 spin_lock(&ctx
->lock
);
1107 while (!list_empty(list
)) {
1110 rq
= list_first_entry(list
, struct request
, queuelist
);
1111 BUG_ON(rq
->mq_ctx
!= ctx
);
1112 list_del_init(&rq
->queuelist
);
1113 __blk_mq_insert_req_list(hctx
, rq
, false);
1115 blk_mq_hctx_mark_pending(hctx
, ctx
);
1116 spin_unlock(&ctx
->lock
);
1118 blk_mq_run_hw_queue(hctx
, from_schedule
);
1121 static int plug_ctx_cmp(void *priv
, struct list_head
*a
, struct list_head
*b
)
1123 struct request
*rqa
= container_of(a
, struct request
, queuelist
);
1124 struct request
*rqb
= container_of(b
, struct request
, queuelist
);
1126 return !(rqa
->mq_ctx
< rqb
->mq_ctx
||
1127 (rqa
->mq_ctx
== rqb
->mq_ctx
&&
1128 blk_rq_pos(rqa
) < blk_rq_pos(rqb
)));
1131 void blk_mq_flush_plug_list(struct blk_plug
*plug
, bool from_schedule
)
1133 struct blk_mq_ctx
*this_ctx
;
1134 struct request_queue
*this_q
;
1137 LIST_HEAD(ctx_list
);
1140 list_splice_init(&plug
->mq_list
, &list
);
1142 list_sort(NULL
, &list
, plug_ctx_cmp
);
1148 while (!list_empty(&list
)) {
1149 rq
= list_entry_rq(list
.next
);
1150 list_del_init(&rq
->queuelist
);
1152 if (rq
->mq_ctx
!= this_ctx
) {
1154 blk_mq_insert_requests(this_q
, this_ctx
,
1159 this_ctx
= rq
->mq_ctx
;
1165 list_add_tail(&rq
->queuelist
, &ctx_list
);
1169 * If 'this_ctx' is set, we know we have entries to complete
1170 * on 'ctx_list'. Do those.
1173 blk_mq_insert_requests(this_q
, this_ctx
, &ctx_list
, depth
,
1178 static void blk_mq_bio_to_request(struct request
*rq
, struct bio
*bio
)
1180 init_request_from_bio(rq
, bio
);
1182 blk_account_io_start(rq
, 1);
1185 static inline bool hctx_allow_merges(struct blk_mq_hw_ctx
*hctx
)
1187 return (hctx
->flags
& BLK_MQ_F_SHOULD_MERGE
) &&
1188 !blk_queue_nomerges(hctx
->queue
);
1191 static inline bool blk_mq_merge_queue_io(struct blk_mq_hw_ctx
*hctx
,
1192 struct blk_mq_ctx
*ctx
,
1193 struct request
*rq
, struct bio
*bio
)
1195 if (!hctx_allow_merges(hctx
) || !bio_mergeable(bio
)) {
1196 blk_mq_bio_to_request(rq
, bio
);
1197 spin_lock(&ctx
->lock
);
1199 __blk_mq_insert_request(hctx
, rq
, false);
1200 spin_unlock(&ctx
->lock
);
1203 struct request_queue
*q
= hctx
->queue
;
1205 spin_lock(&ctx
->lock
);
1206 if (!blk_mq_attempt_merge(q
, ctx
, bio
)) {
1207 blk_mq_bio_to_request(rq
, bio
);
1211 spin_unlock(&ctx
->lock
);
1212 __blk_mq_free_request(hctx
, ctx
, rq
);
1217 struct blk_map_ctx
{
1218 struct blk_mq_hw_ctx
*hctx
;
1219 struct blk_mq_ctx
*ctx
;
1222 static struct request
*blk_mq_map_request(struct request_queue
*q
,
1224 struct blk_map_ctx
*data
)
1226 struct blk_mq_hw_ctx
*hctx
;
1227 struct blk_mq_ctx
*ctx
;
1229 int op
= bio_data_dir(bio
);
1231 struct blk_mq_alloc_data alloc_data
;
1233 blk_queue_enter_live(q
);
1234 ctx
= blk_mq_get_ctx(q
);
1235 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1237 if (rw_is_sync(bio_op(bio
), bio
->bi_opf
))
1238 op_flags
|= REQ_SYNC
;
1240 trace_block_getrq(q
, bio
, op
);
1241 blk_mq_set_alloc_data(&alloc_data
, q
, BLK_MQ_REQ_NOWAIT
, ctx
, hctx
);
1242 rq
= __blk_mq_alloc_request(&alloc_data
, op
, op_flags
);
1243 if (unlikely(!rq
)) {
1244 __blk_mq_run_hw_queue(hctx
);
1245 blk_mq_put_ctx(ctx
);
1246 trace_block_sleeprq(q
, bio
, op
);
1248 ctx
= blk_mq_get_ctx(q
);
1249 hctx
= q
->mq_ops
->map_queue(q
, ctx
->cpu
);
1250 blk_mq_set_alloc_data(&alloc_data
, q
, 0, ctx
, hctx
);
1251 rq
= __blk_mq_alloc_request(&alloc_data
, op
, op_flags
);
1252 ctx
= alloc_data
.ctx
;
1253 hctx
= alloc_data
.hctx
;
1262 static int blk_mq_direct_issue_request(struct request
*rq
, blk_qc_t
*cookie
)
1265 struct request_queue
*q
= rq
->q
;
1266 struct blk_mq_hw_ctx
*hctx
= q
->mq_ops
->map_queue(q
,
1268 struct blk_mq_queue_data bd
= {
1273 blk_qc_t new_cookie
= blk_tag_to_qc_t(rq
->tag
, hctx
->queue_num
);
1276 * For OK queue, we are done. For error, kill it. Any other
1277 * error (busy), just add it to our list as we previously
1280 ret
= q
->mq_ops
->queue_rq(hctx
, &bd
);
1281 if (ret
== BLK_MQ_RQ_QUEUE_OK
) {
1282 *cookie
= new_cookie
;
1286 __blk_mq_requeue_request(rq
);
1288 if (ret
== BLK_MQ_RQ_QUEUE_ERROR
) {
1289 *cookie
= BLK_QC_T_NONE
;
1291 blk_mq_end_request(rq
, rq
->errors
);
1299 * Multiple hardware queue variant. This will not use per-process plugs,
1300 * but will attempt to bypass the hctx queueing if we can go straight to
1301 * hardware for SYNC IO.
1303 static blk_qc_t
blk_mq_make_request(struct request_queue
*q
, struct bio
*bio
)
1305 const int is_sync
= rw_is_sync(bio_op(bio
), bio
->bi_opf
);
1306 const int is_flush_fua
= bio
->bi_opf
& (REQ_PREFLUSH
| REQ_FUA
);
1307 struct blk_map_ctx data
;
1309 unsigned int request_count
= 0;
1310 struct blk_plug
*plug
;
1311 struct request
*same_queue_rq
= NULL
;
1314 blk_queue_bounce(q
, &bio
);
1316 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1318 return BLK_QC_T_NONE
;
1321 blk_queue_split(q
, &bio
, q
->bio_split
);
1323 if (!is_flush_fua
&& !blk_queue_nomerges(q
) &&
1324 blk_attempt_plug_merge(q
, bio
, &request_count
, &same_queue_rq
))
1325 return BLK_QC_T_NONE
;
1327 rq
= blk_mq_map_request(q
, bio
, &data
);
1329 return BLK_QC_T_NONE
;
1331 cookie
= blk_tag_to_qc_t(rq
->tag
, data
.hctx
->queue_num
);
1333 if (unlikely(is_flush_fua
)) {
1334 blk_mq_bio_to_request(rq
, bio
);
1335 blk_insert_flush(rq
);
1339 plug
= current
->plug
;
1341 * If the driver supports defer issued based on 'last', then
1342 * queue it up like normal since we can potentially save some
1345 if (((plug
&& !blk_queue_nomerges(q
)) || is_sync
) &&
1346 !(data
.hctx
->flags
& BLK_MQ_F_DEFER_ISSUE
)) {
1347 struct request
*old_rq
= NULL
;
1349 blk_mq_bio_to_request(rq
, bio
);
1352 * We do limited pluging. If the bio can be merged, do that.
1353 * Otherwise the existing request in the plug list will be
1354 * issued. So the plug list will have one request at most
1358 * The plug list might get flushed before this. If that
1359 * happens, same_queue_rq is invalid and plug list is
1362 if (same_queue_rq
&& !list_empty(&plug
->mq_list
)) {
1363 old_rq
= same_queue_rq
;
1364 list_del_init(&old_rq
->queuelist
);
1366 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
1367 } else /* is_sync */
1369 blk_mq_put_ctx(data
.ctx
);
1372 if (!blk_mq_direct_issue_request(old_rq
, &cookie
))
1374 blk_mq_insert_request(old_rq
, false, true, true);
1378 if (!blk_mq_merge_queue_io(data
.hctx
, data
.ctx
, rq
, bio
)) {
1380 * For a SYNC request, send it to the hardware immediately. For
1381 * an ASYNC request, just ensure that we run it later on. The
1382 * latter allows for merging opportunities and more efficient
1386 blk_mq_run_hw_queue(data
.hctx
, !is_sync
|| is_flush_fua
);
1388 blk_mq_put_ctx(data
.ctx
);
1394 * Single hardware queue variant. This will attempt to use any per-process
1395 * plug for merging and IO deferral.
1397 static blk_qc_t
blk_sq_make_request(struct request_queue
*q
, struct bio
*bio
)
1399 const int is_sync
= rw_is_sync(bio_op(bio
), bio
->bi_opf
);
1400 const int is_flush_fua
= bio
->bi_opf
& (REQ_PREFLUSH
| REQ_FUA
);
1401 struct blk_plug
*plug
;
1402 unsigned int request_count
= 0;
1403 struct blk_map_ctx data
;
1407 blk_queue_bounce(q
, &bio
);
1409 if (bio_integrity_enabled(bio
) && bio_integrity_prep(bio
)) {
1411 return BLK_QC_T_NONE
;
1414 blk_queue_split(q
, &bio
, q
->bio_split
);
1416 if (!is_flush_fua
&& !blk_queue_nomerges(q
)) {
1417 if (blk_attempt_plug_merge(q
, bio
, &request_count
, NULL
))
1418 return BLK_QC_T_NONE
;
1420 request_count
= blk_plug_queued_count(q
);
1422 rq
= blk_mq_map_request(q
, bio
, &data
);
1424 return BLK_QC_T_NONE
;
1426 cookie
= blk_tag_to_qc_t(rq
->tag
, data
.hctx
->queue_num
);
1428 if (unlikely(is_flush_fua
)) {
1429 blk_mq_bio_to_request(rq
, bio
);
1430 blk_insert_flush(rq
);
1435 * A task plug currently exists. Since this is completely lockless,
1436 * utilize that to temporarily store requests until the task is
1437 * either done or scheduled away.
1439 plug
= current
->plug
;
1441 blk_mq_bio_to_request(rq
, bio
);
1443 trace_block_plug(q
);
1445 blk_mq_put_ctx(data
.ctx
);
1447 if (request_count
>= BLK_MAX_REQUEST_COUNT
) {
1448 blk_flush_plug_list(plug
, false);
1449 trace_block_plug(q
);
1452 list_add_tail(&rq
->queuelist
, &plug
->mq_list
);
1456 if (!blk_mq_merge_queue_io(data
.hctx
, data
.ctx
, rq
, bio
)) {
1458 * For a SYNC request, send it to the hardware immediately. For
1459 * an ASYNC request, just ensure that we run it later on. The
1460 * latter allows for merging opportunities and more efficient
1464 blk_mq_run_hw_queue(data
.hctx
, !is_sync
|| is_flush_fua
);
1467 blk_mq_put_ctx(data
.ctx
);
1472 * Default mapping to a software queue, since we use one per CPU.
1474 struct blk_mq_hw_ctx
*blk_mq_map_queue(struct request_queue
*q
, const int cpu
)
1476 return q
->queue_hw_ctx
[q
->mq_map
[cpu
]];
1478 EXPORT_SYMBOL(blk_mq_map_queue
);
1480 static void blk_mq_free_rq_map(struct blk_mq_tag_set
*set
,
1481 struct blk_mq_tags
*tags
, unsigned int hctx_idx
)
1485 if (tags
->rqs
&& set
->ops
->exit_request
) {
1488 for (i
= 0; i
< tags
->nr_tags
; i
++) {
1491 set
->ops
->exit_request(set
->driver_data
, tags
->rqs
[i
],
1493 tags
->rqs
[i
] = NULL
;
1497 while (!list_empty(&tags
->page_list
)) {
1498 page
= list_first_entry(&tags
->page_list
, struct page
, lru
);
1499 list_del_init(&page
->lru
);
1501 * Remove kmemleak object previously allocated in
1502 * blk_mq_init_rq_map().
1504 kmemleak_free(page_address(page
));
1505 __free_pages(page
, page
->private);
1510 blk_mq_free_tags(tags
);
1513 static size_t order_to_size(unsigned int order
)
1515 return (size_t)PAGE_SIZE
<< order
;
1518 static struct blk_mq_tags
*blk_mq_init_rq_map(struct blk_mq_tag_set
*set
,
1519 unsigned int hctx_idx
)
1521 struct blk_mq_tags
*tags
;
1522 unsigned int i
, j
, entries_per_page
, max_order
= 4;
1523 size_t rq_size
, left
;
1525 tags
= blk_mq_init_tags(set
->queue_depth
, set
->reserved_tags
,
1527 BLK_MQ_FLAG_TO_ALLOC_POLICY(set
->flags
));
1531 INIT_LIST_HEAD(&tags
->page_list
);
1533 tags
->rqs
= kzalloc_node(set
->queue_depth
* sizeof(struct request
*),
1534 GFP_KERNEL
| __GFP_NOWARN
| __GFP_NORETRY
,
1537 blk_mq_free_tags(tags
);
1542 * rq_size is the size of the request plus driver payload, rounded
1543 * to the cacheline size
1545 rq_size
= round_up(sizeof(struct request
) + set
->cmd_size
,
1547 left
= rq_size
* set
->queue_depth
;
1549 for (i
= 0; i
< set
->queue_depth
; ) {
1550 int this_order
= max_order
;
1555 while (this_order
&& left
< order_to_size(this_order
- 1))
1559 page
= alloc_pages_node(set
->numa_node
,
1560 GFP_KERNEL
| __GFP_NOWARN
| __GFP_NORETRY
| __GFP_ZERO
,
1566 if (order_to_size(this_order
) < rq_size
)
1573 page
->private = this_order
;
1574 list_add_tail(&page
->lru
, &tags
->page_list
);
1576 p
= page_address(page
);
1578 * Allow kmemleak to scan these pages as they contain pointers
1579 * to additional allocations like via ops->init_request().
1581 kmemleak_alloc(p
, order_to_size(this_order
), 1, GFP_KERNEL
);
1582 entries_per_page
= order_to_size(this_order
) / rq_size
;
1583 to_do
= min(entries_per_page
, set
->queue_depth
- i
);
1584 left
-= to_do
* rq_size
;
1585 for (j
= 0; j
< to_do
; j
++) {
1587 if (set
->ops
->init_request
) {
1588 if (set
->ops
->init_request(set
->driver_data
,
1589 tags
->rqs
[i
], hctx_idx
, i
,
1591 tags
->rqs
[i
] = NULL
;
1603 blk_mq_free_rq_map(set
, tags
, hctx_idx
);
1607 static void blk_mq_free_bitmap(struct blk_mq_ctxmap
*bitmap
)
1612 static int blk_mq_alloc_bitmap(struct blk_mq_ctxmap
*bitmap
, int node
)
1614 unsigned int bpw
= 8, total
, num_maps
, i
;
1616 bitmap
->bits_per_word
= bpw
;
1618 num_maps
= ALIGN(nr_cpu_ids
, bpw
) / bpw
;
1619 bitmap
->map
= kzalloc_node(num_maps
* sizeof(struct blk_align_bitmap
),
1625 for (i
= 0; i
< num_maps
; i
++) {
1626 bitmap
->map
[i
].depth
= min(total
, bitmap
->bits_per_word
);
1627 total
-= bitmap
->map
[i
].depth
;
1634 * 'cpu' is going away. splice any existing rq_list entries from this
1635 * software queue to the hw queue dispatch list, and ensure that it
1638 static int blk_mq_hctx_cpu_offline(struct blk_mq_hw_ctx
*hctx
, int cpu
)
1640 struct blk_mq_ctx
*ctx
;
1643 ctx
= __blk_mq_get_ctx(hctx
->queue
, cpu
);
1645 spin_lock(&ctx
->lock
);
1646 if (!list_empty(&ctx
->rq_list
)) {
1647 list_splice_init(&ctx
->rq_list
, &tmp
);
1648 blk_mq_hctx_clear_pending(hctx
, ctx
);
1650 spin_unlock(&ctx
->lock
);
1652 if (list_empty(&tmp
))
1655 spin_lock(&hctx
->lock
);
1656 list_splice_tail_init(&tmp
, &hctx
->dispatch
);
1657 spin_unlock(&hctx
->lock
);
1659 blk_mq_run_hw_queue(hctx
, true);
1663 static int blk_mq_hctx_notify(void *data
, unsigned long action
,
1666 struct blk_mq_hw_ctx
*hctx
= data
;
1668 if (action
== CPU_DEAD
|| action
== CPU_DEAD_FROZEN
)
1669 return blk_mq_hctx_cpu_offline(hctx
, cpu
);
1672 * In case of CPU online, tags may be reallocated
1673 * in blk_mq_map_swqueue() after mapping is updated.
1679 /* hctx->ctxs will be freed in queue's release handler */
1680 static void blk_mq_exit_hctx(struct request_queue
*q
,
1681 struct blk_mq_tag_set
*set
,
1682 struct blk_mq_hw_ctx
*hctx
, unsigned int hctx_idx
)
1684 unsigned flush_start_tag
= set
->queue_depth
;
1686 blk_mq_tag_idle(hctx
);
1688 if (set
->ops
->exit_request
)
1689 set
->ops
->exit_request(set
->driver_data
,
1690 hctx
->fq
->flush_rq
, hctx_idx
,
1691 flush_start_tag
+ hctx_idx
);
1693 if (set
->ops
->exit_hctx
)
1694 set
->ops
->exit_hctx(hctx
, hctx_idx
);
1696 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1697 blk_free_flush_queue(hctx
->fq
);
1698 blk_mq_free_bitmap(&hctx
->ctx_map
);
1701 static void blk_mq_exit_hw_queues(struct request_queue
*q
,
1702 struct blk_mq_tag_set
*set
, int nr_queue
)
1704 struct blk_mq_hw_ctx
*hctx
;
1707 queue_for_each_hw_ctx(q
, hctx
, i
) {
1710 blk_mq_exit_hctx(q
, set
, hctx
, i
);
1714 static void blk_mq_free_hw_queues(struct request_queue
*q
,
1715 struct blk_mq_tag_set
*set
)
1717 struct blk_mq_hw_ctx
*hctx
;
1720 queue_for_each_hw_ctx(q
, hctx
, i
)
1721 free_cpumask_var(hctx
->cpumask
);
1724 static int blk_mq_init_hctx(struct request_queue
*q
,
1725 struct blk_mq_tag_set
*set
,
1726 struct blk_mq_hw_ctx
*hctx
, unsigned hctx_idx
)
1729 unsigned flush_start_tag
= set
->queue_depth
;
1731 node
= hctx
->numa_node
;
1732 if (node
== NUMA_NO_NODE
)
1733 node
= hctx
->numa_node
= set
->numa_node
;
1735 INIT_WORK(&hctx
->run_work
, blk_mq_run_work_fn
);
1736 INIT_DELAYED_WORK(&hctx
->delay_work
, blk_mq_delay_work_fn
);
1737 spin_lock_init(&hctx
->lock
);
1738 INIT_LIST_HEAD(&hctx
->dispatch
);
1740 hctx
->queue_num
= hctx_idx
;
1741 hctx
->flags
= set
->flags
& ~BLK_MQ_F_TAG_SHARED
;
1743 blk_mq_init_cpu_notifier(&hctx
->cpu_notifier
,
1744 blk_mq_hctx_notify
, hctx
);
1745 blk_mq_register_cpu_notifier(&hctx
->cpu_notifier
);
1747 hctx
->tags
= set
->tags
[hctx_idx
];
1750 * Allocate space for all possible cpus to avoid allocation at
1753 hctx
->ctxs
= kmalloc_node(nr_cpu_ids
* sizeof(void *),
1756 goto unregister_cpu_notifier
;
1758 if (blk_mq_alloc_bitmap(&hctx
->ctx_map
, node
))
1763 if (set
->ops
->init_hctx
&&
1764 set
->ops
->init_hctx(hctx
, set
->driver_data
, hctx_idx
))
1767 hctx
->fq
= blk_alloc_flush_queue(q
, hctx
->numa_node
, set
->cmd_size
);
1771 if (set
->ops
->init_request
&&
1772 set
->ops
->init_request(set
->driver_data
,
1773 hctx
->fq
->flush_rq
, hctx_idx
,
1774 flush_start_tag
+ hctx_idx
, node
))
1782 if (set
->ops
->exit_hctx
)
1783 set
->ops
->exit_hctx(hctx
, hctx_idx
);
1785 blk_mq_free_bitmap(&hctx
->ctx_map
);
1788 unregister_cpu_notifier
:
1789 blk_mq_unregister_cpu_notifier(&hctx
->cpu_notifier
);
1794 static void blk_mq_init_cpu_queues(struct request_queue
*q
,
1795 unsigned int nr_hw_queues
)
1799 for_each_possible_cpu(i
) {
1800 struct blk_mq_ctx
*__ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1801 struct blk_mq_hw_ctx
*hctx
;
1803 memset(__ctx
, 0, sizeof(*__ctx
));
1805 spin_lock_init(&__ctx
->lock
);
1806 INIT_LIST_HEAD(&__ctx
->rq_list
);
1809 /* If the cpu isn't online, the cpu is mapped to first hctx */
1813 hctx
= q
->mq_ops
->map_queue(q
, i
);
1816 * Set local node, IFF we have more than one hw queue. If
1817 * not, we remain on the home node of the device
1819 if (nr_hw_queues
> 1 && hctx
->numa_node
== NUMA_NO_NODE
)
1820 hctx
->numa_node
= local_memory_node(cpu_to_node(i
));
1824 static void blk_mq_map_swqueue(struct request_queue
*q
,
1825 const struct cpumask
*online_mask
)
1828 struct blk_mq_hw_ctx
*hctx
;
1829 struct blk_mq_ctx
*ctx
;
1830 struct blk_mq_tag_set
*set
= q
->tag_set
;
1833 * Avoid others reading imcomplete hctx->cpumask through sysfs
1835 mutex_lock(&q
->sysfs_lock
);
1837 queue_for_each_hw_ctx(q
, hctx
, i
) {
1838 cpumask_clear(hctx
->cpumask
);
1843 * Map software to hardware queues
1845 for_each_possible_cpu(i
) {
1846 /* If the cpu isn't online, the cpu is mapped to first hctx */
1847 if (!cpumask_test_cpu(i
, online_mask
))
1850 ctx
= per_cpu_ptr(q
->queue_ctx
, i
);
1851 hctx
= q
->mq_ops
->map_queue(q
, i
);
1853 cpumask_set_cpu(i
, hctx
->cpumask
);
1854 ctx
->index_hw
= hctx
->nr_ctx
;
1855 hctx
->ctxs
[hctx
->nr_ctx
++] = ctx
;
1858 mutex_unlock(&q
->sysfs_lock
);
1860 queue_for_each_hw_ctx(q
, hctx
, i
) {
1861 struct blk_mq_ctxmap
*map
= &hctx
->ctx_map
;
1864 * If no software queues are mapped to this hardware queue,
1865 * disable it and free the request entries.
1867 if (!hctx
->nr_ctx
) {
1869 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
1870 set
->tags
[i
] = NULL
;
1876 /* unmapped hw queue can be remapped after CPU topo changed */
1878 set
->tags
[i
] = blk_mq_init_rq_map(set
, i
);
1879 hctx
->tags
= set
->tags
[i
];
1880 WARN_ON(!hctx
->tags
);
1882 cpumask_copy(hctx
->tags
->cpumask
, hctx
->cpumask
);
1884 * Set the map size to the number of mapped software queues.
1885 * This is more accurate and more efficient than looping
1886 * over all possibly mapped software queues.
1888 map
->size
= DIV_ROUND_UP(hctx
->nr_ctx
, map
->bits_per_word
);
1891 * Initialize batch roundrobin counts
1893 hctx
->next_cpu
= cpumask_first(hctx
->cpumask
);
1894 hctx
->next_cpu_batch
= BLK_MQ_CPU_WORK_BATCH
;
1898 static void queue_set_hctx_shared(struct request_queue
*q
, bool shared
)
1900 struct blk_mq_hw_ctx
*hctx
;
1903 queue_for_each_hw_ctx(q
, hctx
, i
) {
1905 hctx
->flags
|= BLK_MQ_F_TAG_SHARED
;
1907 hctx
->flags
&= ~BLK_MQ_F_TAG_SHARED
;
1911 static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set
*set
, bool shared
)
1913 struct request_queue
*q
;
1915 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
) {
1916 blk_mq_freeze_queue(q
);
1917 queue_set_hctx_shared(q
, shared
);
1918 blk_mq_unfreeze_queue(q
);
1922 static void blk_mq_del_queue_tag_set(struct request_queue
*q
)
1924 struct blk_mq_tag_set
*set
= q
->tag_set
;
1926 mutex_lock(&set
->tag_list_lock
);
1927 list_del_init(&q
->tag_set_list
);
1928 if (list_is_singular(&set
->tag_list
)) {
1929 /* just transitioned to unshared */
1930 set
->flags
&= ~BLK_MQ_F_TAG_SHARED
;
1931 /* update existing queue */
1932 blk_mq_update_tag_set_depth(set
, false);
1934 mutex_unlock(&set
->tag_list_lock
);
1937 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set
*set
,
1938 struct request_queue
*q
)
1942 mutex_lock(&set
->tag_list_lock
);
1944 /* Check to see if we're transitioning to shared (from 1 to 2 queues). */
1945 if (!list_empty(&set
->tag_list
) && !(set
->flags
& BLK_MQ_F_TAG_SHARED
)) {
1946 set
->flags
|= BLK_MQ_F_TAG_SHARED
;
1947 /* update existing queue */
1948 blk_mq_update_tag_set_depth(set
, true);
1950 if (set
->flags
& BLK_MQ_F_TAG_SHARED
)
1951 queue_set_hctx_shared(q
, true);
1952 list_add_tail(&q
->tag_set_list
, &set
->tag_list
);
1954 mutex_unlock(&set
->tag_list_lock
);
1958 * It is the actual release handler for mq, but we do it from
1959 * request queue's release handler for avoiding use-after-free
1960 * and headache because q->mq_kobj shouldn't have been introduced,
1961 * but we can't group ctx/kctx kobj without it.
1963 void blk_mq_release(struct request_queue
*q
)
1965 struct blk_mq_hw_ctx
*hctx
;
1968 /* hctx kobj stays in hctx */
1969 queue_for_each_hw_ctx(q
, hctx
, i
) {
1979 kfree(q
->queue_hw_ctx
);
1981 /* ctx kobj stays in queue_ctx */
1982 free_percpu(q
->queue_ctx
);
1985 struct request_queue
*blk_mq_init_queue(struct blk_mq_tag_set
*set
)
1987 struct request_queue
*uninit_q
, *q
;
1989 uninit_q
= blk_alloc_queue_node(GFP_KERNEL
, set
->numa_node
);
1991 return ERR_PTR(-ENOMEM
);
1993 q
= blk_mq_init_allocated_queue(set
, uninit_q
);
1995 blk_cleanup_queue(uninit_q
);
1999 EXPORT_SYMBOL(blk_mq_init_queue
);
2001 static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set
*set
,
2002 struct request_queue
*q
)
2005 struct blk_mq_hw_ctx
**hctxs
= q
->queue_hw_ctx
;
2007 blk_mq_sysfs_unregister(q
);
2008 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
2014 node
= blk_mq_hw_queue_to_node(q
->mq_map
, i
);
2015 hctxs
[i
] = kzalloc_node(sizeof(struct blk_mq_hw_ctx
),
2020 if (!zalloc_cpumask_var_node(&hctxs
[i
]->cpumask
, GFP_KERNEL
,
2027 atomic_set(&hctxs
[i
]->nr_active
, 0);
2028 hctxs
[i
]->numa_node
= node
;
2029 hctxs
[i
]->queue_num
= i
;
2031 if (blk_mq_init_hctx(q
, set
, hctxs
[i
], i
)) {
2032 free_cpumask_var(hctxs
[i
]->cpumask
);
2037 blk_mq_hctx_kobj_init(hctxs
[i
]);
2039 for (j
= i
; j
< q
->nr_hw_queues
; j
++) {
2040 struct blk_mq_hw_ctx
*hctx
= hctxs
[j
];
2044 blk_mq_free_rq_map(set
, hctx
->tags
, j
);
2045 set
->tags
[j
] = NULL
;
2047 blk_mq_exit_hctx(q
, set
, hctx
, j
);
2048 free_cpumask_var(hctx
->cpumask
);
2049 kobject_put(&hctx
->kobj
);
2056 q
->nr_hw_queues
= i
;
2057 blk_mq_sysfs_register(q
);
2060 struct request_queue
*blk_mq_init_allocated_queue(struct blk_mq_tag_set
*set
,
2061 struct request_queue
*q
)
2063 /* mark the queue as mq asap */
2064 q
->mq_ops
= set
->ops
;
2066 q
->queue_ctx
= alloc_percpu(struct blk_mq_ctx
);
2070 q
->queue_hw_ctx
= kzalloc_node(nr_cpu_ids
* sizeof(*(q
->queue_hw_ctx
)),
2071 GFP_KERNEL
, set
->numa_node
);
2072 if (!q
->queue_hw_ctx
)
2075 q
->mq_map
= blk_mq_make_queue_map(set
);
2079 blk_mq_realloc_hw_ctxs(set
, q
);
2080 if (!q
->nr_hw_queues
)
2083 INIT_WORK(&q
->timeout_work
, blk_mq_timeout_work
);
2084 blk_queue_rq_timeout(q
, set
->timeout
? set
->timeout
: 30 * HZ
);
2086 q
->nr_queues
= nr_cpu_ids
;
2088 q
->queue_flags
|= QUEUE_FLAG_MQ_DEFAULT
;
2090 if (!(set
->flags
& BLK_MQ_F_SG_MERGE
))
2091 q
->queue_flags
|= 1 << QUEUE_FLAG_NO_SG_MERGE
;
2093 q
->sg_reserved_size
= INT_MAX
;
2095 INIT_DELAYED_WORK(&q
->requeue_work
, blk_mq_requeue_work
);
2096 INIT_LIST_HEAD(&q
->requeue_list
);
2097 spin_lock_init(&q
->requeue_lock
);
2099 if (q
->nr_hw_queues
> 1)
2100 blk_queue_make_request(q
, blk_mq_make_request
);
2102 blk_queue_make_request(q
, blk_sq_make_request
);
2105 * Do this after blk_queue_make_request() overrides it...
2107 q
->nr_requests
= set
->queue_depth
;
2109 if (set
->ops
->complete
)
2110 blk_queue_softirq_done(q
, set
->ops
->complete
);
2112 blk_mq_init_cpu_queues(q
, set
->nr_hw_queues
);
2115 mutex_lock(&all_q_mutex
);
2117 list_add_tail(&q
->all_q_node
, &all_q_list
);
2118 blk_mq_add_queue_tag_set(set
, q
);
2119 blk_mq_map_swqueue(q
, cpu_online_mask
);
2121 mutex_unlock(&all_q_mutex
);
2129 kfree(q
->queue_hw_ctx
);
2131 free_percpu(q
->queue_ctx
);
2134 return ERR_PTR(-ENOMEM
);
2136 EXPORT_SYMBOL(blk_mq_init_allocated_queue
);
2138 void blk_mq_free_queue(struct request_queue
*q
)
2140 struct blk_mq_tag_set
*set
= q
->tag_set
;
2142 mutex_lock(&all_q_mutex
);
2143 list_del_init(&q
->all_q_node
);
2144 mutex_unlock(&all_q_mutex
);
2146 blk_mq_del_queue_tag_set(q
);
2148 blk_mq_exit_hw_queues(q
, set
, set
->nr_hw_queues
);
2149 blk_mq_free_hw_queues(q
, set
);
2152 /* Basically redo blk_mq_init_queue with queue frozen */
2153 static void blk_mq_queue_reinit(struct request_queue
*q
,
2154 const struct cpumask
*online_mask
)
2156 WARN_ON_ONCE(!atomic_read(&q
->mq_freeze_depth
));
2158 blk_mq_sysfs_unregister(q
);
2160 blk_mq_update_queue_map(q
->mq_map
, q
->nr_hw_queues
, online_mask
);
2163 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
2164 * we should change hctx numa_node according to new topology (this
2165 * involves free and re-allocate memory, worthy doing?)
2168 blk_mq_map_swqueue(q
, online_mask
);
2170 blk_mq_sysfs_register(q
);
2173 static int blk_mq_queue_reinit_notify(struct notifier_block
*nb
,
2174 unsigned long action
, void *hcpu
)
2176 struct request_queue
*q
;
2177 int cpu
= (unsigned long)hcpu
;
2179 * New online cpumask which is going to be set in this hotplug event.
2180 * Declare this cpumasks as global as cpu-hotplug operation is invoked
2181 * one-by-one and dynamically allocating this could result in a failure.
2183 static struct cpumask online_new
;
2186 * Before hotadded cpu starts handling requests, new mappings must
2187 * be established. Otherwise, these requests in hw queue might
2188 * never be dispatched.
2190 * For example, there is a single hw queue (hctx) and two CPU queues
2191 * (ctx0 for CPU0, and ctx1 for CPU1).
2193 * Now CPU1 is just onlined and a request is inserted into
2194 * ctx1->rq_list and set bit0 in pending bitmap as ctx1->index_hw is
2197 * And then while running hw queue, flush_busy_ctxs() finds bit0 is
2198 * set in pending bitmap and tries to retrieve requests in
2199 * hctx->ctxs[0]->rq_list. But htx->ctxs[0] is a pointer to ctx0,
2200 * so the request in ctx1->rq_list is ignored.
2202 switch (action
& ~CPU_TASKS_FROZEN
) {
2204 case CPU_UP_CANCELED
:
2205 cpumask_copy(&online_new
, cpu_online_mask
);
2207 case CPU_UP_PREPARE
:
2208 cpumask_copy(&online_new
, cpu_online_mask
);
2209 cpumask_set_cpu(cpu
, &online_new
);
2215 mutex_lock(&all_q_mutex
);
2218 * We need to freeze and reinit all existing queues. Freezing
2219 * involves synchronous wait for an RCU grace period and doing it
2220 * one by one may take a long time. Start freezing all queues in
2221 * one swoop and then wait for the completions so that freezing can
2222 * take place in parallel.
2224 list_for_each_entry(q
, &all_q_list
, all_q_node
)
2225 blk_mq_freeze_queue_start(q
);
2226 list_for_each_entry(q
, &all_q_list
, all_q_node
) {
2227 blk_mq_freeze_queue_wait(q
);
2230 * timeout handler can't touch hw queue during the
2233 del_timer_sync(&q
->timeout
);
2236 list_for_each_entry(q
, &all_q_list
, all_q_node
)
2237 blk_mq_queue_reinit(q
, &online_new
);
2239 list_for_each_entry(q
, &all_q_list
, all_q_node
)
2240 blk_mq_unfreeze_queue(q
);
2242 mutex_unlock(&all_q_mutex
);
2246 static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set
*set
)
2250 for (i
= 0; i
< set
->nr_hw_queues
; i
++) {
2251 set
->tags
[i
] = blk_mq_init_rq_map(set
, i
);
2260 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
2266 * Allocate the request maps associated with this tag_set. Note that this
2267 * may reduce the depth asked for, if memory is tight. set->queue_depth
2268 * will be updated to reflect the allocated depth.
2270 static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set
*set
)
2275 depth
= set
->queue_depth
;
2277 err
= __blk_mq_alloc_rq_maps(set
);
2281 set
->queue_depth
>>= 1;
2282 if (set
->queue_depth
< set
->reserved_tags
+ BLK_MQ_TAG_MIN
) {
2286 } while (set
->queue_depth
);
2288 if (!set
->queue_depth
|| err
) {
2289 pr_err("blk-mq: failed to allocate request map\n");
2293 if (depth
!= set
->queue_depth
)
2294 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2295 depth
, set
->queue_depth
);
2300 struct cpumask
*blk_mq_tags_cpumask(struct blk_mq_tags
*tags
)
2302 return tags
->cpumask
;
2304 EXPORT_SYMBOL_GPL(blk_mq_tags_cpumask
);
2307 * Alloc a tag set to be associated with one or more request queues.
2308 * May fail with EINVAL for various error conditions. May adjust the
2309 * requested depth down, if if it too large. In that case, the set
2310 * value will be stored in set->queue_depth.
2312 int blk_mq_alloc_tag_set(struct blk_mq_tag_set
*set
)
2314 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH
> 1 << BLK_MQ_UNIQUE_TAG_BITS
);
2316 if (!set
->nr_hw_queues
)
2318 if (!set
->queue_depth
)
2320 if (set
->queue_depth
< set
->reserved_tags
+ BLK_MQ_TAG_MIN
)
2323 if (!set
->ops
->queue_rq
|| !set
->ops
->map_queue
)
2326 if (set
->queue_depth
> BLK_MQ_MAX_DEPTH
) {
2327 pr_info("blk-mq: reduced tag depth to %u\n",
2329 set
->queue_depth
= BLK_MQ_MAX_DEPTH
;
2333 * If a crashdump is active, then we are potentially in a very
2334 * memory constrained environment. Limit us to 1 queue and
2335 * 64 tags to prevent using too much memory.
2337 if (is_kdump_kernel()) {
2338 set
->nr_hw_queues
= 1;
2339 set
->queue_depth
= min(64U, set
->queue_depth
);
2342 * There is no use for more h/w queues than cpus.
2344 if (set
->nr_hw_queues
> nr_cpu_ids
)
2345 set
->nr_hw_queues
= nr_cpu_ids
;
2347 set
->tags
= kzalloc_node(nr_cpu_ids
* sizeof(struct blk_mq_tags
*),
2348 GFP_KERNEL
, set
->numa_node
);
2352 if (blk_mq_alloc_rq_maps(set
))
2355 mutex_init(&set
->tag_list_lock
);
2356 INIT_LIST_HEAD(&set
->tag_list
);
2364 EXPORT_SYMBOL(blk_mq_alloc_tag_set
);
2366 void blk_mq_free_tag_set(struct blk_mq_tag_set
*set
)
2370 for (i
= 0; i
< nr_cpu_ids
; i
++) {
2372 blk_mq_free_rq_map(set
, set
->tags
[i
], i
);
2378 EXPORT_SYMBOL(blk_mq_free_tag_set
);
2380 int blk_mq_update_nr_requests(struct request_queue
*q
, unsigned int nr
)
2382 struct blk_mq_tag_set
*set
= q
->tag_set
;
2383 struct blk_mq_hw_ctx
*hctx
;
2386 if (!set
|| nr
> set
->queue_depth
)
2390 queue_for_each_hw_ctx(q
, hctx
, i
) {
2393 ret
= blk_mq_tag_update_depth(hctx
->tags
, nr
);
2399 q
->nr_requests
= nr
;
2404 void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set
*set
, int nr_hw_queues
)
2406 struct request_queue
*q
;
2408 if (nr_hw_queues
> nr_cpu_ids
)
2409 nr_hw_queues
= nr_cpu_ids
;
2410 if (nr_hw_queues
< 1 || nr_hw_queues
== set
->nr_hw_queues
)
2413 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
)
2414 blk_mq_freeze_queue(q
);
2416 set
->nr_hw_queues
= nr_hw_queues
;
2417 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
) {
2418 blk_mq_realloc_hw_ctxs(set
, q
);
2420 if (q
->nr_hw_queues
> 1)
2421 blk_queue_make_request(q
, blk_mq_make_request
);
2423 blk_queue_make_request(q
, blk_sq_make_request
);
2425 blk_mq_queue_reinit(q
, cpu_online_mask
);
2428 list_for_each_entry(q
, &set
->tag_list
, tag_set_list
)
2429 blk_mq_unfreeze_queue(q
);
2431 EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues
);
2433 void blk_mq_disable_hotplug(void)
2435 mutex_lock(&all_q_mutex
);
2438 void blk_mq_enable_hotplug(void)
2440 mutex_unlock(&all_q_mutex
);
2443 static int __init
blk_mq_init(void)
2447 hotcpu_notifier(blk_mq_queue_reinit_notify
, 0);
2451 subsys_initcall(blk_mq_init
);