1 #include <linux/kernel.h>
2 #include <linux/module.h>
3 #include <linux/backing-dev.h>
5 #include <linux/blkdev.h>
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/workqueue.h>
10 #include <linux/smp.h>
11 #include <linux/llist.h>
12 #include <linux/list_sort.h>
13 #include <linux/cpu.h>
14 #include <linux/cache.h>
15 #include <linux/sched/sysctl.h>
16 #include <linux/delay.h>
18 #include <trace/events/block.h>
20 #include <linux/blk-mq.h>
23 #include "blk-mq-tag.h"
25 static DEFINE_MUTEX(all_q_mutex);
26 static LIST_HEAD(all_q_list);
28 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx);
30 static struct blk_mq_ctx *__blk_mq_get_ctx(struct request_queue *q,
33 return per_cpu_ptr(q->queue_ctx, cpu);
37 * This assumes per-cpu software queueing queues. They could be per-node
38 * as well, for instance. For now this is hardcoded as-is. Note that we don't
39 * care about preemption, since we know the ctx's are persistent. This does
40 * mean that we can't rely on ctx always matching the currently running CPU.
42 static struct blk_mq_ctx *blk_mq_get_ctx(struct request_queue *q)
44 return __blk_mq_get_ctx(q, get_cpu());
47 static void blk_mq_put_ctx(struct blk_mq_ctx *ctx)
53 * Check if any of the ctx's have pending work in this hardware queue
55 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
59 for (i = 0; i < hctx->nr_ctx_map; i++)
67 * Mark this ctx as having pending work in this hardware queue
69 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
70 struct blk_mq_ctx *ctx)
72 if (!test_bit(ctx->index_hw, hctx->ctx_map))
73 set_bit(ctx->index_hw, hctx->ctx_map);
76 static struct request *__blk_mq_alloc_request(struct blk_mq_hw_ctx *hctx,
77 gfp_t gfp, bool reserved)
82 tag = blk_mq_get_tag(hctx->tags, gfp, reserved);
83 if (tag != BLK_MQ_TAG_FAIL) {
84 rq = hctx->tags->rqs[tag];
85 blk_rq_init(hctx->queue, rq);
94 static int blk_mq_queue_enter(struct request_queue *q)
98 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
100 /* we have problems to freeze the queue if it's initializing */
101 if (!blk_queue_bypass(q) || !blk_queue_init_done(q))
104 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
106 spin_lock_irq(q->queue_lock);
107 ret = wait_event_interruptible_lock_irq(q->mq_freeze_wq,
108 !blk_queue_bypass(q) || blk_queue_dying(q),
110 /* inc usage with lock hold to avoid freeze_queue runs here */
111 if (!ret && !blk_queue_dying(q))
112 __percpu_counter_add(&q->mq_usage_counter, 1, 1000000);
113 else if (blk_queue_dying(q))
115 spin_unlock_irq(q->queue_lock);
120 static void blk_mq_queue_exit(struct request_queue *q)
122 __percpu_counter_add(&q->mq_usage_counter, -1, 1000000);
125 static void __blk_mq_drain_queue(struct request_queue *q)
130 spin_lock_irq(q->queue_lock);
131 count = percpu_counter_sum(&q->mq_usage_counter);
132 spin_unlock_irq(q->queue_lock);
136 blk_mq_run_queues(q, false);
142 * Guarantee no request is in use, so we can change any data structure of
143 * the queue afterward.
145 static void blk_mq_freeze_queue(struct request_queue *q)
149 spin_lock_irq(q->queue_lock);
150 drain = !q->bypass_depth++;
151 queue_flag_set(QUEUE_FLAG_BYPASS, q);
152 spin_unlock_irq(q->queue_lock);
155 __blk_mq_drain_queue(q);
158 void blk_mq_drain_queue(struct request_queue *q)
160 __blk_mq_drain_queue(q);
163 static void blk_mq_unfreeze_queue(struct request_queue *q)
167 spin_lock_irq(q->queue_lock);
168 if (!--q->bypass_depth) {
169 queue_flag_clear(QUEUE_FLAG_BYPASS, q);
172 WARN_ON_ONCE(q->bypass_depth < 0);
173 spin_unlock_irq(q->queue_lock);
175 wake_up_all(&q->mq_freeze_wq);
178 bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
180 return blk_mq_has_free_tags(hctx->tags);
182 EXPORT_SYMBOL(blk_mq_can_queue);
184 static void blk_mq_rq_ctx_init(struct request_queue *q, struct blk_mq_ctx *ctx,
185 struct request *rq, unsigned int rw_flags)
187 if (blk_queue_io_stat(q))
188 rw_flags |= REQ_IO_STAT;
191 rq->cmd_flags = rw_flags;
192 rq->start_time = jiffies;
193 set_start_time_ns(rq);
194 ctx->rq_dispatched[rw_is_sync(rw_flags)]++;
197 static struct request *blk_mq_alloc_request_pinned(struct request_queue *q,
204 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
205 struct blk_mq_hw_ctx *hctx = q->mq_ops->map_queue(q, ctx->cpu);
207 rq = __blk_mq_alloc_request(hctx, gfp & ~__GFP_WAIT, reserved);
209 blk_mq_rq_ctx_init(q, ctx, rq, rw);
213 if (gfp & __GFP_WAIT) {
214 __blk_mq_run_hw_queue(hctx);
221 blk_mq_wait_for_tags(hctx->tags);
227 struct request *blk_mq_alloc_request(struct request_queue *q, int rw, gfp_t gfp)
231 if (blk_mq_queue_enter(q))
234 rq = blk_mq_alloc_request_pinned(q, rw, gfp, false);
236 blk_mq_put_ctx(rq->mq_ctx);
240 struct request *blk_mq_alloc_reserved_request(struct request_queue *q, int rw,
245 if (blk_mq_queue_enter(q))
248 rq = blk_mq_alloc_request_pinned(q, rw, gfp, true);
250 blk_mq_put_ctx(rq->mq_ctx);
253 EXPORT_SYMBOL(blk_mq_alloc_reserved_request);
255 static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
256 struct blk_mq_ctx *ctx, struct request *rq)
258 const int tag = rq->tag;
259 struct request_queue *q = rq->q;
261 blk_mq_put_tag(hctx->tags, tag);
262 blk_mq_queue_exit(q);
265 void blk_mq_free_request(struct request *rq)
267 struct blk_mq_ctx *ctx = rq->mq_ctx;
268 struct blk_mq_hw_ctx *hctx;
269 struct request_queue *q = rq->q;
271 ctx->rq_completed[rq_is_sync(rq)]++;
273 hctx = q->mq_ops->map_queue(q, ctx->cpu);
274 __blk_mq_free_request(hctx, ctx, rq);
278 * Clone all relevant state from a request that has been put on hold in
279 * the flush state machine into the preallocated flush request that hangs
280 * off the request queue.
282 * For a driver the flush request should be invisible, that's why we are
283 * impersonating the original request here.
285 void blk_mq_clone_flush_request(struct request *flush_rq,
286 struct request *orig_rq)
288 struct blk_mq_hw_ctx *hctx =
289 orig_rq->q->mq_ops->map_queue(orig_rq->q, orig_rq->mq_ctx->cpu);
291 flush_rq->mq_ctx = orig_rq->mq_ctx;
292 flush_rq->tag = orig_rq->tag;
293 memcpy(blk_mq_rq_to_pdu(flush_rq), blk_mq_rq_to_pdu(orig_rq),
297 inline void __blk_mq_end_io(struct request *rq, int error)
299 blk_account_io_done(rq);
302 rq->end_io(rq, error);
304 if (unlikely(blk_bidi_rq(rq)))
305 blk_mq_free_request(rq->next_rq);
306 blk_mq_free_request(rq);
309 EXPORT_SYMBOL(__blk_mq_end_io);
311 void blk_mq_end_io(struct request *rq, int error)
313 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
315 __blk_mq_end_io(rq, error);
317 EXPORT_SYMBOL(blk_mq_end_io);
319 static void __blk_mq_complete_request_remote(void *data)
321 struct request *rq = data;
323 rq->q->softirq_done_fn(rq);
326 void __blk_mq_complete_request(struct request *rq)
328 struct blk_mq_ctx *ctx = rq->mq_ctx;
331 if (!ctx->ipi_redirect) {
332 rq->q->softirq_done_fn(rq);
337 if (cpu != ctx->cpu && cpu_online(ctx->cpu)) {
338 rq->csd.func = __blk_mq_complete_request_remote;
341 smp_call_function_single_async(ctx->cpu, &rq->csd);
343 rq->q->softirq_done_fn(rq);
349 * blk_mq_complete_request - end I/O on a request
350 * @rq: the request being processed
353 * Ends all I/O on a request. It does not handle partial completions.
354 * The actual completion happens out-of-order, through a IPI handler.
356 void blk_mq_complete_request(struct request *rq)
358 if (unlikely(blk_should_fake_timeout(rq->q)))
360 if (!blk_mark_rq_complete(rq))
361 __blk_mq_complete_request(rq);
363 EXPORT_SYMBOL(blk_mq_complete_request);
365 static void blk_mq_start_request(struct request *rq, bool last)
367 struct request_queue *q = rq->q;
369 trace_block_rq_issue(q, rq);
371 rq->resid_len = blk_rq_bytes(rq);
372 if (unlikely(blk_bidi_rq(rq)))
373 rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
376 * Just mark start time and set the started bit. Due to memory
377 * ordering, we know we'll see the correct deadline as long as
378 * REQ_ATOMIC_STARTED is seen.
380 rq->deadline = jiffies + q->rq_timeout;
381 set_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
383 if (q->dma_drain_size && blk_rq_bytes(rq)) {
385 * Make sure space for the drain appears. We know we can do
386 * this because max_hw_segments has been adjusted to be one
387 * fewer than the device can handle.
389 rq->nr_phys_segments++;
393 * Flag the last request in the series so that drivers know when IO
394 * should be kicked off, if they don't do it on a per-request basis.
396 * Note: the flag isn't the only condition drivers should do kick off.
397 * If drive is busy, the last request might not have the bit set.
400 rq->cmd_flags |= REQ_END;
403 static void __blk_mq_requeue_request(struct request *rq)
405 struct request_queue *q = rq->q;
407 trace_block_rq_requeue(q, rq);
408 clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
410 rq->cmd_flags &= ~REQ_END;
412 if (q->dma_drain_size && blk_rq_bytes(rq))
413 rq->nr_phys_segments--;
416 void blk_mq_requeue_request(struct request *rq)
418 struct request_queue *q = rq->q;
420 __blk_mq_requeue_request(rq);
421 blk_clear_rq_complete(rq);
423 trace_block_rq_requeue(q, rq);
425 BUG_ON(blk_queued_rq(rq));
426 blk_mq_insert_request(rq, true, true, false);
428 EXPORT_SYMBOL(blk_mq_requeue_request);
430 struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
432 return tags->rqs[tag];
434 EXPORT_SYMBOL(blk_mq_tag_to_rq);
436 struct blk_mq_timeout_data {
437 struct blk_mq_hw_ctx *hctx;
439 unsigned int *next_set;
442 static void blk_mq_timeout_check(void *__data, unsigned long *free_tags)
444 struct blk_mq_timeout_data *data = __data;
445 struct blk_mq_hw_ctx *hctx = data->hctx;
448 /* It may not be in flight yet (this is where
449 * the REQ_ATOMIC_STARTED flag comes in). The requests are
450 * statically allocated, so we know it's always safe to access the
451 * memory associated with a bit offset into ->rqs[].
457 tag = find_next_zero_bit(free_tags, hctx->tags->nr_tags, tag);
458 if (tag >= hctx->tags->nr_tags)
461 rq = blk_mq_tag_to_rq(hctx->tags, tag++);
462 if (rq->q != hctx->queue)
464 if (!test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
467 blk_rq_check_expired(rq, data->next, data->next_set);
471 static void blk_mq_hw_ctx_check_timeout(struct blk_mq_hw_ctx *hctx,
473 unsigned int *next_set)
475 struct blk_mq_timeout_data data = {
478 .next_set = next_set,
482 * Ask the tagging code to iterate busy requests, so we can
483 * check them for timeout.
485 blk_mq_tag_busy_iter(hctx->tags, blk_mq_timeout_check, &data);
488 static void blk_mq_rq_timer(unsigned long data)
490 struct request_queue *q = (struct request_queue *) data;
491 struct blk_mq_hw_ctx *hctx;
492 unsigned long next = 0;
495 queue_for_each_hw_ctx(q, hctx, i)
496 blk_mq_hw_ctx_check_timeout(hctx, &next, &next_set);
499 mod_timer(&q->timeout, round_jiffies_up(next));
503 * Reverse check our software queue for entries that we could potentially
504 * merge with. Currently includes a hand-wavy stop count of 8, to not spend
505 * too much time checking for merges.
507 static bool blk_mq_attempt_merge(struct request_queue *q,
508 struct blk_mq_ctx *ctx, struct bio *bio)
513 list_for_each_entry_reverse(rq, &ctx->rq_list, queuelist) {
519 if (!blk_rq_merge_ok(rq, bio))
522 el_ret = blk_try_merge(rq, bio);
523 if (el_ret == ELEVATOR_BACK_MERGE) {
524 if (bio_attempt_back_merge(q, rq, bio)) {
529 } else if (el_ret == ELEVATOR_FRONT_MERGE) {
530 if (bio_attempt_front_merge(q, rq, bio)) {
541 void blk_mq_add_timer(struct request *rq)
543 __blk_add_timer(rq, NULL);
547 * Run this hardware queue, pulling any software queues mapped to it in.
548 * Note that this function currently has various problems around ordering
549 * of IO. In particular, we'd like FIFO behaviour on handling existing
550 * items on the hctx->dispatch list. Ignore that for now.
552 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
554 struct request_queue *q = hctx->queue;
555 struct blk_mq_ctx *ctx;
560 WARN_ON(!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask));
562 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
568 * Touch any software queue that has pending entries.
570 for_each_set_bit(bit, hctx->ctx_map, hctx->nr_ctx) {
571 clear_bit(bit, hctx->ctx_map);
572 ctx = hctx->ctxs[bit];
573 BUG_ON(bit != ctx->index_hw);
575 spin_lock(&ctx->lock);
576 list_splice_tail_init(&ctx->rq_list, &rq_list);
577 spin_unlock(&ctx->lock);
581 * If we have previous entries on our dispatch list, grab them
582 * and stuff them at the front for more fair dispatch.
584 if (!list_empty_careful(&hctx->dispatch)) {
585 spin_lock(&hctx->lock);
586 if (!list_empty(&hctx->dispatch))
587 list_splice_init(&hctx->dispatch, &rq_list);
588 spin_unlock(&hctx->lock);
592 * Delete and return all entries from our dispatch list
597 * Now process all the entries, sending them to the driver.
599 while (!list_empty(&rq_list)) {
602 rq = list_first_entry(&rq_list, struct request, queuelist);
603 list_del_init(&rq->queuelist);
605 blk_mq_start_request(rq, list_empty(&rq_list));
607 ret = q->mq_ops->queue_rq(hctx, rq);
609 case BLK_MQ_RQ_QUEUE_OK:
612 case BLK_MQ_RQ_QUEUE_BUSY:
614 * FIXME: we should have a mechanism to stop the queue
615 * like blk_stop_queue, otherwise we will waste cpu
618 list_add(&rq->queuelist, &rq_list);
619 __blk_mq_requeue_request(rq);
622 pr_err("blk-mq: bad return on queue: %d\n", ret);
623 case BLK_MQ_RQ_QUEUE_ERROR:
625 blk_mq_end_io(rq, rq->errors);
629 if (ret == BLK_MQ_RQ_QUEUE_BUSY)
634 hctx->dispatched[0]++;
635 else if (queued < (1 << (BLK_MQ_MAX_DISPATCH_ORDER - 1)))
636 hctx->dispatched[ilog2(queued) + 1]++;
639 * Any items that need requeuing? Stuff them into hctx->dispatch,
640 * that is where we will continue on next queue run.
642 if (!list_empty(&rq_list)) {
643 spin_lock(&hctx->lock);
644 list_splice(&rq_list, &hctx->dispatch);
645 spin_unlock(&hctx->lock);
649 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
651 if (unlikely(test_bit(BLK_MQ_S_STOPPED, &hctx->state)))
654 if (!async && cpumask_test_cpu(smp_processor_id(), hctx->cpumask))
655 __blk_mq_run_hw_queue(hctx);
656 else if (hctx->queue->nr_hw_queues == 1)
657 kblockd_schedule_delayed_work(&hctx->run_work, 0);
662 * It'd be great if the workqueue API had a way to pass
663 * in a mask and had some smarts for more clever placement
664 * than the first CPU. Or we could round-robin here. For now,
665 * just queue on the first CPU.
667 cpu = cpumask_first(hctx->cpumask);
668 kblockd_schedule_delayed_work_on(cpu, &hctx->run_work, 0);
672 void blk_mq_run_queues(struct request_queue *q, bool async)
674 struct blk_mq_hw_ctx *hctx;
677 queue_for_each_hw_ctx(q, hctx, i) {
678 if ((!blk_mq_hctx_has_pending(hctx) &&
679 list_empty_careful(&hctx->dispatch)) ||
680 test_bit(BLK_MQ_S_STOPPED, &hctx->state))
684 blk_mq_run_hw_queue(hctx, async);
688 EXPORT_SYMBOL(blk_mq_run_queues);
690 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
692 cancel_delayed_work(&hctx->run_work);
693 cancel_delayed_work(&hctx->delay_work);
694 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
696 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
698 void blk_mq_stop_hw_queues(struct request_queue *q)
700 struct blk_mq_hw_ctx *hctx;
703 queue_for_each_hw_ctx(q, hctx, i)
704 blk_mq_stop_hw_queue(hctx);
706 EXPORT_SYMBOL(blk_mq_stop_hw_queues);
708 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
710 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
713 __blk_mq_run_hw_queue(hctx);
716 EXPORT_SYMBOL(blk_mq_start_hw_queue);
718 void blk_mq_start_hw_queues(struct request_queue *q)
720 struct blk_mq_hw_ctx *hctx;
723 queue_for_each_hw_ctx(q, hctx, i)
724 blk_mq_start_hw_queue(hctx);
726 EXPORT_SYMBOL(blk_mq_start_hw_queues);
729 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
731 struct blk_mq_hw_ctx *hctx;
734 queue_for_each_hw_ctx(q, hctx, i) {
735 if (!test_bit(BLK_MQ_S_STOPPED, &hctx->state))
738 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
740 blk_mq_run_hw_queue(hctx, async);
744 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
746 static void blk_mq_run_work_fn(struct work_struct *work)
748 struct blk_mq_hw_ctx *hctx;
750 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
752 __blk_mq_run_hw_queue(hctx);
755 static void blk_mq_delay_work_fn(struct work_struct *work)
757 struct blk_mq_hw_ctx *hctx;
759 hctx = container_of(work, struct blk_mq_hw_ctx, delay_work.work);
761 if (test_and_clear_bit(BLK_MQ_S_STOPPED, &hctx->state))
762 __blk_mq_run_hw_queue(hctx);
765 void blk_mq_delay_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
767 unsigned long tmo = msecs_to_jiffies(msecs);
769 if (hctx->queue->nr_hw_queues == 1)
770 kblockd_schedule_delayed_work(&hctx->delay_work, tmo);
775 * It'd be great if the workqueue API had a way to pass
776 * in a mask and had some smarts for more clever placement
777 * than the first CPU. Or we could round-robin here. For now,
778 * just queue on the first CPU.
780 cpu = cpumask_first(hctx->cpumask);
781 kblockd_schedule_delayed_work_on(cpu, &hctx->delay_work, tmo);
784 EXPORT_SYMBOL(blk_mq_delay_queue);
786 static void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx,
787 struct request *rq, bool at_head)
789 struct blk_mq_ctx *ctx = rq->mq_ctx;
791 trace_block_rq_insert(hctx->queue, rq);
794 list_add(&rq->queuelist, &ctx->rq_list);
796 list_add_tail(&rq->queuelist, &ctx->rq_list);
797 blk_mq_hctx_mark_pending(hctx, ctx);
800 * We do this early, to ensure we are on the right CPU.
802 blk_mq_add_timer(rq);
805 void blk_mq_insert_request(struct request *rq, bool at_head, bool run_queue,
808 struct request_queue *q = rq->q;
809 struct blk_mq_hw_ctx *hctx;
810 struct blk_mq_ctx *ctx = rq->mq_ctx, *current_ctx;
812 current_ctx = blk_mq_get_ctx(q);
813 if (!cpu_online(ctx->cpu))
814 rq->mq_ctx = ctx = current_ctx;
816 hctx = q->mq_ops->map_queue(q, ctx->cpu);
818 if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA) &&
819 !(rq->cmd_flags & (REQ_FLUSH_SEQ))) {
820 blk_insert_flush(rq);
822 spin_lock(&ctx->lock);
823 __blk_mq_insert_request(hctx, rq, at_head);
824 spin_unlock(&ctx->lock);
828 blk_mq_run_hw_queue(hctx, async);
830 blk_mq_put_ctx(current_ctx);
833 static void blk_mq_insert_requests(struct request_queue *q,
834 struct blk_mq_ctx *ctx,
835 struct list_head *list,
840 struct blk_mq_hw_ctx *hctx;
841 struct blk_mq_ctx *current_ctx;
843 trace_block_unplug(q, depth, !from_schedule);
845 current_ctx = blk_mq_get_ctx(q);
847 if (!cpu_online(ctx->cpu))
849 hctx = q->mq_ops->map_queue(q, ctx->cpu);
852 * preemption doesn't flush plug list, so it's possible ctx->cpu is
855 spin_lock(&ctx->lock);
856 while (!list_empty(list)) {
859 rq = list_first_entry(list, struct request, queuelist);
860 list_del_init(&rq->queuelist);
862 __blk_mq_insert_request(hctx, rq, false);
864 spin_unlock(&ctx->lock);
866 blk_mq_run_hw_queue(hctx, from_schedule);
867 blk_mq_put_ctx(current_ctx);
870 static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
872 struct request *rqa = container_of(a, struct request, queuelist);
873 struct request *rqb = container_of(b, struct request, queuelist);
875 return !(rqa->mq_ctx < rqb->mq_ctx ||
876 (rqa->mq_ctx == rqb->mq_ctx &&
877 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
880 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
882 struct blk_mq_ctx *this_ctx;
883 struct request_queue *this_q;
889 list_splice_init(&plug->mq_list, &list);
891 list_sort(NULL, &list, plug_ctx_cmp);
897 while (!list_empty(&list)) {
898 rq = list_entry_rq(list.next);
899 list_del_init(&rq->queuelist);
901 if (rq->mq_ctx != this_ctx) {
903 blk_mq_insert_requests(this_q, this_ctx,
908 this_ctx = rq->mq_ctx;
914 list_add_tail(&rq->queuelist, &ctx_list);
918 * If 'this_ctx' is set, we know we have entries to complete
919 * on 'ctx_list'. Do those.
922 blk_mq_insert_requests(this_q, this_ctx, &ctx_list, depth,
927 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
929 init_request_from_bio(rq, bio);
930 blk_account_io_start(rq, 1);
933 static void blk_mq_make_request(struct request_queue *q, struct bio *bio)
935 struct blk_mq_hw_ctx *hctx;
936 struct blk_mq_ctx *ctx;
937 const int is_sync = rw_is_sync(bio->bi_rw);
938 const int is_flush_fua = bio->bi_rw & (REQ_FLUSH | REQ_FUA);
939 int rw = bio_data_dir(bio);
941 unsigned int use_plug, request_count = 0;
944 * If we have multiple hardware queues, just go directly to
945 * one of those for sync IO.
947 use_plug = !is_flush_fua && ((q->nr_hw_queues == 1) || !is_sync);
949 blk_queue_bounce(q, &bio);
951 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
952 bio_endio(bio, -EIO);
956 if (use_plug && blk_attempt_plug_merge(q, bio, &request_count))
959 if (blk_mq_queue_enter(q)) {
960 bio_endio(bio, -EIO);
964 ctx = blk_mq_get_ctx(q);
965 hctx = q->mq_ops->map_queue(q, ctx->cpu);
969 trace_block_getrq(q, bio, rw);
970 rq = __blk_mq_alloc_request(hctx, GFP_ATOMIC, false);
972 blk_mq_rq_ctx_init(q, ctx, rq, rw);
975 trace_block_sleeprq(q, bio, rw);
976 rq = blk_mq_alloc_request_pinned(q, rw, __GFP_WAIT|GFP_ATOMIC,
979 hctx = q->mq_ops->map_queue(q, ctx->cpu);
984 if (unlikely(is_flush_fua)) {
985 blk_mq_bio_to_request(rq, bio);
986 blk_insert_flush(rq);
991 * A task plug currently exists. Since this is completely lockless,
992 * utilize that to temporarily store requests until the task is
993 * either done or scheduled away.
996 struct blk_plug *plug = current->plug;
999 blk_mq_bio_to_request(rq, bio);
1000 if (list_empty(&plug->mq_list))
1001 trace_block_plug(q);
1002 else if (request_count >= BLK_MAX_REQUEST_COUNT) {
1003 blk_flush_plug_list(plug, false);
1004 trace_block_plug(q);
1006 list_add_tail(&rq->queuelist, &plug->mq_list);
1007 blk_mq_put_ctx(ctx);
1012 spin_lock(&ctx->lock);
1014 if ((hctx->flags & BLK_MQ_F_SHOULD_MERGE) &&
1015 blk_mq_attempt_merge(q, ctx, bio))
1016 __blk_mq_free_request(hctx, ctx, rq);
1018 blk_mq_bio_to_request(rq, bio);
1019 __blk_mq_insert_request(hctx, rq, false);
1022 spin_unlock(&ctx->lock);
1025 * For a SYNC request, send it to the hardware immediately. For an
1026 * ASYNC request, just ensure that we run it later on. The latter
1027 * allows for merging opportunities and more efficient dispatching.
1030 blk_mq_run_hw_queue(hctx, !is_sync || is_flush_fua);
1031 blk_mq_put_ctx(ctx);
1035 * Default mapping to a software queue, since we use one per CPU.
1037 struct blk_mq_hw_ctx *blk_mq_map_queue(struct request_queue *q, const int cpu)
1039 return q->queue_hw_ctx[q->mq_map[cpu]];
1041 EXPORT_SYMBOL(blk_mq_map_queue);
1043 struct blk_mq_hw_ctx *blk_mq_alloc_single_hw_queue(struct blk_mq_tag_set *set,
1044 unsigned int hctx_index)
1046 return kmalloc_node(sizeof(struct blk_mq_hw_ctx),
1047 GFP_KERNEL | __GFP_ZERO, set->numa_node);
1049 EXPORT_SYMBOL(blk_mq_alloc_single_hw_queue);
1051 void blk_mq_free_single_hw_queue(struct blk_mq_hw_ctx *hctx,
1052 unsigned int hctx_index)
1056 EXPORT_SYMBOL(blk_mq_free_single_hw_queue);
1058 static void blk_mq_hctx_notify(void *data, unsigned long action,
1061 struct blk_mq_hw_ctx *hctx = data;
1062 struct request_queue *q = hctx->queue;
1063 struct blk_mq_ctx *ctx;
1066 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
1070 * Move ctx entries to new CPU, if this one is going away.
1072 ctx = __blk_mq_get_ctx(q, cpu);
1074 spin_lock(&ctx->lock);
1075 if (!list_empty(&ctx->rq_list)) {
1076 list_splice_init(&ctx->rq_list, &tmp);
1077 clear_bit(ctx->index_hw, hctx->ctx_map);
1079 spin_unlock(&ctx->lock);
1081 if (list_empty(&tmp))
1084 ctx = blk_mq_get_ctx(q);
1085 spin_lock(&ctx->lock);
1087 while (!list_empty(&tmp)) {
1090 rq = list_first_entry(&tmp, struct request, queuelist);
1092 list_move_tail(&rq->queuelist, &ctx->rq_list);
1095 hctx = q->mq_ops->map_queue(q, ctx->cpu);
1096 blk_mq_hctx_mark_pending(hctx, ctx);
1098 spin_unlock(&ctx->lock);
1100 blk_mq_run_hw_queue(hctx, true);
1101 blk_mq_put_ctx(ctx);
1104 static void blk_mq_free_rq_map(struct blk_mq_tag_set *set,
1105 struct blk_mq_tags *tags, unsigned int hctx_idx)
1109 if (tags->rqs && set->ops->exit_request) {
1112 for (i = 0; i < tags->nr_tags; i++) {
1115 set->ops->exit_request(set->driver_data, tags->rqs[i],
1120 while (!list_empty(&tags->page_list)) {
1121 page = list_first_entry(&tags->page_list, struct page, lru);
1122 list_del_init(&page->lru);
1123 __free_pages(page, page->private);
1128 blk_mq_free_tags(tags);
1131 static size_t order_to_size(unsigned int order)
1133 size_t ret = PAGE_SIZE;
1141 static struct blk_mq_tags *blk_mq_init_rq_map(struct blk_mq_tag_set *set,
1142 unsigned int hctx_idx)
1144 struct blk_mq_tags *tags;
1145 unsigned int i, j, entries_per_page, max_order = 4;
1146 size_t rq_size, left;
1148 tags = blk_mq_init_tags(set->queue_depth, set->reserved_tags,
1153 INIT_LIST_HEAD(&tags->page_list);
1155 tags->rqs = kmalloc_node(set->queue_depth * sizeof(struct request *),
1156 GFP_KERNEL, set->numa_node);
1158 blk_mq_free_tags(tags);
1163 * rq_size is the size of the request plus driver payload, rounded
1164 * to the cacheline size
1166 rq_size = round_up(sizeof(struct request) + set->cmd_size,
1168 left = rq_size * set->queue_depth;
1170 for (i = 0; i < set->queue_depth; ) {
1171 int this_order = max_order;
1176 while (left < order_to_size(this_order - 1) && this_order)
1180 page = alloc_pages_node(set->numa_node, GFP_KERNEL,
1186 if (order_to_size(this_order) < rq_size)
1193 page->private = this_order;
1194 list_add_tail(&page->lru, &tags->page_list);
1196 p = page_address(page);
1197 entries_per_page = order_to_size(this_order) / rq_size;
1198 to_do = min(entries_per_page, set->queue_depth - i);
1199 left -= to_do * rq_size;
1200 for (j = 0; j < to_do; j++) {
1202 if (set->ops->init_request) {
1203 if (set->ops->init_request(set->driver_data,
1204 tags->rqs[i], hctx_idx, i,
1217 pr_warn("%s: failed to allocate requests\n", __func__);
1218 blk_mq_free_rq_map(set, tags, hctx_idx);
1222 static int blk_mq_init_hw_queues(struct request_queue *q,
1223 struct blk_mq_tag_set *set)
1225 struct blk_mq_hw_ctx *hctx;
1229 * Initialize hardware queues
1231 queue_for_each_hw_ctx(q, hctx, i) {
1232 unsigned int num_maps;
1235 node = hctx->numa_node;
1236 if (node == NUMA_NO_NODE)
1237 node = hctx->numa_node = set->numa_node;
1239 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
1240 INIT_DELAYED_WORK(&hctx->delay_work, blk_mq_delay_work_fn);
1241 spin_lock_init(&hctx->lock);
1242 INIT_LIST_HEAD(&hctx->dispatch);
1244 hctx->queue_num = i;
1245 hctx->flags = set->flags;
1246 hctx->cmd_size = set->cmd_size;
1248 blk_mq_init_cpu_notifier(&hctx->cpu_notifier,
1249 blk_mq_hctx_notify, hctx);
1250 blk_mq_register_cpu_notifier(&hctx->cpu_notifier);
1252 hctx->tags = set->tags[i];
1255 * Allocate space for all possible cpus to avoid allocation in
1258 hctx->ctxs = kmalloc_node(nr_cpu_ids * sizeof(void *),
1263 num_maps = ALIGN(nr_cpu_ids, BITS_PER_LONG) / BITS_PER_LONG;
1264 hctx->ctx_map = kzalloc_node(num_maps * sizeof(unsigned long),
1269 hctx->nr_ctx_map = num_maps;
1272 if (set->ops->init_hctx &&
1273 set->ops->init_hctx(hctx, set->driver_data, i))
1277 if (i == q->nr_hw_queues)
1283 queue_for_each_hw_ctx(q, hctx, j) {
1287 if (set->ops->exit_hctx)
1288 set->ops->exit_hctx(hctx, j);
1290 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1292 kfree(hctx->ctx_map);
1298 static void blk_mq_init_cpu_queues(struct request_queue *q,
1299 unsigned int nr_hw_queues)
1303 for_each_possible_cpu(i) {
1304 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
1305 struct blk_mq_hw_ctx *hctx;
1307 memset(__ctx, 0, sizeof(*__ctx));
1309 spin_lock_init(&__ctx->lock);
1310 INIT_LIST_HEAD(&__ctx->rq_list);
1313 /* If the cpu isn't online, the cpu is mapped to first hctx */
1317 hctx = q->mq_ops->map_queue(q, i);
1318 cpumask_set_cpu(i, hctx->cpumask);
1322 * Set local node, IFF we have more than one hw queue. If
1323 * not, we remain on the home node of the device
1325 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
1326 hctx->numa_node = cpu_to_node(i);
1330 static void blk_mq_map_swqueue(struct request_queue *q)
1333 struct blk_mq_hw_ctx *hctx;
1334 struct blk_mq_ctx *ctx;
1336 queue_for_each_hw_ctx(q, hctx, i) {
1337 cpumask_clear(hctx->cpumask);
1342 * Map software to hardware queues
1344 queue_for_each_ctx(q, ctx, i) {
1345 /* If the cpu isn't online, the cpu is mapped to first hctx */
1349 hctx = q->mq_ops->map_queue(q, i);
1350 cpumask_set_cpu(i, hctx->cpumask);
1351 ctx->index_hw = hctx->nr_ctx;
1352 hctx->ctxs[hctx->nr_ctx++] = ctx;
1356 struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
1358 struct blk_mq_hw_ctx **hctxs;
1359 struct blk_mq_ctx *ctx;
1360 struct request_queue *q;
1363 ctx = alloc_percpu(struct blk_mq_ctx);
1365 return ERR_PTR(-ENOMEM);
1367 hctxs = kmalloc_node(set->nr_hw_queues * sizeof(*hctxs), GFP_KERNEL,
1373 for (i = 0; i < set->nr_hw_queues; i++) {
1374 hctxs[i] = set->ops->alloc_hctx(set, i);
1378 if (!zalloc_cpumask_var(&hctxs[i]->cpumask, GFP_KERNEL))
1381 hctxs[i]->numa_node = NUMA_NO_NODE;
1382 hctxs[i]->queue_num = i;
1385 q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
1389 q->mq_map = blk_mq_make_queue_map(set);
1393 setup_timer(&q->timeout, blk_mq_rq_timer, (unsigned long) q);
1394 blk_queue_rq_timeout(q, 30000);
1396 q->nr_queues = nr_cpu_ids;
1397 q->nr_hw_queues = set->nr_hw_queues;
1400 q->queue_hw_ctx = hctxs;
1402 q->mq_ops = set->ops;
1403 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
1405 q->sg_reserved_size = INT_MAX;
1407 blk_queue_make_request(q, blk_mq_make_request);
1408 blk_queue_rq_timed_out(q, set->ops->timeout);
1410 blk_queue_rq_timeout(q, set->timeout);
1412 if (set->ops->complete)
1413 blk_queue_softirq_done(q, set->ops->complete);
1415 blk_mq_init_flush(q);
1416 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
1418 q->flush_rq = kzalloc(round_up(sizeof(struct request) +
1419 set->cmd_size, cache_line_size()),
1424 if (blk_mq_init_hw_queues(q, set))
1427 blk_mq_map_swqueue(q);
1429 mutex_lock(&all_q_mutex);
1430 list_add_tail(&q->all_q_node, &all_q_list);
1431 mutex_unlock(&all_q_mutex);
1440 blk_cleanup_queue(q);
1442 for (i = 0; i < set->nr_hw_queues; i++) {
1445 free_cpumask_var(hctxs[i]->cpumask);
1446 set->ops->free_hctx(hctxs[i], i);
1451 return ERR_PTR(-ENOMEM);
1453 EXPORT_SYMBOL(blk_mq_init_queue);
1455 void blk_mq_free_queue(struct request_queue *q)
1457 struct blk_mq_hw_ctx *hctx;
1460 queue_for_each_hw_ctx(q, hctx, i) {
1461 kfree(hctx->ctx_map);
1463 blk_mq_unregister_cpu_notifier(&hctx->cpu_notifier);
1464 if (q->mq_ops->exit_hctx)
1465 q->mq_ops->exit_hctx(hctx, i);
1466 free_cpumask_var(hctx->cpumask);
1467 q->mq_ops->free_hctx(hctx, i);
1470 free_percpu(q->queue_ctx);
1471 kfree(q->queue_hw_ctx);
1474 q->queue_ctx = NULL;
1475 q->queue_hw_ctx = NULL;
1478 mutex_lock(&all_q_mutex);
1479 list_del_init(&q->all_q_node);
1480 mutex_unlock(&all_q_mutex);
1483 /* Basically redo blk_mq_init_queue with queue frozen */
1484 static void blk_mq_queue_reinit(struct request_queue *q)
1486 blk_mq_freeze_queue(q);
1488 blk_mq_update_queue_map(q->mq_map, q->nr_hw_queues);
1491 * redo blk_mq_init_cpu_queues and blk_mq_init_hw_queues. FIXME: maybe
1492 * we should change hctx numa_node according to new topology (this
1493 * involves free and re-allocate memory, worthy doing?)
1496 blk_mq_map_swqueue(q);
1498 blk_mq_unfreeze_queue(q);
1501 static int blk_mq_queue_reinit_notify(struct notifier_block *nb,
1502 unsigned long action, void *hcpu)
1504 struct request_queue *q;
1507 * Before new mapping is established, hotadded cpu might already start
1508 * handling requests. This doesn't break anything as we map offline
1509 * CPUs to first hardware queue. We will re-init queue below to get
1512 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN &&
1513 action != CPU_ONLINE && action != CPU_ONLINE_FROZEN)
1516 mutex_lock(&all_q_mutex);
1517 list_for_each_entry(q, &all_q_list, all_q_node)
1518 blk_mq_queue_reinit(q);
1519 mutex_unlock(&all_q_mutex);
1523 int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
1527 if (!set->nr_hw_queues)
1529 if (!set->queue_depth || set->queue_depth > BLK_MQ_MAX_DEPTH)
1531 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
1534 if (!set->nr_hw_queues ||
1535 !set->ops->queue_rq || !set->ops->map_queue ||
1536 !set->ops->alloc_hctx || !set->ops->free_hctx)
1540 set->tags = kmalloc_node(set->nr_hw_queues *
1541 sizeof(struct blk_mq_tags *),
1542 GFP_KERNEL, set->numa_node);
1546 for (i = 0; i < set->nr_hw_queues; i++) {
1547 set->tags[i] = blk_mq_init_rq_map(set, i);
1556 blk_mq_free_rq_map(set, set->tags[i], i);
1560 EXPORT_SYMBOL(blk_mq_alloc_tag_set);
1562 void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
1566 for (i = 0; i < set->nr_hw_queues; i++)
1567 blk_mq_free_rq_map(set, set->tags[i], i);
1569 EXPORT_SYMBOL(blk_mq_free_tag_set);
1571 void blk_mq_disable_hotplug(void)
1573 mutex_lock(&all_q_mutex);
1576 void blk_mq_enable_hotplug(void)
1578 mutex_unlock(&all_q_mutex);
1581 static int __init blk_mq_init(void)
1585 /* Must be called after percpu_counter_hotcpu_callback() */
1586 hotcpu_notifier(blk_mq_queue_reinit_notify, -10);
1590 subsys_initcall(blk_mq_init);