blob: 24a10c973f79fcf143a2e3a0f104d2c78de24a0b [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Jens Axboe75bb4622014-05-28 10:15:41 -06002/*
3 * Block multiqueue core code
4 *
5 * Copyright (C) 2013-2014 Jens Axboe
6 * Copyright (C) 2013-2014 Christoph Hellwig
7 */
Jens Axboe320ae512013-10-24 09:20:05 +01008#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>
Christoph Hellwigfe45e632021-09-20 14:33:27 +020013#include <linux/blk-integrity.h>
Catalin Marinasf75782e2015-09-14 18:16:02 +010014#include <linux/kmemleak.h>
Jens Axboe320ae512013-10-24 09:20:05 +010015#include <linux/mm.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/workqueue.h>
19#include <linux/smp.h>
Christoph Hellwige41d12f2021-09-20 14:33:13 +020020#include <linux/interrupt.h>
Jens Axboe320ae512013-10-24 09:20:05 +010021#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>
Ingo Molnar105ab3d2017-02-01 16:36:40 +010026#include <linux/sched/topology.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010027#include <linux/sched/signal.h>
Jens Axboe320ae512013-10-24 09:20:05 +010028#include <linux/delay.h>
Jens Axboeaedcd722014-09-17 08:27:03 -060029#include <linux/crash_dump.h>
Jens Axboe88c7b2b2016-08-25 08:07:30 -060030#include <linux/prefetch.h>
Satya Tangiralaa892c8d2020-05-14 00:37:18 +000031#include <linux/blk-crypto.h>
Jens Axboe320ae512013-10-24 09:20:05 +010032
33#include <trace/events/block.h>
34
35#include <linux/blk-mq.h>
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +030036#include <linux/t10-pi.h>
Jens Axboe320ae512013-10-24 09:20:05 +010037#include "blk.h"
38#include "blk-mq.h"
Omar Sandoval9c1051a2017-05-04 08:17:21 -060039#include "blk-mq-debugfs.h"
Jens Axboe320ae512013-10-24 09:20:05 +010040#include "blk-mq-tag.h"
Bart Van Assche986d4132018-09-26 14:01:10 -070041#include "blk-pm.h"
Jens Axboecf43e6b2016-11-07 21:32:37 -070042#include "blk-stat.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070043#include "blk-mq-sched.h"
Josef Bacikc1c80382018-07-03 11:14:59 -040044#include "blk-rq-qos.h"
Jens Axboe320ae512013-10-24 09:20:05 +010045
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +010046static DEFINE_PER_CPU(struct llist_head, blk_cpu_done);
Christoph Hellwigc3077b52020-06-11 08:44:41 +020047
Omar Sandoval34dbad52017-03-21 08:56:08 -070048static void blk_mq_poll_stats_start(struct request_queue *q);
49static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);
50
Stephen Bates720b8cc2017-04-07 06:24:03 -060051static int blk_mq_poll_stats_bkt(const struct request *rq)
52{
Hou Tao3d244302019-05-21 15:59:03 +080053 int ddir, sectors, bucket;
Stephen Bates720b8cc2017-04-07 06:24:03 -060054
Jens Axboe99c749a2017-04-21 07:55:42 -060055 ddir = rq_data_dir(rq);
Hou Tao3d244302019-05-21 15:59:03 +080056 sectors = blk_rq_stats_sectors(rq);
Stephen Bates720b8cc2017-04-07 06:24:03 -060057
Hou Tao3d244302019-05-21 15:59:03 +080058 bucket = ddir + 2 * ilog2(sectors);
Stephen Bates720b8cc2017-04-07 06:24:03 -060059
60 if (bucket < 0)
61 return -1;
62 else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
63 return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
64
65 return bucket;
66}
67
Christoph Hellwigf70299f2021-10-12 13:12:15 +020068static inline struct blk_mq_hw_ctx *blk_qc_to_hctx(struct request_queue *q,
69 blk_qc_t qc)
70{
71 return q->queue_hw_ctx[(qc & ~BLK_QC_T_INTERNAL) >> BLK_QC_T_SHIFT];
72}
73
Christoph Hellwigc6699d62021-10-12 13:12:16 +020074static inline struct request *blk_qc_to_rq(struct blk_mq_hw_ctx *hctx,
75 blk_qc_t qc)
76{
Christoph Hellwigefbabbe2021-10-12 13:12:17 +020077 unsigned int tag = qc & ((1U << BLK_QC_T_SHIFT) - 1);
78
79 if (qc & BLK_QC_T_INTERNAL)
80 return blk_mq_tag_to_rq(hctx->sched_tags, tag);
81 return blk_mq_tag_to_rq(hctx->tags, tag);
Christoph Hellwigc6699d62021-10-12 13:12:16 +020082}
83
Jens Axboe320ae512013-10-24 09:20:05 +010084/*
Yufen Yu85fae292019-03-24 17:57:08 +080085 * Check if any of the ctx, dispatch list or elevator
86 * have pending work in this hardware queue.
Jens Axboe320ae512013-10-24 09:20:05 +010087 */
Jens Axboe79f720a2017-11-10 09:13:21 -070088static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
Jens Axboe320ae512013-10-24 09:20:05 +010089{
Jens Axboe79f720a2017-11-10 09:13:21 -070090 return !list_empty_careful(&hctx->dispatch) ||
91 sbitmap_any_bit_set(&hctx->ctx_map) ||
Jens Axboebd166ef2017-01-17 06:03:22 -070092 blk_mq_sched_has_work(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +010093}
94
95/*
96 * Mark this ctx as having pending work in this hardware queue
97 */
98static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
99 struct blk_mq_ctx *ctx)
100{
Jens Axboef31967f2018-10-29 13:13:29 -0600101 const int bit = ctx->index_hw[hctx->type];
102
103 if (!sbitmap_test_bit(&hctx->ctx_map, bit))
104 sbitmap_set_bit(&hctx->ctx_map, bit);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600105}
106
107static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
108 struct blk_mq_ctx *ctx)
109{
Jens Axboef31967f2018-10-29 13:13:29 -0600110 const int bit = ctx->index_hw[hctx->type];
111
112 sbitmap_clear_bit(&hctx->ctx_map, bit);
Jens Axboe320ae512013-10-24 09:20:05 +0100113}
114
Jens Axboef299b7c2017-08-08 17:51:45 -0600115struct mq_inflight {
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100116 struct block_device *part;
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300117 unsigned int inflight[2];
Jens Axboef299b7c2017-08-08 17:51:45 -0600118};
119
Jens Axboe7baa8572018-11-08 10:24:07 -0700120static bool blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx,
Jens Axboef299b7c2017-08-08 17:51:45 -0600121 struct request *rq, void *priv,
122 bool reserved)
123{
124 struct mq_inflight *mi = priv;
125
Jeffle Xub0d97552020-12-02 19:11:45 +0800126 if ((!mi->part->bd_partno || rq->part == mi->part) &&
127 blk_mq_rq_state(rq) == MQ_RQ_IN_FLIGHT)
Pavel Begunkovbb4e6b12019-09-30 21:55:33 +0300128 mi->inflight[rq_data_dir(rq)]++;
Jens Axboe7baa8572018-11-08 10:24:07 -0700129
130 return true;
Jens Axboef299b7c2017-08-08 17:51:45 -0600131}
132
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100133unsigned int blk_mq_in_flight(struct request_queue *q,
134 struct block_device *part)
Jens Axboef299b7c2017-08-08 17:51:45 -0600135{
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300136 struct mq_inflight mi = { .part = part };
Jens Axboef299b7c2017-08-08 17:51:45 -0600137
Jens Axboef299b7c2017-08-08 17:51:45 -0600138 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
Mikulas Patockae016b782018-12-06 11:41:21 -0500139
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300140 return mi.inflight[0] + mi.inflight[1];
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700141}
142
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100143void blk_mq_in_flight_rw(struct request_queue *q, struct block_device *part,
144 unsigned int inflight[2])
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700145{
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300146 struct mq_inflight mi = { .part = part };
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700147
Pavel Begunkovbb4e6b12019-09-30 21:55:33 +0300148 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300149 inflight[0] = mi.inflight[0];
150 inflight[1] = mi.inflight[1];
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700151}
152
Ming Lei1671d522017-03-27 20:06:57 +0800153void blk_freeze_queue_start(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +0800154{
Bob Liu7996a8b2019-05-21 11:25:55 +0800155 mutex_lock(&q->mq_freeze_lock);
156 if (++q->mq_freeze_depth == 1) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400157 percpu_ref_kill(&q->q_usage_counter);
Bob Liu7996a8b2019-05-21 11:25:55 +0800158 mutex_unlock(&q->mq_freeze_lock);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700159 if (queue_is_mq(q))
Ming Lei055f6e12017-11-09 10:49:53 -0800160 blk_mq_run_hw_queues(q, false);
Bob Liu7996a8b2019-05-21 11:25:55 +0800161 } else {
162 mutex_unlock(&q->mq_freeze_lock);
Tejun Heocddd5d12014-08-16 08:02:24 -0400163 }
Tejun Heof3af0202014-11-04 13:52:27 -0500164}
Ming Lei1671d522017-03-27 20:06:57 +0800165EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
Tejun Heof3af0202014-11-04 13:52:27 -0500166
Keith Busch6bae363e2017-03-01 14:22:10 -0500167void blk_mq_freeze_queue_wait(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500168{
Dan Williams3ef28e82015-10-21 13:20:12 -0400169 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +0800170}
Keith Busch6bae363e2017-03-01 14:22:10 -0500171EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800172
Keith Buschf91328c2017-03-01 14:22:11 -0500173int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
174 unsigned long timeout)
175{
176 return wait_event_timeout(q->mq_freeze_wq,
177 percpu_ref_is_zero(&q->q_usage_counter),
178 timeout);
179}
180EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
Jens Axboe320ae512013-10-24 09:20:05 +0100181
Tejun Heof3af0202014-11-04 13:52:27 -0500182/*
183 * Guarantee no request is in use, so we can change any data structure of
184 * the queue afterward.
185 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400186void blk_freeze_queue(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500187{
Dan Williams3ef28e82015-10-21 13:20:12 -0400188 /*
189 * In the !blk_mq case we are only calling this to kill the
190 * q_usage_counter, otherwise this increases the freeze depth
191 * and waits for it to return to zero. For this reason there is
192 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
193 * exported to drivers as the only user for unfreeze is blk_mq.
194 */
Ming Lei1671d522017-03-27 20:06:57 +0800195 blk_freeze_queue_start(q);
Tejun Heof3af0202014-11-04 13:52:27 -0500196 blk_mq_freeze_queue_wait(q);
197}
Dan Williams3ef28e82015-10-21 13:20:12 -0400198
199void blk_mq_freeze_queue(struct request_queue *q)
200{
201 /*
202 * ...just an alias to keep freeze and unfreeze actions balanced
203 * in the blk_mq_* namespace
204 */
205 blk_freeze_queue(q);
206}
Jens Axboec761d962015-01-02 15:05:12 -0700207EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
Tejun Heof3af0202014-11-04 13:52:27 -0500208
Christoph Hellwigaec89dc2021-09-29 09:12:41 +0200209void __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic)
Jens Axboe320ae512013-10-24 09:20:05 +0100210{
Bob Liu7996a8b2019-05-21 11:25:55 +0800211 mutex_lock(&q->mq_freeze_lock);
Christoph Hellwigaec89dc2021-09-29 09:12:41 +0200212 if (force_atomic)
213 q->q_usage_counter.data->force_atomic = true;
Bob Liu7996a8b2019-05-21 11:25:55 +0800214 q->mq_freeze_depth--;
215 WARN_ON_ONCE(q->mq_freeze_depth < 0);
216 if (!q->mq_freeze_depth) {
Bart Van Asschebdd63162018-09-26 14:01:08 -0700217 percpu_ref_resurrect(&q->q_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100218 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600219 }
Bob Liu7996a8b2019-05-21 11:25:55 +0800220 mutex_unlock(&q->mq_freeze_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100221}
Christoph Hellwigaec89dc2021-09-29 09:12:41 +0200222
223void blk_mq_unfreeze_queue(struct request_queue *q)
224{
225 __blk_mq_unfreeze_queue(q, false);
226}
Keith Buschb4c6a022014-12-19 17:54:14 -0700227EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
Jens Axboe320ae512013-10-24 09:20:05 +0100228
Bart Van Assche852ec802017-06-21 10:55:47 -0700229/*
230 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
231 * mpt3sas driver such that this function can be removed.
232 */
233void blk_mq_quiesce_queue_nowait(struct request_queue *q)
234{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800235 blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
Bart Van Assche852ec802017-06-21 10:55:47 -0700236}
237EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
238
Bart Van Assche6a83e742016-11-02 10:09:51 -0600239/**
Ming Lei69e07c42017-06-06 23:22:07 +0800240 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
Bart Van Assche6a83e742016-11-02 10:09:51 -0600241 * @q: request queue.
242 *
243 * Note: this function does not prevent that the struct request end_io()
Ming Lei69e07c42017-06-06 23:22:07 +0800244 * callback function is invoked. Once this function is returned, we make
245 * sure no dispatch can happen until the queue is unquiesced via
246 * blk_mq_unquiesce_queue().
Bart Van Assche6a83e742016-11-02 10:09:51 -0600247 */
248void blk_mq_quiesce_queue(struct request_queue *q)
249{
250 struct blk_mq_hw_ctx *hctx;
251 unsigned int i;
252 bool rcu = false;
253
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800254 blk_mq_quiesce_queue_nowait(q);
Ming Leif4560ff2017-06-18 14:24:27 -0600255
Bart Van Assche6a83e742016-11-02 10:09:51 -0600256 queue_for_each_hw_ctx(q, hctx, i) {
257 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -0800258 synchronize_srcu(hctx->srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -0600259 else
260 rcu = true;
261 }
262 if (rcu)
263 synchronize_rcu();
264}
265EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
266
Ming Leie4e73912017-06-06 23:22:03 +0800267/*
268 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
269 * @q: request queue.
270 *
271 * This function recovers queue into the state before quiescing
272 * which is done by blk_mq_quiesce_queue.
273 */
274void blk_mq_unquiesce_queue(struct request_queue *q)
275{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800276 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
Ming Leif4560ff2017-06-18 14:24:27 -0600277
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800278 /* dispatch requests which are inserted during quiescing */
279 blk_mq_run_hw_queues(q, true);
Ming Leie4e73912017-06-06 23:22:03 +0800280}
281EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
282
Jens Axboeaed3ea92014-12-22 14:04:42 -0700283void blk_mq_wake_waiters(struct request_queue *q)
284{
285 struct blk_mq_hw_ctx *hctx;
286 unsigned int i;
287
288 queue_for_each_hw_ctx(q, hctx, i)
289 if (blk_mq_hw_queue_mapped(hctx))
290 blk_mq_tag_wakeup_all(hctx->tags, true);
291}
292
Jens Axboefe1f4522018-11-28 10:50:07 -0700293/*
Hou Tao9a91b052019-05-21 15:59:04 +0800294 * Only need start/end time stamping if we have iostat or
295 * blk stats enabled, or using an IO scheduler.
Jens Axboefe1f4522018-11-28 10:50:07 -0700296 */
297static inline bool blk_mq_need_time_stamp(struct request *rq)
298{
Hou Tao9a91b052019-05-21 15:59:04 +0800299 return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS)) || rq->q->elevator;
Jens Axboefe1f4522018-11-28 10:50:07 -0700300}
301
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200302static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200303 unsigned int tag, u64 alloc_time_ns)
Jens Axboe320ae512013-10-24 09:20:05 +0100304{
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200305 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
306 struct request *rq = tags->static_rqs[tag];
Bart Van Asschec3a148d2017-06-20 11:15:43 -0700307
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200308 if (data->q->elevator) {
Christoph Hellwig766473682020-05-29 15:53:12 +0200309 rq->tag = BLK_MQ_NO_TAG;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200310 rq->internal_tag = tag;
311 } else {
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200312 rq->tag = tag;
Christoph Hellwig766473682020-05-29 15:53:12 +0200313 rq->internal_tag = BLK_MQ_NO_TAG;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200314 }
315
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200316 /* csd/requeue_work/fifo_time is initialized before use */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200317 rq->q = data->q;
318 rq->mq_ctx = data->ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600319 rq->mq_hctx = data->hctx;
Ming Lei568f2702020-07-06 22:41:11 +0800320 rq->rq_flags = 0;
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200321 rq->cmd_flags = data->cmd_flags;
Bart Van Assche0854bcd2020-12-08 21:29:45 -0800322 if (data->flags & BLK_MQ_REQ_PM)
323 rq->rq_flags |= RQF_PM;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200324 if (blk_queue_io_stat(data->q))
Christoph Hellwige8064022016-10-20 15:12:13 +0200325 rq->rq_flags |= RQF_IO_STAT;
Jens Axboe7c3fb702018-01-10 11:46:39 -0700326 INIT_LIST_HEAD(&rq->queuelist);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200327 INIT_HLIST_NODE(&rq->hash);
328 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200329 rq->rq_disk = NULL;
330 rq->part = NULL;
Tejun Heo6f816b42019-08-28 15:05:57 -0700331#ifdef CONFIG_BLK_RQ_ALLOC_TIME
332 rq->alloc_time_ns = alloc_time_ns;
333#endif
Jens Axboefe1f4522018-11-28 10:50:07 -0700334 if (blk_mq_need_time_stamp(rq))
335 rq->start_time_ns = ktime_get_ns();
336 else
337 rq->start_time_ns = 0;
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700338 rq->io_start_time_ns = 0;
Hou Tao3d244302019-05-21 15:59:03 +0800339 rq->stats_sectors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200340 rq->nr_phys_segments = 0;
341#if defined(CONFIG_BLK_DEV_INTEGRITY)
342 rq->nr_integrity_segments = 0;
343#endif
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000344 blk_crypto_rq_set_defaults(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200345 /* tag was already set */
Christoph Hellwig079076b2018-11-14 17:02:05 +0100346 WRITE_ONCE(rq->deadline, 0);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200347
Jens Axboef6be4fb2014-06-06 11:03:48 -0600348 rq->timeout = 0;
349
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200350 rq->end_io = NULL;
351 rq->end_io_data = NULL;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200352
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200353 data->ctx->rq_dispatched[op_is_sync(data->cmd_flags)]++;
Keith Busch12f5b932018-05-29 15:52:28 +0200354 refcount_set(&rq->ref, 1);
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200355
356 if (!op_is_flush(data->cmd_flags)) {
357 struct elevator_queue *e = data->q->elevator;
358
359 rq->elv.icq = NULL;
360 if (e && e->type->ops.prepare_request) {
361 if (e->type->icq_cache)
362 blk_mq_sched_assign_ioc(rq);
363
364 e->type->ops.prepare_request(rq);
365 rq->rq_flags |= RQF_ELVPRIV;
366 }
367 }
368
369 data->hctx->queued++;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200370 return rq;
Jens Axboe320ae512013-10-24 09:20:05 +0100371}
372
Jens Axboe349302d2021-10-09 13:10:39 -0600373static inline struct request *
374__blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data,
375 u64 alloc_time_ns)
376{
377 unsigned int tag, tag_offset;
378 struct request *rq;
379 unsigned long tags;
380 int i, nr = 0;
381
382 tags = blk_mq_get_tags(data, data->nr_tags, &tag_offset);
383 if (unlikely(!tags))
384 return NULL;
385
386 for (i = 0; tags; i++) {
387 if (!(tags & (1UL << i)))
388 continue;
389 tag = tag_offset + i;
390 tags &= ~(1UL << i);
391 rq = blk_mq_rq_ctx_init(data, tag, alloc_time_ns);
392 rq->rq_next = *data->cached_rq;
393 *data->cached_rq = rq;
394 }
395 data->nr_tags -= nr;
396
397 if (!data->cached_rq)
398 return NULL;
399
400 rq = *data->cached_rq;
401 *data->cached_rq = rq->rq_next;
402 return rq;
403}
404
Christoph Hellwigb90cfae2021-10-12 12:40:44 +0200405static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data)
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200406{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200407 struct request_queue *q = data->q;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200408 struct elevator_queue *e = q->elevator;
Tejun Heo6f816b42019-08-28 15:05:57 -0700409 u64 alloc_time_ns = 0;
Jens Axboe47c122e2021-10-06 06:34:11 -0600410 struct request *rq;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200411 unsigned int tag;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200412
Tejun Heo6f816b42019-08-28 15:05:57 -0700413 /* alloc_time includes depth and tag waits */
414 if (blk_queue_rq_alloc_time(q))
415 alloc_time_ns = ktime_get_ns();
416
Jens Axboef9afca42018-10-29 13:11:38 -0600417 if (data->cmd_flags & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500418 data->flags |= BLK_MQ_REQ_NOWAIT;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200419
420 if (e) {
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200421 /*
Lin Feng8d663f32021-04-15 11:39:20 +0800422 * Flush/passthrough requests are special and go directly to the
Jens Axboe17a51192018-05-09 13:28:50 -0600423 * dispatch list. Don't include reserved tags in the
424 * limiting, as it isn't useful.
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200425 */
Jens Axboef9afca42018-10-29 13:11:38 -0600426 if (!op_is_flush(data->cmd_flags) &&
Lin Feng8d663f32021-04-15 11:39:20 +0800427 !blk_op_is_passthrough(data->cmd_flags) &&
Jens Axboef9afca42018-10-29 13:11:38 -0600428 e->type->ops.limit_depth &&
Jens Axboe17a51192018-05-09 13:28:50 -0600429 !(data->flags & BLK_MQ_REQ_RESERVED))
Jens Axboef9afca42018-10-29 13:11:38 -0600430 e->type->ops.limit_depth(data->cmd_flags, data);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200431 }
432
Ming Leibf0beec2020-05-29 15:53:15 +0200433retry:
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200434 data->ctx = blk_mq_get_ctx(q);
435 data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx);
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200436 if (!e)
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200437 blk_mq_tag_busy(data->hctx);
438
Ming Leibf0beec2020-05-29 15:53:15 +0200439 /*
Jens Axboe349302d2021-10-09 13:10:39 -0600440 * Try batched alloc if we want more than 1 tag.
441 */
442 if (data->nr_tags > 1) {
443 rq = __blk_mq_alloc_requests_batch(data, alloc_time_ns);
444 if (rq)
445 return rq;
446 data->nr_tags = 1;
447 }
448
449 /*
Ming Leibf0beec2020-05-29 15:53:15 +0200450 * Waiting allocations only fail because of an inactive hctx. In that
451 * case just retry the hctx assignment and tag allocation as CPU hotplug
452 * should have migrated us to an online CPU by now.
453 */
Jens Axboe349302d2021-10-09 13:10:39 -0600454 tag = blk_mq_get_tag(data);
455 if (tag == BLK_MQ_NO_TAG) {
456 if (data->flags & BLK_MQ_REQ_NOWAIT)
457 return NULL;
458 /*
459 * Give up the CPU and sleep for a random short time to
460 * ensure that thread using a realtime scheduling class
461 * are migrated off the CPU, and thus off the hctx that
462 * is going away.
463 */
464 msleep(3);
465 goto retry;
466 }
Ming Leibf0beec2020-05-29 15:53:15 +0200467
Jens Axboe349302d2021-10-09 13:10:39 -0600468 return blk_mq_rq_ctx_init(data, tag, alloc_time_ns);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200469}
470
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700471struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800472 blk_mq_req_flags_t flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100473{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200474 struct blk_mq_alloc_data data = {
475 .q = q,
476 .flags = flags,
477 .cmd_flags = op,
Jens Axboe47c122e2021-10-06 06:34:11 -0600478 .nr_tags = 1,
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200479 };
Jens Axboebd166ef2017-01-17 06:03:22 -0700480 struct request *rq;
Joe Lawrencea492f072014-08-28 08:15:21 -0600481 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100482
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800483 ret = blk_queue_enter(q, flags);
Joe Lawrencea492f072014-08-28 08:15:21 -0600484 if (ret)
485 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100486
Christoph Hellwigb90cfae2021-10-12 12:40:44 +0200487 rq = __blk_mq_alloc_requests(&data);
Jens Axboebd166ef2017-01-17 06:03:22 -0700488 if (!rq)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200489 goto out_queue_exit;
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200490 rq->__data_len = 0;
491 rq->__sector = (sector_t) -1;
492 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100493 return rq;
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200494out_queue_exit:
495 blk_queue_exit(q);
496 return ERR_PTR(-EWOULDBLOCK);
Jens Axboe320ae512013-10-24 09:20:05 +0100497}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600498EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100499
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700500struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800501 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
Ming Lin1f5bd332016-06-13 16:45:21 +0200502{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200503 struct blk_mq_alloc_data data = {
504 .q = q,
505 .flags = flags,
506 .cmd_flags = op,
Jens Axboe47c122e2021-10-06 06:34:11 -0600507 .nr_tags = 1,
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200508 };
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200509 u64 alloc_time_ns = 0;
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800510 unsigned int cpu;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200511 unsigned int tag;
Ming Lin1f5bd332016-06-13 16:45:21 +0200512 int ret;
513
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200514 /* alloc_time includes depth and tag waits */
515 if (blk_queue_rq_alloc_time(q))
516 alloc_time_ns = ktime_get_ns();
517
Ming Lin1f5bd332016-06-13 16:45:21 +0200518 /*
519 * If the tag allocator sleeps we could get an allocation for a
520 * different hardware context. No need to complicate the low level
521 * allocator for this for the rare use case of a command tied to
522 * a specific queue.
523 */
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200524 if (WARN_ON_ONCE(!(flags & (BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED))))
Ming Lin1f5bd332016-06-13 16:45:21 +0200525 return ERR_PTR(-EINVAL);
526
527 if (hctx_idx >= q->nr_hw_queues)
528 return ERR_PTR(-EIO);
529
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800530 ret = blk_queue_enter(q, flags);
Ming Lin1f5bd332016-06-13 16:45:21 +0200531 if (ret)
532 return ERR_PTR(ret);
533
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600534 /*
535 * Check if the hardware context is actually mapped to anything.
536 * If not tell the caller that it should skip this queue.
537 */
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200538 ret = -EXDEV;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200539 data.hctx = q->queue_hw_ctx[hctx_idx];
540 if (!blk_mq_hw_queue_mapped(data.hctx))
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200541 goto out_queue_exit;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200542 cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
543 data.ctx = __blk_mq_get_ctx(q, cpu);
Ming Lin1f5bd332016-06-13 16:45:21 +0200544
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200545 if (!q->elevator)
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200546 blk_mq_tag_busy(data.hctx);
547
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200548 ret = -EWOULDBLOCK;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200549 tag = blk_mq_get_tag(&data);
550 if (tag == BLK_MQ_NO_TAG)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200551 goto out_queue_exit;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200552 return blk_mq_rq_ctx_init(&data, tag, alloc_time_ns);
553
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200554out_queue_exit:
555 blk_queue_exit(q);
556 return ERR_PTR(ret);
Ming Lin1f5bd332016-06-13 16:45:21 +0200557}
558EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
559
Keith Busch12f5b932018-05-29 15:52:28 +0200560static void __blk_mq_free_request(struct request *rq)
561{
562 struct request_queue *q = rq->q;
563 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600564 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Keith Busch12f5b932018-05-29 15:52:28 +0200565 const int sched_tag = rq->internal_tag;
566
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000567 blk_crypto_free_request(rq);
Bart Van Assche986d4132018-09-26 14:01:10 -0700568 blk_pm_mark_last_busy(rq);
Jens Axboeea4f9952018-10-29 15:06:13 -0600569 rq->mq_hctx = NULL;
Christoph Hellwig766473682020-05-29 15:53:12 +0200570 if (rq->tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800571 blk_mq_put_tag(hctx->tags, ctx, rq->tag);
Christoph Hellwig766473682020-05-29 15:53:12 +0200572 if (sched_tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800573 blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
Keith Busch12f5b932018-05-29 15:52:28 +0200574 blk_mq_sched_restart(hctx);
575 blk_queue_exit(q);
576}
577
Christoph Hellwig6af54052017-06-16 18:15:22 +0200578void blk_mq_free_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100579{
Jens Axboe320ae512013-10-24 09:20:05 +0100580 struct request_queue *q = rq->q;
Christoph Hellwig6af54052017-06-16 18:15:22 +0200581 struct elevator_queue *e = q->elevator;
582 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600583 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100584
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200585 if (rq->rq_flags & RQF_ELVPRIV) {
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600586 if (e && e->type->ops.finish_request)
587 e->type->ops.finish_request(rq);
Christoph Hellwig6af54052017-06-16 18:15:22 +0200588 if (rq->elv.icq) {
589 put_io_context(rq->elv.icq->ioc);
590 rq->elv.icq = NULL;
591 }
592 }
593
594 ctx->rq_completed[rq_is_sync(rq)]++;
Christoph Hellwige8064022016-10-20 15:12:13 +0200595 if (rq->rq_flags & RQF_MQ_INFLIGHT)
John Garrybccf5e22020-08-19 23:20:26 +0800596 __blk_mq_dec_active_requests(hctx);
Jens Axboe87760e52016-11-09 12:38:14 -0700597
Jens Axboe7beb2f82017-09-30 02:08:24 -0600598 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
Christoph Hellwigd152c682021-08-16 15:46:24 +0200599 laptop_io_completion(q->disk->bdi);
Jens Axboe7beb2f82017-09-30 02:08:24 -0600600
Josef Bacika7905042018-07-03 09:32:35 -0600601 rq_qos_done(q, rq);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600602
Keith Busch12f5b932018-05-29 15:52:28 +0200603 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
604 if (refcount_dec_and_test(&rq->ref))
605 __blk_mq_free_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100606}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700607EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100608
Jens Axboe47c122e2021-10-06 06:34:11 -0600609void blk_mq_free_plug_rqs(struct blk_plug *plug)
610{
611 while (plug->cached_rq) {
612 struct request *rq;
613
614 rq = plug->cached_rq;
615 plug->cached_rq = rq->rq_next;
616 percpu_ref_get(&rq->q->q_usage_counter);
617 blk_mq_free_request(rq);
618 }
619}
620
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200621inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
Jens Axboe320ae512013-10-24 09:20:05 +0100622{
Pavel Begunkov8971a3b72021-10-13 09:57:13 +0100623 if (blk_mq_need_time_stamp(rq)) {
624 u64 now = ktime_get_ns();
Jens Axboefe1f4522018-11-28 10:50:07 -0700625
Pavel Begunkov8971a3b72021-10-13 09:57:13 +0100626 if (rq->rq_flags & RQF_STATS) {
627 blk_mq_poll_stats_start(rq->q);
628 blk_stat_add(rq, now);
629 }
Omar Sandoval522a7772018-05-09 02:08:53 -0700630
Pavel Begunkov8971a3b72021-10-13 09:57:13 +0100631 blk_mq_sched_completed_request(rq, now);
632 blk_account_io_done(rq, now);
Omar Sandoval4bc63392018-05-09 02:08:52 -0700633 }
634
Christoph Hellwig91b63632014-04-16 09:44:53 +0200635 if (rq->end_io) {
Josef Bacika7905042018-07-03 09:32:35 -0600636 rq_qos_done(rq->q, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100637 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200638 } else {
Jens Axboe320ae512013-10-24 09:20:05 +0100639 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200640 }
Jens Axboe320ae512013-10-24 09:20:05 +0100641}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700642EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200643
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200644void blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200645{
646 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
647 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700648 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200649}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700650EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100651
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100652static void blk_complete_reqs(struct llist_head *list)
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200653{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100654 struct llist_node *entry = llist_reverse_order(llist_del_all(list));
655 struct request *rq, *next;
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200656
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100657 llist_for_each_entry_safe(rq, next, entry, ipi_list)
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200658 rq->q->mq_ops->complete(rq);
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200659}
660
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100661static __latent_entropy void blk_done_softirq(struct softirq_action *h)
Christoph Hellwig115243f2020-06-11 08:44:42 +0200662{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100663 blk_complete_reqs(this_cpu_ptr(&blk_cpu_done));
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200664}
665
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200666static int blk_softirq_cpu_dead(unsigned int cpu)
667{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100668 blk_complete_reqs(&per_cpu(blk_cpu_done, cpu));
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200669 return 0;
670}
671
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800672static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100673{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100674 __raise_softirq_irqoff(BLOCK_SOFTIRQ);
Jens Axboe320ae512013-10-24 09:20:05 +0100675}
676
Christoph Hellwig963395262020-06-11 08:44:49 +0200677static inline bool blk_mq_complete_need_ipi(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100678{
Christoph Hellwig963395262020-06-11 08:44:49 +0200679 int cpu = raw_smp_processor_id();
Jens Axboe320ae512013-10-24 09:20:05 +0100680
Christoph Hellwig963395262020-06-11 08:44:49 +0200681 if (!IS_ENABLED(CONFIG_SMP) ||
682 !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags))
683 return false;
Sebastian Andrzej Siewior71425182020-12-04 20:13:54 +0100684 /*
685 * With force threaded interrupts enabled, raising softirq from an SMP
686 * function call will always result in waking the ksoftirqd thread.
687 * This is probably worse than completing the request on a different
688 * cache domain.
689 */
Tanner Love91cc4702021-06-02 14:03:38 -0400690 if (force_irqthreads())
Sebastian Andrzej Siewior71425182020-12-04 20:13:54 +0100691 return false;
Christoph Hellwig963395262020-06-11 08:44:49 +0200692
693 /* same CPU or cache domain? Complete locally */
694 if (cpu == rq->mq_ctx->cpu ||
695 (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) &&
696 cpus_share_cache(cpu, rq->mq_ctx->cpu)))
697 return false;
698
699 /* don't try to IPI to an offline CPU */
700 return cpu_online(rq->mq_ctx->cpu);
701}
702
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100703static void blk_mq_complete_send_ipi(struct request *rq)
704{
705 struct llist_head *list;
706 unsigned int cpu;
707
708 cpu = rq->mq_ctx->cpu;
709 list = &per_cpu(blk_cpu_done, cpu);
710 if (llist_add(&rq->ipi_list, list)) {
711 INIT_CSD(&rq->csd, __blk_mq_complete_request_remote, rq);
712 smp_call_function_single_async(cpu, &rq->csd);
713 }
714}
715
716static void blk_mq_raise_softirq(struct request *rq)
717{
718 struct llist_head *list;
719
720 preempt_disable();
721 list = this_cpu_ptr(&blk_cpu_done);
722 if (llist_add(&rq->ipi_list, list))
723 raise_softirq(BLOCK_SOFTIRQ);
724 preempt_enable();
725}
726
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200727bool blk_mq_complete_request_remote(struct request *rq)
728{
Keith Buschaf78ff72018-11-26 09:54:30 -0700729 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
Ming Lei36e76532018-09-28 16:42:20 +0800730
Jens Axboe4ab32bf2018-11-18 16:15:35 -0700731 /*
732 * For a polled request, always complete locallly, it's pointless
733 * to redirect the completion.
734 */
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200735 if (rq->cmd_flags & REQ_HIPRI)
736 return false;
Jens Axboe320ae512013-10-24 09:20:05 +0100737
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200738 if (blk_mq_complete_need_ipi(rq)) {
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100739 blk_mq_complete_send_ipi(rq);
740 return true;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800741 }
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200742
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100743 if (rq->q->nr_hw_queues == 1) {
744 blk_mq_raise_softirq(rq);
745 return true;
746 }
747 return false;
Jens Axboe320ae512013-10-24 09:20:05 +0100748}
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200749EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote);
750
Jens Axboe320ae512013-10-24 09:20:05 +0100751/**
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200752 * blk_mq_complete_request - end I/O on a request
753 * @rq: the request being processed
Jens Axboe320ae512013-10-24 09:20:05 +0100754 *
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200755 * Description:
756 * Complete a request by scheduling the ->complete_rq operation.
757 **/
758void blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100759{
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200760 if (!blk_mq_complete_request_remote(rq))
Christoph Hellwig963395262020-06-11 08:44:49 +0200761 rq->q->mq_ops->complete(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100762}
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200763EXPORT_SYMBOL(blk_mq_complete_request);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800764
Jens Axboe04ced152018-01-09 08:29:46 -0800765static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800766 __releases(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800767{
768 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
769 rcu_read_unlock();
770 else
Tejun Heo05707b62018-01-09 08:29:53 -0800771 srcu_read_unlock(hctx->srcu, srcu_idx);
Jens Axboe04ced152018-01-09 08:29:46 -0800772}
773
774static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800775 __acquires(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800776{
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700777 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
778 /* shut up gcc false positive */
779 *srcu_idx = 0;
Jens Axboe04ced152018-01-09 08:29:46 -0800780 rcu_read_lock();
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700781 } else
Tejun Heo05707b62018-01-09 08:29:53 -0800782 *srcu_idx = srcu_read_lock(hctx->srcu);
Jens Axboe04ced152018-01-09 08:29:46 -0800783}
784
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800785/**
André Almeida105663f2020-01-06 15:08:18 -0300786 * blk_mq_start_request - Start processing a request
787 * @rq: Pointer to request to be started
788 *
789 * Function used by device drivers to notify the block layer that a request
790 * is going to be processed now, so blk layer can do proper initializations
791 * such as starting the timeout timer.
792 */
Christoph Hellwige2490072014-09-13 16:40:09 -0700793void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100794{
795 struct request_queue *q = rq->q;
796
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100797 trace_block_rq_issue(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100798
Jens Axboecf43e6b2016-11-07 21:32:37 -0700799 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
Jens Axboe00067072021-10-05 09:23:59 -0600800 u64 start_time;
801#ifdef CONFIG_BLK_CGROUP
802 if (rq->bio)
803 start_time = bio_issue_time(&rq->bio->bi_issue);
804 else
805#endif
806 start_time = ktime_get_ns();
807 rq->io_start_time_ns = start_time;
Hou Tao3d244302019-05-21 15:59:03 +0800808 rq->stats_sectors = blk_rq_sectors(rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700809 rq->rq_flags |= RQF_STATS;
Josef Bacika7905042018-07-03 09:32:35 -0600810 rq_qos_issue(q, rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700811 }
812
Tejun Heo1d9bd512018-01-09 08:29:48 -0800813 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
Jens Axboe538b7532014-09-16 10:37:37 -0600814
Tejun Heo1d9bd512018-01-09 08:29:48 -0800815 blk_add_timer(rq);
Keith Busch12f5b932018-05-29 15:52:28 +0200816 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800817
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +0300818#ifdef CONFIG_BLK_DEV_INTEGRITY
819 if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
820 q->integrity.profile->prepare_fn(rq);
821#endif
Jens Axboe320ae512013-10-24 09:20:05 +0100822}
Christoph Hellwige2490072014-09-13 16:40:09 -0700823EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100824
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200825static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100826{
827 struct request_queue *q = rq->q;
828
Ming Lei923218f2017-11-02 23:24:38 +0800829 blk_mq_put_driver_tag(rq);
830
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100831 trace_block_rq_requeue(rq);
Josef Bacika7905042018-07-03 09:32:35 -0600832 rq_qos_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800833
Keith Busch12f5b932018-05-29 15:52:28 +0200834 if (blk_mq_request_started(rq)) {
835 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Christoph Hellwigda661262018-06-14 13:58:45 +0200836 rq->rq_flags &= ~RQF_TIMED_OUT;
Christoph Hellwige2490072014-09-13 16:40:09 -0700837 }
Jens Axboe320ae512013-10-24 09:20:05 +0100838}
839
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700840void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200841{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200842 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200843
Ming Lei105976f2018-02-23 23:36:56 +0800844 /* this request will be re-inserted to io scheduler queue */
845 blk_mq_sched_requeue_request(rq);
846
Jens Axboe7d692332018-10-24 10:48:12 -0600847 BUG_ON(!list_empty(&rq->queuelist));
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700848 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200849}
850EXPORT_SYMBOL(blk_mq_requeue_request);
851
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600852static void blk_mq_requeue_work(struct work_struct *work)
853{
854 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -0400855 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600856 LIST_HEAD(rq_list);
857 struct request *rq, *next;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600858
Jens Axboe18e97812017-07-27 08:03:57 -0600859 spin_lock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600860 list_splice_init(&q->requeue_list, &rq_list);
Jens Axboe18e97812017-07-27 08:03:57 -0600861 spin_unlock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600862
863 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
Jianchao Wangaef18972019-02-12 09:56:25 +0800864 if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP)))
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600865 continue;
866
Christoph Hellwige8064022016-10-20 15:12:13 +0200867 rq->rq_flags &= ~RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600868 list_del_init(&rq->queuelist);
Jianchao Wangaef18972019-02-12 09:56:25 +0800869 /*
870 * If RQF_DONTPREP, rq has contained some driver specific
871 * data, so insert it to hctx dispatch list to avoid any
872 * merge.
873 */
874 if (rq->rq_flags & RQF_DONTPREP)
Ming Lei01e99ae2020-02-25 09:04:32 +0800875 blk_mq_request_bypass_insert(rq, false, false);
Jianchao Wangaef18972019-02-12 09:56:25 +0800876 else
877 blk_mq_sched_insert_request(rq, true, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600878 }
879
880 while (!list_empty(&rq_list)) {
881 rq = list_entry(rq_list.next, struct request, queuelist);
882 list_del_init(&rq->queuelist);
Mike Snitzer9e97d292018-01-17 11:25:58 -0500883 blk_mq_sched_insert_request(rq, false, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600884 }
885
Bart Van Assche52d7f1b2016-10-28 17:20:32 -0700886 blk_mq_run_hw_queues(q, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600887}
888
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700889void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
890 bool kick_requeue_list)
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600891{
892 struct request_queue *q = rq->q;
893 unsigned long flags;
894
895 /*
896 * We abuse this flag that is otherwise used by the I/O scheduler to
Jens Axboeff821d22017-11-10 22:05:12 -0700897 * request head insertion from the workqueue.
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600898 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200899 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600900
901 spin_lock_irqsave(&q->requeue_lock, flags);
902 if (at_head) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200903 rq->rq_flags |= RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600904 list_add(&rq->queuelist, &q->requeue_list);
905 } else {
906 list_add_tail(&rq->queuelist, &q->requeue_list);
907 }
908 spin_unlock_irqrestore(&q->requeue_lock, flags);
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700909
910 if (kick_requeue_list)
911 blk_mq_kick_requeue_list(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600912}
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600913
914void blk_mq_kick_requeue_list(struct request_queue *q)
915{
Bart Van Asscheae943d22018-01-19 08:58:55 -0800916 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600917}
918EXPORT_SYMBOL(blk_mq_kick_requeue_list);
919
Mike Snitzer28494502016-09-14 13:28:30 -0400920void blk_mq_delay_kick_requeue_list(struct request_queue *q,
921 unsigned long msecs)
922{
Bart Van Assched4acf362017-08-09 11:28:06 -0700923 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
924 msecs_to_jiffies(msecs));
Mike Snitzer28494502016-09-14 13:28:30 -0400925}
926EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
927
Jens Axboe0e62f512014-06-04 10:23:49 -0600928struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
929{
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600930 if (tag < tags->nr_tags) {
931 prefetch(tags->rqs[tag]);
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700932 return tags->rqs[tag];
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600933 }
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700934
935 return NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600936}
937EXPORT_SYMBOL(blk_mq_tag_to_rq);
938
Jens Axboe3c94d832018-12-17 21:11:17 -0700939static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq,
940 void *priv, bool reserved)
Jens Axboeae879912018-11-08 09:03:51 -0700941{
942 /*
Ming Lei05a4fed2020-07-07 11:04:33 -0400943 * If we find a request that isn't idle and the queue matches,
Jens Axboe3c94d832018-12-17 21:11:17 -0700944 * we know the queue is busy. Return false to stop the iteration.
Jens Axboeae879912018-11-08 09:03:51 -0700945 */
Ming Lei05a4fed2020-07-07 11:04:33 -0400946 if (blk_mq_request_started(rq) && rq->q == hctx->queue) {
Jens Axboeae879912018-11-08 09:03:51 -0700947 bool *busy = priv;
948
949 *busy = true;
950 return false;
951 }
952
953 return true;
954}
955
Jens Axboe3c94d832018-12-17 21:11:17 -0700956bool blk_mq_queue_inflight(struct request_queue *q)
Jens Axboeae879912018-11-08 09:03:51 -0700957{
958 bool busy = false;
959
Jens Axboe3c94d832018-12-17 21:11:17 -0700960 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
Jens Axboeae879912018-11-08 09:03:51 -0700961 return busy;
962}
Jens Axboe3c94d832018-12-17 21:11:17 -0700963EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
Jens Axboeae879912018-11-08 09:03:51 -0700964
Tejun Heo358f70d2018-01-09 08:29:50 -0800965static void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100966{
Christoph Hellwigda661262018-06-14 13:58:45 +0200967 req->rq_flags |= RQF_TIMED_OUT;
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200968 if (req->q->mq_ops->timeout) {
969 enum blk_eh_timer_return ret;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600970
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200971 ret = req->q->mq_ops->timeout(req, reserved);
972 if (ret == BLK_EH_DONE)
973 return;
974 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700975 }
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200976
977 blk_add_timer(req);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600978}
Keith Busch5b3f25f2015-01-07 18:55:46 -0700979
Keith Busch12f5b932018-05-29 15:52:28 +0200980static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
981{
982 unsigned long deadline;
983
984 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
985 return false;
Christoph Hellwigda661262018-06-14 13:58:45 +0200986 if (rq->rq_flags & RQF_TIMED_OUT)
987 return false;
Keith Busch12f5b932018-05-29 15:52:28 +0200988
Christoph Hellwig079076b2018-11-14 17:02:05 +0100989 deadline = READ_ONCE(rq->deadline);
Keith Busch12f5b932018-05-29 15:52:28 +0200990 if (time_after_eq(jiffies, deadline))
991 return true;
992
993 if (*next == 0)
994 *next = deadline;
995 else if (time_after(*next, deadline))
996 *next = deadline;
997 return false;
998}
999
Ming Lei2e315dc2021-05-11 23:22:34 +08001000void blk_mq_put_rq_ref(struct request *rq)
1001{
Ming Leia9ed27a2021-08-18 09:09:25 +08001002 if (is_flush_rq(rq))
Ming Lei2e315dc2021-05-11 23:22:34 +08001003 rq->end_io(rq, 0);
1004 else if (refcount_dec_and_test(&rq->ref))
1005 __blk_mq_free_request(rq);
1006}
1007
Jens Axboe7baa8572018-11-08 10:24:07 -07001008static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig81481eb2014-09-13 16:40:11 -07001009 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +01001010{
Keith Busch12f5b932018-05-29 15:52:28 +02001011 unsigned long *next = priv;
Christoph Hellwig81481eb2014-09-13 16:40:11 -07001012
Keith Busch12f5b932018-05-29 15:52:28 +02001013 /*
Ming Leic797b402021-08-11 23:52:02 +08001014 * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot
1015 * be reallocated underneath the timeout handler's processing, then
1016 * the expire check is reliable. If the request is not expired, then
1017 * it was completed and reallocated as a new request after returning
1018 * from blk_mq_check_expired().
Keith Busch12f5b932018-05-29 15:52:28 +02001019 */
1020 if (blk_mq_req_expired(rq, next))
Tejun Heo1d9bd512018-01-09 08:29:48 -08001021 blk_mq_rq_timed_out(rq, reserved);
Jens Axboe7baa8572018-11-08 10:24:07 -07001022 return true;
Tejun Heo1d9bd512018-01-09 08:29:48 -08001023}
1024
Christoph Hellwig287922e2015-10-30 20:57:30 +08001025static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001026{
Christoph Hellwig287922e2015-10-30 20:57:30 +08001027 struct request_queue *q =
1028 container_of(work, struct request_queue, timeout_work);
Keith Busch12f5b932018-05-29 15:52:28 +02001029 unsigned long next = 0;
Tejun Heo1d9bd512018-01-09 08:29:48 -08001030 struct blk_mq_hw_ctx *hctx;
Christoph Hellwig81481eb2014-09-13 16:40:11 -07001031 int i;
Jens Axboe320ae512013-10-24 09:20:05 +01001032
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -06001033 /* A deadlock might occur if a request is stuck requiring a
1034 * timeout at the same time a queue freeze is waiting
1035 * completion, since the timeout code would not be able to
1036 * acquire the queue reference here.
1037 *
1038 * That's why we don't use blk_queue_enter here; instead, we use
1039 * percpu_ref_tryget directly, because we need to be able to
1040 * obtain a reference even in the short window between the queue
1041 * starting to freeze, by dropping the first reference in
Ming Lei1671d522017-03-27 20:06:57 +08001042 * blk_freeze_queue_start, and the moment the last request is
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -06001043 * consumed, marked by the instant q_usage_counter reaches
1044 * zero.
1045 */
1046 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +08001047 return;
1048
Keith Busch12f5b932018-05-29 15:52:28 +02001049 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
Jens Axboe320ae512013-10-24 09:20:05 +01001050
Keith Busch12f5b932018-05-29 15:52:28 +02001051 if (next != 0) {
1052 mod_timer(&q->timeout, next);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001053 } else {
Bart Van Asschefcd36c32018-01-10 08:33:33 -08001054 /*
1055 * Request timeouts are handled as a forward rolling timer. If
1056 * we end up here it means that no requests are pending and
1057 * also that no request has been pending for a while. Mark
1058 * each hctx as idle.
1059 */
Ming Leif054b562015-04-21 10:00:19 +08001060 queue_for_each_hw_ctx(q, hctx, i) {
1061 /* the hctx may be unmapped, so check it here */
1062 if (blk_mq_hw_queue_mapped(hctx))
1063 blk_mq_tag_idle(hctx);
1064 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06001065 }
Christoph Hellwig287922e2015-10-30 20:57:30 +08001066 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001067}
1068
Omar Sandoval88459642016-09-17 08:38:44 -06001069struct flush_busy_ctx_data {
1070 struct blk_mq_hw_ctx *hctx;
1071 struct list_head *list;
1072};
1073
1074static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
1075{
1076 struct flush_busy_ctx_data *flush_data = data;
1077 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
1078 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001079 enum hctx_type type = hctx->type;
Omar Sandoval88459642016-09-17 08:38:44 -06001080
Omar Sandoval88459642016-09-17 08:38:44 -06001081 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001082 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
Omar Sandovale9a99a62018-02-27 16:56:42 -08001083 sbitmap_clear_bit(sb, bitnr);
Omar Sandoval88459642016-09-17 08:38:44 -06001084 spin_unlock(&ctx->lock);
1085 return true;
1086}
1087
Jens Axboe320ae512013-10-24 09:20:05 +01001088/*
Jens Axboe1429d7c2014-05-19 09:23:55 -06001089 * Process software queues that have been marked busy, splicing them
1090 * to the for-dispatch
1091 */
Jens Axboe2c3ad662016-12-14 14:34:47 -07001092void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
Jens Axboe1429d7c2014-05-19 09:23:55 -06001093{
Omar Sandoval88459642016-09-17 08:38:44 -06001094 struct flush_busy_ctx_data data = {
1095 .hctx = hctx,
1096 .list = list,
1097 };
Jens Axboe1429d7c2014-05-19 09:23:55 -06001098
Omar Sandoval88459642016-09-17 08:38:44 -06001099 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001100}
Jens Axboe2c3ad662016-12-14 14:34:47 -07001101EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001102
Ming Leib3476892017-10-14 17:22:30 +08001103struct dispatch_rq_data {
1104 struct blk_mq_hw_ctx *hctx;
1105 struct request *rq;
1106};
1107
1108static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
1109 void *data)
1110{
1111 struct dispatch_rq_data *dispatch_data = data;
1112 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
1113 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001114 enum hctx_type type = hctx->type;
Ming Leib3476892017-10-14 17:22:30 +08001115
1116 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001117 if (!list_empty(&ctx->rq_lists[type])) {
1118 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
Ming Leib3476892017-10-14 17:22:30 +08001119 list_del_init(&dispatch_data->rq->queuelist);
Ming Leic16d6b52018-12-17 08:44:05 -07001120 if (list_empty(&ctx->rq_lists[type]))
Ming Leib3476892017-10-14 17:22:30 +08001121 sbitmap_clear_bit(sb, bitnr);
1122 }
1123 spin_unlock(&ctx->lock);
1124
1125 return !dispatch_data->rq;
1126}
1127
1128struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
1129 struct blk_mq_ctx *start)
1130{
Jens Axboef31967f2018-10-29 13:13:29 -06001131 unsigned off = start ? start->index_hw[hctx->type] : 0;
Ming Leib3476892017-10-14 17:22:30 +08001132 struct dispatch_rq_data data = {
1133 .hctx = hctx,
1134 .rq = NULL,
1135 };
1136
1137 __sbitmap_for_each_set(&hctx->ctx_map, off,
1138 dispatch_rq_from_ctx, &data);
1139
1140 return data.rq;
1141}
1142
Jens Axboe703fd1c2016-09-16 13:59:14 -06001143static inline unsigned int queued_to_index(unsigned int queued)
1144{
1145 if (!queued)
1146 return 0;
Jens Axboe1429d7c2014-05-19 09:23:55 -06001147
Jens Axboe703fd1c2016-09-16 13:59:14 -06001148 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001149}
1150
Ming Lei570e9b72020-06-30 22:03:55 +08001151static bool __blk_mq_get_driver_tag(struct request *rq)
1152{
John Garryae0f1a72021-10-05 18:23:38 +08001153 struct sbitmap_queue *bt = &rq->mq_hctx->tags->bitmap_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001154 unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001155 int tag;
1156
Ming Lei568f2702020-07-06 22:41:11 +08001157 blk_mq_tag_busy(rq->mq_hctx);
1158
Ming Lei570e9b72020-06-30 22:03:55 +08001159 if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) {
John Garryae0f1a72021-10-05 18:23:38 +08001160 bt = &rq->mq_hctx->tags->breserved_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001161 tag_offset = 0;
Ming Lei28500852020-09-11 18:41:14 +08001162 } else {
1163 if (!hctx_may_queue(rq->mq_hctx, bt))
1164 return false;
Ming Lei570e9b72020-06-30 22:03:55 +08001165 }
1166
Ming Lei570e9b72020-06-30 22:03:55 +08001167 tag = __sbitmap_queue_get(bt);
1168 if (tag == BLK_MQ_NO_TAG)
1169 return false;
1170
1171 rq->tag = tag + tag_offset;
Ming Lei570e9b72020-06-30 22:03:55 +08001172 return true;
1173}
1174
Jan Kara613471542021-06-03 12:47:21 +02001175bool blk_mq_get_driver_tag(struct request *rq)
Ming Lei570e9b72020-06-30 22:03:55 +08001176{
Ming Lei568f2702020-07-06 22:41:11 +08001177 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1178
1179 if (rq->tag == BLK_MQ_NO_TAG && !__blk_mq_get_driver_tag(rq))
1180 return false;
1181
Ming Lei51db1c32020-08-19 23:20:19 +08001182 if ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) &&
Ming Lei568f2702020-07-06 22:41:11 +08001183 !(rq->rq_flags & RQF_MQ_INFLIGHT)) {
1184 rq->rq_flags |= RQF_MQ_INFLIGHT;
John Garrybccf5e22020-08-19 23:20:26 +08001185 __blk_mq_inc_active_requests(hctx);
Ming Lei568f2702020-07-06 22:41:11 +08001186 }
1187 hctx->tags->rqs[rq->tag] = rq;
1188 return true;
Ming Lei570e9b72020-06-30 22:03:55 +08001189}
1190
Jens Axboeeb619fd2017-11-09 08:32:43 -07001191static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1192 int flags, void *key)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001193{
1194 struct blk_mq_hw_ctx *hctx;
1195
1196 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1197
Ming Lei5815839b2018-06-25 19:31:47 +08001198 spin_lock(&hctx->dispatch_wait_lock);
Jens Axboee8618572019-03-25 12:34:10 -06001199 if (!list_empty(&wait->entry)) {
1200 struct sbitmap_queue *sbq;
1201
1202 list_del_init(&wait->entry);
John Garryae0f1a72021-10-05 18:23:38 +08001203 sbq = &hctx->tags->bitmap_tags;
Jens Axboee8618572019-03-25 12:34:10 -06001204 atomic_dec(&sbq->ws_active);
1205 }
Ming Lei5815839b2018-06-25 19:31:47 +08001206 spin_unlock(&hctx->dispatch_wait_lock);
1207
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001208 blk_mq_run_hw_queue(hctx, true);
1209 return 1;
1210}
1211
Jens Axboef906a6a2017-11-09 16:10:13 -07001212/*
1213 * Mark us waiting for a tag. For shared tags, this involves hooking us into
Bart Van Asscheee3e4de2018-01-09 10:09:15 -08001214 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1215 * restart. For both cases, take care to check the condition again after
Jens Axboef906a6a2017-11-09 16:10:13 -07001216 * marking us as waiting.
1217 */
Ming Lei2278d692018-06-25 19:31:46 +08001218static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
Jens Axboef906a6a2017-11-09 16:10:13 -07001219 struct request *rq)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001220{
John Garryae0f1a72021-10-05 18:23:38 +08001221 struct sbitmap_queue *sbq = &hctx->tags->bitmap_tags;
Ming Lei5815839b2018-06-25 19:31:47 +08001222 struct wait_queue_head *wq;
Jens Axboef906a6a2017-11-09 16:10:13 -07001223 wait_queue_entry_t *wait;
1224 bool ret;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001225
Ming Lei51db1c32020-08-19 23:20:19 +08001226 if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
Yufen Yu684b7322019-03-15 11:05:10 +08001227 blk_mq_sched_mark_restart_hctx(hctx);
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001228
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001229 /*
1230 * It's possible that a tag was freed in the window between the
1231 * allocation failure and adding the hardware queue to the wait
1232 * queue.
1233 *
1234 * Don't clear RESTART here, someone else could have set it.
1235 * At most this will cost an extra queue run.
1236 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001237 return blk_mq_get_driver_tag(rq);
Jens Axboeeb619fd2017-11-09 08:32:43 -07001238 }
1239
Ming Lei2278d692018-06-25 19:31:46 +08001240 wait = &hctx->dispatch_wait;
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001241 if (!list_empty_careful(&wait->entry))
1242 return false;
1243
Jens Axboee8618572019-03-25 12:34:10 -06001244 wq = &bt_wait_ptr(sbq, hctx)->wait;
Ming Lei5815839b2018-06-25 19:31:47 +08001245
1246 spin_lock_irq(&wq->lock);
1247 spin_lock(&hctx->dispatch_wait_lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001248 if (!list_empty(&wait->entry)) {
Ming Lei5815839b2018-06-25 19:31:47 +08001249 spin_unlock(&hctx->dispatch_wait_lock);
1250 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001251 return false;
1252 }
1253
Jens Axboee8618572019-03-25 12:34:10 -06001254 atomic_inc(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001255 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1256 __add_wait_queue(wq, wait);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001257
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001258 /*
Jens Axboeeb619fd2017-11-09 08:32:43 -07001259 * It's possible that a tag was freed in the window between the
1260 * allocation failure and adding the hardware queue to the wait
1261 * queue.
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001262 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001263 ret = blk_mq_get_driver_tag(rq);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001264 if (!ret) {
Ming Lei5815839b2018-06-25 19:31:47 +08001265 spin_unlock(&hctx->dispatch_wait_lock);
1266 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001267 return false;
Jens Axboef906a6a2017-11-09 16:10:13 -07001268 }
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001269
1270 /*
1271 * We got a tag, remove ourselves from the wait queue to ensure
1272 * someone else gets the wakeup.
1273 */
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001274 list_del_init(&wait->entry);
Jens Axboee8618572019-03-25 12:34:10 -06001275 atomic_dec(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001276 spin_unlock(&hctx->dispatch_wait_lock);
1277 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001278
1279 return true;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001280}
1281
Ming Lei6e7687172018-07-03 09:03:16 -06001282#define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1283#define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1284/*
1285 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1286 * - EWMA is one simple way to compute running average value
1287 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1288 * - take 4 as factor for avoiding to get too small(0) result, and this
1289 * factor doesn't matter because EWMA decreases exponentially
1290 */
1291static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1292{
1293 unsigned int ewma;
1294
Ming Lei6e7687172018-07-03 09:03:16 -06001295 ewma = hctx->dispatch_busy;
1296
1297 if (!ewma && !busy)
1298 return;
1299
1300 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1301 if (busy)
1302 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1303 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1304
1305 hctx->dispatch_busy = ewma;
1306}
1307
Ming Lei86ff7c22018-01-30 22:04:57 -05001308#define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1309
Johannes Thumshirnc92a4102020-03-25 00:24:44 +09001310static void blk_mq_handle_dev_resource(struct request *rq,
1311 struct list_head *list)
1312{
1313 struct request *next =
1314 list_first_entry_or_null(list, struct request, queuelist);
1315
1316 /*
1317 * If an I/O scheduler has been configured and we got a driver tag for
1318 * the next request already, free it.
1319 */
1320 if (next)
1321 blk_mq_put_driver_tag(next);
1322
1323 list_add(&rq->queuelist, list);
1324 __blk_mq_requeue_request(rq);
1325}
1326
Keith Busch0512a752020-05-12 17:55:47 +09001327static void blk_mq_handle_zone_resource(struct request *rq,
1328 struct list_head *zone_list)
1329{
1330 /*
1331 * If we end up here it is because we cannot dispatch a request to a
1332 * specific zone due to LLD level zone-write locking or other zone
1333 * related resource not being available. In this case, set the request
1334 * aside in zone_list for retrying it later.
1335 */
1336 list_add(&rq->queuelist, zone_list);
1337 __blk_mq_requeue_request(rq);
1338}
1339
Ming Lei75383522020-06-30 18:24:58 +08001340enum prep_dispatch {
1341 PREP_DISPATCH_OK,
1342 PREP_DISPATCH_NO_TAG,
1343 PREP_DISPATCH_NO_BUDGET,
1344};
1345
1346static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq,
1347 bool need_budget)
1348{
1349 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Ming Lei2a5a24a2021-01-22 10:33:12 +08001350 int budget_token = -1;
Ming Lei75383522020-06-30 18:24:58 +08001351
Ming Lei2a5a24a2021-01-22 10:33:12 +08001352 if (need_budget) {
1353 budget_token = blk_mq_get_dispatch_budget(rq->q);
1354 if (budget_token < 0) {
1355 blk_mq_put_driver_tag(rq);
1356 return PREP_DISPATCH_NO_BUDGET;
1357 }
1358 blk_mq_set_rq_budget_token(rq, budget_token);
Ming Lei75383522020-06-30 18:24:58 +08001359 }
1360
1361 if (!blk_mq_get_driver_tag(rq)) {
1362 /*
1363 * The initial allocation attempt failed, so we need to
1364 * rerun the hardware queue when a tag is freed. The
1365 * waitqueue takes care of that. If the queue is run
1366 * before we add this entry back on the dispatch list,
1367 * we'll re-run it below.
1368 */
1369 if (!blk_mq_mark_tag_wait(hctx, rq)) {
Ming Lei1fd40b52020-06-30 18:25:00 +08001370 /*
1371 * All budgets not got from this function will be put
1372 * together during handling partial dispatch
1373 */
1374 if (need_budget)
Ming Lei2a5a24a2021-01-22 10:33:12 +08001375 blk_mq_put_dispatch_budget(rq->q, budget_token);
Ming Lei75383522020-06-30 18:24:58 +08001376 return PREP_DISPATCH_NO_TAG;
1377 }
1378 }
1379
1380 return PREP_DISPATCH_OK;
1381}
1382
Ming Lei1fd40b52020-06-30 18:25:00 +08001383/* release all allocated budgets before calling to blk_mq_dispatch_rq_list */
1384static void blk_mq_release_budgets(struct request_queue *q,
Ming Lei2a5a24a2021-01-22 10:33:12 +08001385 struct list_head *list)
Ming Lei1fd40b52020-06-30 18:25:00 +08001386{
Ming Lei2a5a24a2021-01-22 10:33:12 +08001387 struct request *rq;
Ming Lei1fd40b52020-06-30 18:25:00 +08001388
Ming Lei2a5a24a2021-01-22 10:33:12 +08001389 list_for_each_entry(rq, list, queuelist) {
1390 int budget_token = blk_mq_get_rq_budget_token(rq);
1391
1392 if (budget_token >= 0)
1393 blk_mq_put_dispatch_budget(q, budget_token);
1394 }
Ming Lei1fd40b52020-06-30 18:25:00 +08001395}
1396
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001397/*
1398 * Returns true if we did some work AND can potentially do more.
1399 */
Ming Lei445874e2020-06-30 18:24:57 +08001400bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list,
Ming Lei1fd40b52020-06-30 18:25:00 +08001401 unsigned int nr_budgets)
Jens Axboef04c3df2016-12-07 08:41:17 -07001402{
Ming Lei75383522020-06-30 18:24:58 +08001403 enum prep_dispatch prep;
Ming Lei445874e2020-06-30 18:24:57 +08001404 struct request_queue *q = hctx->queue;
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001405 struct request *rq, *nxt;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001406 int errors, queued;
Ming Lei86ff7c22018-01-30 22:04:57 -05001407 blk_status_t ret = BLK_STS_OK;
Keith Busch0512a752020-05-12 17:55:47 +09001408 LIST_HEAD(zone_list);
Jens Axboef04c3df2016-12-07 08:41:17 -07001409
Omar Sandoval81380ca2017-04-07 08:56:26 -06001410 if (list_empty(list))
1411 return false;
1412
Jens Axboef04c3df2016-12-07 08:41:17 -07001413 /*
Jens Axboef04c3df2016-12-07 08:41:17 -07001414 * Now process all the entries, sending them to the driver.
1415 */
Jens Axboe93efe982017-03-24 12:04:19 -06001416 errors = queued = 0;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001417 do {
Jens Axboef04c3df2016-12-07 08:41:17 -07001418 struct blk_mq_queue_data bd;
1419
1420 rq = list_first_entry(list, struct request, queuelist);
Ming Lei0bca7992018-04-05 00:35:21 +08001421
Ming Lei445874e2020-06-30 18:24:57 +08001422 WARN_ON_ONCE(hctx != rq->mq_hctx);
Ming Lei1fd40b52020-06-30 18:25:00 +08001423 prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets);
Ming Lei75383522020-06-30 18:24:58 +08001424 if (prep != PREP_DISPATCH_OK)
Ming Lei0bca7992018-04-05 00:35:21 +08001425 break;
Ming Leide148292017-10-14 17:22:29 +08001426
Jens Axboef04c3df2016-12-07 08:41:17 -07001427 list_del_init(&rq->queuelist);
1428
1429 bd.rq = rq;
Jens Axboe113285b2017-03-02 13:26:04 -07001430
1431 /*
1432 * Flag last if we have no more requests, or if we have more
1433 * but can't assign a driver tag to it.
1434 */
1435 if (list_empty(list))
1436 bd.last = true;
1437 else {
Jens Axboe113285b2017-03-02 13:26:04 -07001438 nxt = list_first_entry(list, struct request, queuelist);
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001439 bd.last = !blk_mq_get_driver_tag(nxt);
Jens Axboe113285b2017-03-02 13:26:04 -07001440 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001441
Ming Lei1fd40b52020-06-30 18:25:00 +08001442 /*
1443 * once the request is queued to lld, no need to cover the
1444 * budget any more
1445 */
1446 if (nr_budgets)
1447 nr_budgets--;
Jens Axboef04c3df2016-12-07 08:41:17 -07001448 ret = q->mq_ops->queue_rq(hctx, &bd);
Ming Lei7bf13722020-07-01 21:58:57 +08001449 switch (ret) {
1450 case BLK_STS_OK:
1451 queued++;
Jens Axboef04c3df2016-12-07 08:41:17 -07001452 break;
Ming Lei7bf13722020-07-01 21:58:57 +08001453 case BLK_STS_RESOURCE:
1454 case BLK_STS_DEV_RESOURCE:
1455 blk_mq_handle_dev_resource(rq, list);
1456 goto out;
1457 case BLK_STS_ZONE_RESOURCE:
Keith Busch0512a752020-05-12 17:55:47 +09001458 /*
1459 * Move the request to zone_list and keep going through
1460 * the dispatch list to find more requests the drive can
1461 * accept.
1462 */
1463 blk_mq_handle_zone_resource(rq, &zone_list);
Ming Lei7bf13722020-07-01 21:58:57 +08001464 break;
1465 default:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001466 errors++;
Hannes Reineckee21ee5a2020-09-30 10:02:53 +02001467 blk_mq_end_request(rq, ret);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001468 }
Omar Sandoval81380ca2017-04-07 08:56:26 -06001469 } while (!list_empty(list));
Ming Lei7bf13722020-07-01 21:58:57 +08001470out:
Keith Busch0512a752020-05-12 17:55:47 +09001471 if (!list_empty(&zone_list))
1472 list_splice_tail_init(&zone_list, list);
1473
Jens Axboef04c3df2016-12-07 08:41:17 -07001474 hctx->dispatched[queued_to_index(queued)]++;
1475
yangerkun632bfb62020-09-05 19:25:56 +08001476 /* If we didn't flush the entire list, we could have told the driver
1477 * there was more coming, but that turned out to be a lie.
1478 */
1479 if ((!list_empty(list) || errors) && q->mq_ops->commit_rqs && queued)
1480 q->mq_ops->commit_rqs(hctx);
Jens Axboef04c3df2016-12-07 08:41:17 -07001481 /*
1482 * Any items that need requeuing? Stuff them into hctx->dispatch,
1483 * that is where we will continue on next queue run.
1484 */
1485 if (!list_empty(list)) {
Ming Lei86ff7c22018-01-30 22:04:57 -05001486 bool needs_restart;
Ming Lei75383522020-06-30 18:24:58 +08001487 /* For non-shared tags, the RESTART check will suffice */
1488 bool no_tag = prep == PREP_DISPATCH_NO_TAG &&
Ming Lei51db1c32020-08-19 23:20:19 +08001489 (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED);
Ming Lei75383522020-06-30 18:24:58 +08001490 bool no_budget_avail = prep == PREP_DISPATCH_NO_BUDGET;
Ming Lei86ff7c22018-01-30 22:04:57 -05001491
Ming Lei2a5a24a2021-01-22 10:33:12 +08001492 if (nr_budgets)
1493 blk_mq_release_budgets(q, list);
Jens Axboef04c3df2016-12-07 08:41:17 -07001494
1495 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001496 list_splice_tail_init(list, &hctx->dispatch);
Jens Axboef04c3df2016-12-07 08:41:17 -07001497 spin_unlock(&hctx->lock);
1498
1499 /*
Ming Leid7d85352020-08-17 18:01:15 +08001500 * Order adding requests to hctx->dispatch and checking
1501 * SCHED_RESTART flag. The pair of this smp_mb() is the one
1502 * in blk_mq_sched_restart(). Avoid restart code path to
1503 * miss the new added requests to hctx->dispatch, meantime
1504 * SCHED_RESTART is observed here.
1505 */
1506 smp_mb();
1507
1508 /*
Bart Van Assche710c7852017-04-07 11:16:51 -07001509 * If SCHED_RESTART was set by the caller of this function and
1510 * it is no longer set that means that it was cleared by another
1511 * thread and hence that a queue rerun is needed.
Jens Axboef04c3df2016-12-07 08:41:17 -07001512 *
Jens Axboeeb619fd2017-11-09 08:32:43 -07001513 * If 'no_tag' is set, that means that we failed getting
1514 * a driver tag with an I/O scheduler attached. If our dispatch
1515 * waitqueue is no longer active, ensure that we run the queue
1516 * AFTER adding our entries back to the list.
Jens Axboebd166ef2017-01-17 06:03:22 -07001517 *
Bart Van Assche710c7852017-04-07 11:16:51 -07001518 * If no I/O scheduler has been configured it is possible that
1519 * the hardware queue got stopped and restarted before requests
1520 * were pushed back onto the dispatch list. Rerun the queue to
1521 * avoid starvation. Notes:
1522 * - blk_mq_run_hw_queue() checks whether or not a queue has
1523 * been stopped before rerunning a queue.
1524 * - Some but not all block drivers stop a queue before
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001525 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
Bart Van Assche710c7852017-04-07 11:16:51 -07001526 * and dm-rq.
Ming Lei86ff7c22018-01-30 22:04:57 -05001527 *
1528 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1529 * bit is set, run queue after a delay to avoid IO stalls
Douglas Andersonab3cee32020-04-20 09:24:51 -07001530 * that could otherwise occur if the queue is idle. We'll do
1531 * similar if we couldn't get budget and SCHED_RESTART is set.
Jens Axboebd166ef2017-01-17 06:03:22 -07001532 */
Ming Lei86ff7c22018-01-30 22:04:57 -05001533 needs_restart = blk_mq_sched_needs_restart(hctx);
1534 if (!needs_restart ||
Jens Axboeeb619fd2017-11-09 08:32:43 -07001535 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
Jens Axboebd166ef2017-01-17 06:03:22 -07001536 blk_mq_run_hw_queue(hctx, true);
Douglas Andersonab3cee32020-04-20 09:24:51 -07001537 else if (needs_restart && (ret == BLK_STS_RESOURCE ||
1538 no_budget_avail))
Ming Lei86ff7c22018-01-30 22:04:57 -05001539 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001540
Ming Lei6e7687172018-07-03 09:03:16 -06001541 blk_mq_update_dispatch_busy(hctx, true);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001542 return false;
Ming Lei6e7687172018-07-03 09:03:16 -06001543 } else
1544 blk_mq_update_dispatch_busy(hctx, false);
Jens Axboef04c3df2016-12-07 08:41:17 -07001545
Jens Axboe93efe982017-03-24 12:04:19 -06001546 return (queued + errors) != 0;
Jens Axboef04c3df2016-12-07 08:41:17 -07001547}
1548
André Almeida105663f2020-01-06 15:08:18 -03001549/**
1550 * __blk_mq_run_hw_queue - Run a hardware queue.
1551 * @hctx: Pointer to the hardware queue to run.
1552 *
1553 * Send pending requests to the hardware.
1554 */
Bart Van Assche6a83e742016-11-02 10:09:51 -06001555static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1556{
1557 int srcu_idx;
1558
Jens Axboeb7a71e62017-08-01 09:28:24 -06001559 /*
Jens Axboeb7a71e62017-08-01 09:28:24 -06001560 * We can't run the queue inline with ints disabled. Ensure that
1561 * we catch bad users of this early.
1562 */
1563 WARN_ON_ONCE(in_interrupt());
1564
Jens Axboe04ced152018-01-09 08:29:46 -08001565 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
Jens Axboebf4907c2017-03-30 12:30:39 -06001566
Jens Axboe04ced152018-01-09 08:29:46 -08001567 hctx_lock(hctx, &srcu_idx);
1568 blk_mq_sched_dispatch_requests(hctx);
1569 hctx_unlock(hctx, srcu_idx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001570}
1571
Ming Leif82ddf12018-04-08 17:48:10 +08001572static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1573{
1574 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1575
1576 if (cpu >= nr_cpu_ids)
1577 cpu = cpumask_first(hctx->cpumask);
1578 return cpu;
1579}
1580
Jens Axboe506e9312014-05-07 10:26:44 -06001581/*
1582 * It'd be great if the workqueue API had a way to pass
1583 * in a mask and had some smarts for more clever placement.
1584 * For now we just round-robin here, switching for every
1585 * BLK_MQ_CPU_WORK_BATCH queued items.
1586 */
1587static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1588{
Ming Lei7bed4592018-01-18 00:41:51 +08001589 bool tried = false;
Ming Lei476f8c92018-04-08 17:48:09 +08001590 int next_cpu = hctx->next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001591
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001592 if (hctx->queue->nr_hw_queues == 1)
1593 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -06001594
1595 if (--hctx->next_cpu_batch <= 0) {
Ming Lei7bed4592018-01-18 00:41:51 +08001596select_cpu:
Ming Lei476f8c92018-04-08 17:48:09 +08001597 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08001598 cpu_online_mask);
Jens Axboe506e9312014-05-07 10:26:44 -06001599 if (next_cpu >= nr_cpu_ids)
Ming Leif82ddf12018-04-08 17:48:10 +08001600 next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06001601 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1602 }
1603
Ming Lei7bed4592018-01-18 00:41:51 +08001604 /*
1605 * Do unbound schedule if we can't find a online CPU for this hctx,
1606 * and it should only happen in the path of handling CPU DEAD.
1607 */
Ming Lei476f8c92018-04-08 17:48:09 +08001608 if (!cpu_online(next_cpu)) {
Ming Lei7bed4592018-01-18 00:41:51 +08001609 if (!tried) {
1610 tried = true;
1611 goto select_cpu;
1612 }
1613
1614 /*
1615 * Make sure to re-select CPU next time once after CPUs
1616 * in hctx->cpumask become online again.
1617 */
Ming Lei476f8c92018-04-08 17:48:09 +08001618 hctx->next_cpu = next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001619 hctx->next_cpu_batch = 1;
1620 return WORK_CPU_UNBOUND;
1621 }
Ming Lei476f8c92018-04-08 17:48:09 +08001622
1623 hctx->next_cpu = next_cpu;
1624 return next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -06001625}
1626
André Almeida105663f2020-01-06 15:08:18 -03001627/**
1628 * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue.
1629 * @hctx: Pointer to the hardware queue to run.
1630 * @async: If we want to run the queue asynchronously.
Minwoo Imfa94ba82020-12-05 00:20:55 +09001631 * @msecs: Milliseconds of delay to wait before running the queue.
André Almeida105663f2020-01-06 15:08:18 -03001632 *
1633 * If !@async, try to run the queue now. Else, run the queue asynchronously and
1634 * with a delay of @msecs.
1635 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001636static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1637 unsigned long msecs)
Jens Axboe320ae512013-10-24 09:20:05 +01001638{
Bart Van Assche5435c022017-06-20 11:15:49 -07001639 if (unlikely(blk_mq_hctx_stopped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +01001640 return;
1641
Jens Axboe1b792f22016-09-21 10:12:13 -06001642 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001643 int cpu = get_cpu();
1644 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +01001645 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001646 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +01001647 return;
1648 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001649
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001650 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -06001651 }
Paolo Bonzini398205b2014-11-07 23:03:59 +01001652
Bart Van Asscheae943d22018-01-19 08:58:55 -08001653 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1654 msecs_to_jiffies(msecs));
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001655}
1656
André Almeida105663f2020-01-06 15:08:18 -03001657/**
1658 * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
1659 * @hctx: Pointer to the hardware queue to run.
Minwoo Imfa94ba82020-12-05 00:20:55 +09001660 * @msecs: Milliseconds of delay to wait before running the queue.
André Almeida105663f2020-01-06 15:08:18 -03001661 *
1662 * Run a hardware queue asynchronously with a delay of @msecs.
1663 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001664void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1665{
1666 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1667}
1668EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1669
André Almeida105663f2020-01-06 15:08:18 -03001670/**
1671 * blk_mq_run_hw_queue - Start to run a hardware queue.
1672 * @hctx: Pointer to the hardware queue to run.
1673 * @async: If we want to run the queue asynchronously.
1674 *
1675 * Check if the request queue is not in a quiesced state and if there are
1676 * pending requests to be sent. If this is true, run the queue to send requests
1677 * to hardware.
1678 */
John Garry626fb732019-10-30 00:59:30 +08001679void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001680{
Ming Lei24f5a902018-01-06 16:27:38 +08001681 int srcu_idx;
1682 bool need_run;
1683
1684 /*
1685 * When queue is quiesced, we may be switching io scheduler, or
1686 * updating nr_hw_queues, or other things, and we can't run queue
1687 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
1688 *
1689 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
1690 * quiesced.
1691 */
Jens Axboe04ced152018-01-09 08:29:46 -08001692 hctx_lock(hctx, &srcu_idx);
1693 need_run = !blk_queue_quiesced(hctx->queue) &&
1694 blk_mq_hctx_has_pending(hctx);
1695 hctx_unlock(hctx, srcu_idx);
Ming Lei24f5a902018-01-06 16:27:38 +08001696
John Garry626fb732019-10-30 00:59:30 +08001697 if (need_run)
Jens Axboe79f720a2017-11-10 09:13:21 -07001698 __blk_mq_delay_run_hw_queue(hctx, async, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01001699}
Omar Sandoval5b727272017-04-14 01:00:00 -07001700EXPORT_SYMBOL(blk_mq_run_hw_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01001701
Jan Karab6e68ee2021-01-11 17:47:17 +01001702/*
1703 * Is the request queue handled by an IO scheduler that does not respect
1704 * hardware queues when dispatching?
1705 */
1706static bool blk_mq_has_sqsched(struct request_queue *q)
1707{
1708 struct elevator_queue *e = q->elevator;
1709
1710 if (e && e->type->ops.dispatch_request &&
1711 !(e->type->elevator_features & ELEVATOR_F_MQ_AWARE))
1712 return true;
1713 return false;
1714}
1715
1716/*
1717 * Return prefered queue to dispatch from (if any) for non-mq aware IO
1718 * scheduler.
1719 */
1720static struct blk_mq_hw_ctx *blk_mq_get_sq_hctx(struct request_queue *q)
1721{
1722 struct blk_mq_hw_ctx *hctx;
1723
1724 /*
1725 * If the IO scheduler does not respect hardware queues when
1726 * dispatching, we just don't bother with multiple HW queues and
1727 * dispatch from hctx for the current CPU since running multiple queues
1728 * just causes lock contention inside the scheduler and pointless cache
1729 * bouncing.
1730 */
1731 hctx = blk_mq_map_queue_type(q, HCTX_TYPE_DEFAULT,
1732 raw_smp_processor_id());
1733 if (!blk_mq_hctx_stopped(hctx))
1734 return hctx;
1735 return NULL;
1736}
1737
André Almeida105663f2020-01-06 15:08:18 -03001738/**
Mauro Carvalho Chehab24f7bb82020-10-23 18:32:54 +02001739 * blk_mq_run_hw_queues - Run all hardware queues in a request queue.
André Almeida105663f2020-01-06 15:08:18 -03001740 * @q: Pointer to the request queue to run.
1741 * @async: If we want to run the queue asynchronously.
1742 */
Mike Snitzerb94ec292015-03-11 23:56:38 -04001743void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001744{
Jan Karab6e68ee2021-01-11 17:47:17 +01001745 struct blk_mq_hw_ctx *hctx, *sq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001746 int i;
1747
Jan Karab6e68ee2021-01-11 17:47:17 +01001748 sq_hctx = NULL;
1749 if (blk_mq_has_sqsched(q))
1750 sq_hctx = blk_mq_get_sq_hctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001751 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe79f720a2017-11-10 09:13:21 -07001752 if (blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +01001753 continue;
Jan Karab6e68ee2021-01-11 17:47:17 +01001754 /*
1755 * Dispatch from this hctx either if there's no hctx preferred
1756 * by IO scheduler or if it has requests that bypass the
1757 * scheduler.
1758 */
1759 if (!sq_hctx || sq_hctx == hctx ||
1760 !list_empty_careful(&hctx->dispatch))
1761 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001762 }
1763}
Mike Snitzerb94ec292015-03-11 23:56:38 -04001764EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001765
Bart Van Asschefd001442016-10-28 17:19:37 -07001766/**
Douglas Andersonb9151e72020-04-20 09:24:52 -07001767 * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously.
1768 * @q: Pointer to the request queue to run.
Minwoo Imfa94ba82020-12-05 00:20:55 +09001769 * @msecs: Milliseconds of delay to wait before running the queues.
Douglas Andersonb9151e72020-04-20 09:24:52 -07001770 */
1771void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs)
1772{
Jan Karab6e68ee2021-01-11 17:47:17 +01001773 struct blk_mq_hw_ctx *hctx, *sq_hctx;
Douglas Andersonb9151e72020-04-20 09:24:52 -07001774 int i;
1775
Jan Karab6e68ee2021-01-11 17:47:17 +01001776 sq_hctx = NULL;
1777 if (blk_mq_has_sqsched(q))
1778 sq_hctx = blk_mq_get_sq_hctx(q);
Douglas Andersonb9151e72020-04-20 09:24:52 -07001779 queue_for_each_hw_ctx(q, hctx, i) {
1780 if (blk_mq_hctx_stopped(hctx))
1781 continue;
Jan Karab6e68ee2021-01-11 17:47:17 +01001782 /*
1783 * Dispatch from this hctx either if there's no hctx preferred
1784 * by IO scheduler or if it has requests that bypass the
1785 * scheduler.
1786 */
1787 if (!sq_hctx || sq_hctx == hctx ||
1788 !list_empty_careful(&hctx->dispatch))
1789 blk_mq_delay_run_hw_queue(hctx, msecs);
Douglas Andersonb9151e72020-04-20 09:24:52 -07001790 }
1791}
1792EXPORT_SYMBOL(blk_mq_delay_run_hw_queues);
1793
1794/**
Bart Van Asschefd001442016-10-28 17:19:37 -07001795 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1796 * @q: request queue.
1797 *
1798 * The caller is responsible for serializing this function against
1799 * blk_mq_{start,stop}_hw_queue().
1800 */
1801bool blk_mq_queue_stopped(struct request_queue *q)
1802{
1803 struct blk_mq_hw_ctx *hctx;
1804 int i;
1805
1806 queue_for_each_hw_ctx(q, hctx, i)
1807 if (blk_mq_hctx_stopped(hctx))
1808 return true;
1809
1810 return false;
1811}
1812EXPORT_SYMBOL(blk_mq_queue_stopped);
1813
Ming Lei39a70c72017-06-06 23:22:09 +08001814/*
1815 * This function is often used for pausing .queue_rq() by driver when
1816 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001817 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001818 *
1819 * We do not guarantee that dispatch can be drained or blocked
1820 * after blk_mq_stop_hw_queue() returns. Please use
1821 * blk_mq_quiesce_queue() for that requirement.
1822 */
Jens Axboe320ae512013-10-24 09:20:05 +01001823void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1824{
Ming Lei641a9ed2017-06-06 23:22:10 +08001825 cancel_delayed_work(&hctx->run_work);
1826
1827 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboe320ae512013-10-24 09:20:05 +01001828}
1829EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1830
Ming Lei39a70c72017-06-06 23:22:09 +08001831/*
1832 * This function is often used for pausing .queue_rq() by driver when
1833 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001834 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001835 *
1836 * We do not guarantee that dispatch can be drained or blocked
1837 * after blk_mq_stop_hw_queues() returns. Please use
1838 * blk_mq_quiesce_queue() for that requirement.
1839 */
Jens Axboe2719aa22017-05-03 11:08:14 -06001840void blk_mq_stop_hw_queues(struct request_queue *q)
1841{
Ming Lei641a9ed2017-06-06 23:22:10 +08001842 struct blk_mq_hw_ctx *hctx;
1843 int i;
1844
1845 queue_for_each_hw_ctx(q, hctx, i)
1846 blk_mq_stop_hw_queue(hctx);
Christoph Hellwig280d45f2013-10-25 14:45:58 +01001847}
1848EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1849
Jens Axboe320ae512013-10-24 09:20:05 +01001850void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1851{
1852 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -06001853
Jens Axboe0ffbce82014-06-25 08:22:34 -06001854 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001855}
1856EXPORT_SYMBOL(blk_mq_start_hw_queue);
1857
Christoph Hellwig2f268552014-04-16 09:44:56 +02001858void blk_mq_start_hw_queues(struct request_queue *q)
1859{
1860 struct blk_mq_hw_ctx *hctx;
1861 int i;
1862
1863 queue_for_each_hw_ctx(q, hctx, i)
1864 blk_mq_start_hw_queue(hctx);
1865}
1866EXPORT_SYMBOL(blk_mq_start_hw_queues);
1867
Jens Axboeae911c52016-12-08 13:19:30 -07001868void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1869{
1870 if (!blk_mq_hctx_stopped(hctx))
1871 return;
1872
1873 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1874 blk_mq_run_hw_queue(hctx, async);
1875}
1876EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1877
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001878void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001879{
1880 struct blk_mq_hw_ctx *hctx;
1881 int i;
1882
Jens Axboeae911c52016-12-08 13:19:30 -07001883 queue_for_each_hw_ctx(q, hctx, i)
1884 blk_mq_start_stopped_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001885}
1886EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1887
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001888static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001889{
1890 struct blk_mq_hw_ctx *hctx;
1891
Jens Axboe9f993732017-04-10 09:54:54 -06001892 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboe21c6e932017-04-10 09:54:56 -06001893
1894 /*
Ming Lei15fe8a902018-04-08 17:48:11 +08001895 * If we are stopped, don't run the queue.
Jens Axboe21c6e932017-04-10 09:54:56 -06001896 */
Yufen Yu08410312020-10-08 23:26:30 -04001897 if (blk_mq_hctx_stopped(hctx))
Jianchao Wang0196d6b2018-06-04 17:03:55 +08001898 return;
Jens Axboee4043dc2014-04-09 10:18:23 -06001899
Jens Axboe320ae512013-10-24 09:20:05 +01001900 __blk_mq_run_hw_queue(hctx);
1901}
1902
Ming Leicfd0c552015-10-20 23:13:57 +08001903static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08001904 struct request *rq,
1905 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01001906{
Jens Axboee57690f2016-08-24 15:34:35 -06001907 struct blk_mq_ctx *ctx = rq->mq_ctx;
Ming Leic16d6b52018-12-17 08:44:05 -07001908 enum hctx_type type = hctx->type;
Jens Axboee57690f2016-08-24 15:34:35 -06001909
Bart Van Assche7b607812017-06-20 11:15:47 -07001910 lockdep_assert_held(&ctx->lock);
1911
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001912 trace_block_rq_insert(rq);
Jens Axboe01b983c2013-11-19 18:59:10 -07001913
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001914 if (at_head)
Ming Leic16d6b52018-12-17 08:44:05 -07001915 list_add(&rq->queuelist, &ctx->rq_lists[type]);
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001916 else
Ming Leic16d6b52018-12-17 08:44:05 -07001917 list_add_tail(&rq->queuelist, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08001918}
Jens Axboe4bb659b2014-05-09 09:36:49 -06001919
Jens Axboe2c3ad662016-12-14 14:34:47 -07001920void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1921 bool at_head)
Ming Leicfd0c552015-10-20 23:13:57 +08001922{
1923 struct blk_mq_ctx *ctx = rq->mq_ctx;
1924
Bart Van Assche7b607812017-06-20 11:15:47 -07001925 lockdep_assert_held(&ctx->lock);
1926
Jens Axboee57690f2016-08-24 15:34:35 -06001927 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01001928 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001929}
1930
André Almeida105663f2020-01-06 15:08:18 -03001931/**
1932 * blk_mq_request_bypass_insert - Insert a request at dispatch list.
1933 * @rq: Pointer to request to be inserted.
Randy Dunlap26bfeb22020-08-16 16:39:34 -07001934 * @at_head: true if the request should be inserted at the head of the list.
André Almeida105663f2020-01-06 15:08:18 -03001935 * @run_queue: If we should run the hardware queue after inserting the request.
1936 *
Jens Axboe157f3772017-09-11 16:43:57 -06001937 * Should only be used carefully, when the caller knows we want to
1938 * bypass a potential IO scheduler on the target device.
1939 */
Ming Lei01e99ae2020-02-25 09:04:32 +08001940void blk_mq_request_bypass_insert(struct request *rq, bool at_head,
1941 bool run_queue)
Jens Axboe157f3772017-09-11 16:43:57 -06001942{
Jens Axboeea4f9952018-10-29 15:06:13 -06001943 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe157f3772017-09-11 16:43:57 -06001944
1945 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001946 if (at_head)
1947 list_add(&rq->queuelist, &hctx->dispatch);
1948 else
1949 list_add_tail(&rq->queuelist, &hctx->dispatch);
Jens Axboe157f3772017-09-11 16:43:57 -06001950 spin_unlock(&hctx->lock);
1951
Ming Leib0850292017-11-02 23:24:34 +08001952 if (run_queue)
1953 blk_mq_run_hw_queue(hctx, false);
Jens Axboe157f3772017-09-11 16:43:57 -06001954}
1955
Jens Axboebd166ef2017-01-17 06:03:22 -07001956void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
1957 struct list_head *list)
Jens Axboe320ae512013-10-24 09:20:05 +01001958
1959{
Ming Lei3f0cedc2018-07-02 17:35:58 +08001960 struct request *rq;
Ming Leic16d6b52018-12-17 08:44:05 -07001961 enum hctx_type type = hctx->type;
Ming Lei3f0cedc2018-07-02 17:35:58 +08001962
Jens Axboe320ae512013-10-24 09:20:05 +01001963 /*
1964 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1965 * offline now
1966 */
Ming Lei3f0cedc2018-07-02 17:35:58 +08001967 list_for_each_entry(rq, list, queuelist) {
Jens Axboee57690f2016-08-24 15:34:35 -06001968 BUG_ON(rq->mq_ctx != ctx);
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001969 trace_block_rq_insert(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001970 }
Ming Lei3f0cedc2018-07-02 17:35:58 +08001971
1972 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001973 list_splice_tail_init(list, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08001974 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001975 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001976}
1977
Sami Tolvanen4f0f5862021-04-08 11:28:34 -07001978static int plug_rq_cmp(void *priv, const struct list_head *a,
1979 const struct list_head *b)
Jens Axboe320ae512013-10-24 09:20:05 +01001980{
1981 struct request *rqa = container_of(a, struct request, queuelist);
1982 struct request *rqb = container_of(b, struct request, queuelist);
1983
Pavel Begunkov7d30a622019-11-29 00:11:53 +03001984 if (rqa->mq_ctx != rqb->mq_ctx)
1985 return rqa->mq_ctx > rqb->mq_ctx;
1986 if (rqa->mq_hctx != rqb->mq_hctx)
1987 return rqa->mq_hctx > rqb->mq_hctx;
Jens Axboe3110fc72018-10-30 12:24:04 -06001988
1989 return blk_rq_pos(rqa) > blk_rq_pos(rqb);
Jens Axboe320ae512013-10-24 09:20:05 +01001990}
1991
1992void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1993{
Jens Axboe320ae512013-10-24 09:20:05 +01001994 LIST_HEAD(list);
Jens Axboe320ae512013-10-24 09:20:05 +01001995
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001996 if (list_empty(&plug->mq_list))
1997 return;
Jens Axboe320ae512013-10-24 09:20:05 +01001998 list_splice_init(&plug->mq_list, &list);
1999
Jens Axboece5b0092018-11-27 17:13:56 -07002000 if (plug->rq_count > 2 && plug->multiple_queues)
2001 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe320ae512013-10-24 09:20:05 +01002002
Dongli Zhangbcc816d2019-04-04 10:57:44 +08002003 plug->rq_count = 0;
2004
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03002005 do {
2006 struct list_head rq_list;
2007 struct request *rq, *head_rq = list_entry_rq(list.next);
2008 struct list_head *pos = &head_rq->queuelist; /* skip first */
2009 struct blk_mq_hw_ctx *this_hctx = head_rq->mq_hctx;
2010 struct blk_mq_ctx *this_ctx = head_rq->mq_ctx;
2011 unsigned int depth = 1;
Jens Axboe320ae512013-10-24 09:20:05 +01002012
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03002013 list_for_each_continue(pos, &list) {
2014 rq = list_entry_rq(pos);
2015 BUG_ON(!rq->q);
2016 if (rq->mq_hctx != this_hctx || rq->mq_ctx != this_ctx)
2017 break;
2018 depth++;
Jens Axboe320ae512013-10-24 09:20:05 +01002019 }
2020
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03002021 list_cut_before(&rq_list, &list, pos);
2022 trace_block_unplug(head_rq->q, depth, !from_schedule);
Jens Axboe67cae4c2018-10-30 11:31:51 -06002023 blk_mq_sched_insert_requests(this_hctx, this_ctx, &rq_list,
Jens Axboebd166ef2017-01-17 06:03:22 -07002024 from_schedule);
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03002025 } while(!list_empty(&list));
Jens Axboe320ae512013-10-24 09:20:05 +01002026}
2027
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002028static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
2029 unsigned int nr_segs)
Jens Axboe320ae512013-10-24 09:20:05 +01002030{
Eric Biggers93f221a2020-09-15 20:53:14 -07002031 int err;
2032
Christoph Hellwigf924cdd2019-06-06 12:29:00 +02002033 if (bio->bi_opf & REQ_RAHEAD)
2034 rq->cmd_flags |= REQ_FAILFAST_MASK;
2035
2036 rq->__sector = bio->bi_iter.bi_sector;
2037 rq->write_hint = bio->bi_write_hint;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002038 blk_rq_bio_prep(rq, bio, nr_segs);
Eric Biggers93f221a2020-09-15 20:53:14 -07002039
2040 /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */
2041 err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO);
2042 WARN_ON_ONCE(err);
Jens Axboe4b570522014-05-29 11:00:11 -06002043
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +02002044 blk_account_io_start(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01002045}
2046
Mike Snitzer0f955492018-01-17 11:25:56 -05002047static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
2048 struct request *rq,
Jens Axboebe94f052018-11-24 10:15:46 -07002049 blk_qc_t *cookie, bool last)
Shaohua Lif984df12015-05-08 10:51:32 -07002050{
Shaohua Lif984df12015-05-08 10:51:32 -07002051 struct request_queue *q = rq->q;
Shaohua Lif984df12015-05-08 10:51:32 -07002052 struct blk_mq_queue_data bd = {
2053 .rq = rq,
Jens Axboebe94f052018-11-24 10:15:46 -07002054 .last = last,
Shaohua Lif984df12015-05-08 10:51:32 -07002055 };
Jens Axboebd166ef2017-01-17 06:03:22 -07002056 blk_qc_t new_cookie;
Jens Axboef06345a2017-06-12 11:22:46 -06002057 blk_status_t ret;
Mike Snitzer0f955492018-01-17 11:25:56 -05002058
2059 new_cookie = request_to_qc_t(hctx, rq);
2060
2061 /*
2062 * For OK queue, we are done. For error, caller may kill it.
2063 * Any other error (busy), just add it to our list as we
2064 * previously would have done.
2065 */
2066 ret = q->mq_ops->queue_rq(hctx, &bd);
2067 switch (ret) {
2068 case BLK_STS_OK:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002069 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05002070 *cookie = new_cookie;
2071 break;
2072 case BLK_STS_RESOURCE:
Ming Lei86ff7c22018-01-30 22:04:57 -05002073 case BLK_STS_DEV_RESOURCE:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002074 blk_mq_update_dispatch_busy(hctx, true);
Mike Snitzer0f955492018-01-17 11:25:56 -05002075 __blk_mq_requeue_request(rq);
2076 break;
2077 default:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002078 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05002079 *cookie = BLK_QC_T_NONE;
2080 break;
2081 }
2082
2083 return ret;
2084}
2085
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002086static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
Mike Snitzer0f955492018-01-17 11:25:56 -05002087 struct request *rq,
Ming Lei396eaf22018-01-17 11:25:57 -05002088 blk_qc_t *cookie,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002089 bool bypass_insert, bool last)
Mike Snitzer0f955492018-01-17 11:25:56 -05002090{
2091 struct request_queue *q = rq->q;
Ming Leid964f042017-06-06 23:22:00 +08002092 bool run_queue = true;
Ming Lei2a5a24a2021-01-22 10:33:12 +08002093 int budget_token;
Ming Leid964f042017-06-06 23:22:00 +08002094
Ming Lei23d4ee12018-01-18 12:06:59 +08002095 /*
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002096 * RCU or SRCU read lock is needed before checking quiesced flag.
Ming Lei23d4ee12018-01-18 12:06:59 +08002097 *
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002098 * When queue is stopped or quiesced, ignore 'bypass_insert' from
2099 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
2100 * and avoid driver to try to dispatch again.
Ming Lei23d4ee12018-01-18 12:06:59 +08002101 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002102 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
Ming Leid964f042017-06-06 23:22:00 +08002103 run_queue = false;
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002104 bypass_insert = false;
2105 goto insert;
Ming Leid964f042017-06-06 23:22:00 +08002106 }
Shaohua Lif984df12015-05-08 10:51:32 -07002107
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002108 if (q->elevator && !bypass_insert)
2109 goto insert;
Bart Van Assche2253efc2016-10-28 17:20:02 -07002110
Ming Lei2a5a24a2021-01-22 10:33:12 +08002111 budget_token = blk_mq_get_dispatch_budget(q);
2112 if (budget_token < 0)
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002113 goto insert;
Jens Axboebd166ef2017-01-17 06:03:22 -07002114
Ming Lei2a5a24a2021-01-22 10:33:12 +08002115 blk_mq_set_rq_budget_token(rq, budget_token);
2116
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08002117 if (!blk_mq_get_driver_tag(rq)) {
Ming Lei2a5a24a2021-01-22 10:33:12 +08002118 blk_mq_put_dispatch_budget(q, budget_token);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002119 goto insert;
Ming Lei88022d72017-11-05 02:21:12 +08002120 }
Ming Leide148292017-10-14 17:22:29 +08002121
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002122 return __blk_mq_issue_directly(hctx, rq, cookie, last);
2123insert:
2124 if (bypass_insert)
2125 return BLK_STS_RESOURCE;
2126
Ming Leidb03f882020-08-18 17:07:28 +08002127 blk_mq_sched_insert_request(rq, false, run_queue, false);
2128
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002129 return BLK_STS_OK;
2130}
2131
André Almeida105663f2020-01-06 15:08:18 -03002132/**
2133 * blk_mq_try_issue_directly - Try to send a request directly to device driver.
2134 * @hctx: Pointer of the associated hardware queue.
2135 * @rq: Pointer to request to be sent.
2136 * @cookie: Request queue cookie.
2137 *
2138 * If the device has enough resources to accept a new request now, send the
2139 * request directly to device driver. Else, insert at hctx->dispatch queue, so
2140 * we can try send it another time in the future. Requests inserted at this
2141 * queue have higher priority.
2142 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002143static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
2144 struct request *rq, blk_qc_t *cookie)
2145{
2146 blk_status_t ret;
2147 int srcu_idx;
2148
2149 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
2150
2151 hctx_lock(hctx, &srcu_idx);
2152
2153 ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false, true);
2154 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
Ming Lei01e99ae2020-02-25 09:04:32 +08002155 blk_mq_request_bypass_insert(rq, false, true);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002156 else if (ret != BLK_STS_OK)
2157 blk_mq_end_request(rq, ret);
2158
Jens Axboe04ced152018-01-09 08:29:46 -08002159 hctx_unlock(hctx, srcu_idx);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002160}
2161
2162blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
2163{
2164 blk_status_t ret;
2165 int srcu_idx;
2166 blk_qc_t unused_cookie;
2167 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
2168
2169 hctx_lock(hctx, &srcu_idx);
2170 ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true, last);
2171 hctx_unlock(hctx, srcu_idx);
Jianchao Wang7f556a42018-12-14 09:28:18 +08002172
2173 return ret;
Christoph Hellwig5eb61262017-03-22 15:01:51 -04002174}
2175
Ming Lei6ce3dd62018-07-10 09:03:31 +08002176void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
2177 struct list_head *list)
2178{
Keith Busch536167d42020-04-07 03:13:48 +09002179 int queued = 0;
yangerkun632bfb62020-09-05 19:25:56 +08002180 int errors = 0;
Keith Busch536167d42020-04-07 03:13:48 +09002181
Ming Lei6ce3dd62018-07-10 09:03:31 +08002182 while (!list_empty(list)) {
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002183 blk_status_t ret;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002184 struct request *rq = list_first_entry(list, struct request,
2185 queuelist);
2186
2187 list_del_init(&rq->queuelist);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002188 ret = blk_mq_request_issue_directly(rq, list_empty(list));
2189 if (ret != BLK_STS_OK) {
2190 if (ret == BLK_STS_RESOURCE ||
2191 ret == BLK_STS_DEV_RESOURCE) {
Ming Lei01e99ae2020-02-25 09:04:32 +08002192 blk_mq_request_bypass_insert(rq, false,
Jens Axboec616cbe2018-12-06 22:17:44 -07002193 list_empty(list));
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002194 break;
2195 }
2196 blk_mq_end_request(rq, ret);
yangerkun632bfb62020-09-05 19:25:56 +08002197 errors++;
Keith Busch536167d42020-04-07 03:13:48 +09002198 } else
2199 queued++;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002200 }
Jens Axboed666ba92018-11-27 17:02:25 -07002201
2202 /*
2203 * If we didn't flush the entire list, we could have told
2204 * the driver there was more coming, but that turned out to
2205 * be a lie.
2206 */
yangerkun632bfb62020-09-05 19:25:56 +08002207 if ((!list_empty(list) || errors) &&
2208 hctx->queue->mq_ops->commit_rqs && queued)
Jens Axboed666ba92018-11-27 17:02:25 -07002209 hctx->queue->mq_ops->commit_rqs(hctx);
Ming Lei6ce3dd62018-07-10 09:03:31 +08002210}
2211
Jens Axboece5b0092018-11-27 17:13:56 -07002212static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
2213{
2214 list_add_tail(&rq->queuelist, &plug->mq_list);
2215 plug->rq_count++;
2216 if (!plug->multiple_queues && !list_is_singular(&plug->mq_list)) {
2217 struct request *tmp;
2218
2219 tmp = list_first_entry(&plug->mq_list, struct request,
2220 queuelist);
2221 if (tmp->q != rq->q)
2222 plug->multiple_queues = true;
2223 }
2224}
2225
Song Liu7f2a6a62021-09-07 16:03:38 -07002226/*
Jens Axboeba0ffdd2021-10-06 12:01:07 -06002227 * Allow 2x BLK_MAX_REQUEST_COUNT requests on plug queue for multiple
Song Liu7f2a6a62021-09-07 16:03:38 -07002228 * queues. This is important for md arrays to benefit from merging
2229 * requests.
2230 */
2231static inline unsigned short blk_plug_max_rq_count(struct blk_plug *plug)
2232{
2233 if (plug->multiple_queues)
Jens Axboeba0ffdd2021-10-06 12:01:07 -06002234 return BLK_MAX_REQUEST_COUNT * 2;
Song Liu7f2a6a62021-09-07 16:03:38 -07002235 return BLK_MAX_REQUEST_COUNT;
2236}
2237
André Almeida105663f2020-01-06 15:08:18 -03002238/**
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02002239 * blk_mq_submit_bio - Create and send a request to block device.
André Almeida105663f2020-01-06 15:08:18 -03002240 * @bio: Bio pointer.
2241 *
2242 * Builds up a request structure from @q and @bio and send to the device. The
2243 * request may not be queued directly to hardware if:
2244 * * This request can be merged with another one
2245 * * We want to place request at plug queue for possible future merging
2246 * * There is an IO scheduler active at this queue
2247 *
2248 * It will not queue the request if there is an error with the bio, or at the
2249 * request creation.
2250 *
2251 * Returns: Request queue cookie.
2252 */
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02002253blk_qc_t blk_mq_submit_bio(struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06002254{
Christoph Hellwig309dca302021-01-24 11:02:34 +01002255 struct request_queue *q = bio->bi_bdev->bd_disk->queue;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002256 const int is_sync = op_is_sync(bio->bi_opf);
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07002257 const int is_flush_fua = op_is_flush(bio->bi_opf);
Jens Axboe07068d52014-05-22 10:40:51 -06002258 struct request *rq;
Shaohua Lif984df12015-05-08 10:51:32 -07002259 struct blk_plug *plug;
Shaohua Li5b3f3412015-05-08 10:51:33 -07002260 struct request *same_queue_rq = NULL;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002261 unsigned int nr_segs;
Jens Axboe7b371632015-11-05 10:41:40 -07002262 blk_qc_t cookie;
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002263 blk_status_t ret;
Jeffle Xucc29e1b2020-11-26 17:18:52 +08002264 bool hipri;
Jens Axboe07068d52014-05-22 10:40:51 -06002265
2266 blk_queue_bounce(q, &bio);
Christoph Hellwigf695ca32020-07-01 10:59:39 +02002267 __blk_queue_split(&bio, &nr_segs);
Wen Xiongf36ea502017-05-10 08:54:11 -05002268
Dmitry Monakhove23947b2017-06-29 11:31:11 -07002269 if (!bio_integrity_prep(bio))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002270 goto queue_exit;
Jens Axboe07068d52014-05-22 10:40:51 -06002271
Omar Sandoval87c279e2016-06-01 22:18:48 -07002272 if (!is_flush_fua && !blk_queue_nomerges(q) &&
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002273 blk_attempt_plug_merge(q, bio, nr_segs, &same_queue_rq))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002274 goto queue_exit;
Shaohua Lif984df12015-05-08 10:51:32 -07002275
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002276 if (blk_mq_sched_bio_merge(q, bio, nr_segs))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002277 goto queue_exit;
Jens Axboebd166ef2017-01-17 06:03:22 -07002278
Christoph Hellwigd5337562018-11-14 17:02:09 +01002279 rq_qos_throttle(q, bio);
Jens Axboe87760e52016-11-09 12:38:14 -07002280
Jeffle Xucc29e1b2020-11-26 17:18:52 +08002281 hipri = bio->bi_opf & REQ_HIPRI;
2282
Jens Axboe47c122e2021-10-06 06:34:11 -06002283 plug = blk_mq_plug(q, bio);
2284 if (plug && plug->cached_rq) {
2285 rq = plug->cached_rq;
2286 plug->cached_rq = rq->rq_next;
2287 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe47c122e2021-10-06 06:34:11 -06002288 } else {
Christoph Hellwig0f38d762021-10-12 12:40:45 +02002289 struct blk_mq_alloc_data data = {
2290 .q = q,
2291 .nr_tags = 1,
2292 .cmd_flags = bio->bi_opf,
2293 };
2294
Jens Axboe47c122e2021-10-06 06:34:11 -06002295 if (plug) {
2296 data.nr_tags = plug->nr_ios;
2297 plug->nr_ios = 1;
2298 data.cached_rq = &plug->cached_rq;
2299 }
Christoph Hellwigb90cfae2021-10-12 12:40:44 +02002300 rq = __blk_mq_alloc_requests(&data);
Jens Axboe47c122e2021-10-06 06:34:11 -06002301 if (unlikely(!rq)) {
2302 rq_qos_cleanup(q, bio);
2303 if (bio->bi_opf & REQ_NOWAIT)
2304 bio_wouldblock_error(bio);
2305 goto queue_exit;
2306 }
Jens Axboe87760e52016-11-09 12:38:14 -07002307 }
2308
Christoph Hellwige8a676d2020-12-03 17:21:36 +01002309 trace_block_getrq(bio);
Xiaoguang Wangd6f1dda2018-10-23 22:30:50 +08002310
Josef Bacikc1c80382018-07-03 11:14:59 -04002311 rq_qos_track(q, rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06002312
Christoph Hellwig0f38d762021-10-12 12:40:45 +02002313 cookie = request_to_qc_t(rq->mq_hctx, rq);
Jens Axboe07068d52014-05-22 10:40:51 -06002314
Bart Van Assche970d1682019-07-01 08:47:30 -07002315 blk_mq_bio_to_request(rq, bio, nr_segs);
2316
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002317 ret = blk_crypto_init_request(rq);
2318 if (ret != BLK_STS_OK) {
2319 bio->bi_status = ret;
2320 bio_endio(bio);
2321 blk_mq_free_request(rq);
2322 return BLK_QC_T_NONE;
2323 }
2324
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04002325 if (unlikely(is_flush_fua)) {
Jens Axboe4a60f362021-10-16 07:34:49 -06002326 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
André Almeida105663f2020-01-06 15:08:18 -03002327 /* Bypass scheduler for flush requests */
Ming Lei923218f2017-11-02 23:24:38 +08002328 blk_insert_flush(rq);
Jens Axboe4a60f362021-10-16 07:34:49 -06002329 blk_mq_run_hw_queue(hctx, true);
Ming Lei03f26d82021-05-14 10:20:52 +08002330 } else if (plug && (q->nr_hw_queues == 1 ||
John Garry079a2e32021-10-05 18:23:39 +08002331 blk_mq_is_shared_tags(rq->mq_hctx->flags) ||
Ming Lei03f26d82021-05-14 10:20:52 +08002332 q->mq_ops->commit_rqs || !blk_queue_nonrot(q))) {
Jens Axboeb2c5d162018-11-29 10:03:42 -07002333 /*
2334 * Use plugging if we have a ->commit_rqs() hook as well, as
2335 * we know the driver uses bd->last in a smart fashion.
Ming Lei3154df22019-09-27 15:24:31 +08002336 *
2337 * Use normal plugging if this disk is slow HDD, as sequential
2338 * IO may benefit a lot from plug merging.
Jens Axboeb2c5d162018-11-29 10:03:42 -07002339 */
Jens Axboe5f0ed772018-11-23 22:04:33 -07002340 unsigned int request_count = plug->rq_count;
Shaohua Li600271d2016-11-03 17:03:54 -07002341 struct request *last = NULL;
2342
Ming Lei676d0602015-10-20 23:13:56 +08002343 if (!request_count)
Jeff Moyere6c44382015-05-08 10:51:30 -07002344 trace_block_plug(q);
Shaohua Li600271d2016-11-03 17:03:54 -07002345 else
2346 last = list_entry_rq(plug->mq_list.prev);
Jens Axboeb094f892015-11-20 20:29:45 -07002347
Song Liu7f2a6a62021-09-07 16:03:38 -07002348 if (request_count >= blk_plug_max_rq_count(plug) || (last &&
Shaohua Li600271d2016-11-03 17:03:54 -07002349 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
Jeff Moyere6c44382015-05-08 10:51:30 -07002350 blk_flush_plug_list(plug, false);
2351 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002352 }
Jens Axboeb094f892015-11-20 20:29:45 -07002353
Jens Axboece5b0092018-11-27 17:13:56 -07002354 blk_add_rq_to_plug(plug, rq);
Ming Leia12de1d2019-09-27 15:24:30 +08002355 } else if (q->elevator) {
André Almeida105663f2020-01-06 15:08:18 -03002356 /* Insert the request at the IO scheduler queue */
Ming Leia12de1d2019-09-27 15:24:30 +08002357 blk_mq_sched_insert_request(rq, false, true, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002358 } else if (plug && !blk_queue_nomerges(q)) {
Jens Axboe320ae512013-10-24 09:20:05 +01002359 /*
2360 * We do limited plugging. If the bio can be merged, do that.
2361 * Otherwise the existing request in the plug list will be
2362 * issued. So the plug list will have one request at most
Christoph Hellwig22997222017-03-22 15:01:52 -04002363 * The plug list might get flushed before this. If that happens,
2364 * the plug list is empty, and same_queue_rq is invalid.
Jens Axboe320ae512013-10-24 09:20:05 +01002365 */
Christoph Hellwig22997222017-03-22 15:01:52 -04002366 if (list_empty(&plug->mq_list))
2367 same_queue_rq = NULL;
Jens Axboe4711b572018-11-27 17:07:17 -07002368 if (same_queue_rq) {
Christoph Hellwig22997222017-03-22 15:01:52 -04002369 list_del_init(&same_queue_rq->queuelist);
Jens Axboe4711b572018-11-27 17:07:17 -07002370 plug->rq_count--;
2371 }
Jens Axboece5b0092018-11-27 17:13:56 -07002372 blk_add_rq_to_plug(plug, rq);
Yufen Yuff3b74b2019-03-26 21:19:25 +08002373 trace_block_plug(q);
Christoph Hellwig22997222017-03-22 15:01:52 -04002374
Ming Leidad7a3b2017-06-06 23:21:59 +08002375 if (same_queue_rq) {
Yufen Yuff3b74b2019-03-26 21:19:25 +08002376 trace_block_unplug(q, 1, true);
Christoph Hellwig0f38d762021-10-12 12:40:45 +02002377 blk_mq_try_issue_directly(same_queue_rq->mq_hctx,
2378 same_queue_rq, &cookie);
Ming Leidad7a3b2017-06-06 23:21:59 +08002379 }
Ming Leia12de1d2019-09-27 15:24:30 +08002380 } else if ((q->nr_hw_queues > 1 && is_sync) ||
Christoph Hellwig0f38d762021-10-12 12:40:45 +02002381 !rq->mq_hctx->dispatch_busy) {
André Almeida105663f2020-01-06 15:08:18 -03002382 /*
2383 * There is no scheduler and we can try to send directly
2384 * to the hardware.
2385 */
Christoph Hellwig0f38d762021-10-12 12:40:45 +02002386 blk_mq_try_issue_directly(rq->mq_hctx, rq, &cookie);
Ming Leiab42f352017-05-26 19:53:19 +08002387 } else {
André Almeida105663f2020-01-06 15:08:18 -03002388 /* Default case. */
huhai8fa9f552018-05-16 08:21:21 -06002389 blk_mq_sched_insert_request(rq, false, true, true);
Ming Leiab42f352017-05-26 19:53:19 +08002390 }
Jens Axboe320ae512013-10-24 09:20:05 +01002391
Jeffle Xucc29e1b2020-11-26 17:18:52 +08002392 if (!hipri)
2393 return BLK_QC_T_NONE;
Jens Axboe7b371632015-11-05 10:41:40 -07002394 return cookie;
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002395queue_exit:
2396 blk_queue_exit(q);
2397 return BLK_QC_T_NONE;
Jens Axboe320ae512013-10-24 09:20:05 +01002398}
2399
Ming Leibd631412021-05-11 23:22:35 +08002400static size_t order_to_size(unsigned int order)
2401{
2402 return (size_t)PAGE_SIZE << order;
2403}
2404
2405/* called before freeing request pool in @tags */
John Garryf32e4ea2021-10-05 18:23:32 +08002406static void blk_mq_clear_rq_mapping(struct blk_mq_tags *drv_tags,
2407 struct blk_mq_tags *tags)
Ming Leibd631412021-05-11 23:22:35 +08002408{
Ming Leibd631412021-05-11 23:22:35 +08002409 struct page *page;
2410 unsigned long flags;
2411
John Garry4f245d52021-10-05 18:23:33 +08002412 /* There is no need to clear a driver tags own mapping */
2413 if (drv_tags == tags)
2414 return;
2415
Ming Leibd631412021-05-11 23:22:35 +08002416 list_for_each_entry(page, &tags->page_list, lru) {
2417 unsigned long start = (unsigned long)page_address(page);
2418 unsigned long end = start + order_to_size(page->private);
2419 int i;
2420
John Garryf32e4ea2021-10-05 18:23:32 +08002421 for (i = 0; i < drv_tags->nr_tags; i++) {
Ming Leibd631412021-05-11 23:22:35 +08002422 struct request *rq = drv_tags->rqs[i];
2423 unsigned long rq_addr = (unsigned long)rq;
2424
2425 if (rq_addr >= start && rq_addr < end) {
2426 WARN_ON_ONCE(refcount_read(&rq->ref) != 0);
2427 cmpxchg(&drv_tags->rqs[i], rq, NULL);
2428 }
2429 }
2430 }
2431
2432 /*
2433 * Wait until all pending iteration is done.
2434 *
2435 * Request reference is cleared and it is guaranteed to be observed
2436 * after the ->lock is released.
2437 */
2438 spin_lock_irqsave(&drv_tags->lock, flags);
2439 spin_unlock_irqrestore(&drv_tags->lock, flags);
2440}
2441
Jens Axboecc71a6f2017-01-11 14:29:56 -07002442void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2443 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01002444{
John Garryf32e4ea2021-10-05 18:23:32 +08002445 struct blk_mq_tags *drv_tags;
Jens Axboe320ae512013-10-24 09:20:05 +01002446 struct page *page;
2447
John Garry079a2e32021-10-05 18:23:39 +08002448 if (blk_mq_is_shared_tags(set->flags))
2449 drv_tags = set->shared_tags;
John Garrye155b0c2021-10-05 18:23:37 +08002450 else
2451 drv_tags = set->tags[hctx_idx];
John Garryf32e4ea2021-10-05 18:23:32 +08002452
John Garry65de57b2021-10-05 18:23:26 +08002453 if (tags->static_rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002454 int i;
2455
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002456 for (i = 0; i < tags->nr_tags; i++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002457 struct request *rq = tags->static_rqs[i];
2458
2459 if (!rq)
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002460 continue;
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002461 set->ops->exit_request(set, rq, hctx_idx);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002462 tags->static_rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002463 }
2464 }
2465
John Garryf32e4ea2021-10-05 18:23:32 +08002466 blk_mq_clear_rq_mapping(drv_tags, tags);
Ming Leibd631412021-05-11 23:22:35 +08002467
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002468 while (!list_empty(&tags->page_list)) {
2469 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07002470 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002471 /*
2472 * Remove kmemleak object previously allocated in
Raul E Rangel273938b2019-05-02 13:48:11 -06002473 * blk_mq_alloc_rqs().
Catalin Marinasf75782e2015-09-14 18:16:02 +01002474 */
2475 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01002476 __free_pages(page, page->private);
2477 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002478}
Jens Axboe320ae512013-10-24 09:20:05 +01002479
John Garrye155b0c2021-10-05 18:23:37 +08002480void blk_mq_free_rq_map(struct blk_mq_tags *tags)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002481{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002482 kfree(tags->rqs);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002483 tags->rqs = NULL;
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002484 kfree(tags->static_rqs);
2485 tags->static_rqs = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002486
John Garrye155b0c2021-10-05 18:23:37 +08002487 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01002488}
2489
John Garry63064be2021-10-05 18:23:35 +08002490static struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
2491 unsigned int hctx_idx,
2492 unsigned int nr_tags,
John Garrye155b0c2021-10-05 18:23:37 +08002493 unsigned int reserved_tags)
Jens Axboe320ae512013-10-24 09:20:05 +01002494{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002495 struct blk_mq_tags *tags;
Shaohua Li59f082e2017-02-01 09:53:14 -08002496 int node;
Jens Axboe320ae512013-10-24 09:20:05 +01002497
Dongli Zhang7d76f852019-02-27 21:35:01 +08002498 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002499 if (node == NUMA_NO_NODE)
2500 node = set->numa_node;
2501
John Garrye155b0c2021-10-05 18:23:37 +08002502 tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
2503 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002504 if (!tags)
2505 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002506
Kees Cook590b5b72018-06-12 14:04:20 -07002507 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002508 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
Shaohua Li59f082e2017-02-01 09:53:14 -08002509 node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002510 if (!tags->rqs) {
John Garrye155b0c2021-10-05 18:23:37 +08002511 blk_mq_free_tags(tags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002512 return NULL;
2513 }
Jens Axboe320ae512013-10-24 09:20:05 +01002514
Kees Cook590b5b72018-06-12 14:04:20 -07002515 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2516 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2517 node);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002518 if (!tags->static_rqs) {
2519 kfree(tags->rqs);
John Garrye155b0c2021-10-05 18:23:37 +08002520 blk_mq_free_tags(tags);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002521 return NULL;
2522 }
2523
Jens Axboecc71a6f2017-01-11 14:29:56 -07002524 return tags;
2525}
2526
Tejun Heo1d9bd512018-01-09 08:29:48 -08002527static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2528 unsigned int hctx_idx, int node)
2529{
2530 int ret;
2531
2532 if (set->ops->init_request) {
2533 ret = set->ops->init_request(set, rq, hctx_idx, node);
2534 if (ret)
2535 return ret;
2536 }
2537
Keith Busch12f5b932018-05-29 15:52:28 +02002538 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Tejun Heo1d9bd512018-01-09 08:29:48 -08002539 return 0;
2540}
2541
John Garry63064be2021-10-05 18:23:35 +08002542static int blk_mq_alloc_rqs(struct blk_mq_tag_set *set,
2543 struct blk_mq_tags *tags,
2544 unsigned int hctx_idx, unsigned int depth)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002545{
2546 unsigned int i, j, entries_per_page, max_order = 4;
2547 size_t rq_size, left;
Shaohua Li59f082e2017-02-01 09:53:14 -08002548 int node;
2549
Dongli Zhang7d76f852019-02-27 21:35:01 +08002550 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002551 if (node == NUMA_NO_NODE)
2552 node = set->numa_node;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002553
2554 INIT_LIST_HEAD(&tags->page_list);
2555
Jens Axboe320ae512013-10-24 09:20:05 +01002556 /*
2557 * rq_size is the size of the request plus driver payload, rounded
2558 * to the cacheline size
2559 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002560 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01002561 cache_line_size());
Jens Axboecc71a6f2017-01-11 14:29:56 -07002562 left = rq_size * depth;
Jens Axboe320ae512013-10-24 09:20:05 +01002563
Jens Axboecc71a6f2017-01-11 14:29:56 -07002564 for (i = 0; i < depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01002565 int this_order = max_order;
2566 struct page *page;
2567 int to_do;
2568 void *p;
2569
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06002570 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01002571 this_order--;
2572
2573 do {
Shaohua Li59f082e2017-02-01 09:53:14 -08002574 page = alloc_pages_node(node,
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002575 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06002576 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01002577 if (page)
2578 break;
2579 if (!this_order--)
2580 break;
2581 if (order_to_size(this_order) < rq_size)
2582 break;
2583 } while (1);
2584
2585 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002586 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01002587
2588 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002589 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01002590
2591 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002592 /*
2593 * Allow kmemleak to scan these pages as they contain pointers
2594 * to additional allocations like via ops->init_request().
2595 */
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002596 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
Jens Axboe320ae512013-10-24 09:20:05 +01002597 entries_per_page = order_to_size(this_order) / rq_size;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002598 to_do = min(entries_per_page, depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01002599 left -= to_do * rq_size;
2600 for (j = 0; j < to_do; j++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002601 struct request *rq = p;
2602
2603 tags->static_rqs[i] = rq;
Tejun Heo1d9bd512018-01-09 08:29:48 -08002604 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
2605 tags->static_rqs[i] = NULL;
2606 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002607 }
2608
Jens Axboe320ae512013-10-24 09:20:05 +01002609 p += rq_size;
2610 i++;
2611 }
2612 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002613 return 0;
Jens Axboe320ae512013-10-24 09:20:05 +01002614
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002615fail:
Jens Axboecc71a6f2017-01-11 14:29:56 -07002616 blk_mq_free_rqs(set, tags, hctx_idx);
2617 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01002618}
2619
Ming Leibf0beec2020-05-29 15:53:15 +02002620struct rq_iter_data {
2621 struct blk_mq_hw_ctx *hctx;
2622 bool has_rq;
2623};
2624
2625static bool blk_mq_has_request(struct request *rq, void *data, bool reserved)
2626{
2627 struct rq_iter_data *iter_data = data;
2628
2629 if (rq->mq_hctx != iter_data->hctx)
2630 return true;
2631 iter_data->has_rq = true;
2632 return false;
2633}
2634
2635static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
2636{
2637 struct blk_mq_tags *tags = hctx->sched_tags ?
2638 hctx->sched_tags : hctx->tags;
2639 struct rq_iter_data data = {
2640 .hctx = hctx,
2641 };
2642
2643 blk_mq_all_tag_iter(tags, blk_mq_has_request, &data);
2644 return data.has_rq;
2645}
2646
2647static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu,
2648 struct blk_mq_hw_ctx *hctx)
2649{
2650 if (cpumask_next_and(-1, hctx->cpumask, cpu_online_mask) != cpu)
2651 return false;
2652 if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids)
2653 return false;
2654 return true;
2655}
2656
2657static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
2658{
2659 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2660 struct blk_mq_hw_ctx, cpuhp_online);
2661
2662 if (!cpumask_test_cpu(cpu, hctx->cpumask) ||
2663 !blk_mq_last_cpu_in_hctx(cpu, hctx))
2664 return 0;
2665
2666 /*
2667 * Prevent new request from being allocated on the current hctx.
2668 *
2669 * The smp_mb__after_atomic() Pairs with the implied barrier in
2670 * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is
2671 * seen once we return from the tag allocator.
2672 */
2673 set_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2674 smp_mb__after_atomic();
2675
2676 /*
2677 * Try to grab a reference to the queue and wait for any outstanding
2678 * requests. If we could not grab a reference the queue has been
2679 * frozen and there are no requests.
2680 */
2681 if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) {
2682 while (blk_mq_hctx_has_requests(hctx))
2683 msleep(5);
2684 percpu_ref_put(&hctx->queue->q_usage_counter);
2685 }
2686
2687 return 0;
2688}
2689
2690static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node)
2691{
2692 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2693 struct blk_mq_hw_ctx, cpuhp_online);
2694
2695 if (cpumask_test_cpu(cpu, hctx->cpumask))
2696 clear_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2697 return 0;
2698}
2699
Jens Axboee57690f2016-08-24 15:34:35 -06002700/*
2701 * 'cpu' is going away. splice any existing rq_list entries from this
2702 * software queue to the hw queue dispatch list, and ensure that it
2703 * gets run.
2704 */
Thomas Gleixner9467f852016-09-22 08:05:17 -06002705static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
Jens Axboe484b4062014-05-21 14:01:15 -06002706{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002707 struct blk_mq_hw_ctx *hctx;
Jens Axboe484b4062014-05-21 14:01:15 -06002708 struct blk_mq_ctx *ctx;
2709 LIST_HEAD(tmp);
Ming Leic16d6b52018-12-17 08:44:05 -07002710 enum hctx_type type;
Jens Axboe484b4062014-05-21 14:01:15 -06002711
Thomas Gleixner9467f852016-09-22 08:05:17 -06002712 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02002713 if (!cpumask_test_cpu(cpu, hctx->cpumask))
2714 return 0;
2715
Jens Axboee57690f2016-08-24 15:34:35 -06002716 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Ming Leic16d6b52018-12-17 08:44:05 -07002717 type = hctx->type;
Jens Axboe484b4062014-05-21 14:01:15 -06002718
2719 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002720 if (!list_empty(&ctx->rq_lists[type])) {
2721 list_splice_init(&ctx->rq_lists[type], &tmp);
Jens Axboe484b4062014-05-21 14:01:15 -06002722 blk_mq_hctx_clear_pending(hctx, ctx);
2723 }
2724 spin_unlock(&ctx->lock);
2725
2726 if (list_empty(&tmp))
Thomas Gleixner9467f852016-09-22 08:05:17 -06002727 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002728
Jens Axboee57690f2016-08-24 15:34:35 -06002729 spin_lock(&hctx->lock);
2730 list_splice_tail_init(&tmp, &hctx->dispatch);
2731 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06002732
2733 blk_mq_run_hw_queue(hctx, true);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002734 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002735}
2736
Thomas Gleixner9467f852016-09-22 08:05:17 -06002737static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
Jens Axboe484b4062014-05-21 14:01:15 -06002738{
Ming Leibf0beec2020-05-29 15:53:15 +02002739 if (!(hctx->flags & BLK_MQ_F_STACKING))
2740 cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2741 &hctx->cpuhp_online);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002742 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
2743 &hctx->cpuhp_dead);
Jens Axboe484b4062014-05-21 14:01:15 -06002744}
2745
Ming Lei364b6182021-05-11 23:22:36 +08002746/*
2747 * Before freeing hw queue, clearing the flush request reference in
2748 * tags->rqs[] for avoiding potential UAF.
2749 */
2750static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags,
2751 unsigned int queue_depth, struct request *flush_rq)
2752{
2753 int i;
2754 unsigned long flags;
2755
2756 /* The hw queue may not be mapped yet */
2757 if (!tags)
2758 return;
2759
2760 WARN_ON_ONCE(refcount_read(&flush_rq->ref) != 0);
2761
2762 for (i = 0; i < queue_depth; i++)
2763 cmpxchg(&tags->rqs[i], flush_rq, NULL);
2764
2765 /*
2766 * Wait until all pending iteration is done.
2767 *
2768 * Request reference is cleared and it is guaranteed to be observed
2769 * after the ->lock is released.
2770 */
2771 spin_lock_irqsave(&tags->lock, flags);
2772 spin_unlock_irqrestore(&tags->lock, flags);
2773}
2774
Ming Leic3b4afc2015-06-04 22:25:04 +08002775/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08002776static void blk_mq_exit_hctx(struct request_queue *q,
2777 struct blk_mq_tag_set *set,
2778 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
2779{
Ming Lei364b6182021-05-11 23:22:36 +08002780 struct request *flush_rq = hctx->fq->flush_rq;
2781
Ming Lei8ab0b7d2018-01-09 21:28:29 +08002782 if (blk_mq_hw_queue_mapped(hctx))
2783 blk_mq_tag_idle(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002784
Ming Lei364b6182021-05-11 23:22:36 +08002785 blk_mq_clear_flush_rq_mapping(set->tags[hctx_idx],
2786 set->queue_depth, flush_rq);
Ming Leif70ced02014-09-25 23:23:47 +08002787 if (set->ops->exit_request)
Ming Lei364b6182021-05-11 23:22:36 +08002788 set->ops->exit_request(set, flush_rq, hctx_idx);
Ming Leif70ced02014-09-25 23:23:47 +08002789
Ming Lei08e98fc2014-09-25 23:23:38 +08002790 if (set->ops->exit_hctx)
2791 set->ops->exit_hctx(hctx, hctx_idx);
2792
Thomas Gleixner9467f852016-09-22 08:05:17 -06002793 blk_mq_remove_cpuhp(hctx);
Ming Lei2f8f1332019-04-30 09:52:27 +08002794
2795 spin_lock(&q->unused_hctx_lock);
2796 list_add(&hctx->hctx_list, &q->unused_hctx_list);
2797 spin_unlock(&q->unused_hctx_lock);
Ming Lei08e98fc2014-09-25 23:23:38 +08002798}
2799
Ming Lei624dbe42014-05-27 23:35:13 +08002800static void blk_mq_exit_hw_queues(struct request_queue *q,
2801 struct blk_mq_tag_set *set, int nr_queue)
2802{
2803 struct blk_mq_hw_ctx *hctx;
2804 unsigned int i;
2805
2806 queue_for_each_hw_ctx(q, hctx, i) {
2807 if (i == nr_queue)
2808 break;
Jianchao Wang477e19d2018-10-12 18:07:25 +08002809 blk_mq_debugfs_unregister_hctx(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002810 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08002811 }
Ming Lei624dbe42014-05-27 23:35:13 +08002812}
2813
Ming Lei7c6c5b72019-04-30 09:52:26 +08002814static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
2815{
2816 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
2817
2818 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
2819 __alignof__(struct blk_mq_hw_ctx)) !=
2820 sizeof(struct blk_mq_hw_ctx));
2821
2822 if (tag_set->flags & BLK_MQ_F_BLOCKING)
2823 hw_ctx_size += sizeof(struct srcu_struct);
2824
2825 return hw_ctx_size;
2826}
2827
Ming Lei08e98fc2014-09-25 23:23:38 +08002828static int blk_mq_init_hctx(struct request_queue *q,
2829 struct blk_mq_tag_set *set,
2830 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
2831{
Ming Lei7c6c5b72019-04-30 09:52:26 +08002832 hctx->queue_num = hctx_idx;
Ming Lei08e98fc2014-09-25 23:23:38 +08002833
Ming Leibf0beec2020-05-29 15:53:15 +02002834 if (!(hctx->flags & BLK_MQ_F_STACKING))
2835 cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2836 &hctx->cpuhp_online);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002837 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
2838
2839 hctx->tags = set->tags[hctx_idx];
2840
2841 if (set->ops->init_hctx &&
2842 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
2843 goto unregister_cpu_notifier;
2844
2845 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
2846 hctx->numa_node))
2847 goto exit_hctx;
2848 return 0;
2849
2850 exit_hctx:
2851 if (set->ops->exit_hctx)
2852 set->ops->exit_hctx(hctx, hctx_idx);
2853 unregister_cpu_notifier:
2854 blk_mq_remove_cpuhp(hctx);
2855 return -1;
2856}
2857
2858static struct blk_mq_hw_ctx *
2859blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
2860 int node)
2861{
2862 struct blk_mq_hw_ctx *hctx;
2863 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
2864
2865 hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node);
2866 if (!hctx)
2867 goto fail_alloc_hctx;
2868
2869 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
2870 goto free_hctx;
2871
2872 atomic_set(&hctx->nr_active, 0);
Ming Lei08e98fc2014-09-25 23:23:38 +08002873 if (node == NUMA_NO_NODE)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002874 node = set->numa_node;
2875 hctx->numa_node = node;
Ming Lei08e98fc2014-09-25 23:23:38 +08002876
Jens Axboe9f993732017-04-10 09:54:54 -06002877 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08002878 spin_lock_init(&hctx->lock);
2879 INIT_LIST_HEAD(&hctx->dispatch);
2880 hctx->queue = q;
Ming Lei51db1c32020-08-19 23:20:19 +08002881 hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08002882
Ming Lei2f8f1332019-04-30 09:52:27 +08002883 INIT_LIST_HEAD(&hctx->hctx_list);
2884
Ming Lei08e98fc2014-09-25 23:23:38 +08002885 /*
2886 * Allocate space for all possible cpus to avoid allocation at
2887 * runtime
2888 */
Johannes Thumshirnd904bfa2017-11-15 17:32:33 -08002889 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
Ming Lei7c6c5b72019-04-30 09:52:26 +08002890 gfp, node);
Ming Lei08e98fc2014-09-25 23:23:38 +08002891 if (!hctx->ctxs)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002892 goto free_cpumask;
Ming Lei08e98fc2014-09-25 23:23:38 +08002893
Jianchao Wang5b202852018-10-12 18:07:26 +08002894 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
Ming Leic548e622021-01-22 10:33:08 +08002895 gfp, node, false, false))
Ming Lei08e98fc2014-09-25 23:23:38 +08002896 goto free_ctxs;
Ming Lei08e98fc2014-09-25 23:23:38 +08002897 hctx->nr_ctx = 0;
2898
Ming Lei5815839b2018-06-25 19:31:47 +08002899 spin_lock_init(&hctx->dispatch_wait_lock);
Jens Axboeeb619fd2017-11-09 08:32:43 -07002900 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
2901 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
2902
Guoqing Jiang754a1572020-03-09 22:41:37 +01002903 hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp);
Ming Leif70ced02014-09-25 23:23:47 +08002904 if (!hctx->fq)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002905 goto free_bitmap;
Ming Leif70ced02014-09-25 23:23:47 +08002906
Bart Van Assche6a83e742016-11-02 10:09:51 -06002907 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -08002908 init_srcu_struct(hctx->srcu);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002909 blk_mq_hctx_kobj_init(hctx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06002910
Ming Lei7c6c5b72019-04-30 09:52:26 +08002911 return hctx;
Ming Lei08e98fc2014-09-25 23:23:38 +08002912
2913 free_bitmap:
Omar Sandoval88459642016-09-17 08:38:44 -06002914 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08002915 free_ctxs:
2916 kfree(hctx->ctxs);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002917 free_cpumask:
2918 free_cpumask_var(hctx->cpumask);
2919 free_hctx:
2920 kfree(hctx);
2921 fail_alloc_hctx:
2922 return NULL;
Ming Lei08e98fc2014-09-25 23:23:38 +08002923}
2924
Jens Axboe320ae512013-10-24 09:20:05 +01002925static void blk_mq_init_cpu_queues(struct request_queue *q,
2926 unsigned int nr_hw_queues)
2927{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002928 struct blk_mq_tag_set *set = q->tag_set;
2929 unsigned int i, j;
Jens Axboe320ae512013-10-24 09:20:05 +01002930
2931 for_each_possible_cpu(i) {
2932 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
2933 struct blk_mq_hw_ctx *hctx;
Ming Leic16d6b52018-12-17 08:44:05 -07002934 int k;
Jens Axboe320ae512013-10-24 09:20:05 +01002935
Jens Axboe320ae512013-10-24 09:20:05 +01002936 __ctx->cpu = i;
2937 spin_lock_init(&__ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002938 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
2939 INIT_LIST_HEAD(&__ctx->rq_lists[k]);
2940
Jens Axboe320ae512013-10-24 09:20:05 +01002941 __ctx->queue = q;
2942
Jens Axboe320ae512013-10-24 09:20:05 +01002943 /*
2944 * Set local node, IFF we have more than one hw queue. If
2945 * not, we remain on the home node of the device
2946 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06002947 for (j = 0; j < set->nr_maps; j++) {
2948 hctx = blk_mq_map_queue_type(q, j, i);
2949 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
Xianting Tian576e85c2020-10-19 16:20:47 +08002950 hctx->numa_node = cpu_to_node(i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06002951 }
Jens Axboe320ae512013-10-24 09:20:05 +01002952 }
2953}
2954
John Garry63064be2021-10-05 18:23:35 +08002955struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
2956 unsigned int hctx_idx,
2957 unsigned int depth)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002958{
John Garry63064be2021-10-05 18:23:35 +08002959 struct blk_mq_tags *tags;
2960 int ret;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002961
John Garrye155b0c2021-10-05 18:23:37 +08002962 tags = blk_mq_alloc_rq_map(set, hctx_idx, depth, set->reserved_tags);
John Garry63064be2021-10-05 18:23:35 +08002963 if (!tags)
2964 return NULL;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002965
John Garry63064be2021-10-05 18:23:35 +08002966 ret = blk_mq_alloc_rqs(set, tags, hctx_idx, depth);
2967 if (ret) {
John Garrye155b0c2021-10-05 18:23:37 +08002968 blk_mq_free_rq_map(tags);
John Garry63064be2021-10-05 18:23:35 +08002969 return NULL;
2970 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002971
John Garry63064be2021-10-05 18:23:35 +08002972 return tags;
2973}
2974
2975static bool __blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
2976 int hctx_idx)
2977{
John Garry079a2e32021-10-05 18:23:39 +08002978 if (blk_mq_is_shared_tags(set->flags)) {
2979 set->tags[hctx_idx] = set->shared_tags;
John Garrye155b0c2021-10-05 18:23:37 +08002980
2981 return true;
2982 }
2983
John Garry63064be2021-10-05 18:23:35 +08002984 set->tags[hctx_idx] = blk_mq_alloc_map_and_rqs(set, hctx_idx,
2985 set->queue_depth);
2986
2987 return set->tags[hctx_idx];
Jens Axboecc71a6f2017-01-11 14:29:56 -07002988}
2989
John Garry645db342021-10-05 18:23:36 +08002990void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
2991 struct blk_mq_tags *tags,
2992 unsigned int hctx_idx)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002993{
John Garry645db342021-10-05 18:23:36 +08002994 if (tags) {
2995 blk_mq_free_rqs(set, tags, hctx_idx);
John Garrye155b0c2021-10-05 18:23:37 +08002996 blk_mq_free_rq_map(tags);
Jens Axboebd166ef2017-01-17 06:03:22 -07002997 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002998}
2999
John Garrye155b0c2021-10-05 18:23:37 +08003000static void __blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
3001 unsigned int hctx_idx)
3002{
John Garry079a2e32021-10-05 18:23:39 +08003003 if (!blk_mq_is_shared_tags(set->flags))
John Garrye155b0c2021-10-05 18:23:37 +08003004 blk_mq_free_map_and_rqs(set, set->tags[hctx_idx], hctx_idx);
3005
3006 set->tags[hctx_idx] = NULL;
3007}
3008
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003009static void blk_mq_map_swqueue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01003010{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003011 unsigned int i, j, hctx_idx;
Jens Axboe320ae512013-10-24 09:20:05 +01003012 struct blk_mq_hw_ctx *hctx;
3013 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08003014 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01003015
3016 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06003017 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01003018 hctx->nr_ctx = 0;
huhaid416c922018-05-18 08:32:30 -06003019 hctx->dispatch_from = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01003020 }
3021
3022 /*
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003023 * Map software to hardware queues.
Ming Lei4412efe2018-04-25 04:01:44 +08003024 *
3025 * If the cpu isn't present, the cpu is mapped to first hctx.
Jens Axboe320ae512013-10-24 09:20:05 +01003026 */
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08003027 for_each_possible_cpu(i) {
Ming Lei4412efe2018-04-25 04:01:44 +08003028
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01003029 ctx = per_cpu_ptr(q->queue_ctx, i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003030 for (j = 0; j < set->nr_maps; j++) {
Jianchao Wangbb94aea2019-01-24 18:25:33 +08003031 if (!set->map[j].nr_queues) {
3032 ctx->hctxs[j] = blk_mq_map_queue_type(q,
3033 HCTX_TYPE_DEFAULT, i);
Ming Leie5edd5f2018-12-18 01:28:56 +08003034 continue;
Jianchao Wangbb94aea2019-01-24 18:25:33 +08003035 }
Ming Leifd689872020-05-07 21:04:08 +08003036 hctx_idx = set->map[j].mq_map[i];
3037 /* unmapped hw queue can be remapped after CPU topo changed */
3038 if (!set->tags[hctx_idx] &&
John Garry63064be2021-10-05 18:23:35 +08003039 !__blk_mq_alloc_map_and_rqs(set, hctx_idx)) {
Ming Leifd689872020-05-07 21:04:08 +08003040 /*
3041 * If tags initialization fail for some hctx,
3042 * that hctx won't be brought online. In this
3043 * case, remap the current ctx to hctx[0] which
3044 * is guaranteed to always have tags allocated
3045 */
3046 set->map[j].mq_map[i] = 0;
3047 }
Ming Leie5edd5f2018-12-18 01:28:56 +08003048
Jens Axboeb3c661b2018-10-30 10:36:06 -06003049 hctx = blk_mq_map_queue_type(q, j, i);
Jianchao Wang8ccdf4a2019-01-24 18:25:32 +08003050 ctx->hctxs[j] = hctx;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003051 /*
3052 * If the CPU is already set in the mask, then we've
3053 * mapped this one already. This can happen if
3054 * devices share queues across queue maps.
3055 */
3056 if (cpumask_test_cpu(i, hctx->cpumask))
3057 continue;
3058
3059 cpumask_set_cpu(i, hctx->cpumask);
3060 hctx->type = j;
3061 ctx->index_hw[hctx->type] = hctx->nr_ctx;
3062 hctx->ctxs[hctx->nr_ctx++] = ctx;
3063
3064 /*
3065 * If the nr_ctx type overflows, we have exceeded the
3066 * amount of sw queues we can support.
3067 */
3068 BUG_ON(!hctx->nr_ctx);
3069 }
Jianchao Wangbb94aea2019-01-24 18:25:33 +08003070
3071 for (; j < HCTX_MAX_TYPES; j++)
3072 ctx->hctxs[j] = blk_mq_map_queue_type(q,
3073 HCTX_TYPE_DEFAULT, i);
Jens Axboe320ae512013-10-24 09:20:05 +01003074 }
Jens Axboe506e9312014-05-07 10:26:44 -06003075
3076 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei4412efe2018-04-25 04:01:44 +08003077 /*
3078 * If no software queues are mapped to this hardware queue,
3079 * disable it and free the request entries.
3080 */
3081 if (!hctx->nr_ctx) {
3082 /* Never unmap queue 0. We need it as a
3083 * fallback in case of a new remap fails
3084 * allocation
3085 */
John Garrye155b0c2021-10-05 18:23:37 +08003086 if (i)
3087 __blk_mq_free_map_and_rqs(set, i);
Ming Lei4412efe2018-04-25 04:01:44 +08003088
3089 hctx->tags = NULL;
3090 continue;
3091 }
Jens Axboe484b4062014-05-21 14:01:15 -06003092
Ming Lei2a34c082015-04-21 10:00:20 +08003093 hctx->tags = set->tags[i];
3094 WARN_ON(!hctx->tags);
3095
Jens Axboe484b4062014-05-21 14:01:15 -06003096 /*
Chong Yuan889fa312015-04-15 11:39:29 -06003097 * Set the map size to the number of mapped software queues.
3098 * This is more accurate and more efficient than looping
3099 * over all possibly mapped software queues.
3100 */
Omar Sandoval88459642016-09-17 08:38:44 -06003101 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
Chong Yuan889fa312015-04-15 11:39:29 -06003102
3103 /*
Jens Axboe484b4062014-05-21 14:01:15 -06003104 * Initialize batch roundrobin counts
3105 */
Ming Leif82ddf12018-04-08 17:48:10 +08003106 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06003107 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
3108 }
Jens Axboe320ae512013-10-24 09:20:05 +01003109}
3110
Jens Axboe8e8320c2017-06-20 17:56:13 -06003111/*
3112 * Caller needs to ensure that we're either frozen/quiesced, or that
3113 * the queue isn't live yet.
3114 */
Jeff Moyer2404e602015-11-03 10:40:06 -05003115static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06003116{
3117 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06003118 int i;
3119
Jeff Moyer2404e602015-11-03 10:40:06 -05003120 queue_for_each_hw_ctx(q, hctx, i) {
Yu Kuai454bb672021-07-31 14:21:30 +08003121 if (shared) {
Ming Lei51db1c32020-08-19 23:20:19 +08003122 hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
Yu Kuai454bb672021-07-31 14:21:30 +08003123 } else {
3124 blk_mq_tag_idle(hctx);
Ming Lei51db1c32020-08-19 23:20:19 +08003125 hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
Yu Kuai454bb672021-07-31 14:21:30 +08003126 }
Jeff Moyer2404e602015-11-03 10:40:06 -05003127 }
3128}
3129
Hannes Reinecke655ac302020-08-19 23:20:20 +08003130static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set,
3131 bool shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05003132{
3133 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06003134
Bart Van Assche705cda92017-04-07 11:16:49 -07003135 lockdep_assert_held(&set->tag_list_lock);
3136
Jens Axboe0d2602c2014-05-13 15:10:52 -06003137 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3138 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05003139 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003140 blk_mq_unfreeze_queue(q);
3141 }
3142}
3143
3144static void blk_mq_del_queue_tag_set(struct request_queue *q)
3145{
3146 struct blk_mq_tag_set *set = q->tag_set;
3147
Jens Axboe0d2602c2014-05-13 15:10:52 -06003148 mutex_lock(&set->tag_list_lock);
Daniel Wagner08c875c2020-07-28 15:29:51 +02003149 list_del(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05003150 if (list_is_singular(&set->tag_list)) {
3151 /* just transitioned to unshared */
Ming Lei51db1c32020-08-19 23:20:19 +08003152 set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05003153 /* update existing queue */
Hannes Reinecke655ac302020-08-19 23:20:20 +08003154 blk_mq_update_tag_set_shared(set, false);
Jeff Moyer2404e602015-11-03 10:40:06 -05003155 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06003156 mutex_unlock(&set->tag_list_lock);
Roman Pena347c7a2018-06-10 22:38:24 +02003157 INIT_LIST_HEAD(&q->tag_set_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003158}
3159
3160static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
3161 struct request_queue *q)
3162{
Jens Axboe0d2602c2014-05-13 15:10:52 -06003163 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05003164
Jens Axboeff821d22017-11-10 22:05:12 -07003165 /*
3166 * Check to see if we're transitioning to shared (from 1 to 2 queues).
3167 */
3168 if (!list_empty(&set->tag_list) &&
Ming Lei51db1c32020-08-19 23:20:19 +08003169 !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
3170 set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05003171 /* update existing queue */
Hannes Reinecke655ac302020-08-19 23:20:20 +08003172 blk_mq_update_tag_set_shared(set, true);
Jeff Moyer2404e602015-11-03 10:40:06 -05003173 }
Ming Lei51db1c32020-08-19 23:20:19 +08003174 if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
Jeff Moyer2404e602015-11-03 10:40:06 -05003175 queue_set_hctx_shared(q, true);
Daniel Wagner08c875c2020-07-28 15:29:51 +02003176 list_add_tail(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05003177
Jens Axboe0d2602c2014-05-13 15:10:52 -06003178 mutex_unlock(&set->tag_list_lock);
3179}
3180
Ming Lei1db49092018-11-20 09:44:35 +08003181/* All allocations will be freed in release handler of q->mq_kobj */
3182static int blk_mq_alloc_ctxs(struct request_queue *q)
3183{
3184 struct blk_mq_ctxs *ctxs;
3185 int cpu;
3186
3187 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
3188 if (!ctxs)
3189 return -ENOMEM;
3190
3191 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
3192 if (!ctxs->queue_ctx)
3193 goto fail;
3194
3195 for_each_possible_cpu(cpu) {
3196 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
3197 ctx->ctxs = ctxs;
3198 }
3199
3200 q->mq_kobj = &ctxs->kobj;
3201 q->queue_ctx = ctxs->queue_ctx;
3202
3203 return 0;
3204 fail:
3205 kfree(ctxs);
3206 return -ENOMEM;
3207}
3208
Ming Leie09aae72015-01-29 20:17:27 +08003209/*
3210 * It is the actual release handler for mq, but we do it from
3211 * request queue's release handler for avoiding use-after-free
3212 * and headache because q->mq_kobj shouldn't have been introduced,
3213 * but we can't group ctx/kctx kobj without it.
3214 */
3215void blk_mq_release(struct request_queue *q)
3216{
Ming Lei2f8f1332019-04-30 09:52:27 +08003217 struct blk_mq_hw_ctx *hctx, *next;
3218 int i;
Ming Leie09aae72015-01-29 20:17:27 +08003219
Ming Lei2f8f1332019-04-30 09:52:27 +08003220 queue_for_each_hw_ctx(q, hctx, i)
3221 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
3222
3223 /* all hctx are in .unused_hctx_list now */
3224 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
3225 list_del_init(&hctx->hctx_list);
Ming Lei6c8b2322017-02-22 18:14:01 +08003226 kobject_put(&hctx->kobj);
Ming Leic3b4afc2015-06-04 22:25:04 +08003227 }
Ming Leie09aae72015-01-29 20:17:27 +08003228
3229 kfree(q->queue_hw_ctx);
3230
Ming Lei7ea5fe32017-02-22 18:14:00 +08003231 /*
3232 * release .mq_kobj and sw queue's kobject now because
3233 * both share lifetime with request queue.
3234 */
3235 blk_mq_sysfs_deinit(q);
Ming Leie09aae72015-01-29 20:17:27 +08003236}
3237
Christoph Hellwig5ec780a2021-06-24 10:10:12 +02003238static struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set,
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003239 void *queuedata)
Jens Axboe320ae512013-10-24 09:20:05 +01003240{
Christoph Hellwig26a97502021-06-02 09:53:17 +03003241 struct request_queue *q;
3242 int ret;
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003243
Christoph Hellwig26a97502021-06-02 09:53:17 +03003244 q = blk_alloc_queue(set->numa_node);
3245 if (!q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003246 return ERR_PTR(-ENOMEM);
Christoph Hellwig26a97502021-06-02 09:53:17 +03003247 q->queuedata = queuedata;
3248 ret = blk_mq_init_allocated_queue(set, q);
3249 if (ret) {
3250 blk_cleanup_queue(q);
3251 return ERR_PTR(ret);
3252 }
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003253 return q;
3254}
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003255
3256struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
3257{
3258 return blk_mq_init_queue_data(set, NULL);
3259}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003260EXPORT_SYMBOL(blk_mq_init_queue);
3261
Christoph Hellwig4dcc4872021-08-16 15:19:05 +02003262struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set, void *queuedata,
3263 struct lock_class_key *lkclass)
Jens Axboe9316a9e2018-10-15 08:40:37 -06003264{
3265 struct request_queue *q;
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003266 struct gendisk *disk;
Jens Axboe9316a9e2018-10-15 08:40:37 -06003267
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003268 q = blk_mq_init_queue_data(set, queuedata);
3269 if (IS_ERR(q))
3270 return ERR_CAST(q);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003271
Christoph Hellwig4a1fa412021-08-16 15:19:08 +02003272 disk = __alloc_disk_node(q, set->numa_node, lkclass);
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003273 if (!disk) {
3274 blk_cleanup_queue(q);
3275 return ERR_PTR(-ENOMEM);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003276 }
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003277 return disk;
Jens Axboe9316a9e2018-10-15 08:40:37 -06003278}
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003279EXPORT_SYMBOL(__blk_mq_alloc_disk);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003280
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003281static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
3282 struct blk_mq_tag_set *set, struct request_queue *q,
3283 int hctx_idx, int node)
3284{
Ming Lei2f8f1332019-04-30 09:52:27 +08003285 struct blk_mq_hw_ctx *hctx = NULL, *tmp;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003286
Ming Lei2f8f1332019-04-30 09:52:27 +08003287 /* reuse dead hctx first */
3288 spin_lock(&q->unused_hctx_lock);
3289 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
3290 if (tmp->numa_node == node) {
3291 hctx = tmp;
3292 break;
3293 }
3294 }
3295 if (hctx)
3296 list_del_init(&hctx->hctx_list);
3297 spin_unlock(&q->unused_hctx_lock);
3298
3299 if (!hctx)
3300 hctx = blk_mq_alloc_hctx(q, set, node);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003301 if (!hctx)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003302 goto fail;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003303
Ming Lei7c6c5b72019-04-30 09:52:26 +08003304 if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
3305 goto free_hctx;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003306
3307 return hctx;
Ming Lei7c6c5b72019-04-30 09:52:26 +08003308
3309 free_hctx:
3310 kobject_put(&hctx->kobj);
3311 fail:
3312 return NULL;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003313}
3314
Keith Busch868f2f02015-12-17 17:08:14 -07003315static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
3316 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003317{
Jianchao Wange01ad462018-10-12 18:07:28 +08003318 int i, j, end;
Keith Busch868f2f02015-12-17 17:08:14 -07003319 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01003320
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003321 if (q->nr_hw_queues < set->nr_hw_queues) {
3322 struct blk_mq_hw_ctx **new_hctxs;
3323
3324 new_hctxs = kcalloc_node(set->nr_hw_queues,
3325 sizeof(*new_hctxs), GFP_KERNEL,
3326 set->numa_node);
3327 if (!new_hctxs)
3328 return;
3329 if (hctxs)
3330 memcpy(new_hctxs, hctxs, q->nr_hw_queues *
3331 sizeof(*hctxs));
3332 q->queue_hw_ctx = new_hctxs;
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003333 kfree(hctxs);
3334 hctxs = new_hctxs;
3335 }
3336
Ming Leifb350e02018-01-06 16:27:40 +08003337 /* protect against switching io scheduler */
3338 mutex_lock(&q->sysfs_lock);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003339 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003340 int node;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003341 struct blk_mq_hw_ctx *hctx;
Keith Busch868f2f02015-12-17 17:08:14 -07003342
Dongli Zhang7d76f852019-02-27 21:35:01 +08003343 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003344 /*
3345 * If the hw queue has been mapped to another numa node,
3346 * we need to realloc the hctx. If allocation fails, fallback
3347 * to use the previous one.
3348 */
3349 if (hctxs[i] && (hctxs[i]->numa_node == node))
3350 continue;
Jens Axboe320ae512013-10-24 09:20:05 +01003351
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003352 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
3353 if (hctx) {
Ming Lei2f8f1332019-04-30 09:52:27 +08003354 if (hctxs[i])
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003355 blk_mq_exit_hctx(q, set, hctxs[i], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003356 hctxs[i] = hctx;
3357 } else {
3358 if (hctxs[i])
3359 pr_warn("Allocate new hctx on node %d fails,\
3360 fallback to previous one on node %d\n",
3361 node, hctxs[i]->numa_node);
3362 else
3363 break;
Keith Busch868f2f02015-12-17 17:08:14 -07003364 }
Jens Axboe320ae512013-10-24 09:20:05 +01003365 }
Jianchao Wange01ad462018-10-12 18:07:28 +08003366 /*
3367 * Increasing nr_hw_queues fails. Free the newly allocated
3368 * hctxs and keep the previous q->nr_hw_queues.
3369 */
3370 if (i != set->nr_hw_queues) {
3371 j = q->nr_hw_queues;
3372 end = i;
3373 } else {
3374 j = i;
3375 end = q->nr_hw_queues;
3376 q->nr_hw_queues = set->nr_hw_queues;
3377 }
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003378
Jianchao Wange01ad462018-10-12 18:07:28 +08003379 for (; j < end; j++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003380 struct blk_mq_hw_ctx *hctx = hctxs[j];
3381
3382 if (hctx) {
John Garrye155b0c2021-10-05 18:23:37 +08003383 __blk_mq_free_map_and_rqs(set, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003384 blk_mq_exit_hctx(q, set, hctx, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003385 hctxs[j] = NULL;
Keith Busch868f2f02015-12-17 17:08:14 -07003386 }
3387 }
Ming Leifb350e02018-01-06 16:27:40 +08003388 mutex_unlock(&q->sysfs_lock);
Keith Busch868f2f02015-12-17 17:08:14 -07003389}
3390
Christoph Hellwig26a97502021-06-02 09:53:17 +03003391int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
3392 struct request_queue *q)
Keith Busch868f2f02015-12-17 17:08:14 -07003393{
Ming Lei66841672016-02-12 15:27:00 +08003394 /* mark the queue as mq asap */
3395 q->mq_ops = set->ops;
3396
Omar Sandoval34dbad52017-03-21 08:56:08 -07003397 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
Stephen Bates720b8cc2017-04-07 06:24:03 -06003398 blk_mq_poll_stats_bkt,
3399 BLK_MQ_POLL_STATS_BKTS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -07003400 if (!q->poll_cb)
3401 goto err_exit;
3402
Ming Lei1db49092018-11-20 09:44:35 +08003403 if (blk_mq_alloc_ctxs(q))
Jes Sorensen41de54c2019-04-19 16:35:44 -04003404 goto err_poll;
Keith Busch868f2f02015-12-17 17:08:14 -07003405
Ming Lei737f98c2017-02-22 18:13:59 +08003406 /* init q->mq_kobj and sw queues' kobjects */
3407 blk_mq_sysfs_init(q);
3408
Ming Lei2f8f1332019-04-30 09:52:27 +08003409 INIT_LIST_HEAD(&q->unused_hctx_list);
3410 spin_lock_init(&q->unused_hctx_lock);
3411
Keith Busch868f2f02015-12-17 17:08:14 -07003412 blk_mq_realloc_hw_ctxs(set, q);
3413 if (!q->nr_hw_queues)
3414 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01003415
Christoph Hellwig287922e2015-10-30 20:57:30 +08003416 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08003417 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01003418
Jens Axboea8908932018-10-16 14:23:06 -06003419 q->tag_set = set;
Jens Axboe320ae512013-10-24 09:20:05 +01003420
Jens Axboe94eddfb2013-11-19 09:25:07 -07003421 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Ming Leicd191812018-12-18 12:15:29 +08003422 if (set->nr_maps > HCTX_TYPE_POLL &&
3423 set->map[HCTX_TYPE_POLL].nr_queues)
Christoph Hellwig6544d222018-12-02 17:46:28 +01003424 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
Jens Axboe320ae512013-10-24 09:20:05 +01003425
Mike Snitzer28494502016-09-14 13:28:30 -04003426 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06003427 INIT_LIST_HEAD(&q->requeue_list);
3428 spin_lock_init(&q->requeue_lock);
3429
Jens Axboeeba71762014-05-20 15:17:27 -06003430 q->nr_requests = set->queue_depth;
3431
Jens Axboe64f1c212016-11-14 13:03:03 -07003432 /*
3433 * Default to classic polling
3434 */
Yufen Yu29ece8b2019-03-18 22:44:41 +08003435 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
Jens Axboe64f1c212016-11-14 13:03:03 -07003436
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003437 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003438 blk_mq_add_queue_tag_set(set, q);
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003439 blk_mq_map_swqueue(q);
Christoph Hellwig26a97502021-06-02 09:53:17 +03003440 return 0;
Christoph Hellwig18741982014-02-10 09:29:00 -07003441
Jens Axboe320ae512013-10-24 09:20:05 +01003442err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07003443 kfree(q->queue_hw_ctx);
zhengbin73d9c8d2019-07-23 22:10:42 +08003444 q->nr_hw_queues = 0;
Ming Lei1db49092018-11-20 09:44:35 +08003445 blk_mq_sysfs_deinit(q);
Jes Sorensen41de54c2019-04-19 16:35:44 -04003446err_poll:
3447 blk_stat_free_callback(q->poll_cb);
3448 q->poll_cb = NULL;
Ming Linc7de5722016-05-25 23:23:27 -07003449err_exit:
3450 q->mq_ops = NULL;
Christoph Hellwig26a97502021-06-02 09:53:17 +03003451 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01003452}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003453EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01003454
Ming Leic7e2d942019-04-30 09:52:25 +08003455/* tags can _not_ be used after returning from blk_mq_exit_queue */
3456void blk_mq_exit_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01003457{
Bart Van Assche630ef622021-05-13 10:15:29 -07003458 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01003459
Bart Van Assche630ef622021-05-13 10:15:29 -07003460 /* Checks hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED. */
Ming Lei624dbe42014-05-27 23:35:13 +08003461 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
Bart Van Assche630ef622021-05-13 10:15:29 -07003462 /* May clear BLK_MQ_F_TAG_QUEUE_SHARED in hctx->flags. */
3463 blk_mq_del_queue_tag_set(q);
Jens Axboe320ae512013-10-24 09:20:05 +01003464}
Jens Axboe320ae512013-10-24 09:20:05 +01003465
Jens Axboea5164402014-09-10 09:02:03 -06003466static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
3467{
3468 int i;
3469
John Garry079a2e32021-10-05 18:23:39 +08003470 if (blk_mq_is_shared_tags(set->flags)) {
3471 set->shared_tags = blk_mq_alloc_map_and_rqs(set,
John Garrye155b0c2021-10-05 18:23:37 +08003472 BLK_MQ_NO_HCTX_IDX,
3473 set->queue_depth);
John Garry079a2e32021-10-05 18:23:39 +08003474 if (!set->shared_tags)
John Garrye155b0c2021-10-05 18:23:37 +08003475 return -ENOMEM;
3476 }
3477
Xianting Tian8229cca2020-09-26 10:39:47 +08003478 for (i = 0; i < set->nr_hw_queues; i++) {
John Garry63064be2021-10-05 18:23:35 +08003479 if (!__blk_mq_alloc_map_and_rqs(set, i))
Jens Axboea5164402014-09-10 09:02:03 -06003480 goto out_unwind;
Xianting Tian8229cca2020-09-26 10:39:47 +08003481 cond_resched();
3482 }
Jens Axboea5164402014-09-10 09:02:03 -06003483
3484 return 0;
3485
3486out_unwind:
John Garrye155b0c2021-10-05 18:23:37 +08003487 while (--i >= 0)
3488 __blk_mq_free_map_and_rqs(set, i);
3489
John Garry079a2e32021-10-05 18:23:39 +08003490 if (blk_mq_is_shared_tags(set->flags)) {
3491 blk_mq_free_map_and_rqs(set, set->shared_tags,
John Garrye155b0c2021-10-05 18:23:37 +08003492 BLK_MQ_NO_HCTX_IDX);
John Garry645db342021-10-05 18:23:36 +08003493 }
Jens Axboea5164402014-09-10 09:02:03 -06003494
Jens Axboea5164402014-09-10 09:02:03 -06003495 return -ENOMEM;
3496}
3497
3498/*
3499 * Allocate the request maps associated with this tag_set. Note that this
3500 * may reduce the depth asked for, if memory is tight. set->queue_depth
3501 * will be updated to reflect the allocated depth.
3502 */
John Garry63064be2021-10-05 18:23:35 +08003503static int blk_mq_alloc_set_map_and_rqs(struct blk_mq_tag_set *set)
Jens Axboea5164402014-09-10 09:02:03 -06003504{
3505 unsigned int depth;
3506 int err;
3507
3508 depth = set->queue_depth;
3509 do {
3510 err = __blk_mq_alloc_rq_maps(set);
3511 if (!err)
3512 break;
3513
3514 set->queue_depth >>= 1;
3515 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
3516 err = -ENOMEM;
3517 break;
3518 }
3519 } while (set->queue_depth);
3520
3521 if (!set->queue_depth || err) {
3522 pr_err("blk-mq: failed to allocate request map\n");
3523 return -ENOMEM;
3524 }
3525
3526 if (depth != set->queue_depth)
3527 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
3528 depth, set->queue_depth);
3529
3530 return 0;
3531}
3532
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003533static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
3534{
Bart Van Assche6e66b492020-03-09 21:26:17 -07003535 /*
3536 * blk_mq_map_queues() and multiple .map_queues() implementations
3537 * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the
3538 * number of hardware queues.
3539 */
3540 if (set->nr_maps == 1)
3541 set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues;
3542
Ming Lei59388702018-12-07 11:03:53 +08003543 if (set->ops->map_queues && !is_kdump_kernel()) {
Jens Axboeb3c661b2018-10-30 10:36:06 -06003544 int i;
3545
Ming Lei7d4901a2018-01-06 16:27:39 +08003546 /*
3547 * transport .map_queues is usually done in the following
3548 * way:
3549 *
3550 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
3551 * mask = get_cpu_mask(queue)
3552 * for_each_cpu(cpu, mask)
Jens Axboeb3c661b2018-10-30 10:36:06 -06003553 * set->map[x].mq_map[cpu] = queue;
Ming Lei7d4901a2018-01-06 16:27:39 +08003554 * }
3555 *
3556 * When we need to remap, the table has to be cleared for
3557 * killing stale mapping since one CPU may not be mapped
3558 * to any hw queue.
3559 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06003560 for (i = 0; i < set->nr_maps; i++)
3561 blk_mq_clear_mq_map(&set->map[i]);
Ming Lei7d4901a2018-01-06 16:27:39 +08003562
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003563 return set->ops->map_queues(set);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003564 } else {
3565 BUG_ON(set->nr_maps > 1);
Dongli Zhang7d76f852019-02-27 21:35:01 +08003566 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003567 }
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003568}
3569
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003570static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set,
3571 int cur_nr_hw_queues, int new_nr_hw_queues)
3572{
3573 struct blk_mq_tags **new_tags;
3574
3575 if (cur_nr_hw_queues >= new_nr_hw_queues)
3576 return 0;
3577
3578 new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *),
3579 GFP_KERNEL, set->numa_node);
3580 if (!new_tags)
3581 return -ENOMEM;
3582
3583 if (set->tags)
3584 memcpy(new_tags, set->tags, cur_nr_hw_queues *
3585 sizeof(*set->tags));
3586 kfree(set->tags);
3587 set->tags = new_tags;
3588 set->nr_hw_queues = new_nr_hw_queues;
3589
3590 return 0;
3591}
3592
Minwoo Im91cdf262020-12-05 00:20:53 +09003593static int blk_mq_alloc_tag_set_tags(struct blk_mq_tag_set *set,
3594 int new_nr_hw_queues)
3595{
3596 return blk_mq_realloc_tag_set_tags(set, 0, new_nr_hw_queues);
3597}
3598
Jens Axboea4391c62014-06-05 15:21:56 -06003599/*
3600 * Alloc a tag set to be associated with one or more request queues.
3601 * May fail with EINVAL for various error conditions. May adjust the
Minwoo Imc018c842018-06-30 22:12:41 +09003602 * requested depth down, if it's too large. In that case, the set
Jens Axboea4391c62014-06-05 15:21:56 -06003603 * value will be stored in set->queue_depth.
3604 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003605int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
3606{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003607 int i, ret;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003608
Bart Van Assche205fb5f2014-10-30 14:45:11 +01003609 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
3610
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003611 if (!set->nr_hw_queues)
3612 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06003613 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003614 return -EINVAL;
3615 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
3616 return -EINVAL;
3617
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02003618 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003619 return -EINVAL;
3620
Ming Leide148292017-10-14 17:22:29 +08003621 if (!set->ops->get_budget ^ !set->ops->put_budget)
3622 return -EINVAL;
3623
Jens Axboea4391c62014-06-05 15:21:56 -06003624 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
3625 pr_info("blk-mq: reduced tag depth to %u\n",
3626 BLK_MQ_MAX_DEPTH);
3627 set->queue_depth = BLK_MQ_MAX_DEPTH;
3628 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003629
Jens Axboeb3c661b2018-10-30 10:36:06 -06003630 if (!set->nr_maps)
3631 set->nr_maps = 1;
3632 else if (set->nr_maps > HCTX_MAX_TYPES)
3633 return -EINVAL;
3634
Shaohua Li6637fad2014-11-30 16:00:58 -08003635 /*
3636 * If a crashdump is active, then we are potentially in a very
3637 * memory constrained environment. Limit us to 1 queue and
3638 * 64 tags to prevent using too much memory.
3639 */
3640 if (is_kdump_kernel()) {
3641 set->nr_hw_queues = 1;
Ming Lei59388702018-12-07 11:03:53 +08003642 set->nr_maps = 1;
Shaohua Li6637fad2014-11-30 16:00:58 -08003643 set->queue_depth = min(64U, set->queue_depth);
3644 }
Keith Busch868f2f02015-12-17 17:08:14 -07003645 /*
Jens Axboe392546a2018-10-29 13:25:27 -06003646 * There is no use for more h/w queues than cpus if we just have
3647 * a single map
Keith Busch868f2f02015-12-17 17:08:14 -07003648 */
Jens Axboe392546a2018-10-29 13:25:27 -06003649 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003650 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08003651
Minwoo Im91cdf262020-12-05 00:20:53 +09003652 if (blk_mq_alloc_tag_set_tags(set, set->nr_hw_queues) < 0)
Jens Axboea5164402014-09-10 09:02:03 -06003653 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003654
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003655 ret = -ENOMEM;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003656 for (i = 0; i < set->nr_maps; i++) {
3657 set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
Ming Lei07b35eb2018-12-17 18:42:45 +08003658 sizeof(set->map[i].mq_map[0]),
Jens Axboeb3c661b2018-10-30 10:36:06 -06003659 GFP_KERNEL, set->numa_node);
3660 if (!set->map[i].mq_map)
3661 goto out_free_mq_map;
Ming Lei59388702018-12-07 11:03:53 +08003662 set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003663 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003664
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003665 ret = blk_mq_update_queue_map(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003666 if (ret)
3667 goto out_free_mq_map;
3668
John Garry63064be2021-10-05 18:23:35 +08003669 ret = blk_mq_alloc_set_map_and_rqs(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003670 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003671 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003672
Jens Axboe0d2602c2014-05-13 15:10:52 -06003673 mutex_init(&set->tag_list_lock);
3674 INIT_LIST_HEAD(&set->tag_list);
3675
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003676 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003677
3678out_free_mq_map:
Jens Axboeb3c661b2018-10-30 10:36:06 -06003679 for (i = 0; i < set->nr_maps; i++) {
3680 kfree(set->map[i].mq_map);
3681 set->map[i].mq_map = NULL;
3682 }
Robert Elliott5676e7b2014-09-02 11:38:44 -05003683 kfree(set->tags);
3684 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003685 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003686}
3687EXPORT_SYMBOL(blk_mq_alloc_tag_set);
3688
Christoph Hellwigcdb14e02021-06-02 09:53:16 +03003689/* allocate and initialize a tagset for a simple single-queue device */
3690int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set,
3691 const struct blk_mq_ops *ops, unsigned int queue_depth,
3692 unsigned int set_flags)
3693{
3694 memset(set, 0, sizeof(*set));
3695 set->ops = ops;
3696 set->nr_hw_queues = 1;
3697 set->nr_maps = 1;
3698 set->queue_depth = queue_depth;
3699 set->numa_node = NUMA_NO_NODE;
3700 set->flags = set_flags;
3701 return blk_mq_alloc_tag_set(set);
3702}
3703EXPORT_SYMBOL_GPL(blk_mq_alloc_sq_tag_set);
3704
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003705void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
3706{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003707 int i, j;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003708
John Garrye155b0c2021-10-05 18:23:37 +08003709 for (i = 0; i < set->nr_hw_queues; i++)
3710 __blk_mq_free_map_and_rqs(set, i);
Jens Axboe484b4062014-05-21 14:01:15 -06003711
John Garry079a2e32021-10-05 18:23:39 +08003712 if (blk_mq_is_shared_tags(set->flags)) {
3713 blk_mq_free_map_and_rqs(set, set->shared_tags,
John Garrye155b0c2021-10-05 18:23:37 +08003714 BLK_MQ_NO_HCTX_IDX);
3715 }
John Garry32bc15a2020-08-19 23:20:24 +08003716
Jens Axboeb3c661b2018-10-30 10:36:06 -06003717 for (j = 0; j < set->nr_maps; j++) {
3718 kfree(set->map[j].mq_map);
3719 set->map[j].mq_map = NULL;
3720 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003721
Ming Lei981bd182014-04-24 00:07:34 +08003722 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05003723 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003724}
3725EXPORT_SYMBOL(blk_mq_free_tag_set);
3726
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003727int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
3728{
3729 struct blk_mq_tag_set *set = q->tag_set;
3730 struct blk_mq_hw_ctx *hctx;
3731 int i, ret;
3732
Jens Axboebd166ef2017-01-17 06:03:22 -07003733 if (!set)
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003734 return -EINVAL;
3735
Aleksei Zakharove5fa8142019-02-08 19:14:05 +03003736 if (q->nr_requests == nr)
3737 return 0;
3738
Jens Axboe70f36b62017-01-19 10:59:07 -07003739 blk_mq_freeze_queue(q);
Ming Lei24f5a902018-01-06 16:27:38 +08003740 blk_mq_quiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003741
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003742 ret = 0;
3743 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07003744 if (!hctx->tags)
3745 continue;
Jens Axboebd166ef2017-01-17 06:03:22 -07003746 /*
3747 * If we're using an MQ scheduler, just update the scheduler
3748 * queue depth. This is similar to what the old code would do.
3749 */
John Garryf6adcef2021-10-05 18:23:29 +08003750 if (hctx->sched_tags) {
Jens Axboe70f36b62017-01-19 10:59:07 -07003751 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
John Garryf6adcef2021-10-05 18:23:29 +08003752 nr, true);
John Garryf6adcef2021-10-05 18:23:29 +08003753 } else {
3754 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
3755 false);
Jens Axboe70f36b62017-01-19 10:59:07 -07003756 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003757 if (ret)
3758 break;
Jens Axboe77f1e0a2019-01-18 10:34:16 -07003759 if (q->elevator && q->elevator->type->ops.depth_updated)
3760 q->elevator->type->ops.depth_updated(hctx);
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003761 }
John Garryd97e5942021-05-13 20:00:58 +08003762 if (!ret) {
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003763 q->nr_requests = nr;
John Garry079a2e32021-10-05 18:23:39 +08003764 if (blk_mq_is_shared_tags(set->flags)) {
John Garry8fa04462021-10-05 18:23:28 +08003765 if (q->elevator)
John Garry079a2e32021-10-05 18:23:39 +08003766 blk_mq_tag_update_sched_shared_tags(q);
John Garry8fa04462021-10-05 18:23:28 +08003767 else
John Garry079a2e32021-10-05 18:23:39 +08003768 blk_mq_tag_resize_shared_tags(set, nr);
John Garry8fa04462021-10-05 18:23:28 +08003769 }
John Garryd97e5942021-05-13 20:00:58 +08003770 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003771
Ming Lei24f5a902018-01-06 16:27:38 +08003772 blk_mq_unquiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003773 blk_mq_unfreeze_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003774
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003775 return ret;
3776}
3777
Jianchao Wangd48ece22018-08-21 15:15:03 +08003778/*
3779 * request_queue and elevator_type pair.
3780 * It is just used by __blk_mq_update_nr_hw_queues to cache
3781 * the elevator_type associated with a request_queue.
3782 */
3783struct blk_mq_qe_pair {
3784 struct list_head node;
3785 struct request_queue *q;
3786 struct elevator_type *type;
3787};
3788
3789/*
3790 * Cache the elevator_type in qe pair list and switch the
3791 * io scheduler to 'none'
3792 */
3793static bool blk_mq_elv_switch_none(struct list_head *head,
3794 struct request_queue *q)
3795{
3796 struct blk_mq_qe_pair *qe;
3797
3798 if (!q->elevator)
3799 return true;
3800
3801 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
3802 if (!qe)
3803 return false;
3804
3805 INIT_LIST_HEAD(&qe->node);
3806 qe->q = q;
3807 qe->type = q->elevator->type;
3808 list_add(&qe->node, head);
3809
3810 mutex_lock(&q->sysfs_lock);
3811 /*
3812 * After elevator_switch_mq, the previous elevator_queue will be
3813 * released by elevator_release. The reference of the io scheduler
3814 * module get by elevator_get will also be put. So we need to get
3815 * a reference of the io scheduler module here to prevent it to be
3816 * removed.
3817 */
3818 __module_get(qe->type->elevator_owner);
3819 elevator_switch_mq(q, NULL);
3820 mutex_unlock(&q->sysfs_lock);
3821
3822 return true;
3823}
3824
3825static void blk_mq_elv_switch_back(struct list_head *head,
3826 struct request_queue *q)
3827{
3828 struct blk_mq_qe_pair *qe;
3829 struct elevator_type *t = NULL;
3830
3831 list_for_each_entry(qe, head, node)
3832 if (qe->q == q) {
3833 t = qe->type;
3834 break;
3835 }
3836
3837 if (!t)
3838 return;
3839
3840 list_del(&qe->node);
3841 kfree(qe);
3842
3843 mutex_lock(&q->sysfs_lock);
3844 elevator_switch_mq(q, t);
3845 mutex_unlock(&q->sysfs_lock);
3846}
3847
Keith Busche4dc2b32017-05-30 14:39:11 -04003848static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
3849 int nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003850{
3851 struct request_queue *q;
Jianchao Wangd48ece22018-08-21 15:15:03 +08003852 LIST_HEAD(head);
Jianchao Wange01ad462018-10-12 18:07:28 +08003853 int prev_nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07003854
Bart Van Assche705cda92017-04-07 11:16:49 -07003855 lockdep_assert_held(&set->tag_list_lock);
3856
Jens Axboe392546a2018-10-29 13:25:27 -06003857 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003858 nr_hw_queues = nr_cpu_ids;
Weiping Zhangfe35ec52020-06-17 14:18:37 +08003859 if (nr_hw_queues < 1)
3860 return;
3861 if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003862 return;
3863
3864 list_for_each_entry(q, &set->tag_list, tag_set_list)
3865 blk_mq_freeze_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +08003866 /*
3867 * Switch IO scheduler to 'none', cleaning up the data associated
3868 * with the previous scheduler. We will switch back once we are done
3869 * updating the new sw to hw queue mappings.
3870 */
3871 list_for_each_entry(q, &set->tag_list, tag_set_list)
3872 if (!blk_mq_elv_switch_none(&head, q))
3873 goto switch_back;
Keith Busch868f2f02015-12-17 17:08:14 -07003874
Jianchao Wang477e19d2018-10-12 18:07:25 +08003875 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3876 blk_mq_debugfs_unregister_hctxs(q);
3877 blk_mq_sysfs_unregister(q);
3878 }
3879
Weiping Zhanga2584e42020-05-07 21:03:56 +08003880 prev_nr_hw_queues = set->nr_hw_queues;
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003881 if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) <
3882 0)
3883 goto reregister;
3884
Keith Busch868f2f02015-12-17 17:08:14 -07003885 set->nr_hw_queues = nr_hw_queues;
Jianchao Wange01ad462018-10-12 18:07:28 +08003886fallback:
Weiping Zhangaa880ad2020-05-13 08:44:05 +08003887 blk_mq_update_queue_map(set);
Keith Busch868f2f02015-12-17 17:08:14 -07003888 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3889 blk_mq_realloc_hw_ctxs(set, q);
Jianchao Wange01ad462018-10-12 18:07:28 +08003890 if (q->nr_hw_queues != set->nr_hw_queues) {
3891 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
3892 nr_hw_queues, prev_nr_hw_queues);
3893 set->nr_hw_queues = prev_nr_hw_queues;
Dongli Zhang7d76f852019-02-27 21:35:01 +08003894 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jianchao Wange01ad462018-10-12 18:07:28 +08003895 goto fallback;
3896 }
Jianchao Wang477e19d2018-10-12 18:07:25 +08003897 blk_mq_map_swqueue(q);
3898 }
3899
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003900reregister:
Jianchao Wang477e19d2018-10-12 18:07:25 +08003901 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3902 blk_mq_sysfs_register(q);
3903 blk_mq_debugfs_register_hctxs(q);
Keith Busch868f2f02015-12-17 17:08:14 -07003904 }
3905
Jianchao Wangd48ece22018-08-21 15:15:03 +08003906switch_back:
3907 list_for_each_entry(q, &set->tag_list, tag_set_list)
3908 blk_mq_elv_switch_back(&head, q);
3909
Keith Busch868f2f02015-12-17 17:08:14 -07003910 list_for_each_entry(q, &set->tag_list, tag_set_list)
3911 blk_mq_unfreeze_queue(q);
3912}
Keith Busche4dc2b32017-05-30 14:39:11 -04003913
3914void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
3915{
3916 mutex_lock(&set->tag_list_lock);
3917 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
3918 mutex_unlock(&set->tag_list_lock);
3919}
Keith Busch868f2f02015-12-17 17:08:14 -07003920EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
3921
Omar Sandoval34dbad52017-03-21 08:56:08 -07003922/* Enable polling stats and return whether they were already enabled. */
3923static bool blk_poll_stats_enable(struct request_queue *q)
3924{
3925 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
Bart Van Assche7dfdbc72018-03-07 17:10:05 -08003926 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
Omar Sandoval34dbad52017-03-21 08:56:08 -07003927 return true;
3928 blk_stat_add_callback(q, q->poll_cb);
3929 return false;
3930}
3931
3932static void blk_mq_poll_stats_start(struct request_queue *q)
3933{
3934 /*
3935 * We don't arm the callback if polling stats are not enabled or the
3936 * callback is already active.
3937 */
3938 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
3939 blk_stat_is_active(q->poll_cb))
3940 return;
3941
3942 blk_stat_activate_msecs(q->poll_cb, 100);
3943}
3944
3945static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
3946{
3947 struct request_queue *q = cb->data;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003948 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -07003949
Stephen Bates720b8cc2017-04-07 06:24:03 -06003950 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
3951 if (cb->stat[bucket].nr_samples)
3952 q->poll_stat[bucket] = cb->stat[bucket];
3953 }
Omar Sandoval34dbad52017-03-21 08:56:08 -07003954}
3955
Jens Axboe64f1c212016-11-14 13:03:03 -07003956static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
Jens Axboe64f1c212016-11-14 13:03:03 -07003957 struct request *rq)
3958{
Jens Axboe64f1c212016-11-14 13:03:03 -07003959 unsigned long ret = 0;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003960 int bucket;
Jens Axboe64f1c212016-11-14 13:03:03 -07003961
3962 /*
3963 * If stats collection isn't on, don't sleep but turn it on for
3964 * future users
3965 */
Omar Sandoval34dbad52017-03-21 08:56:08 -07003966 if (!blk_poll_stats_enable(q))
Jens Axboe64f1c212016-11-14 13:03:03 -07003967 return 0;
3968
3969 /*
Jens Axboe64f1c212016-11-14 13:03:03 -07003970 * As an optimistic guess, use half of the mean service time
3971 * for this type of request. We can (and should) make this smarter.
3972 * For instance, if the completion latencies are tight, we can
3973 * get closer than just half the mean. This is especially
3974 * important on devices where the completion latencies are longer
Stephen Bates720b8cc2017-04-07 06:24:03 -06003975 * than ~10 usec. We do use the stats for the relevant IO size
3976 * if available which does lead to better estimates.
Jens Axboe64f1c212016-11-14 13:03:03 -07003977 */
Stephen Bates720b8cc2017-04-07 06:24:03 -06003978 bucket = blk_mq_poll_stats_bkt(rq);
3979 if (bucket < 0)
3980 return ret;
3981
3982 if (q->poll_stat[bucket].nr_samples)
3983 ret = (q->poll_stat[bucket].mean + 1) / 2;
Jens Axboe64f1c212016-11-14 13:03:03 -07003984
3985 return ret;
3986}
3987
Christoph Hellwigc6699d62021-10-12 13:12:16 +02003988static bool blk_mq_poll_hybrid(struct request_queue *q, blk_qc_t qc)
Jens Axboe06426ad2016-11-14 13:01:59 -07003989{
Christoph Hellwigc6699d62021-10-12 13:12:16 +02003990 struct blk_mq_hw_ctx *hctx = blk_qc_to_hctx(q, qc);
3991 struct request *rq = blk_qc_to_rq(hctx, qc);
Jens Axboe06426ad2016-11-14 13:01:59 -07003992 struct hrtimer_sleeper hs;
3993 enum hrtimer_mode mode;
Jens Axboe64f1c212016-11-14 13:03:03 -07003994 unsigned int nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003995 ktime_t kt;
3996
Christoph Hellwigc6699d62021-10-12 13:12:16 +02003997 /*
3998 * If a request has completed on queue that uses an I/O scheduler, we
3999 * won't get back a request from blk_qc_to_rq.
4000 */
4001 if (!rq || (rq->rq_flags & RQF_MQ_POLL_SLEPT))
Jens Axboe64f1c212016-11-14 13:03:03 -07004002 return false;
4003
4004 /*
Jens Axboe1052b8a2018-11-26 08:21:49 -07004005 * If we get here, hybrid polling is enabled. Hence poll_nsec can be:
Jens Axboe64f1c212016-11-14 13:03:03 -07004006 *
Jens Axboe64f1c212016-11-14 13:03:03 -07004007 * 0: use half of prev avg
4008 * >0: use this specific value
4009 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07004010 if (q->poll_nsec > 0)
Jens Axboe64f1c212016-11-14 13:03:03 -07004011 nsecs = q->poll_nsec;
4012 else
John Garrycae740a2020-02-26 20:10:15 +08004013 nsecs = blk_mq_poll_nsecs(q, rq);
Jens Axboe64f1c212016-11-14 13:03:03 -07004014
4015 if (!nsecs)
Jens Axboe06426ad2016-11-14 13:01:59 -07004016 return false;
4017
Jens Axboe76a86f92018-01-10 11:30:56 -07004018 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
Jens Axboe06426ad2016-11-14 13:01:59 -07004019
4020 /*
4021 * This will be replaced with the stats tracking code, using
4022 * 'avg_completion_time / 2' as the pre-sleep target.
4023 */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01004024 kt = nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07004025
4026 mode = HRTIMER_MODE_REL;
Sebastian Andrzej Siewiordbc16252019-07-26 20:30:50 +02004027 hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07004028 hrtimer_set_expires(&hs.timer, kt);
4029
Jens Axboe06426ad2016-11-14 13:01:59 -07004030 do {
Tejun Heo5a61c362018-01-09 08:29:52 -08004031 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
Jens Axboe06426ad2016-11-14 13:01:59 -07004032 break;
4033 set_current_state(TASK_UNINTERRUPTIBLE);
Thomas Gleixner9dd88132019-07-30 21:16:55 +02004034 hrtimer_sleeper_start_expires(&hs, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07004035 if (hs.task)
4036 io_schedule();
4037 hrtimer_cancel(&hs.timer);
4038 mode = HRTIMER_MODE_ABS;
4039 } while (hs.task && !signal_pending(current));
4040
4041 __set_current_state(TASK_RUNNING);
4042 destroy_hrtimer_on_stack(&hs.timer);
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004043
4044 /*
4045 * If we sleep, have the caller restart the poll loop to reset the
4046 * state. Like for the other success return cases, the caller is
4047 * responsible for checking if the IO completed. If the IO isn't
4048 * complete, we'll get called again and will go straight to the busy
4049 * poll loop.
4050 */
Jens Axboe06426ad2016-11-14 13:01:59 -07004051 return true;
4052}
4053
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004054static int blk_mq_poll_classic(struct request_queue *q, blk_qc_t cookie,
4055 bool spin)
Jens Axboebbd7bb72016-11-04 09:34:34 -06004056{
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004057 struct blk_mq_hw_ctx *hctx = blk_qc_to_hctx(q, cookie);
4058 long state = get_current_state();
4059 int ret;
Jens Axboe1052b8a2018-11-26 08:21:49 -07004060
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004061 hctx->poll_considered++;
Jens Axboe1052b8a2018-11-26 08:21:49 -07004062
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004063 do {
4064 hctx->poll_invoked++;
Jens Axboe1052b8a2018-11-26 08:21:49 -07004065
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004066 ret = q->mq_ops->poll(hctx);
4067 if (ret > 0) {
4068 hctx->poll_success++;
4069 __set_current_state(TASK_RUNNING);
4070 return ret;
4071 }
4072
4073 if (signal_pending_state(state, current))
4074 __set_current_state(TASK_RUNNING);
4075 if (task_is_running(current))
4076 return 1;
4077
4078 if (ret < 0 || !spin)
4079 break;
4080 cpu_relax();
4081 } while (!need_resched());
4082
4083 __set_current_state(TASK_RUNNING);
4084 return 0;
Jens Axboe1052b8a2018-11-26 08:21:49 -07004085}
4086
Christoph Hellwig529262d2018-12-02 17:46:26 +01004087/**
4088 * blk_poll - poll for IO completions
4089 * @q: the queue
4090 * @cookie: cookie passed back at IO submission time
4091 * @spin: whether to spin for completions
4092 *
4093 * Description:
4094 * Poll for completions on the passed in queue. Returns number of
4095 * completed entries found. If @spin is true, then blk_poll will continue
4096 * looping until at least one completion is found, unless the task is
4097 * otherwise marked running (or we need to reschedule).
4098 */
4099int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin)
Jens Axboe1052b8a2018-11-26 08:21:49 -07004100{
Christoph Hellwig529262d2018-12-02 17:46:26 +01004101 if (!blk_qc_t_valid(cookie) ||
4102 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
Jens Axboe1052b8a2018-11-26 08:21:49 -07004103 return 0;
4104
Christoph Hellwig529262d2018-12-02 17:46:26 +01004105 if (current->plug)
4106 blk_flush_plug_list(current->plug, false);
4107
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004108 /* If specified not to spin, we also should not sleep. */
4109 if (spin && q->poll_nsec != BLK_MQ_POLL_CLASSIC) {
4110 if (blk_mq_poll_hybrid(q, cookie))
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004111 return 1;
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004112 }
4113 return blk_mq_poll_classic(q, cookie, spin);
Jens Axboebbd7bb72016-11-04 09:34:34 -06004114}
Christoph Hellwig529262d2018-12-02 17:46:26 +01004115EXPORT_SYMBOL_GPL(blk_poll);
Jens Axboebbd7bb72016-11-04 09:34:34 -06004116
Jens Axboe9cf2bab2018-10-31 17:01:22 -06004117unsigned int blk_mq_rq_cpu(struct request *rq)
4118{
4119 return rq->mq_ctx->cpu;
4120}
4121EXPORT_SYMBOL(blk_mq_rq_cpu);
4122
Jens Axboe320ae512013-10-24 09:20:05 +01004123static int __init blk_mq_init(void)
4124{
Christoph Hellwigc3077b52020-06-11 08:44:41 +02004125 int i;
4126
4127 for_each_possible_cpu(i)
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +01004128 init_llist_head(&per_cpu(blk_cpu_done, i));
Christoph Hellwigc3077b52020-06-11 08:44:41 +02004129 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
4130
4131 cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
4132 "block/softirq:dead", NULL,
4133 blk_softirq_cpu_dead);
Thomas Gleixner9467f852016-09-22 08:05:17 -06004134 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
4135 blk_mq_hctx_notify_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02004136 cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online",
4137 blk_mq_hctx_notify_online,
4138 blk_mq_hctx_notify_offline);
Jens Axboe320ae512013-10-24 09:20:05 +01004139 return 0;
4140}
4141subsys_initcall(blk_mq_init);