blob: 3217139d2e0b34a1bb799ffdd637e56516ef7d9d [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>
Christoph Hellwig4054cff2021-11-17 07:13:56 +010031#include <linux/sched/sysctl.h>
Jens Axboe320ae512013-10-24 09:20:05 +010032
33#include <trace/events/block.h>
34
35#include <linux/blk-mq.h>
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +030036#include <linux/t10-pi.h>
Jens Axboe320ae512013-10-24 09:20:05 +010037#include "blk.h"
38#include "blk-mq.h"
Omar Sandoval9c1051a2017-05-04 08:17:21 -060039#include "blk-mq-debugfs.h"
Jens Axboe320ae512013-10-24 09:20:05 +010040#include "blk-mq-tag.h"
Bart Van Assche986d4132018-09-26 14:01:10 -070041#include "blk-pm.h"
Jens Axboecf43e6b2016-11-07 21:32:37 -070042#include "blk-stat.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070043#include "blk-mq-sched.h"
Josef Bacikc1c80382018-07-03 11:14:59 -040044#include "blk-rq-qos.h"
Jens Axboe320ae512013-10-24 09:20:05 +010045
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +010046static DEFINE_PER_CPU(struct llist_head, blk_cpu_done);
Christoph Hellwigc3077b52020-06-11 08:44:41 +020047
Omar Sandoval34dbad52017-03-21 08:56:08 -070048static void blk_mq_poll_stats_start(struct request_queue *q);
49static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);
50
Stephen Bates720b8cc2017-04-07 06:24:03 -060051static int blk_mq_poll_stats_bkt(const struct request *rq)
52{
Hou Tao3d244302019-05-21 15:59:03 +080053 int ddir, sectors, bucket;
Stephen Bates720b8cc2017-04-07 06:24:03 -060054
Jens Axboe99c749a2017-04-21 07:55:42 -060055 ddir = rq_data_dir(rq);
Hou Tao3d244302019-05-21 15:59:03 +080056 sectors = blk_rq_stats_sectors(rq);
Stephen Bates720b8cc2017-04-07 06:24:03 -060057
Hou Tao3d244302019-05-21 15:59:03 +080058 bucket = ddir + 2 * ilog2(sectors);
Stephen Bates720b8cc2017-04-07 06:24:03 -060059
60 if (bucket < 0)
61 return -1;
62 else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
63 return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
64
65 return bucket;
66}
67
Christoph Hellwig3e087732021-10-12 13:12:24 +020068#define BLK_QC_T_SHIFT 16
69#define BLK_QC_T_INTERNAL (1U << 31)
70
Christoph Hellwigf70299f2021-10-12 13:12:15 +020071static inline struct blk_mq_hw_ctx *blk_qc_to_hctx(struct request_queue *q,
72 blk_qc_t qc)
73{
74 return q->queue_hw_ctx[(qc & ~BLK_QC_T_INTERNAL) >> BLK_QC_T_SHIFT];
75}
76
Christoph Hellwigc6699d62021-10-12 13:12:16 +020077static inline struct request *blk_qc_to_rq(struct blk_mq_hw_ctx *hctx,
78 blk_qc_t qc)
79{
Christoph Hellwigefbabbe2021-10-12 13:12:17 +020080 unsigned int tag = qc & ((1U << BLK_QC_T_SHIFT) - 1);
81
82 if (qc & BLK_QC_T_INTERNAL)
83 return blk_mq_tag_to_rq(hctx->sched_tags, tag);
84 return blk_mq_tag_to_rq(hctx->tags, tag);
Christoph Hellwigc6699d62021-10-12 13:12:16 +020085}
86
Christoph Hellwig3e087732021-10-12 13:12:24 +020087static inline blk_qc_t blk_rq_to_qc(struct request *rq)
88{
89 return (rq->mq_hctx->queue_num << BLK_QC_T_SHIFT) |
90 (rq->tag != -1 ?
91 rq->tag : (rq->internal_tag | BLK_QC_T_INTERNAL));
92}
93
Jens Axboe320ae512013-10-24 09:20:05 +010094/*
Yufen Yu85fae292019-03-24 17:57:08 +080095 * Check if any of the ctx, dispatch list or elevator
96 * have pending work in this hardware queue.
Jens Axboe320ae512013-10-24 09:20:05 +010097 */
Jens Axboe79f720a2017-11-10 09:13:21 -070098static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
Jens Axboe320ae512013-10-24 09:20:05 +010099{
Jens Axboe79f720a2017-11-10 09:13:21 -0700100 return !list_empty_careful(&hctx->dispatch) ||
101 sbitmap_any_bit_set(&hctx->ctx_map) ||
Jens Axboebd166ef2017-01-17 06:03:22 -0700102 blk_mq_sched_has_work(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +0100103}
104
105/*
106 * Mark this ctx as having pending work in this hardware queue
107 */
108static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
109 struct blk_mq_ctx *ctx)
110{
Jens Axboef31967f2018-10-29 13:13:29 -0600111 const int bit = ctx->index_hw[hctx->type];
112
113 if (!sbitmap_test_bit(&hctx->ctx_map, bit))
114 sbitmap_set_bit(&hctx->ctx_map, bit);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600115}
116
117static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
118 struct blk_mq_ctx *ctx)
119{
Jens Axboef31967f2018-10-29 13:13:29 -0600120 const int bit = ctx->index_hw[hctx->type];
121
122 sbitmap_clear_bit(&hctx->ctx_map, bit);
Jens Axboe320ae512013-10-24 09:20:05 +0100123}
124
Jens Axboef299b7c2017-08-08 17:51:45 -0600125struct mq_inflight {
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100126 struct block_device *part;
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300127 unsigned int inflight[2];
Jens Axboef299b7c2017-08-08 17:51:45 -0600128};
129
Jens Axboe7baa8572018-11-08 10:24:07 -0700130static bool blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx,
Jens Axboef299b7c2017-08-08 17:51:45 -0600131 struct request *rq, void *priv,
132 bool reserved)
133{
134 struct mq_inflight *mi = priv;
135
Jeffle Xub0d97552020-12-02 19:11:45 +0800136 if ((!mi->part->bd_partno || rq->part == mi->part) &&
137 blk_mq_rq_state(rq) == MQ_RQ_IN_FLIGHT)
Pavel Begunkovbb4e6b12019-09-30 21:55:33 +0300138 mi->inflight[rq_data_dir(rq)]++;
Jens Axboe7baa8572018-11-08 10:24:07 -0700139
140 return true;
Jens Axboef299b7c2017-08-08 17:51:45 -0600141}
142
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100143unsigned int blk_mq_in_flight(struct request_queue *q,
144 struct block_device *part)
Jens Axboef299b7c2017-08-08 17:51:45 -0600145{
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300146 struct mq_inflight mi = { .part = part };
Jens Axboef299b7c2017-08-08 17:51:45 -0600147
Jens Axboef299b7c2017-08-08 17:51:45 -0600148 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
Mikulas Patockae016b782018-12-06 11:41:21 -0500149
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300150 return mi.inflight[0] + mi.inflight[1];
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700151}
152
Christoph Hellwig8446fe92020-11-24 09:36:54 +0100153void blk_mq_in_flight_rw(struct request_queue *q, struct block_device *part,
154 unsigned int inflight[2])
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700155{
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300156 struct mq_inflight mi = { .part = part };
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700157
Pavel Begunkovbb4e6b12019-09-30 21:55:33 +0300158 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300159 inflight[0] = mi.inflight[0];
160 inflight[1] = mi.inflight[1];
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700161}
162
Ming Lei1671d522017-03-27 20:06:57 +0800163void blk_freeze_queue_start(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +0800164{
Bob Liu7996a8b2019-05-21 11:25:55 +0800165 mutex_lock(&q->mq_freeze_lock);
166 if (++q->mq_freeze_depth == 1) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400167 percpu_ref_kill(&q->q_usage_counter);
Bob Liu7996a8b2019-05-21 11:25:55 +0800168 mutex_unlock(&q->mq_freeze_lock);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700169 if (queue_is_mq(q))
Ming Lei055f6e12017-11-09 10:49:53 -0800170 blk_mq_run_hw_queues(q, false);
Bob Liu7996a8b2019-05-21 11:25:55 +0800171 } else {
172 mutex_unlock(&q->mq_freeze_lock);
Tejun Heocddd5d12014-08-16 08:02:24 -0400173 }
Tejun Heof3af0202014-11-04 13:52:27 -0500174}
Ming Lei1671d522017-03-27 20:06:57 +0800175EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
Tejun Heof3af0202014-11-04 13:52:27 -0500176
Keith Busch6bae363e2017-03-01 14:22:10 -0500177void blk_mq_freeze_queue_wait(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500178{
Dan Williams3ef28e82015-10-21 13:20:12 -0400179 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +0800180}
Keith Busch6bae363e2017-03-01 14:22:10 -0500181EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800182
Keith Buschf91328c2017-03-01 14:22:11 -0500183int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
184 unsigned long timeout)
185{
186 return wait_event_timeout(q->mq_freeze_wq,
187 percpu_ref_is_zero(&q->q_usage_counter),
188 timeout);
189}
190EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
Jens Axboe320ae512013-10-24 09:20:05 +0100191
Tejun Heof3af0202014-11-04 13:52:27 -0500192/*
193 * Guarantee no request is in use, so we can change any data structure of
194 * the queue afterward.
195 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400196void blk_freeze_queue(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500197{
Dan Williams3ef28e82015-10-21 13:20:12 -0400198 /*
199 * In the !blk_mq case we are only calling this to kill the
200 * q_usage_counter, otherwise this increases the freeze depth
201 * and waits for it to return to zero. For this reason there is
202 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
203 * exported to drivers as the only user for unfreeze is blk_mq.
204 */
Ming Lei1671d522017-03-27 20:06:57 +0800205 blk_freeze_queue_start(q);
Tejun Heof3af0202014-11-04 13:52:27 -0500206 blk_mq_freeze_queue_wait(q);
207}
Dan Williams3ef28e82015-10-21 13:20:12 -0400208
209void blk_mq_freeze_queue(struct request_queue *q)
210{
211 /*
212 * ...just an alias to keep freeze and unfreeze actions balanced
213 * in the blk_mq_* namespace
214 */
215 blk_freeze_queue(q);
216}
Jens Axboec761d962015-01-02 15:05:12 -0700217EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
Tejun Heof3af0202014-11-04 13:52:27 -0500218
Christoph Hellwigaec89dc2021-09-29 09:12:41 +0200219void __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic)
Jens Axboe320ae512013-10-24 09:20:05 +0100220{
Bob Liu7996a8b2019-05-21 11:25:55 +0800221 mutex_lock(&q->mq_freeze_lock);
Christoph Hellwigaec89dc2021-09-29 09:12:41 +0200222 if (force_atomic)
223 q->q_usage_counter.data->force_atomic = true;
Bob Liu7996a8b2019-05-21 11:25:55 +0800224 q->mq_freeze_depth--;
225 WARN_ON_ONCE(q->mq_freeze_depth < 0);
226 if (!q->mq_freeze_depth) {
Bart Van Asschebdd63162018-09-26 14:01:08 -0700227 percpu_ref_resurrect(&q->q_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100228 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600229 }
Bob Liu7996a8b2019-05-21 11:25:55 +0800230 mutex_unlock(&q->mq_freeze_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100231}
Christoph Hellwigaec89dc2021-09-29 09:12:41 +0200232
233void blk_mq_unfreeze_queue(struct request_queue *q)
234{
235 __blk_mq_unfreeze_queue(q, false);
236}
Keith Buschb4c6a022014-12-19 17:54:14 -0700237EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
Jens Axboe320ae512013-10-24 09:20:05 +0100238
Bart Van Assche852ec802017-06-21 10:55:47 -0700239/*
240 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
241 * mpt3sas driver such that this function can be removed.
242 */
243void blk_mq_quiesce_queue_nowait(struct request_queue *q)
244{
Ming Leie70feb82021-10-14 16:17:10 +0800245 unsigned long flags;
246
247 spin_lock_irqsave(&q->queue_lock, flags);
248 if (!q->quiesce_depth++)
249 blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
250 spin_unlock_irqrestore(&q->queue_lock, flags);
Bart Van Assche852ec802017-06-21 10:55:47 -0700251}
252EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
253
Bart Van Assche6a83e742016-11-02 10:09:51 -0600254/**
Ming Lei9ef4d022021-11-09 15:11:41 +0800255 * blk_mq_wait_quiesce_done() - wait until in-progress quiesce is done
256 * @q: request queue.
257 *
258 * Note: it is driver's responsibility for making sure that quiesce has
259 * been started.
260 */
261void blk_mq_wait_quiesce_done(struct request_queue *q)
262{
263 struct blk_mq_hw_ctx *hctx;
264 unsigned int i;
265 bool rcu = false;
266
267 queue_for_each_hw_ctx(q, hctx, i) {
268 if (hctx->flags & BLK_MQ_F_BLOCKING)
269 synchronize_srcu(hctx->srcu);
270 else
271 rcu = true;
272 }
273 if (rcu)
274 synchronize_rcu();
275}
276EXPORT_SYMBOL_GPL(blk_mq_wait_quiesce_done);
277
278/**
Ming Lei69e07c42017-06-06 23:22:07 +0800279 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
Bart Van Assche6a83e742016-11-02 10:09:51 -0600280 * @q: request queue.
281 *
282 * Note: this function does not prevent that the struct request end_io()
Ming Lei69e07c42017-06-06 23:22:07 +0800283 * callback function is invoked. Once this function is returned, we make
284 * sure no dispatch can happen until the queue is unquiesced via
285 * blk_mq_unquiesce_queue().
Bart Van Assche6a83e742016-11-02 10:09:51 -0600286 */
287void blk_mq_quiesce_queue(struct request_queue *q)
288{
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800289 blk_mq_quiesce_queue_nowait(q);
Ming Lei9ef4d022021-11-09 15:11:41 +0800290 blk_mq_wait_quiesce_done(q);
Bart Van Assche6a83e742016-11-02 10:09:51 -0600291}
292EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
293
Ming Leie4e73912017-06-06 23:22:03 +0800294/*
295 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
296 * @q: request queue.
297 *
298 * This function recovers queue into the state before quiescing
299 * which is done by blk_mq_quiesce_queue.
300 */
301void blk_mq_unquiesce_queue(struct request_queue *q)
302{
Ming Leie70feb82021-10-14 16:17:10 +0800303 unsigned long flags;
304 bool run_queue = false;
305
306 spin_lock_irqsave(&q->queue_lock, flags);
307 if (WARN_ON_ONCE(q->quiesce_depth <= 0)) {
308 ;
309 } else if (!--q->quiesce_depth) {
310 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
311 run_queue = true;
312 }
313 spin_unlock_irqrestore(&q->queue_lock, flags);
Ming Leif4560ff2017-06-18 14:24:27 -0600314
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800315 /* dispatch requests which are inserted during quiescing */
Ming Leie70feb82021-10-14 16:17:10 +0800316 if (run_queue)
317 blk_mq_run_hw_queues(q, true);
Ming Leie4e73912017-06-06 23:22:03 +0800318}
319EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
320
Jens Axboeaed3ea92014-12-22 14:04:42 -0700321void blk_mq_wake_waiters(struct request_queue *q)
322{
323 struct blk_mq_hw_ctx *hctx;
324 unsigned int i;
325
326 queue_for_each_hw_ctx(q, hctx, i)
327 if (blk_mq_hw_queue_mapped(hctx))
328 blk_mq_tag_wakeup_all(hctx->tags, true);
329}
330
Christoph Hellwig52fdbbc2021-11-17 07:13:59 +0100331void blk_rq_init(struct request_queue *q, struct request *rq)
332{
333 memset(rq, 0, sizeof(*rq));
334
335 INIT_LIST_HEAD(&rq->queuelist);
336 rq->q = q;
337 rq->__sector = (sector_t) -1;
338 INIT_HLIST_NODE(&rq->hash);
339 RB_CLEAR_NODE(&rq->rb_node);
340 rq->tag = BLK_MQ_NO_TAG;
341 rq->internal_tag = BLK_MQ_NO_TAG;
342 rq->start_time_ns = ktime_get_ns();
343 rq->part = NULL;
344 blk_crypto_rq_set_defaults(rq);
345}
346EXPORT_SYMBOL(blk_rq_init);
347
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200348static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
Jens Axboefe6134f2021-10-19 09:32:58 -0600349 struct blk_mq_tags *tags, unsigned int tag, u64 alloc_time_ns)
Jens Axboe320ae512013-10-24 09:20:05 +0100350{
Pavel Begunkov605f7842021-10-18 21:37:28 +0100351 struct blk_mq_ctx *ctx = data->ctx;
352 struct blk_mq_hw_ctx *hctx = data->hctx;
353 struct request_queue *q = data->q;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200354 struct request *rq = tags->static_rqs[tag];
Bart Van Asschec3a148d2017-06-20 11:15:43 -0700355
Jens Axboec7b84d42021-10-19 09:33:00 -0600356 rq->q = q;
357 rq->mq_ctx = ctx;
358 rq->mq_hctx = hctx;
359 rq->cmd_flags = data->cmd_flags;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200360
Pavel Begunkov12845902021-10-18 21:37:29 +0100361 if (data->flags & BLK_MQ_REQ_PM)
Jens Axboe56f8da62021-10-19 09:32:57 -0600362 data->rq_flags |= RQF_PM;
Pavel Begunkov12845902021-10-18 21:37:29 +0100363 if (blk_queue_io_stat(q))
Jens Axboe56f8da62021-10-19 09:32:57 -0600364 data->rq_flags |= RQF_IO_STAT;
365 rq->rq_flags = data->rq_flags;
Pavel Begunkov12845902021-10-18 21:37:29 +0100366
Jens Axboec7b84d42021-10-19 09:33:00 -0600367 if (!(data->rq_flags & RQF_ELV)) {
Jens Axboe320ae512013-10-24 09:20:05 +0100368 rq->tag = tag;
369 rq->internal_tag = BLK_MQ_NO_TAG;
Jens Axboec7b84d42021-10-19 09:33:00 -0600370 } else {
371 rq->tag = BLK_MQ_NO_TAG;
372 rq->internal_tag = tag;
Jens Axboe320ae512013-10-24 09:20:05 +0100373 }
Jens Axboec7b84d42021-10-19 09:33:00 -0600374 rq->timeout = 0;
Jens Axboe320ae512013-10-24 09:20:05 +0100375
Pavel Begunkov4f266f22021-10-18 21:37:27 +0100376 if (blk_mq_need_time_stamp(rq))
377 rq->start_time_ns = ktime_get_ns();
378 else
379 rq->start_time_ns = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200380 rq->rq_disk = NULL;
381 rq->part = NULL;
Tejun Heo6f816b42019-08-28 15:05:57 -0700382#ifdef CONFIG_BLK_RQ_ALLOC_TIME
383 rq->alloc_time_ns = alloc_time_ns;
384#endif
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700385 rq->io_start_time_ns = 0;
Hou Tao3d244302019-05-21 15:59:03 +0800386 rq->stats_sectors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200387 rq->nr_phys_segments = 0;
388#if defined(CONFIG_BLK_DEV_INTEGRITY)
389 rq->nr_integrity_segments = 0;
390#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200391 rq->end_io = NULL;
392 rq->end_io_data = NULL;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200393
Pavel Begunkov4f266f22021-10-18 21:37:27 +0100394 blk_crypto_rq_set_defaults(rq);
395 INIT_LIST_HEAD(&rq->queuelist);
396 /* tag was already set */
397 WRITE_ONCE(rq->deadline, 0);
Keith Busch12f5b932018-05-29 15:52:28 +0200398 refcount_set(&rq->ref, 1);
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200399
Pavel Begunkov4f266f22021-10-18 21:37:27 +0100400 if (rq->rq_flags & RQF_ELV) {
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200401 struct elevator_queue *e = data->q->elevator;
402
403 rq->elv.icq = NULL;
Pavel Begunkov4f266f22021-10-18 21:37:27 +0100404 INIT_HLIST_NODE(&rq->hash);
405 RB_CLEAR_NODE(&rq->rb_node);
406
407 if (!op_is_flush(data->cmd_flags) &&
408 e->type->ops.prepare_request) {
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200409 if (e->type->icq_cache)
410 blk_mq_sched_assign_ioc(rq);
411
412 e->type->ops.prepare_request(rq);
413 rq->rq_flags |= RQF_ELVPRIV;
414 }
415 }
416
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200417 return rq;
Jens Axboe320ae512013-10-24 09:20:05 +0100418}
419
Jens Axboe349302d2021-10-09 13:10:39 -0600420static inline struct request *
421__blk_mq_alloc_requests_batch(struct blk_mq_alloc_data *data,
422 u64 alloc_time_ns)
423{
424 unsigned int tag, tag_offset;
Jens Axboefe6134f2021-10-19 09:32:58 -0600425 struct blk_mq_tags *tags;
Jens Axboe349302d2021-10-09 13:10:39 -0600426 struct request *rq;
Jens Axboefe6134f2021-10-19 09:32:58 -0600427 unsigned long tag_mask;
Jens Axboe349302d2021-10-09 13:10:39 -0600428 int i, nr = 0;
429
Jens Axboefe6134f2021-10-19 09:32:58 -0600430 tag_mask = blk_mq_get_tags(data, data->nr_tags, &tag_offset);
431 if (unlikely(!tag_mask))
Jens Axboe349302d2021-10-09 13:10:39 -0600432 return NULL;
433
Jens Axboefe6134f2021-10-19 09:32:58 -0600434 tags = blk_mq_tags_from_data(data);
435 for (i = 0; tag_mask; i++) {
436 if (!(tag_mask & (1UL << i)))
Jens Axboe349302d2021-10-09 13:10:39 -0600437 continue;
438 tag = tag_offset + i;
Jens Axboea22c00b2021-11-01 06:56:09 -0600439 prefetch(tags->static_rqs[tag]);
Jens Axboefe6134f2021-10-19 09:32:58 -0600440 tag_mask &= ~(1UL << i);
441 rq = blk_mq_rq_ctx_init(data, tags, tag, alloc_time_ns);
Jens Axboe013a7f92021-10-13 07:58:52 -0600442 rq_list_add(data->cached_rq, rq);
Jens Axboec5fc7b92021-11-03 05:49:07 -0600443 nr++;
Jens Axboe349302d2021-10-09 13:10:39 -0600444 }
Jens Axboec5fc7b92021-11-03 05:49:07 -0600445 /* caller already holds a reference, add for remainder */
446 percpu_ref_get_many(&data->q->q_usage_counter, nr - 1);
Jens Axboe349302d2021-10-09 13:10:39 -0600447 data->nr_tags -= nr;
448
Jens Axboe013a7f92021-10-13 07:58:52 -0600449 return rq_list_pop(data->cached_rq);
Jens Axboe349302d2021-10-09 13:10:39 -0600450}
451
Christoph Hellwigb90cfae2021-10-12 12:40:44 +0200452static struct request *__blk_mq_alloc_requests(struct blk_mq_alloc_data *data)
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200453{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200454 struct request_queue *q = data->q;
Tejun Heo6f816b42019-08-28 15:05:57 -0700455 u64 alloc_time_ns = 0;
Jens Axboe47c122e2021-10-06 06:34:11 -0600456 struct request *rq;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200457 unsigned int tag;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200458
Tejun Heo6f816b42019-08-28 15:05:57 -0700459 /* alloc_time includes depth and tag waits */
460 if (blk_queue_rq_alloc_time(q))
461 alloc_time_ns = ktime_get_ns();
462
Jens Axboef9afca42018-10-29 13:11:38 -0600463 if (data->cmd_flags & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500464 data->flags |= BLK_MQ_REQ_NOWAIT;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200465
Jens Axboe781dd832021-11-02 08:34:09 -0600466 if (q->elevator) {
467 struct elevator_queue *e = q->elevator;
468
469 data->rq_flags |= RQF_ELV;
470
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200471 /*
Lin Feng8d663f32021-04-15 11:39:20 +0800472 * Flush/passthrough requests are special and go directly to the
Jens Axboe17a51192018-05-09 13:28:50 -0600473 * dispatch list. Don't include reserved tags in the
474 * limiting, as it isn't useful.
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200475 */
Jens Axboef9afca42018-10-29 13:11:38 -0600476 if (!op_is_flush(data->cmd_flags) &&
Lin Feng8d663f32021-04-15 11:39:20 +0800477 !blk_op_is_passthrough(data->cmd_flags) &&
Jens Axboef9afca42018-10-29 13:11:38 -0600478 e->type->ops.limit_depth &&
Jens Axboe17a51192018-05-09 13:28:50 -0600479 !(data->flags & BLK_MQ_REQ_RESERVED))
Jens Axboef9afca42018-10-29 13:11:38 -0600480 e->type->ops.limit_depth(data->cmd_flags, data);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200481 }
482
Ming Leibf0beec2020-05-29 15:53:15 +0200483retry:
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200484 data->ctx = blk_mq_get_ctx(q);
485 data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx);
Jens Axboe781dd832021-11-02 08:34:09 -0600486 if (!(data->rq_flags & RQF_ELV))
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200487 blk_mq_tag_busy(data->hctx);
488
Ming Leibf0beec2020-05-29 15:53:15 +0200489 /*
Jens Axboe349302d2021-10-09 13:10:39 -0600490 * Try batched alloc if we want more than 1 tag.
491 */
492 if (data->nr_tags > 1) {
493 rq = __blk_mq_alloc_requests_batch(data, alloc_time_ns);
494 if (rq)
495 return rq;
496 data->nr_tags = 1;
497 }
498
499 /*
Ming Leibf0beec2020-05-29 15:53:15 +0200500 * Waiting allocations only fail because of an inactive hctx. In that
501 * case just retry the hctx assignment and tag allocation as CPU hotplug
502 * should have migrated us to an online CPU by now.
503 */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200504 tag = blk_mq_get_tag(data);
Ming Leibf0beec2020-05-29 15:53:15 +0200505 if (tag == BLK_MQ_NO_TAG) {
506 if (data->flags & BLK_MQ_REQ_NOWAIT)
507 return NULL;
Ming Leibf0beec2020-05-29 15:53:15 +0200508 /*
Jens Axboe349302d2021-10-09 13:10:39 -0600509 * Give up the CPU and sleep for a random short time to
510 * ensure that thread using a realtime scheduling class
511 * are migrated off the CPU, and thus off the hctx that
512 * is going away.
Ming Leibf0beec2020-05-29 15:53:15 +0200513 */
514 msleep(3);
515 goto retry;
516 }
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200517
Jens Axboefe6134f2021-10-19 09:32:58 -0600518 return blk_mq_rq_ctx_init(data, blk_mq_tags_from_data(data), tag,
519 alloc_time_ns);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200520}
521
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700522struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800523 blk_mq_req_flags_t flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100524{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200525 struct blk_mq_alloc_data data = {
526 .q = q,
527 .flags = flags,
528 .cmd_flags = op,
Jens Axboe47c122e2021-10-06 06:34:11 -0600529 .nr_tags = 1,
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200530 };
Jens Axboebd166ef2017-01-17 06:03:22 -0700531 struct request *rq;
Joe Lawrencea492f072014-08-28 08:15:21 -0600532 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100533
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800534 ret = blk_queue_enter(q, flags);
Joe Lawrencea492f072014-08-28 08:15:21 -0600535 if (ret)
536 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100537
Christoph Hellwigb90cfae2021-10-12 12:40:44 +0200538 rq = __blk_mq_alloc_requests(&data);
Jens Axboebd166ef2017-01-17 06:03:22 -0700539 if (!rq)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200540 goto out_queue_exit;
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200541 rq->__data_len = 0;
542 rq->__sector = (sector_t) -1;
543 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100544 return rq;
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200545out_queue_exit:
546 blk_queue_exit(q);
547 return ERR_PTR(-EWOULDBLOCK);
Jens Axboe320ae512013-10-24 09:20:05 +0100548}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600549EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100550
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700551struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800552 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
Ming Lin1f5bd332016-06-13 16:45:21 +0200553{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200554 struct blk_mq_alloc_data data = {
555 .q = q,
556 .flags = flags,
557 .cmd_flags = op,
Jens Axboe47c122e2021-10-06 06:34:11 -0600558 .nr_tags = 1,
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200559 };
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200560 u64 alloc_time_ns = 0;
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800561 unsigned int cpu;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200562 unsigned int tag;
Ming Lin1f5bd332016-06-13 16:45:21 +0200563 int ret;
564
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200565 /* alloc_time includes depth and tag waits */
566 if (blk_queue_rq_alloc_time(q))
567 alloc_time_ns = ktime_get_ns();
568
Ming Lin1f5bd332016-06-13 16:45:21 +0200569 /*
570 * If the tag allocator sleeps we could get an allocation for a
571 * different hardware context. No need to complicate the low level
572 * allocator for this for the rare use case of a command tied to
573 * a specific queue.
574 */
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200575 if (WARN_ON_ONCE(!(flags & (BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED))))
Ming Lin1f5bd332016-06-13 16:45:21 +0200576 return ERR_PTR(-EINVAL);
577
578 if (hctx_idx >= q->nr_hw_queues)
579 return ERR_PTR(-EIO);
580
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800581 ret = blk_queue_enter(q, flags);
Ming Lin1f5bd332016-06-13 16:45:21 +0200582 if (ret)
583 return ERR_PTR(ret);
584
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600585 /*
586 * Check if the hardware context is actually mapped to anything.
587 * If not tell the caller that it should skip this queue.
588 */
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200589 ret = -EXDEV;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200590 data.hctx = q->queue_hw_ctx[hctx_idx];
591 if (!blk_mq_hw_queue_mapped(data.hctx))
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200592 goto out_queue_exit;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200593 cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
594 data.ctx = __blk_mq_get_ctx(q, cpu);
Ming Lin1f5bd332016-06-13 16:45:21 +0200595
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200596 if (!q->elevator)
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200597 blk_mq_tag_busy(data.hctx);
Jens Axboe781dd832021-11-02 08:34:09 -0600598 else
599 data.rq_flags |= RQF_ELV;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200600
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200601 ret = -EWOULDBLOCK;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200602 tag = blk_mq_get_tag(&data);
603 if (tag == BLK_MQ_NO_TAG)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200604 goto out_queue_exit;
Jens Axboefe6134f2021-10-19 09:32:58 -0600605 return blk_mq_rq_ctx_init(&data, blk_mq_tags_from_data(&data), tag,
606 alloc_time_ns);
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200607
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200608out_queue_exit:
609 blk_queue_exit(q);
610 return ERR_PTR(ret);
Ming Lin1f5bd332016-06-13 16:45:21 +0200611}
612EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
613
Keith Busch12f5b932018-05-29 15:52:28 +0200614static void __blk_mq_free_request(struct request *rq)
615{
616 struct request_queue *q = rq->q;
617 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600618 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Keith Busch12f5b932018-05-29 15:52:28 +0200619 const int sched_tag = rq->internal_tag;
620
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000621 blk_crypto_free_request(rq);
Bart Van Assche986d4132018-09-26 14:01:10 -0700622 blk_pm_mark_last_busy(rq);
Jens Axboeea4f9952018-10-29 15:06:13 -0600623 rq->mq_hctx = NULL;
Christoph Hellwig766473682020-05-29 15:53:12 +0200624 if (rq->tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800625 blk_mq_put_tag(hctx->tags, ctx, rq->tag);
Christoph Hellwig766473682020-05-29 15:53:12 +0200626 if (sched_tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800627 blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
Keith Busch12f5b932018-05-29 15:52:28 +0200628 blk_mq_sched_restart(hctx);
629 blk_queue_exit(q);
630}
631
Christoph Hellwig6af54052017-06-16 18:15:22 +0200632void blk_mq_free_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100633{
Jens Axboe320ae512013-10-24 09:20:05 +0100634 struct request_queue *q = rq->q;
Jens Axboeea4f9952018-10-29 15:06:13 -0600635 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100636
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200637 if (rq->rq_flags & RQF_ELVPRIV) {
Jens Axboe2ff06822021-10-15 09:44:38 -0600638 struct elevator_queue *e = q->elevator;
639
640 if (e->type->ops.finish_request)
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600641 e->type->ops.finish_request(rq);
Christoph Hellwig6af54052017-06-16 18:15:22 +0200642 if (rq->elv.icq) {
643 put_io_context(rq->elv.icq->ioc);
644 rq->elv.icq = NULL;
645 }
646 }
647
Christoph Hellwige8064022016-10-20 15:12:13 +0200648 if (rq->rq_flags & RQF_MQ_INFLIGHT)
John Garrybccf5e22020-08-19 23:20:26 +0800649 __blk_mq_dec_active_requests(hctx);
Jens Axboe87760e52016-11-09 12:38:14 -0700650
Jens Axboe7beb2f82017-09-30 02:08:24 -0600651 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
Christoph Hellwigd152c682021-08-16 15:46:24 +0200652 laptop_io_completion(q->disk->bdi);
Jens Axboe7beb2f82017-09-30 02:08:24 -0600653
Josef Bacika7905042018-07-03 09:32:35 -0600654 rq_qos_done(q, rq);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600655
Keith Busch12f5b932018-05-29 15:52:28 +0200656 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
657 if (refcount_dec_and_test(&rq->ref))
658 __blk_mq_free_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100659}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700660EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100661
Jens Axboe47c122e2021-10-06 06:34:11 -0600662void blk_mq_free_plug_rqs(struct blk_plug *plug)
Jens Axboe320ae512013-10-24 09:20:05 +0100663{
Jens Axboe013a7f92021-10-13 07:58:52 -0600664 struct request *rq;
Jens Axboefe1f4522018-11-28 10:50:07 -0700665
Jens Axboec5fc7b92021-11-03 05:49:07 -0600666 while ((rq = rq_list_pop(&plug->cached_rq)) != NULL)
Jens Axboe47c122e2021-10-06 06:34:11 -0600667 blk_mq_free_request(rq);
Jens Axboe47c122e2021-10-06 06:34:11 -0600668}
Omar Sandoval522a7772018-05-09 02:08:53 -0700669
Jens Axboe9be3e062021-10-14 09:17:01 -0600670static void req_bio_endio(struct request *rq, struct bio *bio,
671 unsigned int nbytes, blk_status_t error)
672{
Pavel Begunkov478eb722021-10-19 22:24:12 +0100673 if (unlikely(error)) {
Jens Axboe9be3e062021-10-14 09:17:01 -0600674 bio->bi_status = error;
Pavel Begunkov478eb722021-10-19 22:24:12 +0100675 } else if (req_op(rq) == REQ_OP_ZONE_APPEND) {
Jens Axboe9be3e062021-10-14 09:17:01 -0600676 /*
677 * Partial zone append completions cannot be supported as the
678 * BIO fragments may end up not being written sequentially.
679 */
Pavel Begunkov297db732021-10-22 16:01:44 +0100680 if (bio->bi_iter.bi_size != nbytes)
Jens Axboe9be3e062021-10-14 09:17:01 -0600681 bio->bi_status = BLK_STS_IOERR;
682 else
683 bio->bi_iter.bi_sector = rq->__sector;
684 }
685
Pavel Begunkov478eb722021-10-19 22:24:12 +0100686 bio_advance(bio, nbytes);
687
688 if (unlikely(rq->rq_flags & RQF_QUIET))
689 bio_set_flag(bio, BIO_QUIET);
Jens Axboe9be3e062021-10-14 09:17:01 -0600690 /* don't actually finish bio if it's part of flush sequence */
691 if (bio->bi_iter.bi_size == 0 && !(rq->rq_flags & RQF_FLUSH_SEQ))
692 bio_endio(bio);
693}
694
695static void blk_account_io_completion(struct request *req, unsigned int bytes)
696{
697 if (req->part && blk_do_io_stat(req)) {
698 const int sgrp = op_stat_group(req_op(req));
699
700 part_stat_lock();
701 part_stat_add(req->part, sectors[sgrp], bytes >> 9);
702 part_stat_unlock();
703 }
704}
705
706/**
707 * blk_update_request - Complete multiple bytes without completing the request
708 * @req: the request being processed
709 * @error: block status code
710 * @nr_bytes: number of bytes to complete for @req
711 *
712 * Description:
713 * Ends I/O on a number of bytes attached to @req, but doesn't complete
714 * the request structure even if @req doesn't have leftover.
715 * If @req has leftover, sets it up for the next range of segments.
716 *
717 * Passing the result of blk_rq_bytes() as @nr_bytes guarantees
718 * %false return from this function.
719 *
720 * Note:
721 * The RQF_SPECIAL_PAYLOAD flag is ignored on purpose in this function
722 * except in the consistency check at the end of this function.
723 *
724 * Return:
725 * %false - this request doesn't have any more data
726 * %true - this request has more data
727 **/
728bool blk_update_request(struct request *req, blk_status_t error,
729 unsigned int nr_bytes)
730{
731 int total_bytes;
732
Christoph Hellwig8a7d2672021-10-18 10:45:18 +0200733 trace_block_rq_complete(req, error, nr_bytes);
Jens Axboe9be3e062021-10-14 09:17:01 -0600734
735 if (!req->bio)
736 return false;
737
738#ifdef CONFIG_BLK_DEV_INTEGRITY
739 if (blk_integrity_rq(req) && req_op(req) == REQ_OP_READ &&
740 error == BLK_STS_OK)
741 req->q->integrity.profile->complete_fn(req, nr_bytes);
742#endif
743
744 if (unlikely(error && !blk_rq_is_passthrough(req) &&
745 !(req->rq_flags & RQF_QUIET)))
746 blk_print_req_error(req, error);
747
748 blk_account_io_completion(req, nr_bytes);
749
750 total_bytes = 0;
751 while (req->bio) {
752 struct bio *bio = req->bio;
753 unsigned bio_bytes = min(bio->bi_iter.bi_size, nr_bytes);
754
755 if (bio_bytes == bio->bi_iter.bi_size)
756 req->bio = bio->bi_next;
757
758 /* Completion has already been traced */
759 bio_clear_flag(bio, BIO_TRACE_COMPLETION);
760 req_bio_endio(req, bio, bio_bytes, error);
761
762 total_bytes += bio_bytes;
763 nr_bytes -= bio_bytes;
764
765 if (!nr_bytes)
766 break;
767 }
768
769 /*
770 * completely done
771 */
772 if (!req->bio) {
773 /*
774 * Reset counters so that the request stacking driver
775 * can find how many bytes remain in the request
776 * later.
777 */
778 req->__data_len = 0;
779 return false;
780 }
781
782 req->__data_len -= total_bytes;
783
784 /* update sector only for requests with clear definition of sector */
785 if (!blk_rq_is_passthrough(req))
786 req->__sector += total_bytes >> 9;
787
788 /* mixed attributes always follow the first bio */
789 if (req->rq_flags & RQF_MIXED_MERGE) {
790 req->cmd_flags &= ~REQ_FAILFAST_MASK;
791 req->cmd_flags |= req->bio->bi_opf & REQ_FAILFAST_MASK;
792 }
793
794 if (!(req->rq_flags & RQF_SPECIAL_PAYLOAD)) {
795 /*
796 * If total number of sectors is less than the first segment
797 * size, something has gone terribly wrong.
798 */
799 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
800 blk_dump_rq_flags(req, "request botched");
801 req->__data_len = blk_rq_cur_bytes(req);
802 }
803
804 /* recalculate the number of segments */
805 req->nr_phys_segments = blk_recalc_rq_segments(req);
806 }
807
808 return true;
809}
810EXPORT_SYMBOL_GPL(blk_update_request);
811
Jens Axboef794f332021-10-08 05:50:46 -0600812static inline void __blk_mq_end_request_acct(struct request *rq, u64 now)
813{
Omar Sandoval4bc63392018-05-09 02:08:52 -0700814 if (rq->rq_flags & RQF_STATS) {
815 blk_mq_poll_stats_start(rq->q);
Omar Sandoval522a7772018-05-09 02:08:53 -0700816 blk_stat_add(rq, now);
Omar Sandoval4bc63392018-05-09 02:08:52 -0700817 }
818
Baolin Wang87890092020-07-04 15:28:21 +0800819 blk_mq_sched_completed_request(rq, now);
Omar Sandoval522a7772018-05-09 02:08:53 -0700820 blk_account_io_done(rq, now);
Jens Axboef794f332021-10-08 05:50:46 -0600821}
822
Ming Lei0d11e6a2013-12-05 10:50:39 -0700823inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig91b63632014-04-16 09:44:53 +0200824{
Jens Axboef794f332021-10-08 05:50:46 -0600825 if (blk_mq_need_time_stamp(rq))
826 __blk_mq_end_request_acct(rq, ktime_get_ns());
Jens Axboe320ae512013-10-24 09:20:05 +0100827
828 if (rq->end_io) {
Josef Bacika7905042018-07-03 09:32:35 -0600829 rq_qos_done(rq->q, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100830 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200831 } else {
Jens Axboe320ae512013-10-24 09:20:05 +0100832 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200833 }
Jens Axboe320ae512013-10-24 09:20:05 +0100834}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700835EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200836
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200837void blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200838{
839 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
840 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700841 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200842}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700843EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100844
Jens Axboef794f332021-10-08 05:50:46 -0600845#define TAG_COMP_BATCH 32
846
847static inline void blk_mq_flush_tag_batch(struct blk_mq_hw_ctx *hctx,
848 int *tag_array, int nr_tags)
849{
850 struct request_queue *q = hctx->queue;
851
Ming Lei3b87c6e2021-11-02 23:36:19 +0800852 /*
853 * All requests should have been marked as RQF_MQ_INFLIGHT, so
854 * update hctx->nr_active in batch
855 */
856 if (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
857 __blk_mq_sub_active_requests(hctx, nr_tags);
858
Jens Axboef794f332021-10-08 05:50:46 -0600859 blk_mq_put_tags(hctx->tags, tag_array, nr_tags);
860 percpu_ref_put_many(&q->q_usage_counter, nr_tags);
861}
862
863void blk_mq_end_request_batch(struct io_comp_batch *iob)
864{
865 int tags[TAG_COMP_BATCH], nr_tags = 0;
Jens Axboe02f7eab2021-10-28 12:08:34 -0600866 struct blk_mq_hw_ctx *cur_hctx = NULL;
Jens Axboef794f332021-10-08 05:50:46 -0600867 struct request *rq;
868 u64 now = 0;
869
870 if (iob->need_ts)
871 now = ktime_get_ns();
872
873 while ((rq = rq_list_pop(&iob->req_list)) != NULL) {
874 prefetch(rq->bio);
875 prefetch(rq->rq_next);
876
877 blk_update_request(rq, BLK_STS_OK, blk_rq_bytes(rq));
878 if (iob->need_ts)
879 __blk_mq_end_request_acct(rq, now);
880
Jens Axboe98b26a02021-11-26 09:53:23 -0700881 rq_qos_done(rq->q, rq);
882
Jens Axboef794f332021-10-08 05:50:46 -0600883 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
884 if (!refcount_dec_and_test(&rq->ref))
885 continue;
886
887 blk_crypto_free_request(rq);
888 blk_pm_mark_last_busy(rq);
Jens Axboef794f332021-10-08 05:50:46 -0600889
Jens Axboe02f7eab2021-10-28 12:08:34 -0600890 if (nr_tags == TAG_COMP_BATCH || cur_hctx != rq->mq_hctx) {
891 if (cur_hctx)
892 blk_mq_flush_tag_batch(cur_hctx, tags, nr_tags);
Jens Axboef794f332021-10-08 05:50:46 -0600893 nr_tags = 0;
Jens Axboe02f7eab2021-10-28 12:08:34 -0600894 cur_hctx = rq->mq_hctx;
Jens Axboef794f332021-10-08 05:50:46 -0600895 }
896 tags[nr_tags++] = rq->tag;
Jens Axboef794f332021-10-08 05:50:46 -0600897 }
898
899 if (nr_tags)
Jens Axboe02f7eab2021-10-28 12:08:34 -0600900 blk_mq_flush_tag_batch(cur_hctx, tags, nr_tags);
Jens Axboef794f332021-10-08 05:50:46 -0600901}
902EXPORT_SYMBOL_GPL(blk_mq_end_request_batch);
903
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100904static void blk_complete_reqs(struct llist_head *list)
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200905{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100906 struct llist_node *entry = llist_reverse_order(llist_del_all(list));
907 struct request *rq, *next;
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200908
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100909 llist_for_each_entry_safe(rq, next, entry, ipi_list)
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200910 rq->q->mq_ops->complete(rq);
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200911}
912
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100913static __latent_entropy void blk_done_softirq(struct softirq_action *h)
Christoph Hellwig115243f2020-06-11 08:44:42 +0200914{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100915 blk_complete_reqs(this_cpu_ptr(&blk_cpu_done));
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200916}
917
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200918static int blk_softirq_cpu_dead(unsigned int cpu)
919{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100920 blk_complete_reqs(&per_cpu(blk_cpu_done, cpu));
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200921 return 0;
922}
923
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800924static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100925{
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100926 __raise_softirq_irqoff(BLOCK_SOFTIRQ);
Jens Axboe320ae512013-10-24 09:20:05 +0100927}
928
Christoph Hellwig963395262020-06-11 08:44:49 +0200929static inline bool blk_mq_complete_need_ipi(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100930{
Christoph Hellwig963395262020-06-11 08:44:49 +0200931 int cpu = raw_smp_processor_id();
Jens Axboe320ae512013-10-24 09:20:05 +0100932
Christoph Hellwig963395262020-06-11 08:44:49 +0200933 if (!IS_ENABLED(CONFIG_SMP) ||
934 !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags))
935 return false;
Sebastian Andrzej Siewior71425182020-12-04 20:13:54 +0100936 /*
937 * With force threaded interrupts enabled, raising softirq from an SMP
938 * function call will always result in waking the ksoftirqd thread.
939 * This is probably worse than completing the request on a different
940 * cache domain.
941 */
Tanner Love91cc4702021-06-02 14:03:38 -0400942 if (force_irqthreads())
Sebastian Andrzej Siewior71425182020-12-04 20:13:54 +0100943 return false;
Christoph Hellwig963395262020-06-11 08:44:49 +0200944
945 /* same CPU or cache domain? Complete locally */
946 if (cpu == rq->mq_ctx->cpu ||
947 (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) &&
948 cpus_share_cache(cpu, rq->mq_ctx->cpu)))
949 return false;
950
951 /* don't try to IPI to an offline CPU */
952 return cpu_online(rq->mq_ctx->cpu);
953}
954
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100955static void blk_mq_complete_send_ipi(struct request *rq)
956{
957 struct llist_head *list;
958 unsigned int cpu;
959
960 cpu = rq->mq_ctx->cpu;
961 list = &per_cpu(blk_cpu_done, cpu);
962 if (llist_add(&rq->ipi_list, list)) {
963 INIT_CSD(&rq->csd, __blk_mq_complete_request_remote, rq);
964 smp_call_function_single_async(cpu, &rq->csd);
965 }
966}
967
968static void blk_mq_raise_softirq(struct request *rq)
969{
970 struct llist_head *list;
971
972 preempt_disable();
973 list = this_cpu_ptr(&blk_cpu_done);
974 if (llist_add(&rq->ipi_list, list))
975 raise_softirq(BLOCK_SOFTIRQ);
976 preempt_enable();
977}
978
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200979bool blk_mq_complete_request_remote(struct request *rq)
980{
Keith Buschaf78ff72018-11-26 09:54:30 -0700981 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
Ming Lei36e76532018-09-28 16:42:20 +0800982
Jens Axboe4ab32bf2018-11-18 16:15:35 -0700983 /*
984 * For a polled request, always complete locallly, it's pointless
985 * to redirect the completion.
986 */
Christoph Hellwig6ce913f2021-10-12 13:12:21 +0200987 if (rq->cmd_flags & REQ_POLLED)
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200988 return false;
Jens Axboe320ae512013-10-24 09:20:05 +0100989
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200990 if (blk_mq_complete_need_ipi(rq)) {
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100991 blk_mq_complete_send_ipi(rq);
992 return true;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800993 }
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200994
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +0100995 if (rq->q->nr_hw_queues == 1) {
996 blk_mq_raise_softirq(rq);
997 return true;
998 }
999 return false;
Jens Axboe320ae512013-10-24 09:20:05 +01001000}
Christoph Hellwig40d09b52020-06-11 08:44:50 +02001001EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote);
1002
Jens Axboe320ae512013-10-24 09:20:05 +01001003/**
Christoph Hellwig15f73f52020-06-11 08:44:47 +02001004 * blk_mq_complete_request - end I/O on a request
1005 * @rq: the request being processed
Jens Axboe320ae512013-10-24 09:20:05 +01001006 *
Christoph Hellwig15f73f52020-06-11 08:44:47 +02001007 * Description:
1008 * Complete a request by scheduling the ->complete_rq operation.
1009 **/
1010void blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001011{
Christoph Hellwig40d09b52020-06-11 08:44:50 +02001012 if (!blk_mq_complete_request_remote(rq))
Christoph Hellwig963395262020-06-11 08:44:49 +02001013 rq->q->mq_ops->complete(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001014}
Christoph Hellwig15f73f52020-06-11 08:44:47 +02001015EXPORT_SYMBOL(blk_mq_complete_request);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001016
Jens Axboe04ced152018-01-09 08:29:46 -08001017static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -08001018 __releases(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -08001019{
1020 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
1021 rcu_read_unlock();
1022 else
Tejun Heo05707b62018-01-09 08:29:53 -08001023 srcu_read_unlock(hctx->srcu, srcu_idx);
Jens Axboe04ced152018-01-09 08:29:46 -08001024}
1025
1026static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -08001027 __acquires(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -08001028{
Jens Axboe08b5a6e2018-01-09 09:32:25 -07001029 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
1030 /* shut up gcc false positive */
1031 *srcu_idx = 0;
Jens Axboe04ced152018-01-09 08:29:46 -08001032 rcu_read_lock();
Jens Axboe08b5a6e2018-01-09 09:32:25 -07001033 } else
Tejun Heo05707b62018-01-09 08:29:53 -08001034 *srcu_idx = srcu_read_lock(hctx->srcu);
Jens Axboe04ced152018-01-09 08:29:46 -08001035}
1036
Christoph Hellwig30a91cb2014-02-10 03:24:38 -08001037/**
André Almeida105663f2020-01-06 15:08:18 -03001038 * blk_mq_start_request - Start processing a request
1039 * @rq: Pointer to request to be started
1040 *
1041 * Function used by device drivers to notify the block layer that a request
1042 * is going to be processed now, so blk layer can do proper initializations
1043 * such as starting the timeout timer.
1044 */
Christoph Hellwige2490072014-09-13 16:40:09 -07001045void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001046{
1047 struct request_queue *q = rq->q;
1048
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001049 trace_block_rq_issue(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001050
Jens Axboecf43e6b2016-11-07 21:32:37 -07001051 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
Jens Axboe00067072021-10-05 09:23:59 -06001052 u64 start_time;
1053#ifdef CONFIG_BLK_CGROUP
1054 if (rq->bio)
1055 start_time = bio_issue_time(&rq->bio->bi_issue);
1056 else
1057#endif
1058 start_time = ktime_get_ns();
1059 rq->io_start_time_ns = start_time;
Hou Tao3d244302019-05-21 15:59:03 +08001060 rq->stats_sectors = blk_rq_sectors(rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -07001061 rq->rq_flags |= RQF_STATS;
Josef Bacika7905042018-07-03 09:32:35 -06001062 rq_qos_issue(q, rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -07001063 }
1064
Tejun Heo1d9bd512018-01-09 08:29:48 -08001065 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
Jens Axboe538b7532014-09-16 10:37:37 -06001066
Tejun Heo1d9bd512018-01-09 08:29:48 -08001067 blk_add_timer(rq);
Keith Busch12f5b932018-05-29 15:52:28 +02001068 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -08001069
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +03001070#ifdef CONFIG_BLK_DEV_INTEGRITY
1071 if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
1072 q->integrity.profile->prepare_fn(rq);
1073#endif
Christoph Hellwig3e087732021-10-12 13:12:24 +02001074 if (rq->bio && rq->bio->bi_opf & REQ_POLLED)
1075 WRITE_ONCE(rq->bio->bi_cookie, blk_rq_to_qc(rq));
Jens Axboe320ae512013-10-24 09:20:05 +01001076}
Christoph Hellwige2490072014-09-13 16:40:09 -07001077EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +01001078
Christoph Hellwig4054cff2021-11-17 07:13:56 +01001079/**
1080 * blk_end_sync_rq - executes a completion event on a request
1081 * @rq: request to complete
1082 * @error: end I/O status of the request
1083 */
1084static void blk_end_sync_rq(struct request *rq, blk_status_t error)
1085{
1086 struct completion *waiting = rq->end_io_data;
1087
1088 rq->end_io_data = (void *)(uintptr_t)error;
1089
1090 /*
1091 * complete last, if this is a stack request the process (and thus
1092 * the rq pointer) could be invalid right after this complete()
1093 */
1094 complete(waiting);
1095}
1096
1097/**
1098 * blk_execute_rq_nowait - insert a request to I/O scheduler for execution
1099 * @bd_disk: matching gendisk
1100 * @rq: request to insert
1101 * @at_head: insert request at head or tail of queue
1102 * @done: I/O completion handler
1103 *
1104 * Description:
1105 * Insert a fully prepared request at the back of the I/O scheduler queue
1106 * for execution. Don't wait for completion.
1107 *
1108 * Note:
1109 * This function will invoke @done directly if the queue is dead.
1110 */
1111void blk_execute_rq_nowait(struct gendisk *bd_disk, struct request *rq,
1112 int at_head, rq_end_io_fn *done)
1113{
1114 WARN_ON(irqs_disabled());
1115 WARN_ON(!blk_rq_is_passthrough(rq));
1116
1117 rq->rq_disk = bd_disk;
1118 rq->end_io = done;
1119
1120 blk_account_io_start(rq);
1121
1122 /*
1123 * don't check dying flag for MQ because the request won't
1124 * be reused after dying flag is set
1125 */
1126 blk_mq_sched_insert_request(rq, at_head, true, false);
1127}
1128EXPORT_SYMBOL_GPL(blk_execute_rq_nowait);
1129
1130static bool blk_rq_is_poll(struct request *rq)
1131{
1132 if (!rq->mq_hctx)
1133 return false;
1134 if (rq->mq_hctx->type != HCTX_TYPE_POLL)
1135 return false;
1136 if (WARN_ON_ONCE(!rq->bio))
1137 return false;
1138 return true;
1139}
1140
1141static void blk_rq_poll_completion(struct request *rq, struct completion *wait)
1142{
1143 do {
1144 bio_poll(rq->bio, NULL, 0);
1145 cond_resched();
1146 } while (!completion_done(wait));
1147}
1148
1149/**
1150 * blk_execute_rq - insert a request into queue for execution
1151 * @bd_disk: matching gendisk
1152 * @rq: request to insert
1153 * @at_head: insert request at head or tail of queue
1154 *
1155 * Description:
1156 * Insert a fully prepared request at the back of the I/O scheduler queue
1157 * for execution and wait for completion.
1158 * Return: The blk_status_t result provided to blk_mq_end_request().
1159 */
1160blk_status_t blk_execute_rq(struct gendisk *bd_disk, struct request *rq,
1161 int at_head)
1162{
1163 DECLARE_COMPLETION_ONSTACK(wait);
1164 unsigned long hang_check;
1165
1166 rq->end_io_data = &wait;
1167 blk_execute_rq_nowait(bd_disk, rq, at_head, blk_end_sync_rq);
1168
1169 /* Prevent hang_check timer from firing at us during very long I/O */
1170 hang_check = sysctl_hung_task_timeout_secs;
1171
1172 if (blk_rq_is_poll(rq))
1173 blk_rq_poll_completion(rq, &wait);
1174 else if (hang_check)
1175 while (!wait_for_completion_io_timeout(&wait,
1176 hang_check * (HZ/2)))
1177 ;
1178 else
1179 wait_for_completion_io(&wait);
1180
1181 return (blk_status_t)(uintptr_t)rq->end_io_data;
1182}
1183EXPORT_SYMBOL(blk_execute_rq);
1184
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001185static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +01001186{
1187 struct request_queue *q = rq->q;
1188
Ming Lei923218f2017-11-02 23:24:38 +08001189 blk_mq_put_driver_tag(rq);
1190
Christoph Hellwiga54895f2020-12-03 17:21:39 +01001191 trace_block_rq_requeue(rq);
Josef Bacika7905042018-07-03 09:32:35 -06001192 rq_qos_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -08001193
Keith Busch12f5b932018-05-29 15:52:28 +02001194 if (blk_mq_request_started(rq)) {
1195 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Christoph Hellwigda661262018-06-14 13:58:45 +02001196 rq->rq_flags &= ~RQF_TIMED_OUT;
Christoph Hellwige2490072014-09-13 16:40:09 -07001197 }
Jens Axboe320ae512013-10-24 09:20:05 +01001198}
1199
Bart Van Assche2b053ac2016-10-28 17:21:41 -07001200void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001201{
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001202 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001203
Ming Lei105976f2018-02-23 23:36:56 +08001204 /* this request will be re-inserted to io scheduler queue */
1205 blk_mq_sched_requeue_request(rq);
1206
Bart Van Assche2b053ac2016-10-28 17:21:41 -07001207 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +02001208}
1209EXPORT_SYMBOL(blk_mq_requeue_request);
1210
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001211static void blk_mq_requeue_work(struct work_struct *work)
1212{
1213 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -04001214 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001215 LIST_HEAD(rq_list);
1216 struct request *rq, *next;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001217
Jens Axboe18e97812017-07-27 08:03:57 -06001218 spin_lock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001219 list_splice_init(&q->requeue_list, &rq_list);
Jens Axboe18e97812017-07-27 08:03:57 -06001220 spin_unlock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001221
1222 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
Jianchao Wangaef18972019-02-12 09:56:25 +08001223 if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP)))
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001224 continue;
1225
Christoph Hellwige8064022016-10-20 15:12:13 +02001226 rq->rq_flags &= ~RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001227 list_del_init(&rq->queuelist);
Jianchao Wangaef18972019-02-12 09:56:25 +08001228 /*
1229 * If RQF_DONTPREP, rq has contained some driver specific
1230 * data, so insert it to hctx dispatch list to avoid any
1231 * merge.
1232 */
1233 if (rq->rq_flags & RQF_DONTPREP)
Ming Lei01e99ae2020-02-25 09:04:32 +08001234 blk_mq_request_bypass_insert(rq, false, false);
Jianchao Wangaef18972019-02-12 09:56:25 +08001235 else
1236 blk_mq_sched_insert_request(rq, true, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001237 }
1238
1239 while (!list_empty(&rq_list)) {
1240 rq = list_entry(rq_list.next, struct request, queuelist);
1241 list_del_init(&rq->queuelist);
Mike Snitzer9e97d292018-01-17 11:25:58 -05001242 blk_mq_sched_insert_request(rq, false, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001243 }
1244
Bart Van Assche52d7f1b2016-10-28 17:20:32 -07001245 blk_mq_run_hw_queues(q, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001246}
1247
Bart Van Assche2b053ac2016-10-28 17:21:41 -07001248void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
1249 bool kick_requeue_list)
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001250{
1251 struct request_queue *q = rq->q;
1252 unsigned long flags;
1253
1254 /*
1255 * We abuse this flag that is otherwise used by the I/O scheduler to
Jens Axboeff821d22017-11-10 22:05:12 -07001256 * request head insertion from the workqueue.
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001257 */
Christoph Hellwige8064022016-10-20 15:12:13 +02001258 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001259
1260 spin_lock_irqsave(&q->requeue_lock, flags);
1261 if (at_head) {
Christoph Hellwige8064022016-10-20 15:12:13 +02001262 rq->rq_flags |= RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001263 list_add(&rq->queuelist, &q->requeue_list);
1264 } else {
1265 list_add_tail(&rq->queuelist, &q->requeue_list);
1266 }
1267 spin_unlock_irqrestore(&q->requeue_lock, flags);
Bart Van Assche2b053ac2016-10-28 17:21:41 -07001268
1269 if (kick_requeue_list)
1270 blk_mq_kick_requeue_list(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001271}
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001272
1273void blk_mq_kick_requeue_list(struct request_queue *q)
1274{
Bart Van Asscheae943d22018-01-19 08:58:55 -08001275 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06001276}
1277EXPORT_SYMBOL(blk_mq_kick_requeue_list);
1278
Mike Snitzer28494502016-09-14 13:28:30 -04001279void blk_mq_delay_kick_requeue_list(struct request_queue *q,
1280 unsigned long msecs)
1281{
Bart Van Assched4acf362017-08-09 11:28:06 -07001282 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
1283 msecs_to_jiffies(msecs));
Mike Snitzer28494502016-09-14 13:28:30 -04001284}
1285EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
1286
Jens Axboe3c94d832018-12-17 21:11:17 -07001287static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq,
1288 void *priv, bool reserved)
Jens Axboeae879912018-11-08 09:03:51 -07001289{
1290 /*
Ming Lei05a4fed2020-07-07 11:04:33 -04001291 * If we find a request that isn't idle and the queue matches,
Jens Axboe3c94d832018-12-17 21:11:17 -07001292 * we know the queue is busy. Return false to stop the iteration.
Jens Axboeae879912018-11-08 09:03:51 -07001293 */
Ming Lei05a4fed2020-07-07 11:04:33 -04001294 if (blk_mq_request_started(rq) && rq->q == hctx->queue) {
Jens Axboeae879912018-11-08 09:03:51 -07001295 bool *busy = priv;
1296
1297 *busy = true;
1298 return false;
1299 }
1300
1301 return true;
1302}
1303
Jens Axboe3c94d832018-12-17 21:11:17 -07001304bool blk_mq_queue_inflight(struct request_queue *q)
Jens Axboeae879912018-11-08 09:03:51 -07001305{
1306 bool busy = false;
1307
Jens Axboe3c94d832018-12-17 21:11:17 -07001308 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
Jens Axboeae879912018-11-08 09:03:51 -07001309 return busy;
1310}
Jens Axboe3c94d832018-12-17 21:11:17 -07001311EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
Jens Axboeae879912018-11-08 09:03:51 -07001312
Tejun Heo358f70d2018-01-09 08:29:50 -08001313static void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +01001314{
Christoph Hellwigda661262018-06-14 13:58:45 +02001315 req->rq_flags |= RQF_TIMED_OUT;
Christoph Hellwigd1210d52018-05-29 15:52:39 +02001316 if (req->q->mq_ops->timeout) {
1317 enum blk_eh_timer_return ret;
Jens Axboe87ee7b12014-04-24 08:51:47 -06001318
Christoph Hellwigd1210d52018-05-29 15:52:39 +02001319 ret = req->q->mq_ops->timeout(req, reserved);
1320 if (ret == BLK_EH_DONE)
1321 return;
1322 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
Christoph Hellwig46f92d42014-09-13 16:40:12 -07001323 }
Christoph Hellwigd1210d52018-05-29 15:52:39 +02001324
1325 blk_add_timer(req);
Jens Axboe87ee7b12014-04-24 08:51:47 -06001326}
Keith Busch5b3f25f2015-01-07 18:55:46 -07001327
Keith Busch12f5b932018-05-29 15:52:28 +02001328static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
1329{
1330 unsigned long deadline;
1331
1332 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
1333 return false;
Christoph Hellwigda661262018-06-14 13:58:45 +02001334 if (rq->rq_flags & RQF_TIMED_OUT)
1335 return false;
Keith Busch12f5b932018-05-29 15:52:28 +02001336
Christoph Hellwig079076b2018-11-14 17:02:05 +01001337 deadline = READ_ONCE(rq->deadline);
Keith Busch12f5b932018-05-29 15:52:28 +02001338 if (time_after_eq(jiffies, deadline))
1339 return true;
1340
1341 if (*next == 0)
1342 *next = deadline;
1343 else if (time_after(*next, deadline))
1344 *next = deadline;
1345 return false;
1346}
1347
Ming Lei2e315dc2021-05-11 23:22:34 +08001348void blk_mq_put_rq_ref(struct request *rq)
1349{
Ming Leia9ed27a2021-08-18 09:09:25 +08001350 if (is_flush_rq(rq))
Ming Lei2e315dc2021-05-11 23:22:34 +08001351 rq->end_io(rq, 0);
1352 else if (refcount_dec_and_test(&rq->ref))
1353 __blk_mq_free_request(rq);
1354}
1355
Jens Axboe7baa8572018-11-08 10:24:07 -07001356static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig81481eb2014-09-13 16:40:11 -07001357 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +01001358{
Keith Busch12f5b932018-05-29 15:52:28 +02001359 unsigned long *next = priv;
Christoph Hellwig81481eb2014-09-13 16:40:11 -07001360
Keith Busch12f5b932018-05-29 15:52:28 +02001361 /*
Ming Leic797b402021-08-11 23:52:02 +08001362 * blk_mq_queue_tag_busy_iter() has locked the request, so it cannot
1363 * be reallocated underneath the timeout handler's processing, then
1364 * the expire check is reliable. If the request is not expired, then
1365 * it was completed and reallocated as a new request after returning
1366 * from blk_mq_check_expired().
Keith Busch12f5b932018-05-29 15:52:28 +02001367 */
1368 if (blk_mq_req_expired(rq, next))
Tejun Heo1d9bd512018-01-09 08:29:48 -08001369 blk_mq_rq_timed_out(rq, reserved);
Jens Axboe7baa8572018-11-08 10:24:07 -07001370 return true;
Tejun Heo1d9bd512018-01-09 08:29:48 -08001371}
1372
Christoph Hellwig287922e2015-10-30 20:57:30 +08001373static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001374{
Christoph Hellwig287922e2015-10-30 20:57:30 +08001375 struct request_queue *q =
1376 container_of(work, struct request_queue, timeout_work);
Keith Busch12f5b932018-05-29 15:52:28 +02001377 unsigned long next = 0;
Tejun Heo1d9bd512018-01-09 08:29:48 -08001378 struct blk_mq_hw_ctx *hctx;
Christoph Hellwig81481eb2014-09-13 16:40:11 -07001379 int i;
Jens Axboe320ae512013-10-24 09:20:05 +01001380
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -06001381 /* A deadlock might occur if a request is stuck requiring a
1382 * timeout at the same time a queue freeze is waiting
1383 * completion, since the timeout code would not be able to
1384 * acquire the queue reference here.
1385 *
1386 * That's why we don't use blk_queue_enter here; instead, we use
1387 * percpu_ref_tryget directly, because we need to be able to
1388 * obtain a reference even in the short window between the queue
1389 * starting to freeze, by dropping the first reference in
Ming Lei1671d522017-03-27 20:06:57 +08001390 * blk_freeze_queue_start, and the moment the last request is
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -06001391 * consumed, marked by the instant q_usage_counter reaches
1392 * zero.
1393 */
1394 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +08001395 return;
1396
Keith Busch12f5b932018-05-29 15:52:28 +02001397 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
Jens Axboe320ae512013-10-24 09:20:05 +01001398
Keith Busch12f5b932018-05-29 15:52:28 +02001399 if (next != 0) {
1400 mod_timer(&q->timeout, next);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001401 } else {
Bart Van Asschefcd36c32018-01-10 08:33:33 -08001402 /*
1403 * Request timeouts are handled as a forward rolling timer. If
1404 * we end up here it means that no requests are pending and
1405 * also that no request has been pending for a while. Mark
1406 * each hctx as idle.
1407 */
Ming Leif054b562015-04-21 10:00:19 +08001408 queue_for_each_hw_ctx(q, hctx, i) {
1409 /* the hctx may be unmapped, so check it here */
1410 if (blk_mq_hw_queue_mapped(hctx))
1411 blk_mq_tag_idle(hctx);
1412 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06001413 }
Christoph Hellwig287922e2015-10-30 20:57:30 +08001414 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001415}
1416
Omar Sandoval88459642016-09-17 08:38:44 -06001417struct flush_busy_ctx_data {
1418 struct blk_mq_hw_ctx *hctx;
1419 struct list_head *list;
1420};
1421
1422static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
1423{
1424 struct flush_busy_ctx_data *flush_data = data;
1425 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
1426 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001427 enum hctx_type type = hctx->type;
Omar Sandoval88459642016-09-17 08:38:44 -06001428
Omar Sandoval88459642016-09-17 08:38:44 -06001429 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001430 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
Omar Sandovale9a99a62018-02-27 16:56:42 -08001431 sbitmap_clear_bit(sb, bitnr);
Omar Sandoval88459642016-09-17 08:38:44 -06001432 spin_unlock(&ctx->lock);
1433 return true;
1434}
1435
Jens Axboe320ae512013-10-24 09:20:05 +01001436/*
Jens Axboe1429d7c2014-05-19 09:23:55 -06001437 * Process software queues that have been marked busy, splicing them
1438 * to the for-dispatch
1439 */
Jens Axboe2c3ad662016-12-14 14:34:47 -07001440void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
Jens Axboe1429d7c2014-05-19 09:23:55 -06001441{
Omar Sandoval88459642016-09-17 08:38:44 -06001442 struct flush_busy_ctx_data data = {
1443 .hctx = hctx,
1444 .list = list,
1445 };
Jens Axboe1429d7c2014-05-19 09:23:55 -06001446
Omar Sandoval88459642016-09-17 08:38:44 -06001447 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001448}
Jens Axboe2c3ad662016-12-14 14:34:47 -07001449EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001450
Ming Leib3476892017-10-14 17:22:30 +08001451struct dispatch_rq_data {
1452 struct blk_mq_hw_ctx *hctx;
1453 struct request *rq;
1454};
1455
1456static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
1457 void *data)
1458{
1459 struct dispatch_rq_data *dispatch_data = data;
1460 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
1461 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001462 enum hctx_type type = hctx->type;
Ming Leib3476892017-10-14 17:22:30 +08001463
1464 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001465 if (!list_empty(&ctx->rq_lists[type])) {
1466 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
Ming Leib3476892017-10-14 17:22:30 +08001467 list_del_init(&dispatch_data->rq->queuelist);
Ming Leic16d6b52018-12-17 08:44:05 -07001468 if (list_empty(&ctx->rq_lists[type]))
Ming Leib3476892017-10-14 17:22:30 +08001469 sbitmap_clear_bit(sb, bitnr);
1470 }
1471 spin_unlock(&ctx->lock);
1472
1473 return !dispatch_data->rq;
1474}
1475
1476struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
1477 struct blk_mq_ctx *start)
1478{
Jens Axboef31967f2018-10-29 13:13:29 -06001479 unsigned off = start ? start->index_hw[hctx->type] : 0;
Ming Leib3476892017-10-14 17:22:30 +08001480 struct dispatch_rq_data data = {
1481 .hctx = hctx,
1482 .rq = NULL,
1483 };
1484
1485 __sbitmap_for_each_set(&hctx->ctx_map, off,
1486 dispatch_rq_from_ctx, &data);
1487
1488 return data.rq;
1489}
1490
Jens Axboea808a9d2021-10-13 08:28:14 -06001491static bool __blk_mq_alloc_driver_tag(struct request *rq)
Jens Axboe703fd1c2016-09-16 13:59:14 -06001492{
John Garryae0f1a72021-10-05 18:23:38 +08001493 struct sbitmap_queue *bt = &rq->mq_hctx->tags->bitmap_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001494 unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001495 int tag;
1496
Ming Lei568f2702020-07-06 22:41:11 +08001497 blk_mq_tag_busy(rq->mq_hctx);
1498
Ming Lei570e9b72020-06-30 22:03:55 +08001499 if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) {
John Garryae0f1a72021-10-05 18:23:38 +08001500 bt = &rq->mq_hctx->tags->breserved_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001501 tag_offset = 0;
Ming Lei28500852020-09-11 18:41:14 +08001502 } else {
1503 if (!hctx_may_queue(rq->mq_hctx, bt))
1504 return false;
Ming Lei570e9b72020-06-30 22:03:55 +08001505 }
1506
Ming Lei570e9b72020-06-30 22:03:55 +08001507 tag = __sbitmap_queue_get(bt);
1508 if (tag == BLK_MQ_NO_TAG)
1509 return false;
1510
1511 rq->tag = tag + tag_offset;
Ming Lei570e9b72020-06-30 22:03:55 +08001512 return true;
1513}
1514
Jens Axboea808a9d2021-10-13 08:28:14 -06001515bool __blk_mq_get_driver_tag(struct blk_mq_hw_ctx *hctx, struct request *rq)
Ming Lei570e9b72020-06-30 22:03:55 +08001516{
Jens Axboea808a9d2021-10-13 08:28:14 -06001517 if (rq->tag == BLK_MQ_NO_TAG && !__blk_mq_alloc_driver_tag(rq))
Ming Lei568f2702020-07-06 22:41:11 +08001518 return false;
1519
Ming Lei51db1c32020-08-19 23:20:19 +08001520 if ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) &&
Ming Lei568f2702020-07-06 22:41:11 +08001521 !(rq->rq_flags & RQF_MQ_INFLIGHT)) {
1522 rq->rq_flags |= RQF_MQ_INFLIGHT;
John Garrybccf5e22020-08-19 23:20:26 +08001523 __blk_mq_inc_active_requests(hctx);
Ming Lei568f2702020-07-06 22:41:11 +08001524 }
1525 hctx->tags->rqs[rq->tag] = rq;
1526 return true;
Ming Lei570e9b72020-06-30 22:03:55 +08001527}
1528
Jens Axboeeb619fd2017-11-09 08:32:43 -07001529static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1530 int flags, void *key)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001531{
1532 struct blk_mq_hw_ctx *hctx;
1533
1534 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1535
Ming Lei5815839b2018-06-25 19:31:47 +08001536 spin_lock(&hctx->dispatch_wait_lock);
Jens Axboee8618572019-03-25 12:34:10 -06001537 if (!list_empty(&wait->entry)) {
1538 struct sbitmap_queue *sbq;
1539
1540 list_del_init(&wait->entry);
John Garryae0f1a72021-10-05 18:23:38 +08001541 sbq = &hctx->tags->bitmap_tags;
Jens Axboee8618572019-03-25 12:34:10 -06001542 atomic_dec(&sbq->ws_active);
1543 }
Ming Lei5815839b2018-06-25 19:31:47 +08001544 spin_unlock(&hctx->dispatch_wait_lock);
1545
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001546 blk_mq_run_hw_queue(hctx, true);
1547 return 1;
1548}
1549
Jens Axboef906a6a2017-11-09 16:10:13 -07001550/*
1551 * Mark us waiting for a tag. For shared tags, this involves hooking us into
Bart Van Asscheee3e4de2018-01-09 10:09:15 -08001552 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1553 * restart. For both cases, take care to check the condition again after
Jens Axboef906a6a2017-11-09 16:10:13 -07001554 * marking us as waiting.
1555 */
Ming Lei2278d692018-06-25 19:31:46 +08001556static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
Jens Axboef906a6a2017-11-09 16:10:13 -07001557 struct request *rq)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001558{
John Garryae0f1a72021-10-05 18:23:38 +08001559 struct sbitmap_queue *sbq = &hctx->tags->bitmap_tags;
Ming Lei5815839b2018-06-25 19:31:47 +08001560 struct wait_queue_head *wq;
Jens Axboef906a6a2017-11-09 16:10:13 -07001561 wait_queue_entry_t *wait;
1562 bool ret;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001563
Ming Lei51db1c32020-08-19 23:20:19 +08001564 if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
Yufen Yu684b7322019-03-15 11:05:10 +08001565 blk_mq_sched_mark_restart_hctx(hctx);
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001566
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001567 /*
1568 * It's possible that a tag was freed in the window between the
1569 * allocation failure and adding the hardware queue to the wait
1570 * queue.
1571 *
1572 * Don't clear RESTART here, someone else could have set it.
1573 * At most this will cost an extra queue run.
1574 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001575 return blk_mq_get_driver_tag(rq);
Jens Axboeeb619fd2017-11-09 08:32:43 -07001576 }
1577
Ming Lei2278d692018-06-25 19:31:46 +08001578 wait = &hctx->dispatch_wait;
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001579 if (!list_empty_careful(&wait->entry))
1580 return false;
1581
Jens Axboee8618572019-03-25 12:34:10 -06001582 wq = &bt_wait_ptr(sbq, hctx)->wait;
Ming Lei5815839b2018-06-25 19:31:47 +08001583
1584 spin_lock_irq(&wq->lock);
1585 spin_lock(&hctx->dispatch_wait_lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001586 if (!list_empty(&wait->entry)) {
Ming Lei5815839b2018-06-25 19:31:47 +08001587 spin_unlock(&hctx->dispatch_wait_lock);
1588 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001589 return false;
1590 }
1591
Jens Axboee8618572019-03-25 12:34:10 -06001592 atomic_inc(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001593 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1594 __add_wait_queue(wq, wait);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001595
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001596 /*
Jens Axboeeb619fd2017-11-09 08:32:43 -07001597 * It's possible that a tag was freed in the window between the
1598 * allocation failure and adding the hardware queue to the wait
1599 * queue.
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001600 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001601 ret = blk_mq_get_driver_tag(rq);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001602 if (!ret) {
Ming Lei5815839b2018-06-25 19:31:47 +08001603 spin_unlock(&hctx->dispatch_wait_lock);
1604 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001605 return false;
Jens Axboef906a6a2017-11-09 16:10:13 -07001606 }
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001607
1608 /*
1609 * We got a tag, remove ourselves from the wait queue to ensure
1610 * someone else gets the wakeup.
1611 */
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001612 list_del_init(&wait->entry);
Jens Axboee8618572019-03-25 12:34:10 -06001613 atomic_dec(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001614 spin_unlock(&hctx->dispatch_wait_lock);
1615 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001616
1617 return true;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001618}
1619
Ming Lei6e7687172018-07-03 09:03:16 -06001620#define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1621#define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1622/*
1623 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1624 * - EWMA is one simple way to compute running average value
1625 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1626 * - take 4 as factor for avoiding to get too small(0) result, and this
1627 * factor doesn't matter because EWMA decreases exponentially
1628 */
1629static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1630{
1631 unsigned int ewma;
1632
Ming Lei6e7687172018-07-03 09:03:16 -06001633 ewma = hctx->dispatch_busy;
1634
1635 if (!ewma && !busy)
1636 return;
1637
1638 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1639 if (busy)
1640 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1641 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1642
1643 hctx->dispatch_busy = ewma;
1644}
1645
Ming Lei86ff7c22018-01-30 22:04:57 -05001646#define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1647
Johannes Thumshirnc92a4102020-03-25 00:24:44 +09001648static void blk_mq_handle_dev_resource(struct request *rq,
1649 struct list_head *list)
1650{
1651 struct request *next =
1652 list_first_entry_or_null(list, struct request, queuelist);
1653
1654 /*
1655 * If an I/O scheduler has been configured and we got a driver tag for
1656 * the next request already, free it.
1657 */
1658 if (next)
1659 blk_mq_put_driver_tag(next);
1660
1661 list_add(&rq->queuelist, list);
1662 __blk_mq_requeue_request(rq);
1663}
1664
Keith Busch0512a752020-05-12 17:55:47 +09001665static void blk_mq_handle_zone_resource(struct request *rq,
1666 struct list_head *zone_list)
1667{
1668 /*
1669 * If we end up here it is because we cannot dispatch a request to a
1670 * specific zone due to LLD level zone-write locking or other zone
1671 * related resource not being available. In this case, set the request
1672 * aside in zone_list for retrying it later.
1673 */
1674 list_add(&rq->queuelist, zone_list);
1675 __blk_mq_requeue_request(rq);
1676}
1677
Ming Lei75383522020-06-30 18:24:58 +08001678enum prep_dispatch {
1679 PREP_DISPATCH_OK,
1680 PREP_DISPATCH_NO_TAG,
1681 PREP_DISPATCH_NO_BUDGET,
1682};
1683
1684static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq,
1685 bool need_budget)
1686{
1687 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Ming Lei2a5a24a2021-01-22 10:33:12 +08001688 int budget_token = -1;
Ming Lei75383522020-06-30 18:24:58 +08001689
Ming Lei2a5a24a2021-01-22 10:33:12 +08001690 if (need_budget) {
1691 budget_token = blk_mq_get_dispatch_budget(rq->q);
1692 if (budget_token < 0) {
1693 blk_mq_put_driver_tag(rq);
1694 return PREP_DISPATCH_NO_BUDGET;
1695 }
1696 blk_mq_set_rq_budget_token(rq, budget_token);
Ming Lei75383522020-06-30 18:24:58 +08001697 }
1698
1699 if (!blk_mq_get_driver_tag(rq)) {
1700 /*
1701 * The initial allocation attempt failed, so we need to
1702 * rerun the hardware queue when a tag is freed. The
1703 * waitqueue takes care of that. If the queue is run
1704 * before we add this entry back on the dispatch list,
1705 * we'll re-run it below.
1706 */
1707 if (!blk_mq_mark_tag_wait(hctx, rq)) {
Ming Lei1fd40b52020-06-30 18:25:00 +08001708 /*
1709 * All budgets not got from this function will be put
1710 * together during handling partial dispatch
1711 */
1712 if (need_budget)
Ming Lei2a5a24a2021-01-22 10:33:12 +08001713 blk_mq_put_dispatch_budget(rq->q, budget_token);
Ming Lei75383522020-06-30 18:24:58 +08001714 return PREP_DISPATCH_NO_TAG;
1715 }
1716 }
1717
1718 return PREP_DISPATCH_OK;
1719}
1720
Ming Lei1fd40b52020-06-30 18:25:00 +08001721/* release all allocated budgets before calling to blk_mq_dispatch_rq_list */
1722static void blk_mq_release_budgets(struct request_queue *q,
Ming Lei2a5a24a2021-01-22 10:33:12 +08001723 struct list_head *list)
Ming Lei1fd40b52020-06-30 18:25:00 +08001724{
Ming Lei2a5a24a2021-01-22 10:33:12 +08001725 struct request *rq;
Ming Lei1fd40b52020-06-30 18:25:00 +08001726
Ming Lei2a5a24a2021-01-22 10:33:12 +08001727 list_for_each_entry(rq, list, queuelist) {
1728 int budget_token = blk_mq_get_rq_budget_token(rq);
1729
1730 if (budget_token >= 0)
1731 blk_mq_put_dispatch_budget(q, budget_token);
1732 }
Ming Lei1fd40b52020-06-30 18:25:00 +08001733}
1734
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001735/*
1736 * Returns true if we did some work AND can potentially do more.
1737 */
Ming Lei445874e2020-06-30 18:24:57 +08001738bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list,
Ming Lei1fd40b52020-06-30 18:25:00 +08001739 unsigned int nr_budgets)
Jens Axboef04c3df2016-12-07 08:41:17 -07001740{
Ming Lei75383522020-06-30 18:24:58 +08001741 enum prep_dispatch prep;
Ming Lei445874e2020-06-30 18:24:57 +08001742 struct request_queue *q = hctx->queue;
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001743 struct request *rq, *nxt;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001744 int errors, queued;
Ming Lei86ff7c22018-01-30 22:04:57 -05001745 blk_status_t ret = BLK_STS_OK;
Keith Busch0512a752020-05-12 17:55:47 +09001746 LIST_HEAD(zone_list);
Naohiro Aota9586e672021-10-27 01:51:27 +09001747 bool needs_resource = false;
Jens Axboef04c3df2016-12-07 08:41:17 -07001748
Omar Sandoval81380ca2017-04-07 08:56:26 -06001749 if (list_empty(list))
1750 return false;
1751
Jens Axboef04c3df2016-12-07 08:41:17 -07001752 /*
Jens Axboef04c3df2016-12-07 08:41:17 -07001753 * Now process all the entries, sending them to the driver.
1754 */
Jens Axboe93efe982017-03-24 12:04:19 -06001755 errors = queued = 0;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001756 do {
Jens Axboef04c3df2016-12-07 08:41:17 -07001757 struct blk_mq_queue_data bd;
1758
1759 rq = list_first_entry(list, struct request, queuelist);
Ming Lei0bca7992018-04-05 00:35:21 +08001760
Ming Lei445874e2020-06-30 18:24:57 +08001761 WARN_ON_ONCE(hctx != rq->mq_hctx);
Ming Lei1fd40b52020-06-30 18:25:00 +08001762 prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets);
Ming Lei75383522020-06-30 18:24:58 +08001763 if (prep != PREP_DISPATCH_OK)
Ming Lei0bca7992018-04-05 00:35:21 +08001764 break;
Ming Leide148292017-10-14 17:22:29 +08001765
Jens Axboef04c3df2016-12-07 08:41:17 -07001766 list_del_init(&rq->queuelist);
1767
1768 bd.rq = rq;
Jens Axboe113285b2017-03-02 13:26:04 -07001769
1770 /*
1771 * Flag last if we have no more requests, or if we have more
1772 * but can't assign a driver tag to it.
1773 */
1774 if (list_empty(list))
1775 bd.last = true;
1776 else {
Jens Axboe113285b2017-03-02 13:26:04 -07001777 nxt = list_first_entry(list, struct request, queuelist);
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001778 bd.last = !blk_mq_get_driver_tag(nxt);
Jens Axboe113285b2017-03-02 13:26:04 -07001779 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001780
Ming Lei1fd40b52020-06-30 18:25:00 +08001781 /*
1782 * once the request is queued to lld, no need to cover the
1783 * budget any more
1784 */
1785 if (nr_budgets)
1786 nr_budgets--;
Jens Axboef04c3df2016-12-07 08:41:17 -07001787 ret = q->mq_ops->queue_rq(hctx, &bd);
Ming Lei7bf13722020-07-01 21:58:57 +08001788 switch (ret) {
1789 case BLK_STS_OK:
1790 queued++;
Jens Axboef04c3df2016-12-07 08:41:17 -07001791 break;
Ming Lei7bf13722020-07-01 21:58:57 +08001792 case BLK_STS_RESOURCE:
Naohiro Aota9586e672021-10-27 01:51:27 +09001793 needs_resource = true;
1794 fallthrough;
Ming Lei7bf13722020-07-01 21:58:57 +08001795 case BLK_STS_DEV_RESOURCE:
1796 blk_mq_handle_dev_resource(rq, list);
1797 goto out;
1798 case BLK_STS_ZONE_RESOURCE:
Keith Busch0512a752020-05-12 17:55:47 +09001799 /*
1800 * Move the request to zone_list and keep going through
1801 * the dispatch list to find more requests the drive can
1802 * accept.
1803 */
1804 blk_mq_handle_zone_resource(rq, &zone_list);
Naohiro Aota9586e672021-10-27 01:51:27 +09001805 needs_resource = true;
Ming Lei7bf13722020-07-01 21:58:57 +08001806 break;
1807 default:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001808 errors++;
Hannes Reineckee21ee5a2020-09-30 10:02:53 +02001809 blk_mq_end_request(rq, ret);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001810 }
Omar Sandoval81380ca2017-04-07 08:56:26 -06001811 } while (!list_empty(list));
Ming Lei7bf13722020-07-01 21:58:57 +08001812out:
Keith Busch0512a752020-05-12 17:55:47 +09001813 if (!list_empty(&zone_list))
1814 list_splice_tail_init(&zone_list, list);
1815
yangerkun632bfb62020-09-05 19:25:56 +08001816 /* If we didn't flush the entire list, we could have told the driver
1817 * there was more coming, but that turned out to be a lie.
1818 */
1819 if ((!list_empty(list) || errors) && q->mq_ops->commit_rqs && queued)
1820 q->mq_ops->commit_rqs(hctx);
Jens Axboef04c3df2016-12-07 08:41:17 -07001821 /*
1822 * Any items that need requeuing? Stuff them into hctx->dispatch,
1823 * that is where we will continue on next queue run.
1824 */
1825 if (!list_empty(list)) {
Ming Lei86ff7c22018-01-30 22:04:57 -05001826 bool needs_restart;
Ming Lei75383522020-06-30 18:24:58 +08001827 /* For non-shared tags, the RESTART check will suffice */
1828 bool no_tag = prep == PREP_DISPATCH_NO_TAG &&
Ming Lei51db1c32020-08-19 23:20:19 +08001829 (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED);
Ming Lei86ff7c22018-01-30 22:04:57 -05001830
Ming Lei2a5a24a2021-01-22 10:33:12 +08001831 if (nr_budgets)
1832 blk_mq_release_budgets(q, list);
Jens Axboef04c3df2016-12-07 08:41:17 -07001833
1834 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001835 list_splice_tail_init(list, &hctx->dispatch);
Jens Axboef04c3df2016-12-07 08:41:17 -07001836 spin_unlock(&hctx->lock);
1837
1838 /*
Ming Leid7d85352020-08-17 18:01:15 +08001839 * Order adding requests to hctx->dispatch and checking
1840 * SCHED_RESTART flag. The pair of this smp_mb() is the one
1841 * in blk_mq_sched_restart(). Avoid restart code path to
1842 * miss the new added requests to hctx->dispatch, meantime
1843 * SCHED_RESTART is observed here.
1844 */
1845 smp_mb();
1846
1847 /*
Bart Van Assche710c7852017-04-07 11:16:51 -07001848 * If SCHED_RESTART was set by the caller of this function and
1849 * it is no longer set that means that it was cleared by another
1850 * thread and hence that a queue rerun is needed.
Jens Axboef04c3df2016-12-07 08:41:17 -07001851 *
Jens Axboeeb619fd2017-11-09 08:32:43 -07001852 * If 'no_tag' is set, that means that we failed getting
1853 * a driver tag with an I/O scheduler attached. If our dispatch
1854 * waitqueue is no longer active, ensure that we run the queue
1855 * AFTER adding our entries back to the list.
Jens Axboebd166ef2017-01-17 06:03:22 -07001856 *
Bart Van Assche710c7852017-04-07 11:16:51 -07001857 * If no I/O scheduler has been configured it is possible that
1858 * the hardware queue got stopped and restarted before requests
1859 * were pushed back onto the dispatch list. Rerun the queue to
1860 * avoid starvation. Notes:
1861 * - blk_mq_run_hw_queue() checks whether or not a queue has
1862 * been stopped before rerunning a queue.
1863 * - Some but not all block drivers stop a queue before
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001864 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
Bart Van Assche710c7852017-04-07 11:16:51 -07001865 * and dm-rq.
Ming Lei86ff7c22018-01-30 22:04:57 -05001866 *
1867 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1868 * bit is set, run queue after a delay to avoid IO stalls
Douglas Andersonab3cee32020-04-20 09:24:51 -07001869 * that could otherwise occur if the queue is idle. We'll do
Naohiro Aota9586e672021-10-27 01:51:27 +09001870 * similar if we couldn't get budget or couldn't lock a zone
1871 * and SCHED_RESTART is set.
Jens Axboebd166ef2017-01-17 06:03:22 -07001872 */
Ming Lei86ff7c22018-01-30 22:04:57 -05001873 needs_restart = blk_mq_sched_needs_restart(hctx);
Naohiro Aota9586e672021-10-27 01:51:27 +09001874 if (prep == PREP_DISPATCH_NO_BUDGET)
1875 needs_resource = true;
Ming Lei86ff7c22018-01-30 22:04:57 -05001876 if (!needs_restart ||
Jens Axboeeb619fd2017-11-09 08:32:43 -07001877 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
Jens Axboebd166ef2017-01-17 06:03:22 -07001878 blk_mq_run_hw_queue(hctx, true);
Naohiro Aota9586e672021-10-27 01:51:27 +09001879 else if (needs_restart && needs_resource)
Ming Lei86ff7c22018-01-30 22:04:57 -05001880 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001881
Ming Lei6e7687172018-07-03 09:03:16 -06001882 blk_mq_update_dispatch_busy(hctx, true);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001883 return false;
Ming Lei6e7687172018-07-03 09:03:16 -06001884 } else
1885 blk_mq_update_dispatch_busy(hctx, false);
Jens Axboef04c3df2016-12-07 08:41:17 -07001886
Jens Axboe93efe982017-03-24 12:04:19 -06001887 return (queued + errors) != 0;
Jens Axboef04c3df2016-12-07 08:41:17 -07001888}
1889
André Almeida105663f2020-01-06 15:08:18 -03001890/**
1891 * __blk_mq_run_hw_queue - Run a hardware queue.
1892 * @hctx: Pointer to the hardware queue to run.
1893 *
1894 * Send pending requests to the hardware.
1895 */
Bart Van Assche6a83e742016-11-02 10:09:51 -06001896static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1897{
1898 int srcu_idx;
1899
Jens Axboeb7a71e62017-08-01 09:28:24 -06001900 /*
Jens Axboeb7a71e62017-08-01 09:28:24 -06001901 * We can't run the queue inline with ints disabled. Ensure that
1902 * we catch bad users of this early.
1903 */
1904 WARN_ON_ONCE(in_interrupt());
1905
Jens Axboe04ced152018-01-09 08:29:46 -08001906 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
Jens Axboebf4907c2017-03-30 12:30:39 -06001907
Jens Axboe04ced152018-01-09 08:29:46 -08001908 hctx_lock(hctx, &srcu_idx);
1909 blk_mq_sched_dispatch_requests(hctx);
1910 hctx_unlock(hctx, srcu_idx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001911}
1912
Ming Leif82ddf12018-04-08 17:48:10 +08001913static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1914{
1915 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1916
1917 if (cpu >= nr_cpu_ids)
1918 cpu = cpumask_first(hctx->cpumask);
1919 return cpu;
1920}
1921
Jens Axboe506e9312014-05-07 10:26:44 -06001922/*
1923 * It'd be great if the workqueue API had a way to pass
1924 * in a mask and had some smarts for more clever placement.
1925 * For now we just round-robin here, switching for every
1926 * BLK_MQ_CPU_WORK_BATCH queued items.
1927 */
1928static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1929{
Ming Lei7bed4592018-01-18 00:41:51 +08001930 bool tried = false;
Ming Lei476f8c92018-04-08 17:48:09 +08001931 int next_cpu = hctx->next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001932
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001933 if (hctx->queue->nr_hw_queues == 1)
1934 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -06001935
1936 if (--hctx->next_cpu_batch <= 0) {
Ming Lei7bed4592018-01-18 00:41:51 +08001937select_cpu:
Ming Lei476f8c92018-04-08 17:48:09 +08001938 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08001939 cpu_online_mask);
Jens Axboe506e9312014-05-07 10:26:44 -06001940 if (next_cpu >= nr_cpu_ids)
Ming Leif82ddf12018-04-08 17:48:10 +08001941 next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06001942 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1943 }
1944
Ming Lei7bed4592018-01-18 00:41:51 +08001945 /*
1946 * Do unbound schedule if we can't find a online CPU for this hctx,
1947 * and it should only happen in the path of handling CPU DEAD.
1948 */
Ming Lei476f8c92018-04-08 17:48:09 +08001949 if (!cpu_online(next_cpu)) {
Ming Lei7bed4592018-01-18 00:41:51 +08001950 if (!tried) {
1951 tried = true;
1952 goto select_cpu;
1953 }
1954
1955 /*
1956 * Make sure to re-select CPU next time once after CPUs
1957 * in hctx->cpumask become online again.
1958 */
Ming Lei476f8c92018-04-08 17:48:09 +08001959 hctx->next_cpu = next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001960 hctx->next_cpu_batch = 1;
1961 return WORK_CPU_UNBOUND;
1962 }
Ming Lei476f8c92018-04-08 17:48:09 +08001963
1964 hctx->next_cpu = next_cpu;
1965 return next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -06001966}
1967
André Almeida105663f2020-01-06 15:08:18 -03001968/**
1969 * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue.
1970 * @hctx: Pointer to the hardware queue to run.
1971 * @async: If we want to run the queue asynchronously.
Minwoo Imfa94ba82020-12-05 00:20:55 +09001972 * @msecs: Milliseconds of delay to wait before running the queue.
André Almeida105663f2020-01-06 15:08:18 -03001973 *
1974 * If !@async, try to run the queue now. Else, run the queue asynchronously and
1975 * with a delay of @msecs.
1976 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001977static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1978 unsigned long msecs)
Jens Axboe320ae512013-10-24 09:20:05 +01001979{
Bart Van Assche5435c022017-06-20 11:15:49 -07001980 if (unlikely(blk_mq_hctx_stopped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +01001981 return;
1982
Jens Axboe1b792f22016-09-21 10:12:13 -06001983 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001984 int cpu = get_cpu();
1985 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +01001986 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001987 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +01001988 return;
1989 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001990
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001991 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -06001992 }
Paolo Bonzini398205b2014-11-07 23:03:59 +01001993
Bart Van Asscheae943d22018-01-19 08:58:55 -08001994 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1995 msecs_to_jiffies(msecs));
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001996}
1997
André Almeida105663f2020-01-06 15:08:18 -03001998/**
1999 * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
2000 * @hctx: Pointer to the hardware queue to run.
Minwoo Imfa94ba82020-12-05 00:20:55 +09002001 * @msecs: Milliseconds of delay to wait before running the queue.
André Almeida105663f2020-01-06 15:08:18 -03002002 *
2003 * Run a hardware queue asynchronously with a delay of @msecs.
2004 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07002005void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
2006{
2007 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
2008}
2009EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
2010
André Almeida105663f2020-01-06 15:08:18 -03002011/**
2012 * blk_mq_run_hw_queue - Start to run a hardware queue.
2013 * @hctx: Pointer to the hardware queue to run.
2014 * @async: If we want to run the queue asynchronously.
2015 *
2016 * Check if the request queue is not in a quiesced state and if there are
2017 * pending requests to be sent. If this is true, run the queue to send requests
2018 * to hardware.
2019 */
John Garry626fb732019-10-30 00:59:30 +08002020void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
Bart Van Assche7587a5a2017-04-07 11:16:52 -07002021{
Ming Lei24f5a902018-01-06 16:27:38 +08002022 int srcu_idx;
2023 bool need_run;
2024
2025 /*
2026 * When queue is quiesced, we may be switching io scheduler, or
2027 * updating nr_hw_queues, or other things, and we can't run queue
2028 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
2029 *
2030 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
2031 * quiesced.
2032 */
Jens Axboe04ced152018-01-09 08:29:46 -08002033 hctx_lock(hctx, &srcu_idx);
2034 need_run = !blk_queue_quiesced(hctx->queue) &&
2035 blk_mq_hctx_has_pending(hctx);
2036 hctx_unlock(hctx, srcu_idx);
Ming Lei24f5a902018-01-06 16:27:38 +08002037
John Garry626fb732019-10-30 00:59:30 +08002038 if (need_run)
Jens Axboe79f720a2017-11-10 09:13:21 -07002039 __blk_mq_delay_run_hw_queue(hctx, async, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01002040}
Omar Sandoval5b727272017-04-14 01:00:00 -07002041EXPORT_SYMBOL(blk_mq_run_hw_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01002042
Jan Karab6e68ee2021-01-11 17:47:17 +01002043/*
2044 * Is the request queue handled by an IO scheduler that does not respect
2045 * hardware queues when dispatching?
2046 */
2047static bool blk_mq_has_sqsched(struct request_queue *q)
2048{
2049 struct elevator_queue *e = q->elevator;
2050
2051 if (e && e->type->ops.dispatch_request &&
2052 !(e->type->elevator_features & ELEVATOR_F_MQ_AWARE))
2053 return true;
2054 return false;
2055}
2056
2057/*
2058 * Return prefered queue to dispatch from (if any) for non-mq aware IO
2059 * scheduler.
2060 */
2061static struct blk_mq_hw_ctx *blk_mq_get_sq_hctx(struct request_queue *q)
2062{
2063 struct blk_mq_hw_ctx *hctx;
2064
2065 /*
2066 * If the IO scheduler does not respect hardware queues when
2067 * dispatching, we just don't bother with multiple HW queues and
2068 * dispatch from hctx for the current CPU since running multiple queues
2069 * just causes lock contention inside the scheduler and pointless cache
2070 * bouncing.
2071 */
2072 hctx = blk_mq_map_queue_type(q, HCTX_TYPE_DEFAULT,
2073 raw_smp_processor_id());
2074 if (!blk_mq_hctx_stopped(hctx))
2075 return hctx;
2076 return NULL;
2077}
2078
André Almeida105663f2020-01-06 15:08:18 -03002079/**
Mauro Carvalho Chehab24f7bb82020-10-23 18:32:54 +02002080 * blk_mq_run_hw_queues - Run all hardware queues in a request queue.
André Almeida105663f2020-01-06 15:08:18 -03002081 * @q: Pointer to the request queue to run.
2082 * @async: If we want to run the queue asynchronously.
2083 */
Mike Snitzerb94ec292015-03-11 23:56:38 -04002084void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01002085{
Jan Karab6e68ee2021-01-11 17:47:17 +01002086 struct blk_mq_hw_ctx *hctx, *sq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01002087 int i;
2088
Jan Karab6e68ee2021-01-11 17:47:17 +01002089 sq_hctx = NULL;
2090 if (blk_mq_has_sqsched(q))
2091 sq_hctx = blk_mq_get_sq_hctx(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002092 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe79f720a2017-11-10 09:13:21 -07002093 if (blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +01002094 continue;
Jan Karab6e68ee2021-01-11 17:47:17 +01002095 /*
2096 * Dispatch from this hctx either if there's no hctx preferred
2097 * by IO scheduler or if it has requests that bypass the
2098 * scheduler.
2099 */
2100 if (!sq_hctx || sq_hctx == hctx ||
2101 !list_empty_careful(&hctx->dispatch))
2102 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01002103 }
2104}
Mike Snitzerb94ec292015-03-11 23:56:38 -04002105EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01002106
Bart Van Asschefd001442016-10-28 17:19:37 -07002107/**
Douglas Andersonb9151e72020-04-20 09:24:52 -07002108 * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously.
2109 * @q: Pointer to the request queue to run.
Minwoo Imfa94ba82020-12-05 00:20:55 +09002110 * @msecs: Milliseconds of delay to wait before running the queues.
Douglas Andersonb9151e72020-04-20 09:24:52 -07002111 */
2112void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs)
2113{
Jan Karab6e68ee2021-01-11 17:47:17 +01002114 struct blk_mq_hw_ctx *hctx, *sq_hctx;
Douglas Andersonb9151e72020-04-20 09:24:52 -07002115 int i;
2116
Jan Karab6e68ee2021-01-11 17:47:17 +01002117 sq_hctx = NULL;
2118 if (blk_mq_has_sqsched(q))
2119 sq_hctx = blk_mq_get_sq_hctx(q);
Douglas Andersonb9151e72020-04-20 09:24:52 -07002120 queue_for_each_hw_ctx(q, hctx, i) {
2121 if (blk_mq_hctx_stopped(hctx))
2122 continue;
Jan Karab6e68ee2021-01-11 17:47:17 +01002123 /*
2124 * Dispatch from this hctx either if there's no hctx preferred
2125 * by IO scheduler or if it has requests that bypass the
2126 * scheduler.
2127 */
2128 if (!sq_hctx || sq_hctx == hctx ||
2129 !list_empty_careful(&hctx->dispatch))
2130 blk_mq_delay_run_hw_queue(hctx, msecs);
Douglas Andersonb9151e72020-04-20 09:24:52 -07002131 }
2132}
2133EXPORT_SYMBOL(blk_mq_delay_run_hw_queues);
2134
2135/**
Bart Van Asschefd001442016-10-28 17:19:37 -07002136 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
2137 * @q: request queue.
2138 *
2139 * The caller is responsible for serializing this function against
2140 * blk_mq_{start,stop}_hw_queue().
2141 */
2142bool blk_mq_queue_stopped(struct request_queue *q)
2143{
2144 struct blk_mq_hw_ctx *hctx;
2145 int i;
2146
2147 queue_for_each_hw_ctx(q, hctx, i)
2148 if (blk_mq_hctx_stopped(hctx))
2149 return true;
2150
2151 return false;
2152}
2153EXPORT_SYMBOL(blk_mq_queue_stopped);
2154
Ming Lei39a70c72017-06-06 23:22:09 +08002155/*
2156 * This function is often used for pausing .queue_rq() by driver when
2157 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07002158 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08002159 *
2160 * We do not guarantee that dispatch can be drained or blocked
2161 * after blk_mq_stop_hw_queue() returns. Please use
2162 * blk_mq_quiesce_queue() for that requirement.
2163 */
Jens Axboe320ae512013-10-24 09:20:05 +01002164void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
2165{
Ming Lei641a9ed2017-06-06 23:22:10 +08002166 cancel_delayed_work(&hctx->run_work);
2167
2168 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboe320ae512013-10-24 09:20:05 +01002169}
2170EXPORT_SYMBOL(blk_mq_stop_hw_queue);
2171
Ming Lei39a70c72017-06-06 23:22:09 +08002172/*
2173 * This function is often used for pausing .queue_rq() by driver when
2174 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07002175 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08002176 *
2177 * We do not guarantee that dispatch can be drained or blocked
2178 * after blk_mq_stop_hw_queues() returns. Please use
2179 * blk_mq_quiesce_queue() for that requirement.
2180 */
Jens Axboe2719aa22017-05-03 11:08:14 -06002181void blk_mq_stop_hw_queues(struct request_queue *q)
2182{
Ming Lei641a9ed2017-06-06 23:22:10 +08002183 struct blk_mq_hw_ctx *hctx;
2184 int i;
2185
2186 queue_for_each_hw_ctx(q, hctx, i)
2187 blk_mq_stop_hw_queue(hctx);
Christoph Hellwig280d45f2013-10-25 14:45:58 +01002188}
2189EXPORT_SYMBOL(blk_mq_stop_hw_queues);
2190
Jens Axboe320ae512013-10-24 09:20:05 +01002191void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
2192{
2193 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -06002194
Jens Axboe0ffbce82014-06-25 08:22:34 -06002195 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +01002196}
2197EXPORT_SYMBOL(blk_mq_start_hw_queue);
2198
Christoph Hellwig2f268552014-04-16 09:44:56 +02002199void blk_mq_start_hw_queues(struct request_queue *q)
2200{
2201 struct blk_mq_hw_ctx *hctx;
2202 int i;
2203
2204 queue_for_each_hw_ctx(q, hctx, i)
2205 blk_mq_start_hw_queue(hctx);
2206}
2207EXPORT_SYMBOL(blk_mq_start_hw_queues);
2208
Jens Axboeae911c52016-12-08 13:19:30 -07002209void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
2210{
2211 if (!blk_mq_hctx_stopped(hctx))
2212 return;
2213
2214 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
2215 blk_mq_run_hw_queue(hctx, async);
2216}
2217EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
2218
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02002219void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01002220{
2221 struct blk_mq_hw_ctx *hctx;
2222 int i;
2223
Jens Axboeae911c52016-12-08 13:19:30 -07002224 queue_for_each_hw_ctx(q, hctx, i)
2225 blk_mq_start_stopped_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01002226}
2227EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
2228
Christoph Hellwig70f4db62014-04-16 10:48:08 -06002229static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01002230{
2231 struct blk_mq_hw_ctx *hctx;
2232
Jens Axboe9f993732017-04-10 09:54:54 -06002233 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboe21c6e932017-04-10 09:54:56 -06002234
2235 /*
Ming Lei15fe8a902018-04-08 17:48:11 +08002236 * If we are stopped, don't run the queue.
Jens Axboe21c6e932017-04-10 09:54:56 -06002237 */
Yufen Yu08410312020-10-08 23:26:30 -04002238 if (blk_mq_hctx_stopped(hctx))
Jianchao Wang0196d6b2018-06-04 17:03:55 +08002239 return;
Jens Axboee4043dc2014-04-09 10:18:23 -06002240
Jens Axboe320ae512013-10-24 09:20:05 +01002241 __blk_mq_run_hw_queue(hctx);
2242}
2243
Ming Leicfd0c552015-10-20 23:13:57 +08002244static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08002245 struct request *rq,
2246 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01002247{
Jens Axboee57690f2016-08-24 15:34:35 -06002248 struct blk_mq_ctx *ctx = rq->mq_ctx;
Ming Leic16d6b52018-12-17 08:44:05 -07002249 enum hctx_type type = hctx->type;
Jens Axboee57690f2016-08-24 15:34:35 -06002250
Bart Van Assche7b607812017-06-20 11:15:47 -07002251 lockdep_assert_held(&ctx->lock);
2252
Christoph Hellwiga54895f2020-12-03 17:21:39 +01002253 trace_block_rq_insert(rq);
Jens Axboe01b983c2013-11-19 18:59:10 -07002254
Christoph Hellwig72a0a362014-02-07 10:22:36 -08002255 if (at_head)
Ming Leic16d6b52018-12-17 08:44:05 -07002256 list_add(&rq->queuelist, &ctx->rq_lists[type]);
Christoph Hellwig72a0a362014-02-07 10:22:36 -08002257 else
Ming Leic16d6b52018-12-17 08:44:05 -07002258 list_add_tail(&rq->queuelist, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08002259}
Jens Axboe4bb659b2014-05-09 09:36:49 -06002260
Jens Axboe2c3ad662016-12-14 14:34:47 -07002261void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
2262 bool at_head)
Ming Leicfd0c552015-10-20 23:13:57 +08002263{
2264 struct blk_mq_ctx *ctx = rq->mq_ctx;
2265
Bart Van Assche7b607812017-06-20 11:15:47 -07002266 lockdep_assert_held(&ctx->lock);
2267
Jens Axboee57690f2016-08-24 15:34:35 -06002268 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01002269 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01002270}
2271
André Almeida105663f2020-01-06 15:08:18 -03002272/**
2273 * blk_mq_request_bypass_insert - Insert a request at dispatch list.
2274 * @rq: Pointer to request to be inserted.
Randy Dunlap26bfeb22020-08-16 16:39:34 -07002275 * @at_head: true if the request should be inserted at the head of the list.
André Almeida105663f2020-01-06 15:08:18 -03002276 * @run_queue: If we should run the hardware queue after inserting the request.
2277 *
Jens Axboe157f3772017-09-11 16:43:57 -06002278 * Should only be used carefully, when the caller knows we want to
2279 * bypass a potential IO scheduler on the target device.
2280 */
Ming Lei01e99ae2020-02-25 09:04:32 +08002281void blk_mq_request_bypass_insert(struct request *rq, bool at_head,
2282 bool run_queue)
Jens Axboe157f3772017-09-11 16:43:57 -06002283{
Jens Axboeea4f9952018-10-29 15:06:13 -06002284 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe157f3772017-09-11 16:43:57 -06002285
2286 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08002287 if (at_head)
2288 list_add(&rq->queuelist, &hctx->dispatch);
2289 else
2290 list_add_tail(&rq->queuelist, &hctx->dispatch);
Jens Axboe157f3772017-09-11 16:43:57 -06002291 spin_unlock(&hctx->lock);
2292
Ming Leib0850292017-11-02 23:24:34 +08002293 if (run_queue)
2294 blk_mq_run_hw_queue(hctx, false);
Jens Axboe157f3772017-09-11 16:43:57 -06002295}
2296
Jens Axboebd166ef2017-01-17 06:03:22 -07002297void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
2298 struct list_head *list)
Jens Axboe320ae512013-10-24 09:20:05 +01002299
2300{
Ming Lei3f0cedc2018-07-02 17:35:58 +08002301 struct request *rq;
Ming Leic16d6b52018-12-17 08:44:05 -07002302 enum hctx_type type = hctx->type;
Ming Lei3f0cedc2018-07-02 17:35:58 +08002303
Jens Axboe320ae512013-10-24 09:20:05 +01002304 /*
2305 * preemption doesn't flush plug list, so it's possible ctx->cpu is
2306 * offline now
2307 */
Ming Lei3f0cedc2018-07-02 17:35:58 +08002308 list_for_each_entry(rq, list, queuelist) {
Jens Axboee57690f2016-08-24 15:34:35 -06002309 BUG_ON(rq->mq_ctx != ctx);
Christoph Hellwiga54895f2020-12-03 17:21:39 +01002310 trace_block_rq_insert(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01002311 }
Ming Lei3f0cedc2018-07-02 17:35:58 +08002312
2313 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002314 list_splice_tail_init(list, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08002315 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01002316 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01002317}
2318
Jens Axboedc5fc3612021-10-19 06:02:30 -06002319static void blk_mq_commit_rqs(struct blk_mq_hw_ctx *hctx, int *queued,
2320 bool from_schedule)
Jens Axboe320ae512013-10-24 09:20:05 +01002321{
Jens Axboedc5fc3612021-10-19 06:02:30 -06002322 if (hctx->queue->mq_ops->commit_rqs) {
2323 trace_block_unplug(hctx->queue, *queued, !from_schedule);
2324 hctx->queue->mq_ops->commit_rqs(hctx);
2325 }
2326 *queued = 0;
2327}
Jens Axboe320ae512013-10-24 09:20:05 +01002328
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002329static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
2330 unsigned int nr_segs)
Jens Axboe320ae512013-10-24 09:20:05 +01002331{
Eric Biggers93f221a2020-09-15 20:53:14 -07002332 int err;
2333
Christoph Hellwigf924cdd2019-06-06 12:29:00 +02002334 if (bio->bi_opf & REQ_RAHEAD)
2335 rq->cmd_flags |= REQ_FAILFAST_MASK;
2336
2337 rq->__sector = bio->bi_iter.bi_sector;
2338 rq->write_hint = bio->bi_write_hint;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002339 blk_rq_bio_prep(rq, bio, nr_segs);
Eric Biggers93f221a2020-09-15 20:53:14 -07002340
2341 /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */
2342 err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO);
2343 WARN_ON_ONCE(err);
Jens Axboe4b570522014-05-29 11:00:11 -06002344
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +02002345 blk_account_io_start(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01002346}
2347
Mike Snitzer0f955492018-01-17 11:25:56 -05002348static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig3e087732021-10-12 13:12:24 +02002349 struct request *rq, bool last)
Shaohua Lif984df12015-05-08 10:51:32 -07002350{
Shaohua Lif984df12015-05-08 10:51:32 -07002351 struct request_queue *q = rq->q;
Shaohua Lif984df12015-05-08 10:51:32 -07002352 struct blk_mq_queue_data bd = {
2353 .rq = rq,
Jens Axboebe94f052018-11-24 10:15:46 -07002354 .last = last,
Shaohua Lif984df12015-05-08 10:51:32 -07002355 };
Jens Axboef06345a2017-06-12 11:22:46 -06002356 blk_status_t ret;
Mike Snitzer0f955492018-01-17 11:25:56 -05002357
Mike Snitzer0f955492018-01-17 11:25:56 -05002358 /*
2359 * For OK queue, we are done. For error, caller may kill it.
2360 * Any other error (busy), just add it to our list as we
2361 * previously would have done.
2362 */
2363 ret = q->mq_ops->queue_rq(hctx, &bd);
2364 switch (ret) {
2365 case BLK_STS_OK:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002366 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05002367 break;
2368 case BLK_STS_RESOURCE:
Ming Lei86ff7c22018-01-30 22:04:57 -05002369 case BLK_STS_DEV_RESOURCE:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002370 blk_mq_update_dispatch_busy(hctx, true);
Mike Snitzer0f955492018-01-17 11:25:56 -05002371 __blk_mq_requeue_request(rq);
2372 break;
2373 default:
Ming Lei6ce3dd62018-07-10 09:03:31 +08002374 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05002375 break;
2376 }
2377
2378 return ret;
2379}
2380
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002381static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
Mike Snitzer0f955492018-01-17 11:25:56 -05002382 struct request *rq,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002383 bool bypass_insert, bool last)
Mike Snitzer0f955492018-01-17 11:25:56 -05002384{
2385 struct request_queue *q = rq->q;
Ming Leid964f042017-06-06 23:22:00 +08002386 bool run_queue = true;
Ming Lei2a5a24a2021-01-22 10:33:12 +08002387 int budget_token;
Ming Leid964f042017-06-06 23:22:00 +08002388
Ming Lei23d4ee12018-01-18 12:06:59 +08002389 /*
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002390 * RCU or SRCU read lock is needed before checking quiesced flag.
Ming Lei23d4ee12018-01-18 12:06:59 +08002391 *
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002392 * When queue is stopped or quiesced, ignore 'bypass_insert' from
2393 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
2394 * and avoid driver to try to dispatch again.
Ming Lei23d4ee12018-01-18 12:06:59 +08002395 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002396 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
Ming Leid964f042017-06-06 23:22:00 +08002397 run_queue = false;
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002398 bypass_insert = false;
2399 goto insert;
Ming Leid964f042017-06-06 23:22:00 +08002400 }
Shaohua Lif984df12015-05-08 10:51:32 -07002401
Jens Axboe2ff06822021-10-15 09:44:38 -06002402 if ((rq->rq_flags & RQF_ELV) && !bypass_insert)
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002403 goto insert;
Bart Van Assche2253efc2016-10-28 17:20:02 -07002404
Ming Lei2a5a24a2021-01-22 10:33:12 +08002405 budget_token = blk_mq_get_dispatch_budget(q);
2406 if (budget_token < 0)
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002407 goto insert;
Jens Axboebd166ef2017-01-17 06:03:22 -07002408
Ming Lei2a5a24a2021-01-22 10:33:12 +08002409 blk_mq_set_rq_budget_token(rq, budget_token);
2410
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08002411 if (!blk_mq_get_driver_tag(rq)) {
Ming Lei2a5a24a2021-01-22 10:33:12 +08002412 blk_mq_put_dispatch_budget(q, budget_token);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002413 goto insert;
Ming Lei88022d72017-11-05 02:21:12 +08002414 }
Ming Leide148292017-10-14 17:22:29 +08002415
Christoph Hellwig3e087732021-10-12 13:12:24 +02002416 return __blk_mq_issue_directly(hctx, rq, last);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002417insert:
2418 if (bypass_insert)
2419 return BLK_STS_RESOURCE;
2420
Ming Leidb03f882020-08-18 17:07:28 +08002421 blk_mq_sched_insert_request(rq, false, run_queue, false);
2422
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002423 return BLK_STS_OK;
2424}
2425
André Almeida105663f2020-01-06 15:08:18 -03002426/**
2427 * blk_mq_try_issue_directly - Try to send a request directly to device driver.
2428 * @hctx: Pointer of the associated hardware queue.
2429 * @rq: Pointer to request to be sent.
André Almeida105663f2020-01-06 15:08:18 -03002430 *
2431 * If the device has enough resources to accept a new request now, send the
2432 * request directly to device driver. Else, insert at hctx->dispatch queue, so
2433 * we can try send it another time in the future. Requests inserted at this
2434 * queue have higher priority.
2435 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002436static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig3e087732021-10-12 13:12:24 +02002437 struct request *rq)
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002438{
2439 blk_status_t ret;
2440 int srcu_idx;
2441
2442 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
2443
2444 hctx_lock(hctx, &srcu_idx);
2445
Christoph Hellwig3e087732021-10-12 13:12:24 +02002446 ret = __blk_mq_try_issue_directly(hctx, rq, false, true);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002447 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
Ming Lei01e99ae2020-02-25 09:04:32 +08002448 blk_mq_request_bypass_insert(rq, false, true);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002449 else if (ret != BLK_STS_OK)
2450 blk_mq_end_request(rq, ret);
2451
Jens Axboe04ced152018-01-09 08:29:46 -08002452 hctx_unlock(hctx, srcu_idx);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002453}
2454
Christoph Hellwig06c8c692021-11-17 07:13:58 +01002455static blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002456{
2457 blk_status_t ret;
2458 int srcu_idx;
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002459 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
2460
2461 hctx_lock(hctx, &srcu_idx);
Christoph Hellwig3e087732021-10-12 13:12:24 +02002462 ret = __blk_mq_try_issue_directly(hctx, rq, true, last);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002463 hctx_unlock(hctx, srcu_idx);
Jianchao Wang7f556a42018-12-14 09:28:18 +08002464
2465 return ret;
Christoph Hellwig5eb61262017-03-22 15:01:51 -04002466}
2467
Christoph Hellwigb84c5b52021-11-17 07:13:57 +01002468static void blk_mq_plug_issue_direct(struct blk_plug *plug, bool from_schedule)
2469{
2470 struct blk_mq_hw_ctx *hctx = NULL;
2471 struct request *rq;
2472 int queued = 0;
2473 int errors = 0;
2474
2475 while ((rq = rq_list_pop(&plug->mq_list))) {
2476 bool last = rq_list_empty(plug->mq_list);
2477 blk_status_t ret;
2478
2479 if (hctx != rq->mq_hctx) {
2480 if (hctx)
2481 blk_mq_commit_rqs(hctx, &queued, from_schedule);
2482 hctx = rq->mq_hctx;
2483 }
2484
2485 ret = blk_mq_request_issue_directly(rq, last);
2486 switch (ret) {
2487 case BLK_STS_OK:
2488 queued++;
2489 break;
2490 case BLK_STS_RESOURCE:
2491 case BLK_STS_DEV_RESOURCE:
2492 blk_mq_request_bypass_insert(rq, false, last);
2493 blk_mq_commit_rqs(hctx, &queued, from_schedule);
2494 return;
2495 default:
2496 blk_mq_end_request(rq, ret);
2497 errors++;
2498 break;
2499 }
2500 }
2501
2502 /*
2503 * If we didn't flush the entire list, we could have told the driver
2504 * there was more coming, but that turned out to be a lie.
2505 */
2506 if (errors)
2507 blk_mq_commit_rqs(hctx, &queued, from_schedule);
2508}
2509
2510void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
2511{
2512 struct blk_mq_hw_ctx *this_hctx;
2513 struct blk_mq_ctx *this_ctx;
2514 unsigned int depth;
2515 LIST_HEAD(list);
2516
2517 if (rq_list_empty(plug->mq_list))
2518 return;
2519 plug->rq_count = 0;
2520
2521 if (!plug->multiple_queues && !plug->has_elevator && !from_schedule) {
2522 blk_mq_plug_issue_direct(plug, false);
2523 if (rq_list_empty(plug->mq_list))
2524 return;
2525 }
2526
2527 this_hctx = NULL;
2528 this_ctx = NULL;
2529 depth = 0;
2530 do {
2531 struct request *rq;
2532
2533 rq = rq_list_pop(&plug->mq_list);
2534
2535 if (!this_hctx) {
2536 this_hctx = rq->mq_hctx;
2537 this_ctx = rq->mq_ctx;
2538 } else if (this_hctx != rq->mq_hctx || this_ctx != rq->mq_ctx) {
2539 trace_block_unplug(this_hctx->queue, depth,
2540 !from_schedule);
2541 blk_mq_sched_insert_requests(this_hctx, this_ctx,
2542 &list, from_schedule);
2543 depth = 0;
2544 this_hctx = rq->mq_hctx;
2545 this_ctx = rq->mq_ctx;
2546
2547 }
2548
2549 list_add(&rq->queuelist, &list);
2550 depth++;
2551 } while (!rq_list_empty(plug->mq_list));
2552
2553 if (!list_empty(&list)) {
2554 trace_block_unplug(this_hctx->queue, depth, !from_schedule);
2555 blk_mq_sched_insert_requests(this_hctx, this_ctx, &list,
2556 from_schedule);
2557 }
2558}
2559
Ming Lei6ce3dd62018-07-10 09:03:31 +08002560void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
2561 struct list_head *list)
2562{
Keith Busch536167d42020-04-07 03:13:48 +09002563 int queued = 0;
yangerkun632bfb62020-09-05 19:25:56 +08002564 int errors = 0;
Keith Busch536167d42020-04-07 03:13:48 +09002565
Ming Lei6ce3dd62018-07-10 09:03:31 +08002566 while (!list_empty(list)) {
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002567 blk_status_t ret;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002568 struct request *rq = list_first_entry(list, struct request,
2569 queuelist);
2570
2571 list_del_init(&rq->queuelist);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002572 ret = blk_mq_request_issue_directly(rq, list_empty(list));
2573 if (ret != BLK_STS_OK) {
2574 if (ret == BLK_STS_RESOURCE ||
2575 ret == BLK_STS_DEV_RESOURCE) {
Ming Lei01e99ae2020-02-25 09:04:32 +08002576 blk_mq_request_bypass_insert(rq, false,
Jens Axboec616cbe2018-12-06 22:17:44 -07002577 list_empty(list));
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002578 break;
2579 }
2580 blk_mq_end_request(rq, ret);
yangerkun632bfb62020-09-05 19:25:56 +08002581 errors++;
Keith Busch536167d42020-04-07 03:13:48 +09002582 } else
2583 queued++;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002584 }
Jens Axboed666ba92018-11-27 17:02:25 -07002585
2586 /*
2587 * If we didn't flush the entire list, we could have told
2588 * the driver there was more coming, but that turned out to
2589 * be a lie.
2590 */
yangerkun632bfb62020-09-05 19:25:56 +08002591 if ((!list_empty(list) || errors) &&
2592 hctx->queue->mq_ops->commit_rqs && queued)
Jens Axboed666ba92018-11-27 17:02:25 -07002593 hctx->queue->mq_ops->commit_rqs(hctx);
Ming Lei6ce3dd62018-07-10 09:03:31 +08002594}
2595
Jens Axboece5b0092018-11-27 17:13:56 -07002596static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
2597{
Jens Axboebc490f82021-10-18 10:12:12 -06002598 if (!plug->multiple_queues) {
2599 struct request *nxt = rq_list_peek(&plug->mq_list);
Jens Axboece5b0092018-11-27 17:13:56 -07002600
Jens Axboebc490f82021-10-18 10:12:12 -06002601 if (nxt && nxt->q != rq->q)
Jens Axboece5b0092018-11-27 17:13:56 -07002602 plug->multiple_queues = true;
2603 }
Jens Axboedc5fc3612021-10-19 06:02:30 -06002604 if (!plug->has_elevator && (rq->rq_flags & RQF_ELV))
2605 plug->has_elevator = true;
Jens Axboebc490f82021-10-18 10:12:12 -06002606 rq->rq_next = NULL;
2607 rq_list_add(&plug->mq_list, rq);
2608 plug->rq_count++;
Jens Axboece5b0092018-11-27 17:13:56 -07002609}
2610
Song Liu7f2a6a62021-09-07 16:03:38 -07002611/*
Jens Axboeba0ffdd2021-10-06 12:01:07 -06002612 * Allow 2x BLK_MAX_REQUEST_COUNT requests on plug queue for multiple
Song Liu7f2a6a62021-09-07 16:03:38 -07002613 * queues. This is important for md arrays to benefit from merging
2614 * requests.
2615 */
2616static inline unsigned short blk_plug_max_rq_count(struct blk_plug *plug)
2617{
2618 if (plug->multiple_queues)
Jens Axboeba0ffdd2021-10-06 12:01:07 -06002619 return BLK_MAX_REQUEST_COUNT * 2;
Song Liu7f2a6a62021-09-07 16:03:38 -07002620 return BLK_MAX_REQUEST_COUNT;
2621}
2622
Ming Leib131f202021-11-11 16:51:34 +08002623static bool blk_mq_attempt_bio_merge(struct request_queue *q,
2624 struct bio *bio, unsigned int nr_segs,
2625 bool *same_queue_rq)
Jens Axboe900e0802021-11-03 05:47:09 -06002626{
2627 if (!blk_queue_nomerges(q) && bio_mergeable(bio)) {
2628 if (blk_attempt_plug_merge(q, bio, nr_segs, same_queue_rq))
2629 return true;
2630 if (blk_mq_sched_bio_merge(q, bio, nr_segs))
2631 return true;
2632 }
2633 return false;
2634}
2635
Jens Axboe71539712021-11-03 05:52:45 -06002636static struct request *blk_mq_get_new_requests(struct request_queue *q,
2637 struct blk_plug *plug,
Jens Axboe900e0802021-11-03 05:47:09 -06002638 struct bio *bio,
2639 unsigned int nsegs,
2640 bool *same_queue_rq)
Jens Axboe71539712021-11-03 05:52:45 -06002641{
2642 struct blk_mq_alloc_data data = {
2643 .q = q,
2644 .nr_tags = 1,
2645 .cmd_flags = bio->bi_opf,
2646 };
2647 struct request *rq;
2648
Ming Leib131f202021-11-11 16:51:34 +08002649 if (blk_mq_attempt_bio_merge(q, bio, nsegs, same_queue_rq))
Ming Leib6371082021-11-12 20:47:15 +08002650 return NULL;
Jens Axboe900e0802021-11-03 05:47:09 -06002651
2652 rq_qos_throttle(q, bio);
2653
Jens Axboe71539712021-11-03 05:52:45 -06002654 if (plug) {
2655 data.nr_tags = plug->nr_ios;
2656 plug->nr_ios = 1;
2657 data.cached_rq = &plug->cached_rq;
2658 }
2659
2660 rq = __blk_mq_alloc_requests(&data);
2661 if (rq)
2662 return rq;
2663
2664 rq_qos_cleanup(q, bio);
2665 if (bio->bi_opf & REQ_NOWAIT)
2666 bio_wouldblock_error(bio);
Ming Leib6371082021-11-12 20:47:15 +08002667
Jens Axboe71539712021-11-03 05:52:45 -06002668 return NULL;
2669}
2670
Jens Axboe95febeb2021-11-15 14:23:08 -07002671static inline bool blk_mq_can_use_cached_rq(struct request *rq, struct bio *bio)
Ming Leib6371082021-11-12 20:47:15 +08002672{
2673 if (blk_mq_get_hctx_type(bio->bi_opf) != rq->mq_hctx->type)
2674 return false;
2675
2676 if (op_is_flush(rq->cmd_flags) != op_is_flush(bio->bi_opf))
2677 return false;
2678
2679 return true;
2680}
2681
Jens Axboe71539712021-11-03 05:52:45 -06002682static inline struct request *blk_mq_get_request(struct request_queue *q,
2683 struct blk_plug *plug,
Jens Axboe900e0802021-11-03 05:47:09 -06002684 struct bio *bio,
2685 unsigned int nsegs,
2686 bool *same_queue_rq)
Jens Axboe71539712021-11-03 05:52:45 -06002687{
Ming Leib6371082021-11-12 20:47:15 +08002688 struct request *rq;
2689 bool checked = false;
2690
Jens Axboe71539712021-11-03 05:52:45 -06002691 if (plug) {
Jens Axboe71539712021-11-03 05:52:45 -06002692 rq = rq_list_peek(&plug->cached_rq);
Jens Axboe10c47872021-11-04 11:54:47 -06002693 if (rq && rq->q == q) {
Jens Axboe900e0802021-11-03 05:47:09 -06002694 if (unlikely(!submit_bio_checks(bio)))
2695 return NULL;
Ming Leib131f202021-11-11 16:51:34 +08002696 if (blk_mq_attempt_bio_merge(q, bio, nsegs,
2697 same_queue_rq))
Jens Axboe900e0802021-11-03 05:47:09 -06002698 return NULL;
Ming Leib6371082021-11-12 20:47:15 +08002699 checked = true;
2700 if (!blk_mq_can_use_cached_rq(rq, bio))
2701 goto fallback;
2702 rq->cmd_flags = bio->bi_opf;
Jens Axboe71539712021-11-03 05:52:45 -06002703 plug->cached_rq = rq_list_next(rq);
2704 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboe900e0802021-11-03 05:47:09 -06002705 rq_qos_throttle(q, bio);
Jens Axboe71539712021-11-03 05:52:45 -06002706 return rq;
2707 }
2708 }
2709
Ming Leib6371082021-11-12 20:47:15 +08002710fallback:
2711 if (unlikely(bio_queue_enter(bio)))
2712 return NULL;
Jens Axboe95febeb2021-11-15 14:23:08 -07002713 if (unlikely(!checked && !submit_bio_checks(bio)))
2714 goto out_put;
Ming Leib6371082021-11-12 20:47:15 +08002715 rq = blk_mq_get_new_requests(q, plug, bio, nsegs, same_queue_rq);
Jens Axboe95febeb2021-11-15 14:23:08 -07002716 if (rq)
2717 return rq;
2718out_put:
2719 blk_queue_exit(q);
2720 return NULL;
Jens Axboe71539712021-11-03 05:52:45 -06002721}
2722
André Almeida105663f2020-01-06 15:08:18 -03002723/**
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02002724 * blk_mq_submit_bio - Create and send a request to block device.
André Almeida105663f2020-01-06 15:08:18 -03002725 * @bio: Bio pointer.
2726 *
2727 * Builds up a request structure from @q and @bio and send to the device. The
2728 * request may not be queued directly to hardware if:
2729 * * This request can be merged with another one
2730 * * We want to place request at plug queue for possible future merging
2731 * * There is an IO scheduler active at this queue
2732 *
2733 * It will not queue the request if there is an error with the bio, or at the
2734 * request creation.
André Almeida105663f2020-01-06 15:08:18 -03002735 */
Christoph Hellwig3e087732021-10-12 13:12:24 +02002736void blk_mq_submit_bio(struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06002737{
Pavel Begunkoved6cdde2021-10-14 15:03:30 +01002738 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002739 const int is_sync = op_is_sync(bio->bi_opf);
Jens Axboe07068d52014-05-22 10:40:51 -06002740 struct request *rq;
Shaohua Lif984df12015-05-08 10:51:32 -07002741 struct blk_plug *plug;
Jens Axboe87c037d2021-10-18 10:07:09 -06002742 bool same_queue_rq = false;
Jens Axboeabd45c12021-10-13 12:43:41 -06002743 unsigned int nr_segs = 1;
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002744 blk_status_t ret;
Jens Axboe07068d52014-05-22 10:40:51 -06002745
Jens Axboe900e0802021-11-03 05:47:09 -06002746 if (unlikely(!blk_crypto_bio_prep(&bio)))
2747 return;
2748
Jens Axboe07068d52014-05-22 10:40:51 -06002749 blk_queue_bounce(q, &bio);
Jens Axboeabd45c12021-10-13 12:43:41 -06002750 if (blk_may_split(q, bio))
2751 __blk_queue_split(q, &bio, &nr_segs);
Wen Xiongf36ea502017-05-10 08:54:11 -05002752
Dmitry Monakhove23947b2017-06-29 11:31:11 -07002753 if (!bio_integrity_prep(bio))
Jens Axboe900e0802021-11-03 05:47:09 -06002754 return;
Jens Axboe87760e52016-11-09 12:38:14 -07002755
Jens Axboe47c122e2021-10-06 06:34:11 -06002756 plug = blk_mq_plug(q, bio);
Jens Axboe900e0802021-11-03 05:47:09 -06002757 rq = blk_mq_get_request(q, plug, bio, nr_segs, &same_queue_rq);
Jens Axboe71539712021-11-03 05:52:45 -06002758 if (unlikely(!rq))
Jens Axboe900e0802021-11-03 05:47:09 -06002759 return;
Jens Axboe87760e52016-11-09 12:38:14 -07002760
Christoph Hellwige8a676d2020-12-03 17:21:36 +01002761 trace_block_getrq(bio);
Xiaoguang Wangd6f1dda2018-10-23 22:30:50 +08002762
Josef Bacikc1c80382018-07-03 11:14:59 -04002763 rq_qos_track(q, rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06002764
Bart Van Assche970d1682019-07-01 08:47:30 -07002765 blk_mq_bio_to_request(rq, bio, nr_segs);
2766
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002767 ret = blk_crypto_init_request(rq);
2768 if (ret != BLK_STS_OK) {
2769 bio->bi_status = ret;
2770 bio_endio(bio);
2771 blk_mq_free_request(rq);
Christoph Hellwig3e087732021-10-12 13:12:24 +02002772 return;
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002773 }
2774
Ming Lei2b504bd2021-11-18 23:30:41 +08002775 if (op_is_flush(bio->bi_opf)) {
2776 blk_insert_flush(rq);
Christoph Hellwigd92ca9d82021-10-19 14:25:53 +02002777 return;
Ming Lei2b504bd2021-11-18 23:30:41 +08002778 }
Christoph Hellwigd92ca9d82021-10-19 14:25:53 +02002779
2780 if (plug && (q->nr_hw_queues == 1 ||
2781 blk_mq_is_shared_tags(rq->mq_hctx->flags) ||
2782 q->mq_ops->commit_rqs || !blk_queue_nonrot(q))) {
Jens Axboeb2c5d162018-11-29 10:03:42 -07002783 /*
2784 * Use plugging if we have a ->commit_rqs() hook as well, as
2785 * we know the driver uses bd->last in a smart fashion.
Ming Lei3154df22019-09-27 15:24:31 +08002786 *
2787 * Use normal plugging if this disk is slow HDD, as sequential
2788 * IO may benefit a lot from plug merging.
Jens Axboeb2c5d162018-11-29 10:03:42 -07002789 */
Jens Axboe5f0ed772018-11-23 22:04:33 -07002790 unsigned int request_count = plug->rq_count;
Shaohua Li600271d2016-11-03 17:03:54 -07002791 struct request *last = NULL;
2792
Jens Axboebc490f82021-10-18 10:12:12 -06002793 if (!request_count) {
Jeff Moyere6c44382015-05-08 10:51:30 -07002794 trace_block_plug(q);
Jens Axboebc490f82021-10-18 10:12:12 -06002795 } else if (!blk_queue_nomerges(q)) {
2796 last = rq_list_peek(&plug->mq_list);
2797 if (blk_rq_bytes(last) < BLK_PLUG_FLUSH_SIZE)
2798 last = NULL;
2799 }
Jens Axboeb094f892015-11-20 20:29:45 -07002800
Jens Axboebc490f82021-10-18 10:12:12 -06002801 if (request_count >= blk_plug_max_rq_count(plug) || last) {
Christoph Hellwiga214b942021-10-20 16:41:16 +02002802 blk_mq_flush_plug_list(plug, false);
Jeff Moyere6c44382015-05-08 10:51:30 -07002803 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002804 }
Jens Axboeb094f892015-11-20 20:29:45 -07002805
Jens Axboece5b0092018-11-27 17:13:56 -07002806 blk_add_rq_to_plug(plug, rq);
Jens Axboe2ff06822021-10-15 09:44:38 -06002807 } else if (rq->rq_flags & RQF_ELV) {
André Almeida105663f2020-01-06 15:08:18 -03002808 /* Insert the request at the IO scheduler queue */
Ming Leia12de1d2019-09-27 15:24:30 +08002809 blk_mq_sched_insert_request(rq, false, true, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002810 } else if (plug && !blk_queue_nomerges(q)) {
Jens Axboe87c037d2021-10-18 10:07:09 -06002811 struct request *next_rq = NULL;
2812
Jens Axboe320ae512013-10-24 09:20:05 +01002813 /*
2814 * We do limited plugging. If the bio can be merged, do that.
2815 * Otherwise the existing request in the plug list will be
2816 * issued. So the plug list will have one request at most
Christoph Hellwig22997222017-03-22 15:01:52 -04002817 * The plug list might get flushed before this. If that happens,
2818 * the plug list is empty, and same_queue_rq is invalid.
Jens Axboe320ae512013-10-24 09:20:05 +01002819 */
Jens Axboe4711b572018-11-27 17:07:17 -07002820 if (same_queue_rq) {
Jens Axboebc490f82021-10-18 10:12:12 -06002821 next_rq = rq_list_pop(&plug->mq_list);
Jens Axboe4711b572018-11-27 17:07:17 -07002822 plug->rq_count--;
2823 }
Jens Axboece5b0092018-11-27 17:13:56 -07002824 blk_add_rq_to_plug(plug, rq);
Yufen Yuff3b74b2019-03-26 21:19:25 +08002825 trace_block_plug(q);
Christoph Hellwig22997222017-03-22 15:01:52 -04002826
Jens Axboe87c037d2021-10-18 10:07:09 -06002827 if (next_rq) {
Yufen Yuff3b74b2019-03-26 21:19:25 +08002828 trace_block_unplug(q, 1, true);
Jens Axboe87c037d2021-10-18 10:07:09 -06002829 blk_mq_try_issue_directly(next_rq->mq_hctx, next_rq);
Ming Leidad7a3b2017-06-06 23:21:59 +08002830 }
Ming Leia12de1d2019-09-27 15:24:30 +08002831 } else if ((q->nr_hw_queues > 1 && is_sync) ||
Christoph Hellwig0f38d762021-10-12 12:40:45 +02002832 !rq->mq_hctx->dispatch_busy) {
André Almeida105663f2020-01-06 15:08:18 -03002833 /*
2834 * There is no scheduler and we can try to send directly
2835 * to the hardware.
2836 */
Christoph Hellwig3e087732021-10-12 13:12:24 +02002837 blk_mq_try_issue_directly(rq->mq_hctx, rq);
Ming Leiab42f352017-05-26 19:53:19 +08002838 } else {
André Almeida105663f2020-01-06 15:08:18 -03002839 /* Default case. */
huhai8fa9f552018-05-16 08:21:21 -06002840 blk_mq_sched_insert_request(rq, false, true, true);
Ming Leiab42f352017-05-26 19:53:19 +08002841 }
Jens Axboe320ae512013-10-24 09:20:05 +01002842}
2843
Christoph Hellwig06c8c692021-11-17 07:13:58 +01002844/**
2845 * blk_cloned_rq_check_limits - Helper function to check a cloned request
2846 * for the new queue limits
2847 * @q: the queue
2848 * @rq: the request being checked
2849 *
2850 * Description:
2851 * @rq may have been made based on weaker limitations of upper-level queues
2852 * in request stacking drivers, and it may violate the limitation of @q.
2853 * Since the block layer and the underlying device driver trust @rq
2854 * after it is inserted to @q, it should be checked against @q before
2855 * the insertion using this generic function.
2856 *
2857 * Request stacking drivers like request-based dm may change the queue
2858 * limits when retrying requests on other queues. Those requests need
2859 * to be checked against the new queue limits again during dispatch.
2860 */
2861static blk_status_t blk_cloned_rq_check_limits(struct request_queue *q,
2862 struct request *rq)
2863{
2864 unsigned int max_sectors = blk_queue_get_max_sectors(q, req_op(rq));
2865
2866 if (blk_rq_sectors(rq) > max_sectors) {
2867 /*
2868 * SCSI device does not have a good way to return if
2869 * Write Same/Zero is actually supported. If a device rejects
2870 * a non-read/write command (discard, write same,etc.) the
2871 * low-level device driver will set the relevant queue limit to
2872 * 0 to prevent blk-lib from issuing more of the offending
2873 * operations. Commands queued prior to the queue limit being
2874 * reset need to be completed with BLK_STS_NOTSUPP to avoid I/O
2875 * errors being propagated to upper layers.
2876 */
2877 if (max_sectors == 0)
2878 return BLK_STS_NOTSUPP;
2879
2880 printk(KERN_ERR "%s: over max size limit. (%u > %u)\n",
2881 __func__, blk_rq_sectors(rq), max_sectors);
2882 return BLK_STS_IOERR;
2883 }
2884
2885 /*
2886 * The queue settings related to segment counting may differ from the
2887 * original queue.
2888 */
2889 rq->nr_phys_segments = blk_recalc_rq_segments(rq);
2890 if (rq->nr_phys_segments > queue_max_segments(q)) {
2891 printk(KERN_ERR "%s: over max segments limit. (%hu > %hu)\n",
2892 __func__, rq->nr_phys_segments, queue_max_segments(q));
2893 return BLK_STS_IOERR;
2894 }
2895
2896 return BLK_STS_OK;
2897}
2898
2899/**
2900 * blk_insert_cloned_request - Helper for stacking drivers to submit a request
2901 * @q: the queue to submit the request
2902 * @rq: the request being queued
2903 */
2904blk_status_t blk_insert_cloned_request(struct request_queue *q, struct request *rq)
2905{
2906 blk_status_t ret;
2907
2908 ret = blk_cloned_rq_check_limits(q, rq);
2909 if (ret != BLK_STS_OK)
2910 return ret;
2911
2912 if (rq->rq_disk &&
2913 should_fail_request(rq->rq_disk->part0, blk_rq_bytes(rq)))
2914 return BLK_STS_IOERR;
2915
2916 if (blk_crypto_insert_cloned_request(rq))
2917 return BLK_STS_IOERR;
2918
2919 blk_account_io_start(rq);
2920
2921 /*
2922 * Since we have a scheduler attached on the top device,
2923 * bypass a potential scheduler on the bottom device for
2924 * insert.
2925 */
2926 return blk_mq_request_issue_directly(rq, true);
2927}
2928EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
2929
2930/**
2931 * blk_rq_unprep_clone - Helper function to free all bios in a cloned request
2932 * @rq: the clone request to be cleaned up
2933 *
2934 * Description:
2935 * Free all bios in @rq for a cloned request.
2936 */
2937void blk_rq_unprep_clone(struct request *rq)
2938{
2939 struct bio *bio;
2940
2941 while ((bio = rq->bio) != NULL) {
2942 rq->bio = bio->bi_next;
2943
2944 bio_put(bio);
2945 }
2946}
2947EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2948
2949/**
2950 * blk_rq_prep_clone - Helper function to setup clone request
2951 * @rq: the request to be setup
2952 * @rq_src: original request to be cloned
2953 * @bs: bio_set that bios for clone are allocated from
2954 * @gfp_mask: memory allocation mask for bio
2955 * @bio_ctr: setup function to be called for each clone bio.
2956 * Returns %0 for success, non %0 for failure.
2957 * @data: private data to be passed to @bio_ctr
2958 *
2959 * Description:
2960 * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq.
2961 * Also, pages which the original bios are pointing to are not copied
2962 * and the cloned bios just point same pages.
2963 * So cloned bios must be completed before original bios, which means
2964 * the caller must complete @rq before @rq_src.
2965 */
2966int blk_rq_prep_clone(struct request *rq, struct request *rq_src,
2967 struct bio_set *bs, gfp_t gfp_mask,
2968 int (*bio_ctr)(struct bio *, struct bio *, void *),
2969 void *data)
2970{
2971 struct bio *bio, *bio_src;
2972
2973 if (!bs)
2974 bs = &fs_bio_set;
2975
2976 __rq_for_each_bio(bio_src, rq_src) {
2977 bio = bio_clone_fast(bio_src, gfp_mask, bs);
2978 if (!bio)
2979 goto free_and_out;
2980
2981 if (bio_ctr && bio_ctr(bio, bio_src, data))
2982 goto free_and_out;
2983
2984 if (rq->bio) {
2985 rq->biotail->bi_next = bio;
2986 rq->biotail = bio;
2987 } else {
2988 rq->bio = rq->biotail = bio;
2989 }
2990 bio = NULL;
2991 }
2992
2993 /* Copy attributes of the original request to the clone request. */
2994 rq->__sector = blk_rq_pos(rq_src);
2995 rq->__data_len = blk_rq_bytes(rq_src);
2996 if (rq_src->rq_flags & RQF_SPECIAL_PAYLOAD) {
2997 rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
2998 rq->special_vec = rq_src->special_vec;
2999 }
3000 rq->nr_phys_segments = rq_src->nr_phys_segments;
3001 rq->ioprio = rq_src->ioprio;
3002
3003 if (rq->bio && blk_crypto_rq_bio_prep(rq, rq->bio, gfp_mask) < 0)
3004 goto free_and_out;
3005
3006 return 0;
3007
3008free_and_out:
3009 if (bio)
3010 bio_put(bio);
3011 blk_rq_unprep_clone(rq);
3012
3013 return -ENOMEM;
3014}
3015EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
3016
Ming Leibd631412021-05-11 23:22:35 +08003017static size_t order_to_size(unsigned int order)
3018{
3019 return (size_t)PAGE_SIZE << order;
3020}
3021
3022/* called before freeing request pool in @tags */
John Garryf32e4ea2021-10-05 18:23:32 +08003023static void blk_mq_clear_rq_mapping(struct blk_mq_tags *drv_tags,
3024 struct blk_mq_tags *tags)
Ming Leibd631412021-05-11 23:22:35 +08003025{
Ming Leibd631412021-05-11 23:22:35 +08003026 struct page *page;
3027 unsigned long flags;
3028
John Garry4f245d52021-10-05 18:23:33 +08003029 /* There is no need to clear a driver tags own mapping */
3030 if (drv_tags == tags)
3031 return;
3032
Ming Leibd631412021-05-11 23:22:35 +08003033 list_for_each_entry(page, &tags->page_list, lru) {
3034 unsigned long start = (unsigned long)page_address(page);
3035 unsigned long end = start + order_to_size(page->private);
3036 int i;
3037
John Garryf32e4ea2021-10-05 18:23:32 +08003038 for (i = 0; i < drv_tags->nr_tags; i++) {
Ming Leibd631412021-05-11 23:22:35 +08003039 struct request *rq = drv_tags->rqs[i];
3040 unsigned long rq_addr = (unsigned long)rq;
3041
3042 if (rq_addr >= start && rq_addr < end) {
3043 WARN_ON_ONCE(refcount_read(&rq->ref) != 0);
3044 cmpxchg(&drv_tags->rqs[i], rq, NULL);
3045 }
3046 }
3047 }
3048
3049 /*
3050 * Wait until all pending iteration is done.
3051 *
3052 * Request reference is cleared and it is guaranteed to be observed
3053 * after the ->lock is released.
3054 */
3055 spin_lock_irqsave(&drv_tags->lock, flags);
3056 spin_unlock_irqrestore(&drv_tags->lock, flags);
3057}
3058
Jens Axboecc71a6f2017-01-11 14:29:56 -07003059void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
3060 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01003061{
John Garryf32e4ea2021-10-05 18:23:32 +08003062 struct blk_mq_tags *drv_tags;
Jens Axboe320ae512013-10-24 09:20:05 +01003063 struct page *page;
3064
John Garry079a2e32021-10-05 18:23:39 +08003065 if (blk_mq_is_shared_tags(set->flags))
3066 drv_tags = set->shared_tags;
John Garrye155b0c2021-10-05 18:23:37 +08003067 else
3068 drv_tags = set->tags[hctx_idx];
John Garryf32e4ea2021-10-05 18:23:32 +08003069
John Garry65de57b2021-10-05 18:23:26 +08003070 if (tags->static_rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06003071 int i;
3072
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003073 for (i = 0; i < tags->nr_tags; i++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07003074 struct request *rq = tags->static_rqs[i];
3075
3076 if (!rq)
Christoph Hellwige9b267d2014-04-15 13:59:10 -06003077 continue;
Christoph Hellwigd6296d392017-05-01 10:19:08 -06003078 set->ops->exit_request(set, rq, hctx_idx);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07003079 tags->static_rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06003080 }
3081 }
3082
John Garryf32e4ea2021-10-05 18:23:32 +08003083 blk_mq_clear_rq_mapping(drv_tags, tags);
Ming Leibd631412021-05-11 23:22:35 +08003084
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003085 while (!list_empty(&tags->page_list)) {
3086 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07003087 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01003088 /*
3089 * Remove kmemleak object previously allocated in
Raul E Rangel273938b2019-05-02 13:48:11 -06003090 * blk_mq_alloc_rqs().
Catalin Marinasf75782e2015-09-14 18:16:02 +01003091 */
3092 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01003093 __free_pages(page, page->private);
3094 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07003095}
Jens Axboe320ae512013-10-24 09:20:05 +01003096
John Garrye155b0c2021-10-05 18:23:37 +08003097void blk_mq_free_rq_map(struct blk_mq_tags *tags)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003098{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003099 kfree(tags->rqs);
Jens Axboecc71a6f2017-01-11 14:29:56 -07003100 tags->rqs = NULL;
Jens Axboe2af8cbe2017-01-13 14:39:30 -07003101 kfree(tags->static_rqs);
3102 tags->static_rqs = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01003103
John Garrye155b0c2021-10-05 18:23:37 +08003104 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01003105}
3106
John Garry63064be2021-10-05 18:23:35 +08003107static struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
3108 unsigned int hctx_idx,
3109 unsigned int nr_tags,
John Garrye155b0c2021-10-05 18:23:37 +08003110 unsigned int reserved_tags)
Jens Axboe320ae512013-10-24 09:20:05 +01003111{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003112 struct blk_mq_tags *tags;
Shaohua Li59f082e2017-02-01 09:53:14 -08003113 int node;
Jens Axboe320ae512013-10-24 09:20:05 +01003114
Dongli Zhang7d76f852019-02-27 21:35:01 +08003115 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08003116 if (node == NUMA_NO_NODE)
3117 node = set->numa_node;
3118
John Garrye155b0c2021-10-05 18:23:37 +08003119 tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
3120 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003121 if (!tags)
3122 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01003123
Kees Cook590b5b72018-06-12 14:04:20 -07003124 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02003125 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
Shaohua Li59f082e2017-02-01 09:53:14 -08003126 node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003127 if (!tags->rqs) {
John Garrye155b0c2021-10-05 18:23:37 +08003128 blk_mq_free_tags(tags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003129 return NULL;
3130 }
Jens Axboe320ae512013-10-24 09:20:05 +01003131
Kees Cook590b5b72018-06-12 14:04:20 -07003132 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
3133 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
3134 node);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07003135 if (!tags->static_rqs) {
3136 kfree(tags->rqs);
John Garrye155b0c2021-10-05 18:23:37 +08003137 blk_mq_free_tags(tags);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07003138 return NULL;
3139 }
3140
Jens Axboecc71a6f2017-01-11 14:29:56 -07003141 return tags;
3142}
3143
Tejun Heo1d9bd512018-01-09 08:29:48 -08003144static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
3145 unsigned int hctx_idx, int node)
3146{
3147 int ret;
3148
3149 if (set->ops->init_request) {
3150 ret = set->ops->init_request(set, rq, hctx_idx, node);
3151 if (ret)
3152 return ret;
3153 }
3154
Keith Busch12f5b932018-05-29 15:52:28 +02003155 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Tejun Heo1d9bd512018-01-09 08:29:48 -08003156 return 0;
3157}
3158
John Garry63064be2021-10-05 18:23:35 +08003159static int blk_mq_alloc_rqs(struct blk_mq_tag_set *set,
3160 struct blk_mq_tags *tags,
3161 unsigned int hctx_idx, unsigned int depth)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003162{
3163 unsigned int i, j, entries_per_page, max_order = 4;
3164 size_t rq_size, left;
Shaohua Li59f082e2017-02-01 09:53:14 -08003165 int node;
3166
Dongli Zhang7d76f852019-02-27 21:35:01 +08003167 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08003168 if (node == NUMA_NO_NODE)
3169 node = set->numa_node;
Jens Axboecc71a6f2017-01-11 14:29:56 -07003170
3171 INIT_LIST_HEAD(&tags->page_list);
3172
Jens Axboe320ae512013-10-24 09:20:05 +01003173 /*
3174 * rq_size is the size of the request plus driver payload, rounded
3175 * to the cacheline size
3176 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003177 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01003178 cache_line_size());
Jens Axboecc71a6f2017-01-11 14:29:56 -07003179 left = rq_size * depth;
Jens Axboe320ae512013-10-24 09:20:05 +01003180
Jens Axboecc71a6f2017-01-11 14:29:56 -07003181 for (i = 0; i < depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01003182 int this_order = max_order;
3183 struct page *page;
3184 int to_do;
3185 void *p;
3186
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06003187 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01003188 this_order--;
3189
3190 do {
Shaohua Li59f082e2017-02-01 09:53:14 -08003191 page = alloc_pages_node(node,
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02003192 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06003193 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01003194 if (page)
3195 break;
3196 if (!this_order--)
3197 break;
3198 if (order_to_size(this_order) < rq_size)
3199 break;
3200 } while (1);
3201
3202 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003203 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01003204
3205 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003206 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01003207
3208 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01003209 /*
3210 * Allow kmemleak to scan these pages as they contain pointers
3211 * to additional allocations like via ops->init_request().
3212 */
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02003213 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
Jens Axboe320ae512013-10-24 09:20:05 +01003214 entries_per_page = order_to_size(this_order) / rq_size;
Jens Axboecc71a6f2017-01-11 14:29:56 -07003215 to_do = min(entries_per_page, depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01003216 left -= to_do * rq_size;
3217 for (j = 0; j < to_do; j++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07003218 struct request *rq = p;
3219
3220 tags->static_rqs[i] = rq;
Tejun Heo1d9bd512018-01-09 08:29:48 -08003221 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
3222 tags->static_rqs[i] = NULL;
3223 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06003224 }
3225
Jens Axboe320ae512013-10-24 09:20:05 +01003226 p += rq_size;
3227 i++;
3228 }
3229 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07003230 return 0;
Jens Axboe320ae512013-10-24 09:20:05 +01003231
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003232fail:
Jens Axboecc71a6f2017-01-11 14:29:56 -07003233 blk_mq_free_rqs(set, tags, hctx_idx);
3234 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01003235}
3236
Ming Leibf0beec2020-05-29 15:53:15 +02003237struct rq_iter_data {
3238 struct blk_mq_hw_ctx *hctx;
3239 bool has_rq;
3240};
3241
3242static bool blk_mq_has_request(struct request *rq, void *data, bool reserved)
3243{
3244 struct rq_iter_data *iter_data = data;
3245
3246 if (rq->mq_hctx != iter_data->hctx)
3247 return true;
3248 iter_data->has_rq = true;
3249 return false;
3250}
3251
3252static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
3253{
3254 struct blk_mq_tags *tags = hctx->sched_tags ?
3255 hctx->sched_tags : hctx->tags;
3256 struct rq_iter_data data = {
3257 .hctx = hctx,
3258 };
3259
3260 blk_mq_all_tag_iter(tags, blk_mq_has_request, &data);
3261 return data.has_rq;
3262}
3263
3264static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu,
3265 struct blk_mq_hw_ctx *hctx)
3266{
3267 if (cpumask_next_and(-1, hctx->cpumask, cpu_online_mask) != cpu)
3268 return false;
3269 if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids)
3270 return false;
3271 return true;
3272}
3273
3274static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
3275{
3276 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
3277 struct blk_mq_hw_ctx, cpuhp_online);
3278
3279 if (!cpumask_test_cpu(cpu, hctx->cpumask) ||
3280 !blk_mq_last_cpu_in_hctx(cpu, hctx))
3281 return 0;
3282
3283 /*
3284 * Prevent new request from being allocated on the current hctx.
3285 *
3286 * The smp_mb__after_atomic() Pairs with the implied barrier in
3287 * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is
3288 * seen once we return from the tag allocator.
3289 */
3290 set_bit(BLK_MQ_S_INACTIVE, &hctx->state);
3291 smp_mb__after_atomic();
3292
3293 /*
3294 * Try to grab a reference to the queue and wait for any outstanding
3295 * requests. If we could not grab a reference the queue has been
3296 * frozen and there are no requests.
3297 */
3298 if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) {
3299 while (blk_mq_hctx_has_requests(hctx))
3300 msleep(5);
3301 percpu_ref_put(&hctx->queue->q_usage_counter);
3302 }
3303
3304 return 0;
3305}
3306
3307static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node)
3308{
3309 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
3310 struct blk_mq_hw_ctx, cpuhp_online);
3311
3312 if (cpumask_test_cpu(cpu, hctx->cpumask))
3313 clear_bit(BLK_MQ_S_INACTIVE, &hctx->state);
3314 return 0;
3315}
3316
Jens Axboee57690f2016-08-24 15:34:35 -06003317/*
3318 * 'cpu' is going away. splice any existing rq_list entries from this
3319 * software queue to the hw queue dispatch list, and ensure that it
3320 * gets run.
3321 */
Thomas Gleixner9467f852016-09-22 08:05:17 -06003322static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
Jens Axboe484b4062014-05-21 14:01:15 -06003323{
Thomas Gleixner9467f852016-09-22 08:05:17 -06003324 struct blk_mq_hw_ctx *hctx;
Jens Axboe484b4062014-05-21 14:01:15 -06003325 struct blk_mq_ctx *ctx;
3326 LIST_HEAD(tmp);
Ming Leic16d6b52018-12-17 08:44:05 -07003327 enum hctx_type type;
Jens Axboe484b4062014-05-21 14:01:15 -06003328
Thomas Gleixner9467f852016-09-22 08:05:17 -06003329 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02003330 if (!cpumask_test_cpu(cpu, hctx->cpumask))
3331 return 0;
3332
Jens Axboee57690f2016-08-24 15:34:35 -06003333 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Ming Leic16d6b52018-12-17 08:44:05 -07003334 type = hctx->type;
Jens Axboe484b4062014-05-21 14:01:15 -06003335
3336 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07003337 if (!list_empty(&ctx->rq_lists[type])) {
3338 list_splice_init(&ctx->rq_lists[type], &tmp);
Jens Axboe484b4062014-05-21 14:01:15 -06003339 blk_mq_hctx_clear_pending(hctx, ctx);
3340 }
3341 spin_unlock(&ctx->lock);
3342
3343 if (list_empty(&tmp))
Thomas Gleixner9467f852016-09-22 08:05:17 -06003344 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06003345
Jens Axboee57690f2016-08-24 15:34:35 -06003346 spin_lock(&hctx->lock);
3347 list_splice_tail_init(&tmp, &hctx->dispatch);
3348 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06003349
3350 blk_mq_run_hw_queue(hctx, true);
Thomas Gleixner9467f852016-09-22 08:05:17 -06003351 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06003352}
3353
Thomas Gleixner9467f852016-09-22 08:05:17 -06003354static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
Jens Axboe484b4062014-05-21 14:01:15 -06003355{
Ming Leibf0beec2020-05-29 15:53:15 +02003356 if (!(hctx->flags & BLK_MQ_F_STACKING))
3357 cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
3358 &hctx->cpuhp_online);
Thomas Gleixner9467f852016-09-22 08:05:17 -06003359 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
3360 &hctx->cpuhp_dead);
Jens Axboe484b4062014-05-21 14:01:15 -06003361}
3362
Ming Lei364b6182021-05-11 23:22:36 +08003363/*
3364 * Before freeing hw queue, clearing the flush request reference in
3365 * tags->rqs[] for avoiding potential UAF.
3366 */
3367static void blk_mq_clear_flush_rq_mapping(struct blk_mq_tags *tags,
3368 unsigned int queue_depth, struct request *flush_rq)
3369{
3370 int i;
3371 unsigned long flags;
3372
3373 /* The hw queue may not be mapped yet */
3374 if (!tags)
3375 return;
3376
3377 WARN_ON_ONCE(refcount_read(&flush_rq->ref) != 0);
3378
3379 for (i = 0; i < queue_depth; i++)
3380 cmpxchg(&tags->rqs[i], flush_rq, NULL);
3381
3382 /*
3383 * Wait until all pending iteration is done.
3384 *
3385 * Request reference is cleared and it is guaranteed to be observed
3386 * after the ->lock is released.
3387 */
3388 spin_lock_irqsave(&tags->lock, flags);
3389 spin_unlock_irqrestore(&tags->lock, flags);
3390}
3391
Ming Leic3b4afc2015-06-04 22:25:04 +08003392/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08003393static void blk_mq_exit_hctx(struct request_queue *q,
3394 struct blk_mq_tag_set *set,
3395 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
3396{
Ming Lei364b6182021-05-11 23:22:36 +08003397 struct request *flush_rq = hctx->fq->flush_rq;
3398
Ming Lei8ab0b7d2018-01-09 21:28:29 +08003399 if (blk_mq_hw_queue_mapped(hctx))
3400 blk_mq_tag_idle(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08003401
Ming Lei364b6182021-05-11 23:22:36 +08003402 blk_mq_clear_flush_rq_mapping(set->tags[hctx_idx],
3403 set->queue_depth, flush_rq);
Ming Leif70ced02014-09-25 23:23:47 +08003404 if (set->ops->exit_request)
Ming Lei364b6182021-05-11 23:22:36 +08003405 set->ops->exit_request(set, flush_rq, hctx_idx);
Ming Leif70ced02014-09-25 23:23:47 +08003406
Ming Lei08e98fc2014-09-25 23:23:38 +08003407 if (set->ops->exit_hctx)
3408 set->ops->exit_hctx(hctx, hctx_idx);
3409
Thomas Gleixner9467f852016-09-22 08:05:17 -06003410 blk_mq_remove_cpuhp(hctx);
Ming Lei2f8f1332019-04-30 09:52:27 +08003411
3412 spin_lock(&q->unused_hctx_lock);
3413 list_add(&hctx->hctx_list, &q->unused_hctx_list);
3414 spin_unlock(&q->unused_hctx_lock);
Ming Lei08e98fc2014-09-25 23:23:38 +08003415}
3416
Ming Lei624dbe42014-05-27 23:35:13 +08003417static void blk_mq_exit_hw_queues(struct request_queue *q,
3418 struct blk_mq_tag_set *set, int nr_queue)
3419{
3420 struct blk_mq_hw_ctx *hctx;
3421 unsigned int i;
3422
3423 queue_for_each_hw_ctx(q, hctx, i) {
3424 if (i == nr_queue)
3425 break;
Jianchao Wang477e19d2018-10-12 18:07:25 +08003426 blk_mq_debugfs_unregister_hctx(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08003427 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08003428 }
Ming Lei624dbe42014-05-27 23:35:13 +08003429}
3430
Ming Lei7c6c5b72019-04-30 09:52:26 +08003431static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
3432{
3433 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
3434
3435 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
3436 __alignof__(struct blk_mq_hw_ctx)) !=
3437 sizeof(struct blk_mq_hw_ctx));
3438
3439 if (tag_set->flags & BLK_MQ_F_BLOCKING)
3440 hw_ctx_size += sizeof(struct srcu_struct);
3441
3442 return hw_ctx_size;
3443}
3444
Ming Lei08e98fc2014-09-25 23:23:38 +08003445static int blk_mq_init_hctx(struct request_queue *q,
3446 struct blk_mq_tag_set *set,
3447 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
3448{
Ming Lei7c6c5b72019-04-30 09:52:26 +08003449 hctx->queue_num = hctx_idx;
Ming Lei08e98fc2014-09-25 23:23:38 +08003450
Ming Leibf0beec2020-05-29 15:53:15 +02003451 if (!(hctx->flags & BLK_MQ_F_STACKING))
3452 cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
3453 &hctx->cpuhp_online);
Ming Lei7c6c5b72019-04-30 09:52:26 +08003454 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
3455
3456 hctx->tags = set->tags[hctx_idx];
3457
3458 if (set->ops->init_hctx &&
3459 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
3460 goto unregister_cpu_notifier;
3461
3462 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
3463 hctx->numa_node))
3464 goto exit_hctx;
3465 return 0;
3466
3467 exit_hctx:
3468 if (set->ops->exit_hctx)
3469 set->ops->exit_hctx(hctx, hctx_idx);
3470 unregister_cpu_notifier:
3471 blk_mq_remove_cpuhp(hctx);
3472 return -1;
3473}
3474
3475static struct blk_mq_hw_ctx *
3476blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
3477 int node)
3478{
3479 struct blk_mq_hw_ctx *hctx;
3480 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
3481
3482 hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node);
3483 if (!hctx)
3484 goto fail_alloc_hctx;
3485
3486 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
3487 goto free_hctx;
3488
3489 atomic_set(&hctx->nr_active, 0);
Ming Lei08e98fc2014-09-25 23:23:38 +08003490 if (node == NUMA_NO_NODE)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003491 node = set->numa_node;
3492 hctx->numa_node = node;
Ming Lei08e98fc2014-09-25 23:23:38 +08003493
Jens Axboe9f993732017-04-10 09:54:54 -06003494 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08003495 spin_lock_init(&hctx->lock);
3496 INIT_LIST_HEAD(&hctx->dispatch);
3497 hctx->queue = q;
Ming Lei51db1c32020-08-19 23:20:19 +08003498 hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08003499
Ming Lei2f8f1332019-04-30 09:52:27 +08003500 INIT_LIST_HEAD(&hctx->hctx_list);
3501
Ming Lei08e98fc2014-09-25 23:23:38 +08003502 /*
3503 * Allocate space for all possible cpus to avoid allocation at
3504 * runtime
3505 */
Johannes Thumshirnd904bfa2017-11-15 17:32:33 -08003506 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
Ming Lei7c6c5b72019-04-30 09:52:26 +08003507 gfp, node);
Ming Lei08e98fc2014-09-25 23:23:38 +08003508 if (!hctx->ctxs)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003509 goto free_cpumask;
Ming Lei08e98fc2014-09-25 23:23:38 +08003510
Jianchao Wang5b202852018-10-12 18:07:26 +08003511 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
Ming Leic548e622021-01-22 10:33:08 +08003512 gfp, node, false, false))
Ming Lei08e98fc2014-09-25 23:23:38 +08003513 goto free_ctxs;
Ming Lei08e98fc2014-09-25 23:23:38 +08003514 hctx->nr_ctx = 0;
3515
Ming Lei5815839b2018-06-25 19:31:47 +08003516 spin_lock_init(&hctx->dispatch_wait_lock);
Jens Axboeeb619fd2017-11-09 08:32:43 -07003517 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
3518 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
3519
Guoqing Jiang754a1572020-03-09 22:41:37 +01003520 hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp);
Ming Leif70ced02014-09-25 23:23:47 +08003521 if (!hctx->fq)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003522 goto free_bitmap;
Ming Leif70ced02014-09-25 23:23:47 +08003523
Bart Van Assche6a83e742016-11-02 10:09:51 -06003524 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -08003525 init_srcu_struct(hctx->srcu);
Ming Lei7c6c5b72019-04-30 09:52:26 +08003526 blk_mq_hctx_kobj_init(hctx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06003527
Ming Lei7c6c5b72019-04-30 09:52:26 +08003528 return hctx;
Ming Lei08e98fc2014-09-25 23:23:38 +08003529
3530 free_bitmap:
Omar Sandoval88459642016-09-17 08:38:44 -06003531 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08003532 free_ctxs:
3533 kfree(hctx->ctxs);
Ming Lei7c6c5b72019-04-30 09:52:26 +08003534 free_cpumask:
3535 free_cpumask_var(hctx->cpumask);
3536 free_hctx:
3537 kfree(hctx);
3538 fail_alloc_hctx:
3539 return NULL;
Ming Lei08e98fc2014-09-25 23:23:38 +08003540}
3541
Jens Axboe320ae512013-10-24 09:20:05 +01003542static void blk_mq_init_cpu_queues(struct request_queue *q,
3543 unsigned int nr_hw_queues)
3544{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003545 struct blk_mq_tag_set *set = q->tag_set;
3546 unsigned int i, j;
Jens Axboe320ae512013-10-24 09:20:05 +01003547
3548 for_each_possible_cpu(i) {
3549 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
3550 struct blk_mq_hw_ctx *hctx;
Ming Leic16d6b52018-12-17 08:44:05 -07003551 int k;
Jens Axboe320ae512013-10-24 09:20:05 +01003552
Jens Axboe320ae512013-10-24 09:20:05 +01003553 __ctx->cpu = i;
3554 spin_lock_init(&__ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07003555 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
3556 INIT_LIST_HEAD(&__ctx->rq_lists[k]);
3557
Jens Axboe320ae512013-10-24 09:20:05 +01003558 __ctx->queue = q;
3559
Jens Axboe320ae512013-10-24 09:20:05 +01003560 /*
3561 * Set local node, IFF we have more than one hw queue. If
3562 * not, we remain on the home node of the device
3563 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06003564 for (j = 0; j < set->nr_maps; j++) {
3565 hctx = blk_mq_map_queue_type(q, j, i);
3566 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
Xianting Tian576e85c2020-10-19 16:20:47 +08003567 hctx->numa_node = cpu_to_node(i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003568 }
Jens Axboe320ae512013-10-24 09:20:05 +01003569 }
3570}
3571
John Garry63064be2021-10-05 18:23:35 +08003572struct blk_mq_tags *blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
3573 unsigned int hctx_idx,
3574 unsigned int depth)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003575{
John Garry63064be2021-10-05 18:23:35 +08003576 struct blk_mq_tags *tags;
3577 int ret;
Jens Axboecc71a6f2017-01-11 14:29:56 -07003578
John Garrye155b0c2021-10-05 18:23:37 +08003579 tags = blk_mq_alloc_rq_map(set, hctx_idx, depth, set->reserved_tags);
John Garry63064be2021-10-05 18:23:35 +08003580 if (!tags)
3581 return NULL;
Jens Axboecc71a6f2017-01-11 14:29:56 -07003582
John Garry63064be2021-10-05 18:23:35 +08003583 ret = blk_mq_alloc_rqs(set, tags, hctx_idx, depth);
3584 if (ret) {
John Garrye155b0c2021-10-05 18:23:37 +08003585 blk_mq_free_rq_map(tags);
John Garry63064be2021-10-05 18:23:35 +08003586 return NULL;
3587 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07003588
John Garry63064be2021-10-05 18:23:35 +08003589 return tags;
Jens Axboecc71a6f2017-01-11 14:29:56 -07003590}
3591
John Garry63064be2021-10-05 18:23:35 +08003592static bool __blk_mq_alloc_map_and_rqs(struct blk_mq_tag_set *set,
3593 int hctx_idx)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003594{
John Garry079a2e32021-10-05 18:23:39 +08003595 if (blk_mq_is_shared_tags(set->flags)) {
3596 set->tags[hctx_idx] = set->shared_tags;
John Garry1c0706a2020-08-19 23:20:22 +08003597
John Garrye155b0c2021-10-05 18:23:37 +08003598 return true;
Jens Axboebd166ef2017-01-17 06:03:22 -07003599 }
John Garrye155b0c2021-10-05 18:23:37 +08003600
John Garry63064be2021-10-05 18:23:35 +08003601 set->tags[hctx_idx] = blk_mq_alloc_map_and_rqs(set, hctx_idx,
3602 set->queue_depth);
3603
3604 return set->tags[hctx_idx];
Jens Axboecc71a6f2017-01-11 14:29:56 -07003605}
3606
John Garry645db342021-10-05 18:23:36 +08003607void blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
3608 struct blk_mq_tags *tags,
3609 unsigned int hctx_idx)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003610{
John Garry645db342021-10-05 18:23:36 +08003611 if (tags) {
3612 blk_mq_free_rqs(set, tags, hctx_idx);
John Garrye155b0c2021-10-05 18:23:37 +08003613 blk_mq_free_rq_map(tags);
Jens Axboecc71a6f2017-01-11 14:29:56 -07003614 }
3615}
3616
John Garrye155b0c2021-10-05 18:23:37 +08003617static void __blk_mq_free_map_and_rqs(struct blk_mq_tag_set *set,
3618 unsigned int hctx_idx)
3619{
John Garry079a2e32021-10-05 18:23:39 +08003620 if (!blk_mq_is_shared_tags(set->flags))
John Garrye155b0c2021-10-05 18:23:37 +08003621 blk_mq_free_map_and_rqs(set, set->tags[hctx_idx], hctx_idx);
3622
3623 set->tags[hctx_idx] = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01003624}
3625
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003626static void blk_mq_map_swqueue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01003627{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003628 unsigned int i, j, hctx_idx;
Jens Axboe320ae512013-10-24 09:20:05 +01003629 struct blk_mq_hw_ctx *hctx;
3630 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08003631 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01003632
3633 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06003634 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01003635 hctx->nr_ctx = 0;
huhaid416c922018-05-18 08:32:30 -06003636 hctx->dispatch_from = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01003637 }
3638
3639 /*
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003640 * Map software to hardware queues.
Ming Lei4412efe2018-04-25 04:01:44 +08003641 *
3642 * If the cpu isn't present, the cpu is mapped to first hctx.
Jens Axboe320ae512013-10-24 09:20:05 +01003643 */
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08003644 for_each_possible_cpu(i) {
Ming Lei4412efe2018-04-25 04:01:44 +08003645
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01003646 ctx = per_cpu_ptr(q->queue_ctx, i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003647 for (j = 0; j < set->nr_maps; j++) {
Jianchao Wangbb94aea2019-01-24 18:25:33 +08003648 if (!set->map[j].nr_queues) {
3649 ctx->hctxs[j] = blk_mq_map_queue_type(q,
3650 HCTX_TYPE_DEFAULT, i);
Ming Leie5edd5f2018-12-18 01:28:56 +08003651 continue;
Jianchao Wangbb94aea2019-01-24 18:25:33 +08003652 }
Ming Leifd689872020-05-07 21:04:08 +08003653 hctx_idx = set->map[j].mq_map[i];
3654 /* unmapped hw queue can be remapped after CPU topo changed */
3655 if (!set->tags[hctx_idx] &&
John Garry63064be2021-10-05 18:23:35 +08003656 !__blk_mq_alloc_map_and_rqs(set, hctx_idx)) {
Ming Leifd689872020-05-07 21:04:08 +08003657 /*
3658 * If tags initialization fail for some hctx,
3659 * that hctx won't be brought online. In this
3660 * case, remap the current ctx to hctx[0] which
3661 * is guaranteed to always have tags allocated
3662 */
3663 set->map[j].mq_map[i] = 0;
3664 }
Ming Leie5edd5f2018-12-18 01:28:56 +08003665
Jens Axboeb3c661b2018-10-30 10:36:06 -06003666 hctx = blk_mq_map_queue_type(q, j, i);
Jianchao Wang8ccdf4a2019-01-24 18:25:32 +08003667 ctx->hctxs[j] = hctx;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003668 /*
3669 * If the CPU is already set in the mask, then we've
3670 * mapped this one already. This can happen if
3671 * devices share queues across queue maps.
3672 */
3673 if (cpumask_test_cpu(i, hctx->cpumask))
3674 continue;
3675
3676 cpumask_set_cpu(i, hctx->cpumask);
3677 hctx->type = j;
3678 ctx->index_hw[hctx->type] = hctx->nr_ctx;
3679 hctx->ctxs[hctx->nr_ctx++] = ctx;
3680
3681 /*
3682 * If the nr_ctx type overflows, we have exceeded the
3683 * amount of sw queues we can support.
3684 */
3685 BUG_ON(!hctx->nr_ctx);
3686 }
Jianchao Wangbb94aea2019-01-24 18:25:33 +08003687
3688 for (; j < HCTX_MAX_TYPES; j++)
3689 ctx->hctxs[j] = blk_mq_map_queue_type(q,
3690 HCTX_TYPE_DEFAULT, i);
Jens Axboe320ae512013-10-24 09:20:05 +01003691 }
Jens Axboe506e9312014-05-07 10:26:44 -06003692
3693 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei4412efe2018-04-25 04:01:44 +08003694 /*
3695 * If no software queues are mapped to this hardware queue,
3696 * disable it and free the request entries.
3697 */
3698 if (!hctx->nr_ctx) {
3699 /* Never unmap queue 0. We need it as a
3700 * fallback in case of a new remap fails
3701 * allocation
3702 */
John Garrye155b0c2021-10-05 18:23:37 +08003703 if (i)
3704 __blk_mq_free_map_and_rqs(set, i);
Ming Lei4412efe2018-04-25 04:01:44 +08003705
3706 hctx->tags = NULL;
3707 continue;
3708 }
Jens Axboe484b4062014-05-21 14:01:15 -06003709
Ming Lei2a34c082015-04-21 10:00:20 +08003710 hctx->tags = set->tags[i];
3711 WARN_ON(!hctx->tags);
3712
Jens Axboe484b4062014-05-21 14:01:15 -06003713 /*
Chong Yuan889fa312015-04-15 11:39:29 -06003714 * Set the map size to the number of mapped software queues.
3715 * This is more accurate and more efficient than looping
3716 * over all possibly mapped software queues.
3717 */
Omar Sandoval88459642016-09-17 08:38:44 -06003718 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
Chong Yuan889fa312015-04-15 11:39:29 -06003719
3720 /*
Jens Axboe484b4062014-05-21 14:01:15 -06003721 * Initialize batch roundrobin counts
3722 */
Ming Leif82ddf12018-04-08 17:48:10 +08003723 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06003724 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
3725 }
Jens Axboe320ae512013-10-24 09:20:05 +01003726}
3727
Jens Axboe8e8320c2017-06-20 17:56:13 -06003728/*
3729 * Caller needs to ensure that we're either frozen/quiesced, or that
3730 * the queue isn't live yet.
3731 */
Jeff Moyer2404e602015-11-03 10:40:06 -05003732static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06003733{
3734 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06003735 int i;
3736
Jeff Moyer2404e602015-11-03 10:40:06 -05003737 queue_for_each_hw_ctx(q, hctx, i) {
Yu Kuai454bb672021-07-31 14:21:30 +08003738 if (shared) {
Ming Lei51db1c32020-08-19 23:20:19 +08003739 hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
Yu Kuai454bb672021-07-31 14:21:30 +08003740 } else {
3741 blk_mq_tag_idle(hctx);
Ming Lei51db1c32020-08-19 23:20:19 +08003742 hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
Yu Kuai454bb672021-07-31 14:21:30 +08003743 }
Jeff Moyer2404e602015-11-03 10:40:06 -05003744 }
3745}
3746
Hannes Reinecke655ac302020-08-19 23:20:20 +08003747static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set,
3748 bool shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05003749{
3750 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06003751
Bart Van Assche705cda92017-04-07 11:16:49 -07003752 lockdep_assert_held(&set->tag_list_lock);
3753
Jens Axboe0d2602c2014-05-13 15:10:52 -06003754 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3755 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05003756 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003757 blk_mq_unfreeze_queue(q);
3758 }
3759}
3760
3761static void blk_mq_del_queue_tag_set(struct request_queue *q)
3762{
3763 struct blk_mq_tag_set *set = q->tag_set;
3764
Jens Axboe0d2602c2014-05-13 15:10:52 -06003765 mutex_lock(&set->tag_list_lock);
Daniel Wagner08c875c2020-07-28 15:29:51 +02003766 list_del(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05003767 if (list_is_singular(&set->tag_list)) {
3768 /* just transitioned to unshared */
Ming Lei51db1c32020-08-19 23:20:19 +08003769 set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05003770 /* update existing queue */
Hannes Reinecke655ac302020-08-19 23:20:20 +08003771 blk_mq_update_tag_set_shared(set, false);
Jeff Moyer2404e602015-11-03 10:40:06 -05003772 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06003773 mutex_unlock(&set->tag_list_lock);
Roman Pena347c7a2018-06-10 22:38:24 +02003774 INIT_LIST_HEAD(&q->tag_set_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003775}
3776
3777static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
3778 struct request_queue *q)
3779{
Jens Axboe0d2602c2014-05-13 15:10:52 -06003780 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05003781
Jens Axboeff821d22017-11-10 22:05:12 -07003782 /*
3783 * Check to see if we're transitioning to shared (from 1 to 2 queues).
3784 */
3785 if (!list_empty(&set->tag_list) &&
Ming Lei51db1c32020-08-19 23:20:19 +08003786 !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
3787 set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05003788 /* update existing queue */
Hannes Reinecke655ac302020-08-19 23:20:20 +08003789 blk_mq_update_tag_set_shared(set, true);
Jeff Moyer2404e602015-11-03 10:40:06 -05003790 }
Ming Lei51db1c32020-08-19 23:20:19 +08003791 if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
Jeff Moyer2404e602015-11-03 10:40:06 -05003792 queue_set_hctx_shared(q, true);
Daniel Wagner08c875c2020-07-28 15:29:51 +02003793 list_add_tail(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05003794
Jens Axboe0d2602c2014-05-13 15:10:52 -06003795 mutex_unlock(&set->tag_list_lock);
3796}
3797
Ming Lei1db49092018-11-20 09:44:35 +08003798/* All allocations will be freed in release handler of q->mq_kobj */
3799static int blk_mq_alloc_ctxs(struct request_queue *q)
3800{
3801 struct blk_mq_ctxs *ctxs;
3802 int cpu;
3803
3804 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
3805 if (!ctxs)
3806 return -ENOMEM;
3807
3808 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
3809 if (!ctxs->queue_ctx)
3810 goto fail;
3811
3812 for_each_possible_cpu(cpu) {
3813 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
3814 ctx->ctxs = ctxs;
3815 }
3816
3817 q->mq_kobj = &ctxs->kobj;
3818 q->queue_ctx = ctxs->queue_ctx;
3819
3820 return 0;
3821 fail:
3822 kfree(ctxs);
3823 return -ENOMEM;
3824}
3825
Ming Leie09aae72015-01-29 20:17:27 +08003826/*
3827 * It is the actual release handler for mq, but we do it from
3828 * request queue's release handler for avoiding use-after-free
3829 * and headache because q->mq_kobj shouldn't have been introduced,
3830 * but we can't group ctx/kctx kobj without it.
3831 */
3832void blk_mq_release(struct request_queue *q)
3833{
Ming Lei2f8f1332019-04-30 09:52:27 +08003834 struct blk_mq_hw_ctx *hctx, *next;
3835 int i;
Ming Leie09aae72015-01-29 20:17:27 +08003836
Ming Lei2f8f1332019-04-30 09:52:27 +08003837 queue_for_each_hw_ctx(q, hctx, i)
3838 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
3839
3840 /* all hctx are in .unused_hctx_list now */
3841 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
3842 list_del_init(&hctx->hctx_list);
Ming Lei6c8b2322017-02-22 18:14:01 +08003843 kobject_put(&hctx->kobj);
Ming Leic3b4afc2015-06-04 22:25:04 +08003844 }
Ming Leie09aae72015-01-29 20:17:27 +08003845
3846 kfree(q->queue_hw_ctx);
3847
Ming Lei7ea5fe32017-02-22 18:14:00 +08003848 /*
3849 * release .mq_kobj and sw queue's kobject now because
3850 * both share lifetime with request queue.
3851 */
3852 blk_mq_sysfs_deinit(q);
Ming Leie09aae72015-01-29 20:17:27 +08003853}
3854
Christoph Hellwig5ec780a2021-06-24 10:10:12 +02003855static struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set,
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003856 void *queuedata)
Jens Axboe320ae512013-10-24 09:20:05 +01003857{
Christoph Hellwig26a97502021-06-02 09:53:17 +03003858 struct request_queue *q;
3859 int ret;
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003860
Christoph Hellwig26a97502021-06-02 09:53:17 +03003861 q = blk_alloc_queue(set->numa_node);
3862 if (!q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003863 return ERR_PTR(-ENOMEM);
Christoph Hellwig26a97502021-06-02 09:53:17 +03003864 q->queuedata = queuedata;
3865 ret = blk_mq_init_allocated_queue(set, q);
3866 if (ret) {
3867 blk_cleanup_queue(q);
3868 return ERR_PTR(ret);
3869 }
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003870 return q;
3871}
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003872
3873struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
3874{
3875 return blk_mq_init_queue_data(set, NULL);
3876}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003877EXPORT_SYMBOL(blk_mq_init_queue);
3878
Christoph Hellwig4dcc4872021-08-16 15:19:05 +02003879struct gendisk *__blk_mq_alloc_disk(struct blk_mq_tag_set *set, void *queuedata,
3880 struct lock_class_key *lkclass)
Jens Axboe9316a9e2018-10-15 08:40:37 -06003881{
3882 struct request_queue *q;
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003883 struct gendisk *disk;
Jens Axboe9316a9e2018-10-15 08:40:37 -06003884
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003885 q = blk_mq_init_queue_data(set, queuedata);
3886 if (IS_ERR(q))
3887 return ERR_CAST(q);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003888
Christoph Hellwig4a1fa412021-08-16 15:19:08 +02003889 disk = __alloc_disk_node(q, set->numa_node, lkclass);
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003890 if (!disk) {
3891 blk_cleanup_queue(q);
3892 return ERR_PTR(-ENOMEM);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003893 }
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003894 return disk;
Jens Axboe9316a9e2018-10-15 08:40:37 -06003895}
Christoph Hellwigb461dfc2021-06-02 09:53:18 +03003896EXPORT_SYMBOL(__blk_mq_alloc_disk);
Jens Axboe9316a9e2018-10-15 08:40:37 -06003897
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003898static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
3899 struct blk_mq_tag_set *set, struct request_queue *q,
3900 int hctx_idx, int node)
3901{
Ming Lei2f8f1332019-04-30 09:52:27 +08003902 struct blk_mq_hw_ctx *hctx = NULL, *tmp;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003903
Ming Lei2f8f1332019-04-30 09:52:27 +08003904 /* reuse dead hctx first */
3905 spin_lock(&q->unused_hctx_lock);
3906 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
3907 if (tmp->numa_node == node) {
3908 hctx = tmp;
3909 break;
3910 }
3911 }
3912 if (hctx)
3913 list_del_init(&hctx->hctx_list);
3914 spin_unlock(&q->unused_hctx_lock);
3915
3916 if (!hctx)
3917 hctx = blk_mq_alloc_hctx(q, set, node);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003918 if (!hctx)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003919 goto fail;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003920
Ming Lei7c6c5b72019-04-30 09:52:26 +08003921 if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
3922 goto free_hctx;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003923
3924 return hctx;
Ming Lei7c6c5b72019-04-30 09:52:26 +08003925
3926 free_hctx:
3927 kobject_put(&hctx->kobj);
3928 fail:
3929 return NULL;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003930}
3931
Keith Busch868f2f02015-12-17 17:08:14 -07003932static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
3933 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003934{
Jianchao Wange01ad462018-10-12 18:07:28 +08003935 int i, j, end;
Keith Busch868f2f02015-12-17 17:08:14 -07003936 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01003937
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003938 if (q->nr_hw_queues < set->nr_hw_queues) {
3939 struct blk_mq_hw_ctx **new_hctxs;
3940
3941 new_hctxs = kcalloc_node(set->nr_hw_queues,
3942 sizeof(*new_hctxs), GFP_KERNEL,
3943 set->numa_node);
3944 if (!new_hctxs)
3945 return;
3946 if (hctxs)
3947 memcpy(new_hctxs, hctxs, q->nr_hw_queues *
3948 sizeof(*hctxs));
3949 q->queue_hw_ctx = new_hctxs;
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003950 kfree(hctxs);
3951 hctxs = new_hctxs;
3952 }
3953
Ming Leifb350e02018-01-06 16:27:40 +08003954 /* protect against switching io scheduler */
3955 mutex_lock(&q->sysfs_lock);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003956 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003957 int node;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003958 struct blk_mq_hw_ctx *hctx;
Keith Busch868f2f02015-12-17 17:08:14 -07003959
Dongli Zhang7d76f852019-02-27 21:35:01 +08003960 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003961 /*
3962 * If the hw queue has been mapped to another numa node,
3963 * we need to realloc the hctx. If allocation fails, fallback
3964 * to use the previous one.
3965 */
3966 if (hctxs[i] && (hctxs[i]->numa_node == node))
3967 continue;
Jens Axboe320ae512013-10-24 09:20:05 +01003968
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003969 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
3970 if (hctx) {
Ming Lei2f8f1332019-04-30 09:52:27 +08003971 if (hctxs[i])
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003972 blk_mq_exit_hctx(q, set, hctxs[i], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003973 hctxs[i] = hctx;
3974 } else {
3975 if (hctxs[i])
3976 pr_warn("Allocate new hctx on node %d fails,\
3977 fallback to previous one on node %d\n",
3978 node, hctxs[i]->numa_node);
3979 else
3980 break;
Keith Busch868f2f02015-12-17 17:08:14 -07003981 }
Jens Axboe320ae512013-10-24 09:20:05 +01003982 }
Jianchao Wange01ad462018-10-12 18:07:28 +08003983 /*
3984 * Increasing nr_hw_queues fails. Free the newly allocated
3985 * hctxs and keep the previous q->nr_hw_queues.
3986 */
3987 if (i != set->nr_hw_queues) {
3988 j = q->nr_hw_queues;
3989 end = i;
3990 } else {
3991 j = i;
3992 end = q->nr_hw_queues;
3993 q->nr_hw_queues = set->nr_hw_queues;
3994 }
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003995
Jianchao Wange01ad462018-10-12 18:07:28 +08003996 for (; j < end; j++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003997 struct blk_mq_hw_ctx *hctx = hctxs[j];
3998
3999 if (hctx) {
Keith Busch868f2f02015-12-17 17:08:14 -07004000 blk_mq_exit_hctx(q, set, hctx, j);
Keith Busch868f2f02015-12-17 17:08:14 -07004001 hctxs[j] = NULL;
Keith Busch868f2f02015-12-17 17:08:14 -07004002 }
4003 }
Ming Leifb350e02018-01-06 16:27:40 +08004004 mutex_unlock(&q->sysfs_lock);
Keith Busch868f2f02015-12-17 17:08:14 -07004005}
4006
Christoph Hellwig26a97502021-06-02 09:53:17 +03004007int blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
4008 struct request_queue *q)
Keith Busch868f2f02015-12-17 17:08:14 -07004009{
Ming Lei66841672016-02-12 15:27:00 +08004010 /* mark the queue as mq asap */
4011 q->mq_ops = set->ops;
4012
Omar Sandoval34dbad52017-03-21 08:56:08 -07004013 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
Stephen Bates720b8cc2017-04-07 06:24:03 -06004014 blk_mq_poll_stats_bkt,
4015 BLK_MQ_POLL_STATS_BKTS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -07004016 if (!q->poll_cb)
4017 goto err_exit;
4018
Ming Lei1db49092018-11-20 09:44:35 +08004019 if (blk_mq_alloc_ctxs(q))
Jes Sorensen41de54c2019-04-19 16:35:44 -04004020 goto err_poll;
Keith Busch868f2f02015-12-17 17:08:14 -07004021
Ming Lei737f98c2017-02-22 18:13:59 +08004022 /* init q->mq_kobj and sw queues' kobjects */
4023 blk_mq_sysfs_init(q);
4024
Ming Lei2f8f1332019-04-30 09:52:27 +08004025 INIT_LIST_HEAD(&q->unused_hctx_list);
4026 spin_lock_init(&q->unused_hctx_lock);
4027
Keith Busch868f2f02015-12-17 17:08:14 -07004028 blk_mq_realloc_hw_ctxs(set, q);
4029 if (!q->nr_hw_queues)
4030 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01004031
Christoph Hellwig287922e2015-10-30 20:57:30 +08004032 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08004033 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01004034
Jens Axboea8908932018-10-16 14:23:06 -06004035 q->tag_set = set;
Jens Axboe320ae512013-10-24 09:20:05 +01004036
Jens Axboe94eddfb2013-11-19 09:25:07 -07004037 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Ming Leicd191812018-12-18 12:15:29 +08004038 if (set->nr_maps > HCTX_TYPE_POLL &&
4039 set->map[HCTX_TYPE_POLL].nr_queues)
Christoph Hellwig6544d222018-12-02 17:46:28 +01004040 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
Jens Axboe320ae512013-10-24 09:20:05 +01004041
Mike Snitzer28494502016-09-14 13:28:30 -04004042 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06004043 INIT_LIST_HEAD(&q->requeue_list);
4044 spin_lock_init(&q->requeue_lock);
4045
Jens Axboeeba71762014-05-20 15:17:27 -06004046 q->nr_requests = set->queue_depth;
4047
Jens Axboe64f1c212016-11-14 13:03:03 -07004048 /*
4049 * Default to classic polling
4050 */
Yufen Yu29ece8b2019-03-18 22:44:41 +08004051 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
Jens Axboe64f1c212016-11-14 13:03:03 -07004052
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004053 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe0d2602c2014-05-13 15:10:52 -06004054 blk_mq_add_queue_tag_set(set, q);
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02004055 blk_mq_map_swqueue(q);
Christoph Hellwig26a97502021-06-02 09:53:17 +03004056 return 0;
Christoph Hellwig18741982014-02-10 09:29:00 -07004057
Jens Axboe320ae512013-10-24 09:20:05 +01004058err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07004059 kfree(q->queue_hw_ctx);
zhengbin73d9c8d2019-07-23 22:10:42 +08004060 q->nr_hw_queues = 0;
Ming Lei1db49092018-11-20 09:44:35 +08004061 blk_mq_sysfs_deinit(q);
Jes Sorensen41de54c2019-04-19 16:35:44 -04004062err_poll:
4063 blk_stat_free_callback(q->poll_cb);
4064 q->poll_cb = NULL;
Ming Linc7de5722016-05-25 23:23:27 -07004065err_exit:
4066 q->mq_ops = NULL;
Christoph Hellwig26a97502021-06-02 09:53:17 +03004067 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01004068}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04004069EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01004070
Ming Leic7e2d942019-04-30 09:52:25 +08004071/* tags can _not_ be used after returning from blk_mq_exit_queue */
4072void blk_mq_exit_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01004073{
Bart Van Assche630ef622021-05-13 10:15:29 -07004074 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01004075
Bart Van Assche630ef622021-05-13 10:15:29 -07004076 /* Checks hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED. */
Ming Lei624dbe42014-05-27 23:35:13 +08004077 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
Bart Van Assche630ef622021-05-13 10:15:29 -07004078 /* May clear BLK_MQ_F_TAG_QUEUE_SHARED in hctx->flags. */
4079 blk_mq_del_queue_tag_set(q);
Jens Axboe320ae512013-10-24 09:20:05 +01004080}
Jens Axboe320ae512013-10-24 09:20:05 +01004081
Jens Axboea5164402014-09-10 09:02:03 -06004082static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
4083{
4084 int i;
4085
John Garry079a2e32021-10-05 18:23:39 +08004086 if (blk_mq_is_shared_tags(set->flags)) {
4087 set->shared_tags = blk_mq_alloc_map_and_rqs(set,
John Garrye155b0c2021-10-05 18:23:37 +08004088 BLK_MQ_NO_HCTX_IDX,
4089 set->queue_depth);
John Garry079a2e32021-10-05 18:23:39 +08004090 if (!set->shared_tags)
John Garrye155b0c2021-10-05 18:23:37 +08004091 return -ENOMEM;
4092 }
4093
Xianting Tian8229cca2020-09-26 10:39:47 +08004094 for (i = 0; i < set->nr_hw_queues; i++) {
John Garry63064be2021-10-05 18:23:35 +08004095 if (!__blk_mq_alloc_map_and_rqs(set, i))
Jens Axboea5164402014-09-10 09:02:03 -06004096 goto out_unwind;
Xianting Tian8229cca2020-09-26 10:39:47 +08004097 cond_resched();
4098 }
Jens Axboea5164402014-09-10 09:02:03 -06004099
4100 return 0;
4101
4102out_unwind:
4103 while (--i >= 0)
John Garrye155b0c2021-10-05 18:23:37 +08004104 __blk_mq_free_map_and_rqs(set, i);
4105
John Garry079a2e32021-10-05 18:23:39 +08004106 if (blk_mq_is_shared_tags(set->flags)) {
4107 blk_mq_free_map_and_rqs(set, set->shared_tags,
John Garrye155b0c2021-10-05 18:23:37 +08004108 BLK_MQ_NO_HCTX_IDX);
John Garry645db342021-10-05 18:23:36 +08004109 }
Jens Axboea5164402014-09-10 09:02:03 -06004110
Jens Axboea5164402014-09-10 09:02:03 -06004111 return -ENOMEM;
4112}
4113
4114/*
4115 * Allocate the request maps associated with this tag_set. Note that this
4116 * may reduce the depth asked for, if memory is tight. set->queue_depth
4117 * will be updated to reflect the allocated depth.
4118 */
John Garry63064be2021-10-05 18:23:35 +08004119static int blk_mq_alloc_set_map_and_rqs(struct blk_mq_tag_set *set)
Jens Axboea5164402014-09-10 09:02:03 -06004120{
4121 unsigned int depth;
4122 int err;
4123
4124 depth = set->queue_depth;
4125 do {
4126 err = __blk_mq_alloc_rq_maps(set);
4127 if (!err)
4128 break;
4129
4130 set->queue_depth >>= 1;
4131 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
4132 err = -ENOMEM;
4133 break;
4134 }
4135 } while (set->queue_depth);
4136
4137 if (!set->queue_depth || err) {
4138 pr_err("blk-mq: failed to allocate request map\n");
4139 return -ENOMEM;
4140 }
4141
4142 if (depth != set->queue_depth)
4143 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
4144 depth, set->queue_depth);
4145
4146 return 0;
4147}
4148
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06004149static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
4150{
Bart Van Assche6e66b492020-03-09 21:26:17 -07004151 /*
4152 * blk_mq_map_queues() and multiple .map_queues() implementations
4153 * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the
4154 * number of hardware queues.
4155 */
4156 if (set->nr_maps == 1)
4157 set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues;
4158
Ming Lei59388702018-12-07 11:03:53 +08004159 if (set->ops->map_queues && !is_kdump_kernel()) {
Jens Axboeb3c661b2018-10-30 10:36:06 -06004160 int i;
4161
Ming Lei7d4901a2018-01-06 16:27:39 +08004162 /*
4163 * transport .map_queues is usually done in the following
4164 * way:
4165 *
4166 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
4167 * mask = get_cpu_mask(queue)
4168 * for_each_cpu(cpu, mask)
Jens Axboeb3c661b2018-10-30 10:36:06 -06004169 * set->map[x].mq_map[cpu] = queue;
Ming Lei7d4901a2018-01-06 16:27:39 +08004170 * }
4171 *
4172 * When we need to remap, the table has to be cleared for
4173 * killing stale mapping since one CPU may not be mapped
4174 * to any hw queue.
4175 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06004176 for (i = 0; i < set->nr_maps; i++)
4177 blk_mq_clear_mq_map(&set->map[i]);
Ming Lei7d4901a2018-01-06 16:27:39 +08004178
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06004179 return set->ops->map_queues(set);
Jens Axboeb3c661b2018-10-30 10:36:06 -06004180 } else {
4181 BUG_ON(set->nr_maps > 1);
Dongli Zhang7d76f852019-02-27 21:35:01 +08004182 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jens Axboeb3c661b2018-10-30 10:36:06 -06004183 }
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06004184}
4185
Bart Van Asschef7e76db2019-10-25 09:50:10 -07004186static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set,
4187 int cur_nr_hw_queues, int new_nr_hw_queues)
4188{
4189 struct blk_mq_tags **new_tags;
4190
4191 if (cur_nr_hw_queues >= new_nr_hw_queues)
4192 return 0;
4193
4194 new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *),
4195 GFP_KERNEL, set->numa_node);
4196 if (!new_tags)
4197 return -ENOMEM;
4198
4199 if (set->tags)
4200 memcpy(new_tags, set->tags, cur_nr_hw_queues *
4201 sizeof(*set->tags));
4202 kfree(set->tags);
4203 set->tags = new_tags;
4204 set->nr_hw_queues = new_nr_hw_queues;
4205
4206 return 0;
4207}
4208
Minwoo Im91cdf262020-12-05 00:20:53 +09004209static int blk_mq_alloc_tag_set_tags(struct blk_mq_tag_set *set,
4210 int new_nr_hw_queues)
4211{
4212 return blk_mq_realloc_tag_set_tags(set, 0, new_nr_hw_queues);
4213}
4214
Jens Axboea4391c62014-06-05 15:21:56 -06004215/*
4216 * Alloc a tag set to be associated with one or more request queues.
4217 * May fail with EINVAL for various error conditions. May adjust the
Minwoo Imc018c842018-06-30 22:12:41 +09004218 * requested depth down, if it's too large. In that case, the set
Jens Axboea4391c62014-06-05 15:21:56 -06004219 * value will be stored in set->queue_depth.
4220 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004221int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
4222{
Jens Axboeb3c661b2018-10-30 10:36:06 -06004223 int i, ret;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02004224
Bart Van Assche205fb5f2014-10-30 14:45:11 +01004225 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
4226
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004227 if (!set->nr_hw_queues)
4228 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06004229 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004230 return -EINVAL;
4231 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
4232 return -EINVAL;
4233
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02004234 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004235 return -EINVAL;
4236
Ming Leide148292017-10-14 17:22:29 +08004237 if (!set->ops->get_budget ^ !set->ops->put_budget)
4238 return -EINVAL;
4239
Jens Axboea4391c62014-06-05 15:21:56 -06004240 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
4241 pr_info("blk-mq: reduced tag depth to %u\n",
4242 BLK_MQ_MAX_DEPTH);
4243 set->queue_depth = BLK_MQ_MAX_DEPTH;
4244 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004245
Jens Axboeb3c661b2018-10-30 10:36:06 -06004246 if (!set->nr_maps)
4247 set->nr_maps = 1;
4248 else if (set->nr_maps > HCTX_MAX_TYPES)
4249 return -EINVAL;
4250
Shaohua Li6637fad2014-11-30 16:00:58 -08004251 /*
4252 * If a crashdump is active, then we are potentially in a very
4253 * memory constrained environment. Limit us to 1 queue and
4254 * 64 tags to prevent using too much memory.
4255 */
4256 if (is_kdump_kernel()) {
4257 set->nr_hw_queues = 1;
Ming Lei59388702018-12-07 11:03:53 +08004258 set->nr_maps = 1;
Shaohua Li6637fad2014-11-30 16:00:58 -08004259 set->queue_depth = min(64U, set->queue_depth);
4260 }
Keith Busch868f2f02015-12-17 17:08:14 -07004261 /*
Jens Axboe392546a2018-10-29 13:25:27 -06004262 * There is no use for more h/w queues than cpus if we just have
4263 * a single map
Keith Busch868f2f02015-12-17 17:08:14 -07004264 */
Jens Axboe392546a2018-10-29 13:25:27 -06004265 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07004266 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08004267
Minwoo Im91cdf262020-12-05 00:20:53 +09004268 if (blk_mq_alloc_tag_set_tags(set, set->nr_hw_queues) < 0)
Jens Axboea5164402014-09-10 09:02:03 -06004269 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004270
Christoph Hellwigda695ba2016-09-14 16:18:55 +02004271 ret = -ENOMEM;
Jens Axboeb3c661b2018-10-30 10:36:06 -06004272 for (i = 0; i < set->nr_maps; i++) {
4273 set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
Ming Lei07b35eb2018-12-17 18:42:45 +08004274 sizeof(set->map[i].mq_map[0]),
Jens Axboeb3c661b2018-10-30 10:36:06 -06004275 GFP_KERNEL, set->numa_node);
4276 if (!set->map[i].mq_map)
4277 goto out_free_mq_map;
Ming Lei59388702018-12-07 11:03:53 +08004278 set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues;
Jens Axboeb3c661b2018-10-30 10:36:06 -06004279 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02004280
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06004281 ret = blk_mq_update_queue_map(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02004282 if (ret)
4283 goto out_free_mq_map;
4284
John Garry63064be2021-10-05 18:23:35 +08004285 ret = blk_mq_alloc_set_map_and_rqs(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02004286 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02004287 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004288
Jens Axboe0d2602c2014-05-13 15:10:52 -06004289 mutex_init(&set->tag_list_lock);
4290 INIT_LIST_HEAD(&set->tag_list);
4291
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004292 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02004293
4294out_free_mq_map:
Jens Axboeb3c661b2018-10-30 10:36:06 -06004295 for (i = 0; i < set->nr_maps; i++) {
4296 kfree(set->map[i].mq_map);
4297 set->map[i].mq_map = NULL;
4298 }
Robert Elliott5676e7b2014-09-02 11:38:44 -05004299 kfree(set->tags);
4300 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02004301 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004302}
4303EXPORT_SYMBOL(blk_mq_alloc_tag_set);
4304
Christoph Hellwigcdb14e02021-06-02 09:53:16 +03004305/* allocate and initialize a tagset for a simple single-queue device */
4306int blk_mq_alloc_sq_tag_set(struct blk_mq_tag_set *set,
4307 const struct blk_mq_ops *ops, unsigned int queue_depth,
4308 unsigned int set_flags)
4309{
4310 memset(set, 0, sizeof(*set));
4311 set->ops = ops;
4312 set->nr_hw_queues = 1;
4313 set->nr_maps = 1;
4314 set->queue_depth = queue_depth;
4315 set->numa_node = NUMA_NO_NODE;
4316 set->flags = set_flags;
4317 return blk_mq_alloc_tag_set(set);
4318}
4319EXPORT_SYMBOL_GPL(blk_mq_alloc_sq_tag_set);
4320
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004321void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
4322{
Jens Axboeb3c661b2018-10-30 10:36:06 -06004323 int i, j;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004324
Bart Van Asschef7e76db2019-10-25 09:50:10 -07004325 for (i = 0; i < set->nr_hw_queues; i++)
John Garrye155b0c2021-10-05 18:23:37 +08004326 __blk_mq_free_map_and_rqs(set, i);
Jens Axboe484b4062014-05-21 14:01:15 -06004327
John Garry079a2e32021-10-05 18:23:39 +08004328 if (blk_mq_is_shared_tags(set->flags)) {
4329 blk_mq_free_map_and_rqs(set, set->shared_tags,
John Garrye155b0c2021-10-05 18:23:37 +08004330 BLK_MQ_NO_HCTX_IDX);
4331 }
John Garry32bc15a2020-08-19 23:20:24 +08004332
Jens Axboeb3c661b2018-10-30 10:36:06 -06004333 for (j = 0; j < set->nr_maps; j++) {
4334 kfree(set->map[j].mq_map);
4335 set->map[j].mq_map = NULL;
4336 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02004337
Ming Lei981bd182014-04-24 00:07:34 +08004338 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05004339 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06004340}
4341EXPORT_SYMBOL(blk_mq_free_tag_set);
4342
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004343int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
4344{
4345 struct blk_mq_tag_set *set = q->tag_set;
4346 struct blk_mq_hw_ctx *hctx;
4347 int i, ret;
4348
Jens Axboebd166ef2017-01-17 06:03:22 -07004349 if (!set)
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004350 return -EINVAL;
4351
Aleksei Zakharove5fa8142019-02-08 19:14:05 +03004352 if (q->nr_requests == nr)
4353 return 0;
4354
Jens Axboe70f36b62017-01-19 10:59:07 -07004355 blk_mq_freeze_queue(q);
Ming Lei24f5a902018-01-06 16:27:38 +08004356 blk_mq_quiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07004357
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004358 ret = 0;
4359 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07004360 if (!hctx->tags)
4361 continue;
Jens Axboebd166ef2017-01-17 06:03:22 -07004362 /*
4363 * If we're using an MQ scheduler, just update the scheduler
4364 * queue depth. This is similar to what the old code would do.
4365 */
John Garryf6adcef2021-10-05 18:23:29 +08004366 if (hctx->sched_tags) {
Jens Axboe70f36b62017-01-19 10:59:07 -07004367 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
John Garryf6adcef2021-10-05 18:23:29 +08004368 nr, true);
John Garryf6adcef2021-10-05 18:23:29 +08004369 } else {
4370 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
4371 false);
Jens Axboe70f36b62017-01-19 10:59:07 -07004372 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004373 if (ret)
4374 break;
Jens Axboe77f1e0a2019-01-18 10:34:16 -07004375 if (q->elevator && q->elevator->type->ops.depth_updated)
4376 q->elevator->type->ops.depth_updated(hctx);
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004377 }
John Garryd97e5942021-05-13 20:00:58 +08004378 if (!ret) {
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004379 q->nr_requests = nr;
John Garry079a2e32021-10-05 18:23:39 +08004380 if (blk_mq_is_shared_tags(set->flags)) {
John Garry8fa04462021-10-05 18:23:28 +08004381 if (q->elevator)
John Garry079a2e32021-10-05 18:23:39 +08004382 blk_mq_tag_update_sched_shared_tags(q);
John Garry8fa04462021-10-05 18:23:28 +08004383 else
John Garry079a2e32021-10-05 18:23:39 +08004384 blk_mq_tag_resize_shared_tags(set, nr);
John Garry8fa04462021-10-05 18:23:28 +08004385 }
John Garryd97e5942021-05-13 20:00:58 +08004386 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004387
Ming Lei24f5a902018-01-06 16:27:38 +08004388 blk_mq_unquiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07004389 blk_mq_unfreeze_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07004390
Jens Axboee3a2b3f2014-05-20 11:49:02 -06004391 return ret;
4392}
4393
Jianchao Wangd48ece22018-08-21 15:15:03 +08004394/*
4395 * request_queue and elevator_type pair.
4396 * It is just used by __blk_mq_update_nr_hw_queues to cache
4397 * the elevator_type associated with a request_queue.
4398 */
4399struct blk_mq_qe_pair {
4400 struct list_head node;
4401 struct request_queue *q;
4402 struct elevator_type *type;
4403};
4404
4405/*
4406 * Cache the elevator_type in qe pair list and switch the
4407 * io scheduler to 'none'
4408 */
4409static bool blk_mq_elv_switch_none(struct list_head *head,
4410 struct request_queue *q)
4411{
4412 struct blk_mq_qe_pair *qe;
4413
4414 if (!q->elevator)
4415 return true;
4416
4417 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
4418 if (!qe)
4419 return false;
4420
4421 INIT_LIST_HEAD(&qe->node);
4422 qe->q = q;
4423 qe->type = q->elevator->type;
4424 list_add(&qe->node, head);
4425
4426 mutex_lock(&q->sysfs_lock);
4427 /*
4428 * After elevator_switch_mq, the previous elevator_queue will be
4429 * released by elevator_release. The reference of the io scheduler
4430 * module get by elevator_get will also be put. So we need to get
4431 * a reference of the io scheduler module here to prevent it to be
4432 * removed.
4433 */
4434 __module_get(qe->type->elevator_owner);
4435 elevator_switch_mq(q, NULL);
4436 mutex_unlock(&q->sysfs_lock);
4437
4438 return true;
4439}
4440
4441static void blk_mq_elv_switch_back(struct list_head *head,
4442 struct request_queue *q)
4443{
4444 struct blk_mq_qe_pair *qe;
4445 struct elevator_type *t = NULL;
4446
4447 list_for_each_entry(qe, head, node)
4448 if (qe->q == q) {
4449 t = qe->type;
4450 break;
4451 }
4452
4453 if (!t)
4454 return;
4455
4456 list_del(&qe->node);
4457 kfree(qe);
4458
4459 mutex_lock(&q->sysfs_lock);
4460 elevator_switch_mq(q, t);
4461 mutex_unlock(&q->sysfs_lock);
4462}
4463
Keith Busche4dc2b32017-05-30 14:39:11 -04004464static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
4465 int nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07004466{
4467 struct request_queue *q;
Jianchao Wangd48ece22018-08-21 15:15:03 +08004468 LIST_HEAD(head);
Jianchao Wange01ad462018-10-12 18:07:28 +08004469 int prev_nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07004470
Bart Van Assche705cda92017-04-07 11:16:49 -07004471 lockdep_assert_held(&set->tag_list_lock);
4472
Jens Axboe392546a2018-10-29 13:25:27 -06004473 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07004474 nr_hw_queues = nr_cpu_ids;
Weiping Zhangfe35ec52020-06-17 14:18:37 +08004475 if (nr_hw_queues < 1)
4476 return;
4477 if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07004478 return;
4479
4480 list_for_each_entry(q, &set->tag_list, tag_set_list)
4481 blk_mq_freeze_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +08004482 /*
4483 * Switch IO scheduler to 'none', cleaning up the data associated
4484 * with the previous scheduler. We will switch back once we are done
4485 * updating the new sw to hw queue mappings.
4486 */
4487 list_for_each_entry(q, &set->tag_list, tag_set_list)
4488 if (!blk_mq_elv_switch_none(&head, q))
4489 goto switch_back;
Keith Busch868f2f02015-12-17 17:08:14 -07004490
Jianchao Wang477e19d2018-10-12 18:07:25 +08004491 list_for_each_entry(q, &set->tag_list, tag_set_list) {
4492 blk_mq_debugfs_unregister_hctxs(q);
4493 blk_mq_sysfs_unregister(q);
4494 }
4495
Weiping Zhanga2584e42020-05-07 21:03:56 +08004496 prev_nr_hw_queues = set->nr_hw_queues;
Bart Van Asschef7e76db2019-10-25 09:50:10 -07004497 if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) <
4498 0)
4499 goto reregister;
4500
Keith Busch868f2f02015-12-17 17:08:14 -07004501 set->nr_hw_queues = nr_hw_queues;
Jianchao Wange01ad462018-10-12 18:07:28 +08004502fallback:
Weiping Zhangaa880ad2020-05-13 08:44:05 +08004503 blk_mq_update_queue_map(set);
Keith Busch868f2f02015-12-17 17:08:14 -07004504 list_for_each_entry(q, &set->tag_list, tag_set_list) {
4505 blk_mq_realloc_hw_ctxs(set, q);
Jianchao Wange01ad462018-10-12 18:07:28 +08004506 if (q->nr_hw_queues != set->nr_hw_queues) {
Ye Bina846a8e2021-11-08 15:40:19 +08004507 int i = prev_nr_hw_queues;
4508
Jianchao Wange01ad462018-10-12 18:07:28 +08004509 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
4510 nr_hw_queues, prev_nr_hw_queues);
Ye Bina846a8e2021-11-08 15:40:19 +08004511 for (; i < set->nr_hw_queues; i++)
4512 __blk_mq_free_map_and_rqs(set, i);
4513
Jianchao Wange01ad462018-10-12 18:07:28 +08004514 set->nr_hw_queues = prev_nr_hw_queues;
Dongli Zhang7d76f852019-02-27 21:35:01 +08004515 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jianchao Wange01ad462018-10-12 18:07:28 +08004516 goto fallback;
4517 }
Jianchao Wang477e19d2018-10-12 18:07:25 +08004518 blk_mq_map_swqueue(q);
4519 }
4520
Bart Van Asschef7e76db2019-10-25 09:50:10 -07004521reregister:
Jianchao Wang477e19d2018-10-12 18:07:25 +08004522 list_for_each_entry(q, &set->tag_list, tag_set_list) {
4523 blk_mq_sysfs_register(q);
4524 blk_mq_debugfs_register_hctxs(q);
Keith Busch868f2f02015-12-17 17:08:14 -07004525 }
4526
Jianchao Wangd48ece22018-08-21 15:15:03 +08004527switch_back:
4528 list_for_each_entry(q, &set->tag_list, tag_set_list)
4529 blk_mq_elv_switch_back(&head, q);
4530
Keith Busch868f2f02015-12-17 17:08:14 -07004531 list_for_each_entry(q, &set->tag_list, tag_set_list)
4532 blk_mq_unfreeze_queue(q);
4533}
Keith Busche4dc2b32017-05-30 14:39:11 -04004534
4535void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
4536{
4537 mutex_lock(&set->tag_list_lock);
4538 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
4539 mutex_unlock(&set->tag_list_lock);
4540}
Keith Busch868f2f02015-12-17 17:08:14 -07004541EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
4542
Omar Sandoval34dbad52017-03-21 08:56:08 -07004543/* Enable polling stats and return whether they were already enabled. */
4544static bool blk_poll_stats_enable(struct request_queue *q)
4545{
4546 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
Bart Van Assche7dfdbc72018-03-07 17:10:05 -08004547 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
Omar Sandoval34dbad52017-03-21 08:56:08 -07004548 return true;
4549 blk_stat_add_callback(q, q->poll_cb);
4550 return false;
4551}
4552
4553static void blk_mq_poll_stats_start(struct request_queue *q)
4554{
4555 /*
4556 * We don't arm the callback if polling stats are not enabled or the
4557 * callback is already active.
4558 */
4559 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
4560 blk_stat_is_active(q->poll_cb))
4561 return;
4562
4563 blk_stat_activate_msecs(q->poll_cb, 100);
4564}
4565
4566static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
4567{
4568 struct request_queue *q = cb->data;
Stephen Bates720b8cc2017-04-07 06:24:03 -06004569 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -07004570
Stephen Bates720b8cc2017-04-07 06:24:03 -06004571 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
4572 if (cb->stat[bucket].nr_samples)
4573 q->poll_stat[bucket] = cb->stat[bucket];
4574 }
Omar Sandoval34dbad52017-03-21 08:56:08 -07004575}
4576
Jens Axboe64f1c212016-11-14 13:03:03 -07004577static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
Jens Axboe64f1c212016-11-14 13:03:03 -07004578 struct request *rq)
4579{
Jens Axboe64f1c212016-11-14 13:03:03 -07004580 unsigned long ret = 0;
Stephen Bates720b8cc2017-04-07 06:24:03 -06004581 int bucket;
Jens Axboe64f1c212016-11-14 13:03:03 -07004582
4583 /*
4584 * If stats collection isn't on, don't sleep but turn it on for
4585 * future users
4586 */
Omar Sandoval34dbad52017-03-21 08:56:08 -07004587 if (!blk_poll_stats_enable(q))
Jens Axboe64f1c212016-11-14 13:03:03 -07004588 return 0;
4589
4590 /*
Jens Axboe64f1c212016-11-14 13:03:03 -07004591 * As an optimistic guess, use half of the mean service time
4592 * for this type of request. We can (and should) make this smarter.
4593 * For instance, if the completion latencies are tight, we can
4594 * get closer than just half the mean. This is especially
4595 * important on devices where the completion latencies are longer
Stephen Bates720b8cc2017-04-07 06:24:03 -06004596 * than ~10 usec. We do use the stats for the relevant IO size
4597 * if available which does lead to better estimates.
Jens Axboe64f1c212016-11-14 13:03:03 -07004598 */
Stephen Bates720b8cc2017-04-07 06:24:03 -06004599 bucket = blk_mq_poll_stats_bkt(rq);
4600 if (bucket < 0)
4601 return ret;
4602
4603 if (q->poll_stat[bucket].nr_samples)
4604 ret = (q->poll_stat[bucket].mean + 1) / 2;
Jens Axboe64f1c212016-11-14 13:03:03 -07004605
4606 return ret;
4607}
4608
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004609static bool blk_mq_poll_hybrid(struct request_queue *q, blk_qc_t qc)
Jens Axboe06426ad2016-11-14 13:01:59 -07004610{
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004611 struct blk_mq_hw_ctx *hctx = blk_qc_to_hctx(q, qc);
4612 struct request *rq = blk_qc_to_rq(hctx, qc);
Jens Axboe06426ad2016-11-14 13:01:59 -07004613 struct hrtimer_sleeper hs;
4614 enum hrtimer_mode mode;
Jens Axboe64f1c212016-11-14 13:03:03 -07004615 unsigned int nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07004616 ktime_t kt;
4617
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004618 /*
4619 * If a request has completed on queue that uses an I/O scheduler, we
4620 * won't get back a request from blk_qc_to_rq.
4621 */
4622 if (!rq || (rq->rq_flags & RQF_MQ_POLL_SLEPT))
Jens Axboe64f1c212016-11-14 13:03:03 -07004623 return false;
4624
4625 /*
Jens Axboe1052b8a2018-11-26 08:21:49 -07004626 * If we get here, hybrid polling is enabled. Hence poll_nsec can be:
Jens Axboe64f1c212016-11-14 13:03:03 -07004627 *
Jens Axboe64f1c212016-11-14 13:03:03 -07004628 * 0: use half of prev avg
4629 * >0: use this specific value
4630 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07004631 if (q->poll_nsec > 0)
Jens Axboe64f1c212016-11-14 13:03:03 -07004632 nsecs = q->poll_nsec;
4633 else
John Garrycae740a2020-02-26 20:10:15 +08004634 nsecs = blk_mq_poll_nsecs(q, rq);
Jens Axboe64f1c212016-11-14 13:03:03 -07004635
4636 if (!nsecs)
Jens Axboe06426ad2016-11-14 13:01:59 -07004637 return false;
4638
Jens Axboe76a86f92018-01-10 11:30:56 -07004639 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
Jens Axboe06426ad2016-11-14 13:01:59 -07004640
4641 /*
4642 * This will be replaced with the stats tracking code, using
4643 * 'avg_completion_time / 2' as the pre-sleep target.
4644 */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01004645 kt = nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07004646
4647 mode = HRTIMER_MODE_REL;
Sebastian Andrzej Siewiordbc16252019-07-26 20:30:50 +02004648 hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07004649 hrtimer_set_expires(&hs.timer, kt);
4650
Jens Axboe06426ad2016-11-14 13:01:59 -07004651 do {
Tejun Heo5a61c362018-01-09 08:29:52 -08004652 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
Jens Axboe06426ad2016-11-14 13:01:59 -07004653 break;
4654 set_current_state(TASK_UNINTERRUPTIBLE);
Thomas Gleixner9dd88132019-07-30 21:16:55 +02004655 hrtimer_sleeper_start_expires(&hs, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07004656 if (hs.task)
4657 io_schedule();
4658 hrtimer_cancel(&hs.timer);
4659 mode = HRTIMER_MODE_ABS;
4660 } while (hs.task && !signal_pending(current));
4661
4662 __set_current_state(TASK_RUNNING);
4663 destroy_hrtimer_on_stack(&hs.timer);
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004664
4665 /*
4666 * If we sleep, have the caller restart the poll loop to reset the
4667 * state. Like for the other success return cases, the caller is
4668 * responsible for checking if the IO completed. If the IO isn't
4669 * complete, we'll get called again and will go straight to the busy
4670 * poll loop.
4671 */
Jens Axboe06426ad2016-11-14 13:01:59 -07004672 return true;
4673}
4674
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004675static int blk_mq_poll_classic(struct request_queue *q, blk_qc_t cookie,
Jens Axboe5a72e892021-10-12 09:24:29 -06004676 struct io_comp_batch *iob, unsigned int flags)
Jens Axboebbd7bb72016-11-04 09:34:34 -06004677{
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004678 struct blk_mq_hw_ctx *hctx = blk_qc_to_hctx(q, cookie);
4679 long state = get_current_state();
4680 int ret;
Jens Axboe1052b8a2018-11-26 08:21:49 -07004681
Jens Axboeaa61bec2018-11-13 21:32:10 -07004682 do {
Jens Axboe5a72e892021-10-12 09:24:29 -06004683 ret = q->mq_ops->poll(hctx, iob);
Jens Axboebbd7bb72016-11-04 09:34:34 -06004684 if (ret > 0) {
Jens Axboe849a3702018-11-16 08:37:34 -07004685 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004686 return ret;
Jens Axboebbd7bb72016-11-04 09:34:34 -06004687 }
4688
4689 if (signal_pending_state(state, current))
Jens Axboe849a3702018-11-16 08:37:34 -07004690 __set_current_state(TASK_RUNNING);
Peter Zijlstrab03fbd42021-06-11 10:28:12 +02004691 if (task_is_running(current))
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004692 return 1;
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004693
Christoph Hellwigef99b2d2021-10-12 13:12:19 +02004694 if (ret < 0 || (flags & BLK_POLL_ONESHOT))
Jens Axboebbd7bb72016-11-04 09:34:34 -06004695 break;
4696 cpu_relax();
Jens Axboeaa61bec2018-11-13 21:32:10 -07004697 } while (!need_resched());
Jens Axboebbd7bb72016-11-04 09:34:34 -06004698
Nitesh Shetty67b41102018-02-13 21:18:12 +05304699 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07004700 return 0;
Jens Axboebbd7bb72016-11-04 09:34:34 -06004701}
Jens Axboebbd7bb72016-11-04 09:34:34 -06004702
Jens Axboe5a72e892021-10-12 09:24:29 -06004703int blk_mq_poll(struct request_queue *q, blk_qc_t cookie, struct io_comp_batch *iob,
4704 unsigned int flags)
Jens Axboe676141e2014-03-20 13:29:18 -06004705{
Christoph Hellwigd729cf92021-10-12 13:12:20 +02004706 if (!(flags & BLK_POLL_NOSLEEP) &&
4707 q->poll_nsec != BLK_MQ_POLL_CLASSIC) {
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004708 if (blk_mq_poll_hybrid(q, cookie))
Jens Axboe320ae512013-10-24 09:20:05 +01004709 return 1;
Christoph Hellwigc6699d62021-10-12 13:12:16 +02004710 }
Jens Axboe5a72e892021-10-12 09:24:29 -06004711 return blk_mq_poll_classic(q, cookie, iob, flags);
Jens Axboe320ae512013-10-24 09:20:05 +01004712}
4713
Jens Axboe9cf2bab2018-10-31 17:01:22 -06004714unsigned int blk_mq_rq_cpu(struct request *rq)
4715{
4716 return rq->mq_ctx->cpu;
4717}
4718EXPORT_SYMBOL(blk_mq_rq_cpu);
4719
Ming Lei2a19b282021-11-16 09:43:43 +08004720void blk_mq_cancel_work_sync(struct request_queue *q)
4721{
4722 if (queue_is_mq(q)) {
4723 struct blk_mq_hw_ctx *hctx;
4724 int i;
4725
4726 cancel_delayed_work_sync(&q->requeue_work);
4727
4728 queue_for_each_hw_ctx(q, hctx, i)
4729 cancel_delayed_work_sync(&hctx->run_work);
4730 }
4731}
4732
Jens Axboe320ae512013-10-24 09:20:05 +01004733static int __init blk_mq_init(void)
4734{
Christoph Hellwigc3077b52020-06-11 08:44:41 +02004735 int i;
4736
4737 for_each_possible_cpu(i)
Sebastian Andrzej Siewiorf9ab4912021-01-23 21:10:27 +01004738 init_llist_head(&per_cpu(blk_cpu_done, i));
Christoph Hellwigc3077b52020-06-11 08:44:41 +02004739 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
4740
4741 cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
4742 "block/softirq:dead", NULL,
4743 blk_softirq_cpu_dead);
Jens Axboe320ae512013-10-24 09:20:05 +01004744 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
4745 blk_mq_hctx_notify_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02004746 cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online",
4747 blk_mq_hctx_notify_online,
4748 blk_mq_hctx_notify_offline);
Jens Axboe320ae512013-10-24 09:20:05 +01004749 return 0;
4750}
4751subsys_initcall(blk_mq_init);