1 // SPDX-License-Identifier: GPL-2.0
3 * Block multiqueue core code
5 * Copyright (C) 2013-2014 Jens Axboe
6 * Copyright (C) 2013-2014 Christoph Hellwig
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/backing-dev.h>
11 #include <linux/bio.h>
12 #include <linux/blkdev.h>
13 #include <linux/blk-integrity.h>
14 #include <linux/kmemleak.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/workqueue.h>
19 #include <linux/smp.h>
20 #include <linux/interrupt.h>
21 #include <linux/llist.h>
22 #include <linux/list_sort.h>
23 #include <linux/cpu.h>
24 #include <linux/cache.h>
25 #include <linux/sched/sysctl.h>
26 #include <linux/sched/topology.h>
27 #include <linux/sched/signal.h>
28 #include <linux/delay.h>
29 #include <linux/crash_dump.h>
30 #include <linux/prefetch.h>
31 #include <linux/blk-crypto.h>
33 #include <trace/events/block.h>
35 #include <linux/blk-mq.h>
36 #include <linux/t10-pi.h>
39 #include "blk-mq-debugfs.h"
40 #include "blk-mq-tag.h"
43 #include "blk-mq-sched.h"
44 #include "blk-rq-qos.h"
46 static DEFINE_PER_CPU(struct llist_head, blk_cpu_done);
48 static void blk_mq_poll_stats_start(struct request_queue *q);
49 static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);
51 static int blk_mq_poll_stats_bkt(const struct request *rq)
53 int ddir, sectors, bucket;
55 ddir = rq_data_dir(rq);
56 sectors = blk_rq_stats_sectors(rq);
58 bucket = ddir + 2 * ilog2(sectors);
62 else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
63 return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
68 #define BLK_QC_T_SHIFT 16
69 #define BLK_QC_T_INTERNAL (1U << 31)
71 static inline struct blk_mq_hw_ctx *blk_qc_to_hctx(struct request_queue *q,
74 return q->queue_hw_ctx[(qc & ~BLK_QC_T_INTERNAL) >> BLK_QC_T_SHIFT];
77 static inline struct request *blk_qc_to_rq(struct blk_mq_hw_ctx *hctx,
80 unsigned int tag = qc & ((1U << BLK_QC_T_SHIFT) - 1);
82 if (qc & BLK_QC_T_INTERNAL)
83 return blk_mq_tag_to_rq(hctx->sched_tags, tag);
84 return blk_mq_tag_to_rq(hctx->tags, tag);
87 static inline blk_qc_t blk_rq_to_qc(struct request *rq)
89 return (rq->mq_hctx->queue_num << BLK_QC_T_SHIFT) |
91 rq->tag : (rq->internal_tag | BLK_QC_T_INTERNAL));
95 * Check if any of the ctx, dispatch list or elevator
96 * have pending work in this hardware queue.
98 static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
100 return !list_empty_careful(&hctx->dispatch) ||
101 sbitmap_any_bit_set(&hctx->ctx_map) ||
102 blk_mq_sched_has_work(hctx);
106 * Mark this ctx as having pending work in this hardware queue
108 static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
109 struct blk_mq_ctx *ctx)
111 const int bit = ctx->index_hw[hctx->type];
113 if (!sbitmap_test_bit(&hctx->ctx_map, bit))
114 sbitmap_set_bit(&hctx->ctx_map, bit);
117 static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
118 struct blk_mq_ctx *ctx)
120 const int bit = ctx->index_hw[hctx->type];
122 sbitmap_clear_bit(&hctx->ctx_map, bit);
126 struct block_device *part;
127 unsigned int inflight[2];
130 static bool blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx,
131 struct request *rq, void *priv,
134 struct mq_inflight *mi = priv;
136 if ((!mi->part->bd_partno || rq->part == mi->part) &&
137 blk_mq_rq_state(rq) == MQ_RQ_IN_FLIGHT)
138 mi->inflight[rq_data_dir(rq)]++;
143 unsigned int blk_mq_in_flight(struct request_queue *q,
144 struct block_device *part)
146 struct mq_inflight mi = { .part = part };
148 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
150 return mi.inflight[0] + mi.inflight[1];
153 void blk_mq_in_flight_rw(struct request_queue *q, struct block_device *part,
154 unsigned int inflight[2])
156 struct mq_inflight mi = { .part = part };
158 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
159 inflight[0] = mi.inflight[0];
160 inflight[1] = mi.inflight[1];
163 void blk_freeze_queue_start(struct request_queue *q)
165 mutex_lock(&q->mq_freeze_lock);
166 if (++q->mq_freeze_depth == 1) {
167 percpu_ref_kill(&q->q_usage_counter);
168 mutex_unlock(&q->mq_freeze_lock);
170 blk_mq_run_hw_queues(q, false);
172 mutex_unlock(&q->mq_freeze_lock);
175 EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
177 void blk_mq_freeze_queue_wait(struct request_queue *q)
179 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
181 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
183 int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
184 unsigned long timeout)
186 return wait_event_timeout(q->mq_freeze_wq,
187 percpu_ref_is_zero(&q->q_usage_counter),
190 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
193 * Guarantee no request is in use, so we can change any data structure of
194 * the queue afterward.
196 void blk_freeze_queue(struct request_queue *q)
199 * In the !blk_mq case we are only calling this to kill the
200 * q_usage_counter, otherwise this increases the freeze depth
201 * and waits for it to return to zero. For this reason there is
202 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
203 * exported to drivers as the only user for unfreeze is blk_mq.
205 blk_freeze_queue_start(q);
206 blk_mq_freeze_queue_wait(q);
209 void blk_mq_freeze_queue(struct request_queue *q)
212 * ...just an alias to keep freeze and unfreeze actions balanced
213 * in the blk_mq_* namespace
217 EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
219 void __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic)
221 mutex_lock(&q->mq_freeze_lock);
223 q->q_usage_counter.data->force_atomic = true;
224 q->mq_freeze_depth--;
225 WARN_ON_ONCE(q->mq_freeze_depth < 0);
226 if (!q->mq_freeze_depth) {
227 percpu_ref_resurrect(&q->q_usage_counter);
228 wake_up_all(&q->mq_freeze_wq);
230 mutex_unlock(&q->mq_freeze_lock);
233 void blk_mq_unfreeze_queue(struct request_queue *q)
235 __blk_mq_unfreeze_queue(q, false);
237 EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
240 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
241 * mpt3sas driver such that this function can be removed.
243 void blk_mq_quiesce_queue_nowait(struct request_queue *q)
245 blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
247 EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
250 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
253 * Note: this function does not prevent that the struct request end_io()
254 * callback function is invoked. Once this function is returned, we make
255 * sure no dispatch can happen until the queue is unquiesced via
256 * blk_mq_unquiesce_queue().
258 void blk_mq_quiesce_queue(struct request_queue *q)
260 struct blk_mq_hw_ctx *hctx;
264 blk_mq_quiesce_queue_nowait(q);
266 queue_for_each_hw_ctx(q, hctx, i) {
267 if (hctx->flags & BLK_MQ_F_BLOCKING)
268 synchronize_srcu(hctx->srcu);
275 EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
278 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
281 * This function recovers queue into the state before quiescing
282 * which is done by blk_mq_quiesce_queue.
284 void blk_mq_unquiesce_queue(struct request_queue *q)
286 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
288 /* dispatch requests which are inserted during quiescing */
289 blk_mq_run_hw_queues(q, true);
291 EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
293 void blk_mq_wake_waiters(struct request_queue *q)
295 struct blk_mq_hw_ctx *hctx;
298 queue_for_each_hw_ctx(q, hctx, i)
299 if (blk_mq_hw_queue_mapped(hctx))
300 blk_mq_tag_wakeup_all(hctx->tags, true);
304 * Only need start/end time stamping if we have iostat or
305 * blk stats enabled, or using an IO scheduler.
307 static inline bool blk_mq_need_time_stamp(struct request *rq)
309 return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS)) || rq->q->elevator;
312 static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
313 unsigned int tag, u64 alloc_time_ns)
315 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
316 struct request *rq = tags->static_rqs[tag];
318 if (data->q->elevator) {
319 rq->tag = BLK_MQ_NO_TAG;
320 rq->internal_tag = tag;
323 rq->internal_tag = BLK_MQ_NO_TAG;
326 /* csd/requeue_work/fifo_time is initialized before use */
328 rq->mq_ctx = data->ctx;
329 rq->mq_hctx = data->hctx;
331 rq->cmd_flags = data->cmd_flags;
332 if (data->flags & BLK_MQ_REQ_PM)
333 rq->rq_flags |= RQF_PM;
334 if (blk_queue_io_stat(data->q))
335 rq->rq_flags |= RQF_IO_STAT;
336 INIT_LIST_HEAD(&rq->queuelist);
337 INIT_HLIST_NODE(&rq->hash);
338 RB_CLEAR_NODE(&rq->rb_node);
341 #ifdef CONFIG_BLK_RQ_ALLOC_TIME
342 rq->alloc_time_ns = alloc_time_ns;
344 if (blk_mq_need_time_stamp(rq))
345 rq->start_time_ns = ktime_get_ns();
347 rq->start_time_ns = 0;
348 rq->io_start_time_ns = 0;
349 rq->stats_sectors = 0;
350 rq->nr_phys_segments = 0;
351 #if defined(CONFIG_BLK_DEV_INTEGRITY)
352 rq->nr_integrity_segments = 0;
354 blk_crypto_rq_set_defaults(rq);
355 /* tag was already set */
356 WRITE_ONCE(rq->deadline, 0);
361 rq->end_io_data = NULL;
363 data->ctx->rq_dispatched[op_is_sync(data->cmd_flags)]++;
364 refcount_set(&rq->ref, 1);
366 if (!op_is_flush(data->cmd_flags)) {
367 struct elevator_queue *e = data->q->elevator;
370 if (e && e->type->ops.prepare_request) {
371 if (e->type->icq_cache)
372 blk_mq_sched_assign_ioc(rq);
374 e->type->ops.prepare_request(rq);
375 rq->rq_flags |= RQF_ELVPRIV;
379 data->hctx->queued++;
383 static inline struct request *
384 __blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data,
387 unsigned int tag, tag_offset;
392 tags = blk_mq_get_tags(data, data->nr_tags, &tag_offset);
396 for (i = 0; tags; i++) {
397 if (!(tags & (1UL << i)))
399 tag = tag_offset + i;
401 rq = blk_mq_rq_ctx_init(data, tag, alloc_time_ns);
402 rq->rq_next = *data->cached_rq;
403 *data->cached_rq = rq;
407 if (!data->cached_rq)
410 rq = *data->cached_rq;
411 *data->cached_rq = rq->rq_next;
415 static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data)
417 struct request_queue *q = data->q;
418 struct elevator_queue *e = q->elevator;
419 u64 alloc_time_ns = 0;
423 /* alloc_time includes depth and tag waits */
424 if (blk_queue_rq_alloc_time(q))
425 alloc_time_ns = ktime_get_ns();
427 if (data->cmd_flags & REQ_NOWAIT)
428 data->flags |= BLK_MQ_REQ_NOWAIT;
432 * Flush/passthrough requests are special and go directly to the
433 * dispatch list. Don't include reserved tags in the
434 * limiting, as it isn't useful.
436 if (!op_is_flush(data->cmd_flags) &&
437 !blk_op_is_passthrough(data->cmd_flags) &&
438 e->type->ops.limit_depth &&
439 !(data->flags & BLK_MQ_REQ_RESERVED))
440 e->type->ops.limit_depth(data->cmd_flags, data);
444 data->ctx = blk_mq_get_ctx(q);
445 data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx);
447 blk_mq_tag_busy(data->hctx);
450 * Try batched alloc if we want more than 1 tag.
452 if (data->nr_tags > 1) {
453 rq = __blk_mq_alloc_requests_batch(data, alloc_time_ns);
460 * Waiting allocations only fail because of an inactive hctx. In that
461 * case just retry the hctx assignment and tag allocation as CPU hotplug
462 * should have migrated us to an online CPU by now.
464 tag = blk_mq_get_tag(data);
465 if (tag == BLK_MQ_NO_TAG) {
466 if (data->flags & BLK_MQ_REQ_NOWAIT)
469 * Give up the CPU and sleep for a random short time to
470 * ensure that thread using a realtime scheduling class
471 * are migrated off the CPU, and thus off the hctx that
478 return blk_mq_rq_ctx_init(data, tag, alloc_time_ns);
481 struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
482 blk_mq_req_flags_t flags)
484 struct blk_mq_alloc_data data = {
493 ret = blk_queue_enter(q, flags);
497 rq = __blk_mq_alloc_requests(&data);
501 rq->__sector = (sector_t) -1;
502 rq->bio = rq->biotail = NULL;
506 return ERR_PTR(-EWOULDBLOCK);
508 EXPORT_SYMBOL(blk_mq_alloc_request);
510 struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
511 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
513 struct blk_mq_alloc_data data = {
519 u64 alloc_time_ns = 0;
524 /* alloc_time includes depth and tag waits */
525 if (blk_queue_rq_alloc_time(q))
526 alloc_time_ns = ktime_get_ns();
529 * If the tag allocator sleeps we could get an allocation for a
530 * different hardware context. No need to complicate the low level
531 * allocator for this for the rare use case of a command tied to
534 if (WARN_ON_ONCE(!(flags & (BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED))))
535 return ERR_PTR(-EINVAL);
537 if (hctx_idx >= q->nr_hw_queues)
538 return ERR_PTR(-EIO);
540 ret = blk_queue_enter(q, flags);
545 * Check if the hardware context is actually mapped to anything.
546 * If not tell the caller that it should skip this queue.
549 data.hctx = q->queue_hw_ctx[hctx_idx];
550 if (!blk_mq_hw_queue_mapped(data.hctx))
552 cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
553 data.ctx = __blk_mq_get_ctx(q, cpu);
556 blk_mq_tag_busy(data.hctx);
559 tag = blk_mq_get_tag(&data);
560 if (tag == BLK_MQ_NO_TAG)
562 return blk_mq_rq_ctx_init(&data, tag, alloc_time_ns);
568 EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
570 static void __blk_mq_free_request(struct request *rq)
572 struct request_queue *q = rq->q;
573 struct blk_mq_ctx *ctx = rq->mq_ctx;
574 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
575 const int sched_tag = rq->internal_tag;
577 blk_crypto_free_request(rq);
578 blk_pm_mark_last_busy(rq);
580 if (rq->tag != BLK_MQ_NO_TAG)
581 blk_mq_put_tag(hctx->tags, ctx, rq->tag);
582 if (sched_tag != BLK_MQ_NO_TAG)
583 blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
584 blk_mq_sched_restart(hctx);
588 void blk_mq_free_request(struct request *rq)
590 struct request_queue *q = rq->q;
591 struct elevator_queue *e = q->elevator;
592 struct blk_mq_ctx *ctx = rq->mq_ctx;
593 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
595 if (rq->rq_flags & RQF_ELVPRIV) {
596 if (e && e->type->ops.finish_request)
597 e->type->ops.finish_request(rq);
599 put_io_context(rq->elv.icq->ioc);
604 ctx->rq_completed[rq_is_sync(rq)]++;
605 if (rq->rq_flags & RQF_MQ_INFLIGHT)
606 __blk_mq_dec_active_requests(hctx);
608 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
609 laptop_io_completion(q->disk->bdi);
613 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
614 if (refcount_dec_and_test(&rq->ref))
615 __blk_mq_free_request(rq);
617 EXPORT_SYMBOL_GPL(blk_mq_free_request);
619 void blk_mq_free_plug_rqs(struct blk_plug *plug)
621 while (plug->cached_rq) {
624 rq = plug->cached_rq;
625 plug->cached_rq = rq->rq_next;
626 percpu_ref_get(&rq->q->q_usage_counter);
627 blk_mq_free_request(rq);
631 inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
633 if (blk_mq_need_time_stamp(rq)) {
634 u64 now = ktime_get_ns();
636 if (rq->rq_flags & RQF_STATS) {
637 blk_mq_poll_stats_start(rq->q);
638 blk_stat_add(rq, now);
641 blk_mq_sched_completed_request(rq, now);
642 blk_account_io_done(rq, now);
646 rq_qos_done(rq->q, rq);
647 rq->end_io(rq, error);
649 blk_mq_free_request(rq);
652 EXPORT_SYMBOL(__blk_mq_end_request);
654 void blk_mq_end_request(struct request *rq, blk_status_t error)
656 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
658 __blk_mq_end_request(rq, error);
660 EXPORT_SYMBOL(blk_mq_end_request);
662 static void blk_complete_reqs(struct llist_head *list)
664 struct llist_node *entry = llist_reverse_order(llist_del_all(list));
665 struct request *rq, *next;
667 llist_for_each_entry_safe(rq, next, entry, ipi_list)
668 rq->q->mq_ops->complete(rq);
671 static __latent_entropy void blk_done_softirq(struct softirq_action *h)
673 blk_complete_reqs(this_cpu_ptr(&blk_cpu_done));
676 static int blk_softirq_cpu_dead(unsigned int cpu)
678 blk_complete_reqs(&per_cpu(blk_cpu_done, cpu));
682 static void __blk_mq_complete_request_remote(void *data)
684 __raise_softirq_irqoff(BLOCK_SOFTIRQ);
687 static inline bool blk_mq_complete_need_ipi(struct request *rq)
689 int cpu = raw_smp_processor_id();
691 if (!IS_ENABLED(CONFIG_SMP) ||
692 !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags))
695 * With force threaded interrupts enabled, raising softirq from an SMP
696 * function call will always result in waking the ksoftirqd thread.
697 * This is probably worse than completing the request on a different
700 if (force_irqthreads())
703 /* same CPU or cache domain? Complete locally */
704 if (cpu == rq->mq_ctx->cpu ||
705 (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) &&
706 cpus_share_cache(cpu, rq->mq_ctx->cpu)))
709 /* don't try to IPI to an offline CPU */
710 return cpu_online(rq->mq_ctx->cpu);
713 static void blk_mq_complete_send_ipi(struct request *rq)
715 struct llist_head *list;
718 cpu = rq->mq_ctx->cpu;
719 list = &per_cpu(blk_cpu_done, cpu);
720 if (llist_add(&rq->ipi_list, list)) {
721 INIT_CSD(&rq->csd, __blk_mq_complete_request_remote, rq);
722 smp_call_function_single_async(cpu, &rq->csd);
726 static void blk_mq_raise_softirq(struct request *rq)
728 struct llist_head *list;
731 list = this_cpu_ptr(&blk_cpu_done);
732 if (llist_add(&rq->ipi_list, list))
733 raise_softirq(BLOCK_SOFTIRQ);
737 bool blk_mq_complete_request_remote(struct request *rq)
739 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
742 * For a polled request, always complete locallly, it's pointless
743 * to redirect the completion.
745 if (rq->cmd_flags & REQ_POLLED)
748 if (blk_mq_complete_need_ipi(rq)) {
749 blk_mq_complete_send_ipi(rq);
753 if (rq->q->nr_hw_queues == 1) {
754 blk_mq_raise_softirq(rq);
759 EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote);
762 * blk_mq_complete_request - end I/O on a request
763 * @rq: the request being processed
766 * Complete a request by scheduling the ->complete_rq operation.
768 void blk_mq_complete_request(struct request *rq)
770 if (!blk_mq_complete_request_remote(rq))
771 rq->q->mq_ops->complete(rq);
773 EXPORT_SYMBOL(blk_mq_complete_request);
775 static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
776 __releases(hctx->srcu)
778 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
781 srcu_read_unlock(hctx->srcu, srcu_idx);
784 static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
785 __acquires(hctx->srcu)
787 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
788 /* shut up gcc false positive */
792 *srcu_idx = srcu_read_lock(hctx->srcu);
796 * blk_mq_start_request - Start processing a request
797 * @rq: Pointer to request to be started
799 * Function used by device drivers to notify the block layer that a request
800 * is going to be processed now, so blk layer can do proper initializations
801 * such as starting the timeout timer.
803 void blk_mq_start_request(struct request *rq)
805 struct request_queue *q = rq->q;
807 trace_block_rq_issue(rq);
809 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
811 #ifdef CONFIG_BLK_CGROUP
813 start_time = bio_issue_time(&rq->bio->bi_issue);
816 start_time = ktime_get_ns();
817 rq->io_start_time_ns = start_time;
818 rq->stats_sectors = blk_rq_sectors(rq);
819 rq->rq_flags |= RQF_STATS;
823 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
826 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
828 #ifdef CONFIG_BLK_DEV_INTEGRITY
829 if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
830 q->integrity.profile->prepare_fn(rq);
832 if (rq->bio && rq->bio->bi_opf & REQ_POLLED)
833 WRITE_ONCE(rq->bio->bi_cookie, blk_rq_to_qc(rq));
835 EXPORT_SYMBOL(blk_mq_start_request);
837 static void __blk_mq_requeue_request(struct request *rq)
839 struct request_queue *q = rq->q;
841 blk_mq_put_driver_tag(rq);
843 trace_block_rq_requeue(rq);
844 rq_qos_requeue(q, rq);
846 if (blk_mq_request_started(rq)) {
847 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
848 rq->rq_flags &= ~RQF_TIMED_OUT;
852 void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
854 __blk_mq_requeue_request(rq);
856 /* this request will be re-inserted to io scheduler queue */
857 blk_mq_sched_requeue_request(rq);
859 BUG_ON(!list_empty(&rq->queuelist));
860 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
862 EXPORT_SYMBOL(blk_mq_requeue_request);
864 static void blk_mq_requeue_work(struct work_struct *work)
866 struct request_queue *q =
867 container_of(work, struct request_queue, requeue_work.work);
869 struct request *rq, *next;
871 spin_lock_irq(&q->requeue_lock);
872 list_splice_init(&q->requeue_list, &rq_list);
873 spin_unlock_irq(&q->requeue_lock);
875 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
876 if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP)))
879 rq->rq_flags &= ~RQF_SOFTBARRIER;
880 list_del_init(&rq->queuelist);
882 * If RQF_DONTPREP, rq has contained some driver specific
883 * data, so insert it to hctx dispatch list to avoid any
886 if (rq->rq_flags & RQF_DONTPREP)
887 blk_mq_request_bypass_insert(rq, false, false);
889 blk_mq_sched_insert_request(rq, true, false, false);
892 while (!list_empty(&rq_list)) {
893 rq = list_entry(rq_list.next, struct request, queuelist);
894 list_del_init(&rq->queuelist);
895 blk_mq_sched_insert_request(rq, false, false, false);
898 blk_mq_run_hw_queues(q, false);
901 void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
902 bool kick_requeue_list)
904 struct request_queue *q = rq->q;
908 * We abuse this flag that is otherwise used by the I/O scheduler to
909 * request head insertion from the workqueue.
911 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
913 spin_lock_irqsave(&q->requeue_lock, flags);
915 rq->rq_flags |= RQF_SOFTBARRIER;
916 list_add(&rq->queuelist, &q->requeue_list);
918 list_add_tail(&rq->queuelist, &q->requeue_list);
920 spin_unlock_irqrestore(&q->requeue_lock, flags);
922 if (kick_requeue_list)
923 blk_mq_kick_requeue_list(q);
926 void blk_mq_kick_requeue_list(struct request_queue *q)
928 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
930 EXPORT_SYMBOL(blk_mq_kick_requeue_list);
932 void blk_mq_delay_kick_requeue_list(struct request_queue *q,
935 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
936 msecs_to_jiffies(msecs));
938 EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
940 struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
942 if (tag < tags->nr_tags) {
943 prefetch(tags->rqs[tag]);
944 return tags->rqs[tag];
949 EXPORT_SYMBOL(blk_mq_tag_to_rq);
951 static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq,
952 void *priv, bool reserved)
955 * If we find a request that isn't idle and the queue matches,
956 * we know the queue is busy. Return false to stop the iteration.
958 if (blk_mq_request_started(rq) && rq->q == hctx->queue) {
968 bool blk_mq_queue_inflight(struct request_queue *q)
972 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
975 EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
977 static void blk_mq_rq_timed_out(struct request *req, bool reserved)
979 req->rq_flags |= RQF_TIMED_OUT;
980 if (req->q->mq_ops->timeout) {
981 enum blk_eh_timer_return ret;
983 ret = req->q->mq_ops->timeout(req, reserved);
984 if (ret == BLK_EH_DONE)
986 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
992 static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
994 unsigned long deadline;
996 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
998 if (rq->rq_flags & RQF_TIMED_OUT)
1001 deadline = READ_ONCE(rq->deadline);
1002 if (time_after_eq(jiffies, deadline))
1007 else if (time_after(*next, deadline))
1012 void blk_mq_put_rq_ref(struct request *rq)
1014 if (is_flush_rq(rq))
1016 else if (refcount_dec_and_test(&rq->ref))
1017 __blk_mq_free_request(rq);
1020 static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
1021 struct request *rq, void *priv, bool reserved)
1023 unsigned long *next = priv;
1026 * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot
1027 * be reallocated underneath the timeout handler's processing, then
1028 * the expire check is reliable. If the request is not expired, then
1029 * it was completed and reallocated as a new request after returning
1030 * from blk_mq_check_expired().
1032 if (blk_mq_req_expired(rq, next))
1033 blk_mq_rq_timed_out(rq, reserved);
1037 static void blk_mq_timeout_work(struct work_struct *work)
1039 struct request_queue *q =
1040 container_of(work, struct request_queue, timeout_work);
1041 unsigned long next = 0;
1042 struct blk_mq_hw_ctx *hctx;
1045 /* A deadlock might occur if a request is stuck requiring a
1046 * timeout at the same time a queue freeze is waiting
1047 * completion, since the timeout code would not be able to
1048 * acquire the queue reference here.
1050 * That's why we don't use blk_queue_enter here; instead, we use
1051 * percpu_ref_tryget directly, because we need to be able to
1052 * obtain a reference even in the short window between the queue
1053 * starting to freeze, by dropping the first reference in
1054 * blk_freeze_queue_start, and the moment the last request is
1055 * consumed, marked by the instant q_usage_counter reaches
1058 if (!percpu_ref_tryget(&q->q_usage_counter))
1061 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
1064 mod_timer(&q->timeout, next);
1067 * Request timeouts are handled as a forward rolling timer. If
1068 * we end up here it means that no requests are pending and
1069 * also that no request has been pending for a while. Mark
1070 * each hctx as idle.
1072 queue_for_each_hw_ctx(q, hctx, i) {
1073 /* the hctx may be unmapped, so check it here */
1074 if (blk_mq_hw_queue_mapped(hctx))
1075 blk_mq_tag_idle(hctx);
1081 struct flush_busy_ctx_data {
1082 struct blk_mq_hw_ctx *hctx;
1083 struct list_head *list;
1086 static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
1088 struct flush_busy_ctx_data *flush_data = data;
1089 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
1090 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
1091 enum hctx_type type = hctx->type;
1093 spin_lock(&ctx->lock);
1094 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
1095 sbitmap_clear_bit(sb, bitnr);
1096 spin_unlock(&ctx->lock);
1101 * Process software queues that have been marked busy, splicing them
1102 * to the for-dispatch
1104 void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
1106 struct flush_busy_ctx_data data = {
1111 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
1113 EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
1115 struct dispatch_rq_data {
1116 struct blk_mq_hw_ctx *hctx;
1120 static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
1123 struct dispatch_rq_data *dispatch_data = data;
1124 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
1125 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
1126 enum hctx_type type = hctx->type;
1128 spin_lock(&ctx->lock);
1129 if (!list_empty(&ctx->rq_lists[type])) {
1130 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
1131 list_del_init(&dispatch_data->rq->queuelist);
1132 if (list_empty(&ctx->rq_lists[type]))
1133 sbitmap_clear_bit(sb, bitnr);
1135 spin_unlock(&ctx->lock);
1137 return !dispatch_data->rq;
1140 struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
1141 struct blk_mq_ctx *start)
1143 unsigned off = start ? start->index_hw[hctx->type] : 0;
1144 struct dispatch_rq_data data = {
1149 __sbitmap_for_each_set(&hctx->ctx_map, off,
1150 dispatch_rq_from_ctx, &data);
1155 static inline unsigned int queued_to_index(unsigned int queued)
1160 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
1163 static bool __blk_mq_get_driver_tag(struct request *rq)
1165 struct sbitmap_queue *bt = &rq->mq_hctx->tags->bitmap_tags;
1166 unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags;
1169 blk_mq_tag_busy(rq->mq_hctx);
1171 if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) {
1172 bt = &rq->mq_hctx->tags->breserved_tags;
1175 if (!hctx_may_queue(rq->mq_hctx, bt))
1179 tag = __sbitmap_queue_get(bt);
1180 if (tag == BLK_MQ_NO_TAG)
1183 rq->tag = tag + tag_offset;
1187 bool blk_mq_get_driver_tag(struct request *rq)
1189 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1191 if (rq->tag == BLK_MQ_NO_TAG && !__blk_mq_get_driver_tag(rq))
1194 if ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) &&
1195 !(rq->rq_flags & RQF_MQ_INFLIGHT)) {
1196 rq->rq_flags |= RQF_MQ_INFLIGHT;
1197 __blk_mq_inc_active_requests(hctx);
1199 hctx->tags->rqs[rq->tag] = rq;
1203 static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1204 int flags, void *key)
1206 struct blk_mq_hw_ctx *hctx;
1208 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1210 spin_lock(&hctx->dispatch_wait_lock);
1211 if (!list_empty(&wait->entry)) {
1212 struct sbitmap_queue *sbq;
1214 list_del_init(&wait->entry);
1215 sbq = &hctx->tags->bitmap_tags;
1216 atomic_dec(&sbq->ws_active);
1218 spin_unlock(&hctx->dispatch_wait_lock);
1220 blk_mq_run_hw_queue(hctx, true);
1225 * Mark us waiting for a tag. For shared tags, this involves hooking us into
1226 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1227 * restart. For both cases, take care to check the condition again after
1228 * marking us as waiting.
1230 static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
1233 struct sbitmap_queue *sbq = &hctx->tags->bitmap_tags;
1234 struct wait_queue_head *wq;
1235 wait_queue_entry_t *wait;
1238 if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
1239 blk_mq_sched_mark_restart_hctx(hctx);
1242 * It's possible that a tag was freed in the window between the
1243 * allocation failure and adding the hardware queue to the wait
1246 * Don't clear RESTART here, someone else could have set it.
1247 * At most this will cost an extra queue run.
1249 return blk_mq_get_driver_tag(rq);
1252 wait = &hctx->dispatch_wait;
1253 if (!list_empty_careful(&wait->entry))
1256 wq = &bt_wait_ptr(sbq, hctx)->wait;
1258 spin_lock_irq(&wq->lock);
1259 spin_lock(&hctx->dispatch_wait_lock);
1260 if (!list_empty(&wait->entry)) {
1261 spin_unlock(&hctx->dispatch_wait_lock);
1262 spin_unlock_irq(&wq->lock);
1266 atomic_inc(&sbq->ws_active);
1267 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1268 __add_wait_queue(wq, wait);
1271 * It's possible that a tag was freed in the window between the
1272 * allocation failure and adding the hardware queue to the wait
1275 ret = blk_mq_get_driver_tag(rq);
1277 spin_unlock(&hctx->dispatch_wait_lock);
1278 spin_unlock_irq(&wq->lock);
1283 * We got a tag, remove ourselves from the wait queue to ensure
1284 * someone else gets the wakeup.
1286 list_del_init(&wait->entry);
1287 atomic_dec(&sbq->ws_active);
1288 spin_unlock(&hctx->dispatch_wait_lock);
1289 spin_unlock_irq(&wq->lock);
1294 #define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1295 #define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1297 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1298 * - EWMA is one simple way to compute running average value
1299 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1300 * - take 4 as factor for avoiding to get too small(0) result, and this
1301 * factor doesn't matter because EWMA decreases exponentially
1303 static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1307 ewma = hctx->dispatch_busy;
1312 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1314 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1315 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1317 hctx->dispatch_busy = ewma;
1320 #define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1322 static void blk_mq_handle_dev_resource(struct request *rq,
1323 struct list_head *list)
1325 struct request *next =
1326 list_first_entry_or_null(list, struct request, queuelist);
1329 * If an I/O scheduler has been configured and we got a driver tag for
1330 * the next request already, free it.
1333 blk_mq_put_driver_tag(next);
1335 list_add(&rq->queuelist, list);
1336 __blk_mq_requeue_request(rq);
1339 static void blk_mq_handle_zone_resource(struct request *rq,
1340 struct list_head *zone_list)
1343 * If we end up here it is because we cannot dispatch a request to a
1344 * specific zone due to LLD level zone-write locking or other zone
1345 * related resource not being available. In this case, set the request
1346 * aside in zone_list for retrying it later.
1348 list_add(&rq->queuelist, zone_list);
1349 __blk_mq_requeue_request(rq);
1352 enum prep_dispatch {
1354 PREP_DISPATCH_NO_TAG,
1355 PREP_DISPATCH_NO_BUDGET,
1358 static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq,
1361 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1362 int budget_token = -1;
1365 budget_token = blk_mq_get_dispatch_budget(rq->q);
1366 if (budget_token < 0) {
1367 blk_mq_put_driver_tag(rq);
1368 return PREP_DISPATCH_NO_BUDGET;
1370 blk_mq_set_rq_budget_token(rq, budget_token);
1373 if (!blk_mq_get_driver_tag(rq)) {
1375 * The initial allocation attempt failed, so we need to
1376 * rerun the hardware queue when a tag is freed. The
1377 * waitqueue takes care of that. If the queue is run
1378 * before we add this entry back on the dispatch list,
1379 * we'll re-run it below.
1381 if (!blk_mq_mark_tag_wait(hctx, rq)) {
1383 * All budgets not got from this function will be put
1384 * together during handling partial dispatch
1387 blk_mq_put_dispatch_budget(rq->q, budget_token);
1388 return PREP_DISPATCH_NO_TAG;
1392 return PREP_DISPATCH_OK;
1395 /* release all allocated budgets before calling to blk_mq_dispatch_rq_list */
1396 static void blk_mq_release_budgets(struct request_queue *q,
1397 struct list_head *list)
1401 list_for_each_entry(rq, list, queuelist) {
1402 int budget_token = blk_mq_get_rq_budget_token(rq);
1404 if (budget_token >= 0)
1405 blk_mq_put_dispatch_budget(q, budget_token);
1410 * Returns true if we did some work AND can potentially do more.
1412 bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list,
1413 unsigned int nr_budgets)
1415 enum prep_dispatch prep;
1416 struct request_queue *q = hctx->queue;
1417 struct request *rq, *nxt;
1419 blk_status_t ret = BLK_STS_OK;
1420 LIST_HEAD(zone_list);
1422 if (list_empty(list))
1426 * Now process all the entries, sending them to the driver.
1428 errors = queued = 0;
1430 struct blk_mq_queue_data bd;
1432 rq = list_first_entry(list, struct request, queuelist);
1434 WARN_ON_ONCE(hctx != rq->mq_hctx);
1435 prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets);
1436 if (prep != PREP_DISPATCH_OK)
1439 list_del_init(&rq->queuelist);
1444 * Flag last if we have no more requests, or if we have more
1445 * but can't assign a driver tag to it.
1447 if (list_empty(list))
1450 nxt = list_first_entry(list, struct request, queuelist);
1451 bd.last = !blk_mq_get_driver_tag(nxt);
1455 * once the request is queued to lld, no need to cover the
1460 ret = q->mq_ops->queue_rq(hctx, &bd);
1465 case BLK_STS_RESOURCE:
1466 case BLK_STS_DEV_RESOURCE:
1467 blk_mq_handle_dev_resource(rq, list);
1469 case BLK_STS_ZONE_RESOURCE:
1471 * Move the request to zone_list and keep going through
1472 * the dispatch list to find more requests the drive can
1475 blk_mq_handle_zone_resource(rq, &zone_list);
1479 blk_mq_end_request(rq, ret);
1481 } while (!list_empty(list));
1483 if (!list_empty(&zone_list))
1484 list_splice_tail_init(&zone_list, list);
1486 hctx->dispatched[queued_to_index(queued)]++;
1488 /* If we didn't flush the entire list, we could have told the driver
1489 * there was more coming, but that turned out to be a lie.
1491 if ((!list_empty(list) || errors) && q->mq_ops->commit_rqs && queued)
1492 q->mq_ops->commit_rqs(hctx);
1494 * Any items that need requeuing? Stuff them into hctx->dispatch,
1495 * that is where we will continue on next queue run.
1497 if (!list_empty(list)) {
1499 /* For non-shared tags, the RESTART check will suffice */
1500 bool no_tag = prep == PREP_DISPATCH_NO_TAG &&
1501 (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED);
1502 bool no_budget_avail = prep == PREP_DISPATCH_NO_BUDGET;
1505 blk_mq_release_budgets(q, list);
1507 spin_lock(&hctx->lock);
1508 list_splice_tail_init(list, &hctx->dispatch);
1509 spin_unlock(&hctx->lock);
1512 * Order adding requests to hctx->dispatch and checking
1513 * SCHED_RESTART flag. The pair of this smp_mb() is the one
1514 * in blk_mq_sched_restart(). Avoid restart code path to
1515 * miss the new added requests to hctx->dispatch, meantime
1516 * SCHED_RESTART is observed here.
1521 * If SCHED_RESTART was set by the caller of this function and
1522 * it is no longer set that means that it was cleared by another
1523 * thread and hence that a queue rerun is needed.
1525 * If 'no_tag' is set, that means that we failed getting
1526 * a driver tag with an I/O scheduler attached. If our dispatch
1527 * waitqueue is no longer active, ensure that we run the queue
1528 * AFTER adding our entries back to the list.
1530 * If no I/O scheduler has been configured it is possible that
1531 * the hardware queue got stopped and restarted before requests
1532 * were pushed back onto the dispatch list. Rerun the queue to
1533 * avoid starvation. Notes:
1534 * - blk_mq_run_hw_queue() checks whether or not a queue has
1535 * been stopped before rerunning a queue.
1536 * - Some but not all block drivers stop a queue before
1537 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
1540 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1541 * bit is set, run queue after a delay to avoid IO stalls
1542 * that could otherwise occur if the queue is idle. We'll do
1543 * similar if we couldn't get budget and SCHED_RESTART is set.
1545 needs_restart = blk_mq_sched_needs_restart(hctx);
1546 if (!needs_restart ||
1547 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
1548 blk_mq_run_hw_queue(hctx, true);
1549 else if (needs_restart && (ret == BLK_STS_RESOURCE ||
1551 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
1553 blk_mq_update_dispatch_busy(hctx, true);
1556 blk_mq_update_dispatch_busy(hctx, false);
1558 return (queued + errors) != 0;
1562 * __blk_mq_run_hw_queue - Run a hardware queue.
1563 * @hctx: Pointer to the hardware queue to run.
1565 * Send pending requests to the hardware.
1567 static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1572 * We can't run the queue inline with ints disabled. Ensure that
1573 * we catch bad users of this early.
1575 WARN_ON_ONCE(in_interrupt());
1577 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
1579 hctx_lock(hctx, &srcu_idx);
1580 blk_mq_sched_dispatch_requests(hctx);
1581 hctx_unlock(hctx, srcu_idx);
1584 static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1586 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1588 if (cpu >= nr_cpu_ids)
1589 cpu = cpumask_first(hctx->cpumask);
1594 * It'd be great if the workqueue API had a way to pass
1595 * in a mask and had some smarts for more clever placement.
1596 * For now we just round-robin here, switching for every
1597 * BLK_MQ_CPU_WORK_BATCH queued items.
1599 static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1602 int next_cpu = hctx->next_cpu;
1604 if (hctx->queue->nr_hw_queues == 1)
1605 return WORK_CPU_UNBOUND;
1607 if (--hctx->next_cpu_batch <= 0) {
1609 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
1611 if (next_cpu >= nr_cpu_ids)
1612 next_cpu = blk_mq_first_mapped_cpu(hctx);
1613 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1617 * Do unbound schedule if we can't find a online CPU for this hctx,
1618 * and it should only happen in the path of handling CPU DEAD.
1620 if (!cpu_online(next_cpu)) {
1627 * Make sure to re-select CPU next time once after CPUs
1628 * in hctx->cpumask become online again.
1630 hctx->next_cpu = next_cpu;
1631 hctx->next_cpu_batch = 1;
1632 return WORK_CPU_UNBOUND;
1635 hctx->next_cpu = next_cpu;
1640 * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue.
1641 * @hctx: Pointer to the hardware queue to run.
1642 * @async: If we want to run the queue asynchronously.
1643 * @msecs: Milliseconds of delay to wait before running the queue.
1645 * If !@async, try to run the queue now. Else, run the queue asynchronously and
1646 * with a delay of @msecs.
1648 static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1649 unsigned long msecs)
1651 if (unlikely(blk_mq_hctx_stopped(hctx)))
1654 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
1655 int cpu = get_cpu();
1656 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
1657 __blk_mq_run_hw_queue(hctx);
1665 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1666 msecs_to_jiffies(msecs));
1670 * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
1671 * @hctx: Pointer to the hardware queue to run.
1672 * @msecs: Milliseconds of delay to wait before running the queue.
1674 * Run a hardware queue asynchronously with a delay of @msecs.
1676 void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1678 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1680 EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1683 * blk_mq_run_hw_queue - Start to run a hardware queue.
1684 * @hctx: Pointer to the hardware queue to run.
1685 * @async: If we want to run the queue asynchronously.
1687 * Check if the request queue is not in a quiesced state and if there are
1688 * pending requests to be sent. If this is true, run the queue to send requests
1691 void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1697 * When queue is quiesced, we may be switching io scheduler, or
1698 * updating nr_hw_queues, or other things, and we can't run queue
1699 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
1701 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
1704 hctx_lock(hctx, &srcu_idx);
1705 need_run = !blk_queue_quiesced(hctx->queue) &&
1706 blk_mq_hctx_has_pending(hctx);
1707 hctx_unlock(hctx, srcu_idx);
1710 __blk_mq_delay_run_hw_queue(hctx, async, 0);
1712 EXPORT_SYMBOL(blk_mq_run_hw_queue);
1715 * Is the request queue handled by an IO scheduler that does not respect
1716 * hardware queues when dispatching?
1718 static bool blk_mq_has_sqsched(struct request_queue *q)
1720 struct elevator_queue *e = q->elevator;
1722 if (e && e->type->ops.dispatch_request &&
1723 !(e->type->elevator_features & ELEVATOR_F_MQ_AWARE))
1729 * Return prefered queue to dispatch from (if any) for non-mq aware IO
1732 static struct blk_mq_hw_ctx *blk_mq_get_sq_hctx(struct request_queue *q)
1734 struct blk_mq_hw_ctx *hctx;
1737 * If the IO scheduler does not respect hardware queues when
1738 * dispatching, we just don't bother with multiple HW queues and
1739 * dispatch from hctx for the current CPU since running multiple queues
1740 * just causes lock contention inside the scheduler and pointless cache
1743 hctx = blk_mq_map_queue_type(q, HCTX_TYPE_DEFAULT,
1744 raw_smp_processor_id());
1745 if (!blk_mq_hctx_stopped(hctx))
1751 * blk_mq_run_hw_queues - Run all hardware queues in a request queue.
1752 * @q: Pointer to the request queue to run.
1753 * @async: If we want to run the queue asynchronously.
1755 void blk_mq_run_hw_queues(struct request_queue *q, bool async)
1757 struct blk_mq_hw_ctx *hctx, *sq_hctx;
1761 if (blk_mq_has_sqsched(q))
1762 sq_hctx = blk_mq_get_sq_hctx(q);
1763 queue_for_each_hw_ctx(q, hctx, i) {
1764 if (blk_mq_hctx_stopped(hctx))
1767 * Dispatch from this hctx either if there's no hctx preferred
1768 * by IO scheduler or if it has requests that bypass the
1771 if (!sq_hctx || sq_hctx == hctx ||
1772 !list_empty_careful(&hctx->dispatch))
1773 blk_mq_run_hw_queue(hctx, async);
1776 EXPORT_SYMBOL(blk_mq_run_hw_queues);
1779 * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously.
1780 * @q: Pointer to the request queue to run.
1781 * @msecs: Milliseconds of delay to wait before running the queues.
1783 void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs)
1785 struct blk_mq_hw_ctx *hctx, *sq_hctx;
1789 if (blk_mq_has_sqsched(q))
1790 sq_hctx = blk_mq_get_sq_hctx(q);
1791 queue_for_each_hw_ctx(q, hctx, i) {
1792 if (blk_mq_hctx_stopped(hctx))
1795 * Dispatch from this hctx either if there's no hctx preferred
1796 * by IO scheduler or if it has requests that bypass the
1799 if (!sq_hctx || sq_hctx == hctx ||
1800 !list_empty_careful(&hctx->dispatch))
1801 blk_mq_delay_run_hw_queue(hctx, msecs);
1804 EXPORT_SYMBOL(blk_mq_delay_run_hw_queues);
1807 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1808 * @q: request queue.
1810 * The caller is responsible for serializing this function against
1811 * blk_mq_{start,stop}_hw_queue().
1813 bool blk_mq_queue_stopped(struct request_queue *q)
1815 struct blk_mq_hw_ctx *hctx;
1818 queue_for_each_hw_ctx(q, hctx, i)
1819 if (blk_mq_hctx_stopped(hctx))
1824 EXPORT_SYMBOL(blk_mq_queue_stopped);
1827 * This function is often used for pausing .queue_rq() by driver when
1828 * there isn't enough resource or some conditions aren't satisfied, and
1829 * BLK_STS_RESOURCE is usually returned.
1831 * We do not guarantee that dispatch can be drained or blocked
1832 * after blk_mq_stop_hw_queue() returns. Please use
1833 * blk_mq_quiesce_queue() for that requirement.
1835 void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1837 cancel_delayed_work(&hctx->run_work);
1839 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
1841 EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1844 * This function is often used for pausing .queue_rq() by driver when
1845 * there isn't enough resource or some conditions aren't satisfied, and
1846 * BLK_STS_RESOURCE is usually returned.
1848 * We do not guarantee that dispatch can be drained or blocked
1849 * after blk_mq_stop_hw_queues() returns. Please use
1850 * blk_mq_quiesce_queue() for that requirement.
1852 void blk_mq_stop_hw_queues(struct request_queue *q)
1854 struct blk_mq_hw_ctx *hctx;
1857 queue_for_each_hw_ctx(q, hctx, i)
1858 blk_mq_stop_hw_queue(hctx);
1860 EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1862 void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1864 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1866 blk_mq_run_hw_queue(hctx, false);
1868 EXPORT_SYMBOL(blk_mq_start_hw_queue);
1870 void blk_mq_start_hw_queues(struct request_queue *q)
1872 struct blk_mq_hw_ctx *hctx;
1875 queue_for_each_hw_ctx(q, hctx, i)
1876 blk_mq_start_hw_queue(hctx);
1878 EXPORT_SYMBOL(blk_mq_start_hw_queues);
1880 void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1882 if (!blk_mq_hctx_stopped(hctx))
1885 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1886 blk_mq_run_hw_queue(hctx, async);
1888 EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1890 void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
1892 struct blk_mq_hw_ctx *hctx;
1895 queue_for_each_hw_ctx(q, hctx, i)
1896 blk_mq_start_stopped_hw_queue(hctx, async);
1898 EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1900 static void blk_mq_run_work_fn(struct work_struct *work)
1902 struct blk_mq_hw_ctx *hctx;
1904 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
1907 * If we are stopped, don't run the queue.
1909 if (blk_mq_hctx_stopped(hctx))
1912 __blk_mq_run_hw_queue(hctx);
1915 static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
1919 struct blk_mq_ctx *ctx = rq->mq_ctx;
1920 enum hctx_type type = hctx->type;
1922 lockdep_assert_held(&ctx->lock);
1924 trace_block_rq_insert(rq);
1927 list_add(&rq->queuelist, &ctx->rq_lists[type]);
1929 list_add_tail(&rq->queuelist, &ctx->rq_lists[type]);
1932 void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1935 struct blk_mq_ctx *ctx = rq->mq_ctx;
1937 lockdep_assert_held(&ctx->lock);
1939 __blk_mq_insert_req_list(hctx, rq, at_head);
1940 blk_mq_hctx_mark_pending(hctx, ctx);
1944 * blk_mq_request_bypass_insert - Insert a request at dispatch list.
1945 * @rq: Pointer to request to be inserted.
1946 * @at_head: true if the request should be inserted at the head of the list.
1947 * @run_queue: If we should run the hardware queue after inserting the request.
1949 * Should only be used carefully, when the caller knows we want to
1950 * bypass a potential IO scheduler on the target device.
1952 void blk_mq_request_bypass_insert(struct request *rq, bool at_head,
1955 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1957 spin_lock(&hctx->lock);
1959 list_add(&rq->queuelist, &hctx->dispatch);
1961 list_add_tail(&rq->queuelist, &hctx->dispatch);
1962 spin_unlock(&hctx->lock);
1965 blk_mq_run_hw_queue(hctx, false);
1968 void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
1969 struct list_head *list)
1973 enum hctx_type type = hctx->type;
1976 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1979 list_for_each_entry(rq, list, queuelist) {
1980 BUG_ON(rq->mq_ctx != ctx);
1981 trace_block_rq_insert(rq);
1984 spin_lock(&ctx->lock);
1985 list_splice_tail_init(list, &ctx->rq_lists[type]);
1986 blk_mq_hctx_mark_pending(hctx, ctx);
1987 spin_unlock(&ctx->lock);
1990 static int plug_rq_cmp(void *priv, const struct list_head *a,
1991 const struct list_head *b)
1993 struct request *rqa = container_of(a, struct request, queuelist);
1994 struct request *rqb = container_of(b, struct request, queuelist);
1996 if (rqa->mq_ctx != rqb->mq_ctx)
1997 return rqa->mq_ctx > rqb->mq_ctx;
1998 if (rqa->mq_hctx != rqb->mq_hctx)
1999 return rqa->mq_hctx > rqb->mq_hctx;
2001 return blk_rq_pos(rqa) > blk_rq_pos(rqb);
2004 void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
2008 if (list_empty(&plug->mq_list))
2010 list_splice_init(&plug->mq_list, &list);
2012 if (plug->rq_count > 2 && plug->multiple_queues)
2013 list_sort(NULL, &list, plug_rq_cmp);
2018 struct list_head rq_list;
2019 struct request *rq, *head_rq = list_entry_rq(list.next);
2020 struct list_head *pos = &head_rq->queuelist; /* skip first */
2021 struct blk_mq_hw_ctx *this_hctx = head_rq->mq_hctx;
2022 struct blk_mq_ctx *this_ctx = head_rq->mq_ctx;
2023 unsigned int depth = 1;
2025 list_for_each_continue(pos, &list) {
2026 rq = list_entry_rq(pos);
2028 if (rq->mq_hctx != this_hctx || rq->mq_ctx != this_ctx)
2033 list_cut_before(&rq_list, &list, pos);
2034 trace_block_unplug(head_rq->q, depth, !from_schedule);
2035 blk_mq_sched_insert_requests(this_hctx, this_ctx, &rq_list,
2037 } while(!list_empty(&list));
2040 static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
2041 unsigned int nr_segs)
2045 if (bio->bi_opf & REQ_RAHEAD)
2046 rq->cmd_flags |= REQ_FAILFAST_MASK;
2048 rq->__sector = bio->bi_iter.bi_sector;
2049 rq->write_hint = bio->bi_write_hint;
2050 blk_rq_bio_prep(rq, bio, nr_segs);
2052 /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */
2053 err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO);
2056 blk_account_io_start(rq);
2059 static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
2060 struct request *rq, bool last)
2062 struct request_queue *q = rq->q;
2063 struct blk_mq_queue_data bd = {
2070 * For OK queue, we are done. For error, caller may kill it.
2071 * Any other error (busy), just add it to our list as we
2072 * previously would have done.
2074 ret = q->mq_ops->queue_rq(hctx, &bd);
2077 blk_mq_update_dispatch_busy(hctx, false);
2079 case BLK_STS_RESOURCE:
2080 case BLK_STS_DEV_RESOURCE:
2081 blk_mq_update_dispatch_busy(hctx, true);
2082 __blk_mq_requeue_request(rq);
2085 blk_mq_update_dispatch_busy(hctx, false);
2092 static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
2094 bool bypass_insert, bool last)
2096 struct request_queue *q = rq->q;
2097 bool run_queue = true;
2101 * RCU or SRCU read lock is needed before checking quiesced flag.
2103 * When queue is stopped or quiesced, ignore 'bypass_insert' from
2104 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
2105 * and avoid driver to try to dispatch again.
2107 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
2109 bypass_insert = false;
2113 if (q->elevator && !bypass_insert)
2116 budget_token = blk_mq_get_dispatch_budget(q);
2117 if (budget_token < 0)
2120 blk_mq_set_rq_budget_token(rq, budget_token);
2122 if (!blk_mq_get_driver_tag(rq)) {
2123 blk_mq_put_dispatch_budget(q, budget_token);
2127 return __blk_mq_issue_directly(hctx, rq, last);
2130 return BLK_STS_RESOURCE;
2132 blk_mq_sched_insert_request(rq, false, run_queue, false);
2138 * blk_mq_try_issue_directly - Try to send a request directly to device driver.
2139 * @hctx: Pointer of the associated hardware queue.
2140 * @rq: Pointer to request to be sent.
2142 * If the device has enough resources to accept a new request now, send the
2143 * request directly to device driver. Else, insert at hctx->dispatch queue, so
2144 * we can try send it another time in the future. Requests inserted at this
2145 * queue have higher priority.
2147 static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
2153 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
2155 hctx_lock(hctx, &srcu_idx);
2157 ret = __blk_mq_try_issue_directly(hctx, rq, false, true);
2158 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
2159 blk_mq_request_bypass_insert(rq, false, true);
2160 else if (ret != BLK_STS_OK)
2161 blk_mq_end_request(rq, ret);
2163 hctx_unlock(hctx, srcu_idx);
2166 blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
2170 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
2172 hctx_lock(hctx, &srcu_idx);
2173 ret = __blk_mq_try_issue_directly(hctx, rq, true, last);
2174 hctx_unlock(hctx, srcu_idx);
2179 void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
2180 struct list_head *list)
2185 while (!list_empty(list)) {
2187 struct request *rq = list_first_entry(list, struct request,
2190 list_del_init(&rq->queuelist);
2191 ret = blk_mq_request_issue_directly(rq, list_empty(list));
2192 if (ret != BLK_STS_OK) {
2193 if (ret == BLK_STS_RESOURCE ||
2194 ret == BLK_STS_DEV_RESOURCE) {
2195 blk_mq_request_bypass_insert(rq, false,
2199 blk_mq_end_request(rq, ret);
2206 * If we didn't flush the entire list, we could have told
2207 * the driver there was more coming, but that turned out to
2210 if ((!list_empty(list) || errors) &&
2211 hctx->queue->mq_ops->commit_rqs && queued)
2212 hctx->queue->mq_ops->commit_rqs(hctx);
2215 static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
2217 list_add_tail(&rq->queuelist, &plug->mq_list);
2219 if (!plug->multiple_queues && !list_is_singular(&plug->mq_list)) {
2220 struct request *tmp;
2222 tmp = list_first_entry(&plug->mq_list, struct request,
2224 if (tmp->q != rq->q)
2225 plug->multiple_queues = true;
2230 * Allow 2x BLK_MAX_REQUEST_COUNT requests on plug queue for multiple
2231 * queues. This is important for md arrays to benefit from merging
2234 static inline unsigned short blk_plug_max_rq_count(struct blk_plug *plug)
2236 if (plug->multiple_queues)
2237 return BLK_MAX_REQUEST_COUNT * 2;
2238 return BLK_MAX_REQUEST_COUNT;
2242 * blk_mq_submit_bio - Create and send a request to block device.
2243 * @bio: Bio pointer.
2245 * Builds up a request structure from @q and @bio and send to the device. The
2246 * request may not be queued directly to hardware if:
2247 * * This request can be merged with another one
2248 * * We want to place request at plug queue for possible future merging
2249 * * There is an IO scheduler active at this queue
2251 * It will not queue the request if there is an error with the bio, or at the
2254 void blk_mq_submit_bio(struct bio *bio)
2256 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
2257 const int is_sync = op_is_sync(bio->bi_opf);
2258 const int is_flush_fua = op_is_flush(bio->bi_opf);
2260 struct blk_plug *plug;
2261 struct request *same_queue_rq = NULL;
2262 unsigned int nr_segs = 1;
2265 blk_queue_bounce(q, &bio);
2266 if (blk_may_split(q, bio))
2267 __blk_queue_split(q, &bio, &nr_segs);
2269 if (!bio_integrity_prep(bio))
2272 if (!is_flush_fua && !blk_queue_nomerges(q) &&
2273 blk_attempt_plug_merge(q, bio, nr_segs, &same_queue_rq))
2276 if (blk_mq_sched_bio_merge(q, bio, nr_segs))
2279 rq_qos_throttle(q, bio);
2281 plug = blk_mq_plug(q, bio);
2282 if (plug && plug->cached_rq) {
2283 rq = plug->cached_rq;
2284 plug->cached_rq = rq->rq_next;
2285 INIT_LIST_HEAD(&rq->queuelist);
2287 struct blk_mq_alloc_data data = {
2290 .cmd_flags = bio->bi_opf,
2294 data.nr_tags = plug->nr_ios;
2296 data.cached_rq = &plug->cached_rq;
2298 rq = __blk_mq_alloc_requests(&data);
2299 if (unlikely(!rq)) {
2300 rq_qos_cleanup(q, bio);
2301 if (bio->bi_opf & REQ_NOWAIT)
2302 bio_wouldblock_error(bio);
2307 trace_block_getrq(bio);
2309 rq_qos_track(q, rq, bio);
2311 blk_mq_bio_to_request(rq, bio, nr_segs);
2313 ret = blk_crypto_init_request(rq);
2314 if (ret != BLK_STS_OK) {
2315 bio->bi_status = ret;
2317 blk_mq_free_request(rq);
2321 if (unlikely(is_flush_fua)) {
2322 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
2323 /* Bypass scheduler for flush requests */
2324 blk_insert_flush(rq);
2325 blk_mq_run_hw_queue(hctx, true);
2326 } else if (plug && (q->nr_hw_queues == 1 ||
2327 blk_mq_is_shared_tags(rq->mq_hctx->flags) ||
2328 q->mq_ops->commit_rqs || !blk_queue_nonrot(q))) {
2330 * Use plugging if we have a ->commit_rqs() hook as well, as
2331 * we know the driver uses bd->last in a smart fashion.
2333 * Use normal plugging if this disk is slow HDD, as sequential
2334 * IO may benefit a lot from plug merging.
2336 unsigned int request_count = plug->rq_count;
2337 struct request *last = NULL;
2340 trace_block_plug(q);
2342 last = list_entry_rq(plug->mq_list.prev);
2344 if (request_count >= blk_plug_max_rq_count(plug) || (last &&
2345 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
2346 blk_flush_plug_list(plug, false);
2347 trace_block_plug(q);
2350 blk_add_rq_to_plug(plug, rq);
2351 } else if (q->elevator) {
2352 /* Insert the request at the IO scheduler queue */
2353 blk_mq_sched_insert_request(rq, false, true, true);
2354 } else if (plug && !blk_queue_nomerges(q)) {
2356 * We do limited plugging. If the bio can be merged, do that.
2357 * Otherwise the existing request in the plug list will be
2358 * issued. So the plug list will have one request at most
2359 * The plug list might get flushed before this. If that happens,
2360 * the plug list is empty, and same_queue_rq is invalid.
2362 if (list_empty(&plug->mq_list))
2363 same_queue_rq = NULL;
2364 if (same_queue_rq) {
2365 list_del_init(&same_queue_rq->queuelist);
2368 blk_add_rq_to_plug(plug, rq);
2369 trace_block_plug(q);
2371 if (same_queue_rq) {
2372 trace_block_unplug(q, 1, true);
2373 blk_mq_try_issue_directly(same_queue_rq->mq_hctx,
2376 } else if ((q->nr_hw_queues > 1 && is_sync) ||
2377 !rq->mq_hctx->dispatch_busy) {
2379 * There is no scheduler and we can try to send directly
2382 blk_mq_try_issue_directly(rq->mq_hctx, rq);
2385 blk_mq_sched_insert_request(rq, false, true, true);
2393 static size_t order_to_size(unsigned int order)
2395 return (size_t)PAGE_SIZE << order;
2398 /* called before freeing request pool in @tags */
2399 static void blk_mq_clear_rq_mapping(struct blk_mq_tags *drv_tags,
2400 struct blk_mq_tags *tags)
2403 unsigned long flags;
2405 /* There is no need to clear a driver tags own mapping */
2406 if (drv_tags == tags)
2409 list_for_each_entry(page, &tags->page_list, lru) {
2410 unsigned long start = (unsigned long)page_address(page);
2411 unsigned long end = start + order_to_size(page->private);
2414 for (i = 0; i < drv_tags->nr_tags; i++) {
2415 struct request *rq = drv_tags->rqs[i];
2416 unsigned long rq_addr = (unsigned long)rq;
2418 if (rq_addr >= start && rq_addr < end) {
2419 WARN_ON_ONCE(refcount_read(&rq->ref) != 0);
2420 cmpxchg(&drv_tags->rqs[i], rq, NULL);
2426 * Wait until all pending iteration is done.
2428 * Request reference is cleared and it is guaranteed to be observed
2429 * after the ->lock is released.
2431 spin_lock_irqsave(&drv_tags->lock, flags);
2432 spin_unlock_irqrestore(&drv_tags->lock, flags);
2435 void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2436 unsigned int hctx_idx)
2438 struct blk_mq_tags *drv_tags;
2441 if (blk_mq_is_shared_tags(set->flags))
2442 drv_tags = set->shared_tags;
2444 drv_tags = set->tags[hctx_idx];
2446 if (tags->static_rqs && set->ops->exit_request) {
2449 for (i = 0; i < tags->nr_tags; i++) {
2450 struct request *rq = tags->static_rqs[i];
2454 set->ops->exit_request(set, rq, hctx_idx);
2455 tags->static_rqs[i] = NULL;
2459 blk_mq_clear_rq_mapping(drv_tags, tags);
2461 while (!list_empty(&tags->page_list)) {
2462 page = list_first_entry(&tags->page_list, struct page, lru);
2463 list_del_init(&page->lru);
2465 * Remove kmemleak object previously allocated in
2466 * blk_mq_alloc_rqs().
2468 kmemleak_free(page_address(page));
2469 __free_pages(page, page->private);
2473 void blk_mq_free_rq_map(struct blk_mq_tags *tags)
2477 kfree(tags->static_rqs);
2478 tags->static_rqs = NULL;
2480 blk_mq_free_tags(tags);
2483 static struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
2484 unsigned int hctx_idx,
2485 unsigned int nr_tags,
2486 unsigned int reserved_tags)
2488 struct blk_mq_tags *tags;
2491 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
2492 if (node == NUMA_NO_NODE)
2493 node = set->numa_node;
2495 tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
2496 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
2500 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2501 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2504 blk_mq_free_tags(tags);
2508 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2509 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2511 if (!tags->static_rqs) {
2513 blk_mq_free_tags(tags);
2520 static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2521 unsigned int hctx_idx, int node)
2525 if (set->ops->init_request) {
2526 ret = set->ops->init_request(set, rq, hctx_idx, node);
2531 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
2535 static int blk_mq_alloc_rqs(struct blk_mq_tag_set *set,
2536 struct blk_mq_tags *tags,
2537 unsigned int hctx_idx, unsigned int depth)
2539 unsigned int i, j, entries_per_page, max_order = 4;
2540 size_t rq_size, left;
2543 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
2544 if (node == NUMA_NO_NODE)
2545 node = set->numa_node;
2547 INIT_LIST_HEAD(&tags->page_list);
2550 * rq_size is the size of the request plus driver payload, rounded
2551 * to the cacheline size
2553 rq_size = round_up(sizeof(struct request) + set->cmd_size,
2555 left = rq_size * depth;
2557 for (i = 0; i < depth; ) {
2558 int this_order = max_order;
2563 while (this_order && left < order_to_size(this_order - 1))
2567 page = alloc_pages_node(node,
2568 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
2574 if (order_to_size(this_order) < rq_size)
2581 page->private = this_order;
2582 list_add_tail(&page->lru, &tags->page_list);
2584 p = page_address(page);
2586 * Allow kmemleak to scan these pages as they contain pointers
2587 * to additional allocations like via ops->init_request().
2589 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
2590 entries_per_page = order_to_size(this_order) / rq_size;
2591 to_do = min(entries_per_page, depth - i);
2592 left -= to_do * rq_size;
2593 for (j = 0; j < to_do; j++) {
2594 struct request *rq = p;
2596 tags->static_rqs[i] = rq;
2597 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
2598 tags->static_rqs[i] = NULL;
2609 blk_mq_free_rqs(set, tags, hctx_idx);
2613 struct rq_iter_data {
2614 struct blk_mq_hw_ctx *hctx;
2618 static bool blk_mq_has_request(struct request *rq, void *data, bool reserved)
2620 struct rq_iter_data *iter_data = data;
2622 if (rq->mq_hctx != iter_data->hctx)
2624 iter_data->has_rq = true;
2628 static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
2630 struct blk_mq_tags *tags = hctx->sched_tags ?
2631 hctx->sched_tags : hctx->tags;
2632 struct rq_iter_data data = {
2636 blk_mq_all_tag_iter(tags, blk_mq_has_request, &data);
2640 static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu,
2641 struct blk_mq_hw_ctx *hctx)
2643 if (cpumask_next_and(-1, hctx->cpumask, cpu_online_mask) != cpu)
2645 if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids)
2650 static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
2652 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2653 struct blk_mq_hw_ctx, cpuhp_online);
2655 if (!cpumask_test_cpu(cpu, hctx->cpumask) ||
2656 !blk_mq_last_cpu_in_hctx(cpu, hctx))
2660 * Prevent new request from being allocated on the current hctx.
2662 * The smp_mb__after_atomic() Pairs with the implied barrier in
2663 * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is
2664 * seen once we return from the tag allocator.
2666 set_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2667 smp_mb__after_atomic();
2670 * Try to grab a reference to the queue and wait for any outstanding
2671 * requests. If we could not grab a reference the queue has been
2672 * frozen and there are no requests.
2674 if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) {
2675 while (blk_mq_hctx_has_requests(hctx))
2677 percpu_ref_put(&hctx->queue->q_usage_counter);
2683 static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node)
2685 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2686 struct blk_mq_hw_ctx, cpuhp_online);
2688 if (cpumask_test_cpu(cpu, hctx->cpumask))
2689 clear_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2694 * 'cpu' is going away. splice any existing rq_list entries from this
2695 * software queue to the hw queue dispatch list, and ensure that it
2698 static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
2700 struct blk_mq_hw_ctx *hctx;
2701 struct blk_mq_ctx *ctx;
2703 enum hctx_type type;
2705 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
2706 if (!cpumask_test_cpu(cpu, hctx->cpumask))
2709 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
2712 spin_lock(&ctx->lock);
2713 if (!list_empty(&ctx->rq_lists[type])) {
2714 list_splice_init(&ctx->rq_lists[type], &tmp);
2715 blk_mq_hctx_clear_pending(hctx, ctx);
2717 spin_unlock(&ctx->lock);
2719 if (list_empty(&tmp))
2722 spin_lock(&hctx->lock);
2723 list_splice_tail_init(&tmp, &hctx->dispatch);
2724 spin_unlock(&hctx->lock);
2726 blk_mq_run_hw_queue(hctx, true);
2730 static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
2732 if (!(hctx->flags & BLK_MQ_F_STACKING))
2733 cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2734 &hctx->cpuhp_online);
2735 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
2740 * Before freeing hw queue, clearing the flush request reference in
2741 * tags->rqs[] for avoiding potential UAF.
2743 static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags,
2744 unsigned int queue_depth, struct request *flush_rq)
2747 unsigned long flags;
2749 /* The hw queue may not be mapped yet */
2753 WARN_ON_ONCE(refcount_read(&flush_rq->ref) != 0);
2755 for (i = 0; i < queue_depth; i++)
2756 cmpxchg(&tags->rqs[i], flush_rq, NULL);
2759 * Wait until all pending iteration is done.
2761 * Request reference is cleared and it is guaranteed to be observed
2762 * after the ->lock is released.
2764 spin_lock_irqsave(&tags->lock, flags);
2765 spin_unlock_irqrestore(&tags->lock, flags);
2768 /* hctx->ctxs will be freed in queue's release handler */
2769 static void blk_mq_exit_hctx(struct request_queue *q,
2770 struct blk_mq_tag_set *set,
2771 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
2773 struct request *flush_rq = hctx->fq->flush_rq;
2775 if (blk_mq_hw_queue_mapped(hctx))
2776 blk_mq_tag_idle(hctx);
2778 blk_mq_clear_flush_rq_mapping(set->tags[hctx_idx],
2779 set->queue_depth, flush_rq);
2780 if (set->ops->exit_request)
2781 set->ops->exit_request(set, flush_rq, hctx_idx);
2783 if (set->ops->exit_hctx)
2784 set->ops->exit_hctx(hctx, hctx_idx);
2786 blk_mq_remove_cpuhp(hctx);
2788 spin_lock(&q->unused_hctx_lock);
2789 list_add(&hctx->hctx_list, &q->unused_hctx_list);
2790 spin_unlock(&q->unused_hctx_lock);
2793 static void blk_mq_exit_hw_queues(struct request_queue *q,
2794 struct blk_mq_tag_set *set, int nr_queue)
2796 struct blk_mq_hw_ctx *hctx;
2799 queue_for_each_hw_ctx(q, hctx, i) {
2802 blk_mq_debugfs_unregister_hctx(hctx);
2803 blk_mq_exit_hctx(q, set, hctx, i);
2807 static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
2809 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
2811 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
2812 __alignof__(struct blk_mq_hw_ctx)) !=
2813 sizeof(struct blk_mq_hw_ctx));
2815 if (tag_set->flags & BLK_MQ_F_BLOCKING)
2816 hw_ctx_size += sizeof(struct srcu_struct);
2821 static int blk_mq_init_hctx(struct request_queue *q,
2822 struct blk_mq_tag_set *set,
2823 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
2825 hctx->queue_num = hctx_idx;
2827 if (!(hctx->flags & BLK_MQ_F_STACKING))
2828 cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2829 &hctx->cpuhp_online);
2830 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
2832 hctx->tags = set->tags[hctx_idx];
2834 if (set->ops->init_hctx &&
2835 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
2836 goto unregister_cpu_notifier;
2838 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
2844 if (set->ops->exit_hctx)
2845 set->ops->exit_hctx(hctx, hctx_idx);
2846 unregister_cpu_notifier:
2847 blk_mq_remove_cpuhp(hctx);
2851 static struct blk_mq_hw_ctx *
2852 blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
2855 struct blk_mq_hw_ctx *hctx;
2856 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
2858 hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node);
2860 goto fail_alloc_hctx;
2862 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
2865 atomic_set(&hctx->nr_active, 0);
2866 if (node == NUMA_NO_NODE)
2867 node = set->numa_node;
2868 hctx->numa_node = node;
2870 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
2871 spin_lock_init(&hctx->lock);
2872 INIT_LIST_HEAD(&hctx->dispatch);
2874 hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED;
2876 INIT_LIST_HEAD(&hctx->hctx_list);
2879 * Allocate space for all possible cpus to avoid allocation at
2882 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
2887 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
2888 gfp, node, false, false))
2892 spin_lock_init(&hctx->dispatch_wait_lock);
2893 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
2894 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
2896 hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp);
2900 if (hctx->flags & BLK_MQ_F_BLOCKING)
2901 init_srcu_struct(hctx->srcu);
2902 blk_mq_hctx_kobj_init(hctx);
2907 sbitmap_free(&hctx->ctx_map);
2911 free_cpumask_var(hctx->cpumask);
2918 static void blk_mq_init_cpu_queues(struct request_queue *q,
2919 unsigned int nr_hw_queues)
2921 struct blk_mq_tag_set *set = q->tag_set;
2924 for_each_possible_cpu(i) {
2925 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
2926 struct blk_mq_hw_ctx *hctx;
2930 spin_lock_init(&__ctx->lock);
2931 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
2932 INIT_LIST_HEAD(&__ctx->rq_lists[k]);
2937 * Set local node, IFF we have more than one hw queue. If
2938 * not, we remain on the home node of the device
2940 for (j = 0; j < set->nr_maps; j++) {
2941 hctx = blk_mq_map_queue_type(q, j, i);
2942 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
2943 hctx->numa_node = cpu_to_node(i);
2948 struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
2949 unsigned int hctx_idx,
2952 struct blk_mq_tags *tags;
2955 tags = blk_mq_alloc_rq_map(set, hctx_idx, depth, set->reserved_tags);
2959 ret = blk_mq_alloc_rqs(set, tags, hctx_idx, depth);
2961 blk_mq_free_rq_map(tags);
2968 static bool __blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
2971 if (blk_mq_is_shared_tags(set->flags)) {
2972 set->tags[hctx_idx] = set->shared_tags;
2977 set->tags[hctx_idx] = blk_mq_alloc_map_and_rqs(set, hctx_idx,
2980 return set->tags[hctx_idx];
2983 void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
2984 struct blk_mq_tags *tags,
2985 unsigned int hctx_idx)
2988 blk_mq_free_rqs(set, tags, hctx_idx);
2989 blk_mq_free_rq_map(tags);
2993 static void __blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
2994 unsigned int hctx_idx)
2996 if (!blk_mq_is_shared_tags(set->flags))
2997 blk_mq_free_map_and_rqs(set, set->tags[hctx_idx], hctx_idx);
2999 set->tags[hctx_idx] = NULL;
3002 static void blk_mq_map_swqueue(struct request_queue *q)
3004 unsigned int i, j, hctx_idx;
3005 struct blk_mq_hw_ctx *hctx;
3006 struct blk_mq_ctx *ctx;
3007 struct blk_mq_tag_set *set = q->tag_set;
3009 queue_for_each_hw_ctx(q, hctx, i) {
3010 cpumask_clear(hctx->cpumask);
3012 hctx->dispatch_from = NULL;
3016 * Map software to hardware queues.
3018 * If the cpu isn't present, the cpu is mapped to first hctx.
3020 for_each_possible_cpu(i) {
3022 ctx = per_cpu_ptr(q->queue_ctx, i);
3023 for (j = 0; j < set->nr_maps; j++) {
3024 if (!set->map[j].nr_queues) {
3025 ctx->hctxs[j] = blk_mq_map_queue_type(q,
3026 HCTX_TYPE_DEFAULT, i);
3029 hctx_idx = set->map[j].mq_map[i];
3030 /* unmapped hw queue can be remapped after CPU topo changed */
3031 if (!set->tags[hctx_idx] &&
3032 !__blk_mq_alloc_map_and_rqs(set, hctx_idx)) {
3034 * If tags initialization fail for some hctx,
3035 * that hctx won't be brought online. In this
3036 * case, remap the current ctx to hctx[0] which
3037 * is guaranteed to always have tags allocated
3039 set->map[j].mq_map[i] = 0;
3042 hctx = blk_mq_map_queue_type(q, j, i);
3043 ctx->hctxs[j] = hctx;
3045 * If the CPU is already set in the mask, then we've
3046 * mapped this one already. This can happen if
3047 * devices share queues across queue maps.
3049 if (cpumask_test_cpu(i, hctx->cpumask))
3052 cpumask_set_cpu(i, hctx->cpumask);
3054 ctx->index_hw[hctx->type] = hctx->nr_ctx;
3055 hctx->ctxs[hctx->nr_ctx++] = ctx;
3058 * If the nr_ctx type overflows, we have exceeded the
3059 * amount of sw queues we can support.
3061 BUG_ON(!hctx->nr_ctx);
3064 for (; j < HCTX_MAX_TYPES; j++)
3065 ctx->hctxs[j] = blk_mq_map_queue_type(q,
3066 HCTX_TYPE_DEFAULT, i);
3069 queue_for_each_hw_ctx(q, hctx, i) {
3071 * If no software queues are mapped to this hardware queue,
3072 * disable it and free the request entries.
3074 if (!hctx->nr_ctx) {
3075 /* Never unmap queue 0. We need it as a
3076 * fallback in case of a new remap fails
3080 __blk_mq_free_map_and_rqs(set, i);
3086 hctx->tags = set->tags[i];
3087 WARN_ON(!hctx->tags);
3090 * Set the map size to the number of mapped software queues.
3091 * This is more accurate and more efficient than looping
3092 * over all possibly mapped software queues.
3094 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
3097 * Initialize batch roundrobin counts
3099 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
3100 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
3105 * Caller needs to ensure that we're either frozen/quiesced, or that
3106 * the queue isn't live yet.
3108 static void queue_set_hctx_shared(struct request_queue *q, bool shared)
3110 struct blk_mq_hw_ctx *hctx;
3113 queue_for_each_hw_ctx(q, hctx, i) {
3115 hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
3117 blk_mq_tag_idle(hctx);
3118 hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
3123 static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set,
3126 struct request_queue *q;
3128 lockdep_assert_held(&set->tag_list_lock);
3130 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3131 blk_mq_freeze_queue(q);
3132 queue_set_hctx_shared(q, shared);
3133 blk_mq_unfreeze_queue(q);
3137 static void blk_mq_del_queue_tag_set(struct request_queue *q)
3139 struct blk_mq_tag_set *set = q->tag_set;
3141 mutex_lock(&set->tag_list_lock);
3142 list_del(&q->tag_set_list);
3143 if (list_is_singular(&set->tag_list)) {
3144 /* just transitioned to unshared */
3145 set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
3146 /* update existing queue */
3147 blk_mq_update_tag_set_shared(set, false);
3149 mutex_unlock(&set->tag_list_lock);
3150 INIT_LIST_HEAD(&q->tag_set_list);
3153 static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
3154 struct request_queue *q)
3156 mutex_lock(&set->tag_list_lock);
3159 * Check to see if we're transitioning to shared (from 1 to 2 queues).
3161 if (!list_empty(&set->tag_list) &&
3162 !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
3163 set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
3164 /* update existing queue */
3165 blk_mq_update_tag_set_shared(set, true);
3167 if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
3168 queue_set_hctx_shared(q, true);
3169 list_add_tail(&q->tag_set_list, &set->tag_list);
3171 mutex_unlock(&set->tag_list_lock);
3174 /* All allocations will be freed in release handler of q->mq_kobj */
3175 static int blk_mq_alloc_ctxs(struct request_queue *q)
3177 struct blk_mq_ctxs *ctxs;
3180 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
3184 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
3185 if (!ctxs->queue_ctx)
3188 for_each_possible_cpu(cpu) {
3189 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
3193 q->mq_kobj = &ctxs->kobj;
3194 q->queue_ctx = ctxs->queue_ctx;
3203 * It is the actual release handler for mq, but we do it from
3204 * request queue's release handler for avoiding use-after-free
3205 * and headache because q->mq_kobj shouldn't have been introduced,
3206 * but we can't group ctx/kctx kobj without it.
3208 void blk_mq_release(struct request_queue *q)
3210 struct blk_mq_hw_ctx *hctx, *next;
3213 queue_for_each_hw_ctx(q, hctx, i)
3214 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
3216 /* all hctx are in .unused_hctx_list now */
3217 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
3218 list_del_init(&hctx->hctx_list);
3219 kobject_put(&hctx->kobj);
3222 kfree(q->queue_hw_ctx);
3225 * release .mq_kobj and sw queue's kobject now because
3226 * both share lifetime with request queue.
3228 blk_mq_sysfs_deinit(q);
3231 static struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set,
3234 struct request_queue *q;
3237 q = blk_alloc_queue(set->numa_node);
3239 return ERR_PTR(-ENOMEM);
3240 q->queuedata = queuedata;
3241 ret = blk_mq_init_allocated_queue(set, q);
3243 blk_cleanup_queue(q);
3244 return ERR_PTR(ret);
3249 struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
3251 return blk_mq_init_queue_data(set, NULL);
3253 EXPORT_SYMBOL(blk_mq_init_queue);
3255 struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set, void *queuedata,
3256 struct lock_class_key *lkclass)
3258 struct request_queue *q;
3259 struct gendisk *disk;
3261 q = blk_mq_init_queue_data(set, queuedata);
3265 disk = __alloc_disk_node(q, set->numa_node, lkclass);
3267 blk_cleanup_queue(q);
3268 return ERR_PTR(-ENOMEM);
3272 EXPORT_SYMBOL(__blk_mq_alloc_disk);
3274 static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
3275 struct blk_mq_tag_set *set, struct request_queue *q,
3276 int hctx_idx, int node)
3278 struct blk_mq_hw_ctx *hctx = NULL, *tmp;
3280 /* reuse dead hctx first */
3281 spin_lock(&q->unused_hctx_lock);
3282 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
3283 if (tmp->numa_node == node) {
3289 list_del_init(&hctx->hctx_list);
3290 spin_unlock(&q->unused_hctx_lock);
3293 hctx = blk_mq_alloc_hctx(q, set, node);
3297 if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
3303 kobject_put(&hctx->kobj);
3308 static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
3309 struct request_queue *q)
3312 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
3314 if (q->nr_hw_queues < set->nr_hw_queues) {
3315 struct blk_mq_hw_ctx **new_hctxs;
3317 new_hctxs = kcalloc_node(set->nr_hw_queues,
3318 sizeof(*new_hctxs), GFP_KERNEL,
3323 memcpy(new_hctxs, hctxs, q->nr_hw_queues *
3325 q->queue_hw_ctx = new_hctxs;
3330 /* protect against switching io scheduler */
3331 mutex_lock(&q->sysfs_lock);
3332 for (i = 0; i < set->nr_hw_queues; i++) {
3334 struct blk_mq_hw_ctx *hctx;
3336 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i);
3338 * If the hw queue has been mapped to another numa node,
3339 * we need to realloc the hctx. If allocation fails, fallback
3340 * to use the previous one.
3342 if (hctxs[i] && (hctxs[i]->numa_node == node))
3345 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
3348 blk_mq_exit_hctx(q, set, hctxs[i], i);
3352 pr_warn("Allocate new hctx on node %d fails,\
3353 fallback to previous one on node %d\n",
3354 node, hctxs[i]->numa_node);
3360 * Increasing nr_hw_queues fails. Free the newly allocated
3361 * hctxs and keep the previous q->nr_hw_queues.
3363 if (i != set->nr_hw_queues) {
3364 j = q->nr_hw_queues;
3368 end = q->nr_hw_queues;
3369 q->nr_hw_queues = set->nr_hw_queues;
3372 for (; j < end; j++) {
3373 struct blk_mq_hw_ctx *hctx = hctxs[j];
3376 __blk_mq_free_map_and_rqs(set, j);
3377 blk_mq_exit_hctx(q, set, hctx, j);
3381 mutex_unlock(&q->sysfs_lock);
3384 int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
3385 struct request_queue *q)
3387 /* mark the queue as mq asap */
3388 q->mq_ops = set->ops;
3390 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
3391 blk_mq_poll_stats_bkt,
3392 BLK_MQ_POLL_STATS_BKTS, q);
3396 if (blk_mq_alloc_ctxs(q))
3399 /* init q->mq_kobj and sw queues' kobjects */
3400 blk_mq_sysfs_init(q);
3402 INIT_LIST_HEAD(&q->unused_hctx_list);
3403 spin_lock_init(&q->unused_hctx_lock);
3405 blk_mq_realloc_hw_ctxs(set, q);
3406 if (!q->nr_hw_queues)
3409 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
3410 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
3414 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
3415 if (set->nr_maps > HCTX_TYPE_POLL &&
3416 set->map[HCTX_TYPE_POLL].nr_queues)
3417 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
3419 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
3420 INIT_LIST_HEAD(&q->requeue_list);
3421 spin_lock_init(&q->requeue_lock);
3423 q->nr_requests = set->queue_depth;
3426 * Default to classic polling
3428 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
3430 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
3431 blk_mq_add_queue_tag_set(set, q);
3432 blk_mq_map_swqueue(q);
3436 kfree(q->queue_hw_ctx);
3437 q->nr_hw_queues = 0;
3438 blk_mq_sysfs_deinit(q);
3440 blk_stat_free_callback(q->poll_cb);
3446 EXPORT_SYMBOL(blk_mq_init_allocated_queue);
3448 /* tags can _not_ be used after returning from blk_mq_exit_queue */
3449 void blk_mq_exit_queue(struct request_queue *q)
3451 struct blk_mq_tag_set *set = q->tag_set;
3453 /* Checks hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED. */
3454 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
3455 /* May clear BLK_MQ_F_TAG_QUEUE_SHARED in hctx->flags. */
3456 blk_mq_del_queue_tag_set(q);
3459 static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
3463 if (blk_mq_is_shared_tags(set->flags)) {
3464 set->shared_tags = blk_mq_alloc_map_and_rqs(set,
3467 if (!set->shared_tags)
3471 for (i = 0; i < set->nr_hw_queues; i++) {
3472 if (!__blk_mq_alloc_map_and_rqs(set, i))
3481 __blk_mq_free_map_and_rqs(set, i);
3483 if (blk_mq_is_shared_tags(set->flags)) {
3484 blk_mq_free_map_and_rqs(set, set->shared_tags,
3485 BLK_MQ_NO_HCTX_IDX);
3492 * Allocate the request maps associated with this tag_set. Note that this
3493 * may reduce the depth asked for, if memory is tight. set->queue_depth
3494 * will be updated to reflect the allocated depth.
3496 static int blk_mq_alloc_set_map_and_rqs(struct blk_mq_tag_set *set)
3501 depth = set->queue_depth;
3503 err = __blk_mq_alloc_rq_maps(set);
3507 set->queue_depth >>= 1;
3508 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
3512 } while (set->queue_depth);
3514 if (!set->queue_depth || err) {
3515 pr_err("blk-mq: failed to allocate request map\n");
3519 if (depth != set->queue_depth)
3520 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
3521 depth, set->queue_depth);
3526 static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
3529 * blk_mq_map_queues() and multiple .map_queues() implementations
3530 * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the
3531 * number of hardware queues.
3533 if (set->nr_maps == 1)
3534 set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues;
3536 if (set->ops->map_queues && !is_kdump_kernel()) {
3540 * transport .map_queues is usually done in the following
3543 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
3544 * mask = get_cpu_mask(queue)
3545 * for_each_cpu(cpu, mask)
3546 * set->map[x].mq_map[cpu] = queue;
3549 * When we need to remap, the table has to be cleared for
3550 * killing stale mapping since one CPU may not be mapped
3553 for (i = 0; i < set->nr_maps; i++)
3554 blk_mq_clear_mq_map(&set->map[i]);
3556 return set->ops->map_queues(set);
3558 BUG_ON(set->nr_maps > 1);
3559 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
3563 static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set,
3564 int cur_nr_hw_queues, int new_nr_hw_queues)
3566 struct blk_mq_tags **new_tags;
3568 if (cur_nr_hw_queues >= new_nr_hw_queues)
3571 new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *),
3572 GFP_KERNEL, set->numa_node);
3577 memcpy(new_tags, set->tags, cur_nr_hw_queues *
3578 sizeof(*set->tags));
3580 set->tags = new_tags;
3581 set->nr_hw_queues = new_nr_hw_queues;
3586 static int blk_mq_alloc_tag_set_tags(struct blk_mq_tag_set *set,
3587 int new_nr_hw_queues)
3589 return blk_mq_realloc_tag_set_tags(set, 0, new_nr_hw_queues);
3593 * Alloc a tag set to be associated with one or more request queues.
3594 * May fail with EINVAL for various error conditions. May adjust the
3595 * requested depth down, if it's too large. In that case, the set
3596 * value will be stored in set->queue_depth.
3598 int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
3602 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
3604 if (!set->nr_hw_queues)
3606 if (!set->queue_depth)
3608 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
3611 if (!set->ops->queue_rq)
3614 if (!set->ops->get_budget ^ !set->ops->put_budget)
3617 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
3618 pr_info("blk-mq: reduced tag depth to %u\n",
3620 set->queue_depth = BLK_MQ_MAX_DEPTH;
3625 else if (set->nr_maps > HCTX_MAX_TYPES)
3629 * If a crashdump is active, then we are potentially in a very
3630 * memory constrained environment. Limit us to 1 queue and
3631 * 64 tags to prevent using too much memory.
3633 if (is_kdump_kernel()) {
3634 set->nr_hw_queues = 1;
3636 set->queue_depth = min(64U, set->queue_depth);
3639 * There is no use for more h/w queues than cpus if we just have
3642 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
3643 set->nr_hw_queues = nr_cpu_ids;
3645 if (blk_mq_alloc_tag_set_tags(set, set->nr_hw_queues) < 0)
3649 for (i = 0; i < set->nr_maps; i++) {
3650 set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
3651 sizeof(set->map[i].mq_map[0]),
3652 GFP_KERNEL, set->numa_node);
3653 if (!set->map[i].mq_map)
3654 goto out_free_mq_map;
3655 set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues;
3658 ret = blk_mq_update_queue_map(set);
3660 goto out_free_mq_map;
3662 ret = blk_mq_alloc_set_map_and_rqs(set);
3664 goto out_free_mq_map;
3666 mutex_init(&set->tag_list_lock);
3667 INIT_LIST_HEAD(&set->tag_list);
3672 for (i = 0; i < set->nr_maps; i++) {
3673 kfree(set->map[i].mq_map);
3674 set->map[i].mq_map = NULL;
3680 EXPORT_SYMBOL(blk_mq_alloc_tag_set);
3682 /* allocate and initialize a tagset for a simple single-queue device */
3683 int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set,
3684 const struct blk_mq_ops *ops, unsigned int queue_depth,
3685 unsigned int set_flags)
3687 memset(set, 0, sizeof(*set));
3689 set->nr_hw_queues = 1;
3691 set->queue_depth = queue_depth;
3692 set->numa_node = NUMA_NO_NODE;
3693 set->flags = set_flags;
3694 return blk_mq_alloc_tag_set(set);
3696 EXPORT_SYMBOL_GPL(blk_mq_alloc_sq_tag_set);
3698 void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
3702 for (i = 0; i < set->nr_hw_queues; i++)
3703 __blk_mq_free_map_and_rqs(set, i);
3705 if (blk_mq_is_shared_tags(set->flags)) {
3706 blk_mq_free_map_and_rqs(set, set->shared_tags,
3707 BLK_MQ_NO_HCTX_IDX);
3710 for (j = 0; j < set->nr_maps; j++) {
3711 kfree(set->map[j].mq_map);
3712 set->map[j].mq_map = NULL;
3718 EXPORT_SYMBOL(blk_mq_free_tag_set);
3720 int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
3722 struct blk_mq_tag_set *set = q->tag_set;
3723 struct blk_mq_hw_ctx *hctx;
3729 if (q->nr_requests == nr)
3732 blk_mq_freeze_queue(q);
3733 blk_mq_quiesce_queue(q);
3736 queue_for_each_hw_ctx(q, hctx, i) {
3740 * If we're using an MQ scheduler, just update the scheduler
3741 * queue depth. This is similar to what the old code would do.
3743 if (hctx->sched_tags) {
3744 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
3747 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
3752 if (q->elevator && q->elevator->type->ops.depth_updated)
3753 q->elevator->type->ops.depth_updated(hctx);
3756 q->nr_requests = nr;
3757 if (blk_mq_is_shared_tags(set->flags)) {
3759 blk_mq_tag_update_sched_shared_tags(q);
3761 blk_mq_tag_resize_shared_tags(set, nr);
3765 blk_mq_unquiesce_queue(q);
3766 blk_mq_unfreeze_queue(q);
3772 * request_queue and elevator_type pair.
3773 * It is just used by __blk_mq_update_nr_hw_queues to cache
3774 * the elevator_type associated with a request_queue.
3776 struct blk_mq_qe_pair {
3777 struct list_head node;
3778 struct request_queue *q;
3779 struct elevator_type *type;
3783 * Cache the elevator_type in qe pair list and switch the
3784 * io scheduler to 'none'
3786 static bool blk_mq_elv_switch_none(struct list_head *head,
3787 struct request_queue *q)
3789 struct blk_mq_qe_pair *qe;
3794 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
3798 INIT_LIST_HEAD(&qe->node);
3800 qe->type = q->elevator->type;
3801 list_add(&qe->node, head);
3803 mutex_lock(&q->sysfs_lock);
3805 * After elevator_switch_mq, the previous elevator_queue will be
3806 * released by elevator_release. The reference of the io scheduler
3807 * module get by elevator_get will also be put. So we need to get
3808 * a reference of the io scheduler module here to prevent it to be
3811 __module_get(qe->type->elevator_owner);
3812 elevator_switch_mq(q, NULL);
3813 mutex_unlock(&q->sysfs_lock);
3818 static void blk_mq_elv_switch_back(struct list_head *head,
3819 struct request_queue *q)
3821 struct blk_mq_qe_pair *qe;
3822 struct elevator_type *t = NULL;
3824 list_for_each_entry(qe, head, node)
3833 list_del(&qe->node);
3836 mutex_lock(&q->sysfs_lock);
3837 elevator_switch_mq(q, t);
3838 mutex_unlock(&q->sysfs_lock);
3841 static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
3844 struct request_queue *q;
3846 int prev_nr_hw_queues;
3848 lockdep_assert_held(&set->tag_list_lock);
3850 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
3851 nr_hw_queues = nr_cpu_ids;
3852 if (nr_hw_queues < 1)
3854 if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues)
3857 list_for_each_entry(q, &set->tag_list, tag_set_list)
3858 blk_mq_freeze_queue(q);
3860 * Switch IO scheduler to 'none', cleaning up the data associated
3861 * with the previous scheduler. We will switch back once we are done
3862 * updating the new sw to hw queue mappings.
3864 list_for_each_entry(q, &set->tag_list, tag_set_list)
3865 if (!blk_mq_elv_switch_none(&head, q))
3868 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3869 blk_mq_debugfs_unregister_hctxs(q);
3870 blk_mq_sysfs_unregister(q);
3873 prev_nr_hw_queues = set->nr_hw_queues;
3874 if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) <
3878 set->nr_hw_queues = nr_hw_queues;
3880 blk_mq_update_queue_map(set);
3881 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3882 blk_mq_realloc_hw_ctxs(set, q);
3883 if (q->nr_hw_queues != set->nr_hw_queues) {
3884 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
3885 nr_hw_queues, prev_nr_hw_queues);
3886 set->nr_hw_queues = prev_nr_hw_queues;
3887 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
3890 blk_mq_map_swqueue(q);
3894 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3895 blk_mq_sysfs_register(q);
3896 blk_mq_debugfs_register_hctxs(q);
3900 list_for_each_entry(q, &set->tag_list, tag_set_list)
3901 blk_mq_elv_switch_back(&head, q);
3903 list_for_each_entry(q, &set->tag_list, tag_set_list)
3904 blk_mq_unfreeze_queue(q);
3907 void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
3909 mutex_lock(&set->tag_list_lock);
3910 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
3911 mutex_unlock(&set->tag_list_lock);
3913 EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
3915 /* Enable polling stats and return whether they were already enabled. */
3916 static bool blk_poll_stats_enable(struct request_queue *q)
3918 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
3919 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
3921 blk_stat_add_callback(q, q->poll_cb);
3925 static void blk_mq_poll_stats_start(struct request_queue *q)
3928 * We don't arm the callback if polling stats are not enabled or the
3929 * callback is already active.
3931 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
3932 blk_stat_is_active(q->poll_cb))
3935 blk_stat_activate_msecs(q->poll_cb, 100);
3938 static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
3940 struct request_queue *q = cb->data;
3943 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
3944 if (cb->stat[bucket].nr_samples)
3945 q->poll_stat[bucket] = cb->stat[bucket];
3949 static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
3952 unsigned long ret = 0;
3956 * If stats collection isn't on, don't sleep but turn it on for
3959 if (!blk_poll_stats_enable(q))
3963 * As an optimistic guess, use half of the mean service time
3964 * for this type of request. We can (and should) make this smarter.
3965 * For instance, if the completion latencies are tight, we can
3966 * get closer than just half the mean. This is especially
3967 * important on devices where the completion latencies are longer
3968 * than ~10 usec. We do use the stats for the relevant IO size
3969 * if available which does lead to better estimates.
3971 bucket = blk_mq_poll_stats_bkt(rq);
3975 if (q->poll_stat[bucket].nr_samples)
3976 ret = (q->poll_stat[bucket].mean + 1) / 2;
3981 static bool blk_mq_poll_hybrid(struct request_queue *q, blk_qc_t qc)
3983 struct blk_mq_hw_ctx *hctx = blk_qc_to_hctx(q, qc);
3984 struct request *rq = blk_qc_to_rq(hctx, qc);
3985 struct hrtimer_sleeper hs;
3986 enum hrtimer_mode mode;
3991 * If a request has completed on queue that uses an I/O scheduler, we
3992 * won't get back a request from blk_qc_to_rq.
3994 if (!rq || (rq->rq_flags & RQF_MQ_POLL_SLEPT))
3998 * If we get here, hybrid polling is enabled. Hence poll_nsec can be:
4000 * 0: use half of prev avg
4001 * >0: use this specific value
4003 if (q->poll_nsec > 0)
4004 nsecs = q->poll_nsec;
4006 nsecs = blk_mq_poll_nsecs(q, rq);
4011 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
4014 * This will be replaced with the stats tracking code, using
4015 * 'avg_completion_time / 2' as the pre-sleep target.
4019 mode = HRTIMER_MODE_REL;
4020 hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode);
4021 hrtimer_set_expires(&hs.timer, kt);
4024 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
4026 set_current_state(TASK_UNINTERRUPTIBLE);
4027 hrtimer_sleeper_start_expires(&hs, mode);
4030 hrtimer_cancel(&hs.timer);
4031 mode = HRTIMER_MODE_ABS;
4032 } while (hs.task && !signal_pending(current));
4034 __set_current_state(TASK_RUNNING);
4035 destroy_hrtimer_on_stack(&hs.timer);
4038 * If we sleep, have the caller restart the poll loop to reset the
4039 * state. Like for the other success return cases, the caller is
4040 * responsible for checking if the IO completed. If the IO isn't
4041 * complete, we'll get called again and will go straight to the busy
4047 static int blk_mq_poll_classic(struct request_queue *q, blk_qc_t cookie,
4050 struct blk_mq_hw_ctx *hctx = blk_qc_to_hctx(q, cookie);
4051 long state = get_current_state();
4054 hctx->poll_considered++;
4057 hctx->poll_invoked++;
4059 ret = q->mq_ops->poll(hctx);
4061 hctx->poll_success++;
4062 __set_current_state(TASK_RUNNING);
4066 if (signal_pending_state(state, current))
4067 __set_current_state(TASK_RUNNING);
4068 if (task_is_running(current))
4071 if (ret < 0 || (flags & BLK_POLL_ONESHOT))
4074 } while (!need_resched());
4076 __set_current_state(TASK_RUNNING);
4080 int blk_mq_poll(struct request_queue *q, blk_qc_t cookie, unsigned int flags)
4082 if (!(flags & BLK_POLL_NOSLEEP) &&
4083 q->poll_nsec != BLK_MQ_POLL_CLASSIC) {
4084 if (blk_mq_poll_hybrid(q, cookie))
4087 return blk_mq_poll_classic(q, cookie, flags);
4090 unsigned int blk_mq_rq_cpu(struct request *rq)
4092 return rq->mq_ctx->cpu;
4094 EXPORT_SYMBOL(blk_mq_rq_cpu);
4096 static int __init blk_mq_init(void)
4100 for_each_possible_cpu(i)
4101 init_llist_head(&per_cpu(blk_cpu_done, i));
4102 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
4104 cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
4105 "block/softirq:dead", NULL,
4106 blk_softirq_cpu_dead);
4107 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
4108 blk_mq_hctx_notify_dead);
4109 cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online",
4110 blk_mq_hctx_notify_online,
4111 blk_mq_hctx_notify_offline);
4114 subsys_initcall(blk_mq_init);