blob: 08d5571d95e0324dd6b73202d567a130c43e4539 [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Jens Axboe75bb4622014-05-28 10:15:41 -06002/*
3 * Block multiqueue core code
4 *
5 * Copyright (C) 2013-2014 Jens Axboe
6 * Copyright (C) 2013-2014 Christoph Hellwig
7 */
Jens Axboe320ae512013-10-24 09:20:05 +01008#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/backing-dev.h>
11#include <linux/bio.h>
12#include <linux/blkdev.h>
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"
Jens Axboe320ae512013-10-24 09:20:05 +010043
Christoph Hellwigc3077b52020-06-11 08:44:41 +020044static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
45
Omar Sandoval34dbad52017-03-21 08:56:08 -070046static void blk_mq_poll_stats_start(struct request_queue *q);
47static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);
48
Stephen Bates720b8cc2017-04-07 06:24:03 -060049static int blk_mq_poll_stats_bkt(const struct request *rq)
50{
Hou Tao3d244302019-05-21 15:59:03 +080051 int ddir, sectors, bucket;
Stephen Bates720b8cc2017-04-07 06:24:03 -060052
Jens Axboe99c749a2017-04-21 07:55:42 -060053 ddir = rq_data_dir(rq);
Hou Tao3d244302019-05-21 15:59:03 +080054 sectors = blk_rq_stats_sectors(rq);
Stephen Bates720b8cc2017-04-07 06:24:03 -060055
Hou Tao3d244302019-05-21 15:59:03 +080056 bucket = ddir + 2 * ilog2(sectors);
Stephen Bates720b8cc2017-04-07 06:24:03 -060057
58 if (bucket < 0)
59 return -1;
60 else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
61 return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
62
63 return bucket;
64}
65
Jens Axboe320ae512013-10-24 09:20:05 +010066/*
Yufen Yu85fae292019-03-24 17:57:08 +080067 * Check if any of the ctx, dispatch list or elevator
68 * have pending work in this hardware queue.
Jens Axboe320ae512013-10-24 09:20:05 +010069 */
Jens Axboe79f720a2017-11-10 09:13:21 -070070static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
Jens Axboe320ae512013-10-24 09:20:05 +010071{
Jens Axboe79f720a2017-11-10 09:13:21 -070072 return !list_empty_careful(&hctx->dispatch) ||
73 sbitmap_any_bit_set(&hctx->ctx_map) ||
Jens Axboebd166ef2017-01-17 06:03:22 -070074 blk_mq_sched_has_work(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +010075}
76
77/*
78 * Mark this ctx as having pending work in this hardware queue
79 */
80static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
81 struct blk_mq_ctx *ctx)
82{
Jens Axboef31967f2018-10-29 13:13:29 -060083 const int bit = ctx->index_hw[hctx->type];
84
85 if (!sbitmap_test_bit(&hctx->ctx_map, bit))
86 sbitmap_set_bit(&hctx->ctx_map, bit);
Jens Axboe1429d7c2014-05-19 09:23:55 -060087}
88
89static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
90 struct blk_mq_ctx *ctx)
91{
Jens Axboef31967f2018-10-29 13:13:29 -060092 const int bit = ctx->index_hw[hctx->type];
93
94 sbitmap_clear_bit(&hctx->ctx_map, bit);
Jens Axboe320ae512013-10-24 09:20:05 +010095}
96
Jens Axboef299b7c2017-08-08 17:51:45 -060097struct mq_inflight {
98 struct hd_struct *part;
Pavel Begunkova2e80f62019-09-30 21:55:34 +030099 unsigned int inflight[2];
Jens Axboef299b7c2017-08-08 17:51:45 -0600100};
101
Jens Axboe7baa8572018-11-08 10:24:07 -0700102static bool blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx,
Jens Axboef299b7c2017-08-08 17:51:45 -0600103 struct request *rq, void *priv,
104 bool reserved)
105{
106 struct mq_inflight *mi = priv;
107
Jeffle Xube6f5cf2020-12-02 19:11:45 +0800108 if ((!mi->part->partno || rq->part == mi->part) &&
109 blk_mq_rq_state(rq) == MQ_RQ_IN_FLIGHT)
Pavel Begunkovbb4e6b12019-09-30 21:55:33 +0300110 mi->inflight[rq_data_dir(rq)]++;
Jens Axboe7baa8572018-11-08 10:24:07 -0700111
112 return true;
Jens Axboef299b7c2017-08-08 17:51:45 -0600113}
114
Mikulas Patockae016b782018-12-06 11:41:21 -0500115unsigned int blk_mq_in_flight(struct request_queue *q, struct hd_struct *part)
Jens Axboef299b7c2017-08-08 17:51:45 -0600116{
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300117 struct mq_inflight mi = { .part = part };
Jens Axboef299b7c2017-08-08 17:51:45 -0600118
Jens Axboef299b7c2017-08-08 17:51:45 -0600119 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
Mikulas Patockae016b782018-12-06 11:41:21 -0500120
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300121 return mi.inflight[0] + mi.inflight[1];
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700122}
123
124void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,
125 unsigned int inflight[2])
126{
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300127 struct mq_inflight mi = { .part = part };
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700128
Pavel Begunkovbb4e6b12019-09-30 21:55:33 +0300129 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300130 inflight[0] = mi.inflight[0];
131 inflight[1] = mi.inflight[1];
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700132}
133
Ming Lei1671d522017-03-27 20:06:57 +0800134void blk_freeze_queue_start(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +0800135{
Bob Liu7996a8b2019-05-21 11:25:55 +0800136 mutex_lock(&q->mq_freeze_lock);
137 if (++q->mq_freeze_depth == 1) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400138 percpu_ref_kill(&q->q_usage_counter);
Bob Liu7996a8b2019-05-21 11:25:55 +0800139 mutex_unlock(&q->mq_freeze_lock);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700140 if (queue_is_mq(q))
Ming Lei055f6e12017-11-09 10:49:53 -0800141 blk_mq_run_hw_queues(q, false);
Bob Liu7996a8b2019-05-21 11:25:55 +0800142 } else {
143 mutex_unlock(&q->mq_freeze_lock);
Tejun Heocddd5d12014-08-16 08:02:24 -0400144 }
Tejun Heof3af0202014-11-04 13:52:27 -0500145}
Ming Lei1671d522017-03-27 20:06:57 +0800146EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
Tejun Heof3af0202014-11-04 13:52:27 -0500147
Keith Busch6bae3632017-03-01 14:22:10 -0500148void blk_mq_freeze_queue_wait(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500149{
Dan Williams3ef28e82015-10-21 13:20:12 -0400150 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +0800151}
Keith Busch6bae3632017-03-01 14:22:10 -0500152EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800153
Keith Buschf91328c2017-03-01 14:22:11 -0500154int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
155 unsigned long timeout)
156{
157 return wait_event_timeout(q->mq_freeze_wq,
158 percpu_ref_is_zero(&q->q_usage_counter),
159 timeout);
160}
161EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
Jens Axboe320ae512013-10-24 09:20:05 +0100162
Tejun Heof3af0202014-11-04 13:52:27 -0500163/*
164 * Guarantee no request is in use, so we can change any data structure of
165 * the queue afterward.
166 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400167void blk_freeze_queue(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500168{
Dan Williams3ef28e82015-10-21 13:20:12 -0400169 /*
170 * In the !blk_mq case we are only calling this to kill the
171 * q_usage_counter, otherwise this increases the freeze depth
172 * and waits for it to return to zero. For this reason there is
173 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
174 * exported to drivers as the only user for unfreeze is blk_mq.
175 */
Ming Lei1671d522017-03-27 20:06:57 +0800176 blk_freeze_queue_start(q);
Tejun Heof3af0202014-11-04 13:52:27 -0500177 blk_mq_freeze_queue_wait(q);
178}
Dan Williams3ef28e82015-10-21 13:20:12 -0400179
180void blk_mq_freeze_queue(struct request_queue *q)
181{
182 /*
183 * ...just an alias to keep freeze and unfreeze actions balanced
184 * in the blk_mq_* namespace
185 */
186 blk_freeze_queue(q);
187}
Jens Axboec761d962015-01-02 15:05:12 -0700188EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
Tejun Heof3af0202014-11-04 13:52:27 -0500189
Keith Buschb4c6a022014-12-19 17:54:14 -0700190void blk_mq_unfreeze_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +0100191{
Bob Liu7996a8b2019-05-21 11:25:55 +0800192 mutex_lock(&q->mq_freeze_lock);
193 q->mq_freeze_depth--;
194 WARN_ON_ONCE(q->mq_freeze_depth < 0);
195 if (!q->mq_freeze_depth) {
Bart Van Asschebdd63162018-09-26 14:01:08 -0700196 percpu_ref_resurrect(&q->q_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100197 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600198 }
Bob Liu7996a8b2019-05-21 11:25:55 +0800199 mutex_unlock(&q->mq_freeze_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100200}
Keith Buschb4c6a022014-12-19 17:54:14 -0700201EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
Jens Axboe320ae512013-10-24 09:20:05 +0100202
Bart Van Assche852ec802017-06-21 10:55:47 -0700203/*
204 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
205 * mpt3sas driver such that this function can be removed.
206 */
207void blk_mq_quiesce_queue_nowait(struct request_queue *q)
208{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800209 blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
Bart Van Assche852ec802017-06-21 10:55:47 -0700210}
211EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
212
Bart Van Assche6a83e742016-11-02 10:09:51 -0600213/**
Ming Lei69e07c42017-06-06 23:22:07 +0800214 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
Bart Van Assche6a83e742016-11-02 10:09:51 -0600215 * @q: request queue.
216 *
217 * Note: this function does not prevent that the struct request end_io()
Ming Lei69e07c42017-06-06 23:22:07 +0800218 * callback function is invoked. Once this function is returned, we make
219 * sure no dispatch can happen until the queue is unquiesced via
220 * blk_mq_unquiesce_queue().
Bart Van Assche6a83e742016-11-02 10:09:51 -0600221 */
222void blk_mq_quiesce_queue(struct request_queue *q)
223{
224 struct blk_mq_hw_ctx *hctx;
225 unsigned int i;
226 bool rcu = false;
227
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800228 blk_mq_quiesce_queue_nowait(q);
Ming Leif4560ff2017-06-18 14:24:27 -0600229
Bart Van Assche6a83e742016-11-02 10:09:51 -0600230 queue_for_each_hw_ctx(q, hctx, i) {
231 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -0800232 synchronize_srcu(hctx->srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -0600233 else
234 rcu = true;
235 }
236 if (rcu)
237 synchronize_rcu();
238}
239EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
240
Ming Leie4e73912017-06-06 23:22:03 +0800241/*
242 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
243 * @q: request queue.
244 *
245 * This function recovers queue into the state before quiescing
246 * which is done by blk_mq_quiesce_queue.
247 */
248void blk_mq_unquiesce_queue(struct request_queue *q)
249{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800250 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
Ming Leif4560ff2017-06-18 14:24:27 -0600251
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800252 /* dispatch requests which are inserted during quiescing */
253 blk_mq_run_hw_queues(q, true);
Ming Leie4e73912017-06-06 23:22:03 +0800254}
255EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
256
Jens Axboeaed3ea92014-12-22 14:04:42 -0700257void blk_mq_wake_waiters(struct request_queue *q)
258{
259 struct blk_mq_hw_ctx *hctx;
260 unsigned int i;
261
262 queue_for_each_hw_ctx(q, hctx, i)
263 if (blk_mq_hw_queue_mapped(hctx))
264 blk_mq_tag_wakeup_all(hctx->tags, true);
265}
266
Jens Axboefe1f4522018-11-28 10:50:07 -0700267/*
Hou Tao9a91b052019-05-21 15:59:04 +0800268 * Only need start/end time stamping if we have iostat or
269 * blk stats enabled, or using an IO scheduler.
Jens Axboefe1f4522018-11-28 10:50:07 -0700270 */
271static inline bool blk_mq_need_time_stamp(struct request *rq)
272{
Hou Tao9a91b052019-05-21 15:59:04 +0800273 return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS)) || rq->q->elevator;
Jens Axboefe1f4522018-11-28 10:50:07 -0700274}
275
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200276static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200277 unsigned int tag, u64 alloc_time_ns)
Jens Axboe320ae512013-10-24 09:20:05 +0100278{
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200279 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
280 struct request *rq = tags->static_rqs[tag];
Bart Van Asschec3a148d2017-06-20 11:15:43 -0700281
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200282 if (data->q->elevator) {
Christoph Hellwig766473682020-05-29 15:53:12 +0200283 rq->tag = BLK_MQ_NO_TAG;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200284 rq->internal_tag = tag;
285 } else {
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200286 rq->tag = tag;
Christoph Hellwig766473682020-05-29 15:53:12 +0200287 rq->internal_tag = BLK_MQ_NO_TAG;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200288 }
289
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200290 /* csd/requeue_work/fifo_time is initialized before use */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200291 rq->q = data->q;
292 rq->mq_ctx = data->ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600293 rq->mq_hctx = data->hctx;
Ming Lei568f2702020-07-06 22:41:11 +0800294 rq->rq_flags = 0;
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200295 rq->cmd_flags = data->cmd_flags;
Bart Van Assche8ed46b32020-12-08 21:29:45 -0800296 if (data->flags & BLK_MQ_REQ_PM)
297 rq->rq_flags |= RQF_PM;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200298 if (blk_queue_io_stat(data->q))
Christoph Hellwige8064022016-10-20 15:12:13 +0200299 rq->rq_flags |= RQF_IO_STAT;
Jens Axboe7c3fb702018-01-10 11:46:39 -0700300 INIT_LIST_HEAD(&rq->queuelist);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200301 INIT_HLIST_NODE(&rq->hash);
302 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200303 rq->rq_disk = NULL;
304 rq->part = NULL;
Tejun Heo6f816b42019-08-28 15:05:57 -0700305#ifdef CONFIG_BLK_RQ_ALLOC_TIME
306 rq->alloc_time_ns = alloc_time_ns;
307#endif
Jens Axboefe1f4522018-11-28 10:50:07 -0700308 if (blk_mq_need_time_stamp(rq))
309 rq->start_time_ns = ktime_get_ns();
310 else
311 rq->start_time_ns = 0;
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700312 rq->io_start_time_ns = 0;
Hou Tao3d244302019-05-21 15:59:03 +0800313 rq->stats_sectors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200314 rq->nr_phys_segments = 0;
315#if defined(CONFIG_BLK_DEV_INTEGRITY)
316 rq->nr_integrity_segments = 0;
317#endif
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000318 blk_crypto_rq_set_defaults(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200319 /* tag was already set */
Christoph Hellwig079076b2018-11-14 17:02:05 +0100320 WRITE_ONCE(rq->deadline, 0);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200321
Jens Axboef6be4fb2014-06-06 11:03:48 -0600322 rq->timeout = 0;
323
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200324 rq->end_io = NULL;
325 rq->end_io_data = NULL;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200326
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200327 data->ctx->rq_dispatched[op_is_sync(data->cmd_flags)]++;
Keith Busch12f5b932018-05-29 15:52:28 +0200328 refcount_set(&rq->ref, 1);
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200329
330 if (!op_is_flush(data->cmd_flags)) {
331 struct elevator_queue *e = data->q->elevator;
332
333 rq->elv.icq = NULL;
334 if (e && e->type->ops.prepare_request) {
335 if (e->type->icq_cache)
336 blk_mq_sched_assign_ioc(rq);
337
338 e->type->ops.prepare_request(rq);
339 rq->rq_flags |= RQF_ELVPRIV;
340 }
341 }
342
343 data->hctx->queued++;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200344 return rq;
Jens Axboe320ae512013-10-24 09:20:05 +0100345}
346
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200347static struct request *__blk_mq_alloc_request(struct blk_mq_alloc_data *data)
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200348{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200349 struct request_queue *q = data->q;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200350 struct elevator_queue *e = q->elevator;
Tejun Heo6f816b42019-08-28 15:05:57 -0700351 u64 alloc_time_ns = 0;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200352 unsigned int tag;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200353
Tejun Heo6f816b42019-08-28 15:05:57 -0700354 /* alloc_time includes depth and tag waits */
355 if (blk_queue_rq_alloc_time(q))
356 alloc_time_ns = ktime_get_ns();
357
Jens Axboef9afca42018-10-29 13:11:38 -0600358 if (data->cmd_flags & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500359 data->flags |= BLK_MQ_REQ_NOWAIT;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200360
361 if (e) {
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200362 /*
363 * Flush requests are special and go directly to the
Jens Axboe17a51192018-05-09 13:28:50 -0600364 * dispatch list. Don't include reserved tags in the
365 * limiting, as it isn't useful.
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200366 */
Jens Axboef9afca42018-10-29 13:11:38 -0600367 if (!op_is_flush(data->cmd_flags) &&
368 e->type->ops.limit_depth &&
Jens Axboe17a51192018-05-09 13:28:50 -0600369 !(data->flags & BLK_MQ_REQ_RESERVED))
Jens Axboef9afca42018-10-29 13:11:38 -0600370 e->type->ops.limit_depth(data->cmd_flags, data);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200371 }
372
Ming Leibf0beec2020-05-29 15:53:15 +0200373retry:
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200374 data->ctx = blk_mq_get_ctx(q);
375 data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx);
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200376 if (!e)
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200377 blk_mq_tag_busy(data->hctx);
378
Ming Leibf0beec2020-05-29 15:53:15 +0200379 /*
380 * Waiting allocations only fail because of an inactive hctx. In that
381 * case just retry the hctx assignment and tag allocation as CPU hotplug
382 * should have migrated us to an online CPU by now.
383 */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200384 tag = blk_mq_get_tag(data);
Ming Leibf0beec2020-05-29 15:53:15 +0200385 if (tag == BLK_MQ_NO_TAG) {
386 if (data->flags & BLK_MQ_REQ_NOWAIT)
387 return NULL;
388
389 /*
390 * Give up the CPU and sleep for a random short time to ensure
391 * that thread using a realtime scheduling class are migrated
Randy Dunlap70f15a42020-07-30 18:42:31 -0700392 * off the CPU, and thus off the hctx that is going away.
Ming Leibf0beec2020-05-29 15:53:15 +0200393 */
394 msleep(3);
395 goto retry;
396 }
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200397 return blk_mq_rq_ctx_init(data, tag, alloc_time_ns);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200398}
399
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700400struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800401 blk_mq_req_flags_t flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100402{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200403 struct blk_mq_alloc_data data = {
404 .q = q,
405 .flags = flags,
406 .cmd_flags = op,
407 };
Jens Axboebd166ef2017-01-17 06:03:22 -0700408 struct request *rq;
Joe Lawrencea492f072014-08-28 08:15:21 -0600409 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100410
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800411 ret = blk_queue_enter(q, flags);
Joe Lawrencea492f072014-08-28 08:15:21 -0600412 if (ret)
413 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100414
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200415 rq = __blk_mq_alloc_request(&data);
Jens Axboebd166ef2017-01-17 06:03:22 -0700416 if (!rq)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200417 goto out_queue_exit;
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200418 rq->__data_len = 0;
419 rq->__sector = (sector_t) -1;
420 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100421 return rq;
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200422out_queue_exit:
423 blk_queue_exit(q);
424 return ERR_PTR(-EWOULDBLOCK);
Jens Axboe320ae512013-10-24 09:20:05 +0100425}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600426EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100427
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700428struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800429 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
Ming Lin1f5bd332016-06-13 16:45:21 +0200430{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200431 struct blk_mq_alloc_data data = {
432 .q = q,
433 .flags = flags,
434 .cmd_flags = op,
435 };
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200436 u64 alloc_time_ns = 0;
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800437 unsigned int cpu;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200438 unsigned int tag;
Ming Lin1f5bd332016-06-13 16:45:21 +0200439 int ret;
440
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200441 /* alloc_time includes depth and tag waits */
442 if (blk_queue_rq_alloc_time(q))
443 alloc_time_ns = ktime_get_ns();
444
Ming Lin1f5bd332016-06-13 16:45:21 +0200445 /*
446 * If the tag allocator sleeps we could get an allocation for a
447 * different hardware context. No need to complicate the low level
448 * allocator for this for the rare use case of a command tied to
449 * a specific queue.
450 */
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200451 if (WARN_ON_ONCE(!(flags & (BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED))))
Ming Lin1f5bd332016-06-13 16:45:21 +0200452 return ERR_PTR(-EINVAL);
453
454 if (hctx_idx >= q->nr_hw_queues)
455 return ERR_PTR(-EIO);
456
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800457 ret = blk_queue_enter(q, flags);
Ming Lin1f5bd332016-06-13 16:45:21 +0200458 if (ret)
459 return ERR_PTR(ret);
460
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600461 /*
462 * Check if the hardware context is actually mapped to anything.
463 * If not tell the caller that it should skip this queue.
464 */
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200465 ret = -EXDEV;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200466 data.hctx = q->queue_hw_ctx[hctx_idx];
467 if (!blk_mq_hw_queue_mapped(data.hctx))
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200468 goto out_queue_exit;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200469 cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
470 data.ctx = __blk_mq_get_ctx(q, cpu);
Ming Lin1f5bd332016-06-13 16:45:21 +0200471
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200472 if (!q->elevator)
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200473 blk_mq_tag_busy(data.hctx);
474
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200475 ret = -EWOULDBLOCK;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200476 tag = blk_mq_get_tag(&data);
477 if (tag == BLK_MQ_NO_TAG)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200478 goto out_queue_exit;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200479 return blk_mq_rq_ctx_init(&data, tag, alloc_time_ns);
480
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200481out_queue_exit:
482 blk_queue_exit(q);
483 return ERR_PTR(ret);
Ming Lin1f5bd332016-06-13 16:45:21 +0200484}
485EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
486
Keith Busch12f5b932018-05-29 15:52:28 +0200487static void __blk_mq_free_request(struct request *rq)
488{
489 struct request_queue *q = rq->q;
490 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600491 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Keith Busch12f5b932018-05-29 15:52:28 +0200492 const int sched_tag = rq->internal_tag;
493
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000494 blk_crypto_free_request(rq);
Bart Van Assche986d4132018-09-26 14:01:10 -0700495 blk_pm_mark_last_busy(rq);
Jens Axboeea4f9952018-10-29 15:06:13 -0600496 rq->mq_hctx = NULL;
Christoph Hellwig766473682020-05-29 15:53:12 +0200497 if (rq->tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800498 blk_mq_put_tag(hctx->tags, ctx, rq->tag);
Christoph Hellwig766473682020-05-29 15:53:12 +0200499 if (sched_tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800500 blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
Keith Busch12f5b932018-05-29 15:52:28 +0200501 blk_mq_sched_restart(hctx);
502 blk_queue_exit(q);
503}
504
Christoph Hellwig6af54052017-06-16 18:15:22 +0200505void blk_mq_free_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100506{
Jens Axboe320ae512013-10-24 09:20:05 +0100507 struct request_queue *q = rq->q;
Christoph Hellwig6af54052017-06-16 18:15:22 +0200508 struct elevator_queue *e = q->elevator;
509 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600510 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100511
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200512 if (rq->rq_flags & RQF_ELVPRIV) {
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600513 if (e && e->type->ops.finish_request)
514 e->type->ops.finish_request(rq);
Christoph Hellwig6af54052017-06-16 18:15:22 +0200515 if (rq->elv.icq) {
516 put_io_context(rq->elv.icq->ioc);
517 rq->elv.icq = NULL;
518 }
519 }
520
521 ctx->rq_completed[rq_is_sync(rq)]++;
Christoph Hellwige8064022016-10-20 15:12:13 +0200522 if (rq->rq_flags & RQF_MQ_INFLIGHT)
John Garrybccf5e22020-08-19 23:20:26 +0800523 __blk_mq_dec_active_requests(hctx);
Jens Axboe87760e52016-11-09 12:38:14 -0700524
Jens Axboe7beb2f82017-09-30 02:08:24 -0600525 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
526 laptop_io_completion(q->backing_dev_info);
527
Josef Bacika7905042018-07-03 09:32:35 -0600528 rq_qos_done(q, rq);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600529
Keith Busch12f5b932018-05-29 15:52:28 +0200530 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
531 if (refcount_dec_and_test(&rq->ref))
532 __blk_mq_free_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100533}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700534EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100535
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200536inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
Jens Axboe320ae512013-10-24 09:20:05 +0100537{
Jens Axboefe1f4522018-11-28 10:50:07 -0700538 u64 now = 0;
539
540 if (blk_mq_need_time_stamp(rq))
541 now = ktime_get_ns();
Omar Sandoval522a7772018-05-09 02:08:53 -0700542
Omar Sandoval4bc63392018-05-09 02:08:52 -0700543 if (rq->rq_flags & RQF_STATS) {
544 blk_mq_poll_stats_start(rq->q);
Omar Sandoval522a7772018-05-09 02:08:53 -0700545 blk_stat_add(rq, now);
Omar Sandoval4bc63392018-05-09 02:08:52 -0700546 }
547
Baolin Wang87890092020-07-04 15:28:21 +0800548 blk_mq_sched_completed_request(rq, now);
Omar Sandovaled886602018-09-27 15:55:51 -0700549
Omar Sandoval522a7772018-05-09 02:08:53 -0700550 blk_account_io_done(rq, now);
Ming Lei0d11e6a2013-12-05 10:50:39 -0700551
Christoph Hellwig91b63632014-04-16 09:44:53 +0200552 if (rq->end_io) {
Josef Bacika7905042018-07-03 09:32:35 -0600553 rq_qos_done(rq->q, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100554 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200555 } else {
Jens Axboe320ae512013-10-24 09:20:05 +0100556 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200557 }
Jens Axboe320ae512013-10-24 09:20:05 +0100558}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700559EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200560
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200561void blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200562{
563 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
564 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700565 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200566}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700567EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100568
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200569/*
570 * Softirq action handler - move entries to local list and loop over them
571 * while passing them to the queue registered handler.
572 */
573static __latent_entropy void blk_done_softirq(struct softirq_action *h)
574{
575 struct list_head *cpu_list, local_list;
576
577 local_irq_disable();
578 cpu_list = this_cpu_ptr(&blk_cpu_done);
579 list_replace_init(cpu_list, &local_list);
580 local_irq_enable();
581
582 while (!list_empty(&local_list)) {
583 struct request *rq;
584
585 rq = list_entry(local_list.next, struct request, ipi_list);
586 list_del_init(&rq->ipi_list);
587 rq->q->mq_ops->complete(rq);
588 }
589}
590
Christoph Hellwig115243f2020-06-11 08:44:42 +0200591static void blk_mq_trigger_softirq(struct request *rq)
592{
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200593 struct list_head *list;
594 unsigned long flags;
Christoph Hellwig115243f2020-06-11 08:44:42 +0200595
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200596 local_irq_save(flags);
597 list = this_cpu_ptr(&blk_cpu_done);
Christoph Hellwig115243f2020-06-11 08:44:42 +0200598 list_add_tail(&rq->ipi_list, list);
599
600 /*
601 * If the list only contains our just added request, signal a raise of
602 * the softirq. If there are already entries there, someone already
603 * raised the irq but it hasn't run yet.
604 */
605 if (list->next == &rq->ipi_list)
606 raise_softirq_irqoff(BLOCK_SOFTIRQ);
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200607 local_irq_restore(flags);
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200608}
609
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200610static int blk_softirq_cpu_dead(unsigned int cpu)
611{
612 /*
613 * If a CPU goes away, splice its entries to the current CPU
614 * and trigger a run of the softirq
615 */
616 local_irq_disable();
617 list_splice_init(&per_cpu(blk_cpu_done, cpu),
618 this_cpu_ptr(&blk_cpu_done));
619 raise_softirq_irqoff(BLOCK_SOFTIRQ);
620 local_irq_enable();
621
622 return 0;
623}
624
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200625
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800626static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100627{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800628 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100629
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200630 /*
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200631 * For most of single queue controllers, there is only one irq vector
632 * for handling I/O completion, and the only irq's affinity is set
633 * to all possible CPUs. On most of ARCHs, this affinity means the irq
634 * is handled on one specific CPU.
635 *
636 * So complete I/O requests in softirq context in case of single queue
637 * devices to avoid degrading I/O performance due to irqsoff latency.
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200638 */
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200639 if (rq->q->nr_hw_queues == 1)
640 blk_mq_trigger_softirq(rq);
641 else
642 rq->q->mq_ops->complete(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100643}
644
Christoph Hellwig963395262020-06-11 08:44:49 +0200645static inline bool blk_mq_complete_need_ipi(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100646{
Christoph Hellwig963395262020-06-11 08:44:49 +0200647 int cpu = raw_smp_processor_id();
Jens Axboe320ae512013-10-24 09:20:05 +0100648
Christoph Hellwig963395262020-06-11 08:44:49 +0200649 if (!IS_ENABLED(CONFIG_SMP) ||
650 !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags))
651 return false;
652
653 /* same CPU or cache domain? Complete locally */
654 if (cpu == rq->mq_ctx->cpu ||
655 (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) &&
656 cpus_share_cache(cpu, rq->mq_ctx->cpu)))
657 return false;
658
659 /* don't try to IPI to an offline CPU */
660 return cpu_online(rq->mq_ctx->cpu);
661}
662
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200663bool blk_mq_complete_request_remote(struct request *rq)
664{
Keith Buschaf78ff72018-11-26 09:54:30 -0700665 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
Ming Lei36e76532018-09-28 16:42:20 +0800666
Jens Axboe4ab32bf2018-11-18 16:15:35 -0700667 /*
668 * For a polled request, always complete locallly, it's pointless
669 * to redirect the completion.
670 */
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200671 if (rq->cmd_flags & REQ_HIPRI)
672 return false;
Jens Axboe320ae512013-10-24 09:20:05 +0100673
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200674 if (blk_mq_complete_need_ipi(rq)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800675 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800676 rq->csd.info = rq;
677 rq->csd.flags = 0;
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200678 smp_call_function_single_async(rq->mq_ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800679 } else {
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200680 if (rq->q->nr_hw_queues > 1)
681 return false;
682 blk_mq_trigger_softirq(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800683 }
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200684
685 return true;
Jens Axboe320ae512013-10-24 09:20:05 +0100686}
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200687EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote);
688
Jens Axboe320ae512013-10-24 09:20:05 +0100689/**
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200690 * blk_mq_complete_request - end I/O on a request
691 * @rq: the request being processed
Jens Axboe320ae512013-10-24 09:20:05 +0100692 *
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200693 * Description:
694 * Complete a request by scheduling the ->complete_rq operation.
695 **/
696void blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100697{
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200698 if (!blk_mq_complete_request_remote(rq))
Christoph Hellwig963395262020-06-11 08:44:49 +0200699 rq->q->mq_ops->complete(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100700}
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200701EXPORT_SYMBOL(blk_mq_complete_request);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800702
Jens Axboe04ced152018-01-09 08:29:46 -0800703static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800704 __releases(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800705{
706 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
707 rcu_read_unlock();
708 else
Tejun Heo05707b62018-01-09 08:29:53 -0800709 srcu_read_unlock(hctx->srcu, srcu_idx);
Jens Axboe04ced152018-01-09 08:29:46 -0800710}
711
712static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800713 __acquires(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800714{
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700715 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
716 /* shut up gcc false positive */
717 *srcu_idx = 0;
Jens Axboe04ced152018-01-09 08:29:46 -0800718 rcu_read_lock();
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700719 } else
Tejun Heo05707b62018-01-09 08:29:53 -0800720 *srcu_idx = srcu_read_lock(hctx->srcu);
Jens Axboe04ced152018-01-09 08:29:46 -0800721}
722
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800723/**
André Almeida105663f2020-01-06 15:08:18 -0300724 * blk_mq_start_request - Start processing a request
725 * @rq: Pointer to request to be started
726 *
727 * Function used by device drivers to notify the block layer that a request
728 * is going to be processed now, so blk layer can do proper initializations
729 * such as starting the timeout timer.
730 */
Christoph Hellwige2490072014-09-13 16:40:09 -0700731void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100732{
733 struct request_queue *q = rq->q;
734
735 trace_block_rq_issue(q, rq);
736
Jens Axboecf43e6b2016-11-07 21:32:37 -0700737 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700738 rq->io_start_time_ns = ktime_get_ns();
Hou Tao3d244302019-05-21 15:59:03 +0800739 rq->stats_sectors = blk_rq_sectors(rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700740 rq->rq_flags |= RQF_STATS;
Josef Bacika7905042018-07-03 09:32:35 -0600741 rq_qos_issue(q, rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700742 }
743
Tejun Heo1d9bd512018-01-09 08:29:48 -0800744 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
Jens Axboe538b7532014-09-16 10:37:37 -0600745
Tejun Heo1d9bd512018-01-09 08:29:48 -0800746 blk_add_timer(rq);
Keith Busch12f5b932018-05-29 15:52:28 +0200747 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800748
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +0300749#ifdef CONFIG_BLK_DEV_INTEGRITY
750 if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
751 q->integrity.profile->prepare_fn(rq);
752#endif
Jens Axboe320ae512013-10-24 09:20:05 +0100753}
Christoph Hellwige2490072014-09-13 16:40:09 -0700754EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100755
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200756static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100757{
758 struct request_queue *q = rq->q;
759
Ming Lei923218f2017-11-02 23:24:38 +0800760 blk_mq_put_driver_tag(rq);
761
Jens Axboe320ae512013-10-24 09:20:05 +0100762 trace_block_rq_requeue(q, rq);
Josef Bacika7905042018-07-03 09:32:35 -0600763 rq_qos_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800764
Keith Busch12f5b932018-05-29 15:52:28 +0200765 if (blk_mq_request_started(rq)) {
766 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Christoph Hellwigda661262018-06-14 13:58:45 +0200767 rq->rq_flags &= ~RQF_TIMED_OUT;
Christoph Hellwige2490072014-09-13 16:40:09 -0700768 }
Jens Axboe320ae512013-10-24 09:20:05 +0100769}
770
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700771void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200772{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200773 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200774
Ming Lei105976f2018-02-23 23:36:56 +0800775 /* this request will be re-inserted to io scheduler queue */
776 blk_mq_sched_requeue_request(rq);
777
Jens Axboe7d692332018-10-24 10:48:12 -0600778 BUG_ON(!list_empty(&rq->queuelist));
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700779 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200780}
781EXPORT_SYMBOL(blk_mq_requeue_request);
782
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600783static void blk_mq_requeue_work(struct work_struct *work)
784{
785 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -0400786 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600787 LIST_HEAD(rq_list);
788 struct request *rq, *next;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600789
Jens Axboe18e97812017-07-27 08:03:57 -0600790 spin_lock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600791 list_splice_init(&q->requeue_list, &rq_list);
Jens Axboe18e97812017-07-27 08:03:57 -0600792 spin_unlock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600793
794 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
Jianchao Wangaef18972019-02-12 09:56:25 +0800795 if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP)))
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600796 continue;
797
Christoph Hellwige8064022016-10-20 15:12:13 +0200798 rq->rq_flags &= ~RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600799 list_del_init(&rq->queuelist);
Jianchao Wangaef18972019-02-12 09:56:25 +0800800 /*
801 * If RQF_DONTPREP, rq has contained some driver specific
802 * data, so insert it to hctx dispatch list to avoid any
803 * merge.
804 */
805 if (rq->rq_flags & RQF_DONTPREP)
Ming Lei01e99ae2020-02-25 09:04:32 +0800806 blk_mq_request_bypass_insert(rq, false, false);
Jianchao Wangaef18972019-02-12 09:56:25 +0800807 else
808 blk_mq_sched_insert_request(rq, true, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600809 }
810
811 while (!list_empty(&rq_list)) {
812 rq = list_entry(rq_list.next, struct request, queuelist);
813 list_del_init(&rq->queuelist);
Mike Snitzer9e97d292018-01-17 11:25:58 -0500814 blk_mq_sched_insert_request(rq, false, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600815 }
816
Bart Van Assche52d7f1b2016-10-28 17:20:32 -0700817 blk_mq_run_hw_queues(q, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600818}
819
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700820void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
821 bool kick_requeue_list)
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600822{
823 struct request_queue *q = rq->q;
824 unsigned long flags;
825
826 /*
827 * We abuse this flag that is otherwise used by the I/O scheduler to
Jens Axboeff821d22017-11-10 22:05:12 -0700828 * request head insertion from the workqueue.
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600829 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200830 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600831
832 spin_lock_irqsave(&q->requeue_lock, flags);
833 if (at_head) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200834 rq->rq_flags |= RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600835 list_add(&rq->queuelist, &q->requeue_list);
836 } else {
837 list_add_tail(&rq->queuelist, &q->requeue_list);
838 }
839 spin_unlock_irqrestore(&q->requeue_lock, flags);
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700840
841 if (kick_requeue_list)
842 blk_mq_kick_requeue_list(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600843}
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600844
845void blk_mq_kick_requeue_list(struct request_queue *q)
846{
Bart Van Asscheae943d22018-01-19 08:58:55 -0800847 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600848}
849EXPORT_SYMBOL(blk_mq_kick_requeue_list);
850
Mike Snitzer28494502016-09-14 13:28:30 -0400851void blk_mq_delay_kick_requeue_list(struct request_queue *q,
852 unsigned long msecs)
853{
Bart Van Assched4acf362017-08-09 11:28:06 -0700854 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
855 msecs_to_jiffies(msecs));
Mike Snitzer28494502016-09-14 13:28:30 -0400856}
857EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
858
Jens Axboe0e62f512014-06-04 10:23:49 -0600859struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
860{
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600861 if (tag < tags->nr_tags) {
862 prefetch(tags->rqs[tag]);
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700863 return tags->rqs[tag];
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600864 }
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700865
866 return NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600867}
868EXPORT_SYMBOL(blk_mq_tag_to_rq);
869
Jens Axboe3c94d832018-12-17 21:11:17 -0700870static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq,
871 void *priv, bool reserved)
Jens Axboeae879912018-11-08 09:03:51 -0700872{
873 /*
Ming Lei05a4fed2020-07-07 11:04:33 -0400874 * If we find a request that isn't idle and the queue matches,
Jens Axboe3c94d832018-12-17 21:11:17 -0700875 * we know the queue is busy. Return false to stop the iteration.
Jens Axboeae879912018-11-08 09:03:51 -0700876 */
Ming Lei05a4fed2020-07-07 11:04:33 -0400877 if (blk_mq_request_started(rq) && rq->q == hctx->queue) {
Jens Axboeae879912018-11-08 09:03:51 -0700878 bool *busy = priv;
879
880 *busy = true;
881 return false;
882 }
883
884 return true;
885}
886
Jens Axboe3c94d832018-12-17 21:11:17 -0700887bool blk_mq_queue_inflight(struct request_queue *q)
Jens Axboeae879912018-11-08 09:03:51 -0700888{
889 bool busy = false;
890
Jens Axboe3c94d832018-12-17 21:11:17 -0700891 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
Jens Axboeae879912018-11-08 09:03:51 -0700892 return busy;
893}
Jens Axboe3c94d832018-12-17 21:11:17 -0700894EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
Jens Axboeae879912018-11-08 09:03:51 -0700895
Tejun Heo358f70d2018-01-09 08:29:50 -0800896static void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100897{
Christoph Hellwigda661262018-06-14 13:58:45 +0200898 req->rq_flags |= RQF_TIMED_OUT;
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200899 if (req->q->mq_ops->timeout) {
900 enum blk_eh_timer_return ret;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600901
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200902 ret = req->q->mq_ops->timeout(req, reserved);
903 if (ret == BLK_EH_DONE)
904 return;
905 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700906 }
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200907
908 blk_add_timer(req);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600909}
Keith Busch5b3f25f2015-01-07 18:55:46 -0700910
Keith Busch12f5b932018-05-29 15:52:28 +0200911static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
912{
913 unsigned long deadline;
914
915 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
916 return false;
Christoph Hellwigda661262018-06-14 13:58:45 +0200917 if (rq->rq_flags & RQF_TIMED_OUT)
918 return false;
Keith Busch12f5b932018-05-29 15:52:28 +0200919
Christoph Hellwig079076b2018-11-14 17:02:05 +0100920 deadline = READ_ONCE(rq->deadline);
Keith Busch12f5b932018-05-29 15:52:28 +0200921 if (time_after_eq(jiffies, deadline))
922 return true;
923
924 if (*next == 0)
925 *next = deadline;
926 else if (time_after(*next, deadline))
927 *next = deadline;
928 return false;
929}
930
Ming Leic944b0c2021-05-11 23:22:34 +0800931void blk_mq_put_rq_ref(struct request *rq)
932{
933 if (is_flush_rq(rq, rq->mq_hctx))
934 rq->end_io(rq, 0);
935 else if (refcount_dec_and_test(&rq->ref))
936 __blk_mq_free_request(rq);
937}
938
Jens Axboe7baa8572018-11-08 10:24:07 -0700939static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700940 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100941{
Keith Busch12f5b932018-05-29 15:52:28 +0200942 unsigned long *next = priv;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700943
Keith Busch12f5b932018-05-29 15:52:28 +0200944 /*
945 * Just do a quick check if it is expired before locking the request in
946 * so we're not unnecessarilly synchronizing across CPUs.
947 */
948 if (!blk_mq_req_expired(rq, next))
Jens Axboe7baa8572018-11-08 10:24:07 -0700949 return true;
Jens Axboe320ae512013-10-24 09:20:05 +0100950
Tejun Heo1d9bd512018-01-09 08:29:48 -0800951 /*
Keith Busch12f5b932018-05-29 15:52:28 +0200952 * We have reason to believe the request may be expired. Take a
953 * reference on the request to lock this request lifetime into its
954 * currently allocated context to prevent it from being reallocated in
955 * the event the completion by-passes this timeout handler.
956 *
957 * If the reference was already released, then the driver beat the
958 * timeout handler to posting a natural completion.
Tejun Heo1d9bd512018-01-09 08:29:48 -0800959 */
Keith Busch12f5b932018-05-29 15:52:28 +0200960 if (!refcount_inc_not_zero(&rq->ref))
Jens Axboe7baa8572018-11-08 10:24:07 -0700961 return true;
Keith Busch12f5b932018-05-29 15:52:28 +0200962
963 /*
964 * The request is now locked and cannot be reallocated underneath the
965 * timeout handler's processing. Re-verify this exact request is truly
966 * expired; if it is not expired, then the request was completed and
967 * reallocated as a new request.
968 */
969 if (blk_mq_req_expired(rq, next))
Tejun Heo1d9bd512018-01-09 08:29:48 -0800970 blk_mq_rq_timed_out(rq, reserved);
Yufen Yu8d699662019-09-27 16:19:55 +0800971
Ming Leic944b0c2021-05-11 23:22:34 +0800972 blk_mq_put_rq_ref(rq);
Jens Axboe7baa8572018-11-08 10:24:07 -0700973 return true;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800974}
975
Christoph Hellwig287922e2015-10-30 20:57:30 +0800976static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100977{
Christoph Hellwig287922e2015-10-30 20:57:30 +0800978 struct request_queue *q =
979 container_of(work, struct request_queue, timeout_work);
Keith Busch12f5b932018-05-29 15:52:28 +0200980 unsigned long next = 0;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800981 struct blk_mq_hw_ctx *hctx;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700982 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100983
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600984 /* A deadlock might occur if a request is stuck requiring a
985 * timeout at the same time a queue freeze is waiting
986 * completion, since the timeout code would not be able to
987 * acquire the queue reference here.
988 *
989 * That's why we don't use blk_queue_enter here; instead, we use
990 * percpu_ref_tryget directly, because we need to be able to
991 * obtain a reference even in the short window between the queue
992 * starting to freeze, by dropping the first reference in
Ming Lei1671d522017-03-27 20:06:57 +0800993 * blk_freeze_queue_start, and the moment the last request is
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600994 * consumed, marked by the instant q_usage_counter reaches
995 * zero.
996 */
997 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +0800998 return;
999
Keith Busch12f5b932018-05-29 15:52:28 +02001000 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
Jens Axboe320ae512013-10-24 09:20:05 +01001001
Keith Busch12f5b932018-05-29 15:52:28 +02001002 if (next != 0) {
1003 mod_timer(&q->timeout, next);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001004 } else {
Bart Van Asschefcd36c32018-01-10 08:33:33 -08001005 /*
1006 * Request timeouts are handled as a forward rolling timer. If
1007 * we end up here it means that no requests are pending and
1008 * also that no request has been pending for a while. Mark
1009 * each hctx as idle.
1010 */
Ming Leif054b562015-04-21 10:00:19 +08001011 queue_for_each_hw_ctx(q, hctx, i) {
1012 /* the hctx may be unmapped, so check it here */
1013 if (blk_mq_hw_queue_mapped(hctx))
1014 blk_mq_tag_idle(hctx);
1015 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06001016 }
Christoph Hellwig287922e2015-10-30 20:57:30 +08001017 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001018}
1019
Omar Sandoval88459642016-09-17 08:38:44 -06001020struct flush_busy_ctx_data {
1021 struct blk_mq_hw_ctx *hctx;
1022 struct list_head *list;
1023};
1024
1025static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
1026{
1027 struct flush_busy_ctx_data *flush_data = data;
1028 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
1029 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001030 enum hctx_type type = hctx->type;
Omar Sandoval88459642016-09-17 08:38:44 -06001031
Omar Sandoval88459642016-09-17 08:38:44 -06001032 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001033 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
Omar Sandovale9a99a62018-02-27 16:56:42 -08001034 sbitmap_clear_bit(sb, bitnr);
Omar Sandoval88459642016-09-17 08:38:44 -06001035 spin_unlock(&ctx->lock);
1036 return true;
1037}
1038
Jens Axboe320ae512013-10-24 09:20:05 +01001039/*
Jens Axboe1429d7c2014-05-19 09:23:55 -06001040 * Process software queues that have been marked busy, splicing them
1041 * to the for-dispatch
1042 */
Jens Axboe2c3ad662016-12-14 14:34:47 -07001043void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
Jens Axboe1429d7c2014-05-19 09:23:55 -06001044{
Omar Sandoval88459642016-09-17 08:38:44 -06001045 struct flush_busy_ctx_data data = {
1046 .hctx = hctx,
1047 .list = list,
1048 };
Jens Axboe1429d7c2014-05-19 09:23:55 -06001049
Omar Sandoval88459642016-09-17 08:38:44 -06001050 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001051}
Jens Axboe2c3ad662016-12-14 14:34:47 -07001052EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001053
Ming Leib3476892017-10-14 17:22:30 +08001054struct dispatch_rq_data {
1055 struct blk_mq_hw_ctx *hctx;
1056 struct request *rq;
1057};
1058
1059static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
1060 void *data)
1061{
1062 struct dispatch_rq_data *dispatch_data = data;
1063 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
1064 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001065 enum hctx_type type = hctx->type;
Ming Leib3476892017-10-14 17:22:30 +08001066
1067 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001068 if (!list_empty(&ctx->rq_lists[type])) {
1069 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
Ming Leib3476892017-10-14 17:22:30 +08001070 list_del_init(&dispatch_data->rq->queuelist);
Ming Leic16d6b52018-12-17 08:44:05 -07001071 if (list_empty(&ctx->rq_lists[type]))
Ming Leib3476892017-10-14 17:22:30 +08001072 sbitmap_clear_bit(sb, bitnr);
1073 }
1074 spin_unlock(&ctx->lock);
1075
1076 return !dispatch_data->rq;
1077}
1078
1079struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
1080 struct blk_mq_ctx *start)
1081{
Jens Axboef31967f2018-10-29 13:13:29 -06001082 unsigned off = start ? start->index_hw[hctx->type] : 0;
Ming Leib3476892017-10-14 17:22:30 +08001083 struct dispatch_rq_data data = {
1084 .hctx = hctx,
1085 .rq = NULL,
1086 };
1087
1088 __sbitmap_for_each_set(&hctx->ctx_map, off,
1089 dispatch_rq_from_ctx, &data);
1090
1091 return data.rq;
1092}
1093
Jens Axboe703fd1c2016-09-16 13:59:14 -06001094static inline unsigned int queued_to_index(unsigned int queued)
1095{
1096 if (!queued)
1097 return 0;
Jens Axboe1429d7c2014-05-19 09:23:55 -06001098
Jens Axboe703fd1c2016-09-16 13:59:14 -06001099 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001100}
1101
Ming Lei570e9b72020-06-30 22:03:55 +08001102static bool __blk_mq_get_driver_tag(struct request *rq)
1103{
John Garry222a5ae2020-08-19 23:20:23 +08001104 struct sbitmap_queue *bt = rq->mq_hctx->tags->bitmap_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001105 unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001106 int tag;
1107
Ming Lei568f2702020-07-06 22:41:11 +08001108 blk_mq_tag_busy(rq->mq_hctx);
1109
Ming Lei570e9b72020-06-30 22:03:55 +08001110 if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) {
John Garry222a5ae2020-08-19 23:20:23 +08001111 bt = rq->mq_hctx->tags->breserved_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001112 tag_offset = 0;
Ming Lei28500852020-09-11 18:41:14 +08001113 } else {
1114 if (!hctx_may_queue(rq->mq_hctx, bt))
1115 return false;
Ming Lei570e9b72020-06-30 22:03:55 +08001116 }
1117
Ming Lei570e9b72020-06-30 22:03:55 +08001118 tag = __sbitmap_queue_get(bt);
1119 if (tag == BLK_MQ_NO_TAG)
1120 return false;
1121
1122 rq->tag = tag + tag_offset;
Ming Lei570e9b72020-06-30 22:03:55 +08001123 return true;
1124}
1125
1126static bool blk_mq_get_driver_tag(struct request *rq)
1127{
Ming Lei568f2702020-07-06 22:41:11 +08001128 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1129
1130 if (rq->tag == BLK_MQ_NO_TAG && !__blk_mq_get_driver_tag(rq))
1131 return false;
1132
Ming Lei51db1c32020-08-19 23:20:19 +08001133 if ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) &&
Ming Lei568f2702020-07-06 22:41:11 +08001134 !(rq->rq_flags & RQF_MQ_INFLIGHT)) {
1135 rq->rq_flags |= RQF_MQ_INFLIGHT;
John Garrybccf5e22020-08-19 23:20:26 +08001136 __blk_mq_inc_active_requests(hctx);
Ming Lei568f2702020-07-06 22:41:11 +08001137 }
1138 hctx->tags->rqs[rq->tag] = rq;
1139 return true;
Ming Lei570e9b72020-06-30 22:03:55 +08001140}
1141
Jens Axboeeb619fd2017-11-09 08:32:43 -07001142static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1143 int flags, void *key)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001144{
1145 struct blk_mq_hw_ctx *hctx;
1146
1147 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1148
Ming Lei5815839b2018-06-25 19:31:47 +08001149 spin_lock(&hctx->dispatch_wait_lock);
Jens Axboee8618572019-03-25 12:34:10 -06001150 if (!list_empty(&wait->entry)) {
1151 struct sbitmap_queue *sbq;
1152
1153 list_del_init(&wait->entry);
John Garry222a5ae2020-08-19 23:20:23 +08001154 sbq = hctx->tags->bitmap_tags;
Jens Axboee8618572019-03-25 12:34:10 -06001155 atomic_dec(&sbq->ws_active);
1156 }
Ming Lei5815839b2018-06-25 19:31:47 +08001157 spin_unlock(&hctx->dispatch_wait_lock);
1158
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001159 blk_mq_run_hw_queue(hctx, true);
1160 return 1;
1161}
1162
Jens Axboef906a6a2017-11-09 16:10:13 -07001163/*
1164 * Mark us waiting for a tag. For shared tags, this involves hooking us into
Bart Van Asscheee3e4de2018-01-09 10:09:15 -08001165 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1166 * restart. For both cases, take care to check the condition again after
Jens Axboef906a6a2017-11-09 16:10:13 -07001167 * marking us as waiting.
1168 */
Ming Lei2278d692018-06-25 19:31:46 +08001169static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
Jens Axboef906a6a2017-11-09 16:10:13 -07001170 struct request *rq)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001171{
John Garry222a5ae2020-08-19 23:20:23 +08001172 struct sbitmap_queue *sbq = hctx->tags->bitmap_tags;
Ming Lei5815839b2018-06-25 19:31:47 +08001173 struct wait_queue_head *wq;
Jens Axboef906a6a2017-11-09 16:10:13 -07001174 wait_queue_entry_t *wait;
1175 bool ret;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001176
Ming Lei51db1c32020-08-19 23:20:19 +08001177 if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
Yufen Yu684b7322019-03-15 11:05:10 +08001178 blk_mq_sched_mark_restart_hctx(hctx);
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001179
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001180 /*
1181 * It's possible that a tag was freed in the window between the
1182 * allocation failure and adding the hardware queue to the wait
1183 * queue.
1184 *
1185 * Don't clear RESTART here, someone else could have set it.
1186 * At most this will cost an extra queue run.
1187 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001188 return blk_mq_get_driver_tag(rq);
Jens Axboeeb619fd2017-11-09 08:32:43 -07001189 }
1190
Ming Lei2278d692018-06-25 19:31:46 +08001191 wait = &hctx->dispatch_wait;
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001192 if (!list_empty_careful(&wait->entry))
1193 return false;
1194
Jens Axboee8618572019-03-25 12:34:10 -06001195 wq = &bt_wait_ptr(sbq, hctx)->wait;
Ming Lei5815839b2018-06-25 19:31:47 +08001196
1197 spin_lock_irq(&wq->lock);
1198 spin_lock(&hctx->dispatch_wait_lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001199 if (!list_empty(&wait->entry)) {
Ming Lei5815839b2018-06-25 19:31:47 +08001200 spin_unlock(&hctx->dispatch_wait_lock);
1201 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001202 return false;
1203 }
1204
Jens Axboee8618572019-03-25 12:34:10 -06001205 atomic_inc(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001206 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1207 __add_wait_queue(wq, wait);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001208
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001209 /*
Jens Axboeeb619fd2017-11-09 08:32:43 -07001210 * It's possible that a tag was freed in the window between the
1211 * allocation failure and adding the hardware queue to the wait
1212 * queue.
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001213 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001214 ret = blk_mq_get_driver_tag(rq);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001215 if (!ret) {
Ming Lei5815839b2018-06-25 19:31:47 +08001216 spin_unlock(&hctx->dispatch_wait_lock);
1217 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001218 return false;
Jens Axboef906a6a2017-11-09 16:10:13 -07001219 }
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001220
1221 /*
1222 * We got a tag, remove ourselves from the wait queue to ensure
1223 * someone else gets the wakeup.
1224 */
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001225 list_del_init(&wait->entry);
Jens Axboee8618572019-03-25 12:34:10 -06001226 atomic_dec(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001227 spin_unlock(&hctx->dispatch_wait_lock);
1228 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001229
1230 return true;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001231}
1232
Ming Lei6e7687172018-07-03 09:03:16 -06001233#define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1234#define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1235/*
1236 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1237 * - EWMA is one simple way to compute running average value
1238 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1239 * - take 4 as factor for avoiding to get too small(0) result, and this
1240 * factor doesn't matter because EWMA decreases exponentially
1241 */
1242static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1243{
1244 unsigned int ewma;
1245
1246 if (hctx->queue->elevator)
1247 return;
1248
1249 ewma = hctx->dispatch_busy;
1250
1251 if (!ewma && !busy)
1252 return;
1253
1254 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1255 if (busy)
1256 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1257 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1258
1259 hctx->dispatch_busy = ewma;
1260}
1261
Ming Lei86ff7c22018-01-30 22:04:57 -05001262#define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1263
Johannes Thumshirnc92a4102020-03-25 00:24:44 +09001264static void blk_mq_handle_dev_resource(struct request *rq,
1265 struct list_head *list)
1266{
1267 struct request *next =
1268 list_first_entry_or_null(list, struct request, queuelist);
1269
1270 /*
1271 * If an I/O scheduler has been configured and we got a driver tag for
1272 * the next request already, free it.
1273 */
1274 if (next)
1275 blk_mq_put_driver_tag(next);
1276
1277 list_add(&rq->queuelist, list);
1278 __blk_mq_requeue_request(rq);
1279}
1280
Keith Busch0512a752020-05-12 17:55:47 +09001281static void blk_mq_handle_zone_resource(struct request *rq,
1282 struct list_head *zone_list)
1283{
1284 /*
1285 * If we end up here it is because we cannot dispatch a request to a
1286 * specific zone due to LLD level zone-write locking or other zone
1287 * related resource not being available. In this case, set the request
1288 * aside in zone_list for retrying it later.
1289 */
1290 list_add(&rq->queuelist, zone_list);
1291 __blk_mq_requeue_request(rq);
1292}
1293
Ming Lei75383522020-06-30 18:24:58 +08001294enum prep_dispatch {
1295 PREP_DISPATCH_OK,
1296 PREP_DISPATCH_NO_TAG,
1297 PREP_DISPATCH_NO_BUDGET,
1298};
1299
1300static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq,
1301 bool need_budget)
1302{
1303 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1304
1305 if (need_budget && !blk_mq_get_dispatch_budget(rq->q)) {
1306 blk_mq_put_driver_tag(rq);
1307 return PREP_DISPATCH_NO_BUDGET;
1308 }
1309
1310 if (!blk_mq_get_driver_tag(rq)) {
1311 /*
1312 * The initial allocation attempt failed, so we need to
1313 * rerun the hardware queue when a tag is freed. The
1314 * waitqueue takes care of that. If the queue is run
1315 * before we add this entry back on the dispatch list,
1316 * we'll re-run it below.
1317 */
1318 if (!blk_mq_mark_tag_wait(hctx, rq)) {
Ming Lei1fd40b52020-06-30 18:25:00 +08001319 /*
1320 * All budgets not got from this function will be put
1321 * together during handling partial dispatch
1322 */
1323 if (need_budget)
1324 blk_mq_put_dispatch_budget(rq->q);
Ming Lei75383522020-06-30 18:24:58 +08001325 return PREP_DISPATCH_NO_TAG;
1326 }
1327 }
1328
1329 return PREP_DISPATCH_OK;
1330}
1331
Ming Lei1fd40b52020-06-30 18:25:00 +08001332/* release all allocated budgets before calling to blk_mq_dispatch_rq_list */
1333static void blk_mq_release_budgets(struct request_queue *q,
1334 unsigned int nr_budgets)
1335{
1336 int i;
1337
1338 for (i = 0; i < nr_budgets; i++)
1339 blk_mq_put_dispatch_budget(q);
1340}
1341
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001342/*
1343 * Returns true if we did some work AND can potentially do more.
1344 */
Ming Lei445874e2020-06-30 18:24:57 +08001345bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list,
Ming Lei1fd40b52020-06-30 18:25:00 +08001346 unsigned int nr_budgets)
Jens Axboef04c3df2016-12-07 08:41:17 -07001347{
Ming Lei75383522020-06-30 18:24:58 +08001348 enum prep_dispatch prep;
Ming Lei445874e2020-06-30 18:24:57 +08001349 struct request_queue *q = hctx->queue;
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001350 struct request *rq, *nxt;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001351 int errors, queued;
Ming Lei86ff7c22018-01-30 22:04:57 -05001352 blk_status_t ret = BLK_STS_OK;
Keith Busch0512a752020-05-12 17:55:47 +09001353 LIST_HEAD(zone_list);
Jens Axboef04c3df2016-12-07 08:41:17 -07001354
Omar Sandoval81380ca2017-04-07 08:56:26 -06001355 if (list_empty(list))
1356 return false;
1357
Jens Axboef04c3df2016-12-07 08:41:17 -07001358 /*
Jens Axboef04c3df2016-12-07 08:41:17 -07001359 * Now process all the entries, sending them to the driver.
1360 */
Jens Axboe93efe982017-03-24 12:04:19 -06001361 errors = queued = 0;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001362 do {
Jens Axboef04c3df2016-12-07 08:41:17 -07001363 struct blk_mq_queue_data bd;
1364
1365 rq = list_first_entry(list, struct request, queuelist);
Ming Lei0bca7992018-04-05 00:35:21 +08001366
Ming Lei445874e2020-06-30 18:24:57 +08001367 WARN_ON_ONCE(hctx != rq->mq_hctx);
Ming Lei1fd40b52020-06-30 18:25:00 +08001368 prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets);
Ming Lei75383522020-06-30 18:24:58 +08001369 if (prep != PREP_DISPATCH_OK)
Ming Lei0bca7992018-04-05 00:35:21 +08001370 break;
Ming Leide148292017-10-14 17:22:29 +08001371
Jens Axboef04c3df2016-12-07 08:41:17 -07001372 list_del_init(&rq->queuelist);
1373
1374 bd.rq = rq;
Jens Axboe113285b2017-03-02 13:26:04 -07001375
1376 /*
1377 * Flag last if we have no more requests, or if we have more
1378 * but can't assign a driver tag to it.
1379 */
1380 if (list_empty(list))
1381 bd.last = true;
1382 else {
Jens Axboe113285b2017-03-02 13:26:04 -07001383 nxt = list_first_entry(list, struct request, queuelist);
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001384 bd.last = !blk_mq_get_driver_tag(nxt);
Jens Axboe113285b2017-03-02 13:26:04 -07001385 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001386
Ming Lei1fd40b52020-06-30 18:25:00 +08001387 /*
1388 * once the request is queued to lld, no need to cover the
1389 * budget any more
1390 */
1391 if (nr_budgets)
1392 nr_budgets--;
Jens Axboef04c3df2016-12-07 08:41:17 -07001393 ret = q->mq_ops->queue_rq(hctx, &bd);
Ming Lei7bf13722020-07-01 21:58:57 +08001394 switch (ret) {
1395 case BLK_STS_OK:
1396 queued++;
Jens Axboef04c3df2016-12-07 08:41:17 -07001397 break;
Ming Lei7bf13722020-07-01 21:58:57 +08001398 case BLK_STS_RESOURCE:
1399 case BLK_STS_DEV_RESOURCE:
1400 blk_mq_handle_dev_resource(rq, list);
1401 goto out;
1402 case BLK_STS_ZONE_RESOURCE:
Keith Busch0512a752020-05-12 17:55:47 +09001403 /*
1404 * Move the request to zone_list and keep going through
1405 * the dispatch list to find more requests the drive can
1406 * accept.
1407 */
1408 blk_mq_handle_zone_resource(rq, &zone_list);
Ming Lei7bf13722020-07-01 21:58:57 +08001409 break;
1410 default:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001411 errors++;
1412 blk_mq_end_request(rq, BLK_STS_IOERR);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001413 }
Omar Sandoval81380ca2017-04-07 08:56:26 -06001414 } while (!list_empty(list));
Ming Lei7bf13722020-07-01 21:58:57 +08001415out:
Keith Busch0512a752020-05-12 17:55:47 +09001416 if (!list_empty(&zone_list))
1417 list_splice_tail_init(&zone_list, list);
1418
Jens Axboef04c3df2016-12-07 08:41:17 -07001419 hctx->dispatched[queued_to_index(queued)]++;
1420
yangerkun632bfb62020-09-05 19:25:56 +08001421 /* If we didn't flush the entire list, we could have told the driver
1422 * there was more coming, but that turned out to be a lie.
1423 */
1424 if ((!list_empty(list) || errors) && q->mq_ops->commit_rqs && queued)
1425 q->mq_ops->commit_rqs(hctx);
Jens Axboef04c3df2016-12-07 08:41:17 -07001426 /*
1427 * Any items that need requeuing? Stuff them into hctx->dispatch,
1428 * that is where we will continue on next queue run.
1429 */
1430 if (!list_empty(list)) {
Ming Lei86ff7c22018-01-30 22:04:57 -05001431 bool needs_restart;
Ming Lei75383522020-06-30 18:24:58 +08001432 /* For non-shared tags, the RESTART check will suffice */
1433 bool no_tag = prep == PREP_DISPATCH_NO_TAG &&
Ming Lei51db1c32020-08-19 23:20:19 +08001434 (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED);
Ming Lei75383522020-06-30 18:24:58 +08001435 bool no_budget_avail = prep == PREP_DISPATCH_NO_BUDGET;
Ming Lei86ff7c22018-01-30 22:04:57 -05001436
Ming Lei1fd40b52020-06-30 18:25:00 +08001437 blk_mq_release_budgets(q, nr_budgets);
Jens Axboef04c3df2016-12-07 08:41:17 -07001438
1439 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001440 list_splice_tail_init(list, &hctx->dispatch);
Jens Axboef04c3df2016-12-07 08:41:17 -07001441 spin_unlock(&hctx->lock);
1442
1443 /*
Ming Leid7d85352020-08-17 18:01:15 +08001444 * Order adding requests to hctx->dispatch and checking
1445 * SCHED_RESTART flag. The pair of this smp_mb() is the one
1446 * in blk_mq_sched_restart(). Avoid restart code path to
1447 * miss the new added requests to hctx->dispatch, meantime
1448 * SCHED_RESTART is observed here.
1449 */
1450 smp_mb();
1451
1452 /*
Bart Van Assche710c7852017-04-07 11:16:51 -07001453 * If SCHED_RESTART was set by the caller of this function and
1454 * it is no longer set that means that it was cleared by another
1455 * thread and hence that a queue rerun is needed.
Jens Axboef04c3df2016-12-07 08:41:17 -07001456 *
Jens Axboeeb619fd2017-11-09 08:32:43 -07001457 * If 'no_tag' is set, that means that we failed getting
1458 * a driver tag with an I/O scheduler attached. If our dispatch
1459 * waitqueue is no longer active, ensure that we run the queue
1460 * AFTER adding our entries back to the list.
Jens Axboebd166ef2017-01-17 06:03:22 -07001461 *
Bart Van Assche710c7852017-04-07 11:16:51 -07001462 * If no I/O scheduler has been configured it is possible that
1463 * the hardware queue got stopped and restarted before requests
1464 * were pushed back onto the dispatch list. Rerun the queue to
1465 * avoid starvation. Notes:
1466 * - blk_mq_run_hw_queue() checks whether or not a queue has
1467 * been stopped before rerunning a queue.
1468 * - Some but not all block drivers stop a queue before
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001469 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
Bart Van Assche710c7852017-04-07 11:16:51 -07001470 * and dm-rq.
Ming Lei86ff7c22018-01-30 22:04:57 -05001471 *
1472 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1473 * bit is set, run queue after a delay to avoid IO stalls
Douglas Andersonab3cee32020-04-20 09:24:51 -07001474 * that could otherwise occur if the queue is idle. We'll do
1475 * similar if we couldn't get budget and SCHED_RESTART is set.
Jens Axboebd166ef2017-01-17 06:03:22 -07001476 */
Ming Lei86ff7c22018-01-30 22:04:57 -05001477 needs_restart = blk_mq_sched_needs_restart(hctx);
1478 if (!needs_restart ||
Jens Axboeeb619fd2017-11-09 08:32:43 -07001479 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
Jens Axboebd166ef2017-01-17 06:03:22 -07001480 blk_mq_run_hw_queue(hctx, true);
Douglas Andersonab3cee32020-04-20 09:24:51 -07001481 else if (needs_restart && (ret == BLK_STS_RESOURCE ||
1482 no_budget_avail))
Ming Lei86ff7c22018-01-30 22:04:57 -05001483 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001484
Ming Lei6e7687172018-07-03 09:03:16 -06001485 blk_mq_update_dispatch_busy(hctx, true);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001486 return false;
Ming Lei6e7687172018-07-03 09:03:16 -06001487 } else
1488 blk_mq_update_dispatch_busy(hctx, false);
Jens Axboef04c3df2016-12-07 08:41:17 -07001489
Jens Axboe93efe982017-03-24 12:04:19 -06001490 return (queued + errors) != 0;
Jens Axboef04c3df2016-12-07 08:41:17 -07001491}
1492
André Almeida105663f2020-01-06 15:08:18 -03001493/**
1494 * __blk_mq_run_hw_queue - Run a hardware queue.
1495 * @hctx: Pointer to the hardware queue to run.
1496 *
1497 * Send pending requests to the hardware.
1498 */
Bart Van Assche6a83e742016-11-02 10:09:51 -06001499static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1500{
1501 int srcu_idx;
1502
Jens Axboeb7a71e62017-08-01 09:28:24 -06001503 /*
1504 * We should be running this queue from one of the CPUs that
1505 * are mapped to it.
Ming Lei7df938f2018-01-18 00:41:52 +08001506 *
1507 * There are at least two related races now between setting
1508 * hctx->next_cpu from blk_mq_hctx_next_cpu() and running
1509 * __blk_mq_run_hw_queue():
1510 *
1511 * - hctx->next_cpu is found offline in blk_mq_hctx_next_cpu(),
1512 * but later it becomes online, then this warning is harmless
1513 * at all
1514 *
1515 * - hctx->next_cpu is found online in blk_mq_hctx_next_cpu(),
1516 * but later it becomes offline, then the warning can't be
1517 * triggered, and we depend on blk-mq timeout handler to
1518 * handle dispatched requests to this hctx
Jens Axboeb7a71e62017-08-01 09:28:24 -06001519 */
Ming Lei7df938f2018-01-18 00:41:52 +08001520 if (!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
1521 cpu_online(hctx->next_cpu)) {
1522 printk(KERN_WARNING "run queue from wrong CPU %d, hctx %s\n",
1523 raw_smp_processor_id(),
1524 cpumask_empty(hctx->cpumask) ? "inactive": "active");
1525 dump_stack();
1526 }
Bart Van Assche6a83e742016-11-02 10:09:51 -06001527
Jens Axboeb7a71e62017-08-01 09:28:24 -06001528 /*
1529 * We can't run the queue inline with ints disabled. Ensure that
1530 * we catch bad users of this early.
1531 */
1532 WARN_ON_ONCE(in_interrupt());
1533
Jens Axboe04ced152018-01-09 08:29:46 -08001534 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
Jens Axboebf4907c2017-03-30 12:30:39 -06001535
Jens Axboe04ced152018-01-09 08:29:46 -08001536 hctx_lock(hctx, &srcu_idx);
1537 blk_mq_sched_dispatch_requests(hctx);
1538 hctx_unlock(hctx, srcu_idx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001539}
1540
Ming Leif82ddf12018-04-08 17:48:10 +08001541static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1542{
1543 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1544
1545 if (cpu >= nr_cpu_ids)
1546 cpu = cpumask_first(hctx->cpumask);
1547 return cpu;
1548}
1549
Jens Axboe506e9312014-05-07 10:26:44 -06001550/*
1551 * It'd be great if the workqueue API had a way to pass
1552 * in a mask and had some smarts for more clever placement.
1553 * For now we just round-robin here, switching for every
1554 * BLK_MQ_CPU_WORK_BATCH queued items.
1555 */
1556static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1557{
Ming Lei7bed4592018-01-18 00:41:51 +08001558 bool tried = false;
Ming Lei476f8c92018-04-08 17:48:09 +08001559 int next_cpu = hctx->next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001560
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001561 if (hctx->queue->nr_hw_queues == 1)
1562 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -06001563
1564 if (--hctx->next_cpu_batch <= 0) {
Ming Lei7bed4592018-01-18 00:41:51 +08001565select_cpu:
Ming Lei476f8c92018-04-08 17:48:09 +08001566 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08001567 cpu_online_mask);
Jens Axboe506e9312014-05-07 10:26:44 -06001568 if (next_cpu >= nr_cpu_ids)
Ming Leif82ddf12018-04-08 17:48:10 +08001569 next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06001570 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1571 }
1572
Ming Lei7bed4592018-01-18 00:41:51 +08001573 /*
1574 * Do unbound schedule if we can't find a online CPU for this hctx,
1575 * and it should only happen in the path of handling CPU DEAD.
1576 */
Ming Lei476f8c92018-04-08 17:48:09 +08001577 if (!cpu_online(next_cpu)) {
Ming Lei7bed4592018-01-18 00:41:51 +08001578 if (!tried) {
1579 tried = true;
1580 goto select_cpu;
1581 }
1582
1583 /*
1584 * Make sure to re-select CPU next time once after CPUs
1585 * in hctx->cpumask become online again.
1586 */
Ming Lei476f8c92018-04-08 17:48:09 +08001587 hctx->next_cpu = next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001588 hctx->next_cpu_batch = 1;
1589 return WORK_CPU_UNBOUND;
1590 }
Ming Lei476f8c92018-04-08 17:48:09 +08001591
1592 hctx->next_cpu = next_cpu;
1593 return next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -06001594}
1595
André Almeida105663f2020-01-06 15:08:18 -03001596/**
1597 * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue.
1598 * @hctx: Pointer to the hardware queue to run.
1599 * @async: If we want to run the queue asynchronously.
1600 * @msecs: Microseconds of delay to wait before running the queue.
1601 *
1602 * If !@async, try to run the queue now. Else, run the queue asynchronously and
1603 * with a delay of @msecs.
1604 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001605static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1606 unsigned long msecs)
Jens Axboe320ae512013-10-24 09:20:05 +01001607{
Bart Van Assche5435c022017-06-20 11:15:49 -07001608 if (unlikely(blk_mq_hctx_stopped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +01001609 return;
1610
Jens Axboe1b792f22016-09-21 10:12:13 -06001611 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001612 int cpu = get_cpu();
1613 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +01001614 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001615 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +01001616 return;
1617 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001618
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001619 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -06001620 }
Paolo Bonzini398205b2014-11-07 23:03:59 +01001621
Bart Van Asscheae943d22018-01-19 08:58:55 -08001622 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1623 msecs_to_jiffies(msecs));
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001624}
1625
André Almeida105663f2020-01-06 15:08:18 -03001626/**
1627 * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
1628 * @hctx: Pointer to the hardware queue to run.
1629 * @msecs: Microseconds of delay to wait before running the queue.
1630 *
1631 * Run a hardware queue asynchronously with a delay of @msecs.
1632 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001633void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1634{
1635 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1636}
1637EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1638
André Almeida105663f2020-01-06 15:08:18 -03001639/**
1640 * blk_mq_run_hw_queue - Start to run a hardware queue.
1641 * @hctx: Pointer to the hardware queue to run.
1642 * @async: If we want to run the queue asynchronously.
1643 *
1644 * Check if the request queue is not in a quiesced state and if there are
1645 * pending requests to be sent. If this is true, run the queue to send requests
1646 * to hardware.
1647 */
John Garry626fb732019-10-30 00:59:30 +08001648void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001649{
Ming Lei24f5a902018-01-06 16:27:38 +08001650 int srcu_idx;
1651 bool need_run;
1652
1653 /*
1654 * When queue is quiesced, we may be switching io scheduler, or
1655 * updating nr_hw_queues, or other things, and we can't run queue
1656 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
1657 *
1658 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
1659 * quiesced.
1660 */
Jens Axboe04ced152018-01-09 08:29:46 -08001661 hctx_lock(hctx, &srcu_idx);
1662 need_run = !blk_queue_quiesced(hctx->queue) &&
1663 blk_mq_hctx_has_pending(hctx);
1664 hctx_unlock(hctx, srcu_idx);
Ming Lei24f5a902018-01-06 16:27:38 +08001665
John Garry626fb732019-10-30 00:59:30 +08001666 if (need_run)
Jens Axboe79f720a2017-11-10 09:13:21 -07001667 __blk_mq_delay_run_hw_queue(hctx, async, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01001668}
Omar Sandoval5b727272017-04-14 01:00:00 -07001669EXPORT_SYMBOL(blk_mq_run_hw_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01001670
Jan Karad7d00982021-01-11 17:47:17 +01001671/*
1672 * Is the request queue handled by an IO scheduler that does not respect
1673 * hardware queues when dispatching?
1674 */
1675static bool blk_mq_has_sqsched(struct request_queue *q)
1676{
1677 struct elevator_queue *e = q->elevator;
1678
1679 if (e && e->type->ops.dispatch_request &&
1680 !(e->type->elevator_features & ELEVATOR_F_MQ_AWARE))
1681 return true;
1682 return false;
1683}
1684
1685/*
1686 * Return prefered queue to dispatch from (if any) for non-mq aware IO
1687 * scheduler.
1688 */
1689static struct blk_mq_hw_ctx *blk_mq_get_sq_hctx(struct request_queue *q)
1690{
1691 struct blk_mq_hw_ctx *hctx;
1692
1693 /*
1694 * If the IO scheduler does not respect hardware queues when
1695 * dispatching, we just don't bother with multiple HW queues and
1696 * dispatch from hctx for the current CPU since running multiple queues
1697 * just causes lock contention inside the scheduler and pointless cache
1698 * bouncing.
1699 */
1700 hctx = blk_mq_map_queue_type(q, HCTX_TYPE_DEFAULT,
1701 raw_smp_processor_id());
1702 if (!blk_mq_hctx_stopped(hctx))
1703 return hctx;
1704 return NULL;
1705}
1706
André Almeida105663f2020-01-06 15:08:18 -03001707/**
Mauro Carvalho Chehab24f7bb82020-10-23 18:32:54 +02001708 * blk_mq_run_hw_queues - Run all hardware queues in a request queue.
André Almeida105663f2020-01-06 15:08:18 -03001709 * @q: Pointer to the request queue to run.
1710 * @async: If we want to run the queue asynchronously.
1711 */
Mike Snitzerb94ec292015-03-11 23:56:38 -04001712void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001713{
Jan Karad7d00982021-01-11 17:47:17 +01001714 struct blk_mq_hw_ctx *hctx, *sq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001715 int i;
1716
Jan Karad7d00982021-01-11 17:47:17 +01001717 sq_hctx = NULL;
1718 if (blk_mq_has_sqsched(q))
1719 sq_hctx = blk_mq_get_sq_hctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001720 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe79f720a2017-11-10 09:13:21 -07001721 if (blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +01001722 continue;
Jan Karad7d00982021-01-11 17:47:17 +01001723 /*
1724 * Dispatch from this hctx either if there's no hctx preferred
1725 * by IO scheduler or if it has requests that bypass the
1726 * scheduler.
1727 */
1728 if (!sq_hctx || sq_hctx == hctx ||
1729 !list_empty_careful(&hctx->dispatch))
1730 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001731 }
1732}
Mike Snitzerb94ec292015-03-11 23:56:38 -04001733EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001734
Bart Van Asschefd001442016-10-28 17:19:37 -07001735/**
Douglas Andersonb9151e72020-04-20 09:24:52 -07001736 * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously.
1737 * @q: Pointer to the request queue to run.
1738 * @msecs: Microseconds of delay to wait before running the queues.
1739 */
1740void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs)
1741{
Jan Karad7d00982021-01-11 17:47:17 +01001742 struct blk_mq_hw_ctx *hctx, *sq_hctx;
Douglas Andersonb9151e72020-04-20 09:24:52 -07001743 int i;
1744
Jan Karad7d00982021-01-11 17:47:17 +01001745 sq_hctx = NULL;
1746 if (blk_mq_has_sqsched(q))
1747 sq_hctx = blk_mq_get_sq_hctx(q);
Douglas Andersonb9151e72020-04-20 09:24:52 -07001748 queue_for_each_hw_ctx(q, hctx, i) {
1749 if (blk_mq_hctx_stopped(hctx))
1750 continue;
Jan Karad7d00982021-01-11 17:47:17 +01001751 /*
1752 * Dispatch from this hctx either if there's no hctx preferred
1753 * by IO scheduler or if it has requests that bypass the
1754 * scheduler.
1755 */
1756 if (!sq_hctx || sq_hctx == hctx ||
1757 !list_empty_careful(&hctx->dispatch))
1758 blk_mq_delay_run_hw_queue(hctx, msecs);
Douglas Andersonb9151e72020-04-20 09:24:52 -07001759 }
1760}
1761EXPORT_SYMBOL(blk_mq_delay_run_hw_queues);
1762
1763/**
Bart Van Asschefd001442016-10-28 17:19:37 -07001764 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1765 * @q: request queue.
1766 *
1767 * The caller is responsible for serializing this function against
1768 * blk_mq_{start,stop}_hw_queue().
1769 */
1770bool blk_mq_queue_stopped(struct request_queue *q)
1771{
1772 struct blk_mq_hw_ctx *hctx;
1773 int i;
1774
1775 queue_for_each_hw_ctx(q, hctx, i)
1776 if (blk_mq_hctx_stopped(hctx))
1777 return true;
1778
1779 return false;
1780}
1781EXPORT_SYMBOL(blk_mq_queue_stopped);
1782
Ming Lei39a70c72017-06-06 23:22:09 +08001783/*
1784 * This function is often used for pausing .queue_rq() by driver when
1785 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001786 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001787 *
1788 * We do not guarantee that dispatch can be drained or blocked
1789 * after blk_mq_stop_hw_queue() returns. Please use
1790 * blk_mq_quiesce_queue() for that requirement.
1791 */
Jens Axboe320ae512013-10-24 09:20:05 +01001792void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1793{
Ming Lei641a9ed2017-06-06 23:22:10 +08001794 cancel_delayed_work(&hctx->run_work);
1795
1796 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboe320ae512013-10-24 09:20:05 +01001797}
1798EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1799
Ming Lei39a70c72017-06-06 23:22:09 +08001800/*
1801 * This function is often used for pausing .queue_rq() by driver when
1802 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001803 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001804 *
1805 * We do not guarantee that dispatch can be drained or blocked
1806 * after blk_mq_stop_hw_queues() returns. Please use
1807 * blk_mq_quiesce_queue() for that requirement.
1808 */
Jens Axboe2719aa22017-05-03 11:08:14 -06001809void blk_mq_stop_hw_queues(struct request_queue *q)
1810{
Ming Lei641a9ed2017-06-06 23:22:10 +08001811 struct blk_mq_hw_ctx *hctx;
1812 int i;
1813
1814 queue_for_each_hw_ctx(q, hctx, i)
1815 blk_mq_stop_hw_queue(hctx);
Christoph Hellwig280d45f2013-10-25 14:45:58 +01001816}
1817EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1818
Jens Axboe320ae512013-10-24 09:20:05 +01001819void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1820{
1821 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -06001822
Jens Axboe0ffbce82014-06-25 08:22:34 -06001823 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001824}
1825EXPORT_SYMBOL(blk_mq_start_hw_queue);
1826
Christoph Hellwig2f268552014-04-16 09:44:56 +02001827void blk_mq_start_hw_queues(struct request_queue *q)
1828{
1829 struct blk_mq_hw_ctx *hctx;
1830 int i;
1831
1832 queue_for_each_hw_ctx(q, hctx, i)
1833 blk_mq_start_hw_queue(hctx);
1834}
1835EXPORT_SYMBOL(blk_mq_start_hw_queues);
1836
Jens Axboeae911c52016-12-08 13:19:30 -07001837void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1838{
1839 if (!blk_mq_hctx_stopped(hctx))
1840 return;
1841
1842 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1843 blk_mq_run_hw_queue(hctx, async);
1844}
1845EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1846
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001847void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001848{
1849 struct blk_mq_hw_ctx *hctx;
1850 int i;
1851
Jens Axboeae911c52016-12-08 13:19:30 -07001852 queue_for_each_hw_ctx(q, hctx, i)
1853 blk_mq_start_stopped_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001854}
1855EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1856
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001857static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001858{
1859 struct blk_mq_hw_ctx *hctx;
1860
Jens Axboe9f993732017-04-10 09:54:54 -06001861 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboe21c6e932017-04-10 09:54:56 -06001862
1863 /*
Ming Lei15fe8a902018-04-08 17:48:11 +08001864 * If we are stopped, don't run the queue.
Jens Axboe21c6e932017-04-10 09:54:56 -06001865 */
Yufen Yu08410312020-10-08 23:26:30 -04001866 if (blk_mq_hctx_stopped(hctx))
Jianchao Wang0196d6b2018-06-04 17:03:55 +08001867 return;
Jens Axboee4043dc2014-04-09 10:18:23 -06001868
Jens Axboe320ae512013-10-24 09:20:05 +01001869 __blk_mq_run_hw_queue(hctx);
1870}
1871
Ming Leicfd0c552015-10-20 23:13:57 +08001872static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08001873 struct request *rq,
1874 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01001875{
Jens Axboee57690f2016-08-24 15:34:35 -06001876 struct blk_mq_ctx *ctx = rq->mq_ctx;
Ming Leic16d6b52018-12-17 08:44:05 -07001877 enum hctx_type type = hctx->type;
Jens Axboee57690f2016-08-24 15:34:35 -06001878
Bart Van Assche7b607812017-06-20 11:15:47 -07001879 lockdep_assert_held(&ctx->lock);
1880
Jens Axboe01b983c2013-11-19 18:59:10 -07001881 trace_block_rq_insert(hctx->queue, rq);
1882
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001883 if (at_head)
Ming Leic16d6b52018-12-17 08:44:05 -07001884 list_add(&rq->queuelist, &ctx->rq_lists[type]);
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001885 else
Ming Leic16d6b52018-12-17 08:44:05 -07001886 list_add_tail(&rq->queuelist, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08001887}
Jens Axboe4bb659b2014-05-09 09:36:49 -06001888
Jens Axboe2c3ad662016-12-14 14:34:47 -07001889void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1890 bool at_head)
Ming Leicfd0c552015-10-20 23:13:57 +08001891{
1892 struct blk_mq_ctx *ctx = rq->mq_ctx;
1893
Bart Van Assche7b607812017-06-20 11:15:47 -07001894 lockdep_assert_held(&ctx->lock);
1895
Jens Axboee57690f2016-08-24 15:34:35 -06001896 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01001897 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001898}
1899
André Almeida105663f2020-01-06 15:08:18 -03001900/**
1901 * blk_mq_request_bypass_insert - Insert a request at dispatch list.
1902 * @rq: Pointer to request to be inserted.
Randy Dunlap26bfeb22020-08-16 16:39:34 -07001903 * @at_head: true if the request should be inserted at the head of the list.
André Almeida105663f2020-01-06 15:08:18 -03001904 * @run_queue: If we should run the hardware queue after inserting the request.
1905 *
Jens Axboe157f3772017-09-11 16:43:57 -06001906 * Should only be used carefully, when the caller knows we want to
1907 * bypass a potential IO scheduler on the target device.
1908 */
Ming Lei01e99ae2020-02-25 09:04:32 +08001909void blk_mq_request_bypass_insert(struct request *rq, bool at_head,
1910 bool run_queue)
Jens Axboe157f3772017-09-11 16:43:57 -06001911{
Jens Axboeea4f9952018-10-29 15:06:13 -06001912 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe157f3772017-09-11 16:43:57 -06001913
1914 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001915 if (at_head)
1916 list_add(&rq->queuelist, &hctx->dispatch);
1917 else
1918 list_add_tail(&rq->queuelist, &hctx->dispatch);
Jens Axboe157f3772017-09-11 16:43:57 -06001919 spin_unlock(&hctx->lock);
1920
Ming Leib0850292017-11-02 23:24:34 +08001921 if (run_queue)
1922 blk_mq_run_hw_queue(hctx, false);
Jens Axboe157f3772017-09-11 16:43:57 -06001923}
1924
Jens Axboebd166ef2017-01-17 06:03:22 -07001925void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
1926 struct list_head *list)
Jens Axboe320ae512013-10-24 09:20:05 +01001927
1928{
Ming Lei3f0cedc2018-07-02 17:35:58 +08001929 struct request *rq;
Ming Leic16d6b52018-12-17 08:44:05 -07001930 enum hctx_type type = hctx->type;
Ming Lei3f0cedc2018-07-02 17:35:58 +08001931
Jens Axboe320ae512013-10-24 09:20:05 +01001932 /*
1933 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1934 * offline now
1935 */
Ming Lei3f0cedc2018-07-02 17:35:58 +08001936 list_for_each_entry(rq, list, queuelist) {
Jens Axboee57690f2016-08-24 15:34:35 -06001937 BUG_ON(rq->mq_ctx != ctx);
Ming Lei3f0cedc2018-07-02 17:35:58 +08001938 trace_block_rq_insert(hctx->queue, rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001939 }
Ming Lei3f0cedc2018-07-02 17:35:58 +08001940
1941 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001942 list_splice_tail_init(list, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08001943 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001944 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001945}
1946
Jens Axboe3110fc72018-10-30 12:24:04 -06001947static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
Jens Axboe320ae512013-10-24 09:20:05 +01001948{
1949 struct request *rqa = container_of(a, struct request, queuelist);
1950 struct request *rqb = container_of(b, struct request, queuelist);
1951
Pavel Begunkov7d30a622019-11-29 00:11:53 +03001952 if (rqa->mq_ctx != rqb->mq_ctx)
1953 return rqa->mq_ctx > rqb->mq_ctx;
1954 if (rqa->mq_hctx != rqb->mq_hctx)
1955 return rqa->mq_hctx > rqb->mq_hctx;
Jens Axboe3110fc72018-10-30 12:24:04 -06001956
1957 return blk_rq_pos(rqa) > blk_rq_pos(rqb);
Jens Axboe320ae512013-10-24 09:20:05 +01001958}
1959
1960void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1961{
Jens Axboe320ae512013-10-24 09:20:05 +01001962 LIST_HEAD(list);
Jens Axboe320ae512013-10-24 09:20:05 +01001963
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001964 if (list_empty(&plug->mq_list))
1965 return;
Jens Axboe320ae512013-10-24 09:20:05 +01001966 list_splice_init(&plug->mq_list, &list);
1967
Jens Axboece5b0092018-11-27 17:13:56 -07001968 if (plug->rq_count > 2 && plug->multiple_queues)
1969 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe320ae512013-10-24 09:20:05 +01001970
Dongli Zhangbcc816d2019-04-04 10:57:44 +08001971 plug->rq_count = 0;
1972
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001973 do {
1974 struct list_head rq_list;
1975 struct request *rq, *head_rq = list_entry_rq(list.next);
1976 struct list_head *pos = &head_rq->queuelist; /* skip first */
1977 struct blk_mq_hw_ctx *this_hctx = head_rq->mq_hctx;
1978 struct blk_mq_ctx *this_ctx = head_rq->mq_ctx;
1979 unsigned int depth = 1;
Jens Axboe320ae512013-10-24 09:20:05 +01001980
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001981 list_for_each_continue(pos, &list) {
1982 rq = list_entry_rq(pos);
1983 BUG_ON(!rq->q);
1984 if (rq->mq_hctx != this_hctx || rq->mq_ctx != this_ctx)
1985 break;
1986 depth++;
Jens Axboe320ae512013-10-24 09:20:05 +01001987 }
1988
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001989 list_cut_before(&rq_list, &list, pos);
1990 trace_block_unplug(head_rq->q, depth, !from_schedule);
Jens Axboe67cae4c2018-10-30 11:31:51 -06001991 blk_mq_sched_insert_requests(this_hctx, this_ctx, &rq_list,
Jens Axboebd166ef2017-01-17 06:03:22 -07001992 from_schedule);
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001993 } while(!list_empty(&list));
Jens Axboe320ae512013-10-24 09:20:05 +01001994}
1995
Christoph Hellwig14ccb662019-06-06 12:29:01 +02001996static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
1997 unsigned int nr_segs)
Jens Axboe320ae512013-10-24 09:20:05 +01001998{
Eric Biggers93f221a2020-09-15 20:53:14 -07001999 int err;
2000
Christoph Hellwigf924cdd2019-06-06 12:29:00 +02002001 if (bio->bi_opf & REQ_RAHEAD)
2002 rq->cmd_flags |= REQ_FAILFAST_MASK;
2003
2004 rq->__sector = bio->bi_iter.bi_sector;
2005 rq->write_hint = bio->bi_write_hint;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002006 blk_rq_bio_prep(rq, bio, nr_segs);
Eric Biggers93f221a2020-09-15 20:53:14 -07002007
2008 /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */
2009 err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO);
2010 WARN_ON_ONCE(err);
Jens Axboe4b570522014-05-29 11:00:11 -06002011
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +02002012 blk_account_io_start(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01002013}
2014
Mike Snitzer0f955492018-01-17 11:25:56 -05002015static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
2016 struct request *rq,
Jens Axboebe94f052018-11-24 10:15:46 -07002017 blk_qc_t *cookie, bool last)
Shaohua Lif984df12015-05-08 10:51:32 -07002018{
Shaohua Lif984df12015-05-08 10:51:32 -07002019 struct request_queue *q = rq->q;
Shaohua Lif984df12015-05-08 10:51:32 -07002020 struct blk_mq_queue_data bd = {
2021 .rq = rq,
Jens Axboebe94f052018-11-24 10:15:46 -07002022 .last = last,
Shaohua Lif984df12015-05-08 10:51:32 -07002023 };
Jens Axboebd166ef2017-01-17 06:03:22 -07002024 blk_qc_t new_cookie;
Jens Axboef06345a2017-06-12 11:22:46 -06002025 blk_status_t ret;
Mike Snitzer0f955492018-01-17 11:25:56 -05002026
2027 new_cookie = request_to_qc_t(hctx, rq);
2028
2029 /*
2030 * For OK queue, we are done. For error, caller may kill it.
2031 * Any other error (busy), just add it to our list as we
2032 * previously would have done.
2033 */
2034 ret = q->mq_ops->queue_rq(hctx, &bd);
2035 switch (ret) {
2036 case BLK_STS_OK:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002037 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05002038 *cookie = new_cookie;
2039 break;
2040 case BLK_STS_RESOURCE:
Ming Lei86ff7c22018-01-30 22:04:57 -05002041 case BLK_STS_DEV_RESOURCE:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002042 blk_mq_update_dispatch_busy(hctx, true);
Mike Snitzer0f955492018-01-17 11:25:56 -05002043 __blk_mq_requeue_request(rq);
2044 break;
2045 default:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002046 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05002047 *cookie = BLK_QC_T_NONE;
2048 break;
2049 }
2050
2051 return ret;
2052}
2053
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002054static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
Mike Snitzer0f955492018-01-17 11:25:56 -05002055 struct request *rq,
Ming Lei396eaf22018-01-17 11:25:57 -05002056 blk_qc_t *cookie,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002057 bool bypass_insert, bool last)
Mike Snitzer0f955492018-01-17 11:25:56 -05002058{
2059 struct request_queue *q = rq->q;
Ming Leid964f042017-06-06 23:22:00 +08002060 bool run_queue = true;
2061
Ming Lei23d4ee12018-01-18 12:06:59 +08002062 /*
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002063 * RCU or SRCU read lock is needed before checking quiesced flag.
Ming Lei23d4ee12018-01-18 12:06:59 +08002064 *
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002065 * When queue is stopped or quiesced, ignore 'bypass_insert' from
2066 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
2067 * and avoid driver to try to dispatch again.
Ming Lei23d4ee12018-01-18 12:06:59 +08002068 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002069 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
Ming Leid964f042017-06-06 23:22:00 +08002070 run_queue = false;
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002071 bypass_insert = false;
2072 goto insert;
Ming Leid964f042017-06-06 23:22:00 +08002073 }
Shaohua Lif984df12015-05-08 10:51:32 -07002074
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002075 if (q->elevator && !bypass_insert)
2076 goto insert;
Bart Van Assche2253efc2016-10-28 17:20:02 -07002077
Ming Lei65c76362020-06-30 18:24:56 +08002078 if (!blk_mq_get_dispatch_budget(q))
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002079 goto insert;
Jens Axboebd166ef2017-01-17 06:03:22 -07002080
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08002081 if (!blk_mq_get_driver_tag(rq)) {
Ming Lei65c76362020-06-30 18:24:56 +08002082 blk_mq_put_dispatch_budget(q);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002083 goto insert;
Ming Lei88022d72017-11-05 02:21:12 +08002084 }
Ming Leide148292017-10-14 17:22:29 +08002085
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002086 return __blk_mq_issue_directly(hctx, rq, cookie, last);
2087insert:
2088 if (bypass_insert)
2089 return BLK_STS_RESOURCE;
2090
Ming Leidb03f882020-08-18 17:07:28 +08002091 blk_mq_sched_insert_request(rq, false, run_queue, false);
2092
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002093 return BLK_STS_OK;
2094}
2095
André Almeida105663f2020-01-06 15:08:18 -03002096/**
2097 * blk_mq_try_issue_directly - Try to send a request directly to device driver.
2098 * @hctx: Pointer of the associated hardware queue.
2099 * @rq: Pointer to request to be sent.
2100 * @cookie: Request queue cookie.
2101 *
2102 * If the device has enough resources to accept a new request now, send the
2103 * request directly to device driver. Else, insert at hctx->dispatch queue, so
2104 * we can try send it another time in the future. Requests inserted at this
2105 * queue have higher priority.
2106 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002107static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
2108 struct request *rq, blk_qc_t *cookie)
2109{
2110 blk_status_t ret;
2111 int srcu_idx;
2112
2113 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
2114
2115 hctx_lock(hctx, &srcu_idx);
2116
2117 ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false, true);
2118 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
Ming Lei01e99ae2020-02-25 09:04:32 +08002119 blk_mq_request_bypass_insert(rq, false, true);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002120 else if (ret != BLK_STS_OK)
2121 blk_mq_end_request(rq, ret);
2122
Jens Axboe04ced152018-01-09 08:29:46 -08002123 hctx_unlock(hctx, srcu_idx);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002124}
2125
2126blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
2127{
2128 blk_status_t ret;
2129 int srcu_idx;
2130 blk_qc_t unused_cookie;
2131 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
2132
2133 hctx_lock(hctx, &srcu_idx);
2134 ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true, last);
2135 hctx_unlock(hctx, srcu_idx);
Jianchao Wang7f556a42018-12-14 09:28:18 +08002136
2137 return ret;
Christoph Hellwig5eb61262017-03-22 15:01:51 -04002138}
2139
Ming Lei6ce3dd62018-07-10 09:03:31 +08002140void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
2141 struct list_head *list)
2142{
Keith Busch536167d42020-04-07 03:13:48 +09002143 int queued = 0;
yangerkun632bfb62020-09-05 19:25:56 +08002144 int errors = 0;
Keith Busch536167d42020-04-07 03:13:48 +09002145
Ming Lei6ce3dd62018-07-10 09:03:31 +08002146 while (!list_empty(list)) {
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002147 blk_status_t ret;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002148 struct request *rq = list_first_entry(list, struct request,
2149 queuelist);
2150
2151 list_del_init(&rq->queuelist);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002152 ret = blk_mq_request_issue_directly(rq, list_empty(list));
2153 if (ret != BLK_STS_OK) {
2154 if (ret == BLK_STS_RESOURCE ||
2155 ret == BLK_STS_DEV_RESOURCE) {
Ming Lei01e99ae2020-02-25 09:04:32 +08002156 blk_mq_request_bypass_insert(rq, false,
Jens Axboec616cbe2018-12-06 22:17:44 -07002157 list_empty(list));
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002158 break;
2159 }
2160 blk_mq_end_request(rq, ret);
yangerkun632bfb62020-09-05 19:25:56 +08002161 errors++;
Keith Busch536167d42020-04-07 03:13:48 +09002162 } else
2163 queued++;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002164 }
Jens Axboed666ba92018-11-27 17:02:25 -07002165
2166 /*
2167 * If we didn't flush the entire list, we could have told
2168 * the driver there was more coming, but that turned out to
2169 * be a lie.
2170 */
yangerkun632bfb62020-09-05 19:25:56 +08002171 if ((!list_empty(list) || errors) &&
2172 hctx->queue->mq_ops->commit_rqs && queued)
Jens Axboed666ba92018-11-27 17:02:25 -07002173 hctx->queue->mq_ops->commit_rqs(hctx);
Ming Lei6ce3dd62018-07-10 09:03:31 +08002174}
2175
Jens Axboece5b0092018-11-27 17:13:56 -07002176static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
2177{
2178 list_add_tail(&rq->queuelist, &plug->mq_list);
2179 plug->rq_count++;
2180 if (!plug->multiple_queues && !list_is_singular(&plug->mq_list)) {
2181 struct request *tmp;
2182
2183 tmp = list_first_entry(&plug->mq_list, struct request,
2184 queuelist);
2185 if (tmp->q != rq->q)
2186 plug->multiple_queues = true;
2187 }
2188}
2189
André Almeida105663f2020-01-06 15:08:18 -03002190/**
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02002191 * blk_mq_submit_bio - Create and send a request to block device.
André Almeida105663f2020-01-06 15:08:18 -03002192 * @bio: Bio pointer.
2193 *
2194 * Builds up a request structure from @q and @bio and send to the device. The
2195 * request may not be queued directly to hardware if:
2196 * * This request can be merged with another one
2197 * * We want to place request at plug queue for possible future merging
2198 * * There is an IO scheduler active at this queue
2199 *
2200 * It will not queue the request if there is an error with the bio, or at the
2201 * request creation.
2202 *
2203 * Returns: Request queue cookie.
2204 */
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02002205blk_qc_t blk_mq_submit_bio(struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06002206{
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02002207 struct request_queue *q = bio->bi_disk->queue;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002208 const int is_sync = op_is_sync(bio->bi_opf);
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07002209 const int is_flush_fua = op_is_flush(bio->bi_opf);
Christoph Hellwige6e7abf2020-05-29 15:53:09 +02002210 struct blk_mq_alloc_data data = {
2211 .q = q,
2212 };
Jens Axboe07068d52014-05-22 10:40:51 -06002213 struct request *rq;
Shaohua Lif984df12015-05-08 10:51:32 -07002214 struct blk_plug *plug;
Shaohua Li5b3f3412015-05-08 10:51:33 -07002215 struct request *same_queue_rq = NULL;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002216 unsigned int nr_segs;
Jens Axboe7b371632015-11-05 10:41:40 -07002217 blk_qc_t cookie;
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002218 blk_status_t ret;
Jens Axboe07068d52014-05-22 10:40:51 -06002219
2220 blk_queue_bounce(q, &bio);
Christoph Hellwigf695ca32020-07-01 10:59:39 +02002221 __blk_queue_split(&bio, &nr_segs);
Wen Xiongf36ea502017-05-10 08:54:11 -05002222
Dmitry Monakhove23947b2017-06-29 11:31:11 -07002223 if (!bio_integrity_prep(bio))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002224 goto queue_exit;
Jens Axboe07068d52014-05-22 10:40:51 -06002225
Omar Sandoval87c279e2016-06-01 22:18:48 -07002226 if (!is_flush_fua && !blk_queue_nomerges(q) &&
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002227 blk_attempt_plug_merge(q, bio, nr_segs, &same_queue_rq))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002228 goto queue_exit;
Shaohua Lif984df12015-05-08 10:51:32 -07002229
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002230 if (blk_mq_sched_bio_merge(q, bio, nr_segs))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002231 goto queue_exit;
Jens Axboebd166ef2017-01-17 06:03:22 -07002232
Christoph Hellwigd5337562018-11-14 17:02:09 +01002233 rq_qos_throttle(q, bio);
Jens Axboe87760e52016-11-09 12:38:14 -07002234
Ming Lei78091672019-01-16 19:08:15 +08002235 data.cmd_flags = bio->bi_opf;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +02002236 rq = __blk_mq_alloc_request(&data);
Jens Axboe87760e52016-11-09 12:38:14 -07002237 if (unlikely(!rq)) {
Josef Bacikc1c80382018-07-03 11:14:59 -04002238 rq_qos_cleanup(q, bio);
Jens Axboe7b6620d2019-08-15 11:09:16 -06002239 if (bio->bi_opf & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -05002240 bio_wouldblock_error(bio);
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002241 goto queue_exit;
Jens Axboe87760e52016-11-09 12:38:14 -07002242 }
2243
Xiaoguang Wangd6f1dda2018-10-23 22:30:50 +08002244 trace_block_getrq(q, bio, bio->bi_opf);
2245
Josef Bacikc1c80382018-07-03 11:14:59 -04002246 rq_qos_track(q, rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06002247
Jens Axboefd2d3322017-01-12 10:04:45 -07002248 cookie = request_to_qc_t(data.hctx, rq);
Jens Axboe07068d52014-05-22 10:40:51 -06002249
Bart Van Assche970d1682019-07-01 08:47:30 -07002250 blk_mq_bio_to_request(rq, bio, nr_segs);
2251
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002252 ret = blk_crypto_init_request(rq);
2253 if (ret != BLK_STS_OK) {
2254 bio->bi_status = ret;
2255 bio_endio(bio);
2256 blk_mq_free_request(rq);
2257 return BLK_QC_T_NONE;
2258 }
2259
Damien Le Moalb49773e72019-07-11 01:18:31 +09002260 plug = blk_mq_plug(q, bio);
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04002261 if (unlikely(is_flush_fua)) {
André Almeida105663f2020-01-06 15:08:18 -03002262 /* Bypass scheduler for flush requests */
Ming Lei923218f2017-11-02 23:24:38 +08002263 blk_insert_flush(rq);
2264 blk_mq_run_hw_queue(data.hctx, true);
Ming Leic9c1ed02021-05-14 10:20:52 +08002265 } else if (plug && (q->nr_hw_queues == 1 ||
2266 blk_mq_is_sbitmap_shared(rq->mq_hctx->flags) ||
2267 q->mq_ops->commit_rqs || !blk_queue_nonrot(q))) {
Jens Axboeb2c5d162018-11-29 10:03:42 -07002268 /*
2269 * Use plugging if we have a ->commit_rqs() hook as well, as
2270 * we know the driver uses bd->last in a smart fashion.
Ming Lei3154df22019-09-27 15:24:31 +08002271 *
2272 * Use normal plugging if this disk is slow HDD, as sequential
2273 * IO may benefit a lot from plug merging.
Jens Axboeb2c5d162018-11-29 10:03:42 -07002274 */
Jens Axboe5f0ed772018-11-23 22:04:33 -07002275 unsigned int request_count = plug->rq_count;
Shaohua Li600271d2016-11-03 17:03:54 -07002276 struct request *last = NULL;
2277
Ming Lei676d0602015-10-20 23:13:56 +08002278 if (!request_count)
Jeff Moyere6c44382015-05-08 10:51:30 -07002279 trace_block_plug(q);
Shaohua Li600271d2016-11-03 17:03:54 -07002280 else
2281 last = list_entry_rq(plug->mq_list.prev);
Jens Axboeb094f892015-11-20 20:29:45 -07002282
Shaohua Li600271d2016-11-03 17:03:54 -07002283 if (request_count >= BLK_MAX_REQUEST_COUNT || (last &&
2284 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
Jeff Moyere6c44382015-05-08 10:51:30 -07002285 blk_flush_plug_list(plug, false);
2286 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002287 }
Jens Axboeb094f892015-11-20 20:29:45 -07002288
Jens Axboece5b0092018-11-27 17:13:56 -07002289 blk_add_rq_to_plug(plug, rq);
Ming Leia12de1d2019-09-27 15:24:30 +08002290 } else if (q->elevator) {
André Almeida105663f2020-01-06 15:08:18 -03002291 /* Insert the request at the IO scheduler queue */
Ming Leia12de1d2019-09-27 15:24:30 +08002292 blk_mq_sched_insert_request(rq, false, true, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002293 } else if (plug && !blk_queue_nomerges(q)) {
Jens Axboe320ae512013-10-24 09:20:05 +01002294 /*
2295 * We do limited plugging. If the bio can be merged, do that.
2296 * Otherwise the existing request in the plug list will be
2297 * issued. So the plug list will have one request at most
Christoph Hellwig22997222017-03-22 15:01:52 -04002298 * The plug list might get flushed before this. If that happens,
2299 * the plug list is empty, and same_queue_rq is invalid.
Jens Axboe320ae512013-10-24 09:20:05 +01002300 */
Christoph Hellwig22997222017-03-22 15:01:52 -04002301 if (list_empty(&plug->mq_list))
2302 same_queue_rq = NULL;
Jens Axboe4711b572018-11-27 17:07:17 -07002303 if (same_queue_rq) {
Christoph Hellwig22997222017-03-22 15:01:52 -04002304 list_del_init(&same_queue_rq->queuelist);
Jens Axboe4711b572018-11-27 17:07:17 -07002305 plug->rq_count--;
2306 }
Jens Axboece5b0092018-11-27 17:13:56 -07002307 blk_add_rq_to_plug(plug, rq);
Yufen Yuff3b74b2019-03-26 21:19:25 +08002308 trace_block_plug(q);
Christoph Hellwig22997222017-03-22 15:01:52 -04002309
Ming Leidad7a3b2017-06-06 23:21:59 +08002310 if (same_queue_rq) {
Jens Axboeea4f9952018-10-29 15:06:13 -06002311 data.hctx = same_queue_rq->mq_hctx;
Yufen Yuff3b74b2019-03-26 21:19:25 +08002312 trace_block_unplug(q, 1, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002313 blk_mq_try_issue_directly(data.hctx, same_queue_rq,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002314 &cookie);
Ming Leidad7a3b2017-06-06 23:21:59 +08002315 }
Ming Leia12de1d2019-09-27 15:24:30 +08002316 } else if ((q->nr_hw_queues > 1 && is_sync) ||
2317 !data.hctx->dispatch_busy) {
André Almeida105663f2020-01-06 15:08:18 -03002318 /*
2319 * There is no scheduler and we can try to send directly
2320 * to the hardware.
2321 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002322 blk_mq_try_issue_directly(data.hctx, rq, &cookie);
Ming Leiab42f352017-05-26 19:53:19 +08002323 } else {
André Almeida105663f2020-01-06 15:08:18 -03002324 /* Default case. */
huhai8fa9f552018-05-16 08:21:21 -06002325 blk_mq_sched_insert_request(rq, false, true, true);
Ming Leiab42f352017-05-26 19:53:19 +08002326 }
Jens Axboe320ae512013-10-24 09:20:05 +01002327
Jens Axboe7b371632015-11-05 10:41:40 -07002328 return cookie;
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002329queue_exit:
2330 blk_queue_exit(q);
2331 return BLK_QC_T_NONE;
Jens Axboe320ae512013-10-24 09:20:05 +01002332}
2333
Ming Lei72018552021-05-11 23:22:35 +08002334static size_t order_to_size(unsigned int order)
2335{
2336 return (size_t)PAGE_SIZE << order;
2337}
2338
2339/* called before freeing request pool in @tags */
2340static void blk_mq_clear_rq_mapping(struct blk_mq_tag_set *set,
2341 struct blk_mq_tags *tags, unsigned int hctx_idx)
2342{
2343 struct blk_mq_tags *drv_tags = set->tags[hctx_idx];
2344 struct page *page;
2345 unsigned long flags;
2346
2347 list_for_each_entry(page, &tags->page_list, lru) {
2348 unsigned long start = (unsigned long)page_address(page);
2349 unsigned long end = start + order_to_size(page->private);
2350 int i;
2351
2352 for (i = 0; i < set->queue_depth; i++) {
2353 struct request *rq = drv_tags->rqs[i];
2354 unsigned long rq_addr = (unsigned long)rq;
2355
2356 if (rq_addr >= start && rq_addr < end) {
2357 WARN_ON_ONCE(refcount_read(&rq->ref) != 0);
2358 cmpxchg(&drv_tags->rqs[i], rq, NULL);
2359 }
2360 }
2361 }
2362
2363 /*
2364 * Wait until all pending iteration is done.
2365 *
2366 * Request reference is cleared and it is guaranteed to be observed
2367 * after the ->lock is released.
2368 */
2369 spin_lock_irqsave(&drv_tags->lock, flags);
2370 spin_unlock_irqrestore(&drv_tags->lock, flags);
2371}
2372
Jens Axboecc71a6f2017-01-11 14:29:56 -07002373void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2374 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01002375{
2376 struct page *page;
2377
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002378 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002379 int i;
2380
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002381 for (i = 0; i < tags->nr_tags; i++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002382 struct request *rq = tags->static_rqs[i];
2383
2384 if (!rq)
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002385 continue;
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002386 set->ops->exit_request(set, rq, hctx_idx);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002387 tags->static_rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002388 }
2389 }
2390
Ming Lei72018552021-05-11 23:22:35 +08002391 blk_mq_clear_rq_mapping(set, tags, hctx_idx);
2392
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002393 while (!list_empty(&tags->page_list)) {
2394 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07002395 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002396 /*
2397 * Remove kmemleak object previously allocated in
Raul E Rangel273938b2019-05-02 13:48:11 -06002398 * blk_mq_alloc_rqs().
Catalin Marinasf75782e2015-09-14 18:16:02 +01002399 */
2400 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01002401 __free_pages(page, page->private);
2402 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002403}
Jens Axboe320ae512013-10-24 09:20:05 +01002404
John Garry1c0706a2020-08-19 23:20:22 +08002405void blk_mq_free_rq_map(struct blk_mq_tags *tags, unsigned int flags)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002406{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002407 kfree(tags->rqs);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002408 tags->rqs = NULL;
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002409 kfree(tags->static_rqs);
2410 tags->static_rqs = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002411
John Garry1c0706a2020-08-19 23:20:22 +08002412 blk_mq_free_tags(tags, flags);
Jens Axboe320ae512013-10-24 09:20:05 +01002413}
2414
Jens Axboecc71a6f2017-01-11 14:29:56 -07002415struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
2416 unsigned int hctx_idx,
2417 unsigned int nr_tags,
John Garry1c0706a2020-08-19 23:20:22 +08002418 unsigned int reserved_tags,
2419 unsigned int flags)
Jens Axboe320ae512013-10-24 09:20:05 +01002420{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002421 struct blk_mq_tags *tags;
Shaohua Li59f082e2017-02-01 09:53:14 -08002422 int node;
Jens Axboe320ae512013-10-24 09:20:05 +01002423
Dongli Zhang7d76f852019-02-27 21:35:01 +08002424 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002425 if (node == NUMA_NO_NODE)
2426 node = set->numa_node;
2427
John Garry1c0706a2020-08-19 23:20:22 +08002428 tags = blk_mq_init_tags(nr_tags, reserved_tags, node, flags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002429 if (!tags)
2430 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002431
Kees Cook590b5b72018-06-12 14:04:20 -07002432 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002433 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
Shaohua Li59f082e2017-02-01 09:53:14 -08002434 node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002435 if (!tags->rqs) {
John Garry1c0706a2020-08-19 23:20:22 +08002436 blk_mq_free_tags(tags, flags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002437 return NULL;
2438 }
Jens Axboe320ae512013-10-24 09:20:05 +01002439
Kees Cook590b5b72018-06-12 14:04:20 -07002440 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2441 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2442 node);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002443 if (!tags->static_rqs) {
2444 kfree(tags->rqs);
John Garry1c0706a2020-08-19 23:20:22 +08002445 blk_mq_free_tags(tags, flags);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002446 return NULL;
2447 }
2448
Jens Axboecc71a6f2017-01-11 14:29:56 -07002449 return tags;
2450}
2451
Tejun Heo1d9bd512018-01-09 08:29:48 -08002452static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2453 unsigned int hctx_idx, int node)
2454{
2455 int ret;
2456
2457 if (set->ops->init_request) {
2458 ret = set->ops->init_request(set, rq, hctx_idx, node);
2459 if (ret)
2460 return ret;
2461 }
2462
Keith Busch12f5b932018-05-29 15:52:28 +02002463 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Tejun Heo1d9bd512018-01-09 08:29:48 -08002464 return 0;
2465}
2466
Jens Axboecc71a6f2017-01-11 14:29:56 -07002467int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2468 unsigned int hctx_idx, unsigned int depth)
2469{
2470 unsigned int i, j, entries_per_page, max_order = 4;
2471 size_t rq_size, left;
Shaohua Li59f082e2017-02-01 09:53:14 -08002472 int node;
2473
Dongli Zhang7d76f852019-02-27 21:35:01 +08002474 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002475 if (node == NUMA_NO_NODE)
2476 node = set->numa_node;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002477
2478 INIT_LIST_HEAD(&tags->page_list);
2479
Jens Axboe320ae512013-10-24 09:20:05 +01002480 /*
2481 * rq_size is the size of the request plus driver payload, rounded
2482 * to the cacheline size
2483 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002484 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01002485 cache_line_size());
Jens Axboecc71a6f2017-01-11 14:29:56 -07002486 left = rq_size * depth;
Jens Axboe320ae512013-10-24 09:20:05 +01002487
Jens Axboecc71a6f2017-01-11 14:29:56 -07002488 for (i = 0; i < depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01002489 int this_order = max_order;
2490 struct page *page;
2491 int to_do;
2492 void *p;
2493
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06002494 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01002495 this_order--;
2496
2497 do {
Shaohua Li59f082e2017-02-01 09:53:14 -08002498 page = alloc_pages_node(node,
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002499 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06002500 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01002501 if (page)
2502 break;
2503 if (!this_order--)
2504 break;
2505 if (order_to_size(this_order) < rq_size)
2506 break;
2507 } while (1);
2508
2509 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002510 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01002511
2512 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002513 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01002514
2515 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002516 /*
2517 * Allow kmemleak to scan these pages as they contain pointers
2518 * to additional allocations like via ops->init_request().
2519 */
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002520 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
Jens Axboe320ae512013-10-24 09:20:05 +01002521 entries_per_page = order_to_size(this_order) / rq_size;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002522 to_do = min(entries_per_page, depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01002523 left -= to_do * rq_size;
2524 for (j = 0; j < to_do; j++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002525 struct request *rq = p;
2526
2527 tags->static_rqs[i] = rq;
Tejun Heo1d9bd512018-01-09 08:29:48 -08002528 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
2529 tags->static_rqs[i] = NULL;
2530 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002531 }
2532
Jens Axboe320ae512013-10-24 09:20:05 +01002533 p += rq_size;
2534 i++;
2535 }
2536 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002537 return 0;
Jens Axboe320ae512013-10-24 09:20:05 +01002538
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002539fail:
Jens Axboecc71a6f2017-01-11 14:29:56 -07002540 blk_mq_free_rqs(set, tags, hctx_idx);
2541 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01002542}
2543
Ming Leibf0beec2020-05-29 15:53:15 +02002544struct rq_iter_data {
2545 struct blk_mq_hw_ctx *hctx;
2546 bool has_rq;
2547};
2548
2549static bool blk_mq_has_request(struct request *rq, void *data, bool reserved)
2550{
2551 struct rq_iter_data *iter_data = data;
2552
2553 if (rq->mq_hctx != iter_data->hctx)
2554 return true;
2555 iter_data->has_rq = true;
2556 return false;
2557}
2558
2559static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
2560{
2561 struct blk_mq_tags *tags = hctx->sched_tags ?
2562 hctx->sched_tags : hctx->tags;
2563 struct rq_iter_data data = {
2564 .hctx = hctx,
2565 };
2566
2567 blk_mq_all_tag_iter(tags, blk_mq_has_request, &data);
2568 return data.has_rq;
2569}
2570
2571static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu,
2572 struct blk_mq_hw_ctx *hctx)
2573{
2574 if (cpumask_next_and(-1, hctx->cpumask, cpu_online_mask) != cpu)
2575 return false;
2576 if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids)
2577 return false;
2578 return true;
2579}
2580
2581static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
2582{
2583 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2584 struct blk_mq_hw_ctx, cpuhp_online);
2585
2586 if (!cpumask_test_cpu(cpu, hctx->cpumask) ||
2587 !blk_mq_last_cpu_in_hctx(cpu, hctx))
2588 return 0;
2589
2590 /*
2591 * Prevent new request from being allocated on the current hctx.
2592 *
2593 * The smp_mb__after_atomic() Pairs with the implied barrier in
2594 * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is
2595 * seen once we return from the tag allocator.
2596 */
2597 set_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2598 smp_mb__after_atomic();
2599
2600 /*
2601 * Try to grab a reference to the queue and wait for any outstanding
2602 * requests. If we could not grab a reference the queue has been
2603 * frozen and there are no requests.
2604 */
2605 if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) {
2606 while (blk_mq_hctx_has_requests(hctx))
2607 msleep(5);
2608 percpu_ref_put(&hctx->queue->q_usage_counter);
2609 }
2610
2611 return 0;
2612}
2613
2614static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node)
2615{
2616 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2617 struct blk_mq_hw_ctx, cpuhp_online);
2618
2619 if (cpumask_test_cpu(cpu, hctx->cpumask))
2620 clear_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2621 return 0;
2622}
2623
Jens Axboee57690f2016-08-24 15:34:35 -06002624/*
2625 * 'cpu' is going away. splice any existing rq_list entries from this
2626 * software queue to the hw queue dispatch list, and ensure that it
2627 * gets run.
2628 */
Thomas Gleixner9467f852016-09-22 08:05:17 -06002629static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
Jens Axboe484b4062014-05-21 14:01:15 -06002630{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002631 struct blk_mq_hw_ctx *hctx;
Jens Axboe484b4062014-05-21 14:01:15 -06002632 struct blk_mq_ctx *ctx;
2633 LIST_HEAD(tmp);
Ming Leic16d6b52018-12-17 08:44:05 -07002634 enum hctx_type type;
Jens Axboe484b4062014-05-21 14:01:15 -06002635
Thomas Gleixner9467f852016-09-22 08:05:17 -06002636 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02002637 if (!cpumask_test_cpu(cpu, hctx->cpumask))
2638 return 0;
2639
Jens Axboee57690f2016-08-24 15:34:35 -06002640 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Ming Leic16d6b52018-12-17 08:44:05 -07002641 type = hctx->type;
Jens Axboe484b4062014-05-21 14:01:15 -06002642
2643 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002644 if (!list_empty(&ctx->rq_lists[type])) {
2645 list_splice_init(&ctx->rq_lists[type], &tmp);
Jens Axboe484b4062014-05-21 14:01:15 -06002646 blk_mq_hctx_clear_pending(hctx, ctx);
2647 }
2648 spin_unlock(&ctx->lock);
2649
2650 if (list_empty(&tmp))
Thomas Gleixner9467f852016-09-22 08:05:17 -06002651 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002652
Jens Axboee57690f2016-08-24 15:34:35 -06002653 spin_lock(&hctx->lock);
2654 list_splice_tail_init(&tmp, &hctx->dispatch);
2655 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06002656
2657 blk_mq_run_hw_queue(hctx, true);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002658 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002659}
2660
Thomas Gleixner9467f852016-09-22 08:05:17 -06002661static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
Jens Axboe484b4062014-05-21 14:01:15 -06002662{
Ming Leibf0beec2020-05-29 15:53:15 +02002663 if (!(hctx->flags & BLK_MQ_F_STACKING))
2664 cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2665 &hctx->cpuhp_online);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002666 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
2667 &hctx->cpuhp_dead);
Jens Axboe484b4062014-05-21 14:01:15 -06002668}
2669
Ming Lei3a173ef2021-05-11 23:22:36 +08002670/*
2671 * Before freeing hw queue, clearing the flush request reference in
2672 * tags->rqs[] for avoiding potential UAF.
2673 */
2674static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags,
2675 unsigned int queue_depth, struct request *flush_rq)
2676{
2677 int i;
2678 unsigned long flags;
2679
2680 /* The hw queue may not be mapped yet */
2681 if (!tags)
2682 return;
2683
2684 WARN_ON_ONCE(refcount_read(&flush_rq->ref) != 0);
2685
2686 for (i = 0; i < queue_depth; i++)
2687 cmpxchg(&tags->rqs[i], flush_rq, NULL);
2688
2689 /*
2690 * Wait until all pending iteration is done.
2691 *
2692 * Request reference is cleared and it is guaranteed to be observed
2693 * after the ->lock is released.
2694 */
2695 spin_lock_irqsave(&tags->lock, flags);
2696 spin_unlock_irqrestore(&tags->lock, flags);
2697}
2698
Ming Leic3b4afc2015-06-04 22:25:04 +08002699/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08002700static void blk_mq_exit_hctx(struct request_queue *q,
2701 struct blk_mq_tag_set *set,
2702 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
2703{
Ming Lei3a173ef2021-05-11 23:22:36 +08002704 struct request *flush_rq = hctx->fq->flush_rq;
2705
Ming Lei8ab0b7d2018-01-09 21:28:29 +08002706 if (blk_mq_hw_queue_mapped(hctx))
2707 blk_mq_tag_idle(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002708
Ming Lei3a173ef2021-05-11 23:22:36 +08002709 blk_mq_clear_flush_rq_mapping(set->tags[hctx_idx],
2710 set->queue_depth, flush_rq);
Ming Leif70ced02014-09-25 23:23:47 +08002711 if (set->ops->exit_request)
Ming Lei3a173ef2021-05-11 23:22:36 +08002712 set->ops->exit_request(set, flush_rq, hctx_idx);
Ming Leif70ced02014-09-25 23:23:47 +08002713
Ming Lei08e98fc2014-09-25 23:23:38 +08002714 if (set->ops->exit_hctx)
2715 set->ops->exit_hctx(hctx, hctx_idx);
2716
Thomas Gleixner9467f852016-09-22 08:05:17 -06002717 blk_mq_remove_cpuhp(hctx);
Ming Lei2f8f1332019-04-30 09:52:27 +08002718
2719 spin_lock(&q->unused_hctx_lock);
2720 list_add(&hctx->hctx_list, &q->unused_hctx_list);
2721 spin_unlock(&q->unused_hctx_lock);
Ming Lei08e98fc2014-09-25 23:23:38 +08002722}
2723
Ming Lei624dbe42014-05-27 23:35:13 +08002724static void blk_mq_exit_hw_queues(struct request_queue *q,
2725 struct blk_mq_tag_set *set, int nr_queue)
2726{
2727 struct blk_mq_hw_ctx *hctx;
2728 unsigned int i;
2729
2730 queue_for_each_hw_ctx(q, hctx, i) {
2731 if (i == nr_queue)
2732 break;
Jianchao Wang477e19d2018-10-12 18:07:25 +08002733 blk_mq_debugfs_unregister_hctx(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002734 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08002735 }
Ming Lei624dbe42014-05-27 23:35:13 +08002736}
2737
Ming Lei7c6c5b72019-04-30 09:52:26 +08002738static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
2739{
2740 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
2741
2742 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
2743 __alignof__(struct blk_mq_hw_ctx)) !=
2744 sizeof(struct blk_mq_hw_ctx));
2745
2746 if (tag_set->flags & BLK_MQ_F_BLOCKING)
2747 hw_ctx_size += sizeof(struct srcu_struct);
2748
2749 return hw_ctx_size;
2750}
2751
Ming Lei08e98fc2014-09-25 23:23:38 +08002752static int blk_mq_init_hctx(struct request_queue *q,
2753 struct blk_mq_tag_set *set,
2754 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
2755{
Ming Lei7c6c5b72019-04-30 09:52:26 +08002756 hctx->queue_num = hctx_idx;
Ming Lei08e98fc2014-09-25 23:23:38 +08002757
Ming Leibf0beec2020-05-29 15:53:15 +02002758 if (!(hctx->flags & BLK_MQ_F_STACKING))
2759 cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2760 &hctx->cpuhp_online);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002761 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
2762
2763 hctx->tags = set->tags[hctx_idx];
2764
2765 if (set->ops->init_hctx &&
2766 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
2767 goto unregister_cpu_notifier;
2768
2769 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
2770 hctx->numa_node))
2771 goto exit_hctx;
2772 return 0;
2773
2774 exit_hctx:
2775 if (set->ops->exit_hctx)
2776 set->ops->exit_hctx(hctx, hctx_idx);
2777 unregister_cpu_notifier:
2778 blk_mq_remove_cpuhp(hctx);
2779 return -1;
2780}
2781
2782static struct blk_mq_hw_ctx *
2783blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
2784 int node)
2785{
2786 struct blk_mq_hw_ctx *hctx;
2787 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
2788
2789 hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node);
2790 if (!hctx)
2791 goto fail_alloc_hctx;
2792
2793 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
2794 goto free_hctx;
2795
2796 atomic_set(&hctx->nr_active, 0);
Ming Lei08e98fc2014-09-25 23:23:38 +08002797 if (node == NUMA_NO_NODE)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002798 node = set->numa_node;
2799 hctx->numa_node = node;
Ming Lei08e98fc2014-09-25 23:23:38 +08002800
Jens Axboe9f993732017-04-10 09:54:54 -06002801 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08002802 spin_lock_init(&hctx->lock);
2803 INIT_LIST_HEAD(&hctx->dispatch);
2804 hctx->queue = q;
Ming Lei51db1c32020-08-19 23:20:19 +08002805 hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08002806
Ming Lei2f8f1332019-04-30 09:52:27 +08002807 INIT_LIST_HEAD(&hctx->hctx_list);
2808
Ming Lei08e98fc2014-09-25 23:23:38 +08002809 /*
2810 * Allocate space for all possible cpus to avoid allocation at
2811 * runtime
2812 */
Johannes Thumshirnd904bfa2017-11-15 17:32:33 -08002813 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
Ming Lei7c6c5b72019-04-30 09:52:26 +08002814 gfp, node);
Ming Lei08e98fc2014-09-25 23:23:38 +08002815 if (!hctx->ctxs)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002816 goto free_cpumask;
Ming Lei08e98fc2014-09-25 23:23:38 +08002817
Jianchao Wang5b202852018-10-12 18:07:26 +08002818 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
Ming Lei7c6c5b72019-04-30 09:52:26 +08002819 gfp, node))
Ming Lei08e98fc2014-09-25 23:23:38 +08002820 goto free_ctxs;
Ming Lei08e98fc2014-09-25 23:23:38 +08002821 hctx->nr_ctx = 0;
2822
Ming Lei5815839b2018-06-25 19:31:47 +08002823 spin_lock_init(&hctx->dispatch_wait_lock);
Jens Axboeeb619fd2017-11-09 08:32:43 -07002824 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
2825 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
2826
Guoqing Jiang754a1572020-03-09 22:41:37 +01002827 hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp);
Ming Leif70ced02014-09-25 23:23:47 +08002828 if (!hctx->fq)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002829 goto free_bitmap;
Ming Leif70ced02014-09-25 23:23:47 +08002830
Bart Van Assche6a83e742016-11-02 10:09:51 -06002831 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -08002832 init_srcu_struct(hctx->srcu);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002833 blk_mq_hctx_kobj_init(hctx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06002834
Ming Lei7c6c5b72019-04-30 09:52:26 +08002835 return hctx;
Ming Lei08e98fc2014-09-25 23:23:38 +08002836
2837 free_bitmap:
Omar Sandoval88459642016-09-17 08:38:44 -06002838 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08002839 free_ctxs:
2840 kfree(hctx->ctxs);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002841 free_cpumask:
2842 free_cpumask_var(hctx->cpumask);
2843 free_hctx:
2844 kfree(hctx);
2845 fail_alloc_hctx:
2846 return NULL;
Ming Lei08e98fc2014-09-25 23:23:38 +08002847}
2848
Jens Axboe320ae512013-10-24 09:20:05 +01002849static void blk_mq_init_cpu_queues(struct request_queue *q,
2850 unsigned int nr_hw_queues)
2851{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002852 struct blk_mq_tag_set *set = q->tag_set;
2853 unsigned int i, j;
Jens Axboe320ae512013-10-24 09:20:05 +01002854
2855 for_each_possible_cpu(i) {
2856 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
2857 struct blk_mq_hw_ctx *hctx;
Ming Leic16d6b52018-12-17 08:44:05 -07002858 int k;
Jens Axboe320ae512013-10-24 09:20:05 +01002859
Jens Axboe320ae512013-10-24 09:20:05 +01002860 __ctx->cpu = i;
2861 spin_lock_init(&__ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002862 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
2863 INIT_LIST_HEAD(&__ctx->rq_lists[k]);
2864
Jens Axboe320ae512013-10-24 09:20:05 +01002865 __ctx->queue = q;
2866
Jens Axboe320ae512013-10-24 09:20:05 +01002867 /*
2868 * Set local node, IFF we have more than one hw queue. If
2869 * not, we remain on the home node of the device
2870 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06002871 for (j = 0; j < set->nr_maps; j++) {
2872 hctx = blk_mq_map_queue_type(q, j, i);
2873 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
Xianting Tian576e85c2020-10-19 16:20:47 +08002874 hctx->numa_node = cpu_to_node(i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06002875 }
Jens Axboe320ae512013-10-24 09:20:05 +01002876 }
2877}
2878
Weiping Zhang03b63b02020-05-07 21:04:22 +08002879static bool __blk_mq_alloc_map_and_request(struct blk_mq_tag_set *set,
2880 int hctx_idx)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002881{
John Garry1c0706a2020-08-19 23:20:22 +08002882 unsigned int flags = set->flags;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002883 int ret = 0;
2884
2885 set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
John Garry1c0706a2020-08-19 23:20:22 +08002886 set->queue_depth, set->reserved_tags, flags);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002887 if (!set->tags[hctx_idx])
2888 return false;
2889
2890 ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
2891 set->queue_depth);
2892 if (!ret)
2893 return true;
2894
John Garry1c0706a2020-08-19 23:20:22 +08002895 blk_mq_free_rq_map(set->tags[hctx_idx], flags);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002896 set->tags[hctx_idx] = NULL;
2897 return false;
2898}
2899
2900static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
2901 unsigned int hctx_idx)
2902{
John Garry1c0706a2020-08-19 23:20:22 +08002903 unsigned int flags = set->flags;
2904
Dan Carpenter4e6db0f2018-11-29 13:56:54 +03002905 if (set->tags && set->tags[hctx_idx]) {
Jens Axboebd166ef2017-01-17 06:03:22 -07002906 blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
John Garry1c0706a2020-08-19 23:20:22 +08002907 blk_mq_free_rq_map(set->tags[hctx_idx], flags);
Jens Axboebd166ef2017-01-17 06:03:22 -07002908 set->tags[hctx_idx] = NULL;
2909 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002910}
2911
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002912static void blk_mq_map_swqueue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01002913{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002914 unsigned int i, j, hctx_idx;
Jens Axboe320ae512013-10-24 09:20:05 +01002915 struct blk_mq_hw_ctx *hctx;
2916 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08002917 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002918
2919 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06002920 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01002921 hctx->nr_ctx = 0;
huhaid416c922018-05-18 08:32:30 -06002922 hctx->dispatch_from = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002923 }
2924
2925 /*
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002926 * Map software to hardware queues.
Ming Lei4412efe2018-04-25 04:01:44 +08002927 *
2928 * If the cpu isn't present, the cpu is mapped to first hctx.
Jens Axboe320ae512013-10-24 09:20:05 +01002929 */
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08002930 for_each_possible_cpu(i) {
Ming Lei4412efe2018-04-25 04:01:44 +08002931
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01002932 ctx = per_cpu_ptr(q->queue_ctx, i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06002933 for (j = 0; j < set->nr_maps; j++) {
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002934 if (!set->map[j].nr_queues) {
2935 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2936 HCTX_TYPE_DEFAULT, i);
Ming Leie5edd5f2018-12-18 01:28:56 +08002937 continue;
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002938 }
Ming Leifd689872020-05-07 21:04:08 +08002939 hctx_idx = set->map[j].mq_map[i];
2940 /* unmapped hw queue can be remapped after CPU topo changed */
2941 if (!set->tags[hctx_idx] &&
Weiping Zhang03b63b02020-05-07 21:04:22 +08002942 !__blk_mq_alloc_map_and_request(set, hctx_idx)) {
Ming Leifd689872020-05-07 21:04:08 +08002943 /*
2944 * If tags initialization fail for some hctx,
2945 * that hctx won't be brought online. In this
2946 * case, remap the current ctx to hctx[0] which
2947 * is guaranteed to always have tags allocated
2948 */
2949 set->map[j].mq_map[i] = 0;
2950 }
Ming Leie5edd5f2018-12-18 01:28:56 +08002951
Jens Axboeb3c661b2018-10-30 10:36:06 -06002952 hctx = blk_mq_map_queue_type(q, j, i);
Jianchao Wang8ccdf4a2019-01-24 18:25:32 +08002953 ctx->hctxs[j] = hctx;
Jens Axboeb3c661b2018-10-30 10:36:06 -06002954 /*
2955 * If the CPU is already set in the mask, then we've
2956 * mapped this one already. This can happen if
2957 * devices share queues across queue maps.
2958 */
2959 if (cpumask_test_cpu(i, hctx->cpumask))
2960 continue;
2961
2962 cpumask_set_cpu(i, hctx->cpumask);
2963 hctx->type = j;
2964 ctx->index_hw[hctx->type] = hctx->nr_ctx;
2965 hctx->ctxs[hctx->nr_ctx++] = ctx;
2966
2967 /*
2968 * If the nr_ctx type overflows, we have exceeded the
2969 * amount of sw queues we can support.
2970 */
2971 BUG_ON(!hctx->nr_ctx);
2972 }
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002973
2974 for (; j < HCTX_MAX_TYPES; j++)
2975 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2976 HCTX_TYPE_DEFAULT, i);
Jens Axboe320ae512013-10-24 09:20:05 +01002977 }
Jens Axboe506e9312014-05-07 10:26:44 -06002978
2979 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei4412efe2018-04-25 04:01:44 +08002980 /*
2981 * If no software queues are mapped to this hardware queue,
2982 * disable it and free the request entries.
2983 */
2984 if (!hctx->nr_ctx) {
2985 /* Never unmap queue 0. We need it as a
2986 * fallback in case of a new remap fails
2987 * allocation
2988 */
2989 if (i && set->tags[i])
2990 blk_mq_free_map_and_requests(set, i);
2991
2992 hctx->tags = NULL;
2993 continue;
2994 }
Jens Axboe484b4062014-05-21 14:01:15 -06002995
Ming Lei2a34c082015-04-21 10:00:20 +08002996 hctx->tags = set->tags[i];
2997 WARN_ON(!hctx->tags);
2998
Jens Axboe484b4062014-05-21 14:01:15 -06002999 /*
Chong Yuan889fa312015-04-15 11:39:29 -06003000 * Set the map size to the number of mapped software queues.
3001 * This is more accurate and more efficient than looping
3002 * over all possibly mapped software queues.
3003 */
Omar Sandoval88459642016-09-17 08:38:44 -06003004 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
Chong Yuan889fa312015-04-15 11:39:29 -06003005
3006 /*
Jens Axboe484b4062014-05-21 14:01:15 -06003007 * Initialize batch roundrobin counts
3008 */
Ming Leif82ddf12018-04-08 17:48:10 +08003009 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06003010 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
3011 }
Jens Axboe320ae512013-10-24 09:20:05 +01003012}
3013
Jens Axboe8e8320c2017-06-20 17:56:13 -06003014/*
3015 * Caller needs to ensure that we're either frozen/quiesced, or that
3016 * the queue isn't live yet.
3017 */
Jeff Moyer2404e602015-11-03 10:40:06 -05003018static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06003019{
3020 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06003021 int i;
3022
Jeff Moyer2404e602015-11-03 10:40:06 -05003023 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei97889f92018-06-25 19:31:48 +08003024 if (shared)
Ming Lei51db1c32020-08-19 23:20:19 +08003025 hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
Ming Lei97889f92018-06-25 19:31:48 +08003026 else
Ming Lei51db1c32020-08-19 23:20:19 +08003027 hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05003028 }
3029}
3030
Hannes Reinecke655ac302020-08-19 23:20:20 +08003031static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set,
3032 bool shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05003033{
3034 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06003035
Bart Van Assche705cda92017-04-07 11:16:49 -07003036 lockdep_assert_held(&set->tag_list_lock);
3037
Jens Axboe0d2602c2014-05-13 15:10:52 -06003038 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3039 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05003040 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003041 blk_mq_unfreeze_queue(q);
3042 }
3043}
3044
3045static void blk_mq_del_queue_tag_set(struct request_queue *q)
3046{
3047 struct blk_mq_tag_set *set = q->tag_set;
3048
Jens Axboe0d2602c2014-05-13 15:10:52 -06003049 mutex_lock(&set->tag_list_lock);
Daniel Wagner08c875c2020-07-28 15:29:51 +02003050 list_del(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05003051 if (list_is_singular(&set->tag_list)) {
3052 /* just transitioned to unshared */
Ming Lei51db1c32020-08-19 23:20:19 +08003053 set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05003054 /* update existing queue */
Hannes Reinecke655ac302020-08-19 23:20:20 +08003055 blk_mq_update_tag_set_shared(set, false);
Jeff Moyer2404e602015-11-03 10:40:06 -05003056 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06003057 mutex_unlock(&set->tag_list_lock);
Roman Pena347c7a2018-06-10 22:38:24 +02003058 INIT_LIST_HEAD(&q->tag_set_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003059}
3060
3061static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
3062 struct request_queue *q)
3063{
Jens Axboe0d2602c2014-05-13 15:10:52 -06003064 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05003065
Jens Axboeff821d22017-11-10 22:05:12 -07003066 /*
3067 * Check to see if we're transitioning to shared (from 1 to 2 queues).
3068 */
3069 if (!list_empty(&set->tag_list) &&
Ming Lei51db1c32020-08-19 23:20:19 +08003070 !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
3071 set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05003072 /* update existing queue */
Hannes Reinecke655ac302020-08-19 23:20:20 +08003073 blk_mq_update_tag_set_shared(set, true);
Jeff Moyer2404e602015-11-03 10:40:06 -05003074 }
Ming Lei51db1c32020-08-19 23:20:19 +08003075 if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
Jeff Moyer2404e602015-11-03 10:40:06 -05003076 queue_set_hctx_shared(q, true);
Daniel Wagner08c875c2020-07-28 15:29:51 +02003077 list_add_tail(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05003078
Jens Axboe0d2602c2014-05-13 15:10:52 -06003079 mutex_unlock(&set->tag_list_lock);
3080}
3081
Ming Lei1db49092018-11-20 09:44:35 +08003082/* All allocations will be freed in release handler of q->mq_kobj */
3083static int blk_mq_alloc_ctxs(struct request_queue *q)
3084{
3085 struct blk_mq_ctxs *ctxs;
3086 int cpu;
3087
3088 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
3089 if (!ctxs)
3090 return -ENOMEM;
3091
3092 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
3093 if (!ctxs->queue_ctx)
3094 goto fail;
3095
3096 for_each_possible_cpu(cpu) {
3097 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
3098 ctx->ctxs = ctxs;
3099 }
3100
3101 q->mq_kobj = &ctxs->kobj;
3102 q->queue_ctx = ctxs->queue_ctx;
3103
3104 return 0;
3105 fail:
3106 kfree(ctxs);
3107 return -ENOMEM;
3108}
3109
Ming Leie09aae72015-01-29 20:17:27 +08003110/*
3111 * It is the actual release handler for mq, but we do it from
3112 * request queue's release handler for avoiding use-after-free
3113 * and headache because q->mq_kobj shouldn't have been introduced,
3114 * but we can't group ctx/kctx kobj without it.
3115 */
3116void blk_mq_release(struct request_queue *q)
3117{
Ming Lei2f8f1332019-04-30 09:52:27 +08003118 struct blk_mq_hw_ctx *hctx, *next;
3119 int i;
Ming Leie09aae72015-01-29 20:17:27 +08003120
Ming Lei2f8f1332019-04-30 09:52:27 +08003121 queue_for_each_hw_ctx(q, hctx, i)
3122 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
3123
3124 /* all hctx are in .unused_hctx_list now */
3125 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
3126 list_del_init(&hctx->hctx_list);
Ming Lei6c8b2322017-02-22 18:14:01 +08003127 kobject_put(&hctx->kobj);
Ming Leic3b4afc2015-06-04 22:25:04 +08003128 }
Ming Leie09aae72015-01-29 20:17:27 +08003129
3130 kfree(q->queue_hw_ctx);
3131
Ming Lei7ea5fe32017-02-22 18:14:00 +08003132 /*
3133 * release .mq_kobj and sw queue's kobject now because
3134 * both share lifetime with request queue.
3135 */
3136 blk_mq_sysfs_deinit(q);
Ming Leie09aae72015-01-29 20:17:27 +08003137}
3138
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003139struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set,
3140 void *queuedata)
Jens Axboe320ae512013-10-24 09:20:05 +01003141{
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003142 struct request_queue *uninit_q, *q;
3143
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02003144 uninit_q = blk_alloc_queue(set->numa_node);
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003145 if (!uninit_q)
3146 return ERR_PTR(-ENOMEM);
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003147 uninit_q->queuedata = queuedata;
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003148
Damien Le Moal737eb782019-09-05 18:51:33 +09003149 /*
3150 * Initialize the queue without an elevator. device_add_disk() will do
3151 * the initialization.
3152 */
3153 q = blk_mq_init_allocated_queue(set, uninit_q, false);
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003154 if (IS_ERR(q))
3155 blk_cleanup_queue(uninit_q);
3156
3157 return q;
3158}
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003159EXPORT_SYMBOL_GPL(blk_mq_init_queue_data);
3160
3161struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
3162{
3163 return blk_mq_init_queue_data(set, NULL);
3164}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003165EXPORT_SYMBOL(blk_mq_init_queue);
3166
Jens Axboe9316a9e2018-10-15 08:40:37 -06003167/*
3168 * Helper for setting up a queue with mq ops, given queue depth, and
3169 * the passed in mq ops flags.
3170 */
3171struct request_queue *blk_mq_init_sq_queue(struct blk_mq_tag_set *set,
3172 const struct blk_mq_ops *ops,
3173 unsigned int queue_depth,
3174 unsigned int set_flags)
3175{
3176 struct request_queue *q;
3177 int ret;
3178
3179 memset(set, 0, sizeof(*set));
3180 set->ops = ops;
3181 set->nr_hw_queues = 1;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003182 set->nr_maps = 1;
Jens Axboe9316a9e2018-10-15 08:40:37 -06003183 set->queue_depth = queue_depth;
3184 set->numa_node = NUMA_NO_NODE;
3185 set->flags = set_flags;
3186
3187 ret = blk_mq_alloc_tag_set(set);
3188 if (ret)
3189 return ERR_PTR(ret);
3190
3191 q = blk_mq_init_queue(set);
3192 if (IS_ERR(q)) {
3193 blk_mq_free_tag_set(set);
3194 return q;
3195 }
3196
3197 return q;
3198}
3199EXPORT_SYMBOL(blk_mq_init_sq_queue);
3200
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003201static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
3202 struct blk_mq_tag_set *set, struct request_queue *q,
3203 int hctx_idx, int node)
3204{
Ming Lei2f8f1332019-04-30 09:52:27 +08003205 struct blk_mq_hw_ctx *hctx = NULL, *tmp;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003206
Ming Lei2f8f1332019-04-30 09:52:27 +08003207 /* reuse dead hctx first */
3208 spin_lock(&q->unused_hctx_lock);
3209 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
3210 if (tmp->numa_node == node) {
3211 hctx = tmp;
3212 break;
3213 }
3214 }
3215 if (hctx)
3216 list_del_init(&hctx->hctx_list);
3217 spin_unlock(&q->unused_hctx_lock);
3218
3219 if (!hctx)
3220 hctx = blk_mq_alloc_hctx(q, set, node);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003221 if (!hctx)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003222 goto fail;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003223
Ming Lei7c6c5b72019-04-30 09:52:26 +08003224 if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
3225 goto free_hctx;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003226
3227 return hctx;
Ming Lei7c6c5b72019-04-30 09:52:26 +08003228
3229 free_hctx:
3230 kobject_put(&hctx->kobj);
3231 fail:
3232 return NULL;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003233}
3234
Keith Busch868f2f02015-12-17 17:08:14 -07003235static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
3236 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003237{
Jianchao Wange01ad462018-10-12 18:07:28 +08003238 int i, j, end;
Keith Busch868f2f02015-12-17 17:08:14 -07003239 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01003240
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003241 if (q->nr_hw_queues < set->nr_hw_queues) {
3242 struct blk_mq_hw_ctx **new_hctxs;
3243
3244 new_hctxs = kcalloc_node(set->nr_hw_queues,
3245 sizeof(*new_hctxs), GFP_KERNEL,
3246 set->numa_node);
3247 if (!new_hctxs)
3248 return;
3249 if (hctxs)
3250 memcpy(new_hctxs, hctxs, q->nr_hw_queues *
3251 sizeof(*hctxs));
3252 q->queue_hw_ctx = new_hctxs;
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003253 kfree(hctxs);
3254 hctxs = new_hctxs;
3255 }
3256
Ming Leifb350e02018-01-06 16:27:40 +08003257 /* protect against switching io scheduler */
3258 mutex_lock(&q->sysfs_lock);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003259 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003260 int node;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003261 struct blk_mq_hw_ctx *hctx;
Keith Busch868f2f02015-12-17 17:08:14 -07003262
Dongli Zhang7d76f852019-02-27 21:35:01 +08003263 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003264 /*
3265 * If the hw queue has been mapped to another numa node,
3266 * we need to realloc the hctx. If allocation fails, fallback
3267 * to use the previous one.
3268 */
3269 if (hctxs[i] && (hctxs[i]->numa_node == node))
3270 continue;
Jens Axboe320ae512013-10-24 09:20:05 +01003271
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003272 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
3273 if (hctx) {
Ming Lei2f8f1332019-04-30 09:52:27 +08003274 if (hctxs[i])
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003275 blk_mq_exit_hctx(q, set, hctxs[i], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003276 hctxs[i] = hctx;
3277 } else {
3278 if (hctxs[i])
3279 pr_warn("Allocate new hctx on node %d fails,\
3280 fallback to previous one on node %d\n",
3281 node, hctxs[i]->numa_node);
3282 else
3283 break;
Keith Busch868f2f02015-12-17 17:08:14 -07003284 }
Jens Axboe320ae512013-10-24 09:20:05 +01003285 }
Jianchao Wange01ad462018-10-12 18:07:28 +08003286 /*
3287 * Increasing nr_hw_queues fails. Free the newly allocated
3288 * hctxs and keep the previous q->nr_hw_queues.
3289 */
3290 if (i != set->nr_hw_queues) {
3291 j = q->nr_hw_queues;
3292 end = i;
3293 } else {
3294 j = i;
3295 end = q->nr_hw_queues;
3296 q->nr_hw_queues = set->nr_hw_queues;
3297 }
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003298
Jianchao Wange01ad462018-10-12 18:07:28 +08003299 for (; j < end; j++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003300 struct blk_mq_hw_ctx *hctx = hctxs[j];
3301
3302 if (hctx) {
Jens Axboecc71a6f2017-01-11 14:29:56 -07003303 if (hctx->tags)
3304 blk_mq_free_map_and_requests(set, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003305 blk_mq_exit_hctx(q, set, hctx, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003306 hctxs[j] = NULL;
Keith Busch868f2f02015-12-17 17:08:14 -07003307 }
3308 }
Ming Leifb350e02018-01-06 16:27:40 +08003309 mutex_unlock(&q->sysfs_lock);
Keith Busch868f2f02015-12-17 17:08:14 -07003310}
3311
3312struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
Damien Le Moal737eb782019-09-05 18:51:33 +09003313 struct request_queue *q,
3314 bool elevator_init)
Keith Busch868f2f02015-12-17 17:08:14 -07003315{
Ming Lei66841672016-02-12 15:27:00 +08003316 /* mark the queue as mq asap */
3317 q->mq_ops = set->ops;
3318
Omar Sandoval34dbad52017-03-21 08:56:08 -07003319 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
Stephen Bates720b8cc2017-04-07 06:24:03 -06003320 blk_mq_poll_stats_bkt,
3321 BLK_MQ_POLL_STATS_BKTS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -07003322 if (!q->poll_cb)
3323 goto err_exit;
3324
Ming Lei1db49092018-11-20 09:44:35 +08003325 if (blk_mq_alloc_ctxs(q))
Jes Sorensen41de54c2019-04-19 16:35:44 -04003326 goto err_poll;
Keith Busch868f2f02015-12-17 17:08:14 -07003327
Ming Lei737f98c2017-02-22 18:13:59 +08003328 /* init q->mq_kobj and sw queues' kobjects */
3329 blk_mq_sysfs_init(q);
3330
Ming Lei2f8f1332019-04-30 09:52:27 +08003331 INIT_LIST_HEAD(&q->unused_hctx_list);
3332 spin_lock_init(&q->unused_hctx_lock);
3333
Keith Busch868f2f02015-12-17 17:08:14 -07003334 blk_mq_realloc_hw_ctxs(set, q);
3335 if (!q->nr_hw_queues)
3336 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01003337
Christoph Hellwig287922e2015-10-30 20:57:30 +08003338 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08003339 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01003340
Jens Axboea8908932018-10-16 14:23:06 -06003341 q->tag_set = set;
Jens Axboe320ae512013-10-24 09:20:05 +01003342
Jens Axboe94eddfb2013-11-19 09:25:07 -07003343 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Ming Leicd191812018-12-18 12:15:29 +08003344 if (set->nr_maps > HCTX_TYPE_POLL &&
3345 set->map[HCTX_TYPE_POLL].nr_queues)
Christoph Hellwig6544d222018-12-02 17:46:28 +01003346 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
Jens Axboe320ae512013-10-24 09:20:05 +01003347
Christoph Hellwig1be036e2014-02-07 10:22:39 -08003348 q->sg_reserved_size = INT_MAX;
3349
Mike Snitzer28494502016-09-14 13:28:30 -04003350 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06003351 INIT_LIST_HEAD(&q->requeue_list);
3352 spin_lock_init(&q->requeue_lock);
3353
Jens Axboeeba71762014-05-20 15:17:27 -06003354 q->nr_requests = set->queue_depth;
3355
Jens Axboe64f1c212016-11-14 13:03:03 -07003356 /*
3357 * Default to classic polling
3358 */
Yufen Yu29ece8b2019-03-18 22:44:41 +08003359 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
Jens Axboe64f1c212016-11-14 13:03:03 -07003360
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003361 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003362 blk_mq_add_queue_tag_set(set, q);
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003363 blk_mq_map_swqueue(q);
Akinobu Mita4593fdb2015-09-27 02:09:20 +09003364
Damien Le Moal737eb782019-09-05 18:51:33 +09003365 if (elevator_init)
3366 elevator_init_mq(q);
Jens Axboed3484992017-01-13 14:43:58 -07003367
Jens Axboe320ae512013-10-24 09:20:05 +01003368 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07003369
Jens Axboe320ae512013-10-24 09:20:05 +01003370err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07003371 kfree(q->queue_hw_ctx);
zhengbin73d9c8d2019-07-23 22:10:42 +08003372 q->nr_hw_queues = 0;
Ming Lei1db49092018-11-20 09:44:35 +08003373 blk_mq_sysfs_deinit(q);
Jes Sorensen41de54c2019-04-19 16:35:44 -04003374err_poll:
3375 blk_stat_free_callback(q->poll_cb);
3376 q->poll_cb = NULL;
Ming Linc7de5722016-05-25 23:23:27 -07003377err_exit:
3378 q->mq_ops = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01003379 return ERR_PTR(-ENOMEM);
3380}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003381EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01003382
Ming Leic7e2d942019-04-30 09:52:25 +08003383/* tags can _not_ be used after returning from blk_mq_exit_queue */
3384void blk_mq_exit_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01003385{
Bart Van Asschee4d47d92021-05-13 10:15:29 -07003386 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01003387
Bart Van Asschee4d47d92021-05-13 10:15:29 -07003388 /* Checks hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED. */
Ming Lei624dbe42014-05-27 23:35:13 +08003389 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
Bart Van Asschee4d47d92021-05-13 10:15:29 -07003390 /* May clear BLK_MQ_F_TAG_QUEUE_SHARED in hctx->flags. */
3391 blk_mq_del_queue_tag_set(q);
Jens Axboe320ae512013-10-24 09:20:05 +01003392}
Jens Axboe320ae512013-10-24 09:20:05 +01003393
Jens Axboea5164402014-09-10 09:02:03 -06003394static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
3395{
3396 int i;
3397
Xianting Tian8229cca2020-09-26 10:39:47 +08003398 for (i = 0; i < set->nr_hw_queues; i++) {
Weiping Zhang03b63b02020-05-07 21:04:22 +08003399 if (!__blk_mq_alloc_map_and_request(set, i))
Jens Axboea5164402014-09-10 09:02:03 -06003400 goto out_unwind;
Xianting Tian8229cca2020-09-26 10:39:47 +08003401 cond_resched();
3402 }
Jens Axboea5164402014-09-10 09:02:03 -06003403
3404 return 0;
3405
3406out_unwind:
3407 while (--i >= 0)
Weiping Zhang2e194422020-05-07 21:03:39 +08003408 blk_mq_free_map_and_requests(set, i);
Jens Axboea5164402014-09-10 09:02:03 -06003409
Jens Axboea5164402014-09-10 09:02:03 -06003410 return -ENOMEM;
3411}
3412
3413/*
3414 * Allocate the request maps associated with this tag_set. Note that this
3415 * may reduce the depth asked for, if memory is tight. set->queue_depth
3416 * will be updated to reflect the allocated depth.
3417 */
Weiping Zhang79fab522020-05-07 21:04:42 +08003418static int blk_mq_alloc_map_and_requests(struct blk_mq_tag_set *set)
Jens Axboea5164402014-09-10 09:02:03 -06003419{
3420 unsigned int depth;
3421 int err;
3422
3423 depth = set->queue_depth;
3424 do {
3425 err = __blk_mq_alloc_rq_maps(set);
3426 if (!err)
3427 break;
3428
3429 set->queue_depth >>= 1;
3430 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
3431 err = -ENOMEM;
3432 break;
3433 }
3434 } while (set->queue_depth);
3435
3436 if (!set->queue_depth || err) {
3437 pr_err("blk-mq: failed to allocate request map\n");
3438 return -ENOMEM;
3439 }
3440
3441 if (depth != set->queue_depth)
3442 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
3443 depth, set->queue_depth);
3444
3445 return 0;
3446}
3447
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003448static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
3449{
Bart Van Assche6e66b492020-03-09 21:26:17 -07003450 /*
3451 * blk_mq_map_queues() and multiple .map_queues() implementations
3452 * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the
3453 * number of hardware queues.
3454 */
3455 if (set->nr_maps == 1)
3456 set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues;
3457
Ming Lei59388702018-12-07 11:03:53 +08003458 if (set->ops->map_queues && !is_kdump_kernel()) {
Jens Axboeb3c661b2018-10-30 10:36:06 -06003459 int i;
3460
Ming Lei7d4901a2018-01-06 16:27:39 +08003461 /*
3462 * transport .map_queues is usually done in the following
3463 * way:
3464 *
3465 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
3466 * mask = get_cpu_mask(queue)
3467 * for_each_cpu(cpu, mask)
Jens Axboeb3c661b2018-10-30 10:36:06 -06003468 * set->map[x].mq_map[cpu] = queue;
Ming Lei7d4901a2018-01-06 16:27:39 +08003469 * }
3470 *
3471 * When we need to remap, the table has to be cleared for
3472 * killing stale mapping since one CPU may not be mapped
3473 * to any hw queue.
3474 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06003475 for (i = 0; i < set->nr_maps; i++)
3476 blk_mq_clear_mq_map(&set->map[i]);
Ming Lei7d4901a2018-01-06 16:27:39 +08003477
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003478 return set->ops->map_queues(set);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003479 } else {
3480 BUG_ON(set->nr_maps > 1);
Dongli Zhang7d76f852019-02-27 21:35:01 +08003481 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003482 }
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003483}
3484
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003485static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set,
3486 int cur_nr_hw_queues, int new_nr_hw_queues)
3487{
3488 struct blk_mq_tags **new_tags;
3489
3490 if (cur_nr_hw_queues >= new_nr_hw_queues)
3491 return 0;
3492
3493 new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *),
3494 GFP_KERNEL, set->numa_node);
3495 if (!new_tags)
3496 return -ENOMEM;
3497
3498 if (set->tags)
3499 memcpy(new_tags, set->tags, cur_nr_hw_queues *
3500 sizeof(*set->tags));
3501 kfree(set->tags);
3502 set->tags = new_tags;
3503 set->nr_hw_queues = new_nr_hw_queues;
3504
3505 return 0;
3506}
3507
Jens Axboea4391c62014-06-05 15:21:56 -06003508/*
3509 * Alloc a tag set to be associated with one or more request queues.
3510 * May fail with EINVAL for various error conditions. May adjust the
Minwoo Imc018c842018-06-30 22:12:41 +09003511 * requested depth down, if it's too large. In that case, the set
Jens Axboea4391c62014-06-05 15:21:56 -06003512 * value will be stored in set->queue_depth.
3513 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003514int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
3515{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003516 int i, ret;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003517
Bart Van Assche205fb5f2014-10-30 14:45:11 +01003518 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
3519
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003520 if (!set->nr_hw_queues)
3521 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06003522 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003523 return -EINVAL;
3524 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
3525 return -EINVAL;
3526
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02003527 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003528 return -EINVAL;
3529
Ming Leide148292017-10-14 17:22:29 +08003530 if (!set->ops->get_budget ^ !set->ops->put_budget)
3531 return -EINVAL;
3532
Jens Axboea4391c62014-06-05 15:21:56 -06003533 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
3534 pr_info("blk-mq: reduced tag depth to %u\n",
3535 BLK_MQ_MAX_DEPTH);
3536 set->queue_depth = BLK_MQ_MAX_DEPTH;
3537 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003538
Jens Axboeb3c661b2018-10-30 10:36:06 -06003539 if (!set->nr_maps)
3540 set->nr_maps = 1;
3541 else if (set->nr_maps > HCTX_MAX_TYPES)
3542 return -EINVAL;
3543
Shaohua Li6637fad2014-11-30 16:00:58 -08003544 /*
3545 * If a crashdump is active, then we are potentially in a very
3546 * memory constrained environment. Limit us to 1 queue and
3547 * 64 tags to prevent using too much memory.
3548 */
3549 if (is_kdump_kernel()) {
3550 set->nr_hw_queues = 1;
Ming Lei59388702018-12-07 11:03:53 +08003551 set->nr_maps = 1;
Shaohua Li6637fad2014-11-30 16:00:58 -08003552 set->queue_depth = min(64U, set->queue_depth);
3553 }
Keith Busch868f2f02015-12-17 17:08:14 -07003554 /*
Jens Axboe392546a2018-10-29 13:25:27 -06003555 * There is no use for more h/w queues than cpus if we just have
3556 * a single map
Keith Busch868f2f02015-12-17 17:08:14 -07003557 */
Jens Axboe392546a2018-10-29 13:25:27 -06003558 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003559 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08003560
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003561 if (blk_mq_realloc_tag_set_tags(set, 0, set->nr_hw_queues) < 0)
Jens Axboea5164402014-09-10 09:02:03 -06003562 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003563
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003564 ret = -ENOMEM;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003565 for (i = 0; i < set->nr_maps; i++) {
3566 set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
Ming Lei07b35eb2018-12-17 18:42:45 +08003567 sizeof(set->map[i].mq_map[0]),
Jens Axboeb3c661b2018-10-30 10:36:06 -06003568 GFP_KERNEL, set->numa_node);
3569 if (!set->map[i].mq_map)
3570 goto out_free_mq_map;
Ming Lei59388702018-12-07 11:03:53 +08003571 set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003572 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003573
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003574 ret = blk_mq_update_queue_map(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003575 if (ret)
3576 goto out_free_mq_map;
3577
Weiping Zhang79fab522020-05-07 21:04:42 +08003578 ret = blk_mq_alloc_map_and_requests(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003579 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003580 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003581
John Garry32bc15a2020-08-19 23:20:24 +08003582 if (blk_mq_is_sbitmap_shared(set->flags)) {
John Garryf1b49fd2020-08-19 23:20:27 +08003583 atomic_set(&set->active_queues_shared_sbitmap, 0);
3584
John Garry32bc15a2020-08-19 23:20:24 +08003585 if (blk_mq_init_shared_sbitmap(set, set->flags)) {
3586 ret = -ENOMEM;
3587 goto out_free_mq_rq_maps;
3588 }
3589 }
3590
Jens Axboe0d2602c2014-05-13 15:10:52 -06003591 mutex_init(&set->tag_list_lock);
3592 INIT_LIST_HEAD(&set->tag_list);
3593
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003594 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003595
John Garry32bc15a2020-08-19 23:20:24 +08003596out_free_mq_rq_maps:
3597 for (i = 0; i < set->nr_hw_queues; i++)
3598 blk_mq_free_map_and_requests(set, i);
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003599out_free_mq_map:
Jens Axboeb3c661b2018-10-30 10:36:06 -06003600 for (i = 0; i < set->nr_maps; i++) {
3601 kfree(set->map[i].mq_map);
3602 set->map[i].mq_map = NULL;
3603 }
Robert Elliott5676e7b2014-09-02 11:38:44 -05003604 kfree(set->tags);
3605 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003606 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003607}
3608EXPORT_SYMBOL(blk_mq_alloc_tag_set);
3609
3610void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
3611{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003612 int i, j;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003613
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003614 for (i = 0; i < set->nr_hw_queues; i++)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003615 blk_mq_free_map_and_requests(set, i);
Jens Axboe484b4062014-05-21 14:01:15 -06003616
John Garry32bc15a2020-08-19 23:20:24 +08003617 if (blk_mq_is_sbitmap_shared(set->flags))
3618 blk_mq_exit_shared_sbitmap(set);
3619
Jens Axboeb3c661b2018-10-30 10:36:06 -06003620 for (j = 0; j < set->nr_maps; j++) {
3621 kfree(set->map[j].mq_map);
3622 set->map[j].mq_map = NULL;
3623 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003624
Ming Lei981bd182014-04-24 00:07:34 +08003625 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05003626 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003627}
3628EXPORT_SYMBOL(blk_mq_free_tag_set);
3629
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003630int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
3631{
3632 struct blk_mq_tag_set *set = q->tag_set;
3633 struct blk_mq_hw_ctx *hctx;
3634 int i, ret;
3635
Jens Axboebd166ef2017-01-17 06:03:22 -07003636 if (!set)
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003637 return -EINVAL;
3638
Aleksei Zakharove5fa8142019-02-08 19:14:05 +03003639 if (q->nr_requests == nr)
3640 return 0;
3641
Jens Axboe70f36b62017-01-19 10:59:07 -07003642 blk_mq_freeze_queue(q);
Ming Lei24f5a902018-01-06 16:27:38 +08003643 blk_mq_quiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003644
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003645 ret = 0;
3646 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07003647 if (!hctx->tags)
3648 continue;
Jens Axboebd166ef2017-01-17 06:03:22 -07003649 /*
3650 * If we're using an MQ scheduler, just update the scheduler
3651 * queue depth. This is similar to what the old code would do.
3652 */
Jens Axboe70f36b62017-01-19 10:59:07 -07003653 if (!hctx->sched_tags) {
weiping zhangc2e82a22017-09-22 23:36:28 +08003654 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
Jens Axboe70f36b62017-01-19 10:59:07 -07003655 false);
John Garry32bc15a2020-08-19 23:20:24 +08003656 if (!ret && blk_mq_is_sbitmap_shared(set->flags))
3657 blk_mq_tag_resize_shared_sbitmap(set, nr);
Jens Axboe70f36b62017-01-19 10:59:07 -07003658 } else {
3659 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
3660 nr, true);
3661 }
Jens Axboee3a2b3f2014-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 Axboee3a2b3f2014-05-20 11:49:02 -06003666 }
3667
3668 if (!ret)
3669 q->nr_requests = nr;
3670
Ming Lei24f5a902018-01-06 16:27:38 +08003671 blk_mq_unquiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003672 blk_mq_unfreeze_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003673
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003674 return ret;
3675}
3676
Jianchao Wangd48ece22018-08-21 15:15:03 +08003677/*
3678 * request_queue and elevator_type pair.
3679 * It is just used by __blk_mq_update_nr_hw_queues to cache
3680 * the elevator_type associated with a request_queue.
3681 */
3682struct blk_mq_qe_pair {
3683 struct list_head node;
3684 struct request_queue *q;
3685 struct elevator_type *type;
3686};
3687
3688/*
3689 * Cache the elevator_type in qe pair list and switch the
3690 * io scheduler to 'none'
3691 */
3692static bool blk_mq_elv_switch_none(struct list_head *head,
3693 struct request_queue *q)
3694{
3695 struct blk_mq_qe_pair *qe;
3696
3697 if (!q->elevator)
3698 return true;
3699
3700 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
3701 if (!qe)
3702 return false;
3703
3704 INIT_LIST_HEAD(&qe->node);
3705 qe->q = q;
3706 qe->type = q->elevator->type;
3707 list_add(&qe->node, head);
3708
3709 mutex_lock(&q->sysfs_lock);
3710 /*
3711 * After elevator_switch_mq, the previous elevator_queue will be
3712 * released by elevator_release. The reference of the io scheduler
3713 * module get by elevator_get will also be put. So we need to get
3714 * a reference of the io scheduler module here to prevent it to be
3715 * removed.
3716 */
3717 __module_get(qe->type->elevator_owner);
3718 elevator_switch_mq(q, NULL);
3719 mutex_unlock(&q->sysfs_lock);
3720
3721 return true;
3722}
3723
3724static void blk_mq_elv_switch_back(struct list_head *head,
3725 struct request_queue *q)
3726{
3727 struct blk_mq_qe_pair *qe;
3728 struct elevator_type *t = NULL;
3729
3730 list_for_each_entry(qe, head, node)
3731 if (qe->q == q) {
3732 t = qe->type;
3733 break;
3734 }
3735
3736 if (!t)
3737 return;
3738
3739 list_del(&qe->node);
3740 kfree(qe);
3741
3742 mutex_lock(&q->sysfs_lock);
3743 elevator_switch_mq(q, t);
3744 mutex_unlock(&q->sysfs_lock);
3745}
3746
Keith Busche4dc2b32017-05-30 14:39:11 -04003747static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
3748 int nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003749{
3750 struct request_queue *q;
Jianchao Wangd48ece22018-08-21 15:15:03 +08003751 LIST_HEAD(head);
Jianchao Wange01ad462018-10-12 18:07:28 +08003752 int prev_nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07003753
Bart Van Assche705cda92017-04-07 11:16:49 -07003754 lockdep_assert_held(&set->tag_list_lock);
3755
Jens Axboe392546a2018-10-29 13:25:27 -06003756 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003757 nr_hw_queues = nr_cpu_ids;
Weiping Zhangfe35ec52020-06-17 14:18:37 +08003758 if (nr_hw_queues < 1)
3759 return;
3760 if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003761 return;
3762
3763 list_for_each_entry(q, &set->tag_list, tag_set_list)
3764 blk_mq_freeze_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +08003765 /*
3766 * Switch IO scheduler to 'none', cleaning up the data associated
3767 * with the previous scheduler. We will switch back once we are done
3768 * updating the new sw to hw queue mappings.
3769 */
3770 list_for_each_entry(q, &set->tag_list, tag_set_list)
3771 if (!blk_mq_elv_switch_none(&head, q))
3772 goto switch_back;
Keith Busch868f2f02015-12-17 17:08:14 -07003773
Jianchao Wang477e19d2018-10-12 18:07:25 +08003774 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3775 blk_mq_debugfs_unregister_hctxs(q);
3776 blk_mq_sysfs_unregister(q);
3777 }
3778
Weiping Zhanga2584e42020-05-07 21:03:56 +08003779 prev_nr_hw_queues = set->nr_hw_queues;
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003780 if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) <
3781 0)
3782 goto reregister;
3783
Keith Busch868f2f02015-12-17 17:08:14 -07003784 set->nr_hw_queues = nr_hw_queues;
Jianchao Wange01ad462018-10-12 18:07:28 +08003785fallback:
Weiping Zhangaa880ad2020-05-13 08:44:05 +08003786 blk_mq_update_queue_map(set);
Keith Busch868f2f02015-12-17 17:08:14 -07003787 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3788 blk_mq_realloc_hw_ctxs(set, q);
Jianchao Wange01ad462018-10-12 18:07:28 +08003789 if (q->nr_hw_queues != set->nr_hw_queues) {
3790 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
3791 nr_hw_queues, prev_nr_hw_queues);
3792 set->nr_hw_queues = prev_nr_hw_queues;
Dongli Zhang7d76f852019-02-27 21:35:01 +08003793 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jianchao Wange01ad462018-10-12 18:07:28 +08003794 goto fallback;
3795 }
Jianchao Wang477e19d2018-10-12 18:07:25 +08003796 blk_mq_map_swqueue(q);
3797 }
3798
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003799reregister:
Jianchao Wang477e19d2018-10-12 18:07:25 +08003800 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3801 blk_mq_sysfs_register(q);
3802 blk_mq_debugfs_register_hctxs(q);
Keith Busch868f2f02015-12-17 17:08:14 -07003803 }
3804
Jianchao Wangd48ece22018-08-21 15:15:03 +08003805switch_back:
3806 list_for_each_entry(q, &set->tag_list, tag_set_list)
3807 blk_mq_elv_switch_back(&head, q);
3808
Keith Busch868f2f02015-12-17 17:08:14 -07003809 list_for_each_entry(q, &set->tag_list, tag_set_list)
3810 blk_mq_unfreeze_queue(q);
3811}
Keith Busche4dc2b32017-05-30 14:39:11 -04003812
3813void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
3814{
3815 mutex_lock(&set->tag_list_lock);
3816 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
3817 mutex_unlock(&set->tag_list_lock);
3818}
Keith Busch868f2f02015-12-17 17:08:14 -07003819EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
3820
Omar Sandoval34dbad52017-03-21 08:56:08 -07003821/* Enable polling stats and return whether they were already enabled. */
3822static bool blk_poll_stats_enable(struct request_queue *q)
3823{
3824 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
Bart Van Assche7dfdbc72018-03-07 17:10:05 -08003825 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
Omar Sandoval34dbad52017-03-21 08:56:08 -07003826 return true;
3827 blk_stat_add_callback(q, q->poll_cb);
3828 return false;
3829}
3830
3831static void blk_mq_poll_stats_start(struct request_queue *q)
3832{
3833 /*
3834 * We don't arm the callback if polling stats are not enabled or the
3835 * callback is already active.
3836 */
3837 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
3838 blk_stat_is_active(q->poll_cb))
3839 return;
3840
3841 blk_stat_activate_msecs(q->poll_cb, 100);
3842}
3843
3844static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
3845{
3846 struct request_queue *q = cb->data;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003847 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -07003848
Stephen Bates720b8cc2017-04-07 06:24:03 -06003849 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
3850 if (cb->stat[bucket].nr_samples)
3851 q->poll_stat[bucket] = cb->stat[bucket];
3852 }
Omar Sandoval34dbad52017-03-21 08:56:08 -07003853}
3854
Jens Axboe64f1c212016-11-14 13:03:03 -07003855static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
Jens Axboe64f1c212016-11-14 13:03:03 -07003856 struct request *rq)
3857{
Jens Axboe64f1c212016-11-14 13:03:03 -07003858 unsigned long ret = 0;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003859 int bucket;
Jens Axboe64f1c212016-11-14 13:03:03 -07003860
3861 /*
3862 * If stats collection isn't on, don't sleep but turn it on for
3863 * future users
3864 */
Omar Sandoval34dbad52017-03-21 08:56:08 -07003865 if (!blk_poll_stats_enable(q))
Jens Axboe64f1c212016-11-14 13:03:03 -07003866 return 0;
3867
3868 /*
Jens Axboe64f1c212016-11-14 13:03:03 -07003869 * As an optimistic guess, use half of the mean service time
3870 * for this type of request. We can (and should) make this smarter.
3871 * For instance, if the completion latencies are tight, we can
3872 * get closer than just half the mean. This is especially
3873 * important on devices where the completion latencies are longer
Stephen Bates720b8cc2017-04-07 06:24:03 -06003874 * than ~10 usec. We do use the stats for the relevant IO size
3875 * if available which does lead to better estimates.
Jens Axboe64f1c212016-11-14 13:03:03 -07003876 */
Stephen Bates720b8cc2017-04-07 06:24:03 -06003877 bucket = blk_mq_poll_stats_bkt(rq);
3878 if (bucket < 0)
3879 return ret;
3880
3881 if (q->poll_stat[bucket].nr_samples)
3882 ret = (q->poll_stat[bucket].mean + 1) / 2;
Jens Axboe64f1c212016-11-14 13:03:03 -07003883
3884 return ret;
3885}
3886
Jens Axboe06426ad2016-11-14 13:01:59 -07003887static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
3888 struct request *rq)
3889{
3890 struct hrtimer_sleeper hs;
3891 enum hrtimer_mode mode;
Jens Axboe64f1c212016-11-14 13:03:03 -07003892 unsigned int nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003893 ktime_t kt;
3894
Jens Axboe76a86f92018-01-10 11:30:56 -07003895 if (rq->rq_flags & RQF_MQ_POLL_SLEPT)
Jens Axboe64f1c212016-11-14 13:03:03 -07003896 return false;
3897
3898 /*
Jens Axboe1052b8a2018-11-26 08:21:49 -07003899 * If we get here, hybrid polling is enabled. Hence poll_nsec can be:
Jens Axboe64f1c212016-11-14 13:03:03 -07003900 *
Jens Axboe64f1c212016-11-14 13:03:03 -07003901 * 0: use half of prev avg
3902 * >0: use this specific value
3903 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07003904 if (q->poll_nsec > 0)
Jens Axboe64f1c212016-11-14 13:03:03 -07003905 nsecs = q->poll_nsec;
3906 else
John Garrycae740a2020-02-26 20:10:15 +08003907 nsecs = blk_mq_poll_nsecs(q, rq);
Jens Axboe64f1c212016-11-14 13:03:03 -07003908
3909 if (!nsecs)
Jens Axboe06426ad2016-11-14 13:01:59 -07003910 return false;
3911
Jens Axboe76a86f92018-01-10 11:30:56 -07003912 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
Jens Axboe06426ad2016-11-14 13:01:59 -07003913
3914 /*
3915 * This will be replaced with the stats tracking code, using
3916 * 'avg_completion_time / 2' as the pre-sleep target.
3917 */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01003918 kt = nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003919
3920 mode = HRTIMER_MODE_REL;
Sebastian Andrzej Siewiordbc16252019-07-26 20:30:50 +02003921 hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07003922 hrtimer_set_expires(&hs.timer, kt);
3923
Jens Axboe06426ad2016-11-14 13:01:59 -07003924 do {
Tejun Heo5a61c362018-01-09 08:29:52 -08003925 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
Jens Axboe06426ad2016-11-14 13:01:59 -07003926 break;
3927 set_current_state(TASK_UNINTERRUPTIBLE);
Thomas Gleixner9dd88132019-07-30 21:16:55 +02003928 hrtimer_sleeper_start_expires(&hs, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07003929 if (hs.task)
3930 io_schedule();
3931 hrtimer_cancel(&hs.timer);
3932 mode = HRTIMER_MODE_ABS;
3933 } while (hs.task && !signal_pending(current));
3934
3935 __set_current_state(TASK_RUNNING);
3936 destroy_hrtimer_on_stack(&hs.timer);
3937 return true;
3938}
3939
Jens Axboe1052b8a2018-11-26 08:21:49 -07003940static bool blk_mq_poll_hybrid(struct request_queue *q,
3941 struct blk_mq_hw_ctx *hctx, blk_qc_t cookie)
Jens Axboebbd7bb72016-11-04 09:34:34 -06003942{
Jens Axboe1052b8a2018-11-26 08:21:49 -07003943 struct request *rq;
3944
Yufen Yu29ece8b2019-03-18 22:44:41 +08003945 if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
Jens Axboe1052b8a2018-11-26 08:21:49 -07003946 return false;
3947
3948 if (!blk_qc_t_is_internal(cookie))
3949 rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
3950 else {
3951 rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
3952 /*
3953 * With scheduling, if the request has completed, we'll
3954 * get a NULL return here, as we clear the sched tag when
3955 * that happens. The request still remains valid, like always,
3956 * so we should be safe with just the NULL check.
3957 */
3958 if (!rq)
3959 return false;
3960 }
3961
John Garrycae740a2020-02-26 20:10:15 +08003962 return blk_mq_poll_hybrid_sleep(q, rq);
Jens Axboe1052b8a2018-11-26 08:21:49 -07003963}
3964
Christoph Hellwig529262d2018-12-02 17:46:26 +01003965/**
3966 * blk_poll - poll for IO completions
3967 * @q: the queue
3968 * @cookie: cookie passed back at IO submission time
3969 * @spin: whether to spin for completions
3970 *
3971 * Description:
3972 * Poll for completions on the passed in queue. Returns number of
3973 * completed entries found. If @spin is true, then blk_poll will continue
3974 * looping until at least one completion is found, unless the task is
3975 * otherwise marked running (or we need to reschedule).
3976 */
3977int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin)
Jens Axboe1052b8a2018-11-26 08:21:49 -07003978{
3979 struct blk_mq_hw_ctx *hctx;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003980 long state;
3981
Christoph Hellwig529262d2018-12-02 17:46:26 +01003982 if (!blk_qc_t_valid(cookie) ||
3983 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
Jens Axboe1052b8a2018-11-26 08:21:49 -07003984 return 0;
3985
Christoph Hellwig529262d2018-12-02 17:46:26 +01003986 if (current->plug)
3987 blk_flush_plug_list(current->plug, false);
3988
Jens Axboe1052b8a2018-11-26 08:21:49 -07003989 hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
3990
Jens Axboe06426ad2016-11-14 13:01:59 -07003991 /*
3992 * If we sleep, have the caller restart the poll loop to reset
3993 * the state. Like for the other success return cases, the
3994 * caller is responsible for checking if the IO completed. If
3995 * the IO isn't complete, we'll get called again and will go
3996 * straight to the busy poll loop.
3997 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07003998 if (blk_mq_poll_hybrid(q, hctx, cookie))
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003999 return 1;
Jens Axboe06426ad2016-11-14 13:01:59 -07004000
Jens Axboebbd7bb72016-11-04 09:34:34 -06004001 hctx->poll_considered++;
4002
4003 state = current->state;
Jens Axboeaa61bec2018-11-13 21:32:10 -07004004 do {
Jens Axboebbd7bb72016-11-04 09:34:34 -06004005 int ret;
4006
4007 hctx->poll_invoked++;
4008
Jens Axboe97431392018-11-16 09:48:21 -07004009 ret = q->mq_ops->poll(hctx);
Jens Axboebbd7bb72016-11-04 09:34:34 -06004010 if (ret > 0) {
4011 hctx->poll_success++;
Jens Axboe849a3702018-11-16 08:37:34 -07004012 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004013 return ret;
Jens Axboebbd7bb72016-11-04 09:34:34 -06004014 }
4015
4016 if (signal_pending_state(state, current))
Jens Axboe849a3702018-11-16 08:37:34 -07004017 __set_current_state(TASK_RUNNING);
Jens Axboebbd7bb72016-11-04 09:34:34 -06004018
4019 if (current->state == TASK_RUNNING)
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004020 return 1;
Jens Axboe0a1b8b82018-11-26 08:24:43 -07004021 if (ret < 0 || !spin)
Jens Axboebbd7bb72016-11-04 09:34:34 -06004022 break;
4023 cpu_relax();
Jens Axboeaa61bec2018-11-13 21:32:10 -07004024 } while (!need_resched());
Jens Axboebbd7bb72016-11-04 09:34:34 -06004025
Nitesh Shetty67b41102018-02-13 21:18:12 +05304026 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004027 return 0;
Jens Axboebbd7bb72016-11-04 09:34:34 -06004028}
Christoph Hellwig529262d2018-12-02 17:46:26 +01004029EXPORT_SYMBOL_GPL(blk_poll);
Jens Axboebbd7bb72016-11-04 09:34:34 -06004030
Jens Axboe9cf2bab2018-10-31 17:01:22 -06004031unsigned int blk_mq_rq_cpu(struct request *rq)
4032{
4033 return rq->mq_ctx->cpu;
4034}
4035EXPORT_SYMBOL(blk_mq_rq_cpu);
4036
Jens Axboe320ae512013-10-24 09:20:05 +01004037static int __init blk_mq_init(void)
4038{
Christoph Hellwigc3077b52020-06-11 08:44:41 +02004039 int i;
4040
4041 for_each_possible_cpu(i)
4042 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
4043 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
4044
4045 cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
4046 "block/softirq:dead", NULL,
4047 blk_softirq_cpu_dead);
Thomas Gleixner9467f852016-09-22 08:05:17 -06004048 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
4049 blk_mq_hctx_notify_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02004050 cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online",
4051 blk_mq_hctx_notify_online,
4052 blk_mq_hctx_notify_offline);
Jens Axboe320ae512013-10-24 09:20:05 +01004053 return 0;
4054}
4055subsys_initcall(blk_mq_init);