blob: 961635b40999b6d20e8c3ddc490a838b5aae4d7d [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
Omar Sandoval61318372018-04-26 00:21:58 -0700108 if (rq->part == mi->part)
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];
Jens Axboebf9ae8c2018-01-14 10:40:45 -0700280 req_flags_t rq_flags = 0;
Bart Van Asschec3a148d2017-06-20 11:15:43 -0700281
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200282 if (data->flags & BLK_MQ_REQ_INTERNAL) {
Christoph Hellwig766473682020-05-29 15:53:12 +0200283 rq->tag = BLK_MQ_NO_TAG;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200284 rq->internal_tag = tag;
285 } else {
Jianchao Wangd263ed92018-08-09 08:34:17 -0600286 if (data->hctx->flags & BLK_MQ_F_TAG_SHARED) {
Jens Axboebf9ae8c2018-01-14 10:40:45 -0700287 rq_flags = RQF_MQ_INFLIGHT;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200288 atomic_inc(&data->hctx->nr_active);
289 }
290 rq->tag = tag;
Christoph Hellwig766473682020-05-29 15:53:12 +0200291 rq->internal_tag = BLK_MQ_NO_TAG;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200292 data->hctx->tags->rqs[rq->tag] = rq;
293 }
294
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200295 /* csd/requeue_work/fifo_time is initialized before use */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200296 rq->q = data->q;
297 rq->mq_ctx = data->ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600298 rq->mq_hctx = data->hctx;
Jens Axboebf9ae8c2018-01-14 10:40:45 -0700299 rq->rq_flags = rq_flags;
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200300 rq->cmd_flags = data->cmd_flags;
Bart Van Assche1b6d65a2017-11-09 10:49:55 -0800301 if (data->flags & BLK_MQ_REQ_PREEMPT)
302 rq->rq_flags |= RQF_PREEMPT;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200303 if (blk_queue_io_stat(data->q))
Christoph Hellwige8064022016-10-20 15:12:13 +0200304 rq->rq_flags |= RQF_IO_STAT;
Jens Axboe7c3fb702018-01-10 11:46:39 -0700305 INIT_LIST_HEAD(&rq->queuelist);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200306 INIT_HLIST_NODE(&rq->hash);
307 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200308 rq->rq_disk = NULL;
309 rq->part = NULL;
Tejun Heo6f816b42019-08-28 15:05:57 -0700310#ifdef CONFIG_BLK_RQ_ALLOC_TIME
311 rq->alloc_time_ns = alloc_time_ns;
312#endif
Jens Axboefe1f4522018-11-28 10:50:07 -0700313 if (blk_mq_need_time_stamp(rq))
314 rq->start_time_ns = ktime_get_ns();
315 else
316 rq->start_time_ns = 0;
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700317 rq->io_start_time_ns = 0;
Hou Tao3d244302019-05-21 15:59:03 +0800318 rq->stats_sectors = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200319 rq->nr_phys_segments = 0;
320#if defined(CONFIG_BLK_DEV_INTEGRITY)
321 rq->nr_integrity_segments = 0;
322#endif
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000323 blk_crypto_rq_set_defaults(rq);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200324 /* tag was already set */
Christoph Hellwig079076b2018-11-14 17:02:05 +0100325 WRITE_ONCE(rq->deadline, 0);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200326
Jens Axboef6be4fb2014-06-06 11:03:48 -0600327 rq->timeout = 0;
328
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200329 rq->end_io = NULL;
330 rq->end_io_data = NULL;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200331
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200332 data->ctx->rq_dispatched[op_is_sync(data->cmd_flags)]++;
Keith Busch12f5b932018-05-29 15:52:28 +0200333 refcount_set(&rq->ref, 1);
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200334
335 if (!op_is_flush(data->cmd_flags)) {
336 struct elevator_queue *e = data->q->elevator;
337
338 rq->elv.icq = NULL;
339 if (e && e->type->ops.prepare_request) {
340 if (e->type->icq_cache)
341 blk_mq_sched_assign_ioc(rq);
342
343 e->type->ops.prepare_request(rq);
344 rq->rq_flags |= RQF_ELVPRIV;
345 }
346 }
347
348 data->hctx->queued++;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200349 return rq;
Jens Axboe320ae512013-10-24 09:20:05 +0100350}
351
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200352static struct request *__blk_mq_alloc_request(struct blk_mq_alloc_data *data)
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200353{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200354 struct request_queue *q = data->q;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200355 struct elevator_queue *e = q->elevator;
Tejun Heo6f816b42019-08-28 15:05:57 -0700356 u64 alloc_time_ns = 0;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200357 unsigned int tag;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200358
Tejun Heo6f816b42019-08-28 15:05:57 -0700359 /* alloc_time includes depth and tag waits */
360 if (blk_queue_rq_alloc_time(q))
361 alloc_time_ns = ktime_get_ns();
362
Jens Axboef9afca42018-10-29 13:11:38 -0600363 if (data->cmd_flags & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500364 data->flags |= BLK_MQ_REQ_NOWAIT;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200365
366 if (e) {
367 data->flags |= BLK_MQ_REQ_INTERNAL;
368
369 /*
370 * Flush requests are special and go directly to the
Jens Axboe17a51192018-05-09 13:28:50 -0600371 * dispatch list. Don't include reserved tags in the
372 * limiting, as it isn't useful.
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200373 */
Jens Axboef9afca42018-10-29 13:11:38 -0600374 if (!op_is_flush(data->cmd_flags) &&
375 e->type->ops.limit_depth &&
Jens Axboe17a51192018-05-09 13:28:50 -0600376 !(data->flags & BLK_MQ_REQ_RESERVED))
Jens Axboef9afca42018-10-29 13:11:38 -0600377 e->type->ops.limit_depth(data->cmd_flags, data);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200378 }
379
Ming Leibf0beec2020-05-29 15:53:15 +0200380retry:
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200381 data->ctx = blk_mq_get_ctx(q);
382 data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx);
383 if (!(data->flags & BLK_MQ_REQ_INTERNAL))
384 blk_mq_tag_busy(data->hctx);
385
Ming Leibf0beec2020-05-29 15:53:15 +0200386 /*
387 * Waiting allocations only fail because of an inactive hctx. In that
388 * case just retry the hctx assignment and tag allocation as CPU hotplug
389 * should have migrated us to an online CPU by now.
390 */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200391 tag = blk_mq_get_tag(data);
Ming Leibf0beec2020-05-29 15:53:15 +0200392 if (tag == BLK_MQ_NO_TAG) {
393 if (data->flags & BLK_MQ_REQ_NOWAIT)
394 return NULL;
395
396 /*
397 * Give up the CPU and sleep for a random short time to ensure
398 * that thread using a realtime scheduling class are migrated
399 * off the the CPU, and thus off the hctx that is going away.
400 */
401 msleep(3);
402 goto retry;
403 }
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200404 return blk_mq_rq_ctx_init(data, tag, alloc_time_ns);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200405}
406
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700407struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800408 blk_mq_req_flags_t flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100409{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200410 struct blk_mq_alloc_data data = {
411 .q = q,
412 .flags = flags,
413 .cmd_flags = op,
414 };
Jens Axboebd166ef2017-01-17 06:03:22 -0700415 struct request *rq;
Joe Lawrencea492f072014-08-28 08:15:21 -0600416 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100417
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800418 ret = blk_queue_enter(q, flags);
Joe Lawrencea492f072014-08-28 08:15:21 -0600419 if (ret)
420 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100421
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200422 rq = __blk_mq_alloc_request(&data);
Jens Axboebd166ef2017-01-17 06:03:22 -0700423 if (!rq)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200424 goto out_queue_exit;
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200425 rq->__data_len = 0;
426 rq->__sector = (sector_t) -1;
427 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100428 return rq;
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200429out_queue_exit:
430 blk_queue_exit(q);
431 return ERR_PTR(-EWOULDBLOCK);
Jens Axboe320ae512013-10-24 09:20:05 +0100432}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600433EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100434
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700435struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800436 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
Ming Lin1f5bd332016-06-13 16:45:21 +0200437{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200438 struct blk_mq_alloc_data data = {
439 .q = q,
440 .flags = flags,
441 .cmd_flags = op,
442 };
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200443 u64 alloc_time_ns = 0;
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800444 unsigned int cpu;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200445 unsigned int tag;
Ming Lin1f5bd332016-06-13 16:45:21 +0200446 int ret;
447
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200448 /* alloc_time includes depth and tag waits */
449 if (blk_queue_rq_alloc_time(q))
450 alloc_time_ns = ktime_get_ns();
451
Ming Lin1f5bd332016-06-13 16:45:21 +0200452 /*
453 * If the tag allocator sleeps we could get an allocation for a
454 * different hardware context. No need to complicate the low level
455 * allocator for this for the rare use case of a command tied to
456 * a specific queue.
457 */
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200458 if (WARN_ON_ONCE(!(flags & (BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED))))
Ming Lin1f5bd332016-06-13 16:45:21 +0200459 return ERR_PTR(-EINVAL);
460
461 if (hctx_idx >= q->nr_hw_queues)
462 return ERR_PTR(-EIO);
463
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800464 ret = blk_queue_enter(q, flags);
Ming Lin1f5bd332016-06-13 16:45:21 +0200465 if (ret)
466 return ERR_PTR(ret);
467
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600468 /*
469 * Check if the hardware context is actually mapped to anything.
470 * If not tell the caller that it should skip this queue.
471 */
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200472 ret = -EXDEV;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200473 data.hctx = q->queue_hw_ctx[hctx_idx];
474 if (!blk_mq_hw_queue_mapped(data.hctx))
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200475 goto out_queue_exit;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200476 cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
477 data.ctx = __blk_mq_get_ctx(q, cpu);
Ming Lin1f5bd332016-06-13 16:45:21 +0200478
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200479 if (q->elevator)
480 data.flags |= BLK_MQ_REQ_INTERNAL;
481 else
482 blk_mq_tag_busy(data.hctx);
483
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200484 ret = -EWOULDBLOCK;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200485 tag = blk_mq_get_tag(&data);
486 if (tag == BLK_MQ_NO_TAG)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200487 goto out_queue_exit;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200488 return blk_mq_rq_ctx_init(&data, tag, alloc_time_ns);
489
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200490out_queue_exit:
491 blk_queue_exit(q);
492 return ERR_PTR(ret);
Ming Lin1f5bd332016-06-13 16:45:21 +0200493}
494EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
495
Keith Busch12f5b932018-05-29 15:52:28 +0200496static void __blk_mq_free_request(struct request *rq)
497{
498 struct request_queue *q = rq->q;
499 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600500 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Keith Busch12f5b932018-05-29 15:52:28 +0200501 const int sched_tag = rq->internal_tag;
502
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000503 blk_crypto_free_request(rq);
Bart Van Assche986d4132018-09-26 14:01:10 -0700504 blk_pm_mark_last_busy(rq);
Jens Axboeea4f9952018-10-29 15:06:13 -0600505 rq->mq_hctx = NULL;
Christoph Hellwig766473682020-05-29 15:53:12 +0200506 if (rq->tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800507 blk_mq_put_tag(hctx->tags, ctx, rq->tag);
Christoph Hellwig766473682020-05-29 15:53:12 +0200508 if (sched_tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800509 blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
Keith Busch12f5b932018-05-29 15:52:28 +0200510 blk_mq_sched_restart(hctx);
511 blk_queue_exit(q);
512}
513
Christoph Hellwig6af54052017-06-16 18:15:22 +0200514void blk_mq_free_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100515{
Jens Axboe320ae512013-10-24 09:20:05 +0100516 struct request_queue *q = rq->q;
Christoph Hellwig6af54052017-06-16 18:15:22 +0200517 struct elevator_queue *e = q->elevator;
518 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600519 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100520
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200521 if (rq->rq_flags & RQF_ELVPRIV) {
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600522 if (e && e->type->ops.finish_request)
523 e->type->ops.finish_request(rq);
Christoph Hellwig6af54052017-06-16 18:15:22 +0200524 if (rq->elv.icq) {
525 put_io_context(rq->elv.icq->ioc);
526 rq->elv.icq = NULL;
527 }
528 }
529
530 ctx->rq_completed[rq_is_sync(rq)]++;
Christoph Hellwige8064022016-10-20 15:12:13 +0200531 if (rq->rq_flags & RQF_MQ_INFLIGHT)
Jens Axboe0d2602c2014-05-13 15:10:52 -0600532 atomic_dec(&hctx->nr_active);
Jens Axboe87760e52016-11-09 12:38:14 -0700533
Jens Axboe7beb2f82017-09-30 02:08:24 -0600534 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
535 laptop_io_completion(q->backing_dev_info);
536
Josef Bacika7905042018-07-03 09:32:35 -0600537 rq_qos_done(q, rq);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600538
Keith Busch12f5b932018-05-29 15:52:28 +0200539 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
540 if (refcount_dec_and_test(&rq->ref))
541 __blk_mq_free_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100542}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700543EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100544
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200545inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
Jens Axboe320ae512013-10-24 09:20:05 +0100546{
Jens Axboefe1f4522018-11-28 10:50:07 -0700547 u64 now = 0;
548
549 if (blk_mq_need_time_stamp(rq))
550 now = ktime_get_ns();
Omar Sandoval522a7772018-05-09 02:08:53 -0700551
Omar Sandoval4bc63392018-05-09 02:08:52 -0700552 if (rq->rq_flags & RQF_STATS) {
553 blk_mq_poll_stats_start(rq->q);
Omar Sandoval522a7772018-05-09 02:08:53 -0700554 blk_stat_add(rq, now);
Omar Sandoval4bc63392018-05-09 02:08:52 -0700555 }
556
Christoph Hellwig766473682020-05-29 15:53:12 +0200557 if (rq->internal_tag != BLK_MQ_NO_TAG)
Omar Sandovaled886602018-09-27 15:55:51 -0700558 blk_mq_sched_completed_request(rq, now);
559
Omar Sandoval522a7772018-05-09 02:08:53 -0700560 blk_account_io_done(rq, now);
Ming Lei0d11e6a2013-12-05 10:50:39 -0700561
Christoph Hellwig91b63632014-04-16 09:44:53 +0200562 if (rq->end_io) {
Josef Bacika7905042018-07-03 09:32:35 -0600563 rq_qos_done(rq->q, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100564 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200565 } else {
Jens Axboe320ae512013-10-24 09:20:05 +0100566 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200567 }
Jens Axboe320ae512013-10-24 09:20:05 +0100568}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700569EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200570
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200571void blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200572{
573 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
574 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700575 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200576}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700577EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100578
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200579/*
580 * Softirq action handler - move entries to local list and loop over them
581 * while passing them to the queue registered handler.
582 */
583static __latent_entropy void blk_done_softirq(struct softirq_action *h)
584{
585 struct list_head *cpu_list, local_list;
586
587 local_irq_disable();
588 cpu_list = this_cpu_ptr(&blk_cpu_done);
589 list_replace_init(cpu_list, &local_list);
590 local_irq_enable();
591
592 while (!list_empty(&local_list)) {
593 struct request *rq;
594
595 rq = list_entry(local_list.next, struct request, ipi_list);
596 list_del_init(&rq->ipi_list);
597 rq->q->mq_ops->complete(rq);
598 }
599}
600
Christoph Hellwig115243f2020-06-11 08:44:42 +0200601static void blk_mq_trigger_softirq(struct request *rq)
602{
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200603 struct list_head *list;
604 unsigned long flags;
Christoph Hellwig115243f2020-06-11 08:44:42 +0200605
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200606 local_irq_save(flags);
607 list = this_cpu_ptr(&blk_cpu_done);
Christoph Hellwig115243f2020-06-11 08:44:42 +0200608 list_add_tail(&rq->ipi_list, list);
609
610 /*
611 * If the list only contains our just added request, signal a raise of
612 * the softirq. If there are already entries there, someone already
613 * raised the irq but it hasn't run yet.
614 */
615 if (list->next == &rq->ipi_list)
616 raise_softirq_irqoff(BLOCK_SOFTIRQ);
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200617 local_irq_restore(flags);
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200618}
619
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200620static int blk_softirq_cpu_dead(unsigned int cpu)
621{
622 /*
623 * If a CPU goes away, splice its entries to the current CPU
624 * and trigger a run of the softirq
625 */
626 local_irq_disable();
627 list_splice_init(&per_cpu(blk_cpu_done, cpu),
628 this_cpu_ptr(&blk_cpu_done));
629 raise_softirq_irqoff(BLOCK_SOFTIRQ);
630 local_irq_enable();
631
632 return 0;
633}
634
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200635static void __blk_mq_complete_request(struct request *rq)
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200636{
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200637 /*
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200638 * For most of single queue controllers, there is only one irq vector
639 * for handling I/O completion, and the only irq's affinity is set
640 * to all possible CPUs. On most of ARCHs, this affinity means the irq
641 * is handled on one specific CPU.
642 *
643 * So complete I/O requests in softirq context in case of single queue
644 * devices to avoid degrading I/O performance due to irqsoff latency.
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200645 */
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200646 if (rq->q->nr_hw_queues == 1)
647 blk_mq_trigger_softirq(rq);
648 else
649 rq->q->mq_ops->complete(rq);
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200650}
651
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800652static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100653{
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200654 __blk_mq_complete_request(data);
Jens Axboe320ae512013-10-24 09:20:05 +0100655}
656
Christoph Hellwig963395262020-06-11 08:44:49 +0200657static inline bool blk_mq_complete_need_ipi(struct request *rq)
658{
659 int cpu = raw_smp_processor_id();
660
661 if (!IS_ENABLED(CONFIG_SMP) ||
662 !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags))
663 return false;
664
665 /* same CPU or cache domain? Complete locally */
666 if (cpu == rq->mq_ctx->cpu ||
667 (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) &&
668 cpus_share_cache(cpu, rq->mq_ctx->cpu)))
669 return false;
670
671 /* don't try to IPI to an offline CPU */
672 return cpu_online(rq->mq_ctx->cpu);
673}
674
Keith Busch7b11eab2020-05-29 07:51:59 -0700675/**
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200676 * blk_mq_complete_request - end I/O on a request
677 * @rq: the request being processed
Keith Busch7b11eab2020-05-29 07:51:59 -0700678 *
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200679 * Description:
680 * Complete a request by scheduling the ->complete_rq operation.
681 **/
682void blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100683{
Keith Buschaf78ff72018-11-26 09:54:30 -0700684 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
Christoph Hellwig6aab1da2020-06-11 08:44:44 +0200685
686 /*
687 * For a polled request, always complete locallly, it's pointless
688 * to redirect the completion.
689 */
690 if (rq->cmd_flags & REQ_HIPRI) {
Christoph Hellwig963395262020-06-11 08:44:49 +0200691 rq->q->mq_ops->complete(rq);
Christoph Hellwig6aab1da2020-06-11 08:44:44 +0200692 return;
693 }
694
Christoph Hellwig963395262020-06-11 08:44:49 +0200695 if (blk_mq_complete_need_ipi(rq)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800696 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800697 rq->csd.info = rq;
698 rq->csd.flags = 0;
Christoph Hellwig963395262020-06-11 08:44:49 +0200699 smp_call_function_single_async(rq->mq_ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800700 } else {
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200701 __blk_mq_complete_request(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800702 }
Jens Axboe320ae512013-10-24 09:20:05 +0100703}
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200704EXPORT_SYMBOL(blk_mq_complete_request);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800705
Jens Axboe04ced152018-01-09 08:29:46 -0800706static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800707 __releases(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800708{
709 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
710 rcu_read_unlock();
711 else
Tejun Heo05707b62018-01-09 08:29:53 -0800712 srcu_read_unlock(hctx->srcu, srcu_idx);
Jens Axboe04ced152018-01-09 08:29:46 -0800713}
714
715static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800716 __acquires(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800717{
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700718 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
719 /* shut up gcc false positive */
720 *srcu_idx = 0;
Jens Axboe04ced152018-01-09 08:29:46 -0800721 rcu_read_lock();
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700722 } else
Tejun Heo05707b62018-01-09 08:29:53 -0800723 *srcu_idx = srcu_read_lock(hctx->srcu);
Jens Axboe04ced152018-01-09 08:29:46 -0800724}
725
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800726/**
André Almeida105663f2020-01-06 15:08:18 -0300727 * blk_mq_start_request - Start processing a request
728 * @rq: Pointer to request to be started
729 *
730 * Function used by device drivers to notify the block layer that a request
731 * is going to be processed now, so blk layer can do proper initializations
732 * such as starting the timeout timer.
733 */
Christoph Hellwige2490072014-09-13 16:40:09 -0700734void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100735{
736 struct request_queue *q = rq->q;
737
738 trace_block_rq_issue(q, rq);
739
Jens Axboecf43e6b2016-11-07 21:32:37 -0700740 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700741 rq->io_start_time_ns = ktime_get_ns();
Hou Tao3d244302019-05-21 15:59:03 +0800742 rq->stats_sectors = blk_rq_sectors(rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700743 rq->rq_flags |= RQF_STATS;
Josef Bacika7905042018-07-03 09:32:35 -0600744 rq_qos_issue(q, rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700745 }
746
Tejun Heo1d9bd512018-01-09 08:29:48 -0800747 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
Jens Axboe538b7532014-09-16 10:37:37 -0600748
Tejun Heo1d9bd512018-01-09 08:29:48 -0800749 blk_add_timer(rq);
Keith Busch12f5b932018-05-29 15:52:28 +0200750 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800751
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +0300752#ifdef CONFIG_BLK_DEV_INTEGRITY
753 if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
754 q->integrity.profile->prepare_fn(rq);
755#endif
Jens Axboe320ae512013-10-24 09:20:05 +0100756}
Christoph Hellwige2490072014-09-13 16:40:09 -0700757EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100758
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200759static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100760{
761 struct request_queue *q = rq->q;
762
Ming Lei923218f2017-11-02 23:24:38 +0800763 blk_mq_put_driver_tag(rq);
764
Jens Axboe320ae512013-10-24 09:20:05 +0100765 trace_block_rq_requeue(q, rq);
Josef Bacika7905042018-07-03 09:32:35 -0600766 rq_qos_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800767
Keith Busch12f5b932018-05-29 15:52:28 +0200768 if (blk_mq_request_started(rq)) {
769 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Christoph Hellwigda661262018-06-14 13:58:45 +0200770 rq->rq_flags &= ~RQF_TIMED_OUT;
Christoph Hellwige2490072014-09-13 16:40:09 -0700771 }
Jens Axboe320ae512013-10-24 09:20:05 +0100772}
773
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700774void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200775{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200776 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200777
Ming Lei105976f2018-02-23 23:36:56 +0800778 /* this request will be re-inserted to io scheduler queue */
779 blk_mq_sched_requeue_request(rq);
780
Jens Axboe7d692332018-10-24 10:48:12 -0600781 BUG_ON(!list_empty(&rq->queuelist));
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700782 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200783}
784EXPORT_SYMBOL(blk_mq_requeue_request);
785
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600786static void blk_mq_requeue_work(struct work_struct *work)
787{
788 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -0400789 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600790 LIST_HEAD(rq_list);
791 struct request *rq, *next;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600792
Jens Axboe18e97812017-07-27 08:03:57 -0600793 spin_lock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600794 list_splice_init(&q->requeue_list, &rq_list);
Jens Axboe18e97812017-07-27 08:03:57 -0600795 spin_unlock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600796
797 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
Jianchao Wangaef18972019-02-12 09:56:25 +0800798 if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP)))
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600799 continue;
800
Christoph Hellwige8064022016-10-20 15:12:13 +0200801 rq->rq_flags &= ~RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600802 list_del_init(&rq->queuelist);
Jianchao Wangaef18972019-02-12 09:56:25 +0800803 /*
804 * If RQF_DONTPREP, rq has contained some driver specific
805 * data, so insert it to hctx dispatch list to avoid any
806 * merge.
807 */
808 if (rq->rq_flags & RQF_DONTPREP)
Ming Lei01e99ae2020-02-25 09:04:32 +0800809 blk_mq_request_bypass_insert(rq, false, false);
Jianchao Wangaef18972019-02-12 09:56:25 +0800810 else
811 blk_mq_sched_insert_request(rq, true, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600812 }
813
814 while (!list_empty(&rq_list)) {
815 rq = list_entry(rq_list.next, struct request, queuelist);
816 list_del_init(&rq->queuelist);
Mike Snitzer9e97d292018-01-17 11:25:58 -0500817 blk_mq_sched_insert_request(rq, false, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600818 }
819
Bart Van Assche52d7f1b2016-10-28 17:20:32 -0700820 blk_mq_run_hw_queues(q, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600821}
822
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700823void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
824 bool kick_requeue_list)
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600825{
826 struct request_queue *q = rq->q;
827 unsigned long flags;
828
829 /*
830 * We abuse this flag that is otherwise used by the I/O scheduler to
Jens Axboeff821d22017-11-10 22:05:12 -0700831 * request head insertion from the workqueue.
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600832 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200833 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600834
835 spin_lock_irqsave(&q->requeue_lock, flags);
836 if (at_head) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200837 rq->rq_flags |= RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600838 list_add(&rq->queuelist, &q->requeue_list);
839 } else {
840 list_add_tail(&rq->queuelist, &q->requeue_list);
841 }
842 spin_unlock_irqrestore(&q->requeue_lock, flags);
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700843
844 if (kick_requeue_list)
845 blk_mq_kick_requeue_list(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600846}
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600847
848void blk_mq_kick_requeue_list(struct request_queue *q)
849{
Bart Van Asscheae943d22018-01-19 08:58:55 -0800850 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600851}
852EXPORT_SYMBOL(blk_mq_kick_requeue_list);
853
Mike Snitzer28494502016-09-14 13:28:30 -0400854void blk_mq_delay_kick_requeue_list(struct request_queue *q,
855 unsigned long msecs)
856{
Bart Van Assched4acf362017-08-09 11:28:06 -0700857 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
858 msecs_to_jiffies(msecs));
Mike Snitzer28494502016-09-14 13:28:30 -0400859}
860EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
861
Jens Axboe0e62f512014-06-04 10:23:49 -0600862struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
863{
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600864 if (tag < tags->nr_tags) {
865 prefetch(tags->rqs[tag]);
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700866 return tags->rqs[tag];
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600867 }
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700868
869 return NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600870}
871EXPORT_SYMBOL(blk_mq_tag_to_rq);
872
Jens Axboe3c94d832018-12-17 21:11:17 -0700873static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq,
874 void *priv, bool reserved)
Jens Axboeae879912018-11-08 09:03:51 -0700875{
876 /*
Jens Axboe3c94d832018-12-17 21:11:17 -0700877 * If we find a request that is inflight and the queue matches,
878 * we know the queue is busy. Return false to stop the iteration.
Jens Axboeae879912018-11-08 09:03:51 -0700879 */
Jens Axboe3c94d832018-12-17 21:11:17 -0700880 if (rq->state == MQ_RQ_IN_FLIGHT && rq->q == hctx->queue) {
Jens Axboeae879912018-11-08 09:03:51 -0700881 bool *busy = priv;
882
883 *busy = true;
884 return false;
885 }
886
887 return true;
888}
889
Jens Axboe3c94d832018-12-17 21:11:17 -0700890bool blk_mq_queue_inflight(struct request_queue *q)
Jens Axboeae879912018-11-08 09:03:51 -0700891{
892 bool busy = false;
893
Jens Axboe3c94d832018-12-17 21:11:17 -0700894 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
Jens Axboeae879912018-11-08 09:03:51 -0700895 return busy;
896}
Jens Axboe3c94d832018-12-17 21:11:17 -0700897EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
Jens Axboeae879912018-11-08 09:03:51 -0700898
Tejun Heo358f70d2018-01-09 08:29:50 -0800899static void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100900{
Christoph Hellwigda661262018-06-14 13:58:45 +0200901 req->rq_flags |= RQF_TIMED_OUT;
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200902 if (req->q->mq_ops->timeout) {
903 enum blk_eh_timer_return ret;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600904
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200905 ret = req->q->mq_ops->timeout(req, reserved);
906 if (ret == BLK_EH_DONE)
907 return;
908 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700909 }
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200910
911 blk_add_timer(req);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600912}
Keith Busch5b3f25f2015-01-07 18:55:46 -0700913
Keith Busch12f5b932018-05-29 15:52:28 +0200914static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
915{
916 unsigned long deadline;
917
918 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
919 return false;
Christoph Hellwigda661262018-06-14 13:58:45 +0200920 if (rq->rq_flags & RQF_TIMED_OUT)
921 return false;
Keith Busch12f5b932018-05-29 15:52:28 +0200922
Christoph Hellwig079076b2018-11-14 17:02:05 +0100923 deadline = READ_ONCE(rq->deadline);
Keith Busch12f5b932018-05-29 15:52:28 +0200924 if (time_after_eq(jiffies, deadline))
925 return true;
926
927 if (*next == 0)
928 *next = deadline;
929 else if (time_after(*next, deadline))
930 *next = deadline;
931 return false;
932}
933
Jens Axboe7baa8572018-11-08 10:24:07 -0700934static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700935 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100936{
Keith Busch12f5b932018-05-29 15:52:28 +0200937 unsigned long *next = priv;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700938
Keith Busch12f5b932018-05-29 15:52:28 +0200939 /*
940 * Just do a quick check if it is expired before locking the request in
941 * so we're not unnecessarilly synchronizing across CPUs.
942 */
943 if (!blk_mq_req_expired(rq, next))
Jens Axboe7baa8572018-11-08 10:24:07 -0700944 return true;
Jens Axboe320ae512013-10-24 09:20:05 +0100945
Tejun Heo1d9bd512018-01-09 08:29:48 -0800946 /*
Keith Busch12f5b932018-05-29 15:52:28 +0200947 * We have reason to believe the request may be expired. Take a
948 * reference on the request to lock this request lifetime into its
949 * currently allocated context to prevent it from being reallocated in
950 * the event the completion by-passes this timeout handler.
951 *
952 * If the reference was already released, then the driver beat the
953 * timeout handler to posting a natural completion.
Tejun Heo1d9bd512018-01-09 08:29:48 -0800954 */
Keith Busch12f5b932018-05-29 15:52:28 +0200955 if (!refcount_inc_not_zero(&rq->ref))
Jens Axboe7baa8572018-11-08 10:24:07 -0700956 return true;
Keith Busch12f5b932018-05-29 15:52:28 +0200957
958 /*
959 * The request is now locked and cannot be reallocated underneath the
960 * timeout handler's processing. Re-verify this exact request is truly
961 * expired; if it is not expired, then the request was completed and
962 * reallocated as a new request.
963 */
964 if (blk_mq_req_expired(rq, next))
Tejun Heo1d9bd512018-01-09 08:29:48 -0800965 blk_mq_rq_timed_out(rq, reserved);
Yufen Yu8d699662019-09-27 16:19:55 +0800966
967 if (is_flush_rq(rq, hctx))
968 rq->end_io(rq, 0);
969 else if (refcount_dec_and_test(&rq->ref))
Keith Busch12f5b932018-05-29 15:52:28 +0200970 __blk_mq_free_request(rq);
Jens Axboe7baa8572018-11-08 10:24:07 -0700971
972 return true;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800973}
974
Christoph Hellwig287922e2015-10-30 20:57:30 +0800975static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100976{
Christoph Hellwig287922e2015-10-30 20:57:30 +0800977 struct request_queue *q =
978 container_of(work, struct request_queue, timeout_work);
Keith Busch12f5b932018-05-29 15:52:28 +0200979 unsigned long next = 0;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800980 struct blk_mq_hw_ctx *hctx;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700981 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100982
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600983 /* A deadlock might occur if a request is stuck requiring a
984 * timeout at the same time a queue freeze is waiting
985 * completion, since the timeout code would not be able to
986 * acquire the queue reference here.
987 *
988 * That's why we don't use blk_queue_enter here; instead, we use
989 * percpu_ref_tryget directly, because we need to be able to
990 * obtain a reference even in the short window between the queue
991 * starting to freeze, by dropping the first reference in
Ming Lei1671d522017-03-27 20:06:57 +0800992 * blk_freeze_queue_start, and the moment the last request is
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600993 * consumed, marked by the instant q_usage_counter reaches
994 * zero.
995 */
996 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +0800997 return;
998
Keith Busch12f5b932018-05-29 15:52:28 +0200999 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
Jens Axboe320ae512013-10-24 09:20:05 +01001000
Keith Busch12f5b932018-05-29 15:52:28 +02001001 if (next != 0) {
1002 mod_timer(&q->timeout, next);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001003 } else {
Bart Van Asschefcd36c32018-01-10 08:33:33 -08001004 /*
1005 * Request timeouts are handled as a forward rolling timer. If
1006 * we end up here it means that no requests are pending and
1007 * also that no request has been pending for a while. Mark
1008 * each hctx as idle.
1009 */
Ming Leif054b562015-04-21 10:00:19 +08001010 queue_for_each_hw_ctx(q, hctx, i) {
1011 /* the hctx may be unmapped, so check it here */
1012 if (blk_mq_hw_queue_mapped(hctx))
1013 blk_mq_tag_idle(hctx);
1014 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06001015 }
Christoph Hellwig287922e2015-10-30 20:57:30 +08001016 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001017}
1018
Omar Sandoval88459642016-09-17 08:38:44 -06001019struct flush_busy_ctx_data {
1020 struct blk_mq_hw_ctx *hctx;
1021 struct list_head *list;
1022};
1023
1024static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
1025{
1026 struct flush_busy_ctx_data *flush_data = data;
1027 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
1028 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001029 enum hctx_type type = hctx->type;
Omar Sandoval88459642016-09-17 08:38:44 -06001030
Omar Sandoval88459642016-09-17 08:38:44 -06001031 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001032 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
Omar Sandovale9a99a62018-02-27 16:56:42 -08001033 sbitmap_clear_bit(sb, bitnr);
Omar Sandoval88459642016-09-17 08:38:44 -06001034 spin_unlock(&ctx->lock);
1035 return true;
1036}
1037
Jens Axboe320ae512013-10-24 09:20:05 +01001038/*
Jens Axboe1429d7c2014-05-19 09:23:55 -06001039 * Process software queues that have been marked busy, splicing them
1040 * to the for-dispatch
1041 */
Jens Axboe2c3ad662016-12-14 14:34:47 -07001042void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
Jens Axboe1429d7c2014-05-19 09:23:55 -06001043{
Omar Sandoval88459642016-09-17 08:38:44 -06001044 struct flush_busy_ctx_data data = {
1045 .hctx = hctx,
1046 .list = list,
1047 };
Jens Axboe1429d7c2014-05-19 09:23:55 -06001048
Omar Sandoval88459642016-09-17 08:38:44 -06001049 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001050}
Jens Axboe2c3ad662016-12-14 14:34:47 -07001051EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001052
Ming Leib3476892017-10-14 17:22:30 +08001053struct dispatch_rq_data {
1054 struct blk_mq_hw_ctx *hctx;
1055 struct request *rq;
1056};
1057
1058static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
1059 void *data)
1060{
1061 struct dispatch_rq_data *dispatch_data = data;
1062 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
1063 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001064 enum hctx_type type = hctx->type;
Ming Leib3476892017-10-14 17:22:30 +08001065
1066 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001067 if (!list_empty(&ctx->rq_lists[type])) {
1068 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
Ming Leib3476892017-10-14 17:22:30 +08001069 list_del_init(&dispatch_data->rq->queuelist);
Ming Leic16d6b52018-12-17 08:44:05 -07001070 if (list_empty(&ctx->rq_lists[type]))
Ming Leib3476892017-10-14 17:22:30 +08001071 sbitmap_clear_bit(sb, bitnr);
1072 }
1073 spin_unlock(&ctx->lock);
1074
1075 return !dispatch_data->rq;
1076}
1077
1078struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
1079 struct blk_mq_ctx *start)
1080{
Jens Axboef31967f2018-10-29 13:13:29 -06001081 unsigned off = start ? start->index_hw[hctx->type] : 0;
Ming Leib3476892017-10-14 17:22:30 +08001082 struct dispatch_rq_data data = {
1083 .hctx = hctx,
1084 .rq = NULL,
1085 };
1086
1087 __sbitmap_for_each_set(&hctx->ctx_map, off,
1088 dispatch_rq_from_ctx, &data);
1089
1090 return data.rq;
1091}
1092
Jens Axboe703fd1c2016-09-16 13:59:14 -06001093static inline unsigned int queued_to_index(unsigned int queued)
1094{
1095 if (!queued)
1096 return 0;
Jens Axboe1429d7c2014-05-19 09:23:55 -06001097
Jens Axboe703fd1c2016-09-16 13:59:14 -06001098 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001099}
1100
Jens Axboeeb619fd2017-11-09 08:32:43 -07001101static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1102 int flags, void *key)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001103{
1104 struct blk_mq_hw_ctx *hctx;
1105
1106 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1107
Ming Lei5815839b2018-06-25 19:31:47 +08001108 spin_lock(&hctx->dispatch_wait_lock);
Jens Axboee8618572019-03-25 12:34:10 -06001109 if (!list_empty(&wait->entry)) {
1110 struct sbitmap_queue *sbq;
1111
1112 list_del_init(&wait->entry);
1113 sbq = &hctx->tags->bitmap_tags;
1114 atomic_dec(&sbq->ws_active);
1115 }
Ming Lei5815839b2018-06-25 19:31:47 +08001116 spin_unlock(&hctx->dispatch_wait_lock);
1117
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001118 blk_mq_run_hw_queue(hctx, true);
1119 return 1;
1120}
1121
Jens Axboef906a6a2017-11-09 16:10:13 -07001122/*
1123 * Mark us waiting for a tag. For shared tags, this involves hooking us into
Bart Van Asscheee3e4de2018-01-09 10:09:15 -08001124 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1125 * restart. For both cases, take care to check the condition again after
Jens Axboef906a6a2017-11-09 16:10:13 -07001126 * marking us as waiting.
1127 */
Ming Lei2278d692018-06-25 19:31:46 +08001128static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
Jens Axboef906a6a2017-11-09 16:10:13 -07001129 struct request *rq)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001130{
Jens Axboee8618572019-03-25 12:34:10 -06001131 struct sbitmap_queue *sbq = &hctx->tags->bitmap_tags;
Ming Lei5815839b2018-06-25 19:31:47 +08001132 struct wait_queue_head *wq;
Jens Axboef906a6a2017-11-09 16:10:13 -07001133 wait_queue_entry_t *wait;
1134 bool ret;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001135
Ming Lei2278d692018-06-25 19:31:46 +08001136 if (!(hctx->flags & BLK_MQ_F_TAG_SHARED)) {
Yufen Yu684b7322019-03-15 11:05:10 +08001137 blk_mq_sched_mark_restart_hctx(hctx);
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001138
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001139 /*
1140 * It's possible that a tag was freed in the window between the
1141 * allocation failure and adding the hardware queue to the wait
1142 * queue.
1143 *
1144 * Don't clear RESTART here, someone else could have set it.
1145 * At most this will cost an extra queue run.
1146 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001147 return blk_mq_get_driver_tag(rq);
Jens Axboeeb619fd2017-11-09 08:32:43 -07001148 }
1149
Ming Lei2278d692018-06-25 19:31:46 +08001150 wait = &hctx->dispatch_wait;
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001151 if (!list_empty_careful(&wait->entry))
1152 return false;
1153
Jens Axboee8618572019-03-25 12:34:10 -06001154 wq = &bt_wait_ptr(sbq, hctx)->wait;
Ming Lei5815839b2018-06-25 19:31:47 +08001155
1156 spin_lock_irq(&wq->lock);
1157 spin_lock(&hctx->dispatch_wait_lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001158 if (!list_empty(&wait->entry)) {
Ming Lei5815839b2018-06-25 19:31:47 +08001159 spin_unlock(&hctx->dispatch_wait_lock);
1160 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001161 return false;
1162 }
1163
Jens Axboee8618572019-03-25 12:34:10 -06001164 atomic_inc(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001165 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1166 __add_wait_queue(wq, wait);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001167
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001168 /*
Jens Axboeeb619fd2017-11-09 08:32:43 -07001169 * It's possible that a tag was freed in the window between the
1170 * allocation failure and adding the hardware queue to the wait
1171 * queue.
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001172 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001173 ret = blk_mq_get_driver_tag(rq);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001174 if (!ret) {
Ming Lei5815839b2018-06-25 19:31:47 +08001175 spin_unlock(&hctx->dispatch_wait_lock);
1176 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001177 return false;
Jens Axboef906a6a2017-11-09 16:10:13 -07001178 }
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001179
1180 /*
1181 * We got a tag, remove ourselves from the wait queue to ensure
1182 * someone else gets the wakeup.
1183 */
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001184 list_del_init(&wait->entry);
Jens Axboee8618572019-03-25 12:34:10 -06001185 atomic_dec(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001186 spin_unlock(&hctx->dispatch_wait_lock);
1187 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001188
1189 return true;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001190}
1191
Ming Lei6e7687172018-07-03 09:03:16 -06001192#define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1193#define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1194/*
1195 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1196 * - EWMA is one simple way to compute running average value
1197 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1198 * - take 4 as factor for avoiding to get too small(0) result, and this
1199 * factor doesn't matter because EWMA decreases exponentially
1200 */
1201static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1202{
1203 unsigned int ewma;
1204
1205 if (hctx->queue->elevator)
1206 return;
1207
1208 ewma = hctx->dispatch_busy;
1209
1210 if (!ewma && !busy)
1211 return;
1212
1213 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1214 if (busy)
1215 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1216 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1217
1218 hctx->dispatch_busy = ewma;
1219}
1220
Ming Lei86ff7c22018-01-30 22:04:57 -05001221#define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1222
Johannes Thumshirnc92a4102020-03-25 00:24:44 +09001223static void blk_mq_handle_dev_resource(struct request *rq,
1224 struct list_head *list)
1225{
1226 struct request *next =
1227 list_first_entry_or_null(list, struct request, queuelist);
1228
1229 /*
1230 * If an I/O scheduler has been configured and we got a driver tag for
1231 * the next request already, free it.
1232 */
1233 if (next)
1234 blk_mq_put_driver_tag(next);
1235
1236 list_add(&rq->queuelist, list);
1237 __blk_mq_requeue_request(rq);
1238}
1239
Keith Busch0512a752020-05-12 17:55:47 +09001240static void blk_mq_handle_zone_resource(struct request *rq,
1241 struct list_head *zone_list)
1242{
1243 /*
1244 * If we end up here it is because we cannot dispatch a request to a
1245 * specific zone due to LLD level zone-write locking or other zone
1246 * related resource not being available. In this case, set the request
1247 * aside in zone_list for retrying it later.
1248 */
1249 list_add(&rq->queuelist, zone_list);
1250 __blk_mq_requeue_request(rq);
1251}
1252
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001253/*
1254 * Returns true if we did some work AND can potentially do more.
1255 */
Ming Leide148292017-10-14 17:22:29 +08001256bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list,
Jens Axboeeb619fd2017-11-09 08:32:43 -07001257 bool got_budget)
Jens Axboef04c3df2016-12-07 08:41:17 -07001258{
Omar Sandoval81380ca2017-04-07 08:56:26 -06001259 struct blk_mq_hw_ctx *hctx;
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001260 struct request *rq, *nxt;
Jens Axboeeb619fd2017-11-09 08:32:43 -07001261 bool no_tag = false;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001262 int errors, queued;
Ming Lei86ff7c22018-01-30 22:04:57 -05001263 blk_status_t ret = BLK_STS_OK;
Douglas Andersonab3cee32020-04-20 09:24:51 -07001264 bool no_budget_avail = false;
Keith Busch0512a752020-05-12 17:55:47 +09001265 LIST_HEAD(zone_list);
Jens Axboef04c3df2016-12-07 08:41:17 -07001266
Omar Sandoval81380ca2017-04-07 08:56:26 -06001267 if (list_empty(list))
1268 return false;
1269
Ming Leide148292017-10-14 17:22:29 +08001270 WARN_ON(!list_is_singular(list) && got_budget);
1271
Jens Axboef04c3df2016-12-07 08:41:17 -07001272 /*
Jens Axboef04c3df2016-12-07 08:41:17 -07001273 * Now process all the entries, sending them to the driver.
1274 */
Jens Axboe93efe982017-03-24 12:04:19 -06001275 errors = queued = 0;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001276 do {
Jens Axboef04c3df2016-12-07 08:41:17 -07001277 struct blk_mq_queue_data bd;
1278
1279 rq = list_first_entry(list, struct request, queuelist);
Ming Lei0bca7992018-04-05 00:35:21 +08001280
Jens Axboeea4f9952018-10-29 15:06:13 -06001281 hctx = rq->mq_hctx;
John Garry5fe56de2020-04-16 19:18:51 +08001282 if (!got_budget && !blk_mq_get_dispatch_budget(hctx)) {
1283 blk_mq_put_driver_tag(rq);
Douglas Andersonab3cee32020-04-20 09:24:51 -07001284 no_budget_avail = true;
Ming Lei0bca7992018-04-05 00:35:21 +08001285 break;
John Garry5fe56de2020-04-16 19:18:51 +08001286 }
Ming Lei0bca7992018-04-05 00:35:21 +08001287
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001288 if (!blk_mq_get_driver_tag(rq)) {
Jens Axboe3c782d62017-01-26 12:50:36 -07001289 /*
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001290 * The initial allocation attempt failed, so we need to
Jens Axboeeb619fd2017-11-09 08:32:43 -07001291 * rerun the hardware queue when a tag is freed. The
1292 * waitqueue takes care of that. If the queue is run
1293 * before we add this entry back on the dispatch list,
1294 * we'll re-run it below.
Jens Axboe3c782d62017-01-26 12:50:36 -07001295 */
Ming Lei2278d692018-06-25 19:31:46 +08001296 if (!blk_mq_mark_tag_wait(hctx, rq)) {
Ming Lei0bca7992018-04-05 00:35:21 +08001297 blk_mq_put_dispatch_budget(hctx);
Jens Axboef906a6a2017-11-09 16:10:13 -07001298 /*
1299 * For non-shared tags, the RESTART check
1300 * will suffice.
1301 */
1302 if (hctx->flags & BLK_MQ_F_TAG_SHARED)
1303 no_tag = true;
Omar Sandoval807b1042017-04-05 12:01:35 -07001304 break;
Ming Leide148292017-10-14 17:22:29 +08001305 }
1306 }
1307
Jens Axboef04c3df2016-12-07 08:41:17 -07001308 list_del_init(&rq->queuelist);
1309
1310 bd.rq = rq;
Jens Axboe113285b2017-03-02 13:26:04 -07001311
1312 /*
1313 * Flag last if we have no more requests, or if we have more
1314 * but can't assign a driver tag to it.
1315 */
1316 if (list_empty(list))
1317 bd.last = true;
1318 else {
Jens Axboe113285b2017-03-02 13:26:04 -07001319 nxt = list_first_entry(list, struct request, queuelist);
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001320 bd.last = !blk_mq_get_driver_tag(nxt);
Jens Axboe113285b2017-03-02 13:26:04 -07001321 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001322
1323 ret = q->mq_ops->queue_rq(hctx, &bd);
Ming Lei86ff7c22018-01-30 22:04:57 -05001324 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) {
Johannes Thumshirnc92a4102020-03-25 00:24:44 +09001325 blk_mq_handle_dev_resource(rq, list);
Jens Axboef04c3df2016-12-07 08:41:17 -07001326 break;
Keith Busch0512a752020-05-12 17:55:47 +09001327 } else if (ret == BLK_STS_ZONE_RESOURCE) {
1328 /*
1329 * Move the request to zone_list and keep going through
1330 * the dispatch list to find more requests the drive can
1331 * accept.
1332 */
1333 blk_mq_handle_zone_resource(rq, &zone_list);
1334 if (list_empty(list))
1335 break;
1336 continue;
Jens Axboef04c3df2016-12-07 08:41:17 -07001337 }
1338
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001339 if (unlikely(ret != BLK_STS_OK)) {
1340 errors++;
1341 blk_mq_end_request(rq, BLK_STS_IOERR);
1342 continue;
1343 }
1344
1345 queued++;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001346 } while (!list_empty(list));
Jens Axboef04c3df2016-12-07 08:41:17 -07001347
Keith Busch0512a752020-05-12 17:55:47 +09001348 if (!list_empty(&zone_list))
1349 list_splice_tail_init(&zone_list, list);
1350
Jens Axboef04c3df2016-12-07 08:41:17 -07001351 hctx->dispatched[queued_to_index(queued)]++;
1352
1353 /*
1354 * Any items that need requeuing? Stuff them into hctx->dispatch,
1355 * that is where we will continue on next queue run.
1356 */
1357 if (!list_empty(list)) {
Ming Lei86ff7c22018-01-30 22:04:57 -05001358 bool needs_restart;
1359
Jens Axboed666ba92018-11-27 17:02:25 -07001360 /*
1361 * If we didn't flush the entire list, we could have told
1362 * the driver there was more coming, but that turned out to
1363 * be a lie.
1364 */
Keith Busch536167d42020-04-07 03:13:48 +09001365 if (q->mq_ops->commit_rqs && queued)
Jens Axboed666ba92018-11-27 17:02:25 -07001366 q->mq_ops->commit_rqs(hctx);
1367
Jens Axboef04c3df2016-12-07 08:41:17 -07001368 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001369 list_splice_tail_init(list, &hctx->dispatch);
Jens Axboef04c3df2016-12-07 08:41:17 -07001370 spin_unlock(&hctx->lock);
1371
1372 /*
Bart Van Assche710c7852017-04-07 11:16:51 -07001373 * If SCHED_RESTART was set by the caller of this function and
1374 * it is no longer set that means that it was cleared by another
1375 * thread and hence that a queue rerun is needed.
Jens Axboef04c3df2016-12-07 08:41:17 -07001376 *
Jens Axboeeb619fd2017-11-09 08:32:43 -07001377 * If 'no_tag' is set, that means that we failed getting
1378 * a driver tag with an I/O scheduler attached. If our dispatch
1379 * waitqueue is no longer active, ensure that we run the queue
1380 * AFTER adding our entries back to the list.
Jens Axboebd166ef2017-01-17 06:03:22 -07001381 *
Bart Van Assche710c7852017-04-07 11:16:51 -07001382 * If no I/O scheduler has been configured it is possible that
1383 * the hardware queue got stopped and restarted before requests
1384 * were pushed back onto the dispatch list. Rerun the queue to
1385 * avoid starvation. Notes:
1386 * - blk_mq_run_hw_queue() checks whether or not a queue has
1387 * been stopped before rerunning a queue.
1388 * - Some but not all block drivers stop a queue before
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001389 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
Bart Van Assche710c7852017-04-07 11:16:51 -07001390 * and dm-rq.
Ming Lei86ff7c22018-01-30 22:04:57 -05001391 *
1392 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1393 * bit is set, run queue after a delay to avoid IO stalls
Douglas Andersonab3cee32020-04-20 09:24:51 -07001394 * that could otherwise occur if the queue is idle. We'll do
1395 * similar if we couldn't get budget and SCHED_RESTART is set.
Jens Axboebd166ef2017-01-17 06:03:22 -07001396 */
Ming Lei86ff7c22018-01-30 22:04:57 -05001397 needs_restart = blk_mq_sched_needs_restart(hctx);
1398 if (!needs_restart ||
Jens Axboeeb619fd2017-11-09 08:32:43 -07001399 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
Jens Axboebd166ef2017-01-17 06:03:22 -07001400 blk_mq_run_hw_queue(hctx, true);
Douglas Andersonab3cee32020-04-20 09:24:51 -07001401 else if (needs_restart && (ret == BLK_STS_RESOURCE ||
1402 no_budget_avail))
Ming Lei86ff7c22018-01-30 22:04:57 -05001403 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001404
Ming Lei6e7687172018-07-03 09:03:16 -06001405 blk_mq_update_dispatch_busy(hctx, true);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001406 return false;
Ming Lei6e7687172018-07-03 09:03:16 -06001407 } else
1408 blk_mq_update_dispatch_busy(hctx, false);
Jens Axboef04c3df2016-12-07 08:41:17 -07001409
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001410 /*
1411 * If the host/device is unable to accept more work, inform the
1412 * caller of that.
1413 */
1414 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
1415 return false;
1416
Jens Axboe93efe982017-03-24 12:04:19 -06001417 return (queued + errors) != 0;
Jens Axboef04c3df2016-12-07 08:41:17 -07001418}
1419
André Almeida105663f2020-01-06 15:08:18 -03001420/**
1421 * __blk_mq_run_hw_queue - Run a hardware queue.
1422 * @hctx: Pointer to the hardware queue to run.
1423 *
1424 * Send pending requests to the hardware.
1425 */
Bart Van Assche6a83e742016-11-02 10:09:51 -06001426static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1427{
1428 int srcu_idx;
1429
Jens Axboeb7a71e62017-08-01 09:28:24 -06001430 /*
1431 * We should be running this queue from one of the CPUs that
1432 * are mapped to it.
Ming Lei7df938f2018-01-18 00:41:52 +08001433 *
1434 * There are at least two related races now between setting
1435 * hctx->next_cpu from blk_mq_hctx_next_cpu() and running
1436 * __blk_mq_run_hw_queue():
1437 *
1438 * - hctx->next_cpu is found offline in blk_mq_hctx_next_cpu(),
1439 * but later it becomes online, then this warning is harmless
1440 * at all
1441 *
1442 * - hctx->next_cpu is found online in blk_mq_hctx_next_cpu(),
1443 * but later it becomes offline, then the warning can't be
1444 * triggered, and we depend on blk-mq timeout handler to
1445 * handle dispatched requests to this hctx
Jens Axboeb7a71e62017-08-01 09:28:24 -06001446 */
Ming Lei7df938f2018-01-18 00:41:52 +08001447 if (!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
1448 cpu_online(hctx->next_cpu)) {
1449 printk(KERN_WARNING "run queue from wrong CPU %d, hctx %s\n",
1450 raw_smp_processor_id(),
1451 cpumask_empty(hctx->cpumask) ? "inactive": "active");
1452 dump_stack();
1453 }
Bart Van Assche6a83e742016-11-02 10:09:51 -06001454
Jens Axboeb7a71e62017-08-01 09:28:24 -06001455 /*
1456 * We can't run the queue inline with ints disabled. Ensure that
1457 * we catch bad users of this early.
1458 */
1459 WARN_ON_ONCE(in_interrupt());
1460
Jens Axboe04ced152018-01-09 08:29:46 -08001461 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
Jens Axboebf4907c2017-03-30 12:30:39 -06001462
Jens Axboe04ced152018-01-09 08:29:46 -08001463 hctx_lock(hctx, &srcu_idx);
1464 blk_mq_sched_dispatch_requests(hctx);
1465 hctx_unlock(hctx, srcu_idx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001466}
1467
Ming Leif82ddf12018-04-08 17:48:10 +08001468static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1469{
1470 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1471
1472 if (cpu >= nr_cpu_ids)
1473 cpu = cpumask_first(hctx->cpumask);
1474 return cpu;
1475}
1476
Jens Axboe506e9312014-05-07 10:26:44 -06001477/*
1478 * It'd be great if the workqueue API had a way to pass
1479 * in a mask and had some smarts for more clever placement.
1480 * For now we just round-robin here, switching for every
1481 * BLK_MQ_CPU_WORK_BATCH queued items.
1482 */
1483static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1484{
Ming Lei7bed4592018-01-18 00:41:51 +08001485 bool tried = false;
Ming Lei476f8c92018-04-08 17:48:09 +08001486 int next_cpu = hctx->next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001487
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001488 if (hctx->queue->nr_hw_queues == 1)
1489 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -06001490
1491 if (--hctx->next_cpu_batch <= 0) {
Ming Lei7bed4592018-01-18 00:41:51 +08001492select_cpu:
Ming Lei476f8c92018-04-08 17:48:09 +08001493 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08001494 cpu_online_mask);
Jens Axboe506e9312014-05-07 10:26:44 -06001495 if (next_cpu >= nr_cpu_ids)
Ming Leif82ddf12018-04-08 17:48:10 +08001496 next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06001497 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1498 }
1499
Ming Lei7bed4592018-01-18 00:41:51 +08001500 /*
1501 * Do unbound schedule if we can't find a online CPU for this hctx,
1502 * and it should only happen in the path of handling CPU DEAD.
1503 */
Ming Lei476f8c92018-04-08 17:48:09 +08001504 if (!cpu_online(next_cpu)) {
Ming Lei7bed4592018-01-18 00:41:51 +08001505 if (!tried) {
1506 tried = true;
1507 goto select_cpu;
1508 }
1509
1510 /*
1511 * Make sure to re-select CPU next time once after CPUs
1512 * in hctx->cpumask become online again.
1513 */
Ming Lei476f8c92018-04-08 17:48:09 +08001514 hctx->next_cpu = next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001515 hctx->next_cpu_batch = 1;
1516 return WORK_CPU_UNBOUND;
1517 }
Ming Lei476f8c92018-04-08 17:48:09 +08001518
1519 hctx->next_cpu = next_cpu;
1520 return next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -06001521}
1522
André Almeida105663f2020-01-06 15:08:18 -03001523/**
1524 * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue.
1525 * @hctx: Pointer to the hardware queue to run.
1526 * @async: If we want to run the queue asynchronously.
1527 * @msecs: Microseconds of delay to wait before running the queue.
1528 *
1529 * If !@async, try to run the queue now. Else, run the queue asynchronously and
1530 * with a delay of @msecs.
1531 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001532static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1533 unsigned long msecs)
Jens Axboe320ae512013-10-24 09:20:05 +01001534{
Bart Van Assche5435c022017-06-20 11:15:49 -07001535 if (unlikely(blk_mq_hctx_stopped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +01001536 return;
1537
Jens Axboe1b792f22016-09-21 10:12:13 -06001538 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001539 int cpu = get_cpu();
1540 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +01001541 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001542 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +01001543 return;
1544 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001545
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001546 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -06001547 }
Paolo Bonzini398205b2014-11-07 23:03:59 +01001548
Bart Van Asscheae943d22018-01-19 08:58:55 -08001549 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1550 msecs_to_jiffies(msecs));
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001551}
1552
André Almeida105663f2020-01-06 15:08:18 -03001553/**
1554 * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
1555 * @hctx: Pointer to the hardware queue to run.
1556 * @msecs: Microseconds of delay to wait before running the queue.
1557 *
1558 * Run a hardware queue asynchronously with a delay of @msecs.
1559 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001560void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1561{
1562 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1563}
1564EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1565
André Almeida105663f2020-01-06 15:08:18 -03001566/**
1567 * blk_mq_run_hw_queue - Start to run a hardware queue.
1568 * @hctx: Pointer to the hardware queue to run.
1569 * @async: If we want to run the queue asynchronously.
1570 *
1571 * Check if the request queue is not in a quiesced state and if there are
1572 * pending requests to be sent. If this is true, run the queue to send requests
1573 * to hardware.
1574 */
John Garry626fb732019-10-30 00:59:30 +08001575void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001576{
Ming Lei24f5a902018-01-06 16:27:38 +08001577 int srcu_idx;
1578 bool need_run;
1579
1580 /*
1581 * When queue is quiesced, we may be switching io scheduler, or
1582 * updating nr_hw_queues, or other things, and we can't run queue
1583 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
1584 *
1585 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
1586 * quiesced.
1587 */
Jens Axboe04ced152018-01-09 08:29:46 -08001588 hctx_lock(hctx, &srcu_idx);
1589 need_run = !blk_queue_quiesced(hctx->queue) &&
1590 blk_mq_hctx_has_pending(hctx);
1591 hctx_unlock(hctx, srcu_idx);
Ming Lei24f5a902018-01-06 16:27:38 +08001592
John Garry626fb732019-10-30 00:59:30 +08001593 if (need_run)
Jens Axboe79f720a2017-11-10 09:13:21 -07001594 __blk_mq_delay_run_hw_queue(hctx, async, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01001595}
Omar Sandoval5b727272017-04-14 01:00:00 -07001596EXPORT_SYMBOL(blk_mq_run_hw_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01001597
André Almeida105663f2020-01-06 15:08:18 -03001598/**
1599 * blk_mq_run_hw_queue - Run all hardware queues in a request queue.
1600 * @q: Pointer to the request queue to run.
1601 * @async: If we want to run the queue asynchronously.
1602 */
Mike Snitzerb94ec292015-03-11 23:56:38 -04001603void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001604{
1605 struct blk_mq_hw_ctx *hctx;
1606 int i;
1607
1608 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe79f720a2017-11-10 09:13:21 -07001609 if (blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +01001610 continue;
1611
Mike Snitzerb94ec292015-03-11 23:56:38 -04001612 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001613 }
1614}
Mike Snitzerb94ec292015-03-11 23:56:38 -04001615EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001616
Bart Van Asschefd001442016-10-28 17:19:37 -07001617/**
Douglas Andersonb9151e72020-04-20 09:24:52 -07001618 * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously.
1619 * @q: Pointer to the request queue to run.
1620 * @msecs: Microseconds of delay to wait before running the queues.
1621 */
1622void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs)
1623{
1624 struct blk_mq_hw_ctx *hctx;
1625 int i;
1626
1627 queue_for_each_hw_ctx(q, hctx, i) {
1628 if (blk_mq_hctx_stopped(hctx))
1629 continue;
1630
1631 blk_mq_delay_run_hw_queue(hctx, msecs);
1632 }
1633}
1634EXPORT_SYMBOL(blk_mq_delay_run_hw_queues);
1635
1636/**
Bart Van Asschefd001442016-10-28 17:19:37 -07001637 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1638 * @q: request queue.
1639 *
1640 * The caller is responsible for serializing this function against
1641 * blk_mq_{start,stop}_hw_queue().
1642 */
1643bool blk_mq_queue_stopped(struct request_queue *q)
1644{
1645 struct blk_mq_hw_ctx *hctx;
1646 int i;
1647
1648 queue_for_each_hw_ctx(q, hctx, i)
1649 if (blk_mq_hctx_stopped(hctx))
1650 return true;
1651
1652 return false;
1653}
1654EXPORT_SYMBOL(blk_mq_queue_stopped);
1655
Ming Lei39a70c72017-06-06 23:22:09 +08001656/*
1657 * This function is often used for pausing .queue_rq() by driver when
1658 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001659 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001660 *
1661 * We do not guarantee that dispatch can be drained or blocked
1662 * after blk_mq_stop_hw_queue() returns. Please use
1663 * blk_mq_quiesce_queue() for that requirement.
1664 */
Jens Axboe320ae512013-10-24 09:20:05 +01001665void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1666{
Ming Lei641a9ed2017-06-06 23:22:10 +08001667 cancel_delayed_work(&hctx->run_work);
1668
1669 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboe320ae512013-10-24 09:20:05 +01001670}
1671EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1672
Ming Lei39a70c72017-06-06 23:22:09 +08001673/*
1674 * This function is often used for pausing .queue_rq() by driver when
1675 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001676 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001677 *
1678 * We do not guarantee that dispatch can be drained or blocked
1679 * after blk_mq_stop_hw_queues() returns. Please use
1680 * blk_mq_quiesce_queue() for that requirement.
1681 */
Jens Axboe2719aa22017-05-03 11:08:14 -06001682void blk_mq_stop_hw_queues(struct request_queue *q)
1683{
Ming Lei641a9ed2017-06-06 23:22:10 +08001684 struct blk_mq_hw_ctx *hctx;
1685 int i;
1686
1687 queue_for_each_hw_ctx(q, hctx, i)
1688 blk_mq_stop_hw_queue(hctx);
Christoph Hellwig280d45f2013-10-25 14:45:58 +01001689}
1690EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1691
Jens Axboe320ae512013-10-24 09:20:05 +01001692void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1693{
1694 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -06001695
Jens Axboe0ffbce82014-06-25 08:22:34 -06001696 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001697}
1698EXPORT_SYMBOL(blk_mq_start_hw_queue);
1699
Christoph Hellwig2f268552014-04-16 09:44:56 +02001700void blk_mq_start_hw_queues(struct request_queue *q)
1701{
1702 struct blk_mq_hw_ctx *hctx;
1703 int i;
1704
1705 queue_for_each_hw_ctx(q, hctx, i)
1706 blk_mq_start_hw_queue(hctx);
1707}
1708EXPORT_SYMBOL(blk_mq_start_hw_queues);
1709
Jens Axboeae911c52016-12-08 13:19:30 -07001710void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1711{
1712 if (!blk_mq_hctx_stopped(hctx))
1713 return;
1714
1715 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1716 blk_mq_run_hw_queue(hctx, async);
1717}
1718EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1719
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001720void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001721{
1722 struct blk_mq_hw_ctx *hctx;
1723 int i;
1724
Jens Axboeae911c52016-12-08 13:19:30 -07001725 queue_for_each_hw_ctx(q, hctx, i)
1726 blk_mq_start_stopped_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001727}
1728EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1729
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001730static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001731{
1732 struct blk_mq_hw_ctx *hctx;
1733
Jens Axboe9f993732017-04-10 09:54:54 -06001734 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboe21c6e932017-04-10 09:54:56 -06001735
1736 /*
Ming Lei15fe8a902018-04-08 17:48:11 +08001737 * If we are stopped, don't run the queue.
Jens Axboe21c6e932017-04-10 09:54:56 -06001738 */
Ming Lei15fe8a902018-04-08 17:48:11 +08001739 if (test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jianchao Wang0196d6b2018-06-04 17:03:55 +08001740 return;
Jens Axboee4043dc2014-04-09 10:18:23 -06001741
Jens Axboe320ae512013-10-24 09:20:05 +01001742 __blk_mq_run_hw_queue(hctx);
1743}
1744
Ming Leicfd0c552015-10-20 23:13:57 +08001745static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08001746 struct request *rq,
1747 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01001748{
Jens Axboee57690f2016-08-24 15:34:35 -06001749 struct blk_mq_ctx *ctx = rq->mq_ctx;
Ming Leic16d6b52018-12-17 08:44:05 -07001750 enum hctx_type type = hctx->type;
Jens Axboee57690f2016-08-24 15:34:35 -06001751
Bart Van Assche7b607812017-06-20 11:15:47 -07001752 lockdep_assert_held(&ctx->lock);
1753
Jens Axboe01b983c2013-11-19 18:59:10 -07001754 trace_block_rq_insert(hctx->queue, rq);
1755
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001756 if (at_head)
Ming Leic16d6b52018-12-17 08:44:05 -07001757 list_add(&rq->queuelist, &ctx->rq_lists[type]);
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001758 else
Ming Leic16d6b52018-12-17 08:44:05 -07001759 list_add_tail(&rq->queuelist, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08001760}
Jens Axboe4bb659b2014-05-09 09:36:49 -06001761
Jens Axboe2c3ad662016-12-14 14:34:47 -07001762void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1763 bool at_head)
Ming Leicfd0c552015-10-20 23:13:57 +08001764{
1765 struct blk_mq_ctx *ctx = rq->mq_ctx;
1766
Bart Van Assche7b607812017-06-20 11:15:47 -07001767 lockdep_assert_held(&ctx->lock);
1768
Jens Axboee57690f2016-08-24 15:34:35 -06001769 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01001770 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001771}
1772
André Almeida105663f2020-01-06 15:08:18 -03001773/**
1774 * blk_mq_request_bypass_insert - Insert a request at dispatch list.
1775 * @rq: Pointer to request to be inserted.
1776 * @run_queue: If we should run the hardware queue after inserting the request.
1777 *
Jens Axboe157f3772017-09-11 16:43:57 -06001778 * Should only be used carefully, when the caller knows we want to
1779 * bypass a potential IO scheduler on the target device.
1780 */
Ming Lei01e99ae2020-02-25 09:04:32 +08001781void blk_mq_request_bypass_insert(struct request *rq, bool at_head,
1782 bool run_queue)
Jens Axboe157f3772017-09-11 16:43:57 -06001783{
Jens Axboeea4f9952018-10-29 15:06:13 -06001784 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe157f3772017-09-11 16:43:57 -06001785
1786 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001787 if (at_head)
1788 list_add(&rq->queuelist, &hctx->dispatch);
1789 else
1790 list_add_tail(&rq->queuelist, &hctx->dispatch);
Jens Axboe157f3772017-09-11 16:43:57 -06001791 spin_unlock(&hctx->lock);
1792
Ming Leib0850292017-11-02 23:24:34 +08001793 if (run_queue)
1794 blk_mq_run_hw_queue(hctx, false);
Jens Axboe157f3772017-09-11 16:43:57 -06001795}
1796
Jens Axboebd166ef2017-01-17 06:03:22 -07001797void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
1798 struct list_head *list)
Jens Axboe320ae512013-10-24 09:20:05 +01001799
1800{
Ming Lei3f0cedc2018-07-02 17:35:58 +08001801 struct request *rq;
Ming Leic16d6b52018-12-17 08:44:05 -07001802 enum hctx_type type = hctx->type;
Ming Lei3f0cedc2018-07-02 17:35:58 +08001803
Jens Axboe320ae512013-10-24 09:20:05 +01001804 /*
1805 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1806 * offline now
1807 */
Ming Lei3f0cedc2018-07-02 17:35:58 +08001808 list_for_each_entry(rq, list, queuelist) {
Jens Axboee57690f2016-08-24 15:34:35 -06001809 BUG_ON(rq->mq_ctx != ctx);
Ming Lei3f0cedc2018-07-02 17:35:58 +08001810 trace_block_rq_insert(hctx->queue, rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001811 }
Ming Lei3f0cedc2018-07-02 17:35:58 +08001812
1813 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001814 list_splice_tail_init(list, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08001815 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001816 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001817}
1818
Jens Axboe3110fc72018-10-30 12:24:04 -06001819static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
Jens Axboe320ae512013-10-24 09:20:05 +01001820{
1821 struct request *rqa = container_of(a, struct request, queuelist);
1822 struct request *rqb = container_of(b, struct request, queuelist);
1823
Pavel Begunkov7d30a622019-11-29 00:11:53 +03001824 if (rqa->mq_ctx != rqb->mq_ctx)
1825 return rqa->mq_ctx > rqb->mq_ctx;
1826 if (rqa->mq_hctx != rqb->mq_hctx)
1827 return rqa->mq_hctx > rqb->mq_hctx;
Jens Axboe3110fc72018-10-30 12:24:04 -06001828
1829 return blk_rq_pos(rqa) > blk_rq_pos(rqb);
Jens Axboe320ae512013-10-24 09:20:05 +01001830}
1831
1832void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1833{
Jens Axboe320ae512013-10-24 09:20:05 +01001834 LIST_HEAD(list);
Jens Axboe320ae512013-10-24 09:20:05 +01001835
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001836 if (list_empty(&plug->mq_list))
1837 return;
Jens Axboe320ae512013-10-24 09:20:05 +01001838 list_splice_init(&plug->mq_list, &list);
1839
Jens Axboece5b0092018-11-27 17:13:56 -07001840 if (plug->rq_count > 2 && plug->multiple_queues)
1841 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe320ae512013-10-24 09:20:05 +01001842
Dongli Zhangbcc816d2019-04-04 10:57:44 +08001843 plug->rq_count = 0;
1844
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001845 do {
1846 struct list_head rq_list;
1847 struct request *rq, *head_rq = list_entry_rq(list.next);
1848 struct list_head *pos = &head_rq->queuelist; /* skip first */
1849 struct blk_mq_hw_ctx *this_hctx = head_rq->mq_hctx;
1850 struct blk_mq_ctx *this_ctx = head_rq->mq_ctx;
1851 unsigned int depth = 1;
Jens Axboe320ae512013-10-24 09:20:05 +01001852
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001853 list_for_each_continue(pos, &list) {
1854 rq = list_entry_rq(pos);
1855 BUG_ON(!rq->q);
1856 if (rq->mq_hctx != this_hctx || rq->mq_ctx != this_ctx)
1857 break;
1858 depth++;
Jens Axboe320ae512013-10-24 09:20:05 +01001859 }
1860
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001861 list_cut_before(&rq_list, &list, pos);
1862 trace_block_unplug(head_rq->q, depth, !from_schedule);
Jens Axboe67cae4c2018-10-30 11:31:51 -06001863 blk_mq_sched_insert_requests(this_hctx, this_ctx, &rq_list,
Jens Axboebd166ef2017-01-17 06:03:22 -07001864 from_schedule);
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001865 } while(!list_empty(&list));
Jens Axboe320ae512013-10-24 09:20:05 +01001866}
1867
Christoph Hellwig14ccb662019-06-06 12:29:01 +02001868static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
1869 unsigned int nr_segs)
Jens Axboe320ae512013-10-24 09:20:05 +01001870{
Christoph Hellwigf924cdd2019-06-06 12:29:00 +02001871 if (bio->bi_opf & REQ_RAHEAD)
1872 rq->cmd_flags |= REQ_FAILFAST_MASK;
1873
1874 rq->__sector = bio->bi_iter.bi_sector;
1875 rq->write_hint = bio->bi_write_hint;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02001876 blk_rq_bio_prep(rq, bio, nr_segs);
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00001877 blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO);
Jens Axboe4b570522014-05-29 11:00:11 -06001878
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +02001879 blk_account_io_start(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001880}
1881
Mike Snitzer0f955492018-01-17 11:25:56 -05001882static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
1883 struct request *rq,
Jens Axboebe94f052018-11-24 10:15:46 -07001884 blk_qc_t *cookie, bool last)
Shaohua Lif984df12015-05-08 10:51:32 -07001885{
Shaohua Lif984df12015-05-08 10:51:32 -07001886 struct request_queue *q = rq->q;
Shaohua Lif984df12015-05-08 10:51:32 -07001887 struct blk_mq_queue_data bd = {
1888 .rq = rq,
Jens Axboebe94f052018-11-24 10:15:46 -07001889 .last = last,
Shaohua Lif984df12015-05-08 10:51:32 -07001890 };
Jens Axboebd166ef2017-01-17 06:03:22 -07001891 blk_qc_t new_cookie;
Jens Axboef06345a2017-06-12 11:22:46 -06001892 blk_status_t ret;
Mike Snitzer0f955492018-01-17 11:25:56 -05001893
1894 new_cookie = request_to_qc_t(hctx, rq);
1895
1896 /*
1897 * For OK queue, we are done. For error, caller may kill it.
1898 * Any other error (busy), just add it to our list as we
1899 * previously would have done.
1900 */
1901 ret = q->mq_ops->queue_rq(hctx, &bd);
1902 switch (ret) {
1903 case BLK_STS_OK:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001904 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001905 *cookie = new_cookie;
1906 break;
1907 case BLK_STS_RESOURCE:
Ming Lei86ff7c22018-01-30 22:04:57 -05001908 case BLK_STS_DEV_RESOURCE:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001909 blk_mq_update_dispatch_busy(hctx, true);
Mike Snitzer0f955492018-01-17 11:25:56 -05001910 __blk_mq_requeue_request(rq);
1911 break;
1912 default:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001913 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001914 *cookie = BLK_QC_T_NONE;
1915 break;
1916 }
1917
1918 return ret;
1919}
1920
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001921static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
Mike Snitzer0f955492018-01-17 11:25:56 -05001922 struct request *rq,
Ming Lei396eaf22018-01-17 11:25:57 -05001923 blk_qc_t *cookie,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001924 bool bypass_insert, bool last)
Mike Snitzer0f955492018-01-17 11:25:56 -05001925{
1926 struct request_queue *q = rq->q;
Ming Leid964f042017-06-06 23:22:00 +08001927 bool run_queue = true;
1928
Ming Lei23d4ee12018-01-18 12:06:59 +08001929 /*
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001930 * RCU or SRCU read lock is needed before checking quiesced flag.
Ming Lei23d4ee12018-01-18 12:06:59 +08001931 *
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001932 * When queue is stopped or quiesced, ignore 'bypass_insert' from
1933 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
1934 * and avoid driver to try to dispatch again.
Ming Lei23d4ee12018-01-18 12:06:59 +08001935 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001936 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
Ming Leid964f042017-06-06 23:22:00 +08001937 run_queue = false;
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001938 bypass_insert = false;
1939 goto insert;
Ming Leid964f042017-06-06 23:22:00 +08001940 }
Shaohua Lif984df12015-05-08 10:51:32 -07001941
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001942 if (q->elevator && !bypass_insert)
1943 goto insert;
Bart Van Assche2253efc2016-10-28 17:20:02 -07001944
Ming Lei0bca7992018-04-05 00:35:21 +08001945 if (!blk_mq_get_dispatch_budget(hctx))
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001946 goto insert;
Jens Axboebd166ef2017-01-17 06:03:22 -07001947
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001948 if (!blk_mq_get_driver_tag(rq)) {
Ming Lei0bca7992018-04-05 00:35:21 +08001949 blk_mq_put_dispatch_budget(hctx);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001950 goto insert;
Ming Lei88022d72017-11-05 02:21:12 +08001951 }
Ming Leide148292017-10-14 17:22:29 +08001952
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001953 return __blk_mq_issue_directly(hctx, rq, cookie, last);
1954insert:
1955 if (bypass_insert)
1956 return BLK_STS_RESOURCE;
1957
Ming Lei01e99ae2020-02-25 09:04:32 +08001958 blk_mq_request_bypass_insert(rq, false, run_queue);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001959 return BLK_STS_OK;
1960}
1961
André Almeida105663f2020-01-06 15:08:18 -03001962/**
1963 * blk_mq_try_issue_directly - Try to send a request directly to device driver.
1964 * @hctx: Pointer of the associated hardware queue.
1965 * @rq: Pointer to request to be sent.
1966 * @cookie: Request queue cookie.
1967 *
1968 * If the device has enough resources to accept a new request now, send the
1969 * request directly to device driver. Else, insert at hctx->dispatch queue, so
1970 * we can try send it another time in the future. Requests inserted at this
1971 * queue have higher priority.
1972 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001973static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
1974 struct request *rq, blk_qc_t *cookie)
1975{
1976 blk_status_t ret;
1977 int srcu_idx;
1978
1979 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
1980
1981 hctx_lock(hctx, &srcu_idx);
1982
1983 ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false, true);
1984 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
Ming Lei01e99ae2020-02-25 09:04:32 +08001985 blk_mq_request_bypass_insert(rq, false, true);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001986 else if (ret != BLK_STS_OK)
1987 blk_mq_end_request(rq, ret);
1988
Jens Axboe04ced152018-01-09 08:29:46 -08001989 hctx_unlock(hctx, srcu_idx);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001990}
1991
1992blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
1993{
1994 blk_status_t ret;
1995 int srcu_idx;
1996 blk_qc_t unused_cookie;
1997 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1998
1999 hctx_lock(hctx, &srcu_idx);
2000 ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true, last);
2001 hctx_unlock(hctx, srcu_idx);
Jianchao Wang7f556a42018-12-14 09:28:18 +08002002
2003 return ret;
Christoph Hellwig5eb61262017-03-22 15:01:51 -04002004}
2005
Ming Lei6ce3dd62018-07-10 09:03:31 +08002006void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
2007 struct list_head *list)
2008{
Keith Busch536167d42020-04-07 03:13:48 +09002009 int queued = 0;
2010
Ming Lei6ce3dd62018-07-10 09:03:31 +08002011 while (!list_empty(list)) {
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002012 blk_status_t ret;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002013 struct request *rq = list_first_entry(list, struct request,
2014 queuelist);
2015
2016 list_del_init(&rq->queuelist);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002017 ret = blk_mq_request_issue_directly(rq, list_empty(list));
2018 if (ret != BLK_STS_OK) {
2019 if (ret == BLK_STS_RESOURCE ||
2020 ret == BLK_STS_DEV_RESOURCE) {
Ming Lei01e99ae2020-02-25 09:04:32 +08002021 blk_mq_request_bypass_insert(rq, false,
Jens Axboec616cbe2018-12-06 22:17:44 -07002022 list_empty(list));
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002023 break;
2024 }
2025 blk_mq_end_request(rq, ret);
Keith Busch536167d42020-04-07 03:13:48 +09002026 } else
2027 queued++;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002028 }
Jens Axboed666ba92018-11-27 17:02:25 -07002029
2030 /*
2031 * If we didn't flush the entire list, we could have told
2032 * the driver there was more coming, but that turned out to
2033 * be a lie.
2034 */
Keith Busch536167d42020-04-07 03:13:48 +09002035 if (!list_empty(list) && hctx->queue->mq_ops->commit_rqs && queued)
Jens Axboed666ba92018-11-27 17:02:25 -07002036 hctx->queue->mq_ops->commit_rqs(hctx);
Ming Lei6ce3dd62018-07-10 09:03:31 +08002037}
2038
Jens Axboece5b0092018-11-27 17:13:56 -07002039static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
2040{
2041 list_add_tail(&rq->queuelist, &plug->mq_list);
2042 plug->rq_count++;
2043 if (!plug->multiple_queues && !list_is_singular(&plug->mq_list)) {
2044 struct request *tmp;
2045
2046 tmp = list_first_entry(&plug->mq_list, struct request,
2047 queuelist);
2048 if (tmp->q != rq->q)
2049 plug->multiple_queues = true;
2050 }
2051}
2052
André Almeida105663f2020-01-06 15:08:18 -03002053/**
2054 * blk_mq_make_request - Create and send a request to block device.
2055 * @q: Request queue pointer.
2056 * @bio: Bio pointer.
2057 *
2058 * Builds up a request structure from @q and @bio and send to the device. The
2059 * request may not be queued directly to hardware if:
2060 * * This request can be merged with another one
2061 * * We want to place request at plug queue for possible future merging
2062 * * There is an IO scheduler active at this queue
2063 *
2064 * It will not queue the request if there is an error with the bio, or at the
2065 * request creation.
2066 *
2067 * Returns: Request queue cookie.
2068 */
Christoph Hellwig8cf79612020-04-25 09:53:36 +02002069blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06002070{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002071 const int is_sync = op_is_sync(bio->bi_opf);
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07002072 const int is_flush_fua = op_is_flush(bio->bi_opf);
Christoph Hellwige6e7abf2020-05-29 15:53:09 +02002073 struct blk_mq_alloc_data data = {
2074 .q = q,
2075 };
Jens Axboe07068d52014-05-22 10:40:51 -06002076 struct request *rq;
Shaohua Lif984df12015-05-08 10:51:32 -07002077 struct blk_plug *plug;
Shaohua Li5b3f3412015-05-08 10:51:33 -07002078 struct request *same_queue_rq = NULL;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002079 unsigned int nr_segs;
Jens Axboe7b371632015-11-05 10:41:40 -07002080 blk_qc_t cookie;
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002081 blk_status_t ret;
Jens Axboe07068d52014-05-22 10:40:51 -06002082
2083 blk_queue_bounce(q, &bio);
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002084 __blk_queue_split(q, &bio, &nr_segs);
Wen Xiongf36ea502017-05-10 08:54:11 -05002085
Dmitry Monakhove23947b2017-06-29 11:31:11 -07002086 if (!bio_integrity_prep(bio))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002087 goto queue_exit;
Jens Axboe07068d52014-05-22 10:40:51 -06002088
Omar Sandoval87c279e2016-06-01 22:18:48 -07002089 if (!is_flush_fua && !blk_queue_nomerges(q) &&
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002090 blk_attempt_plug_merge(q, bio, nr_segs, &same_queue_rq))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002091 goto queue_exit;
Shaohua Lif984df12015-05-08 10:51:32 -07002092
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002093 if (blk_mq_sched_bio_merge(q, bio, nr_segs))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002094 goto queue_exit;
Jens Axboebd166ef2017-01-17 06:03:22 -07002095
Christoph Hellwigd5337562018-11-14 17:02:09 +01002096 rq_qos_throttle(q, bio);
Jens Axboe87760e52016-11-09 12:38:14 -07002097
Ming Lei78091672019-01-16 19:08:15 +08002098 data.cmd_flags = bio->bi_opf;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +02002099 rq = __blk_mq_alloc_request(&data);
Jens Axboe87760e52016-11-09 12:38:14 -07002100 if (unlikely(!rq)) {
Josef Bacikc1c80382018-07-03 11:14:59 -04002101 rq_qos_cleanup(q, bio);
Jens Axboe7b6620d2019-08-15 11:09:16 -06002102 if (bio->bi_opf & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -05002103 bio_wouldblock_error(bio);
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002104 goto queue_exit;
Jens Axboe87760e52016-11-09 12:38:14 -07002105 }
2106
Xiaoguang Wangd6f1dda2018-10-23 22:30:50 +08002107 trace_block_getrq(q, bio, bio->bi_opf);
2108
Josef Bacikc1c80382018-07-03 11:14:59 -04002109 rq_qos_track(q, rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06002110
Jens Axboefd2d3322017-01-12 10:04:45 -07002111 cookie = request_to_qc_t(data.hctx, rq);
Jens Axboe07068d52014-05-22 10:40:51 -06002112
Bart Van Assche970d1682019-07-01 08:47:30 -07002113 blk_mq_bio_to_request(rq, bio, nr_segs);
2114
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002115 ret = blk_crypto_init_request(rq);
2116 if (ret != BLK_STS_OK) {
2117 bio->bi_status = ret;
2118 bio_endio(bio);
2119 blk_mq_free_request(rq);
2120 return BLK_QC_T_NONE;
2121 }
2122
Damien Le Moalb49773e72019-07-11 01:18:31 +09002123 plug = blk_mq_plug(q, bio);
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04002124 if (unlikely(is_flush_fua)) {
André Almeida105663f2020-01-06 15:08:18 -03002125 /* Bypass scheduler for flush requests */
Ming Lei923218f2017-11-02 23:24:38 +08002126 blk_insert_flush(rq);
2127 blk_mq_run_hw_queue(data.hctx, true);
Ming Lei3154df22019-09-27 15:24:31 +08002128 } else if (plug && (q->nr_hw_queues == 1 || q->mq_ops->commit_rqs ||
2129 !blk_queue_nonrot(q))) {
Jens Axboeb2c5d162018-11-29 10:03:42 -07002130 /*
2131 * Use plugging if we have a ->commit_rqs() hook as well, as
2132 * we know the driver uses bd->last in a smart fashion.
Ming Lei3154df22019-09-27 15:24:31 +08002133 *
2134 * Use normal plugging if this disk is slow HDD, as sequential
2135 * IO may benefit a lot from plug merging.
Jens Axboeb2c5d162018-11-29 10:03:42 -07002136 */
Jens Axboe5f0ed772018-11-23 22:04:33 -07002137 unsigned int request_count = plug->rq_count;
Shaohua Li600271d2016-11-03 17:03:54 -07002138 struct request *last = NULL;
2139
Ming Lei676d0602015-10-20 23:13:56 +08002140 if (!request_count)
Jeff Moyere6c44382015-05-08 10:51:30 -07002141 trace_block_plug(q);
Shaohua Li600271d2016-11-03 17:03:54 -07002142 else
2143 last = list_entry_rq(plug->mq_list.prev);
Jens Axboeb094f892015-11-20 20:29:45 -07002144
Shaohua Li600271d2016-11-03 17:03:54 -07002145 if (request_count >= BLK_MAX_REQUEST_COUNT || (last &&
2146 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
Jeff Moyere6c44382015-05-08 10:51:30 -07002147 blk_flush_plug_list(plug, false);
2148 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002149 }
Jens Axboeb094f892015-11-20 20:29:45 -07002150
Jens Axboece5b0092018-11-27 17:13:56 -07002151 blk_add_rq_to_plug(plug, rq);
Ming Leia12de1d2019-09-27 15:24:30 +08002152 } else if (q->elevator) {
André Almeida105663f2020-01-06 15:08:18 -03002153 /* Insert the request at the IO scheduler queue */
Ming Leia12de1d2019-09-27 15:24:30 +08002154 blk_mq_sched_insert_request(rq, false, true, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002155 } else if (plug && !blk_queue_nomerges(q)) {
Jens Axboe320ae512013-10-24 09:20:05 +01002156 /*
2157 * We do limited plugging. If the bio can be merged, do that.
2158 * Otherwise the existing request in the plug list will be
2159 * issued. So the plug list will have one request at most
Christoph Hellwig22997222017-03-22 15:01:52 -04002160 * The plug list might get flushed before this. If that happens,
2161 * the plug list is empty, and same_queue_rq is invalid.
Jens Axboe320ae512013-10-24 09:20:05 +01002162 */
Christoph Hellwig22997222017-03-22 15:01:52 -04002163 if (list_empty(&plug->mq_list))
2164 same_queue_rq = NULL;
Jens Axboe4711b572018-11-27 17:07:17 -07002165 if (same_queue_rq) {
Christoph Hellwig22997222017-03-22 15:01:52 -04002166 list_del_init(&same_queue_rq->queuelist);
Jens Axboe4711b572018-11-27 17:07:17 -07002167 plug->rq_count--;
2168 }
Jens Axboece5b0092018-11-27 17:13:56 -07002169 blk_add_rq_to_plug(plug, rq);
Yufen Yuff3b74b2019-03-26 21:19:25 +08002170 trace_block_plug(q);
Christoph Hellwig22997222017-03-22 15:01:52 -04002171
Ming Leidad7a3b2017-06-06 23:21:59 +08002172 if (same_queue_rq) {
Jens Axboeea4f9952018-10-29 15:06:13 -06002173 data.hctx = same_queue_rq->mq_hctx;
Yufen Yuff3b74b2019-03-26 21:19:25 +08002174 trace_block_unplug(q, 1, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002175 blk_mq_try_issue_directly(data.hctx, same_queue_rq,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002176 &cookie);
Ming Leidad7a3b2017-06-06 23:21:59 +08002177 }
Ming Leia12de1d2019-09-27 15:24:30 +08002178 } else if ((q->nr_hw_queues > 1 && is_sync) ||
2179 !data.hctx->dispatch_busy) {
André Almeida105663f2020-01-06 15:08:18 -03002180 /*
2181 * There is no scheduler and we can try to send directly
2182 * to the hardware.
2183 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002184 blk_mq_try_issue_directly(data.hctx, rq, &cookie);
Ming Leiab42f352017-05-26 19:53:19 +08002185 } else {
André Almeida105663f2020-01-06 15:08:18 -03002186 /* Default case. */
huhai8fa9f552018-05-16 08:21:21 -06002187 blk_mq_sched_insert_request(rq, false, true, true);
Ming Leiab42f352017-05-26 19:53:19 +08002188 }
Jens Axboe320ae512013-10-24 09:20:05 +01002189
Jens Axboe7b371632015-11-05 10:41:40 -07002190 return cookie;
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002191queue_exit:
2192 blk_queue_exit(q);
2193 return BLK_QC_T_NONE;
Jens Axboe320ae512013-10-24 09:20:05 +01002194}
Christoph Hellwig8cf79612020-04-25 09:53:36 +02002195EXPORT_SYMBOL_GPL(blk_mq_make_request); /* only for request based dm */
Jens Axboe320ae512013-10-24 09:20:05 +01002196
Jens Axboecc71a6f2017-01-11 14:29:56 -07002197void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2198 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01002199{
2200 struct page *page;
2201
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002202 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002203 int i;
2204
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002205 for (i = 0; i < tags->nr_tags; i++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002206 struct request *rq = tags->static_rqs[i];
2207
2208 if (!rq)
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002209 continue;
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002210 set->ops->exit_request(set, rq, hctx_idx);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002211 tags->static_rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002212 }
2213 }
2214
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002215 while (!list_empty(&tags->page_list)) {
2216 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07002217 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002218 /*
2219 * Remove kmemleak object previously allocated in
Raul E Rangel273938b2019-05-02 13:48:11 -06002220 * blk_mq_alloc_rqs().
Catalin Marinasf75782e2015-09-14 18:16:02 +01002221 */
2222 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01002223 __free_pages(page, page->private);
2224 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002225}
Jens Axboe320ae512013-10-24 09:20:05 +01002226
Jens Axboecc71a6f2017-01-11 14:29:56 -07002227void blk_mq_free_rq_map(struct blk_mq_tags *tags)
2228{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002229 kfree(tags->rqs);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002230 tags->rqs = NULL;
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002231 kfree(tags->static_rqs);
2232 tags->static_rqs = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002233
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002234 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01002235}
2236
Jens Axboecc71a6f2017-01-11 14:29:56 -07002237struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
2238 unsigned int hctx_idx,
2239 unsigned int nr_tags,
2240 unsigned int reserved_tags)
Jens Axboe320ae512013-10-24 09:20:05 +01002241{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002242 struct blk_mq_tags *tags;
Shaohua Li59f082e2017-02-01 09:53:14 -08002243 int node;
Jens Axboe320ae512013-10-24 09:20:05 +01002244
Dongli Zhang7d76f852019-02-27 21:35:01 +08002245 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002246 if (node == NUMA_NO_NODE)
2247 node = set->numa_node;
2248
2249 tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
Shaohua Li24391c02015-01-23 14:18:00 -07002250 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002251 if (!tags)
2252 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002253
Kees Cook590b5b72018-06-12 14:04:20 -07002254 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002255 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
Shaohua Li59f082e2017-02-01 09:53:14 -08002256 node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002257 if (!tags->rqs) {
2258 blk_mq_free_tags(tags);
2259 return NULL;
2260 }
Jens Axboe320ae512013-10-24 09:20:05 +01002261
Kees Cook590b5b72018-06-12 14:04:20 -07002262 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2263 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2264 node);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002265 if (!tags->static_rqs) {
2266 kfree(tags->rqs);
2267 blk_mq_free_tags(tags);
2268 return NULL;
2269 }
2270
Jens Axboecc71a6f2017-01-11 14:29:56 -07002271 return tags;
2272}
2273
2274static size_t order_to_size(unsigned int order)
2275{
2276 return (size_t)PAGE_SIZE << order;
2277}
2278
Tejun Heo1d9bd512018-01-09 08:29:48 -08002279static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2280 unsigned int hctx_idx, int node)
2281{
2282 int ret;
2283
2284 if (set->ops->init_request) {
2285 ret = set->ops->init_request(set, rq, hctx_idx, node);
2286 if (ret)
2287 return ret;
2288 }
2289
Keith Busch12f5b932018-05-29 15:52:28 +02002290 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Tejun Heo1d9bd512018-01-09 08:29:48 -08002291 return 0;
2292}
2293
Jens Axboecc71a6f2017-01-11 14:29:56 -07002294int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2295 unsigned int hctx_idx, unsigned int depth)
2296{
2297 unsigned int i, j, entries_per_page, max_order = 4;
2298 size_t rq_size, left;
Shaohua Li59f082e2017-02-01 09:53:14 -08002299 int node;
2300
Dongli Zhang7d76f852019-02-27 21:35:01 +08002301 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002302 if (node == NUMA_NO_NODE)
2303 node = set->numa_node;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002304
2305 INIT_LIST_HEAD(&tags->page_list);
2306
Jens Axboe320ae512013-10-24 09:20:05 +01002307 /*
2308 * rq_size is the size of the request plus driver payload, rounded
2309 * to the cacheline size
2310 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002311 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01002312 cache_line_size());
Jens Axboecc71a6f2017-01-11 14:29:56 -07002313 left = rq_size * depth;
Jens Axboe320ae512013-10-24 09:20:05 +01002314
Jens Axboecc71a6f2017-01-11 14:29:56 -07002315 for (i = 0; i < depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01002316 int this_order = max_order;
2317 struct page *page;
2318 int to_do;
2319 void *p;
2320
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06002321 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01002322 this_order--;
2323
2324 do {
Shaohua Li59f082e2017-02-01 09:53:14 -08002325 page = alloc_pages_node(node,
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002326 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06002327 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01002328 if (page)
2329 break;
2330 if (!this_order--)
2331 break;
2332 if (order_to_size(this_order) < rq_size)
2333 break;
2334 } while (1);
2335
2336 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002337 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01002338
2339 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002340 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01002341
2342 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002343 /*
2344 * Allow kmemleak to scan these pages as they contain pointers
2345 * to additional allocations like via ops->init_request().
2346 */
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002347 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
Jens Axboe320ae512013-10-24 09:20:05 +01002348 entries_per_page = order_to_size(this_order) / rq_size;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002349 to_do = min(entries_per_page, depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01002350 left -= to_do * rq_size;
2351 for (j = 0; j < to_do; j++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002352 struct request *rq = p;
2353
2354 tags->static_rqs[i] = rq;
Tejun Heo1d9bd512018-01-09 08:29:48 -08002355 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
2356 tags->static_rqs[i] = NULL;
2357 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002358 }
2359
Jens Axboe320ae512013-10-24 09:20:05 +01002360 p += rq_size;
2361 i++;
2362 }
2363 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002364 return 0;
Jens Axboe320ae512013-10-24 09:20:05 +01002365
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002366fail:
Jens Axboecc71a6f2017-01-11 14:29:56 -07002367 blk_mq_free_rqs(set, tags, hctx_idx);
2368 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01002369}
2370
Ming Leibf0beec2020-05-29 15:53:15 +02002371struct rq_iter_data {
2372 struct blk_mq_hw_ctx *hctx;
2373 bool has_rq;
2374};
2375
2376static bool blk_mq_has_request(struct request *rq, void *data, bool reserved)
2377{
2378 struct rq_iter_data *iter_data = data;
2379
2380 if (rq->mq_hctx != iter_data->hctx)
2381 return true;
2382 iter_data->has_rq = true;
2383 return false;
2384}
2385
2386static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
2387{
2388 struct blk_mq_tags *tags = hctx->sched_tags ?
2389 hctx->sched_tags : hctx->tags;
2390 struct rq_iter_data data = {
2391 .hctx = hctx,
2392 };
2393
2394 blk_mq_all_tag_iter(tags, blk_mq_has_request, &data);
2395 return data.has_rq;
2396}
2397
2398static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu,
2399 struct blk_mq_hw_ctx *hctx)
2400{
2401 if (cpumask_next_and(-1, hctx->cpumask, cpu_online_mask) != cpu)
2402 return false;
2403 if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids)
2404 return false;
2405 return true;
2406}
2407
2408static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
2409{
2410 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2411 struct blk_mq_hw_ctx, cpuhp_online);
2412
2413 if (!cpumask_test_cpu(cpu, hctx->cpumask) ||
2414 !blk_mq_last_cpu_in_hctx(cpu, hctx))
2415 return 0;
2416
2417 /*
2418 * Prevent new request from being allocated on the current hctx.
2419 *
2420 * The smp_mb__after_atomic() Pairs with the implied barrier in
2421 * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is
2422 * seen once we return from the tag allocator.
2423 */
2424 set_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2425 smp_mb__after_atomic();
2426
2427 /*
2428 * Try to grab a reference to the queue and wait for any outstanding
2429 * requests. If we could not grab a reference the queue has been
2430 * frozen and there are no requests.
2431 */
2432 if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) {
2433 while (blk_mq_hctx_has_requests(hctx))
2434 msleep(5);
2435 percpu_ref_put(&hctx->queue->q_usage_counter);
2436 }
2437
2438 return 0;
2439}
2440
2441static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node)
2442{
2443 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2444 struct blk_mq_hw_ctx, cpuhp_online);
2445
2446 if (cpumask_test_cpu(cpu, hctx->cpumask))
2447 clear_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2448 return 0;
2449}
2450
Jens Axboee57690f2016-08-24 15:34:35 -06002451/*
2452 * 'cpu' is going away. splice any existing rq_list entries from this
2453 * software queue to the hw queue dispatch list, and ensure that it
2454 * gets run.
2455 */
Thomas Gleixner9467f852016-09-22 08:05:17 -06002456static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
Jens Axboe484b4062014-05-21 14:01:15 -06002457{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002458 struct blk_mq_hw_ctx *hctx;
Jens Axboe484b4062014-05-21 14:01:15 -06002459 struct blk_mq_ctx *ctx;
2460 LIST_HEAD(tmp);
Ming Leic16d6b52018-12-17 08:44:05 -07002461 enum hctx_type type;
Jens Axboe484b4062014-05-21 14:01:15 -06002462
Thomas Gleixner9467f852016-09-22 08:05:17 -06002463 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02002464 if (!cpumask_test_cpu(cpu, hctx->cpumask))
2465 return 0;
2466
Jens Axboee57690f2016-08-24 15:34:35 -06002467 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Ming Leic16d6b52018-12-17 08:44:05 -07002468 type = hctx->type;
Jens Axboe484b4062014-05-21 14:01:15 -06002469
2470 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002471 if (!list_empty(&ctx->rq_lists[type])) {
2472 list_splice_init(&ctx->rq_lists[type], &tmp);
Jens Axboe484b4062014-05-21 14:01:15 -06002473 blk_mq_hctx_clear_pending(hctx, ctx);
2474 }
2475 spin_unlock(&ctx->lock);
2476
2477 if (list_empty(&tmp))
Thomas Gleixner9467f852016-09-22 08:05:17 -06002478 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002479
Jens Axboee57690f2016-08-24 15:34:35 -06002480 spin_lock(&hctx->lock);
2481 list_splice_tail_init(&tmp, &hctx->dispatch);
2482 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06002483
2484 blk_mq_run_hw_queue(hctx, true);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002485 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002486}
2487
Thomas Gleixner9467f852016-09-22 08:05:17 -06002488static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
Jens Axboe484b4062014-05-21 14:01:15 -06002489{
Ming Leibf0beec2020-05-29 15:53:15 +02002490 if (!(hctx->flags & BLK_MQ_F_STACKING))
2491 cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2492 &hctx->cpuhp_online);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002493 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
2494 &hctx->cpuhp_dead);
Jens Axboe484b4062014-05-21 14:01:15 -06002495}
2496
Ming Leic3b4afc2015-06-04 22:25:04 +08002497/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08002498static void blk_mq_exit_hctx(struct request_queue *q,
2499 struct blk_mq_tag_set *set,
2500 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
2501{
Ming Lei8ab0b7d2018-01-09 21:28:29 +08002502 if (blk_mq_hw_queue_mapped(hctx))
2503 blk_mq_tag_idle(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002504
Ming Leif70ced02014-09-25 23:23:47 +08002505 if (set->ops->exit_request)
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002506 set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx);
Ming Leif70ced02014-09-25 23:23:47 +08002507
Ming Lei08e98fc2014-09-25 23:23:38 +08002508 if (set->ops->exit_hctx)
2509 set->ops->exit_hctx(hctx, hctx_idx);
2510
Thomas Gleixner9467f852016-09-22 08:05:17 -06002511 blk_mq_remove_cpuhp(hctx);
Ming Lei2f8f1332019-04-30 09:52:27 +08002512
2513 spin_lock(&q->unused_hctx_lock);
2514 list_add(&hctx->hctx_list, &q->unused_hctx_list);
2515 spin_unlock(&q->unused_hctx_lock);
Ming Lei08e98fc2014-09-25 23:23:38 +08002516}
2517
Ming Lei624dbe42014-05-27 23:35:13 +08002518static void blk_mq_exit_hw_queues(struct request_queue *q,
2519 struct blk_mq_tag_set *set, int nr_queue)
2520{
2521 struct blk_mq_hw_ctx *hctx;
2522 unsigned int i;
2523
2524 queue_for_each_hw_ctx(q, hctx, i) {
2525 if (i == nr_queue)
2526 break;
Jianchao Wang477e19d2018-10-12 18:07:25 +08002527 blk_mq_debugfs_unregister_hctx(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002528 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08002529 }
Ming Lei624dbe42014-05-27 23:35:13 +08002530}
2531
Ming Lei7c6c5b72019-04-30 09:52:26 +08002532static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
2533{
2534 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
2535
2536 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
2537 __alignof__(struct blk_mq_hw_ctx)) !=
2538 sizeof(struct blk_mq_hw_ctx));
2539
2540 if (tag_set->flags & BLK_MQ_F_BLOCKING)
2541 hw_ctx_size += sizeof(struct srcu_struct);
2542
2543 return hw_ctx_size;
2544}
2545
Ming Lei08e98fc2014-09-25 23:23:38 +08002546static int blk_mq_init_hctx(struct request_queue *q,
2547 struct blk_mq_tag_set *set,
2548 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
2549{
Ming Lei7c6c5b72019-04-30 09:52:26 +08002550 hctx->queue_num = hctx_idx;
Ming Lei08e98fc2014-09-25 23:23:38 +08002551
Ming Leibf0beec2020-05-29 15:53:15 +02002552 if (!(hctx->flags & BLK_MQ_F_STACKING))
2553 cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2554 &hctx->cpuhp_online);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002555 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
2556
2557 hctx->tags = set->tags[hctx_idx];
2558
2559 if (set->ops->init_hctx &&
2560 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
2561 goto unregister_cpu_notifier;
2562
2563 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
2564 hctx->numa_node))
2565 goto exit_hctx;
2566 return 0;
2567
2568 exit_hctx:
2569 if (set->ops->exit_hctx)
2570 set->ops->exit_hctx(hctx, hctx_idx);
2571 unregister_cpu_notifier:
2572 blk_mq_remove_cpuhp(hctx);
2573 return -1;
2574}
2575
2576static struct blk_mq_hw_ctx *
2577blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
2578 int node)
2579{
2580 struct blk_mq_hw_ctx *hctx;
2581 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
2582
2583 hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node);
2584 if (!hctx)
2585 goto fail_alloc_hctx;
2586
2587 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
2588 goto free_hctx;
2589
2590 atomic_set(&hctx->nr_active, 0);
Ming Lei08e98fc2014-09-25 23:23:38 +08002591 if (node == NUMA_NO_NODE)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002592 node = set->numa_node;
2593 hctx->numa_node = node;
Ming Lei08e98fc2014-09-25 23:23:38 +08002594
Jens Axboe9f993732017-04-10 09:54:54 -06002595 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08002596 spin_lock_init(&hctx->lock);
2597 INIT_LIST_HEAD(&hctx->dispatch);
2598 hctx->queue = q;
Jeff Moyer2404e602015-11-03 10:40:06 -05002599 hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08002600
Ming Lei2f8f1332019-04-30 09:52:27 +08002601 INIT_LIST_HEAD(&hctx->hctx_list);
2602
Ming Lei08e98fc2014-09-25 23:23:38 +08002603 /*
2604 * Allocate space for all possible cpus to avoid allocation at
2605 * runtime
2606 */
Johannes Thumshirnd904bfa2017-11-15 17:32:33 -08002607 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
Ming Lei7c6c5b72019-04-30 09:52:26 +08002608 gfp, node);
Ming Lei08e98fc2014-09-25 23:23:38 +08002609 if (!hctx->ctxs)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002610 goto free_cpumask;
Ming Lei08e98fc2014-09-25 23:23:38 +08002611
Jianchao Wang5b202852018-10-12 18:07:26 +08002612 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
Ming Lei7c6c5b72019-04-30 09:52:26 +08002613 gfp, node))
Ming Lei08e98fc2014-09-25 23:23:38 +08002614 goto free_ctxs;
Ming Lei08e98fc2014-09-25 23:23:38 +08002615 hctx->nr_ctx = 0;
2616
Ming Lei5815839b2018-06-25 19:31:47 +08002617 spin_lock_init(&hctx->dispatch_wait_lock);
Jens Axboeeb619fd2017-11-09 08:32:43 -07002618 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
2619 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
2620
Guoqing Jiang754a1572020-03-09 22:41:37 +01002621 hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp);
Ming Leif70ced02014-09-25 23:23:47 +08002622 if (!hctx->fq)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002623 goto free_bitmap;
Ming Leif70ced02014-09-25 23:23:47 +08002624
Bart Van Assche6a83e742016-11-02 10:09:51 -06002625 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -08002626 init_srcu_struct(hctx->srcu);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002627 blk_mq_hctx_kobj_init(hctx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06002628
Ming Lei7c6c5b72019-04-30 09:52:26 +08002629 return hctx;
Ming Lei08e98fc2014-09-25 23:23:38 +08002630
2631 free_bitmap:
Omar Sandoval88459642016-09-17 08:38:44 -06002632 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08002633 free_ctxs:
2634 kfree(hctx->ctxs);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002635 free_cpumask:
2636 free_cpumask_var(hctx->cpumask);
2637 free_hctx:
2638 kfree(hctx);
2639 fail_alloc_hctx:
2640 return NULL;
Ming Lei08e98fc2014-09-25 23:23:38 +08002641}
2642
Jens Axboe320ae512013-10-24 09:20:05 +01002643static void blk_mq_init_cpu_queues(struct request_queue *q,
2644 unsigned int nr_hw_queues)
2645{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002646 struct blk_mq_tag_set *set = q->tag_set;
2647 unsigned int i, j;
Jens Axboe320ae512013-10-24 09:20:05 +01002648
2649 for_each_possible_cpu(i) {
2650 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
2651 struct blk_mq_hw_ctx *hctx;
Ming Leic16d6b52018-12-17 08:44:05 -07002652 int k;
Jens Axboe320ae512013-10-24 09:20:05 +01002653
Jens Axboe320ae512013-10-24 09:20:05 +01002654 __ctx->cpu = i;
2655 spin_lock_init(&__ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002656 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
2657 INIT_LIST_HEAD(&__ctx->rq_lists[k]);
2658
Jens Axboe320ae512013-10-24 09:20:05 +01002659 __ctx->queue = q;
2660
Jens Axboe320ae512013-10-24 09:20:05 +01002661 /*
2662 * Set local node, IFF we have more than one hw queue. If
2663 * not, we remain on the home node of the device
2664 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06002665 for (j = 0; j < set->nr_maps; j++) {
2666 hctx = blk_mq_map_queue_type(q, j, i);
2667 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
2668 hctx->numa_node = local_memory_node(cpu_to_node(i));
2669 }
Jens Axboe320ae512013-10-24 09:20:05 +01002670 }
2671}
2672
Weiping Zhang03b63b02020-05-07 21:04:22 +08002673static bool __blk_mq_alloc_map_and_request(struct blk_mq_tag_set *set,
2674 int hctx_idx)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002675{
2676 int ret = 0;
2677
2678 set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
2679 set->queue_depth, set->reserved_tags);
2680 if (!set->tags[hctx_idx])
2681 return false;
2682
2683 ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
2684 set->queue_depth);
2685 if (!ret)
2686 return true;
2687
2688 blk_mq_free_rq_map(set->tags[hctx_idx]);
2689 set->tags[hctx_idx] = NULL;
2690 return false;
2691}
2692
2693static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
2694 unsigned int hctx_idx)
2695{
Dan Carpenter4e6db0f2018-11-29 13:56:54 +03002696 if (set->tags && set->tags[hctx_idx]) {
Jens Axboebd166ef2017-01-17 06:03:22 -07002697 blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
2698 blk_mq_free_rq_map(set->tags[hctx_idx]);
2699 set->tags[hctx_idx] = NULL;
2700 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002701}
2702
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002703static void blk_mq_map_swqueue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01002704{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002705 unsigned int i, j, hctx_idx;
Jens Axboe320ae512013-10-24 09:20:05 +01002706 struct blk_mq_hw_ctx *hctx;
2707 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08002708 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002709
2710 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06002711 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01002712 hctx->nr_ctx = 0;
huhaid416c922018-05-18 08:32:30 -06002713 hctx->dispatch_from = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002714 }
2715
2716 /*
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002717 * Map software to hardware queues.
Ming Lei4412efe2018-04-25 04:01:44 +08002718 *
2719 * If the cpu isn't present, the cpu is mapped to first hctx.
Jens Axboe320ae512013-10-24 09:20:05 +01002720 */
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08002721 for_each_possible_cpu(i) {
Ming Lei4412efe2018-04-25 04:01:44 +08002722
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01002723 ctx = per_cpu_ptr(q->queue_ctx, i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06002724 for (j = 0; j < set->nr_maps; j++) {
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002725 if (!set->map[j].nr_queues) {
2726 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2727 HCTX_TYPE_DEFAULT, i);
Ming Leie5edd5f2018-12-18 01:28:56 +08002728 continue;
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002729 }
Ming Leifd689872020-05-07 21:04:08 +08002730 hctx_idx = set->map[j].mq_map[i];
2731 /* unmapped hw queue can be remapped after CPU topo changed */
2732 if (!set->tags[hctx_idx] &&
Weiping Zhang03b63b02020-05-07 21:04:22 +08002733 !__blk_mq_alloc_map_and_request(set, hctx_idx)) {
Ming Leifd689872020-05-07 21:04:08 +08002734 /*
2735 * If tags initialization fail for some hctx,
2736 * that hctx won't be brought online. In this
2737 * case, remap the current ctx to hctx[0] which
2738 * is guaranteed to always have tags allocated
2739 */
2740 set->map[j].mq_map[i] = 0;
2741 }
Ming Leie5edd5f2018-12-18 01:28:56 +08002742
Jens Axboeb3c661b2018-10-30 10:36:06 -06002743 hctx = blk_mq_map_queue_type(q, j, i);
Jianchao Wang8ccdf4a2019-01-24 18:25:32 +08002744 ctx->hctxs[j] = hctx;
Jens Axboeb3c661b2018-10-30 10:36:06 -06002745 /*
2746 * If the CPU is already set in the mask, then we've
2747 * mapped this one already. This can happen if
2748 * devices share queues across queue maps.
2749 */
2750 if (cpumask_test_cpu(i, hctx->cpumask))
2751 continue;
2752
2753 cpumask_set_cpu(i, hctx->cpumask);
2754 hctx->type = j;
2755 ctx->index_hw[hctx->type] = hctx->nr_ctx;
2756 hctx->ctxs[hctx->nr_ctx++] = ctx;
2757
2758 /*
2759 * If the nr_ctx type overflows, we have exceeded the
2760 * amount of sw queues we can support.
2761 */
2762 BUG_ON(!hctx->nr_ctx);
2763 }
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002764
2765 for (; j < HCTX_MAX_TYPES; j++)
2766 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2767 HCTX_TYPE_DEFAULT, i);
Jens Axboe320ae512013-10-24 09:20:05 +01002768 }
Jens Axboe506e9312014-05-07 10:26:44 -06002769
2770 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei4412efe2018-04-25 04:01:44 +08002771 /*
2772 * If no software queues are mapped to this hardware queue,
2773 * disable it and free the request entries.
2774 */
2775 if (!hctx->nr_ctx) {
2776 /* Never unmap queue 0. We need it as a
2777 * fallback in case of a new remap fails
2778 * allocation
2779 */
2780 if (i && set->tags[i])
2781 blk_mq_free_map_and_requests(set, i);
2782
2783 hctx->tags = NULL;
2784 continue;
2785 }
Jens Axboe484b4062014-05-21 14:01:15 -06002786
Ming Lei2a34c082015-04-21 10:00:20 +08002787 hctx->tags = set->tags[i];
2788 WARN_ON(!hctx->tags);
2789
Jens Axboe484b4062014-05-21 14:01:15 -06002790 /*
Chong Yuan889fa312015-04-15 11:39:29 -06002791 * Set the map size to the number of mapped software queues.
2792 * This is more accurate and more efficient than looping
2793 * over all possibly mapped software queues.
2794 */
Omar Sandoval88459642016-09-17 08:38:44 -06002795 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
Chong Yuan889fa312015-04-15 11:39:29 -06002796
2797 /*
Jens Axboe484b4062014-05-21 14:01:15 -06002798 * Initialize batch roundrobin counts
2799 */
Ming Leif82ddf12018-04-08 17:48:10 +08002800 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06002801 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
2802 }
Jens Axboe320ae512013-10-24 09:20:05 +01002803}
2804
Jens Axboe8e8320c2017-06-20 17:56:13 -06002805/*
2806 * Caller needs to ensure that we're either frozen/quiesced, or that
2807 * the queue isn't live yet.
2808 */
Jeff Moyer2404e602015-11-03 10:40:06 -05002809static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06002810{
2811 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06002812 int i;
2813
Jeff Moyer2404e602015-11-03 10:40:06 -05002814 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei97889f92018-06-25 19:31:48 +08002815 if (shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05002816 hctx->flags |= BLK_MQ_F_TAG_SHARED;
Ming Lei97889f92018-06-25 19:31:48 +08002817 else
Jeff Moyer2404e602015-11-03 10:40:06 -05002818 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
2819 }
2820}
2821
Jens Axboe8e8320c2017-06-20 17:56:13 -06002822static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
2823 bool shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05002824{
2825 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06002826
Bart Van Assche705cda92017-04-07 11:16:49 -07002827 lockdep_assert_held(&set->tag_list_lock);
2828
Jens Axboe0d2602c2014-05-13 15:10:52 -06002829 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2830 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05002831 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002832 blk_mq_unfreeze_queue(q);
2833 }
2834}
2835
2836static void blk_mq_del_queue_tag_set(struct request_queue *q)
2837{
2838 struct blk_mq_tag_set *set = q->tag_set;
2839
Jens Axboe0d2602c2014-05-13 15:10:52 -06002840 mutex_lock(&set->tag_list_lock);
Bart Van Assche705cda92017-04-07 11:16:49 -07002841 list_del_rcu(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05002842 if (list_is_singular(&set->tag_list)) {
2843 /* just transitioned to unshared */
2844 set->flags &= ~BLK_MQ_F_TAG_SHARED;
2845 /* update existing queue */
2846 blk_mq_update_tag_set_depth(set, false);
2847 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06002848 mutex_unlock(&set->tag_list_lock);
Roman Pena347c7a2018-06-10 22:38:24 +02002849 INIT_LIST_HEAD(&q->tag_set_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002850}
2851
2852static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
2853 struct request_queue *q)
2854{
Jens Axboe0d2602c2014-05-13 15:10:52 -06002855 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05002856
Jens Axboeff821d22017-11-10 22:05:12 -07002857 /*
2858 * Check to see if we're transitioning to shared (from 1 to 2 queues).
2859 */
2860 if (!list_empty(&set->tag_list) &&
2861 !(set->flags & BLK_MQ_F_TAG_SHARED)) {
Jeff Moyer2404e602015-11-03 10:40:06 -05002862 set->flags |= BLK_MQ_F_TAG_SHARED;
2863 /* update existing queue */
2864 blk_mq_update_tag_set_depth(set, true);
2865 }
2866 if (set->flags & BLK_MQ_F_TAG_SHARED)
2867 queue_set_hctx_shared(q, true);
Bart Van Assche705cda92017-04-07 11:16:49 -07002868 list_add_tail_rcu(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05002869
Jens Axboe0d2602c2014-05-13 15:10:52 -06002870 mutex_unlock(&set->tag_list_lock);
2871}
2872
Ming Lei1db49092018-11-20 09:44:35 +08002873/* All allocations will be freed in release handler of q->mq_kobj */
2874static int blk_mq_alloc_ctxs(struct request_queue *q)
2875{
2876 struct blk_mq_ctxs *ctxs;
2877 int cpu;
2878
2879 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
2880 if (!ctxs)
2881 return -ENOMEM;
2882
2883 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2884 if (!ctxs->queue_ctx)
2885 goto fail;
2886
2887 for_each_possible_cpu(cpu) {
2888 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
2889 ctx->ctxs = ctxs;
2890 }
2891
2892 q->mq_kobj = &ctxs->kobj;
2893 q->queue_ctx = ctxs->queue_ctx;
2894
2895 return 0;
2896 fail:
2897 kfree(ctxs);
2898 return -ENOMEM;
2899}
2900
Ming Leie09aae72015-01-29 20:17:27 +08002901/*
2902 * It is the actual release handler for mq, but we do it from
2903 * request queue's release handler for avoiding use-after-free
2904 * and headache because q->mq_kobj shouldn't have been introduced,
2905 * but we can't group ctx/kctx kobj without it.
2906 */
2907void blk_mq_release(struct request_queue *q)
2908{
Ming Lei2f8f1332019-04-30 09:52:27 +08002909 struct blk_mq_hw_ctx *hctx, *next;
2910 int i;
Ming Leie09aae72015-01-29 20:17:27 +08002911
Ming Lei2f8f1332019-04-30 09:52:27 +08002912 queue_for_each_hw_ctx(q, hctx, i)
2913 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
2914
2915 /* all hctx are in .unused_hctx_list now */
2916 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
2917 list_del_init(&hctx->hctx_list);
Ming Lei6c8b2322017-02-22 18:14:01 +08002918 kobject_put(&hctx->kobj);
Ming Leic3b4afc2015-06-04 22:25:04 +08002919 }
Ming Leie09aae72015-01-29 20:17:27 +08002920
2921 kfree(q->queue_hw_ctx);
2922
Ming Lei7ea5fe32017-02-22 18:14:00 +08002923 /*
2924 * release .mq_kobj and sw queue's kobject now because
2925 * both share lifetime with request queue.
2926 */
2927 blk_mq_sysfs_deinit(q);
Ming Leie09aae72015-01-29 20:17:27 +08002928}
2929
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01002930struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set,
2931 void *queuedata)
Jens Axboe320ae512013-10-24 09:20:05 +01002932{
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002933 struct request_queue *uninit_q, *q;
2934
Christoph Hellwig3d745ea2020-03-27 09:30:11 +01002935 uninit_q = __blk_alloc_queue(set->numa_node);
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002936 if (!uninit_q)
2937 return ERR_PTR(-ENOMEM);
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01002938 uninit_q->queuedata = queuedata;
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002939
Damien Le Moal737eb782019-09-05 18:51:33 +09002940 /*
2941 * Initialize the queue without an elevator. device_add_disk() will do
2942 * the initialization.
2943 */
2944 q = blk_mq_init_allocated_queue(set, uninit_q, false);
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002945 if (IS_ERR(q))
2946 blk_cleanup_queue(uninit_q);
2947
2948 return q;
2949}
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01002950EXPORT_SYMBOL_GPL(blk_mq_init_queue_data);
2951
2952struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
2953{
2954 return blk_mq_init_queue_data(set, NULL);
2955}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002956EXPORT_SYMBOL(blk_mq_init_queue);
2957
Jens Axboe9316a9e2018-10-15 08:40:37 -06002958/*
2959 * Helper for setting up a queue with mq ops, given queue depth, and
2960 * the passed in mq ops flags.
2961 */
2962struct request_queue *blk_mq_init_sq_queue(struct blk_mq_tag_set *set,
2963 const struct blk_mq_ops *ops,
2964 unsigned int queue_depth,
2965 unsigned int set_flags)
2966{
2967 struct request_queue *q;
2968 int ret;
2969
2970 memset(set, 0, sizeof(*set));
2971 set->ops = ops;
2972 set->nr_hw_queues = 1;
Jens Axboeb3c661b2018-10-30 10:36:06 -06002973 set->nr_maps = 1;
Jens Axboe9316a9e2018-10-15 08:40:37 -06002974 set->queue_depth = queue_depth;
2975 set->numa_node = NUMA_NO_NODE;
2976 set->flags = set_flags;
2977
2978 ret = blk_mq_alloc_tag_set(set);
2979 if (ret)
2980 return ERR_PTR(ret);
2981
2982 q = blk_mq_init_queue(set);
2983 if (IS_ERR(q)) {
2984 blk_mq_free_tag_set(set);
2985 return q;
2986 }
2987
2988 return q;
2989}
2990EXPORT_SYMBOL(blk_mq_init_sq_queue);
2991
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002992static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
2993 struct blk_mq_tag_set *set, struct request_queue *q,
2994 int hctx_idx, int node)
2995{
Ming Lei2f8f1332019-04-30 09:52:27 +08002996 struct blk_mq_hw_ctx *hctx = NULL, *tmp;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002997
Ming Lei2f8f1332019-04-30 09:52:27 +08002998 /* reuse dead hctx first */
2999 spin_lock(&q->unused_hctx_lock);
3000 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
3001 if (tmp->numa_node == node) {
3002 hctx = tmp;
3003 break;
3004 }
3005 }
3006 if (hctx)
3007 list_del_init(&hctx->hctx_list);
3008 spin_unlock(&q->unused_hctx_lock);
3009
3010 if (!hctx)
3011 hctx = blk_mq_alloc_hctx(q, set, node);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003012 if (!hctx)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003013 goto fail;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003014
Ming Lei7c6c5b72019-04-30 09:52:26 +08003015 if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
3016 goto free_hctx;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003017
3018 return hctx;
Ming Lei7c6c5b72019-04-30 09:52:26 +08003019
3020 free_hctx:
3021 kobject_put(&hctx->kobj);
3022 fail:
3023 return NULL;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003024}
3025
Keith Busch868f2f02015-12-17 17:08:14 -07003026static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
3027 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003028{
Jianchao Wange01ad462018-10-12 18:07:28 +08003029 int i, j, end;
Keith Busch868f2f02015-12-17 17:08:14 -07003030 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01003031
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003032 if (q->nr_hw_queues < set->nr_hw_queues) {
3033 struct blk_mq_hw_ctx **new_hctxs;
3034
3035 new_hctxs = kcalloc_node(set->nr_hw_queues,
3036 sizeof(*new_hctxs), GFP_KERNEL,
3037 set->numa_node);
3038 if (!new_hctxs)
3039 return;
3040 if (hctxs)
3041 memcpy(new_hctxs, hctxs, q->nr_hw_queues *
3042 sizeof(*hctxs));
3043 q->queue_hw_ctx = new_hctxs;
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003044 kfree(hctxs);
3045 hctxs = new_hctxs;
3046 }
3047
Ming Leifb350e02018-01-06 16:27:40 +08003048 /* protect against switching io scheduler */
3049 mutex_lock(&q->sysfs_lock);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003050 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003051 int node;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003052 struct blk_mq_hw_ctx *hctx;
Keith Busch868f2f02015-12-17 17:08:14 -07003053
Dongli Zhang7d76f852019-02-27 21:35:01 +08003054 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003055 /*
3056 * If the hw queue has been mapped to another numa node,
3057 * we need to realloc the hctx. If allocation fails, fallback
3058 * to use the previous one.
3059 */
3060 if (hctxs[i] && (hctxs[i]->numa_node == node))
3061 continue;
Jens Axboe320ae512013-10-24 09:20:05 +01003062
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003063 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
3064 if (hctx) {
Ming Lei2f8f1332019-04-30 09:52:27 +08003065 if (hctxs[i])
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003066 blk_mq_exit_hctx(q, set, hctxs[i], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003067 hctxs[i] = hctx;
3068 } else {
3069 if (hctxs[i])
3070 pr_warn("Allocate new hctx on node %d fails,\
3071 fallback to previous one on node %d\n",
3072 node, hctxs[i]->numa_node);
3073 else
3074 break;
Keith Busch868f2f02015-12-17 17:08:14 -07003075 }
Jens Axboe320ae512013-10-24 09:20:05 +01003076 }
Jianchao Wange01ad462018-10-12 18:07:28 +08003077 /*
3078 * Increasing nr_hw_queues fails. Free the newly allocated
3079 * hctxs and keep the previous q->nr_hw_queues.
3080 */
3081 if (i != set->nr_hw_queues) {
3082 j = q->nr_hw_queues;
3083 end = i;
3084 } else {
3085 j = i;
3086 end = q->nr_hw_queues;
3087 q->nr_hw_queues = set->nr_hw_queues;
3088 }
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003089
Jianchao Wange01ad462018-10-12 18:07:28 +08003090 for (; j < end; j++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003091 struct blk_mq_hw_ctx *hctx = hctxs[j];
3092
3093 if (hctx) {
Jens Axboecc71a6f2017-01-11 14:29:56 -07003094 if (hctx->tags)
3095 blk_mq_free_map_and_requests(set, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003096 blk_mq_exit_hctx(q, set, hctx, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003097 hctxs[j] = NULL;
Keith Busch868f2f02015-12-17 17:08:14 -07003098 }
3099 }
Ming Leifb350e02018-01-06 16:27:40 +08003100 mutex_unlock(&q->sysfs_lock);
Keith Busch868f2f02015-12-17 17:08:14 -07003101}
3102
3103struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
Damien Le Moal737eb782019-09-05 18:51:33 +09003104 struct request_queue *q,
3105 bool elevator_init)
Keith Busch868f2f02015-12-17 17:08:14 -07003106{
Ming Lei66841672016-02-12 15:27:00 +08003107 /* mark the queue as mq asap */
3108 q->mq_ops = set->ops;
3109
Omar Sandoval34dbad52017-03-21 08:56:08 -07003110 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
Stephen Bates720b8cc2017-04-07 06:24:03 -06003111 blk_mq_poll_stats_bkt,
3112 BLK_MQ_POLL_STATS_BKTS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -07003113 if (!q->poll_cb)
3114 goto err_exit;
3115
Ming Lei1db49092018-11-20 09:44:35 +08003116 if (blk_mq_alloc_ctxs(q))
Jes Sorensen41de54c2019-04-19 16:35:44 -04003117 goto err_poll;
Keith Busch868f2f02015-12-17 17:08:14 -07003118
Ming Lei737f98c2017-02-22 18:13:59 +08003119 /* init q->mq_kobj and sw queues' kobjects */
3120 blk_mq_sysfs_init(q);
3121
Ming Lei2f8f1332019-04-30 09:52:27 +08003122 INIT_LIST_HEAD(&q->unused_hctx_list);
3123 spin_lock_init(&q->unused_hctx_lock);
3124
Keith Busch868f2f02015-12-17 17:08:14 -07003125 blk_mq_realloc_hw_ctxs(set, q);
3126 if (!q->nr_hw_queues)
3127 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01003128
Christoph Hellwig287922e2015-10-30 20:57:30 +08003129 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08003130 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01003131
Jens Axboea8908932018-10-16 14:23:06 -06003132 q->tag_set = set;
Jens Axboe320ae512013-10-24 09:20:05 +01003133
Jens Axboe94eddfb2013-11-19 09:25:07 -07003134 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Ming Leicd191812018-12-18 12:15:29 +08003135 if (set->nr_maps > HCTX_TYPE_POLL &&
3136 set->map[HCTX_TYPE_POLL].nr_queues)
Christoph Hellwig6544d222018-12-02 17:46:28 +01003137 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
Jens Axboe320ae512013-10-24 09:20:05 +01003138
Christoph Hellwig1be036e2014-02-07 10:22:39 -08003139 q->sg_reserved_size = INT_MAX;
3140
Mike Snitzer28494502016-09-14 13:28:30 -04003141 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06003142 INIT_LIST_HEAD(&q->requeue_list);
3143 spin_lock_init(&q->requeue_lock);
3144
Jens Axboeeba71762014-05-20 15:17:27 -06003145 q->nr_requests = set->queue_depth;
3146
Jens Axboe64f1c212016-11-14 13:03:03 -07003147 /*
3148 * Default to classic polling
3149 */
Yufen Yu29ece8b2019-03-18 22:44:41 +08003150 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
Jens Axboe64f1c212016-11-14 13:03:03 -07003151
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003152 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003153 blk_mq_add_queue_tag_set(set, q);
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003154 blk_mq_map_swqueue(q);
Akinobu Mita4593fdb2015-09-27 02:09:20 +09003155
Damien Le Moal737eb782019-09-05 18:51:33 +09003156 if (elevator_init)
3157 elevator_init_mq(q);
Jens Axboed3484992017-01-13 14:43:58 -07003158
Jens Axboe320ae512013-10-24 09:20:05 +01003159 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07003160
Jens Axboe320ae512013-10-24 09:20:05 +01003161err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07003162 kfree(q->queue_hw_ctx);
zhengbin73d9c8d2019-07-23 22:10:42 +08003163 q->nr_hw_queues = 0;
Ming Lei1db49092018-11-20 09:44:35 +08003164 blk_mq_sysfs_deinit(q);
Jes Sorensen41de54c2019-04-19 16:35:44 -04003165err_poll:
3166 blk_stat_free_callback(q->poll_cb);
3167 q->poll_cb = NULL;
Ming Linc7de5722016-05-25 23:23:27 -07003168err_exit:
3169 q->mq_ops = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01003170 return ERR_PTR(-ENOMEM);
3171}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003172EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01003173
Ming Leic7e2d942019-04-30 09:52:25 +08003174/* tags can _not_ be used after returning from blk_mq_exit_queue */
3175void blk_mq_exit_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01003176{
Ming Lei624dbe42014-05-27 23:35:13 +08003177 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01003178
Jens Axboe0d2602c2014-05-13 15:10:52 -06003179 blk_mq_del_queue_tag_set(q);
Ming Lei624dbe42014-05-27 23:35:13 +08003180 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01003181}
Jens Axboe320ae512013-10-24 09:20:05 +01003182
Jens Axboea5164402014-09-10 09:02:03 -06003183static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
3184{
3185 int i;
3186
Jens Axboecc71a6f2017-01-11 14:29:56 -07003187 for (i = 0; i < set->nr_hw_queues; i++)
Weiping Zhang03b63b02020-05-07 21:04:22 +08003188 if (!__blk_mq_alloc_map_and_request(set, i))
Jens Axboea5164402014-09-10 09:02:03 -06003189 goto out_unwind;
Jens Axboea5164402014-09-10 09:02:03 -06003190
3191 return 0;
3192
3193out_unwind:
3194 while (--i >= 0)
Weiping Zhang2e194422020-05-07 21:03:39 +08003195 blk_mq_free_map_and_requests(set, i);
Jens Axboea5164402014-09-10 09:02:03 -06003196
Jens Axboea5164402014-09-10 09:02:03 -06003197 return -ENOMEM;
3198}
3199
3200/*
3201 * Allocate the request maps associated with this tag_set. Note that this
3202 * may reduce the depth asked for, if memory is tight. set->queue_depth
3203 * will be updated to reflect the allocated depth.
3204 */
Weiping Zhang79fab522020-05-07 21:04:42 +08003205static int blk_mq_alloc_map_and_requests(struct blk_mq_tag_set *set)
Jens Axboea5164402014-09-10 09:02:03 -06003206{
3207 unsigned int depth;
3208 int err;
3209
3210 depth = set->queue_depth;
3211 do {
3212 err = __blk_mq_alloc_rq_maps(set);
3213 if (!err)
3214 break;
3215
3216 set->queue_depth >>= 1;
3217 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
3218 err = -ENOMEM;
3219 break;
3220 }
3221 } while (set->queue_depth);
3222
3223 if (!set->queue_depth || err) {
3224 pr_err("blk-mq: failed to allocate request map\n");
3225 return -ENOMEM;
3226 }
3227
3228 if (depth != set->queue_depth)
3229 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
3230 depth, set->queue_depth);
3231
3232 return 0;
3233}
3234
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003235static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
3236{
Bart Van Assche6e66b492020-03-09 21:26:17 -07003237 /*
3238 * blk_mq_map_queues() and multiple .map_queues() implementations
3239 * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the
3240 * number of hardware queues.
3241 */
3242 if (set->nr_maps == 1)
3243 set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues;
3244
Ming Lei59388702018-12-07 11:03:53 +08003245 if (set->ops->map_queues && !is_kdump_kernel()) {
Jens Axboeb3c661b2018-10-30 10:36:06 -06003246 int i;
3247
Ming Lei7d4901a2018-01-06 16:27:39 +08003248 /*
3249 * transport .map_queues is usually done in the following
3250 * way:
3251 *
3252 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
3253 * mask = get_cpu_mask(queue)
3254 * for_each_cpu(cpu, mask)
Jens Axboeb3c661b2018-10-30 10:36:06 -06003255 * set->map[x].mq_map[cpu] = queue;
Ming Lei7d4901a2018-01-06 16:27:39 +08003256 * }
3257 *
3258 * When we need to remap, the table has to be cleared for
3259 * killing stale mapping since one CPU may not be mapped
3260 * to any hw queue.
3261 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06003262 for (i = 0; i < set->nr_maps; i++)
3263 blk_mq_clear_mq_map(&set->map[i]);
Ming Lei7d4901a2018-01-06 16:27:39 +08003264
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003265 return set->ops->map_queues(set);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003266 } else {
3267 BUG_ON(set->nr_maps > 1);
Dongli Zhang7d76f852019-02-27 21:35:01 +08003268 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003269 }
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003270}
3271
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003272static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set,
3273 int cur_nr_hw_queues, int new_nr_hw_queues)
3274{
3275 struct blk_mq_tags **new_tags;
3276
3277 if (cur_nr_hw_queues >= new_nr_hw_queues)
3278 return 0;
3279
3280 new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *),
3281 GFP_KERNEL, set->numa_node);
3282 if (!new_tags)
3283 return -ENOMEM;
3284
3285 if (set->tags)
3286 memcpy(new_tags, set->tags, cur_nr_hw_queues *
3287 sizeof(*set->tags));
3288 kfree(set->tags);
3289 set->tags = new_tags;
3290 set->nr_hw_queues = new_nr_hw_queues;
3291
3292 return 0;
3293}
3294
Jens Axboea4391c62014-06-05 15:21:56 -06003295/*
3296 * Alloc a tag set to be associated with one or more request queues.
3297 * May fail with EINVAL for various error conditions. May adjust the
Minwoo Imc018c842018-06-30 22:12:41 +09003298 * requested depth down, if it's too large. In that case, the set
Jens Axboea4391c62014-06-05 15:21:56 -06003299 * value will be stored in set->queue_depth.
3300 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003301int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
3302{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003303 int i, ret;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003304
Bart Van Assche205fb5f2014-10-30 14:45:11 +01003305 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
3306
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003307 if (!set->nr_hw_queues)
3308 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06003309 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003310 return -EINVAL;
3311 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
3312 return -EINVAL;
3313
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02003314 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003315 return -EINVAL;
3316
Ming Leide148292017-10-14 17:22:29 +08003317 if (!set->ops->get_budget ^ !set->ops->put_budget)
3318 return -EINVAL;
3319
Jens Axboea4391c62014-06-05 15:21:56 -06003320 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
3321 pr_info("blk-mq: reduced tag depth to %u\n",
3322 BLK_MQ_MAX_DEPTH);
3323 set->queue_depth = BLK_MQ_MAX_DEPTH;
3324 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003325
Jens Axboeb3c661b2018-10-30 10:36:06 -06003326 if (!set->nr_maps)
3327 set->nr_maps = 1;
3328 else if (set->nr_maps > HCTX_MAX_TYPES)
3329 return -EINVAL;
3330
Shaohua Li6637fad2014-11-30 16:00:58 -08003331 /*
3332 * If a crashdump is active, then we are potentially in a very
3333 * memory constrained environment. Limit us to 1 queue and
3334 * 64 tags to prevent using too much memory.
3335 */
3336 if (is_kdump_kernel()) {
3337 set->nr_hw_queues = 1;
Ming Lei59388702018-12-07 11:03:53 +08003338 set->nr_maps = 1;
Shaohua Li6637fad2014-11-30 16:00:58 -08003339 set->queue_depth = min(64U, set->queue_depth);
3340 }
Keith Busch868f2f02015-12-17 17:08:14 -07003341 /*
Jens Axboe392546a2018-10-29 13:25:27 -06003342 * There is no use for more h/w queues than cpus if we just have
3343 * a single map
Keith Busch868f2f02015-12-17 17:08:14 -07003344 */
Jens Axboe392546a2018-10-29 13:25:27 -06003345 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003346 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08003347
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003348 if (blk_mq_realloc_tag_set_tags(set, 0, set->nr_hw_queues) < 0)
Jens Axboea5164402014-09-10 09:02:03 -06003349 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003350
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003351 ret = -ENOMEM;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003352 for (i = 0; i < set->nr_maps; i++) {
3353 set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
Ming Lei07b35eb2018-12-17 18:42:45 +08003354 sizeof(set->map[i].mq_map[0]),
Jens Axboeb3c661b2018-10-30 10:36:06 -06003355 GFP_KERNEL, set->numa_node);
3356 if (!set->map[i].mq_map)
3357 goto out_free_mq_map;
Ming Lei59388702018-12-07 11:03:53 +08003358 set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003359 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003360
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003361 ret = blk_mq_update_queue_map(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003362 if (ret)
3363 goto out_free_mq_map;
3364
Weiping Zhang79fab522020-05-07 21:04:42 +08003365 ret = blk_mq_alloc_map_and_requests(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003366 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003367 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003368
Jens Axboe0d2602c2014-05-13 15:10:52 -06003369 mutex_init(&set->tag_list_lock);
3370 INIT_LIST_HEAD(&set->tag_list);
3371
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003372 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003373
3374out_free_mq_map:
Jens Axboeb3c661b2018-10-30 10:36:06 -06003375 for (i = 0; i < set->nr_maps; i++) {
3376 kfree(set->map[i].mq_map);
3377 set->map[i].mq_map = NULL;
3378 }
Robert Elliott5676e7b2014-09-02 11:38:44 -05003379 kfree(set->tags);
3380 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003381 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003382}
3383EXPORT_SYMBOL(blk_mq_alloc_tag_set);
3384
3385void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
3386{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003387 int i, j;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003388
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003389 for (i = 0; i < set->nr_hw_queues; i++)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003390 blk_mq_free_map_and_requests(set, i);
Jens Axboe484b4062014-05-21 14:01:15 -06003391
Jens Axboeb3c661b2018-10-30 10:36:06 -06003392 for (j = 0; j < set->nr_maps; j++) {
3393 kfree(set->map[j].mq_map);
3394 set->map[j].mq_map = NULL;
3395 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003396
Ming Lei981bd182014-04-24 00:07:34 +08003397 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05003398 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003399}
3400EXPORT_SYMBOL(blk_mq_free_tag_set);
3401
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003402int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
3403{
3404 struct blk_mq_tag_set *set = q->tag_set;
3405 struct blk_mq_hw_ctx *hctx;
3406 int i, ret;
3407
Jens Axboebd166ef2017-01-17 06:03:22 -07003408 if (!set)
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003409 return -EINVAL;
3410
Aleksei Zakharove5fa8142019-02-08 19:14:05 +03003411 if (q->nr_requests == nr)
3412 return 0;
3413
Jens Axboe70f36b62017-01-19 10:59:07 -07003414 blk_mq_freeze_queue(q);
Ming Lei24f5a902018-01-06 16:27:38 +08003415 blk_mq_quiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003416
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003417 ret = 0;
3418 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07003419 if (!hctx->tags)
3420 continue;
Jens Axboebd166ef2017-01-17 06:03:22 -07003421 /*
3422 * If we're using an MQ scheduler, just update the scheduler
3423 * queue depth. This is similar to what the old code would do.
3424 */
Jens Axboe70f36b62017-01-19 10:59:07 -07003425 if (!hctx->sched_tags) {
weiping zhangc2e82a22017-09-22 23:36:28 +08003426 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
Jens Axboe70f36b62017-01-19 10:59:07 -07003427 false);
3428 } else {
3429 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
3430 nr, true);
3431 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003432 if (ret)
3433 break;
Jens Axboe77f1e0a2019-01-18 10:34:16 -07003434 if (q->elevator && q->elevator->type->ops.depth_updated)
3435 q->elevator->type->ops.depth_updated(hctx);
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003436 }
3437
3438 if (!ret)
3439 q->nr_requests = nr;
3440
Ming Lei24f5a902018-01-06 16:27:38 +08003441 blk_mq_unquiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003442 blk_mq_unfreeze_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003443
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003444 return ret;
3445}
3446
Jianchao Wangd48ece22018-08-21 15:15:03 +08003447/*
3448 * request_queue and elevator_type pair.
3449 * It is just used by __blk_mq_update_nr_hw_queues to cache
3450 * the elevator_type associated with a request_queue.
3451 */
3452struct blk_mq_qe_pair {
3453 struct list_head node;
3454 struct request_queue *q;
3455 struct elevator_type *type;
3456};
3457
3458/*
3459 * Cache the elevator_type in qe pair list and switch the
3460 * io scheduler to 'none'
3461 */
3462static bool blk_mq_elv_switch_none(struct list_head *head,
3463 struct request_queue *q)
3464{
3465 struct blk_mq_qe_pair *qe;
3466
3467 if (!q->elevator)
3468 return true;
3469
3470 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
3471 if (!qe)
3472 return false;
3473
3474 INIT_LIST_HEAD(&qe->node);
3475 qe->q = q;
3476 qe->type = q->elevator->type;
3477 list_add(&qe->node, head);
3478
3479 mutex_lock(&q->sysfs_lock);
3480 /*
3481 * After elevator_switch_mq, the previous elevator_queue will be
3482 * released by elevator_release. The reference of the io scheduler
3483 * module get by elevator_get will also be put. So we need to get
3484 * a reference of the io scheduler module here to prevent it to be
3485 * removed.
3486 */
3487 __module_get(qe->type->elevator_owner);
3488 elevator_switch_mq(q, NULL);
3489 mutex_unlock(&q->sysfs_lock);
3490
3491 return true;
3492}
3493
3494static void blk_mq_elv_switch_back(struct list_head *head,
3495 struct request_queue *q)
3496{
3497 struct blk_mq_qe_pair *qe;
3498 struct elevator_type *t = NULL;
3499
3500 list_for_each_entry(qe, head, node)
3501 if (qe->q == q) {
3502 t = qe->type;
3503 break;
3504 }
3505
3506 if (!t)
3507 return;
3508
3509 list_del(&qe->node);
3510 kfree(qe);
3511
3512 mutex_lock(&q->sysfs_lock);
3513 elevator_switch_mq(q, t);
3514 mutex_unlock(&q->sysfs_lock);
3515}
3516
Keith Busche4dc2b32017-05-30 14:39:11 -04003517static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
3518 int nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003519{
3520 struct request_queue *q;
Jianchao Wangd48ece22018-08-21 15:15:03 +08003521 LIST_HEAD(head);
Jianchao Wange01ad462018-10-12 18:07:28 +08003522 int prev_nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07003523
Bart Van Assche705cda92017-04-07 11:16:49 -07003524 lockdep_assert_held(&set->tag_list_lock);
3525
Jens Axboe392546a2018-10-29 13:25:27 -06003526 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003527 nr_hw_queues = nr_cpu_ids;
Weiping Zhangfe35ec52020-06-17 14:18:37 +08003528 if (nr_hw_queues < 1)
3529 return;
3530 if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003531 return;
3532
3533 list_for_each_entry(q, &set->tag_list, tag_set_list)
3534 blk_mq_freeze_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +08003535 /*
3536 * Switch IO scheduler to 'none', cleaning up the data associated
3537 * with the previous scheduler. We will switch back once we are done
3538 * updating the new sw to hw queue mappings.
3539 */
3540 list_for_each_entry(q, &set->tag_list, tag_set_list)
3541 if (!blk_mq_elv_switch_none(&head, q))
3542 goto switch_back;
Keith Busch868f2f02015-12-17 17:08:14 -07003543
Jianchao Wang477e19d2018-10-12 18:07:25 +08003544 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3545 blk_mq_debugfs_unregister_hctxs(q);
3546 blk_mq_sysfs_unregister(q);
3547 }
3548
Weiping Zhanga2584e42020-05-07 21:03:56 +08003549 prev_nr_hw_queues = set->nr_hw_queues;
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003550 if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) <
3551 0)
3552 goto reregister;
3553
Keith Busch868f2f02015-12-17 17:08:14 -07003554 set->nr_hw_queues = nr_hw_queues;
Jianchao Wange01ad462018-10-12 18:07:28 +08003555fallback:
Weiping Zhangaa880ad2020-05-13 08:44:05 +08003556 blk_mq_update_queue_map(set);
Keith Busch868f2f02015-12-17 17:08:14 -07003557 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3558 blk_mq_realloc_hw_ctxs(set, q);
Jianchao Wange01ad462018-10-12 18:07:28 +08003559 if (q->nr_hw_queues != set->nr_hw_queues) {
3560 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
3561 nr_hw_queues, prev_nr_hw_queues);
3562 set->nr_hw_queues = prev_nr_hw_queues;
Dongli Zhang7d76f852019-02-27 21:35:01 +08003563 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jianchao Wange01ad462018-10-12 18:07:28 +08003564 goto fallback;
3565 }
Jianchao Wang477e19d2018-10-12 18:07:25 +08003566 blk_mq_map_swqueue(q);
3567 }
3568
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003569reregister:
Jianchao Wang477e19d2018-10-12 18:07:25 +08003570 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3571 blk_mq_sysfs_register(q);
3572 blk_mq_debugfs_register_hctxs(q);
Keith Busch868f2f02015-12-17 17:08:14 -07003573 }
3574
Jianchao Wangd48ece22018-08-21 15:15:03 +08003575switch_back:
3576 list_for_each_entry(q, &set->tag_list, tag_set_list)
3577 blk_mq_elv_switch_back(&head, q);
3578
Keith Busch868f2f02015-12-17 17:08:14 -07003579 list_for_each_entry(q, &set->tag_list, tag_set_list)
3580 blk_mq_unfreeze_queue(q);
3581}
Keith Busche4dc2b32017-05-30 14:39:11 -04003582
3583void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
3584{
3585 mutex_lock(&set->tag_list_lock);
3586 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
3587 mutex_unlock(&set->tag_list_lock);
3588}
Keith Busch868f2f02015-12-17 17:08:14 -07003589EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
3590
Omar Sandoval34dbad52017-03-21 08:56:08 -07003591/* Enable polling stats and return whether they were already enabled. */
3592static bool blk_poll_stats_enable(struct request_queue *q)
3593{
3594 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
Bart Van Assche7dfdbc72018-03-07 17:10:05 -08003595 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
Omar Sandoval34dbad52017-03-21 08:56:08 -07003596 return true;
3597 blk_stat_add_callback(q, q->poll_cb);
3598 return false;
3599}
3600
3601static void blk_mq_poll_stats_start(struct request_queue *q)
3602{
3603 /*
3604 * We don't arm the callback if polling stats are not enabled or the
3605 * callback is already active.
3606 */
3607 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
3608 blk_stat_is_active(q->poll_cb))
3609 return;
3610
3611 blk_stat_activate_msecs(q->poll_cb, 100);
3612}
3613
3614static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
3615{
3616 struct request_queue *q = cb->data;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003617 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -07003618
Stephen Bates720b8cc2017-04-07 06:24:03 -06003619 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
3620 if (cb->stat[bucket].nr_samples)
3621 q->poll_stat[bucket] = cb->stat[bucket];
3622 }
Omar Sandoval34dbad52017-03-21 08:56:08 -07003623}
3624
Jens Axboe64f1c212016-11-14 13:03:03 -07003625static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
Jens Axboe64f1c212016-11-14 13:03:03 -07003626 struct request *rq)
3627{
Jens Axboe64f1c212016-11-14 13:03:03 -07003628 unsigned long ret = 0;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003629 int bucket;
Jens Axboe64f1c212016-11-14 13:03:03 -07003630
3631 /*
3632 * If stats collection isn't on, don't sleep but turn it on for
3633 * future users
3634 */
Omar Sandoval34dbad52017-03-21 08:56:08 -07003635 if (!blk_poll_stats_enable(q))
Jens Axboe64f1c212016-11-14 13:03:03 -07003636 return 0;
3637
3638 /*
Jens Axboe64f1c212016-11-14 13:03:03 -07003639 * As an optimistic guess, use half of the mean service time
3640 * for this type of request. We can (and should) make this smarter.
3641 * For instance, if the completion latencies are tight, we can
3642 * get closer than just half the mean. This is especially
3643 * important on devices where the completion latencies are longer
Stephen Bates720b8cc2017-04-07 06:24:03 -06003644 * than ~10 usec. We do use the stats for the relevant IO size
3645 * if available which does lead to better estimates.
Jens Axboe64f1c212016-11-14 13:03:03 -07003646 */
Stephen Bates720b8cc2017-04-07 06:24:03 -06003647 bucket = blk_mq_poll_stats_bkt(rq);
3648 if (bucket < 0)
3649 return ret;
3650
3651 if (q->poll_stat[bucket].nr_samples)
3652 ret = (q->poll_stat[bucket].mean + 1) / 2;
Jens Axboe64f1c212016-11-14 13:03:03 -07003653
3654 return ret;
3655}
3656
Jens Axboe06426ad2016-11-14 13:01:59 -07003657static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
3658 struct request *rq)
3659{
3660 struct hrtimer_sleeper hs;
3661 enum hrtimer_mode mode;
Jens Axboe64f1c212016-11-14 13:03:03 -07003662 unsigned int nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003663 ktime_t kt;
3664
Jens Axboe76a86f92018-01-10 11:30:56 -07003665 if (rq->rq_flags & RQF_MQ_POLL_SLEPT)
Jens Axboe64f1c212016-11-14 13:03:03 -07003666 return false;
3667
3668 /*
Jens Axboe1052b8a2018-11-26 08:21:49 -07003669 * If we get here, hybrid polling is enabled. Hence poll_nsec can be:
Jens Axboe64f1c212016-11-14 13:03:03 -07003670 *
Jens Axboe64f1c212016-11-14 13:03:03 -07003671 * 0: use half of prev avg
3672 * >0: use this specific value
3673 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07003674 if (q->poll_nsec > 0)
Jens Axboe64f1c212016-11-14 13:03:03 -07003675 nsecs = q->poll_nsec;
3676 else
John Garrycae740a2020-02-26 20:10:15 +08003677 nsecs = blk_mq_poll_nsecs(q, rq);
Jens Axboe64f1c212016-11-14 13:03:03 -07003678
3679 if (!nsecs)
Jens Axboe06426ad2016-11-14 13:01:59 -07003680 return false;
3681
Jens Axboe76a86f92018-01-10 11:30:56 -07003682 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
Jens Axboe06426ad2016-11-14 13:01:59 -07003683
3684 /*
3685 * This will be replaced with the stats tracking code, using
3686 * 'avg_completion_time / 2' as the pre-sleep target.
3687 */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01003688 kt = nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003689
3690 mode = HRTIMER_MODE_REL;
Sebastian Andrzej Siewiordbc16252019-07-26 20:30:50 +02003691 hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07003692 hrtimer_set_expires(&hs.timer, kt);
3693
Jens Axboe06426ad2016-11-14 13:01:59 -07003694 do {
Tejun Heo5a61c362018-01-09 08:29:52 -08003695 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
Jens Axboe06426ad2016-11-14 13:01:59 -07003696 break;
3697 set_current_state(TASK_UNINTERRUPTIBLE);
Thomas Gleixner9dd88132019-07-30 21:16:55 +02003698 hrtimer_sleeper_start_expires(&hs, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07003699 if (hs.task)
3700 io_schedule();
3701 hrtimer_cancel(&hs.timer);
3702 mode = HRTIMER_MODE_ABS;
3703 } while (hs.task && !signal_pending(current));
3704
3705 __set_current_state(TASK_RUNNING);
3706 destroy_hrtimer_on_stack(&hs.timer);
3707 return true;
3708}
3709
Jens Axboe1052b8a2018-11-26 08:21:49 -07003710static bool blk_mq_poll_hybrid(struct request_queue *q,
3711 struct blk_mq_hw_ctx *hctx, blk_qc_t cookie)
Jens Axboebbd7bb72016-11-04 09:34:34 -06003712{
Jens Axboe1052b8a2018-11-26 08:21:49 -07003713 struct request *rq;
3714
Yufen Yu29ece8b2019-03-18 22:44:41 +08003715 if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
Jens Axboe1052b8a2018-11-26 08:21:49 -07003716 return false;
3717
3718 if (!blk_qc_t_is_internal(cookie))
3719 rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
3720 else {
3721 rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
3722 /*
3723 * With scheduling, if the request has completed, we'll
3724 * get a NULL return here, as we clear the sched tag when
3725 * that happens. The request still remains valid, like always,
3726 * so we should be safe with just the NULL check.
3727 */
3728 if (!rq)
3729 return false;
3730 }
3731
John Garrycae740a2020-02-26 20:10:15 +08003732 return blk_mq_poll_hybrid_sleep(q, rq);
Jens Axboe1052b8a2018-11-26 08:21:49 -07003733}
3734
Christoph Hellwig529262d2018-12-02 17:46:26 +01003735/**
3736 * blk_poll - poll for IO completions
3737 * @q: the queue
3738 * @cookie: cookie passed back at IO submission time
3739 * @spin: whether to spin for completions
3740 *
3741 * Description:
3742 * Poll for completions on the passed in queue. Returns number of
3743 * completed entries found. If @spin is true, then blk_poll will continue
3744 * looping until at least one completion is found, unless the task is
3745 * otherwise marked running (or we need to reschedule).
3746 */
3747int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin)
Jens Axboe1052b8a2018-11-26 08:21:49 -07003748{
3749 struct blk_mq_hw_ctx *hctx;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003750 long state;
3751
Christoph Hellwig529262d2018-12-02 17:46:26 +01003752 if (!blk_qc_t_valid(cookie) ||
3753 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
Jens Axboe1052b8a2018-11-26 08:21:49 -07003754 return 0;
3755
Christoph Hellwig529262d2018-12-02 17:46:26 +01003756 if (current->plug)
3757 blk_flush_plug_list(current->plug, false);
3758
Jens Axboe1052b8a2018-11-26 08:21:49 -07003759 hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
3760
Jens Axboe06426ad2016-11-14 13:01:59 -07003761 /*
3762 * If we sleep, have the caller restart the poll loop to reset
3763 * the state. Like for the other success return cases, the
3764 * caller is responsible for checking if the IO completed. If
3765 * the IO isn't complete, we'll get called again and will go
3766 * straight to the busy poll loop.
3767 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07003768 if (blk_mq_poll_hybrid(q, hctx, cookie))
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003769 return 1;
Jens Axboe06426ad2016-11-14 13:01:59 -07003770
Jens Axboebbd7bb72016-11-04 09:34:34 -06003771 hctx->poll_considered++;
3772
3773 state = current->state;
Jens Axboeaa61bec2018-11-13 21:32:10 -07003774 do {
Jens Axboebbd7bb72016-11-04 09:34:34 -06003775 int ret;
3776
3777 hctx->poll_invoked++;
3778
Jens Axboe97431392018-11-16 09:48:21 -07003779 ret = q->mq_ops->poll(hctx);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003780 if (ret > 0) {
3781 hctx->poll_success++;
Jens Axboe849a3702018-11-16 08:37:34 -07003782 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003783 return ret;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003784 }
3785
3786 if (signal_pending_state(state, current))
Jens Axboe849a3702018-11-16 08:37:34 -07003787 __set_current_state(TASK_RUNNING);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003788
3789 if (current->state == TASK_RUNNING)
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003790 return 1;
Jens Axboe0a1b8b82018-11-26 08:24:43 -07003791 if (ret < 0 || !spin)
Jens Axboebbd7bb72016-11-04 09:34:34 -06003792 break;
3793 cpu_relax();
Jens Axboeaa61bec2018-11-13 21:32:10 -07003794 } while (!need_resched());
Jens Axboebbd7bb72016-11-04 09:34:34 -06003795
Nitesh Shetty67b41102018-02-13 21:18:12 +05303796 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003797 return 0;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003798}
Christoph Hellwig529262d2018-12-02 17:46:26 +01003799EXPORT_SYMBOL_GPL(blk_poll);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003800
Jens Axboe9cf2bab2018-10-31 17:01:22 -06003801unsigned int blk_mq_rq_cpu(struct request *rq)
3802{
3803 return rq->mq_ctx->cpu;
3804}
3805EXPORT_SYMBOL(blk_mq_rq_cpu);
3806
Jens Axboe320ae512013-10-24 09:20:05 +01003807static int __init blk_mq_init(void)
3808{
Christoph Hellwigc3077b52020-06-11 08:44:41 +02003809 int i;
3810
3811 for_each_possible_cpu(i)
3812 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
3813 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
3814
3815 cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
3816 "block/softirq:dead", NULL,
3817 blk_softirq_cpu_dead);
Thomas Gleixner9467f852016-09-22 08:05:17 -06003818 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
3819 blk_mq_hctx_notify_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02003820 cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online",
3821 blk_mq_hctx_notify_online,
3822 blk_mq_hctx_notify_offline);
Jens Axboe320ae512013-10-24 09:20:05 +01003823 return 0;
3824}
3825subsys_initcall(blk_mq_init);