blob: 101466ece4c4f56607a841c8dbd18913bc3063ad [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 Lei69e07c42017-06-06 23:22:07 +0800254 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
Bart Van Assche6a83e742016-11-02 10:09:51 -0600255 * @q: request queue.
256 *
257 * Note: this function does not prevent that the struct request end_io()
Ming Lei69e07c42017-06-06 23:22:07 +0800258 * callback function is invoked. Once this function is returned, we make
259 * sure no dispatch can happen until the queue is unquiesced via
260 * blk_mq_unquiesce_queue().
Bart Van Assche6a83e742016-11-02 10:09:51 -0600261 */
262void blk_mq_quiesce_queue(struct request_queue *q)
263{
264 struct blk_mq_hw_ctx *hctx;
265 unsigned int i;
266 bool rcu = false;
267
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800268 blk_mq_quiesce_queue_nowait(q);
Ming Leif4560ff2017-06-18 14:24:27 -0600269
Bart Van Assche6a83e742016-11-02 10:09:51 -0600270 queue_for_each_hw_ctx(q, hctx, i) {
271 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -0800272 synchronize_srcu(hctx->srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -0600273 else
274 rcu = true;
275 }
276 if (rcu)
277 synchronize_rcu();
278}
279EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
280
Ming Leie4e73912017-06-06 23:22:03 +0800281/*
282 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
283 * @q: request queue.
284 *
285 * This function recovers queue into the state before quiescing
286 * which is done by blk_mq_quiesce_queue.
287 */
288void blk_mq_unquiesce_queue(struct request_queue *q)
289{
Ming Leie70feb82021-10-14 16:17:10 +0800290 unsigned long flags;
291 bool run_queue = false;
292
293 spin_lock_irqsave(&q->queue_lock, flags);
294 if (WARN_ON_ONCE(q->quiesce_depth <= 0)) {
295 ;
296 } else if (!--q->quiesce_depth) {
297 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
298 run_queue = true;
299 }
300 spin_unlock_irqrestore(&q->queue_lock, flags);
Ming Leif4560ff2017-06-18 14:24:27 -0600301
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800302 /* dispatch requests which are inserted during quiescing */
Ming Leie70feb82021-10-14 16:17:10 +0800303 if (run_queue)
304 blk_mq_run_hw_queues(q, true);
Ming Leie4e73912017-06-06 23:22:03 +0800305}
306EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
307
Jens Axboeaed3ea92014-12-22 14:04:42 -0700308void blk_mq_wake_waiters(struct request_queue *q)
309{
310 struct blk_mq_hw_ctx *hctx;
311 unsigned int i;
312
313 queue_for_each_hw_ctx(q, hctx, i)
314 if (blk_mq_hw_queue_mapped(hctx))
315 blk_mq_tag_wakeup_all(hctx->tags, true);
316}
317
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200318static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200319 unsigned int tag, u64 alloc_time_ns)
Jens Axboe320ae512013-10-24 09:20:05 +0100320{
Pavel Begunkov605f7842021-10-18 21:37:28 +0100321 struct blk_mq_ctx *ctx = data->ctx;
322 struct blk_mq_hw_ctx *hctx = data->hctx;
323 struct request_queue *q = data->q;
324 struct elevator_queue *e = q->elevator;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200325 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
326 struct request *rq = tags->static_rqs[tag];
Pavel Begunkov12845902021-10-18 21:37:29 +0100327 unsigned int rq_flags = 0;
Bart Van Asschec3a148d2017-06-20 11:15:43 -0700328
Pavel Begunkov605f7842021-10-18 21:37:28 +0100329 if (e) {
Pavel Begunkov12845902021-10-18 21:37:29 +0100330 rq_flags = RQF_ELV;
Christoph Hellwig766473682020-05-29 15:53:12 +0200331 rq->tag = BLK_MQ_NO_TAG;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200332 rq->internal_tag = tag;
333 } else {
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200334 rq->tag = tag;
Christoph Hellwig766473682020-05-29 15:53:12 +0200335 rq->internal_tag = BLK_MQ_NO_TAG;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200336 }
337
Pavel Begunkov12845902021-10-18 21:37:29 +0100338 if (data->flags & BLK_MQ_REQ_PM)
339 rq_flags |= RQF_PM;
340 if (blk_queue_io_stat(q))
341 rq_flags |= RQF_IO_STAT;
342 rq->rq_flags = rq_flags;
343
Pavel Begunkov4f266f22021-10-18 21:37:27 +0100344 if (blk_mq_need_time_stamp(rq))
345 rq->start_time_ns = ktime_get_ns();
346 else
347 rq->start_time_ns = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200348 /* csd/requeue_work/fifo_time is initialized before use */
Pavel Begunkov605f7842021-10-18 21:37:28 +0100349 rq->q = q;
350 rq->mq_ctx = ctx;
351 rq->mq_hctx = hctx;
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200352 rq->cmd_flags = data->cmd_flags;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200353 rq->rq_disk = NULL;
354 rq->part = NULL;
Tejun Heo6f816b42019-08-28 15:05:57 -0700355#ifdef CONFIG_BLK_RQ_ALLOC_TIME
356 rq->alloc_time_ns = alloc_time_ns;
357#endif
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700358 rq->io_start_time_ns = 0;
Hou Tao3d244302019-05-21 15:59:03 +0800359 rq->stats_sectors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200360 rq->nr_phys_segments = 0;
361#if defined(CONFIG_BLK_DEV_INTEGRITY)
362 rq->nr_integrity_segments = 0;
363#endif
Jens Axboef6be4fb2014-06-06 11:03:48 -0600364 rq->timeout = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200365 rq->end_io = NULL;
366 rq->end_io_data = NULL;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200367
Pavel Begunkov4f266f22021-10-18 21:37:27 +0100368 blk_crypto_rq_set_defaults(rq);
369 INIT_LIST_HEAD(&rq->queuelist);
370 /* tag was already set */
371 WRITE_ONCE(rq->deadline, 0);
Keith Busch12f5b932018-05-29 15:52:28 +0200372 refcount_set(&rq->ref, 1);
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200373
Pavel Begunkov4f266f22021-10-18 21:37:27 +0100374 if (rq->rq_flags & RQF_ELV) {
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200375 struct elevator_queue *e = data->q->elevator;
376
377 rq->elv.icq = NULL;
Pavel Begunkov4f266f22021-10-18 21:37:27 +0100378 INIT_HLIST_NODE(&rq->hash);
379 RB_CLEAR_NODE(&rq->rb_node);
380
381 if (!op_is_flush(data->cmd_flags) &&
382 e->type->ops.prepare_request) {
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200383 if (e->type->icq_cache)
384 blk_mq_sched_assign_ioc(rq);
385
386 e->type->ops.prepare_request(rq);
387 rq->rq_flags |= RQF_ELVPRIV;
388 }
389 }
390
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200391 return rq;
Jens Axboe320ae512013-10-24 09:20:05 +0100392}
393
Jens Axboe349302d2021-10-09 13:10:39 -0600394static inline struct request *
395__blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data,
396 u64 alloc_time_ns)
397{
398 unsigned int tag, tag_offset;
399 struct request *rq;
400 unsigned long tags;
401 int i, nr = 0;
402
403 tags = blk_mq_get_tags(data, data->nr_tags, &tag_offset);
404 if (unlikely(!tags))
405 return NULL;
406
407 for (i = 0; tags; i++) {
408 if (!(tags & (1UL << i)))
409 continue;
410 tag = tag_offset + i;
411 tags &= ~(1UL << i);
412 rq = blk_mq_rq_ctx_init(data, tag, alloc_time_ns);
Jens Axboe013a7f92021-10-13 07:58:52 -0600413 rq_list_add(data->cached_rq, rq);
Jens Axboe349302d2021-10-09 13:10:39 -0600414 }
415 data->nr_tags -= nr;
416
Jens Axboe013a7f92021-10-13 07:58:52 -0600417 return rq_list_pop(data->cached_rq);
Jens Axboe349302d2021-10-09 13:10:39 -0600418}
419
Christoph Hellwigb90cfae2021-10-12 12:40:44 +0200420static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data)
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200421{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200422 struct request_queue *q = data->q;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200423 struct elevator_queue *e = q->elevator;
Tejun Heo6f816b42019-08-28 15:05:57 -0700424 u64 alloc_time_ns = 0;
Jens Axboe47c122e2021-10-06 06:34:11 -0600425 struct request *rq;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200426 unsigned int tag;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200427
Tejun Heo6f816b42019-08-28 15:05:57 -0700428 /* alloc_time includes depth and tag waits */
429 if (blk_queue_rq_alloc_time(q))
430 alloc_time_ns = ktime_get_ns();
431
Jens Axboef9afca42018-10-29 13:11:38 -0600432 if (data->cmd_flags & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500433 data->flags |= BLK_MQ_REQ_NOWAIT;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200434
435 if (e) {
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200436 /*
Lin Feng8d663f32021-04-15 11:39:20 +0800437 * Flush/passthrough requests are special and go directly to the
Jens Axboe17a51192018-05-09 13:28:50 -0600438 * dispatch list. Don't include reserved tags in the
439 * limiting, as it isn't useful.
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200440 */
Jens Axboef9afca42018-10-29 13:11:38 -0600441 if (!op_is_flush(data->cmd_flags) &&
Lin Feng8d663f32021-04-15 11:39:20 +0800442 !blk_op_is_passthrough(data->cmd_flags) &&
Jens Axboef9afca42018-10-29 13:11:38 -0600443 e->type->ops.limit_depth &&
Jens Axboe17a51192018-05-09 13:28:50 -0600444 !(data->flags & BLK_MQ_REQ_RESERVED))
Jens Axboef9afca42018-10-29 13:11:38 -0600445 e->type->ops.limit_depth(data->cmd_flags, data);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200446 }
447
Ming Leibf0beec2020-05-29 15:53:15 +0200448retry:
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200449 data->ctx = blk_mq_get_ctx(q);
450 data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx);
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200451 if (!e)
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200452 blk_mq_tag_busy(data->hctx);
453
Ming Leibf0beec2020-05-29 15:53:15 +0200454 /*
Jens Axboe349302d2021-10-09 13:10:39 -0600455 * Try batched alloc if we want more than 1 tag.
456 */
457 if (data->nr_tags > 1) {
458 rq = __blk_mq_alloc_requests_batch(data, alloc_time_ns);
459 if (rq)
460 return rq;
461 data->nr_tags = 1;
462 }
463
464 /*
Ming Leibf0beec2020-05-29 15:53:15 +0200465 * Waiting allocations only fail because of an inactive hctx. In that
466 * case just retry the hctx assignment and tag allocation as CPU hotplug
467 * should have migrated us to an online CPU by now.
468 */
Jens Axboe349302d2021-10-09 13:10:39 -0600469 tag = blk_mq_get_tag(data);
470 if (tag == BLK_MQ_NO_TAG) {
471 if (data->flags & BLK_MQ_REQ_NOWAIT)
472 return NULL;
473 /*
474 * Give up the CPU and sleep for a random short time to
475 * ensure that thread using a realtime scheduling class
476 * are migrated off the CPU, and thus off the hctx that
477 * is going away.
478 */
479 msleep(3);
480 goto retry;
481 }
Ming Leibf0beec2020-05-29 15:53:15 +0200482
Jens Axboe349302d2021-10-09 13:10:39 -0600483 return blk_mq_rq_ctx_init(data, tag, alloc_time_ns);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200484}
485
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700486struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800487 blk_mq_req_flags_t flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100488{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200489 struct blk_mq_alloc_data data = {
490 .q = q,
491 .flags = flags,
492 .cmd_flags = op,
Jens Axboe47c122e2021-10-06 06:34:11 -0600493 .nr_tags = 1,
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200494 };
Jens Axboebd166ef2017-01-17 06:03:22 -0700495 struct request *rq;
Joe Lawrencea492f072014-08-28 08:15:21 -0600496 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100497
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800498 ret = blk_queue_enter(q, flags);
Joe Lawrencea492f072014-08-28 08:15:21 -0600499 if (ret)
500 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100501
Christoph Hellwigb90cfae2021-10-12 12:40:44 +0200502 rq = __blk_mq_alloc_requests(&data);
Jens Axboebd166ef2017-01-17 06:03:22 -0700503 if (!rq)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200504 goto out_queue_exit;
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200505 rq->__data_len = 0;
506 rq->__sector = (sector_t) -1;
507 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100508 return rq;
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200509out_queue_exit:
510 blk_queue_exit(q);
511 return ERR_PTR(-EWOULDBLOCK);
Jens Axboe320ae512013-10-24 09:20:05 +0100512}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600513EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100514
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700515struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800516 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
Ming Lin1f5bd332016-06-13 16:45:21 +0200517{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200518 struct blk_mq_alloc_data data = {
519 .q = q,
520 .flags = flags,
521 .cmd_flags = op,
Jens Axboe47c122e2021-10-06 06:34:11 -0600522 .nr_tags = 1,
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200523 };
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200524 u64 alloc_time_ns = 0;
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800525 unsigned int cpu;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200526 unsigned int tag;
Ming Lin1f5bd332016-06-13 16:45:21 +0200527 int ret;
528
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200529 /* alloc_time includes depth and tag waits */
530 if (blk_queue_rq_alloc_time(q))
531 alloc_time_ns = ktime_get_ns();
532
Ming Lin1f5bd332016-06-13 16:45:21 +0200533 /*
534 * If the tag allocator sleeps we could get an allocation for a
535 * different hardware context. No need to complicate the low level
536 * allocator for this for the rare use case of a command tied to
537 * a specific queue.
538 */
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200539 if (WARN_ON_ONCE(!(flags & (BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED))))
Ming Lin1f5bd332016-06-13 16:45:21 +0200540 return ERR_PTR(-EINVAL);
541
542 if (hctx_idx >= q->nr_hw_queues)
543 return ERR_PTR(-EIO);
544
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800545 ret = blk_queue_enter(q, flags);
Ming Lin1f5bd332016-06-13 16:45:21 +0200546 if (ret)
547 return ERR_PTR(ret);
548
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600549 /*
550 * Check if the hardware context is actually mapped to anything.
551 * If not tell the caller that it should skip this queue.
552 */
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200553 ret = -EXDEV;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200554 data.hctx = q->queue_hw_ctx[hctx_idx];
555 if (!blk_mq_hw_queue_mapped(data.hctx))
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200556 goto out_queue_exit;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200557 cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
558 data.ctx = __blk_mq_get_ctx(q, cpu);
Ming Lin1f5bd332016-06-13 16:45:21 +0200559
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200560 if (!q->elevator)
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200561 blk_mq_tag_busy(data.hctx);
562
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200563 ret = -EWOULDBLOCK;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200564 tag = blk_mq_get_tag(&data);
565 if (tag == BLK_MQ_NO_TAG)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200566 goto out_queue_exit;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200567 return blk_mq_rq_ctx_init(&data, tag, alloc_time_ns);
568
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200569out_queue_exit:
570 blk_queue_exit(q);
571 return ERR_PTR(ret);
Ming Lin1f5bd332016-06-13 16:45:21 +0200572}
573EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
574
Keith Busch12f5b932018-05-29 15:52:28 +0200575static void __blk_mq_free_request(struct request *rq)
576{
577 struct request_queue *q = rq->q;
578 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600579 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Keith Busch12f5b932018-05-29 15:52:28 +0200580 const int sched_tag = rq->internal_tag;
581
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000582 blk_crypto_free_request(rq);
Bart Van Assche986d4132018-09-26 14:01:10 -0700583 blk_pm_mark_last_busy(rq);
Jens Axboeea4f9952018-10-29 15:06:13 -0600584 rq->mq_hctx = NULL;
Christoph Hellwig766473682020-05-29 15:53:12 +0200585 if (rq->tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800586 blk_mq_put_tag(hctx->tags, ctx, rq->tag);
Christoph Hellwig766473682020-05-29 15:53:12 +0200587 if (sched_tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800588 blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
Keith Busch12f5b932018-05-29 15:52:28 +0200589 blk_mq_sched_restart(hctx);
590 blk_queue_exit(q);
591}
592
Christoph Hellwig6af54052017-06-16 18:15:22 +0200593void blk_mq_free_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100594{
Jens Axboe320ae512013-10-24 09:20:05 +0100595 struct request_queue *q = rq->q;
Jens Axboeea4f9952018-10-29 15:06:13 -0600596 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100597
Jens Axboee0d78af2021-10-18 20:54:39 -0600598 if (rq->rq_flags & RQF_ELVPRIV) {
Jens Axboe2ff06822021-10-15 09:44:38 -0600599 struct elevator_queue *e = q->elevator;
600
601 if (e->type->ops.finish_request)
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600602 e->type->ops.finish_request(rq);
Christoph Hellwig6af54052017-06-16 18:15:22 +0200603 if (rq->elv.icq) {
604 put_io_context(rq->elv.icq->ioc);
605 rq->elv.icq = NULL;
606 }
607 }
608
Christoph Hellwige8064022016-10-20 15:12:13 +0200609 if (rq->rq_flags & RQF_MQ_INFLIGHT)
John Garrybccf5e22020-08-19 23:20:26 +0800610 __blk_mq_dec_active_requests(hctx);
Jens Axboe87760e52016-11-09 12:38:14 -0700611
Jens Axboe7beb2f82017-09-30 02:08:24 -0600612 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
Christoph Hellwigd152c682021-08-16 15:46:24 +0200613 laptop_io_completion(q->disk->bdi);
Jens Axboe7beb2f82017-09-30 02:08:24 -0600614
Josef Bacika7905042018-07-03 09:32:35 -0600615 rq_qos_done(q, rq);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600616
Keith Busch12f5b932018-05-29 15:52:28 +0200617 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
618 if (refcount_dec_and_test(&rq->ref))
619 __blk_mq_free_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100620}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700621EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100622
Jens Axboe47c122e2021-10-06 06:34:11 -0600623void blk_mq_free_plug_rqs(struct blk_plug *plug)
624{
Jens Axboe013a7f92021-10-13 07:58:52 -0600625 struct request *rq;
Jens Axboe47c122e2021-10-06 06:34:11 -0600626
Jens Axboe013a7f92021-10-13 07:58:52 -0600627 while ((rq = rq_list_pop(&plug->cached_rq)) != NULL) {
Jens Axboe47c122e2021-10-06 06:34:11 -0600628 percpu_ref_get(&rq->q->q_usage_counter);
629 blk_mq_free_request(rq);
630 }
631}
632
Jens Axboe9be3e062021-10-14 09:17:01 -0600633static void req_bio_endio(struct request *rq, struct bio *bio,
634 unsigned int nbytes, blk_status_t error)
635{
Pavel Begunkov478eb722021-10-19 22:24:12 +0100636 if (unlikely(error)) {
Jens Axboe9be3e062021-10-14 09:17:01 -0600637 bio->bi_status = error;
Pavel Begunkov478eb722021-10-19 22:24:12 +0100638 } else if (req_op(rq) == REQ_OP_ZONE_APPEND) {
Jens Axboe9be3e062021-10-14 09:17:01 -0600639 /*
640 * Partial zone append completions cannot be supported as the
641 * BIO fragments may end up not being written sequentially.
642 */
Pavel Begunkov478eb722021-10-19 22:24:12 +0100643 if (bio->bi_iter.bi_size == nbytes)
Jens Axboe9be3e062021-10-14 09:17:01 -0600644 bio->bi_status = BLK_STS_IOERR;
645 else
646 bio->bi_iter.bi_sector = rq->__sector;
647 }
648
Pavel Begunkov478eb722021-10-19 22:24:12 +0100649 bio_advance(bio, nbytes);
650
651 if (unlikely(rq->rq_flags & RQF_QUIET))
652 bio_set_flag(bio, BIO_QUIET);
Jens Axboe9be3e062021-10-14 09:17:01 -0600653 /* don't actually finish bio if it's part of flush sequence */
654 if (bio->bi_iter.bi_size == 0 && !(rq->rq_flags & RQF_FLUSH_SEQ))
655 bio_endio(bio);
656}
657
658static void blk_account_io_completion(struct request *req, unsigned int bytes)
659{
660 if (req->part && blk_do_io_stat(req)) {
661 const int sgrp = op_stat_group(req_op(req));
662
663 part_stat_lock();
664 part_stat_add(req->part, sectors[sgrp], bytes >> 9);
665 part_stat_unlock();
666 }
667}
668
669/**
670 * blk_update_request - Complete multiple bytes without completing the request
671 * @req: the request being processed
672 * @error: block status code
673 * @nr_bytes: number of bytes to complete for @req
674 *
675 * Description:
676 * Ends I/O on a number of bytes attached to @req, but doesn't complete
677 * the request structure even if @req doesn't have leftover.
678 * If @req has leftover, sets it up for the next range of segments.
679 *
680 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
681 * %false return from this function.
682 *
683 * Note:
684 * The RQF_SPECIAL_PAYLOAD flag is ignored on purpose in this function
685 * except in the consistency check at the end of this function.
686 *
687 * Return:
688 * %false - this request doesn't have any more data
689 * %true - this request has more data
690 **/
691bool blk_update_request(struct request *req, blk_status_t error,
692 unsigned int nr_bytes)
693{
694 int total_bytes;
695
Christoph Hellwig8a7d2672021-10-18 10:45:18 +0200696 trace_block_rq_complete(req, error, nr_bytes);
Jens Axboe9be3e062021-10-14 09:17:01 -0600697
698 if (!req->bio)
699 return false;
700
701#ifdef CONFIG_BLK_DEV_INTEGRITY
702 if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ &&
703 error == BLK_STS_OK)
704 req->q->integrity.profile->complete_fn(req, nr_bytes);
705#endif
706
707 if (unlikely(error && !blk_rq_is_passthrough(req) &&
708 !(req->rq_flags & RQF_QUIET)))
709 blk_print_req_error(req, error);
710
711 blk_account_io_completion(req, nr_bytes);
712
713 total_bytes = 0;
714 while (req->bio) {
715 struct bio *bio = req->bio;
716 unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
717
718 if (bio_bytes == bio->bi_iter.bi_size)
719 req->bio = bio->bi_next;
720
721 /* Completion has already been traced */
722 bio_clear_flag(bio, BIO_TRACE_COMPLETION);
723 req_bio_endio(req, bio, bio_bytes, error);
724
725 total_bytes += bio_bytes;
726 nr_bytes -= bio_bytes;
727
728 if (!nr_bytes)
729 break;
730 }
731
732 /*
733 * completely done
734 */
735 if (!req->bio) {
736 /*
737 * Reset counters so that the request stacking driver
738 * can find how many bytes remain in the request
739 * later.
740 */
741 req->__data_len = 0;
742 return false;
743 }
744
745 req->__data_len -= total_bytes;
746
747 /* update sector only for requests with clear definition of sector */
748 if (!blk_rq_is_passthrough(req))
749 req->__sector += total_bytes >> 9;
750
751 /* mixed attributes always follow the first bio */
752 if (req->rq_flags & RQF_MIXED_MERGE) {
753 req->cmd_flags &= ~REQ_FAILFAST_MASK;
754 req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK;
755 }
756
757 if (!(req->rq_flags & RQF_SPECIAL_PAYLOAD)) {
758 /*
759 * If total number of sectors is less than the first segment
760 * size, something has gone terribly wrong.
761 */
762 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
763 blk_dump_rq_flags(req, "request botched");
764 req->__data_len = blk_rq_cur_bytes(req);
765 }
766
767 /* recalculate the number of segments */
768 req->nr_phys_segments = blk_recalc_rq_segments(req);
769 }
770
771 return true;
772}
773EXPORT_SYMBOL_GPL(blk_update_request);
774
Jens Axboef794f332021-10-08 05:50:46 -0600775static inline void __blk_mq_end_request_acct(struct request *rq, u64 now)
776{
777 if (rq->rq_flags & RQF_STATS) {
778 blk_mq_poll_stats_start(rq->q);
779 blk_stat_add(rq, now);
780 }
781
782 blk_mq_sched_completed_request(rq, now);
783 blk_account_io_done(rq, now);
784}
785
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200786inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
Jens Axboe320ae512013-10-24 09:20:05 +0100787{
Jens Axboef794f332021-10-08 05:50:46 -0600788 if (blk_mq_need_time_stamp(rq))
789 __blk_mq_end_request_acct(rq, ktime_get_ns());
Omar Sandoval4bc63392018-05-09 02:08:52 -0700790
Christoph Hellwig91b63632014-04-16 09:44:53 +0200791 if (rq->end_io) {
Josef Bacika7905042018-07-03 09:32:35 -0600792 rq_qos_done(rq->q, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100793 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200794 } else {
Jens Axboe320ae512013-10-24 09:20:05 +0100795 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200796 }
Jens Axboe320ae512013-10-24 09:20:05 +0100797}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700798EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200799
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200800void blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200801{
802 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
803 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700804 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200805}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700806EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100807
Jens Axboef794f332021-10-08 05:50:46 -0600808#define TAG_COMP_BATCH 32
809
810static inline void blk_mq_flush_tag_batch(struct blk_mq_hw_ctx *hctx,
811 int *tag_array, int nr_tags)
812{
813 struct request_queue *q = hctx->queue;
814
815 blk_mq_put_tags(hctx->tags, tag_array, nr_tags);
816 percpu_ref_put_many(&q->q_usage_counter, nr_tags);
817}
818
819void blk_mq_end_request_batch(struct io_comp_batch *iob)
820{
821 int tags[TAG_COMP_BATCH], nr_tags = 0;
822 struct blk_mq_hw_ctx *last_hctx = NULL;
823 struct request *rq;
824 u64 now = 0;
825
826 if (iob->need_ts)
827 now = ktime_get_ns();
828
829 while ((rq = rq_list_pop(&iob->req_list)) != NULL) {
830 prefetch(rq->bio);
831 prefetch(rq->rq_next);
832
833 blk_update_request(rq, BLK_STS_OK, blk_rq_bytes(rq));
834 if (iob->need_ts)
835 __blk_mq_end_request_acct(rq, now);
836
837 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
838 if (!refcount_dec_and_test(&rq->ref))
839 continue;
840
841 blk_crypto_free_request(rq);
842 blk_pm_mark_last_busy(rq);
843 rq_qos_done(rq->q, rq);
844
845 if (nr_tags == TAG_COMP_BATCH ||
846 (last_hctx && last_hctx != rq->mq_hctx)) {
847 blk_mq_flush_tag_batch(last_hctx, tags, nr_tags);
848 nr_tags = 0;
849 }
850 tags[nr_tags++] = rq->tag;
851 last_hctx = rq->mq_hctx;
852 }
853
854 if (nr_tags)
855 blk_mq_flush_tag_batch(last_hctx, tags, nr_tags);
856}
857EXPORT_SYMBOL_GPL(blk_mq_end_request_batch);
858
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100859static void blk_complete_reqs(struct llist_head *list)
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200860{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100861 struct llist_node *entry = llist_reverse_order(llist_del_all(list));
862 struct request *rq, *next;
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200863
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100864 llist_for_each_entry_safe(rq, next, entry, ipi_list)
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200865 rq->q->mq_ops->complete(rq);
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200866}
867
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100868static __latent_entropy void blk_done_softirq(struct softirq_action *h)
Christoph Hellwig115243f2020-06-11 08:44:42 +0200869{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100870 blk_complete_reqs(this_cpu_ptr(&blk_cpu_done));
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200871}
872
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200873static int blk_softirq_cpu_dead(unsigned int cpu)
874{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100875 blk_complete_reqs(&per_cpu(blk_cpu_done, cpu));
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200876 return 0;
877}
878
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800879static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100880{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100881 __raise_softirq_irqoff(BLOCK_SOFTIRQ);
Jens Axboe320ae512013-10-24 09:20:05 +0100882}
883
Christoph Hellwig963395262020-06-11 08:44:49 +0200884static inline bool blk_mq_complete_need_ipi(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100885{
Christoph Hellwig963395262020-06-11 08:44:49 +0200886 int cpu = raw_smp_processor_id();
Jens Axboe320ae512013-10-24 09:20:05 +0100887
Christoph Hellwig963395262020-06-11 08:44:49 +0200888 if (!IS_ENABLED(CONFIG_SMP) ||
889 !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags))
890 return false;
Sebastian Andrzej Siewior71425182020-12-04 20:13:54 +0100891 /*
892 * With force threaded interrupts enabled, raising softirq from an SMP
893 * function call will always result in waking the ksoftirqd thread.
894 * This is probably worse than completing the request on a different
895 * cache domain.
896 */
Tanner Love91cc4702021-06-02 14:03:38 -0400897 if (force_irqthreads())
Sebastian Andrzej Siewior71425182020-12-04 20:13:54 +0100898 return false;
Christoph Hellwig963395262020-06-11 08:44:49 +0200899
900 /* same CPU or cache domain? Complete locally */
901 if (cpu == rq->mq_ctx->cpu ||
902 (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) &&
903 cpus_share_cache(cpu, rq->mq_ctx->cpu)))
904 return false;
905
906 /* don't try to IPI to an offline CPU */
907 return cpu_online(rq->mq_ctx->cpu);
908}
909
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100910static void blk_mq_complete_send_ipi(struct request *rq)
911{
912 struct llist_head *list;
913 unsigned int cpu;
914
915 cpu = rq->mq_ctx->cpu;
916 list = &per_cpu(blk_cpu_done, cpu);
917 if (llist_add(&rq->ipi_list, list)) {
918 INIT_CSD(&rq->csd, __blk_mq_complete_request_remote, rq);
919 smp_call_function_single_async(cpu, &rq->csd);
920 }
921}
922
923static void blk_mq_raise_softirq(struct request *rq)
924{
925 struct llist_head *list;
926
927 preempt_disable();
928 list = this_cpu_ptr(&blk_cpu_done);
929 if (llist_add(&rq->ipi_list, list))
930 raise_softirq(BLOCK_SOFTIRQ);
931 preempt_enable();
932}
933
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200934bool blk_mq_complete_request_remote(struct request *rq)
935{
Keith Buschaf78ff72018-11-26 09:54:30 -0700936 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
Ming Lei36e76532018-09-28 16:42:20 +0800937
Jens Axboe4ab32bf2018-11-18 16:15:35 -0700938 /*
939 * For a polled request, always complete locallly, it's pointless
940 * to redirect the completion.
941 */
Christoph Hellwig6ce913f2021-10-12 13:12:21 +0200942 if (rq->cmd_flags & REQ_POLLED)
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200943 return false;
Jens Axboe320ae512013-10-24 09:20:05 +0100944
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200945 if (blk_mq_complete_need_ipi(rq)) {
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100946 blk_mq_complete_send_ipi(rq);
947 return true;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800948 }
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200949
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100950 if (rq->q->nr_hw_queues == 1) {
951 blk_mq_raise_softirq(rq);
952 return true;
953 }
954 return false;
Jens Axboe320ae512013-10-24 09:20:05 +0100955}
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200956EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote);
957
Jens Axboe320ae512013-10-24 09:20:05 +0100958/**
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200959 * blk_mq_complete_request - end I/O on a request
960 * @rq: the request being processed
Jens Axboe320ae512013-10-24 09:20:05 +0100961 *
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200962 * Description:
963 * Complete a request by scheduling the ->complete_rq operation.
964 **/
965void blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100966{
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200967 if (!blk_mq_complete_request_remote(rq))
Christoph Hellwig963395262020-06-11 08:44:49 +0200968 rq->q->mq_ops->complete(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100969}
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200970EXPORT_SYMBOL(blk_mq_complete_request);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800971
Jens Axboe04ced152018-01-09 08:29:46 -0800972static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800973 __releases(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800974{
975 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
976 rcu_read_unlock();
977 else
Tejun Heo05707b62018-01-09 08:29:53 -0800978 srcu_read_unlock(hctx->srcu, srcu_idx);
Jens Axboe04ced152018-01-09 08:29:46 -0800979}
980
981static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800982 __acquires(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800983{
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700984 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
985 /* shut up gcc false positive */
986 *srcu_idx = 0;
Jens Axboe04ced152018-01-09 08:29:46 -0800987 rcu_read_lock();
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700988 } else
Tejun Heo05707b62018-01-09 08:29:53 -0800989 *srcu_idx = srcu_read_lock(hctx->srcu);
Jens Axboe04ced152018-01-09 08:29:46 -0800990}
991
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800992/**
André Almeida105663f2020-01-06 15:08:18 -0300993 * blk_mq_start_request - Start processing a request
994 * @rq: Pointer to request to be started
995 *
996 * Function used by device drivers to notify the block layer that a request
997 * is going to be processed now, so blk layer can do proper initializations
998 * such as starting the timeout timer.
999 */
Christoph Hellwige2490072014-09-13 16:40:09 -07001000void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001001{
1002 struct request_queue *q = rq->q;
1003
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001004 trace_block_rq_issue(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001005
Jens Axboecf43e6b2016-11-07 21:32:37 -07001006 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
Jens Axboe00067072021-10-05 09:23:59 -06001007 u64 start_time;
1008#ifdef CONFIG_BLK_CGROUP
1009 if (rq->bio)
1010 start_time = bio_issue_time(&rq->bio->bi_issue);
1011 else
1012#endif
1013 start_time = ktime_get_ns();
1014 rq->io_start_time_ns = start_time;
Hou Tao3d244302019-05-21 15:59:03 +08001015 rq->stats_sectors = blk_rq_sectors(rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -07001016 rq->rq_flags |= RQF_STATS;
Josef Bacika7905042018-07-03 09:32:35 -06001017 rq_qos_issue(q, rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -07001018 }
1019
Tejun Heo1d9bd512018-01-09 08:29:48 -08001020 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
Jens Axboe538b7532014-09-16 10:37:37 -06001021
Tejun Heo1d9bd512018-01-09 08:29:48 -08001022 blk_add_timer(rq);
Keith Busch12f5b932018-05-29 15:52:28 +02001023 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -08001024
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +03001025#ifdef CONFIG_BLK_DEV_INTEGRITY
1026 if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
1027 q->integrity.profile->prepare_fn(rq);
1028#endif
Christoph Hellwig3e087732021-10-12 13:12:24 +02001029 if (rq->bio && rq->bio->bi_opf & REQ_POLLED)
1030 WRITE_ONCE(rq->bio->bi_cookie, blk_rq_to_qc(rq));
Jens Axboe320ae512013-10-24 09:20:05 +01001031}
Christoph Hellwige2490072014-09-13 16:40:09 -07001032EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +01001033
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001034static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001035{
1036 struct request_queue *q = rq->q;
1037
Ming Lei923218f2017-11-02 23:24:38 +08001038 blk_mq_put_driver_tag(rq);
1039
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001040 trace_block_rq_requeue(rq);
Josef Bacika7905042018-07-03 09:32:35 -06001041 rq_qos_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -08001042
Keith Busch12f5b932018-05-29 15:52:28 +02001043 if (blk_mq_request_started(rq)) {
1044 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Christoph Hellwigda661262018-06-14 13:58:45 +02001045 rq->rq_flags &= ~RQF_TIMED_OUT;
Christoph Hellwige2490072014-09-13 16:40:09 -07001046 }
Jens Axboe320ae512013-10-24 09:20:05 +01001047}
1048
Bart Van Assche2b053ac2016-10-28 17:21:41 -07001049void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001050{
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001051 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001052
Ming Lei105976f2018-02-23 23:36:56 +08001053 /* this request will be re-inserted to io scheduler queue */
1054 blk_mq_sched_requeue_request(rq);
1055
Bart Van Assche2b053ac2016-10-28 17:21:41 -07001056 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001057}
1058EXPORT_SYMBOL(blk_mq_requeue_request);
1059
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001060static void blk_mq_requeue_work(struct work_struct *work)
1061{
1062 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -04001063 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001064 LIST_HEAD(rq_list);
1065 struct request *rq, *next;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001066
Jens Axboe18e97812017-07-27 08:03:57 -06001067 spin_lock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001068 list_splice_init(&q->requeue_list, &rq_list);
Jens Axboe18e97812017-07-27 08:03:57 -06001069 spin_unlock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001070
1071 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
Jianchao Wangaef18972019-02-12 09:56:25 +08001072 if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP)))
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001073 continue;
1074
Christoph Hellwige8064022016-10-20 15:12:13 +02001075 rq->rq_flags &= ~RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001076 list_del_init(&rq->queuelist);
Jianchao Wangaef18972019-02-12 09:56:25 +08001077 /*
1078 * If RQF_DONTPREP, rq has contained some driver specific
1079 * data, so insert it to hctx dispatch list to avoid any
1080 * merge.
1081 */
1082 if (rq->rq_flags & RQF_DONTPREP)
Ming Lei01e99ae2020-02-25 09:04:32 +08001083 blk_mq_request_bypass_insert(rq, false, false);
Jianchao Wangaef18972019-02-12 09:56:25 +08001084 else
1085 blk_mq_sched_insert_request(rq, true, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001086 }
1087
1088 while (!list_empty(&rq_list)) {
1089 rq = list_entry(rq_list.next, struct request, queuelist);
1090 list_del_init(&rq->queuelist);
Mike Snitzer9e97d292018-01-17 11:25:58 -05001091 blk_mq_sched_insert_request(rq, false, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001092 }
1093
Bart Van Assche52d7f1b2016-10-28 17:20:32 -07001094 blk_mq_run_hw_queues(q, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001095}
1096
Bart Van Assche2b053ac2016-10-28 17:21:41 -07001097void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
1098 bool kick_requeue_list)
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001099{
1100 struct request_queue *q = rq->q;
1101 unsigned long flags;
1102
1103 /*
1104 * We abuse this flag that is otherwise used by the I/O scheduler to
Jens Axboeff821d22017-11-10 22:05:12 -07001105 * request head insertion from the workqueue.
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001106 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001107 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001108
1109 spin_lock_irqsave(&q->requeue_lock, flags);
1110 if (at_head) {
Christoph Hellwige8064022016-10-20 15:12:13 +02001111 rq->rq_flags |= RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001112 list_add(&rq->queuelist, &q->requeue_list);
1113 } else {
1114 list_add_tail(&rq->queuelist, &q->requeue_list);
1115 }
1116 spin_unlock_irqrestore(&q->requeue_lock, flags);
Bart Van Assche2b053ac2016-10-28 17:21:41 -07001117
1118 if (kick_requeue_list)
1119 blk_mq_kick_requeue_list(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001120}
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001121
1122void blk_mq_kick_requeue_list(struct request_queue *q)
1123{
Bart Van Asscheae943d22018-01-19 08:58:55 -08001124 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001125}
1126EXPORT_SYMBOL(blk_mq_kick_requeue_list);
1127
Mike Snitzer28494502016-09-14 13:28:30 -04001128void blk_mq_delay_kick_requeue_list(struct request_queue *q,
1129 unsigned long msecs)
1130{
Bart Van Assched4acf362017-08-09 11:28:06 -07001131 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
1132 msecs_to_jiffies(msecs));
Mike Snitzer28494502016-09-14 13:28:30 -04001133}
1134EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
1135
Jens Axboe3c94d832018-12-17 21:11:17 -07001136static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq,
1137 void *priv, bool reserved)
Jens Axboeae879912018-11-08 09:03:51 -07001138{
1139 /*
Ming Lei05a4fed2020-07-07 11:04:33 -04001140 * If we find a request that isn't idle and the queue matches,
Jens Axboe3c94d832018-12-17 21:11:17 -07001141 * we know the queue is busy. Return false to stop the iteration.
Jens Axboeae879912018-11-08 09:03:51 -07001142 */
Ming Lei05a4fed2020-07-07 11:04:33 -04001143 if (blk_mq_request_started(rq) && rq->q == hctx->queue) {
Jens Axboeae879912018-11-08 09:03:51 -07001144 bool *busy = priv;
1145
1146 *busy = true;
1147 return false;
1148 }
1149
1150 return true;
1151}
1152
Jens Axboe3c94d832018-12-17 21:11:17 -07001153bool blk_mq_queue_inflight(struct request_queue *q)
Jens Axboeae879912018-11-08 09:03:51 -07001154{
1155 bool busy = false;
1156
Jens Axboe3c94d832018-12-17 21:11:17 -07001157 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
Jens Axboeae879912018-11-08 09:03:51 -07001158 return busy;
1159}
Jens Axboe3c94d832018-12-17 21:11:17 -07001160EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
Jens Axboeae879912018-11-08 09:03:51 -07001161
Tejun Heo358f70d2018-01-09 08:29:50 -08001162static void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +01001163{
Christoph Hellwigda661262018-06-14 13:58:45 +02001164 req->rq_flags |= RQF_TIMED_OUT;
Christoph Hellwigd1210d52018-05-29 15:52:39 +02001165 if (req->q->mq_ops->timeout) {
1166 enum blk_eh_timer_return ret;
Jens Axboe87ee7b12014-04-24 08:51:47 -06001167
Christoph Hellwigd1210d52018-05-29 15:52:39 +02001168 ret = req->q->mq_ops->timeout(req, reserved);
1169 if (ret == BLK_EH_DONE)
1170 return;
1171 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
Christoph Hellwig46f92d42014-09-13 16:40:12 -07001172 }
Christoph Hellwigd1210d52018-05-29 15:52:39 +02001173
1174 blk_add_timer(req);
Jens Axboe87ee7b12014-04-24 08:51:47 -06001175}
Keith Busch5b3f25f2015-01-07 18:55:46 -07001176
Keith Busch12f5b932018-05-29 15:52:28 +02001177static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
1178{
1179 unsigned long deadline;
1180
1181 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
1182 return false;
Christoph Hellwigda661262018-06-14 13:58:45 +02001183 if (rq->rq_flags & RQF_TIMED_OUT)
1184 return false;
Keith Busch12f5b932018-05-29 15:52:28 +02001185
Christoph Hellwig079076b2018-11-14 17:02:05 +01001186 deadline = READ_ONCE(rq->deadline);
Keith Busch12f5b932018-05-29 15:52:28 +02001187 if (time_after_eq(jiffies, deadline))
1188 return true;
1189
1190 if (*next == 0)
1191 *next = deadline;
1192 else if (time_after(*next, deadline))
1193 *next = deadline;
1194 return false;
1195}
1196
Ming Lei2e315dc2021-05-11 23:22:34 +08001197void blk_mq_put_rq_ref(struct request *rq)
1198{
Ming Leia9ed27a2021-08-18 09:09:25 +08001199 if (is_flush_rq(rq))
Ming Lei2e315dc2021-05-11 23:22:34 +08001200 rq->end_io(rq, 0);
1201 else if (refcount_dec_and_test(&rq->ref))
1202 __blk_mq_free_request(rq);
1203}
1204
Jens Axboe7baa8572018-11-08 10:24:07 -07001205static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig81481eb2014-09-13 16:40:11 -07001206 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +01001207{
Keith Busch12f5b932018-05-29 15:52:28 +02001208 unsigned long *next = priv;
Christoph Hellwig81481eb2014-09-13 16:40:11 -07001209
Keith Busch12f5b932018-05-29 15:52:28 +02001210 /*
Ming Leic797b402021-08-11 23:52:02 +08001211 * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot
1212 * be reallocated underneath the timeout handler's processing, then
1213 * the expire check is reliable. If the request is not expired, then
1214 * it was completed and reallocated as a new request after returning
1215 * from blk_mq_check_expired().
Keith Busch12f5b932018-05-29 15:52:28 +02001216 */
1217 if (blk_mq_req_expired(rq, next))
Tejun Heo1d9bd512018-01-09 08:29:48 -08001218 blk_mq_rq_timed_out(rq, reserved);
Jens Axboe7baa8572018-11-08 10:24:07 -07001219 return true;
Tejun Heo1d9bd512018-01-09 08:29:48 -08001220}
1221
Christoph Hellwig287922e2015-10-30 20:57:30 +08001222static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001223{
Christoph Hellwig287922e2015-10-30 20:57:30 +08001224 struct request_queue *q =
1225 container_of(work, struct request_queue, timeout_work);
Keith Busch12f5b932018-05-29 15:52:28 +02001226 unsigned long next = 0;
Tejun Heo1d9bd512018-01-09 08:29:48 -08001227 struct blk_mq_hw_ctx *hctx;
Christoph Hellwig81481eb2014-09-13 16:40:11 -07001228 int i;
Jens Axboe320ae512013-10-24 09:20:05 +01001229
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -06001230 /* A deadlock might occur if a request is stuck requiring a
1231 * timeout at the same time a queue freeze is waiting
1232 * completion, since the timeout code would not be able to
1233 * acquire the queue reference here.
1234 *
1235 * That's why we don't use blk_queue_enter here; instead, we use
1236 * percpu_ref_tryget directly, because we need to be able to
1237 * obtain a reference even in the short window between the queue
1238 * starting to freeze, by dropping the first reference in
Ming Lei1671d522017-03-27 20:06:57 +08001239 * blk_freeze_queue_start, and the moment the last request is
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -06001240 * consumed, marked by the instant q_usage_counter reaches
1241 * zero.
1242 */
1243 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +08001244 return;
1245
Keith Busch12f5b932018-05-29 15:52:28 +02001246 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
Jens Axboe320ae512013-10-24 09:20:05 +01001247
Keith Busch12f5b932018-05-29 15:52:28 +02001248 if (next != 0) {
1249 mod_timer(&q->timeout, next);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001250 } else {
Bart Van Asschefcd36c32018-01-10 08:33:33 -08001251 /*
1252 * Request timeouts are handled as a forward rolling timer. If
1253 * we end up here it means that no requests are pending and
1254 * also that no request has been pending for a while. Mark
1255 * each hctx as idle.
1256 */
Ming Leif054b562015-04-21 10:00:19 +08001257 queue_for_each_hw_ctx(q, hctx, i) {
1258 /* the hctx may be unmapped, so check it here */
1259 if (blk_mq_hw_queue_mapped(hctx))
1260 blk_mq_tag_idle(hctx);
1261 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06001262 }
Christoph Hellwig287922e2015-10-30 20:57:30 +08001263 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001264}
1265
Omar Sandoval88459642016-09-17 08:38:44 -06001266struct flush_busy_ctx_data {
1267 struct blk_mq_hw_ctx *hctx;
1268 struct list_head *list;
1269};
1270
1271static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
1272{
1273 struct flush_busy_ctx_data *flush_data = data;
1274 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
1275 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001276 enum hctx_type type = hctx->type;
Omar Sandoval88459642016-09-17 08:38:44 -06001277
Omar Sandoval88459642016-09-17 08:38:44 -06001278 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001279 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
Omar Sandovale9a99a62018-02-27 16:56:42 -08001280 sbitmap_clear_bit(sb, bitnr);
Omar Sandoval88459642016-09-17 08:38:44 -06001281 spin_unlock(&ctx->lock);
1282 return true;
1283}
1284
Jens Axboe320ae512013-10-24 09:20:05 +01001285/*
Jens Axboe1429d7c2014-05-19 09:23:55 -06001286 * Process software queues that have been marked busy, splicing them
1287 * to the for-dispatch
1288 */
Jens Axboe2c3ad662016-12-14 14:34:47 -07001289void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
Jens Axboe1429d7c2014-05-19 09:23:55 -06001290{
Omar Sandoval88459642016-09-17 08:38:44 -06001291 struct flush_busy_ctx_data data = {
1292 .hctx = hctx,
1293 .list = list,
1294 };
Jens Axboe1429d7c2014-05-19 09:23:55 -06001295
Omar Sandoval88459642016-09-17 08:38:44 -06001296 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001297}
Jens Axboe2c3ad662016-12-14 14:34:47 -07001298EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001299
Ming Leib3476892017-10-14 17:22:30 +08001300struct dispatch_rq_data {
1301 struct blk_mq_hw_ctx *hctx;
1302 struct request *rq;
1303};
1304
1305static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
1306 void *data)
1307{
1308 struct dispatch_rq_data *dispatch_data = data;
1309 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
1310 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001311 enum hctx_type type = hctx->type;
Ming Leib3476892017-10-14 17:22:30 +08001312
1313 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001314 if (!list_empty(&ctx->rq_lists[type])) {
1315 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
Ming Leib3476892017-10-14 17:22:30 +08001316 list_del_init(&dispatch_data->rq->queuelist);
Ming Leic16d6b52018-12-17 08:44:05 -07001317 if (list_empty(&ctx->rq_lists[type]))
Ming Leib3476892017-10-14 17:22:30 +08001318 sbitmap_clear_bit(sb, bitnr);
1319 }
1320 spin_unlock(&ctx->lock);
1321
1322 return !dispatch_data->rq;
1323}
1324
1325struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
1326 struct blk_mq_ctx *start)
1327{
Jens Axboef31967f2018-10-29 13:13:29 -06001328 unsigned off = start ? start->index_hw[hctx->type] : 0;
Ming Leib3476892017-10-14 17:22:30 +08001329 struct dispatch_rq_data data = {
1330 .hctx = hctx,
1331 .rq = NULL,
1332 };
1333
1334 __sbitmap_for_each_set(&hctx->ctx_map, off,
1335 dispatch_rq_from_ctx, &data);
1336
1337 return data.rq;
1338}
1339
Jens Axboea808a9d2021-10-13 08:28:14 -06001340static bool __blk_mq_alloc_driver_tag(struct request *rq)
Ming Lei570e9b72020-06-30 22:03:55 +08001341{
John Garryae0f1a72021-10-05 18:23:38 +08001342 struct sbitmap_queue *bt = &rq->mq_hctx->tags->bitmap_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001343 unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001344 int tag;
1345
Ming Lei568f2702020-07-06 22:41:11 +08001346 blk_mq_tag_busy(rq->mq_hctx);
1347
Ming Lei570e9b72020-06-30 22:03:55 +08001348 if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) {
John Garryae0f1a72021-10-05 18:23:38 +08001349 bt = &rq->mq_hctx->tags->breserved_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001350 tag_offset = 0;
Ming Lei28500852020-09-11 18:41:14 +08001351 } else {
1352 if (!hctx_may_queue(rq->mq_hctx, bt))
1353 return false;
Ming Lei570e9b72020-06-30 22:03:55 +08001354 }
1355
Ming Lei570e9b72020-06-30 22:03:55 +08001356 tag = __sbitmap_queue_get(bt);
1357 if (tag == BLK_MQ_NO_TAG)
1358 return false;
1359
1360 rq->tag = tag + tag_offset;
Ming Lei570e9b72020-06-30 22:03:55 +08001361 return true;
1362}
1363
Jens Axboea808a9d2021-10-13 08:28:14 -06001364bool __blk_mq_get_driver_tag(struct blk_mq_hw_ctx *hctx, struct request *rq)
Ming Lei570e9b72020-06-30 22:03:55 +08001365{
Jens Axboea808a9d2021-10-13 08:28:14 -06001366 if (rq->tag == BLK_MQ_NO_TAG && !__blk_mq_alloc_driver_tag(rq))
Ming Lei568f2702020-07-06 22:41:11 +08001367 return false;
1368
Ming Lei51db1c32020-08-19 23:20:19 +08001369 if ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) &&
Ming Lei568f2702020-07-06 22:41:11 +08001370 !(rq->rq_flags & RQF_MQ_INFLIGHT)) {
1371 rq->rq_flags |= RQF_MQ_INFLIGHT;
John Garrybccf5e22020-08-19 23:20:26 +08001372 __blk_mq_inc_active_requests(hctx);
Ming Lei568f2702020-07-06 22:41:11 +08001373 }
1374 hctx->tags->rqs[rq->tag] = rq;
1375 return true;
Ming Lei570e9b72020-06-30 22:03:55 +08001376}
1377
Jens Axboeeb619fd2017-11-09 08:32:43 -07001378static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1379 int flags, void *key)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001380{
1381 struct blk_mq_hw_ctx *hctx;
1382
1383 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1384
Ming Lei5815839b2018-06-25 19:31:47 +08001385 spin_lock(&hctx->dispatch_wait_lock);
Jens Axboee8618572019-03-25 12:34:10 -06001386 if (!list_empty(&wait->entry)) {
1387 struct sbitmap_queue *sbq;
1388
1389 list_del_init(&wait->entry);
John Garryae0f1a72021-10-05 18:23:38 +08001390 sbq = &hctx->tags->bitmap_tags;
Jens Axboee8618572019-03-25 12:34:10 -06001391 atomic_dec(&sbq->ws_active);
1392 }
Ming Lei5815839b2018-06-25 19:31:47 +08001393 spin_unlock(&hctx->dispatch_wait_lock);
1394
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001395 blk_mq_run_hw_queue(hctx, true);
1396 return 1;
1397}
1398
Jens Axboef906a6a2017-11-09 16:10:13 -07001399/*
1400 * Mark us waiting for a tag. For shared tags, this involves hooking us into
Bart Van Asscheee3e4de2018-01-09 10:09:15 -08001401 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1402 * restart. For both cases, take care to check the condition again after
Jens Axboef906a6a2017-11-09 16:10:13 -07001403 * marking us as waiting.
1404 */
Ming Lei2278d692018-06-25 19:31:46 +08001405static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
Jens Axboef906a6a2017-11-09 16:10:13 -07001406 struct request *rq)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001407{
John Garryae0f1a72021-10-05 18:23:38 +08001408 struct sbitmap_queue *sbq = &hctx->tags->bitmap_tags;
Ming Lei5815839b2018-06-25 19:31:47 +08001409 struct wait_queue_head *wq;
Jens Axboef906a6a2017-11-09 16:10:13 -07001410 wait_queue_entry_t *wait;
1411 bool ret;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001412
Ming Lei51db1c32020-08-19 23:20:19 +08001413 if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
Yufen Yu684b7322019-03-15 11:05:10 +08001414 blk_mq_sched_mark_restart_hctx(hctx);
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001415
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001416 /*
1417 * It's possible that a tag was freed in the window between the
1418 * allocation failure and adding the hardware queue to the wait
1419 * queue.
1420 *
1421 * Don't clear RESTART here, someone else could have set it.
1422 * At most this will cost an extra queue run.
1423 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001424 return blk_mq_get_driver_tag(rq);
Jens Axboeeb619fd2017-11-09 08:32:43 -07001425 }
1426
Ming Lei2278d692018-06-25 19:31:46 +08001427 wait = &hctx->dispatch_wait;
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001428 if (!list_empty_careful(&wait->entry))
1429 return false;
1430
Jens Axboee8618572019-03-25 12:34:10 -06001431 wq = &bt_wait_ptr(sbq, hctx)->wait;
Ming Lei5815839b2018-06-25 19:31:47 +08001432
1433 spin_lock_irq(&wq->lock);
1434 spin_lock(&hctx->dispatch_wait_lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001435 if (!list_empty(&wait->entry)) {
Ming Lei5815839b2018-06-25 19:31:47 +08001436 spin_unlock(&hctx->dispatch_wait_lock);
1437 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001438 return false;
1439 }
1440
Jens Axboee8618572019-03-25 12:34:10 -06001441 atomic_inc(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001442 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1443 __add_wait_queue(wq, wait);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001444
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001445 /*
Jens Axboeeb619fd2017-11-09 08:32:43 -07001446 * It's possible that a tag was freed in the window between the
1447 * allocation failure and adding the hardware queue to the wait
1448 * queue.
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001449 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001450 ret = blk_mq_get_driver_tag(rq);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001451 if (!ret) {
Ming Lei5815839b2018-06-25 19:31:47 +08001452 spin_unlock(&hctx->dispatch_wait_lock);
1453 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001454 return false;
Jens Axboef906a6a2017-11-09 16:10:13 -07001455 }
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001456
1457 /*
1458 * We got a tag, remove ourselves from the wait queue to ensure
1459 * someone else gets the wakeup.
1460 */
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001461 list_del_init(&wait->entry);
Jens Axboee8618572019-03-25 12:34:10 -06001462 atomic_dec(&sbq->ws_active);
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
1466 return true;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001467}
1468
Ming Lei6e7687172018-07-03 09:03:16 -06001469#define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1470#define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1471/*
1472 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1473 * - EWMA is one simple way to compute running average value
1474 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1475 * - take 4 as factor for avoiding to get too small(0) result, and this
1476 * factor doesn't matter because EWMA decreases exponentially
1477 */
1478static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1479{
1480 unsigned int ewma;
1481
Ming Lei6e7687172018-07-03 09:03:16 -06001482 ewma = hctx->dispatch_busy;
1483
1484 if (!ewma && !busy)
1485 return;
1486
1487 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1488 if (busy)
1489 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1490 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1491
1492 hctx->dispatch_busy = ewma;
1493}
1494
Ming Lei86ff7c22018-01-30 22:04:57 -05001495#define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1496
Johannes Thumshirnc92a4102020-03-25 00:24:44 +09001497static void blk_mq_handle_dev_resource(struct request *rq,
1498 struct list_head *list)
1499{
1500 struct request *next =
1501 list_first_entry_or_null(list, struct request, queuelist);
1502
1503 /*
1504 * If an I/O scheduler has been configured and we got a driver tag for
1505 * the next request already, free it.
1506 */
1507 if (next)
1508 blk_mq_put_driver_tag(next);
1509
1510 list_add(&rq->queuelist, list);
1511 __blk_mq_requeue_request(rq);
1512}
1513
Keith Busch0512a752020-05-12 17:55:47 +09001514static void blk_mq_handle_zone_resource(struct request *rq,
1515 struct list_head *zone_list)
1516{
1517 /*
1518 * If we end up here it is because we cannot dispatch a request to a
1519 * specific zone due to LLD level zone-write locking or other zone
1520 * related resource not being available. In this case, set the request
1521 * aside in zone_list for retrying it later.
1522 */
1523 list_add(&rq->queuelist, zone_list);
1524 __blk_mq_requeue_request(rq);
1525}
1526
Ming Lei75383522020-06-30 18:24:58 +08001527enum prep_dispatch {
1528 PREP_DISPATCH_OK,
1529 PREP_DISPATCH_NO_TAG,
1530 PREP_DISPATCH_NO_BUDGET,
1531};
1532
1533static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq,
1534 bool need_budget)
1535{
1536 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Ming Lei2a5a24a2021-01-22 10:33:12 +08001537 int budget_token = -1;
Ming Lei75383522020-06-30 18:24:58 +08001538
Ming Lei2a5a24a2021-01-22 10:33:12 +08001539 if (need_budget) {
1540 budget_token = blk_mq_get_dispatch_budget(rq->q);
1541 if (budget_token < 0) {
1542 blk_mq_put_driver_tag(rq);
1543 return PREP_DISPATCH_NO_BUDGET;
1544 }
1545 blk_mq_set_rq_budget_token(rq, budget_token);
Ming Lei75383522020-06-30 18:24:58 +08001546 }
1547
1548 if (!blk_mq_get_driver_tag(rq)) {
1549 /*
1550 * The initial allocation attempt failed, so we need to
1551 * rerun the hardware queue when a tag is freed. The
1552 * waitqueue takes care of that. If the queue is run
1553 * before we add this entry back on the dispatch list,
1554 * we'll re-run it below.
1555 */
1556 if (!blk_mq_mark_tag_wait(hctx, rq)) {
Ming Lei1fd40b52020-06-30 18:25:00 +08001557 /*
1558 * All budgets not got from this function will be put
1559 * together during handling partial dispatch
1560 */
1561 if (need_budget)
Ming Lei2a5a24a2021-01-22 10:33:12 +08001562 blk_mq_put_dispatch_budget(rq->q, budget_token);
Ming Lei75383522020-06-30 18:24:58 +08001563 return PREP_DISPATCH_NO_TAG;
1564 }
1565 }
1566
1567 return PREP_DISPATCH_OK;
1568}
1569
Ming Lei1fd40b52020-06-30 18:25:00 +08001570/* release all allocated budgets before calling to blk_mq_dispatch_rq_list */
1571static void blk_mq_release_budgets(struct request_queue *q,
Ming Lei2a5a24a2021-01-22 10:33:12 +08001572 struct list_head *list)
Ming Lei1fd40b52020-06-30 18:25:00 +08001573{
Ming Lei2a5a24a2021-01-22 10:33:12 +08001574 struct request *rq;
Ming Lei1fd40b52020-06-30 18:25:00 +08001575
Ming Lei2a5a24a2021-01-22 10:33:12 +08001576 list_for_each_entry(rq, list, queuelist) {
1577 int budget_token = blk_mq_get_rq_budget_token(rq);
1578
1579 if (budget_token >= 0)
1580 blk_mq_put_dispatch_budget(q, budget_token);
1581 }
Ming Lei1fd40b52020-06-30 18:25:00 +08001582}
1583
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001584/*
1585 * Returns true if we did some work AND can potentially do more.
1586 */
Ming Lei445874e2020-06-30 18:24:57 +08001587bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list,
Ming Lei1fd40b52020-06-30 18:25:00 +08001588 unsigned int nr_budgets)
Jens Axboef04c3df2016-12-07 08:41:17 -07001589{
Ming Lei75383522020-06-30 18:24:58 +08001590 enum prep_dispatch prep;
Ming Lei445874e2020-06-30 18:24:57 +08001591 struct request_queue *q = hctx->queue;
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001592 struct request *rq, *nxt;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001593 int errors, queued;
Ming Lei86ff7c22018-01-30 22:04:57 -05001594 blk_status_t ret = BLK_STS_OK;
Keith Busch0512a752020-05-12 17:55:47 +09001595 LIST_HEAD(zone_list);
Jens Axboef04c3df2016-12-07 08:41:17 -07001596
Omar Sandoval81380ca2017-04-07 08:56:26 -06001597 if (list_empty(list))
1598 return false;
1599
Jens Axboef04c3df2016-12-07 08:41:17 -07001600 /*
Jens Axboef04c3df2016-12-07 08:41:17 -07001601 * Now process all the entries, sending them to the driver.
1602 */
Jens Axboe93efe982017-03-24 12:04:19 -06001603 errors = queued = 0;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001604 do {
Jens Axboef04c3df2016-12-07 08:41:17 -07001605 struct blk_mq_queue_data bd;
1606
1607 rq = list_first_entry(list, struct request, queuelist);
Ming Lei0bca7992018-04-05 00:35:21 +08001608
Ming Lei445874e2020-06-30 18:24:57 +08001609 WARN_ON_ONCE(hctx != rq->mq_hctx);
Ming Lei1fd40b52020-06-30 18:25:00 +08001610 prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets);
Ming Lei75383522020-06-30 18:24:58 +08001611 if (prep != PREP_DISPATCH_OK)
Ming Lei0bca7992018-04-05 00:35:21 +08001612 break;
Ming Leide148292017-10-14 17:22:29 +08001613
Jens Axboef04c3df2016-12-07 08:41:17 -07001614 list_del_init(&rq->queuelist);
1615
1616 bd.rq = rq;
Jens Axboe113285b2017-03-02 13:26:04 -07001617
1618 /*
1619 * Flag last if we have no more requests, or if we have more
1620 * but can't assign a driver tag to it.
1621 */
1622 if (list_empty(list))
1623 bd.last = true;
1624 else {
Jens Axboe113285b2017-03-02 13:26:04 -07001625 nxt = list_first_entry(list, struct request, queuelist);
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001626 bd.last = !blk_mq_get_driver_tag(nxt);
Jens Axboe113285b2017-03-02 13:26:04 -07001627 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001628
Ming Lei1fd40b52020-06-30 18:25:00 +08001629 /*
1630 * once the request is queued to lld, no need to cover the
1631 * budget any more
1632 */
1633 if (nr_budgets)
1634 nr_budgets--;
Jens Axboef04c3df2016-12-07 08:41:17 -07001635 ret = q->mq_ops->queue_rq(hctx, &bd);
Ming Lei7bf13722020-07-01 21:58:57 +08001636 switch (ret) {
1637 case BLK_STS_OK:
1638 queued++;
Jens Axboef04c3df2016-12-07 08:41:17 -07001639 break;
Ming Lei7bf13722020-07-01 21:58:57 +08001640 case BLK_STS_RESOURCE:
1641 case BLK_STS_DEV_RESOURCE:
1642 blk_mq_handle_dev_resource(rq, list);
1643 goto out;
1644 case BLK_STS_ZONE_RESOURCE:
Keith Busch0512a752020-05-12 17:55:47 +09001645 /*
1646 * Move the request to zone_list and keep going through
1647 * the dispatch list to find more requests the drive can
1648 * accept.
1649 */
1650 blk_mq_handle_zone_resource(rq, &zone_list);
Ming Lei7bf13722020-07-01 21:58:57 +08001651 break;
1652 default:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001653 errors++;
Hannes Reineckee21ee5a2020-09-30 10:02:53 +02001654 blk_mq_end_request(rq, ret);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001655 }
Omar Sandoval81380ca2017-04-07 08:56:26 -06001656 } while (!list_empty(list));
Ming Lei7bf13722020-07-01 21:58:57 +08001657out:
Keith Busch0512a752020-05-12 17:55:47 +09001658 if (!list_empty(&zone_list))
1659 list_splice_tail_init(&zone_list, list);
1660
yangerkun632bfb62020-09-05 19:25:56 +08001661 /* If we didn't flush the entire list, we could have told the driver
1662 * there was more coming, but that turned out to be a lie.
1663 */
1664 if ((!list_empty(list) || errors) && q->mq_ops->commit_rqs && queued)
1665 q->mq_ops->commit_rqs(hctx);
Jens Axboef04c3df2016-12-07 08:41:17 -07001666 /*
1667 * Any items that need requeuing? Stuff them into hctx->dispatch,
1668 * that is where we will continue on next queue run.
1669 */
1670 if (!list_empty(list)) {
Ming Lei86ff7c22018-01-30 22:04:57 -05001671 bool needs_restart;
Ming Lei75383522020-06-30 18:24:58 +08001672 /* For non-shared tags, the RESTART check will suffice */
1673 bool no_tag = prep == PREP_DISPATCH_NO_TAG &&
Ming Lei51db1c32020-08-19 23:20:19 +08001674 (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED);
Ming Lei75383522020-06-30 18:24:58 +08001675 bool no_budget_avail = prep == PREP_DISPATCH_NO_BUDGET;
Ming Lei86ff7c22018-01-30 22:04:57 -05001676
Ming Lei2a5a24a2021-01-22 10:33:12 +08001677 if (nr_budgets)
1678 blk_mq_release_budgets(q, list);
Jens Axboef04c3df2016-12-07 08:41:17 -07001679
1680 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001681 list_splice_tail_init(list, &hctx->dispatch);
Jens Axboef04c3df2016-12-07 08:41:17 -07001682 spin_unlock(&hctx->lock);
1683
1684 /*
Ming Leid7d85352020-08-17 18:01:15 +08001685 * Order adding requests to hctx->dispatch and checking
1686 * SCHED_RESTART flag. The pair of this smp_mb() is the one
1687 * in blk_mq_sched_restart(). Avoid restart code path to
1688 * miss the new added requests to hctx->dispatch, meantime
1689 * SCHED_RESTART is observed here.
1690 */
1691 smp_mb();
1692
1693 /*
Bart Van Assche710c7852017-04-07 11:16:51 -07001694 * If SCHED_RESTART was set by the caller of this function and
1695 * it is no longer set that means that it was cleared by another
1696 * thread and hence that a queue rerun is needed.
Jens Axboef04c3df2016-12-07 08:41:17 -07001697 *
Jens Axboeeb619fd2017-11-09 08:32:43 -07001698 * If 'no_tag' is set, that means that we failed getting
1699 * a driver tag with an I/O scheduler attached. If our dispatch
1700 * waitqueue is no longer active, ensure that we run the queue
1701 * AFTER adding our entries back to the list.
Jens Axboebd166ef2017-01-17 06:03:22 -07001702 *
Bart Van Assche710c7852017-04-07 11:16:51 -07001703 * If no I/O scheduler has been configured it is possible that
1704 * the hardware queue got stopped and restarted before requests
1705 * were pushed back onto the dispatch list. Rerun the queue to
1706 * avoid starvation. Notes:
1707 * - blk_mq_run_hw_queue() checks whether or not a queue has
1708 * been stopped before rerunning a queue.
1709 * - Some but not all block drivers stop a queue before
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001710 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
Bart Van Assche710c7852017-04-07 11:16:51 -07001711 * and dm-rq.
Ming Lei86ff7c22018-01-30 22:04:57 -05001712 *
1713 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1714 * bit is set, run queue after a delay to avoid IO stalls
Douglas Andersonab3cee32020-04-20 09:24:51 -07001715 * that could otherwise occur if the queue is idle. We'll do
1716 * similar if we couldn't get budget and SCHED_RESTART is set.
Jens Axboebd166ef2017-01-17 06:03:22 -07001717 */
Ming Lei86ff7c22018-01-30 22:04:57 -05001718 needs_restart = blk_mq_sched_needs_restart(hctx);
1719 if (!needs_restart ||
Jens Axboeeb619fd2017-11-09 08:32:43 -07001720 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
Jens Axboebd166ef2017-01-17 06:03:22 -07001721 blk_mq_run_hw_queue(hctx, true);
Douglas Andersonab3cee32020-04-20 09:24:51 -07001722 else if (needs_restart && (ret == BLK_STS_RESOURCE ||
1723 no_budget_avail))
Ming Lei86ff7c22018-01-30 22:04:57 -05001724 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001725
Ming Lei6e7687172018-07-03 09:03:16 -06001726 blk_mq_update_dispatch_busy(hctx, true);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001727 return false;
Ming Lei6e7687172018-07-03 09:03:16 -06001728 } else
1729 blk_mq_update_dispatch_busy(hctx, false);
Jens Axboef04c3df2016-12-07 08:41:17 -07001730
Jens Axboe93efe982017-03-24 12:04:19 -06001731 return (queued + errors) != 0;
Jens Axboef04c3df2016-12-07 08:41:17 -07001732}
1733
André Almeida105663f2020-01-06 15:08:18 -03001734/**
1735 * __blk_mq_run_hw_queue - Run a hardware queue.
1736 * @hctx: Pointer to the hardware queue to run.
1737 *
1738 * Send pending requests to the hardware.
1739 */
Bart Van Assche6a83e742016-11-02 10:09:51 -06001740static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1741{
1742 int srcu_idx;
1743
Jens Axboeb7a71e62017-08-01 09:28:24 -06001744 /*
Jens Axboeb7a71e62017-08-01 09:28:24 -06001745 * We can't run the queue inline with ints disabled. Ensure that
1746 * we catch bad users of this early.
1747 */
1748 WARN_ON_ONCE(in_interrupt());
1749
Jens Axboe04ced152018-01-09 08:29:46 -08001750 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
Jens Axboebf4907c2017-03-30 12:30:39 -06001751
Jens Axboe04ced152018-01-09 08:29:46 -08001752 hctx_lock(hctx, &srcu_idx);
1753 blk_mq_sched_dispatch_requests(hctx);
1754 hctx_unlock(hctx, srcu_idx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001755}
1756
Ming Leif82ddf12018-04-08 17:48:10 +08001757static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1758{
1759 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1760
1761 if (cpu >= nr_cpu_ids)
1762 cpu = cpumask_first(hctx->cpumask);
1763 return cpu;
1764}
1765
Jens Axboe506e9312014-05-07 10:26:44 -06001766/*
1767 * It'd be great if the workqueue API had a way to pass
1768 * in a mask and had some smarts for more clever placement.
1769 * For now we just round-robin here, switching for every
1770 * BLK_MQ_CPU_WORK_BATCH queued items.
1771 */
1772static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1773{
Ming Lei7bed4592018-01-18 00:41:51 +08001774 bool tried = false;
Ming Lei476f8c92018-04-08 17:48:09 +08001775 int next_cpu = hctx->next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001776
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001777 if (hctx->queue->nr_hw_queues == 1)
1778 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -06001779
1780 if (--hctx->next_cpu_batch <= 0) {
Ming Lei7bed4592018-01-18 00:41:51 +08001781select_cpu:
Ming Lei476f8c92018-04-08 17:48:09 +08001782 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08001783 cpu_online_mask);
Jens Axboe506e9312014-05-07 10:26:44 -06001784 if (next_cpu >= nr_cpu_ids)
Ming Leif82ddf12018-04-08 17:48:10 +08001785 next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06001786 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1787 }
1788
Ming Lei7bed4592018-01-18 00:41:51 +08001789 /*
1790 * Do unbound schedule if we can't find a online CPU for this hctx,
1791 * and it should only happen in the path of handling CPU DEAD.
1792 */
Ming Lei476f8c92018-04-08 17:48:09 +08001793 if (!cpu_online(next_cpu)) {
Ming Lei7bed4592018-01-18 00:41:51 +08001794 if (!tried) {
1795 tried = true;
1796 goto select_cpu;
1797 }
1798
1799 /*
1800 * Make sure to re-select CPU next time once after CPUs
1801 * in hctx->cpumask become online again.
1802 */
Ming Lei476f8c92018-04-08 17:48:09 +08001803 hctx->next_cpu = next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001804 hctx->next_cpu_batch = 1;
1805 return WORK_CPU_UNBOUND;
1806 }
Ming Lei476f8c92018-04-08 17:48:09 +08001807
1808 hctx->next_cpu = next_cpu;
1809 return next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -06001810}
1811
André Almeida105663f2020-01-06 15:08:18 -03001812/**
1813 * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue.
1814 * @hctx: Pointer to the hardware queue to run.
1815 * @async: If we want to run the queue asynchronously.
Minwoo Imfa94ba82020-12-05 00:20:55 +09001816 * @msecs: Milliseconds of delay to wait before running the queue.
André Almeida105663f2020-01-06 15:08:18 -03001817 *
1818 * If !@async, try to run the queue now. Else, run the queue asynchronously and
1819 * with a delay of @msecs.
1820 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001821static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1822 unsigned long msecs)
Jens Axboe320ae512013-10-24 09:20:05 +01001823{
Bart Van Assche5435c022017-06-20 11:15:49 -07001824 if (unlikely(blk_mq_hctx_stopped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +01001825 return;
1826
Jens Axboe1b792f22016-09-21 10:12:13 -06001827 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001828 int cpu = get_cpu();
1829 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +01001830 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001831 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +01001832 return;
1833 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001834
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001835 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -06001836 }
Paolo Bonzini398205b2014-11-07 23:03:59 +01001837
Bart Van Asscheae943d22018-01-19 08:58:55 -08001838 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1839 msecs_to_jiffies(msecs));
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001840}
1841
André Almeida105663f2020-01-06 15:08:18 -03001842/**
1843 * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
1844 * @hctx: Pointer to the hardware queue to run.
Minwoo Imfa94ba82020-12-05 00:20:55 +09001845 * @msecs: Milliseconds of delay to wait before running the queue.
André Almeida105663f2020-01-06 15:08:18 -03001846 *
1847 * Run a hardware queue asynchronously with a delay of @msecs.
1848 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001849void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1850{
1851 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1852}
1853EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1854
André Almeida105663f2020-01-06 15:08:18 -03001855/**
1856 * blk_mq_run_hw_queue - Start to run a hardware queue.
1857 * @hctx: Pointer to the hardware queue to run.
1858 * @async: If we want to run the queue asynchronously.
1859 *
1860 * Check if the request queue is not in a quiesced state and if there are
1861 * pending requests to be sent. If this is true, run the queue to send requests
1862 * to hardware.
1863 */
John Garry626fb732019-10-30 00:59:30 +08001864void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001865{
Ming Lei24f5a902018-01-06 16:27:38 +08001866 int srcu_idx;
1867 bool need_run;
1868
1869 /*
1870 * When queue is quiesced, we may be switching io scheduler, or
1871 * updating nr_hw_queues, or other things, and we can't run queue
1872 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
1873 *
1874 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
1875 * quiesced.
1876 */
Jens Axboe04ced152018-01-09 08:29:46 -08001877 hctx_lock(hctx, &srcu_idx);
1878 need_run = !blk_queue_quiesced(hctx->queue) &&
1879 blk_mq_hctx_has_pending(hctx);
1880 hctx_unlock(hctx, srcu_idx);
Ming Lei24f5a902018-01-06 16:27:38 +08001881
John Garry626fb732019-10-30 00:59:30 +08001882 if (need_run)
Jens Axboe79f720a2017-11-10 09:13:21 -07001883 __blk_mq_delay_run_hw_queue(hctx, async, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01001884}
Omar Sandoval5b727272017-04-14 01:00:00 -07001885EXPORT_SYMBOL(blk_mq_run_hw_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01001886
Jan Karab6e68ee2021-01-11 17:47:17 +01001887/*
1888 * Is the request queue handled by an IO scheduler that does not respect
1889 * hardware queues when dispatching?
1890 */
1891static bool blk_mq_has_sqsched(struct request_queue *q)
1892{
1893 struct elevator_queue *e = q->elevator;
1894
1895 if (e && e->type->ops.dispatch_request &&
1896 !(e->type->elevator_features & ELEVATOR_F_MQ_AWARE))
1897 return true;
1898 return false;
1899}
1900
1901/*
1902 * Return prefered queue to dispatch from (if any) for non-mq aware IO
1903 * scheduler.
1904 */
1905static struct blk_mq_hw_ctx *blk_mq_get_sq_hctx(struct request_queue *q)
1906{
1907 struct blk_mq_hw_ctx *hctx;
1908
1909 /*
1910 * If the IO scheduler does not respect hardware queues when
1911 * dispatching, we just don't bother with multiple HW queues and
1912 * dispatch from hctx for the current CPU since running multiple queues
1913 * just causes lock contention inside the scheduler and pointless cache
1914 * bouncing.
1915 */
1916 hctx = blk_mq_map_queue_type(q, HCTX_TYPE_DEFAULT,
1917 raw_smp_processor_id());
1918 if (!blk_mq_hctx_stopped(hctx))
1919 return hctx;
1920 return NULL;
1921}
1922
André Almeida105663f2020-01-06 15:08:18 -03001923/**
Mauro Carvalho Chehab24f7bb82020-10-23 18:32:54 +02001924 * blk_mq_run_hw_queues - Run all hardware queues in a request queue.
André Almeida105663f2020-01-06 15:08:18 -03001925 * @q: Pointer to the request queue to run.
1926 * @async: If we want to run the queue asynchronously.
1927 */
Mike Snitzerb94ec292015-03-11 23:56:38 -04001928void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001929{
Jan Karab6e68ee2021-01-11 17:47:17 +01001930 struct blk_mq_hw_ctx *hctx, *sq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001931 int i;
1932
Jan Karab6e68ee2021-01-11 17:47:17 +01001933 sq_hctx = NULL;
1934 if (blk_mq_has_sqsched(q))
1935 sq_hctx = blk_mq_get_sq_hctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001936 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe79f720a2017-11-10 09:13:21 -07001937 if (blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +01001938 continue;
Jan Karab6e68ee2021-01-11 17:47:17 +01001939 /*
1940 * Dispatch from this hctx either if there's no hctx preferred
1941 * by IO scheduler or if it has requests that bypass the
1942 * scheduler.
1943 */
1944 if (!sq_hctx || sq_hctx == hctx ||
1945 !list_empty_careful(&hctx->dispatch))
1946 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001947 }
1948}
Mike Snitzerb94ec292015-03-11 23:56:38 -04001949EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001950
Bart Van Asschefd001442016-10-28 17:19:37 -07001951/**
Douglas Andersonb9151e72020-04-20 09:24:52 -07001952 * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously.
1953 * @q: Pointer to the request queue to run.
Minwoo Imfa94ba82020-12-05 00:20:55 +09001954 * @msecs: Milliseconds of delay to wait before running the queues.
Douglas Andersonb9151e72020-04-20 09:24:52 -07001955 */
1956void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs)
1957{
Jan Karab6e68ee2021-01-11 17:47:17 +01001958 struct blk_mq_hw_ctx *hctx, *sq_hctx;
Douglas Andersonb9151e72020-04-20 09:24:52 -07001959 int i;
1960
Jan Karab6e68ee2021-01-11 17:47:17 +01001961 sq_hctx = NULL;
1962 if (blk_mq_has_sqsched(q))
1963 sq_hctx = blk_mq_get_sq_hctx(q);
Douglas Andersonb9151e72020-04-20 09:24:52 -07001964 queue_for_each_hw_ctx(q, hctx, i) {
1965 if (blk_mq_hctx_stopped(hctx))
1966 continue;
Jan Karab6e68ee2021-01-11 17:47:17 +01001967 /*
1968 * Dispatch from this hctx either if there's no hctx preferred
1969 * by IO scheduler or if it has requests that bypass the
1970 * scheduler.
1971 */
1972 if (!sq_hctx || sq_hctx == hctx ||
1973 !list_empty_careful(&hctx->dispatch))
1974 blk_mq_delay_run_hw_queue(hctx, msecs);
Douglas Andersonb9151e72020-04-20 09:24:52 -07001975 }
1976}
1977EXPORT_SYMBOL(blk_mq_delay_run_hw_queues);
1978
1979/**
Bart Van Asschefd001442016-10-28 17:19:37 -07001980 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1981 * @q: request queue.
1982 *
1983 * The caller is responsible for serializing this function against
1984 * blk_mq_{start,stop}_hw_queue().
1985 */
1986bool blk_mq_queue_stopped(struct request_queue *q)
1987{
1988 struct blk_mq_hw_ctx *hctx;
1989 int i;
1990
1991 queue_for_each_hw_ctx(q, hctx, i)
1992 if (blk_mq_hctx_stopped(hctx))
1993 return true;
1994
1995 return false;
1996}
1997EXPORT_SYMBOL(blk_mq_queue_stopped);
1998
Ming Lei39a70c72017-06-06 23:22:09 +08001999/*
2000 * This function is often used for pausing .queue_rq() by driver when
2001 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07002002 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08002003 *
2004 * We do not guarantee that dispatch can be drained or blocked
2005 * after blk_mq_stop_hw_queue() returns. Please use
2006 * blk_mq_quiesce_queue() for that requirement.
2007 */
Jens Axboe320ae512013-10-24 09:20:05 +01002008void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
2009{
Ming Lei641a9ed2017-06-06 23:22:10 +08002010 cancel_delayed_work(&hctx->run_work);
2011
2012 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboe320ae512013-10-24 09:20:05 +01002013}
2014EXPORT_SYMBOL(blk_mq_stop_hw_queue);
2015
Ming Lei39a70c72017-06-06 23:22:09 +08002016/*
2017 * This function is often used for pausing .queue_rq() by driver when
2018 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07002019 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08002020 *
2021 * We do not guarantee that dispatch can be drained or blocked
2022 * after blk_mq_stop_hw_queues() returns. Please use
2023 * blk_mq_quiesce_queue() for that requirement.
2024 */
Jens Axboe2719aa22017-05-03 11:08:14 -06002025void blk_mq_stop_hw_queues(struct request_queue *q)
2026{
Ming Lei641a9ed2017-06-06 23:22:10 +08002027 struct blk_mq_hw_ctx *hctx;
2028 int i;
2029
2030 queue_for_each_hw_ctx(q, hctx, i)
2031 blk_mq_stop_hw_queue(hctx);
Christoph Hellwig280d45f2013-10-25 14:45:58 +01002032}
2033EXPORT_SYMBOL(blk_mq_stop_hw_queues);
2034
Jens Axboe320ae512013-10-24 09:20:05 +01002035void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
2036{
2037 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -06002038
Jens Axboe0ffbce82014-06-25 08:22:34 -06002039 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +01002040}
2041EXPORT_SYMBOL(blk_mq_start_hw_queue);
2042
Christoph Hellwig2f268552014-04-16 09:44:56 +02002043void blk_mq_start_hw_queues(struct request_queue *q)
2044{
2045 struct blk_mq_hw_ctx *hctx;
2046 int i;
2047
2048 queue_for_each_hw_ctx(q, hctx, i)
2049 blk_mq_start_hw_queue(hctx);
2050}
2051EXPORT_SYMBOL(blk_mq_start_hw_queues);
2052
Jens Axboeae911c52016-12-08 13:19:30 -07002053void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
2054{
2055 if (!blk_mq_hctx_stopped(hctx))
2056 return;
2057
2058 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
2059 blk_mq_run_hw_queue(hctx, async);
2060}
2061EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
2062
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02002063void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01002064{
2065 struct blk_mq_hw_ctx *hctx;
2066 int i;
2067
Jens Axboeae911c52016-12-08 13:19:30 -07002068 queue_for_each_hw_ctx(q, hctx, i)
2069 blk_mq_start_stopped_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01002070}
2071EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
2072
Christoph Hellwig70f4db62014-04-16 10:48:08 -06002073static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01002074{
2075 struct blk_mq_hw_ctx *hctx;
2076
Jens Axboe9f993732017-04-10 09:54:54 -06002077 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboe21c6e932017-04-10 09:54:56 -06002078
2079 /*
Ming Lei15fe8a902018-04-08 17:48:11 +08002080 * If we are stopped, don't run the queue.
Jens Axboe21c6e932017-04-10 09:54:56 -06002081 */
Yufen Yu08410312020-10-08 23:26:30 -04002082 if (blk_mq_hctx_stopped(hctx))
Jianchao Wang0196d6b2018-06-04 17:03:55 +08002083 return;
Jens Axboee4043dc2014-04-09 10:18:23 -06002084
Jens Axboe320ae512013-10-24 09:20:05 +01002085 __blk_mq_run_hw_queue(hctx);
2086}
2087
Ming Leicfd0c552015-10-20 23:13:57 +08002088static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08002089 struct request *rq,
2090 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01002091{
Jens Axboee57690f2016-08-24 15:34:35 -06002092 struct blk_mq_ctx *ctx = rq->mq_ctx;
Ming Leic16d6b52018-12-17 08:44:05 -07002093 enum hctx_type type = hctx->type;
Jens Axboee57690f2016-08-24 15:34:35 -06002094
Bart Van Assche7b607812017-06-20 11:15:47 -07002095 lockdep_assert_held(&ctx->lock);
2096
Christoph Hellwiga54895f2020-12-03 17:21:39 +01002097 trace_block_rq_insert(rq);
Jens Axboe01b983c2013-11-19 18:59:10 -07002098
Christoph Hellwig72a0a362014-02-07 10:22:36 -08002099 if (at_head)
Ming Leic16d6b52018-12-17 08:44:05 -07002100 list_add(&rq->queuelist, &ctx->rq_lists[type]);
Christoph Hellwig72a0a362014-02-07 10:22:36 -08002101 else
Ming Leic16d6b52018-12-17 08:44:05 -07002102 list_add_tail(&rq->queuelist, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08002103}
Jens Axboe4bb659b2014-05-09 09:36:49 -06002104
Jens Axboe2c3ad662016-12-14 14:34:47 -07002105void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
2106 bool at_head)
Ming Leicfd0c552015-10-20 23:13:57 +08002107{
2108 struct blk_mq_ctx *ctx = rq->mq_ctx;
2109
Bart Van Assche7b607812017-06-20 11:15:47 -07002110 lockdep_assert_held(&ctx->lock);
2111
Jens Axboee57690f2016-08-24 15:34:35 -06002112 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01002113 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01002114}
2115
André Almeida105663f2020-01-06 15:08:18 -03002116/**
2117 * blk_mq_request_bypass_insert - Insert a request at dispatch list.
2118 * @rq: Pointer to request to be inserted.
Randy Dunlap26bfeb22020-08-16 16:39:34 -07002119 * @at_head: true if the request should be inserted at the head of the list.
André Almeida105663f2020-01-06 15:08:18 -03002120 * @run_queue: If we should run the hardware queue after inserting the request.
2121 *
Jens Axboe157f3772017-09-11 16:43:57 -06002122 * Should only be used carefully, when the caller knows we want to
2123 * bypass a potential IO scheduler on the target device.
2124 */
Ming Lei01e99ae2020-02-25 09:04:32 +08002125void blk_mq_request_bypass_insert(struct request *rq, bool at_head,
2126 bool run_queue)
Jens Axboe157f3772017-09-11 16:43:57 -06002127{
Jens Axboeea4f9952018-10-29 15:06:13 -06002128 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe157f3772017-09-11 16:43:57 -06002129
2130 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08002131 if (at_head)
2132 list_add(&rq->queuelist, &hctx->dispatch);
2133 else
2134 list_add_tail(&rq->queuelist, &hctx->dispatch);
Jens Axboe157f3772017-09-11 16:43:57 -06002135 spin_unlock(&hctx->lock);
2136
Ming Leib0850292017-11-02 23:24:34 +08002137 if (run_queue)
2138 blk_mq_run_hw_queue(hctx, false);
Jens Axboe157f3772017-09-11 16:43:57 -06002139}
2140
Jens Axboebd166ef2017-01-17 06:03:22 -07002141void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
2142 struct list_head *list)
Jens Axboe320ae512013-10-24 09:20:05 +01002143
2144{
Ming Lei3f0cedc2018-07-02 17:35:58 +08002145 struct request *rq;
Ming Leic16d6b52018-12-17 08:44:05 -07002146 enum hctx_type type = hctx->type;
Ming Lei3f0cedc2018-07-02 17:35:58 +08002147
Jens Axboe320ae512013-10-24 09:20:05 +01002148 /*
2149 * preemption doesn't flush plug list, so it's possible ctx->cpu is
2150 * offline now
2151 */
Ming Lei3f0cedc2018-07-02 17:35:58 +08002152 list_for_each_entry(rq, list, queuelist) {
Jens Axboee57690f2016-08-24 15:34:35 -06002153 BUG_ON(rq->mq_ctx != ctx);
Christoph Hellwiga54895f2020-12-03 17:21:39 +01002154 trace_block_rq_insert(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01002155 }
Ming Lei3f0cedc2018-07-02 17:35:58 +08002156
2157 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002158 list_splice_tail_init(list, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08002159 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01002160 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01002161}
2162
Jens Axboedc5fc3612021-10-19 06:02:30 -06002163static void blk_mq_commit_rqs(struct blk_mq_hw_ctx *hctx, int *queued,
2164 bool from_schedule)
2165{
2166 if (hctx->queue->mq_ops->commit_rqs) {
2167 trace_block_unplug(hctx->queue, *queued, !from_schedule);
2168 hctx->queue->mq_ops->commit_rqs(hctx);
2169 }
2170 *queued = 0;
2171}
2172
2173static void blk_mq_plug_issue_direct(struct blk_plug *plug, bool from_schedule)
2174{
2175 struct blk_mq_hw_ctx *hctx = NULL;
2176 struct request *rq;
2177 int queued = 0;
2178 int errors = 0;
2179
2180 while ((rq = rq_list_pop(&plug->mq_list))) {
2181 bool last = rq_list_empty(plug->mq_list);
2182 blk_status_t ret;
2183
2184 if (hctx != rq->mq_hctx) {
2185 if (hctx)
2186 blk_mq_commit_rqs(hctx, &queued, from_schedule);
2187 hctx = rq->mq_hctx;
2188 }
2189
2190 ret = blk_mq_request_issue_directly(rq, last);
2191 switch (ret) {
2192 case BLK_STS_OK:
2193 queued++;
2194 break;
2195 case BLK_STS_RESOURCE:
2196 case BLK_STS_DEV_RESOURCE:
2197 blk_mq_request_bypass_insert(rq, false, last);
2198 blk_mq_commit_rqs(hctx, &queued, from_schedule);
2199 return;
2200 default:
2201 blk_mq_end_request(rq, ret);
2202 errors++;
2203 break;
2204 }
2205 }
2206
2207 /*
2208 * If we didn't flush the entire list, we could have told the driver
2209 * there was more coming, but that turned out to be a lie.
2210 */
2211 if (errors)
2212 blk_mq_commit_rqs(hctx, &queued, from_schedule);
2213}
2214
Jens Axboe320ae512013-10-24 09:20:05 +01002215void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
2216{
Jens Axboebc490f82021-10-18 10:12:12 -06002217 struct blk_mq_hw_ctx *this_hctx;
2218 struct blk_mq_ctx *this_ctx;
2219 unsigned int depth;
Jens Axboe320ae512013-10-24 09:20:05 +01002220 LIST_HEAD(list);
Jens Axboe320ae512013-10-24 09:20:05 +01002221
Jens Axboebc490f82021-10-18 10:12:12 -06002222 if (rq_list_empty(plug->mq_list))
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03002223 return;
Dongli Zhangbcc816d2019-04-04 10:57:44 +08002224 plug->rq_count = 0;
2225
Jens Axboedc5fc3612021-10-19 06:02:30 -06002226 if (!plug->multiple_queues && !plug->has_elevator) {
2227 blk_mq_plug_issue_direct(plug, from_schedule);
2228 if (rq_list_empty(plug->mq_list))
2229 return;
2230 }
2231
Jens Axboebc490f82021-10-18 10:12:12 -06002232 this_hctx = NULL;
2233 this_ctx = NULL;
2234 depth = 0;
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03002235 do {
Jens Axboebc490f82021-10-18 10:12:12 -06002236 struct request *rq;
Jens Axboe320ae512013-10-24 09:20:05 +01002237
Jens Axboebc490f82021-10-18 10:12:12 -06002238 rq = rq_list_pop(&plug->mq_list);
2239
2240 if (!this_hctx) {
2241 this_hctx = rq->mq_hctx;
2242 this_ctx = rq->mq_ctx;
2243 } else if (this_hctx != rq->mq_hctx || this_ctx != rq->mq_ctx) {
2244 trace_block_unplug(this_hctx->queue, depth,
2245 !from_schedule);
2246 blk_mq_sched_insert_requests(this_hctx, this_ctx,
2247 &list, from_schedule);
2248 depth = 0;
2249 this_hctx = rq->mq_hctx;
2250 this_ctx = rq->mq_ctx;
2251
Jens Axboe320ae512013-10-24 09:20:05 +01002252 }
2253
Jens Axboebc490f82021-10-18 10:12:12 -06002254 list_add(&rq->queuelist, &list);
2255 depth++;
2256 } while (!rq_list_empty(plug->mq_list));
2257
2258 if (!list_empty(&list)) {
2259 trace_block_unplug(this_hctx->queue, depth, !from_schedule);
2260 blk_mq_sched_insert_requests(this_hctx, this_ctx, &list,
Jens Axboebd166ef2017-01-17 06:03:22 -07002261 from_schedule);
Jens Axboebc490f82021-10-18 10:12:12 -06002262 }
Jens Axboe320ae512013-10-24 09:20:05 +01002263}
2264
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002265static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
2266 unsigned int nr_segs)
Jens Axboe320ae512013-10-24 09:20:05 +01002267{
Eric Biggers93f221a2020-09-15 20:53:14 -07002268 int err;
2269
Christoph Hellwigf924cdd2019-06-06 12:29:00 +02002270 if (bio->bi_opf & REQ_RAHEAD)
2271 rq->cmd_flags |= REQ_FAILFAST_MASK;
2272
2273 rq->__sector = bio->bi_iter.bi_sector;
2274 rq->write_hint = bio->bi_write_hint;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002275 blk_rq_bio_prep(rq, bio, nr_segs);
Eric Biggers93f221a2020-09-15 20:53:14 -07002276
2277 /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */
2278 err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO);
2279 WARN_ON_ONCE(err);
Jens Axboe4b570522014-05-29 11:00:11 -06002280
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +02002281 blk_account_io_start(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01002282}
2283
Mike Snitzer0f955492018-01-17 11:25:56 -05002284static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig3e087732021-10-12 13:12:24 +02002285 struct request *rq, bool last)
Shaohua Lif984df12015-05-08 10:51:32 -07002286{
Shaohua Lif984df12015-05-08 10:51:32 -07002287 struct request_queue *q = rq->q;
Shaohua Lif984df12015-05-08 10:51:32 -07002288 struct blk_mq_queue_data bd = {
2289 .rq = rq,
Jens Axboebe94f052018-11-24 10:15:46 -07002290 .last = last,
Shaohua Lif984df12015-05-08 10:51:32 -07002291 };
Jens Axboef06345a2017-06-12 11:22:46 -06002292 blk_status_t ret;
Mike Snitzer0f955492018-01-17 11:25:56 -05002293
Mike Snitzer0f955492018-01-17 11:25:56 -05002294 /*
2295 * For OK queue, we are done. For error, caller may kill it.
2296 * Any other error (busy), just add it to our list as we
2297 * previously would have done.
2298 */
2299 ret = q->mq_ops->queue_rq(hctx, &bd);
2300 switch (ret) {
2301 case BLK_STS_OK:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002302 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05002303 break;
2304 case BLK_STS_RESOURCE:
Ming Lei86ff7c22018-01-30 22:04:57 -05002305 case BLK_STS_DEV_RESOURCE:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002306 blk_mq_update_dispatch_busy(hctx, true);
Mike Snitzer0f955492018-01-17 11:25:56 -05002307 __blk_mq_requeue_request(rq);
2308 break;
2309 default:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002310 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05002311 break;
2312 }
2313
2314 return ret;
2315}
2316
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002317static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
Mike Snitzer0f955492018-01-17 11:25:56 -05002318 struct request *rq,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002319 bool bypass_insert, bool last)
Mike Snitzer0f955492018-01-17 11:25:56 -05002320{
2321 struct request_queue *q = rq->q;
Ming Leid964f042017-06-06 23:22:00 +08002322 bool run_queue = true;
Ming Lei2a5a24a2021-01-22 10:33:12 +08002323 int budget_token;
Ming Leid964f042017-06-06 23:22:00 +08002324
Ming Lei23d4ee12018-01-18 12:06:59 +08002325 /*
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002326 * RCU or SRCU read lock is needed before checking quiesced flag.
Ming Lei23d4ee12018-01-18 12:06:59 +08002327 *
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002328 * When queue is stopped or quiesced, ignore 'bypass_insert' from
2329 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
2330 * and avoid driver to try to dispatch again.
Ming Lei23d4ee12018-01-18 12:06:59 +08002331 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002332 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
Ming Leid964f042017-06-06 23:22:00 +08002333 run_queue = false;
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002334 bypass_insert = false;
2335 goto insert;
Ming Leid964f042017-06-06 23:22:00 +08002336 }
Shaohua Lif984df12015-05-08 10:51:32 -07002337
Jens Axboe2ff06822021-10-15 09:44:38 -06002338 if ((rq->rq_flags & RQF_ELV) && !bypass_insert)
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002339 goto insert;
Bart Van Assche2253efc2016-10-28 17:20:02 -07002340
Ming Lei2a5a24a2021-01-22 10:33:12 +08002341 budget_token = blk_mq_get_dispatch_budget(q);
2342 if (budget_token < 0)
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002343 goto insert;
Jens Axboebd166ef2017-01-17 06:03:22 -07002344
Ming Lei2a5a24a2021-01-22 10:33:12 +08002345 blk_mq_set_rq_budget_token(rq, budget_token);
2346
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08002347 if (!blk_mq_get_driver_tag(rq)) {
Ming Lei2a5a24a2021-01-22 10:33:12 +08002348 blk_mq_put_dispatch_budget(q, budget_token);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002349 goto insert;
Ming Lei88022d72017-11-05 02:21:12 +08002350 }
Ming Leide148292017-10-14 17:22:29 +08002351
Christoph Hellwig3e087732021-10-12 13:12:24 +02002352 return __blk_mq_issue_directly(hctx, rq, last);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002353insert:
2354 if (bypass_insert)
2355 return BLK_STS_RESOURCE;
2356
Ming Leidb03f882020-08-18 17:07:28 +08002357 blk_mq_sched_insert_request(rq, false, run_queue, false);
2358
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002359 return BLK_STS_OK;
2360}
2361
André Almeida105663f2020-01-06 15:08:18 -03002362/**
2363 * blk_mq_try_issue_directly - Try to send a request directly to device driver.
2364 * @hctx: Pointer of the associated hardware queue.
2365 * @rq: Pointer to request to be sent.
André Almeida105663f2020-01-06 15:08:18 -03002366 *
2367 * If the device has enough resources to accept a new request now, send the
2368 * request directly to device driver. Else, insert at hctx->dispatch queue, so
2369 * we can try send it another time in the future. Requests inserted at this
2370 * queue have higher priority.
2371 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002372static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig3e087732021-10-12 13:12:24 +02002373 struct request *rq)
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002374{
2375 blk_status_t ret;
2376 int srcu_idx;
2377
2378 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
2379
2380 hctx_lock(hctx, &srcu_idx);
2381
Christoph Hellwig3e087732021-10-12 13:12:24 +02002382 ret = __blk_mq_try_issue_directly(hctx, rq, false, true);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002383 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
Ming Lei01e99ae2020-02-25 09:04:32 +08002384 blk_mq_request_bypass_insert(rq, false, true);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002385 else if (ret != BLK_STS_OK)
2386 blk_mq_end_request(rq, ret);
2387
Jens Axboe04ced152018-01-09 08:29:46 -08002388 hctx_unlock(hctx, srcu_idx);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002389}
2390
2391blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
2392{
2393 blk_status_t ret;
2394 int srcu_idx;
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002395 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
2396
2397 hctx_lock(hctx, &srcu_idx);
Christoph Hellwig3e087732021-10-12 13:12:24 +02002398 ret = __blk_mq_try_issue_directly(hctx, rq, true, last);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002399 hctx_unlock(hctx, srcu_idx);
Jianchao Wang7f556a42018-12-14 09:28:18 +08002400
2401 return ret;
Christoph Hellwig5eb61262017-03-22 15:01:51 -04002402}
2403
Ming Lei6ce3dd62018-07-10 09:03:31 +08002404void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
2405 struct list_head *list)
2406{
Keith Busch536167d42020-04-07 03:13:48 +09002407 int queued = 0;
yangerkun632bfb62020-09-05 19:25:56 +08002408 int errors = 0;
Keith Busch536167d42020-04-07 03:13:48 +09002409
Ming Lei6ce3dd62018-07-10 09:03:31 +08002410 while (!list_empty(list)) {
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002411 blk_status_t ret;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002412 struct request *rq = list_first_entry(list, struct request,
2413 queuelist);
2414
2415 list_del_init(&rq->queuelist);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002416 ret = blk_mq_request_issue_directly(rq, list_empty(list));
2417 if (ret != BLK_STS_OK) {
2418 if (ret == BLK_STS_RESOURCE ||
2419 ret == BLK_STS_DEV_RESOURCE) {
Ming Lei01e99ae2020-02-25 09:04:32 +08002420 blk_mq_request_bypass_insert(rq, false,
Jens Axboec616cbe2018-12-06 22:17:44 -07002421 list_empty(list));
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002422 break;
2423 }
2424 blk_mq_end_request(rq, ret);
yangerkun632bfb62020-09-05 19:25:56 +08002425 errors++;
Keith Busch536167d42020-04-07 03:13:48 +09002426 } else
2427 queued++;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002428 }
Jens Axboed666ba92018-11-27 17:02:25 -07002429
2430 /*
2431 * If we didn't flush the entire list, we could have told
2432 * the driver there was more coming, but that turned out to
2433 * be a lie.
2434 */
yangerkun632bfb62020-09-05 19:25:56 +08002435 if ((!list_empty(list) || errors) &&
2436 hctx->queue->mq_ops->commit_rqs && queued)
Jens Axboed666ba92018-11-27 17:02:25 -07002437 hctx->queue->mq_ops->commit_rqs(hctx);
Ming Lei6ce3dd62018-07-10 09:03:31 +08002438}
2439
Jens Axboece5b0092018-11-27 17:13:56 -07002440static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
2441{
Jens Axboebc490f82021-10-18 10:12:12 -06002442 if (!plug->multiple_queues) {
2443 struct request *nxt = rq_list_peek(&plug->mq_list);
Jens Axboece5b0092018-11-27 17:13:56 -07002444
Jens Axboebc490f82021-10-18 10:12:12 -06002445 if (nxt && nxt->q != rq->q)
Jens Axboece5b0092018-11-27 17:13:56 -07002446 plug->multiple_queues = true;
2447 }
Jens Axboedc5fc3612021-10-19 06:02:30 -06002448 if (!plug->has_elevator && (rq->rq_flags & RQF_ELV))
2449 plug->has_elevator = true;
Jens Axboebc490f82021-10-18 10:12:12 -06002450 rq->rq_next = NULL;
2451 rq_list_add(&plug->mq_list, rq);
2452 plug->rq_count++;
Jens Axboece5b0092018-11-27 17:13:56 -07002453}
2454
Song Liu7f2a6a62021-09-07 16:03:38 -07002455/*
Jens Axboeba0ffdd2021-10-06 12:01:07 -06002456 * Allow 2x BLK_MAX_REQUEST_COUNT requests on plug queue for multiple
Song Liu7f2a6a62021-09-07 16:03:38 -07002457 * queues. This is important for md arrays to benefit from merging
2458 * requests.
2459 */
2460static inline unsigned short blk_plug_max_rq_count(struct blk_plug *plug)
2461{
2462 if (plug->multiple_queues)
Jens Axboeba0ffdd2021-10-06 12:01:07 -06002463 return BLK_MAX_REQUEST_COUNT * 2;
Song Liu7f2a6a62021-09-07 16:03:38 -07002464 return BLK_MAX_REQUEST_COUNT;
2465}
2466
André Almeida105663f2020-01-06 15:08:18 -03002467/**
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02002468 * blk_mq_submit_bio - Create and send a request to block device.
André Almeida105663f2020-01-06 15:08:18 -03002469 * @bio: Bio pointer.
2470 *
2471 * Builds up a request structure from @q and @bio and send to the device. The
2472 * request may not be queued directly to hardware if:
2473 * * This request can be merged with another one
2474 * * We want to place request at plug queue for possible future merging
2475 * * There is an IO scheduler active at this queue
2476 *
2477 * It will not queue the request if there is an error with the bio, or at the
2478 * request creation.
André Almeida105663f2020-01-06 15:08:18 -03002479 */
Christoph Hellwig3e087732021-10-12 13:12:24 +02002480void blk_mq_submit_bio(struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06002481{
Pavel Begunkoved6cdde2021-10-14 15:03:30 +01002482 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002483 const int is_sync = op_is_sync(bio->bi_opf);
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07002484 const int is_flush_fua = op_is_flush(bio->bi_opf);
Jens Axboe07068d52014-05-22 10:40:51 -06002485 struct request *rq;
Shaohua Lif984df12015-05-08 10:51:32 -07002486 struct blk_plug *plug;
Jens Axboe87c037d2021-10-18 10:07:09 -06002487 bool same_queue_rq = false;
Jens Axboeabd45c12021-10-13 12:43:41 -06002488 unsigned int nr_segs = 1;
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002489 blk_status_t ret;
Jens Axboe07068d52014-05-22 10:40:51 -06002490
2491 blk_queue_bounce(q, &bio);
Jens Axboeabd45c12021-10-13 12:43:41 -06002492 if (blk_may_split(q, bio))
2493 __blk_queue_split(q, &bio, &nr_segs);
Wen Xiongf36ea502017-05-10 08:54:11 -05002494
Dmitry Monakhove23947b2017-06-29 11:31:11 -07002495 if (!bio_integrity_prep(bio))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002496 goto queue_exit;
Jens Axboe07068d52014-05-22 10:40:51 -06002497
Omar Sandoval87c279e2016-06-01 22:18:48 -07002498 if (!is_flush_fua && !blk_queue_nomerges(q) &&
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002499 blk_attempt_plug_merge(q, bio, nr_segs, &same_queue_rq))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002500 goto queue_exit;
Shaohua Lif984df12015-05-08 10:51:32 -07002501
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002502 if (blk_mq_sched_bio_merge(q, bio, nr_segs))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002503 goto queue_exit;
Jens Axboebd166ef2017-01-17 06:03:22 -07002504
Christoph Hellwigd5337562018-11-14 17:02:09 +01002505 rq_qos_throttle(q, bio);
Jens Axboe87760e52016-11-09 12:38:14 -07002506
Jens Axboe47c122e2021-10-06 06:34:11 -06002507 plug = blk_mq_plug(q, bio);
2508 if (plug && plug->cached_rq) {
Jens Axboe013a7f92021-10-13 07:58:52 -06002509 rq = rq_list_pop(&plug->cached_rq);
Jens Axboe47c122e2021-10-06 06:34:11 -06002510 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe47c122e2021-10-06 06:34:11 -06002511 } else {
Christoph Hellwig0f38d762021-10-12 12:40:45 +02002512 struct blk_mq_alloc_data data = {
2513 .q = q,
2514 .nr_tags = 1,
2515 .cmd_flags = bio->bi_opf,
2516 };
2517
Jens Axboe47c122e2021-10-06 06:34:11 -06002518 if (plug) {
2519 data.nr_tags = plug->nr_ios;
2520 plug->nr_ios = 1;
2521 data.cached_rq = &plug->cached_rq;
2522 }
Christoph Hellwigb90cfae2021-10-12 12:40:44 +02002523 rq = __blk_mq_alloc_requests(&data);
Jens Axboe47c122e2021-10-06 06:34:11 -06002524 if (unlikely(!rq)) {
2525 rq_qos_cleanup(q, bio);
2526 if (bio->bi_opf & REQ_NOWAIT)
2527 bio_wouldblock_error(bio);
2528 goto queue_exit;
2529 }
Jens Axboe87760e52016-11-09 12:38:14 -07002530 }
2531
Christoph Hellwige8a676d2020-12-03 17:21:36 +01002532 trace_block_getrq(bio);
Xiaoguang Wangd6f1dda2018-10-23 22:30:50 +08002533
Josef Bacikc1c80382018-07-03 11:14:59 -04002534 rq_qos_track(q, rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06002535
Bart Van Assche970d1682019-07-01 08:47:30 -07002536 blk_mq_bio_to_request(rq, bio, nr_segs);
2537
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002538 ret = blk_crypto_init_request(rq);
2539 if (ret != BLK_STS_OK) {
2540 bio->bi_status = ret;
2541 bio_endio(bio);
2542 blk_mq_free_request(rq);
Christoph Hellwig3e087732021-10-12 13:12:24 +02002543 return;
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002544 }
2545
Christoph Hellwigd92ca9d82021-10-19 14:25:53 +02002546 if (is_flush_fua && blk_insert_flush(rq))
2547 return;
2548
2549 if (plug && (q->nr_hw_queues == 1 ||
2550 blk_mq_is_shared_tags(rq->mq_hctx->flags) ||
2551 q->mq_ops->commit_rqs || !blk_queue_nonrot(q))) {
Jens Axboeb2c5d162018-11-29 10:03:42 -07002552 /*
2553 * Use plugging if we have a ->commit_rqs() hook as well, as
2554 * we know the driver uses bd->last in a smart fashion.
Ming Lei3154df22019-09-27 15:24:31 +08002555 *
2556 * Use normal plugging if this disk is slow HDD, as sequential
2557 * IO may benefit a lot from plug merging.
Jens Axboeb2c5d162018-11-29 10:03:42 -07002558 */
Jens Axboe5f0ed772018-11-23 22:04:33 -07002559 unsigned int request_count = plug->rq_count;
Shaohua Li600271d2016-11-03 17:03:54 -07002560 struct request *last = NULL;
2561
Jens Axboebc490f82021-10-18 10:12:12 -06002562 if (!request_count) {
Jeff Moyere6c44382015-05-08 10:51:30 -07002563 trace_block_plug(q);
Jens Axboebc490f82021-10-18 10:12:12 -06002564 } else if (!blk_queue_nomerges(q)) {
2565 last = rq_list_peek(&plug->mq_list);
2566 if (blk_rq_bytes(last) < BLK_PLUG_FLUSH_SIZE)
2567 last = NULL;
2568 }
Jens Axboeb094f892015-11-20 20:29:45 -07002569
Jens Axboebc490f82021-10-18 10:12:12 -06002570 if (request_count >= blk_plug_max_rq_count(plug) || last) {
Christoph Hellwiga214b942021-10-20 16:41:16 +02002571 blk_mq_flush_plug_list(plug, false);
Jeff Moyere6c44382015-05-08 10:51:30 -07002572 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002573 }
Jens Axboeb094f892015-11-20 20:29:45 -07002574
Jens Axboece5b0092018-11-27 17:13:56 -07002575 blk_add_rq_to_plug(plug, rq);
Jens Axboe2ff06822021-10-15 09:44:38 -06002576 } else if (rq->rq_flags & RQF_ELV) {
André Almeida105663f2020-01-06 15:08:18 -03002577 /* Insert the request at the IO scheduler queue */
Ming Leia12de1d2019-09-27 15:24:30 +08002578 blk_mq_sched_insert_request(rq, false, true, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002579 } else if (plug && !blk_queue_nomerges(q)) {
Jens Axboe87c037d2021-10-18 10:07:09 -06002580 struct request *next_rq = NULL;
2581
Jens Axboe320ae512013-10-24 09:20:05 +01002582 /*
2583 * We do limited plugging. If the bio can be merged, do that.
2584 * Otherwise the existing request in the plug list will be
2585 * issued. So the plug list will have one request at most
Christoph Hellwig22997222017-03-22 15:01:52 -04002586 * The plug list might get flushed before this. If that happens,
2587 * the plug list is empty, and same_queue_rq is invalid.
Jens Axboe320ae512013-10-24 09:20:05 +01002588 */
Jens Axboe4711b572018-11-27 17:07:17 -07002589 if (same_queue_rq) {
Jens Axboebc490f82021-10-18 10:12:12 -06002590 next_rq = rq_list_pop(&plug->mq_list);
Jens Axboe4711b572018-11-27 17:07:17 -07002591 plug->rq_count--;
2592 }
Jens Axboece5b0092018-11-27 17:13:56 -07002593 blk_add_rq_to_plug(plug, rq);
Yufen Yuff3b74b2019-03-26 21:19:25 +08002594 trace_block_plug(q);
Christoph Hellwig22997222017-03-22 15:01:52 -04002595
Jens Axboe87c037d2021-10-18 10:07:09 -06002596 if (next_rq) {
Yufen Yuff3b74b2019-03-26 21:19:25 +08002597 trace_block_unplug(q, 1, true);
Jens Axboe87c037d2021-10-18 10:07:09 -06002598 blk_mq_try_issue_directly(next_rq->mq_hctx, next_rq);
Ming Leidad7a3b2017-06-06 23:21:59 +08002599 }
Ming Leia12de1d2019-09-27 15:24:30 +08002600 } else if ((q->nr_hw_queues > 1 && is_sync) ||
Christoph Hellwig0f38d762021-10-12 12:40:45 +02002601 !rq->mq_hctx->dispatch_busy) {
André Almeida105663f2020-01-06 15:08:18 -03002602 /*
2603 * There is no scheduler and we can try to send directly
2604 * to the hardware.
2605 */
Christoph Hellwig3e087732021-10-12 13:12:24 +02002606 blk_mq_try_issue_directly(rq->mq_hctx, rq);
Ming Leiab42f352017-05-26 19:53:19 +08002607 } else {
André Almeida105663f2020-01-06 15:08:18 -03002608 /* Default case. */
huhai8fa9f552018-05-16 08:21:21 -06002609 blk_mq_sched_insert_request(rq, false, true, true);
Ming Leiab42f352017-05-26 19:53:19 +08002610 }
Jens Axboe320ae512013-10-24 09:20:05 +01002611
Christoph Hellwig3e087732021-10-12 13:12:24 +02002612 return;
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002613queue_exit:
2614 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002615}
2616
Ming Leibd631412021-05-11 23:22:35 +08002617static size_t order_to_size(unsigned int order)
2618{
2619 return (size_t)PAGE_SIZE << order;
2620}
2621
2622/* called before freeing request pool in @tags */
John Garryf32e4ea2021-10-05 18:23:32 +08002623static void blk_mq_clear_rq_mapping(struct blk_mq_tags *drv_tags,
2624 struct blk_mq_tags *tags)
Ming Leibd631412021-05-11 23:22:35 +08002625{
Ming Leibd631412021-05-11 23:22:35 +08002626 struct page *page;
2627 unsigned long flags;
2628
John Garry4f245d52021-10-05 18:23:33 +08002629 /* There is no need to clear a driver tags own mapping */
2630 if (drv_tags == tags)
2631 return;
2632
Ming Leibd631412021-05-11 23:22:35 +08002633 list_for_each_entry(page, &tags->page_list, lru) {
2634 unsigned long start = (unsigned long)page_address(page);
2635 unsigned long end = start + order_to_size(page->private);
2636 int i;
2637
John Garryf32e4ea2021-10-05 18:23:32 +08002638 for (i = 0; i < drv_tags->nr_tags; i++) {
Ming Leibd631412021-05-11 23:22:35 +08002639 struct request *rq = drv_tags->rqs[i];
2640 unsigned long rq_addr = (unsigned long)rq;
2641
2642 if (rq_addr >= start && rq_addr < end) {
2643 WARN_ON_ONCE(refcount_read(&rq->ref) != 0);
2644 cmpxchg(&drv_tags->rqs[i], rq, NULL);
2645 }
2646 }
2647 }
2648
2649 /*
2650 * Wait until all pending iteration is done.
2651 *
2652 * Request reference is cleared and it is guaranteed to be observed
2653 * after the ->lock is released.
2654 */
2655 spin_lock_irqsave(&drv_tags->lock, flags);
2656 spin_unlock_irqrestore(&drv_tags->lock, flags);
2657}
2658
Jens Axboecc71a6f2017-01-11 14:29:56 -07002659void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2660 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01002661{
John Garryf32e4ea2021-10-05 18:23:32 +08002662 struct blk_mq_tags *drv_tags;
Jens Axboe320ae512013-10-24 09:20:05 +01002663 struct page *page;
2664
John Garry079a2e32021-10-05 18:23:39 +08002665 if (blk_mq_is_shared_tags(set->flags))
2666 drv_tags = set->shared_tags;
John Garrye155b0c2021-10-05 18:23:37 +08002667 else
2668 drv_tags = set->tags[hctx_idx];
John Garryf32e4ea2021-10-05 18:23:32 +08002669
John Garry65de57b2021-10-05 18:23:26 +08002670 if (tags->static_rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002671 int i;
2672
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002673 for (i = 0; i < tags->nr_tags; i++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002674 struct request *rq = tags->static_rqs[i];
2675
2676 if (!rq)
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002677 continue;
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002678 set->ops->exit_request(set, rq, hctx_idx);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002679 tags->static_rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002680 }
2681 }
2682
John Garryf32e4ea2021-10-05 18:23:32 +08002683 blk_mq_clear_rq_mapping(drv_tags, tags);
Ming Leibd631412021-05-11 23:22:35 +08002684
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002685 while (!list_empty(&tags->page_list)) {
2686 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07002687 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002688 /*
2689 * Remove kmemleak object previously allocated in
Raul E Rangel273938b2019-05-02 13:48:11 -06002690 * blk_mq_alloc_rqs().
Catalin Marinasf75782e2015-09-14 18:16:02 +01002691 */
2692 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01002693 __free_pages(page, page->private);
2694 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002695}
Jens Axboe320ae512013-10-24 09:20:05 +01002696
John Garrye155b0c2021-10-05 18:23:37 +08002697void blk_mq_free_rq_map(struct blk_mq_tags *tags)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002698{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002699 kfree(tags->rqs);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002700 tags->rqs = NULL;
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002701 kfree(tags->static_rqs);
2702 tags->static_rqs = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002703
John Garrye155b0c2021-10-05 18:23:37 +08002704 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01002705}
2706
John Garry63064be2021-10-05 18:23:35 +08002707static struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
2708 unsigned int hctx_idx,
2709 unsigned int nr_tags,
John Garrye155b0c2021-10-05 18:23:37 +08002710 unsigned int reserved_tags)
Jens Axboe320ae512013-10-24 09:20:05 +01002711{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002712 struct blk_mq_tags *tags;
Shaohua Li59f082e2017-02-01 09:53:14 -08002713 int node;
Jens Axboe320ae512013-10-24 09:20:05 +01002714
Dongli Zhang7d76f852019-02-27 21:35:01 +08002715 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002716 if (node == NUMA_NO_NODE)
2717 node = set->numa_node;
2718
John Garrye155b0c2021-10-05 18:23:37 +08002719 tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
2720 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002721 if (!tags)
2722 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002723
Kees Cook590b5b72018-06-12 14:04:20 -07002724 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002725 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
Shaohua Li59f082e2017-02-01 09:53:14 -08002726 node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002727 if (!tags->rqs) {
John Garrye155b0c2021-10-05 18:23:37 +08002728 blk_mq_free_tags(tags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002729 return NULL;
2730 }
Jens Axboe320ae512013-10-24 09:20:05 +01002731
Kees Cook590b5b72018-06-12 14:04:20 -07002732 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2733 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2734 node);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002735 if (!tags->static_rqs) {
2736 kfree(tags->rqs);
John Garrye155b0c2021-10-05 18:23:37 +08002737 blk_mq_free_tags(tags);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002738 return NULL;
2739 }
2740
Jens Axboecc71a6f2017-01-11 14:29:56 -07002741 return tags;
2742}
2743
Tejun Heo1d9bd512018-01-09 08:29:48 -08002744static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2745 unsigned int hctx_idx, int node)
2746{
2747 int ret;
2748
2749 if (set->ops->init_request) {
2750 ret = set->ops->init_request(set, rq, hctx_idx, node);
2751 if (ret)
2752 return ret;
2753 }
2754
Keith Busch12f5b932018-05-29 15:52:28 +02002755 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Tejun Heo1d9bd512018-01-09 08:29:48 -08002756 return 0;
2757}
2758
John Garry63064be2021-10-05 18:23:35 +08002759static int blk_mq_alloc_rqs(struct blk_mq_tag_set *set,
2760 struct blk_mq_tags *tags,
2761 unsigned int hctx_idx, unsigned int depth)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002762{
2763 unsigned int i, j, entries_per_page, max_order = 4;
2764 size_t rq_size, left;
Shaohua Li59f082e2017-02-01 09:53:14 -08002765 int node;
2766
Dongli Zhang7d76f852019-02-27 21:35:01 +08002767 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002768 if (node == NUMA_NO_NODE)
2769 node = set->numa_node;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002770
2771 INIT_LIST_HEAD(&tags->page_list);
2772
Jens Axboe320ae512013-10-24 09:20:05 +01002773 /*
2774 * rq_size is the size of the request plus driver payload, rounded
2775 * to the cacheline size
2776 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002777 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01002778 cache_line_size());
Jens Axboecc71a6f2017-01-11 14:29:56 -07002779 left = rq_size * depth;
Jens Axboe320ae512013-10-24 09:20:05 +01002780
Jens Axboecc71a6f2017-01-11 14:29:56 -07002781 for (i = 0; i < depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01002782 int this_order = max_order;
2783 struct page *page;
2784 int to_do;
2785 void *p;
2786
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06002787 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01002788 this_order--;
2789
2790 do {
Shaohua Li59f082e2017-02-01 09:53:14 -08002791 page = alloc_pages_node(node,
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002792 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06002793 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01002794 if (page)
2795 break;
2796 if (!this_order--)
2797 break;
2798 if (order_to_size(this_order) < rq_size)
2799 break;
2800 } while (1);
2801
2802 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002803 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01002804
2805 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002806 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01002807
2808 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002809 /*
2810 * Allow kmemleak to scan these pages as they contain pointers
2811 * to additional allocations like via ops->init_request().
2812 */
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002813 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
Jens Axboe320ae512013-10-24 09:20:05 +01002814 entries_per_page = order_to_size(this_order) / rq_size;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002815 to_do = min(entries_per_page, depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01002816 left -= to_do * rq_size;
2817 for (j = 0; j < to_do; j++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002818 struct request *rq = p;
2819
2820 tags->static_rqs[i] = rq;
Tejun Heo1d9bd512018-01-09 08:29:48 -08002821 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
2822 tags->static_rqs[i] = NULL;
2823 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002824 }
2825
Jens Axboe320ae512013-10-24 09:20:05 +01002826 p += rq_size;
2827 i++;
2828 }
2829 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002830 return 0;
Jens Axboe320ae512013-10-24 09:20:05 +01002831
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002832fail:
Jens Axboecc71a6f2017-01-11 14:29:56 -07002833 blk_mq_free_rqs(set, tags, hctx_idx);
2834 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01002835}
2836
Ming Leibf0beec2020-05-29 15:53:15 +02002837struct rq_iter_data {
2838 struct blk_mq_hw_ctx *hctx;
2839 bool has_rq;
2840};
2841
2842static bool blk_mq_has_request(struct request *rq, void *data, bool reserved)
2843{
2844 struct rq_iter_data *iter_data = data;
2845
2846 if (rq->mq_hctx != iter_data->hctx)
2847 return true;
2848 iter_data->has_rq = true;
2849 return false;
2850}
2851
2852static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
2853{
2854 struct blk_mq_tags *tags = hctx->sched_tags ?
2855 hctx->sched_tags : hctx->tags;
2856 struct rq_iter_data data = {
2857 .hctx = hctx,
2858 };
2859
2860 blk_mq_all_tag_iter(tags, blk_mq_has_request, &data);
2861 return data.has_rq;
2862}
2863
2864static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu,
2865 struct blk_mq_hw_ctx *hctx)
2866{
2867 if (cpumask_next_and(-1, hctx->cpumask, cpu_online_mask) != cpu)
2868 return false;
2869 if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids)
2870 return false;
2871 return true;
2872}
2873
2874static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
2875{
2876 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2877 struct blk_mq_hw_ctx, cpuhp_online);
2878
2879 if (!cpumask_test_cpu(cpu, hctx->cpumask) ||
2880 !blk_mq_last_cpu_in_hctx(cpu, hctx))
2881 return 0;
2882
2883 /*
2884 * Prevent new request from being allocated on the current hctx.
2885 *
2886 * The smp_mb__after_atomic() Pairs with the implied barrier in
2887 * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is
2888 * seen once we return from the tag allocator.
2889 */
2890 set_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2891 smp_mb__after_atomic();
2892
2893 /*
2894 * Try to grab a reference to the queue and wait for any outstanding
2895 * requests. If we could not grab a reference the queue has been
2896 * frozen and there are no requests.
2897 */
2898 if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) {
2899 while (blk_mq_hctx_has_requests(hctx))
2900 msleep(5);
2901 percpu_ref_put(&hctx->queue->q_usage_counter);
2902 }
2903
2904 return 0;
2905}
2906
2907static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node)
2908{
2909 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2910 struct blk_mq_hw_ctx, cpuhp_online);
2911
2912 if (cpumask_test_cpu(cpu, hctx->cpumask))
2913 clear_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2914 return 0;
2915}
2916
Jens Axboee57690f2016-08-24 15:34:35 -06002917/*
2918 * 'cpu' is going away. splice any existing rq_list entries from this
2919 * software queue to the hw queue dispatch list, and ensure that it
2920 * gets run.
2921 */
Thomas Gleixner9467f852016-09-22 08:05:17 -06002922static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
Jens Axboe484b4062014-05-21 14:01:15 -06002923{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002924 struct blk_mq_hw_ctx *hctx;
Jens Axboe484b4062014-05-21 14:01:15 -06002925 struct blk_mq_ctx *ctx;
2926 LIST_HEAD(tmp);
Ming Leic16d6b52018-12-17 08:44:05 -07002927 enum hctx_type type;
Jens Axboe484b4062014-05-21 14:01:15 -06002928
Thomas Gleixner9467f852016-09-22 08:05:17 -06002929 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02002930 if (!cpumask_test_cpu(cpu, hctx->cpumask))
2931 return 0;
2932
Jens Axboee57690f2016-08-24 15:34:35 -06002933 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Ming Leic16d6b52018-12-17 08:44:05 -07002934 type = hctx->type;
Jens Axboe484b4062014-05-21 14:01:15 -06002935
2936 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002937 if (!list_empty(&ctx->rq_lists[type])) {
2938 list_splice_init(&ctx->rq_lists[type], &tmp);
Jens Axboe484b4062014-05-21 14:01:15 -06002939 blk_mq_hctx_clear_pending(hctx, ctx);
2940 }
2941 spin_unlock(&ctx->lock);
2942
2943 if (list_empty(&tmp))
Thomas Gleixner9467f852016-09-22 08:05:17 -06002944 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002945
Jens Axboee57690f2016-08-24 15:34:35 -06002946 spin_lock(&hctx->lock);
2947 list_splice_tail_init(&tmp, &hctx->dispatch);
2948 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06002949
2950 blk_mq_run_hw_queue(hctx, true);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002951 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002952}
2953
Thomas Gleixner9467f852016-09-22 08:05:17 -06002954static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
Jens Axboe484b4062014-05-21 14:01:15 -06002955{
Ming Leibf0beec2020-05-29 15:53:15 +02002956 if (!(hctx->flags & BLK_MQ_F_STACKING))
2957 cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2958 &hctx->cpuhp_online);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002959 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
2960 &hctx->cpuhp_dead);
Jens Axboe484b4062014-05-21 14:01:15 -06002961}
2962
Ming Lei364b6182021-05-11 23:22:36 +08002963/*
2964 * Before freeing hw queue, clearing the flush request reference in
2965 * tags->rqs[] for avoiding potential UAF.
2966 */
2967static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags,
2968 unsigned int queue_depth, struct request *flush_rq)
2969{
2970 int i;
2971 unsigned long flags;
2972
2973 /* The hw queue may not be mapped yet */
2974 if (!tags)
2975 return;
2976
2977 WARN_ON_ONCE(refcount_read(&flush_rq->ref) != 0);
2978
2979 for (i = 0; i < queue_depth; i++)
2980 cmpxchg(&tags->rqs[i], flush_rq, NULL);
2981
2982 /*
2983 * Wait until all pending iteration is done.
2984 *
2985 * Request reference is cleared and it is guaranteed to be observed
2986 * after the ->lock is released.
2987 */
2988 spin_lock_irqsave(&tags->lock, flags);
2989 spin_unlock_irqrestore(&tags->lock, flags);
2990}
2991
Ming Leic3b4afc2015-06-04 22:25:04 +08002992/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08002993static void blk_mq_exit_hctx(struct request_queue *q,
2994 struct blk_mq_tag_set *set,
2995 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
2996{
Ming Lei364b6182021-05-11 23:22:36 +08002997 struct request *flush_rq = hctx->fq->flush_rq;
2998
Ming Lei8ab0b7d2018-01-09 21:28:29 +08002999 if (blk_mq_hw_queue_mapped(hctx))
3000 blk_mq_tag_idle(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08003001
Ming Lei364b6182021-05-11 23:22:36 +08003002 blk_mq_clear_flush_rq_mapping(set->tags[hctx_idx],
3003 set->queue_depth, flush_rq);
Ming Leif70ced02014-09-25 23:23:47 +08003004 if (set->ops->exit_request)
Ming Lei364b6182021-05-11 23:22:36 +08003005 set->ops->exit_request(set, flush_rq, hctx_idx);
Ming Leif70ced02014-09-25 23:23:47 +08003006
Ming Lei08e98fc2014-09-25 23:23:38 +08003007 if (set->ops->exit_hctx)
3008 set->ops->exit_hctx(hctx, hctx_idx);
3009
Thomas Gleixner9467f852016-09-22 08:05:17 -06003010 blk_mq_remove_cpuhp(hctx);
Ming Lei2f8f1332019-04-30 09:52:27 +08003011
3012 spin_lock(&q->unused_hctx_lock);
3013 list_add(&hctx->hctx_list, &q->unused_hctx_list);
3014 spin_unlock(&q->unused_hctx_lock);
Ming Lei08e98fc2014-09-25 23:23:38 +08003015}
3016
Ming Lei624dbe42014-05-27 23:35:13 +08003017static void blk_mq_exit_hw_queues(struct request_queue *q,
3018 struct blk_mq_tag_set *set, int nr_queue)
3019{
3020 struct blk_mq_hw_ctx *hctx;
3021 unsigned int i;
3022
3023 queue_for_each_hw_ctx(q, hctx, i) {
3024 if (i == nr_queue)
3025 break;
Jianchao Wang477e19d2018-10-12 18:07:25 +08003026 blk_mq_debugfs_unregister_hctx(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08003027 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08003028 }
Ming Lei624dbe42014-05-27 23:35:13 +08003029}
3030
Ming Lei7c6c5b72019-04-30 09:52:26 +08003031static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
3032{
3033 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
3034
3035 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
3036 __alignof__(struct blk_mq_hw_ctx)) !=
3037 sizeof(struct blk_mq_hw_ctx));
3038
3039 if (tag_set->flags & BLK_MQ_F_BLOCKING)
3040 hw_ctx_size += sizeof(struct srcu_struct);
3041
3042 return hw_ctx_size;
3043}
3044
Ming Lei08e98fc2014-09-25 23:23:38 +08003045static int blk_mq_init_hctx(struct request_queue *q,
3046 struct blk_mq_tag_set *set,
3047 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
3048{
Ming Lei7c6c5b72019-04-30 09:52:26 +08003049 hctx->queue_num = hctx_idx;
Ming Lei08e98fc2014-09-25 23:23:38 +08003050
Ming Leibf0beec2020-05-29 15:53:15 +02003051 if (!(hctx->flags & BLK_MQ_F_STACKING))
3052 cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
3053 &hctx->cpuhp_online);
Ming Lei7c6c5b72019-04-30 09:52:26 +08003054 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
3055
3056 hctx->tags = set->tags[hctx_idx];
3057
3058 if (set->ops->init_hctx &&
3059 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
3060 goto unregister_cpu_notifier;
3061
3062 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
3063 hctx->numa_node))
3064 goto exit_hctx;
3065 return 0;
3066
3067 exit_hctx:
3068 if (set->ops->exit_hctx)
3069 set->ops->exit_hctx(hctx, hctx_idx);
3070 unregister_cpu_notifier:
3071 blk_mq_remove_cpuhp(hctx);
3072 return -1;
3073}
3074
3075static struct blk_mq_hw_ctx *
3076blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
3077 int node)
3078{
3079 struct blk_mq_hw_ctx *hctx;
3080 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
3081
3082 hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node);
3083 if (!hctx)
3084 goto fail_alloc_hctx;
3085
3086 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
3087 goto free_hctx;
3088
3089 atomic_set(&hctx->nr_active, 0);
Ming Lei08e98fc2014-09-25 23:23:38 +08003090 if (node == NUMA_NO_NODE)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003091 node = set->numa_node;
3092 hctx->numa_node = node;
Ming Lei08e98fc2014-09-25 23:23:38 +08003093
Jens Axboe9f993732017-04-10 09:54:54 -06003094 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08003095 spin_lock_init(&hctx->lock);
3096 INIT_LIST_HEAD(&hctx->dispatch);
3097 hctx->queue = q;
Ming Lei51db1c32020-08-19 23:20:19 +08003098 hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08003099
Ming Lei2f8f1332019-04-30 09:52:27 +08003100 INIT_LIST_HEAD(&hctx->hctx_list);
3101
Ming Lei08e98fc2014-09-25 23:23:38 +08003102 /*
3103 * Allocate space for all possible cpus to avoid allocation at
3104 * runtime
3105 */
Johannes Thumshirnd904bfa2017-11-15 17:32:33 -08003106 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
Ming Lei7c6c5b72019-04-30 09:52:26 +08003107 gfp, node);
Ming Lei08e98fc2014-09-25 23:23:38 +08003108 if (!hctx->ctxs)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003109 goto free_cpumask;
Ming Lei08e98fc2014-09-25 23:23:38 +08003110
Jianchao Wang5b202852018-10-12 18:07:26 +08003111 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
Ming Leic548e622021-01-22 10:33:08 +08003112 gfp, node, false, false))
Ming Lei08e98fc2014-09-25 23:23:38 +08003113 goto free_ctxs;
Ming Lei08e98fc2014-09-25 23:23:38 +08003114 hctx->nr_ctx = 0;
3115
Ming Lei5815839b2018-06-25 19:31:47 +08003116 spin_lock_init(&hctx->dispatch_wait_lock);
Jens Axboeeb619fd2017-11-09 08:32:43 -07003117 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
3118 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
3119
Guoqing Jiang754a1572020-03-09 22:41:37 +01003120 hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp);
Ming Leif70ced02014-09-25 23:23:47 +08003121 if (!hctx->fq)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003122 goto free_bitmap;
Ming Leif70ced02014-09-25 23:23:47 +08003123
Bart Van Assche6a83e742016-11-02 10:09:51 -06003124 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -08003125 init_srcu_struct(hctx->srcu);
Ming Lei7c6c5b72019-04-30 09:52:26 +08003126 blk_mq_hctx_kobj_init(hctx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06003127
Ming Lei7c6c5b72019-04-30 09:52:26 +08003128 return hctx;
Ming Lei08e98fc2014-09-25 23:23:38 +08003129
3130 free_bitmap:
Omar Sandoval88459642016-09-17 08:38:44 -06003131 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08003132 free_ctxs:
3133 kfree(hctx->ctxs);
Ming Lei7c6c5b72019-04-30 09:52:26 +08003134 free_cpumask:
3135 free_cpumask_var(hctx->cpumask);
3136 free_hctx:
3137 kfree(hctx);
3138 fail_alloc_hctx:
3139 return NULL;
Ming Lei08e98fc2014-09-25 23:23:38 +08003140}
3141
Jens Axboe320ae512013-10-24 09:20:05 +01003142static void blk_mq_init_cpu_queues(struct request_queue *q,
3143 unsigned int nr_hw_queues)
3144{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003145 struct blk_mq_tag_set *set = q->tag_set;
3146 unsigned int i, j;
Jens Axboe320ae512013-10-24 09:20:05 +01003147
3148 for_each_possible_cpu(i) {
3149 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
3150 struct blk_mq_hw_ctx *hctx;
Ming Leic16d6b52018-12-17 08:44:05 -07003151 int k;
Jens Axboe320ae512013-10-24 09:20:05 +01003152
Jens Axboe320ae512013-10-24 09:20:05 +01003153 __ctx->cpu = i;
3154 spin_lock_init(&__ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07003155 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
3156 INIT_LIST_HEAD(&__ctx->rq_lists[k]);
3157
Jens Axboe320ae512013-10-24 09:20:05 +01003158 __ctx->queue = q;
3159
Jens Axboe320ae512013-10-24 09:20:05 +01003160 /*
3161 * Set local node, IFF we have more than one hw queue. If
3162 * not, we remain on the home node of the device
3163 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06003164 for (j = 0; j < set->nr_maps; j++) {
3165 hctx = blk_mq_map_queue_type(q, j, i);
3166 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
Xianting Tian576e85c2020-10-19 16:20:47 +08003167 hctx->numa_node = cpu_to_node(i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003168 }
Jens Axboe320ae512013-10-24 09:20:05 +01003169 }
3170}
3171
John Garry63064be2021-10-05 18:23:35 +08003172struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
3173 unsigned int hctx_idx,
3174 unsigned int depth)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003175{
John Garry63064be2021-10-05 18:23:35 +08003176 struct blk_mq_tags *tags;
3177 int ret;
Jens Axboecc71a6f2017-01-11 14:29:56 -07003178
John Garrye155b0c2021-10-05 18:23:37 +08003179 tags = blk_mq_alloc_rq_map(set, hctx_idx, depth, set->reserved_tags);
John Garry63064be2021-10-05 18:23:35 +08003180 if (!tags)
3181 return NULL;
Jens Axboecc71a6f2017-01-11 14:29:56 -07003182
John Garry63064be2021-10-05 18:23:35 +08003183 ret = blk_mq_alloc_rqs(set, tags, hctx_idx, depth);
3184 if (ret) {
John Garrye155b0c2021-10-05 18:23:37 +08003185 blk_mq_free_rq_map(tags);
John Garry63064be2021-10-05 18:23:35 +08003186 return NULL;
3187 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07003188
John Garry63064be2021-10-05 18:23:35 +08003189 return tags;
3190}
3191
3192static bool __blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
3193 int hctx_idx)
3194{
John Garry079a2e32021-10-05 18:23:39 +08003195 if (blk_mq_is_shared_tags(set->flags)) {
3196 set->tags[hctx_idx] = set->shared_tags;
John Garrye155b0c2021-10-05 18:23:37 +08003197
3198 return true;
3199 }
3200
John Garry63064be2021-10-05 18:23:35 +08003201 set->tags[hctx_idx] = blk_mq_alloc_map_and_rqs(set, hctx_idx,
3202 set->queue_depth);
3203
3204 return set->tags[hctx_idx];
Jens Axboecc71a6f2017-01-11 14:29:56 -07003205}
3206
John Garry645db342021-10-05 18:23:36 +08003207void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
3208 struct blk_mq_tags *tags,
3209 unsigned int hctx_idx)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003210{
John Garry645db342021-10-05 18:23:36 +08003211 if (tags) {
3212 blk_mq_free_rqs(set, tags, hctx_idx);
John Garrye155b0c2021-10-05 18:23:37 +08003213 blk_mq_free_rq_map(tags);
Jens Axboebd166ef2017-01-17 06:03:22 -07003214 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07003215}
3216
John Garrye155b0c2021-10-05 18:23:37 +08003217static void __blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
3218 unsigned int hctx_idx)
3219{
John Garry079a2e32021-10-05 18:23:39 +08003220 if (!blk_mq_is_shared_tags(set->flags))
John Garrye155b0c2021-10-05 18:23:37 +08003221 blk_mq_free_map_and_rqs(set, set->tags[hctx_idx], hctx_idx);
3222
3223 set->tags[hctx_idx] = NULL;
3224}
3225
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003226static void blk_mq_map_swqueue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01003227{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003228 unsigned int i, j, hctx_idx;
Jens Axboe320ae512013-10-24 09:20:05 +01003229 struct blk_mq_hw_ctx *hctx;
3230 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08003231 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01003232
3233 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06003234 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01003235 hctx->nr_ctx = 0;
huhaid416c922018-05-18 08:32:30 -06003236 hctx->dispatch_from = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01003237 }
3238
3239 /*
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003240 * Map software to hardware queues.
Ming Lei4412efe2018-04-25 04:01:44 +08003241 *
3242 * If the cpu isn't present, the cpu is mapped to first hctx.
Jens Axboe320ae512013-10-24 09:20:05 +01003243 */
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08003244 for_each_possible_cpu(i) {
Ming Lei4412efe2018-04-25 04:01:44 +08003245
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01003246 ctx = per_cpu_ptr(q->queue_ctx, i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003247 for (j = 0; j < set->nr_maps; j++) {
Jianchao Wangbb94aea2019-01-24 18:25:33 +08003248 if (!set->map[j].nr_queues) {
3249 ctx->hctxs[j] = blk_mq_map_queue_type(q,
3250 HCTX_TYPE_DEFAULT, i);
Ming Leie5edd5f2018-12-18 01:28:56 +08003251 continue;
Jianchao Wangbb94aea2019-01-24 18:25:33 +08003252 }
Ming Leifd689872020-05-07 21:04:08 +08003253 hctx_idx = set->map[j].mq_map[i];
3254 /* unmapped hw queue can be remapped after CPU topo changed */
3255 if (!set->tags[hctx_idx] &&
John Garry63064be2021-10-05 18:23:35 +08003256 !__blk_mq_alloc_map_and_rqs(set, hctx_idx)) {
Ming Leifd689872020-05-07 21:04:08 +08003257 /*
3258 * If tags initialization fail for some hctx,
3259 * that hctx won't be brought online. In this
3260 * case, remap the current ctx to hctx[0] which
3261 * is guaranteed to always have tags allocated
3262 */
3263 set->map[j].mq_map[i] = 0;
3264 }
Ming Leie5edd5f2018-12-18 01:28:56 +08003265
Jens Axboeb3c661b2018-10-30 10:36:06 -06003266 hctx = blk_mq_map_queue_type(q, j, i);
Jianchao Wang8ccdf4a2019-01-24 18:25:32 +08003267 ctx->hctxs[j] = hctx;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003268 /*
3269 * If the CPU is already set in the mask, then we've
3270 * mapped this one already. This can happen if
3271 * devices share queues across queue maps.
3272 */
3273 if (cpumask_test_cpu(i, hctx->cpumask))
3274 continue;
3275
3276 cpumask_set_cpu(i, hctx->cpumask);
3277 hctx->type = j;
3278 ctx->index_hw[hctx->type] = hctx->nr_ctx;
3279 hctx->ctxs[hctx->nr_ctx++] = ctx;
3280
3281 /*
3282 * If the nr_ctx type overflows, we have exceeded the
3283 * amount of sw queues we can support.
3284 */
3285 BUG_ON(!hctx->nr_ctx);
3286 }
Jianchao Wangbb94aea2019-01-24 18:25:33 +08003287
3288 for (; j < HCTX_MAX_TYPES; j++)
3289 ctx->hctxs[j] = blk_mq_map_queue_type(q,
3290 HCTX_TYPE_DEFAULT, i);
Jens Axboe320ae512013-10-24 09:20:05 +01003291 }
Jens Axboe506e9312014-05-07 10:26:44 -06003292
3293 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei4412efe2018-04-25 04:01:44 +08003294 /*
3295 * If no software queues are mapped to this hardware queue,
3296 * disable it and free the request entries.
3297 */
3298 if (!hctx->nr_ctx) {
3299 /* Never unmap queue 0. We need it as a
3300 * fallback in case of a new remap fails
3301 * allocation
3302 */
John Garrye155b0c2021-10-05 18:23:37 +08003303 if (i)
3304 __blk_mq_free_map_and_rqs(set, i);
Ming Lei4412efe2018-04-25 04:01:44 +08003305
3306 hctx->tags = NULL;
3307 continue;
3308 }
Jens Axboe484b4062014-05-21 14:01:15 -06003309
Ming Lei2a34c082015-04-21 10:00:20 +08003310 hctx->tags = set->tags[i];
3311 WARN_ON(!hctx->tags);
3312
Jens Axboe484b4062014-05-21 14:01:15 -06003313 /*
Chong Yuan889fa312015-04-15 11:39:29 -06003314 * Set the map size to the number of mapped software queues.
3315 * This is more accurate and more efficient than looping
3316 * over all possibly mapped software queues.
3317 */
Omar Sandoval88459642016-09-17 08:38:44 -06003318 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
Chong Yuan889fa312015-04-15 11:39:29 -06003319
3320 /*
Jens Axboe484b4062014-05-21 14:01:15 -06003321 * Initialize batch roundrobin counts
3322 */
Ming Leif82ddf12018-04-08 17:48:10 +08003323 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06003324 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
3325 }
Jens Axboe320ae512013-10-24 09:20:05 +01003326}
3327
Jens Axboe8e8320c2017-06-20 17:56:13 -06003328/*
3329 * Caller needs to ensure that we're either frozen/quiesced, or that
3330 * the queue isn't live yet.
3331 */
Jeff Moyer2404e602015-11-03 10:40:06 -05003332static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06003333{
3334 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06003335 int i;
3336
Jeff Moyer2404e602015-11-03 10:40:06 -05003337 queue_for_each_hw_ctx(q, hctx, i) {
Yu Kuai454bb672021-07-31 14:21:30 +08003338 if (shared) {
Ming Lei51db1c32020-08-19 23:20:19 +08003339 hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
Yu Kuai454bb672021-07-31 14:21:30 +08003340 } else {
3341 blk_mq_tag_idle(hctx);
Ming Lei51db1c32020-08-19 23:20:19 +08003342 hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
Yu Kuai454bb672021-07-31 14:21:30 +08003343 }
Jeff Moyer2404e602015-11-03 10:40:06 -05003344 }
3345}
3346
Hannes Reinecke655ac302020-08-19 23:20:20 +08003347static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set,
3348 bool shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05003349{
3350 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06003351
Bart Van Assche705cda92017-04-07 11:16:49 -07003352 lockdep_assert_held(&set->tag_list_lock);
3353
Jens Axboe0d2602c2014-05-13 15:10:52 -06003354 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3355 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05003356 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003357 blk_mq_unfreeze_queue(q);
3358 }
3359}
3360
3361static void blk_mq_del_queue_tag_set(struct request_queue *q)
3362{
3363 struct blk_mq_tag_set *set = q->tag_set;
3364
Jens Axboe0d2602c2014-05-13 15:10:52 -06003365 mutex_lock(&set->tag_list_lock);
Daniel Wagner08c875c2020-07-28 15:29:51 +02003366 list_del(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05003367 if (list_is_singular(&set->tag_list)) {
3368 /* just transitioned to unshared */
Ming Lei51db1c32020-08-19 23:20:19 +08003369 set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05003370 /* update existing queue */
Hannes Reinecke655ac302020-08-19 23:20:20 +08003371 blk_mq_update_tag_set_shared(set, false);
Jeff Moyer2404e602015-11-03 10:40:06 -05003372 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06003373 mutex_unlock(&set->tag_list_lock);
Roman Pena347c7a2018-06-10 22:38:24 +02003374 INIT_LIST_HEAD(&q->tag_set_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003375}
3376
3377static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
3378 struct request_queue *q)
3379{
Jens Axboe0d2602c2014-05-13 15:10:52 -06003380 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05003381
Jens Axboeff821d22017-11-10 22:05:12 -07003382 /*
3383 * Check to see if we're transitioning to shared (from 1 to 2 queues).
3384 */
3385 if (!list_empty(&set->tag_list) &&
Ming Lei51db1c32020-08-19 23:20:19 +08003386 !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
3387 set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05003388 /* update existing queue */
Hannes Reinecke655ac302020-08-19 23:20:20 +08003389 blk_mq_update_tag_set_shared(set, true);
Jeff Moyer2404e602015-11-03 10:40:06 -05003390 }
Ming Lei51db1c32020-08-19 23:20:19 +08003391 if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
Jeff Moyer2404e602015-11-03 10:40:06 -05003392 queue_set_hctx_shared(q, true);
Daniel Wagner08c875c2020-07-28 15:29:51 +02003393 list_add_tail(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05003394
Jens Axboe0d2602c2014-05-13 15:10:52 -06003395 mutex_unlock(&set->tag_list_lock);
3396}
3397
Ming Lei1db49092018-11-20 09:44:35 +08003398/* All allocations will be freed in release handler of q->mq_kobj */
3399static int blk_mq_alloc_ctxs(struct request_queue *q)
3400{
3401 struct blk_mq_ctxs *ctxs;
3402 int cpu;
3403
3404 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
3405 if (!ctxs)
3406 return -ENOMEM;
3407
3408 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
3409 if (!ctxs->queue_ctx)
3410 goto fail;
3411
3412 for_each_possible_cpu(cpu) {
3413 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
3414 ctx->ctxs = ctxs;
3415 }
3416
3417 q->mq_kobj = &ctxs->kobj;
3418 q->queue_ctx = ctxs->queue_ctx;
3419
3420 return 0;
3421 fail:
3422 kfree(ctxs);
3423 return -ENOMEM;
3424}
3425
Ming Leie09aae72015-01-29 20:17:27 +08003426/*
3427 * It is the actual release handler for mq, but we do it from
3428 * request queue's release handler for avoiding use-after-free
3429 * and headache because q->mq_kobj shouldn't have been introduced,
3430 * but we can't group ctx/kctx kobj without it.
3431 */
3432void blk_mq_release(struct request_queue *q)
3433{
Ming Lei2f8f1332019-04-30 09:52:27 +08003434 struct blk_mq_hw_ctx *hctx, *next;
3435 int i;
Ming Leie09aae72015-01-29 20:17:27 +08003436
Ming Lei2f8f1332019-04-30 09:52:27 +08003437 queue_for_each_hw_ctx(q, hctx, i)
3438 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
3439
3440 /* all hctx are in .unused_hctx_list now */
3441 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
3442 list_del_init(&hctx->hctx_list);
Ming Lei6c8b2322017-02-22 18:14:01 +08003443 kobject_put(&hctx->kobj);
Ming Leic3b4afc2015-06-04 22:25:04 +08003444 }
Ming Leie09aae72015-01-29 20:17:27 +08003445
3446 kfree(q->queue_hw_ctx);
3447
Ming Lei7ea5fe32017-02-22 18:14:00 +08003448 /*
3449 * release .mq_kobj and sw queue's kobject now because
3450 * both share lifetime with request queue.
3451 */
3452 blk_mq_sysfs_deinit(q);
Ming Leie09aae72015-01-29 20:17:27 +08003453}
3454
Christoph Hellwig5ec780a2021-06-24 10:10:12 +02003455static struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set,
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003456 void *queuedata)
Jens Axboe320ae512013-10-24 09:20:05 +01003457{
Christoph Hellwig26a97502021-06-02 09:53:17 +03003458 struct request_queue *q;
3459 int ret;
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003460
Christoph Hellwig26a97502021-06-02 09:53:17 +03003461 q = blk_alloc_queue(set->numa_node);
3462 if (!q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003463 return ERR_PTR(-ENOMEM);
Christoph Hellwig26a97502021-06-02 09:53:17 +03003464 q->queuedata = queuedata;
3465 ret = blk_mq_init_allocated_queue(set, q);
3466 if (ret) {
3467 blk_cleanup_queue(q);
3468 return ERR_PTR(ret);
3469 }
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003470 return q;
3471}
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003472
3473struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
3474{
3475 return blk_mq_init_queue_data(set, NULL);
3476}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003477EXPORT_SYMBOL(blk_mq_init_queue);
3478
Christoph Hellwig4dcc4872021-08-16 15:19:05 +02003479struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set, void *queuedata,
3480 struct lock_class_key *lkclass)
Jens Axboe9316a9e2018-10-15 08:40:37 -06003481{
3482 struct request_queue *q;
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003483 struct gendisk *disk;
Jens Axboe9316a9e2018-10-15 08:40:37 -06003484
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003485 q = blk_mq_init_queue_data(set, queuedata);
3486 if (IS_ERR(q))
3487 return ERR_CAST(q);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003488
Christoph Hellwig4a1fa412021-08-16 15:19:08 +02003489 disk = __alloc_disk_node(q, set->numa_node, lkclass);
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003490 if (!disk) {
3491 blk_cleanup_queue(q);
3492 return ERR_PTR(-ENOMEM);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003493 }
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003494 return disk;
Jens Axboe9316a9e2018-10-15 08:40:37 -06003495}
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003496EXPORT_SYMBOL(__blk_mq_alloc_disk);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003497
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003498static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
3499 struct blk_mq_tag_set *set, struct request_queue *q,
3500 int hctx_idx, int node)
3501{
Ming Lei2f8f1332019-04-30 09:52:27 +08003502 struct blk_mq_hw_ctx *hctx = NULL, *tmp;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003503
Ming Lei2f8f1332019-04-30 09:52:27 +08003504 /* reuse dead hctx first */
3505 spin_lock(&q->unused_hctx_lock);
3506 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
3507 if (tmp->numa_node == node) {
3508 hctx = tmp;
3509 break;
3510 }
3511 }
3512 if (hctx)
3513 list_del_init(&hctx->hctx_list);
3514 spin_unlock(&q->unused_hctx_lock);
3515
3516 if (!hctx)
3517 hctx = blk_mq_alloc_hctx(q, set, node);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003518 if (!hctx)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003519 goto fail;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003520
Ming Lei7c6c5b72019-04-30 09:52:26 +08003521 if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
3522 goto free_hctx;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003523
3524 return hctx;
Ming Lei7c6c5b72019-04-30 09:52:26 +08003525
3526 free_hctx:
3527 kobject_put(&hctx->kobj);
3528 fail:
3529 return NULL;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003530}
3531
Keith Busch868f2f02015-12-17 17:08:14 -07003532static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
3533 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003534{
Jianchao Wange01ad462018-10-12 18:07:28 +08003535 int i, j, end;
Keith Busch868f2f02015-12-17 17:08:14 -07003536 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01003537
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003538 if (q->nr_hw_queues < set->nr_hw_queues) {
3539 struct blk_mq_hw_ctx **new_hctxs;
3540
3541 new_hctxs = kcalloc_node(set->nr_hw_queues,
3542 sizeof(*new_hctxs), GFP_KERNEL,
3543 set->numa_node);
3544 if (!new_hctxs)
3545 return;
3546 if (hctxs)
3547 memcpy(new_hctxs, hctxs, q->nr_hw_queues *
3548 sizeof(*hctxs));
3549 q->queue_hw_ctx = new_hctxs;
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003550 kfree(hctxs);
3551 hctxs = new_hctxs;
3552 }
3553
Ming Leifb350e02018-01-06 16:27:40 +08003554 /* protect against switching io scheduler */
3555 mutex_lock(&q->sysfs_lock);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003556 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003557 int node;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003558 struct blk_mq_hw_ctx *hctx;
Keith Busch868f2f02015-12-17 17:08:14 -07003559
Dongli Zhang7d76f852019-02-27 21:35:01 +08003560 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003561 /*
3562 * If the hw queue has been mapped to another numa node,
3563 * we need to realloc the hctx. If allocation fails, fallback
3564 * to use the previous one.
3565 */
3566 if (hctxs[i] && (hctxs[i]->numa_node == node))
3567 continue;
Jens Axboe320ae512013-10-24 09:20:05 +01003568
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003569 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
3570 if (hctx) {
Ming Lei2f8f1332019-04-30 09:52:27 +08003571 if (hctxs[i])
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003572 blk_mq_exit_hctx(q, set, hctxs[i], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003573 hctxs[i] = hctx;
3574 } else {
3575 if (hctxs[i])
3576 pr_warn("Allocate new hctx on node %d fails,\
3577 fallback to previous one on node %d\n",
3578 node, hctxs[i]->numa_node);
3579 else
3580 break;
Keith Busch868f2f02015-12-17 17:08:14 -07003581 }
Jens Axboe320ae512013-10-24 09:20:05 +01003582 }
Jianchao Wange01ad462018-10-12 18:07:28 +08003583 /*
3584 * Increasing nr_hw_queues fails. Free the newly allocated
3585 * hctxs and keep the previous q->nr_hw_queues.
3586 */
3587 if (i != set->nr_hw_queues) {
3588 j = q->nr_hw_queues;
3589 end = i;
3590 } else {
3591 j = i;
3592 end = q->nr_hw_queues;
3593 q->nr_hw_queues = set->nr_hw_queues;
3594 }
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003595
Jianchao Wange01ad462018-10-12 18:07:28 +08003596 for (; j < end; j++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003597 struct blk_mq_hw_ctx *hctx = hctxs[j];
3598
3599 if (hctx) {
John Garrye155b0c2021-10-05 18:23:37 +08003600 __blk_mq_free_map_and_rqs(set, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003601 blk_mq_exit_hctx(q, set, hctx, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003602 hctxs[j] = NULL;
Keith Busch868f2f02015-12-17 17:08:14 -07003603 }
3604 }
Ming Leifb350e02018-01-06 16:27:40 +08003605 mutex_unlock(&q->sysfs_lock);
Keith Busch868f2f02015-12-17 17:08:14 -07003606}
3607
Christoph Hellwig26a97502021-06-02 09:53:17 +03003608int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
3609 struct request_queue *q)
Keith Busch868f2f02015-12-17 17:08:14 -07003610{
Ming Lei66841672016-02-12 15:27:00 +08003611 /* mark the queue as mq asap */
3612 q->mq_ops = set->ops;
3613
Omar Sandoval34dbad52017-03-21 08:56:08 -07003614 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
Stephen Bates720b8cc2017-04-07 06:24:03 -06003615 blk_mq_poll_stats_bkt,
3616 BLK_MQ_POLL_STATS_BKTS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -07003617 if (!q->poll_cb)
3618 goto err_exit;
3619
Ming Lei1db49092018-11-20 09:44:35 +08003620 if (blk_mq_alloc_ctxs(q))
Jes Sorensen41de54c2019-04-19 16:35:44 -04003621 goto err_poll;
Keith Busch868f2f02015-12-17 17:08:14 -07003622
Ming Lei737f98c2017-02-22 18:13:59 +08003623 /* init q->mq_kobj and sw queues' kobjects */
3624 blk_mq_sysfs_init(q);
3625
Ming Lei2f8f1332019-04-30 09:52:27 +08003626 INIT_LIST_HEAD(&q->unused_hctx_list);
3627 spin_lock_init(&q->unused_hctx_lock);
3628
Keith Busch868f2f02015-12-17 17:08:14 -07003629 blk_mq_realloc_hw_ctxs(set, q);
3630 if (!q->nr_hw_queues)
3631 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01003632
Christoph Hellwig287922e2015-10-30 20:57:30 +08003633 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08003634 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01003635
Jens Axboea8908932018-10-16 14:23:06 -06003636 q->tag_set = set;
Jens Axboe320ae512013-10-24 09:20:05 +01003637
Jens Axboe94eddfb2013-11-19 09:25:07 -07003638 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Ming Leicd191812018-12-18 12:15:29 +08003639 if (set->nr_maps > HCTX_TYPE_POLL &&
3640 set->map[HCTX_TYPE_POLL].nr_queues)
Christoph Hellwig6544d222018-12-02 17:46:28 +01003641 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
Jens Axboe320ae512013-10-24 09:20:05 +01003642
Mike Snitzer28494502016-09-14 13:28:30 -04003643 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06003644 INIT_LIST_HEAD(&q->requeue_list);
3645 spin_lock_init(&q->requeue_lock);
3646
Jens Axboeeba71762014-05-20 15:17:27 -06003647 q->nr_requests = set->queue_depth;
3648
Jens Axboe64f1c212016-11-14 13:03:03 -07003649 /*
3650 * Default to classic polling
3651 */
Yufen Yu29ece8b2019-03-18 22:44:41 +08003652 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
Jens Axboe64f1c212016-11-14 13:03:03 -07003653
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003654 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003655 blk_mq_add_queue_tag_set(set, q);
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003656 blk_mq_map_swqueue(q);
Christoph Hellwig26a97502021-06-02 09:53:17 +03003657 return 0;
Christoph Hellwig18741982014-02-10 09:29:00 -07003658
Jens Axboe320ae512013-10-24 09:20:05 +01003659err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07003660 kfree(q->queue_hw_ctx);
zhengbin73d9c8d2019-07-23 22:10:42 +08003661 q->nr_hw_queues = 0;
Ming Lei1db49092018-11-20 09:44:35 +08003662 blk_mq_sysfs_deinit(q);
Jes Sorensen41de54c2019-04-19 16:35:44 -04003663err_poll:
3664 blk_stat_free_callback(q->poll_cb);
3665 q->poll_cb = NULL;
Ming Linc7de5722016-05-25 23:23:27 -07003666err_exit:
3667 q->mq_ops = NULL;
Christoph Hellwig26a97502021-06-02 09:53:17 +03003668 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01003669}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003670EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01003671
Ming Leic7e2d942019-04-30 09:52:25 +08003672/* tags can _not_ be used after returning from blk_mq_exit_queue */
3673void blk_mq_exit_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01003674{
Bart Van Assche630ef622021-05-13 10:15:29 -07003675 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01003676
Bart Van Assche630ef622021-05-13 10:15:29 -07003677 /* Checks hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED. */
Ming Lei624dbe42014-05-27 23:35:13 +08003678 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
Bart Van Assche630ef622021-05-13 10:15:29 -07003679 /* May clear BLK_MQ_F_TAG_QUEUE_SHARED in hctx->flags. */
3680 blk_mq_del_queue_tag_set(q);
Jens Axboe320ae512013-10-24 09:20:05 +01003681}
Jens Axboe320ae512013-10-24 09:20:05 +01003682
Jens Axboea5164402014-09-10 09:02:03 -06003683static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
3684{
3685 int i;
3686
John Garry079a2e32021-10-05 18:23:39 +08003687 if (blk_mq_is_shared_tags(set->flags)) {
3688 set->shared_tags = blk_mq_alloc_map_and_rqs(set,
John Garrye155b0c2021-10-05 18:23:37 +08003689 BLK_MQ_NO_HCTX_IDX,
3690 set->queue_depth);
John Garry079a2e32021-10-05 18:23:39 +08003691 if (!set->shared_tags)
John Garrye155b0c2021-10-05 18:23:37 +08003692 return -ENOMEM;
3693 }
3694
Xianting Tian8229cca2020-09-26 10:39:47 +08003695 for (i = 0; i < set->nr_hw_queues; i++) {
John Garry63064be2021-10-05 18:23:35 +08003696 if (!__blk_mq_alloc_map_and_rqs(set, i))
Jens Axboea5164402014-09-10 09:02:03 -06003697 goto out_unwind;
Xianting Tian8229cca2020-09-26 10:39:47 +08003698 cond_resched();
3699 }
Jens Axboea5164402014-09-10 09:02:03 -06003700
3701 return 0;
3702
3703out_unwind:
John Garrye155b0c2021-10-05 18:23:37 +08003704 while (--i >= 0)
3705 __blk_mq_free_map_and_rqs(set, i);
3706
John Garry079a2e32021-10-05 18:23:39 +08003707 if (blk_mq_is_shared_tags(set->flags)) {
3708 blk_mq_free_map_and_rqs(set, set->shared_tags,
John Garrye155b0c2021-10-05 18:23:37 +08003709 BLK_MQ_NO_HCTX_IDX);
John Garry645db342021-10-05 18:23:36 +08003710 }
Jens Axboea5164402014-09-10 09:02:03 -06003711
Jens Axboea5164402014-09-10 09:02:03 -06003712 return -ENOMEM;
3713}
3714
3715/*
3716 * Allocate the request maps associated with this tag_set. Note that this
3717 * may reduce the depth asked for, if memory is tight. set->queue_depth
3718 * will be updated to reflect the allocated depth.
3719 */
John Garry63064be2021-10-05 18:23:35 +08003720static int blk_mq_alloc_set_map_and_rqs(struct blk_mq_tag_set *set)
Jens Axboea5164402014-09-10 09:02:03 -06003721{
3722 unsigned int depth;
3723 int err;
3724
3725 depth = set->queue_depth;
3726 do {
3727 err = __blk_mq_alloc_rq_maps(set);
3728 if (!err)
3729 break;
3730
3731 set->queue_depth >>= 1;
3732 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
3733 err = -ENOMEM;
3734 break;
3735 }
3736 } while (set->queue_depth);
3737
3738 if (!set->queue_depth || err) {
3739 pr_err("blk-mq: failed to allocate request map\n");
3740 return -ENOMEM;
3741 }
3742
3743 if (depth != set->queue_depth)
3744 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
3745 depth, set->queue_depth);
3746
3747 return 0;
3748}
3749
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003750static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
3751{
Bart Van Assche6e66b492020-03-09 21:26:17 -07003752 /*
3753 * blk_mq_map_queues() and multiple .map_queues() implementations
3754 * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the
3755 * number of hardware queues.
3756 */
3757 if (set->nr_maps == 1)
3758 set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues;
3759
Ming Lei59388702018-12-07 11:03:53 +08003760 if (set->ops->map_queues && !is_kdump_kernel()) {
Jens Axboeb3c661b2018-10-30 10:36:06 -06003761 int i;
3762
Ming Lei7d4901a2018-01-06 16:27:39 +08003763 /*
3764 * transport .map_queues is usually done in the following
3765 * way:
3766 *
3767 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
3768 * mask = get_cpu_mask(queue)
3769 * for_each_cpu(cpu, mask)
Jens Axboeb3c661b2018-10-30 10:36:06 -06003770 * set->map[x].mq_map[cpu] = queue;
Ming Lei7d4901a2018-01-06 16:27:39 +08003771 * }
3772 *
3773 * When we need to remap, the table has to be cleared for
3774 * killing stale mapping since one CPU may not be mapped
3775 * to any hw queue.
3776 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06003777 for (i = 0; i < set->nr_maps; i++)
3778 blk_mq_clear_mq_map(&set->map[i]);
Ming Lei7d4901a2018-01-06 16:27:39 +08003779
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003780 return set->ops->map_queues(set);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003781 } else {
3782 BUG_ON(set->nr_maps > 1);
Dongli Zhang7d76f852019-02-27 21:35:01 +08003783 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003784 }
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003785}
3786
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003787static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set,
3788 int cur_nr_hw_queues, int new_nr_hw_queues)
3789{
3790 struct blk_mq_tags **new_tags;
3791
3792 if (cur_nr_hw_queues >= new_nr_hw_queues)
3793 return 0;
3794
3795 new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *),
3796 GFP_KERNEL, set->numa_node);
3797 if (!new_tags)
3798 return -ENOMEM;
3799
3800 if (set->tags)
3801 memcpy(new_tags, set->tags, cur_nr_hw_queues *
3802 sizeof(*set->tags));
3803 kfree(set->tags);
3804 set->tags = new_tags;
3805 set->nr_hw_queues = new_nr_hw_queues;
3806
3807 return 0;
3808}
3809
Minwoo Im91cdf262020-12-05 00:20:53 +09003810static int blk_mq_alloc_tag_set_tags(struct blk_mq_tag_set *set,
3811 int new_nr_hw_queues)
3812{
3813 return blk_mq_realloc_tag_set_tags(set, 0, new_nr_hw_queues);
3814}
3815
Jens Axboea4391c62014-06-05 15:21:56 -06003816/*
3817 * Alloc a tag set to be associated with one or more request queues.
3818 * May fail with EINVAL for various error conditions. May adjust the
Minwoo Imc018c842018-06-30 22:12:41 +09003819 * requested depth down, if it's too large. In that case, the set
Jens Axboea4391c62014-06-05 15:21:56 -06003820 * value will be stored in set->queue_depth.
3821 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003822int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
3823{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003824 int i, ret;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003825
Bart Van Assche205fb5f2014-10-30 14:45:11 +01003826 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
3827
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003828 if (!set->nr_hw_queues)
3829 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06003830 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003831 return -EINVAL;
3832 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
3833 return -EINVAL;
3834
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02003835 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003836 return -EINVAL;
3837
Ming Leide148292017-10-14 17:22:29 +08003838 if (!set->ops->get_budget ^ !set->ops->put_budget)
3839 return -EINVAL;
3840
Jens Axboea4391c62014-06-05 15:21:56 -06003841 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
3842 pr_info("blk-mq: reduced tag depth to %u\n",
3843 BLK_MQ_MAX_DEPTH);
3844 set->queue_depth = BLK_MQ_MAX_DEPTH;
3845 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003846
Jens Axboeb3c661b2018-10-30 10:36:06 -06003847 if (!set->nr_maps)
3848 set->nr_maps = 1;
3849 else if (set->nr_maps > HCTX_MAX_TYPES)
3850 return -EINVAL;
3851
Shaohua Li6637fad2014-11-30 16:00:58 -08003852 /*
3853 * If a crashdump is active, then we are potentially in a very
3854 * memory constrained environment. Limit us to 1 queue and
3855 * 64 tags to prevent using too much memory.
3856 */
3857 if (is_kdump_kernel()) {
3858 set->nr_hw_queues = 1;
Ming Lei59388702018-12-07 11:03:53 +08003859 set->nr_maps = 1;
Shaohua Li6637fad2014-11-30 16:00:58 -08003860 set->queue_depth = min(64U, set->queue_depth);
3861 }
Keith Busch868f2f02015-12-17 17:08:14 -07003862 /*
Jens Axboe392546a2018-10-29 13:25:27 -06003863 * There is no use for more h/w queues than cpus if we just have
3864 * a single map
Keith Busch868f2f02015-12-17 17:08:14 -07003865 */
Jens Axboe392546a2018-10-29 13:25:27 -06003866 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003867 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08003868
Minwoo Im91cdf262020-12-05 00:20:53 +09003869 if (blk_mq_alloc_tag_set_tags(set, set->nr_hw_queues) < 0)
Jens Axboea5164402014-09-10 09:02:03 -06003870 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003871
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003872 ret = -ENOMEM;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003873 for (i = 0; i < set->nr_maps; i++) {
3874 set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
Ming Lei07b35eb2018-12-17 18:42:45 +08003875 sizeof(set->map[i].mq_map[0]),
Jens Axboeb3c661b2018-10-30 10:36:06 -06003876 GFP_KERNEL, set->numa_node);
3877 if (!set->map[i].mq_map)
3878 goto out_free_mq_map;
Ming Lei59388702018-12-07 11:03:53 +08003879 set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003880 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003881
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003882 ret = blk_mq_update_queue_map(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003883 if (ret)
3884 goto out_free_mq_map;
3885
John Garry63064be2021-10-05 18:23:35 +08003886 ret = blk_mq_alloc_set_map_and_rqs(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003887 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003888 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003889
Jens Axboe0d2602c2014-05-13 15:10:52 -06003890 mutex_init(&set->tag_list_lock);
3891 INIT_LIST_HEAD(&set->tag_list);
3892
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003893 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003894
3895out_free_mq_map:
Jens Axboeb3c661b2018-10-30 10:36:06 -06003896 for (i = 0; i < set->nr_maps; i++) {
3897 kfree(set->map[i].mq_map);
3898 set->map[i].mq_map = NULL;
3899 }
Robert Elliott5676e7b2014-09-02 11:38:44 -05003900 kfree(set->tags);
3901 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003902 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003903}
3904EXPORT_SYMBOL(blk_mq_alloc_tag_set);
3905
Christoph Hellwigcdb14e02021-06-02 09:53:16 +03003906/* allocate and initialize a tagset for a simple single-queue device */
3907int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set,
3908 const struct blk_mq_ops *ops, unsigned int queue_depth,
3909 unsigned int set_flags)
3910{
3911 memset(set, 0, sizeof(*set));
3912 set->ops = ops;
3913 set->nr_hw_queues = 1;
3914 set->nr_maps = 1;
3915 set->queue_depth = queue_depth;
3916 set->numa_node = NUMA_NO_NODE;
3917 set->flags = set_flags;
3918 return blk_mq_alloc_tag_set(set);
3919}
3920EXPORT_SYMBOL_GPL(blk_mq_alloc_sq_tag_set);
3921
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003922void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
3923{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003924 int i, j;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003925
John Garrye155b0c2021-10-05 18:23:37 +08003926 for (i = 0; i < set->nr_hw_queues; i++)
3927 __blk_mq_free_map_and_rqs(set, i);
Jens Axboe484b4062014-05-21 14:01:15 -06003928
John Garry079a2e32021-10-05 18:23:39 +08003929 if (blk_mq_is_shared_tags(set->flags)) {
3930 blk_mq_free_map_and_rqs(set, set->shared_tags,
John Garrye155b0c2021-10-05 18:23:37 +08003931 BLK_MQ_NO_HCTX_IDX);
3932 }
John Garry32bc15a2020-08-19 23:20:24 +08003933
Jens Axboeb3c661b2018-10-30 10:36:06 -06003934 for (j = 0; j < set->nr_maps; j++) {
3935 kfree(set->map[j].mq_map);
3936 set->map[j].mq_map = NULL;
3937 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003938
Ming Lei981bd182014-04-24 00:07:34 +08003939 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05003940 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003941}
3942EXPORT_SYMBOL(blk_mq_free_tag_set);
3943
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003944int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
3945{
3946 struct blk_mq_tag_set *set = q->tag_set;
3947 struct blk_mq_hw_ctx *hctx;
3948 int i, ret;
3949
Jens Axboebd166ef2017-01-17 06:03:22 -07003950 if (!set)
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003951 return -EINVAL;
3952
Aleksei Zakharove5fa8142019-02-08 19:14:05 +03003953 if (q->nr_requests == nr)
3954 return 0;
3955
Jens Axboe70f36b62017-01-19 10:59:07 -07003956 blk_mq_freeze_queue(q);
Ming Lei24f5a902018-01-06 16:27:38 +08003957 blk_mq_quiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003958
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003959 ret = 0;
3960 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07003961 if (!hctx->tags)
3962 continue;
Jens Axboebd166ef2017-01-17 06:03:22 -07003963 /*
3964 * If we're using an MQ scheduler, just update the scheduler
3965 * queue depth. This is similar to what the old code would do.
3966 */
John Garryf6adcef2021-10-05 18:23:29 +08003967 if (hctx->sched_tags) {
Jens Axboe70f36b62017-01-19 10:59:07 -07003968 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
John Garryf6adcef2021-10-05 18:23:29 +08003969 nr, true);
John Garryf6adcef2021-10-05 18:23:29 +08003970 } else {
3971 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
3972 false);
Jens Axboe70f36b62017-01-19 10:59:07 -07003973 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003974 if (ret)
3975 break;
Jens Axboe77f1e0a2019-01-18 10:34:16 -07003976 if (q->elevator && q->elevator->type->ops.depth_updated)
3977 q->elevator->type->ops.depth_updated(hctx);
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003978 }
John Garryd97e5942021-05-13 20:00:58 +08003979 if (!ret) {
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003980 q->nr_requests = nr;
John Garry079a2e32021-10-05 18:23:39 +08003981 if (blk_mq_is_shared_tags(set->flags)) {
John Garry8fa04462021-10-05 18:23:28 +08003982 if (q->elevator)
John Garry079a2e32021-10-05 18:23:39 +08003983 blk_mq_tag_update_sched_shared_tags(q);
John Garry8fa04462021-10-05 18:23:28 +08003984 else
John Garry079a2e32021-10-05 18:23:39 +08003985 blk_mq_tag_resize_shared_tags(set, nr);
John Garry8fa04462021-10-05 18:23:28 +08003986 }
John Garryd97e5942021-05-13 20:00:58 +08003987 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003988
Ming Lei24f5a902018-01-06 16:27:38 +08003989 blk_mq_unquiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003990 blk_mq_unfreeze_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003991
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003992 return ret;
3993}
3994
Jianchao Wangd48ece22018-08-21 15:15:03 +08003995/*
3996 * request_queue and elevator_type pair.
3997 * It is just used by __blk_mq_update_nr_hw_queues to cache
3998 * the elevator_type associated with a request_queue.
3999 */
4000struct blk_mq_qe_pair {
4001 struct list_head node;
4002 struct request_queue *q;
4003 struct elevator_type *type;
4004};
4005
4006/*
4007 * Cache the elevator_type in qe pair list and switch the
4008 * io scheduler to 'none'
4009 */
4010static bool blk_mq_elv_switch_none(struct list_head *head,
4011 struct request_queue *q)
4012{
4013 struct blk_mq_qe_pair *qe;
4014
4015 if (!q->elevator)
4016 return true;
4017
4018 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
4019 if (!qe)
4020 return false;
4021
4022 INIT_LIST_HEAD(&qe->node);
4023 qe->q = q;
4024 qe->type = q->elevator->type;
4025 list_add(&qe->node, head);
4026
4027 mutex_lock(&q->sysfs_lock);
4028 /*
4029 * After elevator_switch_mq, the previous elevator_queue will be
4030 * released by elevator_release. The reference of the io scheduler
4031 * module get by elevator_get will also be put. So we need to get
4032 * a reference of the io scheduler module here to prevent it to be
4033 * removed.
4034 */
4035 __module_get(qe->type->elevator_owner);
4036 elevator_switch_mq(q, NULL);
4037 mutex_unlock(&q->sysfs_lock);
4038
4039 return true;
4040}
4041
4042static void blk_mq_elv_switch_back(struct list_head *head,
4043 struct request_queue *q)
4044{
4045 struct blk_mq_qe_pair *qe;
4046 struct elevator_type *t = NULL;
4047
4048 list_for_each_entry(qe, head, node)
4049 if (qe->q == q) {
4050 t = qe->type;
4051 break;
4052 }
4053
4054 if (!t)
4055 return;
4056
4057 list_del(&qe->node);
4058 kfree(qe);
4059
4060 mutex_lock(&q->sysfs_lock);
4061 elevator_switch_mq(q, t);
4062 mutex_unlock(&q->sysfs_lock);
4063}
4064
Keith Busche4dc2b32017-05-30 14:39:11 -04004065static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
4066 int nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07004067{
4068 struct request_queue *q;
Jianchao Wangd48ece22018-08-21 15:15:03 +08004069 LIST_HEAD(head);
Jianchao Wange01ad462018-10-12 18:07:28 +08004070 int prev_nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07004071
Bart Van Assche705cda92017-04-07 11:16:49 -07004072 lockdep_assert_held(&set->tag_list_lock);
4073
Jens Axboe392546a2018-10-29 13:25:27 -06004074 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07004075 nr_hw_queues = nr_cpu_ids;
Weiping Zhangfe35ec52020-06-17 14:18:37 +08004076 if (nr_hw_queues < 1)
4077 return;
4078 if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07004079 return;
4080
4081 list_for_each_entry(q, &set->tag_list, tag_set_list)
4082 blk_mq_freeze_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +08004083 /*
4084 * Switch IO scheduler to 'none', cleaning up the data associated
4085 * with the previous scheduler. We will switch back once we are done
4086 * updating the new sw to hw queue mappings.
4087 */
4088 list_for_each_entry(q, &set->tag_list, tag_set_list)
4089 if (!blk_mq_elv_switch_none(&head, q))
4090 goto switch_back;
Keith Busch868f2f02015-12-17 17:08:14 -07004091
Jianchao Wang477e19d2018-10-12 18:07:25 +08004092 list_for_each_entry(q, &set->tag_list, tag_set_list) {
4093 blk_mq_debugfs_unregister_hctxs(q);
4094 blk_mq_sysfs_unregister(q);
4095 }
4096
Weiping Zhanga2584e42020-05-07 21:03:56 +08004097 prev_nr_hw_queues = set->nr_hw_queues;
Bart Van Asschef7e76db2019-10-25 09:50:10 -07004098 if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) <
4099 0)
4100 goto reregister;
4101
Keith Busch868f2f02015-12-17 17:08:14 -07004102 set->nr_hw_queues = nr_hw_queues;
Jianchao Wange01ad462018-10-12 18:07:28 +08004103fallback:
Weiping Zhangaa880ad2020-05-13 08:44:05 +08004104 blk_mq_update_queue_map(set);
Keith Busch868f2f02015-12-17 17:08:14 -07004105 list_for_each_entry(q, &set->tag_list, tag_set_list) {
4106 blk_mq_realloc_hw_ctxs(set, q);
Jianchao Wange01ad462018-10-12 18:07:28 +08004107 if (q->nr_hw_queues != set->nr_hw_queues) {
4108 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
4109 nr_hw_queues, prev_nr_hw_queues);
4110 set->nr_hw_queues = prev_nr_hw_queues;
Dongli Zhang7d76f852019-02-27 21:35:01 +08004111 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jianchao Wange01ad462018-10-12 18:07:28 +08004112 goto fallback;
4113 }
Jianchao Wang477e19d2018-10-12 18:07:25 +08004114 blk_mq_map_swqueue(q);
4115 }
4116
Bart Van Asschef7e76db2019-10-25 09:50:10 -07004117reregister:
Jianchao Wang477e19d2018-10-12 18:07:25 +08004118 list_for_each_entry(q, &set->tag_list, tag_set_list) {
4119 blk_mq_sysfs_register(q);
4120 blk_mq_debugfs_register_hctxs(q);
Keith Busch868f2f02015-12-17 17:08:14 -07004121 }
4122
Jianchao Wangd48ece22018-08-21 15:15:03 +08004123switch_back:
4124 list_for_each_entry(q, &set->tag_list, tag_set_list)
4125 blk_mq_elv_switch_back(&head, q);
4126
Keith Busch868f2f02015-12-17 17:08:14 -07004127 list_for_each_entry(q, &set->tag_list, tag_set_list)
4128 blk_mq_unfreeze_queue(q);
4129}
Keith Busche4dc2b32017-05-30 14:39:11 -04004130
4131void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
4132{
4133 mutex_lock(&set->tag_list_lock);
4134 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
4135 mutex_unlock(&set->tag_list_lock);
4136}
Keith Busch868f2f02015-12-17 17:08:14 -07004137EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
4138
Omar Sandoval34dbad52017-03-21 08:56:08 -07004139/* Enable polling stats and return whether they were already enabled. */
4140static bool blk_poll_stats_enable(struct request_queue *q)
4141{
4142 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
Bart Van Assche7dfdbc72018-03-07 17:10:05 -08004143 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
Omar Sandoval34dbad52017-03-21 08:56:08 -07004144 return true;
4145 blk_stat_add_callback(q, q->poll_cb);
4146 return false;
4147}
4148
4149static void blk_mq_poll_stats_start(struct request_queue *q)
4150{
4151 /*
4152 * We don't arm the callback if polling stats are not enabled or the
4153 * callback is already active.
4154 */
4155 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
4156 blk_stat_is_active(q->poll_cb))
4157 return;
4158
4159 blk_stat_activate_msecs(q->poll_cb, 100);
4160}
4161
4162static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
4163{
4164 struct request_queue *q = cb->data;
Stephen Bates720b8cc2017-04-07 06:24:03 -06004165 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -07004166
Stephen Bates720b8cc2017-04-07 06:24:03 -06004167 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
4168 if (cb->stat[bucket].nr_samples)
4169 q->poll_stat[bucket] = cb->stat[bucket];
4170 }
Omar Sandoval34dbad52017-03-21 08:56:08 -07004171}
4172
Jens Axboe64f1c212016-11-14 13:03:03 -07004173static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
Jens Axboe64f1c212016-11-14 13:03:03 -07004174 struct request *rq)
4175{
Jens Axboe64f1c212016-11-14 13:03:03 -07004176 unsigned long ret = 0;
Stephen Bates720b8cc2017-04-07 06:24:03 -06004177 int bucket;
Jens Axboe64f1c212016-11-14 13:03:03 -07004178
4179 /*
4180 * If stats collection isn't on, don't sleep but turn it on for
4181 * future users
4182 */
Omar Sandoval34dbad52017-03-21 08:56:08 -07004183 if (!blk_poll_stats_enable(q))
Jens Axboe64f1c212016-11-14 13:03:03 -07004184 return 0;
4185
4186 /*
Jens Axboe64f1c212016-11-14 13:03:03 -07004187 * As an optimistic guess, use half of the mean service time
4188 * for this type of request. We can (and should) make this smarter.
4189 * For instance, if the completion latencies are tight, we can
4190 * get closer than just half the mean. This is especially
4191 * important on devices where the completion latencies are longer
Stephen Bates720b8cc2017-04-07 06:24:03 -06004192 * than ~10 usec. We do use the stats for the relevant IO size
4193 * if available which does lead to better estimates.
Jens Axboe64f1c212016-11-14 13:03:03 -07004194 */
Stephen Bates720b8cc2017-04-07 06:24:03 -06004195 bucket = blk_mq_poll_stats_bkt(rq);
4196 if (bucket < 0)
4197 return ret;
4198
4199 if (q->poll_stat[bucket].nr_samples)
4200 ret = (q->poll_stat[bucket].mean + 1) / 2;
Jens Axboe64f1c212016-11-14 13:03:03 -07004201
4202 return ret;
4203}
4204
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004205static bool blk_mq_poll_hybrid(struct request_queue *q, blk_qc_t qc)
Jens Axboe06426ad2016-11-14 13:01:59 -07004206{
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004207 struct blk_mq_hw_ctx *hctx = blk_qc_to_hctx(q, qc);
4208 struct request *rq = blk_qc_to_rq(hctx, qc);
Jens Axboe06426ad2016-11-14 13:01:59 -07004209 struct hrtimer_sleeper hs;
4210 enum hrtimer_mode mode;
Jens Axboe64f1c212016-11-14 13:03:03 -07004211 unsigned int nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07004212 ktime_t kt;
4213
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004214 /*
4215 * If a request has completed on queue that uses an I/O scheduler, we
4216 * won't get back a request from blk_qc_to_rq.
4217 */
4218 if (!rq || (rq->rq_flags & RQF_MQ_POLL_SLEPT))
Jens Axboe64f1c212016-11-14 13:03:03 -07004219 return false;
4220
4221 /*
Jens Axboe1052b8a2018-11-26 08:21:49 -07004222 * If we get here, hybrid polling is enabled. Hence poll_nsec can be:
Jens Axboe64f1c212016-11-14 13:03:03 -07004223 *
Jens Axboe64f1c212016-11-14 13:03:03 -07004224 * 0: use half of prev avg
4225 * >0: use this specific value
4226 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07004227 if (q->poll_nsec > 0)
Jens Axboe64f1c212016-11-14 13:03:03 -07004228 nsecs = q->poll_nsec;
4229 else
John Garrycae740a2020-02-26 20:10:15 +08004230 nsecs = blk_mq_poll_nsecs(q, rq);
Jens Axboe64f1c212016-11-14 13:03:03 -07004231
4232 if (!nsecs)
Jens Axboe06426ad2016-11-14 13:01:59 -07004233 return false;
4234
Jens Axboe76a86f92018-01-10 11:30:56 -07004235 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
Jens Axboe06426ad2016-11-14 13:01:59 -07004236
4237 /*
4238 * This will be replaced with the stats tracking code, using
4239 * 'avg_completion_time / 2' as the pre-sleep target.
4240 */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01004241 kt = nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07004242
4243 mode = HRTIMER_MODE_REL;
Sebastian Andrzej Siewiordbc16252019-07-26 20:30:50 +02004244 hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07004245 hrtimer_set_expires(&hs.timer, kt);
4246
Jens Axboe06426ad2016-11-14 13:01:59 -07004247 do {
Tejun Heo5a61c362018-01-09 08:29:52 -08004248 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
Jens Axboe06426ad2016-11-14 13:01:59 -07004249 break;
4250 set_current_state(TASK_UNINTERRUPTIBLE);
Thomas Gleixner9dd88132019-07-30 21:16:55 +02004251 hrtimer_sleeper_start_expires(&hs, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07004252 if (hs.task)
4253 io_schedule();
4254 hrtimer_cancel(&hs.timer);
4255 mode = HRTIMER_MODE_ABS;
4256 } while (hs.task && !signal_pending(current));
4257
4258 __set_current_state(TASK_RUNNING);
4259 destroy_hrtimer_on_stack(&hs.timer);
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004260
4261 /*
4262 * If we sleep, have the caller restart the poll loop to reset the
4263 * state. Like for the other success return cases, the caller is
4264 * responsible for checking if the IO completed. If the IO isn't
4265 * complete, we'll get called again and will go straight to the busy
4266 * poll loop.
4267 */
Jens Axboe06426ad2016-11-14 13:01:59 -07004268 return true;
4269}
4270
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004271static int blk_mq_poll_classic(struct request_queue *q, blk_qc_t cookie,
Jens Axboe5a72e892021-10-12 09:24:29 -06004272 struct io_comp_batch *iob, unsigned int flags)
Jens Axboebbd7bb72016-11-04 09:34:34 -06004273{
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004274 struct blk_mq_hw_ctx *hctx = blk_qc_to_hctx(q, cookie);
4275 long state = get_current_state();
4276 int ret;
Jens Axboe1052b8a2018-11-26 08:21:49 -07004277
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004278 do {
Jens Axboe5a72e892021-10-12 09:24:29 -06004279 ret = q->mq_ops->poll(hctx, iob);
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004280 if (ret > 0) {
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004281 __set_current_state(TASK_RUNNING);
4282 return ret;
4283 }
4284
4285 if (signal_pending_state(state, current))
4286 __set_current_state(TASK_RUNNING);
4287 if (task_is_running(current))
4288 return 1;
4289
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02004290 if (ret < 0 || (flags & BLK_POLL_ONESHOT))
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004291 break;
4292 cpu_relax();
4293 } while (!need_resched());
4294
4295 __set_current_state(TASK_RUNNING);
4296 return 0;
Jens Axboe1052b8a2018-11-26 08:21:49 -07004297}
4298
Jens Axboe5a72e892021-10-12 09:24:29 -06004299int blk_mq_poll(struct request_queue *q, blk_qc_t cookie, struct io_comp_batch *iob,
4300 unsigned int flags)
Jens Axboe1052b8a2018-11-26 08:21:49 -07004301{
Christoph Hellwigd729cf92021-10-12 13:12:20 +02004302 if (!(flags & BLK_POLL_NOSLEEP) &&
4303 q->poll_nsec != BLK_MQ_POLL_CLASSIC) {
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004304 if (blk_mq_poll_hybrid(q, cookie))
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004305 return 1;
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004306 }
Jens Axboe5a72e892021-10-12 09:24:29 -06004307 return blk_mq_poll_classic(q, cookie, iob, flags);
Jens Axboebbd7bb72016-11-04 09:34:34 -06004308}
4309
Jens Axboe9cf2bab2018-10-31 17:01:22 -06004310unsigned int blk_mq_rq_cpu(struct request *rq)
4311{
4312 return rq->mq_ctx->cpu;
4313}
4314EXPORT_SYMBOL(blk_mq_rq_cpu);
4315
Jens Axboe320ae512013-10-24 09:20:05 +01004316static int __init blk_mq_init(void)
4317{
Christoph Hellwigc3077b52020-06-11 08:44:41 +02004318 int i;
4319
4320 for_each_possible_cpu(i)
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +01004321 init_llist_head(&per_cpu(blk_cpu_done, i));
Christoph Hellwigc3077b52020-06-11 08:44:41 +02004322 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
4323
4324 cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
4325 "block/softirq:dead", NULL,
4326 blk_softirq_cpu_dead);
Thomas Gleixner9467f852016-09-22 08:05:17 -06004327 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
4328 blk_mq_hctx_notify_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02004329 cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online",
4330 blk_mq_hctx_notify_online,
4331 blk_mq_hctx_notify_offline);
Jens Axboe320ae512013-10-24 09:20:05 +01004332 return 0;
4333}
4334subsys_initcall(blk_mq_init);