blob: 8874a63ae952b28031eb9c38323d06dca4f3129e [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Jens Axboe75bb4622014-05-28 10:15:41 -06002/*
3 * Block multiqueue core code
4 *
5 * Copyright (C) 2013-2014 Jens Axboe
6 * Copyright (C) 2013-2014 Christoph Hellwig
7 */
Jens Axboe320ae512013-10-24 09:20:05 +01008#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/backing-dev.h>
11#include <linux/bio.h>
12#include <linux/blkdev.h>
Christoph Hellwigfe45e632021-09-20 14:33:27 +020013#include <linux/blk-integrity.h>
Catalin Marinasf75782e2015-09-14 18:16:02 +010014#include <linux/kmemleak.h>
Jens Axboe320ae512013-10-24 09:20:05 +010015#include <linux/mm.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/workqueue.h>
19#include <linux/smp.h>
Christoph Hellwige41d12f2021-09-20 14:33:13 +020020#include <linux/interrupt.h>
Jens Axboe320ae512013-10-24 09:20:05 +010021#include <linux/llist.h>
Jens Axboe320ae512013-10-24 09:20:05 +010022#include <linux/cpu.h>
23#include <linux/cache.h>
24#include <linux/sched/sysctl.h>
Ingo Molnar105ab3d2017-02-01 16:36:40 +010025#include <linux/sched/topology.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010026#include <linux/sched/signal.h>
Jens Axboe320ae512013-10-24 09:20:05 +010027#include <linux/delay.h>
Jens Axboeaedcd722014-09-17 08:27:03 -060028#include <linux/crash_dump.h>
Jens Axboe88c7b2b2016-08-25 08:07:30 -060029#include <linux/prefetch.h>
Satya Tangiralaa892c8d2020-05-14 00:37:18 +000030#include <linux/blk-crypto.h>
Jens Axboe320ae512013-10-24 09:20:05 +010031
32#include <trace/events/block.h>
33
34#include <linux/blk-mq.h>
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +030035#include <linux/t10-pi.h>
Jens Axboe320ae512013-10-24 09:20:05 +010036#include "blk.h"
37#include "blk-mq.h"
Omar Sandoval9c1051a2017-05-04 08:17:21 -060038#include "blk-mq-debugfs.h"
Jens Axboe320ae512013-10-24 09:20:05 +010039#include "blk-mq-tag.h"
Bart Van Assche986d4132018-09-26 14:01:10 -070040#include "blk-pm.h"
Jens Axboecf43e6b2016-11-07 21:32:37 -070041#include "blk-stat.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070042#include "blk-mq-sched.h"
Josef Bacikc1c80382018-07-03 11:14:59 -040043#include "blk-rq-qos.h"
Jens Axboe320ae512013-10-24 09:20:05 +010044
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +010045static DEFINE_PER_CPU(struct llist_head, blk_cpu_done);
Christoph Hellwigc3077b52020-06-11 08:44:41 +020046
Omar Sandoval34dbad52017-03-21 08:56:08 -070047static void blk_mq_poll_stats_start(struct request_queue *q);
48static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);
49
Stephen Bates720b8cc2017-04-07 06:24:03 -060050static int blk_mq_poll_stats_bkt(const struct request *rq)
51{
Hou Tao3d244302019-05-21 15:59:03 +080052 int ddir, sectors, bucket;
Stephen Bates720b8cc2017-04-07 06:24:03 -060053
Jens Axboe99c749a2017-04-21 07:55:42 -060054 ddir = rq_data_dir(rq);
Hou Tao3d244302019-05-21 15:59:03 +080055 sectors = blk_rq_stats_sectors(rq);
Stephen Bates720b8cc2017-04-07 06:24:03 -060056
Hou Tao3d244302019-05-21 15:59:03 +080057 bucket = ddir + 2 * ilog2(sectors);
Stephen Bates720b8cc2017-04-07 06:24:03 -060058
59 if (bucket < 0)
60 return -1;
61 else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
62 return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
63
64 return bucket;
65}
66
Christoph Hellwig3e087732021-10-12 13:12:24 +020067#define BLK_QC_T_SHIFT 16
68#define BLK_QC_T_INTERNAL (1U << 31)
69
Christoph Hellwigf70299f2021-10-12 13:12:15 +020070static inline struct blk_mq_hw_ctx *blk_qc_to_hctx(struct request_queue *q,
71 blk_qc_t qc)
72{
73 return q->queue_hw_ctx[(qc & ~BLK_QC_T_INTERNAL) >> BLK_QC_T_SHIFT];
74}
75
Christoph Hellwigc6699d62021-10-12 13:12:16 +020076static inline struct request *blk_qc_to_rq(struct blk_mq_hw_ctx *hctx,
77 blk_qc_t qc)
78{
Christoph Hellwigefbabbe2021-10-12 13:12:17 +020079 unsigned int tag = qc & ((1U << BLK_QC_T_SHIFT) - 1);
80
81 if (qc & BLK_QC_T_INTERNAL)
82 return blk_mq_tag_to_rq(hctx->sched_tags, tag);
83 return blk_mq_tag_to_rq(hctx->tags, tag);
Christoph Hellwigc6699d62021-10-12 13:12:16 +020084}
85
Christoph Hellwig3e087732021-10-12 13:12:24 +020086static inline blk_qc_t blk_rq_to_qc(struct request *rq)
87{
88 return (rq->mq_hctx->queue_num << BLK_QC_T_SHIFT) |
89 (rq->tag != -1 ?
90 rq->tag : (rq->internal_tag | BLK_QC_T_INTERNAL));
91}
92
Jens Axboe320ae512013-10-24 09:20:05 +010093/*
Yufen Yu85fae292019-03-24 17:57:08 +080094 * Check if any of the ctx, dispatch list or elevator
95 * have pending work in this hardware queue.
Jens Axboe320ae512013-10-24 09:20:05 +010096 */
Jens Axboe79f720a2017-11-10 09:13:21 -070097static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
Jens Axboe320ae512013-10-24 09:20:05 +010098{
Jens Axboe79f720a2017-11-10 09:13:21 -070099 return !list_empty_careful(&hctx->dispatch) ||
100 sbitmap_any_bit_set(&hctx->ctx_map) ||
Jens Axboebd166ef2017-01-17 06:03:22 -0700101 blk_mq_sched_has_work(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100102}
103
104/*
105 * Mark this ctx as having pending work in this hardware queue
106 */
107static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
108 struct blk_mq_ctx *ctx)
109{
Jens Axboef31967f2018-10-29 13:13:29 -0600110 const int bit = ctx->index_hw[hctx->type];
111
112 if (!sbitmap_test_bit(&hctx->ctx_map, bit))
113 sbitmap_set_bit(&hctx->ctx_map, bit);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600114}
115
116static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
117 struct blk_mq_ctx *ctx)
118{
Jens Axboef31967f2018-10-29 13:13:29 -0600119 const int bit = ctx->index_hw[hctx->type];
120
121 sbitmap_clear_bit(&hctx->ctx_map, bit);
Jens Axboe320ae512013-10-24 09:20:05 +0100122}
123
Jens Axboef299b7c2017-08-08 17:51:45 -0600124struct mq_inflight {
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100125 struct block_device *part;
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300126 unsigned int inflight[2];
Jens Axboef299b7c2017-08-08 17:51:45 -0600127};
128
Jens Axboe7baa8572018-11-08 10:24:07 -0700129static bool blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx,
Jens Axboef299b7c2017-08-08 17:51:45 -0600130 struct request *rq, void *priv,
131 bool reserved)
132{
133 struct mq_inflight *mi = priv;
134
Jeffle Xub0d97552020-12-02 19:11:45 +0800135 if ((!mi->part->bd_partno || rq->part == mi->part) &&
136 blk_mq_rq_state(rq) == MQ_RQ_IN_FLIGHT)
Pavel Begunkovbb4e6b12019-09-30 21:55:33 +0300137 mi->inflight[rq_data_dir(rq)]++;
Jens Axboe7baa8572018-11-08 10:24:07 -0700138
139 return true;
Jens Axboef299b7c2017-08-08 17:51:45 -0600140}
141
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100142unsigned int blk_mq_in_flight(struct request_queue *q,
143 struct block_device *part)
Jens Axboef299b7c2017-08-08 17:51:45 -0600144{
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300145 struct mq_inflight mi = { .part = part };
Jens Axboef299b7c2017-08-08 17:51:45 -0600146
Jens Axboef299b7c2017-08-08 17:51:45 -0600147 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
Mikulas Patockae016b782018-12-06 11:41:21 -0500148
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300149 return mi.inflight[0] + mi.inflight[1];
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700150}
151
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100152void blk_mq_in_flight_rw(struct request_queue *q, struct block_device *part,
153 unsigned int inflight[2])
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700154{
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300155 struct mq_inflight mi = { .part = part };
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700156
Pavel Begunkovbb4e6b12019-09-30 21:55:33 +0300157 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300158 inflight[0] = mi.inflight[0];
159 inflight[1] = mi.inflight[1];
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700160}
161
Ming Lei1671d522017-03-27 20:06:57 +0800162void blk_freeze_queue_start(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +0800163{
Bob Liu7996a8b2019-05-21 11:25:55 +0800164 mutex_lock(&q->mq_freeze_lock);
165 if (++q->mq_freeze_depth == 1) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400166 percpu_ref_kill(&q->q_usage_counter);
Bob Liu7996a8b2019-05-21 11:25:55 +0800167 mutex_unlock(&q->mq_freeze_lock);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700168 if (queue_is_mq(q))
Ming Lei055f6e12017-11-09 10:49:53 -0800169 blk_mq_run_hw_queues(q, false);
Bob Liu7996a8b2019-05-21 11:25:55 +0800170 } else {
171 mutex_unlock(&q->mq_freeze_lock);
Tejun Heocddd5d12014-08-16 08:02:24 -0400172 }
Tejun Heof3af0202014-11-04 13:52:27 -0500173}
Ming Lei1671d522017-03-27 20:06:57 +0800174EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
Tejun Heof3af0202014-11-04 13:52:27 -0500175
Keith Busch6bae363e2017-03-01 14:22:10 -0500176void blk_mq_freeze_queue_wait(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500177{
Dan Williams3ef28e82015-10-21 13:20:12 -0400178 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +0800179}
Keith Busch6bae363e2017-03-01 14:22:10 -0500180EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800181
Keith Buschf91328c2017-03-01 14:22:11 -0500182int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
183 unsigned long timeout)
184{
185 return wait_event_timeout(q->mq_freeze_wq,
186 percpu_ref_is_zero(&q->q_usage_counter),
187 timeout);
188}
189EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
Jens Axboe320ae512013-10-24 09:20:05 +0100190
Tejun Heof3af0202014-11-04 13:52:27 -0500191/*
192 * Guarantee no request is in use, so we can change any data structure of
193 * the queue afterward.
194 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400195void blk_freeze_queue(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500196{
Dan Williams3ef28e82015-10-21 13:20:12 -0400197 /*
198 * In the !blk_mq case we are only calling this to kill the
199 * q_usage_counter, otherwise this increases the freeze depth
200 * and waits for it to return to zero. For this reason there is
201 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
202 * exported to drivers as the only user for unfreeze is blk_mq.
203 */
Ming Lei1671d522017-03-27 20:06:57 +0800204 blk_freeze_queue_start(q);
Tejun Heof3af0202014-11-04 13:52:27 -0500205 blk_mq_freeze_queue_wait(q);
206}
Dan Williams3ef28e82015-10-21 13:20:12 -0400207
208void blk_mq_freeze_queue(struct request_queue *q)
209{
210 /*
211 * ...just an alias to keep freeze and unfreeze actions balanced
212 * in the blk_mq_* namespace
213 */
214 blk_freeze_queue(q);
215}
Jens Axboec761d962015-01-02 15:05:12 -0700216EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
Tejun Heof3af0202014-11-04 13:52:27 -0500217
Christoph Hellwigaec89dc2021-09-29 09:12:41 +0200218void __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic)
Jens Axboe320ae512013-10-24 09:20:05 +0100219{
Bob Liu7996a8b2019-05-21 11:25:55 +0800220 mutex_lock(&q->mq_freeze_lock);
Christoph Hellwigaec89dc2021-09-29 09:12:41 +0200221 if (force_atomic)
222 q->q_usage_counter.data->force_atomic = true;
Bob Liu7996a8b2019-05-21 11:25:55 +0800223 q->mq_freeze_depth--;
224 WARN_ON_ONCE(q->mq_freeze_depth < 0);
225 if (!q->mq_freeze_depth) {
Bart Van Asschebdd63162018-09-26 14:01:08 -0700226 percpu_ref_resurrect(&q->q_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100227 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600228 }
Bob Liu7996a8b2019-05-21 11:25:55 +0800229 mutex_unlock(&q->mq_freeze_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100230}
Christoph Hellwigaec89dc2021-09-29 09:12:41 +0200231
232void blk_mq_unfreeze_queue(struct request_queue *q)
233{
234 __blk_mq_unfreeze_queue(q, false);
235}
Keith Buschb4c6a022014-12-19 17:54:14 -0700236EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
Jens Axboe320ae512013-10-24 09:20:05 +0100237
Bart Van Assche852ec802017-06-21 10:55:47 -0700238/*
239 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
240 * mpt3sas driver such that this function can be removed.
241 */
242void blk_mq_quiesce_queue_nowait(struct request_queue *q)
243{
Ming Leie70feb82021-10-14 16:17:10 +0800244 unsigned long flags;
245
246 spin_lock_irqsave(&q->queue_lock, flags);
247 if (!q->quiesce_depth++)
248 blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
249 spin_unlock_irqrestore(&q->queue_lock, flags);
Bart Van Assche852ec802017-06-21 10:55:47 -0700250}
251EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
252
Bart Van Assche6a83e742016-11-02 10:09:51 -0600253/**
Ming Lei9ef4d022021-11-09 15:11:41 +0800254 * blk_mq_wait_quiesce_done() - wait until in-progress quiesce is done
255 * @q: request queue.
256 *
257 * Note: it is driver's responsibility for making sure that quiesce has
258 * been started.
259 */
260void blk_mq_wait_quiesce_done(struct request_queue *q)
261{
262 struct blk_mq_hw_ctx *hctx;
263 unsigned int i;
264 bool rcu = false;
265
266 queue_for_each_hw_ctx(q, hctx, i) {
267 if (hctx->flags & BLK_MQ_F_BLOCKING)
268 synchronize_srcu(hctx->srcu);
269 else
270 rcu = true;
271 }
272 if (rcu)
273 synchronize_rcu();
274}
275EXPORT_SYMBOL_GPL(blk_mq_wait_quiesce_done);
276
277/**
Ming Lei69e07c42017-06-06 23:22:07 +0800278 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
Bart Van Assche6a83e742016-11-02 10:09:51 -0600279 * @q: request queue.
280 *
281 * Note: this function does not prevent that the struct request end_io()
Ming Lei69e07c42017-06-06 23:22:07 +0800282 * callback function is invoked. Once this function is returned, we make
283 * sure no dispatch can happen until the queue is unquiesced via
284 * blk_mq_unquiesce_queue().
Bart Van Assche6a83e742016-11-02 10:09:51 -0600285 */
286void blk_mq_quiesce_queue(struct request_queue *q)
287{
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800288 blk_mq_quiesce_queue_nowait(q);
Ming Lei9ef4d022021-11-09 15:11:41 +0800289 blk_mq_wait_quiesce_done(q);
Bart Van Assche6a83e742016-11-02 10:09:51 -0600290}
291EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
292
Ming Leie4e73912017-06-06 23:22:03 +0800293/*
294 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
295 * @q: request queue.
296 *
297 * This function recovers queue into the state before quiescing
298 * which is done by blk_mq_quiesce_queue.
299 */
300void blk_mq_unquiesce_queue(struct request_queue *q)
301{
Ming Leie70feb82021-10-14 16:17:10 +0800302 unsigned long flags;
303 bool run_queue = false;
304
305 spin_lock_irqsave(&q->queue_lock, flags);
306 if (WARN_ON_ONCE(q->quiesce_depth <= 0)) {
307 ;
308 } else if (!--q->quiesce_depth) {
309 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
310 run_queue = true;
311 }
312 spin_unlock_irqrestore(&q->queue_lock, flags);
Ming Leif4560ff2017-06-18 14:24:27 -0600313
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800314 /* dispatch requests which are inserted during quiescing */
Ming Leie70feb82021-10-14 16:17:10 +0800315 if (run_queue)
316 blk_mq_run_hw_queues(q, true);
Ming Leie4e73912017-06-06 23:22:03 +0800317}
318EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
319
Jens Axboeaed3ea92014-12-22 14:04:42 -0700320void blk_mq_wake_waiters(struct request_queue *q)
321{
322 struct blk_mq_hw_ctx *hctx;
323 unsigned int i;
324
325 queue_for_each_hw_ctx(q, hctx, i)
326 if (blk_mq_hw_queue_mapped(hctx))
327 blk_mq_tag_wakeup_all(hctx->tags, true);
328}
329
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200330static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
Jens Axboefe6134f2021-10-19 09:32:58 -0600331 struct blk_mq_tags *tags, unsigned int tag, u64 alloc_time_ns)
Jens Axboe320ae512013-10-24 09:20:05 +0100332{
Pavel Begunkov605f7842021-10-18 21:37:28 +0100333 struct blk_mq_ctx *ctx = data->ctx;
334 struct blk_mq_hw_ctx *hctx = data->hctx;
335 struct request_queue *q = data->q;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200336 struct request *rq = tags->static_rqs[tag];
Bart Van Asschec3a148d2017-06-20 11:15:43 -0700337
Jens Axboec7b84d42021-10-19 09:33:00 -0600338 rq->q = q;
339 rq->mq_ctx = ctx;
340 rq->mq_hctx = hctx;
341 rq->cmd_flags = data->cmd_flags;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200342
Pavel Begunkov12845902021-10-18 21:37:29 +0100343 if (data->flags & BLK_MQ_REQ_PM)
Jens Axboe56f8da62021-10-19 09:32:57 -0600344 data->rq_flags |= RQF_PM;
Pavel Begunkov12845902021-10-18 21:37:29 +0100345 if (blk_queue_io_stat(q))
Jens Axboe56f8da62021-10-19 09:32:57 -0600346 data->rq_flags |= RQF_IO_STAT;
347 rq->rq_flags = data->rq_flags;
Pavel Begunkov12845902021-10-18 21:37:29 +0100348
Jens Axboec7b84d42021-10-19 09:33:00 -0600349 if (!(data->rq_flags & RQF_ELV)) {
Jens Axboe320ae512013-10-24 09:20:05 +0100350 rq->tag = tag;
351 rq->internal_tag = BLK_MQ_NO_TAG;
Jens Axboec7b84d42021-10-19 09:33:00 -0600352 } else {
353 rq->tag = BLK_MQ_NO_TAG;
354 rq->internal_tag = tag;
Jens Axboe320ae512013-10-24 09:20:05 +0100355 }
Jens Axboec7b84d42021-10-19 09:33:00 -0600356 rq->timeout = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100357
Pavel Begunkov4f266f22021-10-18 21:37:27 +0100358 if (blk_mq_need_time_stamp(rq))
359 rq->start_time_ns = ktime_get_ns();
360 else
361 rq->start_time_ns = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200362 rq->rq_disk = NULL;
363 rq->part = NULL;
Tejun Heo6f816b42019-08-28 15:05:57 -0700364#ifdef CONFIG_BLK_RQ_ALLOC_TIME
365 rq->alloc_time_ns = alloc_time_ns;
366#endif
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700367 rq->io_start_time_ns = 0;
Hou Tao3d244302019-05-21 15:59:03 +0800368 rq->stats_sectors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200369 rq->nr_phys_segments = 0;
370#if defined(CONFIG_BLK_DEV_INTEGRITY)
371 rq->nr_integrity_segments = 0;
372#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200373 rq->end_io = NULL;
374 rq->end_io_data = NULL;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200375
Pavel Begunkov4f266f22021-10-18 21:37:27 +0100376 blk_crypto_rq_set_defaults(rq);
377 INIT_LIST_HEAD(&rq->queuelist);
378 /* tag was already set */
379 WRITE_ONCE(rq->deadline, 0);
Keith Busch12f5b932018-05-29 15:52:28 +0200380 refcount_set(&rq->ref, 1);
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200381
Pavel Begunkov4f266f22021-10-18 21:37:27 +0100382 if (rq->rq_flags & RQF_ELV) {
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200383 struct elevator_queue *e = data->q->elevator;
384
385 rq->elv.icq = NULL;
Pavel Begunkov4f266f22021-10-18 21:37:27 +0100386 INIT_HLIST_NODE(&rq->hash);
387 RB_CLEAR_NODE(&rq->rb_node);
388
389 if (!op_is_flush(data->cmd_flags) &&
390 e->type->ops.prepare_request) {
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200391 if (e->type->icq_cache)
392 blk_mq_sched_assign_ioc(rq);
393
394 e->type->ops.prepare_request(rq);
395 rq->rq_flags |= RQF_ELVPRIV;
396 }
397 }
398
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200399 return rq;
Jens Axboe320ae512013-10-24 09:20:05 +0100400}
401
Jens Axboe349302d2021-10-09 13:10:39 -0600402static inline struct request *
403__blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data,
404 u64 alloc_time_ns)
405{
406 unsigned int tag, tag_offset;
Jens Axboefe6134f2021-10-19 09:32:58 -0600407 struct blk_mq_tags *tags;
Jens Axboe349302d2021-10-09 13:10:39 -0600408 struct request *rq;
Jens Axboefe6134f2021-10-19 09:32:58 -0600409 unsigned long tag_mask;
Jens Axboe349302d2021-10-09 13:10:39 -0600410 int i, nr = 0;
411
Jens Axboefe6134f2021-10-19 09:32:58 -0600412 tag_mask = blk_mq_get_tags(data, data->nr_tags, &tag_offset);
413 if (unlikely(!tag_mask))
Jens Axboe349302d2021-10-09 13:10:39 -0600414 return NULL;
415
Jens Axboefe6134f2021-10-19 09:32:58 -0600416 tags = blk_mq_tags_from_data(data);
417 for (i = 0; tag_mask; i++) {
418 if (!(tag_mask & (1UL << i)))
Jens Axboe349302d2021-10-09 13:10:39 -0600419 continue;
420 tag = tag_offset + i;
Jens Axboea22c00b2021-11-01 06:56:09 -0600421 prefetch(tags->static_rqs[tag]);
Jens Axboefe6134f2021-10-19 09:32:58 -0600422 tag_mask &= ~(1UL << i);
423 rq = blk_mq_rq_ctx_init(data, tags, tag, alloc_time_ns);
Jens Axboe013a7f92021-10-13 07:58:52 -0600424 rq_list_add(data->cached_rq, rq);
Jens Axboec5fc7b92021-11-03 05:49:07 -0600425 nr++;
Jens Axboe349302d2021-10-09 13:10:39 -0600426 }
Jens Axboec5fc7b92021-11-03 05:49:07 -0600427 /* caller already holds a reference, add for remainder */
428 percpu_ref_get_many(&data->q->q_usage_counter, nr - 1);
Jens Axboe349302d2021-10-09 13:10:39 -0600429 data->nr_tags -= nr;
430
Jens Axboe013a7f92021-10-13 07:58:52 -0600431 return rq_list_pop(data->cached_rq);
Jens Axboe349302d2021-10-09 13:10:39 -0600432}
433
Christoph Hellwigb90cfae2021-10-12 12:40:44 +0200434static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data)
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200435{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200436 struct request_queue *q = data->q;
Tejun Heo6f816b42019-08-28 15:05:57 -0700437 u64 alloc_time_ns = 0;
Jens Axboe47c122e2021-10-06 06:34:11 -0600438 struct request *rq;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200439 unsigned int tag;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200440
Tejun Heo6f816b42019-08-28 15:05:57 -0700441 /* alloc_time includes depth and tag waits */
442 if (blk_queue_rq_alloc_time(q))
443 alloc_time_ns = ktime_get_ns();
444
Jens Axboef9afca42018-10-29 13:11:38 -0600445 if (data->cmd_flags & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500446 data->flags |= BLK_MQ_REQ_NOWAIT;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200447
Jens Axboe781dd832021-11-02 08:34:09 -0600448 if (q->elevator) {
449 struct elevator_queue *e = q->elevator;
450
451 data->rq_flags |= RQF_ELV;
452
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200453 /*
Lin Feng8d663f32021-04-15 11:39:20 +0800454 * Flush/passthrough requests are special and go directly to the
Jens Axboe17a51192018-05-09 13:28:50 -0600455 * dispatch list. Don't include reserved tags in the
456 * limiting, as it isn't useful.
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200457 */
Jens Axboef9afca42018-10-29 13:11:38 -0600458 if (!op_is_flush(data->cmd_flags) &&
Lin Feng8d663f32021-04-15 11:39:20 +0800459 !blk_op_is_passthrough(data->cmd_flags) &&
Jens Axboef9afca42018-10-29 13:11:38 -0600460 e->type->ops.limit_depth &&
Jens Axboe17a51192018-05-09 13:28:50 -0600461 !(data->flags & BLK_MQ_REQ_RESERVED))
Jens Axboef9afca42018-10-29 13:11:38 -0600462 e->type->ops.limit_depth(data->cmd_flags, data);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200463 }
464
Ming Leibf0beec2020-05-29 15:53:15 +0200465retry:
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200466 data->ctx = blk_mq_get_ctx(q);
467 data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx);
Jens Axboe781dd832021-11-02 08:34:09 -0600468 if (!(data->rq_flags & RQF_ELV))
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200469 blk_mq_tag_busy(data->hctx);
470
Ming Leibf0beec2020-05-29 15:53:15 +0200471 /*
Jens Axboe349302d2021-10-09 13:10:39 -0600472 * Try batched alloc if we want more than 1 tag.
473 */
474 if (data->nr_tags > 1) {
475 rq = __blk_mq_alloc_requests_batch(data, alloc_time_ns);
476 if (rq)
477 return rq;
478 data->nr_tags = 1;
479 }
480
481 /*
Ming Leibf0beec2020-05-29 15:53:15 +0200482 * Waiting allocations only fail because of an inactive hctx. In that
483 * case just retry the hctx assignment and tag allocation as CPU hotplug
484 * should have migrated us to an online CPU by now.
485 */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200486 tag = blk_mq_get_tag(data);
Ming Leibf0beec2020-05-29 15:53:15 +0200487 if (tag == BLK_MQ_NO_TAG) {
488 if (data->flags & BLK_MQ_REQ_NOWAIT)
489 return NULL;
Ming Leibf0beec2020-05-29 15:53:15 +0200490 /*
Jens Axboe349302d2021-10-09 13:10:39 -0600491 * Give up the CPU and sleep for a random short time to
492 * ensure that thread using a realtime scheduling class
493 * are migrated off the CPU, and thus off the hctx that
494 * is going away.
Ming Leibf0beec2020-05-29 15:53:15 +0200495 */
496 msleep(3);
497 goto retry;
498 }
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200499
Jens Axboefe6134f2021-10-19 09:32:58 -0600500 return blk_mq_rq_ctx_init(data, blk_mq_tags_from_data(data), tag,
501 alloc_time_ns);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200502}
503
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700504struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800505 blk_mq_req_flags_t flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100506{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200507 struct blk_mq_alloc_data data = {
508 .q = q,
509 .flags = flags,
510 .cmd_flags = op,
Jens Axboe47c122e2021-10-06 06:34:11 -0600511 .nr_tags = 1,
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200512 };
Jens Axboebd166ef2017-01-17 06:03:22 -0700513 struct request *rq;
Joe Lawrencea492f072014-08-28 08:15:21 -0600514 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100515
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800516 ret = blk_queue_enter(q, flags);
Joe Lawrencea492f072014-08-28 08:15:21 -0600517 if (ret)
518 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100519
Christoph Hellwigb90cfae2021-10-12 12:40:44 +0200520 rq = __blk_mq_alloc_requests(&data);
Jens Axboebd166ef2017-01-17 06:03:22 -0700521 if (!rq)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200522 goto out_queue_exit;
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200523 rq->__data_len = 0;
524 rq->__sector = (sector_t) -1;
525 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100526 return rq;
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200527out_queue_exit:
528 blk_queue_exit(q);
529 return ERR_PTR(-EWOULDBLOCK);
Jens Axboe320ae512013-10-24 09:20:05 +0100530}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600531EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100532
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700533struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800534 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
Ming Lin1f5bd332016-06-13 16:45:21 +0200535{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200536 struct blk_mq_alloc_data data = {
537 .q = q,
538 .flags = flags,
539 .cmd_flags = op,
Jens Axboe47c122e2021-10-06 06:34:11 -0600540 .nr_tags = 1,
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200541 };
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200542 u64 alloc_time_ns = 0;
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800543 unsigned int cpu;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200544 unsigned int tag;
Ming Lin1f5bd332016-06-13 16:45:21 +0200545 int ret;
546
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200547 /* alloc_time includes depth and tag waits */
548 if (blk_queue_rq_alloc_time(q))
549 alloc_time_ns = ktime_get_ns();
550
Ming Lin1f5bd332016-06-13 16:45:21 +0200551 /*
552 * If the tag allocator sleeps we could get an allocation for a
553 * different hardware context. No need to complicate the low level
554 * allocator for this for the rare use case of a command tied to
555 * a specific queue.
556 */
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200557 if (WARN_ON_ONCE(!(flags & (BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED))))
Ming Lin1f5bd332016-06-13 16:45:21 +0200558 return ERR_PTR(-EINVAL);
559
560 if (hctx_idx >= q->nr_hw_queues)
561 return ERR_PTR(-EIO);
562
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800563 ret = blk_queue_enter(q, flags);
Ming Lin1f5bd332016-06-13 16:45:21 +0200564 if (ret)
565 return ERR_PTR(ret);
566
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600567 /*
568 * Check if the hardware context is actually mapped to anything.
569 * If not tell the caller that it should skip this queue.
570 */
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200571 ret = -EXDEV;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200572 data.hctx = q->queue_hw_ctx[hctx_idx];
573 if (!blk_mq_hw_queue_mapped(data.hctx))
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200574 goto out_queue_exit;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200575 cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
576 data.ctx = __blk_mq_get_ctx(q, cpu);
Ming Lin1f5bd332016-06-13 16:45:21 +0200577
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200578 if (!q->elevator)
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200579 blk_mq_tag_busy(data.hctx);
Jens Axboe781dd832021-11-02 08:34:09 -0600580 else
581 data.rq_flags |= RQF_ELV;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200582
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200583 ret = -EWOULDBLOCK;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200584 tag = blk_mq_get_tag(&data);
585 if (tag == BLK_MQ_NO_TAG)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200586 goto out_queue_exit;
Jens Axboefe6134f2021-10-19 09:32:58 -0600587 return blk_mq_rq_ctx_init(&data, blk_mq_tags_from_data(&data), tag,
588 alloc_time_ns);
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200589
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200590out_queue_exit:
591 blk_queue_exit(q);
592 return ERR_PTR(ret);
Ming Lin1f5bd332016-06-13 16:45:21 +0200593}
594EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
595
Keith Busch12f5b932018-05-29 15:52:28 +0200596static void __blk_mq_free_request(struct request *rq)
597{
598 struct request_queue *q = rq->q;
599 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600600 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Keith Busch12f5b932018-05-29 15:52:28 +0200601 const int sched_tag = rq->internal_tag;
602
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000603 blk_crypto_free_request(rq);
Bart Van Assche986d4132018-09-26 14:01:10 -0700604 blk_pm_mark_last_busy(rq);
Jens Axboeea4f9952018-10-29 15:06:13 -0600605 rq->mq_hctx = NULL;
Christoph Hellwig766473682020-05-29 15:53:12 +0200606 if (rq->tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800607 blk_mq_put_tag(hctx->tags, ctx, rq->tag);
Christoph Hellwig766473682020-05-29 15:53:12 +0200608 if (sched_tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800609 blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
Keith Busch12f5b932018-05-29 15:52:28 +0200610 blk_mq_sched_restart(hctx);
611 blk_queue_exit(q);
612}
613
Christoph Hellwig6af54052017-06-16 18:15:22 +0200614void blk_mq_free_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100615{
Jens Axboe320ae512013-10-24 09:20:05 +0100616 struct request_queue *q = rq->q;
Jens Axboeea4f9952018-10-29 15:06:13 -0600617 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100618
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200619 if (rq->rq_flags & RQF_ELVPRIV) {
Jens Axboe2ff06822021-10-15 09:44:38 -0600620 struct elevator_queue *e = q->elevator;
621
622 if (e->type->ops.finish_request)
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600623 e->type->ops.finish_request(rq);
Christoph Hellwig6af54052017-06-16 18:15:22 +0200624 if (rq->elv.icq) {
625 put_io_context(rq->elv.icq->ioc);
626 rq->elv.icq = NULL;
627 }
628 }
629
Christoph Hellwige8064022016-10-20 15:12:13 +0200630 if (rq->rq_flags & RQF_MQ_INFLIGHT)
John Garrybccf5e22020-08-19 23:20:26 +0800631 __blk_mq_dec_active_requests(hctx);
Jens Axboe87760e52016-11-09 12:38:14 -0700632
Jens Axboe7beb2f82017-09-30 02:08:24 -0600633 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
Christoph Hellwigd152c682021-08-16 15:46:24 +0200634 laptop_io_completion(q->disk->bdi);
Jens Axboe7beb2f82017-09-30 02:08:24 -0600635
Josef Bacika7905042018-07-03 09:32:35 -0600636 rq_qos_done(q, rq);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600637
Keith Busch12f5b932018-05-29 15:52:28 +0200638 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
639 if (refcount_dec_and_test(&rq->ref))
640 __blk_mq_free_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100641}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700642EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100643
Jens Axboe47c122e2021-10-06 06:34:11 -0600644void blk_mq_free_plug_rqs(struct blk_plug *plug)
Jens Axboe320ae512013-10-24 09:20:05 +0100645{
Jens Axboe013a7f92021-10-13 07:58:52 -0600646 struct request *rq;
Jens Axboefe1f4522018-11-28 10:50:07 -0700647
Jens Axboec5fc7b92021-11-03 05:49:07 -0600648 while ((rq = rq_list_pop(&plug->cached_rq)) != NULL)
Jens Axboe47c122e2021-10-06 06:34:11 -0600649 blk_mq_free_request(rq);
Jens Axboe47c122e2021-10-06 06:34:11 -0600650}
Omar Sandoval522a7772018-05-09 02:08:53 -0700651
Jens Axboe9be3e062021-10-14 09:17:01 -0600652static void req_bio_endio(struct request *rq, struct bio *bio,
653 unsigned int nbytes, blk_status_t error)
654{
Pavel Begunkov478eb722021-10-19 22:24:12 +0100655 if (unlikely(error)) {
Jens Axboe9be3e062021-10-14 09:17:01 -0600656 bio->bi_status = error;
Pavel Begunkov478eb722021-10-19 22:24:12 +0100657 } else if (req_op(rq) == REQ_OP_ZONE_APPEND) {
Jens Axboe9be3e062021-10-14 09:17:01 -0600658 /*
659 * Partial zone append completions cannot be supported as the
660 * BIO fragments may end up not being written sequentially.
661 */
Pavel Begunkov297db732021-10-22 16:01:44 +0100662 if (bio->bi_iter.bi_size != nbytes)
Jens Axboe9be3e062021-10-14 09:17:01 -0600663 bio->bi_status = BLK_STS_IOERR;
664 else
665 bio->bi_iter.bi_sector = rq->__sector;
666 }
667
Pavel Begunkov478eb722021-10-19 22:24:12 +0100668 bio_advance(bio, nbytes);
669
670 if (unlikely(rq->rq_flags & RQF_QUIET))
671 bio_set_flag(bio, BIO_QUIET);
Jens Axboe9be3e062021-10-14 09:17:01 -0600672 /* don't actually finish bio if it's part of flush sequence */
673 if (bio->bi_iter.bi_size == 0 && !(rq->rq_flags & RQF_FLUSH_SEQ))
674 bio_endio(bio);
675}
676
677static void blk_account_io_completion(struct request *req, unsigned int bytes)
678{
679 if (req->part && blk_do_io_stat(req)) {
680 const int sgrp = op_stat_group(req_op(req));
681
682 part_stat_lock();
683 part_stat_add(req->part, sectors[sgrp], bytes >> 9);
684 part_stat_unlock();
685 }
686}
687
688/**
689 * blk_update_request - Complete multiple bytes without completing the request
690 * @req: the request being processed
691 * @error: block status code
692 * @nr_bytes: number of bytes to complete for @req
693 *
694 * Description:
695 * Ends I/O on a number of bytes attached to @req, but doesn't complete
696 * the request structure even if @req doesn't have leftover.
697 * If @req has leftover, sets it up for the next range of segments.
698 *
699 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
700 * %false return from this function.
701 *
702 * Note:
703 * The RQF_SPECIAL_PAYLOAD flag is ignored on purpose in this function
704 * except in the consistency check at the end of this function.
705 *
706 * Return:
707 * %false - this request doesn't have any more data
708 * %true - this request has more data
709 **/
710bool blk_update_request(struct request *req, blk_status_t error,
711 unsigned int nr_bytes)
712{
713 int total_bytes;
714
Christoph Hellwig8a7d2672021-10-18 10:45:18 +0200715 trace_block_rq_complete(req, error, nr_bytes);
Jens Axboe9be3e062021-10-14 09:17:01 -0600716
717 if (!req->bio)
718 return false;
719
720#ifdef CONFIG_BLK_DEV_INTEGRITY
721 if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ &&
722 error == BLK_STS_OK)
723 req->q->integrity.profile->complete_fn(req, nr_bytes);
724#endif
725
726 if (unlikely(error && !blk_rq_is_passthrough(req) &&
727 !(req->rq_flags & RQF_QUIET)))
728 blk_print_req_error(req, error);
729
730 blk_account_io_completion(req, nr_bytes);
731
732 total_bytes = 0;
733 while (req->bio) {
734 struct bio *bio = req->bio;
735 unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
736
737 if (bio_bytes == bio->bi_iter.bi_size)
738 req->bio = bio->bi_next;
739
740 /* Completion has already been traced */
741 bio_clear_flag(bio, BIO_TRACE_COMPLETION);
742 req_bio_endio(req, bio, bio_bytes, error);
743
744 total_bytes += bio_bytes;
745 nr_bytes -= bio_bytes;
746
747 if (!nr_bytes)
748 break;
749 }
750
751 /*
752 * completely done
753 */
754 if (!req->bio) {
755 /*
756 * Reset counters so that the request stacking driver
757 * can find how many bytes remain in the request
758 * later.
759 */
760 req->__data_len = 0;
761 return false;
762 }
763
764 req->__data_len -= total_bytes;
765
766 /* update sector only for requests with clear definition of sector */
767 if (!blk_rq_is_passthrough(req))
768 req->__sector += total_bytes >> 9;
769
770 /* mixed attributes always follow the first bio */
771 if (req->rq_flags & RQF_MIXED_MERGE) {
772 req->cmd_flags &= ~REQ_FAILFAST_MASK;
773 req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK;
774 }
775
776 if (!(req->rq_flags & RQF_SPECIAL_PAYLOAD)) {
777 /*
778 * If total number of sectors is less than the first segment
779 * size, something has gone terribly wrong.
780 */
781 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
782 blk_dump_rq_flags(req, "request botched");
783 req->__data_len = blk_rq_cur_bytes(req);
784 }
785
786 /* recalculate the number of segments */
787 req->nr_phys_segments = blk_recalc_rq_segments(req);
788 }
789
790 return true;
791}
792EXPORT_SYMBOL_GPL(blk_update_request);
793
Jens Axboef794f332021-10-08 05:50:46 -0600794static inline void __blk_mq_end_request_acct(struct request *rq, u64 now)
795{
Omar Sandoval4bc63392018-05-09 02:08:52 -0700796 if (rq->rq_flags & RQF_STATS) {
797 blk_mq_poll_stats_start(rq->q);
Omar Sandoval522a7772018-05-09 02:08:53 -0700798 blk_stat_add(rq, now);
Omar Sandoval4bc63392018-05-09 02:08:52 -0700799 }
800
Baolin Wang87890092020-07-04 15:28:21 +0800801 blk_mq_sched_completed_request(rq, now);
Omar Sandoval522a7772018-05-09 02:08:53 -0700802 blk_account_io_done(rq, now);
Jens Axboef794f332021-10-08 05:50:46 -0600803}
804
Ming Lei0d11e6a2013-12-05 10:50:39 -0700805inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig91b63632014-04-16 09:44:53 +0200806{
Jens Axboef794f332021-10-08 05:50:46 -0600807 if (blk_mq_need_time_stamp(rq))
808 __blk_mq_end_request_acct(rq, ktime_get_ns());
Jens Axboe320ae512013-10-24 09:20:05 +0100809
810 if (rq->end_io) {
Josef Bacika7905042018-07-03 09:32:35 -0600811 rq_qos_done(rq->q, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100812 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200813 } else {
Jens Axboe320ae512013-10-24 09:20:05 +0100814 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200815 }
Jens Axboe320ae512013-10-24 09:20:05 +0100816}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700817EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200818
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200819void blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200820{
821 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
822 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700823 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200824}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700825EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100826
Jens Axboef794f332021-10-08 05:50:46 -0600827#define TAG_COMP_BATCH 32
828
829static inline void blk_mq_flush_tag_batch(struct blk_mq_hw_ctx *hctx,
830 int *tag_array, int nr_tags)
831{
832 struct request_queue *q = hctx->queue;
833
Ming Lei3b87c6e2021-11-02 23:36:19 +0800834 /*
835 * All requests should have been marked as RQF_MQ_INFLIGHT, so
836 * update hctx->nr_active in batch
837 */
838 if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
839 __blk_mq_sub_active_requests(hctx, nr_tags);
840
Jens Axboef794f332021-10-08 05:50:46 -0600841 blk_mq_put_tags(hctx->tags, tag_array, nr_tags);
842 percpu_ref_put_many(&q->q_usage_counter, nr_tags);
843}
844
845void blk_mq_end_request_batch(struct io_comp_batch *iob)
846{
847 int tags[TAG_COMP_BATCH], nr_tags = 0;
Jens Axboe02f7eab2021-10-28 12:08:34 -0600848 struct blk_mq_hw_ctx *cur_hctx = NULL;
Jens Axboef794f332021-10-08 05:50:46 -0600849 struct request *rq;
850 u64 now = 0;
851
852 if (iob->need_ts)
853 now = ktime_get_ns();
854
855 while ((rq = rq_list_pop(&iob->req_list)) != NULL) {
856 prefetch(rq->bio);
857 prefetch(rq->rq_next);
858
859 blk_update_request(rq, BLK_STS_OK, blk_rq_bytes(rq));
860 if (iob->need_ts)
861 __blk_mq_end_request_acct(rq, now);
862
Jens Axboe98b26a02021-11-26 09:53:23 -0700863 rq_qos_done(rq->q, rq);
864
Jens Axboef794f332021-10-08 05:50:46 -0600865 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
866 if (!refcount_dec_and_test(&rq->ref))
867 continue;
868
869 blk_crypto_free_request(rq);
870 blk_pm_mark_last_busy(rq);
Jens Axboef794f332021-10-08 05:50:46 -0600871
Jens Axboe02f7eab2021-10-28 12:08:34 -0600872 if (nr_tags == TAG_COMP_BATCH || cur_hctx != rq->mq_hctx) {
873 if (cur_hctx)
874 blk_mq_flush_tag_batch(cur_hctx, tags, nr_tags);
Jens Axboef794f332021-10-08 05:50:46 -0600875 nr_tags = 0;
Jens Axboe02f7eab2021-10-28 12:08:34 -0600876 cur_hctx = rq->mq_hctx;
Jens Axboef794f332021-10-08 05:50:46 -0600877 }
878 tags[nr_tags++] = rq->tag;
Jens Axboef794f332021-10-08 05:50:46 -0600879 }
880
881 if (nr_tags)
Jens Axboe02f7eab2021-10-28 12:08:34 -0600882 blk_mq_flush_tag_batch(cur_hctx, tags, nr_tags);
Jens Axboef794f332021-10-08 05:50:46 -0600883}
884EXPORT_SYMBOL_GPL(blk_mq_end_request_batch);
885
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100886static void blk_complete_reqs(struct llist_head *list)
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200887{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100888 struct llist_node *entry = llist_reverse_order(llist_del_all(list));
889 struct request *rq, *next;
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200890
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100891 llist_for_each_entry_safe(rq, next, entry, ipi_list)
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200892 rq->q->mq_ops->complete(rq);
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200893}
894
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100895static __latent_entropy void blk_done_softirq(struct softirq_action *h)
Christoph Hellwig115243f2020-06-11 08:44:42 +0200896{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100897 blk_complete_reqs(this_cpu_ptr(&blk_cpu_done));
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200898}
899
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200900static int blk_softirq_cpu_dead(unsigned int cpu)
901{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100902 blk_complete_reqs(&per_cpu(blk_cpu_done, cpu));
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200903 return 0;
904}
905
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800906static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100907{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100908 __raise_softirq_irqoff(BLOCK_SOFTIRQ);
Jens Axboe320ae512013-10-24 09:20:05 +0100909}
910
Christoph Hellwig963395262020-06-11 08:44:49 +0200911static inline bool blk_mq_complete_need_ipi(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100912{
Christoph Hellwig963395262020-06-11 08:44:49 +0200913 int cpu = raw_smp_processor_id();
Jens Axboe320ae512013-10-24 09:20:05 +0100914
Christoph Hellwig963395262020-06-11 08:44:49 +0200915 if (!IS_ENABLED(CONFIG_SMP) ||
916 !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags))
917 return false;
Sebastian Andrzej Siewior71425182020-12-04 20:13:54 +0100918 /*
919 * With force threaded interrupts enabled, raising softirq from an SMP
920 * function call will always result in waking the ksoftirqd thread.
921 * This is probably worse than completing the request on a different
922 * cache domain.
923 */
Tanner Love91cc4702021-06-02 14:03:38 -0400924 if (force_irqthreads())
Sebastian Andrzej Siewior71425182020-12-04 20:13:54 +0100925 return false;
Christoph Hellwig963395262020-06-11 08:44:49 +0200926
927 /* same CPU or cache domain? Complete locally */
928 if (cpu == rq->mq_ctx->cpu ||
929 (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) &&
930 cpus_share_cache(cpu, rq->mq_ctx->cpu)))
931 return false;
932
933 /* don't try to IPI to an offline CPU */
934 return cpu_online(rq->mq_ctx->cpu);
935}
936
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100937static void blk_mq_complete_send_ipi(struct request *rq)
938{
939 struct llist_head *list;
940 unsigned int cpu;
941
942 cpu = rq->mq_ctx->cpu;
943 list = &per_cpu(blk_cpu_done, cpu);
944 if (llist_add(&rq->ipi_list, list)) {
945 INIT_CSD(&rq->csd, __blk_mq_complete_request_remote, rq);
946 smp_call_function_single_async(cpu, &rq->csd);
947 }
948}
949
950static void blk_mq_raise_softirq(struct request *rq)
951{
952 struct llist_head *list;
953
954 preempt_disable();
955 list = this_cpu_ptr(&blk_cpu_done);
956 if (llist_add(&rq->ipi_list, list))
957 raise_softirq(BLOCK_SOFTIRQ);
958 preempt_enable();
959}
960
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200961bool blk_mq_complete_request_remote(struct request *rq)
962{
Keith Buschaf78ff72018-11-26 09:54:30 -0700963 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
Ming Lei36e76532018-09-28 16:42:20 +0800964
Jens Axboe4ab32bf2018-11-18 16:15:35 -0700965 /*
966 * For a polled request, always complete locallly, it's pointless
967 * to redirect the completion.
968 */
Christoph Hellwig6ce913f2021-10-12 13:12:21 +0200969 if (rq->cmd_flags & REQ_POLLED)
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200970 return false;
Jens Axboe320ae512013-10-24 09:20:05 +0100971
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200972 if (blk_mq_complete_need_ipi(rq)) {
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100973 blk_mq_complete_send_ipi(rq);
974 return true;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800975 }
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200976
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100977 if (rq->q->nr_hw_queues == 1) {
978 blk_mq_raise_softirq(rq);
979 return true;
980 }
981 return false;
Jens Axboe320ae512013-10-24 09:20:05 +0100982}
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200983EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote);
984
Jens Axboe320ae512013-10-24 09:20:05 +0100985/**
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200986 * blk_mq_complete_request - end I/O on a request
987 * @rq: the request being processed
Jens Axboe320ae512013-10-24 09:20:05 +0100988 *
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200989 * Description:
990 * Complete a request by scheduling the ->complete_rq operation.
991 **/
992void blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100993{
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200994 if (!blk_mq_complete_request_remote(rq))
Christoph Hellwig963395262020-06-11 08:44:49 +0200995 rq->q->mq_ops->complete(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100996}
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200997EXPORT_SYMBOL(blk_mq_complete_request);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800998
Jens Axboe04ced152018-01-09 08:29:46 -0800999static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -08001000 __releases(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -08001001{
1002 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
1003 rcu_read_unlock();
1004 else
Tejun Heo05707b62018-01-09 08:29:53 -08001005 srcu_read_unlock(hctx->srcu, srcu_idx);
Jens Axboe04ced152018-01-09 08:29:46 -08001006}
1007
1008static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -08001009 __acquires(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -08001010{
Jens Axboe08b5a6e2018-01-09 09:32:25 -07001011 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
1012 /* shut up gcc false positive */
1013 *srcu_idx = 0;
Jens Axboe04ced152018-01-09 08:29:46 -08001014 rcu_read_lock();
Jens Axboe08b5a6e2018-01-09 09:32:25 -07001015 } else
Tejun Heo05707b62018-01-09 08:29:53 -08001016 *srcu_idx = srcu_read_lock(hctx->srcu);
Jens Axboe04ced152018-01-09 08:29:46 -08001017}
1018
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001019/**
André Almeida105663f2020-01-06 15:08:18 -03001020 * blk_mq_start_request - Start processing a request
1021 * @rq: Pointer to request to be started
1022 *
1023 * Function used by device drivers to notify the block layer that a request
1024 * is going to be processed now, so blk layer can do proper initializations
1025 * such as starting the timeout timer.
1026 */
Christoph Hellwige2490072014-09-13 16:40:09 -07001027void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001028{
1029 struct request_queue *q = rq->q;
1030
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001031 trace_block_rq_issue(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001032
Jens Axboecf43e6b2016-11-07 21:32:37 -07001033 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
Jens Axboe00067072021-10-05 09:23:59 -06001034 u64 start_time;
1035#ifdef CONFIG_BLK_CGROUP
1036 if (rq->bio)
1037 start_time = bio_issue_time(&rq->bio->bi_issue);
1038 else
1039#endif
1040 start_time = ktime_get_ns();
1041 rq->io_start_time_ns = start_time;
Hou Tao3d244302019-05-21 15:59:03 +08001042 rq->stats_sectors = blk_rq_sectors(rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -07001043 rq->rq_flags |= RQF_STATS;
Josef Bacika7905042018-07-03 09:32:35 -06001044 rq_qos_issue(q, rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -07001045 }
1046
Tejun Heo1d9bd512018-01-09 08:29:48 -08001047 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
Jens Axboe538b7532014-09-16 10:37:37 -06001048
Tejun Heo1d9bd512018-01-09 08:29:48 -08001049 blk_add_timer(rq);
Keith Busch12f5b932018-05-29 15:52:28 +02001050 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -08001051
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +03001052#ifdef CONFIG_BLK_DEV_INTEGRITY
1053 if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
1054 q->integrity.profile->prepare_fn(rq);
1055#endif
Christoph Hellwig3e087732021-10-12 13:12:24 +02001056 if (rq->bio && rq->bio->bi_opf & REQ_POLLED)
1057 WRITE_ONCE(rq->bio->bi_cookie, blk_rq_to_qc(rq));
Jens Axboe320ae512013-10-24 09:20:05 +01001058}
Christoph Hellwige2490072014-09-13 16:40:09 -07001059EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +01001060
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001061static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001062{
1063 struct request_queue *q = rq->q;
1064
Ming Lei923218f2017-11-02 23:24:38 +08001065 blk_mq_put_driver_tag(rq);
1066
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001067 trace_block_rq_requeue(rq);
Josef Bacika7905042018-07-03 09:32:35 -06001068 rq_qos_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -08001069
Keith Busch12f5b932018-05-29 15:52:28 +02001070 if (blk_mq_request_started(rq)) {
1071 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Christoph Hellwigda661262018-06-14 13:58:45 +02001072 rq->rq_flags &= ~RQF_TIMED_OUT;
Christoph Hellwige2490072014-09-13 16:40:09 -07001073 }
Jens Axboe320ae512013-10-24 09:20:05 +01001074}
1075
Bart Van Assche2b053ac2016-10-28 17:21:41 -07001076void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001077{
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001078 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001079
Ming Lei105976f2018-02-23 23:36:56 +08001080 /* this request will be re-inserted to io scheduler queue */
1081 blk_mq_sched_requeue_request(rq);
1082
Bart Van Assche2b053ac2016-10-28 17:21:41 -07001083 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001084}
1085EXPORT_SYMBOL(blk_mq_requeue_request);
1086
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001087static void blk_mq_requeue_work(struct work_struct *work)
1088{
1089 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -04001090 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001091 LIST_HEAD(rq_list);
1092 struct request *rq, *next;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001093
Jens Axboe18e97812017-07-27 08:03:57 -06001094 spin_lock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001095 list_splice_init(&q->requeue_list, &rq_list);
Jens Axboe18e97812017-07-27 08:03:57 -06001096 spin_unlock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001097
1098 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
Jianchao Wangaef18972019-02-12 09:56:25 +08001099 if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP)))
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001100 continue;
1101
Christoph Hellwige8064022016-10-20 15:12:13 +02001102 rq->rq_flags &= ~RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001103 list_del_init(&rq->queuelist);
Jianchao Wangaef18972019-02-12 09:56:25 +08001104 /*
1105 * If RQF_DONTPREP, rq has contained some driver specific
1106 * data, so insert it to hctx dispatch list to avoid any
1107 * merge.
1108 */
1109 if (rq->rq_flags & RQF_DONTPREP)
Ming Lei01e99ae2020-02-25 09:04:32 +08001110 blk_mq_request_bypass_insert(rq, false, false);
Jianchao Wangaef18972019-02-12 09:56:25 +08001111 else
1112 blk_mq_sched_insert_request(rq, true, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001113 }
1114
1115 while (!list_empty(&rq_list)) {
1116 rq = list_entry(rq_list.next, struct request, queuelist);
1117 list_del_init(&rq->queuelist);
Mike Snitzer9e97d292018-01-17 11:25:58 -05001118 blk_mq_sched_insert_request(rq, false, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001119 }
1120
Bart Van Assche52d7f1b2016-10-28 17:20:32 -07001121 blk_mq_run_hw_queues(q, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001122}
1123
Bart Van Assche2b053ac2016-10-28 17:21:41 -07001124void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
1125 bool kick_requeue_list)
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001126{
1127 struct request_queue *q = rq->q;
1128 unsigned long flags;
1129
1130 /*
1131 * We abuse this flag that is otherwise used by the I/O scheduler to
Jens Axboeff821d22017-11-10 22:05:12 -07001132 * request head insertion from the workqueue.
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001133 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001134 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001135
1136 spin_lock_irqsave(&q->requeue_lock, flags);
1137 if (at_head) {
Christoph Hellwige8064022016-10-20 15:12:13 +02001138 rq->rq_flags |= RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001139 list_add(&rq->queuelist, &q->requeue_list);
1140 } else {
1141 list_add_tail(&rq->queuelist, &q->requeue_list);
1142 }
1143 spin_unlock_irqrestore(&q->requeue_lock, flags);
Bart Van Assche2b053ac2016-10-28 17:21:41 -07001144
1145 if (kick_requeue_list)
1146 blk_mq_kick_requeue_list(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001147}
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001148
1149void blk_mq_kick_requeue_list(struct request_queue *q)
1150{
Bart Van Asscheae943d22018-01-19 08:58:55 -08001151 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001152}
1153EXPORT_SYMBOL(blk_mq_kick_requeue_list);
1154
Mike Snitzer28494502016-09-14 13:28:30 -04001155void blk_mq_delay_kick_requeue_list(struct request_queue *q,
1156 unsigned long msecs)
1157{
Bart Van Assched4acf362017-08-09 11:28:06 -07001158 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
1159 msecs_to_jiffies(msecs));
Mike Snitzer28494502016-09-14 13:28:30 -04001160}
1161EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
1162
Jens Axboe3c94d832018-12-17 21:11:17 -07001163static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq,
1164 void *priv, bool reserved)
Jens Axboeae879912018-11-08 09:03:51 -07001165{
1166 /*
Ming Lei05a4fed2020-07-07 11:04:33 -04001167 * If we find a request that isn't idle and the queue matches,
Jens Axboe3c94d832018-12-17 21:11:17 -07001168 * we know the queue is busy. Return false to stop the iteration.
Jens Axboeae879912018-11-08 09:03:51 -07001169 */
Ming Lei05a4fed2020-07-07 11:04:33 -04001170 if (blk_mq_request_started(rq) && rq->q == hctx->queue) {
Jens Axboeae879912018-11-08 09:03:51 -07001171 bool *busy = priv;
1172
1173 *busy = true;
1174 return false;
1175 }
1176
1177 return true;
1178}
1179
Jens Axboe3c94d832018-12-17 21:11:17 -07001180bool blk_mq_queue_inflight(struct request_queue *q)
Jens Axboeae879912018-11-08 09:03:51 -07001181{
1182 bool busy = false;
1183
Jens Axboe3c94d832018-12-17 21:11:17 -07001184 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
Jens Axboeae879912018-11-08 09:03:51 -07001185 return busy;
1186}
Jens Axboe3c94d832018-12-17 21:11:17 -07001187EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
Jens Axboeae879912018-11-08 09:03:51 -07001188
Tejun Heo358f70d2018-01-09 08:29:50 -08001189static void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +01001190{
Christoph Hellwigda661262018-06-14 13:58:45 +02001191 req->rq_flags |= RQF_TIMED_OUT;
Christoph Hellwigd1210d52018-05-29 15:52:39 +02001192 if (req->q->mq_ops->timeout) {
1193 enum blk_eh_timer_return ret;
Jens Axboe87ee7b12014-04-24 08:51:47 -06001194
Christoph Hellwigd1210d52018-05-29 15:52:39 +02001195 ret = req->q->mq_ops->timeout(req, reserved);
1196 if (ret == BLK_EH_DONE)
1197 return;
1198 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
Christoph Hellwig46f92d42014-09-13 16:40:12 -07001199 }
Christoph Hellwigd1210d52018-05-29 15:52:39 +02001200
1201 blk_add_timer(req);
Jens Axboe87ee7b12014-04-24 08:51:47 -06001202}
Keith Busch5b3f25f2015-01-07 18:55:46 -07001203
Keith Busch12f5b932018-05-29 15:52:28 +02001204static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
1205{
1206 unsigned long deadline;
1207
1208 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
1209 return false;
Christoph Hellwigda661262018-06-14 13:58:45 +02001210 if (rq->rq_flags & RQF_TIMED_OUT)
1211 return false;
Keith Busch12f5b932018-05-29 15:52:28 +02001212
Christoph Hellwig079076b2018-11-14 17:02:05 +01001213 deadline = READ_ONCE(rq->deadline);
Keith Busch12f5b932018-05-29 15:52:28 +02001214 if (time_after_eq(jiffies, deadline))
1215 return true;
1216
1217 if (*next == 0)
1218 *next = deadline;
1219 else if (time_after(*next, deadline))
1220 *next = deadline;
1221 return false;
1222}
1223
Ming Lei2e315dc2021-05-11 23:22:34 +08001224void blk_mq_put_rq_ref(struct request *rq)
1225{
Ming Leia9ed27a2021-08-18 09:09:25 +08001226 if (is_flush_rq(rq))
Ming Lei2e315dc2021-05-11 23:22:34 +08001227 rq->end_io(rq, 0);
1228 else if (refcount_dec_and_test(&rq->ref))
1229 __blk_mq_free_request(rq);
1230}
1231
Jens Axboe7baa8572018-11-08 10:24:07 -07001232static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig81481eb2014-09-13 16:40:11 -07001233 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +01001234{
Keith Busch12f5b932018-05-29 15:52:28 +02001235 unsigned long *next = priv;
Christoph Hellwig81481eb2014-09-13 16:40:11 -07001236
Keith Busch12f5b932018-05-29 15:52:28 +02001237 /*
Ming Leic797b402021-08-11 23:52:02 +08001238 * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot
1239 * be reallocated underneath the timeout handler's processing, then
1240 * the expire check is reliable. If the request is not expired, then
1241 * it was completed and reallocated as a new request after returning
1242 * from blk_mq_check_expired().
Keith Busch12f5b932018-05-29 15:52:28 +02001243 */
1244 if (blk_mq_req_expired(rq, next))
Tejun Heo1d9bd512018-01-09 08:29:48 -08001245 blk_mq_rq_timed_out(rq, reserved);
Jens Axboe7baa8572018-11-08 10:24:07 -07001246 return true;
Tejun Heo1d9bd512018-01-09 08:29:48 -08001247}
1248
Christoph Hellwig287922e2015-10-30 20:57:30 +08001249static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001250{
Christoph Hellwig287922e2015-10-30 20:57:30 +08001251 struct request_queue *q =
1252 container_of(work, struct request_queue, timeout_work);
Keith Busch12f5b932018-05-29 15:52:28 +02001253 unsigned long next = 0;
Tejun Heo1d9bd512018-01-09 08:29:48 -08001254 struct blk_mq_hw_ctx *hctx;
Christoph Hellwig81481eb2014-09-13 16:40:11 -07001255 int i;
Jens Axboe320ae512013-10-24 09:20:05 +01001256
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -06001257 /* A deadlock might occur if a request is stuck requiring a
1258 * timeout at the same time a queue freeze is waiting
1259 * completion, since the timeout code would not be able to
1260 * acquire the queue reference here.
1261 *
1262 * That's why we don't use blk_queue_enter here; instead, we use
1263 * percpu_ref_tryget directly, because we need to be able to
1264 * obtain a reference even in the short window between the queue
1265 * starting to freeze, by dropping the first reference in
Ming Lei1671d522017-03-27 20:06:57 +08001266 * blk_freeze_queue_start, and the moment the last request is
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -06001267 * consumed, marked by the instant q_usage_counter reaches
1268 * zero.
1269 */
1270 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +08001271 return;
1272
Keith Busch12f5b932018-05-29 15:52:28 +02001273 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
Jens Axboe320ae512013-10-24 09:20:05 +01001274
Keith Busch12f5b932018-05-29 15:52:28 +02001275 if (next != 0) {
1276 mod_timer(&q->timeout, next);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001277 } else {
Bart Van Asschefcd36c32018-01-10 08:33:33 -08001278 /*
1279 * Request timeouts are handled as a forward rolling timer. If
1280 * we end up here it means that no requests are pending and
1281 * also that no request has been pending for a while. Mark
1282 * each hctx as idle.
1283 */
Ming Leif054b562015-04-21 10:00:19 +08001284 queue_for_each_hw_ctx(q, hctx, i) {
1285 /* the hctx may be unmapped, so check it here */
1286 if (blk_mq_hw_queue_mapped(hctx))
1287 blk_mq_tag_idle(hctx);
1288 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06001289 }
Christoph Hellwig287922e2015-10-30 20:57:30 +08001290 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001291}
1292
Omar Sandoval88459642016-09-17 08:38:44 -06001293struct flush_busy_ctx_data {
1294 struct blk_mq_hw_ctx *hctx;
1295 struct list_head *list;
1296};
1297
1298static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
1299{
1300 struct flush_busy_ctx_data *flush_data = data;
1301 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
1302 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001303 enum hctx_type type = hctx->type;
Omar Sandoval88459642016-09-17 08:38:44 -06001304
Omar Sandoval88459642016-09-17 08:38:44 -06001305 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001306 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
Omar Sandovale9a99a62018-02-27 16:56:42 -08001307 sbitmap_clear_bit(sb, bitnr);
Omar Sandoval88459642016-09-17 08:38:44 -06001308 spin_unlock(&ctx->lock);
1309 return true;
1310}
1311
Jens Axboe320ae512013-10-24 09:20:05 +01001312/*
Jens Axboe1429d7c2014-05-19 09:23:55 -06001313 * Process software queues that have been marked busy, splicing them
1314 * to the for-dispatch
1315 */
Jens Axboe2c3ad662016-12-14 14:34:47 -07001316void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
Jens Axboe1429d7c2014-05-19 09:23:55 -06001317{
Omar Sandoval88459642016-09-17 08:38:44 -06001318 struct flush_busy_ctx_data data = {
1319 .hctx = hctx,
1320 .list = list,
1321 };
Jens Axboe1429d7c2014-05-19 09:23:55 -06001322
Omar Sandoval88459642016-09-17 08:38:44 -06001323 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001324}
Jens Axboe2c3ad662016-12-14 14:34:47 -07001325EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001326
Ming Leib3476892017-10-14 17:22:30 +08001327struct dispatch_rq_data {
1328 struct blk_mq_hw_ctx *hctx;
1329 struct request *rq;
1330};
1331
1332static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
1333 void *data)
1334{
1335 struct dispatch_rq_data *dispatch_data = data;
1336 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
1337 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001338 enum hctx_type type = hctx->type;
Ming Leib3476892017-10-14 17:22:30 +08001339
1340 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001341 if (!list_empty(&ctx->rq_lists[type])) {
1342 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
Ming Leib3476892017-10-14 17:22:30 +08001343 list_del_init(&dispatch_data->rq->queuelist);
Ming Leic16d6b52018-12-17 08:44:05 -07001344 if (list_empty(&ctx->rq_lists[type]))
Ming Leib3476892017-10-14 17:22:30 +08001345 sbitmap_clear_bit(sb, bitnr);
1346 }
1347 spin_unlock(&ctx->lock);
1348
1349 return !dispatch_data->rq;
1350}
1351
1352struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
1353 struct blk_mq_ctx *start)
1354{
Jens Axboef31967f2018-10-29 13:13:29 -06001355 unsigned off = start ? start->index_hw[hctx->type] : 0;
Ming Leib3476892017-10-14 17:22:30 +08001356 struct dispatch_rq_data data = {
1357 .hctx = hctx,
1358 .rq = NULL,
1359 };
1360
1361 __sbitmap_for_each_set(&hctx->ctx_map, off,
1362 dispatch_rq_from_ctx, &data);
1363
1364 return data.rq;
1365}
1366
Jens Axboea808a9d2021-10-13 08:28:14 -06001367static bool __blk_mq_alloc_driver_tag(struct request *rq)
Jens Axboe703fd1c2016-09-16 13:59:14 -06001368{
John Garryae0f1a72021-10-05 18:23:38 +08001369 struct sbitmap_queue *bt = &rq->mq_hctx->tags->bitmap_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001370 unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001371 int tag;
1372
Ming Lei568f2702020-07-06 22:41:11 +08001373 blk_mq_tag_busy(rq->mq_hctx);
1374
Ming Lei570e9b72020-06-30 22:03:55 +08001375 if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) {
John Garryae0f1a72021-10-05 18:23:38 +08001376 bt = &rq->mq_hctx->tags->breserved_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001377 tag_offset = 0;
Ming Lei28500852020-09-11 18:41:14 +08001378 } else {
1379 if (!hctx_may_queue(rq->mq_hctx, bt))
1380 return false;
Ming Lei570e9b72020-06-30 22:03:55 +08001381 }
1382
Ming Lei570e9b72020-06-30 22:03:55 +08001383 tag = __sbitmap_queue_get(bt);
1384 if (tag == BLK_MQ_NO_TAG)
1385 return false;
1386
1387 rq->tag = tag + tag_offset;
Ming Lei570e9b72020-06-30 22:03:55 +08001388 return true;
1389}
1390
Jens Axboea808a9d2021-10-13 08:28:14 -06001391bool __blk_mq_get_driver_tag(struct blk_mq_hw_ctx *hctx, struct request *rq)
Ming Lei570e9b72020-06-30 22:03:55 +08001392{
Jens Axboea808a9d2021-10-13 08:28:14 -06001393 if (rq->tag == BLK_MQ_NO_TAG && !__blk_mq_alloc_driver_tag(rq))
Ming Lei568f2702020-07-06 22:41:11 +08001394 return false;
1395
Ming Lei51db1c32020-08-19 23:20:19 +08001396 if ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) &&
Ming Lei568f2702020-07-06 22:41:11 +08001397 !(rq->rq_flags & RQF_MQ_INFLIGHT)) {
1398 rq->rq_flags |= RQF_MQ_INFLIGHT;
John Garrybccf5e22020-08-19 23:20:26 +08001399 __blk_mq_inc_active_requests(hctx);
Ming Lei568f2702020-07-06 22:41:11 +08001400 }
1401 hctx->tags->rqs[rq->tag] = rq;
1402 return true;
Ming Lei570e9b72020-06-30 22:03:55 +08001403}
1404
Jens Axboeeb619fd2017-11-09 08:32:43 -07001405static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1406 int flags, void *key)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001407{
1408 struct blk_mq_hw_ctx *hctx;
1409
1410 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1411
Ming Lei5815839b2018-06-25 19:31:47 +08001412 spin_lock(&hctx->dispatch_wait_lock);
Jens Axboee8618572019-03-25 12:34:10 -06001413 if (!list_empty(&wait->entry)) {
1414 struct sbitmap_queue *sbq;
1415
1416 list_del_init(&wait->entry);
John Garryae0f1a72021-10-05 18:23:38 +08001417 sbq = &hctx->tags->bitmap_tags;
Jens Axboee8618572019-03-25 12:34:10 -06001418 atomic_dec(&sbq->ws_active);
1419 }
Ming Lei5815839b2018-06-25 19:31:47 +08001420 spin_unlock(&hctx->dispatch_wait_lock);
1421
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001422 blk_mq_run_hw_queue(hctx, true);
1423 return 1;
1424}
1425
Jens Axboef906a6a2017-11-09 16:10:13 -07001426/*
1427 * Mark us waiting for a tag. For shared tags, this involves hooking us into
Bart Van Asscheee3e4de2018-01-09 10:09:15 -08001428 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1429 * restart. For both cases, take care to check the condition again after
Jens Axboef906a6a2017-11-09 16:10:13 -07001430 * marking us as waiting.
1431 */
Ming Lei2278d692018-06-25 19:31:46 +08001432static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
Jens Axboef906a6a2017-11-09 16:10:13 -07001433 struct request *rq)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001434{
John Garryae0f1a72021-10-05 18:23:38 +08001435 struct sbitmap_queue *sbq = &hctx->tags->bitmap_tags;
Ming Lei5815839b2018-06-25 19:31:47 +08001436 struct wait_queue_head *wq;
Jens Axboef906a6a2017-11-09 16:10:13 -07001437 wait_queue_entry_t *wait;
1438 bool ret;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001439
Ming Lei51db1c32020-08-19 23:20:19 +08001440 if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
Yufen Yu684b7322019-03-15 11:05:10 +08001441 blk_mq_sched_mark_restart_hctx(hctx);
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001442
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001443 /*
1444 * It's possible that a tag was freed in the window between the
1445 * allocation failure and adding the hardware queue to the wait
1446 * queue.
1447 *
1448 * Don't clear RESTART here, someone else could have set it.
1449 * At most this will cost an extra queue run.
1450 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001451 return blk_mq_get_driver_tag(rq);
Jens Axboeeb619fd2017-11-09 08:32:43 -07001452 }
1453
Ming Lei2278d692018-06-25 19:31:46 +08001454 wait = &hctx->dispatch_wait;
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001455 if (!list_empty_careful(&wait->entry))
1456 return false;
1457
Jens Axboee8618572019-03-25 12:34:10 -06001458 wq = &bt_wait_ptr(sbq, hctx)->wait;
Ming Lei5815839b2018-06-25 19:31:47 +08001459
1460 spin_lock_irq(&wq->lock);
1461 spin_lock(&hctx->dispatch_wait_lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001462 if (!list_empty(&wait->entry)) {
Ming Lei5815839b2018-06-25 19:31:47 +08001463 spin_unlock(&hctx->dispatch_wait_lock);
1464 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001465 return false;
1466 }
1467
Jens Axboee8618572019-03-25 12:34:10 -06001468 atomic_inc(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001469 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1470 __add_wait_queue(wq, wait);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001471
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001472 /*
Jens Axboeeb619fd2017-11-09 08:32:43 -07001473 * It's possible that a tag was freed in the window between the
1474 * allocation failure and adding the hardware queue to the wait
1475 * queue.
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001476 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001477 ret = blk_mq_get_driver_tag(rq);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001478 if (!ret) {
Ming Lei5815839b2018-06-25 19:31:47 +08001479 spin_unlock(&hctx->dispatch_wait_lock);
1480 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001481 return false;
Jens Axboef906a6a2017-11-09 16:10:13 -07001482 }
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001483
1484 /*
1485 * We got a tag, remove ourselves from the wait queue to ensure
1486 * someone else gets the wakeup.
1487 */
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001488 list_del_init(&wait->entry);
Jens Axboee8618572019-03-25 12:34:10 -06001489 atomic_dec(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001490 spin_unlock(&hctx->dispatch_wait_lock);
1491 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001492
1493 return true;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001494}
1495
Ming Lei6e7687172018-07-03 09:03:16 -06001496#define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1497#define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1498/*
1499 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1500 * - EWMA is one simple way to compute running average value
1501 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1502 * - take 4 as factor for avoiding to get too small(0) result, and this
1503 * factor doesn't matter because EWMA decreases exponentially
1504 */
1505static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1506{
1507 unsigned int ewma;
1508
Ming Lei6e7687172018-07-03 09:03:16 -06001509 ewma = hctx->dispatch_busy;
1510
1511 if (!ewma && !busy)
1512 return;
1513
1514 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1515 if (busy)
1516 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1517 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1518
1519 hctx->dispatch_busy = ewma;
1520}
1521
Ming Lei86ff7c22018-01-30 22:04:57 -05001522#define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1523
Johannes Thumshirnc92a4102020-03-25 00:24:44 +09001524static void blk_mq_handle_dev_resource(struct request *rq,
1525 struct list_head *list)
1526{
1527 struct request *next =
1528 list_first_entry_or_null(list, struct request, queuelist);
1529
1530 /*
1531 * If an I/O scheduler has been configured and we got a driver tag for
1532 * the next request already, free it.
1533 */
1534 if (next)
1535 blk_mq_put_driver_tag(next);
1536
1537 list_add(&rq->queuelist, list);
1538 __blk_mq_requeue_request(rq);
1539}
1540
Keith Busch0512a752020-05-12 17:55:47 +09001541static void blk_mq_handle_zone_resource(struct request *rq,
1542 struct list_head *zone_list)
1543{
1544 /*
1545 * If we end up here it is because we cannot dispatch a request to a
1546 * specific zone due to LLD level zone-write locking or other zone
1547 * related resource not being available. In this case, set the request
1548 * aside in zone_list for retrying it later.
1549 */
1550 list_add(&rq->queuelist, zone_list);
1551 __blk_mq_requeue_request(rq);
1552}
1553
Ming Lei75383522020-06-30 18:24:58 +08001554enum prep_dispatch {
1555 PREP_DISPATCH_OK,
1556 PREP_DISPATCH_NO_TAG,
1557 PREP_DISPATCH_NO_BUDGET,
1558};
1559
1560static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq,
1561 bool need_budget)
1562{
1563 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Ming Lei2a5a24a2021-01-22 10:33:12 +08001564 int budget_token = -1;
Ming Lei75383522020-06-30 18:24:58 +08001565
Ming Lei2a5a24a2021-01-22 10:33:12 +08001566 if (need_budget) {
1567 budget_token = blk_mq_get_dispatch_budget(rq->q);
1568 if (budget_token < 0) {
1569 blk_mq_put_driver_tag(rq);
1570 return PREP_DISPATCH_NO_BUDGET;
1571 }
1572 blk_mq_set_rq_budget_token(rq, budget_token);
Ming Lei75383522020-06-30 18:24:58 +08001573 }
1574
1575 if (!blk_mq_get_driver_tag(rq)) {
1576 /*
1577 * The initial allocation attempt failed, so we need to
1578 * rerun the hardware queue when a tag is freed. The
1579 * waitqueue takes care of that. If the queue is run
1580 * before we add this entry back on the dispatch list,
1581 * we'll re-run it below.
1582 */
1583 if (!blk_mq_mark_tag_wait(hctx, rq)) {
Ming Lei1fd40b52020-06-30 18:25:00 +08001584 /*
1585 * All budgets not got from this function will be put
1586 * together during handling partial dispatch
1587 */
1588 if (need_budget)
Ming Lei2a5a24a2021-01-22 10:33:12 +08001589 blk_mq_put_dispatch_budget(rq->q, budget_token);
Ming Lei75383522020-06-30 18:24:58 +08001590 return PREP_DISPATCH_NO_TAG;
1591 }
1592 }
1593
1594 return PREP_DISPATCH_OK;
1595}
1596
Ming Lei1fd40b52020-06-30 18:25:00 +08001597/* release all allocated budgets before calling to blk_mq_dispatch_rq_list */
1598static void blk_mq_release_budgets(struct request_queue *q,
Ming Lei2a5a24a2021-01-22 10:33:12 +08001599 struct list_head *list)
Ming Lei1fd40b52020-06-30 18:25:00 +08001600{
Ming Lei2a5a24a2021-01-22 10:33:12 +08001601 struct request *rq;
Ming Lei1fd40b52020-06-30 18:25:00 +08001602
Ming Lei2a5a24a2021-01-22 10:33:12 +08001603 list_for_each_entry(rq, list, queuelist) {
1604 int budget_token = blk_mq_get_rq_budget_token(rq);
1605
1606 if (budget_token >= 0)
1607 blk_mq_put_dispatch_budget(q, budget_token);
1608 }
Ming Lei1fd40b52020-06-30 18:25:00 +08001609}
1610
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001611/*
1612 * Returns true if we did some work AND can potentially do more.
1613 */
Ming Lei445874e2020-06-30 18:24:57 +08001614bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list,
Ming Lei1fd40b52020-06-30 18:25:00 +08001615 unsigned int nr_budgets)
Jens Axboef04c3df2016-12-07 08:41:17 -07001616{
Ming Lei75383522020-06-30 18:24:58 +08001617 enum prep_dispatch prep;
Ming Lei445874e2020-06-30 18:24:57 +08001618 struct request_queue *q = hctx->queue;
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001619 struct request *rq, *nxt;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001620 int errors, queued;
Ming Lei86ff7c22018-01-30 22:04:57 -05001621 blk_status_t ret = BLK_STS_OK;
Keith Busch0512a752020-05-12 17:55:47 +09001622 LIST_HEAD(zone_list);
Naohiro Aota9586e672021-10-27 01:51:27 +09001623 bool needs_resource = false;
Jens Axboef04c3df2016-12-07 08:41:17 -07001624
Omar Sandoval81380ca2017-04-07 08:56:26 -06001625 if (list_empty(list))
1626 return false;
1627
Jens Axboef04c3df2016-12-07 08:41:17 -07001628 /*
Jens Axboef04c3df2016-12-07 08:41:17 -07001629 * Now process all the entries, sending them to the driver.
1630 */
Jens Axboe93efe982017-03-24 12:04:19 -06001631 errors = queued = 0;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001632 do {
Jens Axboef04c3df2016-12-07 08:41:17 -07001633 struct blk_mq_queue_data bd;
1634
1635 rq = list_first_entry(list, struct request, queuelist);
Ming Lei0bca7992018-04-05 00:35:21 +08001636
Ming Lei445874e2020-06-30 18:24:57 +08001637 WARN_ON_ONCE(hctx != rq->mq_hctx);
Ming Lei1fd40b52020-06-30 18:25:00 +08001638 prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets);
Ming Lei75383522020-06-30 18:24:58 +08001639 if (prep != PREP_DISPATCH_OK)
Ming Lei0bca7992018-04-05 00:35:21 +08001640 break;
Ming Leide148292017-10-14 17:22:29 +08001641
Jens Axboef04c3df2016-12-07 08:41:17 -07001642 list_del_init(&rq->queuelist);
1643
1644 bd.rq = rq;
Jens Axboe113285b2017-03-02 13:26:04 -07001645
1646 /*
1647 * Flag last if we have no more requests, or if we have more
1648 * but can't assign a driver tag to it.
1649 */
1650 if (list_empty(list))
1651 bd.last = true;
1652 else {
Jens Axboe113285b2017-03-02 13:26:04 -07001653 nxt = list_first_entry(list, struct request, queuelist);
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001654 bd.last = !blk_mq_get_driver_tag(nxt);
Jens Axboe113285b2017-03-02 13:26:04 -07001655 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001656
Ming Lei1fd40b52020-06-30 18:25:00 +08001657 /*
1658 * once the request is queued to lld, no need to cover the
1659 * budget any more
1660 */
1661 if (nr_budgets)
1662 nr_budgets--;
Jens Axboef04c3df2016-12-07 08:41:17 -07001663 ret = q->mq_ops->queue_rq(hctx, &bd);
Ming Lei7bf13722020-07-01 21:58:57 +08001664 switch (ret) {
1665 case BLK_STS_OK:
1666 queued++;
Jens Axboef04c3df2016-12-07 08:41:17 -07001667 break;
Ming Lei7bf13722020-07-01 21:58:57 +08001668 case BLK_STS_RESOURCE:
Naohiro Aota9586e672021-10-27 01:51:27 +09001669 needs_resource = true;
1670 fallthrough;
Ming Lei7bf13722020-07-01 21:58:57 +08001671 case BLK_STS_DEV_RESOURCE:
1672 blk_mq_handle_dev_resource(rq, list);
1673 goto out;
1674 case BLK_STS_ZONE_RESOURCE:
Keith Busch0512a752020-05-12 17:55:47 +09001675 /*
1676 * Move the request to zone_list and keep going through
1677 * the dispatch list to find more requests the drive can
1678 * accept.
1679 */
1680 blk_mq_handle_zone_resource(rq, &zone_list);
Naohiro Aota9586e672021-10-27 01:51:27 +09001681 needs_resource = true;
Ming Lei7bf13722020-07-01 21:58:57 +08001682 break;
1683 default:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001684 errors++;
Hannes Reineckee21ee5a2020-09-30 10:02:53 +02001685 blk_mq_end_request(rq, ret);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001686 }
Omar Sandoval81380ca2017-04-07 08:56:26 -06001687 } while (!list_empty(list));
Ming Lei7bf13722020-07-01 21:58:57 +08001688out:
Keith Busch0512a752020-05-12 17:55:47 +09001689 if (!list_empty(&zone_list))
1690 list_splice_tail_init(&zone_list, list);
1691
yangerkun632bfb62020-09-05 19:25:56 +08001692 /* If we didn't flush the entire list, we could have told the driver
1693 * there was more coming, but that turned out to be a lie.
1694 */
1695 if ((!list_empty(list) || errors) && q->mq_ops->commit_rqs && queued)
1696 q->mq_ops->commit_rqs(hctx);
Jens Axboef04c3df2016-12-07 08:41:17 -07001697 /*
1698 * Any items that need requeuing? Stuff them into hctx->dispatch,
1699 * that is where we will continue on next queue run.
1700 */
1701 if (!list_empty(list)) {
Ming Lei86ff7c22018-01-30 22:04:57 -05001702 bool needs_restart;
Ming Lei75383522020-06-30 18:24:58 +08001703 /* For non-shared tags, the RESTART check will suffice */
1704 bool no_tag = prep == PREP_DISPATCH_NO_TAG &&
Ming Lei51db1c32020-08-19 23:20:19 +08001705 (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED);
Ming Lei86ff7c22018-01-30 22:04:57 -05001706
Ming Lei2a5a24a2021-01-22 10:33:12 +08001707 if (nr_budgets)
1708 blk_mq_release_budgets(q, list);
Jens Axboef04c3df2016-12-07 08:41:17 -07001709
1710 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001711 list_splice_tail_init(list, &hctx->dispatch);
Jens Axboef04c3df2016-12-07 08:41:17 -07001712 spin_unlock(&hctx->lock);
1713
1714 /*
Ming Leid7d85352020-08-17 18:01:15 +08001715 * Order adding requests to hctx->dispatch and checking
1716 * SCHED_RESTART flag. The pair of this smp_mb() is the one
1717 * in blk_mq_sched_restart(). Avoid restart code path to
1718 * miss the new added requests to hctx->dispatch, meantime
1719 * SCHED_RESTART is observed here.
1720 */
1721 smp_mb();
1722
1723 /*
Bart Van Assche710c7852017-04-07 11:16:51 -07001724 * If SCHED_RESTART was set by the caller of this function and
1725 * it is no longer set that means that it was cleared by another
1726 * thread and hence that a queue rerun is needed.
Jens Axboef04c3df2016-12-07 08:41:17 -07001727 *
Jens Axboeeb619fd2017-11-09 08:32:43 -07001728 * If 'no_tag' is set, that means that we failed getting
1729 * a driver tag with an I/O scheduler attached. If our dispatch
1730 * waitqueue is no longer active, ensure that we run the queue
1731 * AFTER adding our entries back to the list.
Jens Axboebd166ef2017-01-17 06:03:22 -07001732 *
Bart Van Assche710c7852017-04-07 11:16:51 -07001733 * If no I/O scheduler has been configured it is possible that
1734 * the hardware queue got stopped and restarted before requests
1735 * were pushed back onto the dispatch list. Rerun the queue to
1736 * avoid starvation. Notes:
1737 * - blk_mq_run_hw_queue() checks whether or not a queue has
1738 * been stopped before rerunning a queue.
1739 * - Some but not all block drivers stop a queue before
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001740 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
Bart Van Assche710c7852017-04-07 11:16:51 -07001741 * and dm-rq.
Ming Lei86ff7c22018-01-30 22:04:57 -05001742 *
1743 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1744 * bit is set, run queue after a delay to avoid IO stalls
Douglas Andersonab3cee32020-04-20 09:24:51 -07001745 * that could otherwise occur if the queue is idle. We'll do
Naohiro Aota9586e672021-10-27 01:51:27 +09001746 * similar if we couldn't get budget or couldn't lock a zone
1747 * and SCHED_RESTART is set.
Jens Axboebd166ef2017-01-17 06:03:22 -07001748 */
Ming Lei86ff7c22018-01-30 22:04:57 -05001749 needs_restart = blk_mq_sched_needs_restart(hctx);
Naohiro Aota9586e672021-10-27 01:51:27 +09001750 if (prep == PREP_DISPATCH_NO_BUDGET)
1751 needs_resource = true;
Ming Lei86ff7c22018-01-30 22:04:57 -05001752 if (!needs_restart ||
Jens Axboeeb619fd2017-11-09 08:32:43 -07001753 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
Jens Axboebd166ef2017-01-17 06:03:22 -07001754 blk_mq_run_hw_queue(hctx, true);
Naohiro Aota9586e672021-10-27 01:51:27 +09001755 else if (needs_restart && needs_resource)
Ming Lei86ff7c22018-01-30 22:04:57 -05001756 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001757
Ming Lei6e7687172018-07-03 09:03:16 -06001758 blk_mq_update_dispatch_busy(hctx, true);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001759 return false;
Ming Lei6e7687172018-07-03 09:03:16 -06001760 } else
1761 blk_mq_update_dispatch_busy(hctx, false);
Jens Axboef04c3df2016-12-07 08:41:17 -07001762
Jens Axboe93efe982017-03-24 12:04:19 -06001763 return (queued + errors) != 0;
Jens Axboef04c3df2016-12-07 08:41:17 -07001764}
1765
André Almeida105663f2020-01-06 15:08:18 -03001766/**
1767 * __blk_mq_run_hw_queue - Run a hardware queue.
1768 * @hctx: Pointer to the hardware queue to run.
1769 *
1770 * Send pending requests to the hardware.
1771 */
Bart Van Assche6a83e742016-11-02 10:09:51 -06001772static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1773{
1774 int srcu_idx;
1775
Jens Axboeb7a71e62017-08-01 09:28:24 -06001776 /*
Jens Axboeb7a71e62017-08-01 09:28:24 -06001777 * We can't run the queue inline with ints disabled. Ensure that
1778 * we catch bad users of this early.
1779 */
1780 WARN_ON_ONCE(in_interrupt());
1781
Jens Axboe04ced152018-01-09 08:29:46 -08001782 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
Jens Axboebf4907c2017-03-30 12:30:39 -06001783
Jens Axboe04ced152018-01-09 08:29:46 -08001784 hctx_lock(hctx, &srcu_idx);
1785 blk_mq_sched_dispatch_requests(hctx);
1786 hctx_unlock(hctx, srcu_idx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001787}
1788
Ming Leif82ddf12018-04-08 17:48:10 +08001789static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1790{
1791 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1792
1793 if (cpu >= nr_cpu_ids)
1794 cpu = cpumask_first(hctx->cpumask);
1795 return cpu;
1796}
1797
Jens Axboe506e9312014-05-07 10:26:44 -06001798/*
1799 * It'd be great if the workqueue API had a way to pass
1800 * in a mask and had some smarts for more clever placement.
1801 * For now we just round-robin here, switching for every
1802 * BLK_MQ_CPU_WORK_BATCH queued items.
1803 */
1804static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1805{
Ming Lei7bed4592018-01-18 00:41:51 +08001806 bool tried = false;
Ming Lei476f8c92018-04-08 17:48:09 +08001807 int next_cpu = hctx->next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001808
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001809 if (hctx->queue->nr_hw_queues == 1)
1810 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -06001811
1812 if (--hctx->next_cpu_batch <= 0) {
Ming Lei7bed4592018-01-18 00:41:51 +08001813select_cpu:
Ming Lei476f8c92018-04-08 17:48:09 +08001814 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08001815 cpu_online_mask);
Jens Axboe506e9312014-05-07 10:26:44 -06001816 if (next_cpu >= nr_cpu_ids)
Ming Leif82ddf12018-04-08 17:48:10 +08001817 next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06001818 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1819 }
1820
Ming Lei7bed4592018-01-18 00:41:51 +08001821 /*
1822 * Do unbound schedule if we can't find a online CPU for this hctx,
1823 * and it should only happen in the path of handling CPU DEAD.
1824 */
Ming Lei476f8c92018-04-08 17:48:09 +08001825 if (!cpu_online(next_cpu)) {
Ming Lei7bed4592018-01-18 00:41:51 +08001826 if (!tried) {
1827 tried = true;
1828 goto select_cpu;
1829 }
1830
1831 /*
1832 * Make sure to re-select CPU next time once after CPUs
1833 * in hctx->cpumask become online again.
1834 */
Ming Lei476f8c92018-04-08 17:48:09 +08001835 hctx->next_cpu = next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001836 hctx->next_cpu_batch = 1;
1837 return WORK_CPU_UNBOUND;
1838 }
Ming Lei476f8c92018-04-08 17:48:09 +08001839
1840 hctx->next_cpu = next_cpu;
1841 return next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -06001842}
1843
André Almeida105663f2020-01-06 15:08:18 -03001844/**
1845 * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue.
1846 * @hctx: Pointer to the hardware queue to run.
1847 * @async: If we want to run the queue asynchronously.
Minwoo Imfa94ba82020-12-05 00:20:55 +09001848 * @msecs: Milliseconds of delay to wait before running the queue.
André Almeida105663f2020-01-06 15:08:18 -03001849 *
1850 * If !@async, try to run the queue now. Else, run the queue asynchronously and
1851 * with a delay of @msecs.
1852 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001853static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1854 unsigned long msecs)
Jens Axboe320ae512013-10-24 09:20:05 +01001855{
Bart Van Assche5435c022017-06-20 11:15:49 -07001856 if (unlikely(blk_mq_hctx_stopped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +01001857 return;
1858
Jens Axboe1b792f22016-09-21 10:12:13 -06001859 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001860 int cpu = get_cpu();
1861 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +01001862 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001863 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +01001864 return;
1865 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001866
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001867 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -06001868 }
Paolo Bonzini398205b2014-11-07 23:03:59 +01001869
Bart Van Asscheae943d22018-01-19 08:58:55 -08001870 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1871 msecs_to_jiffies(msecs));
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001872}
1873
André Almeida105663f2020-01-06 15:08:18 -03001874/**
1875 * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
1876 * @hctx: Pointer to the hardware queue to run.
Minwoo Imfa94ba82020-12-05 00:20:55 +09001877 * @msecs: Milliseconds of delay to wait before running the queue.
André Almeida105663f2020-01-06 15:08:18 -03001878 *
1879 * Run a hardware queue asynchronously with a delay of @msecs.
1880 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001881void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1882{
1883 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1884}
1885EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1886
André Almeida105663f2020-01-06 15:08:18 -03001887/**
1888 * blk_mq_run_hw_queue - Start to run a hardware queue.
1889 * @hctx: Pointer to the hardware queue to run.
1890 * @async: If we want to run the queue asynchronously.
1891 *
1892 * Check if the request queue is not in a quiesced state and if there are
1893 * pending requests to be sent. If this is true, run the queue to send requests
1894 * to hardware.
1895 */
John Garry626fb732019-10-30 00:59:30 +08001896void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001897{
Ming Lei24f5a902018-01-06 16:27:38 +08001898 int srcu_idx;
1899 bool need_run;
1900
1901 /*
1902 * When queue is quiesced, we may be switching io scheduler, or
1903 * updating nr_hw_queues, or other things, and we can't run queue
1904 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
1905 *
1906 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
1907 * quiesced.
1908 */
Jens Axboe04ced152018-01-09 08:29:46 -08001909 hctx_lock(hctx, &srcu_idx);
1910 need_run = !blk_queue_quiesced(hctx->queue) &&
1911 blk_mq_hctx_has_pending(hctx);
1912 hctx_unlock(hctx, srcu_idx);
Ming Lei24f5a902018-01-06 16:27:38 +08001913
John Garry626fb732019-10-30 00:59:30 +08001914 if (need_run)
Jens Axboe79f720a2017-11-10 09:13:21 -07001915 __blk_mq_delay_run_hw_queue(hctx, async, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01001916}
Omar Sandoval5b727272017-04-14 01:00:00 -07001917EXPORT_SYMBOL(blk_mq_run_hw_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01001918
Jan Karab6e68ee2021-01-11 17:47:17 +01001919/*
1920 * Is the request queue handled by an IO scheduler that does not respect
1921 * hardware queues when dispatching?
1922 */
1923static bool blk_mq_has_sqsched(struct request_queue *q)
1924{
1925 struct elevator_queue *e = q->elevator;
1926
1927 if (e && e->type->ops.dispatch_request &&
1928 !(e->type->elevator_features & ELEVATOR_F_MQ_AWARE))
1929 return true;
1930 return false;
1931}
1932
1933/*
1934 * Return prefered queue to dispatch from (if any) for non-mq aware IO
1935 * scheduler.
1936 */
1937static struct blk_mq_hw_ctx *blk_mq_get_sq_hctx(struct request_queue *q)
1938{
1939 struct blk_mq_hw_ctx *hctx;
1940
1941 /*
1942 * If the IO scheduler does not respect hardware queues when
1943 * dispatching, we just don't bother with multiple HW queues and
1944 * dispatch from hctx for the current CPU since running multiple queues
1945 * just causes lock contention inside the scheduler and pointless cache
1946 * bouncing.
1947 */
1948 hctx = blk_mq_map_queue_type(q, HCTX_TYPE_DEFAULT,
1949 raw_smp_processor_id());
1950 if (!blk_mq_hctx_stopped(hctx))
1951 return hctx;
1952 return NULL;
1953}
1954
André Almeida105663f2020-01-06 15:08:18 -03001955/**
Mauro Carvalho Chehab24f7bb82020-10-23 18:32:54 +02001956 * blk_mq_run_hw_queues - Run all hardware queues in a request queue.
André Almeida105663f2020-01-06 15:08:18 -03001957 * @q: Pointer to the request queue to run.
1958 * @async: If we want to run the queue asynchronously.
1959 */
Mike Snitzerb94ec292015-03-11 23:56:38 -04001960void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001961{
Jan Karab6e68ee2021-01-11 17:47:17 +01001962 struct blk_mq_hw_ctx *hctx, *sq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001963 int i;
1964
Jan Karab6e68ee2021-01-11 17:47:17 +01001965 sq_hctx = NULL;
1966 if (blk_mq_has_sqsched(q))
1967 sq_hctx = blk_mq_get_sq_hctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001968 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe79f720a2017-11-10 09:13:21 -07001969 if (blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +01001970 continue;
Jan Karab6e68ee2021-01-11 17:47:17 +01001971 /*
1972 * Dispatch from this hctx either if there's no hctx preferred
1973 * by IO scheduler or if it has requests that bypass the
1974 * scheduler.
1975 */
1976 if (!sq_hctx || sq_hctx == hctx ||
1977 !list_empty_careful(&hctx->dispatch))
1978 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001979 }
1980}
Mike Snitzerb94ec292015-03-11 23:56:38 -04001981EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001982
Bart Van Asschefd001442016-10-28 17:19:37 -07001983/**
Douglas Andersonb9151e72020-04-20 09:24:52 -07001984 * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously.
1985 * @q: Pointer to the request queue to run.
Minwoo Imfa94ba82020-12-05 00:20:55 +09001986 * @msecs: Milliseconds of delay to wait before running the queues.
Douglas Andersonb9151e72020-04-20 09:24:52 -07001987 */
1988void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs)
1989{
Jan Karab6e68ee2021-01-11 17:47:17 +01001990 struct blk_mq_hw_ctx *hctx, *sq_hctx;
Douglas Andersonb9151e72020-04-20 09:24:52 -07001991 int i;
1992
Jan Karab6e68ee2021-01-11 17:47:17 +01001993 sq_hctx = NULL;
1994 if (blk_mq_has_sqsched(q))
1995 sq_hctx = blk_mq_get_sq_hctx(q);
Douglas Andersonb9151e72020-04-20 09:24:52 -07001996 queue_for_each_hw_ctx(q, hctx, i) {
1997 if (blk_mq_hctx_stopped(hctx))
1998 continue;
Jan Karab6e68ee2021-01-11 17:47:17 +01001999 /*
2000 * Dispatch from this hctx either if there's no hctx preferred
2001 * by IO scheduler or if it has requests that bypass the
2002 * scheduler.
2003 */
2004 if (!sq_hctx || sq_hctx == hctx ||
2005 !list_empty_careful(&hctx->dispatch))
2006 blk_mq_delay_run_hw_queue(hctx, msecs);
Douglas Andersonb9151e72020-04-20 09:24:52 -07002007 }
2008}
2009EXPORT_SYMBOL(blk_mq_delay_run_hw_queues);
2010
2011/**
Bart Van Asschefd001442016-10-28 17:19:37 -07002012 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
2013 * @q: request queue.
2014 *
2015 * The caller is responsible for serializing this function against
2016 * blk_mq_{start,stop}_hw_queue().
2017 */
2018bool blk_mq_queue_stopped(struct request_queue *q)
2019{
2020 struct blk_mq_hw_ctx *hctx;
2021 int i;
2022
2023 queue_for_each_hw_ctx(q, hctx, i)
2024 if (blk_mq_hctx_stopped(hctx))
2025 return true;
2026
2027 return false;
2028}
2029EXPORT_SYMBOL(blk_mq_queue_stopped);
2030
Ming Lei39a70c72017-06-06 23:22:09 +08002031/*
2032 * This function is often used for pausing .queue_rq() by driver when
2033 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07002034 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08002035 *
2036 * We do not guarantee that dispatch can be drained or blocked
2037 * after blk_mq_stop_hw_queue() returns. Please use
2038 * blk_mq_quiesce_queue() for that requirement.
2039 */
Jens Axboe320ae512013-10-24 09:20:05 +01002040void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
2041{
Ming Lei641a9ed2017-06-06 23:22:10 +08002042 cancel_delayed_work(&hctx->run_work);
2043
2044 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboe320ae512013-10-24 09:20:05 +01002045}
2046EXPORT_SYMBOL(blk_mq_stop_hw_queue);
2047
Ming Lei39a70c72017-06-06 23:22:09 +08002048/*
2049 * This function is often used for pausing .queue_rq() by driver when
2050 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07002051 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08002052 *
2053 * We do not guarantee that dispatch can be drained or blocked
2054 * after blk_mq_stop_hw_queues() returns. Please use
2055 * blk_mq_quiesce_queue() for that requirement.
2056 */
Jens Axboe2719aa22017-05-03 11:08:14 -06002057void blk_mq_stop_hw_queues(struct request_queue *q)
2058{
Ming Lei641a9ed2017-06-06 23:22:10 +08002059 struct blk_mq_hw_ctx *hctx;
2060 int i;
2061
2062 queue_for_each_hw_ctx(q, hctx, i)
2063 blk_mq_stop_hw_queue(hctx);
Christoph Hellwig280d45f2013-10-25 14:45:58 +01002064}
2065EXPORT_SYMBOL(blk_mq_stop_hw_queues);
2066
Jens Axboe320ae512013-10-24 09:20:05 +01002067void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
2068{
2069 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -06002070
Jens Axboe0ffbce82014-06-25 08:22:34 -06002071 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +01002072}
2073EXPORT_SYMBOL(blk_mq_start_hw_queue);
2074
Christoph Hellwig2f268552014-04-16 09:44:56 +02002075void blk_mq_start_hw_queues(struct request_queue *q)
2076{
2077 struct blk_mq_hw_ctx *hctx;
2078 int i;
2079
2080 queue_for_each_hw_ctx(q, hctx, i)
2081 blk_mq_start_hw_queue(hctx);
2082}
2083EXPORT_SYMBOL(blk_mq_start_hw_queues);
2084
Jens Axboeae911c52016-12-08 13:19:30 -07002085void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
2086{
2087 if (!blk_mq_hctx_stopped(hctx))
2088 return;
2089
2090 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
2091 blk_mq_run_hw_queue(hctx, async);
2092}
2093EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
2094
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02002095void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01002096{
2097 struct blk_mq_hw_ctx *hctx;
2098 int i;
2099
Jens Axboeae911c52016-12-08 13:19:30 -07002100 queue_for_each_hw_ctx(q, hctx, i)
2101 blk_mq_start_stopped_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01002102}
2103EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
2104
Christoph Hellwig70f4db62014-04-16 10:48:08 -06002105static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01002106{
2107 struct blk_mq_hw_ctx *hctx;
2108
Jens Axboe9f993732017-04-10 09:54:54 -06002109 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboe21c6e932017-04-10 09:54:56 -06002110
2111 /*
Ming Lei15fe8a902018-04-08 17:48:11 +08002112 * If we are stopped, don't run the queue.
Jens Axboe21c6e932017-04-10 09:54:56 -06002113 */
Yufen Yu08410312020-10-08 23:26:30 -04002114 if (blk_mq_hctx_stopped(hctx))
Jianchao Wang0196d6b2018-06-04 17:03:55 +08002115 return;
Jens Axboee4043dc2014-04-09 10:18:23 -06002116
Jens Axboe320ae512013-10-24 09:20:05 +01002117 __blk_mq_run_hw_queue(hctx);
2118}
2119
Ming Leicfd0c552015-10-20 23:13:57 +08002120static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08002121 struct request *rq,
2122 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01002123{
Jens Axboee57690f2016-08-24 15:34:35 -06002124 struct blk_mq_ctx *ctx = rq->mq_ctx;
Ming Leic16d6b52018-12-17 08:44:05 -07002125 enum hctx_type type = hctx->type;
Jens Axboee57690f2016-08-24 15:34:35 -06002126
Bart Van Assche7b607812017-06-20 11:15:47 -07002127 lockdep_assert_held(&ctx->lock);
2128
Christoph Hellwiga54895f2020-12-03 17:21:39 +01002129 trace_block_rq_insert(rq);
Jens Axboe01b983c2013-11-19 18:59:10 -07002130
Christoph Hellwig72a0a362014-02-07 10:22:36 -08002131 if (at_head)
Ming Leic16d6b52018-12-17 08:44:05 -07002132 list_add(&rq->queuelist, &ctx->rq_lists[type]);
Christoph Hellwig72a0a362014-02-07 10:22:36 -08002133 else
Ming Leic16d6b52018-12-17 08:44:05 -07002134 list_add_tail(&rq->queuelist, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08002135}
Jens Axboe4bb659b2014-05-09 09:36:49 -06002136
Jens Axboe2c3ad662016-12-14 14:34:47 -07002137void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
2138 bool at_head)
Ming Leicfd0c552015-10-20 23:13:57 +08002139{
2140 struct blk_mq_ctx *ctx = rq->mq_ctx;
2141
Bart Van Assche7b607812017-06-20 11:15:47 -07002142 lockdep_assert_held(&ctx->lock);
2143
Jens Axboee57690f2016-08-24 15:34:35 -06002144 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01002145 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01002146}
2147
André Almeida105663f2020-01-06 15:08:18 -03002148/**
2149 * blk_mq_request_bypass_insert - Insert a request at dispatch list.
2150 * @rq: Pointer to request to be inserted.
Randy Dunlap26bfeb22020-08-16 16:39:34 -07002151 * @at_head: true if the request should be inserted at the head of the list.
André Almeida105663f2020-01-06 15:08:18 -03002152 * @run_queue: If we should run the hardware queue after inserting the request.
2153 *
Jens Axboe157f3772017-09-11 16:43:57 -06002154 * Should only be used carefully, when the caller knows we want to
2155 * bypass a potential IO scheduler on the target device.
2156 */
Ming Lei01e99ae2020-02-25 09:04:32 +08002157void blk_mq_request_bypass_insert(struct request *rq, bool at_head,
2158 bool run_queue)
Jens Axboe157f3772017-09-11 16:43:57 -06002159{
Jens Axboeea4f9952018-10-29 15:06:13 -06002160 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe157f3772017-09-11 16:43:57 -06002161
2162 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08002163 if (at_head)
2164 list_add(&rq->queuelist, &hctx->dispatch);
2165 else
2166 list_add_tail(&rq->queuelist, &hctx->dispatch);
Jens Axboe157f3772017-09-11 16:43:57 -06002167 spin_unlock(&hctx->lock);
2168
Ming Leib0850292017-11-02 23:24:34 +08002169 if (run_queue)
2170 blk_mq_run_hw_queue(hctx, false);
Jens Axboe157f3772017-09-11 16:43:57 -06002171}
2172
Jens Axboebd166ef2017-01-17 06:03:22 -07002173void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
2174 struct list_head *list)
Jens Axboe320ae512013-10-24 09:20:05 +01002175
2176{
Ming Lei3f0cedc2018-07-02 17:35:58 +08002177 struct request *rq;
Ming Leic16d6b52018-12-17 08:44:05 -07002178 enum hctx_type type = hctx->type;
Ming Lei3f0cedc2018-07-02 17:35:58 +08002179
Jens Axboe320ae512013-10-24 09:20:05 +01002180 /*
2181 * preemption doesn't flush plug list, so it's possible ctx->cpu is
2182 * offline now
2183 */
Ming Lei3f0cedc2018-07-02 17:35:58 +08002184 list_for_each_entry(rq, list, queuelist) {
Jens Axboee57690f2016-08-24 15:34:35 -06002185 BUG_ON(rq->mq_ctx != ctx);
Christoph Hellwiga54895f2020-12-03 17:21:39 +01002186 trace_block_rq_insert(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01002187 }
Ming Lei3f0cedc2018-07-02 17:35:58 +08002188
2189 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002190 list_splice_tail_init(list, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08002191 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01002192 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01002193}
2194
Jens Axboedc5fc3612021-10-19 06:02:30 -06002195static void blk_mq_commit_rqs(struct blk_mq_hw_ctx *hctx, int *queued,
2196 bool from_schedule)
Jens Axboe320ae512013-10-24 09:20:05 +01002197{
Jens Axboedc5fc3612021-10-19 06:02:30 -06002198 if (hctx->queue->mq_ops->commit_rqs) {
2199 trace_block_unplug(hctx->queue, *queued, !from_schedule);
2200 hctx->queue->mq_ops->commit_rqs(hctx);
2201 }
2202 *queued = 0;
2203}
Jens Axboe320ae512013-10-24 09:20:05 +01002204
Jens Axboedc5fc3612021-10-19 06:02:30 -06002205static void blk_mq_plug_issue_direct(struct blk_plug *plug, bool from_schedule)
2206{
2207 struct blk_mq_hw_ctx *hctx = NULL;
2208 struct request *rq;
2209 int queued = 0;
2210 int errors = 0;
Jens Axboe3110fc72018-10-30 12:24:04 -06002211
Jens Axboedc5fc3612021-10-19 06:02:30 -06002212 while ((rq = rq_list_pop(&plug->mq_list))) {
2213 bool last = rq_list_empty(plug->mq_list);
2214 blk_status_t ret;
2215
2216 if (hctx != rq->mq_hctx) {
2217 if (hctx)
2218 blk_mq_commit_rqs(hctx, &queued, from_schedule);
2219 hctx = rq->mq_hctx;
2220 }
2221
2222 ret = blk_mq_request_issue_directly(rq, last);
2223 switch (ret) {
2224 case BLK_STS_OK:
2225 queued++;
2226 break;
2227 case BLK_STS_RESOURCE:
2228 case BLK_STS_DEV_RESOURCE:
2229 blk_mq_request_bypass_insert(rq, false, last);
2230 blk_mq_commit_rqs(hctx, &queued, from_schedule);
2231 return;
2232 default:
2233 blk_mq_end_request(rq, ret);
2234 errors++;
2235 break;
2236 }
2237 }
2238
2239 /*
2240 * If we didn't flush the entire list, we could have told the driver
2241 * there was more coming, but that turned out to be a lie.
2242 */
2243 if (errors)
2244 blk_mq_commit_rqs(hctx, &queued, from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01002245}
2246
2247void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
2248{
Jens Axboebc490f82021-10-18 10:12:12 -06002249 struct blk_mq_hw_ctx *this_hctx;
2250 struct blk_mq_ctx *this_ctx;
2251 unsigned int depth;
Jens Axboe320ae512013-10-24 09:20:05 +01002252 LIST_HEAD(list);
Jens Axboe320ae512013-10-24 09:20:05 +01002253
Jens Axboebc490f82021-10-18 10:12:12 -06002254 if (rq_list_empty(plug->mq_list))
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03002255 return;
Dongli Zhangbcc816d2019-04-04 10:57:44 +08002256 plug->rq_count = 0;
2257
Ming Leiff1552232021-10-26 16:22:57 +08002258 if (!plug->multiple_queues && !plug->has_elevator && !from_schedule) {
Jens Axboeb2280902021-11-01 13:40:12 -06002259 blk_mq_plug_issue_direct(plug, false);
Jens Axboedc5fc3612021-10-19 06:02:30 -06002260 if (rq_list_empty(plug->mq_list))
2261 return;
2262 }
Jens Axboe320ae512013-10-24 09:20:05 +01002263
Jens Axboebc490f82021-10-18 10:12:12 -06002264 this_hctx = NULL;
2265 this_ctx = NULL;
2266 depth = 0;
Jens Axboe320ae512013-10-24 09:20:05 +01002267 do {
Jens Axboebc490f82021-10-18 10:12:12 -06002268 struct request *rq;
Jens Axboe320ae512013-10-24 09:20:05 +01002269
Jens Axboebc490f82021-10-18 10:12:12 -06002270 rq = rq_list_pop(&plug->mq_list);
2271
2272 if (!this_hctx) {
2273 this_hctx = rq->mq_hctx;
2274 this_ctx = rq->mq_ctx;
2275 } else if (this_hctx != rq->mq_hctx || this_ctx != rq->mq_ctx) {
2276 trace_block_unplug(this_hctx->queue, depth,
2277 !from_schedule);
2278 blk_mq_sched_insert_requests(this_hctx, this_ctx,
2279 &list, from_schedule);
2280 depth = 0;
2281 this_hctx = rq->mq_hctx;
2282 this_ctx = rq->mq_ctx;
2283
Jens Axboe320ae512013-10-24 09:20:05 +01002284 }
2285
Jens Axboebc490f82021-10-18 10:12:12 -06002286 list_add(&rq->queuelist, &list);
2287 depth++;
2288 } while (!rq_list_empty(plug->mq_list));
2289
2290 if (!list_empty(&list)) {
2291 trace_block_unplug(this_hctx->queue, depth, !from_schedule);
2292 blk_mq_sched_insert_requests(this_hctx, this_ctx, &list,
Jens Axboebd166ef2017-01-17 06:03:22 -07002293 from_schedule);
Jens Axboebc490f82021-10-18 10:12:12 -06002294 }
Jens Axboe320ae512013-10-24 09:20:05 +01002295}
2296
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002297static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
2298 unsigned int nr_segs)
Jens Axboe320ae512013-10-24 09:20:05 +01002299{
Eric Biggers93f221a2020-09-15 20:53:14 -07002300 int err;
2301
Christoph Hellwigf924cdd2019-06-06 12:29:00 +02002302 if (bio->bi_opf & REQ_RAHEAD)
2303 rq->cmd_flags |= REQ_FAILFAST_MASK;
2304
2305 rq->__sector = bio->bi_iter.bi_sector;
2306 rq->write_hint = bio->bi_write_hint;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002307 blk_rq_bio_prep(rq, bio, nr_segs);
Eric Biggers93f221a2020-09-15 20:53:14 -07002308
2309 /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */
2310 err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO);
2311 WARN_ON_ONCE(err);
Jens Axboe4b570522014-05-29 11:00:11 -06002312
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +02002313 blk_account_io_start(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01002314}
2315
Mike Snitzer0f955492018-01-17 11:25:56 -05002316static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig3e087732021-10-12 13:12:24 +02002317 struct request *rq, bool last)
Shaohua Lif984df12015-05-08 10:51:32 -07002318{
Shaohua Lif984df12015-05-08 10:51:32 -07002319 struct request_queue *q = rq->q;
Shaohua Lif984df12015-05-08 10:51:32 -07002320 struct blk_mq_queue_data bd = {
2321 .rq = rq,
Jens Axboebe94f052018-11-24 10:15:46 -07002322 .last = last,
Shaohua Lif984df12015-05-08 10:51:32 -07002323 };
Jens Axboef06345a2017-06-12 11:22:46 -06002324 blk_status_t ret;
Mike Snitzer0f955492018-01-17 11:25:56 -05002325
Mike Snitzer0f955492018-01-17 11:25:56 -05002326 /*
2327 * For OK queue, we are done. For error, caller may kill it.
2328 * Any other error (busy), just add it to our list as we
2329 * previously would have done.
2330 */
2331 ret = q->mq_ops->queue_rq(hctx, &bd);
2332 switch (ret) {
2333 case BLK_STS_OK:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002334 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05002335 break;
2336 case BLK_STS_RESOURCE:
Ming Lei86ff7c22018-01-30 22:04:57 -05002337 case BLK_STS_DEV_RESOURCE:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002338 blk_mq_update_dispatch_busy(hctx, true);
Mike Snitzer0f955492018-01-17 11:25:56 -05002339 __blk_mq_requeue_request(rq);
2340 break;
2341 default:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002342 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05002343 break;
2344 }
2345
2346 return ret;
2347}
2348
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002349static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
Mike Snitzer0f955492018-01-17 11:25:56 -05002350 struct request *rq,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002351 bool bypass_insert, bool last)
Mike Snitzer0f955492018-01-17 11:25:56 -05002352{
2353 struct request_queue *q = rq->q;
Ming Leid964f042017-06-06 23:22:00 +08002354 bool run_queue = true;
Ming Lei2a5a24a2021-01-22 10:33:12 +08002355 int budget_token;
Ming Leid964f042017-06-06 23:22:00 +08002356
Ming Lei23d4ee12018-01-18 12:06:59 +08002357 /*
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002358 * RCU or SRCU read lock is needed before checking quiesced flag.
Ming Lei23d4ee12018-01-18 12:06:59 +08002359 *
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002360 * When queue is stopped or quiesced, ignore 'bypass_insert' from
2361 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
2362 * and avoid driver to try to dispatch again.
Ming Lei23d4ee12018-01-18 12:06:59 +08002363 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002364 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
Ming Leid964f042017-06-06 23:22:00 +08002365 run_queue = false;
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002366 bypass_insert = false;
2367 goto insert;
Ming Leid964f042017-06-06 23:22:00 +08002368 }
Shaohua Lif984df12015-05-08 10:51:32 -07002369
Jens Axboe2ff06822021-10-15 09:44:38 -06002370 if ((rq->rq_flags & RQF_ELV) && !bypass_insert)
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002371 goto insert;
Bart Van Assche2253efc2016-10-28 17:20:02 -07002372
Ming Lei2a5a24a2021-01-22 10:33:12 +08002373 budget_token = blk_mq_get_dispatch_budget(q);
2374 if (budget_token < 0)
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002375 goto insert;
Jens Axboebd166ef2017-01-17 06:03:22 -07002376
Ming Lei2a5a24a2021-01-22 10:33:12 +08002377 blk_mq_set_rq_budget_token(rq, budget_token);
2378
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08002379 if (!blk_mq_get_driver_tag(rq)) {
Ming Lei2a5a24a2021-01-22 10:33:12 +08002380 blk_mq_put_dispatch_budget(q, budget_token);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002381 goto insert;
Ming Lei88022d72017-11-05 02:21:12 +08002382 }
Ming Leide148292017-10-14 17:22:29 +08002383
Christoph Hellwig3e087732021-10-12 13:12:24 +02002384 return __blk_mq_issue_directly(hctx, rq, last);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002385insert:
2386 if (bypass_insert)
2387 return BLK_STS_RESOURCE;
2388
Ming Leidb03f882020-08-18 17:07:28 +08002389 blk_mq_sched_insert_request(rq, false, run_queue, false);
2390
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002391 return BLK_STS_OK;
2392}
2393
André Almeida105663f2020-01-06 15:08:18 -03002394/**
2395 * blk_mq_try_issue_directly - Try to send a request directly to device driver.
2396 * @hctx: Pointer of the associated hardware queue.
2397 * @rq: Pointer to request to be sent.
André Almeida105663f2020-01-06 15:08:18 -03002398 *
2399 * If the device has enough resources to accept a new request now, send the
2400 * request directly to device driver. Else, insert at hctx->dispatch queue, so
2401 * we can try send it another time in the future. Requests inserted at this
2402 * queue have higher priority.
2403 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002404static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig3e087732021-10-12 13:12:24 +02002405 struct request *rq)
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002406{
2407 blk_status_t ret;
2408 int srcu_idx;
2409
2410 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
2411
2412 hctx_lock(hctx, &srcu_idx);
2413
Christoph Hellwig3e087732021-10-12 13:12:24 +02002414 ret = __blk_mq_try_issue_directly(hctx, rq, false, true);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002415 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
Ming Lei01e99ae2020-02-25 09:04:32 +08002416 blk_mq_request_bypass_insert(rq, false, true);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002417 else if (ret != BLK_STS_OK)
2418 blk_mq_end_request(rq, ret);
2419
Jens Axboe04ced152018-01-09 08:29:46 -08002420 hctx_unlock(hctx, srcu_idx);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002421}
2422
2423blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
2424{
2425 blk_status_t ret;
2426 int srcu_idx;
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002427 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
2428
2429 hctx_lock(hctx, &srcu_idx);
Christoph Hellwig3e087732021-10-12 13:12:24 +02002430 ret = __blk_mq_try_issue_directly(hctx, rq, true, last);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002431 hctx_unlock(hctx, srcu_idx);
Jianchao Wang7f556a42018-12-14 09:28:18 +08002432
2433 return ret;
Christoph Hellwig5eb61262017-03-22 15:01:51 -04002434}
2435
Ming Lei6ce3dd62018-07-10 09:03:31 +08002436void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
2437 struct list_head *list)
2438{
Keith Busch536167d42020-04-07 03:13:48 +09002439 int queued = 0;
yangerkun632bfb62020-09-05 19:25:56 +08002440 int errors = 0;
Keith Busch536167d42020-04-07 03:13:48 +09002441
Ming Lei6ce3dd62018-07-10 09:03:31 +08002442 while (!list_empty(list)) {
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002443 blk_status_t ret;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002444 struct request *rq = list_first_entry(list, struct request,
2445 queuelist);
2446
2447 list_del_init(&rq->queuelist);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002448 ret = blk_mq_request_issue_directly(rq, list_empty(list));
2449 if (ret != BLK_STS_OK) {
2450 if (ret == BLK_STS_RESOURCE ||
2451 ret == BLK_STS_DEV_RESOURCE) {
Ming Lei01e99ae2020-02-25 09:04:32 +08002452 blk_mq_request_bypass_insert(rq, false,
Jens Axboec616cbe2018-12-06 22:17:44 -07002453 list_empty(list));
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002454 break;
2455 }
2456 blk_mq_end_request(rq, ret);
yangerkun632bfb62020-09-05 19:25:56 +08002457 errors++;
Keith Busch536167d42020-04-07 03:13:48 +09002458 } else
2459 queued++;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002460 }
Jens Axboed666ba92018-11-27 17:02:25 -07002461
2462 /*
2463 * If we didn't flush the entire list, we could have told
2464 * the driver there was more coming, but that turned out to
2465 * be a lie.
2466 */
yangerkun632bfb62020-09-05 19:25:56 +08002467 if ((!list_empty(list) || errors) &&
2468 hctx->queue->mq_ops->commit_rqs && queued)
Jens Axboed666ba92018-11-27 17:02:25 -07002469 hctx->queue->mq_ops->commit_rqs(hctx);
Ming Lei6ce3dd62018-07-10 09:03:31 +08002470}
2471
Jens Axboece5b0092018-11-27 17:13:56 -07002472static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
2473{
Jens Axboebc490f82021-10-18 10:12:12 -06002474 if (!plug->multiple_queues) {
2475 struct request *nxt = rq_list_peek(&plug->mq_list);
Jens Axboece5b0092018-11-27 17:13:56 -07002476
Jens Axboebc490f82021-10-18 10:12:12 -06002477 if (nxt && nxt->q != rq->q)
Jens Axboece5b0092018-11-27 17:13:56 -07002478 plug->multiple_queues = true;
2479 }
Jens Axboedc5fc3612021-10-19 06:02:30 -06002480 if (!plug->has_elevator && (rq->rq_flags & RQF_ELV))
2481 plug->has_elevator = true;
Jens Axboebc490f82021-10-18 10:12:12 -06002482 rq->rq_next = NULL;
2483 rq_list_add(&plug->mq_list, rq);
2484 plug->rq_count++;
Jens Axboece5b0092018-11-27 17:13:56 -07002485}
2486
Song Liu7f2a6a62021-09-07 16:03:38 -07002487/*
Jens Axboeba0ffdd2021-10-06 12:01:07 -06002488 * Allow 2x BLK_MAX_REQUEST_COUNT requests on plug queue for multiple
Song Liu7f2a6a62021-09-07 16:03:38 -07002489 * queues. This is important for md arrays to benefit from merging
2490 * requests.
2491 */
2492static inline unsigned short blk_plug_max_rq_count(struct blk_plug *plug)
2493{
2494 if (plug->multiple_queues)
Jens Axboeba0ffdd2021-10-06 12:01:07 -06002495 return BLK_MAX_REQUEST_COUNT * 2;
Song Liu7f2a6a62021-09-07 16:03:38 -07002496 return BLK_MAX_REQUEST_COUNT;
2497}
2498
Ming Leib131f202021-11-11 16:51:34 +08002499static bool blk_mq_attempt_bio_merge(struct request_queue *q,
2500 struct bio *bio, unsigned int nr_segs,
2501 bool *same_queue_rq)
Jens Axboe900e0802021-11-03 05:47:09 -06002502{
2503 if (!blk_queue_nomerges(q) && bio_mergeable(bio)) {
2504 if (blk_attempt_plug_merge(q, bio, nr_segs, same_queue_rq))
2505 return true;
2506 if (blk_mq_sched_bio_merge(q, bio, nr_segs))
2507 return true;
2508 }
2509 return false;
2510}
2511
Jens Axboe71539712021-11-03 05:52:45 -06002512static struct request *blk_mq_get_new_requests(struct request_queue *q,
2513 struct blk_plug *plug,
Jens Axboe900e0802021-11-03 05:47:09 -06002514 struct bio *bio,
2515 unsigned int nsegs,
2516 bool *same_queue_rq)
Jens Axboe71539712021-11-03 05:52:45 -06002517{
2518 struct blk_mq_alloc_data data = {
2519 .q = q,
2520 .nr_tags = 1,
2521 .cmd_flags = bio->bi_opf,
2522 };
2523 struct request *rq;
2524
Ming Leib131f202021-11-11 16:51:34 +08002525 if (blk_mq_attempt_bio_merge(q, bio, nsegs, same_queue_rq))
Ming Leib6371082021-11-12 20:47:15 +08002526 return NULL;
Jens Axboe900e0802021-11-03 05:47:09 -06002527
2528 rq_qos_throttle(q, bio);
2529
Jens Axboe71539712021-11-03 05:52:45 -06002530 if (plug) {
2531 data.nr_tags = plug->nr_ios;
2532 plug->nr_ios = 1;
2533 data.cached_rq = &plug->cached_rq;
2534 }
2535
2536 rq = __blk_mq_alloc_requests(&data);
2537 if (rq)
2538 return rq;
2539
2540 rq_qos_cleanup(q, bio);
2541 if (bio->bi_opf & REQ_NOWAIT)
2542 bio_wouldblock_error(bio);
Ming Leib6371082021-11-12 20:47:15 +08002543
Jens Axboe71539712021-11-03 05:52:45 -06002544 return NULL;
2545}
2546
Jens Axboe95febeb2021-11-15 14:23:08 -07002547static inline bool blk_mq_can_use_cached_rq(struct request *rq, struct bio *bio)
Ming Leib6371082021-11-12 20:47:15 +08002548{
2549 if (blk_mq_get_hctx_type(bio->bi_opf) != rq->mq_hctx->type)
2550 return false;
2551
2552 if (op_is_flush(rq->cmd_flags) != op_is_flush(bio->bi_opf))
2553 return false;
2554
2555 return true;
2556}
2557
Jens Axboe71539712021-11-03 05:52:45 -06002558static inline struct request *blk_mq_get_request(struct request_queue *q,
2559 struct blk_plug *plug,
Jens Axboe900e0802021-11-03 05:47:09 -06002560 struct bio *bio,
2561 unsigned int nsegs,
2562 bool *same_queue_rq)
Jens Axboe71539712021-11-03 05:52:45 -06002563{
Ming Leib6371082021-11-12 20:47:15 +08002564 struct request *rq;
2565 bool checked = false;
2566
Jens Axboe71539712021-11-03 05:52:45 -06002567 if (plug) {
Jens Axboe71539712021-11-03 05:52:45 -06002568 rq = rq_list_peek(&plug->cached_rq);
Jens Axboe10c47872021-11-04 11:54:47 -06002569 if (rq && rq->q == q) {
Jens Axboe900e0802021-11-03 05:47:09 -06002570 if (unlikely(!submit_bio_checks(bio)))
2571 return NULL;
Ming Leib131f202021-11-11 16:51:34 +08002572 if (blk_mq_attempt_bio_merge(q, bio, nsegs,
2573 same_queue_rq))
Jens Axboe900e0802021-11-03 05:47:09 -06002574 return NULL;
Ming Leib6371082021-11-12 20:47:15 +08002575 checked = true;
2576 if (!blk_mq_can_use_cached_rq(rq, bio))
2577 goto fallback;
2578 rq->cmd_flags = bio->bi_opf;
Jens Axboe71539712021-11-03 05:52:45 -06002579 plug->cached_rq = rq_list_next(rq);
2580 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe900e0802021-11-03 05:47:09 -06002581 rq_qos_throttle(q, bio);
Jens Axboe71539712021-11-03 05:52:45 -06002582 return rq;
2583 }
2584 }
2585
Ming Leib6371082021-11-12 20:47:15 +08002586fallback:
2587 if (unlikely(bio_queue_enter(bio)))
2588 return NULL;
Jens Axboe95febeb2021-11-15 14:23:08 -07002589 if (unlikely(!checked && !submit_bio_checks(bio)))
2590 goto out_put;
Ming Leib6371082021-11-12 20:47:15 +08002591 rq = blk_mq_get_new_requests(q, plug, bio, nsegs, same_queue_rq);
Jens Axboe95febeb2021-11-15 14:23:08 -07002592 if (rq)
2593 return rq;
2594out_put:
2595 blk_queue_exit(q);
2596 return NULL;
Jens Axboe71539712021-11-03 05:52:45 -06002597}
2598
André Almeida105663f2020-01-06 15:08:18 -03002599/**
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02002600 * blk_mq_submit_bio - Create and send a request to block device.
André Almeida105663f2020-01-06 15:08:18 -03002601 * @bio: Bio pointer.
2602 *
2603 * Builds up a request structure from @q and @bio and send to the device. The
2604 * request may not be queued directly to hardware if:
2605 * * This request can be merged with another one
2606 * * We want to place request at plug queue for possible future merging
2607 * * There is an IO scheduler active at this queue
2608 *
2609 * It will not queue the request if there is an error with the bio, or at the
2610 * request creation.
André Almeida105663f2020-01-06 15:08:18 -03002611 */
Christoph Hellwig3e087732021-10-12 13:12:24 +02002612void blk_mq_submit_bio(struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06002613{
Pavel Begunkoved6cdde2021-10-14 15:03:30 +01002614 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002615 const int is_sync = op_is_sync(bio->bi_opf);
Jens Axboe07068d52014-05-22 10:40:51 -06002616 struct request *rq;
Shaohua Lif984df12015-05-08 10:51:32 -07002617 struct blk_plug *plug;
Jens Axboe87c037d2021-10-18 10:07:09 -06002618 bool same_queue_rq = false;
Jens Axboeabd45c12021-10-13 12:43:41 -06002619 unsigned int nr_segs = 1;
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002620 blk_status_t ret;
Jens Axboe07068d52014-05-22 10:40:51 -06002621
Jens Axboe900e0802021-11-03 05:47:09 -06002622 if (unlikely(!blk_crypto_bio_prep(&bio)))
2623 return;
2624
Jens Axboe07068d52014-05-22 10:40:51 -06002625 blk_queue_bounce(q, &bio);
Jens Axboeabd45c12021-10-13 12:43:41 -06002626 if (blk_may_split(q, bio))
2627 __blk_queue_split(q, &bio, &nr_segs);
Wen Xiongf36ea502017-05-10 08:54:11 -05002628
Dmitry Monakhove23947b2017-06-29 11:31:11 -07002629 if (!bio_integrity_prep(bio))
Jens Axboe900e0802021-11-03 05:47:09 -06002630 return;
Jens Axboe87760e52016-11-09 12:38:14 -07002631
Jens Axboe47c122e2021-10-06 06:34:11 -06002632 plug = blk_mq_plug(q, bio);
Jens Axboe900e0802021-11-03 05:47:09 -06002633 rq = blk_mq_get_request(q, plug, bio, nr_segs, &same_queue_rq);
Jens Axboe71539712021-11-03 05:52:45 -06002634 if (unlikely(!rq))
Jens Axboe900e0802021-11-03 05:47:09 -06002635 return;
Jens Axboe87760e52016-11-09 12:38:14 -07002636
Christoph Hellwige8a676d2020-12-03 17:21:36 +01002637 trace_block_getrq(bio);
Xiaoguang Wangd6f1dda2018-10-23 22:30:50 +08002638
Josef Bacikc1c80382018-07-03 11:14:59 -04002639 rq_qos_track(q, rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06002640
Bart Van Assche970d1682019-07-01 08:47:30 -07002641 blk_mq_bio_to_request(rq, bio, nr_segs);
2642
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002643 ret = blk_crypto_init_request(rq);
2644 if (ret != BLK_STS_OK) {
2645 bio->bi_status = ret;
2646 bio_endio(bio);
2647 blk_mq_free_request(rq);
Christoph Hellwig3e087732021-10-12 13:12:24 +02002648 return;
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002649 }
2650
Ming Lei2b504bd2021-11-18 23:30:41 +08002651 if (op_is_flush(bio->bi_opf)) {
2652 blk_insert_flush(rq);
Christoph Hellwigd92ca9d82021-10-19 14:25:53 +02002653 return;
Ming Lei2b504bd2021-11-18 23:30:41 +08002654 }
Christoph Hellwigd92ca9d82021-10-19 14:25:53 +02002655
2656 if (plug && (q->nr_hw_queues == 1 ||
2657 blk_mq_is_shared_tags(rq->mq_hctx->flags) ||
2658 q->mq_ops->commit_rqs || !blk_queue_nonrot(q))) {
Jens Axboeb2c5d162018-11-29 10:03:42 -07002659 /*
2660 * Use plugging if we have a ->commit_rqs() hook as well, as
2661 * we know the driver uses bd->last in a smart fashion.
Ming Lei3154df22019-09-27 15:24:31 +08002662 *
2663 * Use normal plugging if this disk is slow HDD, as sequential
2664 * IO may benefit a lot from plug merging.
Jens Axboeb2c5d162018-11-29 10:03:42 -07002665 */
Jens Axboe5f0ed772018-11-23 22:04:33 -07002666 unsigned int request_count = plug->rq_count;
Shaohua Li600271d2016-11-03 17:03:54 -07002667 struct request *last = NULL;
2668
Jens Axboebc490f82021-10-18 10:12:12 -06002669 if (!request_count) {
Jeff Moyere6c44382015-05-08 10:51:30 -07002670 trace_block_plug(q);
Jens Axboebc490f82021-10-18 10:12:12 -06002671 } else if (!blk_queue_nomerges(q)) {
2672 last = rq_list_peek(&plug->mq_list);
2673 if (blk_rq_bytes(last) < BLK_PLUG_FLUSH_SIZE)
2674 last = NULL;
2675 }
Jens Axboeb094f892015-11-20 20:29:45 -07002676
Jens Axboebc490f82021-10-18 10:12:12 -06002677 if (request_count >= blk_plug_max_rq_count(plug) || last) {
Christoph Hellwiga214b942021-10-20 16:41:16 +02002678 blk_mq_flush_plug_list(plug, false);
Jeff Moyere6c44382015-05-08 10:51:30 -07002679 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002680 }
Jens Axboeb094f892015-11-20 20:29:45 -07002681
Jens Axboece5b0092018-11-27 17:13:56 -07002682 blk_add_rq_to_plug(plug, rq);
Jens Axboe2ff06822021-10-15 09:44:38 -06002683 } else if (rq->rq_flags & RQF_ELV) {
André Almeida105663f2020-01-06 15:08:18 -03002684 /* Insert the request at the IO scheduler queue */
Ming Leia12de1d2019-09-27 15:24:30 +08002685 blk_mq_sched_insert_request(rq, false, true, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002686 } else if (plug && !blk_queue_nomerges(q)) {
Jens Axboe87c037d2021-10-18 10:07:09 -06002687 struct request *next_rq = NULL;
2688
Jens Axboe320ae512013-10-24 09:20:05 +01002689 /*
2690 * We do limited plugging. If the bio can be merged, do that.
2691 * Otherwise the existing request in the plug list will be
2692 * issued. So the plug list will have one request at most
Christoph Hellwig22997222017-03-22 15:01:52 -04002693 * The plug list might get flushed before this. If that happens,
2694 * the plug list is empty, and same_queue_rq is invalid.
Jens Axboe320ae512013-10-24 09:20:05 +01002695 */
Jens Axboe4711b572018-11-27 17:07:17 -07002696 if (same_queue_rq) {
Jens Axboebc490f82021-10-18 10:12:12 -06002697 next_rq = rq_list_pop(&plug->mq_list);
Jens Axboe4711b572018-11-27 17:07:17 -07002698 plug->rq_count--;
2699 }
Jens Axboece5b0092018-11-27 17:13:56 -07002700 blk_add_rq_to_plug(plug, rq);
Yufen Yuff3b74b2019-03-26 21:19:25 +08002701 trace_block_plug(q);
Christoph Hellwig22997222017-03-22 15:01:52 -04002702
Jens Axboe87c037d2021-10-18 10:07:09 -06002703 if (next_rq) {
Yufen Yuff3b74b2019-03-26 21:19:25 +08002704 trace_block_unplug(q, 1, true);
Jens Axboe87c037d2021-10-18 10:07:09 -06002705 blk_mq_try_issue_directly(next_rq->mq_hctx, next_rq);
Ming Leidad7a3b2017-06-06 23:21:59 +08002706 }
Ming Leia12de1d2019-09-27 15:24:30 +08002707 } else if ((q->nr_hw_queues > 1 && is_sync) ||
Christoph Hellwig0f38d762021-10-12 12:40:45 +02002708 !rq->mq_hctx->dispatch_busy) {
André Almeida105663f2020-01-06 15:08:18 -03002709 /*
2710 * There is no scheduler and we can try to send directly
2711 * to the hardware.
2712 */
Christoph Hellwig3e087732021-10-12 13:12:24 +02002713 blk_mq_try_issue_directly(rq->mq_hctx, rq);
Ming Leiab42f352017-05-26 19:53:19 +08002714 } else {
André Almeida105663f2020-01-06 15:08:18 -03002715 /* Default case. */
huhai8fa9f552018-05-16 08:21:21 -06002716 blk_mq_sched_insert_request(rq, false, true, true);
Ming Leiab42f352017-05-26 19:53:19 +08002717 }
Jens Axboe320ae512013-10-24 09:20:05 +01002718}
2719
Ming Leibd631412021-05-11 23:22:35 +08002720static size_t order_to_size(unsigned int order)
2721{
2722 return (size_t)PAGE_SIZE << order;
2723}
2724
2725/* called before freeing request pool in @tags */
John Garryf32e4ea2021-10-05 18:23:32 +08002726static void blk_mq_clear_rq_mapping(struct blk_mq_tags *drv_tags,
2727 struct blk_mq_tags *tags)
Ming Leibd631412021-05-11 23:22:35 +08002728{
Ming Leibd631412021-05-11 23:22:35 +08002729 struct page *page;
2730 unsigned long flags;
2731
John Garry4f245d52021-10-05 18:23:33 +08002732 /* There is no need to clear a driver tags own mapping */
2733 if (drv_tags == tags)
2734 return;
2735
Ming Leibd631412021-05-11 23:22:35 +08002736 list_for_each_entry(page, &tags->page_list, lru) {
2737 unsigned long start = (unsigned long)page_address(page);
2738 unsigned long end = start + order_to_size(page->private);
2739 int i;
2740
John Garryf32e4ea2021-10-05 18:23:32 +08002741 for (i = 0; i < drv_tags->nr_tags; i++) {
Ming Leibd631412021-05-11 23:22:35 +08002742 struct request *rq = drv_tags->rqs[i];
2743 unsigned long rq_addr = (unsigned long)rq;
2744
2745 if (rq_addr >= start && rq_addr < end) {
2746 WARN_ON_ONCE(refcount_read(&rq->ref) != 0);
2747 cmpxchg(&drv_tags->rqs[i], rq, NULL);
2748 }
2749 }
2750 }
2751
2752 /*
2753 * Wait until all pending iteration is done.
2754 *
2755 * Request reference is cleared and it is guaranteed to be observed
2756 * after the ->lock is released.
2757 */
2758 spin_lock_irqsave(&drv_tags->lock, flags);
2759 spin_unlock_irqrestore(&drv_tags->lock, flags);
2760}
2761
Jens Axboecc71a6f2017-01-11 14:29:56 -07002762void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2763 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01002764{
John Garryf32e4ea2021-10-05 18:23:32 +08002765 struct blk_mq_tags *drv_tags;
Jens Axboe320ae512013-10-24 09:20:05 +01002766 struct page *page;
2767
John Garry079a2e32021-10-05 18:23:39 +08002768 if (blk_mq_is_shared_tags(set->flags))
2769 drv_tags = set->shared_tags;
John Garrye155b0c2021-10-05 18:23:37 +08002770 else
2771 drv_tags = set->tags[hctx_idx];
John Garryf32e4ea2021-10-05 18:23:32 +08002772
John Garry65de57b2021-10-05 18:23:26 +08002773 if (tags->static_rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002774 int i;
2775
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002776 for (i = 0; i < tags->nr_tags; i++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002777 struct request *rq = tags->static_rqs[i];
2778
2779 if (!rq)
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002780 continue;
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002781 set->ops->exit_request(set, rq, hctx_idx);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002782 tags->static_rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002783 }
2784 }
2785
John Garryf32e4ea2021-10-05 18:23:32 +08002786 blk_mq_clear_rq_mapping(drv_tags, tags);
Ming Leibd631412021-05-11 23:22:35 +08002787
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002788 while (!list_empty(&tags->page_list)) {
2789 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07002790 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002791 /*
2792 * Remove kmemleak object previously allocated in
Raul E Rangel273938b2019-05-02 13:48:11 -06002793 * blk_mq_alloc_rqs().
Catalin Marinasf75782e2015-09-14 18:16:02 +01002794 */
2795 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01002796 __free_pages(page, page->private);
2797 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002798}
Jens Axboe320ae512013-10-24 09:20:05 +01002799
John Garrye155b0c2021-10-05 18:23:37 +08002800void blk_mq_free_rq_map(struct blk_mq_tags *tags)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002801{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002802 kfree(tags->rqs);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002803 tags->rqs = NULL;
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002804 kfree(tags->static_rqs);
2805 tags->static_rqs = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002806
John Garrye155b0c2021-10-05 18:23:37 +08002807 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01002808}
2809
John Garry63064be2021-10-05 18:23:35 +08002810static struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
2811 unsigned int hctx_idx,
2812 unsigned int nr_tags,
John Garrye155b0c2021-10-05 18:23:37 +08002813 unsigned int reserved_tags)
Jens Axboe320ae512013-10-24 09:20:05 +01002814{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002815 struct blk_mq_tags *tags;
Shaohua Li59f082e2017-02-01 09:53:14 -08002816 int node;
Jens Axboe320ae512013-10-24 09:20:05 +01002817
Dongli Zhang7d76f852019-02-27 21:35:01 +08002818 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002819 if (node == NUMA_NO_NODE)
2820 node = set->numa_node;
2821
John Garrye155b0c2021-10-05 18:23:37 +08002822 tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
2823 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002824 if (!tags)
2825 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002826
Kees Cook590b5b72018-06-12 14:04:20 -07002827 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002828 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
Shaohua Li59f082e2017-02-01 09:53:14 -08002829 node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002830 if (!tags->rqs) {
John Garrye155b0c2021-10-05 18:23:37 +08002831 blk_mq_free_tags(tags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002832 return NULL;
2833 }
Jens Axboe320ae512013-10-24 09:20:05 +01002834
Kees Cook590b5b72018-06-12 14:04:20 -07002835 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2836 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2837 node);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002838 if (!tags->static_rqs) {
2839 kfree(tags->rqs);
John Garrye155b0c2021-10-05 18:23:37 +08002840 blk_mq_free_tags(tags);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002841 return NULL;
2842 }
2843
Jens Axboecc71a6f2017-01-11 14:29:56 -07002844 return tags;
2845}
2846
Tejun Heo1d9bd512018-01-09 08:29:48 -08002847static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2848 unsigned int hctx_idx, int node)
2849{
2850 int ret;
2851
2852 if (set->ops->init_request) {
2853 ret = set->ops->init_request(set, rq, hctx_idx, node);
2854 if (ret)
2855 return ret;
2856 }
2857
Keith Busch12f5b932018-05-29 15:52:28 +02002858 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Tejun Heo1d9bd512018-01-09 08:29:48 -08002859 return 0;
2860}
2861
John Garry63064be2021-10-05 18:23:35 +08002862static int blk_mq_alloc_rqs(struct blk_mq_tag_set *set,
2863 struct blk_mq_tags *tags,
2864 unsigned int hctx_idx, unsigned int depth)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002865{
2866 unsigned int i, j, entries_per_page, max_order = 4;
2867 size_t rq_size, left;
Shaohua Li59f082e2017-02-01 09:53:14 -08002868 int node;
2869
Dongli Zhang7d76f852019-02-27 21:35:01 +08002870 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002871 if (node == NUMA_NO_NODE)
2872 node = set->numa_node;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002873
2874 INIT_LIST_HEAD(&tags->page_list);
2875
Jens Axboe320ae512013-10-24 09:20:05 +01002876 /*
2877 * rq_size is the size of the request plus driver payload, rounded
2878 * to the cacheline size
2879 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002880 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01002881 cache_line_size());
Jens Axboecc71a6f2017-01-11 14:29:56 -07002882 left = rq_size * depth;
Jens Axboe320ae512013-10-24 09:20:05 +01002883
Jens Axboecc71a6f2017-01-11 14:29:56 -07002884 for (i = 0; i < depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01002885 int this_order = max_order;
2886 struct page *page;
2887 int to_do;
2888 void *p;
2889
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06002890 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01002891 this_order--;
2892
2893 do {
Shaohua Li59f082e2017-02-01 09:53:14 -08002894 page = alloc_pages_node(node,
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002895 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06002896 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01002897 if (page)
2898 break;
2899 if (!this_order--)
2900 break;
2901 if (order_to_size(this_order) < rq_size)
2902 break;
2903 } while (1);
2904
2905 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002906 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01002907
2908 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002909 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01002910
2911 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002912 /*
2913 * Allow kmemleak to scan these pages as they contain pointers
2914 * to additional allocations like via ops->init_request().
2915 */
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002916 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
Jens Axboe320ae512013-10-24 09:20:05 +01002917 entries_per_page = order_to_size(this_order) / rq_size;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002918 to_do = min(entries_per_page, depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01002919 left -= to_do * rq_size;
2920 for (j = 0; j < to_do; j++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002921 struct request *rq = p;
2922
2923 tags->static_rqs[i] = rq;
Tejun Heo1d9bd512018-01-09 08:29:48 -08002924 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
2925 tags->static_rqs[i] = NULL;
2926 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002927 }
2928
Jens Axboe320ae512013-10-24 09:20:05 +01002929 p += rq_size;
2930 i++;
2931 }
2932 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002933 return 0;
Jens Axboe320ae512013-10-24 09:20:05 +01002934
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002935fail:
Jens Axboecc71a6f2017-01-11 14:29:56 -07002936 blk_mq_free_rqs(set, tags, hctx_idx);
2937 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01002938}
2939
Ming Leibf0beec2020-05-29 15:53:15 +02002940struct rq_iter_data {
2941 struct blk_mq_hw_ctx *hctx;
2942 bool has_rq;
2943};
2944
2945static bool blk_mq_has_request(struct request *rq, void *data, bool reserved)
2946{
2947 struct rq_iter_data *iter_data = data;
2948
2949 if (rq->mq_hctx != iter_data->hctx)
2950 return true;
2951 iter_data->has_rq = true;
2952 return false;
2953}
2954
2955static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
2956{
2957 struct blk_mq_tags *tags = hctx->sched_tags ?
2958 hctx->sched_tags : hctx->tags;
2959 struct rq_iter_data data = {
2960 .hctx = hctx,
2961 };
2962
2963 blk_mq_all_tag_iter(tags, blk_mq_has_request, &data);
2964 return data.has_rq;
2965}
2966
2967static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu,
2968 struct blk_mq_hw_ctx *hctx)
2969{
2970 if (cpumask_next_and(-1, hctx->cpumask, cpu_online_mask) != cpu)
2971 return false;
2972 if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids)
2973 return false;
2974 return true;
2975}
2976
2977static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
2978{
2979 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2980 struct blk_mq_hw_ctx, cpuhp_online);
2981
2982 if (!cpumask_test_cpu(cpu, hctx->cpumask) ||
2983 !blk_mq_last_cpu_in_hctx(cpu, hctx))
2984 return 0;
2985
2986 /*
2987 * Prevent new request from being allocated on the current hctx.
2988 *
2989 * The smp_mb__after_atomic() Pairs with the implied barrier in
2990 * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is
2991 * seen once we return from the tag allocator.
2992 */
2993 set_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2994 smp_mb__after_atomic();
2995
2996 /*
2997 * Try to grab a reference to the queue and wait for any outstanding
2998 * requests. If we could not grab a reference the queue has been
2999 * frozen and there are no requests.
3000 */
3001 if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) {
3002 while (blk_mq_hctx_has_requests(hctx))
3003 msleep(5);
3004 percpu_ref_put(&hctx->queue->q_usage_counter);
3005 }
3006
3007 return 0;
3008}
3009
3010static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node)
3011{
3012 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
3013 struct blk_mq_hw_ctx, cpuhp_online);
3014
3015 if (cpumask_test_cpu(cpu, hctx->cpumask))
3016 clear_bit(BLK_MQ_S_INACTIVE, &hctx->state);
3017 return 0;
3018}
3019
Jens Axboee57690f2016-08-24 15:34:35 -06003020/*
3021 * 'cpu' is going away. splice any existing rq_list entries from this
3022 * software queue to the hw queue dispatch list, and ensure that it
3023 * gets run.
3024 */
Thomas Gleixner9467f852016-09-22 08:05:17 -06003025static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
Jens Axboe484b4062014-05-21 14:01:15 -06003026{
Thomas Gleixner9467f852016-09-22 08:05:17 -06003027 struct blk_mq_hw_ctx *hctx;
Jens Axboe484b4062014-05-21 14:01:15 -06003028 struct blk_mq_ctx *ctx;
3029 LIST_HEAD(tmp);
Ming Leic16d6b52018-12-17 08:44:05 -07003030 enum hctx_type type;
Jens Axboe484b4062014-05-21 14:01:15 -06003031
Thomas Gleixner9467f852016-09-22 08:05:17 -06003032 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02003033 if (!cpumask_test_cpu(cpu, hctx->cpumask))
3034 return 0;
3035
Jens Axboee57690f2016-08-24 15:34:35 -06003036 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Ming Leic16d6b52018-12-17 08:44:05 -07003037 type = hctx->type;
Jens Axboe484b4062014-05-21 14:01:15 -06003038
3039 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07003040 if (!list_empty(&ctx->rq_lists[type])) {
3041 list_splice_init(&ctx->rq_lists[type], &tmp);
Jens Axboe484b4062014-05-21 14:01:15 -06003042 blk_mq_hctx_clear_pending(hctx, ctx);
3043 }
3044 spin_unlock(&ctx->lock);
3045
3046 if (list_empty(&tmp))
Thomas Gleixner9467f852016-09-22 08:05:17 -06003047 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06003048
Jens Axboee57690f2016-08-24 15:34:35 -06003049 spin_lock(&hctx->lock);
3050 list_splice_tail_init(&tmp, &hctx->dispatch);
3051 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06003052
3053 blk_mq_run_hw_queue(hctx, true);
Thomas Gleixner9467f852016-09-22 08:05:17 -06003054 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06003055}
3056
Thomas Gleixner9467f852016-09-22 08:05:17 -06003057static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
Jens Axboe484b4062014-05-21 14:01:15 -06003058{
Ming Leibf0beec2020-05-29 15:53:15 +02003059 if (!(hctx->flags & BLK_MQ_F_STACKING))
3060 cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
3061 &hctx->cpuhp_online);
Thomas Gleixner9467f852016-09-22 08:05:17 -06003062 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
3063 &hctx->cpuhp_dead);
Jens Axboe484b4062014-05-21 14:01:15 -06003064}
3065
Ming Lei364b6182021-05-11 23:22:36 +08003066/*
3067 * Before freeing hw queue, clearing the flush request reference in
3068 * tags->rqs[] for avoiding potential UAF.
3069 */
3070static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags,
3071 unsigned int queue_depth, struct request *flush_rq)
3072{
3073 int i;
3074 unsigned long flags;
3075
3076 /* The hw queue may not be mapped yet */
3077 if (!tags)
3078 return;
3079
3080 WARN_ON_ONCE(refcount_read(&flush_rq->ref) != 0);
3081
3082 for (i = 0; i < queue_depth; i++)
3083 cmpxchg(&tags->rqs[i], flush_rq, NULL);
3084
3085 /*
3086 * Wait until all pending iteration is done.
3087 *
3088 * Request reference is cleared and it is guaranteed to be observed
3089 * after the ->lock is released.
3090 */
3091 spin_lock_irqsave(&tags->lock, flags);
3092 spin_unlock_irqrestore(&tags->lock, flags);
3093}
3094
Ming Leic3b4afc2015-06-04 22:25:04 +08003095/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08003096static void blk_mq_exit_hctx(struct request_queue *q,
3097 struct blk_mq_tag_set *set,
3098 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
3099{
Ming Lei364b6182021-05-11 23:22:36 +08003100 struct request *flush_rq = hctx->fq->flush_rq;
3101
Ming Lei8ab0b7d2018-01-09 21:28:29 +08003102 if (blk_mq_hw_queue_mapped(hctx))
3103 blk_mq_tag_idle(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08003104
Ming Lei364b6182021-05-11 23:22:36 +08003105 blk_mq_clear_flush_rq_mapping(set->tags[hctx_idx],
3106 set->queue_depth, flush_rq);
Ming Leif70ced02014-09-25 23:23:47 +08003107 if (set->ops->exit_request)
Ming Lei364b6182021-05-11 23:22:36 +08003108 set->ops->exit_request(set, flush_rq, hctx_idx);
Ming Leif70ced02014-09-25 23:23:47 +08003109
Ming Lei08e98fc2014-09-25 23:23:38 +08003110 if (set->ops->exit_hctx)
3111 set->ops->exit_hctx(hctx, hctx_idx);
3112
Thomas Gleixner9467f852016-09-22 08:05:17 -06003113 blk_mq_remove_cpuhp(hctx);
Ming Lei2f8f1332019-04-30 09:52:27 +08003114
3115 spin_lock(&q->unused_hctx_lock);
3116 list_add(&hctx->hctx_list, &q->unused_hctx_list);
3117 spin_unlock(&q->unused_hctx_lock);
Ming Lei08e98fc2014-09-25 23:23:38 +08003118}
3119
Ming Lei624dbe42014-05-27 23:35:13 +08003120static void blk_mq_exit_hw_queues(struct request_queue *q,
3121 struct blk_mq_tag_set *set, int nr_queue)
3122{
3123 struct blk_mq_hw_ctx *hctx;
3124 unsigned int i;
3125
3126 queue_for_each_hw_ctx(q, hctx, i) {
3127 if (i == nr_queue)
3128 break;
Jianchao Wang477e19d2018-10-12 18:07:25 +08003129 blk_mq_debugfs_unregister_hctx(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08003130 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08003131 }
Ming Lei624dbe42014-05-27 23:35:13 +08003132}
3133
Ming Lei7c6c5b72019-04-30 09:52:26 +08003134static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
3135{
3136 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
3137
3138 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
3139 __alignof__(struct blk_mq_hw_ctx)) !=
3140 sizeof(struct blk_mq_hw_ctx));
3141
3142 if (tag_set->flags & BLK_MQ_F_BLOCKING)
3143 hw_ctx_size += sizeof(struct srcu_struct);
3144
3145 return hw_ctx_size;
3146}
3147
Ming Lei08e98fc2014-09-25 23:23:38 +08003148static int blk_mq_init_hctx(struct request_queue *q,
3149 struct blk_mq_tag_set *set,
3150 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
3151{
Ming Lei7c6c5b72019-04-30 09:52:26 +08003152 hctx->queue_num = hctx_idx;
Ming Lei08e98fc2014-09-25 23:23:38 +08003153
Ming Leibf0beec2020-05-29 15:53:15 +02003154 if (!(hctx->flags & BLK_MQ_F_STACKING))
3155 cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
3156 &hctx->cpuhp_online);
Ming Lei7c6c5b72019-04-30 09:52:26 +08003157 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
3158
3159 hctx->tags = set->tags[hctx_idx];
3160
3161 if (set->ops->init_hctx &&
3162 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
3163 goto unregister_cpu_notifier;
3164
3165 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
3166 hctx->numa_node))
3167 goto exit_hctx;
3168 return 0;
3169
3170 exit_hctx:
3171 if (set->ops->exit_hctx)
3172 set->ops->exit_hctx(hctx, hctx_idx);
3173 unregister_cpu_notifier:
3174 blk_mq_remove_cpuhp(hctx);
3175 return -1;
3176}
3177
3178static struct blk_mq_hw_ctx *
3179blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
3180 int node)
3181{
3182 struct blk_mq_hw_ctx *hctx;
3183 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
3184
3185 hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node);
3186 if (!hctx)
3187 goto fail_alloc_hctx;
3188
3189 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
3190 goto free_hctx;
3191
3192 atomic_set(&hctx->nr_active, 0);
Ming Lei08e98fc2014-09-25 23:23:38 +08003193 if (node == NUMA_NO_NODE)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003194 node = set->numa_node;
3195 hctx->numa_node = node;
Ming Lei08e98fc2014-09-25 23:23:38 +08003196
Jens Axboe9f993732017-04-10 09:54:54 -06003197 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08003198 spin_lock_init(&hctx->lock);
3199 INIT_LIST_HEAD(&hctx->dispatch);
3200 hctx->queue = q;
Ming Lei51db1c32020-08-19 23:20:19 +08003201 hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08003202
Ming Lei2f8f1332019-04-30 09:52:27 +08003203 INIT_LIST_HEAD(&hctx->hctx_list);
3204
Ming Lei08e98fc2014-09-25 23:23:38 +08003205 /*
3206 * Allocate space for all possible cpus to avoid allocation at
3207 * runtime
3208 */
Johannes Thumshirnd904bfa2017-11-15 17:32:33 -08003209 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
Ming Lei7c6c5b72019-04-30 09:52:26 +08003210 gfp, node);
Ming Lei08e98fc2014-09-25 23:23:38 +08003211 if (!hctx->ctxs)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003212 goto free_cpumask;
Ming Lei08e98fc2014-09-25 23:23:38 +08003213
Jianchao Wang5b202852018-10-12 18:07:26 +08003214 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
Ming Leic548e622021-01-22 10:33:08 +08003215 gfp, node, false, false))
Ming Lei08e98fc2014-09-25 23:23:38 +08003216 goto free_ctxs;
Ming Lei08e98fc2014-09-25 23:23:38 +08003217 hctx->nr_ctx = 0;
3218
Ming Lei5815839b2018-06-25 19:31:47 +08003219 spin_lock_init(&hctx->dispatch_wait_lock);
Jens Axboeeb619fd2017-11-09 08:32:43 -07003220 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
3221 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
3222
Guoqing Jiang754a1572020-03-09 22:41:37 +01003223 hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp);
Ming Leif70ced02014-09-25 23:23:47 +08003224 if (!hctx->fq)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003225 goto free_bitmap;
Ming Leif70ced02014-09-25 23:23:47 +08003226
Bart Van Assche6a83e742016-11-02 10:09:51 -06003227 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -08003228 init_srcu_struct(hctx->srcu);
Ming Lei7c6c5b72019-04-30 09:52:26 +08003229 blk_mq_hctx_kobj_init(hctx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06003230
Ming Lei7c6c5b72019-04-30 09:52:26 +08003231 return hctx;
Ming Lei08e98fc2014-09-25 23:23:38 +08003232
3233 free_bitmap:
Omar Sandoval88459642016-09-17 08:38:44 -06003234 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08003235 free_ctxs:
3236 kfree(hctx->ctxs);
Ming Lei7c6c5b72019-04-30 09:52:26 +08003237 free_cpumask:
3238 free_cpumask_var(hctx->cpumask);
3239 free_hctx:
3240 kfree(hctx);
3241 fail_alloc_hctx:
3242 return NULL;
Ming Lei08e98fc2014-09-25 23:23:38 +08003243}
3244
Jens Axboe320ae512013-10-24 09:20:05 +01003245static void blk_mq_init_cpu_queues(struct request_queue *q,
3246 unsigned int nr_hw_queues)
3247{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003248 struct blk_mq_tag_set *set = q->tag_set;
3249 unsigned int i, j;
Jens Axboe320ae512013-10-24 09:20:05 +01003250
3251 for_each_possible_cpu(i) {
3252 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
3253 struct blk_mq_hw_ctx *hctx;
Ming Leic16d6b52018-12-17 08:44:05 -07003254 int k;
Jens Axboe320ae512013-10-24 09:20:05 +01003255
Jens Axboe320ae512013-10-24 09:20:05 +01003256 __ctx->cpu = i;
3257 spin_lock_init(&__ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07003258 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
3259 INIT_LIST_HEAD(&__ctx->rq_lists[k]);
3260
Jens Axboe320ae512013-10-24 09:20:05 +01003261 __ctx->queue = q;
3262
Jens Axboe320ae512013-10-24 09:20:05 +01003263 /*
3264 * Set local node, IFF we have more than one hw queue. If
3265 * not, we remain on the home node of the device
3266 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06003267 for (j = 0; j < set->nr_maps; j++) {
3268 hctx = blk_mq_map_queue_type(q, j, i);
3269 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
Xianting Tian576e85c2020-10-19 16:20:47 +08003270 hctx->numa_node = cpu_to_node(i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003271 }
Jens Axboe320ae512013-10-24 09:20:05 +01003272 }
3273}
3274
John Garry63064be2021-10-05 18:23:35 +08003275struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
3276 unsigned int hctx_idx,
3277 unsigned int depth)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003278{
John Garry63064be2021-10-05 18:23:35 +08003279 struct blk_mq_tags *tags;
3280 int ret;
Jens Axboecc71a6f2017-01-11 14:29:56 -07003281
John Garrye155b0c2021-10-05 18:23:37 +08003282 tags = blk_mq_alloc_rq_map(set, hctx_idx, depth, set->reserved_tags);
John Garry63064be2021-10-05 18:23:35 +08003283 if (!tags)
3284 return NULL;
Jens Axboecc71a6f2017-01-11 14:29:56 -07003285
John Garry63064be2021-10-05 18:23:35 +08003286 ret = blk_mq_alloc_rqs(set, tags, hctx_idx, depth);
3287 if (ret) {
John Garrye155b0c2021-10-05 18:23:37 +08003288 blk_mq_free_rq_map(tags);
John Garry63064be2021-10-05 18:23:35 +08003289 return NULL;
3290 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07003291
John Garry63064be2021-10-05 18:23:35 +08003292 return tags;
Jens Axboecc71a6f2017-01-11 14:29:56 -07003293}
3294
John Garry63064be2021-10-05 18:23:35 +08003295static bool __blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
3296 int hctx_idx)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003297{
John Garry079a2e32021-10-05 18:23:39 +08003298 if (blk_mq_is_shared_tags(set->flags)) {
3299 set->tags[hctx_idx] = set->shared_tags;
John Garry1c0706a2020-08-19 23:20:22 +08003300
John Garrye155b0c2021-10-05 18:23:37 +08003301 return true;
Jens Axboebd166ef2017-01-17 06:03:22 -07003302 }
John Garrye155b0c2021-10-05 18:23:37 +08003303
John Garry63064be2021-10-05 18:23:35 +08003304 set->tags[hctx_idx] = blk_mq_alloc_map_and_rqs(set, hctx_idx,
3305 set->queue_depth);
3306
3307 return set->tags[hctx_idx];
Jens Axboecc71a6f2017-01-11 14:29:56 -07003308}
3309
John Garry645db342021-10-05 18:23:36 +08003310void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
3311 struct blk_mq_tags *tags,
3312 unsigned int hctx_idx)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003313{
John Garry645db342021-10-05 18:23:36 +08003314 if (tags) {
3315 blk_mq_free_rqs(set, tags, hctx_idx);
John Garrye155b0c2021-10-05 18:23:37 +08003316 blk_mq_free_rq_map(tags);
Jens Axboecc71a6f2017-01-11 14:29:56 -07003317 }
3318}
3319
John Garrye155b0c2021-10-05 18:23:37 +08003320static void __blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
3321 unsigned int hctx_idx)
3322{
John Garry079a2e32021-10-05 18:23:39 +08003323 if (!blk_mq_is_shared_tags(set->flags))
John Garrye155b0c2021-10-05 18:23:37 +08003324 blk_mq_free_map_and_rqs(set, set->tags[hctx_idx], hctx_idx);
3325
3326 set->tags[hctx_idx] = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01003327}
3328
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003329static void blk_mq_map_swqueue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01003330{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003331 unsigned int i, j, hctx_idx;
Jens Axboe320ae512013-10-24 09:20:05 +01003332 struct blk_mq_hw_ctx *hctx;
3333 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08003334 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01003335
3336 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06003337 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01003338 hctx->nr_ctx = 0;
huhaid416c922018-05-18 08:32:30 -06003339 hctx->dispatch_from = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01003340 }
3341
3342 /*
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003343 * Map software to hardware queues.
Ming Lei4412efe2018-04-25 04:01:44 +08003344 *
3345 * If the cpu isn't present, the cpu is mapped to first hctx.
Jens Axboe320ae512013-10-24 09:20:05 +01003346 */
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08003347 for_each_possible_cpu(i) {
Ming Lei4412efe2018-04-25 04:01:44 +08003348
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01003349 ctx = per_cpu_ptr(q->queue_ctx, i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003350 for (j = 0; j < set->nr_maps; j++) {
Jianchao Wangbb94aea2019-01-24 18:25:33 +08003351 if (!set->map[j].nr_queues) {
3352 ctx->hctxs[j] = blk_mq_map_queue_type(q,
3353 HCTX_TYPE_DEFAULT, i);
Ming Leie5edd5f2018-12-18 01:28:56 +08003354 continue;
Jianchao Wangbb94aea2019-01-24 18:25:33 +08003355 }
Ming Leifd689872020-05-07 21:04:08 +08003356 hctx_idx = set->map[j].mq_map[i];
3357 /* unmapped hw queue can be remapped after CPU topo changed */
3358 if (!set->tags[hctx_idx] &&
John Garry63064be2021-10-05 18:23:35 +08003359 !__blk_mq_alloc_map_and_rqs(set, hctx_idx)) {
Ming Leifd689872020-05-07 21:04:08 +08003360 /*
3361 * If tags initialization fail for some hctx,
3362 * that hctx won't be brought online. In this
3363 * case, remap the current ctx to hctx[0] which
3364 * is guaranteed to always have tags allocated
3365 */
3366 set->map[j].mq_map[i] = 0;
3367 }
Ming Leie5edd5f2018-12-18 01:28:56 +08003368
Jens Axboeb3c661b2018-10-30 10:36:06 -06003369 hctx = blk_mq_map_queue_type(q, j, i);
Jianchao Wang8ccdf4a2019-01-24 18:25:32 +08003370 ctx->hctxs[j] = hctx;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003371 /*
3372 * If the CPU is already set in the mask, then we've
3373 * mapped this one already. This can happen if
3374 * devices share queues across queue maps.
3375 */
3376 if (cpumask_test_cpu(i, hctx->cpumask))
3377 continue;
3378
3379 cpumask_set_cpu(i, hctx->cpumask);
3380 hctx->type = j;
3381 ctx->index_hw[hctx->type] = hctx->nr_ctx;
3382 hctx->ctxs[hctx->nr_ctx++] = ctx;
3383
3384 /*
3385 * If the nr_ctx type overflows, we have exceeded the
3386 * amount of sw queues we can support.
3387 */
3388 BUG_ON(!hctx->nr_ctx);
3389 }
Jianchao Wangbb94aea2019-01-24 18:25:33 +08003390
3391 for (; j < HCTX_MAX_TYPES; j++)
3392 ctx->hctxs[j] = blk_mq_map_queue_type(q,
3393 HCTX_TYPE_DEFAULT, i);
Jens Axboe320ae512013-10-24 09:20:05 +01003394 }
Jens Axboe506e9312014-05-07 10:26:44 -06003395
3396 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei4412efe2018-04-25 04:01:44 +08003397 /*
3398 * If no software queues are mapped to this hardware queue,
3399 * disable it and free the request entries.
3400 */
3401 if (!hctx->nr_ctx) {
3402 /* Never unmap queue 0. We need it as a
3403 * fallback in case of a new remap fails
3404 * allocation
3405 */
John Garrye155b0c2021-10-05 18:23:37 +08003406 if (i)
3407 __blk_mq_free_map_and_rqs(set, i);
Ming Lei4412efe2018-04-25 04:01:44 +08003408
3409 hctx->tags = NULL;
3410 continue;
3411 }
Jens Axboe484b4062014-05-21 14:01:15 -06003412
Ming Lei2a34c082015-04-21 10:00:20 +08003413 hctx->tags = set->tags[i];
3414 WARN_ON(!hctx->tags);
3415
Jens Axboe484b4062014-05-21 14:01:15 -06003416 /*
Chong Yuan889fa312015-04-15 11:39:29 -06003417 * Set the map size to the number of mapped software queues.
3418 * This is more accurate and more efficient than looping
3419 * over all possibly mapped software queues.
3420 */
Omar Sandoval88459642016-09-17 08:38:44 -06003421 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
Chong Yuan889fa312015-04-15 11:39:29 -06003422
3423 /*
Jens Axboe484b4062014-05-21 14:01:15 -06003424 * Initialize batch roundrobin counts
3425 */
Ming Leif82ddf12018-04-08 17:48:10 +08003426 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06003427 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
3428 }
Jens Axboe320ae512013-10-24 09:20:05 +01003429}
3430
Jens Axboe8e8320c2017-06-20 17:56:13 -06003431/*
3432 * Caller needs to ensure that we're either frozen/quiesced, or that
3433 * the queue isn't live yet.
3434 */
Jeff Moyer2404e602015-11-03 10:40:06 -05003435static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06003436{
3437 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06003438 int i;
3439
Jeff Moyer2404e602015-11-03 10:40:06 -05003440 queue_for_each_hw_ctx(q, hctx, i) {
Yu Kuai454bb672021-07-31 14:21:30 +08003441 if (shared) {
Ming Lei51db1c32020-08-19 23:20:19 +08003442 hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
Yu Kuai454bb672021-07-31 14:21:30 +08003443 } else {
3444 blk_mq_tag_idle(hctx);
Ming Lei51db1c32020-08-19 23:20:19 +08003445 hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
Yu Kuai454bb672021-07-31 14:21:30 +08003446 }
Jeff Moyer2404e602015-11-03 10:40:06 -05003447 }
3448}
3449
Hannes Reinecke655ac302020-08-19 23:20:20 +08003450static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set,
3451 bool shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05003452{
3453 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06003454
Bart Van Assche705cda92017-04-07 11:16:49 -07003455 lockdep_assert_held(&set->tag_list_lock);
3456
Jens Axboe0d2602c2014-05-13 15:10:52 -06003457 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3458 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05003459 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003460 blk_mq_unfreeze_queue(q);
3461 }
3462}
3463
3464static void blk_mq_del_queue_tag_set(struct request_queue *q)
3465{
3466 struct blk_mq_tag_set *set = q->tag_set;
3467
Jens Axboe0d2602c2014-05-13 15:10:52 -06003468 mutex_lock(&set->tag_list_lock);
Daniel Wagner08c875c2020-07-28 15:29:51 +02003469 list_del(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05003470 if (list_is_singular(&set->tag_list)) {
3471 /* just transitioned to unshared */
Ming Lei51db1c32020-08-19 23:20:19 +08003472 set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05003473 /* update existing queue */
Hannes Reinecke655ac302020-08-19 23:20:20 +08003474 blk_mq_update_tag_set_shared(set, false);
Jeff Moyer2404e602015-11-03 10:40:06 -05003475 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06003476 mutex_unlock(&set->tag_list_lock);
Roman Pena347c7a2018-06-10 22:38:24 +02003477 INIT_LIST_HEAD(&q->tag_set_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003478}
3479
3480static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
3481 struct request_queue *q)
3482{
Jens Axboe0d2602c2014-05-13 15:10:52 -06003483 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05003484
Jens Axboeff821d22017-11-10 22:05:12 -07003485 /*
3486 * Check to see if we're transitioning to shared (from 1 to 2 queues).
3487 */
3488 if (!list_empty(&set->tag_list) &&
Ming Lei51db1c32020-08-19 23:20:19 +08003489 !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
3490 set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05003491 /* update existing queue */
Hannes Reinecke655ac302020-08-19 23:20:20 +08003492 blk_mq_update_tag_set_shared(set, true);
Jeff Moyer2404e602015-11-03 10:40:06 -05003493 }
Ming Lei51db1c32020-08-19 23:20:19 +08003494 if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
Jeff Moyer2404e602015-11-03 10:40:06 -05003495 queue_set_hctx_shared(q, true);
Daniel Wagner08c875c2020-07-28 15:29:51 +02003496 list_add_tail(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05003497
Jens Axboe0d2602c2014-05-13 15:10:52 -06003498 mutex_unlock(&set->tag_list_lock);
3499}
3500
Ming Lei1db49092018-11-20 09:44:35 +08003501/* All allocations will be freed in release handler of q->mq_kobj */
3502static int blk_mq_alloc_ctxs(struct request_queue *q)
3503{
3504 struct blk_mq_ctxs *ctxs;
3505 int cpu;
3506
3507 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
3508 if (!ctxs)
3509 return -ENOMEM;
3510
3511 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
3512 if (!ctxs->queue_ctx)
3513 goto fail;
3514
3515 for_each_possible_cpu(cpu) {
3516 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
3517 ctx->ctxs = ctxs;
3518 }
3519
3520 q->mq_kobj = &ctxs->kobj;
3521 q->queue_ctx = ctxs->queue_ctx;
3522
3523 return 0;
3524 fail:
3525 kfree(ctxs);
3526 return -ENOMEM;
3527}
3528
Ming Leie09aae72015-01-29 20:17:27 +08003529/*
3530 * It is the actual release handler for mq, but we do it from
3531 * request queue's release handler for avoiding use-after-free
3532 * and headache because q->mq_kobj shouldn't have been introduced,
3533 * but we can't group ctx/kctx kobj without it.
3534 */
3535void blk_mq_release(struct request_queue *q)
3536{
Ming Lei2f8f1332019-04-30 09:52:27 +08003537 struct blk_mq_hw_ctx *hctx, *next;
3538 int i;
Ming Leie09aae72015-01-29 20:17:27 +08003539
Ming Lei2f8f1332019-04-30 09:52:27 +08003540 queue_for_each_hw_ctx(q, hctx, i)
3541 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
3542
3543 /* all hctx are in .unused_hctx_list now */
3544 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
3545 list_del_init(&hctx->hctx_list);
Ming Lei6c8b2322017-02-22 18:14:01 +08003546 kobject_put(&hctx->kobj);
Ming Leic3b4afc2015-06-04 22:25:04 +08003547 }
Ming Leie09aae72015-01-29 20:17:27 +08003548
3549 kfree(q->queue_hw_ctx);
3550
Ming Lei7ea5fe32017-02-22 18:14:00 +08003551 /*
3552 * release .mq_kobj and sw queue's kobject now because
3553 * both share lifetime with request queue.
3554 */
3555 blk_mq_sysfs_deinit(q);
Ming Leie09aae72015-01-29 20:17:27 +08003556}
3557
Christoph Hellwig5ec780a2021-06-24 10:10:12 +02003558static struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set,
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003559 void *queuedata)
Jens Axboe320ae512013-10-24 09:20:05 +01003560{
Christoph Hellwig26a97502021-06-02 09:53:17 +03003561 struct request_queue *q;
3562 int ret;
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003563
Christoph Hellwig26a97502021-06-02 09:53:17 +03003564 q = blk_alloc_queue(set->numa_node);
3565 if (!q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003566 return ERR_PTR(-ENOMEM);
Christoph Hellwig26a97502021-06-02 09:53:17 +03003567 q->queuedata = queuedata;
3568 ret = blk_mq_init_allocated_queue(set, q);
3569 if (ret) {
3570 blk_cleanup_queue(q);
3571 return ERR_PTR(ret);
3572 }
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003573 return q;
3574}
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003575
3576struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
3577{
3578 return blk_mq_init_queue_data(set, NULL);
3579}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003580EXPORT_SYMBOL(blk_mq_init_queue);
3581
Christoph Hellwig4dcc4872021-08-16 15:19:05 +02003582struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set, void *queuedata,
3583 struct lock_class_key *lkclass)
Jens Axboe9316a9e2018-10-15 08:40:37 -06003584{
3585 struct request_queue *q;
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003586 struct gendisk *disk;
Jens Axboe9316a9e2018-10-15 08:40:37 -06003587
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003588 q = blk_mq_init_queue_data(set, queuedata);
3589 if (IS_ERR(q))
3590 return ERR_CAST(q);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003591
Christoph Hellwig4a1fa412021-08-16 15:19:08 +02003592 disk = __alloc_disk_node(q, set->numa_node, lkclass);
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003593 if (!disk) {
3594 blk_cleanup_queue(q);
3595 return ERR_PTR(-ENOMEM);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003596 }
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003597 return disk;
Jens Axboe9316a9e2018-10-15 08:40:37 -06003598}
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003599EXPORT_SYMBOL(__blk_mq_alloc_disk);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003600
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003601static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
3602 struct blk_mq_tag_set *set, struct request_queue *q,
3603 int hctx_idx, int node)
3604{
Ming Lei2f8f1332019-04-30 09:52:27 +08003605 struct blk_mq_hw_ctx *hctx = NULL, *tmp;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003606
Ming Lei2f8f1332019-04-30 09:52:27 +08003607 /* reuse dead hctx first */
3608 spin_lock(&q->unused_hctx_lock);
3609 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
3610 if (tmp->numa_node == node) {
3611 hctx = tmp;
3612 break;
3613 }
3614 }
3615 if (hctx)
3616 list_del_init(&hctx->hctx_list);
3617 spin_unlock(&q->unused_hctx_lock);
3618
3619 if (!hctx)
3620 hctx = blk_mq_alloc_hctx(q, set, node);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003621 if (!hctx)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003622 goto fail;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003623
Ming Lei7c6c5b72019-04-30 09:52:26 +08003624 if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
3625 goto free_hctx;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003626
3627 return hctx;
Ming Lei7c6c5b72019-04-30 09:52:26 +08003628
3629 free_hctx:
3630 kobject_put(&hctx->kobj);
3631 fail:
3632 return NULL;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003633}
3634
Keith Busch868f2f02015-12-17 17:08:14 -07003635static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
3636 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003637{
Jianchao Wange01ad462018-10-12 18:07:28 +08003638 int i, j, end;
Keith Busch868f2f02015-12-17 17:08:14 -07003639 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01003640
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003641 if (q->nr_hw_queues < set->nr_hw_queues) {
3642 struct blk_mq_hw_ctx **new_hctxs;
3643
3644 new_hctxs = kcalloc_node(set->nr_hw_queues,
3645 sizeof(*new_hctxs), GFP_KERNEL,
3646 set->numa_node);
3647 if (!new_hctxs)
3648 return;
3649 if (hctxs)
3650 memcpy(new_hctxs, hctxs, q->nr_hw_queues *
3651 sizeof(*hctxs));
3652 q->queue_hw_ctx = new_hctxs;
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003653 kfree(hctxs);
3654 hctxs = new_hctxs;
3655 }
3656
Ming Leifb350e02018-01-06 16:27:40 +08003657 /* protect against switching io scheduler */
3658 mutex_lock(&q->sysfs_lock);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003659 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003660 int node;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003661 struct blk_mq_hw_ctx *hctx;
Keith Busch868f2f02015-12-17 17:08:14 -07003662
Dongli Zhang7d76f852019-02-27 21:35:01 +08003663 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003664 /*
3665 * If the hw queue has been mapped to another numa node,
3666 * we need to realloc the hctx. If allocation fails, fallback
3667 * to use the previous one.
3668 */
3669 if (hctxs[i] && (hctxs[i]->numa_node == node))
3670 continue;
Jens Axboe320ae512013-10-24 09:20:05 +01003671
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003672 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
3673 if (hctx) {
Ming Lei2f8f1332019-04-30 09:52:27 +08003674 if (hctxs[i])
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003675 blk_mq_exit_hctx(q, set, hctxs[i], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003676 hctxs[i] = hctx;
3677 } else {
3678 if (hctxs[i])
3679 pr_warn("Allocate new hctx on node %d fails,\
3680 fallback to previous one on node %d\n",
3681 node, hctxs[i]->numa_node);
3682 else
3683 break;
Keith Busch868f2f02015-12-17 17:08:14 -07003684 }
Jens Axboe320ae512013-10-24 09:20:05 +01003685 }
Jianchao Wange01ad462018-10-12 18:07:28 +08003686 /*
3687 * Increasing nr_hw_queues fails. Free the newly allocated
3688 * hctxs and keep the previous q->nr_hw_queues.
3689 */
3690 if (i != set->nr_hw_queues) {
3691 j = q->nr_hw_queues;
3692 end = i;
3693 } else {
3694 j = i;
3695 end = q->nr_hw_queues;
3696 q->nr_hw_queues = set->nr_hw_queues;
3697 }
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003698
Jianchao Wange01ad462018-10-12 18:07:28 +08003699 for (; j < end; j++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003700 struct blk_mq_hw_ctx *hctx = hctxs[j];
3701
3702 if (hctx) {
Keith Busch868f2f02015-12-17 17:08:14 -07003703 blk_mq_exit_hctx(q, set, hctx, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003704 hctxs[j] = NULL;
Keith Busch868f2f02015-12-17 17:08:14 -07003705 }
3706 }
Ming Leifb350e02018-01-06 16:27:40 +08003707 mutex_unlock(&q->sysfs_lock);
Keith Busch868f2f02015-12-17 17:08:14 -07003708}
3709
Christoph Hellwig26a97502021-06-02 09:53:17 +03003710int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
3711 struct request_queue *q)
Keith Busch868f2f02015-12-17 17:08:14 -07003712{
Ming Lei66841672016-02-12 15:27:00 +08003713 /* mark the queue as mq asap */
3714 q->mq_ops = set->ops;
3715
Omar Sandoval34dbad52017-03-21 08:56:08 -07003716 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
Stephen Bates720b8cc2017-04-07 06:24:03 -06003717 blk_mq_poll_stats_bkt,
3718 BLK_MQ_POLL_STATS_BKTS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -07003719 if (!q->poll_cb)
3720 goto err_exit;
3721
Ming Lei1db49092018-11-20 09:44:35 +08003722 if (blk_mq_alloc_ctxs(q))
Jes Sorensen41de54c2019-04-19 16:35:44 -04003723 goto err_poll;
Keith Busch868f2f02015-12-17 17:08:14 -07003724
Ming Lei737f98c2017-02-22 18:13:59 +08003725 /* init q->mq_kobj and sw queues' kobjects */
3726 blk_mq_sysfs_init(q);
3727
Ming Lei2f8f1332019-04-30 09:52:27 +08003728 INIT_LIST_HEAD(&q->unused_hctx_list);
3729 spin_lock_init(&q->unused_hctx_lock);
3730
Keith Busch868f2f02015-12-17 17:08:14 -07003731 blk_mq_realloc_hw_ctxs(set, q);
3732 if (!q->nr_hw_queues)
3733 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01003734
Christoph Hellwig287922e2015-10-30 20:57:30 +08003735 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08003736 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01003737
Jens Axboea8908932018-10-16 14:23:06 -06003738 q->tag_set = set;
Jens Axboe320ae512013-10-24 09:20:05 +01003739
Jens Axboe94eddfb2013-11-19 09:25:07 -07003740 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Ming Leicd191812018-12-18 12:15:29 +08003741 if (set->nr_maps > HCTX_TYPE_POLL &&
3742 set->map[HCTX_TYPE_POLL].nr_queues)
Christoph Hellwig6544d222018-12-02 17:46:28 +01003743 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
Jens Axboe320ae512013-10-24 09:20:05 +01003744
Mike Snitzer28494502016-09-14 13:28:30 -04003745 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06003746 INIT_LIST_HEAD(&q->requeue_list);
3747 spin_lock_init(&q->requeue_lock);
3748
Jens Axboeeba71762014-05-20 15:17:27 -06003749 q->nr_requests = set->queue_depth;
3750
Jens Axboe64f1c212016-11-14 13:03:03 -07003751 /*
3752 * Default to classic polling
3753 */
Yufen Yu29ece8b2019-03-18 22:44:41 +08003754 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
Jens Axboe64f1c212016-11-14 13:03:03 -07003755
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003756 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003757 blk_mq_add_queue_tag_set(set, q);
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003758 blk_mq_map_swqueue(q);
Christoph Hellwig26a97502021-06-02 09:53:17 +03003759 return 0;
Christoph Hellwig18741982014-02-10 09:29:00 -07003760
Jens Axboe320ae512013-10-24 09:20:05 +01003761err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07003762 kfree(q->queue_hw_ctx);
zhengbin73d9c8d2019-07-23 22:10:42 +08003763 q->nr_hw_queues = 0;
Ming Lei1db49092018-11-20 09:44:35 +08003764 blk_mq_sysfs_deinit(q);
Jes Sorensen41de54c2019-04-19 16:35:44 -04003765err_poll:
3766 blk_stat_free_callback(q->poll_cb);
3767 q->poll_cb = NULL;
Ming Linc7de5722016-05-25 23:23:27 -07003768err_exit:
3769 q->mq_ops = NULL;
Christoph Hellwig26a97502021-06-02 09:53:17 +03003770 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01003771}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003772EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01003773
Ming Leic7e2d942019-04-30 09:52:25 +08003774/* tags can _not_ be used after returning from blk_mq_exit_queue */
3775void blk_mq_exit_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01003776{
Bart Van Assche630ef622021-05-13 10:15:29 -07003777 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01003778
Bart Van Assche630ef622021-05-13 10:15:29 -07003779 /* Checks hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED. */
Ming Lei624dbe42014-05-27 23:35:13 +08003780 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
Bart Van Assche630ef622021-05-13 10:15:29 -07003781 /* May clear BLK_MQ_F_TAG_QUEUE_SHARED in hctx->flags. */
3782 blk_mq_del_queue_tag_set(q);
Jens Axboe320ae512013-10-24 09:20:05 +01003783}
Jens Axboe320ae512013-10-24 09:20:05 +01003784
Jens Axboea5164402014-09-10 09:02:03 -06003785static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
3786{
3787 int i;
3788
John Garry079a2e32021-10-05 18:23:39 +08003789 if (blk_mq_is_shared_tags(set->flags)) {
3790 set->shared_tags = blk_mq_alloc_map_and_rqs(set,
John Garrye155b0c2021-10-05 18:23:37 +08003791 BLK_MQ_NO_HCTX_IDX,
3792 set->queue_depth);
John Garry079a2e32021-10-05 18:23:39 +08003793 if (!set->shared_tags)
John Garrye155b0c2021-10-05 18:23:37 +08003794 return -ENOMEM;
3795 }
3796
Xianting Tian8229cca2020-09-26 10:39:47 +08003797 for (i = 0; i < set->nr_hw_queues; i++) {
John Garry63064be2021-10-05 18:23:35 +08003798 if (!__blk_mq_alloc_map_and_rqs(set, i))
Jens Axboea5164402014-09-10 09:02:03 -06003799 goto out_unwind;
Xianting Tian8229cca2020-09-26 10:39:47 +08003800 cond_resched();
3801 }
Jens Axboea5164402014-09-10 09:02:03 -06003802
3803 return 0;
3804
3805out_unwind:
3806 while (--i >= 0)
John Garrye155b0c2021-10-05 18:23:37 +08003807 __blk_mq_free_map_and_rqs(set, i);
3808
John Garry079a2e32021-10-05 18:23:39 +08003809 if (blk_mq_is_shared_tags(set->flags)) {
3810 blk_mq_free_map_and_rqs(set, set->shared_tags,
John Garrye155b0c2021-10-05 18:23:37 +08003811 BLK_MQ_NO_HCTX_IDX);
John Garry645db342021-10-05 18:23:36 +08003812 }
Jens Axboea5164402014-09-10 09:02:03 -06003813
Jens Axboea5164402014-09-10 09:02:03 -06003814 return -ENOMEM;
3815}
3816
3817/*
3818 * Allocate the request maps associated with this tag_set. Note that this
3819 * may reduce the depth asked for, if memory is tight. set->queue_depth
3820 * will be updated to reflect the allocated depth.
3821 */
John Garry63064be2021-10-05 18:23:35 +08003822static int blk_mq_alloc_set_map_and_rqs(struct blk_mq_tag_set *set)
Jens Axboea5164402014-09-10 09:02:03 -06003823{
3824 unsigned int depth;
3825 int err;
3826
3827 depth = set->queue_depth;
3828 do {
3829 err = __blk_mq_alloc_rq_maps(set);
3830 if (!err)
3831 break;
3832
3833 set->queue_depth >>= 1;
3834 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
3835 err = -ENOMEM;
3836 break;
3837 }
3838 } while (set->queue_depth);
3839
3840 if (!set->queue_depth || err) {
3841 pr_err("blk-mq: failed to allocate request map\n");
3842 return -ENOMEM;
3843 }
3844
3845 if (depth != set->queue_depth)
3846 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
3847 depth, set->queue_depth);
3848
3849 return 0;
3850}
3851
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003852static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
3853{
Bart Van Assche6e66b492020-03-09 21:26:17 -07003854 /*
3855 * blk_mq_map_queues() and multiple .map_queues() implementations
3856 * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the
3857 * number of hardware queues.
3858 */
3859 if (set->nr_maps == 1)
3860 set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues;
3861
Ming Lei59388702018-12-07 11:03:53 +08003862 if (set->ops->map_queues && !is_kdump_kernel()) {
Jens Axboeb3c661b2018-10-30 10:36:06 -06003863 int i;
3864
Ming Lei7d4901a2018-01-06 16:27:39 +08003865 /*
3866 * transport .map_queues is usually done in the following
3867 * way:
3868 *
3869 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
3870 * mask = get_cpu_mask(queue)
3871 * for_each_cpu(cpu, mask)
Jens Axboeb3c661b2018-10-30 10:36:06 -06003872 * set->map[x].mq_map[cpu] = queue;
Ming Lei7d4901a2018-01-06 16:27:39 +08003873 * }
3874 *
3875 * When we need to remap, the table has to be cleared for
3876 * killing stale mapping since one CPU may not be mapped
3877 * to any hw queue.
3878 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06003879 for (i = 0; i < set->nr_maps; i++)
3880 blk_mq_clear_mq_map(&set->map[i]);
Ming Lei7d4901a2018-01-06 16:27:39 +08003881
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003882 return set->ops->map_queues(set);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003883 } else {
3884 BUG_ON(set->nr_maps > 1);
Dongli Zhang7d76f852019-02-27 21:35:01 +08003885 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003886 }
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003887}
3888
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003889static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set,
3890 int cur_nr_hw_queues, int new_nr_hw_queues)
3891{
3892 struct blk_mq_tags **new_tags;
3893
3894 if (cur_nr_hw_queues >= new_nr_hw_queues)
3895 return 0;
3896
3897 new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *),
3898 GFP_KERNEL, set->numa_node);
3899 if (!new_tags)
3900 return -ENOMEM;
3901
3902 if (set->tags)
3903 memcpy(new_tags, set->tags, cur_nr_hw_queues *
3904 sizeof(*set->tags));
3905 kfree(set->tags);
3906 set->tags = new_tags;
3907 set->nr_hw_queues = new_nr_hw_queues;
3908
3909 return 0;
3910}
3911
Minwoo Im91cdf262020-12-05 00:20:53 +09003912static int blk_mq_alloc_tag_set_tags(struct blk_mq_tag_set *set,
3913 int new_nr_hw_queues)
3914{
3915 return blk_mq_realloc_tag_set_tags(set, 0, new_nr_hw_queues);
3916}
3917
Jens Axboea4391c62014-06-05 15:21:56 -06003918/*
3919 * Alloc a tag set to be associated with one or more request queues.
3920 * May fail with EINVAL for various error conditions. May adjust the
Minwoo Imc018c842018-06-30 22:12:41 +09003921 * requested depth down, if it's too large. In that case, the set
Jens Axboea4391c62014-06-05 15:21:56 -06003922 * value will be stored in set->queue_depth.
3923 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003924int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
3925{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003926 int i, ret;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003927
Bart Van Assche205fb5f2014-10-30 14:45:11 +01003928 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
3929
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003930 if (!set->nr_hw_queues)
3931 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06003932 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003933 return -EINVAL;
3934 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
3935 return -EINVAL;
3936
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02003937 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003938 return -EINVAL;
3939
Ming Leide148292017-10-14 17:22:29 +08003940 if (!set->ops->get_budget ^ !set->ops->put_budget)
3941 return -EINVAL;
3942
Jens Axboea4391c62014-06-05 15:21:56 -06003943 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
3944 pr_info("blk-mq: reduced tag depth to %u\n",
3945 BLK_MQ_MAX_DEPTH);
3946 set->queue_depth = BLK_MQ_MAX_DEPTH;
3947 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003948
Jens Axboeb3c661b2018-10-30 10:36:06 -06003949 if (!set->nr_maps)
3950 set->nr_maps = 1;
3951 else if (set->nr_maps > HCTX_MAX_TYPES)
3952 return -EINVAL;
3953
Shaohua Li6637fad2014-11-30 16:00:58 -08003954 /*
3955 * If a crashdump is active, then we are potentially in a very
3956 * memory constrained environment. Limit us to 1 queue and
3957 * 64 tags to prevent using too much memory.
3958 */
3959 if (is_kdump_kernel()) {
3960 set->nr_hw_queues = 1;
Ming Lei59388702018-12-07 11:03:53 +08003961 set->nr_maps = 1;
Shaohua Li6637fad2014-11-30 16:00:58 -08003962 set->queue_depth = min(64U, set->queue_depth);
3963 }
Keith Busch868f2f02015-12-17 17:08:14 -07003964 /*
Jens Axboe392546a2018-10-29 13:25:27 -06003965 * There is no use for more h/w queues than cpus if we just have
3966 * a single map
Keith Busch868f2f02015-12-17 17:08:14 -07003967 */
Jens Axboe392546a2018-10-29 13:25:27 -06003968 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003969 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08003970
Minwoo Im91cdf262020-12-05 00:20:53 +09003971 if (blk_mq_alloc_tag_set_tags(set, set->nr_hw_queues) < 0)
Jens Axboea5164402014-09-10 09:02:03 -06003972 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003973
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003974 ret = -ENOMEM;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003975 for (i = 0; i < set->nr_maps; i++) {
3976 set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
Ming Lei07b35eb2018-12-17 18:42:45 +08003977 sizeof(set->map[i].mq_map[0]),
Jens Axboeb3c661b2018-10-30 10:36:06 -06003978 GFP_KERNEL, set->numa_node);
3979 if (!set->map[i].mq_map)
3980 goto out_free_mq_map;
Ming Lei59388702018-12-07 11:03:53 +08003981 set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003982 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003983
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003984 ret = blk_mq_update_queue_map(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003985 if (ret)
3986 goto out_free_mq_map;
3987
John Garry63064be2021-10-05 18:23:35 +08003988 ret = blk_mq_alloc_set_map_and_rqs(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003989 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003990 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003991
Jens Axboe0d2602c2014-05-13 15:10:52 -06003992 mutex_init(&set->tag_list_lock);
3993 INIT_LIST_HEAD(&set->tag_list);
3994
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003995 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003996
3997out_free_mq_map:
Jens Axboeb3c661b2018-10-30 10:36:06 -06003998 for (i = 0; i < set->nr_maps; i++) {
3999 kfree(set->map[i].mq_map);
4000 set->map[i].mq_map = NULL;
4001 }
Robert Elliott5676e7b2014-09-02 11:38:44 -05004002 kfree(set->tags);
4003 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02004004 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004005}
4006EXPORT_SYMBOL(blk_mq_alloc_tag_set);
4007
Christoph Hellwigcdb14e02021-06-02 09:53:16 +03004008/* allocate and initialize a tagset for a simple single-queue device */
4009int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set,
4010 const struct blk_mq_ops *ops, unsigned int queue_depth,
4011 unsigned int set_flags)
4012{
4013 memset(set, 0, sizeof(*set));
4014 set->ops = ops;
4015 set->nr_hw_queues = 1;
4016 set->nr_maps = 1;
4017 set->queue_depth = queue_depth;
4018 set->numa_node = NUMA_NO_NODE;
4019 set->flags = set_flags;
4020 return blk_mq_alloc_tag_set(set);
4021}
4022EXPORT_SYMBOL_GPL(blk_mq_alloc_sq_tag_set);
4023
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004024void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
4025{
Jens Axboeb3c661b2018-10-30 10:36:06 -06004026 int i, j;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004027
Bart Van Asschef7e76db2019-10-25 09:50:10 -07004028 for (i = 0; i < set->nr_hw_queues; i++)
John Garrye155b0c2021-10-05 18:23:37 +08004029 __blk_mq_free_map_and_rqs(set, i);
Jens Axboe484b4062014-05-21 14:01:15 -06004030
John Garry079a2e32021-10-05 18:23:39 +08004031 if (blk_mq_is_shared_tags(set->flags)) {
4032 blk_mq_free_map_and_rqs(set, set->shared_tags,
John Garrye155b0c2021-10-05 18:23:37 +08004033 BLK_MQ_NO_HCTX_IDX);
4034 }
John Garry32bc15a2020-08-19 23:20:24 +08004035
Jens Axboeb3c661b2018-10-30 10:36:06 -06004036 for (j = 0; j < set->nr_maps; j++) {
4037 kfree(set->map[j].mq_map);
4038 set->map[j].mq_map = NULL;
4039 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02004040
Ming Lei981bd182014-04-24 00:07:34 +08004041 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05004042 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004043}
4044EXPORT_SYMBOL(blk_mq_free_tag_set);
4045
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004046int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
4047{
4048 struct blk_mq_tag_set *set = q->tag_set;
4049 struct blk_mq_hw_ctx *hctx;
4050 int i, ret;
4051
Jens Axboebd166ef2017-01-17 06:03:22 -07004052 if (!set)
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004053 return -EINVAL;
4054
Aleksei Zakharove5fa8142019-02-08 19:14:05 +03004055 if (q->nr_requests == nr)
4056 return 0;
4057
Jens Axboe70f36b62017-01-19 10:59:07 -07004058 blk_mq_freeze_queue(q);
Ming Lei24f5a902018-01-06 16:27:38 +08004059 blk_mq_quiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07004060
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004061 ret = 0;
4062 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07004063 if (!hctx->tags)
4064 continue;
Jens Axboebd166ef2017-01-17 06:03:22 -07004065 /*
4066 * If we're using an MQ scheduler, just update the scheduler
4067 * queue depth. This is similar to what the old code would do.
4068 */
John Garryf6adcef2021-10-05 18:23:29 +08004069 if (hctx->sched_tags) {
Jens Axboe70f36b62017-01-19 10:59:07 -07004070 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
John Garryf6adcef2021-10-05 18:23:29 +08004071 nr, true);
John Garryf6adcef2021-10-05 18:23:29 +08004072 } else {
4073 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
4074 false);
Jens Axboe70f36b62017-01-19 10:59:07 -07004075 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004076 if (ret)
4077 break;
Jens Axboe77f1e0a2019-01-18 10:34:16 -07004078 if (q->elevator && q->elevator->type->ops.depth_updated)
4079 q->elevator->type->ops.depth_updated(hctx);
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004080 }
John Garryd97e5942021-05-13 20:00:58 +08004081 if (!ret) {
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004082 q->nr_requests = nr;
John Garry079a2e32021-10-05 18:23:39 +08004083 if (blk_mq_is_shared_tags(set->flags)) {
John Garry8fa04462021-10-05 18:23:28 +08004084 if (q->elevator)
John Garry079a2e32021-10-05 18:23:39 +08004085 blk_mq_tag_update_sched_shared_tags(q);
John Garry8fa04462021-10-05 18:23:28 +08004086 else
John Garry079a2e32021-10-05 18:23:39 +08004087 blk_mq_tag_resize_shared_tags(set, nr);
John Garry8fa04462021-10-05 18:23:28 +08004088 }
John Garryd97e5942021-05-13 20:00:58 +08004089 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004090
Ming Lei24f5a902018-01-06 16:27:38 +08004091 blk_mq_unquiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07004092 blk_mq_unfreeze_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07004093
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004094 return ret;
4095}
4096
Jianchao Wangd48ece22018-08-21 15:15:03 +08004097/*
4098 * request_queue and elevator_type pair.
4099 * It is just used by __blk_mq_update_nr_hw_queues to cache
4100 * the elevator_type associated with a request_queue.
4101 */
4102struct blk_mq_qe_pair {
4103 struct list_head node;
4104 struct request_queue *q;
4105 struct elevator_type *type;
4106};
4107
4108/*
4109 * Cache the elevator_type in qe pair list and switch the
4110 * io scheduler to 'none'
4111 */
4112static bool blk_mq_elv_switch_none(struct list_head *head,
4113 struct request_queue *q)
4114{
4115 struct blk_mq_qe_pair *qe;
4116
4117 if (!q->elevator)
4118 return true;
4119
4120 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
4121 if (!qe)
4122 return false;
4123
4124 INIT_LIST_HEAD(&qe->node);
4125 qe->q = q;
4126 qe->type = q->elevator->type;
4127 list_add(&qe->node, head);
4128
4129 mutex_lock(&q->sysfs_lock);
4130 /*
4131 * After elevator_switch_mq, the previous elevator_queue will be
4132 * released by elevator_release. The reference of the io scheduler
4133 * module get by elevator_get will also be put. So we need to get
4134 * a reference of the io scheduler module here to prevent it to be
4135 * removed.
4136 */
4137 __module_get(qe->type->elevator_owner);
4138 elevator_switch_mq(q, NULL);
4139 mutex_unlock(&q->sysfs_lock);
4140
4141 return true;
4142}
4143
4144static void blk_mq_elv_switch_back(struct list_head *head,
4145 struct request_queue *q)
4146{
4147 struct blk_mq_qe_pair *qe;
4148 struct elevator_type *t = NULL;
4149
4150 list_for_each_entry(qe, head, node)
4151 if (qe->q == q) {
4152 t = qe->type;
4153 break;
4154 }
4155
4156 if (!t)
4157 return;
4158
4159 list_del(&qe->node);
4160 kfree(qe);
4161
4162 mutex_lock(&q->sysfs_lock);
4163 elevator_switch_mq(q, t);
4164 mutex_unlock(&q->sysfs_lock);
4165}
4166
Keith Busche4dc2b32017-05-30 14:39:11 -04004167static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
4168 int nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07004169{
4170 struct request_queue *q;
Jianchao Wangd48ece22018-08-21 15:15:03 +08004171 LIST_HEAD(head);
Jianchao Wange01ad462018-10-12 18:07:28 +08004172 int prev_nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07004173
Bart Van Assche705cda92017-04-07 11:16:49 -07004174 lockdep_assert_held(&set->tag_list_lock);
4175
Jens Axboe392546a2018-10-29 13:25:27 -06004176 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07004177 nr_hw_queues = nr_cpu_ids;
Weiping Zhangfe35ec52020-06-17 14:18:37 +08004178 if (nr_hw_queues < 1)
4179 return;
4180 if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07004181 return;
4182
4183 list_for_each_entry(q, &set->tag_list, tag_set_list)
4184 blk_mq_freeze_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +08004185 /*
4186 * Switch IO scheduler to 'none', cleaning up the data associated
4187 * with the previous scheduler. We will switch back once we are done
4188 * updating the new sw to hw queue mappings.
4189 */
4190 list_for_each_entry(q, &set->tag_list, tag_set_list)
4191 if (!blk_mq_elv_switch_none(&head, q))
4192 goto switch_back;
Keith Busch868f2f02015-12-17 17:08:14 -07004193
Jianchao Wang477e19d2018-10-12 18:07:25 +08004194 list_for_each_entry(q, &set->tag_list, tag_set_list) {
4195 blk_mq_debugfs_unregister_hctxs(q);
4196 blk_mq_sysfs_unregister(q);
4197 }
4198
Weiping Zhanga2584e42020-05-07 21:03:56 +08004199 prev_nr_hw_queues = set->nr_hw_queues;
Bart Van Asschef7e76db2019-10-25 09:50:10 -07004200 if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) <
4201 0)
4202 goto reregister;
4203
Keith Busch868f2f02015-12-17 17:08:14 -07004204 set->nr_hw_queues = nr_hw_queues;
Jianchao Wange01ad462018-10-12 18:07:28 +08004205fallback:
Weiping Zhangaa880ad2020-05-13 08:44:05 +08004206 blk_mq_update_queue_map(set);
Keith Busch868f2f02015-12-17 17:08:14 -07004207 list_for_each_entry(q, &set->tag_list, tag_set_list) {
4208 blk_mq_realloc_hw_ctxs(set, q);
Jianchao Wange01ad462018-10-12 18:07:28 +08004209 if (q->nr_hw_queues != set->nr_hw_queues) {
Ye Bina846a8e2021-11-08 15:40:19 +08004210 int i = prev_nr_hw_queues;
4211
Jianchao Wange01ad462018-10-12 18:07:28 +08004212 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
4213 nr_hw_queues, prev_nr_hw_queues);
Ye Bina846a8e2021-11-08 15:40:19 +08004214 for (; i < set->nr_hw_queues; i++)
4215 __blk_mq_free_map_and_rqs(set, i);
4216
Jianchao Wange01ad462018-10-12 18:07:28 +08004217 set->nr_hw_queues = prev_nr_hw_queues;
Dongli Zhang7d76f852019-02-27 21:35:01 +08004218 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jianchao Wange01ad462018-10-12 18:07:28 +08004219 goto fallback;
4220 }
Jianchao Wang477e19d2018-10-12 18:07:25 +08004221 blk_mq_map_swqueue(q);
4222 }
4223
Bart Van Asschef7e76db2019-10-25 09:50:10 -07004224reregister:
Jianchao Wang477e19d2018-10-12 18:07:25 +08004225 list_for_each_entry(q, &set->tag_list, tag_set_list) {
4226 blk_mq_sysfs_register(q);
4227 blk_mq_debugfs_register_hctxs(q);
Keith Busch868f2f02015-12-17 17:08:14 -07004228 }
4229
Jianchao Wangd48ece22018-08-21 15:15:03 +08004230switch_back:
4231 list_for_each_entry(q, &set->tag_list, tag_set_list)
4232 blk_mq_elv_switch_back(&head, q);
4233
Keith Busch868f2f02015-12-17 17:08:14 -07004234 list_for_each_entry(q, &set->tag_list, tag_set_list)
4235 blk_mq_unfreeze_queue(q);
4236}
Keith Busche4dc2b32017-05-30 14:39:11 -04004237
4238void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
4239{
4240 mutex_lock(&set->tag_list_lock);
4241 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
4242 mutex_unlock(&set->tag_list_lock);
4243}
Keith Busch868f2f02015-12-17 17:08:14 -07004244EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
4245
Omar Sandoval34dbad52017-03-21 08:56:08 -07004246/* Enable polling stats and return whether they were already enabled. */
4247static bool blk_poll_stats_enable(struct request_queue *q)
4248{
4249 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
Bart Van Assche7dfdbc72018-03-07 17:10:05 -08004250 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
Omar Sandoval34dbad52017-03-21 08:56:08 -07004251 return true;
4252 blk_stat_add_callback(q, q->poll_cb);
4253 return false;
4254}
4255
4256static void blk_mq_poll_stats_start(struct request_queue *q)
4257{
4258 /*
4259 * We don't arm the callback if polling stats are not enabled or the
4260 * callback is already active.
4261 */
4262 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
4263 blk_stat_is_active(q->poll_cb))
4264 return;
4265
4266 blk_stat_activate_msecs(q->poll_cb, 100);
4267}
4268
4269static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
4270{
4271 struct request_queue *q = cb->data;
Stephen Bates720b8cc2017-04-07 06:24:03 -06004272 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -07004273
Stephen Bates720b8cc2017-04-07 06:24:03 -06004274 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
4275 if (cb->stat[bucket].nr_samples)
4276 q->poll_stat[bucket] = cb->stat[bucket];
4277 }
Omar Sandoval34dbad52017-03-21 08:56:08 -07004278}
4279
Jens Axboe64f1c212016-11-14 13:03:03 -07004280static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
Jens Axboe64f1c212016-11-14 13:03:03 -07004281 struct request *rq)
4282{
Jens Axboe64f1c212016-11-14 13:03:03 -07004283 unsigned long ret = 0;
Stephen Bates720b8cc2017-04-07 06:24:03 -06004284 int bucket;
Jens Axboe64f1c212016-11-14 13:03:03 -07004285
4286 /*
4287 * If stats collection isn't on, don't sleep but turn it on for
4288 * future users
4289 */
Omar Sandoval34dbad52017-03-21 08:56:08 -07004290 if (!blk_poll_stats_enable(q))
Jens Axboe64f1c212016-11-14 13:03:03 -07004291 return 0;
4292
4293 /*
Jens Axboe64f1c212016-11-14 13:03:03 -07004294 * As an optimistic guess, use half of the mean service time
4295 * for this type of request. We can (and should) make this smarter.
4296 * For instance, if the completion latencies are tight, we can
4297 * get closer than just half the mean. This is especially
4298 * important on devices where the completion latencies are longer
Stephen Bates720b8cc2017-04-07 06:24:03 -06004299 * than ~10 usec. We do use the stats for the relevant IO size
4300 * if available which does lead to better estimates.
Jens Axboe64f1c212016-11-14 13:03:03 -07004301 */
Stephen Bates720b8cc2017-04-07 06:24:03 -06004302 bucket = blk_mq_poll_stats_bkt(rq);
4303 if (bucket < 0)
4304 return ret;
4305
4306 if (q->poll_stat[bucket].nr_samples)
4307 ret = (q->poll_stat[bucket].mean + 1) / 2;
Jens Axboe64f1c212016-11-14 13:03:03 -07004308
4309 return ret;
4310}
4311
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004312static bool blk_mq_poll_hybrid(struct request_queue *q, blk_qc_t qc)
Jens Axboe06426ad2016-11-14 13:01:59 -07004313{
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004314 struct blk_mq_hw_ctx *hctx = blk_qc_to_hctx(q, qc);
4315 struct request *rq = blk_qc_to_rq(hctx, qc);
Jens Axboe06426ad2016-11-14 13:01:59 -07004316 struct hrtimer_sleeper hs;
4317 enum hrtimer_mode mode;
Jens Axboe64f1c212016-11-14 13:03:03 -07004318 unsigned int nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07004319 ktime_t kt;
4320
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004321 /*
4322 * If a request has completed on queue that uses an I/O scheduler, we
4323 * won't get back a request from blk_qc_to_rq.
4324 */
4325 if (!rq || (rq->rq_flags & RQF_MQ_POLL_SLEPT))
Jens Axboe64f1c212016-11-14 13:03:03 -07004326 return false;
4327
4328 /*
Jens Axboe1052b8a2018-11-26 08:21:49 -07004329 * If we get here, hybrid polling is enabled. Hence poll_nsec can be:
Jens Axboe64f1c212016-11-14 13:03:03 -07004330 *
Jens Axboe64f1c212016-11-14 13:03:03 -07004331 * 0: use half of prev avg
4332 * >0: use this specific value
4333 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07004334 if (q->poll_nsec > 0)
Jens Axboe64f1c212016-11-14 13:03:03 -07004335 nsecs = q->poll_nsec;
4336 else
John Garrycae740a2020-02-26 20:10:15 +08004337 nsecs = blk_mq_poll_nsecs(q, rq);
Jens Axboe64f1c212016-11-14 13:03:03 -07004338
4339 if (!nsecs)
Jens Axboe06426ad2016-11-14 13:01:59 -07004340 return false;
4341
Jens Axboe76a86f92018-01-10 11:30:56 -07004342 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
Jens Axboe06426ad2016-11-14 13:01:59 -07004343
4344 /*
4345 * This will be replaced with the stats tracking code, using
4346 * 'avg_completion_time / 2' as the pre-sleep target.
4347 */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01004348 kt = nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07004349
4350 mode = HRTIMER_MODE_REL;
Sebastian Andrzej Siewiordbc16252019-07-26 20:30:50 +02004351 hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07004352 hrtimer_set_expires(&hs.timer, kt);
4353
Jens Axboe06426ad2016-11-14 13:01:59 -07004354 do {
Tejun Heo5a61c362018-01-09 08:29:52 -08004355 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
Jens Axboe06426ad2016-11-14 13:01:59 -07004356 break;
4357 set_current_state(TASK_UNINTERRUPTIBLE);
Thomas Gleixner9dd88132019-07-30 21:16:55 +02004358 hrtimer_sleeper_start_expires(&hs, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07004359 if (hs.task)
4360 io_schedule();
4361 hrtimer_cancel(&hs.timer);
4362 mode = HRTIMER_MODE_ABS;
4363 } while (hs.task && !signal_pending(current));
4364
4365 __set_current_state(TASK_RUNNING);
4366 destroy_hrtimer_on_stack(&hs.timer);
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004367
4368 /*
4369 * If we sleep, have the caller restart the poll loop to reset the
4370 * state. Like for the other success return cases, the caller is
4371 * responsible for checking if the IO completed. If the IO isn't
4372 * complete, we'll get called again and will go straight to the busy
4373 * poll loop.
4374 */
Jens Axboe06426ad2016-11-14 13:01:59 -07004375 return true;
4376}
4377
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004378static int blk_mq_poll_classic(struct request_queue *q, blk_qc_t cookie,
Jens Axboe5a72e892021-10-12 09:24:29 -06004379 struct io_comp_batch *iob, unsigned int flags)
Jens Axboebbd7bb72016-11-04 09:34:34 -06004380{
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004381 struct blk_mq_hw_ctx *hctx = blk_qc_to_hctx(q, cookie);
4382 long state = get_current_state();
4383 int ret;
Jens Axboe1052b8a2018-11-26 08:21:49 -07004384
Jens Axboeaa61bec2018-11-13 21:32:10 -07004385 do {
Jens Axboe5a72e892021-10-12 09:24:29 -06004386 ret = q->mq_ops->poll(hctx, iob);
Jens Axboebbd7bb72016-11-04 09:34:34 -06004387 if (ret > 0) {
Jens Axboe849a3702018-11-16 08:37:34 -07004388 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004389 return ret;
Jens Axboebbd7bb72016-11-04 09:34:34 -06004390 }
4391
4392 if (signal_pending_state(state, current))
Jens Axboe849a3702018-11-16 08:37:34 -07004393 __set_current_state(TASK_RUNNING);
Peter Zijlstrab03fbd42021-06-11 10:28:12 +02004394 if (task_is_running(current))
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004395 return 1;
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004396
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02004397 if (ret < 0 || (flags & BLK_POLL_ONESHOT))
Jens Axboebbd7bb72016-11-04 09:34:34 -06004398 break;
4399 cpu_relax();
Jens Axboeaa61bec2018-11-13 21:32:10 -07004400 } while (!need_resched());
Jens Axboebbd7bb72016-11-04 09:34:34 -06004401
Nitesh Shetty67b41102018-02-13 21:18:12 +05304402 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004403 return 0;
Jens Axboebbd7bb72016-11-04 09:34:34 -06004404}
Jens Axboebbd7bb72016-11-04 09:34:34 -06004405
Jens Axboe5a72e892021-10-12 09:24:29 -06004406int blk_mq_poll(struct request_queue *q, blk_qc_t cookie, struct io_comp_batch *iob,
4407 unsigned int flags)
Jens Axboe676141e2014-03-20 13:29:18 -06004408{
Christoph Hellwigd729cf92021-10-12 13:12:20 +02004409 if (!(flags & BLK_POLL_NOSLEEP) &&
4410 q->poll_nsec != BLK_MQ_POLL_CLASSIC) {
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004411 if (blk_mq_poll_hybrid(q, cookie))
Jens Axboe320ae512013-10-24 09:20:05 +01004412 return 1;
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004413 }
Jens Axboe5a72e892021-10-12 09:24:29 -06004414 return blk_mq_poll_classic(q, cookie, iob, flags);
Jens Axboe320ae512013-10-24 09:20:05 +01004415}
4416
Jens Axboe9cf2bab2018-10-31 17:01:22 -06004417unsigned int blk_mq_rq_cpu(struct request *rq)
4418{
4419 return rq->mq_ctx->cpu;
4420}
4421EXPORT_SYMBOL(blk_mq_rq_cpu);
4422
Ming Lei2a19b282021-11-16 09:43:43 +08004423void blk_mq_cancel_work_sync(struct request_queue *q)
4424{
4425 if (queue_is_mq(q)) {
4426 struct blk_mq_hw_ctx *hctx;
4427 int i;
4428
4429 cancel_delayed_work_sync(&q->requeue_work);
4430
4431 queue_for_each_hw_ctx(q, hctx, i)
4432 cancel_delayed_work_sync(&hctx->run_work);
4433 }
4434}
4435
Jens Axboe320ae512013-10-24 09:20:05 +01004436static int __init blk_mq_init(void)
4437{
Christoph Hellwigc3077b52020-06-11 08:44:41 +02004438 int i;
4439
4440 for_each_possible_cpu(i)
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +01004441 init_llist_head(&per_cpu(blk_cpu_done, i));
Christoph Hellwigc3077b52020-06-11 08:44:41 +02004442 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
4443
4444 cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
4445 "block/softirq:dead", NULL,
4446 blk_softirq_cpu_dead);
Jens Axboe320ae512013-10-24 09:20:05 +01004447 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
4448 blk_mq_hctx_notify_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02004449 cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online",
4450 blk_mq_hctx_notify_online,
4451 blk_mq_hctx_notify_offline);
Jens Axboe320ae512013-10-24 09:20:05 +01004452 return 0;
4453}
4454subsys_initcall(blk_mq_init);