blob: d35b3c0c876aa6fc5ec773e63b2a0c1537a19a9e [file] [log] [blame]
Christoph Hellwig3dcf60b2019-04-30 14:42:43 -04001// SPDX-License-Identifier: GPL-2.0
Jens Axboe75bb4622014-05-28 10:15:41 -06002/*
3 * Block multiqueue core code
4 *
5 * Copyright (C) 2013-2014 Jens Axboe
6 * Copyright (C) 2013-2014 Christoph Hellwig
7 */
Jens Axboe320ae512013-10-24 09:20:05 +01008#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/backing-dev.h>
11#include <linux/bio.h>
12#include <linux/blkdev.h>
Catalin Marinasf75782e2015-09-14 18:16:02 +010013#include <linux/kmemleak.h>
Jens Axboe320ae512013-10-24 09:20:05 +010014#include <linux/mm.h>
15#include <linux/init.h>
16#include <linux/slab.h>
17#include <linux/workqueue.h>
18#include <linux/smp.h>
19#include <linux/llist.h>
20#include <linux/list_sort.h>
21#include <linux/cpu.h>
22#include <linux/cache.h>
23#include <linux/sched/sysctl.h>
Ingo Molnar105ab3d2017-02-01 16:36:40 +010024#include <linux/sched/topology.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010025#include <linux/sched/signal.h>
Jens Axboe320ae512013-10-24 09:20:05 +010026#include <linux/delay.h>
Jens Axboeaedcd722014-09-17 08:27:03 -060027#include <linux/crash_dump.h>
Jens Axboe88c7b2b2016-08-25 08:07:30 -060028#include <linux/prefetch.h>
Satya Tangiralaa892c8d2020-05-14 00:37:18 +000029#include <linux/blk-crypto.h>
Jens Axboe320ae512013-10-24 09:20:05 +010030
31#include <trace/events/block.h>
32
33#include <linux/blk-mq.h>
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +030034#include <linux/t10-pi.h>
Jens Axboe320ae512013-10-24 09:20:05 +010035#include "blk.h"
36#include "blk-mq.h"
Omar Sandoval9c1051a2017-05-04 08:17:21 -060037#include "blk-mq-debugfs.h"
Jens Axboe320ae512013-10-24 09:20:05 +010038#include "blk-mq-tag.h"
Bart Van Assche986d4132018-09-26 14:01:10 -070039#include "blk-pm.h"
Jens Axboecf43e6b2016-11-07 21:32:37 -070040#include "blk-stat.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070041#include "blk-mq-sched.h"
Josef Bacikc1c80382018-07-03 11:14:59 -040042#include "blk-rq-qos.h"
Jens Axboe320ae512013-10-24 09:20:05 +010043
Christoph Hellwigc3077b52020-06-11 08:44:41 +020044static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
45
Omar Sandoval34dbad52017-03-21 08:56:08 -070046static void blk_mq_poll_stats_start(struct request_queue *q);
47static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);
48
Stephen Bates720b8cc2017-04-07 06:24:03 -060049static int blk_mq_poll_stats_bkt(const struct request *rq)
50{
Hou Tao3d244302019-05-21 15:59:03 +080051 int ddir, sectors, bucket;
Stephen Bates720b8cc2017-04-07 06:24:03 -060052
Jens Axboe99c749a2017-04-21 07:55:42 -060053 ddir = rq_data_dir(rq);
Hou Tao3d244302019-05-21 15:59:03 +080054 sectors = blk_rq_stats_sectors(rq);
Stephen Bates720b8cc2017-04-07 06:24:03 -060055
Hou Tao3d244302019-05-21 15:59:03 +080056 bucket = ddir + 2 * ilog2(sectors);
Stephen Bates720b8cc2017-04-07 06:24:03 -060057
58 if (bucket < 0)
59 return -1;
60 else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
61 return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
62
63 return bucket;
64}
65
Jens Axboe320ae512013-10-24 09:20:05 +010066/*
Yufen Yu85fae292019-03-24 17:57:08 +080067 * Check if any of the ctx, dispatch list or elevator
68 * have pending work in this hardware queue.
Jens Axboe320ae512013-10-24 09:20:05 +010069 */
Jens Axboe79f720a2017-11-10 09:13:21 -070070static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
Jens Axboe320ae512013-10-24 09:20:05 +010071{
Jens Axboe79f720a2017-11-10 09:13:21 -070072 return !list_empty_careful(&hctx->dispatch) ||
73 sbitmap_any_bit_set(&hctx->ctx_map) ||
Jens Axboebd166ef2017-01-17 06:03:22 -070074 blk_mq_sched_has_work(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +010075}
76
77/*
78 * Mark this ctx as having pending work in this hardware queue
79 */
80static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
81 struct blk_mq_ctx *ctx)
82{
Jens Axboef31967f2018-10-29 13:13:29 -060083 const int bit = ctx->index_hw[hctx->type];
84
85 if (!sbitmap_test_bit(&hctx->ctx_map, bit))
86 sbitmap_set_bit(&hctx->ctx_map, bit);
Jens Axboe1429d7c2014-05-19 09:23:55 -060087}
88
89static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
90 struct blk_mq_ctx *ctx)
91{
Jens Axboef31967f2018-10-29 13:13:29 -060092 const int bit = ctx->index_hw[hctx->type];
93
94 sbitmap_clear_bit(&hctx->ctx_map, bit);
Jens Axboe320ae512013-10-24 09:20:05 +010095}
96
Jens Axboef299b7c2017-08-08 17:51:45 -060097struct mq_inflight {
98 struct hd_struct *part;
Pavel Begunkova2e80f62019-09-30 21:55:34 +030099 unsigned int inflight[2];
Jens Axboef299b7c2017-08-08 17:51:45 -0600100};
101
Jens Axboe7baa8572018-11-08 10:24:07 -0700102static bool blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx,
Jens Axboef299b7c2017-08-08 17:51:45 -0600103 struct request *rq, void *priv,
104 bool reserved)
105{
106 struct mq_inflight *mi = priv;
107
Gabriel Krisman Bertazia926c7a2020-10-06 15:41:25 -0400108 if (rq->part == mi->part && blk_mq_rq_state(rq) == MQ_RQ_IN_FLIGHT)
Pavel Begunkovbb4e6b12019-09-30 21:55:33 +0300109 mi->inflight[rq_data_dir(rq)]++;
Jens Axboe7baa8572018-11-08 10:24:07 -0700110
111 return true;
Jens Axboef299b7c2017-08-08 17:51:45 -0600112}
113
Mikulas Patockae016b782018-12-06 11:41:21 -0500114unsigned int blk_mq_in_flight(struct request_queue *q, struct hd_struct *part)
Jens Axboef299b7c2017-08-08 17:51:45 -0600115{
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300116 struct mq_inflight mi = { .part = part };
Jens Axboef299b7c2017-08-08 17:51:45 -0600117
Jens Axboef299b7c2017-08-08 17:51:45 -0600118 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
Mikulas Patockae016b782018-12-06 11:41:21 -0500119
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300120 return mi.inflight[0] + mi.inflight[1];
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700121}
122
123void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,
124 unsigned int inflight[2])
125{
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300126 struct mq_inflight mi = { .part = part };
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700127
Pavel Begunkovbb4e6b12019-09-30 21:55:33 +0300128 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
Pavel Begunkova2e80f62019-09-30 21:55:34 +0300129 inflight[0] = mi.inflight[0];
130 inflight[1] = mi.inflight[1];
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700131}
132
Ming Lei1671d522017-03-27 20:06:57 +0800133void blk_freeze_queue_start(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +0800134{
Bob Liu7996a8b2019-05-21 11:25:55 +0800135 mutex_lock(&q->mq_freeze_lock);
136 if (++q->mq_freeze_depth == 1) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400137 percpu_ref_kill(&q->q_usage_counter);
Bob Liu7996a8b2019-05-21 11:25:55 +0800138 mutex_unlock(&q->mq_freeze_lock);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700139 if (queue_is_mq(q))
Ming Lei055f6e12017-11-09 10:49:53 -0800140 blk_mq_run_hw_queues(q, false);
Bob Liu7996a8b2019-05-21 11:25:55 +0800141 } else {
142 mutex_unlock(&q->mq_freeze_lock);
Tejun Heocddd5d12014-08-16 08:02:24 -0400143 }
Tejun Heof3af0202014-11-04 13:52:27 -0500144}
Ming Lei1671d522017-03-27 20:06:57 +0800145EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
Tejun Heof3af0202014-11-04 13:52:27 -0500146
Keith Busch6bae363e2017-03-01 14:22:10 -0500147void blk_mq_freeze_queue_wait(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500148{
Dan Williams3ef28e82015-10-21 13:20:12 -0400149 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +0800150}
Keith Busch6bae363e2017-03-01 14:22:10 -0500151EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800152
Keith Buschf91328c2017-03-01 14:22:11 -0500153int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
154 unsigned long timeout)
155{
156 return wait_event_timeout(q->mq_freeze_wq,
157 percpu_ref_is_zero(&q->q_usage_counter),
158 timeout);
159}
160EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
Jens Axboe320ae512013-10-24 09:20:05 +0100161
Tejun Heof3af0202014-11-04 13:52:27 -0500162/*
163 * Guarantee no request is in use, so we can change any data structure of
164 * the queue afterward.
165 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400166void blk_freeze_queue(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500167{
Dan Williams3ef28e82015-10-21 13:20:12 -0400168 /*
169 * In the !blk_mq case we are only calling this to kill the
170 * q_usage_counter, otherwise this increases the freeze depth
171 * and waits for it to return to zero. For this reason there is
172 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
173 * exported to drivers as the only user for unfreeze is blk_mq.
174 */
Ming Lei1671d522017-03-27 20:06:57 +0800175 blk_freeze_queue_start(q);
Tejun Heof3af0202014-11-04 13:52:27 -0500176 blk_mq_freeze_queue_wait(q);
177}
Dan Williams3ef28e82015-10-21 13:20:12 -0400178
179void blk_mq_freeze_queue(struct request_queue *q)
180{
181 /*
182 * ...just an alias to keep freeze and unfreeze actions balanced
183 * in the blk_mq_* namespace
184 */
185 blk_freeze_queue(q);
186}
Jens Axboec761d962015-01-02 15:05:12 -0700187EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
Tejun Heof3af0202014-11-04 13:52:27 -0500188
Keith Buschb4c6a022014-12-19 17:54:14 -0700189void blk_mq_unfreeze_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +0100190{
Bob Liu7996a8b2019-05-21 11:25:55 +0800191 mutex_lock(&q->mq_freeze_lock);
192 q->mq_freeze_depth--;
193 WARN_ON_ONCE(q->mq_freeze_depth < 0);
194 if (!q->mq_freeze_depth) {
Bart Van Asschebdd63162018-09-26 14:01:08 -0700195 percpu_ref_resurrect(&q->q_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100196 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600197 }
Bob Liu7996a8b2019-05-21 11:25:55 +0800198 mutex_unlock(&q->mq_freeze_lock);
Jens Axboe320ae512013-10-24 09:20:05 +0100199}
Keith Buschb4c6a022014-12-19 17:54:14 -0700200EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
Jens Axboe320ae512013-10-24 09:20:05 +0100201
Bart Van Assche852ec802017-06-21 10:55:47 -0700202/*
203 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
204 * mpt3sas driver such that this function can be removed.
205 */
206void blk_mq_quiesce_queue_nowait(struct request_queue *q)
207{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800208 blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
Bart Van Assche852ec802017-06-21 10:55:47 -0700209}
210EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
211
Bart Van Assche6a83e742016-11-02 10:09:51 -0600212/**
Ming Lei69e07c42017-06-06 23:22:07 +0800213 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
Bart Van Assche6a83e742016-11-02 10:09:51 -0600214 * @q: request queue.
215 *
216 * Note: this function does not prevent that the struct request end_io()
Ming Lei69e07c42017-06-06 23:22:07 +0800217 * callback function is invoked. Once this function is returned, we make
218 * sure no dispatch can happen until the queue is unquiesced via
219 * blk_mq_unquiesce_queue().
Bart Van Assche6a83e742016-11-02 10:09:51 -0600220 */
221void blk_mq_quiesce_queue(struct request_queue *q)
222{
223 struct blk_mq_hw_ctx *hctx;
224 unsigned int i;
225 bool rcu = false;
226
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800227 blk_mq_quiesce_queue_nowait(q);
Ming Leif4560ff2017-06-18 14:24:27 -0600228
Bart Van Assche6a83e742016-11-02 10:09:51 -0600229 queue_for_each_hw_ctx(q, hctx, i) {
230 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -0800231 synchronize_srcu(hctx->srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -0600232 else
233 rcu = true;
234 }
235 if (rcu)
236 synchronize_rcu();
237}
238EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
239
Ming Leie4e73912017-06-06 23:22:03 +0800240/*
241 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
242 * @q: request queue.
243 *
244 * This function recovers queue into the state before quiescing
245 * which is done by blk_mq_quiesce_queue.
246 */
247void blk_mq_unquiesce_queue(struct request_queue *q)
248{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800249 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
Ming Leif4560ff2017-06-18 14:24:27 -0600250
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800251 /* dispatch requests which are inserted during quiescing */
252 blk_mq_run_hw_queues(q, true);
Ming Leie4e73912017-06-06 23:22:03 +0800253}
254EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
255
Jens Axboeaed3ea92014-12-22 14:04:42 -0700256void blk_mq_wake_waiters(struct request_queue *q)
257{
258 struct blk_mq_hw_ctx *hctx;
259 unsigned int i;
260
261 queue_for_each_hw_ctx(q, hctx, i)
262 if (blk_mq_hw_queue_mapped(hctx))
263 blk_mq_tag_wakeup_all(hctx->tags, true);
264}
265
Jens Axboefe1f4522018-11-28 10:50:07 -0700266/*
Hou Tao9a91b052019-05-21 15:59:04 +0800267 * Only need start/end time stamping if we have iostat or
268 * blk stats enabled, or using an IO scheduler.
Jens Axboefe1f4522018-11-28 10:50:07 -0700269 */
270static inline bool blk_mq_need_time_stamp(struct request *rq)
271{
Hou Tao9a91b052019-05-21 15:59:04 +0800272 return (rq->rq_flags & (RQF_IO_STAT | RQF_STATS)) || rq->q->elevator;
Jens Axboefe1f4522018-11-28 10:50:07 -0700273}
274
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200275static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200276 unsigned int tag, u64 alloc_time_ns)
Jens Axboe320ae512013-10-24 09:20:05 +0100277{
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200278 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
279 struct request *rq = tags->static_rqs[tag];
Bart Van Asschec3a148d2017-06-20 11:15:43 -0700280
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200281 if (data->q->elevator) {
Christoph Hellwig766473682020-05-29 15:53:12 +0200282 rq->tag = BLK_MQ_NO_TAG;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200283 rq->internal_tag = tag;
284 } else {
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200285 rq->tag = tag;
Christoph Hellwig766473682020-05-29 15:53:12 +0200286 rq->internal_tag = BLK_MQ_NO_TAG;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200287 }
288
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200289 /* csd/requeue_work/fifo_time is initialized before use */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200290 rq->q = data->q;
291 rq->mq_ctx = data->ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600292 rq->mq_hctx = data->hctx;
Ming Lei568f2702020-07-06 22:41:11 +0800293 rq->rq_flags = 0;
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200294 rq->cmd_flags = data->cmd_flags;
Bart Van Assche1b6d65a2017-11-09 10:49:55 -0800295 if (data->flags & BLK_MQ_REQ_PREEMPT)
296 rq->rq_flags |= RQF_PREEMPT;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200297 if (blk_queue_io_stat(data->q))
Christoph Hellwige8064022016-10-20 15:12:13 +0200298 rq->rq_flags |= RQF_IO_STAT;
Jens Axboe7c3fb702018-01-10 11:46:39 -0700299 INIT_LIST_HEAD(&rq->queuelist);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200300 INIT_HLIST_NODE(&rq->hash);
301 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200302 rq->rq_disk = NULL;
303 rq->part = NULL;
Tejun Heo6f816b42019-08-28 15:05:57 -0700304#ifdef CONFIG_BLK_RQ_ALLOC_TIME
305 rq->alloc_time_ns = alloc_time_ns;
306#endif
Jens Axboefe1f4522018-11-28 10:50:07 -0700307 if (blk_mq_need_time_stamp(rq))
308 rq->start_time_ns = ktime_get_ns();
309 else
310 rq->start_time_ns = 0;
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700311 rq->io_start_time_ns = 0;
Hou Tao3d244302019-05-21 15:59:03 +0800312 rq->stats_sectors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200313 rq->nr_phys_segments = 0;
314#if defined(CONFIG_BLK_DEV_INTEGRITY)
315 rq->nr_integrity_segments = 0;
316#endif
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000317 blk_crypto_rq_set_defaults(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200318 /* tag was already set */
Christoph Hellwig079076b2018-11-14 17:02:05 +0100319 WRITE_ONCE(rq->deadline, 0);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200320
Jens Axboef6be4fb2014-06-06 11:03:48 -0600321 rq->timeout = 0;
322
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200323 rq->end_io = NULL;
324 rq->end_io_data = NULL;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200325
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200326 data->ctx->rq_dispatched[op_is_sync(data->cmd_flags)]++;
Keith Busch12f5b932018-05-29 15:52:28 +0200327 refcount_set(&rq->ref, 1);
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200328
329 if (!op_is_flush(data->cmd_flags)) {
330 struct elevator_queue *e = data->q->elevator;
331
332 rq->elv.icq = NULL;
333 if (e && e->type->ops.prepare_request) {
334 if (e->type->icq_cache)
335 blk_mq_sched_assign_ioc(rq);
336
337 e->type->ops.prepare_request(rq);
338 rq->rq_flags |= RQF_ELVPRIV;
339 }
340 }
341
342 data->hctx->queued++;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200343 return rq;
Jens Axboe320ae512013-10-24 09:20:05 +0100344}
345
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200346static struct request *__blk_mq_alloc_request(struct blk_mq_alloc_data *data)
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200347{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200348 struct request_queue *q = data->q;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200349 struct elevator_queue *e = q->elevator;
Tejun Heo6f816b42019-08-28 15:05:57 -0700350 u64 alloc_time_ns = 0;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200351 unsigned int tag;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200352
Tejun Heo6f816b42019-08-28 15:05:57 -0700353 /* alloc_time includes depth and tag waits */
354 if (blk_queue_rq_alloc_time(q))
355 alloc_time_ns = ktime_get_ns();
356
Jens Axboef9afca42018-10-29 13:11:38 -0600357 if (data->cmd_flags & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500358 data->flags |= BLK_MQ_REQ_NOWAIT;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200359
360 if (e) {
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200361 /*
362 * Flush requests are special and go directly to the
Jens Axboe17a51192018-05-09 13:28:50 -0600363 * dispatch list. Don't include reserved tags in the
364 * limiting, as it isn't useful.
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200365 */
Jens Axboef9afca42018-10-29 13:11:38 -0600366 if (!op_is_flush(data->cmd_flags) &&
367 e->type->ops.limit_depth &&
Jens Axboe17a51192018-05-09 13:28:50 -0600368 !(data->flags & BLK_MQ_REQ_RESERVED))
Jens Axboef9afca42018-10-29 13:11:38 -0600369 e->type->ops.limit_depth(data->cmd_flags, data);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200370 }
371
Ming Leibf0beec2020-05-29 15:53:15 +0200372retry:
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200373 data->ctx = blk_mq_get_ctx(q);
374 data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx);
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200375 if (!e)
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200376 blk_mq_tag_busy(data->hctx);
377
Ming Leibf0beec2020-05-29 15:53:15 +0200378 /*
379 * Waiting allocations only fail because of an inactive hctx. In that
380 * case just retry the hctx assignment and tag allocation as CPU hotplug
381 * should have migrated us to an online CPU by now.
382 */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200383 tag = blk_mq_get_tag(data);
Ming Leibf0beec2020-05-29 15:53:15 +0200384 if (tag == BLK_MQ_NO_TAG) {
385 if (data->flags & BLK_MQ_REQ_NOWAIT)
386 return NULL;
387
388 /*
389 * Give up the CPU and sleep for a random short time to ensure
390 * that thread using a realtime scheduling class are migrated
Randy Dunlap70f15a42020-07-30 18:42:31 -0700391 * off the CPU, and thus off the hctx that is going away.
Ming Leibf0beec2020-05-29 15:53:15 +0200392 */
393 msleep(3);
394 goto retry;
395 }
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200396 return blk_mq_rq_ctx_init(data, tag, alloc_time_ns);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200397}
398
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700399struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800400 blk_mq_req_flags_t flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100401{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200402 struct blk_mq_alloc_data data = {
403 .q = q,
404 .flags = flags,
405 .cmd_flags = op,
406 };
Jens Axboebd166ef2017-01-17 06:03:22 -0700407 struct request *rq;
Joe Lawrencea492f072014-08-28 08:15:21 -0600408 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100409
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800410 ret = blk_queue_enter(q, flags);
Joe Lawrencea492f072014-08-28 08:15:21 -0600411 if (ret)
412 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100413
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200414 rq = __blk_mq_alloc_request(&data);
Jens Axboebd166ef2017-01-17 06:03:22 -0700415 if (!rq)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200416 goto out_queue_exit;
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200417 rq->__data_len = 0;
418 rq->__sector = (sector_t) -1;
419 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100420 return rq;
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200421out_queue_exit:
422 blk_queue_exit(q);
423 return ERR_PTR(-EWOULDBLOCK);
Jens Axboe320ae512013-10-24 09:20:05 +0100424}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600425EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100426
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700427struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800428 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
Ming Lin1f5bd332016-06-13 16:45:21 +0200429{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200430 struct blk_mq_alloc_data data = {
431 .q = q,
432 .flags = flags,
433 .cmd_flags = op,
434 };
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200435 u64 alloc_time_ns = 0;
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800436 unsigned int cpu;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200437 unsigned int tag;
Ming Lin1f5bd332016-06-13 16:45:21 +0200438 int ret;
439
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200440 /* alloc_time includes depth and tag waits */
441 if (blk_queue_rq_alloc_time(q))
442 alloc_time_ns = ktime_get_ns();
443
Ming Lin1f5bd332016-06-13 16:45:21 +0200444 /*
445 * If the tag allocator sleeps we could get an allocation for a
446 * different hardware context. No need to complicate the low level
447 * allocator for this for the rare use case of a command tied to
448 * a specific queue.
449 */
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200450 if (WARN_ON_ONCE(!(flags & (BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED))))
Ming Lin1f5bd332016-06-13 16:45:21 +0200451 return ERR_PTR(-EINVAL);
452
453 if (hctx_idx >= q->nr_hw_queues)
454 return ERR_PTR(-EIO);
455
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800456 ret = blk_queue_enter(q, flags);
Ming Lin1f5bd332016-06-13 16:45:21 +0200457 if (ret)
458 return ERR_PTR(ret);
459
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600460 /*
461 * Check if the hardware context is actually mapped to anything.
462 * If not tell the caller that it should skip this queue.
463 */
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200464 ret = -EXDEV;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200465 data.hctx = q->queue_hw_ctx[hctx_idx];
466 if (!blk_mq_hw_queue_mapped(data.hctx))
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200467 goto out_queue_exit;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200468 cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
469 data.ctx = __blk_mq_get_ctx(q, cpu);
Ming Lin1f5bd332016-06-13 16:45:21 +0200470
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200471 if (!q->elevator)
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200472 blk_mq_tag_busy(data.hctx);
473
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200474 ret = -EWOULDBLOCK;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200475 tag = blk_mq_get_tag(&data);
476 if (tag == BLK_MQ_NO_TAG)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200477 goto out_queue_exit;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200478 return blk_mq_rq_ctx_init(&data, tag, alloc_time_ns);
479
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200480out_queue_exit:
481 blk_queue_exit(q);
482 return ERR_PTR(ret);
Ming Lin1f5bd332016-06-13 16:45:21 +0200483}
484EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
485
Keith Busch12f5b932018-05-29 15:52:28 +0200486static void __blk_mq_free_request(struct request *rq)
487{
488 struct request_queue *q = rq->q;
489 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600490 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Keith Busch12f5b932018-05-29 15:52:28 +0200491 const int sched_tag = rq->internal_tag;
492
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000493 blk_crypto_free_request(rq);
Bart Van Assche986d4132018-09-26 14:01:10 -0700494 blk_pm_mark_last_busy(rq);
Jens Axboeea4f9952018-10-29 15:06:13 -0600495 rq->mq_hctx = NULL;
Christoph Hellwig766473682020-05-29 15:53:12 +0200496 if (rq->tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800497 blk_mq_put_tag(hctx->tags, ctx, rq->tag);
Christoph Hellwig766473682020-05-29 15:53:12 +0200498 if (sched_tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800499 blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
Keith Busch12f5b932018-05-29 15:52:28 +0200500 blk_mq_sched_restart(hctx);
501 blk_queue_exit(q);
502}
503
Christoph Hellwig6af54052017-06-16 18:15:22 +0200504void blk_mq_free_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100505{
Jens Axboe320ae512013-10-24 09:20:05 +0100506 struct request_queue *q = rq->q;
Christoph Hellwig6af54052017-06-16 18:15:22 +0200507 struct elevator_queue *e = q->elevator;
508 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600509 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100510
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200511 if (rq->rq_flags & RQF_ELVPRIV) {
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600512 if (e && e->type->ops.finish_request)
513 e->type->ops.finish_request(rq);
Christoph Hellwig6af54052017-06-16 18:15:22 +0200514 if (rq->elv.icq) {
515 put_io_context(rq->elv.icq->ioc);
516 rq->elv.icq = NULL;
517 }
518 }
519
520 ctx->rq_completed[rq_is_sync(rq)]++;
Christoph Hellwige8064022016-10-20 15:12:13 +0200521 if (rq->rq_flags & RQF_MQ_INFLIGHT)
John Garrybccf5e22020-08-19 23:20:26 +0800522 __blk_mq_dec_active_requests(hctx);
Jens Axboe87760e52016-11-09 12:38:14 -0700523
Jens Axboe7beb2f82017-09-30 02:08:24 -0600524 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
525 laptop_io_completion(q->backing_dev_info);
526
Josef Bacika7905042018-07-03 09:32:35 -0600527 rq_qos_done(q, rq);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600528
Keith Busch12f5b932018-05-29 15:52:28 +0200529 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
530 if (refcount_dec_and_test(&rq->ref))
531 __blk_mq_free_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100532}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700533EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100534
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200535inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
Jens Axboe320ae512013-10-24 09:20:05 +0100536{
Jens Axboefe1f4522018-11-28 10:50:07 -0700537 u64 now = 0;
538
539 if (blk_mq_need_time_stamp(rq))
540 now = ktime_get_ns();
Omar Sandoval522a7772018-05-09 02:08:53 -0700541
Omar Sandoval4bc63392018-05-09 02:08:52 -0700542 if (rq->rq_flags & RQF_STATS) {
543 blk_mq_poll_stats_start(rq->q);
Omar Sandoval522a7772018-05-09 02:08:53 -0700544 blk_stat_add(rq, now);
Omar Sandoval4bc63392018-05-09 02:08:52 -0700545 }
546
Baolin Wang87890092020-07-04 15:28:21 +0800547 blk_mq_sched_completed_request(rq, now);
Omar Sandovaled886602018-09-27 15:55:51 -0700548
Omar Sandoval522a7772018-05-09 02:08:53 -0700549 blk_account_io_done(rq, now);
Ming Lei0d11e6a2013-12-05 10:50:39 -0700550
Christoph Hellwig91b63632014-04-16 09:44:53 +0200551 if (rq->end_io) {
Josef Bacika7905042018-07-03 09:32:35 -0600552 rq_qos_done(rq->q, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100553 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200554 } else {
Jens Axboe320ae512013-10-24 09:20:05 +0100555 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200556 }
Jens Axboe320ae512013-10-24 09:20:05 +0100557}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700558EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200559
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200560void blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200561{
562 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
563 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700564 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200565}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700566EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100567
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200568/*
569 * Softirq action handler - move entries to local list and loop over them
570 * while passing them to the queue registered handler.
571 */
572static __latent_entropy void blk_done_softirq(struct softirq_action *h)
573{
574 struct list_head *cpu_list, local_list;
575
576 local_irq_disable();
577 cpu_list = this_cpu_ptr(&blk_cpu_done);
578 list_replace_init(cpu_list, &local_list);
579 local_irq_enable();
580
581 while (!list_empty(&local_list)) {
582 struct request *rq;
583
584 rq = list_entry(local_list.next, struct request, ipi_list);
585 list_del_init(&rq->ipi_list);
586 rq->q->mq_ops->complete(rq);
587 }
588}
589
Christoph Hellwig115243f2020-06-11 08:44:42 +0200590static void blk_mq_trigger_softirq(struct request *rq)
591{
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200592 struct list_head *list;
593 unsigned long flags;
Christoph Hellwig115243f2020-06-11 08:44:42 +0200594
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200595 local_irq_save(flags);
596 list = this_cpu_ptr(&blk_cpu_done);
Christoph Hellwig115243f2020-06-11 08:44:42 +0200597 list_add_tail(&rq->ipi_list, list);
598
599 /*
600 * If the list only contains our just added request, signal a raise of
601 * the softirq. If there are already entries there, someone already
602 * raised the irq but it hasn't run yet.
603 */
604 if (list->next == &rq->ipi_list)
605 raise_softirq_irqoff(BLOCK_SOFTIRQ);
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200606 local_irq_restore(flags);
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200607}
608
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200609static int blk_softirq_cpu_dead(unsigned int cpu)
610{
611 /*
612 * If a CPU goes away, splice its entries to the current CPU
613 * and trigger a run of the softirq
614 */
615 local_irq_disable();
616 list_splice_init(&per_cpu(blk_cpu_done, cpu),
617 this_cpu_ptr(&blk_cpu_done));
618 raise_softirq_irqoff(BLOCK_SOFTIRQ);
619 local_irq_enable();
620
621 return 0;
622}
623
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200624
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800625static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100626{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800627 struct request *rq = data;
Jens Axboe320ae512013-10-24 09:20:05 +0100628
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200629 /*
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200630 * For most of single queue controllers, there is only one irq vector
631 * for handling I/O completion, and the only irq's affinity is set
632 * to all possible CPUs. On most of ARCHs, this affinity means the irq
633 * is handled on one specific CPU.
634 *
635 * So complete I/O requests in softirq context in case of single queue
636 * devices to avoid degrading I/O performance due to irqsoff latency.
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200637 */
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200638 if (rq->q->nr_hw_queues == 1)
639 blk_mq_trigger_softirq(rq);
640 else
641 rq->q->mq_ops->complete(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100642}
643
Christoph Hellwig963395262020-06-11 08:44:49 +0200644static inline bool blk_mq_complete_need_ipi(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100645{
Christoph Hellwig963395262020-06-11 08:44:49 +0200646 int cpu = raw_smp_processor_id();
Jens Axboe320ae512013-10-24 09:20:05 +0100647
Christoph Hellwig963395262020-06-11 08:44:49 +0200648 if (!IS_ENABLED(CONFIG_SMP) ||
649 !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags))
650 return false;
651
652 /* same CPU or cache domain? Complete locally */
653 if (cpu == rq->mq_ctx->cpu ||
654 (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) &&
655 cpus_share_cache(cpu, rq->mq_ctx->cpu)))
656 return false;
657
658 /* don't try to IPI to an offline CPU */
659 return cpu_online(rq->mq_ctx->cpu);
660}
661
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200662bool blk_mq_complete_request_remote(struct request *rq)
663{
Keith Buschaf78ff72018-11-26 09:54:30 -0700664 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
Ming Lei36e76532018-09-28 16:42:20 +0800665
Jens Axboe4ab32bf2018-11-18 16:15:35 -0700666 /*
667 * For a polled request, always complete locallly, it's pointless
668 * to redirect the completion.
669 */
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200670 if (rq->cmd_flags & REQ_HIPRI)
671 return false;
Jens Axboe320ae512013-10-24 09:20:05 +0100672
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200673 if (blk_mq_complete_need_ipi(rq)) {
Peter Zijlstra545b8c82020-06-15 11:29:31 +0200674 INIT_CSD(&rq->csd, __blk_mq_complete_request_remote, rq);
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200675 smp_call_function_single_async(rq->mq_ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800676 } else {
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200677 if (rq->q->nr_hw_queues > 1)
678 return false;
679 blk_mq_trigger_softirq(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800680 }
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200681
682 return true;
Jens Axboe320ae512013-10-24 09:20:05 +0100683}
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200684EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote);
685
Jens Axboe320ae512013-10-24 09:20:05 +0100686/**
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200687 * blk_mq_complete_request - end I/O on a request
688 * @rq: the request being processed
Jens Axboe320ae512013-10-24 09:20:05 +0100689 *
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200690 * Description:
691 * Complete a request by scheduling the ->complete_rq operation.
692 **/
693void blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100694{
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200695 if (!blk_mq_complete_request_remote(rq))
Christoph Hellwig963395262020-06-11 08:44:49 +0200696 rq->q->mq_ops->complete(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100697}
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200698EXPORT_SYMBOL(blk_mq_complete_request);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800699
Jens Axboe04ced152018-01-09 08:29:46 -0800700static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800701 __releases(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800702{
703 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
704 rcu_read_unlock();
705 else
Tejun Heo05707b62018-01-09 08:29:53 -0800706 srcu_read_unlock(hctx->srcu, srcu_idx);
Jens Axboe04ced152018-01-09 08:29:46 -0800707}
708
709static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800710 __acquires(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800711{
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700712 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
713 /* shut up gcc false positive */
714 *srcu_idx = 0;
Jens Axboe04ced152018-01-09 08:29:46 -0800715 rcu_read_lock();
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700716 } else
Tejun Heo05707b62018-01-09 08:29:53 -0800717 *srcu_idx = srcu_read_lock(hctx->srcu);
Jens Axboe04ced152018-01-09 08:29:46 -0800718}
719
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800720/**
André Almeida105663f2020-01-06 15:08:18 -0300721 * blk_mq_start_request - Start processing a request
722 * @rq: Pointer to request to be started
723 *
724 * Function used by device drivers to notify the block layer that a request
725 * is going to be processed now, so blk layer can do proper initializations
726 * such as starting the timeout timer.
727 */
Christoph Hellwige2490072014-09-13 16:40:09 -0700728void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100729{
730 struct request_queue *q = rq->q;
731
732 trace_block_rq_issue(q, rq);
733
Jens Axboecf43e6b2016-11-07 21:32:37 -0700734 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700735 rq->io_start_time_ns = ktime_get_ns();
Hou Tao3d244302019-05-21 15:59:03 +0800736 rq->stats_sectors = blk_rq_sectors(rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700737 rq->rq_flags |= RQF_STATS;
Josef Bacika7905042018-07-03 09:32:35 -0600738 rq_qos_issue(q, rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700739 }
740
Tejun Heo1d9bd512018-01-09 08:29:48 -0800741 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
Jens Axboe538b7532014-09-16 10:37:37 -0600742
Tejun Heo1d9bd512018-01-09 08:29:48 -0800743 blk_add_timer(rq);
Keith Busch12f5b932018-05-29 15:52:28 +0200744 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800745
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +0300746#ifdef CONFIG_BLK_DEV_INTEGRITY
747 if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
748 q->integrity.profile->prepare_fn(rq);
749#endif
Jens Axboe320ae512013-10-24 09:20:05 +0100750}
Christoph Hellwige2490072014-09-13 16:40:09 -0700751EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100752
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200753static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100754{
755 struct request_queue *q = rq->q;
756
Ming Lei923218f2017-11-02 23:24:38 +0800757 blk_mq_put_driver_tag(rq);
758
Jens Axboe320ae512013-10-24 09:20:05 +0100759 trace_block_rq_requeue(q, rq);
Josef Bacika7905042018-07-03 09:32:35 -0600760 rq_qos_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800761
Keith Busch12f5b932018-05-29 15:52:28 +0200762 if (blk_mq_request_started(rq)) {
763 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Christoph Hellwigda661262018-06-14 13:58:45 +0200764 rq->rq_flags &= ~RQF_TIMED_OUT;
Christoph Hellwige2490072014-09-13 16:40:09 -0700765 }
Jens Axboe320ae512013-10-24 09:20:05 +0100766}
767
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700768void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200769{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200770 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200771
Ming Lei105976f2018-02-23 23:36:56 +0800772 /* this request will be re-inserted to io scheduler queue */
773 blk_mq_sched_requeue_request(rq);
774
Jens Axboe7d692332018-10-24 10:48:12 -0600775 BUG_ON(!list_empty(&rq->queuelist));
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700776 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200777}
778EXPORT_SYMBOL(blk_mq_requeue_request);
779
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600780static void blk_mq_requeue_work(struct work_struct *work)
781{
782 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -0400783 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600784 LIST_HEAD(rq_list);
785 struct request *rq, *next;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600786
Jens Axboe18e97812017-07-27 08:03:57 -0600787 spin_lock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600788 list_splice_init(&q->requeue_list, &rq_list);
Jens Axboe18e97812017-07-27 08:03:57 -0600789 spin_unlock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600790
791 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
Jianchao Wangaef18972019-02-12 09:56:25 +0800792 if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP)))
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600793 continue;
794
Christoph Hellwige8064022016-10-20 15:12:13 +0200795 rq->rq_flags &= ~RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600796 list_del_init(&rq->queuelist);
Jianchao Wangaef18972019-02-12 09:56:25 +0800797 /*
798 * If RQF_DONTPREP, rq has contained some driver specific
799 * data, so insert it to hctx dispatch list to avoid any
800 * merge.
801 */
802 if (rq->rq_flags & RQF_DONTPREP)
Ming Lei01e99ae2020-02-25 09:04:32 +0800803 blk_mq_request_bypass_insert(rq, false, false);
Jianchao Wangaef18972019-02-12 09:56:25 +0800804 else
805 blk_mq_sched_insert_request(rq, true, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600806 }
807
808 while (!list_empty(&rq_list)) {
809 rq = list_entry(rq_list.next, struct request, queuelist);
810 list_del_init(&rq->queuelist);
Mike Snitzer9e97d292018-01-17 11:25:58 -0500811 blk_mq_sched_insert_request(rq, false, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600812 }
813
Bart Van Assche52d7f1b2016-10-28 17:20:32 -0700814 blk_mq_run_hw_queues(q, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600815}
816
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700817void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
818 bool kick_requeue_list)
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600819{
820 struct request_queue *q = rq->q;
821 unsigned long flags;
822
823 /*
824 * We abuse this flag that is otherwise used by the I/O scheduler to
Jens Axboeff821d22017-11-10 22:05:12 -0700825 * request head insertion from the workqueue.
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600826 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200827 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600828
829 spin_lock_irqsave(&q->requeue_lock, flags);
830 if (at_head) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200831 rq->rq_flags |= RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600832 list_add(&rq->queuelist, &q->requeue_list);
833 } else {
834 list_add_tail(&rq->queuelist, &q->requeue_list);
835 }
836 spin_unlock_irqrestore(&q->requeue_lock, flags);
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700837
838 if (kick_requeue_list)
839 blk_mq_kick_requeue_list(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600840}
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600841
842void blk_mq_kick_requeue_list(struct request_queue *q)
843{
Bart Van Asscheae943d22018-01-19 08:58:55 -0800844 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600845}
846EXPORT_SYMBOL(blk_mq_kick_requeue_list);
847
Mike Snitzer28494502016-09-14 13:28:30 -0400848void blk_mq_delay_kick_requeue_list(struct request_queue *q,
849 unsigned long msecs)
850{
Bart Van Assched4acf362017-08-09 11:28:06 -0700851 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
852 msecs_to_jiffies(msecs));
Mike Snitzer28494502016-09-14 13:28:30 -0400853}
854EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
855
Jens Axboe0e62f512014-06-04 10:23:49 -0600856struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
857{
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600858 if (tag < tags->nr_tags) {
859 prefetch(tags->rqs[tag]);
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700860 return tags->rqs[tag];
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600861 }
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700862
863 return NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600864}
865EXPORT_SYMBOL(blk_mq_tag_to_rq);
866
Jens Axboe3c94d832018-12-17 21:11:17 -0700867static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq,
868 void *priv, bool reserved)
Jens Axboeae879912018-11-08 09:03:51 -0700869{
870 /*
Ming Lei05a4fed2020-07-07 11:04:33 -0400871 * If we find a request that isn't idle and the queue matches,
Jens Axboe3c94d832018-12-17 21:11:17 -0700872 * we know the queue is busy. Return false to stop the iteration.
Jens Axboeae879912018-11-08 09:03:51 -0700873 */
Ming Lei05a4fed2020-07-07 11:04:33 -0400874 if (blk_mq_request_started(rq) && rq->q == hctx->queue) {
Jens Axboeae879912018-11-08 09:03:51 -0700875 bool *busy = priv;
876
877 *busy = true;
878 return false;
879 }
880
881 return true;
882}
883
Jens Axboe3c94d832018-12-17 21:11:17 -0700884bool blk_mq_queue_inflight(struct request_queue *q)
Jens Axboeae879912018-11-08 09:03:51 -0700885{
886 bool busy = false;
887
Jens Axboe3c94d832018-12-17 21:11:17 -0700888 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
Jens Axboeae879912018-11-08 09:03:51 -0700889 return busy;
890}
Jens Axboe3c94d832018-12-17 21:11:17 -0700891EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
Jens Axboeae879912018-11-08 09:03:51 -0700892
Tejun Heo358f70d2018-01-09 08:29:50 -0800893static void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100894{
Christoph Hellwigda661262018-06-14 13:58:45 +0200895 req->rq_flags |= RQF_TIMED_OUT;
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200896 if (req->q->mq_ops->timeout) {
897 enum blk_eh_timer_return ret;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600898
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200899 ret = req->q->mq_ops->timeout(req, reserved);
900 if (ret == BLK_EH_DONE)
901 return;
902 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700903 }
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200904
905 blk_add_timer(req);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600906}
Keith Busch5b3f25f2015-01-07 18:55:46 -0700907
Keith Busch12f5b932018-05-29 15:52:28 +0200908static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
909{
910 unsigned long deadline;
911
912 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
913 return false;
Christoph Hellwigda661262018-06-14 13:58:45 +0200914 if (rq->rq_flags & RQF_TIMED_OUT)
915 return false;
Keith Busch12f5b932018-05-29 15:52:28 +0200916
Christoph Hellwig079076b2018-11-14 17:02:05 +0100917 deadline = READ_ONCE(rq->deadline);
Keith Busch12f5b932018-05-29 15:52:28 +0200918 if (time_after_eq(jiffies, deadline))
919 return true;
920
921 if (*next == 0)
922 *next = deadline;
923 else if (time_after(*next, deadline))
924 *next = deadline;
925 return false;
926}
927
Jens Axboe7baa8572018-11-08 10:24:07 -0700928static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700929 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100930{
Keith Busch12f5b932018-05-29 15:52:28 +0200931 unsigned long *next = priv;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700932
Keith Busch12f5b932018-05-29 15:52:28 +0200933 /*
934 * Just do a quick check if it is expired before locking the request in
935 * so we're not unnecessarilly synchronizing across CPUs.
936 */
937 if (!blk_mq_req_expired(rq, next))
Jens Axboe7baa8572018-11-08 10:24:07 -0700938 return true;
Jens Axboe320ae512013-10-24 09:20:05 +0100939
Tejun Heo1d9bd512018-01-09 08:29:48 -0800940 /*
Keith Busch12f5b932018-05-29 15:52:28 +0200941 * We have reason to believe the request may be expired. Take a
942 * reference on the request to lock this request lifetime into its
943 * currently allocated context to prevent it from being reallocated in
944 * the event the completion by-passes this timeout handler.
945 *
946 * If the reference was already released, then the driver beat the
947 * timeout handler to posting a natural completion.
Tejun Heo1d9bd512018-01-09 08:29:48 -0800948 */
Keith Busch12f5b932018-05-29 15:52:28 +0200949 if (!refcount_inc_not_zero(&rq->ref))
Jens Axboe7baa8572018-11-08 10:24:07 -0700950 return true;
Keith Busch12f5b932018-05-29 15:52:28 +0200951
952 /*
953 * The request is now locked and cannot be reallocated underneath the
954 * timeout handler's processing. Re-verify this exact request is truly
955 * expired; if it is not expired, then the request was completed and
956 * reallocated as a new request.
957 */
958 if (blk_mq_req_expired(rq, next))
Tejun Heo1d9bd512018-01-09 08:29:48 -0800959 blk_mq_rq_timed_out(rq, reserved);
Yufen Yu8d699662019-09-27 16:19:55 +0800960
961 if (is_flush_rq(rq, hctx))
962 rq->end_io(rq, 0);
963 else if (refcount_dec_and_test(&rq->ref))
Keith Busch12f5b932018-05-29 15:52:28 +0200964 __blk_mq_free_request(rq);
Jens Axboe7baa8572018-11-08 10:24:07 -0700965
966 return true;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800967}
968
Christoph Hellwig287922e2015-10-30 20:57:30 +0800969static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100970{
Christoph Hellwig287922e2015-10-30 20:57:30 +0800971 struct request_queue *q =
972 container_of(work, struct request_queue, timeout_work);
Keith Busch12f5b932018-05-29 15:52:28 +0200973 unsigned long next = 0;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800974 struct blk_mq_hw_ctx *hctx;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700975 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100976
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600977 /* A deadlock might occur if a request is stuck requiring a
978 * timeout at the same time a queue freeze is waiting
979 * completion, since the timeout code would not be able to
980 * acquire the queue reference here.
981 *
982 * That's why we don't use blk_queue_enter here; instead, we use
983 * percpu_ref_tryget directly, because we need to be able to
984 * obtain a reference even in the short window between the queue
985 * starting to freeze, by dropping the first reference in
Ming Lei1671d522017-03-27 20:06:57 +0800986 * blk_freeze_queue_start, and the moment the last request is
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600987 * consumed, marked by the instant q_usage_counter reaches
988 * zero.
989 */
990 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +0800991 return;
992
Keith Busch12f5b932018-05-29 15:52:28 +0200993 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
Jens Axboe320ae512013-10-24 09:20:05 +0100994
Keith Busch12f5b932018-05-29 15:52:28 +0200995 if (next != 0) {
996 mod_timer(&q->timeout, next);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600997 } else {
Bart Van Asschefcd36c32018-01-10 08:33:33 -0800998 /*
999 * Request timeouts are handled as a forward rolling timer. If
1000 * we end up here it means that no requests are pending and
1001 * also that no request has been pending for a while. Mark
1002 * each hctx as idle.
1003 */
Ming Leif054b562015-04-21 10:00:19 +08001004 queue_for_each_hw_ctx(q, hctx, i) {
1005 /* the hctx may be unmapped, so check it here */
1006 if (blk_mq_hw_queue_mapped(hctx))
1007 blk_mq_tag_idle(hctx);
1008 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06001009 }
Christoph Hellwig287922e2015-10-30 20:57:30 +08001010 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001011}
1012
Omar Sandoval88459642016-09-17 08:38:44 -06001013struct flush_busy_ctx_data {
1014 struct blk_mq_hw_ctx *hctx;
1015 struct list_head *list;
1016};
1017
1018static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
1019{
1020 struct flush_busy_ctx_data *flush_data = data;
1021 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
1022 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001023 enum hctx_type type = hctx->type;
Omar Sandoval88459642016-09-17 08:38:44 -06001024
Omar Sandoval88459642016-09-17 08:38:44 -06001025 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001026 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
Omar Sandovale9a99a62018-02-27 16:56:42 -08001027 sbitmap_clear_bit(sb, bitnr);
Omar Sandoval88459642016-09-17 08:38:44 -06001028 spin_unlock(&ctx->lock);
1029 return true;
1030}
1031
Jens Axboe320ae512013-10-24 09:20:05 +01001032/*
Jens Axboe1429d7c2014-05-19 09:23:55 -06001033 * Process software queues that have been marked busy, splicing them
1034 * to the for-dispatch
1035 */
Jens Axboe2c3ad662016-12-14 14:34:47 -07001036void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
Jens Axboe1429d7c2014-05-19 09:23:55 -06001037{
Omar Sandoval88459642016-09-17 08:38:44 -06001038 struct flush_busy_ctx_data data = {
1039 .hctx = hctx,
1040 .list = list,
1041 };
Jens Axboe1429d7c2014-05-19 09:23:55 -06001042
Omar Sandoval88459642016-09-17 08:38:44 -06001043 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001044}
Jens Axboe2c3ad662016-12-14 14:34:47 -07001045EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001046
Ming Leib3476892017-10-14 17:22:30 +08001047struct dispatch_rq_data {
1048 struct blk_mq_hw_ctx *hctx;
1049 struct request *rq;
1050};
1051
1052static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
1053 void *data)
1054{
1055 struct dispatch_rq_data *dispatch_data = data;
1056 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
1057 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001058 enum hctx_type type = hctx->type;
Ming Leib3476892017-10-14 17:22:30 +08001059
1060 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001061 if (!list_empty(&ctx->rq_lists[type])) {
1062 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
Ming Leib3476892017-10-14 17:22:30 +08001063 list_del_init(&dispatch_data->rq->queuelist);
Ming Leic16d6b52018-12-17 08:44:05 -07001064 if (list_empty(&ctx->rq_lists[type]))
Ming Leib3476892017-10-14 17:22:30 +08001065 sbitmap_clear_bit(sb, bitnr);
1066 }
1067 spin_unlock(&ctx->lock);
1068
1069 return !dispatch_data->rq;
1070}
1071
1072struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
1073 struct blk_mq_ctx *start)
1074{
Jens Axboef31967f2018-10-29 13:13:29 -06001075 unsigned off = start ? start->index_hw[hctx->type] : 0;
Ming Leib3476892017-10-14 17:22:30 +08001076 struct dispatch_rq_data data = {
1077 .hctx = hctx,
1078 .rq = NULL,
1079 };
1080
1081 __sbitmap_for_each_set(&hctx->ctx_map, off,
1082 dispatch_rq_from_ctx, &data);
1083
1084 return data.rq;
1085}
1086
Jens Axboe703fd1c2016-09-16 13:59:14 -06001087static inline unsigned int queued_to_index(unsigned int queued)
1088{
1089 if (!queued)
1090 return 0;
Jens Axboe1429d7c2014-05-19 09:23:55 -06001091
Jens Axboe703fd1c2016-09-16 13:59:14 -06001092 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001093}
1094
Ming Lei570e9b72020-06-30 22:03:55 +08001095static bool __blk_mq_get_driver_tag(struct request *rq)
1096{
John Garry222a5ae2020-08-19 23:20:23 +08001097 struct sbitmap_queue *bt = rq->mq_hctx->tags->bitmap_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001098 unsigned int tag_offset = rq->mq_hctx->tags->nr_reserved_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001099 int tag;
1100
Ming Lei568f2702020-07-06 22:41:11 +08001101 blk_mq_tag_busy(rq->mq_hctx);
1102
Ming Lei570e9b72020-06-30 22:03:55 +08001103 if (blk_mq_tag_is_reserved(rq->mq_hctx->sched_tags, rq->internal_tag)) {
John Garry222a5ae2020-08-19 23:20:23 +08001104 bt = rq->mq_hctx->tags->breserved_tags;
Ming Lei570e9b72020-06-30 22:03:55 +08001105 tag_offset = 0;
Ming Lei28500852020-09-11 18:41:14 +08001106 } else {
1107 if (!hctx_may_queue(rq->mq_hctx, bt))
1108 return false;
Ming Lei570e9b72020-06-30 22:03:55 +08001109 }
1110
Ming Lei570e9b72020-06-30 22:03:55 +08001111 tag = __sbitmap_queue_get(bt);
1112 if (tag == BLK_MQ_NO_TAG)
1113 return false;
1114
1115 rq->tag = tag + tag_offset;
Ming Lei570e9b72020-06-30 22:03:55 +08001116 return true;
1117}
1118
1119static bool blk_mq_get_driver_tag(struct request *rq)
1120{
Ming Lei568f2702020-07-06 22:41:11 +08001121 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1122
1123 if (rq->tag == BLK_MQ_NO_TAG && !__blk_mq_get_driver_tag(rq))
1124 return false;
1125
Ming Lei51db1c32020-08-19 23:20:19 +08001126 if ((hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED) &&
Ming Lei568f2702020-07-06 22:41:11 +08001127 !(rq->rq_flags & RQF_MQ_INFLIGHT)) {
1128 rq->rq_flags |= RQF_MQ_INFLIGHT;
John Garrybccf5e22020-08-19 23:20:26 +08001129 __blk_mq_inc_active_requests(hctx);
Ming Lei568f2702020-07-06 22:41:11 +08001130 }
1131 hctx->tags->rqs[rq->tag] = rq;
1132 return true;
Ming Lei570e9b72020-06-30 22:03:55 +08001133}
1134
Jens Axboeeb619fd2017-11-09 08:32:43 -07001135static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1136 int flags, void *key)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001137{
1138 struct blk_mq_hw_ctx *hctx;
1139
1140 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1141
Ming Lei5815839b2018-06-25 19:31:47 +08001142 spin_lock(&hctx->dispatch_wait_lock);
Jens Axboee8618572019-03-25 12:34:10 -06001143 if (!list_empty(&wait->entry)) {
1144 struct sbitmap_queue *sbq;
1145
1146 list_del_init(&wait->entry);
John Garry222a5ae2020-08-19 23:20:23 +08001147 sbq = hctx->tags->bitmap_tags;
Jens Axboee8618572019-03-25 12:34:10 -06001148 atomic_dec(&sbq->ws_active);
1149 }
Ming Lei5815839b2018-06-25 19:31:47 +08001150 spin_unlock(&hctx->dispatch_wait_lock);
1151
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001152 blk_mq_run_hw_queue(hctx, true);
1153 return 1;
1154}
1155
Jens Axboef906a6a2017-11-09 16:10:13 -07001156/*
1157 * Mark us waiting for a tag. For shared tags, this involves hooking us into
Bart Van Asscheee3e4de2018-01-09 10:09:15 -08001158 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1159 * restart. For both cases, take care to check the condition again after
Jens Axboef906a6a2017-11-09 16:10:13 -07001160 * marking us as waiting.
1161 */
Ming Lei2278d692018-06-25 19:31:46 +08001162static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
Jens Axboef906a6a2017-11-09 16:10:13 -07001163 struct request *rq)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001164{
John Garry222a5ae2020-08-19 23:20:23 +08001165 struct sbitmap_queue *sbq = hctx->tags->bitmap_tags;
Ming Lei5815839b2018-06-25 19:31:47 +08001166 struct wait_queue_head *wq;
Jens Axboef906a6a2017-11-09 16:10:13 -07001167 wait_queue_entry_t *wait;
1168 bool ret;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001169
Ming Lei51db1c32020-08-19 23:20:19 +08001170 if (!(hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
Yufen Yu684b7322019-03-15 11:05:10 +08001171 blk_mq_sched_mark_restart_hctx(hctx);
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001172
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001173 /*
1174 * It's possible that a tag was freed in the window between the
1175 * allocation failure and adding the hardware queue to the wait
1176 * queue.
1177 *
1178 * Don't clear RESTART here, someone else could have set it.
1179 * At most this will cost an extra queue run.
1180 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001181 return blk_mq_get_driver_tag(rq);
Jens Axboeeb619fd2017-11-09 08:32:43 -07001182 }
1183
Ming Lei2278d692018-06-25 19:31:46 +08001184 wait = &hctx->dispatch_wait;
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001185 if (!list_empty_careful(&wait->entry))
1186 return false;
1187
Jens Axboee8618572019-03-25 12:34:10 -06001188 wq = &bt_wait_ptr(sbq, hctx)->wait;
Ming Lei5815839b2018-06-25 19:31:47 +08001189
1190 spin_lock_irq(&wq->lock);
1191 spin_lock(&hctx->dispatch_wait_lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001192 if (!list_empty(&wait->entry)) {
Ming Lei5815839b2018-06-25 19:31:47 +08001193 spin_unlock(&hctx->dispatch_wait_lock);
1194 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001195 return false;
1196 }
1197
Jens Axboee8618572019-03-25 12:34:10 -06001198 atomic_inc(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001199 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1200 __add_wait_queue(wq, wait);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001201
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001202 /*
Jens Axboeeb619fd2017-11-09 08:32:43 -07001203 * It's possible that a tag was freed in the window between the
1204 * allocation failure and adding the hardware queue to the wait
1205 * queue.
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001206 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001207 ret = blk_mq_get_driver_tag(rq);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001208 if (!ret) {
Ming Lei5815839b2018-06-25 19:31:47 +08001209 spin_unlock(&hctx->dispatch_wait_lock);
1210 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001211 return false;
Jens Axboef906a6a2017-11-09 16:10:13 -07001212 }
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001213
1214 /*
1215 * We got a tag, remove ourselves from the wait queue to ensure
1216 * someone else gets the wakeup.
1217 */
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001218 list_del_init(&wait->entry);
Jens Axboee8618572019-03-25 12:34:10 -06001219 atomic_dec(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001220 spin_unlock(&hctx->dispatch_wait_lock);
1221 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001222
1223 return true;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001224}
1225
Ming Lei6e7687172018-07-03 09:03:16 -06001226#define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1227#define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1228/*
1229 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1230 * - EWMA is one simple way to compute running average value
1231 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1232 * - take 4 as factor for avoiding to get too small(0) result, and this
1233 * factor doesn't matter because EWMA decreases exponentially
1234 */
1235static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1236{
1237 unsigned int ewma;
1238
1239 if (hctx->queue->elevator)
1240 return;
1241
1242 ewma = hctx->dispatch_busy;
1243
1244 if (!ewma && !busy)
1245 return;
1246
1247 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1248 if (busy)
1249 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1250 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1251
1252 hctx->dispatch_busy = ewma;
1253}
1254
Ming Lei86ff7c22018-01-30 22:04:57 -05001255#define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1256
Johannes Thumshirnc92a4102020-03-25 00:24:44 +09001257static void blk_mq_handle_dev_resource(struct request *rq,
1258 struct list_head *list)
1259{
1260 struct request *next =
1261 list_first_entry_or_null(list, struct request, queuelist);
1262
1263 /*
1264 * If an I/O scheduler has been configured and we got a driver tag for
1265 * the next request already, free it.
1266 */
1267 if (next)
1268 blk_mq_put_driver_tag(next);
1269
1270 list_add(&rq->queuelist, list);
1271 __blk_mq_requeue_request(rq);
1272}
1273
Keith Busch0512a752020-05-12 17:55:47 +09001274static void blk_mq_handle_zone_resource(struct request *rq,
1275 struct list_head *zone_list)
1276{
1277 /*
1278 * If we end up here it is because we cannot dispatch a request to a
1279 * specific zone due to LLD level zone-write locking or other zone
1280 * related resource not being available. In this case, set the request
1281 * aside in zone_list for retrying it later.
1282 */
1283 list_add(&rq->queuelist, zone_list);
1284 __blk_mq_requeue_request(rq);
1285}
1286
Ming Lei75383522020-06-30 18:24:58 +08001287enum prep_dispatch {
1288 PREP_DISPATCH_OK,
1289 PREP_DISPATCH_NO_TAG,
1290 PREP_DISPATCH_NO_BUDGET,
1291};
1292
1293static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq,
1294 bool need_budget)
1295{
1296 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1297
1298 if (need_budget && !blk_mq_get_dispatch_budget(rq->q)) {
1299 blk_mq_put_driver_tag(rq);
1300 return PREP_DISPATCH_NO_BUDGET;
1301 }
1302
1303 if (!blk_mq_get_driver_tag(rq)) {
1304 /*
1305 * The initial allocation attempt failed, so we need to
1306 * rerun the hardware queue when a tag is freed. The
1307 * waitqueue takes care of that. If the queue is run
1308 * before we add this entry back on the dispatch list,
1309 * we'll re-run it below.
1310 */
1311 if (!blk_mq_mark_tag_wait(hctx, rq)) {
Ming Lei1fd40b52020-06-30 18:25:00 +08001312 /*
1313 * All budgets not got from this function will be put
1314 * together during handling partial dispatch
1315 */
1316 if (need_budget)
1317 blk_mq_put_dispatch_budget(rq->q);
Ming Lei75383522020-06-30 18:24:58 +08001318 return PREP_DISPATCH_NO_TAG;
1319 }
1320 }
1321
1322 return PREP_DISPATCH_OK;
1323}
1324
Ming Lei1fd40b52020-06-30 18:25:00 +08001325/* release all allocated budgets before calling to blk_mq_dispatch_rq_list */
1326static void blk_mq_release_budgets(struct request_queue *q,
1327 unsigned int nr_budgets)
1328{
1329 int i;
1330
1331 for (i = 0; i < nr_budgets; i++)
1332 blk_mq_put_dispatch_budget(q);
1333}
1334
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001335/*
1336 * Returns true if we did some work AND can potentially do more.
1337 */
Ming Lei445874e2020-06-30 18:24:57 +08001338bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list,
Ming Lei1fd40b52020-06-30 18:25:00 +08001339 unsigned int nr_budgets)
Jens Axboef04c3df2016-12-07 08:41:17 -07001340{
Ming Lei75383522020-06-30 18:24:58 +08001341 enum prep_dispatch prep;
Ming Lei445874e2020-06-30 18:24:57 +08001342 struct request_queue *q = hctx->queue;
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001343 struct request *rq, *nxt;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001344 int errors, queued;
Ming Lei86ff7c22018-01-30 22:04:57 -05001345 blk_status_t ret = BLK_STS_OK;
Keith Busch0512a752020-05-12 17:55:47 +09001346 LIST_HEAD(zone_list);
Jens Axboef04c3df2016-12-07 08:41:17 -07001347
Omar Sandoval81380ca2017-04-07 08:56:26 -06001348 if (list_empty(list))
1349 return false;
1350
Jens Axboef04c3df2016-12-07 08:41:17 -07001351 /*
Jens Axboef04c3df2016-12-07 08:41:17 -07001352 * Now process all the entries, sending them to the driver.
1353 */
Jens Axboe93efe982017-03-24 12:04:19 -06001354 errors = queued = 0;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001355 do {
Jens Axboef04c3df2016-12-07 08:41:17 -07001356 struct blk_mq_queue_data bd;
1357
1358 rq = list_first_entry(list, struct request, queuelist);
Ming Lei0bca7992018-04-05 00:35:21 +08001359
Ming Lei445874e2020-06-30 18:24:57 +08001360 WARN_ON_ONCE(hctx != rq->mq_hctx);
Ming Lei1fd40b52020-06-30 18:25:00 +08001361 prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets);
Ming Lei75383522020-06-30 18:24:58 +08001362 if (prep != PREP_DISPATCH_OK)
Ming Lei0bca7992018-04-05 00:35:21 +08001363 break;
Ming Leide148292017-10-14 17:22:29 +08001364
Jens Axboef04c3df2016-12-07 08:41:17 -07001365 list_del_init(&rq->queuelist);
1366
1367 bd.rq = rq;
Jens Axboe113285b2017-03-02 13:26:04 -07001368
1369 /*
1370 * Flag last if we have no more requests, or if we have more
1371 * but can't assign a driver tag to it.
1372 */
1373 if (list_empty(list))
1374 bd.last = true;
1375 else {
Jens Axboe113285b2017-03-02 13:26:04 -07001376 nxt = list_first_entry(list, struct request, queuelist);
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001377 bd.last = !blk_mq_get_driver_tag(nxt);
Jens Axboe113285b2017-03-02 13:26:04 -07001378 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001379
Ming Lei1fd40b52020-06-30 18:25:00 +08001380 /*
1381 * once the request is queued to lld, no need to cover the
1382 * budget any more
1383 */
1384 if (nr_budgets)
1385 nr_budgets--;
Jens Axboef04c3df2016-12-07 08:41:17 -07001386 ret = q->mq_ops->queue_rq(hctx, &bd);
Ming Lei7bf13722020-07-01 21:58:57 +08001387 switch (ret) {
1388 case BLK_STS_OK:
1389 queued++;
Jens Axboef04c3df2016-12-07 08:41:17 -07001390 break;
Ming Lei7bf13722020-07-01 21:58:57 +08001391 case BLK_STS_RESOURCE:
1392 case BLK_STS_DEV_RESOURCE:
1393 blk_mq_handle_dev_resource(rq, list);
1394 goto out;
1395 case BLK_STS_ZONE_RESOURCE:
Keith Busch0512a752020-05-12 17:55:47 +09001396 /*
1397 * Move the request to zone_list and keep going through
1398 * the dispatch list to find more requests the drive can
1399 * accept.
1400 */
1401 blk_mq_handle_zone_resource(rq, &zone_list);
Ming Lei7bf13722020-07-01 21:58:57 +08001402 break;
1403 default:
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001404 errors++;
1405 blk_mq_end_request(rq, BLK_STS_IOERR);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001406 }
Omar Sandoval81380ca2017-04-07 08:56:26 -06001407 } while (!list_empty(list));
Ming Lei7bf13722020-07-01 21:58:57 +08001408out:
Keith Busch0512a752020-05-12 17:55:47 +09001409 if (!list_empty(&zone_list))
1410 list_splice_tail_init(&zone_list, list);
1411
Jens Axboef04c3df2016-12-07 08:41:17 -07001412 hctx->dispatched[queued_to_index(queued)]++;
1413
yangerkun632bfb62020-09-05 19:25:56 +08001414 /* If we didn't flush the entire list, we could have told the driver
1415 * there was more coming, but that turned out to be a lie.
1416 */
1417 if ((!list_empty(list) || errors) && q->mq_ops->commit_rqs && queued)
1418 q->mq_ops->commit_rqs(hctx);
Jens Axboef04c3df2016-12-07 08:41:17 -07001419 /*
1420 * Any items that need requeuing? Stuff them into hctx->dispatch,
1421 * that is where we will continue on next queue run.
1422 */
1423 if (!list_empty(list)) {
Ming Lei86ff7c22018-01-30 22:04:57 -05001424 bool needs_restart;
Ming Lei75383522020-06-30 18:24:58 +08001425 /* For non-shared tags, the RESTART check will suffice */
1426 bool no_tag = prep == PREP_DISPATCH_NO_TAG &&
Ming Lei51db1c32020-08-19 23:20:19 +08001427 (hctx->flags & BLK_MQ_F_TAG_QUEUE_SHARED);
Ming Lei75383522020-06-30 18:24:58 +08001428 bool no_budget_avail = prep == PREP_DISPATCH_NO_BUDGET;
Ming Lei86ff7c22018-01-30 22:04:57 -05001429
Ming Lei1fd40b52020-06-30 18:25:00 +08001430 blk_mq_release_budgets(q, nr_budgets);
Jens Axboef04c3df2016-12-07 08:41:17 -07001431
1432 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001433 list_splice_tail_init(list, &hctx->dispatch);
Jens Axboef04c3df2016-12-07 08:41:17 -07001434 spin_unlock(&hctx->lock);
1435
1436 /*
Ming Leid7d85352020-08-17 18:01:15 +08001437 * Order adding requests to hctx->dispatch and checking
1438 * SCHED_RESTART flag. The pair of this smp_mb() is the one
1439 * in blk_mq_sched_restart(). Avoid restart code path to
1440 * miss the new added requests to hctx->dispatch, meantime
1441 * SCHED_RESTART is observed here.
1442 */
1443 smp_mb();
1444
1445 /*
Bart Van Assche710c7852017-04-07 11:16:51 -07001446 * If SCHED_RESTART was set by the caller of this function and
1447 * it is no longer set that means that it was cleared by another
1448 * thread and hence that a queue rerun is needed.
Jens Axboef04c3df2016-12-07 08:41:17 -07001449 *
Jens Axboeeb619fd2017-11-09 08:32:43 -07001450 * If 'no_tag' is set, that means that we failed getting
1451 * a driver tag with an I/O scheduler attached. If our dispatch
1452 * waitqueue is no longer active, ensure that we run the queue
1453 * AFTER adding our entries back to the list.
Jens Axboebd166ef2017-01-17 06:03:22 -07001454 *
Bart Van Assche710c7852017-04-07 11:16:51 -07001455 * If no I/O scheduler has been configured it is possible that
1456 * the hardware queue got stopped and restarted before requests
1457 * were pushed back onto the dispatch list. Rerun the queue to
1458 * avoid starvation. Notes:
1459 * - blk_mq_run_hw_queue() checks whether or not a queue has
1460 * been stopped before rerunning a queue.
1461 * - Some but not all block drivers stop a queue before
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001462 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
Bart Van Assche710c7852017-04-07 11:16:51 -07001463 * and dm-rq.
Ming Lei86ff7c22018-01-30 22:04:57 -05001464 *
1465 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1466 * bit is set, run queue after a delay to avoid IO stalls
Douglas Andersonab3cee32020-04-20 09:24:51 -07001467 * that could otherwise occur if the queue is idle. We'll do
1468 * similar if we couldn't get budget and SCHED_RESTART is set.
Jens Axboebd166ef2017-01-17 06:03:22 -07001469 */
Ming Lei86ff7c22018-01-30 22:04:57 -05001470 needs_restart = blk_mq_sched_needs_restart(hctx);
1471 if (!needs_restart ||
Jens Axboeeb619fd2017-11-09 08:32:43 -07001472 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
Jens Axboebd166ef2017-01-17 06:03:22 -07001473 blk_mq_run_hw_queue(hctx, true);
Douglas Andersonab3cee32020-04-20 09:24:51 -07001474 else if (needs_restart && (ret == BLK_STS_RESOURCE ||
1475 no_budget_avail))
Ming Lei86ff7c22018-01-30 22:04:57 -05001476 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001477
Ming Lei6e7687172018-07-03 09:03:16 -06001478 blk_mq_update_dispatch_busy(hctx, true);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001479 return false;
Ming Lei6e7687172018-07-03 09:03:16 -06001480 } else
1481 blk_mq_update_dispatch_busy(hctx, false);
Jens Axboef04c3df2016-12-07 08:41:17 -07001482
Jens Axboe93efe982017-03-24 12:04:19 -06001483 return (queued + errors) != 0;
Jens Axboef04c3df2016-12-07 08:41:17 -07001484}
1485
André Almeida105663f2020-01-06 15:08:18 -03001486/**
1487 * __blk_mq_run_hw_queue - Run a hardware queue.
1488 * @hctx: Pointer to the hardware queue to run.
1489 *
1490 * Send pending requests to the hardware.
1491 */
Bart Van Assche6a83e742016-11-02 10:09:51 -06001492static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1493{
1494 int srcu_idx;
1495
Jens Axboeb7a71e62017-08-01 09:28:24 -06001496 /*
1497 * We should be running this queue from one of the CPUs that
1498 * are mapped to it.
Ming Lei7df938f2018-01-18 00:41:52 +08001499 *
1500 * There are at least two related races now between setting
1501 * hctx->next_cpu from blk_mq_hctx_next_cpu() and running
1502 * __blk_mq_run_hw_queue():
1503 *
1504 * - hctx->next_cpu is found offline in blk_mq_hctx_next_cpu(),
1505 * but later it becomes online, then this warning is harmless
1506 * at all
1507 *
1508 * - hctx->next_cpu is found online in blk_mq_hctx_next_cpu(),
1509 * but later it becomes offline, then the warning can't be
1510 * triggered, and we depend on blk-mq timeout handler to
1511 * handle dispatched requests to this hctx
Jens Axboeb7a71e62017-08-01 09:28:24 -06001512 */
Ming Lei7df938f2018-01-18 00:41:52 +08001513 if (!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
1514 cpu_online(hctx->next_cpu)) {
1515 printk(KERN_WARNING "run queue from wrong CPU %d, hctx %s\n",
1516 raw_smp_processor_id(),
1517 cpumask_empty(hctx->cpumask) ? "inactive": "active");
1518 dump_stack();
1519 }
Bart Van Assche6a83e742016-11-02 10:09:51 -06001520
Jens Axboeb7a71e62017-08-01 09:28:24 -06001521 /*
1522 * We can't run the queue inline with ints disabled. Ensure that
1523 * we catch bad users of this early.
1524 */
1525 WARN_ON_ONCE(in_interrupt());
1526
Jens Axboe04ced152018-01-09 08:29:46 -08001527 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
Jens Axboebf4907c2017-03-30 12:30:39 -06001528
Jens Axboe04ced152018-01-09 08:29:46 -08001529 hctx_lock(hctx, &srcu_idx);
1530 blk_mq_sched_dispatch_requests(hctx);
1531 hctx_unlock(hctx, srcu_idx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001532}
1533
Ming Leif82ddf12018-04-08 17:48:10 +08001534static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1535{
1536 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1537
1538 if (cpu >= nr_cpu_ids)
1539 cpu = cpumask_first(hctx->cpumask);
1540 return cpu;
1541}
1542
Jens Axboe506e9312014-05-07 10:26:44 -06001543/*
1544 * It'd be great if the workqueue API had a way to pass
1545 * in a mask and had some smarts for more clever placement.
1546 * For now we just round-robin here, switching for every
1547 * BLK_MQ_CPU_WORK_BATCH queued items.
1548 */
1549static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1550{
Ming Lei7bed4592018-01-18 00:41:51 +08001551 bool tried = false;
Ming Lei476f8c92018-04-08 17:48:09 +08001552 int next_cpu = hctx->next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001553
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001554 if (hctx->queue->nr_hw_queues == 1)
1555 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -06001556
1557 if (--hctx->next_cpu_batch <= 0) {
Ming Lei7bed4592018-01-18 00:41:51 +08001558select_cpu:
Ming Lei476f8c92018-04-08 17:48:09 +08001559 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08001560 cpu_online_mask);
Jens Axboe506e9312014-05-07 10:26:44 -06001561 if (next_cpu >= nr_cpu_ids)
Ming Leif82ddf12018-04-08 17:48:10 +08001562 next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06001563 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1564 }
1565
Ming Lei7bed4592018-01-18 00:41:51 +08001566 /*
1567 * Do unbound schedule if we can't find a online CPU for this hctx,
1568 * and it should only happen in the path of handling CPU DEAD.
1569 */
Ming Lei476f8c92018-04-08 17:48:09 +08001570 if (!cpu_online(next_cpu)) {
Ming Lei7bed4592018-01-18 00:41:51 +08001571 if (!tried) {
1572 tried = true;
1573 goto select_cpu;
1574 }
1575
1576 /*
1577 * Make sure to re-select CPU next time once after CPUs
1578 * in hctx->cpumask become online again.
1579 */
Ming Lei476f8c92018-04-08 17:48:09 +08001580 hctx->next_cpu = next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001581 hctx->next_cpu_batch = 1;
1582 return WORK_CPU_UNBOUND;
1583 }
Ming Lei476f8c92018-04-08 17:48:09 +08001584
1585 hctx->next_cpu = next_cpu;
1586 return next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -06001587}
1588
André Almeida105663f2020-01-06 15:08:18 -03001589/**
1590 * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue.
1591 * @hctx: Pointer to the hardware queue to run.
1592 * @async: If we want to run the queue asynchronously.
1593 * @msecs: Microseconds of delay to wait before running the queue.
1594 *
1595 * If !@async, try to run the queue now. Else, run the queue asynchronously and
1596 * with a delay of @msecs.
1597 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001598static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1599 unsigned long msecs)
Jens Axboe320ae512013-10-24 09:20:05 +01001600{
Bart Van Assche5435c022017-06-20 11:15:49 -07001601 if (unlikely(blk_mq_hctx_stopped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +01001602 return;
1603
Jens Axboe1b792f22016-09-21 10:12:13 -06001604 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001605 int cpu = get_cpu();
1606 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +01001607 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001608 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +01001609 return;
1610 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001611
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001612 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -06001613 }
Paolo Bonzini398205b2014-11-07 23:03:59 +01001614
Bart Van Asscheae943d22018-01-19 08:58:55 -08001615 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1616 msecs_to_jiffies(msecs));
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001617}
1618
André Almeida105663f2020-01-06 15:08:18 -03001619/**
1620 * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
1621 * @hctx: Pointer to the hardware queue to run.
1622 * @msecs: Microseconds of delay to wait before running the queue.
1623 *
1624 * Run a hardware queue asynchronously with a delay of @msecs.
1625 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001626void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1627{
1628 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1629}
1630EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1631
André Almeida105663f2020-01-06 15:08:18 -03001632/**
1633 * blk_mq_run_hw_queue - Start to run a hardware queue.
1634 * @hctx: Pointer to the hardware queue to run.
1635 * @async: If we want to run the queue asynchronously.
1636 *
1637 * Check if the request queue is not in a quiesced state and if there are
1638 * pending requests to be sent. If this is true, run the queue to send requests
1639 * to hardware.
1640 */
John Garry626fb732019-10-30 00:59:30 +08001641void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001642{
Ming Lei24f5a902018-01-06 16:27:38 +08001643 int srcu_idx;
1644 bool need_run;
1645
1646 /*
1647 * When queue is quiesced, we may be switching io scheduler, or
1648 * updating nr_hw_queues, or other things, and we can't run queue
1649 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
1650 *
1651 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
1652 * quiesced.
1653 */
Jens Axboe04ced152018-01-09 08:29:46 -08001654 hctx_lock(hctx, &srcu_idx);
1655 need_run = !blk_queue_quiesced(hctx->queue) &&
1656 blk_mq_hctx_has_pending(hctx);
1657 hctx_unlock(hctx, srcu_idx);
Ming Lei24f5a902018-01-06 16:27:38 +08001658
John Garry626fb732019-10-30 00:59:30 +08001659 if (need_run)
Jens Axboe79f720a2017-11-10 09:13:21 -07001660 __blk_mq_delay_run_hw_queue(hctx, async, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01001661}
Omar Sandoval5b727272017-04-14 01:00:00 -07001662EXPORT_SYMBOL(blk_mq_run_hw_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01001663
André Almeida105663f2020-01-06 15:08:18 -03001664/**
Mauro Carvalho Chehab24f7bb82020-10-23 18:32:54 +02001665 * blk_mq_run_hw_queues - Run all hardware queues in a request queue.
André Almeida105663f2020-01-06 15:08:18 -03001666 * @q: Pointer to the request queue to run.
1667 * @async: If we want to run the queue asynchronously.
1668 */
Mike Snitzerb94ec292015-03-11 23:56:38 -04001669void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001670{
1671 struct blk_mq_hw_ctx *hctx;
1672 int i;
1673
1674 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe79f720a2017-11-10 09:13:21 -07001675 if (blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +01001676 continue;
1677
Mike Snitzerb94ec292015-03-11 23:56:38 -04001678 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001679 }
1680}
Mike Snitzerb94ec292015-03-11 23:56:38 -04001681EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001682
Bart Van Asschefd001442016-10-28 17:19:37 -07001683/**
Douglas Andersonb9151e72020-04-20 09:24:52 -07001684 * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously.
1685 * @q: Pointer to the request queue to run.
1686 * @msecs: Microseconds of delay to wait before running the queues.
1687 */
1688void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs)
1689{
1690 struct blk_mq_hw_ctx *hctx;
1691 int i;
1692
1693 queue_for_each_hw_ctx(q, hctx, i) {
1694 if (blk_mq_hctx_stopped(hctx))
1695 continue;
1696
1697 blk_mq_delay_run_hw_queue(hctx, msecs);
1698 }
1699}
1700EXPORT_SYMBOL(blk_mq_delay_run_hw_queues);
1701
1702/**
Bart Van Asschefd001442016-10-28 17:19:37 -07001703 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1704 * @q: request queue.
1705 *
1706 * The caller is responsible for serializing this function against
1707 * blk_mq_{start,stop}_hw_queue().
1708 */
1709bool blk_mq_queue_stopped(struct request_queue *q)
1710{
1711 struct blk_mq_hw_ctx *hctx;
1712 int i;
1713
1714 queue_for_each_hw_ctx(q, hctx, i)
1715 if (blk_mq_hctx_stopped(hctx))
1716 return true;
1717
1718 return false;
1719}
1720EXPORT_SYMBOL(blk_mq_queue_stopped);
1721
Ming Lei39a70c72017-06-06 23:22:09 +08001722/*
1723 * This function is often used for pausing .queue_rq() by driver when
1724 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001725 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001726 *
1727 * We do not guarantee that dispatch can be drained or blocked
1728 * after blk_mq_stop_hw_queue() returns. Please use
1729 * blk_mq_quiesce_queue() for that requirement.
1730 */
Jens Axboe320ae512013-10-24 09:20:05 +01001731void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1732{
Ming Lei641a9ed2017-06-06 23:22:10 +08001733 cancel_delayed_work(&hctx->run_work);
1734
1735 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboe320ae512013-10-24 09:20:05 +01001736}
1737EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1738
Ming Lei39a70c72017-06-06 23:22:09 +08001739/*
1740 * This function is often used for pausing .queue_rq() by driver when
1741 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001742 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001743 *
1744 * We do not guarantee that dispatch can be drained or blocked
1745 * after blk_mq_stop_hw_queues() returns. Please use
1746 * blk_mq_quiesce_queue() for that requirement.
1747 */
Jens Axboe2719aa22017-05-03 11:08:14 -06001748void blk_mq_stop_hw_queues(struct request_queue *q)
1749{
Ming Lei641a9ed2017-06-06 23:22:10 +08001750 struct blk_mq_hw_ctx *hctx;
1751 int i;
1752
1753 queue_for_each_hw_ctx(q, hctx, i)
1754 blk_mq_stop_hw_queue(hctx);
Christoph Hellwig280d45f2013-10-25 14:45:58 +01001755}
1756EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1757
Jens Axboe320ae512013-10-24 09:20:05 +01001758void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1759{
1760 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -06001761
Jens Axboe0ffbce82014-06-25 08:22:34 -06001762 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001763}
1764EXPORT_SYMBOL(blk_mq_start_hw_queue);
1765
Christoph Hellwig2f268552014-04-16 09:44:56 +02001766void blk_mq_start_hw_queues(struct request_queue *q)
1767{
1768 struct blk_mq_hw_ctx *hctx;
1769 int i;
1770
1771 queue_for_each_hw_ctx(q, hctx, i)
1772 blk_mq_start_hw_queue(hctx);
1773}
1774EXPORT_SYMBOL(blk_mq_start_hw_queues);
1775
Jens Axboeae911c52016-12-08 13:19:30 -07001776void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1777{
1778 if (!blk_mq_hctx_stopped(hctx))
1779 return;
1780
1781 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1782 blk_mq_run_hw_queue(hctx, async);
1783}
1784EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1785
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001786void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001787{
1788 struct blk_mq_hw_ctx *hctx;
1789 int i;
1790
Jens Axboeae911c52016-12-08 13:19:30 -07001791 queue_for_each_hw_ctx(q, hctx, i)
1792 blk_mq_start_stopped_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001793}
1794EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1795
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001796static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001797{
1798 struct blk_mq_hw_ctx *hctx;
1799
Jens Axboe9f993732017-04-10 09:54:54 -06001800 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboe21c6e932017-04-10 09:54:56 -06001801
1802 /*
Ming Lei15fe8a902018-04-08 17:48:11 +08001803 * If we are stopped, don't run the queue.
Jens Axboe21c6e932017-04-10 09:54:56 -06001804 */
Yufen Yu08410312020-10-08 23:26:30 -04001805 if (blk_mq_hctx_stopped(hctx))
Jianchao Wang0196d6b2018-06-04 17:03:55 +08001806 return;
Jens Axboee4043dc2014-04-09 10:18:23 -06001807
Jens Axboe320ae512013-10-24 09:20:05 +01001808 __blk_mq_run_hw_queue(hctx);
1809}
1810
Ming Leicfd0c552015-10-20 23:13:57 +08001811static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08001812 struct request *rq,
1813 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01001814{
Jens Axboee57690f2016-08-24 15:34:35 -06001815 struct blk_mq_ctx *ctx = rq->mq_ctx;
Ming Leic16d6b52018-12-17 08:44:05 -07001816 enum hctx_type type = hctx->type;
Jens Axboee57690f2016-08-24 15:34:35 -06001817
Bart Van Assche7b607812017-06-20 11:15:47 -07001818 lockdep_assert_held(&ctx->lock);
1819
Jens Axboe01b983c2013-11-19 18:59:10 -07001820 trace_block_rq_insert(hctx->queue, rq);
1821
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001822 if (at_head)
Ming Leic16d6b52018-12-17 08:44:05 -07001823 list_add(&rq->queuelist, &ctx->rq_lists[type]);
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001824 else
Ming Leic16d6b52018-12-17 08:44:05 -07001825 list_add_tail(&rq->queuelist, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08001826}
Jens Axboe4bb659b2014-05-09 09:36:49 -06001827
Jens Axboe2c3ad662016-12-14 14:34:47 -07001828void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1829 bool at_head)
Ming Leicfd0c552015-10-20 23:13:57 +08001830{
1831 struct blk_mq_ctx *ctx = rq->mq_ctx;
1832
Bart Van Assche7b607812017-06-20 11:15:47 -07001833 lockdep_assert_held(&ctx->lock);
1834
Jens Axboee57690f2016-08-24 15:34:35 -06001835 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01001836 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001837}
1838
André Almeida105663f2020-01-06 15:08:18 -03001839/**
1840 * blk_mq_request_bypass_insert - Insert a request at dispatch list.
1841 * @rq: Pointer to request to be inserted.
Randy Dunlap26bfeb22020-08-16 16:39:34 -07001842 * @at_head: true if the request should be inserted at the head of the list.
André Almeida105663f2020-01-06 15:08:18 -03001843 * @run_queue: If we should run the hardware queue after inserting the request.
1844 *
Jens Axboe157f3772017-09-11 16:43:57 -06001845 * Should only be used carefully, when the caller knows we want to
1846 * bypass a potential IO scheduler on the target device.
1847 */
Ming Lei01e99ae2020-02-25 09:04:32 +08001848void blk_mq_request_bypass_insert(struct request *rq, bool at_head,
1849 bool run_queue)
Jens Axboe157f3772017-09-11 16:43:57 -06001850{
Jens Axboeea4f9952018-10-29 15:06:13 -06001851 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe157f3772017-09-11 16:43:57 -06001852
1853 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001854 if (at_head)
1855 list_add(&rq->queuelist, &hctx->dispatch);
1856 else
1857 list_add_tail(&rq->queuelist, &hctx->dispatch);
Jens Axboe157f3772017-09-11 16:43:57 -06001858 spin_unlock(&hctx->lock);
1859
Ming Leib0850292017-11-02 23:24:34 +08001860 if (run_queue)
1861 blk_mq_run_hw_queue(hctx, false);
Jens Axboe157f3772017-09-11 16:43:57 -06001862}
1863
Jens Axboebd166ef2017-01-17 06:03:22 -07001864void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
1865 struct list_head *list)
Jens Axboe320ae512013-10-24 09:20:05 +01001866
1867{
Ming Lei3f0cedc2018-07-02 17:35:58 +08001868 struct request *rq;
Ming Leic16d6b52018-12-17 08:44:05 -07001869 enum hctx_type type = hctx->type;
Ming Lei3f0cedc2018-07-02 17:35:58 +08001870
Jens Axboe320ae512013-10-24 09:20:05 +01001871 /*
1872 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1873 * offline now
1874 */
Ming Lei3f0cedc2018-07-02 17:35:58 +08001875 list_for_each_entry(rq, list, queuelist) {
Jens Axboee57690f2016-08-24 15:34:35 -06001876 BUG_ON(rq->mq_ctx != ctx);
Ming Lei3f0cedc2018-07-02 17:35:58 +08001877 trace_block_rq_insert(hctx->queue, rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001878 }
Ming Lei3f0cedc2018-07-02 17:35:58 +08001879
1880 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001881 list_splice_tail_init(list, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08001882 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001883 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001884}
1885
Jens Axboe3110fc72018-10-30 12:24:04 -06001886static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
Jens Axboe320ae512013-10-24 09:20:05 +01001887{
1888 struct request *rqa = container_of(a, struct request, queuelist);
1889 struct request *rqb = container_of(b, struct request, queuelist);
1890
Pavel Begunkov7d30a622019-11-29 00:11:53 +03001891 if (rqa->mq_ctx != rqb->mq_ctx)
1892 return rqa->mq_ctx > rqb->mq_ctx;
1893 if (rqa->mq_hctx != rqb->mq_hctx)
1894 return rqa->mq_hctx > rqb->mq_hctx;
Jens Axboe3110fc72018-10-30 12:24:04 -06001895
1896 return blk_rq_pos(rqa) > blk_rq_pos(rqb);
Jens Axboe320ae512013-10-24 09:20:05 +01001897}
1898
1899void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1900{
Jens Axboe320ae512013-10-24 09:20:05 +01001901 LIST_HEAD(list);
Jens Axboe320ae512013-10-24 09:20:05 +01001902
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001903 if (list_empty(&plug->mq_list))
1904 return;
Jens Axboe320ae512013-10-24 09:20:05 +01001905 list_splice_init(&plug->mq_list, &list);
1906
Jens Axboece5b0092018-11-27 17:13:56 -07001907 if (plug->rq_count > 2 && plug->multiple_queues)
1908 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe320ae512013-10-24 09:20:05 +01001909
Dongli Zhangbcc816d2019-04-04 10:57:44 +08001910 plug->rq_count = 0;
1911
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001912 do {
1913 struct list_head rq_list;
1914 struct request *rq, *head_rq = list_entry_rq(list.next);
1915 struct list_head *pos = &head_rq->queuelist; /* skip first */
1916 struct blk_mq_hw_ctx *this_hctx = head_rq->mq_hctx;
1917 struct blk_mq_ctx *this_ctx = head_rq->mq_ctx;
1918 unsigned int depth = 1;
Jens Axboe320ae512013-10-24 09:20:05 +01001919
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001920 list_for_each_continue(pos, &list) {
1921 rq = list_entry_rq(pos);
1922 BUG_ON(!rq->q);
1923 if (rq->mq_hctx != this_hctx || rq->mq_ctx != this_ctx)
1924 break;
1925 depth++;
Jens Axboe320ae512013-10-24 09:20:05 +01001926 }
1927
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001928 list_cut_before(&rq_list, &list, pos);
1929 trace_block_unplug(head_rq->q, depth, !from_schedule);
Jens Axboe67cae4c2018-10-30 11:31:51 -06001930 blk_mq_sched_insert_requests(this_hctx, this_ctx, &rq_list,
Jens Axboebd166ef2017-01-17 06:03:22 -07001931 from_schedule);
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001932 } while(!list_empty(&list));
Jens Axboe320ae512013-10-24 09:20:05 +01001933}
1934
Christoph Hellwig14ccb662019-06-06 12:29:01 +02001935static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
1936 unsigned int nr_segs)
Jens Axboe320ae512013-10-24 09:20:05 +01001937{
Eric Biggers93f221a2020-09-15 20:53:14 -07001938 int err;
1939
Christoph Hellwigf924cdd2019-06-06 12:29:00 +02001940 if (bio->bi_opf & REQ_RAHEAD)
1941 rq->cmd_flags |= REQ_FAILFAST_MASK;
1942
1943 rq->__sector = bio->bi_iter.bi_sector;
1944 rq->write_hint = bio->bi_write_hint;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02001945 blk_rq_bio_prep(rq, bio, nr_segs);
Eric Biggers93f221a2020-09-15 20:53:14 -07001946
1947 /* This can't fail, since GFP_NOIO includes __GFP_DIRECT_RECLAIM. */
1948 err = blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO);
1949 WARN_ON_ONCE(err);
Jens Axboe4b570522014-05-29 11:00:11 -06001950
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +02001951 blk_account_io_start(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001952}
1953
Mike Snitzer0f955492018-01-17 11:25:56 -05001954static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
1955 struct request *rq,
Jens Axboebe94f052018-11-24 10:15:46 -07001956 blk_qc_t *cookie, bool last)
Shaohua Lif984df12015-05-08 10:51:32 -07001957{
Shaohua Lif984df12015-05-08 10:51:32 -07001958 struct request_queue *q = rq->q;
Shaohua Lif984df12015-05-08 10:51:32 -07001959 struct blk_mq_queue_data bd = {
1960 .rq = rq,
Jens Axboebe94f052018-11-24 10:15:46 -07001961 .last = last,
Shaohua Lif984df12015-05-08 10:51:32 -07001962 };
Jens Axboebd166ef2017-01-17 06:03:22 -07001963 blk_qc_t new_cookie;
Jens Axboef06345a2017-06-12 11:22:46 -06001964 blk_status_t ret;
Mike Snitzer0f955492018-01-17 11:25:56 -05001965
1966 new_cookie = request_to_qc_t(hctx, rq);
1967
1968 /*
1969 * For OK queue, we are done. For error, caller may kill it.
1970 * Any other error (busy), just add it to our list as we
1971 * previously would have done.
1972 */
1973 ret = q->mq_ops->queue_rq(hctx, &bd);
1974 switch (ret) {
1975 case BLK_STS_OK:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001976 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001977 *cookie = new_cookie;
1978 break;
1979 case BLK_STS_RESOURCE:
Ming Lei86ff7c22018-01-30 22:04:57 -05001980 case BLK_STS_DEV_RESOURCE:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001981 blk_mq_update_dispatch_busy(hctx, true);
Mike Snitzer0f955492018-01-17 11:25:56 -05001982 __blk_mq_requeue_request(rq);
1983 break;
1984 default:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001985 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001986 *cookie = BLK_QC_T_NONE;
1987 break;
1988 }
1989
1990 return ret;
1991}
1992
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001993static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
Mike Snitzer0f955492018-01-17 11:25:56 -05001994 struct request *rq,
Ming Lei396eaf22018-01-17 11:25:57 -05001995 blk_qc_t *cookie,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001996 bool bypass_insert, bool last)
Mike Snitzer0f955492018-01-17 11:25:56 -05001997{
1998 struct request_queue *q = rq->q;
Ming Leid964f042017-06-06 23:22:00 +08001999 bool run_queue = true;
2000
Ming Lei23d4ee12018-01-18 12:06:59 +08002001 /*
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002002 * RCU or SRCU read lock is needed before checking quiesced flag.
Ming Lei23d4ee12018-01-18 12:06:59 +08002003 *
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002004 * When queue is stopped or quiesced, ignore 'bypass_insert' from
2005 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
2006 * and avoid driver to try to dispatch again.
Ming Lei23d4ee12018-01-18 12:06:59 +08002007 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002008 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
Ming Leid964f042017-06-06 23:22:00 +08002009 run_queue = false;
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002010 bypass_insert = false;
2011 goto insert;
Ming Leid964f042017-06-06 23:22:00 +08002012 }
Shaohua Lif984df12015-05-08 10:51:32 -07002013
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002014 if (q->elevator && !bypass_insert)
2015 goto insert;
Bart Van Assche2253efc2016-10-28 17:20:02 -07002016
Ming Lei65c76362020-06-30 18:24:56 +08002017 if (!blk_mq_get_dispatch_budget(q))
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002018 goto insert;
Jens Axboebd166ef2017-01-17 06:03:22 -07002019
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08002020 if (!blk_mq_get_driver_tag(rq)) {
Ming Lei65c76362020-06-30 18:24:56 +08002021 blk_mq_put_dispatch_budget(q);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002022 goto insert;
Ming Lei88022d72017-11-05 02:21:12 +08002023 }
Ming Leide148292017-10-14 17:22:29 +08002024
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002025 return __blk_mq_issue_directly(hctx, rq, cookie, last);
2026insert:
2027 if (bypass_insert)
2028 return BLK_STS_RESOURCE;
2029
Ming Leidb03f882020-08-18 17:07:28 +08002030 blk_mq_sched_insert_request(rq, false, run_queue, false);
2031
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002032 return BLK_STS_OK;
2033}
2034
André Almeida105663f2020-01-06 15:08:18 -03002035/**
2036 * blk_mq_try_issue_directly - Try to send a request directly to device driver.
2037 * @hctx: Pointer of the associated hardware queue.
2038 * @rq: Pointer to request to be sent.
2039 * @cookie: Request queue cookie.
2040 *
2041 * If the device has enough resources to accept a new request now, send the
2042 * request directly to device driver. Else, insert at hctx->dispatch queue, so
2043 * we can try send it another time in the future. Requests inserted at this
2044 * queue have higher priority.
2045 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002046static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
2047 struct request *rq, blk_qc_t *cookie)
2048{
2049 blk_status_t ret;
2050 int srcu_idx;
2051
2052 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
2053
2054 hctx_lock(hctx, &srcu_idx);
2055
2056 ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false, true);
2057 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
Ming Lei01e99ae2020-02-25 09:04:32 +08002058 blk_mq_request_bypass_insert(rq, false, true);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002059 else if (ret != BLK_STS_OK)
2060 blk_mq_end_request(rq, ret);
2061
Jens Axboe04ced152018-01-09 08:29:46 -08002062 hctx_unlock(hctx, srcu_idx);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002063}
2064
2065blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
2066{
2067 blk_status_t ret;
2068 int srcu_idx;
2069 blk_qc_t unused_cookie;
2070 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
2071
2072 hctx_lock(hctx, &srcu_idx);
2073 ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true, last);
2074 hctx_unlock(hctx, srcu_idx);
Jianchao Wang7f556a42018-12-14 09:28:18 +08002075
2076 return ret;
Christoph Hellwig5eb61262017-03-22 15:01:51 -04002077}
2078
Ming Lei6ce3dd62018-07-10 09:03:31 +08002079void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
2080 struct list_head *list)
2081{
Keith Busch536167d42020-04-07 03:13:48 +09002082 int queued = 0;
yangerkun632bfb62020-09-05 19:25:56 +08002083 int errors = 0;
Keith Busch536167d42020-04-07 03:13:48 +09002084
Ming Lei6ce3dd62018-07-10 09:03:31 +08002085 while (!list_empty(list)) {
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002086 blk_status_t ret;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002087 struct request *rq = list_first_entry(list, struct request,
2088 queuelist);
2089
2090 list_del_init(&rq->queuelist);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002091 ret = blk_mq_request_issue_directly(rq, list_empty(list));
2092 if (ret != BLK_STS_OK) {
2093 if (ret == BLK_STS_RESOURCE ||
2094 ret == BLK_STS_DEV_RESOURCE) {
Ming Lei01e99ae2020-02-25 09:04:32 +08002095 blk_mq_request_bypass_insert(rq, false,
Jens Axboec616cbe2018-12-06 22:17:44 -07002096 list_empty(list));
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002097 break;
2098 }
2099 blk_mq_end_request(rq, ret);
yangerkun632bfb62020-09-05 19:25:56 +08002100 errors++;
Keith Busch536167d42020-04-07 03:13:48 +09002101 } else
2102 queued++;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002103 }
Jens Axboed666ba92018-11-27 17:02:25 -07002104
2105 /*
2106 * If we didn't flush the entire list, we could have told
2107 * the driver there was more coming, but that turned out to
2108 * be a lie.
2109 */
yangerkun632bfb62020-09-05 19:25:56 +08002110 if ((!list_empty(list) || errors) &&
2111 hctx->queue->mq_ops->commit_rqs && queued)
Jens Axboed666ba92018-11-27 17:02:25 -07002112 hctx->queue->mq_ops->commit_rqs(hctx);
Ming Lei6ce3dd62018-07-10 09:03:31 +08002113}
2114
Jens Axboece5b0092018-11-27 17:13:56 -07002115static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
2116{
2117 list_add_tail(&rq->queuelist, &plug->mq_list);
2118 plug->rq_count++;
2119 if (!plug->multiple_queues && !list_is_singular(&plug->mq_list)) {
2120 struct request *tmp;
2121
2122 tmp = list_first_entry(&plug->mq_list, struct request,
2123 queuelist);
2124 if (tmp->q != rq->q)
2125 plug->multiple_queues = true;
2126 }
2127}
2128
André Almeida105663f2020-01-06 15:08:18 -03002129/**
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02002130 * blk_mq_submit_bio - Create and send a request to block device.
André Almeida105663f2020-01-06 15:08:18 -03002131 * @bio: Bio pointer.
2132 *
2133 * Builds up a request structure from @q and @bio and send to the device. The
2134 * request may not be queued directly to hardware if:
2135 * * This request can be merged with another one
2136 * * We want to place request at plug queue for possible future merging
2137 * * There is an IO scheduler active at this queue
2138 *
2139 * It will not queue the request if there is an error with the bio, or at the
2140 * request creation.
2141 *
2142 * Returns: Request queue cookie.
2143 */
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02002144blk_qc_t blk_mq_submit_bio(struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06002145{
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02002146 struct request_queue *q = bio->bi_disk->queue;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002147 const int is_sync = op_is_sync(bio->bi_opf);
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07002148 const int is_flush_fua = op_is_flush(bio->bi_opf);
Christoph Hellwige6e7abf2020-05-29 15:53:09 +02002149 struct blk_mq_alloc_data data = {
2150 .q = q,
2151 };
Jens Axboe07068d52014-05-22 10:40:51 -06002152 struct request *rq;
Shaohua Lif984df12015-05-08 10:51:32 -07002153 struct blk_plug *plug;
Shaohua Li5b3f3412015-05-08 10:51:33 -07002154 struct request *same_queue_rq = NULL;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002155 unsigned int nr_segs;
Jens Axboe7b371632015-11-05 10:41:40 -07002156 blk_qc_t cookie;
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002157 blk_status_t ret;
Jens Axboe07068d52014-05-22 10:40:51 -06002158
2159 blk_queue_bounce(q, &bio);
Christoph Hellwigf695ca32020-07-01 10:59:39 +02002160 __blk_queue_split(&bio, &nr_segs);
Wen Xiongf36ea502017-05-10 08:54:11 -05002161
Dmitry Monakhove23947b2017-06-29 11:31:11 -07002162 if (!bio_integrity_prep(bio))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002163 goto queue_exit;
Jens Axboe07068d52014-05-22 10:40:51 -06002164
Omar Sandoval87c279e2016-06-01 22:18:48 -07002165 if (!is_flush_fua && !blk_queue_nomerges(q) &&
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002166 blk_attempt_plug_merge(q, bio, nr_segs, &same_queue_rq))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002167 goto queue_exit;
Shaohua Lif984df12015-05-08 10:51:32 -07002168
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002169 if (blk_mq_sched_bio_merge(q, bio, nr_segs))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002170 goto queue_exit;
Jens Axboebd166ef2017-01-17 06:03:22 -07002171
Christoph Hellwigd5337562018-11-14 17:02:09 +01002172 rq_qos_throttle(q, bio);
Jens Axboe87760e52016-11-09 12:38:14 -07002173
Ming Lei78091672019-01-16 19:08:15 +08002174 data.cmd_flags = bio->bi_opf;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +02002175 rq = __blk_mq_alloc_request(&data);
Jens Axboe87760e52016-11-09 12:38:14 -07002176 if (unlikely(!rq)) {
Josef Bacikc1c80382018-07-03 11:14:59 -04002177 rq_qos_cleanup(q, bio);
Jens Axboe7b6620d2019-08-15 11:09:16 -06002178 if (bio->bi_opf & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -05002179 bio_wouldblock_error(bio);
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002180 goto queue_exit;
Jens Axboe87760e52016-11-09 12:38:14 -07002181 }
2182
Xiaoguang Wangd6f1dda2018-10-23 22:30:50 +08002183 trace_block_getrq(q, bio, bio->bi_opf);
2184
Josef Bacikc1c80382018-07-03 11:14:59 -04002185 rq_qos_track(q, rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06002186
Jens Axboefd2d3322017-01-12 10:04:45 -07002187 cookie = request_to_qc_t(data.hctx, rq);
Jens Axboe07068d52014-05-22 10:40:51 -06002188
Bart Van Assche970d1682019-07-01 08:47:30 -07002189 blk_mq_bio_to_request(rq, bio, nr_segs);
2190
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002191 ret = blk_crypto_init_request(rq);
2192 if (ret != BLK_STS_OK) {
2193 bio->bi_status = ret;
2194 bio_endio(bio);
2195 blk_mq_free_request(rq);
2196 return BLK_QC_T_NONE;
2197 }
2198
Damien Le Moalb49773e72019-07-11 01:18:31 +09002199 plug = blk_mq_plug(q, bio);
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04002200 if (unlikely(is_flush_fua)) {
André Almeida105663f2020-01-06 15:08:18 -03002201 /* Bypass scheduler for flush requests */
Ming Lei923218f2017-11-02 23:24:38 +08002202 blk_insert_flush(rq);
2203 blk_mq_run_hw_queue(data.hctx, true);
Ming Lei3154df22019-09-27 15:24:31 +08002204 } else if (plug && (q->nr_hw_queues == 1 || q->mq_ops->commit_rqs ||
2205 !blk_queue_nonrot(q))) {
Jens Axboeb2c5d162018-11-29 10:03:42 -07002206 /*
2207 * Use plugging if we have a ->commit_rqs() hook as well, as
2208 * we know the driver uses bd->last in a smart fashion.
Ming Lei3154df22019-09-27 15:24:31 +08002209 *
2210 * Use normal plugging if this disk is slow HDD, as sequential
2211 * IO may benefit a lot from plug merging.
Jens Axboeb2c5d162018-11-29 10:03:42 -07002212 */
Jens Axboe5f0ed772018-11-23 22:04:33 -07002213 unsigned int request_count = plug->rq_count;
Shaohua Li600271d2016-11-03 17:03:54 -07002214 struct request *last = NULL;
2215
Ming Lei676d0602015-10-20 23:13:56 +08002216 if (!request_count)
Jeff Moyere6c44382015-05-08 10:51:30 -07002217 trace_block_plug(q);
Shaohua Li600271d2016-11-03 17:03:54 -07002218 else
2219 last = list_entry_rq(plug->mq_list.prev);
Jens Axboeb094f892015-11-20 20:29:45 -07002220
Shaohua Li600271d2016-11-03 17:03:54 -07002221 if (request_count >= BLK_MAX_REQUEST_COUNT || (last &&
2222 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
Jeff Moyere6c44382015-05-08 10:51:30 -07002223 blk_flush_plug_list(plug, false);
2224 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002225 }
Jens Axboeb094f892015-11-20 20:29:45 -07002226
Jens Axboece5b0092018-11-27 17:13:56 -07002227 blk_add_rq_to_plug(plug, rq);
Ming Leia12de1d2019-09-27 15:24:30 +08002228 } else if (q->elevator) {
André Almeida105663f2020-01-06 15:08:18 -03002229 /* Insert the request at the IO scheduler queue */
Ming Leia12de1d2019-09-27 15:24:30 +08002230 blk_mq_sched_insert_request(rq, false, true, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002231 } else if (plug && !blk_queue_nomerges(q)) {
Jens Axboe320ae512013-10-24 09:20:05 +01002232 /*
2233 * We do limited plugging. If the bio can be merged, do that.
2234 * Otherwise the existing request in the plug list will be
2235 * issued. So the plug list will have one request at most
Christoph Hellwig22997222017-03-22 15:01:52 -04002236 * The plug list might get flushed before this. If that happens,
2237 * the plug list is empty, and same_queue_rq is invalid.
Jens Axboe320ae512013-10-24 09:20:05 +01002238 */
Christoph Hellwig22997222017-03-22 15:01:52 -04002239 if (list_empty(&plug->mq_list))
2240 same_queue_rq = NULL;
Jens Axboe4711b572018-11-27 17:07:17 -07002241 if (same_queue_rq) {
Christoph Hellwig22997222017-03-22 15:01:52 -04002242 list_del_init(&same_queue_rq->queuelist);
Jens Axboe4711b572018-11-27 17:07:17 -07002243 plug->rq_count--;
2244 }
Jens Axboece5b0092018-11-27 17:13:56 -07002245 blk_add_rq_to_plug(plug, rq);
Yufen Yuff3b74b2019-03-26 21:19:25 +08002246 trace_block_plug(q);
Christoph Hellwig22997222017-03-22 15:01:52 -04002247
Ming Leidad7a3b2017-06-06 23:21:59 +08002248 if (same_queue_rq) {
Jens Axboeea4f9952018-10-29 15:06:13 -06002249 data.hctx = same_queue_rq->mq_hctx;
Yufen Yuff3b74b2019-03-26 21:19:25 +08002250 trace_block_unplug(q, 1, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002251 blk_mq_try_issue_directly(data.hctx, same_queue_rq,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002252 &cookie);
Ming Leidad7a3b2017-06-06 23:21:59 +08002253 }
Ming Leia12de1d2019-09-27 15:24:30 +08002254 } else if ((q->nr_hw_queues > 1 && is_sync) ||
2255 !data.hctx->dispatch_busy) {
André Almeida105663f2020-01-06 15:08:18 -03002256 /*
2257 * There is no scheduler and we can try to send directly
2258 * to the hardware.
2259 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002260 blk_mq_try_issue_directly(data.hctx, rq, &cookie);
Ming Leiab42f352017-05-26 19:53:19 +08002261 } else {
André Almeida105663f2020-01-06 15:08:18 -03002262 /* Default case. */
huhai8fa9f552018-05-16 08:21:21 -06002263 blk_mq_sched_insert_request(rq, false, true, true);
Ming Leiab42f352017-05-26 19:53:19 +08002264 }
Jens Axboe320ae512013-10-24 09:20:05 +01002265
Jens Axboe7b371632015-11-05 10:41:40 -07002266 return cookie;
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002267queue_exit:
2268 blk_queue_exit(q);
2269 return BLK_QC_T_NONE;
Jens Axboe320ae512013-10-24 09:20:05 +01002270}
2271
Jens Axboecc71a6f2017-01-11 14:29:56 -07002272void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2273 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01002274{
2275 struct page *page;
2276
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002277 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002278 int i;
2279
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002280 for (i = 0; i < tags->nr_tags; i++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002281 struct request *rq = tags->static_rqs[i];
2282
2283 if (!rq)
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002284 continue;
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002285 set->ops->exit_request(set, rq, hctx_idx);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002286 tags->static_rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002287 }
2288 }
2289
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002290 while (!list_empty(&tags->page_list)) {
2291 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07002292 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002293 /*
2294 * Remove kmemleak object previously allocated in
Raul E Rangel273938b2019-05-02 13:48:11 -06002295 * blk_mq_alloc_rqs().
Catalin Marinasf75782e2015-09-14 18:16:02 +01002296 */
2297 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01002298 __free_pages(page, page->private);
2299 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002300}
Jens Axboe320ae512013-10-24 09:20:05 +01002301
John Garry1c0706a2020-08-19 23:20:22 +08002302void blk_mq_free_rq_map(struct blk_mq_tags *tags, unsigned int flags)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002303{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002304 kfree(tags->rqs);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002305 tags->rqs = NULL;
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002306 kfree(tags->static_rqs);
2307 tags->static_rqs = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002308
John Garry1c0706a2020-08-19 23:20:22 +08002309 blk_mq_free_tags(tags, flags);
Jens Axboe320ae512013-10-24 09:20:05 +01002310}
2311
Jens Axboecc71a6f2017-01-11 14:29:56 -07002312struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
2313 unsigned int hctx_idx,
2314 unsigned int nr_tags,
John Garry1c0706a2020-08-19 23:20:22 +08002315 unsigned int reserved_tags,
2316 unsigned int flags)
Jens Axboe320ae512013-10-24 09:20:05 +01002317{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002318 struct blk_mq_tags *tags;
Shaohua Li59f082e2017-02-01 09:53:14 -08002319 int node;
Jens Axboe320ae512013-10-24 09:20:05 +01002320
Dongli Zhang7d76f852019-02-27 21:35:01 +08002321 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002322 if (node == NUMA_NO_NODE)
2323 node = set->numa_node;
2324
John Garry1c0706a2020-08-19 23:20:22 +08002325 tags = blk_mq_init_tags(nr_tags, reserved_tags, node, flags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002326 if (!tags)
2327 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002328
Kees Cook590b5b72018-06-12 14:04:20 -07002329 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002330 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
Shaohua Li59f082e2017-02-01 09:53:14 -08002331 node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002332 if (!tags->rqs) {
John Garry1c0706a2020-08-19 23:20:22 +08002333 blk_mq_free_tags(tags, flags);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002334 return NULL;
2335 }
Jens Axboe320ae512013-10-24 09:20:05 +01002336
Kees Cook590b5b72018-06-12 14:04:20 -07002337 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2338 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2339 node);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002340 if (!tags->static_rqs) {
2341 kfree(tags->rqs);
John Garry1c0706a2020-08-19 23:20:22 +08002342 blk_mq_free_tags(tags, flags);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002343 return NULL;
2344 }
2345
Jens Axboecc71a6f2017-01-11 14:29:56 -07002346 return tags;
2347}
2348
2349static size_t order_to_size(unsigned int order)
2350{
2351 return (size_t)PAGE_SIZE << order;
2352}
2353
Tejun Heo1d9bd512018-01-09 08:29:48 -08002354static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2355 unsigned int hctx_idx, int node)
2356{
2357 int ret;
2358
2359 if (set->ops->init_request) {
2360 ret = set->ops->init_request(set, rq, hctx_idx, node);
2361 if (ret)
2362 return ret;
2363 }
2364
Keith Busch12f5b932018-05-29 15:52:28 +02002365 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Tejun Heo1d9bd512018-01-09 08:29:48 -08002366 return 0;
2367}
2368
Jens Axboecc71a6f2017-01-11 14:29:56 -07002369int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2370 unsigned int hctx_idx, unsigned int depth)
2371{
2372 unsigned int i, j, entries_per_page, max_order = 4;
2373 size_t rq_size, left;
Shaohua Li59f082e2017-02-01 09:53:14 -08002374 int node;
2375
Dongli Zhang7d76f852019-02-27 21:35:01 +08002376 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002377 if (node == NUMA_NO_NODE)
2378 node = set->numa_node;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002379
2380 INIT_LIST_HEAD(&tags->page_list);
2381
Jens Axboe320ae512013-10-24 09:20:05 +01002382 /*
2383 * rq_size is the size of the request plus driver payload, rounded
2384 * to the cacheline size
2385 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002386 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01002387 cache_line_size());
Jens Axboecc71a6f2017-01-11 14:29:56 -07002388 left = rq_size * depth;
Jens Axboe320ae512013-10-24 09:20:05 +01002389
Jens Axboecc71a6f2017-01-11 14:29:56 -07002390 for (i = 0; i < depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01002391 int this_order = max_order;
2392 struct page *page;
2393 int to_do;
2394 void *p;
2395
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06002396 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01002397 this_order--;
2398
2399 do {
Shaohua Li59f082e2017-02-01 09:53:14 -08002400 page = alloc_pages_node(node,
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002401 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06002402 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01002403 if (page)
2404 break;
2405 if (!this_order--)
2406 break;
2407 if (order_to_size(this_order) < rq_size)
2408 break;
2409 } while (1);
2410
2411 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002412 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01002413
2414 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002415 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01002416
2417 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002418 /*
2419 * Allow kmemleak to scan these pages as they contain pointers
2420 * to additional allocations like via ops->init_request().
2421 */
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002422 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
Jens Axboe320ae512013-10-24 09:20:05 +01002423 entries_per_page = order_to_size(this_order) / rq_size;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002424 to_do = min(entries_per_page, depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01002425 left -= to_do * rq_size;
2426 for (j = 0; j < to_do; j++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002427 struct request *rq = p;
2428
2429 tags->static_rqs[i] = rq;
Tejun Heo1d9bd512018-01-09 08:29:48 -08002430 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
2431 tags->static_rqs[i] = NULL;
2432 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002433 }
2434
Jens Axboe320ae512013-10-24 09:20:05 +01002435 p += rq_size;
2436 i++;
2437 }
2438 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002439 return 0;
Jens Axboe320ae512013-10-24 09:20:05 +01002440
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002441fail:
Jens Axboecc71a6f2017-01-11 14:29:56 -07002442 blk_mq_free_rqs(set, tags, hctx_idx);
2443 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01002444}
2445
Ming Leibf0beec2020-05-29 15:53:15 +02002446struct rq_iter_data {
2447 struct blk_mq_hw_ctx *hctx;
2448 bool has_rq;
2449};
2450
2451static bool blk_mq_has_request(struct request *rq, void *data, bool reserved)
2452{
2453 struct rq_iter_data *iter_data = data;
2454
2455 if (rq->mq_hctx != iter_data->hctx)
2456 return true;
2457 iter_data->has_rq = true;
2458 return false;
2459}
2460
2461static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
2462{
2463 struct blk_mq_tags *tags = hctx->sched_tags ?
2464 hctx->sched_tags : hctx->tags;
2465 struct rq_iter_data data = {
2466 .hctx = hctx,
2467 };
2468
2469 blk_mq_all_tag_iter(tags, blk_mq_has_request, &data);
2470 return data.has_rq;
2471}
2472
2473static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu,
2474 struct blk_mq_hw_ctx *hctx)
2475{
2476 if (cpumask_next_and(-1, hctx->cpumask, cpu_online_mask) != cpu)
2477 return false;
2478 if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids)
2479 return false;
2480 return true;
2481}
2482
2483static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
2484{
2485 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2486 struct blk_mq_hw_ctx, cpuhp_online);
2487
2488 if (!cpumask_test_cpu(cpu, hctx->cpumask) ||
2489 !blk_mq_last_cpu_in_hctx(cpu, hctx))
2490 return 0;
2491
2492 /*
2493 * Prevent new request from being allocated on the current hctx.
2494 *
2495 * The smp_mb__after_atomic() Pairs with the implied barrier in
2496 * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is
2497 * seen once we return from the tag allocator.
2498 */
2499 set_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2500 smp_mb__after_atomic();
2501
2502 /*
2503 * Try to grab a reference to the queue and wait for any outstanding
2504 * requests. If we could not grab a reference the queue has been
2505 * frozen and there are no requests.
2506 */
2507 if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) {
2508 while (blk_mq_hctx_has_requests(hctx))
2509 msleep(5);
2510 percpu_ref_put(&hctx->queue->q_usage_counter);
2511 }
2512
2513 return 0;
2514}
2515
2516static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node)
2517{
2518 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2519 struct blk_mq_hw_ctx, cpuhp_online);
2520
2521 if (cpumask_test_cpu(cpu, hctx->cpumask))
2522 clear_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2523 return 0;
2524}
2525
Jens Axboee57690f2016-08-24 15:34:35 -06002526/*
2527 * 'cpu' is going away. splice any existing rq_list entries from this
2528 * software queue to the hw queue dispatch list, and ensure that it
2529 * gets run.
2530 */
Thomas Gleixner9467f852016-09-22 08:05:17 -06002531static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
Jens Axboe484b4062014-05-21 14:01:15 -06002532{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002533 struct blk_mq_hw_ctx *hctx;
Jens Axboe484b4062014-05-21 14:01:15 -06002534 struct blk_mq_ctx *ctx;
2535 LIST_HEAD(tmp);
Ming Leic16d6b52018-12-17 08:44:05 -07002536 enum hctx_type type;
Jens Axboe484b4062014-05-21 14:01:15 -06002537
Thomas Gleixner9467f852016-09-22 08:05:17 -06002538 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02002539 if (!cpumask_test_cpu(cpu, hctx->cpumask))
2540 return 0;
2541
Jens Axboee57690f2016-08-24 15:34:35 -06002542 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Ming Leic16d6b52018-12-17 08:44:05 -07002543 type = hctx->type;
Jens Axboe484b4062014-05-21 14:01:15 -06002544
2545 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002546 if (!list_empty(&ctx->rq_lists[type])) {
2547 list_splice_init(&ctx->rq_lists[type], &tmp);
Jens Axboe484b4062014-05-21 14:01:15 -06002548 blk_mq_hctx_clear_pending(hctx, ctx);
2549 }
2550 spin_unlock(&ctx->lock);
2551
2552 if (list_empty(&tmp))
Thomas Gleixner9467f852016-09-22 08:05:17 -06002553 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002554
Jens Axboee57690f2016-08-24 15:34:35 -06002555 spin_lock(&hctx->lock);
2556 list_splice_tail_init(&tmp, &hctx->dispatch);
2557 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06002558
2559 blk_mq_run_hw_queue(hctx, true);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002560 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002561}
2562
Thomas Gleixner9467f852016-09-22 08:05:17 -06002563static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
Jens Axboe484b4062014-05-21 14:01:15 -06002564{
Ming Leibf0beec2020-05-29 15:53:15 +02002565 if (!(hctx->flags & BLK_MQ_F_STACKING))
2566 cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2567 &hctx->cpuhp_online);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002568 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
2569 &hctx->cpuhp_dead);
Jens Axboe484b4062014-05-21 14:01:15 -06002570}
2571
Ming Leic3b4afc2015-06-04 22:25:04 +08002572/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08002573static void blk_mq_exit_hctx(struct request_queue *q,
2574 struct blk_mq_tag_set *set,
2575 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
2576{
Ming Lei8ab0b7d2018-01-09 21:28:29 +08002577 if (blk_mq_hw_queue_mapped(hctx))
2578 blk_mq_tag_idle(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002579
Ming Leif70ced02014-09-25 23:23:47 +08002580 if (set->ops->exit_request)
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002581 set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx);
Ming Leif70ced02014-09-25 23:23:47 +08002582
Ming Lei08e98fc2014-09-25 23:23:38 +08002583 if (set->ops->exit_hctx)
2584 set->ops->exit_hctx(hctx, hctx_idx);
2585
Thomas Gleixner9467f852016-09-22 08:05:17 -06002586 blk_mq_remove_cpuhp(hctx);
Ming Lei2f8f1332019-04-30 09:52:27 +08002587
2588 spin_lock(&q->unused_hctx_lock);
2589 list_add(&hctx->hctx_list, &q->unused_hctx_list);
2590 spin_unlock(&q->unused_hctx_lock);
Ming Lei08e98fc2014-09-25 23:23:38 +08002591}
2592
Ming Lei624dbe42014-05-27 23:35:13 +08002593static void blk_mq_exit_hw_queues(struct request_queue *q,
2594 struct blk_mq_tag_set *set, int nr_queue)
2595{
2596 struct blk_mq_hw_ctx *hctx;
2597 unsigned int i;
2598
2599 queue_for_each_hw_ctx(q, hctx, i) {
2600 if (i == nr_queue)
2601 break;
Jianchao Wang477e19d2018-10-12 18:07:25 +08002602 blk_mq_debugfs_unregister_hctx(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002603 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08002604 }
Ming Lei624dbe42014-05-27 23:35:13 +08002605}
2606
Ming Lei7c6c5b72019-04-30 09:52:26 +08002607static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
2608{
2609 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
2610
2611 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
2612 __alignof__(struct blk_mq_hw_ctx)) !=
2613 sizeof(struct blk_mq_hw_ctx));
2614
2615 if (tag_set->flags & BLK_MQ_F_BLOCKING)
2616 hw_ctx_size += sizeof(struct srcu_struct);
2617
2618 return hw_ctx_size;
2619}
2620
Ming Lei08e98fc2014-09-25 23:23:38 +08002621static int blk_mq_init_hctx(struct request_queue *q,
2622 struct blk_mq_tag_set *set,
2623 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
2624{
Ming Lei7c6c5b72019-04-30 09:52:26 +08002625 hctx->queue_num = hctx_idx;
Ming Lei08e98fc2014-09-25 23:23:38 +08002626
Ming Leibf0beec2020-05-29 15:53:15 +02002627 if (!(hctx->flags & BLK_MQ_F_STACKING))
2628 cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2629 &hctx->cpuhp_online);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002630 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
2631
2632 hctx->tags = set->tags[hctx_idx];
2633
2634 if (set->ops->init_hctx &&
2635 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
2636 goto unregister_cpu_notifier;
2637
2638 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
2639 hctx->numa_node))
2640 goto exit_hctx;
2641 return 0;
2642
2643 exit_hctx:
2644 if (set->ops->exit_hctx)
2645 set->ops->exit_hctx(hctx, hctx_idx);
2646 unregister_cpu_notifier:
2647 blk_mq_remove_cpuhp(hctx);
2648 return -1;
2649}
2650
2651static struct blk_mq_hw_ctx *
2652blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
2653 int node)
2654{
2655 struct blk_mq_hw_ctx *hctx;
2656 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
2657
2658 hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node);
2659 if (!hctx)
2660 goto fail_alloc_hctx;
2661
2662 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
2663 goto free_hctx;
2664
2665 atomic_set(&hctx->nr_active, 0);
Kashyap Desaib4455472020-08-19 23:20:28 +08002666 atomic_set(&hctx->elevator_queued, 0);
Ming Lei08e98fc2014-09-25 23:23:38 +08002667 if (node == NUMA_NO_NODE)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002668 node = set->numa_node;
2669 hctx->numa_node = node;
Ming Lei08e98fc2014-09-25 23:23:38 +08002670
Jens Axboe9f993732017-04-10 09:54:54 -06002671 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08002672 spin_lock_init(&hctx->lock);
2673 INIT_LIST_HEAD(&hctx->dispatch);
2674 hctx->queue = q;
Ming Lei51db1c32020-08-19 23:20:19 +08002675 hctx->flags = set->flags & ~BLK_MQ_F_TAG_QUEUE_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08002676
Ming Lei2f8f1332019-04-30 09:52:27 +08002677 INIT_LIST_HEAD(&hctx->hctx_list);
2678
Ming Lei08e98fc2014-09-25 23:23:38 +08002679 /*
2680 * Allocate space for all possible cpus to avoid allocation at
2681 * runtime
2682 */
Johannes Thumshirnd904bfa2017-11-15 17:32:33 -08002683 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
Ming Lei7c6c5b72019-04-30 09:52:26 +08002684 gfp, node);
Ming Lei08e98fc2014-09-25 23:23:38 +08002685 if (!hctx->ctxs)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002686 goto free_cpumask;
Ming Lei08e98fc2014-09-25 23:23:38 +08002687
Jianchao Wang5b202852018-10-12 18:07:26 +08002688 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
Ming Lei7c6c5b72019-04-30 09:52:26 +08002689 gfp, node))
Ming Lei08e98fc2014-09-25 23:23:38 +08002690 goto free_ctxs;
Ming Lei08e98fc2014-09-25 23:23:38 +08002691 hctx->nr_ctx = 0;
2692
Ming Lei5815839b2018-06-25 19:31:47 +08002693 spin_lock_init(&hctx->dispatch_wait_lock);
Jens Axboeeb619fd2017-11-09 08:32:43 -07002694 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
2695 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
2696
Guoqing Jiang754a1572020-03-09 22:41:37 +01002697 hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp);
Ming Leif70ced02014-09-25 23:23:47 +08002698 if (!hctx->fq)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002699 goto free_bitmap;
Ming Leif70ced02014-09-25 23:23:47 +08002700
Bart Van Assche6a83e742016-11-02 10:09:51 -06002701 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -08002702 init_srcu_struct(hctx->srcu);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002703 blk_mq_hctx_kobj_init(hctx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06002704
Ming Lei7c6c5b72019-04-30 09:52:26 +08002705 return hctx;
Ming Lei08e98fc2014-09-25 23:23:38 +08002706
2707 free_bitmap:
Omar Sandoval88459642016-09-17 08:38:44 -06002708 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08002709 free_ctxs:
2710 kfree(hctx->ctxs);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002711 free_cpumask:
2712 free_cpumask_var(hctx->cpumask);
2713 free_hctx:
2714 kfree(hctx);
2715 fail_alloc_hctx:
2716 return NULL;
Ming Lei08e98fc2014-09-25 23:23:38 +08002717}
2718
Jens Axboe320ae512013-10-24 09:20:05 +01002719static void blk_mq_init_cpu_queues(struct request_queue *q,
2720 unsigned int nr_hw_queues)
2721{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002722 struct blk_mq_tag_set *set = q->tag_set;
2723 unsigned int i, j;
Jens Axboe320ae512013-10-24 09:20:05 +01002724
2725 for_each_possible_cpu(i) {
2726 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
2727 struct blk_mq_hw_ctx *hctx;
Ming Leic16d6b52018-12-17 08:44:05 -07002728 int k;
Jens Axboe320ae512013-10-24 09:20:05 +01002729
Jens Axboe320ae512013-10-24 09:20:05 +01002730 __ctx->cpu = i;
2731 spin_lock_init(&__ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002732 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
2733 INIT_LIST_HEAD(&__ctx->rq_lists[k]);
2734
Jens Axboe320ae512013-10-24 09:20:05 +01002735 __ctx->queue = q;
2736
Jens Axboe320ae512013-10-24 09:20:05 +01002737 /*
2738 * Set local node, IFF we have more than one hw queue. If
2739 * not, we remain on the home node of the device
2740 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06002741 for (j = 0; j < set->nr_maps; j++) {
2742 hctx = blk_mq_map_queue_type(q, j, i);
2743 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
Xianting Tian576e85c2020-10-19 16:20:47 +08002744 hctx->numa_node = cpu_to_node(i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06002745 }
Jens Axboe320ae512013-10-24 09:20:05 +01002746 }
2747}
2748
Weiping Zhang03b63b02020-05-07 21:04:22 +08002749static bool __blk_mq_alloc_map_and_request(struct blk_mq_tag_set *set,
2750 int hctx_idx)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002751{
John Garry1c0706a2020-08-19 23:20:22 +08002752 unsigned int flags = set->flags;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002753 int ret = 0;
2754
2755 set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
John Garry1c0706a2020-08-19 23:20:22 +08002756 set->queue_depth, set->reserved_tags, flags);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002757 if (!set->tags[hctx_idx])
2758 return false;
2759
2760 ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
2761 set->queue_depth);
2762 if (!ret)
2763 return true;
2764
John Garry1c0706a2020-08-19 23:20:22 +08002765 blk_mq_free_rq_map(set->tags[hctx_idx], flags);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002766 set->tags[hctx_idx] = NULL;
2767 return false;
2768}
2769
2770static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
2771 unsigned int hctx_idx)
2772{
John Garry1c0706a2020-08-19 23:20:22 +08002773 unsigned int flags = set->flags;
2774
Dan Carpenter4e6db0f2018-11-29 13:56:54 +03002775 if (set->tags && set->tags[hctx_idx]) {
Jens Axboebd166ef2017-01-17 06:03:22 -07002776 blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
John Garry1c0706a2020-08-19 23:20:22 +08002777 blk_mq_free_rq_map(set->tags[hctx_idx], flags);
Jens Axboebd166ef2017-01-17 06:03:22 -07002778 set->tags[hctx_idx] = NULL;
2779 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002780}
2781
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002782static void blk_mq_map_swqueue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01002783{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002784 unsigned int i, j, hctx_idx;
Jens Axboe320ae512013-10-24 09:20:05 +01002785 struct blk_mq_hw_ctx *hctx;
2786 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08002787 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002788
2789 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06002790 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01002791 hctx->nr_ctx = 0;
huhaid416c922018-05-18 08:32:30 -06002792 hctx->dispatch_from = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002793 }
2794
2795 /*
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002796 * Map software to hardware queues.
Ming Lei4412efe2018-04-25 04:01:44 +08002797 *
2798 * If the cpu isn't present, the cpu is mapped to first hctx.
Jens Axboe320ae512013-10-24 09:20:05 +01002799 */
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08002800 for_each_possible_cpu(i) {
Ming Lei4412efe2018-04-25 04:01:44 +08002801
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01002802 ctx = per_cpu_ptr(q->queue_ctx, i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06002803 for (j = 0; j < set->nr_maps; j++) {
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002804 if (!set->map[j].nr_queues) {
2805 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2806 HCTX_TYPE_DEFAULT, i);
Ming Leie5edd5f2018-12-18 01:28:56 +08002807 continue;
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002808 }
Ming Leifd689872020-05-07 21:04:08 +08002809 hctx_idx = set->map[j].mq_map[i];
2810 /* unmapped hw queue can be remapped after CPU topo changed */
2811 if (!set->tags[hctx_idx] &&
Weiping Zhang03b63b02020-05-07 21:04:22 +08002812 !__blk_mq_alloc_map_and_request(set, hctx_idx)) {
Ming Leifd689872020-05-07 21:04:08 +08002813 /*
2814 * If tags initialization fail for some hctx,
2815 * that hctx won't be brought online. In this
2816 * case, remap the current ctx to hctx[0] which
2817 * is guaranteed to always have tags allocated
2818 */
2819 set->map[j].mq_map[i] = 0;
2820 }
Ming Leie5edd5f2018-12-18 01:28:56 +08002821
Jens Axboeb3c661b2018-10-30 10:36:06 -06002822 hctx = blk_mq_map_queue_type(q, j, i);
Jianchao Wang8ccdf4a2019-01-24 18:25:32 +08002823 ctx->hctxs[j] = hctx;
Jens Axboeb3c661b2018-10-30 10:36:06 -06002824 /*
2825 * If the CPU is already set in the mask, then we've
2826 * mapped this one already. This can happen if
2827 * devices share queues across queue maps.
2828 */
2829 if (cpumask_test_cpu(i, hctx->cpumask))
2830 continue;
2831
2832 cpumask_set_cpu(i, hctx->cpumask);
2833 hctx->type = j;
2834 ctx->index_hw[hctx->type] = hctx->nr_ctx;
2835 hctx->ctxs[hctx->nr_ctx++] = ctx;
2836
2837 /*
2838 * If the nr_ctx type overflows, we have exceeded the
2839 * amount of sw queues we can support.
2840 */
2841 BUG_ON(!hctx->nr_ctx);
2842 }
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002843
2844 for (; j < HCTX_MAX_TYPES; j++)
2845 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2846 HCTX_TYPE_DEFAULT, i);
Jens Axboe320ae512013-10-24 09:20:05 +01002847 }
Jens Axboe506e9312014-05-07 10:26:44 -06002848
2849 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei4412efe2018-04-25 04:01:44 +08002850 /*
2851 * If no software queues are mapped to this hardware queue,
2852 * disable it and free the request entries.
2853 */
2854 if (!hctx->nr_ctx) {
2855 /* Never unmap queue 0. We need it as a
2856 * fallback in case of a new remap fails
2857 * allocation
2858 */
2859 if (i && set->tags[i])
2860 blk_mq_free_map_and_requests(set, i);
2861
2862 hctx->tags = NULL;
2863 continue;
2864 }
Jens Axboe484b4062014-05-21 14:01:15 -06002865
Ming Lei2a34c082015-04-21 10:00:20 +08002866 hctx->tags = set->tags[i];
2867 WARN_ON(!hctx->tags);
2868
Jens Axboe484b4062014-05-21 14:01:15 -06002869 /*
Chong Yuan889fa312015-04-15 11:39:29 -06002870 * Set the map size to the number of mapped software queues.
2871 * This is more accurate and more efficient than looping
2872 * over all possibly mapped software queues.
2873 */
Omar Sandoval88459642016-09-17 08:38:44 -06002874 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
Chong Yuan889fa312015-04-15 11:39:29 -06002875
2876 /*
Jens Axboe484b4062014-05-21 14:01:15 -06002877 * Initialize batch roundrobin counts
2878 */
Ming Leif82ddf12018-04-08 17:48:10 +08002879 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06002880 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
2881 }
Jens Axboe320ae512013-10-24 09:20:05 +01002882}
2883
Jens Axboe8e8320c2017-06-20 17:56:13 -06002884/*
2885 * Caller needs to ensure that we're either frozen/quiesced, or that
2886 * the queue isn't live yet.
2887 */
Jeff Moyer2404e602015-11-03 10:40:06 -05002888static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06002889{
2890 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06002891 int i;
2892
Jeff Moyer2404e602015-11-03 10:40:06 -05002893 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei97889f92018-06-25 19:31:48 +08002894 if (shared)
Ming Lei51db1c32020-08-19 23:20:19 +08002895 hctx->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
Ming Lei97889f92018-06-25 19:31:48 +08002896 else
Ming Lei51db1c32020-08-19 23:20:19 +08002897 hctx->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05002898 }
2899}
2900
Hannes Reinecke655ac302020-08-19 23:20:20 +08002901static void blk_mq_update_tag_set_shared(struct blk_mq_tag_set *set,
2902 bool shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05002903{
2904 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06002905
Bart Van Assche705cda92017-04-07 11:16:49 -07002906 lockdep_assert_held(&set->tag_list_lock);
2907
Jens Axboe0d2602c2014-05-13 15:10:52 -06002908 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2909 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05002910 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002911 blk_mq_unfreeze_queue(q);
2912 }
2913}
2914
2915static void blk_mq_del_queue_tag_set(struct request_queue *q)
2916{
2917 struct blk_mq_tag_set *set = q->tag_set;
2918
Jens Axboe0d2602c2014-05-13 15:10:52 -06002919 mutex_lock(&set->tag_list_lock);
Daniel Wagner08c875c2020-07-28 15:29:51 +02002920 list_del(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05002921 if (list_is_singular(&set->tag_list)) {
2922 /* just transitioned to unshared */
Ming Lei51db1c32020-08-19 23:20:19 +08002923 set->flags &= ~BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05002924 /* update existing queue */
Hannes Reinecke655ac302020-08-19 23:20:20 +08002925 blk_mq_update_tag_set_shared(set, false);
Jeff Moyer2404e602015-11-03 10:40:06 -05002926 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06002927 mutex_unlock(&set->tag_list_lock);
Roman Pena347c7a2018-06-10 22:38:24 +02002928 INIT_LIST_HEAD(&q->tag_set_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002929}
2930
2931static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
2932 struct request_queue *q)
2933{
Jens Axboe0d2602c2014-05-13 15:10:52 -06002934 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05002935
Jens Axboeff821d22017-11-10 22:05:12 -07002936 /*
2937 * Check to see if we're transitioning to shared (from 1 to 2 queues).
2938 */
2939 if (!list_empty(&set->tag_list) &&
Ming Lei51db1c32020-08-19 23:20:19 +08002940 !(set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)) {
2941 set->flags |= BLK_MQ_F_TAG_QUEUE_SHARED;
Jeff Moyer2404e602015-11-03 10:40:06 -05002942 /* update existing queue */
Hannes Reinecke655ac302020-08-19 23:20:20 +08002943 blk_mq_update_tag_set_shared(set, true);
Jeff Moyer2404e602015-11-03 10:40:06 -05002944 }
Ming Lei51db1c32020-08-19 23:20:19 +08002945 if (set->flags & BLK_MQ_F_TAG_QUEUE_SHARED)
Jeff Moyer2404e602015-11-03 10:40:06 -05002946 queue_set_hctx_shared(q, true);
Daniel Wagner08c875c2020-07-28 15:29:51 +02002947 list_add_tail(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05002948
Jens Axboe0d2602c2014-05-13 15:10:52 -06002949 mutex_unlock(&set->tag_list_lock);
2950}
2951
Ming Lei1db49092018-11-20 09:44:35 +08002952/* All allocations will be freed in release handler of q->mq_kobj */
2953static int blk_mq_alloc_ctxs(struct request_queue *q)
2954{
2955 struct blk_mq_ctxs *ctxs;
2956 int cpu;
2957
2958 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
2959 if (!ctxs)
2960 return -ENOMEM;
2961
2962 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2963 if (!ctxs->queue_ctx)
2964 goto fail;
2965
2966 for_each_possible_cpu(cpu) {
2967 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
2968 ctx->ctxs = ctxs;
2969 }
2970
2971 q->mq_kobj = &ctxs->kobj;
2972 q->queue_ctx = ctxs->queue_ctx;
2973
2974 return 0;
2975 fail:
2976 kfree(ctxs);
2977 return -ENOMEM;
2978}
2979
Ming Leie09aae72015-01-29 20:17:27 +08002980/*
2981 * It is the actual release handler for mq, but we do it from
2982 * request queue's release handler for avoiding use-after-free
2983 * and headache because q->mq_kobj shouldn't have been introduced,
2984 * but we can't group ctx/kctx kobj without it.
2985 */
2986void blk_mq_release(struct request_queue *q)
2987{
Ming Lei2f8f1332019-04-30 09:52:27 +08002988 struct blk_mq_hw_ctx *hctx, *next;
2989 int i;
Ming Leie09aae72015-01-29 20:17:27 +08002990
Ming Lei2f8f1332019-04-30 09:52:27 +08002991 queue_for_each_hw_ctx(q, hctx, i)
2992 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
2993
2994 /* all hctx are in .unused_hctx_list now */
2995 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
2996 list_del_init(&hctx->hctx_list);
Ming Lei6c8b2322017-02-22 18:14:01 +08002997 kobject_put(&hctx->kobj);
Ming Leic3b4afc2015-06-04 22:25:04 +08002998 }
Ming Leie09aae72015-01-29 20:17:27 +08002999
3000 kfree(q->queue_hw_ctx);
3001
Ming Lei7ea5fe32017-02-22 18:14:00 +08003002 /*
3003 * release .mq_kobj and sw queue's kobject now because
3004 * both share lifetime with request queue.
3005 */
3006 blk_mq_sysfs_deinit(q);
Ming Leie09aae72015-01-29 20:17:27 +08003007}
3008
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003009struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set,
3010 void *queuedata)
Jens Axboe320ae512013-10-24 09:20:05 +01003011{
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003012 struct request_queue *uninit_q, *q;
3013
Christoph Hellwigc62b37d2020-07-01 10:59:43 +02003014 uninit_q = blk_alloc_queue(set->numa_node);
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003015 if (!uninit_q)
3016 return ERR_PTR(-ENOMEM);
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003017 uninit_q->queuedata = queuedata;
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003018
Damien Le Moal737eb782019-09-05 18:51:33 +09003019 /*
3020 * Initialize the queue without an elevator. device_add_disk() will do
3021 * the initialization.
3022 */
3023 q = blk_mq_init_allocated_queue(set, uninit_q, false);
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003024 if (IS_ERR(q))
3025 blk_cleanup_queue(uninit_q);
3026
3027 return q;
3028}
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01003029EXPORT_SYMBOL_GPL(blk_mq_init_queue_data);
3030
3031struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
3032{
3033 return blk_mq_init_queue_data(set, NULL);
3034}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003035EXPORT_SYMBOL(blk_mq_init_queue);
3036
Jens Axboe9316a9e2018-10-15 08:40:37 -06003037/*
3038 * Helper for setting up a queue with mq ops, given queue depth, and
3039 * the passed in mq ops flags.
3040 */
3041struct request_queue *blk_mq_init_sq_queue(struct blk_mq_tag_set *set,
3042 const struct blk_mq_ops *ops,
3043 unsigned int queue_depth,
3044 unsigned int set_flags)
3045{
3046 struct request_queue *q;
3047 int ret;
3048
3049 memset(set, 0, sizeof(*set));
3050 set->ops = ops;
3051 set->nr_hw_queues = 1;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003052 set->nr_maps = 1;
Jens Axboe9316a9e2018-10-15 08:40:37 -06003053 set->queue_depth = queue_depth;
3054 set->numa_node = NUMA_NO_NODE;
3055 set->flags = set_flags;
3056
3057 ret = blk_mq_alloc_tag_set(set);
3058 if (ret)
3059 return ERR_PTR(ret);
3060
3061 q = blk_mq_init_queue(set);
3062 if (IS_ERR(q)) {
3063 blk_mq_free_tag_set(set);
3064 return q;
3065 }
3066
3067 return q;
3068}
3069EXPORT_SYMBOL(blk_mq_init_sq_queue);
3070
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003071static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
3072 struct blk_mq_tag_set *set, struct request_queue *q,
3073 int hctx_idx, int node)
3074{
Ming Lei2f8f1332019-04-30 09:52:27 +08003075 struct blk_mq_hw_ctx *hctx = NULL, *tmp;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003076
Ming Lei2f8f1332019-04-30 09:52:27 +08003077 /* reuse dead hctx first */
3078 spin_lock(&q->unused_hctx_lock);
3079 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
3080 if (tmp->numa_node == node) {
3081 hctx = tmp;
3082 break;
3083 }
3084 }
3085 if (hctx)
3086 list_del_init(&hctx->hctx_list);
3087 spin_unlock(&q->unused_hctx_lock);
3088
3089 if (!hctx)
3090 hctx = blk_mq_alloc_hctx(q, set, node);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003091 if (!hctx)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003092 goto fail;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003093
Ming Lei7c6c5b72019-04-30 09:52:26 +08003094 if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
3095 goto free_hctx;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003096
3097 return hctx;
Ming Lei7c6c5b72019-04-30 09:52:26 +08003098
3099 free_hctx:
3100 kobject_put(&hctx->kobj);
3101 fail:
3102 return NULL;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003103}
3104
Keith Busch868f2f02015-12-17 17:08:14 -07003105static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
3106 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003107{
Jianchao Wange01ad462018-10-12 18:07:28 +08003108 int i, j, end;
Keith Busch868f2f02015-12-17 17:08:14 -07003109 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01003110
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003111 if (q->nr_hw_queues < set->nr_hw_queues) {
3112 struct blk_mq_hw_ctx **new_hctxs;
3113
3114 new_hctxs = kcalloc_node(set->nr_hw_queues,
3115 sizeof(*new_hctxs), GFP_KERNEL,
3116 set->numa_node);
3117 if (!new_hctxs)
3118 return;
3119 if (hctxs)
3120 memcpy(new_hctxs, hctxs, q->nr_hw_queues *
3121 sizeof(*hctxs));
3122 q->queue_hw_ctx = new_hctxs;
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003123 kfree(hctxs);
3124 hctxs = new_hctxs;
3125 }
3126
Ming Leifb350e02018-01-06 16:27:40 +08003127 /* protect against switching io scheduler */
3128 mutex_lock(&q->sysfs_lock);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003129 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003130 int node;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003131 struct blk_mq_hw_ctx *hctx;
Keith Busch868f2f02015-12-17 17:08:14 -07003132
Dongli Zhang7d76f852019-02-27 21:35:01 +08003133 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003134 /*
3135 * If the hw queue has been mapped to another numa node,
3136 * we need to realloc the hctx. If allocation fails, fallback
3137 * to use the previous one.
3138 */
3139 if (hctxs[i] && (hctxs[i]->numa_node == node))
3140 continue;
Jens Axboe320ae512013-10-24 09:20:05 +01003141
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003142 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
3143 if (hctx) {
Ming Lei2f8f1332019-04-30 09:52:27 +08003144 if (hctxs[i])
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003145 blk_mq_exit_hctx(q, set, hctxs[i], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003146 hctxs[i] = hctx;
3147 } else {
3148 if (hctxs[i])
3149 pr_warn("Allocate new hctx on node %d fails,\
3150 fallback to previous one on node %d\n",
3151 node, hctxs[i]->numa_node);
3152 else
3153 break;
Keith Busch868f2f02015-12-17 17:08:14 -07003154 }
Jens Axboe320ae512013-10-24 09:20:05 +01003155 }
Jianchao Wange01ad462018-10-12 18:07:28 +08003156 /*
3157 * Increasing nr_hw_queues fails. Free the newly allocated
3158 * hctxs and keep the previous q->nr_hw_queues.
3159 */
3160 if (i != set->nr_hw_queues) {
3161 j = q->nr_hw_queues;
3162 end = i;
3163 } else {
3164 j = i;
3165 end = q->nr_hw_queues;
3166 q->nr_hw_queues = set->nr_hw_queues;
3167 }
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003168
Jianchao Wange01ad462018-10-12 18:07:28 +08003169 for (; j < end; j++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003170 struct blk_mq_hw_ctx *hctx = hctxs[j];
3171
3172 if (hctx) {
Jens Axboecc71a6f2017-01-11 14:29:56 -07003173 if (hctx->tags)
3174 blk_mq_free_map_and_requests(set, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003175 blk_mq_exit_hctx(q, set, hctx, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003176 hctxs[j] = NULL;
Keith Busch868f2f02015-12-17 17:08:14 -07003177 }
3178 }
Ming Leifb350e02018-01-06 16:27:40 +08003179 mutex_unlock(&q->sysfs_lock);
Keith Busch868f2f02015-12-17 17:08:14 -07003180}
3181
3182struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
Damien Le Moal737eb782019-09-05 18:51:33 +09003183 struct request_queue *q,
3184 bool elevator_init)
Keith Busch868f2f02015-12-17 17:08:14 -07003185{
Ming Lei66841672016-02-12 15:27:00 +08003186 /* mark the queue as mq asap */
3187 q->mq_ops = set->ops;
3188
Omar Sandoval34dbad52017-03-21 08:56:08 -07003189 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
Stephen Bates720b8cc2017-04-07 06:24:03 -06003190 blk_mq_poll_stats_bkt,
3191 BLK_MQ_POLL_STATS_BKTS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -07003192 if (!q->poll_cb)
3193 goto err_exit;
3194
Ming Lei1db49092018-11-20 09:44:35 +08003195 if (blk_mq_alloc_ctxs(q))
Jes Sorensen41de54c2019-04-19 16:35:44 -04003196 goto err_poll;
Keith Busch868f2f02015-12-17 17:08:14 -07003197
Ming Lei737f98c2017-02-22 18:13:59 +08003198 /* init q->mq_kobj and sw queues' kobjects */
3199 blk_mq_sysfs_init(q);
3200
Ming Lei2f8f1332019-04-30 09:52:27 +08003201 INIT_LIST_HEAD(&q->unused_hctx_list);
3202 spin_lock_init(&q->unused_hctx_lock);
3203
Keith Busch868f2f02015-12-17 17:08:14 -07003204 blk_mq_realloc_hw_ctxs(set, q);
3205 if (!q->nr_hw_queues)
3206 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01003207
Christoph Hellwig287922e2015-10-30 20:57:30 +08003208 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08003209 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01003210
Jens Axboea8908932018-10-16 14:23:06 -06003211 q->tag_set = set;
Jens Axboe320ae512013-10-24 09:20:05 +01003212
Jens Axboe94eddfb2013-11-19 09:25:07 -07003213 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Ming Leicd191812018-12-18 12:15:29 +08003214 if (set->nr_maps > HCTX_TYPE_POLL &&
3215 set->map[HCTX_TYPE_POLL].nr_queues)
Christoph Hellwig6544d222018-12-02 17:46:28 +01003216 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
Jens Axboe320ae512013-10-24 09:20:05 +01003217
Christoph Hellwig1be036e2014-02-07 10:22:39 -08003218 q->sg_reserved_size = INT_MAX;
3219
Mike Snitzer28494502016-09-14 13:28:30 -04003220 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06003221 INIT_LIST_HEAD(&q->requeue_list);
3222 spin_lock_init(&q->requeue_lock);
3223
Jens Axboeeba71762014-05-20 15:17:27 -06003224 q->nr_requests = set->queue_depth;
3225
Jens Axboe64f1c212016-11-14 13:03:03 -07003226 /*
3227 * Default to classic polling
3228 */
Yufen Yu29ece8b2019-03-18 22:44:41 +08003229 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
Jens Axboe64f1c212016-11-14 13:03:03 -07003230
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003231 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003232 blk_mq_add_queue_tag_set(set, q);
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003233 blk_mq_map_swqueue(q);
Akinobu Mita4593fdb2015-09-27 02:09:20 +09003234
Damien Le Moal737eb782019-09-05 18:51:33 +09003235 if (elevator_init)
3236 elevator_init_mq(q);
Jens Axboed3484992017-01-13 14:43:58 -07003237
Jens Axboe320ae512013-10-24 09:20:05 +01003238 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07003239
Jens Axboe320ae512013-10-24 09:20:05 +01003240err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07003241 kfree(q->queue_hw_ctx);
zhengbin73d9c8d2019-07-23 22:10:42 +08003242 q->nr_hw_queues = 0;
Ming Lei1db49092018-11-20 09:44:35 +08003243 blk_mq_sysfs_deinit(q);
Jes Sorensen41de54c2019-04-19 16:35:44 -04003244err_poll:
3245 blk_stat_free_callback(q->poll_cb);
3246 q->poll_cb = NULL;
Ming Linc7de5722016-05-25 23:23:27 -07003247err_exit:
3248 q->mq_ops = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01003249 return ERR_PTR(-ENOMEM);
3250}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003251EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01003252
Ming Leic7e2d942019-04-30 09:52:25 +08003253/* tags can _not_ be used after returning from blk_mq_exit_queue */
3254void blk_mq_exit_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01003255{
Ming Lei624dbe42014-05-27 23:35:13 +08003256 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01003257
Jens Axboe0d2602c2014-05-13 15:10:52 -06003258 blk_mq_del_queue_tag_set(q);
Ming Lei624dbe42014-05-27 23:35:13 +08003259 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01003260}
Jens Axboe320ae512013-10-24 09:20:05 +01003261
Jens Axboea5164402014-09-10 09:02:03 -06003262static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
3263{
3264 int i;
3265
Xianting Tian8229cca2020-09-26 10:39:47 +08003266 for (i = 0; i < set->nr_hw_queues; i++) {
Weiping Zhang03b63b02020-05-07 21:04:22 +08003267 if (!__blk_mq_alloc_map_and_request(set, i))
Jens Axboea5164402014-09-10 09:02:03 -06003268 goto out_unwind;
Xianting Tian8229cca2020-09-26 10:39:47 +08003269 cond_resched();
3270 }
Jens Axboea5164402014-09-10 09:02:03 -06003271
3272 return 0;
3273
3274out_unwind:
3275 while (--i >= 0)
Weiping Zhang2e194422020-05-07 21:03:39 +08003276 blk_mq_free_map_and_requests(set, i);
Jens Axboea5164402014-09-10 09:02:03 -06003277
Jens Axboea5164402014-09-10 09:02:03 -06003278 return -ENOMEM;
3279}
3280
3281/*
3282 * Allocate the request maps associated with this tag_set. Note that this
3283 * may reduce the depth asked for, if memory is tight. set->queue_depth
3284 * will be updated to reflect the allocated depth.
3285 */
Weiping Zhang79fab522020-05-07 21:04:42 +08003286static int blk_mq_alloc_map_and_requests(struct blk_mq_tag_set *set)
Jens Axboea5164402014-09-10 09:02:03 -06003287{
3288 unsigned int depth;
3289 int err;
3290
3291 depth = set->queue_depth;
3292 do {
3293 err = __blk_mq_alloc_rq_maps(set);
3294 if (!err)
3295 break;
3296
3297 set->queue_depth >>= 1;
3298 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
3299 err = -ENOMEM;
3300 break;
3301 }
3302 } while (set->queue_depth);
3303
3304 if (!set->queue_depth || err) {
3305 pr_err("blk-mq: failed to allocate request map\n");
3306 return -ENOMEM;
3307 }
3308
3309 if (depth != set->queue_depth)
3310 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
3311 depth, set->queue_depth);
3312
3313 return 0;
3314}
3315
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003316static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
3317{
Bart Van Assche6e66b492020-03-09 21:26:17 -07003318 /*
3319 * blk_mq_map_queues() and multiple .map_queues() implementations
3320 * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the
3321 * number of hardware queues.
3322 */
3323 if (set->nr_maps == 1)
3324 set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues;
3325
Ming Lei59388702018-12-07 11:03:53 +08003326 if (set->ops->map_queues && !is_kdump_kernel()) {
Jens Axboeb3c661b2018-10-30 10:36:06 -06003327 int i;
3328
Ming Lei7d4901a2018-01-06 16:27:39 +08003329 /*
3330 * transport .map_queues is usually done in the following
3331 * way:
3332 *
3333 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
3334 * mask = get_cpu_mask(queue)
3335 * for_each_cpu(cpu, mask)
Jens Axboeb3c661b2018-10-30 10:36:06 -06003336 * set->map[x].mq_map[cpu] = queue;
Ming Lei7d4901a2018-01-06 16:27:39 +08003337 * }
3338 *
3339 * When we need to remap, the table has to be cleared for
3340 * killing stale mapping since one CPU may not be mapped
3341 * to any hw queue.
3342 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06003343 for (i = 0; i < set->nr_maps; i++)
3344 blk_mq_clear_mq_map(&set->map[i]);
Ming Lei7d4901a2018-01-06 16:27:39 +08003345
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003346 return set->ops->map_queues(set);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003347 } else {
3348 BUG_ON(set->nr_maps > 1);
Dongli Zhang7d76f852019-02-27 21:35:01 +08003349 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003350 }
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003351}
3352
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003353static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set,
3354 int cur_nr_hw_queues, int new_nr_hw_queues)
3355{
3356 struct blk_mq_tags **new_tags;
3357
3358 if (cur_nr_hw_queues >= new_nr_hw_queues)
3359 return 0;
3360
3361 new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *),
3362 GFP_KERNEL, set->numa_node);
3363 if (!new_tags)
3364 return -ENOMEM;
3365
3366 if (set->tags)
3367 memcpy(new_tags, set->tags, cur_nr_hw_queues *
3368 sizeof(*set->tags));
3369 kfree(set->tags);
3370 set->tags = new_tags;
3371 set->nr_hw_queues = new_nr_hw_queues;
3372
3373 return 0;
3374}
3375
Jens Axboea4391c62014-06-05 15:21:56 -06003376/*
3377 * Alloc a tag set to be associated with one or more request queues.
3378 * May fail with EINVAL for various error conditions. May adjust the
Minwoo Imc018c842018-06-30 22:12:41 +09003379 * requested depth down, if it's too large. In that case, the set
Jens Axboea4391c62014-06-05 15:21:56 -06003380 * value will be stored in set->queue_depth.
3381 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003382int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
3383{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003384 int i, ret;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003385
Bart Van Assche205fb5f2014-10-30 14:45:11 +01003386 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
3387
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003388 if (!set->nr_hw_queues)
3389 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06003390 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003391 return -EINVAL;
3392 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
3393 return -EINVAL;
3394
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02003395 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003396 return -EINVAL;
3397
Ming Leide148292017-10-14 17:22:29 +08003398 if (!set->ops->get_budget ^ !set->ops->put_budget)
3399 return -EINVAL;
3400
Jens Axboea4391c62014-06-05 15:21:56 -06003401 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
3402 pr_info("blk-mq: reduced tag depth to %u\n",
3403 BLK_MQ_MAX_DEPTH);
3404 set->queue_depth = BLK_MQ_MAX_DEPTH;
3405 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003406
Jens Axboeb3c661b2018-10-30 10:36:06 -06003407 if (!set->nr_maps)
3408 set->nr_maps = 1;
3409 else if (set->nr_maps > HCTX_MAX_TYPES)
3410 return -EINVAL;
3411
Shaohua Li6637fad2014-11-30 16:00:58 -08003412 /*
3413 * If a crashdump is active, then we are potentially in a very
3414 * memory constrained environment. Limit us to 1 queue and
3415 * 64 tags to prevent using too much memory.
3416 */
3417 if (is_kdump_kernel()) {
3418 set->nr_hw_queues = 1;
Ming Lei59388702018-12-07 11:03:53 +08003419 set->nr_maps = 1;
Shaohua Li6637fad2014-11-30 16:00:58 -08003420 set->queue_depth = min(64U, set->queue_depth);
3421 }
Keith Busch868f2f02015-12-17 17:08:14 -07003422 /*
Jens Axboe392546a2018-10-29 13:25:27 -06003423 * There is no use for more h/w queues than cpus if we just have
3424 * a single map
Keith Busch868f2f02015-12-17 17:08:14 -07003425 */
Jens Axboe392546a2018-10-29 13:25:27 -06003426 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003427 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08003428
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003429 if (blk_mq_realloc_tag_set_tags(set, 0, set->nr_hw_queues) < 0)
Jens Axboea5164402014-09-10 09:02:03 -06003430 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003431
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003432 ret = -ENOMEM;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003433 for (i = 0; i < set->nr_maps; i++) {
3434 set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
Ming Lei07b35eb2018-12-17 18:42:45 +08003435 sizeof(set->map[i].mq_map[0]),
Jens Axboeb3c661b2018-10-30 10:36:06 -06003436 GFP_KERNEL, set->numa_node);
3437 if (!set->map[i].mq_map)
3438 goto out_free_mq_map;
Ming Lei59388702018-12-07 11:03:53 +08003439 set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003440 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003441
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003442 ret = blk_mq_update_queue_map(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003443 if (ret)
3444 goto out_free_mq_map;
3445
Weiping Zhang79fab522020-05-07 21:04:42 +08003446 ret = blk_mq_alloc_map_and_requests(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003447 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003448 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003449
John Garry32bc15a2020-08-19 23:20:24 +08003450 if (blk_mq_is_sbitmap_shared(set->flags)) {
John Garryf1b49fd2020-08-19 23:20:27 +08003451 atomic_set(&set->active_queues_shared_sbitmap, 0);
3452
John Garry32bc15a2020-08-19 23:20:24 +08003453 if (blk_mq_init_shared_sbitmap(set, set->flags)) {
3454 ret = -ENOMEM;
3455 goto out_free_mq_rq_maps;
3456 }
3457 }
3458
Jens Axboe0d2602c2014-05-13 15:10:52 -06003459 mutex_init(&set->tag_list_lock);
3460 INIT_LIST_HEAD(&set->tag_list);
3461
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003462 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003463
John Garry32bc15a2020-08-19 23:20:24 +08003464out_free_mq_rq_maps:
3465 for (i = 0; i < set->nr_hw_queues; i++)
3466 blk_mq_free_map_and_requests(set, i);
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003467out_free_mq_map:
Jens Axboeb3c661b2018-10-30 10:36:06 -06003468 for (i = 0; i < set->nr_maps; i++) {
3469 kfree(set->map[i].mq_map);
3470 set->map[i].mq_map = NULL;
3471 }
Robert Elliott5676e7b2014-09-02 11:38:44 -05003472 kfree(set->tags);
3473 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003474 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003475}
3476EXPORT_SYMBOL(blk_mq_alloc_tag_set);
3477
3478void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
3479{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003480 int i, j;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003481
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003482 for (i = 0; i < set->nr_hw_queues; i++)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003483 blk_mq_free_map_and_requests(set, i);
Jens Axboe484b4062014-05-21 14:01:15 -06003484
John Garry32bc15a2020-08-19 23:20:24 +08003485 if (blk_mq_is_sbitmap_shared(set->flags))
3486 blk_mq_exit_shared_sbitmap(set);
3487
Jens Axboeb3c661b2018-10-30 10:36:06 -06003488 for (j = 0; j < set->nr_maps; j++) {
3489 kfree(set->map[j].mq_map);
3490 set->map[j].mq_map = NULL;
3491 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003492
Ming Lei981bd182014-04-24 00:07:34 +08003493 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05003494 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003495}
3496EXPORT_SYMBOL(blk_mq_free_tag_set);
3497
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003498int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
3499{
3500 struct blk_mq_tag_set *set = q->tag_set;
3501 struct blk_mq_hw_ctx *hctx;
3502 int i, ret;
3503
Jens Axboebd166ef2017-01-17 06:03:22 -07003504 if (!set)
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003505 return -EINVAL;
3506
Aleksei Zakharove5fa8142019-02-08 19:14:05 +03003507 if (q->nr_requests == nr)
3508 return 0;
3509
Jens Axboe70f36b62017-01-19 10:59:07 -07003510 blk_mq_freeze_queue(q);
Ming Lei24f5a902018-01-06 16:27:38 +08003511 blk_mq_quiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003512
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003513 ret = 0;
3514 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07003515 if (!hctx->tags)
3516 continue;
Jens Axboebd166ef2017-01-17 06:03:22 -07003517 /*
3518 * If we're using an MQ scheduler, just update the scheduler
3519 * queue depth. This is similar to what the old code would do.
3520 */
Jens Axboe70f36b62017-01-19 10:59:07 -07003521 if (!hctx->sched_tags) {
weiping zhangc2e82a22017-09-22 23:36:28 +08003522 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
Jens Axboe70f36b62017-01-19 10:59:07 -07003523 false);
John Garry32bc15a2020-08-19 23:20:24 +08003524 if (!ret && blk_mq_is_sbitmap_shared(set->flags))
3525 blk_mq_tag_resize_shared_sbitmap(set, nr);
Jens Axboe70f36b62017-01-19 10:59:07 -07003526 } else {
3527 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
3528 nr, true);
3529 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003530 if (ret)
3531 break;
Jens Axboe77f1e0a2019-01-18 10:34:16 -07003532 if (q->elevator && q->elevator->type->ops.depth_updated)
3533 q->elevator->type->ops.depth_updated(hctx);
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003534 }
3535
3536 if (!ret)
3537 q->nr_requests = nr;
3538
Ming Lei24f5a902018-01-06 16:27:38 +08003539 blk_mq_unquiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003540 blk_mq_unfreeze_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003541
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003542 return ret;
3543}
3544
Jianchao Wangd48ece22018-08-21 15:15:03 +08003545/*
3546 * request_queue and elevator_type pair.
3547 * It is just used by __blk_mq_update_nr_hw_queues to cache
3548 * the elevator_type associated with a request_queue.
3549 */
3550struct blk_mq_qe_pair {
3551 struct list_head node;
3552 struct request_queue *q;
3553 struct elevator_type *type;
3554};
3555
3556/*
3557 * Cache the elevator_type in qe pair list and switch the
3558 * io scheduler to 'none'
3559 */
3560static bool blk_mq_elv_switch_none(struct list_head *head,
3561 struct request_queue *q)
3562{
3563 struct blk_mq_qe_pair *qe;
3564
3565 if (!q->elevator)
3566 return true;
3567
3568 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
3569 if (!qe)
3570 return false;
3571
3572 INIT_LIST_HEAD(&qe->node);
3573 qe->q = q;
3574 qe->type = q->elevator->type;
3575 list_add(&qe->node, head);
3576
3577 mutex_lock(&q->sysfs_lock);
3578 /*
3579 * After elevator_switch_mq, the previous elevator_queue will be
3580 * released by elevator_release. The reference of the io scheduler
3581 * module get by elevator_get will also be put. So we need to get
3582 * a reference of the io scheduler module here to prevent it to be
3583 * removed.
3584 */
3585 __module_get(qe->type->elevator_owner);
3586 elevator_switch_mq(q, NULL);
3587 mutex_unlock(&q->sysfs_lock);
3588
3589 return true;
3590}
3591
3592static void blk_mq_elv_switch_back(struct list_head *head,
3593 struct request_queue *q)
3594{
3595 struct blk_mq_qe_pair *qe;
3596 struct elevator_type *t = NULL;
3597
3598 list_for_each_entry(qe, head, node)
3599 if (qe->q == q) {
3600 t = qe->type;
3601 break;
3602 }
3603
3604 if (!t)
3605 return;
3606
3607 list_del(&qe->node);
3608 kfree(qe);
3609
3610 mutex_lock(&q->sysfs_lock);
3611 elevator_switch_mq(q, t);
3612 mutex_unlock(&q->sysfs_lock);
3613}
3614
Keith Busche4dc2b32017-05-30 14:39:11 -04003615static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
3616 int nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003617{
3618 struct request_queue *q;
Jianchao Wangd48ece22018-08-21 15:15:03 +08003619 LIST_HEAD(head);
Jianchao Wange01ad462018-10-12 18:07:28 +08003620 int prev_nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07003621
Bart Van Assche705cda92017-04-07 11:16:49 -07003622 lockdep_assert_held(&set->tag_list_lock);
3623
Jens Axboe392546a2018-10-29 13:25:27 -06003624 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003625 nr_hw_queues = nr_cpu_ids;
Weiping Zhangfe35ec52020-06-17 14:18:37 +08003626 if (nr_hw_queues < 1)
3627 return;
3628 if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003629 return;
3630
3631 list_for_each_entry(q, &set->tag_list, tag_set_list)
3632 blk_mq_freeze_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +08003633 /*
3634 * Switch IO scheduler to 'none', cleaning up the data associated
3635 * with the previous scheduler. We will switch back once we are done
3636 * updating the new sw to hw queue mappings.
3637 */
3638 list_for_each_entry(q, &set->tag_list, tag_set_list)
3639 if (!blk_mq_elv_switch_none(&head, q))
3640 goto switch_back;
Keith Busch868f2f02015-12-17 17:08:14 -07003641
Jianchao Wang477e19d2018-10-12 18:07:25 +08003642 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3643 blk_mq_debugfs_unregister_hctxs(q);
3644 blk_mq_sysfs_unregister(q);
3645 }
3646
Weiping Zhanga2584e42020-05-07 21:03:56 +08003647 prev_nr_hw_queues = set->nr_hw_queues;
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003648 if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) <
3649 0)
3650 goto reregister;
3651
Keith Busch868f2f02015-12-17 17:08:14 -07003652 set->nr_hw_queues = nr_hw_queues;
Jianchao Wange01ad462018-10-12 18:07:28 +08003653fallback:
Weiping Zhangaa880ad2020-05-13 08:44:05 +08003654 blk_mq_update_queue_map(set);
Keith Busch868f2f02015-12-17 17:08:14 -07003655 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3656 blk_mq_realloc_hw_ctxs(set, q);
Jianchao Wange01ad462018-10-12 18:07:28 +08003657 if (q->nr_hw_queues != set->nr_hw_queues) {
3658 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
3659 nr_hw_queues, prev_nr_hw_queues);
3660 set->nr_hw_queues = prev_nr_hw_queues;
Dongli Zhang7d76f852019-02-27 21:35:01 +08003661 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jianchao Wange01ad462018-10-12 18:07:28 +08003662 goto fallback;
3663 }
Jianchao Wang477e19d2018-10-12 18:07:25 +08003664 blk_mq_map_swqueue(q);
3665 }
3666
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003667reregister:
Jianchao Wang477e19d2018-10-12 18:07:25 +08003668 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3669 blk_mq_sysfs_register(q);
3670 blk_mq_debugfs_register_hctxs(q);
Keith Busch868f2f02015-12-17 17:08:14 -07003671 }
3672
Jianchao Wangd48ece22018-08-21 15:15:03 +08003673switch_back:
3674 list_for_each_entry(q, &set->tag_list, tag_set_list)
3675 blk_mq_elv_switch_back(&head, q);
3676
Keith Busch868f2f02015-12-17 17:08:14 -07003677 list_for_each_entry(q, &set->tag_list, tag_set_list)
3678 blk_mq_unfreeze_queue(q);
3679}
Keith Busche4dc2b32017-05-30 14:39:11 -04003680
3681void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
3682{
3683 mutex_lock(&set->tag_list_lock);
3684 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
3685 mutex_unlock(&set->tag_list_lock);
3686}
Keith Busch868f2f02015-12-17 17:08:14 -07003687EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
3688
Omar Sandoval34dbad52017-03-21 08:56:08 -07003689/* Enable polling stats and return whether they were already enabled. */
3690static bool blk_poll_stats_enable(struct request_queue *q)
3691{
3692 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
Bart Van Assche7dfdbc72018-03-07 17:10:05 -08003693 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
Omar Sandoval34dbad52017-03-21 08:56:08 -07003694 return true;
3695 blk_stat_add_callback(q, q->poll_cb);
3696 return false;
3697}
3698
3699static void blk_mq_poll_stats_start(struct request_queue *q)
3700{
3701 /*
3702 * We don't arm the callback if polling stats are not enabled or the
3703 * callback is already active.
3704 */
3705 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
3706 blk_stat_is_active(q->poll_cb))
3707 return;
3708
3709 blk_stat_activate_msecs(q->poll_cb, 100);
3710}
3711
3712static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
3713{
3714 struct request_queue *q = cb->data;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003715 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -07003716
Stephen Bates720b8cc2017-04-07 06:24:03 -06003717 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
3718 if (cb->stat[bucket].nr_samples)
3719 q->poll_stat[bucket] = cb->stat[bucket];
3720 }
Omar Sandoval34dbad52017-03-21 08:56:08 -07003721}
3722
Jens Axboe64f1c212016-11-14 13:03:03 -07003723static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
Jens Axboe64f1c212016-11-14 13:03:03 -07003724 struct request *rq)
3725{
Jens Axboe64f1c212016-11-14 13:03:03 -07003726 unsigned long ret = 0;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003727 int bucket;
Jens Axboe64f1c212016-11-14 13:03:03 -07003728
3729 /*
3730 * If stats collection isn't on, don't sleep but turn it on for
3731 * future users
3732 */
Omar Sandoval34dbad52017-03-21 08:56:08 -07003733 if (!blk_poll_stats_enable(q))
Jens Axboe64f1c212016-11-14 13:03:03 -07003734 return 0;
3735
3736 /*
Jens Axboe64f1c212016-11-14 13:03:03 -07003737 * As an optimistic guess, use half of the mean service time
3738 * for this type of request. We can (and should) make this smarter.
3739 * For instance, if the completion latencies are tight, we can
3740 * get closer than just half the mean. This is especially
3741 * important on devices where the completion latencies are longer
Stephen Bates720b8cc2017-04-07 06:24:03 -06003742 * than ~10 usec. We do use the stats for the relevant IO size
3743 * if available which does lead to better estimates.
Jens Axboe64f1c212016-11-14 13:03:03 -07003744 */
Stephen Bates720b8cc2017-04-07 06:24:03 -06003745 bucket = blk_mq_poll_stats_bkt(rq);
3746 if (bucket < 0)
3747 return ret;
3748
3749 if (q->poll_stat[bucket].nr_samples)
3750 ret = (q->poll_stat[bucket].mean + 1) / 2;
Jens Axboe64f1c212016-11-14 13:03:03 -07003751
3752 return ret;
3753}
3754
Jens Axboe06426ad2016-11-14 13:01:59 -07003755static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
3756 struct request *rq)
3757{
3758 struct hrtimer_sleeper hs;
3759 enum hrtimer_mode mode;
Jens Axboe64f1c212016-11-14 13:03:03 -07003760 unsigned int nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003761 ktime_t kt;
3762
Jens Axboe76a86f92018-01-10 11:30:56 -07003763 if (rq->rq_flags & RQF_MQ_POLL_SLEPT)
Jens Axboe64f1c212016-11-14 13:03:03 -07003764 return false;
3765
3766 /*
Jens Axboe1052b8a2018-11-26 08:21:49 -07003767 * If we get here, hybrid polling is enabled. Hence poll_nsec can be:
Jens Axboe64f1c212016-11-14 13:03:03 -07003768 *
Jens Axboe64f1c212016-11-14 13:03:03 -07003769 * 0: use half of prev avg
3770 * >0: use this specific value
3771 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07003772 if (q->poll_nsec > 0)
Jens Axboe64f1c212016-11-14 13:03:03 -07003773 nsecs = q->poll_nsec;
3774 else
John Garrycae740a2020-02-26 20:10:15 +08003775 nsecs = blk_mq_poll_nsecs(q, rq);
Jens Axboe64f1c212016-11-14 13:03:03 -07003776
3777 if (!nsecs)
Jens Axboe06426ad2016-11-14 13:01:59 -07003778 return false;
3779
Jens Axboe76a86f92018-01-10 11:30:56 -07003780 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
Jens Axboe06426ad2016-11-14 13:01:59 -07003781
3782 /*
3783 * This will be replaced with the stats tracking code, using
3784 * 'avg_completion_time / 2' as the pre-sleep target.
3785 */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01003786 kt = nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003787
3788 mode = HRTIMER_MODE_REL;
Sebastian Andrzej Siewiordbc16252019-07-26 20:30:50 +02003789 hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07003790 hrtimer_set_expires(&hs.timer, kt);
3791
Jens Axboe06426ad2016-11-14 13:01:59 -07003792 do {
Tejun Heo5a61c362018-01-09 08:29:52 -08003793 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
Jens Axboe06426ad2016-11-14 13:01:59 -07003794 break;
3795 set_current_state(TASK_UNINTERRUPTIBLE);
Thomas Gleixner9dd88132019-07-30 21:16:55 +02003796 hrtimer_sleeper_start_expires(&hs, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07003797 if (hs.task)
3798 io_schedule();
3799 hrtimer_cancel(&hs.timer);
3800 mode = HRTIMER_MODE_ABS;
3801 } while (hs.task && !signal_pending(current));
3802
3803 __set_current_state(TASK_RUNNING);
3804 destroy_hrtimer_on_stack(&hs.timer);
3805 return true;
3806}
3807
Jens Axboe1052b8a2018-11-26 08:21:49 -07003808static bool blk_mq_poll_hybrid(struct request_queue *q,
3809 struct blk_mq_hw_ctx *hctx, blk_qc_t cookie)
Jens Axboebbd7bb72016-11-04 09:34:34 -06003810{
Jens Axboe1052b8a2018-11-26 08:21:49 -07003811 struct request *rq;
3812
Yufen Yu29ece8b2019-03-18 22:44:41 +08003813 if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
Jens Axboe1052b8a2018-11-26 08:21:49 -07003814 return false;
3815
3816 if (!blk_qc_t_is_internal(cookie))
3817 rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
3818 else {
3819 rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
3820 /*
3821 * With scheduling, if the request has completed, we'll
3822 * get a NULL return here, as we clear the sched tag when
3823 * that happens. The request still remains valid, like always,
3824 * so we should be safe with just the NULL check.
3825 */
3826 if (!rq)
3827 return false;
3828 }
3829
John Garrycae740a2020-02-26 20:10:15 +08003830 return blk_mq_poll_hybrid_sleep(q, rq);
Jens Axboe1052b8a2018-11-26 08:21:49 -07003831}
3832
Christoph Hellwig529262d2018-12-02 17:46:26 +01003833/**
3834 * blk_poll - poll for IO completions
3835 * @q: the queue
3836 * @cookie: cookie passed back at IO submission time
3837 * @spin: whether to spin for completions
3838 *
3839 * Description:
3840 * Poll for completions on the passed in queue. Returns number of
3841 * completed entries found. If @spin is true, then blk_poll will continue
3842 * looping until at least one completion is found, unless the task is
3843 * otherwise marked running (or we need to reschedule).
3844 */
3845int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin)
Jens Axboe1052b8a2018-11-26 08:21:49 -07003846{
3847 struct blk_mq_hw_ctx *hctx;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003848 long state;
3849
Christoph Hellwig529262d2018-12-02 17:46:26 +01003850 if (!blk_qc_t_valid(cookie) ||
3851 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
Jens Axboe1052b8a2018-11-26 08:21:49 -07003852 return 0;
3853
Christoph Hellwig529262d2018-12-02 17:46:26 +01003854 if (current->plug)
3855 blk_flush_plug_list(current->plug, false);
3856
Jens Axboe1052b8a2018-11-26 08:21:49 -07003857 hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
3858
Jens Axboe06426ad2016-11-14 13:01:59 -07003859 /*
3860 * If we sleep, have the caller restart the poll loop to reset
3861 * the state. Like for the other success return cases, the
3862 * caller is responsible for checking if the IO completed. If
3863 * the IO isn't complete, we'll get called again and will go
3864 * straight to the busy poll loop.
3865 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07003866 if (blk_mq_poll_hybrid(q, hctx, cookie))
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003867 return 1;
Jens Axboe06426ad2016-11-14 13:01:59 -07003868
Jens Axboebbd7bb72016-11-04 09:34:34 -06003869 hctx->poll_considered++;
3870
3871 state = current->state;
Jens Axboeaa61bec2018-11-13 21:32:10 -07003872 do {
Jens Axboebbd7bb72016-11-04 09:34:34 -06003873 int ret;
3874
3875 hctx->poll_invoked++;
3876
Jens Axboe97431392018-11-16 09:48:21 -07003877 ret = q->mq_ops->poll(hctx);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003878 if (ret > 0) {
3879 hctx->poll_success++;
Jens Axboe849a3702018-11-16 08:37:34 -07003880 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003881 return ret;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003882 }
3883
3884 if (signal_pending_state(state, current))
Jens Axboe849a3702018-11-16 08:37:34 -07003885 __set_current_state(TASK_RUNNING);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003886
3887 if (current->state == TASK_RUNNING)
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003888 return 1;
Jens Axboe0a1b8b82018-11-26 08:24:43 -07003889 if (ret < 0 || !spin)
Jens Axboebbd7bb72016-11-04 09:34:34 -06003890 break;
3891 cpu_relax();
Jens Axboeaa61bec2018-11-13 21:32:10 -07003892 } while (!need_resched());
Jens Axboebbd7bb72016-11-04 09:34:34 -06003893
Nitesh Shetty67b41102018-02-13 21:18:12 +05303894 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003895 return 0;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003896}
Christoph Hellwig529262d2018-12-02 17:46:26 +01003897EXPORT_SYMBOL_GPL(blk_poll);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003898
Jens Axboe9cf2bab2018-10-31 17:01:22 -06003899unsigned int blk_mq_rq_cpu(struct request *rq)
3900{
3901 return rq->mq_ctx->cpu;
3902}
3903EXPORT_SYMBOL(blk_mq_rq_cpu);
3904
Jens Axboe320ae512013-10-24 09:20:05 +01003905static int __init blk_mq_init(void)
3906{
Christoph Hellwigc3077b52020-06-11 08:44:41 +02003907 int i;
3908
3909 for_each_possible_cpu(i)
3910 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
3911 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
3912
3913 cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
3914 "block/softirq:dead", NULL,
3915 blk_softirq_cpu_dead);
Thomas Gleixner9467f852016-09-22 08:05:17 -06003916 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
3917 blk_mq_hctx_notify_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02003918 cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online",
3919 blk_mq_hctx_notify_online,
3920 blk_mq_hctx_notify_offline);
Jens Axboe320ae512013-10-24 09:20:05 +01003921 return 0;
3922}
3923subsys_initcall(blk_mq_init);