blob: 2aacffa6add396a5c6b9ec61258043a6aea54053 [file] [log] [blame]
Christoph Hellwig3dcf60bc2019-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>
Catalin Marinasf75782e2015-09-14 18:16:02 +010013#include <linux/kmemleak.h>
Jens Axboe320ae512013-10-24 09:20:05 +010014#include <linux/mm.h>
15#include <linux/init.h>
16#include <linux/slab.h>
17#include <linux/workqueue.h>
18#include <linux/smp.h>
19#include <linux/llist.h>
20#include <linux/list_sort.h>
21#include <linux/cpu.h>
22#include <linux/cache.h>
23#include <linux/sched/sysctl.h>
Ingo Molnar105ab3d2017-02-01 16:36:40 +010024#include <linux/sched/topology.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010025#include <linux/sched/signal.h>
Jens Axboe320ae512013-10-24 09:20:05 +010026#include <linux/delay.h>
Jens Axboeaedcd722014-09-17 08:27:03 -060027#include <linux/crash_dump.h>
Jens Axboe88c7b2b2016-08-25 08:07:30 -060028#include <linux/prefetch.h>
Satya Tangiralaa892c8d2020-05-14 00:37:18 +000029#include <linux/blk-crypto.h>
Jens Axboe320ae512013-10-24 09:20:05 +010030
31#include <trace/events/block.h>
32
33#include <linux/blk-mq.h>
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +030034#include <linux/t10-pi.h>
Jens Axboe320ae512013-10-24 09:20:05 +010035#include "blk.h"
36#include "blk-mq.h"
Omar Sandoval9c1051a2017-05-04 08:17:21 -060037#include "blk-mq-debugfs.h"
Jens Axboe320ae512013-10-24 09:20:05 +010038#include "blk-mq-tag.h"
Bart Van Assche986d4132018-09-26 14:01:10 -070039#include "blk-pm.h"
Jens Axboecf43e6b2016-11-07 21:32:37 -070040#include "blk-stat.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070041#include "blk-mq-sched.h"
Josef Bacikc1c80382018-07-03 11:14:59 -040042#include "blk-rq-qos.h"
Jan Karab029b532022-06-23 09:48:32 +020043#include "blk-ioprio.h"
Jens Axboe320ae512013-10-24 09:20:05 +010044
Yang Yang1c6391d2022-06-01 15:15:12 +080045#include <trace/hooks/block.h>
46
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +010047static DEFINE_PER_CPU(struct llist_head, blk_cpu_done);
Christoph Hellwigc3077b52020-06-11 08:44:41 +020048
Omar Sandoval34dbad52017-03-21 08:56:08 -070049static void blk_mq_poll_stats_start(struct request_queue *q);
50static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);
51
Stephen Bates720b8cc2017-04-07 06:24:03 -060052static int blk_mq_poll_stats_bkt(const struct request *rq)
53{
Hou Tao3d244302019-05-21 15:59:03 +080054 int ddir, sectors, bucket;
Stephen Bates720b8cc2017-04-07 06:24:03 -060055
Jens Axboe99c749a2017-04-21 07:55:42 -060056 ddir = rq_data_dir(rq);
Hou Tao3d244302019-05-21 15:59:03 +080057 sectors = blk_rq_stats_sectors(rq);
Stephen Bates720b8cc2017-04-07 06:24:03 -060058
Hou Tao3d244302019-05-21 15:59:03 +080059 bucket = ddir + 2 * ilog2(sectors);
Stephen Bates720b8cc2017-04-07 06:24:03 -060060
61 if (bucket < 0)
62 return -1;
63 else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
64 return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
65
66 return bucket;
67}
68
Jens Axboe320ae512013-10-24 09:20:05 +010069/*
Yufen Yu85fae292019-03-24 17:57:08 +080070 * Check if any of the ctx, dispatch list or elevator
71 * have pending work in this hardware queue.
Jens Axboe320ae512013-10-24 09:20:05 +010072 */
Jens Axboe79f720a2017-11-10 09:13:21 -070073static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
Jens Axboe320ae512013-10-24 09:20:05 +010074{
Jens Axboe79f720a2017-11-10 09:13:21 -070075 return !list_empty_careful(&hctx->dispatch) ||
76 sbitmap_any_bit_set(&hctx->ctx_map) ||
Jens Axboebd166ef2017-01-17 06:03:22 -070077 blk_mq_sched_has_work(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +010078}
79
80/*
81 * Mark this ctx as having pending work in this hardware queue
82 */
83static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
84 struct blk_mq_ctx *ctx)
85{
Jens Axboef31967f2018-10-29 13:13:29 -060086 const int bit = ctx->index_hw[hctx->type];
87
88 if (!sbitmap_test_bit(&hctx->ctx_map, bit))
89 sbitmap_set_bit(&hctx->ctx_map, bit);
Jens Axboe1429d7c2014-05-19 09:23:55 -060090}
91
92static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
93 struct blk_mq_ctx *ctx)
94{
Jens Axboef31967f2018-10-29 13:13:29 -060095 const int bit = ctx->index_hw[hctx->type];
96
97 sbitmap_clear_bit(&hctx->ctx_map, bit);
Jens Axboe320ae512013-10-24 09:20:05 +010098}
99
Jens Axboef299b7c2017-08-08 17:51:45 -0600100struct mq_inflight {
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100101 struct block_device *part;
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300102 unsigned int inflight[2];
Jens Axboef299b7c2017-08-08 17:51:45 -0600103};
104
Jens Axboe7baa8572018-11-08 10:24:07 -0700105static bool blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx,
Jens Axboef299b7c2017-08-08 17:51:45 -0600106 struct request *rq, void *priv,
107 bool reserved)
108{
109 struct mq_inflight *mi = priv;
110
Jeffle Xub0d97552020-12-02 19:11:45 +0800111 if ((!mi->part->bd_partno || rq->part == mi->part) &&
112 blk_mq_rq_state(rq) == MQ_RQ_IN_FLIGHT)
Pavel Begunkovbb4e6b12019-09-30 21:55:33 +0300113 mi->inflight[rq_data_dir(rq)]++;
Jens Axboe7baa8572018-11-08 10:24:07 -0700114
115 return true;
Jens Axboef299b7c2017-08-08 17:51:45 -0600116}
117
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100118unsigned int blk_mq_in_flight(struct request_queue *q,
119 struct block_device *part)
Jens Axboef299b7c2017-08-08 17:51:45 -0600120{
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300121 struct mq_inflight mi = { .part = part };
Jens Axboef299b7c2017-08-08 17:51:45 -0600122
Jens Axboef299b7c2017-08-08 17:51:45 -0600123 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
Mikulas Patockae016b782018-12-06 11:41:21 -0500124
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300125 return mi.inflight[0] + mi.inflight[1];
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700126}
127
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100128void blk_mq_in_flight_rw(struct request_queue *q, struct block_device *part,
129 unsigned int inflight[2])
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700130{
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300131 struct mq_inflight mi = { .part = part };
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700132
Pavel Begunkovbb4e6b12019-09-30 21:55:33 +0300133 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300134 inflight[0] = mi.inflight[0];
135 inflight[1] = mi.inflight[1];
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700136}
137
Ming Lei1671d522017-03-27 20:06:57 +0800138void blk_freeze_queue_start(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +0800139{
Bob Liu7996a8b2019-05-21 11:25:55 +0800140 mutex_lock(&q->mq_freeze_lock);
141 if (++q->mq_freeze_depth == 1) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400142 percpu_ref_kill(&q->q_usage_counter);
Bob Liu7996a8b2019-05-21 11:25:55 +0800143 mutex_unlock(&q->mq_freeze_lock);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700144 if (queue_is_mq(q))
Ming Lei055f6e12017-11-09 10:49:53 -0800145 blk_mq_run_hw_queues(q, false);
Bob Liu7996a8b2019-05-21 11:25:55 +0800146 } else {
147 mutex_unlock(&q->mq_freeze_lock);
Tejun Heocddd5d12014-08-16 08:02:24 -0400148 }
Tejun Heof3af0202014-11-04 13:52:27 -0500149}
Ming Lei1671d522017-03-27 20:06:57 +0800150EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
Tejun Heof3af0202014-11-04 13:52:27 -0500151
Keith Busch6bae363e2017-03-01 14:22:10 -0500152void blk_mq_freeze_queue_wait(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500153{
Dan Williams3ef28e82015-10-21 13:20:12 -0400154 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +0800155}
Keith Busch6bae363e2017-03-01 14:22:10 -0500156EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800157
Keith Buschf91328c2017-03-01 14:22:11 -0500158int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
159 unsigned long timeout)
160{
161 return wait_event_timeout(q->mq_freeze_wq,
162 percpu_ref_is_zero(&q->q_usage_counter),
163 timeout);
164}
165EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
Jens Axboe320ae512013-10-24 09:20:05 +0100166
Tejun Heof3af0202014-11-04 13:52:27 -0500167/*
168 * Guarantee no request is in use, so we can change any data structure of
169 * the queue afterward.
170 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400171void blk_freeze_queue(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500172{
Dan Williams3ef28e82015-10-21 13:20:12 -0400173 /*
174 * In the !blk_mq case we are only calling this to kill the
175 * q_usage_counter, otherwise this increases the freeze depth
176 * and waits for it to return to zero. For this reason there is
177 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
178 * exported to drivers as the only user for unfreeze is blk_mq.
179 */
Ming Lei1671d522017-03-27 20:06:57 +0800180 blk_freeze_queue_start(q);
Tejun Heof3af0202014-11-04 13:52:27 -0500181 blk_mq_freeze_queue_wait(q);
182}
Dan Williams3ef28e82015-10-21 13:20:12 -0400183
184void blk_mq_freeze_queue(struct request_queue *q)
185{
186 /*
187 * ...just an alias to keep freeze and unfreeze actions balanced
188 * in the blk_mq_* namespace
189 */
190 blk_freeze_queue(q);
191}
Jens Axboec761d962015-01-02 15:05:12 -0700192EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
Tejun Heof3af0202014-11-04 13:52:27 -0500193
Christoph Hellwigaec89dc2021-09-29 09:12:41 +0200194void __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic)
Jens Axboe320ae512013-10-24 09:20:05 +0100195{
Bob Liu7996a8b2019-05-21 11:25:55 +0800196 mutex_lock(&q->mq_freeze_lock);
Christoph Hellwigaec89dc2021-09-29 09:12:41 +0200197 if (force_atomic)
198 q->q_usage_counter.data->force_atomic = true;
Bob Liu7996a8b2019-05-21 11:25:55 +0800199 q->mq_freeze_depth--;
200 WARN_ON_ONCE(q->mq_freeze_depth < 0);
201 if (!q->mq_freeze_depth) {
Bart Van Asschebdd63162018-09-26 14:01:08 -0700202 percpu_ref_resurrect(&q->q_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100203 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600204 }
Bob Liu7996a8b2019-05-21 11:25:55 +0800205 mutex_unlock(&q->mq_freeze_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100206}
Christoph Hellwigaec89dc2021-09-29 09:12:41 +0200207
208void blk_mq_unfreeze_queue(struct request_queue *q)
209{
210 __blk_mq_unfreeze_queue(q, false);
211}
Keith Buschb4c6a022014-12-19 17:54:14 -0700212EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
Jens Axboe320ae512013-10-24 09:20:05 +0100213
Bart Van Assche852ec802017-06-21 10:55:47 -0700214/*
215 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
216 * mpt3sas driver such that this function can be removed.
217 */
218void blk_mq_quiesce_queue_nowait(struct request_queue *q)
219{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800220 blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
Bart Van Assche852ec802017-06-21 10:55:47 -0700221}
222EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
223
Bart Van Assche6a83e742016-11-02 10:09:51 -0600224/**
Ming Lei69e07c42017-06-06 23:22:07 +0800225 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
Bart Van Assche6a83e742016-11-02 10:09:51 -0600226 * @q: request queue.
227 *
228 * Note: this function does not prevent that the struct request end_io()
Ming Lei69e07c42017-06-06 23:22:07 +0800229 * callback function is invoked. Once this function is returned, we make
230 * sure no dispatch can happen until the queue is unquiesced via
231 * blk_mq_unquiesce_queue().
Bart Van Assche6a83e742016-11-02 10:09:51 -0600232 */
233void blk_mq_quiesce_queue(struct request_queue *q)
234{
235 struct blk_mq_hw_ctx *hctx;
236 unsigned int i;
237 bool rcu = false;
238
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800239 blk_mq_quiesce_queue_nowait(q);
Ming Leif4560ff2017-06-18 14:24:27 -0600240
Bart Van Assche6a83e742016-11-02 10:09:51 -0600241 queue_for_each_hw_ctx(q, hctx, i) {
242 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -0800243 synchronize_srcu(hctx->srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -0600244 else
245 rcu = true;
246 }
247 if (rcu)
248 synchronize_rcu();
249}
250EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
251
Ming Leie4e73912017-06-06 23:22:03 +0800252/*
253 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
254 * @q: request queue.
255 *
256 * This function recovers queue into the state before quiescing
257 * which is done by blk_mq_quiesce_queue.
258 */
259void blk_mq_unquiesce_queue(struct request_queue *q)
260{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800261 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
Ming Leif4560ff2017-06-18 14:24:27 -0600262
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800263 /* dispatch requests which are inserted during quiescing */
264 blk_mq_run_hw_queues(q, true);
Ming Leie4e73912017-06-06 23:22:03 +0800265}
266EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
267
Jens Axboeaed3ea92014-12-22 14:04:42 -0700268void blk_mq_wake_waiters(struct request_queue *q)
269{
270 struct blk_mq_hw_ctx *hctx;
271 unsigned int i;
272
273 queue_for_each_hw_ctx(q, hctx, i)
274 if (blk_mq_hw_queue_mapped(hctx))
275 blk_mq_tag_wakeup_all(hctx->tags, true);
276}
277
Jens Axboefe1f4522018-11-28 10:50:07 -0700278/*
Hou Tao9a91b052019-05-21 15:59:04 +0800279 * Only need start/end time stamping if we have iostat or
280 * blk stats enabled, or using an IO scheduler.
Jens Axboefe1f4522018-11-28 10:50:07 -0700281 */
282static inline bool blk_mq_need_time_stamp(struct request *rq)
283{
Hou Tao9a91b052019-05-21 15:59:04 +0800284 return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS)) || rq->q->elevator;
Jens Axboefe1f4522018-11-28 10:50:07 -0700285}
286
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200287static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200288 unsigned int tag, u64 alloc_time_ns)
Jens Axboe320ae512013-10-24 09:20:05 +0100289{
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200290 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
291 struct request *rq = tags->static_rqs[tag];
Bart Van Asschec3a148d2017-06-20 11:15:43 -0700292
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200293 if (data->q->elevator) {
Christoph Hellwig766473682020-05-29 15:53:12 +0200294 rq->tag = BLK_MQ_NO_TAG;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200295 rq->internal_tag = tag;
296 } else {
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200297 rq->tag = tag;
Christoph Hellwig766473682020-05-29 15:53:12 +0200298 rq->internal_tag = BLK_MQ_NO_TAG;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200299 }
300
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200301 /* csd/requeue_work/fifo_time is initialized before use */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200302 rq->q = data->q;
303 rq->mq_ctx = data->ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600304 rq->mq_hctx = data->hctx;
Ming Lei568f2702020-07-06 22:41:11 +0800305 rq->rq_flags = 0;
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200306 rq->cmd_flags = data->cmd_flags;
Bart Van Assche0854bcd2020-12-08 21:29:45 -0800307 if (data->flags & BLK_MQ_REQ_PM)
308 rq->rq_flags |= RQF_PM;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200309 if (blk_queue_io_stat(data->q))
Christoph Hellwige8064022016-10-20 15:12:13 +0200310 rq->rq_flags |= RQF_IO_STAT;
Jens Axboe7c3fb702018-01-10 11:46:39 -0700311 INIT_LIST_HEAD(&rq->queuelist);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200312 INIT_HLIST_NODE(&rq->hash);
313 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200314 rq->rq_disk = NULL;
315 rq->part = NULL;
Tejun Heo6f816b42019-08-28 15:05:57 -0700316#ifdef CONFIG_BLK_RQ_ALLOC_TIME
317 rq->alloc_time_ns = alloc_time_ns;
318#endif
Jens Axboefe1f4522018-11-28 10:50:07 -0700319 if (blk_mq_need_time_stamp(rq))
320 rq->start_time_ns = ktime_get_ns();
321 else
322 rq->start_time_ns = 0;
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700323 rq->io_start_time_ns = 0;
Hou Tao3d244302019-05-21 15:59:03 +0800324 rq->stats_sectors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200325 rq->nr_phys_segments = 0;
326#if defined(CONFIG_BLK_DEV_INTEGRITY)
327 rq->nr_integrity_segments = 0;
328#endif
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000329 blk_crypto_rq_set_defaults(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200330 /* tag was already set */
Christoph Hellwig079076b2018-11-14 17:02:05 +0100331 WRITE_ONCE(rq->deadline, 0);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200332
Jens Axboef6be4fb2014-06-06 11:03:48 -0600333 rq->timeout = 0;
334
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200335 rq->end_io = NULL;
336 rq->end_io_data = NULL;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200337
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200338 data->ctx->rq_dispatched[op_is_sync(data->cmd_flags)]++;
Keith Busch12f5b932018-05-29 15:52:28 +0200339 refcount_set(&rq->ref, 1);
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200340
341 if (!op_is_flush(data->cmd_flags)) {
342 struct elevator_queue *e = data->q->elevator;
343
344 rq->elv.icq = NULL;
345 if (e && e->type->ops.prepare_request) {
346 if (e->type->icq_cache)
347 blk_mq_sched_assign_ioc(rq);
348
349 e->type->ops.prepare_request(rq);
350 rq->rq_flags |= RQF_ELVPRIV;
351 }
352 }
353
354 data->hctx->queued++;
Yang Yanga41c70e2022-06-01 15:22:49 -0600355 trace_android_vh_blk_rq_ctx_init(rq, tags, data, alloc_time_ns);
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200356 return rq;
Jens Axboe320ae512013-10-24 09:20:05 +0100357}
358
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200359static struct request *__blk_mq_alloc_request(struct blk_mq_alloc_data *data)
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200360{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200361 struct request_queue *q = data->q;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200362 struct elevator_queue *e = q->elevator;
Tejun Heo6f816b42019-08-28 15:05:57 -0700363 u64 alloc_time_ns = 0;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200364 unsigned int tag;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200365
Tejun Heo6f816b42019-08-28 15:05:57 -0700366 /* alloc_time includes depth and tag waits */
367 if (blk_queue_rq_alloc_time(q))
368 alloc_time_ns = ktime_get_ns();
369
Jens Axboef9afca42018-10-29 13:11:38 -0600370 if (data->cmd_flags & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500371 data->flags |= BLK_MQ_REQ_NOWAIT;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200372
373 if (e) {
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200374 /*
Lin Feng8d663f32021-04-15 11:39:20 +0800375 * Flush/passthrough requests are special and go directly to the
Jens Axboe17a51192018-05-09 13:28:50 -0600376 * dispatch list. Don't include reserved tags in the
377 * limiting, as it isn't useful.
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200378 */
Jens Axboef9afca42018-10-29 13:11:38 -0600379 if (!op_is_flush(data->cmd_flags) &&
Lin Feng8d663f32021-04-15 11:39:20 +0800380 !blk_op_is_passthrough(data->cmd_flags) &&
Jens Axboef9afca42018-10-29 13:11:38 -0600381 e->type->ops.limit_depth &&
Jens Axboe17a51192018-05-09 13:28:50 -0600382 !(data->flags & BLK_MQ_REQ_RESERVED))
Jens Axboef9afca42018-10-29 13:11:38 -0600383 e->type->ops.limit_depth(data->cmd_flags, data);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200384 }
385
Ming Leibf0beec2020-05-29 15:53:15 +0200386retry:
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200387 data->ctx = blk_mq_get_ctx(q);
388 data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx);
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200389 if (!e)
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200390 blk_mq_tag_busy(data->hctx);
391
Ming Leibf0beec2020-05-29 15:53:15 +0200392 /*
393 * Waiting allocations only fail because of an inactive hctx. In that
394 * case just retry the hctx assignment and tag allocation as CPU hotplug
395 * should have migrated us to an online CPU by now.
396 */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200397 tag = blk_mq_get_tag(data);
Ming Leibf0beec2020-05-29 15:53:15 +0200398 if (tag == BLK_MQ_NO_TAG) {
399 if (data->flags & BLK_MQ_REQ_NOWAIT)
400 return NULL;
401
402 /*
403 * Give up the CPU and sleep for a random short time to ensure
404 * that thread using a realtime scheduling class are migrated
Randy Dunlap70f15a42020-07-30 18:42:31 -0700405 * off the CPU, and thus off the hctx that is going away.
Ming Leibf0beec2020-05-29 15:53:15 +0200406 */
407 msleep(3);
408 goto retry;
409 }
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200410 return blk_mq_rq_ctx_init(data, tag, alloc_time_ns);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200411}
412
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700413struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800414 blk_mq_req_flags_t flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100415{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200416 struct blk_mq_alloc_data data = {
417 .q = q,
418 .flags = flags,
419 .cmd_flags = op,
420 };
Jens Axboebd166ef2017-01-17 06:03:22 -0700421 struct request *rq;
Joe Lawrencea492f072014-08-28 08:15:21 -0600422 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100423
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800424 ret = blk_queue_enter(q, flags);
Joe Lawrencea492f072014-08-28 08:15:21 -0600425 if (ret)
426 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100427
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200428 rq = __blk_mq_alloc_request(&data);
Jens Axboebd166ef2017-01-17 06:03:22 -0700429 if (!rq)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200430 goto out_queue_exit;
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200431 rq->__data_len = 0;
432 rq->__sector = (sector_t) -1;
433 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100434 return rq;
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200435out_queue_exit:
436 blk_queue_exit(q);
437 return ERR_PTR(-EWOULDBLOCK);
Jens Axboe320ae512013-10-24 09:20:05 +0100438}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600439EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100440
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700441struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800442 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
Ming Lin1f5bd332016-06-13 16:45:21 +0200443{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200444 struct blk_mq_alloc_data data = {
445 .q = q,
446 .flags = flags,
447 .cmd_flags = op,
448 };
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200449 u64 alloc_time_ns = 0;
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800450 unsigned int cpu;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200451 unsigned int tag;
Ming Lin1f5bd332016-06-13 16:45:21 +0200452 int ret;
453
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200454 /* alloc_time includes depth and tag waits */
455 if (blk_queue_rq_alloc_time(q))
456 alloc_time_ns = ktime_get_ns();
457
Ming Lin1f5bd332016-06-13 16:45:21 +0200458 /*
459 * If the tag allocator sleeps we could get an allocation for a
460 * different hardware context. No need to complicate the low level
461 * allocator for this for the rare use case of a command tied to
462 * a specific queue.
463 */
Kemeng Shi5eb99e72023-01-18 17:37:13 +0800464 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)) ||
465 WARN_ON_ONCE(!(flags & BLK_MQ_REQ_RESERVED)))
Ming Lin1f5bd332016-06-13 16:45:21 +0200466 return ERR_PTR(-EINVAL);
467
468 if (hctx_idx >= q->nr_hw_queues)
469 return ERR_PTR(-EIO);
470
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800471 ret = blk_queue_enter(q, flags);
Ming Lin1f5bd332016-06-13 16:45:21 +0200472 if (ret)
473 return ERR_PTR(ret);
474
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600475 /*
476 * Check if the hardware context is actually mapped to anything.
477 * If not tell the caller that it should skip this queue.
478 */
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200479 ret = -EXDEV;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200480 data.hctx = q->queue_hw_ctx[hctx_idx];
481 if (!blk_mq_hw_queue_mapped(data.hctx))
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200482 goto out_queue_exit;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200483 cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
Bart Van Asscheb5e65ef2022-06-15 14:00:04 -0700484 if (cpu >= nr_cpu_ids)
485 goto out_queue_exit;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200486 data.ctx = __blk_mq_get_ctx(q, cpu);
Ming Lin1f5bd332016-06-13 16:45:21 +0200487
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200488 if (!q->elevator)
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200489 blk_mq_tag_busy(data.hctx);
490
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200491 ret = -EWOULDBLOCK;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200492 tag = blk_mq_get_tag(&data);
493 if (tag == BLK_MQ_NO_TAG)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200494 goto out_queue_exit;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200495 return blk_mq_rq_ctx_init(&data, tag, alloc_time_ns);
496
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200497out_queue_exit:
498 blk_queue_exit(q);
499 return ERR_PTR(ret);
Ming Lin1f5bd332016-06-13 16:45:21 +0200500}
501EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
502
Keith Busch12f5b932018-05-29 15:52:28 +0200503static void __blk_mq_free_request(struct request *rq)
504{
505 struct request_queue *q = rq->q;
506 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600507 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Keith Busch12f5b932018-05-29 15:52:28 +0200508 const int sched_tag = rq->internal_tag;
509
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000510 blk_crypto_free_request(rq);
Bart Van Assche986d4132018-09-26 14:01:10 -0700511 blk_pm_mark_last_busy(rq);
Jens Axboeea4f9952018-10-29 15:06:13 -0600512 rq->mq_hctx = NULL;
Christoph Hellwig766473682020-05-29 15:53:12 +0200513 if (rq->tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800514 blk_mq_put_tag(hctx->tags, ctx, rq->tag);
Christoph Hellwig766473682020-05-29 15:53:12 +0200515 if (sched_tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800516 blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
Keith Busch12f5b932018-05-29 15:52:28 +0200517 blk_mq_sched_restart(hctx);
518 blk_queue_exit(q);
519}
520
Christoph Hellwig6af54052017-06-16 18:15:22 +0200521void blk_mq_free_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100522{
Jens Axboe320ae512013-10-24 09:20:05 +0100523 struct request_queue *q = rq->q;
Christoph Hellwig6af54052017-06-16 18:15:22 +0200524 struct elevator_queue *e = q->elevator;
525 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600526 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100527
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200528 if (rq->rq_flags & RQF_ELVPRIV) {
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600529 if (e && e->type->ops.finish_request)
530 e->type->ops.finish_request(rq);
Christoph Hellwig6af54052017-06-16 18:15:22 +0200531 if (rq->elv.icq) {
532 put_io_context(rq->elv.icq->ioc);
533 rq->elv.icq = NULL;
534 }
535 }
536
537 ctx->rq_completed[rq_is_sync(rq)]++;
Christoph Hellwige8064022016-10-20 15:12:13 +0200538 if (rq->rq_flags & RQF_MQ_INFLIGHT)
John Garrybccf5e22020-08-19 23:20:26 +0800539 __blk_mq_dec_active_requests(hctx);
Jens Axboe87760e52016-11-09 12:38:14 -0700540
Jens Axboe7beb2f82017-09-30 02:08:24 -0600541 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
Christoph Hellwigd152c682021-08-16 15:46:24 +0200542 laptop_io_completion(q->disk->bdi);
Jens Axboe7beb2f82017-09-30 02:08:24 -0600543
Josef Bacika7905042018-07-03 09:32:35 -0600544 rq_qos_done(q, rq);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600545
Keith Busch12f5b932018-05-29 15:52:28 +0200546 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
547 if (refcount_dec_and_test(&rq->ref))
548 __blk_mq_free_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100549}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700550EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100551
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200552inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
Jens Axboe320ae512013-10-24 09:20:05 +0100553{
Jens Axboefe1f4522018-11-28 10:50:07 -0700554 u64 now = 0;
555
556 if (blk_mq_need_time_stamp(rq))
557 now = ktime_get_ns();
Omar Sandoval522a7772018-05-09 02:08:53 -0700558
Omar Sandoval4bc63392018-05-09 02:08:52 -0700559 if (rq->rq_flags & RQF_STATS) {
560 blk_mq_poll_stats_start(rq->q);
Omar Sandoval522a7772018-05-09 02:08:53 -0700561 blk_stat_add(rq, now);
Omar Sandoval4bc63392018-05-09 02:08:52 -0700562 }
563
Baolin Wang87890092020-07-04 15:28:21 +0800564 blk_mq_sched_completed_request(rq, now);
Omar Sandovaled886602018-09-27 15:55:51 -0700565
Omar Sandoval522a7772018-05-09 02:08:53 -0700566 blk_account_io_done(rq, now);
Ming Lei0d11e6ac2013-12-05 10:50:39 -0700567
Christoph Hellwig91b63632014-04-16 09:44:53 +0200568 if (rq->end_io) {
Josef Bacika7905042018-07-03 09:32:35 -0600569 rq_qos_done(rq->q, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100570 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200571 } else {
Jens Axboe320ae512013-10-24 09:20:05 +0100572 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200573 }
Jens Axboe320ae512013-10-24 09:20:05 +0100574}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700575EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200576
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200577void blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200578{
579 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
580 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700581 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200582}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700583EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100584
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100585static void blk_complete_reqs(struct llist_head *list)
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200586{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100587 struct llist_node *entry = llist_reverse_order(llist_del_all(list));
588 struct request *rq, *next;
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200589
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100590 llist_for_each_entry_safe(rq, next, entry, ipi_list)
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200591 rq->q->mq_ops->complete(rq);
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200592}
593
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100594static __latent_entropy void blk_done_softirq(struct softirq_action *h)
Christoph Hellwig115243f2020-06-11 08:44:42 +0200595{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100596 blk_complete_reqs(this_cpu_ptr(&blk_cpu_done));
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200597}
598
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200599static int blk_softirq_cpu_dead(unsigned int cpu)
600{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100601 blk_complete_reqs(&per_cpu(blk_cpu_done, cpu));
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200602 return 0;
603}
604
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800605static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100606{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100607 __raise_softirq_irqoff(BLOCK_SOFTIRQ);
Jens Axboe320ae512013-10-24 09:20:05 +0100608}
609
Christoph Hellwig963395262020-06-11 08:44:49 +0200610static inline bool blk_mq_complete_need_ipi(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100611{
Christoph Hellwig963395262020-06-11 08:44:49 +0200612 int cpu = raw_smp_processor_id();
Jens Axboe320ae512013-10-24 09:20:05 +0100613
Christoph Hellwig963395262020-06-11 08:44:49 +0200614 if (!IS_ENABLED(CONFIG_SMP) ||
615 !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags))
616 return false;
Sebastian Andrzej Siewior71425182020-12-04 20:13:54 +0100617 /*
618 * With force threaded interrupts enabled, raising softirq from an SMP
619 * function call will always result in waking the ksoftirqd thread.
620 * This is probably worse than completing the request on a different
621 * cache domain.
622 */
Tanner Love91cc4702021-06-02 14:03:38 -0400623 if (force_irqthreads())
Sebastian Andrzej Siewior71425182020-12-04 20:13:54 +0100624 return false;
Christoph Hellwig963395262020-06-11 08:44:49 +0200625
626 /* same CPU or cache domain? Complete locally */
627 if (cpu == rq->mq_ctx->cpu ||
628 (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) &&
629 cpus_share_cache(cpu, rq->mq_ctx->cpu)))
630 return false;
631
632 /* don't try to IPI to an offline CPU */
633 return cpu_online(rq->mq_ctx->cpu);
634}
635
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100636static void blk_mq_complete_send_ipi(struct request *rq)
637{
638 struct llist_head *list;
639 unsigned int cpu;
640
641 cpu = rq->mq_ctx->cpu;
642 list = &per_cpu(blk_cpu_done, cpu);
643 if (llist_add(&rq->ipi_list, list)) {
644 INIT_CSD(&rq->csd, __blk_mq_complete_request_remote, rq);
645 smp_call_function_single_async(cpu, &rq->csd);
646 }
647}
648
649static void blk_mq_raise_softirq(struct request *rq)
650{
651 struct llist_head *list;
652
653 preempt_disable();
654 list = this_cpu_ptr(&blk_cpu_done);
655 if (llist_add(&rq->ipi_list, list))
656 raise_softirq(BLOCK_SOFTIRQ);
657 preempt_enable();
658}
659
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200660bool blk_mq_complete_request_remote(struct request *rq)
661{
Keith Buschaf78ff72018-11-26 09:54:30 -0700662 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
Ming Lei36e76532018-09-28 16:42:20 +0800663
Jens Axboe4ab32bf2018-11-18 16:15:35 -0700664 /*
665 * For a polled request, always complete locallly, it's pointless
666 * to redirect the completion.
667 */
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200668 if (rq->cmd_flags & REQ_HIPRI)
669 return false;
Jens Axboe320ae512013-10-24 09:20:05 +0100670
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200671 if (blk_mq_complete_need_ipi(rq)) {
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100672 blk_mq_complete_send_ipi(rq);
673 return true;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800674 }
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200675
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100676 if (rq->q->nr_hw_queues == 1) {
677 blk_mq_raise_softirq(rq);
678 return true;
679 }
680 return false;
Jens Axboe320ae512013-10-24 09:20:05 +0100681}
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200682EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote);
683
Jens Axboe320ae512013-10-24 09:20:05 +0100684/**
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200685 * blk_mq_complete_request - end I/O on a request
686 * @rq: the request being processed
Jens Axboe320ae512013-10-24 09:20:05 +0100687 *
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200688 * Description:
689 * Complete a request by scheduling the ->complete_rq operation.
690 **/
691void blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100692{
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200693 if (!blk_mq_complete_request_remote(rq))
Christoph Hellwig963395262020-06-11 08:44:49 +0200694 rq->q->mq_ops->complete(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100695}
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200696EXPORT_SYMBOL(blk_mq_complete_request);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800697
Jens Axboe04ced1592018-01-09 08:29:46 -0800698static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
Bart Van Asscheb7435db82018-01-10 11:34:27 -0800699 __releases(hctx->srcu)
Jens Axboe04ced1592018-01-09 08:29:46 -0800700{
701 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
702 rcu_read_unlock();
703 else
Tejun Heo05707b62018-01-09 08:29:53 -0800704 srcu_read_unlock(hctx->srcu, srcu_idx);
Jens Axboe04ced1592018-01-09 08:29:46 -0800705}
706
707static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
Bart Van Asscheb7435db82018-01-10 11:34:27 -0800708 __acquires(hctx->srcu)
Jens Axboe04ced1592018-01-09 08:29:46 -0800709{
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700710 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
711 /* shut up gcc false positive */
712 *srcu_idx = 0;
Jens Axboe04ced1592018-01-09 08:29:46 -0800713 rcu_read_lock();
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700714 } else
Tejun Heo05707b62018-01-09 08:29:53 -0800715 *srcu_idx = srcu_read_lock(hctx->srcu);
Jens Axboe04ced1592018-01-09 08:29:46 -0800716}
717
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800718/**
André Almeida105663f2020-01-06 15:08:18 -0300719 * blk_mq_start_request - Start processing a request
720 * @rq: Pointer to request to be started
721 *
722 * Function used by device drivers to notify the block layer that a request
723 * is going to be processed now, so blk layer can do proper initializations
724 * such as starting the timeout timer.
725 */
Christoph Hellwige2490072014-09-13 16:40:09 -0700726void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100727{
728 struct request_queue *q = rq->q;
729
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100730 trace_block_rq_issue(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100731
Jens Axboecf43e6b2016-11-07 21:32:37 -0700732 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700733 rq->io_start_time_ns = ktime_get_ns();
Hou Tao3d244302019-05-21 15:59:03 +0800734 rq->stats_sectors = blk_rq_sectors(rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700735 rq->rq_flags |= RQF_STATS;
Josef Bacika7905042018-07-03 09:32:35 -0600736 rq_qos_issue(q, rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700737 }
738
Tejun Heo1d9bd512018-01-09 08:29:48 -0800739 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
Jens Axboe538b7532014-09-16 10:37:37 -0600740
Tejun Heo1d9bd512018-01-09 08:29:48 -0800741 blk_add_timer(rq);
Keith Busch12f5b932018-05-29 15:52:28 +0200742 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
Christoph Hellwig49f5baa52014-02-11 08:27:14 -0800743
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +0300744#ifdef CONFIG_BLK_DEV_INTEGRITY
745 if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
746 q->integrity.profile->prepare_fn(rq);
747#endif
Jens Axboe320ae512013-10-24 09:20:05 +0100748}
Christoph Hellwige2490072014-09-13 16:40:09 -0700749EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100750
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200751static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100752{
753 struct request_queue *q = rq->q;
754
Ming Lei923218f2017-11-02 23:24:38 +0800755 blk_mq_put_driver_tag(rq);
756
Christoph Hellwiga54895f2020-12-03 17:21:39 +0100757 trace_block_rq_requeue(rq);
Josef Bacika7905042018-07-03 09:32:35 -0600758 rq_qos_requeue(q, rq);
Christoph Hellwig49f5baa52014-02-11 08:27:14 -0800759
Keith Busch12f5b932018-05-29 15:52:28 +0200760 if (blk_mq_request_started(rq)) {
761 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Christoph Hellwigda661262018-06-14 13:58:45 +0200762 rq->rq_flags &= ~RQF_TIMED_OUT;
Christoph Hellwige2490072014-09-13 16:40:09 -0700763 }
Jens Axboe320ae512013-10-24 09:20:05 +0100764}
765
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700766void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200767{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200768 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200769
Ming Lei105976f2018-02-23 23:36:56 +0800770 /* this request will be re-inserted to io scheduler queue */
771 blk_mq_sched_requeue_request(rq);
772
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700773 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200774}
775EXPORT_SYMBOL(blk_mq_requeue_request);
776
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600777static void blk_mq_requeue_work(struct work_struct *work)
778{
779 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -0400780 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600781 LIST_HEAD(rq_list);
782 struct request *rq, *next;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600783
Jens Axboe18e97812017-07-27 08:03:57 -0600784 spin_lock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600785 list_splice_init(&q->requeue_list, &rq_list);
Jens Axboe18e97812017-07-27 08:03:57 -0600786 spin_unlock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600787
788 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
Jianchao Wangaef18972019-02-12 09:56:25 +0800789 if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP)))
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600790 continue;
791
Christoph Hellwige8064022016-10-20 15:12:13 +0200792 rq->rq_flags &= ~RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600793 list_del_init(&rq->queuelist);
Jianchao Wangaef18972019-02-12 09:56:25 +0800794 /*
795 * If RQF_DONTPREP, rq has contained some driver specific
796 * data, so insert it to hctx dispatch list to avoid any
797 * merge.
798 */
799 if (rq->rq_flags & RQF_DONTPREP)
Ming Lei01e99ae2020-02-25 09:04:32 +0800800 blk_mq_request_bypass_insert(rq, false, false);
Jianchao Wangaef18972019-02-12 09:56:25 +0800801 else
802 blk_mq_sched_insert_request(rq, true, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600803 }
804
805 while (!list_empty(&rq_list)) {
806 rq = list_entry(rq_list.next, struct request, queuelist);
807 list_del_init(&rq->queuelist);
Mike Snitzer9e97d292018-01-17 11:25:58 -0500808 blk_mq_sched_insert_request(rq, false, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600809 }
810
Bart Van Assche52d7f1b2016-10-28 17:20:32 -0700811 blk_mq_run_hw_queues(q, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600812}
813
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700814void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
815 bool kick_requeue_list)
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600816{
817 struct request_queue *q = rq->q;
818 unsigned long flags;
819
820 /*
821 * We abuse this flag that is otherwise used by the I/O scheduler to
Jens Axboeff821d22017-11-10 22:05:12 -0700822 * request head insertion from the workqueue.
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600823 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200824 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600825
826 spin_lock_irqsave(&q->requeue_lock, flags);
827 if (at_head) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200828 rq->rq_flags |= RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600829 list_add(&rq->queuelist, &q->requeue_list);
830 } else {
831 list_add_tail(&rq->queuelist, &q->requeue_list);
832 }
833 spin_unlock_irqrestore(&q->requeue_lock, flags);
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700834
835 if (kick_requeue_list)
836 blk_mq_kick_requeue_list(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600837}
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600838
839void blk_mq_kick_requeue_list(struct request_queue *q)
840{
Bart Van Asscheae943d22018-01-19 08:58:55 -0800841 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600842}
843EXPORT_SYMBOL(blk_mq_kick_requeue_list);
844
Mike Snitzer28494502016-09-14 13:28:30 -0400845void blk_mq_delay_kick_requeue_list(struct request_queue *q,
846 unsigned long msecs)
847{
Bart Van Assched4acf362017-08-09 11:28:06 -0700848 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
849 msecs_to_jiffies(msecs));
Mike Snitzer28494502016-09-14 13:28:30 -0400850}
851EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
852
Jens Axboe0e62f512014-06-04 10:23:49 -0600853struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
854{
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600855 if (tag < tags->nr_tags) {
856 prefetch(tags->rqs[tag]);
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700857 return tags->rqs[tag];
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600858 }
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700859
860 return NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600861}
862EXPORT_SYMBOL(blk_mq_tag_to_rq);
863
Jens Axboe3c94d83c2018-12-17 21:11:17 -0700864static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq,
865 void *priv, bool reserved)
Jens Axboeae879912018-11-08 09:03:51 -0700866{
867 /*
Ming Lei05a4fed2020-07-07 11:04:33 -0400868 * If we find a request that isn't idle and the queue matches,
Jens Axboe3c94d83c2018-12-17 21:11:17 -0700869 * we know the queue is busy. Return false to stop the iteration.
Jens Axboeae879912018-11-08 09:03:51 -0700870 */
Ming Lei05a4fed2020-07-07 11:04:33 -0400871 if (blk_mq_request_started(rq) && rq->q == hctx->queue) {
Jens Axboeae879912018-11-08 09:03:51 -0700872 bool *busy = priv;
873
874 *busy = true;
875 return false;
876 }
877
878 return true;
879}
880
Jens Axboe3c94d83c2018-12-17 21:11:17 -0700881bool blk_mq_queue_inflight(struct request_queue *q)
Jens Axboeae879912018-11-08 09:03:51 -0700882{
883 bool busy = false;
884
Jens Axboe3c94d83c2018-12-17 21:11:17 -0700885 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
Jens Axboeae879912018-11-08 09:03:51 -0700886 return busy;
887}
Jens Axboe3c94d83c2018-12-17 21:11:17 -0700888EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
Jens Axboeae879912018-11-08 09:03:51 -0700889
Tejun Heo358f70d2018-01-09 08:29:50 -0800890static void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100891{
Christoph Hellwigda661262018-06-14 13:58:45 +0200892 req->rq_flags |= RQF_TIMED_OUT;
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200893 if (req->q->mq_ops->timeout) {
894 enum blk_eh_timer_return ret;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600895
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200896 ret = req->q->mq_ops->timeout(req, reserved);
897 if (ret == BLK_EH_DONE)
898 return;
899 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700900 }
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200901
902 blk_add_timer(req);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600903}
Keith Busch5b3f25f2015-01-07 18:55:46 -0700904
Keith Busch12f5b932018-05-29 15:52:28 +0200905static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
906{
907 unsigned long deadline;
908
909 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
910 return false;
Christoph Hellwigda661262018-06-14 13:58:45 +0200911 if (rq->rq_flags & RQF_TIMED_OUT)
912 return false;
Keith Busch12f5b932018-05-29 15:52:28 +0200913
Christoph Hellwig079076b2018-11-14 17:02:05 +0100914 deadline = READ_ONCE(rq->deadline);
Keith Busch12f5b932018-05-29 15:52:28 +0200915 if (time_after_eq(jiffies, deadline))
916 return true;
917
918 if (*next == 0)
919 *next = deadline;
920 else if (time_after(*next, deadline))
921 *next = deadline;
922 return false;
923}
924
Ming Lei2e315dc2021-05-11 23:22:34 +0800925void blk_mq_put_rq_ref(struct request *rq)
926{
Ming Leia9ed27a2021-08-18 09:09:25 +0800927 if (is_flush_rq(rq))
Ming Lei2e315dc2021-05-11 23:22:34 +0800928 rq->end_io(rq, 0);
929 else if (refcount_dec_and_test(&rq->ref))
930 __blk_mq_free_request(rq);
931}
932
Jens Axboe7baa8572018-11-08 10:24:07 -0700933static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700934 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100935{
Keith Busch12f5b932018-05-29 15:52:28 +0200936 unsigned long *next = priv;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700937
Keith Busch12f5b932018-05-29 15:52:28 +0200938 /*
Ming Leic797b402021-08-11 23:52:02 +0800939 * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot
940 * be reallocated underneath the timeout handler's processing, then
941 * the expire check is reliable. If the request is not expired, then
942 * it was completed and reallocated as a new request after returning
943 * from blk_mq_check_expired().
Keith Busch12f5b932018-05-29 15:52:28 +0200944 */
945 if (blk_mq_req_expired(rq, next))
Tejun Heo1d9bd512018-01-09 08:29:48 -0800946 blk_mq_rq_timed_out(rq, reserved);
Jens Axboe7baa8572018-11-08 10:24:07 -0700947 return true;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800948}
949
Christoph Hellwig287922e2015-10-30 20:57:30 +0800950static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100951{
Christoph Hellwig287922e2015-10-30 20:57:30 +0800952 struct request_queue *q =
953 container_of(work, struct request_queue, timeout_work);
Keith Busch12f5b932018-05-29 15:52:28 +0200954 unsigned long next = 0;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800955 struct blk_mq_hw_ctx *hctx;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700956 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100957
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600958 /* A deadlock might occur if a request is stuck requiring a
959 * timeout at the same time a queue freeze is waiting
960 * completion, since the timeout code would not be able to
961 * acquire the queue reference here.
962 *
963 * That's why we don't use blk_queue_enter here; instead, we use
964 * percpu_ref_tryget directly, because we need to be able to
965 * obtain a reference even in the short window between the queue
966 * starting to freeze, by dropping the first reference in
Ming Lei1671d522017-03-27 20:06:57 +0800967 * blk_freeze_queue_start, and the moment the last request is
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600968 * consumed, marked by the instant q_usage_counter reaches
969 * zero.
970 */
971 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +0800972 return;
973
Keith Busch12f5b932018-05-29 15:52:28 +0200974 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
Jens Axboe320ae512013-10-24 09:20:05 +0100975
Keith Busch12f5b932018-05-29 15:52:28 +0200976 if (next != 0) {
977 mod_timer(&q->timeout, next);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600978 } else {
Bart Van Asschefcd36c32018-01-10 08:33:33 -0800979 /*
980 * Request timeouts are handled as a forward rolling timer. If
981 * we end up here it means that no requests are pending and
982 * also that no request has been pending for a while. Mark
983 * each hctx as idle.
984 */
Ming Leif054b562015-04-21 10:00:19 +0800985 queue_for_each_hw_ctx(q, hctx, i) {
986 /* the hctx may be unmapped, so check it here */
987 if (blk_mq_hw_queue_mapped(hctx))
988 blk_mq_tag_idle(hctx);
989 }
Jens Axboe0d2602c2014-05-13 15:10:52 -0600990 }
Christoph Hellwig287922e2015-10-30 20:57:30 +0800991 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100992}
993
Omar Sandoval88459642016-09-17 08:38:44 -0600994struct flush_busy_ctx_data {
995 struct blk_mq_hw_ctx *hctx;
996 struct list_head *list;
997};
998
999static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
1000{
1001 struct flush_busy_ctx_data *flush_data = data;
1002 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
1003 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001004 enum hctx_type type = hctx->type;
Omar Sandoval88459642016-09-17 08:38:44 -06001005
Omar Sandoval88459642016-09-17 08:38:44 -06001006 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001007 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
Omar Sandovale9a99a62018-02-27 16:56:42 -08001008 sbitmap_clear_bit(sb, bitnr);
Omar Sandoval88459642016-09-17 08:38:44 -06001009 spin_unlock(&ctx->lock);
1010 return true;
1011}
1012
Jens Axboe320ae512013-10-24 09:20:05 +01001013/*
Jens Axboe1429d7c2014-05-19 09:23:55 -06001014 * Process software queues that have been marked busy, splicing them
1015 * to the for-dispatch
1016 */
Jens Axboe2c3ad662016-12-14 14:34:47 -07001017void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
Jens Axboe1429d7c2014-05-19 09:23:55 -06001018{
Omar Sandoval88459642016-09-17 08:38:44 -06001019 struct flush_busy_ctx_data data = {
1020 .hctx = hctx,
1021 .list = list,
1022 };
Jens Axboe1429d7c2014-05-19 09:23:55 -06001023
Omar Sandoval88459642016-09-17 08:38:44 -06001024 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001025}
Jens Axboe2c3ad662016-12-14 14:34:47 -07001026EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001027
Ming Leib3476892017-10-14 17:22:30 +08001028struct dispatch_rq_data {
1029 struct blk_mq_hw_ctx *hctx;
1030 struct request *rq;
1031};
1032
1033static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
1034 void *data)
1035{
1036 struct dispatch_rq_data *dispatch_data = data;
1037 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
1038 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001039 enum hctx_type type = hctx->type;
Ming Leib3476892017-10-14 17:22:30 +08001040
1041 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001042 if (!list_empty(&ctx->rq_lists[type])) {
1043 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
Ming Leib3476892017-10-14 17:22:30 +08001044 list_del_init(&dispatch_data->rq->queuelist);
Ming Leic16d6b52018-12-17 08:44:05 -07001045 if (list_empty(&ctx->rq_lists[type]))
Ming Leib3476892017-10-14 17:22:30 +08001046 sbitmap_clear_bit(sb, bitnr);
1047 }
1048 spin_unlock(&ctx->lock);
1049
1050 return !dispatch_data->rq;
1051}
1052
1053struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
1054 struct blk_mq_ctx *start)
1055{
Jens Axboef31967f2018-10-29 13:13:29 -06001056 unsigned off = start ? start->index_hw[hctx->type] : 0;
Ming Leib3476892017-10-14 17:22:30 +08001057 struct dispatch_rq_data data = {
1058 .hctx = hctx,
1059 .rq = NULL,
1060 };
1061
1062 __sbitmap_for_each_set(&hctx->ctx_map, off,
1063 dispatch_rq_from_ctx, &data);
1064
1065 return data.rq;
1066}
1067
Jens Axboe703fd1c2016-09-16 13:59:14 -06001068static inline unsigned int queued_to_index(unsigned int queued)
1069{
1070 if (!queued)
1071 return 0;
Jens Axboe1429d7c2014-05-19 09:23:55 -06001072
Jens Axboe703fd1c2016-09-16 13:59:14 -06001073 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001074}
1075
Ming Lei570e9b72020-06-30 22:03:55 +08001076static bool __blk_mq_get_driver_tag(struct request *rq)
1077{
John Garry222a5ae02020-08-19 23:20:23 +08001078 struct sbitmap_queue *bt = rq->mq_hctx->tags->bitmap_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001079 unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001080 int tag;
1081
Ming Lei568f2702020-07-06 22:41:11 +08001082 blk_mq_tag_busy(rq->mq_hctx);
1083
Ming Lei570e9b72020-06-30 22:03:55 +08001084 if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) {
John Garry222a5ae02020-08-19 23:20:23 +08001085 bt = rq->mq_hctx->tags->breserved_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001086 tag_offset = 0;
Ming Lei285008502020-09-11 18:41:14 +08001087 } else {
1088 if (!hctx_may_queue(rq->mq_hctx, bt))
1089 return false;
Ming Lei570e9b72020-06-30 22:03:55 +08001090 }
1091
Ming Lei570e9b72020-06-30 22:03:55 +08001092 tag = __sbitmap_queue_get(bt);
1093 if (tag == BLK_MQ_NO_TAG)
1094 return false;
1095
1096 rq->tag = tag + tag_offset;
Ming Lei570e9b72020-06-30 22:03:55 +08001097 return true;
1098}
1099
Jan Kara613471542021-06-03 12:47:21 +02001100bool blk_mq_get_driver_tag(struct request *rq)
Ming Lei570e9b72020-06-30 22:03:55 +08001101{
Ming Lei568f2702020-07-06 22:41:11 +08001102 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1103
1104 if (rq->tag == BLK_MQ_NO_TAG && !__blk_mq_get_driver_tag(rq))
1105 return false;
1106
Ming Lei51db1c32020-08-19 23:20:19 +08001107 if ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) &&
Ming Lei568f2702020-07-06 22:41:11 +08001108 !(rq->rq_flags & RQF_MQ_INFLIGHT)) {
1109 rq->rq_flags |= RQF_MQ_INFLIGHT;
John Garrybccf5e22020-08-19 23:20:26 +08001110 __blk_mq_inc_active_requests(hctx);
Ming Lei568f2702020-07-06 22:41:11 +08001111 }
1112 hctx->tags->rqs[rq->tag] = rq;
1113 return true;
Ming Lei570e9b72020-06-30 22:03:55 +08001114}
1115
Jens Axboeeb619fd2017-11-09 08:32:43 -07001116static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1117 int flags, void *key)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001118{
1119 struct blk_mq_hw_ctx *hctx;
1120
1121 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1122
Ming Lei5815839b2018-06-25 19:31:47 +08001123 spin_lock(&hctx->dispatch_wait_lock);
Jens Axboee8618572019-03-25 12:34:10 -06001124 if (!list_empty(&wait->entry)) {
1125 struct sbitmap_queue *sbq;
1126
1127 list_del_init(&wait->entry);
John Garry222a5ae02020-08-19 23:20:23 +08001128 sbq = hctx->tags->bitmap_tags;
Jens Axboee8618572019-03-25 12:34:10 -06001129 atomic_dec(&sbq->ws_active);
1130 }
Ming Lei5815839b2018-06-25 19:31:47 +08001131 spin_unlock(&hctx->dispatch_wait_lock);
1132
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001133 blk_mq_run_hw_queue(hctx, true);
1134 return 1;
1135}
1136
Jens Axboef906a6a2017-11-09 16:10:13 -07001137/*
1138 * Mark us waiting for a tag. For shared tags, this involves hooking us into
Bart Van Asscheee3e4de2018-01-09 10:09:15 -08001139 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1140 * restart. For both cases, take care to check the condition again after
Jens Axboef906a6a2017-11-09 16:10:13 -07001141 * marking us as waiting.
1142 */
Ming Lei2278d692018-06-25 19:31:46 +08001143static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
Jens Axboef906a6a2017-11-09 16:10:13 -07001144 struct request *rq)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001145{
John Garry222a5ae02020-08-19 23:20:23 +08001146 struct sbitmap_queue *sbq = hctx->tags->bitmap_tags;
Ming Lei5815839b2018-06-25 19:31:47 +08001147 struct wait_queue_head *wq;
Jens Axboef906a6a2017-11-09 16:10:13 -07001148 wait_queue_entry_t *wait;
1149 bool ret;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001150
Ming Lei51db1c32020-08-19 23:20:19 +08001151 if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
Yufen Yu684b7322019-03-15 11:05:10 +08001152 blk_mq_sched_mark_restart_hctx(hctx);
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001153
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001154 /*
1155 * It's possible that a tag was freed in the window between the
1156 * allocation failure and adding the hardware queue to the wait
1157 * queue.
1158 *
1159 * Don't clear RESTART here, someone else could have set it.
1160 * At most this will cost an extra queue run.
1161 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001162 return blk_mq_get_driver_tag(rq);
Jens Axboeeb619fd2017-11-09 08:32:43 -07001163 }
1164
Ming Lei2278d692018-06-25 19:31:46 +08001165 wait = &hctx->dispatch_wait;
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001166 if (!list_empty_careful(&wait->entry))
1167 return false;
1168
Jens Axboee8618572019-03-25 12:34:10 -06001169 wq = &bt_wait_ptr(sbq, hctx)->wait;
Ming Lei5815839b2018-06-25 19:31:47 +08001170
1171 spin_lock_irq(&wq->lock);
1172 spin_lock(&hctx->dispatch_wait_lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001173 if (!list_empty(&wait->entry)) {
Ming Lei5815839b2018-06-25 19:31:47 +08001174 spin_unlock(&hctx->dispatch_wait_lock);
1175 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001176 return false;
1177 }
1178
Jens Axboee8618572019-03-25 12:34:10 -06001179 atomic_inc(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001180 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1181 __add_wait_queue(wq, wait);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001182
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001183 /*
Jens Axboeeb619fd2017-11-09 08:32:43 -07001184 * It's possible that a tag was freed in the window between the
1185 * allocation failure and adding the hardware queue to the wait
1186 * queue.
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001187 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001188 ret = blk_mq_get_driver_tag(rq);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001189 if (!ret) {
Ming Lei5815839b2018-06-25 19:31:47 +08001190 spin_unlock(&hctx->dispatch_wait_lock);
1191 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001192 return false;
Jens Axboef906a6a2017-11-09 16:10:13 -07001193 }
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001194
1195 /*
1196 * We got a tag, remove ourselves from the wait queue to ensure
1197 * someone else gets the wakeup.
1198 */
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001199 list_del_init(&wait->entry);
Jens Axboee8618572019-03-25 12:34:10 -06001200 atomic_dec(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001201 spin_unlock(&hctx->dispatch_wait_lock);
1202 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001203
1204 return true;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001205}
1206
Ming Lei6e7687172018-07-03 09:03:16 -06001207#define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1208#define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1209/*
1210 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1211 * - EWMA is one simple way to compute running average value
1212 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1213 * - take 4 as factor for avoiding to get too small(0) result, and this
1214 * factor doesn't matter because EWMA decreases exponentially
1215 */
1216static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1217{
1218 unsigned int ewma;
1219
Ming Lei6e7687172018-07-03 09:03:16 -06001220 ewma = hctx->dispatch_busy;
1221
1222 if (!ewma && !busy)
1223 return;
1224
1225 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1226 if (busy)
1227 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1228 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1229
1230 hctx->dispatch_busy = ewma;
1231}
1232
Ming Lei86ff7c22018-01-30 22:04:57 -05001233#define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1234
Johannes Thumshirnc92a4102020-03-25 00:24:44 +09001235static void blk_mq_handle_dev_resource(struct request *rq,
1236 struct list_head *list)
1237{
1238 struct request *next =
1239 list_first_entry_or_null(list, struct request, queuelist);
1240
1241 /*
1242 * If an I/O scheduler has been configured and we got a driver tag for
1243 * the next request already, free it.
1244 */
1245 if (next)
1246 blk_mq_put_driver_tag(next);
1247
1248 list_add(&rq->queuelist, list);
1249 __blk_mq_requeue_request(rq);
1250}
1251
Keith Busch0512a752020-05-12 17:55:47 +09001252static void blk_mq_handle_zone_resource(struct request *rq,
1253 struct list_head *zone_list)
1254{
1255 /*
1256 * If we end up here it is because we cannot dispatch a request to a
1257 * specific zone due to LLD level zone-write locking or other zone
1258 * related resource not being available. In this case, set the request
1259 * aside in zone_list for retrying it later.
1260 */
1261 list_add(&rq->queuelist, zone_list);
1262 __blk_mq_requeue_request(rq);
1263}
1264
Ming Lei75383522020-06-30 18:24:58 +08001265enum prep_dispatch {
1266 PREP_DISPATCH_OK,
1267 PREP_DISPATCH_NO_TAG,
1268 PREP_DISPATCH_NO_BUDGET,
1269};
1270
1271static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq,
1272 bool need_budget)
1273{
1274 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Ming Lei2a5a24a2021-01-22 10:33:12 +08001275 int budget_token = -1;
Ming Lei75383522020-06-30 18:24:58 +08001276
Ming Lei2a5a24a2021-01-22 10:33:12 +08001277 if (need_budget) {
1278 budget_token = blk_mq_get_dispatch_budget(rq->q);
1279 if (budget_token < 0) {
1280 blk_mq_put_driver_tag(rq);
1281 return PREP_DISPATCH_NO_BUDGET;
1282 }
1283 blk_mq_set_rq_budget_token(rq, budget_token);
Ming Lei75383522020-06-30 18:24:58 +08001284 }
1285
1286 if (!blk_mq_get_driver_tag(rq)) {
1287 /*
1288 * The initial allocation attempt failed, so we need to
1289 * rerun the hardware queue when a tag is freed. The
1290 * waitqueue takes care of that. If the queue is run
1291 * before we add this entry back on the dispatch list,
1292 * we'll re-run it below.
1293 */
1294 if (!blk_mq_mark_tag_wait(hctx, rq)) {
Ming Lei1fd40b52020-06-30 18:25:00 +08001295 /*
1296 * All budgets not got from this function will be put
1297 * together during handling partial dispatch
1298 */
1299 if (need_budget)
Ming Lei2a5a24a2021-01-22 10:33:12 +08001300 blk_mq_put_dispatch_budget(rq->q, budget_token);
Ming Lei75383522020-06-30 18:24:58 +08001301 return PREP_DISPATCH_NO_TAG;
1302 }
1303 }
1304
1305 return PREP_DISPATCH_OK;
1306}
1307
Ming Lei1fd40b52020-06-30 18:25:00 +08001308/* release all allocated budgets before calling to blk_mq_dispatch_rq_list */
1309static void blk_mq_release_budgets(struct request_queue *q,
Ming Lei2a5a24a2021-01-22 10:33:12 +08001310 struct list_head *list)
Ming Lei1fd40b52020-06-30 18:25:00 +08001311{
Ming Lei2a5a24a2021-01-22 10:33:12 +08001312 struct request *rq;
Ming Lei1fd40b52020-06-30 18:25:00 +08001313
Ming Lei2a5a24a2021-01-22 10:33:12 +08001314 list_for_each_entry(rq, list, queuelist) {
1315 int budget_token = blk_mq_get_rq_budget_token(rq);
1316
1317 if (budget_token >= 0)
1318 blk_mq_put_dispatch_budget(q, budget_token);
1319 }
Ming Lei1fd40b52020-06-30 18:25:00 +08001320}
1321
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001322/*
1323 * Returns true if we did some work AND can potentially do more.
1324 */
Ming Lei445874e2020-06-30 18:24:57 +08001325bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list,
Ming Lei1fd40b52020-06-30 18:25:00 +08001326 unsigned int nr_budgets)
Jens Axboef04c3df2016-12-07 08:41:17 -07001327{
Ming Lei75383522020-06-30 18:24:58 +08001328 enum prep_dispatch prep;
Ming Lei445874e2020-06-30 18:24:57 +08001329 struct request_queue *q = hctx->queue;
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001330 struct request *rq, *nxt;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001331 int errors, queued;
Ming Lei86ff7c22018-01-30 22:04:57 -05001332 blk_status_t ret = BLK_STS_OK;
Keith Busch0512a752020-05-12 17:55:47 +09001333 LIST_HEAD(zone_list);
Naohiro Aota9586e672021-10-27 01:51:27 +09001334 bool needs_resource = false;
Jens Axboef04c3df2016-12-07 08:41:17 -07001335
Omar Sandoval81380ca2017-04-07 08:56:26 -06001336 if (list_empty(list))
1337 return false;
1338
Jens Axboef04c3df2016-12-07 08:41:17 -07001339 /*
Jens Axboef04c3df2016-12-07 08:41:17 -07001340 * Now process all the entries, sending them to the driver.
1341 */
Jens Axboe93efe982017-03-24 12:04:19 -06001342 errors = queued = 0;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001343 do {
Jens Axboef04c3df2016-12-07 08:41:17 -07001344 struct blk_mq_queue_data bd;
1345
1346 rq = list_first_entry(list, struct request, queuelist);
Ming Lei0bca7992018-04-05 00:35:21 +08001347
Ming Lei445874e2020-06-30 18:24:57 +08001348 WARN_ON_ONCE(hctx != rq->mq_hctx);
Ming Lei1fd40b52020-06-30 18:25:00 +08001349 prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets);
Ming Lei75383522020-06-30 18:24:58 +08001350 if (prep != PREP_DISPATCH_OK)
Ming Lei0bca7992018-04-05 00:35:21 +08001351 break;
Ming Leide148292017-10-14 17:22:29 +08001352
Jens Axboef04c3df2016-12-07 08:41:17 -07001353 list_del_init(&rq->queuelist);
1354
1355 bd.rq = rq;
Jens Axboe113285b2017-03-02 13:26:04 -07001356
1357 /*
1358 * Flag last if we have no more requests, or if we have more
1359 * but can't assign a driver tag to it.
1360 */
1361 if (list_empty(list))
1362 bd.last = true;
1363 else {
Jens Axboe113285b2017-03-02 13:26:04 -07001364 nxt = list_first_entry(list, struct request, queuelist);
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001365 bd.last = !blk_mq_get_driver_tag(nxt);
Jens Axboe113285b2017-03-02 13:26:04 -07001366 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001367
Ming Lei1fd40b52020-06-30 18:25:00 +08001368 /*
1369 * once the request is queued to lld, no need to cover the
1370 * budget any more
1371 */
1372 if (nr_budgets)
1373 nr_budgets--;
Jens Axboef04c3df2016-12-07 08:41:17 -07001374 ret = q->mq_ops->queue_rq(hctx, &bd);
Ming Lei7bf13722020-07-01 21:58:57 +08001375 switch (ret) {
1376 case BLK_STS_OK:
1377 queued++;
Jens Axboef04c3df2016-12-07 08:41:17 -07001378 break;
Ming Lei7bf13722020-07-01 21:58:57 +08001379 case BLK_STS_RESOURCE:
Naohiro Aota9586e672021-10-27 01:51:27 +09001380 needs_resource = true;
1381 fallthrough;
Ming Lei7bf13722020-07-01 21:58:57 +08001382 case BLK_STS_DEV_RESOURCE:
1383 blk_mq_handle_dev_resource(rq, list);
1384 goto out;
1385 case BLK_STS_ZONE_RESOURCE:
Keith Busch0512a752020-05-12 17:55:47 +09001386 /*
1387 * Move the request to zone_list and keep going through
1388 * the dispatch list to find more requests the drive can
1389 * accept.
1390 */
1391 blk_mq_handle_zone_resource(rq, &zone_list);
Naohiro Aota9586e672021-10-27 01:51:27 +09001392 needs_resource = true;
Ming Lei7bf13722020-07-01 21:58:57 +08001393 break;
1394 default:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001395 errors++;
Hannes Reineckee21ee5a2020-09-30 10:02:53 +02001396 blk_mq_end_request(rq, ret);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001397 }
Omar Sandoval81380ca2017-04-07 08:56:26 -06001398 } while (!list_empty(list));
Ming Lei7bf13722020-07-01 21:58:57 +08001399out:
Keith Busch0512a752020-05-12 17:55:47 +09001400 if (!list_empty(&zone_list))
1401 list_splice_tail_init(&zone_list, list);
1402
Jens Axboef04c3df2016-12-07 08:41:17 -07001403 hctx->dispatched[queued_to_index(queued)]++;
1404
yangerkun632bfb62020-09-05 19:25:56 +08001405 /* If we didn't flush the entire list, we could have told the driver
1406 * there was more coming, but that turned out to be a lie.
1407 */
Yu Kuai77864ed2022-07-26 20:22:24 +08001408 if ((!list_empty(list) || errors || needs_resource ||
1409 ret == BLK_STS_DEV_RESOURCE) && q->mq_ops->commit_rqs && queued)
yangerkun632bfb62020-09-05 19:25:56 +08001410 q->mq_ops->commit_rqs(hctx);
Jens Axboef04c3df2016-12-07 08:41:17 -07001411 /*
1412 * Any items that need requeuing? Stuff them into hctx->dispatch,
1413 * that is where we will continue on next queue run.
1414 */
1415 if (!list_empty(list)) {
Ming Lei86ff7c22018-01-30 22:04:57 -05001416 bool needs_restart;
Ming Lei75383522020-06-30 18:24:58 +08001417 /* For non-shared tags, the RESTART check will suffice */
1418 bool no_tag = prep == PREP_DISPATCH_NO_TAG &&
Ming Lei51db1c32020-08-19 23:20:19 +08001419 (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED);
Ming Lei86ff7c22018-01-30 22:04:57 -05001420
Ming Lei2a5a24a2021-01-22 10:33:12 +08001421 if (nr_budgets)
1422 blk_mq_release_budgets(q, list);
Jens Axboef04c3df2016-12-07 08:41:17 -07001423
1424 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001425 list_splice_tail_init(list, &hctx->dispatch);
Jens Axboef04c3df2016-12-07 08:41:17 -07001426 spin_unlock(&hctx->lock);
1427
1428 /*
Ming Leid7d85352020-08-17 18:01:15 +08001429 * Order adding requests to hctx->dispatch and checking
1430 * SCHED_RESTART flag. The pair of this smp_mb() is the one
1431 * in blk_mq_sched_restart(). Avoid restart code path to
1432 * miss the new added requests to hctx->dispatch, meantime
1433 * SCHED_RESTART is observed here.
1434 */
1435 smp_mb();
1436
1437 /*
Bart Van Assche710c7852017-04-07 11:16:51 -07001438 * If SCHED_RESTART was set by the caller of this function and
1439 * it is no longer set that means that it was cleared by another
1440 * thread and hence that a queue rerun is needed.
Jens Axboef04c3df2016-12-07 08:41:17 -07001441 *
Jens Axboeeb619fd2017-11-09 08:32:43 -07001442 * If 'no_tag' is set, that means that we failed getting
1443 * a driver tag with an I/O scheduler attached. If our dispatch
1444 * waitqueue is no longer active, ensure that we run the queue
1445 * AFTER adding our entries back to the list.
Jens Axboebd166ef2017-01-17 06:03:22 -07001446 *
Bart Van Assche710c7852017-04-07 11:16:51 -07001447 * If no I/O scheduler has been configured it is possible that
1448 * the hardware queue got stopped and restarted before requests
1449 * were pushed back onto the dispatch list. Rerun the queue to
1450 * avoid starvation. Notes:
1451 * - blk_mq_run_hw_queue() checks whether or not a queue has
1452 * been stopped before rerunning a queue.
1453 * - Some but not all block drivers stop a queue before
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001454 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
Bart Van Assche710c7852017-04-07 11:16:51 -07001455 * and dm-rq.
Ming Lei86ff7c22018-01-30 22:04:57 -05001456 *
1457 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1458 * bit is set, run queue after a delay to avoid IO stalls
Douglas Andersonab3cee32020-04-20 09:24:51 -07001459 * that could otherwise occur if the queue is idle. We'll do
Naohiro Aota9586e672021-10-27 01:51:27 +09001460 * similar if we couldn't get budget or couldn't lock a zone
1461 * and SCHED_RESTART is set.
Jens Axboebd166ef2017-01-17 06:03:22 -07001462 */
Ming Lei86ff7c22018-01-30 22:04:57 -05001463 needs_restart = blk_mq_sched_needs_restart(hctx);
Naohiro Aota9586e672021-10-27 01:51:27 +09001464 if (prep == PREP_DISPATCH_NO_BUDGET)
1465 needs_resource = true;
Ming Lei86ff7c22018-01-30 22:04:57 -05001466 if (!needs_restart ||
Jens Axboeeb619fd2017-11-09 08:32:43 -07001467 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
Jens Axboebd166ef2017-01-17 06:03:22 -07001468 blk_mq_run_hw_queue(hctx, true);
Naohiro Aota9586e672021-10-27 01:51:27 +09001469 else if (needs_restart && needs_resource)
Ming Lei86ff7c22018-01-30 22:04:57 -05001470 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001471
Ming Lei6e7687172018-07-03 09:03:16 -06001472 blk_mq_update_dispatch_busy(hctx, true);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001473 return false;
Ming Lei6e7687172018-07-03 09:03:16 -06001474 } else
1475 blk_mq_update_dispatch_busy(hctx, false);
Jens Axboef04c3df2016-12-07 08:41:17 -07001476
Jens Axboe93efe982017-03-24 12:04:19 -06001477 return (queued + errors) != 0;
Jens Axboef04c3df2016-12-07 08:41:17 -07001478}
1479
André Almeida105663f2020-01-06 15:08:18 -03001480/**
1481 * __blk_mq_run_hw_queue - Run a hardware queue.
1482 * @hctx: Pointer to the hardware queue to run.
1483 *
1484 * Send pending requests to the hardware.
1485 */
Bart Van Assche6a83e742016-11-02 10:09:51 -06001486static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1487{
1488 int srcu_idx;
1489
Jens Axboeb7a71e62017-08-01 09:28:24 -06001490 /*
Jens Axboeb7a71e62017-08-01 09:28:24 -06001491 * We can't run the queue inline with ints disabled. Ensure that
1492 * we catch bad users of this early.
1493 */
1494 WARN_ON_ONCE(in_interrupt());
1495
Jens Axboe04ced1592018-01-09 08:29:46 -08001496 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
Jens Axboebf4907c2017-03-30 12:30:39 -06001497
Jens Axboe04ced1592018-01-09 08:29:46 -08001498 hctx_lock(hctx, &srcu_idx);
1499 blk_mq_sched_dispatch_requests(hctx);
1500 hctx_unlock(hctx, srcu_idx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001501}
1502
Ming Leif82ddf12018-04-08 17:48:10 +08001503static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1504{
1505 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1506
1507 if (cpu >= nr_cpu_ids)
1508 cpu = cpumask_first(hctx->cpumask);
1509 return cpu;
1510}
1511
Jens Axboe506e9312014-05-07 10:26:44 -06001512/*
1513 * It'd be great if the workqueue API had a way to pass
1514 * in a mask and had some smarts for more clever placement.
1515 * For now we just round-robin here, switching for every
1516 * BLK_MQ_CPU_WORK_BATCH queued items.
1517 */
1518static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1519{
Ming Lei7bed45952018-01-18 00:41:51 +08001520 bool tried = false;
Ming Lei476f8c92018-04-08 17:48:09 +08001521 int next_cpu = hctx->next_cpu;
Ming Lei7bed45952018-01-18 00:41:51 +08001522
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001523 if (hctx->queue->nr_hw_queues == 1)
1524 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -06001525
1526 if (--hctx->next_cpu_batch <= 0) {
Ming Lei7bed45952018-01-18 00:41:51 +08001527select_cpu:
Ming Lei476f8c92018-04-08 17:48:09 +08001528 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08001529 cpu_online_mask);
Jens Axboe506e9312014-05-07 10:26:44 -06001530 if (next_cpu >= nr_cpu_ids)
Ming Leif82ddf12018-04-08 17:48:10 +08001531 next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06001532 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1533 }
1534
Ming Lei7bed45952018-01-18 00:41:51 +08001535 /*
1536 * Do unbound schedule if we can't find a online CPU for this hctx,
1537 * and it should only happen in the path of handling CPU DEAD.
1538 */
Ming Lei476f8c92018-04-08 17:48:09 +08001539 if (!cpu_online(next_cpu)) {
Ming Lei7bed45952018-01-18 00:41:51 +08001540 if (!tried) {
1541 tried = true;
1542 goto select_cpu;
1543 }
1544
1545 /*
1546 * Make sure to re-select CPU next time once after CPUs
1547 * in hctx->cpumask become online again.
1548 */
Ming Lei476f8c92018-04-08 17:48:09 +08001549 hctx->next_cpu = next_cpu;
Ming Lei7bed45952018-01-18 00:41:51 +08001550 hctx->next_cpu_batch = 1;
1551 return WORK_CPU_UNBOUND;
1552 }
Ming Lei476f8c92018-04-08 17:48:09 +08001553
1554 hctx->next_cpu = next_cpu;
1555 return next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -06001556}
1557
André Almeida105663f2020-01-06 15:08:18 -03001558/**
1559 * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue.
1560 * @hctx: Pointer to the hardware queue to run.
1561 * @async: If we want to run the queue asynchronously.
Minwoo Imfa94ba82020-12-05 00:20:55 +09001562 * @msecs: Milliseconds of delay to wait before running the queue.
André Almeida105663f2020-01-06 15:08:18 -03001563 *
1564 * If !@async, try to run the queue now. Else, run the queue asynchronously and
1565 * with a delay of @msecs.
1566 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001567static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1568 unsigned long msecs)
Jens Axboe320ae512013-10-24 09:20:05 +01001569{
Bart Van Assche5435c022017-06-20 11:15:49 -07001570 if (unlikely(blk_mq_hctx_stopped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +01001571 return;
1572
Jens Axboe1b792f22016-09-21 10:12:13 -06001573 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001574 int cpu = get_cpu();
1575 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +01001576 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001577 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +01001578 return;
1579 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001580
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001581 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -06001582 }
Paolo Bonzini398205b2014-11-07 23:03:59 +01001583
Bart Van Asscheae943d22018-01-19 08:58:55 -08001584 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1585 msecs_to_jiffies(msecs));
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001586}
1587
André Almeida105663f2020-01-06 15:08:18 -03001588/**
1589 * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
1590 * @hctx: Pointer to the hardware queue to run.
Minwoo Imfa94ba82020-12-05 00:20:55 +09001591 * @msecs: Milliseconds of delay to wait before running the queue.
André Almeida105663f2020-01-06 15:08:18 -03001592 *
1593 * Run a hardware queue asynchronously with a delay of @msecs.
1594 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001595void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1596{
1597 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1598}
1599EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1600
André Almeida105663f2020-01-06 15:08:18 -03001601/**
1602 * blk_mq_run_hw_queue - Start to run a hardware queue.
1603 * @hctx: Pointer to the hardware queue to run.
1604 * @async: If we want to run the queue asynchronously.
1605 *
1606 * Check if the request queue is not in a quiesced state and if there are
1607 * pending requests to be sent. If this is true, run the queue to send requests
1608 * to hardware.
1609 */
John Garry626fb732019-10-30 00:59:30 +08001610void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001611{
Ming Lei24f5a902018-01-06 16:27:38 +08001612 int srcu_idx;
1613 bool need_run;
1614
1615 /*
1616 * When queue is quiesced, we may be switching io scheduler, or
1617 * updating nr_hw_queues, or other things, and we can't run queue
1618 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
1619 *
1620 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
1621 * quiesced.
1622 */
Jens Axboe04ced1592018-01-09 08:29:46 -08001623 hctx_lock(hctx, &srcu_idx);
1624 need_run = !blk_queue_quiesced(hctx->queue) &&
1625 blk_mq_hctx_has_pending(hctx);
1626 hctx_unlock(hctx, srcu_idx);
Ming Lei24f5a902018-01-06 16:27:38 +08001627
John Garry626fb732019-10-30 00:59:30 +08001628 if (need_run)
Jens Axboe79f720a2017-11-10 09:13:21 -07001629 __blk_mq_delay_run_hw_queue(hctx, async, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01001630}
Omar Sandoval5b727272017-04-14 01:00:00 -07001631EXPORT_SYMBOL(blk_mq_run_hw_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01001632
Jan Karab6e68ee2021-01-11 17:47:17 +01001633/*
1634 * Is the request queue handled by an IO scheduler that does not respect
1635 * hardware queues when dispatching?
1636 */
1637static bool blk_mq_has_sqsched(struct request_queue *q)
1638{
1639 struct elevator_queue *e = q->elevator;
1640
1641 if (e && e->type->ops.dispatch_request &&
1642 !(e->type->elevator_features & ELEVATOR_F_MQ_AWARE))
1643 return true;
1644 return false;
1645}
1646
1647/*
1648 * Return prefered queue to dispatch from (if any) for non-mq aware IO
1649 * scheduler.
1650 */
1651static struct blk_mq_hw_ctx *blk_mq_get_sq_hctx(struct request_queue *q)
1652{
Ming Lei460aa282022-05-22 20:23:50 +08001653 struct blk_mq_ctx *ctx = blk_mq_get_ctx(q);
Jan Karab6e68ee2021-01-11 17:47:17 +01001654 /*
1655 * If the IO scheduler does not respect hardware queues when
1656 * dispatching, we just don't bother with multiple HW queues and
1657 * dispatch from hctx for the current CPU since running multiple queues
1658 * just causes lock contention inside the scheduler and pointless cache
1659 * bouncing.
1660 */
Ming Lei460aa282022-05-22 20:23:50 +08001661 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, 0, ctx);
1662
Jan Karab6e68ee2021-01-11 17:47:17 +01001663 if (!blk_mq_hctx_stopped(hctx))
1664 return hctx;
1665 return NULL;
1666}
1667
André Almeida105663f2020-01-06 15:08:18 -03001668/**
Mauro Carvalho Chehab24f7bb82020-10-23 18:32:54 +02001669 * blk_mq_run_hw_queues - Run all hardware queues in a request queue.
André Almeida105663f2020-01-06 15:08:18 -03001670 * @q: Pointer to the request queue to run.
1671 * @async: If we want to run the queue asynchronously.
1672 */
Mike Snitzerb94ec292015-03-11 23:56:38 -04001673void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001674{
Jan Karab6e68ee2021-01-11 17:47:17 +01001675 struct blk_mq_hw_ctx *hctx, *sq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001676 int i;
1677
Jan Karab6e68ee2021-01-11 17:47:17 +01001678 sq_hctx = NULL;
1679 if (blk_mq_has_sqsched(q))
1680 sq_hctx = blk_mq_get_sq_hctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001681 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe79f720a2017-11-10 09:13:21 -07001682 if (blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +01001683 continue;
Jan Karab6e68ee2021-01-11 17:47:17 +01001684 /*
1685 * Dispatch from this hctx either if there's no hctx preferred
1686 * by IO scheduler or if it has requests that bypass the
1687 * scheduler.
1688 */
1689 if (!sq_hctx || sq_hctx == hctx ||
1690 !list_empty_careful(&hctx->dispatch))
1691 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001692 }
1693}
Mike Snitzerb94ec292015-03-11 23:56:38 -04001694EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001695
Bart Van Asschefd001442016-10-28 17:19:37 -07001696/**
Douglas Andersonb9151e72020-04-20 09:24:52 -07001697 * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously.
1698 * @q: Pointer to the request queue to run.
Minwoo Imfa94ba82020-12-05 00:20:55 +09001699 * @msecs: Milliseconds of delay to wait before running the queues.
Douglas Andersonb9151e72020-04-20 09:24:52 -07001700 */
1701void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs)
1702{
Jan Karab6e68ee2021-01-11 17:47:17 +01001703 struct blk_mq_hw_ctx *hctx, *sq_hctx;
Douglas Andersonb9151e72020-04-20 09:24:52 -07001704 int i;
1705
Jan Karab6e68ee2021-01-11 17:47:17 +01001706 sq_hctx = NULL;
1707 if (blk_mq_has_sqsched(q))
1708 sq_hctx = blk_mq_get_sq_hctx(q);
Douglas Andersonb9151e72020-04-20 09:24:52 -07001709 queue_for_each_hw_ctx(q, hctx, i) {
1710 if (blk_mq_hctx_stopped(hctx))
1711 continue;
Jan Karab6e68ee2021-01-11 17:47:17 +01001712 /*
1713 * Dispatch from this hctx either if there's no hctx preferred
1714 * by IO scheduler or if it has requests that bypass the
1715 * scheduler.
1716 */
1717 if (!sq_hctx || sq_hctx == hctx ||
1718 !list_empty_careful(&hctx->dispatch))
1719 blk_mq_delay_run_hw_queue(hctx, msecs);
Douglas Andersonb9151e72020-04-20 09:24:52 -07001720 }
1721}
1722EXPORT_SYMBOL(blk_mq_delay_run_hw_queues);
1723
1724/**
Bart Van Asschefd001442016-10-28 17:19:37 -07001725 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1726 * @q: request queue.
1727 *
1728 * The caller is responsible for serializing this function against
1729 * blk_mq_{start,stop}_hw_queue().
1730 */
1731bool blk_mq_queue_stopped(struct request_queue *q)
1732{
1733 struct blk_mq_hw_ctx *hctx;
1734 int i;
1735
1736 queue_for_each_hw_ctx(q, hctx, i)
1737 if (blk_mq_hctx_stopped(hctx))
1738 return true;
1739
1740 return false;
1741}
1742EXPORT_SYMBOL(blk_mq_queue_stopped);
1743
Ming Lei39a70c72017-06-06 23:22:09 +08001744/*
1745 * This function is often used for pausing .queue_rq() by driver when
1746 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001747 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001748 *
1749 * We do not guarantee that dispatch can be drained or blocked
1750 * after blk_mq_stop_hw_queue() returns. Please use
1751 * blk_mq_quiesce_queue() for that requirement.
1752 */
Jens Axboe320ae512013-10-24 09:20:05 +01001753void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1754{
Ming Lei641a9ed2017-06-06 23:22:10 +08001755 cancel_delayed_work(&hctx->run_work);
1756
1757 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboe320ae512013-10-24 09:20:05 +01001758}
1759EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1760
Ming Lei39a70c72017-06-06 23:22:09 +08001761/*
1762 * This function is often used for pausing .queue_rq() by driver when
1763 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001764 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001765 *
1766 * We do not guarantee that dispatch can be drained or blocked
1767 * after blk_mq_stop_hw_queues() returns. Please use
1768 * blk_mq_quiesce_queue() for that requirement.
1769 */
Jens Axboe2719aa22017-05-03 11:08:14 -06001770void blk_mq_stop_hw_queues(struct request_queue *q)
1771{
Ming Lei641a9ed2017-06-06 23:22:10 +08001772 struct blk_mq_hw_ctx *hctx;
1773 int i;
1774
1775 queue_for_each_hw_ctx(q, hctx, i)
1776 blk_mq_stop_hw_queue(hctx);
Christoph Hellwig280d45f2013-10-25 14:45:58 +01001777}
1778EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1779
Jens Axboe320ae512013-10-24 09:20:05 +01001780void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1781{
1782 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -06001783
Jens Axboe0ffbce82014-06-25 08:22:34 -06001784 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001785}
1786EXPORT_SYMBOL(blk_mq_start_hw_queue);
1787
Christoph Hellwig2f268552014-04-16 09:44:56 +02001788void blk_mq_start_hw_queues(struct request_queue *q)
1789{
1790 struct blk_mq_hw_ctx *hctx;
1791 int i;
1792
1793 queue_for_each_hw_ctx(q, hctx, i)
1794 blk_mq_start_hw_queue(hctx);
1795}
1796EXPORT_SYMBOL(blk_mq_start_hw_queues);
1797
Jens Axboeae911c52016-12-08 13:19:30 -07001798void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1799{
1800 if (!blk_mq_hctx_stopped(hctx))
1801 return;
1802
1803 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1804 blk_mq_run_hw_queue(hctx, async);
1805}
1806EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1807
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001808void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001809{
1810 struct blk_mq_hw_ctx *hctx;
1811 int i;
1812
Jens Axboeae911c52016-12-08 13:19:30 -07001813 queue_for_each_hw_ctx(q, hctx, i)
1814 blk_mq_start_stopped_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001815}
1816EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1817
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001818static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001819{
1820 struct blk_mq_hw_ctx *hctx;
1821
Jens Axboe9f993732017-04-10 09:54:54 -06001822 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboe21c6e932017-04-10 09:54:56 -06001823
1824 /*
Ming Lei15fe8a902018-04-08 17:48:11 +08001825 * If we are stopped, don't run the queue.
Jens Axboe21c6e932017-04-10 09:54:56 -06001826 */
Yufen Yu08410312020-10-08 23:26:30 -04001827 if (blk_mq_hctx_stopped(hctx))
Jianchao Wang0196d6b2018-06-04 17:03:55 +08001828 return;
Jens Axboee4043dc2014-04-09 10:18:23 -06001829
Jens Axboe320ae512013-10-24 09:20:05 +01001830 __blk_mq_run_hw_queue(hctx);
1831}
1832
Ming Leicfd0c552015-10-20 23:13:57 +08001833static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08001834 struct request *rq,
1835 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01001836{
Jens Axboee57690f2016-08-24 15:34:35 -06001837 struct blk_mq_ctx *ctx = rq->mq_ctx;
Ming Leic16d6b52018-12-17 08:44:05 -07001838 enum hctx_type type = hctx->type;
Jens Axboee57690f2016-08-24 15:34:35 -06001839
Bart Van Assche7b607812017-06-20 11:15:47 -07001840 lockdep_assert_held(&ctx->lock);
1841
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001842 trace_block_rq_insert(rq);
Jens Axboe01b983c2013-11-19 18:59:10 -07001843
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001844 if (at_head)
Ming Leic16d6b52018-12-17 08:44:05 -07001845 list_add(&rq->queuelist, &ctx->rq_lists[type]);
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001846 else
Ming Leic16d6b52018-12-17 08:44:05 -07001847 list_add_tail(&rq->queuelist, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08001848}
Jens Axboe4bb659b2014-05-09 09:36:49 -06001849
Jens Axboe2c3ad662016-12-14 14:34:47 -07001850void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1851 bool at_head)
Ming Leicfd0c552015-10-20 23:13:57 +08001852{
1853 struct blk_mq_ctx *ctx = rq->mq_ctx;
1854
Bart Van Assche7b607812017-06-20 11:15:47 -07001855 lockdep_assert_held(&ctx->lock);
1856
Jens Axboee57690f2016-08-24 15:34:35 -06001857 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01001858 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001859}
1860
André Almeida105663f2020-01-06 15:08:18 -03001861/**
1862 * blk_mq_request_bypass_insert - Insert a request at dispatch list.
1863 * @rq: Pointer to request to be inserted.
Randy Dunlap26bfeb22020-08-16 16:39:34 -07001864 * @at_head: true if the request should be inserted at the head of the list.
André Almeida105663f2020-01-06 15:08:18 -03001865 * @run_queue: If we should run the hardware queue after inserting the request.
1866 *
Jens Axboe157f3772017-09-11 16:43:57 -06001867 * Should only be used carefully, when the caller knows we want to
1868 * bypass a potential IO scheduler on the target device.
1869 */
Ming Lei01e99ae2020-02-25 09:04:32 +08001870void blk_mq_request_bypass_insert(struct request *rq, bool at_head,
1871 bool run_queue)
Jens Axboe157f3772017-09-11 16:43:57 -06001872{
Jens Axboeea4f9952018-10-29 15:06:13 -06001873 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe157f3772017-09-11 16:43:57 -06001874
1875 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001876 if (at_head)
1877 list_add(&rq->queuelist, &hctx->dispatch);
1878 else
1879 list_add_tail(&rq->queuelist, &hctx->dispatch);
Jens Axboe157f3772017-09-11 16:43:57 -06001880 spin_unlock(&hctx->lock);
1881
Ming Leib0850292017-11-02 23:24:34 +08001882 if (run_queue)
1883 blk_mq_run_hw_queue(hctx, false);
Jens Axboe157f3772017-09-11 16:43:57 -06001884}
1885
Jens Axboebd166ef2017-01-17 06:03:22 -07001886void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
1887 struct list_head *list)
Jens Axboe320ae512013-10-24 09:20:05 +01001888
1889{
Ming Lei3f0cedc2018-07-02 17:35:58 +08001890 struct request *rq;
Ming Leic16d6b52018-12-17 08:44:05 -07001891 enum hctx_type type = hctx->type;
Ming Lei3f0cedc2018-07-02 17:35:58 +08001892
Jens Axboe320ae512013-10-24 09:20:05 +01001893 /*
1894 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1895 * offline now
1896 */
Ming Lei3f0cedc2018-07-02 17:35:58 +08001897 list_for_each_entry(rq, list, queuelist) {
Jens Axboee57690f2016-08-24 15:34:35 -06001898 BUG_ON(rq->mq_ctx != ctx);
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001899 trace_block_rq_insert(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001900 }
Ming Lei3f0cedc2018-07-02 17:35:58 +08001901
1902 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001903 list_splice_tail_init(list, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08001904 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001905 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001906}
1907
Sami Tolvanen4f0f5862021-04-08 11:28:34 -07001908static int plug_rq_cmp(void *priv, const struct list_head *a,
1909 const struct list_head *b)
Jens Axboe320ae512013-10-24 09:20:05 +01001910{
1911 struct request *rqa = container_of(a, struct request, queuelist);
1912 struct request *rqb = container_of(b, struct request, queuelist);
1913
Pavel Begunkov7d30a622019-11-29 00:11:53 +03001914 if (rqa->mq_ctx != rqb->mq_ctx)
1915 return rqa->mq_ctx > rqb->mq_ctx;
1916 if (rqa->mq_hctx != rqb->mq_hctx)
1917 return rqa->mq_hctx > rqb->mq_hctx;
Jens Axboe3110fc72018-10-30 12:24:04 -06001918
1919 return blk_rq_pos(rqa) > blk_rq_pos(rqb);
Jens Axboe320ae512013-10-24 09:20:05 +01001920}
1921
1922void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1923{
Jens Axboe320ae512013-10-24 09:20:05 +01001924 LIST_HEAD(list);
Jens Axboe320ae512013-10-24 09:20:05 +01001925
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001926 if (list_empty(&plug->mq_list))
1927 return;
Jens Axboe320ae512013-10-24 09:20:05 +01001928 list_splice_init(&plug->mq_list, &list);
1929
Jens Axboece5b0092018-11-27 17:13:56 -07001930 if (plug->rq_count > 2 && plug->multiple_queues)
1931 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe320ae512013-10-24 09:20:05 +01001932
Dongli Zhangbcc816d2019-04-04 10:57:44 +08001933 plug->rq_count = 0;
1934
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001935 do {
1936 struct list_head rq_list;
1937 struct request *rq, *head_rq = list_entry_rq(list.next);
1938 struct list_head *pos = &head_rq->queuelist; /* skip first */
1939 struct blk_mq_hw_ctx *this_hctx = head_rq->mq_hctx;
1940 struct blk_mq_ctx *this_ctx = head_rq->mq_ctx;
1941 unsigned int depth = 1;
Jens Axboe320ae512013-10-24 09:20:05 +01001942
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001943 list_for_each_continue(pos, &list) {
1944 rq = list_entry_rq(pos);
1945 BUG_ON(!rq->q);
1946 if (rq->mq_hctx != this_hctx || rq->mq_ctx != this_ctx)
1947 break;
1948 depth++;
Jens Axboe320ae512013-10-24 09:20:05 +01001949 }
1950
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001951 list_cut_before(&rq_list, &list, pos);
1952 trace_block_unplug(head_rq->q, depth, !from_schedule);
Jens Axboe67cae4c2018-10-30 11:31:51 -06001953 blk_mq_sched_insert_requests(this_hctx, this_ctx, &rq_list,
Jens Axboebd166ef2017-01-17 06:03:22 -07001954 from_schedule);
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001955 } while(!list_empty(&list));
Jens Axboe320ae512013-10-24 09:20:05 +01001956}
1957
Christoph Hellwig14ccb662019-06-06 12:29:01 +02001958static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
1959 unsigned int nr_segs)
Jens Axboe320ae512013-10-24 09:20:05 +01001960{
Eric Biggers93f221a2020-09-15 20:53:14 -07001961 int err;
1962
Christoph Hellwigf924cdd2019-06-06 12:29:00 +02001963 if (bio->bi_opf & REQ_RAHEAD)
1964 rq->cmd_flags |= REQ_FAILFAST_MASK;
1965
1966 rq->__sector = bio->bi_iter.bi_sector;
1967 rq->write_hint = bio->bi_write_hint;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02001968 blk_rq_bio_prep(rq, bio, nr_segs);
Eric Biggers93f221a2020-09-15 20:53:14 -07001969
1970 /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */
1971 err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO);
1972 WARN_ON_ONCE(err);
Jens Axboe4b570522014-05-29 11:00:11 -06001973
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +02001974 blk_account_io_start(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001975}
1976
Mike Snitzer0f955492018-01-17 11:25:56 -05001977static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
1978 struct request *rq,
Jens Axboebe94f052018-11-24 10:15:46 -07001979 blk_qc_t *cookie, bool last)
Shaohua Lif984df12015-05-08 10:51:32 -07001980{
Shaohua Lif984df12015-05-08 10:51:32 -07001981 struct request_queue *q = rq->q;
Shaohua Lif984df12015-05-08 10:51:32 -07001982 struct blk_mq_queue_data bd = {
1983 .rq = rq,
Jens Axboebe94f052018-11-24 10:15:46 -07001984 .last = last,
Shaohua Lif984df12015-05-08 10:51:32 -07001985 };
Jens Axboebd166ef2017-01-17 06:03:22 -07001986 blk_qc_t new_cookie;
Jens Axboef06345a2017-06-12 11:22:46 -06001987 blk_status_t ret;
Mike Snitzer0f955492018-01-17 11:25:56 -05001988
1989 new_cookie = request_to_qc_t(hctx, rq);
1990
1991 /*
1992 * For OK queue, we are done. For error, caller may kill it.
1993 * Any other error (busy), just add it to our list as we
1994 * previously would have done.
1995 */
1996 ret = q->mq_ops->queue_rq(hctx, &bd);
1997 switch (ret) {
1998 case BLK_STS_OK:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001999 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05002000 *cookie = new_cookie;
2001 break;
2002 case BLK_STS_RESOURCE:
Ming Lei86ff7c22018-01-30 22:04:57 -05002003 case BLK_STS_DEV_RESOURCE:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002004 blk_mq_update_dispatch_busy(hctx, true);
Mike Snitzer0f955492018-01-17 11:25:56 -05002005 __blk_mq_requeue_request(rq);
2006 break;
2007 default:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002008 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05002009 *cookie = BLK_QC_T_NONE;
2010 break;
2011 }
2012
2013 return ret;
2014}
2015
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002016static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
Mike Snitzer0f955492018-01-17 11:25:56 -05002017 struct request *rq,
Ming Lei396eaf22018-01-17 11:25:57 -05002018 blk_qc_t *cookie,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002019 bool bypass_insert, bool last)
Mike Snitzer0f955492018-01-17 11:25:56 -05002020{
2021 struct request_queue *q = rq->q;
Ming Leid964f042017-06-06 23:22:00 +08002022 bool run_queue = true;
Ming Lei2a5a24a2021-01-22 10:33:12 +08002023 int budget_token;
Ming Leid964f042017-06-06 23:22:00 +08002024
Ming Lei23d4ee12018-01-18 12:06:59 +08002025 /*
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002026 * RCU or SRCU read lock is needed before checking quiesced flag.
Ming Lei23d4ee12018-01-18 12:06:59 +08002027 *
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002028 * When queue is stopped or quiesced, ignore 'bypass_insert' from
2029 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
2030 * and avoid driver to try to dispatch again.
Ming Lei23d4ee12018-01-18 12:06:59 +08002031 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002032 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
Ming Leid964f042017-06-06 23:22:00 +08002033 run_queue = false;
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002034 bypass_insert = false;
2035 goto insert;
Ming Leid964f042017-06-06 23:22:00 +08002036 }
Shaohua Lif984df12015-05-08 10:51:32 -07002037
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002038 if (q->elevator && !bypass_insert)
2039 goto insert;
Bart Van Assche2253efc2016-10-28 17:20:02 -07002040
Ming Lei2a5a24a2021-01-22 10:33:12 +08002041 budget_token = blk_mq_get_dispatch_budget(q);
2042 if (budget_token < 0)
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002043 goto insert;
Jens Axboebd166ef2017-01-17 06:03:22 -07002044
Ming Lei2a5a24a2021-01-22 10:33:12 +08002045 blk_mq_set_rq_budget_token(rq, budget_token);
2046
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08002047 if (!blk_mq_get_driver_tag(rq)) {
Ming Lei2a5a24a2021-01-22 10:33:12 +08002048 blk_mq_put_dispatch_budget(q, budget_token);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002049 goto insert;
Ming Lei88022d72017-11-05 02:21:12 +08002050 }
Ming Leide148292017-10-14 17:22:29 +08002051
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002052 return __blk_mq_issue_directly(hctx, rq, cookie, last);
2053insert:
2054 if (bypass_insert)
2055 return BLK_STS_RESOURCE;
2056
Ming Leidb03f882020-08-18 17:07:28 +08002057 blk_mq_sched_insert_request(rq, false, run_queue, false);
2058
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002059 return BLK_STS_OK;
2060}
2061
André Almeida105663f2020-01-06 15:08:18 -03002062/**
2063 * blk_mq_try_issue_directly - Try to send a request directly to device driver.
2064 * @hctx: Pointer of the associated hardware queue.
2065 * @rq: Pointer to request to be sent.
2066 * @cookie: Request queue cookie.
2067 *
2068 * If the device has enough resources to accept a new request now, send the
2069 * request directly to device driver. Else, insert at hctx->dispatch queue, so
2070 * we can try send it another time in the future. Requests inserted at this
2071 * queue have higher priority.
2072 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002073static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
2074 struct request *rq, blk_qc_t *cookie)
2075{
2076 blk_status_t ret;
2077 int srcu_idx;
2078
2079 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
2080
2081 hctx_lock(hctx, &srcu_idx);
2082
2083 ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false, true);
2084 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
Ming Lei01e99ae2020-02-25 09:04:32 +08002085 blk_mq_request_bypass_insert(rq, false, true);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002086 else if (ret != BLK_STS_OK)
2087 blk_mq_end_request(rq, ret);
2088
Jens Axboe04ced1592018-01-09 08:29:46 -08002089 hctx_unlock(hctx, srcu_idx);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002090}
2091
2092blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
2093{
2094 blk_status_t ret;
2095 int srcu_idx;
2096 blk_qc_t unused_cookie;
2097 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
2098
2099 hctx_lock(hctx, &srcu_idx);
2100 ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true, last);
2101 hctx_unlock(hctx, srcu_idx);
Jianchao Wang7f556a42018-12-14 09:28:18 +08002102
2103 return ret;
Christoph Hellwig5eb61262017-03-22 15:01:51 -04002104}
2105
Ming Lei6ce3dd62018-07-10 09:03:31 +08002106void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
2107 struct list_head *list)
2108{
Keith Busch536167d42020-04-07 03:13:48 +09002109 int queued = 0;
yangerkun632bfb62020-09-05 19:25:56 +08002110 int errors = 0;
Keith Busch536167d42020-04-07 03:13:48 +09002111
Ming Lei6ce3dd62018-07-10 09:03:31 +08002112 while (!list_empty(list)) {
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002113 blk_status_t ret;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002114 struct request *rq = list_first_entry(list, struct request,
2115 queuelist);
2116
2117 list_del_init(&rq->queuelist);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002118 ret = blk_mq_request_issue_directly(rq, list_empty(list));
2119 if (ret != BLK_STS_OK) {
Yu Kuai77864ed2022-07-26 20:22:24 +08002120 errors++;
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002121 if (ret == BLK_STS_RESOURCE ||
2122 ret == BLK_STS_DEV_RESOURCE) {
Ming Lei01e99ae2020-02-25 09:04:32 +08002123 blk_mq_request_bypass_insert(rq, false,
Jens Axboec616cbe2018-12-06 22:17:44 -07002124 list_empty(list));
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002125 break;
2126 }
2127 blk_mq_end_request(rq, ret);
Keith Busch536167d42020-04-07 03:13:48 +09002128 } else
2129 queued++;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002130 }
Jens Axboed666ba92018-11-27 17:02:25 -07002131
2132 /*
2133 * If we didn't flush the entire list, we could have told
2134 * the driver there was more coming, but that turned out to
2135 * be a lie.
2136 */
yangerkun632bfb62020-09-05 19:25:56 +08002137 if ((!list_empty(list) || errors) &&
2138 hctx->queue->mq_ops->commit_rqs && queued)
Jens Axboed666ba92018-11-27 17:02:25 -07002139 hctx->queue->mq_ops->commit_rqs(hctx);
Ming Lei6ce3dd62018-07-10 09:03:31 +08002140}
2141
Jens Axboece5b0092018-11-27 17:13:56 -07002142static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
2143{
2144 list_add_tail(&rq->queuelist, &plug->mq_list);
2145 plug->rq_count++;
2146 if (!plug->multiple_queues && !list_is_singular(&plug->mq_list)) {
2147 struct request *tmp;
2148
2149 tmp = list_first_entry(&plug->mq_list, struct request,
2150 queuelist);
2151 if (tmp->q != rq->q)
2152 plug->multiple_queues = true;
2153 }
2154}
2155
Song Liu7f2a6a62021-09-07 16:03:38 -07002156/*
Jens Axboe396c9e82021-10-06 12:01:07 -06002157 * Allow 2x BLK_MAX_REQUEST_COUNT requests on plug queue for multiple
Song Liu7f2a6a62021-09-07 16:03:38 -07002158 * queues. This is important for md arrays to benefit from merging
2159 * requests.
2160 */
2161static inline unsigned short blk_plug_max_rq_count(struct blk_plug *plug)
2162{
2163 if (plug->multiple_queues)
Jens Axboe396c9e82021-10-06 12:01:07 -06002164 return BLK_MAX_REQUEST_COUNT * 2;
Song Liu7f2a6a62021-09-07 16:03:38 -07002165 return BLK_MAX_REQUEST_COUNT;
2166}
2167
Jan Karab029b532022-06-23 09:48:32 +02002168static void bio_set_ioprio(struct bio *bio)
2169{
Jan Karaa0c7043a2022-06-23 09:48:34 +02002170 /* Nobody set ioprio so far? Initialize it based on task's nice value */
2171 if (IOPRIO_PRIO_CLASS(bio->bi_ioprio) == IOPRIO_CLASS_NONE)
2172 bio->bi_ioprio = get_current_ioprio();
Jan Karab029b532022-06-23 09:48:32 +02002173 blkcg_set_ioprio(bio);
2174}
2175
André Almeida105663f2020-01-06 15:08:18 -03002176/**
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02002177 * blk_mq_submit_bio - Create and send a request to block device.
André Almeida105663f2020-01-06 15:08:18 -03002178 * @bio: Bio pointer.
2179 *
2180 * Builds up a request structure from @q and @bio and send to the device. The
2181 * request may not be queued directly to hardware if:
2182 * * This request can be merged with another one
2183 * * We want to place request at plug queue for possible future merging
2184 * * There is an IO scheduler active at this queue
2185 *
2186 * It will not queue the request if there is an error with the bio, or at the
2187 * request creation.
2188 *
2189 * Returns: Request queue cookie.
2190 */
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02002191blk_qc_t blk_mq_submit_bio(struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06002192{
Christoph Hellwig309dca302021-01-24 11:02:34 +01002193 struct request_queue *q = bio->bi_bdev->bd_disk->queue;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002194 const int is_sync = op_is_sync(bio->bi_opf);
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07002195 const int is_flush_fua = op_is_flush(bio->bi_opf);
Christoph Hellwige6e7abf2020-05-29 15:53:09 +02002196 struct blk_mq_alloc_data data = {
2197 .q = q,
2198 };
Jens Axboe07068d52014-05-22 10:40:51 -06002199 struct request *rq;
Shaohua Lif984df12015-05-08 10:51:32 -07002200 struct blk_plug *plug;
Shaohua Li5b3f3412015-05-08 10:51:33 -07002201 struct request *same_queue_rq = NULL;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002202 unsigned int nr_segs;
Jens Axboe7b371632015-11-05 10:41:40 -07002203 blk_qc_t cookie;
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002204 blk_status_t ret;
Jeffle Xucc29e1b2020-11-26 17:18:52 +08002205 bool hipri;
Jens Axboe07068d52014-05-22 10:40:51 -06002206
2207 blk_queue_bounce(q, &bio);
Christoph Hellwigf695ca32020-07-01 10:59:39 +02002208 __blk_queue_split(&bio, &nr_segs);
Jens Axboe7ec9a452023-01-04 08:51:19 -07002209 if (!bio)
2210 goto queue_exit;
Wen Xiongf36ea502017-05-10 08:54:11 -05002211
Dmitry Monakhove23947b2017-06-29 11:31:11 -07002212 if (!bio_integrity_prep(bio))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002213 goto queue_exit;
Jens Axboe07068d52014-05-22 10:40:51 -06002214
Jan Kara739e44e2022-06-23 09:48:33 +02002215 bio_set_ioprio(bio);
2216
Omar Sandoval87c279e62016-06-01 22:18:48 -07002217 if (!is_flush_fua && !blk_queue_nomerges(q) &&
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002218 blk_attempt_plug_merge(q, bio, nr_segs, &same_queue_rq))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002219 goto queue_exit;
Shaohua Lif984df12015-05-08 10:51:32 -07002220
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002221 if (blk_mq_sched_bio_merge(q, bio, nr_segs))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002222 goto queue_exit;
Jens Axboebd166ef2017-01-17 06:03:22 -07002223
Christoph Hellwigd5337562018-11-14 17:02:09 +01002224 rq_qos_throttle(q, bio);
Jens Axboe87760e52016-11-09 12:38:14 -07002225
Jeffle Xucc29e1b2020-11-26 17:18:52 +08002226 hipri = bio->bi_opf & REQ_HIPRI;
2227
Ming Lei78091672019-01-16 19:08:15 +08002228 data.cmd_flags = bio->bi_opf;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +02002229 rq = __blk_mq_alloc_request(&data);
Jens Axboe87760e52016-11-09 12:38:14 -07002230 if (unlikely(!rq)) {
Josef Bacikc1c80382018-07-03 11:14:59 -04002231 rq_qos_cleanup(q, bio);
Jens Axboe7b6620d2019-08-15 11:09:16 -06002232 if (bio->bi_opf & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -05002233 bio_wouldblock_error(bio);
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002234 goto queue_exit;
Jens Axboe87760e52016-11-09 12:38:14 -07002235 }
2236
Christoph Hellwige8a676d2020-12-03 17:21:36 +01002237 trace_block_getrq(bio);
Xiaoguang Wangd6f1dda2018-10-23 22:30:50 +08002238
Josef Bacikc1c80382018-07-03 11:14:59 -04002239 rq_qos_track(q, rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06002240
Jens Axboefd2d3322017-01-12 10:04:45 -07002241 cookie = request_to_qc_t(data.hctx, rq);
Jens Axboe07068d52014-05-22 10:40:51 -06002242
Bart Van Assche970d1682019-07-01 08:47:30 -07002243 blk_mq_bio_to_request(rq, bio, nr_segs);
2244
Eric Biggers1f978b52023-03-15 11:39:02 -07002245 ret = blk_crypto_rq_get_keyslot(rq);
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002246 if (ret != BLK_STS_OK) {
2247 bio->bi_status = ret;
2248 bio_endio(bio);
2249 blk_mq_free_request(rq);
2250 return BLK_QC_T_NONE;
2251 }
2252
Damien Le Moalb49773e72019-07-11 01:18:31 +09002253 plug = blk_mq_plug(q, bio);
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04002254 if (unlikely(is_flush_fua)) {
André Almeida105663f2020-01-06 15:08:18 -03002255 /* Bypass scheduler for flush requests */
Ming Lei923218f2017-11-02 23:24:38 +08002256 blk_insert_flush(rq);
2257 blk_mq_run_hw_queue(data.hctx, true);
Ming Lei03f26d82021-05-14 10:20:52 +08002258 } else if (plug && (q->nr_hw_queues == 1 ||
2259 blk_mq_is_sbitmap_shared(rq->mq_hctx->flags) ||
2260 q->mq_ops->commit_rqs || !blk_queue_nonrot(q))) {
Jens Axboeb2c5d162018-11-29 10:03:42 -07002261 /*
2262 * Use plugging if we have a ->commit_rqs() hook as well, as
2263 * we know the driver uses bd->last in a smart fashion.
Ming Lei3154df22019-09-27 15:24:31 +08002264 *
2265 * Use normal plugging if this disk is slow HDD, as sequential
2266 * IO may benefit a lot from plug merging.
Jens Axboeb2c5d162018-11-29 10:03:42 -07002267 */
Jens Axboe5f0ed772018-11-23 22:04:33 -07002268 unsigned int request_count = plug->rq_count;
Shaohua Li600271d2016-11-03 17:03:54 -07002269 struct request *last = NULL;
2270
Ming Lei676d0602015-10-20 23:13:56 +08002271 if (!request_count)
Jeff Moyere6c44382015-05-08 10:51:30 -07002272 trace_block_plug(q);
Shaohua Li600271d2016-11-03 17:03:54 -07002273 else
2274 last = list_entry_rq(plug->mq_list.prev);
Jens Axboeb094f892015-11-20 20:29:45 -07002275
Song Liu7f2a6a62021-09-07 16:03:38 -07002276 if (request_count >= blk_plug_max_rq_count(plug) || (last &&
Shaohua Li600271d2016-11-03 17:03:54 -07002277 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
Jeff Moyere6c44382015-05-08 10:51:30 -07002278 blk_flush_plug_list(plug, false);
2279 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002280 }
Jens Axboeb094f892015-11-20 20:29:45 -07002281
Jens Axboece5b0092018-11-27 17:13:56 -07002282 blk_add_rq_to_plug(plug, rq);
Ming Leia12de1d2019-09-27 15:24:30 +08002283 } else if (q->elevator) {
André Almeida105663f2020-01-06 15:08:18 -03002284 /* Insert the request at the IO scheduler queue */
Ming Leia12de1d2019-09-27 15:24:30 +08002285 blk_mq_sched_insert_request(rq, false, true, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002286 } else if (plug && !blk_queue_nomerges(q)) {
Jens Axboe320ae512013-10-24 09:20:05 +01002287 /*
2288 * We do limited plugging. If the bio can be merged, do that.
2289 * Otherwise the existing request in the plug list will be
2290 * issued. So the plug list will have one request at most
Christoph Hellwig22997222017-03-22 15:01:52 -04002291 * The plug list might get flushed before this. If that happens,
2292 * the plug list is empty, and same_queue_rq is invalid.
Jens Axboe320ae512013-10-24 09:20:05 +01002293 */
Christoph Hellwig22997222017-03-22 15:01:52 -04002294 if (list_empty(&plug->mq_list))
2295 same_queue_rq = NULL;
Jens Axboe4711b572018-11-27 17:07:17 -07002296 if (same_queue_rq) {
Christoph Hellwig22997222017-03-22 15:01:52 -04002297 list_del_init(&same_queue_rq->queuelist);
Jens Axboe4711b572018-11-27 17:07:17 -07002298 plug->rq_count--;
2299 }
Jens Axboece5b0092018-11-27 17:13:56 -07002300 blk_add_rq_to_plug(plug, rq);
Yufen Yuff3b74b2019-03-26 21:19:25 +08002301 trace_block_plug(q);
Christoph Hellwig22997222017-03-22 15:01:52 -04002302
Ming Leidad7a3b2017-06-06 23:21:59 +08002303 if (same_queue_rq) {
Jens Axboeea4f9952018-10-29 15:06:13 -06002304 data.hctx = same_queue_rq->mq_hctx;
Yufen Yuff3b74b2019-03-26 21:19:25 +08002305 trace_block_unplug(q, 1, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002306 blk_mq_try_issue_directly(data.hctx, same_queue_rq,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002307 &cookie);
Ming Leidad7a3b2017-06-06 23:21:59 +08002308 }
Ming Leia12de1d2019-09-27 15:24:30 +08002309 } else if ((q->nr_hw_queues > 1 && is_sync) ||
2310 !data.hctx->dispatch_busy) {
André Almeida105663f2020-01-06 15:08:18 -03002311 /*
2312 * There is no scheduler and we can try to send directly
2313 * to the hardware.
2314 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002315 blk_mq_try_issue_directly(data.hctx, rq, &cookie);
Ming Leiab42f352017-05-26 19:53:19 +08002316 } else {
André Almeida105663f2020-01-06 15:08:18 -03002317 /* Default case. */
huhai8fa9f552018-05-16 08:21:21 -06002318 blk_mq_sched_insert_request(rq, false, true, true);
Ming Leiab42f352017-05-26 19:53:19 +08002319 }
Jens Axboe320ae512013-10-24 09:20:05 +01002320
Jeffle Xucc29e1b2020-11-26 17:18:52 +08002321 if (!hipri)
2322 return BLK_QC_T_NONE;
Jens Axboe7b371632015-11-05 10:41:40 -07002323 return cookie;
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002324queue_exit:
2325 blk_queue_exit(q);
2326 return BLK_QC_T_NONE;
Jens Axboe320ae512013-10-24 09:20:05 +01002327}
2328
Ming Leibd631412021-05-11 23:22:35 +08002329static size_t order_to_size(unsigned int order)
2330{
2331 return (size_t)PAGE_SIZE << order;
2332}
2333
2334/* called before freeing request pool in @tags */
2335static void blk_mq_clear_rq_mapping(struct blk_mq_tag_set *set,
2336 struct blk_mq_tags *tags, unsigned int hctx_idx)
2337{
2338 struct blk_mq_tags *drv_tags = set->tags[hctx_idx];
2339 struct page *page;
2340 unsigned long flags;
2341
2342 list_for_each_entry(page, &tags->page_list, lru) {
2343 unsigned long start = (unsigned long)page_address(page);
2344 unsigned long end = start + order_to_size(page->private);
2345 int i;
2346
2347 for (i = 0; i < set->queue_depth; i++) {
2348 struct request *rq = drv_tags->rqs[i];
2349 unsigned long rq_addr = (unsigned long)rq;
2350
2351 if (rq_addr >= start && rq_addr < end) {
2352 WARN_ON_ONCE(refcount_read(&rq->ref) != 0);
2353 cmpxchg(&drv_tags->rqs[i], rq, NULL);
2354 }
2355 }
2356 }
2357
2358 /*
2359 * Wait until all pending iteration is done.
2360 *
2361 * Request reference is cleared and it is guaranteed to be observed
2362 * after the ->lock is released.
2363 */
2364 spin_lock_irqsave(&drv_tags->lock, flags);
2365 spin_unlock_irqrestore(&drv_tags->lock, flags);
2366}
2367
Jens Axboecc71a6f2017-01-11 14:29:56 -07002368void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2369 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01002370{
2371 struct page *page;
2372
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002373 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002374 int i;
2375
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002376 for (i = 0; i < tags->nr_tags; i++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002377 struct request *rq = tags->static_rqs[i];
2378
2379 if (!rq)
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002380 continue;
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002381 set->ops->exit_request(set, rq, hctx_idx);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002382 tags->static_rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002383 }
2384 }
2385
Ming Leibd631412021-05-11 23:22:35 +08002386 blk_mq_clear_rq_mapping(set, tags, hctx_idx);
2387
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002388 while (!list_empty(&tags->page_list)) {
2389 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07002390 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002391 /*
2392 * Remove kmemleak object previously allocated in
Raul E Rangel273938b2019-05-02 13:48:11 -06002393 * blk_mq_alloc_rqs().
Catalin Marinasf75782e2015-09-14 18:16:02 +01002394 */
2395 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01002396 __free_pages(page, page->private);
2397 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002398}
Jens Axboe320ae512013-10-24 09:20:05 +01002399
John Garry1c0706a2020-08-19 23:20:22 +08002400void blk_mq_free_rq_map(struct blk_mq_tags *tags, unsigned int flags)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002401{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002402 kfree(tags->rqs);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002403 tags->rqs = NULL;
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002404 kfree(tags->static_rqs);
2405 tags->static_rqs = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002406
John Garry1c0706a2020-08-19 23:20:22 +08002407 blk_mq_free_tags(tags, flags);
Jens Axboe320ae512013-10-24 09:20:05 +01002408}
2409
Jens Axboecc71a6f2017-01-11 14:29:56 -07002410struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
2411 unsigned int hctx_idx,
2412 unsigned int nr_tags,
John Garry1c0706a2020-08-19 23:20:22 +08002413 unsigned int reserved_tags,
2414 unsigned int flags)
Jens Axboe320ae512013-10-24 09:20:05 +01002415{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002416 struct blk_mq_tags *tags;
Shaohua Li59f082e2017-02-01 09:53:14 -08002417 int node;
Jens Axboe320ae512013-10-24 09:20:05 +01002418
Dongli Zhang7d76f852019-02-27 21:35:01 +08002419 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002420 if (node == NUMA_NO_NODE)
2421 node = set->numa_node;
2422
John Garry1c0706a2020-08-19 23:20:22 +08002423 tags = blk_mq_init_tags(nr_tags, reserved_tags, node, flags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002424 if (!tags)
2425 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002426
Kees Cook590b5b72018-06-12 14:04:20 -07002427 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002428 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
Shaohua Li59f082e2017-02-01 09:53:14 -08002429 node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002430 if (!tags->rqs) {
John Garry1c0706a2020-08-19 23:20:22 +08002431 blk_mq_free_tags(tags, flags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002432 return NULL;
2433 }
Jens Axboe320ae512013-10-24 09:20:05 +01002434
Kees Cook590b5b72018-06-12 14:04:20 -07002435 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2436 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2437 node);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002438 if (!tags->static_rqs) {
2439 kfree(tags->rqs);
John Garry1c0706a2020-08-19 23:20:22 +08002440 blk_mq_free_tags(tags, flags);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002441 return NULL;
2442 }
2443
Jens Axboecc71a6f2017-01-11 14:29:56 -07002444 return tags;
2445}
2446
Tejun Heo1d9bd512018-01-09 08:29:48 -08002447static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2448 unsigned int hctx_idx, int node)
2449{
2450 int ret;
2451
2452 if (set->ops->init_request) {
2453 ret = set->ops->init_request(set, rq, hctx_idx, node);
2454 if (ret)
2455 return ret;
2456 }
2457
Keith Busch12f5b932018-05-29 15:52:28 +02002458 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Tejun Heo1d9bd512018-01-09 08:29:48 -08002459 return 0;
2460}
2461
Jens Axboecc71a6f2017-01-11 14:29:56 -07002462int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2463 unsigned int hctx_idx, unsigned int depth)
2464{
2465 unsigned int i, j, entries_per_page, max_order = 4;
2466 size_t rq_size, left;
Shaohua Li59f082e2017-02-01 09:53:14 -08002467 int node;
2468
Dongli Zhang7d76f852019-02-27 21:35:01 +08002469 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002470 if (node == NUMA_NO_NODE)
2471 node = set->numa_node;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002472
2473 INIT_LIST_HEAD(&tags->page_list);
2474
Jens Axboe320ae512013-10-24 09:20:05 +01002475 /*
2476 * rq_size is the size of the request plus driver payload, rounded
2477 * to the cacheline size
2478 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002479 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01002480 cache_line_size());
Yang Yang4c1cf362022-10-08 11:27:36 +08002481 trace_android_vh_blk_alloc_rqs(&rq_size, set, tags, hctx_idx);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002482 left = rq_size * depth;
Jens Axboe320ae512013-10-24 09:20:05 +01002483
Jens Axboecc71a6f2017-01-11 14:29:56 -07002484 for (i = 0; i < depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01002485 int this_order = max_order;
2486 struct page *page;
2487 int to_do;
2488 void *p;
2489
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06002490 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01002491 this_order--;
2492
2493 do {
Shaohua Li59f082e2017-02-01 09:53:14 -08002494 page = alloc_pages_node(node,
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002495 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06002496 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01002497 if (page)
2498 break;
2499 if (!this_order--)
2500 break;
2501 if (order_to_size(this_order) < rq_size)
2502 break;
2503 } while (1);
2504
2505 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002506 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01002507
2508 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002509 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01002510
2511 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002512 /*
2513 * Allow kmemleak to scan these pages as they contain pointers
2514 * to additional allocations like via ops->init_request().
2515 */
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002516 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
Jens Axboe320ae512013-10-24 09:20:05 +01002517 entries_per_page = order_to_size(this_order) / rq_size;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002518 to_do = min(entries_per_page, depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01002519 left -= to_do * rq_size;
2520 for (j = 0; j < to_do; j++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002521 struct request *rq = p;
2522
2523 tags->static_rqs[i] = rq;
Tejun Heo1d9bd512018-01-09 08:29:48 -08002524 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
2525 tags->static_rqs[i] = NULL;
2526 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002527 }
2528
Jens Axboe320ae512013-10-24 09:20:05 +01002529 p += rq_size;
2530 i++;
2531 }
2532 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002533 return 0;
Jens Axboe320ae512013-10-24 09:20:05 +01002534
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002535fail:
Jens Axboecc71a6f2017-01-11 14:29:56 -07002536 blk_mq_free_rqs(set, tags, hctx_idx);
2537 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01002538}
2539
Ming Leibf0beec2020-05-29 15:53:15 +02002540struct rq_iter_data {
2541 struct blk_mq_hw_ctx *hctx;
2542 bool has_rq;
2543};
2544
2545static bool blk_mq_has_request(struct request *rq, void *data, bool reserved)
2546{
2547 struct rq_iter_data *iter_data = data;
2548
2549 if (rq->mq_hctx != iter_data->hctx)
2550 return true;
2551 iter_data->has_rq = true;
2552 return false;
2553}
2554
2555static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
2556{
2557 struct blk_mq_tags *tags = hctx->sched_tags ?
2558 hctx->sched_tags : hctx->tags;
2559 struct rq_iter_data data = {
2560 .hctx = hctx,
2561 };
2562
2563 blk_mq_all_tag_iter(tags, blk_mq_has_request, &data);
2564 return data.has_rq;
2565}
2566
2567static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu,
2568 struct blk_mq_hw_ctx *hctx)
2569{
2570 if (cpumask_next_and(-1, hctx->cpumask, cpu_online_mask) != cpu)
2571 return false;
2572 if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids)
2573 return false;
2574 return true;
2575}
2576
2577static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
2578{
2579 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2580 struct blk_mq_hw_ctx, cpuhp_online);
2581
2582 if (!cpumask_test_cpu(cpu, hctx->cpumask) ||
2583 !blk_mq_last_cpu_in_hctx(cpu, hctx))
2584 return 0;
2585
2586 /*
2587 * Prevent new request from being allocated on the current hctx.
2588 *
2589 * The smp_mb__after_atomic() Pairs with the implied barrier in
2590 * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is
2591 * seen once we return from the tag allocator.
2592 */
2593 set_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2594 smp_mb__after_atomic();
2595
2596 /*
2597 * Try to grab a reference to the queue and wait for any outstanding
2598 * requests. If we could not grab a reference the queue has been
2599 * frozen and there are no requests.
2600 */
2601 if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) {
2602 while (blk_mq_hctx_has_requests(hctx))
2603 msleep(5);
2604 percpu_ref_put(&hctx->queue->q_usage_counter);
2605 }
2606
2607 return 0;
2608}
2609
2610static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node)
2611{
2612 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2613 struct blk_mq_hw_ctx, cpuhp_online);
2614
2615 if (cpumask_test_cpu(cpu, hctx->cpumask))
2616 clear_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2617 return 0;
2618}
2619
Jens Axboee57690f2016-08-24 15:34:35 -06002620/*
2621 * 'cpu' is going away. splice any existing rq_list entries from this
2622 * software queue to the hw queue dispatch list, and ensure that it
2623 * gets run.
2624 */
Thomas Gleixner9467f852016-09-22 08:05:17 -06002625static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
Jens Axboe484b4062014-05-21 14:01:15 -06002626{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002627 struct blk_mq_hw_ctx *hctx;
Jens Axboe484b4062014-05-21 14:01:15 -06002628 struct blk_mq_ctx *ctx;
2629 LIST_HEAD(tmp);
Ming Leic16d6b52018-12-17 08:44:05 -07002630 enum hctx_type type;
Jens Axboe484b4062014-05-21 14:01:15 -06002631
Thomas Gleixner9467f852016-09-22 08:05:17 -06002632 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02002633 if (!cpumask_test_cpu(cpu, hctx->cpumask))
2634 return 0;
2635
Jens Axboee57690f2016-08-24 15:34:35 -06002636 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Ming Leic16d6b52018-12-17 08:44:05 -07002637 type = hctx->type;
Jens Axboe484b4062014-05-21 14:01:15 -06002638
2639 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002640 if (!list_empty(&ctx->rq_lists[type])) {
2641 list_splice_init(&ctx->rq_lists[type], &tmp);
Jens Axboe484b4062014-05-21 14:01:15 -06002642 blk_mq_hctx_clear_pending(hctx, ctx);
2643 }
2644 spin_unlock(&ctx->lock);
2645
2646 if (list_empty(&tmp))
Thomas Gleixner9467f852016-09-22 08:05:17 -06002647 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002648
Jens Axboee57690f2016-08-24 15:34:35 -06002649 spin_lock(&hctx->lock);
2650 list_splice_tail_init(&tmp, &hctx->dispatch);
2651 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06002652
2653 blk_mq_run_hw_queue(hctx, true);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002654 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002655}
2656
Thomas Gleixner9467f852016-09-22 08:05:17 -06002657static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
Jens Axboe484b4062014-05-21 14:01:15 -06002658{
Ming Leibf0beec2020-05-29 15:53:15 +02002659 if (!(hctx->flags & BLK_MQ_F_STACKING))
2660 cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2661 &hctx->cpuhp_online);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002662 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
2663 &hctx->cpuhp_dead);
Jens Axboe484b4062014-05-21 14:01:15 -06002664}
2665
Ming Lei364b6182021-05-11 23:22:36 +08002666/*
2667 * Before freeing hw queue, clearing the flush request reference in
2668 * tags->rqs[] for avoiding potential UAF.
2669 */
2670static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags,
2671 unsigned int queue_depth, struct request *flush_rq)
2672{
2673 int i;
2674 unsigned long flags;
2675
2676 /* The hw queue may not be mapped yet */
2677 if (!tags)
2678 return;
2679
2680 WARN_ON_ONCE(refcount_read(&flush_rq->ref) != 0);
2681
2682 for (i = 0; i < queue_depth; i++)
2683 cmpxchg(&tags->rqs[i], flush_rq, NULL);
2684
2685 /*
2686 * Wait until all pending iteration is done.
2687 *
2688 * Request reference is cleared and it is guaranteed to be observed
2689 * after the ->lock is released.
2690 */
2691 spin_lock_irqsave(&tags->lock, flags);
2692 spin_unlock_irqrestore(&tags->lock, flags);
2693}
2694
Ming Leic3b4afc2015-06-04 22:25:04 +08002695/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08002696static void blk_mq_exit_hctx(struct request_queue *q,
2697 struct blk_mq_tag_set *set,
2698 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
2699{
Ming Lei364b6182021-05-11 23:22:36 +08002700 struct request *flush_rq = hctx->fq->flush_rq;
2701
Ming Lei8ab0b7d2018-01-09 21:28:29 +08002702 if (blk_mq_hw_queue_mapped(hctx))
2703 blk_mq_tag_idle(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002704
Ming Lei364b6182021-05-11 23:22:36 +08002705 blk_mq_clear_flush_rq_mapping(set->tags[hctx_idx],
2706 set->queue_depth, flush_rq);
Ming Leif70ced02014-09-25 23:23:47 +08002707 if (set->ops->exit_request)
Ming Lei364b6182021-05-11 23:22:36 +08002708 set->ops->exit_request(set, flush_rq, hctx_idx);
Ming Leif70ced02014-09-25 23:23:47 +08002709
Ming Lei08e98fc2014-09-25 23:23:38 +08002710 if (set->ops->exit_hctx)
2711 set->ops->exit_hctx(hctx, hctx_idx);
2712
Thomas Gleixner9467f852016-09-22 08:05:17 -06002713 blk_mq_remove_cpuhp(hctx);
Ming Lei2f8f1332019-04-30 09:52:27 +08002714
2715 spin_lock(&q->unused_hctx_lock);
2716 list_add(&hctx->hctx_list, &q->unused_hctx_list);
2717 spin_unlock(&q->unused_hctx_lock);
Ming Lei08e98fc2014-09-25 23:23:38 +08002718}
2719
Ming Lei624dbe42014-05-27 23:35:13 +08002720static void blk_mq_exit_hw_queues(struct request_queue *q,
2721 struct blk_mq_tag_set *set, int nr_queue)
2722{
2723 struct blk_mq_hw_ctx *hctx;
2724 unsigned int i;
2725
2726 queue_for_each_hw_ctx(q, hctx, i) {
2727 if (i == nr_queue)
2728 break;
Jianchao Wang477e19d2018-10-12 18:07:25 +08002729 blk_mq_debugfs_unregister_hctx(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002730 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08002731 }
Ming Lei624dbe42014-05-27 23:35:13 +08002732}
2733
Ming Lei7c6c5b72019-04-30 09:52:26 +08002734static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
2735{
2736 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
2737
2738 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
2739 __alignof__(struct blk_mq_hw_ctx)) !=
2740 sizeof(struct blk_mq_hw_ctx));
2741
2742 if (tag_set->flags & BLK_MQ_F_BLOCKING)
2743 hw_ctx_size += sizeof(struct srcu_struct);
2744
2745 return hw_ctx_size;
2746}
2747
Ming Lei08e98fc2014-09-25 23:23:38 +08002748static int blk_mq_init_hctx(struct request_queue *q,
2749 struct blk_mq_tag_set *set,
2750 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
2751{
Ming Lei7c6c5b72019-04-30 09:52:26 +08002752 hctx->queue_num = hctx_idx;
Ming Lei08e98fc2014-09-25 23:23:38 +08002753
Ming Leibf0beec2020-05-29 15:53:15 +02002754 if (!(hctx->flags & BLK_MQ_F_STACKING))
2755 cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2756 &hctx->cpuhp_online);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002757 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
2758
2759 hctx->tags = set->tags[hctx_idx];
2760
2761 if (set->ops->init_hctx &&
2762 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
2763 goto unregister_cpu_notifier;
2764
2765 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
2766 hctx->numa_node))
2767 goto exit_hctx;
2768 return 0;
2769
2770 exit_hctx:
2771 if (set->ops->exit_hctx)
2772 set->ops->exit_hctx(hctx, hctx_idx);
2773 unregister_cpu_notifier:
2774 blk_mq_remove_cpuhp(hctx);
2775 return -1;
2776}
2777
2778static struct blk_mq_hw_ctx *
2779blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
2780 int node)
2781{
2782 struct blk_mq_hw_ctx *hctx;
2783 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
2784
2785 hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node);
2786 if (!hctx)
2787 goto fail_alloc_hctx;
2788
2789 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
2790 goto free_hctx;
2791
2792 atomic_set(&hctx->nr_active, 0);
Ming Lei08e98fc2014-09-25 23:23:38 +08002793 if (node == NUMA_NO_NODE)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002794 node = set->numa_node;
2795 hctx->numa_node = node;
Ming Lei08e98fc2014-09-25 23:23:38 +08002796
Jens Axboe9f993732017-04-10 09:54:54 -06002797 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08002798 spin_lock_init(&hctx->lock);
2799 INIT_LIST_HEAD(&hctx->dispatch);
2800 hctx->queue = q;
Ming Lei51db1c32020-08-19 23:20:19 +08002801 hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08002802
Ming Lei2f8f1332019-04-30 09:52:27 +08002803 INIT_LIST_HEAD(&hctx->hctx_list);
2804
Ming Lei08e98fc2014-09-25 23:23:38 +08002805 /*
2806 * Allocate space for all possible cpus to avoid allocation at
2807 * runtime
2808 */
Johannes Thumshirnd904bfa2017-11-15 17:32:33 -08002809 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
Ming Lei7c6c5b72019-04-30 09:52:26 +08002810 gfp, node);
Ming Lei08e98fc2014-09-25 23:23:38 +08002811 if (!hctx->ctxs)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002812 goto free_cpumask;
Ming Lei08e98fc2014-09-25 23:23:38 +08002813
Jianchao Wang5b202852018-10-12 18:07:26 +08002814 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
Ming Leic548e622021-01-22 10:33:08 +08002815 gfp, node, false, false))
Ming Lei08e98fc2014-09-25 23:23:38 +08002816 goto free_ctxs;
Ming Lei08e98fc2014-09-25 23:23:38 +08002817 hctx->nr_ctx = 0;
2818
Ming Lei5815839b2018-06-25 19:31:47 +08002819 spin_lock_init(&hctx->dispatch_wait_lock);
Jens Axboeeb619fd2017-11-09 08:32:43 -07002820 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
2821 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
2822
Guoqing Jiang754a1572020-03-09 22:41:37 +01002823 hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp);
Ming Leif70ced02014-09-25 23:23:47 +08002824 if (!hctx->fq)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002825 goto free_bitmap;
Ming Leif70ced02014-09-25 23:23:47 +08002826
Bart Van Assche6a83e742016-11-02 10:09:51 -06002827 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -08002828 init_srcu_struct(hctx->srcu);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002829 blk_mq_hctx_kobj_init(hctx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06002830
Ming Lei7c6c5b72019-04-30 09:52:26 +08002831 return hctx;
Ming Lei08e98fc2014-09-25 23:23:38 +08002832
2833 free_bitmap:
Omar Sandoval88459642016-09-17 08:38:44 -06002834 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08002835 free_ctxs:
2836 kfree(hctx->ctxs);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002837 free_cpumask:
2838 free_cpumask_var(hctx->cpumask);
2839 free_hctx:
2840 kfree(hctx);
2841 fail_alloc_hctx:
2842 return NULL;
Ming Lei08e98fc2014-09-25 23:23:38 +08002843}
2844
Jens Axboe320ae512013-10-24 09:20:05 +01002845static void blk_mq_init_cpu_queues(struct request_queue *q,
2846 unsigned int nr_hw_queues)
2847{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002848 struct blk_mq_tag_set *set = q->tag_set;
2849 unsigned int i, j;
Jens Axboe320ae512013-10-24 09:20:05 +01002850
2851 for_each_possible_cpu(i) {
2852 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
2853 struct blk_mq_hw_ctx *hctx;
Ming Leic16d6b52018-12-17 08:44:05 -07002854 int k;
Jens Axboe320ae512013-10-24 09:20:05 +01002855
Jens Axboe320ae512013-10-24 09:20:05 +01002856 __ctx->cpu = i;
2857 spin_lock_init(&__ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002858 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
2859 INIT_LIST_HEAD(&__ctx->rq_lists[k]);
2860
Jens Axboe320ae512013-10-24 09:20:05 +01002861 __ctx->queue = q;
2862
Jens Axboe320ae512013-10-24 09:20:05 +01002863 /*
2864 * Set local node, IFF we have more than one hw queue. If
2865 * not, we remain on the home node of the device
2866 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06002867 for (j = 0; j < set->nr_maps; j++) {
2868 hctx = blk_mq_map_queue_type(q, j, i);
2869 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
Xianting Tian576e85c2020-10-19 16:20:47 +08002870 hctx->numa_node = cpu_to_node(i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06002871 }
Jens Axboe320ae512013-10-24 09:20:05 +01002872 }
2873}
2874
Weiping Zhang03b63b02020-05-07 21:04:22 +08002875static bool __blk_mq_alloc_map_and_request(struct blk_mq_tag_set *set,
2876 int hctx_idx)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002877{
John Garry1c0706a2020-08-19 23:20:22 +08002878 unsigned int flags = set->flags;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002879 int ret = 0;
2880
2881 set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
John Garry1c0706a2020-08-19 23:20:22 +08002882 set->queue_depth, set->reserved_tags, flags);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002883 if (!set->tags[hctx_idx])
2884 return false;
2885
2886 ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
2887 set->queue_depth);
2888 if (!ret)
2889 return true;
2890
John Garry1c0706a2020-08-19 23:20:22 +08002891 blk_mq_free_rq_map(set->tags[hctx_idx], flags);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002892 set->tags[hctx_idx] = NULL;
2893 return false;
2894}
2895
2896static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
2897 unsigned int hctx_idx)
2898{
John Garry1c0706a2020-08-19 23:20:22 +08002899 unsigned int flags = set->flags;
2900
Dan Carpenter4e6db0f2018-11-29 13:56:54 +03002901 if (set->tags && set->tags[hctx_idx]) {
Jens Axboebd166ef2017-01-17 06:03:22 -07002902 blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
John Garry1c0706a2020-08-19 23:20:22 +08002903 blk_mq_free_rq_map(set->tags[hctx_idx], flags);
Jens Axboebd166ef2017-01-17 06:03:22 -07002904 set->tags[hctx_idx] = NULL;
2905 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002906}
2907
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002908static void blk_mq_map_swqueue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01002909{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002910 unsigned int i, j, hctx_idx;
Jens Axboe320ae512013-10-24 09:20:05 +01002911 struct blk_mq_hw_ctx *hctx;
2912 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08002913 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002914
2915 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06002916 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01002917 hctx->nr_ctx = 0;
huhaid416c922018-05-18 08:32:30 -06002918 hctx->dispatch_from = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002919 }
2920
2921 /*
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002922 * Map software to hardware queues.
Ming Lei4412efe2018-04-25 04:01:44 +08002923 *
2924 * If the cpu isn't present, the cpu is mapped to first hctx.
Jens Axboe320ae512013-10-24 09:20:05 +01002925 */
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08002926 for_each_possible_cpu(i) {
Ming Lei4412efe2018-04-25 04:01:44 +08002927
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01002928 ctx = per_cpu_ptr(q->queue_ctx, i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06002929 for (j = 0; j < set->nr_maps; j++) {
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002930 if (!set->map[j].nr_queues) {
2931 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2932 HCTX_TYPE_DEFAULT, i);
Ming Leie5edd5f2018-12-18 01:28:56 +08002933 continue;
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002934 }
Ming Leifd689872020-05-07 21:04:08 +08002935 hctx_idx = set->map[j].mq_map[i];
2936 /* unmapped hw queue can be remapped after CPU topo changed */
2937 if (!set->tags[hctx_idx] &&
Weiping Zhang03b63b02020-05-07 21:04:22 +08002938 !__blk_mq_alloc_map_and_request(set, hctx_idx)) {
Ming Leifd689872020-05-07 21:04:08 +08002939 /*
2940 * If tags initialization fail for some hctx,
2941 * that hctx won't be brought online. In this
2942 * case, remap the current ctx to hctx[0] which
2943 * is guaranteed to always have tags allocated
2944 */
2945 set->map[j].mq_map[i] = 0;
2946 }
Ming Leie5edd5f2018-12-18 01:28:56 +08002947
Jens Axboeb3c661b2018-10-30 10:36:06 -06002948 hctx = blk_mq_map_queue_type(q, j, i);
Jianchao Wang8ccdf4a2019-01-24 18:25:32 +08002949 ctx->hctxs[j] = hctx;
Jens Axboeb3c661b2018-10-30 10:36:06 -06002950 /*
2951 * If the CPU is already set in the mask, then we've
2952 * mapped this one already. This can happen if
2953 * devices share queues across queue maps.
2954 */
2955 if (cpumask_test_cpu(i, hctx->cpumask))
2956 continue;
2957
2958 cpumask_set_cpu(i, hctx->cpumask);
2959 hctx->type = j;
2960 ctx->index_hw[hctx->type] = hctx->nr_ctx;
2961 hctx->ctxs[hctx->nr_ctx++] = ctx;
2962
2963 /*
2964 * If the nr_ctx type overflows, we have exceeded the
2965 * amount of sw queues we can support.
2966 */
2967 BUG_ON(!hctx->nr_ctx);
2968 }
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002969
2970 for (; j < HCTX_MAX_TYPES; j++)
2971 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2972 HCTX_TYPE_DEFAULT, i);
Jens Axboe320ae512013-10-24 09:20:05 +01002973 }
Jens Axboe506e9312014-05-07 10:26:44 -06002974
2975 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei4412efe2018-04-25 04:01:44 +08002976 /*
2977 * If no software queues are mapped to this hardware queue,
2978 * disable it and free the request entries.
2979 */
2980 if (!hctx->nr_ctx) {
2981 /* Never unmap queue 0. We need it as a
2982 * fallback in case of a new remap fails
2983 * allocation
2984 */
2985 if (i && set->tags[i])
2986 blk_mq_free_map_and_requests(set, i);
2987
2988 hctx->tags = NULL;
2989 continue;
2990 }
Jens Axboe484b4062014-05-21 14:01:15 -06002991
Ming Lei2a34c082015-04-21 10:00:20 +08002992 hctx->tags = set->tags[i];
2993 WARN_ON(!hctx->tags);
2994
Jens Axboe484b4062014-05-21 14:01:15 -06002995 /*
Chong Yuan889fa312015-04-15 11:39:29 -06002996 * Set the map size to the number of mapped software queues.
2997 * This is more accurate and more efficient than looping
2998 * over all possibly mapped software queues.
2999 */
Omar Sandoval88459642016-09-17 08:38:44 -06003000 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
Chong Yuan889fa312015-04-15 11:39:29 -06003001
3002 /*
Jens Axboe484b4062014-05-21 14:01:15 -06003003 * Initialize batch roundrobin counts
3004 */
Ming Leif82ddf12018-04-08 17:48:10 +08003005 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06003006 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
3007 }
Jens Axboe320ae512013-10-24 09:20:05 +01003008}
3009
Jens Axboe8e8320c2017-06-20 17:56:13 -06003010/*
3011 * Caller needs to ensure that we're either frozen/quiesced, or that
3012 * the queue isn't live yet.
3013 */
Jeff Moyer2404e602015-11-03 10:40:06 -05003014static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06003015{
3016 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06003017 int i;
3018
Jeff Moyer2404e602015-11-03 10:40:06 -05003019 queue_for_each_hw_ctx(q, hctx, i) {
Yu Kuai454bb672021-07-31 14:21:30 +08003020 if (shared) {
Ming Lei51db1c32020-08-19 23:20:19 +08003021 hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
Yu Kuai454bb672021-07-31 14:21:30 +08003022 } else {
3023 blk_mq_tag_idle(hctx);
Ming Lei51db1c32020-08-19 23:20:19 +08003024 hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
Yu Kuai454bb672021-07-31 14:21:30 +08003025 }
Jeff Moyer2404e602015-11-03 10:40:06 -05003026 }
3027}
3028
Hannes Reinecke655ac302020-08-19 23:20:20 +08003029static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set,
3030 bool shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05003031{
3032 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06003033
Bart Van Assche705cda92017-04-07 11:16:49 -07003034 lockdep_assert_held(&set->tag_list_lock);
3035
Jens Axboe0d2602c2014-05-13 15:10:52 -06003036 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3037 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05003038 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003039 blk_mq_unfreeze_queue(q);
3040 }
3041}
3042
3043static void blk_mq_del_queue_tag_set(struct request_queue *q)
3044{
3045 struct blk_mq_tag_set *set = q->tag_set;
3046
Jens Axboe0d2602c2014-05-13 15:10:52 -06003047 mutex_lock(&set->tag_list_lock);
Daniel Wagner08c875c2020-07-28 15:29:51 +02003048 list_del(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05003049 if (list_is_singular(&set->tag_list)) {
3050 /* just transitioned to unshared */
Ming Lei51db1c32020-08-19 23:20:19 +08003051 set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05003052 /* update existing queue */
Hannes Reinecke655ac302020-08-19 23:20:20 +08003053 blk_mq_update_tag_set_shared(set, false);
Jeff Moyer2404e602015-11-03 10:40:06 -05003054 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06003055 mutex_unlock(&set->tag_list_lock);
Roman Pena347c7a2018-06-10 22:38:24 +02003056 INIT_LIST_HEAD(&q->tag_set_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003057}
3058
3059static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
3060 struct request_queue *q)
3061{
Jens Axboe0d2602c2014-05-13 15:10:52 -06003062 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05003063
Jens Axboeff821d22017-11-10 22:05:12 -07003064 /*
3065 * Check to see if we're transitioning to shared (from 1 to 2 queues).
3066 */
3067 if (!list_empty(&set->tag_list) &&
Ming Lei51db1c32020-08-19 23:20:19 +08003068 !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
3069 set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05003070 /* update existing queue */
Hannes Reinecke655ac302020-08-19 23:20:20 +08003071 blk_mq_update_tag_set_shared(set, true);
Jeff Moyer2404e602015-11-03 10:40:06 -05003072 }
Ming Lei51db1c32020-08-19 23:20:19 +08003073 if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
Jeff Moyer2404e602015-11-03 10:40:06 -05003074 queue_set_hctx_shared(q, true);
Daniel Wagner08c875c2020-07-28 15:29:51 +02003075 list_add_tail(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05003076
Jens Axboe0d2602c2014-05-13 15:10:52 -06003077 mutex_unlock(&set->tag_list_lock);
3078}
3079
Ming Lei1db49092018-11-20 09:44:35 +08003080/* All allocations will be freed in release handler of q->mq_kobj */
3081static int blk_mq_alloc_ctxs(struct request_queue *q)
3082{
3083 struct blk_mq_ctxs *ctxs;
3084 int cpu;
3085
3086 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
3087 if (!ctxs)
3088 return -ENOMEM;
3089
3090 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
3091 if (!ctxs->queue_ctx)
3092 goto fail;
3093
3094 for_each_possible_cpu(cpu) {
3095 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
3096 ctx->ctxs = ctxs;
3097 }
3098
3099 q->mq_kobj = &ctxs->kobj;
3100 q->queue_ctx = ctxs->queue_ctx;
3101
3102 return 0;
3103 fail:
3104 kfree(ctxs);
3105 return -ENOMEM;
3106}
3107
Ming Leie09aae7e2015-01-29 20:17:27 +08003108/*
3109 * It is the actual release handler for mq, but we do it from
3110 * request queue's release handler for avoiding use-after-free
3111 * and headache because q->mq_kobj shouldn't have been introduced,
3112 * but we can't group ctx/kctx kobj without it.
3113 */
3114void blk_mq_release(struct request_queue *q)
3115{
Ming Lei2f8f1332019-04-30 09:52:27 +08003116 struct blk_mq_hw_ctx *hctx, *next;
3117 int i;
Ming Leie09aae7e2015-01-29 20:17:27 +08003118
Ming Lei2f8f1332019-04-30 09:52:27 +08003119 queue_for_each_hw_ctx(q, hctx, i)
3120 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
3121
3122 /* all hctx are in .unused_hctx_list now */
3123 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
3124 list_del_init(&hctx->hctx_list);
Ming Lei6c8b2322017-02-22 18:14:01 +08003125 kobject_put(&hctx->kobj);
Ming Leic3b4afc2015-06-04 22:25:04 +08003126 }
Ming Leie09aae7e2015-01-29 20:17:27 +08003127
3128 kfree(q->queue_hw_ctx);
3129
Ming Lei7ea5fe32017-02-22 18:14:00 +08003130 /*
3131 * release .mq_kobj and sw queue's kobject now because
3132 * both share lifetime with request queue.
3133 */
3134 blk_mq_sysfs_deinit(q);
Ming Leie09aae7e2015-01-29 20:17:27 +08003135}
3136
Christoph Hellwig5ec780a2021-06-24 10:10:12 +02003137static struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set,
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003138 void *queuedata)
Jens Axboe320ae512013-10-24 09:20:05 +01003139{
Christoph Hellwig26a97502021-06-02 09:53:17 +03003140 struct request_queue *q;
3141 int ret;
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003142
Christoph Hellwig26a97502021-06-02 09:53:17 +03003143 q = blk_alloc_queue(set->numa_node);
3144 if (!q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003145 return ERR_PTR(-ENOMEM);
Christoph Hellwig26a97502021-06-02 09:53:17 +03003146 q->queuedata = queuedata;
3147 ret = blk_mq_init_allocated_queue(set, q);
3148 if (ret) {
3149 blk_cleanup_queue(q);
3150 return ERR_PTR(ret);
3151 }
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003152 return q;
3153}
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003154
3155struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
3156{
3157 return blk_mq_init_queue_data(set, NULL);
3158}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003159EXPORT_SYMBOL(blk_mq_init_queue);
3160
Christoph Hellwig4dcc48742021-08-16 15:19:05 +02003161struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set, void *queuedata,
3162 struct lock_class_key *lkclass)
Jens Axboe9316a9e2018-10-15 08:40:37 -06003163{
3164 struct request_queue *q;
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003165 struct gendisk *disk;
Jens Axboe9316a9e2018-10-15 08:40:37 -06003166
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003167 q = blk_mq_init_queue_data(set, queuedata);
3168 if (IS_ERR(q))
3169 return ERR_CAST(q);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003170
Christoph Hellwig4a1fa412021-08-16 15:19:08 +02003171 disk = __alloc_disk_node(q, set->numa_node, lkclass);
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003172 if (!disk) {
3173 blk_cleanup_queue(q);
3174 return ERR_PTR(-ENOMEM);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003175 }
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003176 return disk;
Jens Axboe9316a9e2018-10-15 08:40:37 -06003177}
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003178EXPORT_SYMBOL(__blk_mq_alloc_disk);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003179
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003180static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
3181 struct blk_mq_tag_set *set, struct request_queue *q,
3182 int hctx_idx, int node)
3183{
Ming Lei2f8f1332019-04-30 09:52:27 +08003184 struct blk_mq_hw_ctx *hctx = NULL, *tmp;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003185
Ming Lei2f8f1332019-04-30 09:52:27 +08003186 /* reuse dead hctx first */
3187 spin_lock(&q->unused_hctx_lock);
3188 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
3189 if (tmp->numa_node == node) {
3190 hctx = tmp;
3191 break;
3192 }
3193 }
3194 if (hctx)
3195 list_del_init(&hctx->hctx_list);
3196 spin_unlock(&q->unused_hctx_lock);
3197
3198 if (!hctx)
3199 hctx = blk_mq_alloc_hctx(q, set, node);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003200 if (!hctx)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003201 goto fail;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003202
Ming Lei7c6c5b72019-04-30 09:52:26 +08003203 if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
3204 goto free_hctx;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003205
3206 return hctx;
Ming Lei7c6c5b72019-04-30 09:52:26 +08003207
3208 free_hctx:
3209 kobject_put(&hctx->kobj);
3210 fail:
3211 return NULL;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003212}
3213
Keith Busch868f2f02015-12-17 17:08:14 -07003214static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
3215 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003216{
Jianchao Wange01ad462018-10-12 18:07:28 +08003217 int i, j, end;
Keith Busch868f2f02015-12-17 17:08:14 -07003218 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01003219
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003220 if (q->nr_hw_queues < set->nr_hw_queues) {
3221 struct blk_mq_hw_ctx **new_hctxs;
3222
3223 new_hctxs = kcalloc_node(set->nr_hw_queues,
3224 sizeof(*new_hctxs), GFP_KERNEL,
3225 set->numa_node);
3226 if (!new_hctxs)
3227 return;
3228 if (hctxs)
3229 memcpy(new_hctxs, hctxs, q->nr_hw_queues *
3230 sizeof(*hctxs));
3231 q->queue_hw_ctx = new_hctxs;
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003232 kfree(hctxs);
3233 hctxs = new_hctxs;
3234 }
3235
Ming Leifb350e02018-01-06 16:27:40 +08003236 /* protect against switching io scheduler */
3237 mutex_lock(&q->sysfs_lock);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003238 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003239 int node;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003240 struct blk_mq_hw_ctx *hctx;
Keith Busch868f2f02015-12-17 17:08:14 -07003241
Dongli Zhang7d76f852019-02-27 21:35:01 +08003242 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003243 /*
3244 * If the hw queue has been mapped to another numa node,
3245 * we need to realloc the hctx. If allocation fails, fallback
3246 * to use the previous one.
3247 */
3248 if (hctxs[i] && (hctxs[i]->numa_node == node))
3249 continue;
Jens Axboe320ae512013-10-24 09:20:05 +01003250
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003251 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
3252 if (hctx) {
Ming Lei2f8f1332019-04-30 09:52:27 +08003253 if (hctxs[i])
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003254 blk_mq_exit_hctx(q, set, hctxs[i], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003255 hctxs[i] = hctx;
3256 } else {
3257 if (hctxs[i])
3258 pr_warn("Allocate new hctx on node %d fails,\
3259 fallback to previous one on node %d\n",
3260 node, hctxs[i]->numa_node);
3261 else
3262 break;
Keith Busch868f2f02015-12-17 17:08:14 -07003263 }
Jens Axboe320ae512013-10-24 09:20:05 +01003264 }
Jianchao Wange01ad462018-10-12 18:07:28 +08003265 /*
3266 * Increasing nr_hw_queues fails. Free the newly allocated
3267 * hctxs and keep the previous q->nr_hw_queues.
3268 */
3269 if (i != set->nr_hw_queues) {
3270 j = q->nr_hw_queues;
3271 end = i;
3272 } else {
3273 j = i;
3274 end = q->nr_hw_queues;
3275 q->nr_hw_queues = set->nr_hw_queues;
3276 }
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003277
Jianchao Wange01ad462018-10-12 18:07:28 +08003278 for (; j < end; j++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003279 struct blk_mq_hw_ctx *hctx = hctxs[j];
3280
3281 if (hctx) {
Jens Axboecc71a6f2017-01-11 14:29:56 -07003282 if (hctx->tags)
3283 blk_mq_free_map_and_requests(set, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003284 blk_mq_exit_hctx(q, set, hctx, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003285 hctxs[j] = NULL;
Keith Busch868f2f02015-12-17 17:08:14 -07003286 }
3287 }
Ming Leifb350e02018-01-06 16:27:40 +08003288 mutex_unlock(&q->sysfs_lock);
Keith Busch868f2f02015-12-17 17:08:14 -07003289}
3290
Christoph Hellwig26a97502021-06-02 09:53:17 +03003291int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
3292 struct request_queue *q)
Keith Busch868f2f02015-12-17 17:08:14 -07003293{
Ming Lei66841672016-02-12 15:27:00 +08003294 /* mark the queue as mq asap */
3295 q->mq_ops = set->ops;
3296
Omar Sandoval34dbad52017-03-21 08:56:08 -07003297 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
Stephen Bates720b8cc2017-04-07 06:24:03 -06003298 blk_mq_poll_stats_bkt,
3299 BLK_MQ_POLL_STATS_BKTS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -07003300 if (!q->poll_cb)
3301 goto err_exit;
3302
Ming Lei1db49092018-11-20 09:44:35 +08003303 if (blk_mq_alloc_ctxs(q))
Jes Sorensen41de54c2019-04-19 16:35:44 -04003304 goto err_poll;
Keith Busch868f2f02015-12-17 17:08:14 -07003305
Ming Lei737f98c2017-02-22 18:13:59 +08003306 /* init q->mq_kobj and sw queues' kobjects */
3307 blk_mq_sysfs_init(q);
3308
Ming Lei2f8f1332019-04-30 09:52:27 +08003309 INIT_LIST_HEAD(&q->unused_hctx_list);
3310 spin_lock_init(&q->unused_hctx_lock);
3311
Keith Busch868f2f02015-12-17 17:08:14 -07003312 blk_mq_realloc_hw_ctxs(set, q);
3313 if (!q->nr_hw_queues)
3314 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01003315
Christoph Hellwig287922e2015-10-30 20:57:30 +08003316 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08003317 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01003318
Jens Axboea8908932018-10-16 14:23:06 -06003319 q->tag_set = set;
Jens Axboe320ae512013-10-24 09:20:05 +01003320
Jens Axboe94eddfb2013-11-19 09:25:07 -07003321 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Ming Leicd191812018-12-18 12:15:29 +08003322 if (set->nr_maps > HCTX_TYPE_POLL &&
3323 set->map[HCTX_TYPE_POLL].nr_queues)
Christoph Hellwig6544d222018-12-02 17:46:28 +01003324 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
Jens Axboe320ae512013-10-24 09:20:05 +01003325
Mike Snitzer28494502016-09-14 13:28:30 -04003326 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06003327 INIT_LIST_HEAD(&q->requeue_list);
3328 spin_lock_init(&q->requeue_lock);
3329
Jens Axboeeba71762014-05-20 15:17:27 -06003330 q->nr_requests = set->queue_depth;
3331
Jens Axboe64f1c212016-11-14 13:03:03 -07003332 /*
3333 * Default to classic polling
3334 */
Yufen Yu29ece8b2019-03-18 22:44:41 +08003335 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
Jens Axboe64f1c212016-11-14 13:03:03 -07003336
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003337 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003338 blk_mq_add_queue_tag_set(set, q);
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003339 blk_mq_map_swqueue(q);
Christoph Hellwig26a97502021-06-02 09:53:17 +03003340 return 0;
Christoph Hellwig18741982014-02-10 09:29:00 -07003341
Jens Axboe320ae512013-10-24 09:20:05 +01003342err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07003343 kfree(q->queue_hw_ctx);
zhengbin73d9c8d2019-07-23 22:10:42 +08003344 q->nr_hw_queues = 0;
Ming Lei1db49092018-11-20 09:44:35 +08003345 blk_mq_sysfs_deinit(q);
Jes Sorensen41de54c2019-04-19 16:35:44 -04003346err_poll:
3347 blk_stat_free_callback(q->poll_cb);
3348 q->poll_cb = NULL;
Ming Linc7de5722016-05-25 23:23:27 -07003349err_exit:
3350 q->mq_ops = NULL;
Christoph Hellwig26a97502021-06-02 09:53:17 +03003351 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01003352}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003353EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01003354
Ming Leic7e2d942019-04-30 09:52:25 +08003355/* tags can _not_ be used after returning from blk_mq_exit_queue */
3356void blk_mq_exit_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01003357{
Bart Van Assche630ef622021-05-13 10:15:29 -07003358 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01003359
Bart Van Assche630ef622021-05-13 10:15:29 -07003360 /* Checks hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED. */
Ming Lei624dbe42014-05-27 23:35:13 +08003361 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
Bart Van Assche630ef622021-05-13 10:15:29 -07003362 /* May clear BLK_MQ_F_TAG_QUEUE_SHARED in hctx->flags. */
3363 blk_mq_del_queue_tag_set(q);
Jens Axboe320ae512013-10-24 09:20:05 +01003364}
Jens Axboe320ae512013-10-24 09:20:05 +01003365
Jens Axboea5164402014-09-10 09:02:03 -06003366static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
3367{
3368 int i;
3369
Xianting Tian8229cca2020-09-26 10:39:47 +08003370 for (i = 0; i < set->nr_hw_queues; i++) {
Weiping Zhang03b63b02020-05-07 21:04:22 +08003371 if (!__blk_mq_alloc_map_and_request(set, i))
Jens Axboea5164402014-09-10 09:02:03 -06003372 goto out_unwind;
Xianting Tian8229cca2020-09-26 10:39:47 +08003373 cond_resched();
3374 }
Jens Axboea5164402014-09-10 09:02:03 -06003375
3376 return 0;
3377
3378out_unwind:
3379 while (--i >= 0)
Weiping Zhang2e194422020-05-07 21:03:39 +08003380 blk_mq_free_map_and_requests(set, i);
Jens Axboea5164402014-09-10 09:02:03 -06003381
Jens Axboea5164402014-09-10 09:02:03 -06003382 return -ENOMEM;
3383}
3384
3385/*
3386 * Allocate the request maps associated with this tag_set. Note that this
3387 * may reduce the depth asked for, if memory is tight. set->queue_depth
3388 * will be updated to reflect the allocated depth.
3389 */
Weiping Zhang79fab522020-05-07 21:04:42 +08003390static int blk_mq_alloc_map_and_requests(struct blk_mq_tag_set *set)
Jens Axboea5164402014-09-10 09:02:03 -06003391{
3392 unsigned int depth;
3393 int err;
3394
3395 depth = set->queue_depth;
3396 do {
3397 err = __blk_mq_alloc_rq_maps(set);
3398 if (!err)
3399 break;
3400
3401 set->queue_depth >>= 1;
3402 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
3403 err = -ENOMEM;
3404 break;
3405 }
3406 } while (set->queue_depth);
3407
3408 if (!set->queue_depth || err) {
3409 pr_err("blk-mq: failed to allocate request map\n");
3410 return -ENOMEM;
3411 }
3412
3413 if (depth != set->queue_depth)
3414 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
3415 depth, set->queue_depth);
3416
3417 return 0;
3418}
3419
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003420static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
3421{
Bart Van Assche6e66b492020-03-09 21:26:17 -07003422 /*
3423 * blk_mq_map_queues() and multiple .map_queues() implementations
3424 * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the
3425 * number of hardware queues.
3426 */
3427 if (set->nr_maps == 1)
3428 set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues;
3429
Ming Lei59388702018-12-07 11:03:53 +08003430 if (set->ops->map_queues && !is_kdump_kernel()) {
Jens Axboeb3c661b2018-10-30 10:36:06 -06003431 int i;
3432
Ming Lei7d4901a2018-01-06 16:27:39 +08003433 /*
3434 * transport .map_queues is usually done in the following
3435 * way:
3436 *
3437 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
3438 * mask = get_cpu_mask(queue)
3439 * for_each_cpu(cpu, mask)
Jens Axboeb3c661b2018-10-30 10:36:06 -06003440 * set->map[x].mq_map[cpu] = queue;
Ming Lei7d4901a2018-01-06 16:27:39 +08003441 * }
3442 *
3443 * When we need to remap, the table has to be cleared for
3444 * killing stale mapping since one CPU may not be mapped
3445 * to any hw queue.
3446 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06003447 for (i = 0; i < set->nr_maps; i++)
3448 blk_mq_clear_mq_map(&set->map[i]);
Ming Lei7d4901a2018-01-06 16:27:39 +08003449
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003450 return set->ops->map_queues(set);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003451 } else {
3452 BUG_ON(set->nr_maps > 1);
Dongli Zhang7d76f852019-02-27 21:35:01 +08003453 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003454 }
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003455}
3456
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003457static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set,
3458 int cur_nr_hw_queues, int new_nr_hw_queues)
3459{
3460 struct blk_mq_tags **new_tags;
3461
3462 if (cur_nr_hw_queues >= new_nr_hw_queues)
3463 return 0;
3464
3465 new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *),
3466 GFP_KERNEL, set->numa_node);
3467 if (!new_tags)
3468 return -ENOMEM;
3469
3470 if (set->tags)
3471 memcpy(new_tags, set->tags, cur_nr_hw_queues *
3472 sizeof(*set->tags));
3473 kfree(set->tags);
3474 set->tags = new_tags;
3475 set->nr_hw_queues = new_nr_hw_queues;
3476
3477 return 0;
3478}
3479
Minwoo Im91cdf262020-12-05 00:20:53 +09003480static int blk_mq_alloc_tag_set_tags(struct blk_mq_tag_set *set,
3481 int new_nr_hw_queues)
3482{
3483 return blk_mq_realloc_tag_set_tags(set, 0, new_nr_hw_queues);
3484}
3485
Jens Axboea4391c62014-06-05 15:21:56 -06003486/*
3487 * Alloc a tag set to be associated with one or more request queues.
3488 * May fail with EINVAL for various error conditions. May adjust the
Minwoo Imc018c842018-06-30 22:12:41 +09003489 * requested depth down, if it's too large. In that case, the set
Jens Axboea4391c62014-06-05 15:21:56 -06003490 * value will be stored in set->queue_depth.
3491 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003492int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
3493{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003494 int i, ret;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003495
Bart Van Assche205fb5f52014-10-30 14:45:11 +01003496 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
3497
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003498 if (!set->nr_hw_queues)
3499 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06003500 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003501 return -EINVAL;
3502 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
3503 return -EINVAL;
3504
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02003505 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003506 return -EINVAL;
3507
Ming Leide148292017-10-14 17:22:29 +08003508 if (!set->ops->get_budget ^ !set->ops->put_budget)
3509 return -EINVAL;
3510
Jens Axboea4391c62014-06-05 15:21:56 -06003511 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
3512 pr_info("blk-mq: reduced tag depth to %u\n",
3513 BLK_MQ_MAX_DEPTH);
3514 set->queue_depth = BLK_MQ_MAX_DEPTH;
3515 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003516
Jens Axboeb3c661b2018-10-30 10:36:06 -06003517 if (!set->nr_maps)
3518 set->nr_maps = 1;
3519 else if (set->nr_maps > HCTX_MAX_TYPES)
3520 return -EINVAL;
3521
Shaohua Li6637fad2014-11-30 16:00:58 -08003522 /*
3523 * If a crashdump is active, then we are potentially in a very
3524 * memory constrained environment. Limit us to 1 queue and
3525 * 64 tags to prevent using too much memory.
3526 */
3527 if (is_kdump_kernel()) {
3528 set->nr_hw_queues = 1;
Ming Lei59388702018-12-07 11:03:53 +08003529 set->nr_maps = 1;
Shaohua Li6637fad2014-11-30 16:00:58 -08003530 set->queue_depth = min(64U, set->queue_depth);
3531 }
Keith Busch868f2f02015-12-17 17:08:14 -07003532 /*
Jens Axboe392546a2018-10-29 13:25:27 -06003533 * There is no use for more h/w queues than cpus if we just have
3534 * a single map
Keith Busch868f2f02015-12-17 17:08:14 -07003535 */
Jens Axboe392546a2018-10-29 13:25:27 -06003536 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003537 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08003538
Minwoo Im91cdf262020-12-05 00:20:53 +09003539 if (blk_mq_alloc_tag_set_tags(set, set->nr_hw_queues) < 0)
Jens Axboea5164402014-09-10 09:02:03 -06003540 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003541
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003542 ret = -ENOMEM;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003543 for (i = 0; i < set->nr_maps; i++) {
3544 set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
Ming Lei07b35eb2018-12-17 18:42:45 +08003545 sizeof(set->map[i].mq_map[0]),
Jens Axboeb3c661b2018-10-30 10:36:06 -06003546 GFP_KERNEL, set->numa_node);
3547 if (!set->map[i].mq_map)
3548 goto out_free_mq_map;
Ming Lei59388702018-12-07 11:03:53 +08003549 set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003550 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003551
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003552 ret = blk_mq_update_queue_map(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003553 if (ret)
3554 goto out_free_mq_map;
3555
Weiping Zhang79fab522020-05-07 21:04:42 +08003556 ret = blk_mq_alloc_map_and_requests(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003557 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003558 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003559
John Garry32bc15a2020-08-19 23:20:24 +08003560 if (blk_mq_is_sbitmap_shared(set->flags)) {
John Garryf1b49fd2020-08-19 23:20:27 +08003561 atomic_set(&set->active_queues_shared_sbitmap, 0);
3562
John Garry56b680852021-05-13 20:00:57 +08003563 if (blk_mq_init_shared_sbitmap(set)) {
John Garry32bc15a2020-08-19 23:20:24 +08003564 ret = -ENOMEM;
3565 goto out_free_mq_rq_maps;
3566 }
3567 }
3568
Jens Axboe0d2602c2014-05-13 15:10:52 -06003569 mutex_init(&set->tag_list_lock);
3570 INIT_LIST_HEAD(&set->tag_list);
3571
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003572 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003573
John Garry32bc15a2020-08-19 23:20:24 +08003574out_free_mq_rq_maps:
3575 for (i = 0; i < set->nr_hw_queues; i++)
3576 blk_mq_free_map_and_requests(set, i);
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003577out_free_mq_map:
Jens Axboeb3c661b2018-10-30 10:36:06 -06003578 for (i = 0; i < set->nr_maps; i++) {
3579 kfree(set->map[i].mq_map);
3580 set->map[i].mq_map = NULL;
3581 }
Robert Elliott5676e7b62014-09-02 11:38:44 -05003582 kfree(set->tags);
3583 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003584 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003585}
3586EXPORT_SYMBOL(blk_mq_alloc_tag_set);
3587
Christoph Hellwigcdb14e02021-06-02 09:53:16 +03003588/* allocate and initialize a tagset for a simple single-queue device */
3589int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set,
3590 const struct blk_mq_ops *ops, unsigned int queue_depth,
3591 unsigned int set_flags)
3592{
3593 memset(set, 0, sizeof(*set));
3594 set->ops = ops;
3595 set->nr_hw_queues = 1;
3596 set->nr_maps = 1;
3597 set->queue_depth = queue_depth;
3598 set->numa_node = NUMA_NO_NODE;
3599 set->flags = set_flags;
3600 return blk_mq_alloc_tag_set(set);
3601}
3602EXPORT_SYMBOL_GPL(blk_mq_alloc_sq_tag_set);
3603
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003604void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
3605{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003606 int i, j;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003607
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003608 for (i = 0; i < set->nr_hw_queues; i++)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003609 blk_mq_free_map_and_requests(set, i);
Jens Axboe484b4062014-05-21 14:01:15 -06003610
John Garry32bc15a2020-08-19 23:20:24 +08003611 if (blk_mq_is_sbitmap_shared(set->flags))
3612 blk_mq_exit_shared_sbitmap(set);
3613
Jens Axboeb3c661b2018-10-30 10:36:06 -06003614 for (j = 0; j < set->nr_maps; j++) {
3615 kfree(set->map[j].mq_map);
3616 set->map[j].mq_map = NULL;
3617 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003618
Ming Lei981bd182014-04-24 00:07:34 +08003619 kfree(set->tags);
Robert Elliott5676e7b62014-09-02 11:38:44 -05003620 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003621}
3622EXPORT_SYMBOL(blk_mq_free_tag_set);
3623
Jens Axboee3a2b3f92014-05-20 11:49:02 -06003624int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
3625{
3626 struct blk_mq_tag_set *set = q->tag_set;
3627 struct blk_mq_hw_ctx *hctx;
3628 int i, ret;
3629
Jens Axboebd166ef2017-01-17 06:03:22 -07003630 if (!set)
Jens Axboee3a2b3f92014-05-20 11:49:02 -06003631 return -EINVAL;
3632
Aleksei Zakharove5fa8142019-02-08 19:14:05 +03003633 if (q->nr_requests == nr)
3634 return 0;
3635
Jens Axboe70f36b62017-01-19 10:59:07 -07003636 blk_mq_freeze_queue(q);
Ming Lei24f5a902018-01-06 16:27:38 +08003637 blk_mq_quiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003638
Jens Axboee3a2b3f92014-05-20 11:49:02 -06003639 ret = 0;
3640 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d4b2016-02-18 14:56:35 -07003641 if (!hctx->tags)
3642 continue;
Jens Axboebd166ef2017-01-17 06:03:22 -07003643 /*
3644 * If we're using an MQ scheduler, just update the scheduler
3645 * queue depth. This is similar to what the old code would do.
3646 */
Jens Axboe70f36b62017-01-19 10:59:07 -07003647 if (!hctx->sched_tags) {
weiping zhangc2e82a22017-09-22 23:36:28 +08003648 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
Jens Axboe70f36b62017-01-19 10:59:07 -07003649 false);
John Garry32bc15a2020-08-19 23:20:24 +08003650 if (!ret && blk_mq_is_sbitmap_shared(set->flags))
3651 blk_mq_tag_resize_shared_sbitmap(set, nr);
Jens Axboe70f36b62017-01-19 10:59:07 -07003652 } else {
3653 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
3654 nr, true);
John Garryd97e5942021-05-13 20:00:58 +08003655 if (blk_mq_is_sbitmap_shared(set->flags)) {
3656 hctx->sched_tags->bitmap_tags =
3657 &q->sched_bitmap_tags;
3658 hctx->sched_tags->breserved_tags =
3659 &q->sched_breserved_tags;
3660 }
Jens Axboe70f36b62017-01-19 10:59:07 -07003661 }
Jens Axboee3a2b3f92014-05-20 11:49:02 -06003662 if (ret)
3663 break;
Jens Axboe77f1e0a2019-01-18 10:34:16 -07003664 if (q->elevator && q->elevator->type->ops.depth_updated)
3665 q->elevator->type->ops.depth_updated(hctx);
Jens Axboee3a2b3f92014-05-20 11:49:02 -06003666 }
John Garryd97e5942021-05-13 20:00:58 +08003667 if (!ret) {
Jens Axboee3a2b3f92014-05-20 11:49:02 -06003668 q->nr_requests = nr;
John Garryd97e5942021-05-13 20:00:58 +08003669 if (q->elevator && blk_mq_is_sbitmap_shared(set->flags))
3670 sbitmap_queue_resize(&q->sched_bitmap_tags,
3671 nr - set->reserved_tags);
3672 }
Jens Axboee3a2b3f92014-05-20 11:49:02 -06003673
Ming Lei24f5a902018-01-06 16:27:38 +08003674 blk_mq_unquiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003675 blk_mq_unfreeze_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003676
Jens Axboee3a2b3f92014-05-20 11:49:02 -06003677 return ret;
3678}
3679
Jianchao Wangd48ece22018-08-21 15:15:03 +08003680/*
3681 * request_queue and elevator_type pair.
3682 * It is just used by __blk_mq_update_nr_hw_queues to cache
3683 * the elevator_type associated with a request_queue.
3684 */
3685struct blk_mq_qe_pair {
3686 struct list_head node;
3687 struct request_queue *q;
3688 struct elevator_type *type;
3689};
3690
3691/*
3692 * Cache the elevator_type in qe pair list and switch the
3693 * io scheduler to 'none'
3694 */
3695static bool blk_mq_elv_switch_none(struct list_head *head,
3696 struct request_queue *q)
3697{
3698 struct blk_mq_qe_pair *qe;
3699
3700 if (!q->elevator)
3701 return true;
3702
3703 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
3704 if (!qe)
3705 return false;
3706
3707 INIT_LIST_HEAD(&qe->node);
3708 qe->q = q;
3709 qe->type = q->elevator->type;
3710 list_add(&qe->node, head);
3711
3712 mutex_lock(&q->sysfs_lock);
3713 /*
3714 * After elevator_switch_mq, the previous elevator_queue will be
3715 * released by elevator_release. The reference of the io scheduler
3716 * module get by elevator_get will also be put. So we need to get
3717 * a reference of the io scheduler module here to prevent it to be
3718 * removed.
3719 */
3720 __module_get(qe->type->elevator_owner);
3721 elevator_switch_mq(q, NULL);
3722 mutex_unlock(&q->sysfs_lock);
3723
3724 return true;
3725}
3726
3727static void blk_mq_elv_switch_back(struct list_head *head,
3728 struct request_queue *q)
3729{
3730 struct blk_mq_qe_pair *qe;
3731 struct elevator_type *t = NULL;
3732
3733 list_for_each_entry(qe, head, node)
3734 if (qe->q == q) {
3735 t = qe->type;
3736 break;
3737 }
3738
3739 if (!t)
3740 return;
3741
3742 list_del(&qe->node);
3743 kfree(qe);
3744
3745 mutex_lock(&q->sysfs_lock);
3746 elevator_switch_mq(q, t);
3747 mutex_unlock(&q->sysfs_lock);
3748}
3749
Keith Busche4dc2b32017-05-30 14:39:11 -04003750static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
3751 int nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003752{
3753 struct request_queue *q;
Jianchao Wangd48ece22018-08-21 15:15:03 +08003754 LIST_HEAD(head);
Jianchao Wange01ad462018-10-12 18:07:28 +08003755 int prev_nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07003756
Bart Van Assche705cda92017-04-07 11:16:49 -07003757 lockdep_assert_held(&set->tag_list_lock);
3758
Jens Axboe392546a2018-10-29 13:25:27 -06003759 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003760 nr_hw_queues = nr_cpu_ids;
Weiping Zhangfe35ec52020-06-17 14:18:37 +08003761 if (nr_hw_queues < 1)
3762 return;
3763 if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003764 return;
3765
3766 list_for_each_entry(q, &set->tag_list, tag_set_list)
3767 blk_mq_freeze_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +08003768 /*
3769 * Switch IO scheduler to 'none', cleaning up the data associated
3770 * with the previous scheduler. We will switch back once we are done
3771 * updating the new sw to hw queue mappings.
3772 */
3773 list_for_each_entry(q, &set->tag_list, tag_set_list)
3774 if (!blk_mq_elv_switch_none(&head, q))
3775 goto switch_back;
Keith Busch868f2f02015-12-17 17:08:14 -07003776
Jianchao Wang477e19d2018-10-12 18:07:25 +08003777 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3778 blk_mq_debugfs_unregister_hctxs(q);
3779 blk_mq_sysfs_unregister(q);
3780 }
3781
Weiping Zhanga2584e42020-05-07 21:03:56 +08003782 prev_nr_hw_queues = set->nr_hw_queues;
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003783 if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) <
3784 0)
3785 goto reregister;
3786
Keith Busch868f2f02015-12-17 17:08:14 -07003787 set->nr_hw_queues = nr_hw_queues;
Jianchao Wange01ad462018-10-12 18:07:28 +08003788fallback:
Weiping Zhangaa880ad2020-05-13 08:44:05 +08003789 blk_mq_update_queue_map(set);
Keith Busch868f2f02015-12-17 17:08:14 -07003790 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3791 blk_mq_realloc_hw_ctxs(set, q);
Jianchao Wange01ad462018-10-12 18:07:28 +08003792 if (q->nr_hw_queues != set->nr_hw_queues) {
3793 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
3794 nr_hw_queues, prev_nr_hw_queues);
3795 set->nr_hw_queues = prev_nr_hw_queues;
Dongli Zhang7d76f852019-02-27 21:35:01 +08003796 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jianchao Wange01ad462018-10-12 18:07:28 +08003797 goto fallback;
3798 }
Jianchao Wang477e19d2018-10-12 18:07:25 +08003799 blk_mq_map_swqueue(q);
3800 }
3801
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003802reregister:
Jianchao Wang477e19d2018-10-12 18:07:25 +08003803 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3804 blk_mq_sysfs_register(q);
3805 blk_mq_debugfs_register_hctxs(q);
Keith Busch868f2f02015-12-17 17:08:14 -07003806 }
3807
Jianchao Wangd48ece22018-08-21 15:15:03 +08003808switch_back:
3809 list_for_each_entry(q, &set->tag_list, tag_set_list)
3810 blk_mq_elv_switch_back(&head, q);
3811
Keith Busch868f2f02015-12-17 17:08:14 -07003812 list_for_each_entry(q, &set->tag_list, tag_set_list)
3813 blk_mq_unfreeze_queue(q);
3814}
Keith Busche4dc2b32017-05-30 14:39:11 -04003815
3816void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
3817{
3818 mutex_lock(&set->tag_list_lock);
3819 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
3820 mutex_unlock(&set->tag_list_lock);
3821}
Keith Busch868f2f02015-12-17 17:08:14 -07003822EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
3823
Omar Sandoval34dbad52017-03-21 08:56:08 -07003824/* Enable polling stats and return whether they were already enabled. */
3825static bool blk_poll_stats_enable(struct request_queue *q)
3826{
3827 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
Bart Van Assche7dfdbc72018-03-07 17:10:05 -08003828 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
Omar Sandoval34dbad52017-03-21 08:56:08 -07003829 return true;
3830 blk_stat_add_callback(q, q->poll_cb);
3831 return false;
3832}
3833
3834static void blk_mq_poll_stats_start(struct request_queue *q)
3835{
3836 /*
3837 * We don't arm the callback if polling stats are not enabled or the
3838 * callback is already active.
3839 */
3840 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
3841 blk_stat_is_active(q->poll_cb))
3842 return;
3843
3844 blk_stat_activate_msecs(q->poll_cb, 100);
3845}
3846
3847static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
3848{
3849 struct request_queue *q = cb->data;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003850 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -07003851
Stephen Bates720b8cc2017-04-07 06:24:03 -06003852 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
3853 if (cb->stat[bucket].nr_samples)
3854 q->poll_stat[bucket] = cb->stat[bucket];
3855 }
Omar Sandoval34dbad52017-03-21 08:56:08 -07003856}
3857
Jens Axboe64f1c212016-11-14 13:03:03 -07003858static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
Jens Axboe64f1c212016-11-14 13:03:03 -07003859 struct request *rq)
3860{
Jens Axboe64f1c212016-11-14 13:03:03 -07003861 unsigned long ret = 0;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003862 int bucket;
Jens Axboe64f1c212016-11-14 13:03:03 -07003863
3864 /*
3865 * If stats collection isn't on, don't sleep but turn it on for
3866 * future users
3867 */
Omar Sandoval34dbad52017-03-21 08:56:08 -07003868 if (!blk_poll_stats_enable(q))
Jens Axboe64f1c212016-11-14 13:03:03 -07003869 return 0;
3870
3871 /*
Jens Axboe64f1c212016-11-14 13:03:03 -07003872 * As an optimistic guess, use half of the mean service time
3873 * for this type of request. We can (and should) make this smarter.
3874 * For instance, if the completion latencies are tight, we can
3875 * get closer than just half the mean. This is especially
3876 * important on devices where the completion latencies are longer
Stephen Bates720b8cc2017-04-07 06:24:03 -06003877 * than ~10 usec. We do use the stats for the relevant IO size
3878 * if available which does lead to better estimates.
Jens Axboe64f1c212016-11-14 13:03:03 -07003879 */
Stephen Bates720b8cc2017-04-07 06:24:03 -06003880 bucket = blk_mq_poll_stats_bkt(rq);
3881 if (bucket < 0)
3882 return ret;
3883
3884 if (q->poll_stat[bucket].nr_samples)
3885 ret = (q->poll_stat[bucket].mean + 1) / 2;
Jens Axboe64f1c212016-11-14 13:03:03 -07003886
3887 return ret;
3888}
3889
Jens Axboe06426ad2016-11-14 13:01:59 -07003890static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
3891 struct request *rq)
3892{
3893 struct hrtimer_sleeper hs;
3894 enum hrtimer_mode mode;
Jens Axboe64f1c212016-11-14 13:03:03 -07003895 unsigned int nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003896 ktime_t kt;
3897
Jens Axboe76a86f92018-01-10 11:30:56 -07003898 if (rq->rq_flags & RQF_MQ_POLL_SLEPT)
Jens Axboe64f1c212016-11-14 13:03:03 -07003899 return false;
3900
3901 /*
Jens Axboe1052b8a2018-11-26 08:21:49 -07003902 * If we get here, hybrid polling is enabled. Hence poll_nsec can be:
Jens Axboe64f1c212016-11-14 13:03:03 -07003903 *
Jens Axboe64f1c212016-11-14 13:03:03 -07003904 * 0: use half of prev avg
3905 * >0: use this specific value
3906 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07003907 if (q->poll_nsec > 0)
Jens Axboe64f1c212016-11-14 13:03:03 -07003908 nsecs = q->poll_nsec;
3909 else
John Garrycae740a2020-02-26 20:10:15 +08003910 nsecs = blk_mq_poll_nsecs(q, rq);
Jens Axboe64f1c212016-11-14 13:03:03 -07003911
3912 if (!nsecs)
Jens Axboe06426ad2016-11-14 13:01:59 -07003913 return false;
3914
Jens Axboe76a86f92018-01-10 11:30:56 -07003915 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
Jens Axboe06426ad2016-11-14 13:01:59 -07003916
3917 /*
3918 * This will be replaced with the stats tracking code, using
3919 * 'avg_completion_time / 2' as the pre-sleep target.
3920 */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01003921 kt = nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003922
3923 mode = HRTIMER_MODE_REL;
Sebastian Andrzej Siewiordbc16252019-07-26 20:30:50 +02003924 hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07003925 hrtimer_set_expires(&hs.timer, kt);
3926
Jens Axboe06426ad2016-11-14 13:01:59 -07003927 do {
Tejun Heo5a61c362018-01-09 08:29:52 -08003928 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
Jens Axboe06426ad2016-11-14 13:01:59 -07003929 break;
3930 set_current_state(TASK_UNINTERRUPTIBLE);
Thomas Gleixner9dd88132019-07-30 21:16:55 +02003931 hrtimer_sleeper_start_expires(&hs, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07003932 if (hs.task)
3933 io_schedule();
3934 hrtimer_cancel(&hs.timer);
3935 mode = HRTIMER_MODE_ABS;
3936 } while (hs.task && !signal_pending(current));
3937
3938 __set_current_state(TASK_RUNNING);
3939 destroy_hrtimer_on_stack(&hs.timer);
3940 return true;
3941}
3942
Jens Axboe1052b8a2018-11-26 08:21:49 -07003943static bool blk_mq_poll_hybrid(struct request_queue *q,
3944 struct blk_mq_hw_ctx *hctx, blk_qc_t cookie)
Jens Axboebbd7bb72016-11-04 09:34:34 -06003945{
Jens Axboe1052b8a2018-11-26 08:21:49 -07003946 struct request *rq;
3947
Yufen Yu29ece8b2019-03-18 22:44:41 +08003948 if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
Jens Axboe1052b8a2018-11-26 08:21:49 -07003949 return false;
3950
3951 if (!blk_qc_t_is_internal(cookie))
3952 rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
3953 else {
3954 rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
3955 /*
3956 * With scheduling, if the request has completed, we'll
3957 * get a NULL return here, as we clear the sched tag when
3958 * that happens. The request still remains valid, like always,
3959 * so we should be safe with just the NULL check.
3960 */
3961 if (!rq)
3962 return false;
3963 }
3964
John Garrycae740a2020-02-26 20:10:15 +08003965 return blk_mq_poll_hybrid_sleep(q, rq);
Jens Axboe1052b8a2018-11-26 08:21:49 -07003966}
3967
Christoph Hellwig529262d2018-12-02 17:46:26 +01003968/**
3969 * blk_poll - poll for IO completions
3970 * @q: the queue
3971 * @cookie: cookie passed back at IO submission time
3972 * @spin: whether to spin for completions
3973 *
3974 * Description:
3975 * Poll for completions on the passed in queue. Returns number of
3976 * completed entries found. If @spin is true, then blk_poll will continue
3977 * looping until at least one completion is found, unless the task is
3978 * otherwise marked running (or we need to reschedule).
3979 */
3980int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin)
Jens Axboe1052b8a2018-11-26 08:21:49 -07003981{
3982 struct blk_mq_hw_ctx *hctx;
Peter Zijlstra2f064a52021-06-11 10:28:17 +02003983 unsigned int state;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003984
Christoph Hellwig529262d2018-12-02 17:46:26 +01003985 if (!blk_qc_t_valid(cookie) ||
3986 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
Jens Axboe1052b8a2018-11-26 08:21:49 -07003987 return 0;
3988
Christoph Hellwig529262d2018-12-02 17:46:26 +01003989 if (current->plug)
3990 blk_flush_plug_list(current->plug, false);
3991
Jens Axboe1052b8a2018-11-26 08:21:49 -07003992 hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
3993
Jens Axboe06426ad2016-11-14 13:01:59 -07003994 /*
3995 * If we sleep, have the caller restart the poll loop to reset
3996 * the state. Like for the other success return cases, the
3997 * caller is responsible for checking if the IO completed. If
3998 * the IO isn't complete, we'll get called again and will go
Pavel Begunkovf6f371f2020-12-06 14:04:39 +00003999 * straight to the busy poll loop. If specified not to spin,
4000 * we also should not sleep.
Jens Axboe06426ad2016-11-14 13:01:59 -07004001 */
Pavel Begunkovf6f371f2020-12-06 14:04:39 +00004002 if (spin && blk_mq_poll_hybrid(q, hctx, cookie))
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004003 return 1;
Jens Axboe06426ad2016-11-14 13:01:59 -07004004
Jens Axboebbd7bb72016-11-04 09:34:34 -06004005 hctx->poll_considered++;
4006
Peter Zijlstrad6c23bb2021-06-11 10:28:14 +02004007 state = get_current_state();
Jens Axboeaa61bec2018-11-13 21:32:10 -07004008 do {
Jens Axboebbd7bb72016-11-04 09:34:34 -06004009 int ret;
4010
4011 hctx->poll_invoked++;
4012
Jens Axboe97431392018-11-16 09:48:21 -07004013 ret = q->mq_ops->poll(hctx);
Jens Axboebbd7bb72016-11-04 09:34:34 -06004014 if (ret > 0) {
4015 hctx->poll_success++;
Jens Axboe849a3702018-11-16 08:37:34 -07004016 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004017 return ret;
Jens Axboebbd7bb72016-11-04 09:34:34 -06004018 }
4019
4020 if (signal_pending_state(state, current))
Jens Axboe849a3702018-11-16 08:37:34 -07004021 __set_current_state(TASK_RUNNING);
Jens Axboebbd7bb72016-11-04 09:34:34 -06004022
Peter Zijlstrab03fbd42021-06-11 10:28:12 +02004023 if (task_is_running(current))
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004024 return 1;
Jens Axboe0a1b8b82018-11-26 08:24:43 -07004025 if (ret < 0 || !spin)
Jens Axboebbd7bb72016-11-04 09:34:34 -06004026 break;
4027 cpu_relax();
Jens Axboeaa61bec2018-11-13 21:32:10 -07004028 } while (!need_resched());
Jens Axboebbd7bb72016-11-04 09:34:34 -06004029
Nitesh Shetty67b41102018-02-13 21:18:12 +05304030 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004031 return 0;
Jens Axboebbd7bb72016-11-04 09:34:34 -06004032}
Christoph Hellwig529262d2018-12-02 17:46:26 +01004033EXPORT_SYMBOL_GPL(blk_poll);
Jens Axboebbd7bb72016-11-04 09:34:34 -06004034
Jens Axboe9cf2bab2018-10-31 17:01:22 -06004035unsigned int blk_mq_rq_cpu(struct request *rq)
4036{
4037 return rq->mq_ctx->cpu;
4038}
4039EXPORT_SYMBOL(blk_mq_rq_cpu);
4040
Ming Leie03513f52021-11-16 09:43:43 +08004041void blk_mq_cancel_work_sync(struct request_queue *q)
4042{
4043 if (queue_is_mq(q)) {
4044 struct blk_mq_hw_ctx *hctx;
4045 int i;
4046
4047 cancel_delayed_work_sync(&q->requeue_work);
4048
4049 queue_for_each_hw_ctx(q, hctx, i)
4050 cancel_delayed_work_sync(&hctx->run_work);
4051 }
4052}
4053
Jens Axboe320ae512013-10-24 09:20:05 +01004054static int __init blk_mq_init(void)
4055{
Christoph Hellwigc3077b52020-06-11 08:44:41 +02004056 int i;
4057
4058 for_each_possible_cpu(i)
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +01004059 init_llist_head(&per_cpu(blk_cpu_done, i));
Christoph Hellwigc3077b52020-06-11 08:44:41 +02004060 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
4061
4062 cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
4063 "block/softirq:dead", NULL,
4064 blk_softirq_cpu_dead);
Thomas Gleixner9467f852016-09-22 08:05:17 -06004065 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
4066 blk_mq_hctx_notify_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02004067 cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online",
4068 blk_mq_hctx_notify_online,
4069 blk_mq_hctx_notify_offline);
Jens Axboe320ae512013-10-24 09:20:05 +01004070 return 0;
4071}
4072subsys_initcall(blk_mq_init);