blob: 1b701e1666818139fe25880e4c6cfd12dd0ace58 [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 Busch6bae3632017-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 Busch6bae3632017-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 Hellwig42fdc5e2020-06-29 17:08:34 +0200282 if (data->q->elevator) {
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) {
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200367 /*
368 * Flush requests are special and go directly to the
Jens Axboe17a51192018-05-09 13:28:50 -0600369 * dispatch list. Don't include reserved tags in the
370 * limiting, as it isn't useful.
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200371 */
Jens Axboef9afca42018-10-29 13:11:38 -0600372 if (!op_is_flush(data->cmd_flags) &&
373 e->type->ops.limit_depth &&
Jens Axboe17a51192018-05-09 13:28:50 -0600374 !(data->flags & BLK_MQ_REQ_RESERVED))
Jens Axboef9afca42018-10-29 13:11:38 -0600375 e->type->ops.limit_depth(data->cmd_flags, data);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200376 }
377
Ming Leibf0beec2020-05-29 15:53:15 +0200378retry:
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200379 data->ctx = blk_mq_get_ctx(q);
380 data->hctx = blk_mq_map_queue(q, data->cmd_flags, data->ctx);
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200381 if (!e)
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200382 blk_mq_tag_busy(data->hctx);
383
Ming Leibf0beec2020-05-29 15:53:15 +0200384 /*
385 * Waiting allocations only fail because of an inactive hctx. In that
386 * case just retry the hctx assignment and tag allocation as CPU hotplug
387 * should have migrated us to an online CPU by now.
388 */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200389 tag = blk_mq_get_tag(data);
Ming Leibf0beec2020-05-29 15:53:15 +0200390 if (tag == BLK_MQ_NO_TAG) {
391 if (data->flags & BLK_MQ_REQ_NOWAIT)
392 return NULL;
393
394 /*
395 * Give up the CPU and sleep for a random short time to ensure
396 * that thread using a realtime scheduling class are migrated
397 * off the the CPU, and thus off the hctx that is going away.
398 */
399 msleep(3);
400 goto retry;
401 }
Christoph Hellwig7ea4d8a2020-05-29 15:53:10 +0200402 return blk_mq_rq_ctx_init(data, tag, alloc_time_ns);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200403}
404
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700405struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800406 blk_mq_req_flags_t flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100407{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200408 struct blk_mq_alloc_data data = {
409 .q = q,
410 .flags = flags,
411 .cmd_flags = op,
412 };
Jens Axboebd166ef2017-01-17 06:03:22 -0700413 struct request *rq;
Joe Lawrencea492f072014-08-28 08:15:21 -0600414 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100415
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800416 ret = blk_queue_enter(q, flags);
Joe Lawrencea492f072014-08-28 08:15:21 -0600417 if (ret)
418 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100419
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200420 rq = __blk_mq_alloc_request(&data);
Jens Axboebd166ef2017-01-17 06:03:22 -0700421 if (!rq)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200422 goto out_queue_exit;
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200423 rq->__data_len = 0;
424 rq->__sector = (sector_t) -1;
425 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100426 return rq;
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200427out_queue_exit:
428 blk_queue_exit(q);
429 return ERR_PTR(-EWOULDBLOCK);
Jens Axboe320ae512013-10-24 09:20:05 +0100430}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600431EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100432
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700433struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800434 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
Ming Lin1f5bd332016-06-13 16:45:21 +0200435{
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200436 struct blk_mq_alloc_data data = {
437 .q = q,
438 .flags = flags,
439 .cmd_flags = op,
440 };
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200441 u64 alloc_time_ns = 0;
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800442 unsigned int cpu;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200443 unsigned int tag;
Ming Lin1f5bd332016-06-13 16:45:21 +0200444 int ret;
445
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200446 /* alloc_time includes depth and tag waits */
447 if (blk_queue_rq_alloc_time(q))
448 alloc_time_ns = ktime_get_ns();
449
Ming Lin1f5bd332016-06-13 16:45:21 +0200450 /*
451 * If the tag allocator sleeps we could get an allocation for a
452 * different hardware context. No need to complicate the low level
453 * allocator for this for the rare use case of a command tied to
454 * a specific queue.
455 */
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200456 if (WARN_ON_ONCE(!(flags & (BLK_MQ_REQ_NOWAIT | BLK_MQ_REQ_RESERVED))))
Ming Lin1f5bd332016-06-13 16:45:21 +0200457 return ERR_PTR(-EINVAL);
458
459 if (hctx_idx >= q->nr_hw_queues)
460 return ERR_PTR(-EIO);
461
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800462 ret = blk_queue_enter(q, flags);
Ming Lin1f5bd332016-06-13 16:45:21 +0200463 if (ret)
464 return ERR_PTR(ret);
465
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600466 /*
467 * Check if the hardware context is actually mapped to anything.
468 * If not tell the caller that it should skip this queue.
469 */
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200470 ret = -EXDEV;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200471 data.hctx = q->queue_hw_ctx[hctx_idx];
472 if (!blk_mq_hw_queue_mapped(data.hctx))
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200473 goto out_queue_exit;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +0200474 cpu = cpumask_first_and(data.hctx->cpumask, cpu_online_mask);
475 data.ctx = __blk_mq_get_ctx(q, cpu);
Ming Lin1f5bd332016-06-13 16:45:21 +0200476
Christoph Hellwig42fdc5e2020-06-29 17:08:34 +0200477 if (!q->elevator)
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200478 blk_mq_tag_busy(data.hctx);
479
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200480 ret = -EWOULDBLOCK;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200481 tag = blk_mq_get_tag(&data);
482 if (tag == BLK_MQ_NO_TAG)
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200483 goto out_queue_exit;
Christoph Hellwig600c3b02020-05-29 15:53:13 +0200484 return blk_mq_rq_ctx_init(&data, tag, alloc_time_ns);
485
Christoph Hellwiga5ea581102020-05-16 20:27:58 +0200486out_queue_exit:
487 blk_queue_exit(q);
488 return ERR_PTR(ret);
Ming Lin1f5bd332016-06-13 16:45:21 +0200489}
490EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
491
Keith Busch12f5b932018-05-29 15:52:28 +0200492static void __blk_mq_free_request(struct request *rq)
493{
494 struct request_queue *q = rq->q;
495 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600496 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Keith Busch12f5b932018-05-29 15:52:28 +0200497 const int sched_tag = rq->internal_tag;
498
Satya Tangiralaa892c8d2020-05-14 00:37:18 +0000499 blk_crypto_free_request(rq);
Bart Van Assche986d4132018-09-26 14:01:10 -0700500 blk_pm_mark_last_busy(rq);
Jens Axboeea4f9952018-10-29 15:06:13 -0600501 rq->mq_hctx = NULL;
Christoph Hellwig766473682020-05-29 15:53:12 +0200502 if (rq->tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800503 blk_mq_put_tag(hctx->tags, ctx, rq->tag);
Christoph Hellwig766473682020-05-29 15:53:12 +0200504 if (sched_tag != BLK_MQ_NO_TAG)
John Garrycae740a2020-02-26 20:10:15 +0800505 blk_mq_put_tag(hctx->sched_tags, ctx, sched_tag);
Keith Busch12f5b932018-05-29 15:52:28 +0200506 blk_mq_sched_restart(hctx);
507 blk_queue_exit(q);
508}
509
Christoph Hellwig6af54052017-06-16 18:15:22 +0200510void blk_mq_free_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100511{
Jens Axboe320ae512013-10-24 09:20:05 +0100512 struct request_queue *q = rq->q;
Christoph Hellwig6af54052017-06-16 18:15:22 +0200513 struct elevator_queue *e = q->elevator;
514 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600515 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100516
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200517 if (rq->rq_flags & RQF_ELVPRIV) {
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600518 if (e && e->type->ops.finish_request)
519 e->type->ops.finish_request(rq);
Christoph Hellwig6af54052017-06-16 18:15:22 +0200520 if (rq->elv.icq) {
521 put_io_context(rq->elv.icq->ioc);
522 rq->elv.icq = NULL;
523 }
524 }
525
526 ctx->rq_completed[rq_is_sync(rq)]++;
Christoph Hellwige8064022016-10-20 15:12:13 +0200527 if (rq->rq_flags & RQF_MQ_INFLIGHT)
Jens Axboe0d2602c2014-05-13 15:10:52 -0600528 atomic_dec(&hctx->nr_active);
Jens Axboe87760e52016-11-09 12:38:14 -0700529
Jens Axboe7beb2f82017-09-30 02:08:24 -0600530 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
531 laptop_io_completion(q->backing_dev_info);
532
Josef Bacika7905042018-07-03 09:32:35 -0600533 rq_qos_done(q, rq);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600534
Keith Busch12f5b932018-05-29 15:52:28 +0200535 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
536 if (refcount_dec_and_test(&rq->ref))
537 __blk_mq_free_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100538}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700539EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100540
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200541inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
Jens Axboe320ae512013-10-24 09:20:05 +0100542{
Jens Axboefe1f4522018-11-28 10:50:07 -0700543 u64 now = 0;
544
545 if (blk_mq_need_time_stamp(rq))
546 now = ktime_get_ns();
Omar Sandoval522a7772018-05-09 02:08:53 -0700547
Omar Sandoval4bc63392018-05-09 02:08:52 -0700548 if (rq->rq_flags & RQF_STATS) {
549 blk_mq_poll_stats_start(rq->q);
Omar Sandoval522a7772018-05-09 02:08:53 -0700550 blk_stat_add(rq, now);
Omar Sandoval4bc63392018-05-09 02:08:52 -0700551 }
552
Christoph Hellwig766473682020-05-29 15:53:12 +0200553 if (rq->internal_tag != BLK_MQ_NO_TAG)
Omar Sandovaled886602018-09-27 15:55:51 -0700554 blk_mq_sched_completed_request(rq, now);
555
Omar Sandoval522a7772018-05-09 02:08:53 -0700556 blk_account_io_done(rq, now);
Ming Lei0d11e6a2013-12-05 10:50:39 -0700557
Christoph Hellwig91b63632014-04-16 09:44:53 +0200558 if (rq->end_io) {
Josef Bacika7905042018-07-03 09:32:35 -0600559 rq_qos_done(rq->q, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100560 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200561 } else {
Jens Axboe320ae512013-10-24 09:20:05 +0100562 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200563 }
Jens Axboe320ae512013-10-24 09:20:05 +0100564}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700565EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200566
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200567void blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200568{
569 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
570 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700571 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200572}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700573EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100574
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200575/*
576 * Softirq action handler - move entries to local list and loop over them
577 * while passing them to the queue registered handler.
578 */
579static __latent_entropy void blk_done_softirq(struct softirq_action *h)
580{
581 struct list_head *cpu_list, local_list;
582
583 local_irq_disable();
584 cpu_list = this_cpu_ptr(&blk_cpu_done);
585 list_replace_init(cpu_list, &local_list);
586 local_irq_enable();
587
588 while (!list_empty(&local_list)) {
589 struct request *rq;
590
591 rq = list_entry(local_list.next, struct request, ipi_list);
592 list_del_init(&rq->ipi_list);
593 rq->q->mq_ops->complete(rq);
594 }
595}
596
Christoph Hellwig115243f2020-06-11 08:44:42 +0200597static void blk_mq_trigger_softirq(struct request *rq)
598{
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200599 struct list_head *list;
600 unsigned long flags;
Christoph Hellwig115243f2020-06-11 08:44:42 +0200601
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200602 local_irq_save(flags);
603 list = this_cpu_ptr(&blk_cpu_done);
Christoph Hellwig115243f2020-06-11 08:44:42 +0200604 list_add_tail(&rq->ipi_list, list);
605
606 /*
607 * If the list only contains our just added request, signal a raise of
608 * the softirq. If there are already entries there, someone already
609 * raised the irq but it hasn't run yet.
610 */
611 if (list->next == &rq->ipi_list)
612 raise_softirq_irqoff(BLOCK_SOFTIRQ);
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200613 local_irq_restore(flags);
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200614}
615
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200616static int blk_softirq_cpu_dead(unsigned int cpu)
617{
618 /*
619 * If a CPU goes away, splice its entries to the current CPU
620 * and trigger a run of the softirq
621 */
622 local_irq_disable();
623 list_splice_init(&per_cpu(blk_cpu_done, cpu),
624 this_cpu_ptr(&blk_cpu_done));
625 raise_softirq_irqoff(BLOCK_SOFTIRQ);
626 local_irq_enable();
627
628 return 0;
629}
630
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200631
632static void __blk_mq_complete_request_remote(void *data)
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200633{
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200634 struct request *rq = data;
635
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200636 /*
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200637 * For most of single queue controllers, there is only one irq vector
638 * for handling I/O completion, and the only irq's affinity is set
639 * to all possible CPUs. On most of ARCHs, this affinity means the irq
640 * is handled on one specific CPU.
641 *
642 * So complete I/O requests in softirq context in case of single queue
643 * devices to avoid degrading I/O performance due to irqsoff latency.
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200644 */
Christoph Hellwigd391a7a2020-06-11 08:44:46 +0200645 if (rq->q->nr_hw_queues == 1)
646 blk_mq_trigger_softirq(rq);
647 else
648 rq->q->mq_ops->complete(rq);
Christoph Hellwigc3077b52020-06-11 08:44:41 +0200649}
650
Christoph Hellwig963395262020-06-11 08:44:49 +0200651static inline bool blk_mq_complete_need_ipi(struct request *rq)
652{
653 int cpu = raw_smp_processor_id();
654
655 if (!IS_ENABLED(CONFIG_SMP) ||
656 !test_bit(QUEUE_FLAG_SAME_COMP, &rq->q->queue_flags))
657 return false;
658
659 /* same CPU or cache domain? Complete locally */
660 if (cpu == rq->mq_ctx->cpu ||
661 (!test_bit(QUEUE_FLAG_SAME_FORCE, &rq->q->queue_flags) &&
662 cpus_share_cache(cpu, rq->mq_ctx->cpu)))
663 return false;
664
665 /* don't try to IPI to an offline CPU */
666 return cpu_online(rq->mq_ctx->cpu);
667}
668
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200669bool blk_mq_complete_request_remote(struct request *rq)
670{
671 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
672
Ming Lei36a3df52020-06-29 17:47:59 +0800673 blk_mq_put_driver_tag(rq);
674
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200675 /*
676 * For a polled request, always complete locallly, it's pointless
677 * to redirect the completion.
678 */
679 if (rq->cmd_flags & REQ_HIPRI)
680 return false;
681
682 if (blk_mq_complete_need_ipi(rq)) {
683 rq->csd.func = __blk_mq_complete_request_remote;
684 rq->csd.info = rq;
685 rq->csd.flags = 0;
686 smp_call_function_single_async(rq->mq_ctx->cpu, &rq->csd);
687 } else {
688 if (rq->q->nr_hw_queues > 1)
689 return false;
690 blk_mq_trigger_softirq(rq);
691 }
692
693 return true;
694}
695EXPORT_SYMBOL_GPL(blk_mq_complete_request_remote);
696
Keith Busch7b11eab2020-05-29 07:51:59 -0700697/**
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200698 * blk_mq_complete_request - end I/O on a request
699 * @rq: the request being processed
Keith Busch7b11eab2020-05-29 07:51:59 -0700700 *
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200701 * Description:
702 * Complete a request by scheduling the ->complete_rq operation.
703 **/
704void blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100705{
Christoph Hellwig40d09b52020-06-11 08:44:50 +0200706 if (!blk_mq_complete_request_remote(rq))
Christoph Hellwig963395262020-06-11 08:44:49 +0200707 rq->q->mq_ops->complete(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100708}
Christoph Hellwig15f73f52020-06-11 08:44:47 +0200709EXPORT_SYMBOL(blk_mq_complete_request);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800710
Jens Axboe04ced152018-01-09 08:29:46 -0800711static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800712 __releases(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800713{
714 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
715 rcu_read_unlock();
716 else
Tejun Heo05707b62018-01-09 08:29:53 -0800717 srcu_read_unlock(hctx->srcu, srcu_idx);
Jens Axboe04ced152018-01-09 08:29:46 -0800718}
719
720static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800721 __acquires(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800722{
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700723 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
724 /* shut up gcc false positive */
725 *srcu_idx = 0;
Jens Axboe04ced152018-01-09 08:29:46 -0800726 rcu_read_lock();
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700727 } else
Tejun Heo05707b62018-01-09 08:29:53 -0800728 *srcu_idx = srcu_read_lock(hctx->srcu);
Jens Axboe04ced152018-01-09 08:29:46 -0800729}
730
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800731/**
André Almeida105663f2020-01-06 15:08:18 -0300732 * blk_mq_start_request - Start processing a request
733 * @rq: Pointer to request to be started
734 *
735 * Function used by device drivers to notify the block layer that a request
736 * is going to be processed now, so blk layer can do proper initializations
737 * such as starting the timeout timer.
738 */
Christoph Hellwige2490072014-09-13 16:40:09 -0700739void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100740{
741 struct request_queue *q = rq->q;
742
743 trace_block_rq_issue(q, rq);
744
Jens Axboecf43e6b2016-11-07 21:32:37 -0700745 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700746 rq->io_start_time_ns = ktime_get_ns();
Hou Tao3d244302019-05-21 15:59:03 +0800747 rq->stats_sectors = blk_rq_sectors(rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700748 rq->rq_flags |= RQF_STATS;
Josef Bacika7905042018-07-03 09:32:35 -0600749 rq_qos_issue(q, rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700750 }
751
Tejun Heo1d9bd512018-01-09 08:29:48 -0800752 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
Jens Axboe538b7532014-09-16 10:37:37 -0600753
Tejun Heo1d9bd512018-01-09 08:29:48 -0800754 blk_add_timer(rq);
Keith Busch12f5b932018-05-29 15:52:28 +0200755 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800756
Max Gurtovoy54d4e6a2019-09-16 18:44:29 +0300757#ifdef CONFIG_BLK_DEV_INTEGRITY
758 if (blk_integrity_rq(rq) && req_op(rq) == REQ_OP_WRITE)
759 q->integrity.profile->prepare_fn(rq);
760#endif
Jens Axboe320ae512013-10-24 09:20:05 +0100761}
Christoph Hellwige2490072014-09-13 16:40:09 -0700762EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100763
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200764static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100765{
766 struct request_queue *q = rq->q;
767
Ming Lei923218f2017-11-02 23:24:38 +0800768 blk_mq_put_driver_tag(rq);
769
Jens Axboe320ae512013-10-24 09:20:05 +0100770 trace_block_rq_requeue(q, rq);
Josef Bacika7905042018-07-03 09:32:35 -0600771 rq_qos_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800772
Keith Busch12f5b932018-05-29 15:52:28 +0200773 if (blk_mq_request_started(rq)) {
774 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Christoph Hellwigda661262018-06-14 13:58:45 +0200775 rq->rq_flags &= ~RQF_TIMED_OUT;
Christoph Hellwige2490072014-09-13 16:40:09 -0700776 }
Jens Axboe320ae512013-10-24 09:20:05 +0100777}
778
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700779void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200780{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200781 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200782
Ming Lei105976f2018-02-23 23:36:56 +0800783 /* this request will be re-inserted to io scheduler queue */
784 blk_mq_sched_requeue_request(rq);
785
Jens Axboe7d692332018-10-24 10:48:12 -0600786 BUG_ON(!list_empty(&rq->queuelist));
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700787 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200788}
789EXPORT_SYMBOL(blk_mq_requeue_request);
790
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600791static void blk_mq_requeue_work(struct work_struct *work)
792{
793 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -0400794 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600795 LIST_HEAD(rq_list);
796 struct request *rq, *next;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600797
Jens Axboe18e97812017-07-27 08:03:57 -0600798 spin_lock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600799 list_splice_init(&q->requeue_list, &rq_list);
Jens Axboe18e97812017-07-27 08:03:57 -0600800 spin_unlock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600801
802 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
Jianchao Wangaef18972019-02-12 09:56:25 +0800803 if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP)))
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600804 continue;
805
Christoph Hellwige8064022016-10-20 15:12:13 +0200806 rq->rq_flags &= ~RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600807 list_del_init(&rq->queuelist);
Jianchao Wangaef18972019-02-12 09:56:25 +0800808 /*
809 * If RQF_DONTPREP, rq has contained some driver specific
810 * data, so insert it to hctx dispatch list to avoid any
811 * merge.
812 */
813 if (rq->rq_flags & RQF_DONTPREP)
Ming Lei01e99ae2020-02-25 09:04:32 +0800814 blk_mq_request_bypass_insert(rq, false, false);
Jianchao Wangaef18972019-02-12 09:56:25 +0800815 else
816 blk_mq_sched_insert_request(rq, true, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600817 }
818
819 while (!list_empty(&rq_list)) {
820 rq = list_entry(rq_list.next, struct request, queuelist);
821 list_del_init(&rq->queuelist);
Mike Snitzer9e97d292018-01-17 11:25:58 -0500822 blk_mq_sched_insert_request(rq, false, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600823 }
824
Bart Van Assche52d7f1b2016-10-28 17:20:32 -0700825 blk_mq_run_hw_queues(q, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600826}
827
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700828void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
829 bool kick_requeue_list)
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600830{
831 struct request_queue *q = rq->q;
832 unsigned long flags;
833
834 /*
835 * We abuse this flag that is otherwise used by the I/O scheduler to
Jens Axboeff821d22017-11-10 22:05:12 -0700836 * request head insertion from the workqueue.
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600837 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200838 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600839
840 spin_lock_irqsave(&q->requeue_lock, flags);
841 if (at_head) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200842 rq->rq_flags |= RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600843 list_add(&rq->queuelist, &q->requeue_list);
844 } else {
845 list_add_tail(&rq->queuelist, &q->requeue_list);
846 }
847 spin_unlock_irqrestore(&q->requeue_lock, flags);
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700848
849 if (kick_requeue_list)
850 blk_mq_kick_requeue_list(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600851}
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600852
853void blk_mq_kick_requeue_list(struct request_queue *q)
854{
Bart Van Asscheae943d22018-01-19 08:58:55 -0800855 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600856}
857EXPORT_SYMBOL(blk_mq_kick_requeue_list);
858
Mike Snitzer28494502016-09-14 13:28:30 -0400859void blk_mq_delay_kick_requeue_list(struct request_queue *q,
860 unsigned long msecs)
861{
Bart Van Assched4acf362017-08-09 11:28:06 -0700862 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
863 msecs_to_jiffies(msecs));
Mike Snitzer28494502016-09-14 13:28:30 -0400864}
865EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
866
Jens Axboe0e62f512014-06-04 10:23:49 -0600867struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
868{
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600869 if (tag < tags->nr_tags) {
870 prefetch(tags->rqs[tag]);
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700871 return tags->rqs[tag];
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600872 }
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700873
874 return NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600875}
876EXPORT_SYMBOL(blk_mq_tag_to_rq);
877
Jens Axboe3c94d832018-12-17 21:11:17 -0700878static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq,
879 void *priv, bool reserved)
Jens Axboeae879912018-11-08 09:03:51 -0700880{
881 /*
Jens Axboe3c94d832018-12-17 21:11:17 -0700882 * If we find a request that is inflight and the queue matches,
883 * we know the queue is busy. Return false to stop the iteration.
Jens Axboeae879912018-11-08 09:03:51 -0700884 */
Jens Axboe3c94d832018-12-17 21:11:17 -0700885 if (rq->state == MQ_RQ_IN_FLIGHT && rq->q == hctx->queue) {
Jens Axboeae879912018-11-08 09:03:51 -0700886 bool *busy = priv;
887
888 *busy = true;
889 return false;
890 }
891
892 return true;
893}
894
Jens Axboe3c94d832018-12-17 21:11:17 -0700895bool blk_mq_queue_inflight(struct request_queue *q)
Jens Axboeae879912018-11-08 09:03:51 -0700896{
897 bool busy = false;
898
Jens Axboe3c94d832018-12-17 21:11:17 -0700899 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
Jens Axboeae879912018-11-08 09:03:51 -0700900 return busy;
901}
Jens Axboe3c94d832018-12-17 21:11:17 -0700902EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
Jens Axboeae879912018-11-08 09:03:51 -0700903
Tejun Heo358f70d2018-01-09 08:29:50 -0800904static void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100905{
Christoph Hellwigda661262018-06-14 13:58:45 +0200906 req->rq_flags |= RQF_TIMED_OUT;
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200907 if (req->q->mq_ops->timeout) {
908 enum blk_eh_timer_return ret;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600909
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200910 ret = req->q->mq_ops->timeout(req, reserved);
911 if (ret == BLK_EH_DONE)
912 return;
913 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700914 }
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200915
916 blk_add_timer(req);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600917}
Keith Busch5b3f25f2015-01-07 18:55:46 -0700918
Keith Busch12f5b932018-05-29 15:52:28 +0200919static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
920{
921 unsigned long deadline;
922
923 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
924 return false;
Christoph Hellwigda661262018-06-14 13:58:45 +0200925 if (rq->rq_flags & RQF_TIMED_OUT)
926 return false;
Keith Busch12f5b932018-05-29 15:52:28 +0200927
Christoph Hellwig079076b2018-11-14 17:02:05 +0100928 deadline = READ_ONCE(rq->deadline);
Keith Busch12f5b932018-05-29 15:52:28 +0200929 if (time_after_eq(jiffies, deadline))
930 return true;
931
932 if (*next == 0)
933 *next = deadline;
934 else if (time_after(*next, deadline))
935 *next = deadline;
936 return false;
937}
938
Jens Axboe7baa8572018-11-08 10:24:07 -0700939static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700940 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100941{
Keith Busch12f5b932018-05-29 15:52:28 +0200942 unsigned long *next = priv;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700943
Keith Busch12f5b932018-05-29 15:52:28 +0200944 /*
945 * Just do a quick check if it is expired before locking the request in
946 * so we're not unnecessarilly synchronizing across CPUs.
947 */
948 if (!blk_mq_req_expired(rq, next))
Jens Axboe7baa8572018-11-08 10:24:07 -0700949 return true;
Jens Axboe320ae512013-10-24 09:20:05 +0100950
Tejun Heo1d9bd512018-01-09 08:29:48 -0800951 /*
Keith Busch12f5b932018-05-29 15:52:28 +0200952 * We have reason to believe the request may be expired. Take a
953 * reference on the request to lock this request lifetime into its
954 * currently allocated context to prevent it from being reallocated in
955 * the event the completion by-passes this timeout handler.
956 *
957 * If the reference was already released, then the driver beat the
958 * timeout handler to posting a natural completion.
Tejun Heo1d9bd512018-01-09 08:29:48 -0800959 */
Keith Busch12f5b932018-05-29 15:52:28 +0200960 if (!refcount_inc_not_zero(&rq->ref))
Jens Axboe7baa8572018-11-08 10:24:07 -0700961 return true;
Keith Busch12f5b932018-05-29 15:52:28 +0200962
963 /*
964 * The request is now locked and cannot be reallocated underneath the
965 * timeout handler's processing. Re-verify this exact request is truly
966 * expired; if it is not expired, then the request was completed and
967 * reallocated as a new request.
968 */
969 if (blk_mq_req_expired(rq, next))
Tejun Heo1d9bd512018-01-09 08:29:48 -0800970 blk_mq_rq_timed_out(rq, reserved);
Yufen Yu8d699662019-09-27 16:19:55 +0800971
972 if (is_flush_rq(rq, hctx))
973 rq->end_io(rq, 0);
974 else if (refcount_dec_and_test(&rq->ref))
Keith Busch12f5b932018-05-29 15:52:28 +0200975 __blk_mq_free_request(rq);
Jens Axboe7baa8572018-11-08 10:24:07 -0700976
977 return true;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800978}
979
Christoph Hellwig287922e2015-10-30 20:57:30 +0800980static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100981{
Christoph Hellwig287922e2015-10-30 20:57:30 +0800982 struct request_queue *q =
983 container_of(work, struct request_queue, timeout_work);
Keith Busch12f5b932018-05-29 15:52:28 +0200984 unsigned long next = 0;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800985 struct blk_mq_hw_ctx *hctx;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700986 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100987
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600988 /* A deadlock might occur if a request is stuck requiring a
989 * timeout at the same time a queue freeze is waiting
990 * completion, since the timeout code would not be able to
991 * acquire the queue reference here.
992 *
993 * That's why we don't use blk_queue_enter here; instead, we use
994 * percpu_ref_tryget directly, because we need to be able to
995 * obtain a reference even in the short window between the queue
996 * starting to freeze, by dropping the first reference in
Ming Lei1671d522017-03-27 20:06:57 +0800997 * blk_freeze_queue_start, and the moment the last request is
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600998 * consumed, marked by the instant q_usage_counter reaches
999 * zero.
1000 */
1001 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +08001002 return;
1003
Keith Busch12f5b932018-05-29 15:52:28 +02001004 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
Jens Axboe320ae512013-10-24 09:20:05 +01001005
Keith Busch12f5b932018-05-29 15:52:28 +02001006 if (next != 0) {
1007 mod_timer(&q->timeout, next);
Jens Axboe0d2602c2014-05-13 15:10:52 -06001008 } else {
Bart Van Asschefcd36c32018-01-10 08:33:33 -08001009 /*
1010 * Request timeouts are handled as a forward rolling timer. If
1011 * we end up here it means that no requests are pending and
1012 * also that no request has been pending for a while. Mark
1013 * each hctx as idle.
1014 */
Ming Leif054b562015-04-21 10:00:19 +08001015 queue_for_each_hw_ctx(q, hctx, i) {
1016 /* the hctx may be unmapped, so check it here */
1017 if (blk_mq_hw_queue_mapped(hctx))
1018 blk_mq_tag_idle(hctx);
1019 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06001020 }
Christoph Hellwig287922e2015-10-30 20:57:30 +08001021 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001022}
1023
Omar Sandoval88459642016-09-17 08:38:44 -06001024struct flush_busy_ctx_data {
1025 struct blk_mq_hw_ctx *hctx;
1026 struct list_head *list;
1027};
1028
1029static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
1030{
1031 struct flush_busy_ctx_data *flush_data = data;
1032 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
1033 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001034 enum hctx_type type = hctx->type;
Omar Sandoval88459642016-09-17 08:38:44 -06001035
Omar Sandoval88459642016-09-17 08:38:44 -06001036 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001037 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
Omar Sandovale9a99a62018-02-27 16:56:42 -08001038 sbitmap_clear_bit(sb, bitnr);
Omar Sandoval88459642016-09-17 08:38:44 -06001039 spin_unlock(&ctx->lock);
1040 return true;
1041}
1042
Jens Axboe320ae512013-10-24 09:20:05 +01001043/*
Jens Axboe1429d7c2014-05-19 09:23:55 -06001044 * Process software queues that have been marked busy, splicing them
1045 * to the for-dispatch
1046 */
Jens Axboe2c3ad662016-12-14 14:34:47 -07001047void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
Jens Axboe1429d7c2014-05-19 09:23:55 -06001048{
Omar Sandoval88459642016-09-17 08:38:44 -06001049 struct flush_busy_ctx_data data = {
1050 .hctx = hctx,
1051 .list = list,
1052 };
Jens Axboe1429d7c2014-05-19 09:23:55 -06001053
Omar Sandoval88459642016-09-17 08:38:44 -06001054 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001055}
Jens Axboe2c3ad662016-12-14 14:34:47 -07001056EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001057
Ming Leib3476892017-10-14 17:22:30 +08001058struct dispatch_rq_data {
1059 struct blk_mq_hw_ctx *hctx;
1060 struct request *rq;
1061};
1062
1063static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
1064 void *data)
1065{
1066 struct dispatch_rq_data *dispatch_data = data;
1067 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
1068 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001069 enum hctx_type type = hctx->type;
Ming Leib3476892017-10-14 17:22:30 +08001070
1071 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001072 if (!list_empty(&ctx->rq_lists[type])) {
1073 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
Ming Leib3476892017-10-14 17:22:30 +08001074 list_del_init(&dispatch_data->rq->queuelist);
Ming Leic16d6b52018-12-17 08:44:05 -07001075 if (list_empty(&ctx->rq_lists[type]))
Ming Leib3476892017-10-14 17:22:30 +08001076 sbitmap_clear_bit(sb, bitnr);
1077 }
1078 spin_unlock(&ctx->lock);
1079
1080 return !dispatch_data->rq;
1081}
1082
1083struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
1084 struct blk_mq_ctx *start)
1085{
Jens Axboef31967f2018-10-29 13:13:29 -06001086 unsigned off = start ? start->index_hw[hctx->type] : 0;
Ming Leib3476892017-10-14 17:22:30 +08001087 struct dispatch_rq_data data = {
1088 .hctx = hctx,
1089 .rq = NULL,
1090 };
1091
1092 __sbitmap_for_each_set(&hctx->ctx_map, off,
1093 dispatch_rq_from_ctx, &data);
1094
1095 return data.rq;
1096}
1097
Jens Axboe703fd1c2016-09-16 13:59:14 -06001098static inline unsigned int queued_to_index(unsigned int queued)
1099{
1100 if (!queued)
1101 return 0;
Jens Axboe1429d7c2014-05-19 09:23:55 -06001102
Jens Axboe703fd1c2016-09-16 13:59:14 -06001103 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001104}
1105
Jens Axboeeb619fd2017-11-09 08:32:43 -07001106static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1107 int flags, void *key)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001108{
1109 struct blk_mq_hw_ctx *hctx;
1110
1111 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1112
Ming Lei5815839b2018-06-25 19:31:47 +08001113 spin_lock(&hctx->dispatch_wait_lock);
Jens Axboee8618572019-03-25 12:34:10 -06001114 if (!list_empty(&wait->entry)) {
1115 struct sbitmap_queue *sbq;
1116
1117 list_del_init(&wait->entry);
1118 sbq = &hctx->tags->bitmap_tags;
1119 atomic_dec(&sbq->ws_active);
1120 }
Ming Lei5815839b2018-06-25 19:31:47 +08001121 spin_unlock(&hctx->dispatch_wait_lock);
1122
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001123 blk_mq_run_hw_queue(hctx, true);
1124 return 1;
1125}
1126
Jens Axboef906a6a2017-11-09 16:10:13 -07001127/*
1128 * Mark us waiting for a tag. For shared tags, this involves hooking us into
Bart Van Asscheee3e4de2018-01-09 10:09:15 -08001129 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1130 * restart. For both cases, take care to check the condition again after
Jens Axboef906a6a2017-11-09 16:10:13 -07001131 * marking us as waiting.
1132 */
Ming Lei2278d692018-06-25 19:31:46 +08001133static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
Jens Axboef906a6a2017-11-09 16:10:13 -07001134 struct request *rq)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001135{
Jens Axboee8618572019-03-25 12:34:10 -06001136 struct sbitmap_queue *sbq = &hctx->tags->bitmap_tags;
Ming Lei5815839b2018-06-25 19:31:47 +08001137 struct wait_queue_head *wq;
Jens Axboef906a6a2017-11-09 16:10:13 -07001138 wait_queue_entry_t *wait;
1139 bool ret;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001140
Ming Lei2278d692018-06-25 19:31:46 +08001141 if (!(hctx->flags & BLK_MQ_F_TAG_SHARED)) {
Yufen Yu684b7322019-03-15 11:05:10 +08001142 blk_mq_sched_mark_restart_hctx(hctx);
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001143
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001144 /*
1145 * It's possible that a tag was freed in the window between the
1146 * allocation failure and adding the hardware queue to the wait
1147 * queue.
1148 *
1149 * Don't clear RESTART here, someone else could have set it.
1150 * At most this will cost an extra queue run.
1151 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001152 return blk_mq_get_driver_tag(rq);
Jens Axboeeb619fd2017-11-09 08:32:43 -07001153 }
1154
Ming Lei2278d692018-06-25 19:31:46 +08001155 wait = &hctx->dispatch_wait;
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001156 if (!list_empty_careful(&wait->entry))
1157 return false;
1158
Jens Axboee8618572019-03-25 12:34:10 -06001159 wq = &bt_wait_ptr(sbq, hctx)->wait;
Ming Lei5815839b2018-06-25 19:31:47 +08001160
1161 spin_lock_irq(&wq->lock);
1162 spin_lock(&hctx->dispatch_wait_lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001163 if (!list_empty(&wait->entry)) {
Ming Lei5815839b2018-06-25 19:31:47 +08001164 spin_unlock(&hctx->dispatch_wait_lock);
1165 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001166 return false;
1167 }
1168
Jens Axboee8618572019-03-25 12:34:10 -06001169 atomic_inc(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001170 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1171 __add_wait_queue(wq, wait);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001172
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001173 /*
Jens Axboeeb619fd2017-11-09 08:32:43 -07001174 * It's possible that a tag was freed in the window between the
1175 * allocation failure and adding the hardware queue to the wait
1176 * queue.
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001177 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001178 ret = blk_mq_get_driver_tag(rq);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001179 if (!ret) {
Ming Lei5815839b2018-06-25 19:31:47 +08001180 spin_unlock(&hctx->dispatch_wait_lock);
1181 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001182 return false;
Jens Axboef906a6a2017-11-09 16:10:13 -07001183 }
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001184
1185 /*
1186 * We got a tag, remove ourselves from the wait queue to ensure
1187 * someone else gets the wakeup.
1188 */
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001189 list_del_init(&wait->entry);
Jens Axboee8618572019-03-25 12:34:10 -06001190 atomic_dec(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001191 spin_unlock(&hctx->dispatch_wait_lock);
1192 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001193
1194 return true;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001195}
1196
Ming Lei6e7687172018-07-03 09:03:16 -06001197#define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1198#define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1199/*
1200 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1201 * - EWMA is one simple way to compute running average value
1202 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1203 * - take 4 as factor for avoiding to get too small(0) result, and this
1204 * factor doesn't matter because EWMA decreases exponentially
1205 */
1206static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1207{
1208 unsigned int ewma;
1209
1210 if (hctx->queue->elevator)
1211 return;
1212
1213 ewma = hctx->dispatch_busy;
1214
1215 if (!ewma && !busy)
1216 return;
1217
1218 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1219 if (busy)
1220 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1221 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1222
1223 hctx->dispatch_busy = ewma;
1224}
1225
Ming Lei86ff7c22018-01-30 22:04:57 -05001226#define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1227
Johannes Thumshirnc92a4102020-03-25 00:24:44 +09001228static void blk_mq_handle_dev_resource(struct request *rq,
1229 struct list_head *list)
1230{
1231 struct request *next =
1232 list_first_entry_or_null(list, struct request, queuelist);
1233
1234 /*
1235 * If an I/O scheduler has been configured and we got a driver tag for
1236 * the next request already, free it.
1237 */
1238 if (next)
1239 blk_mq_put_driver_tag(next);
1240
1241 list_add(&rq->queuelist, list);
1242 __blk_mq_requeue_request(rq);
1243}
1244
Keith Busch0512a752020-05-12 17:55:47 +09001245static void blk_mq_handle_zone_resource(struct request *rq,
1246 struct list_head *zone_list)
1247{
1248 /*
1249 * If we end up here it is because we cannot dispatch a request to a
1250 * specific zone due to LLD level zone-write locking or other zone
1251 * related resource not being available. In this case, set the request
1252 * aside in zone_list for retrying it later.
1253 */
1254 list_add(&rq->queuelist, zone_list);
1255 __blk_mq_requeue_request(rq);
1256}
1257
Ming Lei75383522020-06-30 18:24:58 +08001258enum prep_dispatch {
1259 PREP_DISPATCH_OK,
1260 PREP_DISPATCH_NO_TAG,
1261 PREP_DISPATCH_NO_BUDGET,
1262};
1263
1264static enum prep_dispatch blk_mq_prep_dispatch_rq(struct request *rq,
1265 bool need_budget)
1266{
1267 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1268
1269 if (need_budget && !blk_mq_get_dispatch_budget(rq->q)) {
1270 blk_mq_put_driver_tag(rq);
1271 return PREP_DISPATCH_NO_BUDGET;
1272 }
1273
1274 if (!blk_mq_get_driver_tag(rq)) {
1275 /*
1276 * The initial allocation attempt failed, so we need to
1277 * rerun the hardware queue when a tag is freed. The
1278 * waitqueue takes care of that. If the queue is run
1279 * before we add this entry back on the dispatch list,
1280 * we'll re-run it below.
1281 */
1282 if (!blk_mq_mark_tag_wait(hctx, rq)) {
Ming Lei1fd40b52020-06-30 18:25:00 +08001283 /*
1284 * All budgets not got from this function will be put
1285 * together during handling partial dispatch
1286 */
1287 if (need_budget)
1288 blk_mq_put_dispatch_budget(rq->q);
Ming Lei75383522020-06-30 18:24:58 +08001289 return PREP_DISPATCH_NO_TAG;
1290 }
1291 }
1292
1293 return PREP_DISPATCH_OK;
1294}
1295
Ming Lei1fd40b52020-06-30 18:25:00 +08001296/* release all allocated budgets before calling to blk_mq_dispatch_rq_list */
1297static void blk_mq_release_budgets(struct request_queue *q,
1298 unsigned int nr_budgets)
1299{
1300 int i;
1301
1302 for (i = 0; i < nr_budgets; i++)
1303 blk_mq_put_dispatch_budget(q);
1304}
1305
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001306/*
1307 * Returns true if we did some work AND can potentially do more.
1308 */
Ming Lei445874e2020-06-30 18:24:57 +08001309bool blk_mq_dispatch_rq_list(struct blk_mq_hw_ctx *hctx, struct list_head *list,
Ming Lei1fd40b52020-06-30 18:25:00 +08001310 unsigned int nr_budgets)
Jens Axboef04c3df2016-12-07 08:41:17 -07001311{
Ming Lei75383522020-06-30 18:24:58 +08001312 enum prep_dispatch prep;
Ming Lei445874e2020-06-30 18:24:57 +08001313 struct request_queue *q = hctx->queue;
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001314 struct request *rq, *nxt;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001315 int errors, queued;
Ming Lei86ff7c22018-01-30 22:04:57 -05001316 blk_status_t ret = BLK_STS_OK;
Keith Busch0512a752020-05-12 17:55:47 +09001317 LIST_HEAD(zone_list);
Jens Axboef04c3df2016-12-07 08:41:17 -07001318
Omar Sandoval81380ca2017-04-07 08:56:26 -06001319 if (list_empty(list))
1320 return false;
1321
Ming Lei1fd40b52020-06-30 18:25:00 +08001322 WARN_ON(!list_is_singular(list) && nr_budgets);
Ming Leide148292017-10-14 17:22:29 +08001323
Jens Axboef04c3df2016-12-07 08:41:17 -07001324 /*
Jens Axboef04c3df2016-12-07 08:41:17 -07001325 * Now process all the entries, sending them to the driver.
1326 */
Jens Axboe93efe982017-03-24 12:04:19 -06001327 errors = queued = 0;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001328 do {
Jens Axboef04c3df2016-12-07 08:41:17 -07001329 struct blk_mq_queue_data bd;
1330
1331 rq = list_first_entry(list, struct request, queuelist);
Ming Lei0bca7992018-04-05 00:35:21 +08001332
Ming Lei445874e2020-06-30 18:24:57 +08001333 WARN_ON_ONCE(hctx != rq->mq_hctx);
Ming Lei1fd40b52020-06-30 18:25:00 +08001334 prep = blk_mq_prep_dispatch_rq(rq, !nr_budgets);
Ming Lei75383522020-06-30 18:24:58 +08001335 if (prep != PREP_DISPATCH_OK)
Ming Lei0bca7992018-04-05 00:35:21 +08001336 break;
Ming Leide148292017-10-14 17:22:29 +08001337
Jens Axboef04c3df2016-12-07 08:41:17 -07001338 list_del_init(&rq->queuelist);
1339
1340 bd.rq = rq;
Jens Axboe113285b2017-03-02 13:26:04 -07001341
1342 /*
1343 * Flag last if we have no more requests, or if we have more
1344 * but can't assign a driver tag to it.
1345 */
1346 if (list_empty(list))
1347 bd.last = true;
1348 else {
Jens Axboe113285b2017-03-02 13:26:04 -07001349 nxt = list_first_entry(list, struct request, queuelist);
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001350 bd.last = !blk_mq_get_driver_tag(nxt);
Jens Axboe113285b2017-03-02 13:26:04 -07001351 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001352
Ming Lei1fd40b52020-06-30 18:25:00 +08001353 /*
1354 * once the request is queued to lld, no need to cover the
1355 * budget any more
1356 */
1357 if (nr_budgets)
1358 nr_budgets--;
Jens Axboef04c3df2016-12-07 08:41:17 -07001359 ret = q->mq_ops->queue_rq(hctx, &bd);
Ming Lei86ff7c22018-01-30 22:04:57 -05001360 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) {
Johannes Thumshirnc92a4102020-03-25 00:24:44 +09001361 blk_mq_handle_dev_resource(rq, list);
Jens Axboef04c3df2016-12-07 08:41:17 -07001362 break;
Keith Busch0512a752020-05-12 17:55:47 +09001363 } else if (ret == BLK_STS_ZONE_RESOURCE) {
1364 /*
1365 * Move the request to zone_list and keep going through
1366 * the dispatch list to find more requests the drive can
1367 * accept.
1368 */
1369 blk_mq_handle_zone_resource(rq, &zone_list);
1370 if (list_empty(list))
1371 break;
1372 continue;
Jens Axboef04c3df2016-12-07 08:41:17 -07001373 }
1374
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001375 if (unlikely(ret != BLK_STS_OK)) {
1376 errors++;
1377 blk_mq_end_request(rq, BLK_STS_IOERR);
1378 continue;
1379 }
1380
1381 queued++;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001382 } while (!list_empty(list));
Jens Axboef04c3df2016-12-07 08:41:17 -07001383
Keith Busch0512a752020-05-12 17:55:47 +09001384 if (!list_empty(&zone_list))
1385 list_splice_tail_init(&zone_list, list);
1386
Jens Axboef04c3df2016-12-07 08:41:17 -07001387 hctx->dispatched[queued_to_index(queued)]++;
1388
1389 /*
1390 * Any items that need requeuing? Stuff them into hctx->dispatch,
1391 * that is where we will continue on next queue run.
1392 */
1393 if (!list_empty(list)) {
Ming Lei86ff7c22018-01-30 22:04:57 -05001394 bool needs_restart;
Ming Lei75383522020-06-30 18:24:58 +08001395 /* For non-shared tags, the RESTART check will suffice */
1396 bool no_tag = prep == PREP_DISPATCH_NO_TAG &&
1397 (hctx->flags & BLK_MQ_F_TAG_SHARED);
1398 bool no_budget_avail = prep == PREP_DISPATCH_NO_BUDGET;
Ming Lei86ff7c22018-01-30 22:04:57 -05001399
Ming Lei1fd40b52020-06-30 18:25:00 +08001400 blk_mq_release_budgets(q, nr_budgets);
1401
Jens Axboed666ba92018-11-27 17:02:25 -07001402 /*
1403 * If we didn't flush the entire list, we could have told
1404 * the driver there was more coming, but that turned out to
1405 * be a lie.
1406 */
Keith Busch536167d42020-04-07 03:13:48 +09001407 if (q->mq_ops->commit_rqs && queued)
Jens Axboed666ba92018-11-27 17:02:25 -07001408 q->mq_ops->commit_rqs(hctx);
1409
Jens Axboef04c3df2016-12-07 08:41:17 -07001410 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001411 list_splice_tail_init(list, &hctx->dispatch);
Jens Axboef04c3df2016-12-07 08:41:17 -07001412 spin_unlock(&hctx->lock);
1413
1414 /*
Bart Van Assche710c7852017-04-07 11:16:51 -07001415 * If SCHED_RESTART was set by the caller of this function and
1416 * it is no longer set that means that it was cleared by another
1417 * thread and hence that a queue rerun is needed.
Jens Axboef04c3df2016-12-07 08:41:17 -07001418 *
Jens Axboeeb619fd2017-11-09 08:32:43 -07001419 * If 'no_tag' is set, that means that we failed getting
1420 * a driver tag with an I/O scheduler attached. If our dispatch
1421 * waitqueue is no longer active, ensure that we run the queue
1422 * AFTER adding our entries back to the list.
Jens Axboebd166ef2017-01-17 06:03:22 -07001423 *
Bart Van Assche710c7852017-04-07 11:16:51 -07001424 * If no I/O scheduler has been configured it is possible that
1425 * the hardware queue got stopped and restarted before requests
1426 * were pushed back onto the dispatch list. Rerun the queue to
1427 * avoid starvation. Notes:
1428 * - blk_mq_run_hw_queue() checks whether or not a queue has
1429 * been stopped before rerunning a queue.
1430 * - Some but not all block drivers stop a queue before
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001431 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
Bart Van Assche710c7852017-04-07 11:16:51 -07001432 * and dm-rq.
Ming Lei86ff7c22018-01-30 22:04:57 -05001433 *
1434 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1435 * bit is set, run queue after a delay to avoid IO stalls
Douglas Andersonab3cee32020-04-20 09:24:51 -07001436 * that could otherwise occur if the queue is idle. We'll do
1437 * similar if we couldn't get budget and SCHED_RESTART is set.
Jens Axboebd166ef2017-01-17 06:03:22 -07001438 */
Ming Lei86ff7c22018-01-30 22:04:57 -05001439 needs_restart = blk_mq_sched_needs_restart(hctx);
1440 if (!needs_restart ||
Jens Axboeeb619fd2017-11-09 08:32:43 -07001441 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
Jens Axboebd166ef2017-01-17 06:03:22 -07001442 blk_mq_run_hw_queue(hctx, true);
Douglas Andersonab3cee32020-04-20 09:24:51 -07001443 else if (needs_restart && (ret == BLK_STS_RESOURCE ||
1444 no_budget_avail))
Ming Lei86ff7c22018-01-30 22:04:57 -05001445 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001446
Ming Lei6e7687172018-07-03 09:03:16 -06001447 blk_mq_update_dispatch_busy(hctx, true);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001448 return false;
Ming Lei6e7687172018-07-03 09:03:16 -06001449 } else
1450 blk_mq_update_dispatch_busy(hctx, false);
Jens Axboef04c3df2016-12-07 08:41:17 -07001451
Jens Axboe93efe982017-03-24 12:04:19 -06001452 return (queued + errors) != 0;
Jens Axboef04c3df2016-12-07 08:41:17 -07001453}
1454
André Almeida105663f2020-01-06 15:08:18 -03001455/**
1456 * __blk_mq_run_hw_queue - Run a hardware queue.
1457 * @hctx: Pointer to the hardware queue to run.
1458 *
1459 * Send pending requests to the hardware.
1460 */
Bart Van Assche6a83e742016-11-02 10:09:51 -06001461static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1462{
1463 int srcu_idx;
1464
Jens Axboeb7a71e62017-08-01 09:28:24 -06001465 /*
1466 * We should be running this queue from one of the CPUs that
1467 * are mapped to it.
Ming Lei7df938f2018-01-18 00:41:52 +08001468 *
1469 * There are at least two related races now between setting
1470 * hctx->next_cpu from blk_mq_hctx_next_cpu() and running
1471 * __blk_mq_run_hw_queue():
1472 *
1473 * - hctx->next_cpu is found offline in blk_mq_hctx_next_cpu(),
1474 * but later it becomes online, then this warning is harmless
1475 * at all
1476 *
1477 * - hctx->next_cpu is found online in blk_mq_hctx_next_cpu(),
1478 * but later it becomes offline, then the warning can't be
1479 * triggered, and we depend on blk-mq timeout handler to
1480 * handle dispatched requests to this hctx
Jens Axboeb7a71e62017-08-01 09:28:24 -06001481 */
Ming Lei7df938f2018-01-18 00:41:52 +08001482 if (!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
1483 cpu_online(hctx->next_cpu)) {
1484 printk(KERN_WARNING "run queue from wrong CPU %d, hctx %s\n",
1485 raw_smp_processor_id(),
1486 cpumask_empty(hctx->cpumask) ? "inactive": "active");
1487 dump_stack();
1488 }
Bart Van Assche6a83e742016-11-02 10:09:51 -06001489
Jens Axboeb7a71e62017-08-01 09:28:24 -06001490 /*
1491 * We can't run the queue inline with ints disabled. Ensure that
1492 * we catch bad users of this early.
1493 */
1494 WARN_ON_ONCE(in_interrupt());
1495
Jens Axboe04ced152018-01-09 08:29:46 -08001496 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
Jens Axboebf4907c2017-03-30 12:30:39 -06001497
Jens Axboe04ced152018-01-09 08:29:46 -08001498 hctx_lock(hctx, &srcu_idx);
1499 blk_mq_sched_dispatch_requests(hctx);
1500 hctx_unlock(hctx, srcu_idx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001501}
1502
Ming Leif82ddf12018-04-08 17:48:10 +08001503static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1504{
1505 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1506
1507 if (cpu >= nr_cpu_ids)
1508 cpu = cpumask_first(hctx->cpumask);
1509 return cpu;
1510}
1511
Jens Axboe506e9312014-05-07 10:26:44 -06001512/*
1513 * It'd be great if the workqueue API had a way to pass
1514 * in a mask and had some smarts for more clever placement.
1515 * For now we just round-robin here, switching for every
1516 * BLK_MQ_CPU_WORK_BATCH queued items.
1517 */
1518static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1519{
Ming Lei7bed4592018-01-18 00:41:51 +08001520 bool tried = false;
Ming Lei476f8c92018-04-08 17:48:09 +08001521 int next_cpu = hctx->next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001522
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001523 if (hctx->queue->nr_hw_queues == 1)
1524 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -06001525
1526 if (--hctx->next_cpu_batch <= 0) {
Ming Lei7bed4592018-01-18 00:41:51 +08001527select_cpu:
Ming Lei476f8c92018-04-08 17:48:09 +08001528 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08001529 cpu_online_mask);
Jens Axboe506e9312014-05-07 10:26:44 -06001530 if (next_cpu >= nr_cpu_ids)
Ming Leif82ddf12018-04-08 17:48:10 +08001531 next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06001532 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1533 }
1534
Ming Lei7bed4592018-01-18 00:41:51 +08001535 /*
1536 * Do unbound schedule if we can't find a online CPU for this hctx,
1537 * and it should only happen in the path of handling CPU DEAD.
1538 */
Ming Lei476f8c92018-04-08 17:48:09 +08001539 if (!cpu_online(next_cpu)) {
Ming Lei7bed4592018-01-18 00:41:51 +08001540 if (!tried) {
1541 tried = true;
1542 goto select_cpu;
1543 }
1544
1545 /*
1546 * Make sure to re-select CPU next time once after CPUs
1547 * in hctx->cpumask become online again.
1548 */
Ming Lei476f8c92018-04-08 17:48:09 +08001549 hctx->next_cpu = next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001550 hctx->next_cpu_batch = 1;
1551 return WORK_CPU_UNBOUND;
1552 }
Ming Lei476f8c92018-04-08 17:48:09 +08001553
1554 hctx->next_cpu = next_cpu;
1555 return next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -06001556}
1557
André Almeida105663f2020-01-06 15:08:18 -03001558/**
1559 * __blk_mq_delay_run_hw_queue - Run (or schedule to run) a hardware queue.
1560 * @hctx: Pointer to the hardware queue to run.
1561 * @async: If we want to run the queue asynchronously.
1562 * @msecs: Microseconds of delay to wait before running the queue.
1563 *
1564 * If !@async, try to run the queue now. Else, run the queue asynchronously and
1565 * with a delay of @msecs.
1566 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001567static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1568 unsigned long msecs)
Jens Axboe320ae512013-10-24 09:20:05 +01001569{
Bart Van Assche5435c022017-06-20 11:15:49 -07001570 if (unlikely(blk_mq_hctx_stopped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +01001571 return;
1572
Jens Axboe1b792f22016-09-21 10:12:13 -06001573 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001574 int cpu = get_cpu();
1575 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +01001576 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001577 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +01001578 return;
1579 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001580
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001581 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -06001582 }
Paolo Bonzini398205b2014-11-07 23:03:59 +01001583
Bart Van Asscheae943d22018-01-19 08:58:55 -08001584 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1585 msecs_to_jiffies(msecs));
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001586}
1587
André Almeida105663f2020-01-06 15:08:18 -03001588/**
1589 * blk_mq_delay_run_hw_queue - Run a hardware queue asynchronously.
1590 * @hctx: Pointer to the hardware queue to run.
1591 * @msecs: Microseconds of delay to wait before running the queue.
1592 *
1593 * Run a hardware queue asynchronously with a delay of @msecs.
1594 */
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001595void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1596{
1597 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1598}
1599EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1600
André Almeida105663f2020-01-06 15:08:18 -03001601/**
1602 * blk_mq_run_hw_queue - Start to run a hardware queue.
1603 * @hctx: Pointer to the hardware queue to run.
1604 * @async: If we want to run the queue asynchronously.
1605 *
1606 * Check if the request queue is not in a quiesced state and if there are
1607 * pending requests to be sent. If this is true, run the queue to send requests
1608 * to hardware.
1609 */
John Garry626fb732019-10-30 00:59:30 +08001610void blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001611{
Ming Lei24f5a902018-01-06 16:27:38 +08001612 int srcu_idx;
1613 bool need_run;
1614
1615 /*
1616 * When queue is quiesced, we may be switching io scheduler, or
1617 * updating nr_hw_queues, or other things, and we can't run queue
1618 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
1619 *
1620 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
1621 * quiesced.
1622 */
Jens Axboe04ced152018-01-09 08:29:46 -08001623 hctx_lock(hctx, &srcu_idx);
1624 need_run = !blk_queue_quiesced(hctx->queue) &&
1625 blk_mq_hctx_has_pending(hctx);
1626 hctx_unlock(hctx, srcu_idx);
Ming Lei24f5a902018-01-06 16:27:38 +08001627
John Garry626fb732019-10-30 00:59:30 +08001628 if (need_run)
Jens Axboe79f720a2017-11-10 09:13:21 -07001629 __blk_mq_delay_run_hw_queue(hctx, async, 0);
Jens Axboe320ae512013-10-24 09:20:05 +01001630}
Omar Sandoval5b727272017-04-14 01:00:00 -07001631EXPORT_SYMBOL(blk_mq_run_hw_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01001632
André Almeida105663f2020-01-06 15:08:18 -03001633/**
1634 * blk_mq_run_hw_queue - Run all hardware queues in a request queue.
1635 * @q: Pointer to the request queue to run.
1636 * @async: If we want to run the queue asynchronously.
1637 */
Mike Snitzerb94ec292015-03-11 23:56:38 -04001638void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001639{
1640 struct blk_mq_hw_ctx *hctx;
1641 int i;
1642
1643 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe79f720a2017-11-10 09:13:21 -07001644 if (blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +01001645 continue;
1646
Mike Snitzerb94ec292015-03-11 23:56:38 -04001647 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001648 }
1649}
Mike Snitzerb94ec292015-03-11 23:56:38 -04001650EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001651
Bart Van Asschefd001442016-10-28 17:19:37 -07001652/**
Douglas Andersonb9151e72020-04-20 09:24:52 -07001653 * blk_mq_delay_run_hw_queues - Run all hardware queues asynchronously.
1654 * @q: Pointer to the request queue to run.
1655 * @msecs: Microseconds of delay to wait before running the queues.
1656 */
1657void blk_mq_delay_run_hw_queues(struct request_queue *q, unsigned long msecs)
1658{
1659 struct blk_mq_hw_ctx *hctx;
1660 int i;
1661
1662 queue_for_each_hw_ctx(q, hctx, i) {
1663 if (blk_mq_hctx_stopped(hctx))
1664 continue;
1665
1666 blk_mq_delay_run_hw_queue(hctx, msecs);
1667 }
1668}
1669EXPORT_SYMBOL(blk_mq_delay_run_hw_queues);
1670
1671/**
Bart Van Asschefd001442016-10-28 17:19:37 -07001672 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1673 * @q: request queue.
1674 *
1675 * The caller is responsible for serializing this function against
1676 * blk_mq_{start,stop}_hw_queue().
1677 */
1678bool blk_mq_queue_stopped(struct request_queue *q)
1679{
1680 struct blk_mq_hw_ctx *hctx;
1681 int i;
1682
1683 queue_for_each_hw_ctx(q, hctx, i)
1684 if (blk_mq_hctx_stopped(hctx))
1685 return true;
1686
1687 return false;
1688}
1689EXPORT_SYMBOL(blk_mq_queue_stopped);
1690
Ming Lei39a70c72017-06-06 23:22:09 +08001691/*
1692 * This function is often used for pausing .queue_rq() by driver when
1693 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001694 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001695 *
1696 * We do not guarantee that dispatch can be drained or blocked
1697 * after blk_mq_stop_hw_queue() returns. Please use
1698 * blk_mq_quiesce_queue() for that requirement.
1699 */
Jens Axboe320ae512013-10-24 09:20:05 +01001700void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1701{
Ming Lei641a9ed2017-06-06 23:22:10 +08001702 cancel_delayed_work(&hctx->run_work);
1703
1704 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboe320ae512013-10-24 09:20:05 +01001705}
1706EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1707
Ming Lei39a70c72017-06-06 23:22:09 +08001708/*
1709 * This function is often used for pausing .queue_rq() by driver when
1710 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001711 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001712 *
1713 * We do not guarantee that dispatch can be drained or blocked
1714 * after blk_mq_stop_hw_queues() returns. Please use
1715 * blk_mq_quiesce_queue() for that requirement.
1716 */
Jens Axboe2719aa22017-05-03 11:08:14 -06001717void blk_mq_stop_hw_queues(struct request_queue *q)
1718{
Ming Lei641a9ed2017-06-06 23:22:10 +08001719 struct blk_mq_hw_ctx *hctx;
1720 int i;
1721
1722 queue_for_each_hw_ctx(q, hctx, i)
1723 blk_mq_stop_hw_queue(hctx);
Christoph Hellwig280d45f2013-10-25 14:45:58 +01001724}
1725EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1726
Jens Axboe320ae512013-10-24 09:20:05 +01001727void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1728{
1729 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -06001730
Jens Axboe0ffbce82014-06-25 08:22:34 -06001731 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001732}
1733EXPORT_SYMBOL(blk_mq_start_hw_queue);
1734
Christoph Hellwig2f268552014-04-16 09:44:56 +02001735void blk_mq_start_hw_queues(struct request_queue *q)
1736{
1737 struct blk_mq_hw_ctx *hctx;
1738 int i;
1739
1740 queue_for_each_hw_ctx(q, hctx, i)
1741 blk_mq_start_hw_queue(hctx);
1742}
1743EXPORT_SYMBOL(blk_mq_start_hw_queues);
1744
Jens Axboeae911c52016-12-08 13:19:30 -07001745void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1746{
1747 if (!blk_mq_hctx_stopped(hctx))
1748 return;
1749
1750 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1751 blk_mq_run_hw_queue(hctx, async);
1752}
1753EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1754
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001755void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001756{
1757 struct blk_mq_hw_ctx *hctx;
1758 int i;
1759
Jens Axboeae911c52016-12-08 13:19:30 -07001760 queue_for_each_hw_ctx(q, hctx, i)
1761 blk_mq_start_stopped_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001762}
1763EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1764
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001765static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001766{
1767 struct blk_mq_hw_ctx *hctx;
1768
Jens Axboe9f993732017-04-10 09:54:54 -06001769 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboe21c6e932017-04-10 09:54:56 -06001770
1771 /*
Ming Lei15fe8a902018-04-08 17:48:11 +08001772 * If we are stopped, don't run the queue.
Jens Axboe21c6e932017-04-10 09:54:56 -06001773 */
Ming Lei15fe8a902018-04-08 17:48:11 +08001774 if (test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jianchao Wang0196d6b2018-06-04 17:03:55 +08001775 return;
Jens Axboee4043dc2014-04-09 10:18:23 -06001776
Jens Axboe320ae512013-10-24 09:20:05 +01001777 __blk_mq_run_hw_queue(hctx);
1778}
1779
Ming Leicfd0c552015-10-20 23:13:57 +08001780static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08001781 struct request *rq,
1782 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01001783{
Jens Axboee57690f2016-08-24 15:34:35 -06001784 struct blk_mq_ctx *ctx = rq->mq_ctx;
Ming Leic16d6b52018-12-17 08:44:05 -07001785 enum hctx_type type = hctx->type;
Jens Axboee57690f2016-08-24 15:34:35 -06001786
Bart Van Assche7b607812017-06-20 11:15:47 -07001787 lockdep_assert_held(&ctx->lock);
1788
Jens Axboe01b983c2013-11-19 18:59:10 -07001789 trace_block_rq_insert(hctx->queue, rq);
1790
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001791 if (at_head)
Ming Leic16d6b52018-12-17 08:44:05 -07001792 list_add(&rq->queuelist, &ctx->rq_lists[type]);
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001793 else
Ming Leic16d6b52018-12-17 08:44:05 -07001794 list_add_tail(&rq->queuelist, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08001795}
Jens Axboe4bb659b2014-05-09 09:36:49 -06001796
Jens Axboe2c3ad662016-12-14 14:34:47 -07001797void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1798 bool at_head)
Ming Leicfd0c552015-10-20 23:13:57 +08001799{
1800 struct blk_mq_ctx *ctx = rq->mq_ctx;
1801
Bart Van Assche7b607812017-06-20 11:15:47 -07001802 lockdep_assert_held(&ctx->lock);
1803
Jens Axboee57690f2016-08-24 15:34:35 -06001804 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01001805 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001806}
1807
André Almeida105663f2020-01-06 15:08:18 -03001808/**
1809 * blk_mq_request_bypass_insert - Insert a request at dispatch list.
1810 * @rq: Pointer to request to be inserted.
1811 * @run_queue: If we should run the hardware queue after inserting the request.
1812 *
Jens Axboe157f3772017-09-11 16:43:57 -06001813 * Should only be used carefully, when the caller knows we want to
1814 * bypass a potential IO scheduler on the target device.
1815 */
Ming Lei01e99ae2020-02-25 09:04:32 +08001816void blk_mq_request_bypass_insert(struct request *rq, bool at_head,
1817 bool run_queue)
Jens Axboe157f3772017-09-11 16:43:57 -06001818{
Jens Axboeea4f9952018-10-29 15:06:13 -06001819 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe157f3772017-09-11 16:43:57 -06001820
1821 spin_lock(&hctx->lock);
Ming Lei01e99ae2020-02-25 09:04:32 +08001822 if (at_head)
1823 list_add(&rq->queuelist, &hctx->dispatch);
1824 else
1825 list_add_tail(&rq->queuelist, &hctx->dispatch);
Jens Axboe157f3772017-09-11 16:43:57 -06001826 spin_unlock(&hctx->lock);
1827
Ming Leib0850292017-11-02 23:24:34 +08001828 if (run_queue)
1829 blk_mq_run_hw_queue(hctx, false);
Jens Axboe157f3772017-09-11 16:43:57 -06001830}
1831
Jens Axboebd166ef2017-01-17 06:03:22 -07001832void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
1833 struct list_head *list)
Jens Axboe320ae512013-10-24 09:20:05 +01001834
1835{
Ming Lei3f0cedc2018-07-02 17:35:58 +08001836 struct request *rq;
Ming Leic16d6b52018-12-17 08:44:05 -07001837 enum hctx_type type = hctx->type;
Ming Lei3f0cedc2018-07-02 17:35:58 +08001838
Jens Axboe320ae512013-10-24 09:20:05 +01001839 /*
1840 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1841 * offline now
1842 */
Ming Lei3f0cedc2018-07-02 17:35:58 +08001843 list_for_each_entry(rq, list, queuelist) {
Jens Axboee57690f2016-08-24 15:34:35 -06001844 BUG_ON(rq->mq_ctx != ctx);
Ming Lei3f0cedc2018-07-02 17:35:58 +08001845 trace_block_rq_insert(hctx->queue, rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001846 }
Ming Lei3f0cedc2018-07-02 17:35:58 +08001847
1848 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001849 list_splice_tail_init(list, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08001850 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001851 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001852}
1853
Jens Axboe3110fc72018-10-30 12:24:04 -06001854static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
Jens Axboe320ae512013-10-24 09:20:05 +01001855{
1856 struct request *rqa = container_of(a, struct request, queuelist);
1857 struct request *rqb = container_of(b, struct request, queuelist);
1858
Pavel Begunkov7d30a622019-11-29 00:11:53 +03001859 if (rqa->mq_ctx != rqb->mq_ctx)
1860 return rqa->mq_ctx > rqb->mq_ctx;
1861 if (rqa->mq_hctx != rqb->mq_hctx)
1862 return rqa->mq_hctx > rqb->mq_hctx;
Jens Axboe3110fc72018-10-30 12:24:04 -06001863
1864 return blk_rq_pos(rqa) > blk_rq_pos(rqb);
Jens Axboe320ae512013-10-24 09:20:05 +01001865}
1866
1867void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1868{
Jens Axboe320ae512013-10-24 09:20:05 +01001869 LIST_HEAD(list);
Jens Axboe320ae512013-10-24 09:20:05 +01001870
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001871 if (list_empty(&plug->mq_list))
1872 return;
Jens Axboe320ae512013-10-24 09:20:05 +01001873 list_splice_init(&plug->mq_list, &list);
1874
Jens Axboece5b0092018-11-27 17:13:56 -07001875 if (plug->rq_count > 2 && plug->multiple_queues)
1876 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe320ae512013-10-24 09:20:05 +01001877
Dongli Zhangbcc816d2019-04-04 10:57:44 +08001878 plug->rq_count = 0;
1879
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001880 do {
1881 struct list_head rq_list;
1882 struct request *rq, *head_rq = list_entry_rq(list.next);
1883 struct list_head *pos = &head_rq->queuelist; /* skip first */
1884 struct blk_mq_hw_ctx *this_hctx = head_rq->mq_hctx;
1885 struct blk_mq_ctx *this_ctx = head_rq->mq_ctx;
1886 unsigned int depth = 1;
Jens Axboe320ae512013-10-24 09:20:05 +01001887
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001888 list_for_each_continue(pos, &list) {
1889 rq = list_entry_rq(pos);
1890 BUG_ON(!rq->q);
1891 if (rq->mq_hctx != this_hctx || rq->mq_ctx != this_ctx)
1892 break;
1893 depth++;
Jens Axboe320ae512013-10-24 09:20:05 +01001894 }
1895
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001896 list_cut_before(&rq_list, &list, pos);
1897 trace_block_unplug(head_rq->q, depth, !from_schedule);
Jens Axboe67cae4c2018-10-30 11:31:51 -06001898 blk_mq_sched_insert_requests(this_hctx, this_ctx, &rq_list,
Jens Axboebd166ef2017-01-17 06:03:22 -07001899 from_schedule);
Pavel Begunkov95ed0c52019-11-29 00:11:55 +03001900 } while(!list_empty(&list));
Jens Axboe320ae512013-10-24 09:20:05 +01001901}
1902
Christoph Hellwig14ccb662019-06-06 12:29:01 +02001903static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
1904 unsigned int nr_segs)
Jens Axboe320ae512013-10-24 09:20:05 +01001905{
Christoph Hellwigf924cdd2019-06-06 12:29:00 +02001906 if (bio->bi_opf & REQ_RAHEAD)
1907 rq->cmd_flags |= REQ_FAILFAST_MASK;
1908
1909 rq->__sector = bio->bi_iter.bi_sector;
1910 rq->write_hint = bio->bi_write_hint;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02001911 blk_rq_bio_prep(rq, bio, nr_segs);
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00001912 blk_crypto_rq_bio_prep(rq, bio, GFP_NOIO);
Jens Axboe4b570522014-05-29 11:00:11 -06001913
Konstantin Khlebnikovb5af37a2020-05-27 07:24:16 +02001914 blk_account_io_start(rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001915}
1916
Mike Snitzer0f955492018-01-17 11:25:56 -05001917static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
1918 struct request *rq,
Jens Axboebe94f052018-11-24 10:15:46 -07001919 blk_qc_t *cookie, bool last)
Shaohua Lif984df12015-05-08 10:51:32 -07001920{
Shaohua Lif984df12015-05-08 10:51:32 -07001921 struct request_queue *q = rq->q;
Shaohua Lif984df12015-05-08 10:51:32 -07001922 struct blk_mq_queue_data bd = {
1923 .rq = rq,
Jens Axboebe94f052018-11-24 10:15:46 -07001924 .last = last,
Shaohua Lif984df12015-05-08 10:51:32 -07001925 };
Jens Axboebd166ef2017-01-17 06:03:22 -07001926 blk_qc_t new_cookie;
Jens Axboef06345a2017-06-12 11:22:46 -06001927 blk_status_t ret;
Mike Snitzer0f955492018-01-17 11:25:56 -05001928
1929 new_cookie = request_to_qc_t(hctx, rq);
1930
1931 /*
1932 * For OK queue, we are done. For error, caller may kill it.
1933 * Any other error (busy), just add it to our list as we
1934 * previously would have done.
1935 */
1936 ret = q->mq_ops->queue_rq(hctx, &bd);
1937 switch (ret) {
1938 case BLK_STS_OK:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001939 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001940 *cookie = new_cookie;
1941 break;
1942 case BLK_STS_RESOURCE:
Ming Lei86ff7c22018-01-30 22:04:57 -05001943 case BLK_STS_DEV_RESOURCE:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001944 blk_mq_update_dispatch_busy(hctx, true);
Mike Snitzer0f955492018-01-17 11:25:56 -05001945 __blk_mq_requeue_request(rq);
1946 break;
1947 default:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001948 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001949 *cookie = BLK_QC_T_NONE;
1950 break;
1951 }
1952
1953 return ret;
1954}
1955
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001956static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
Mike Snitzer0f955492018-01-17 11:25:56 -05001957 struct request *rq,
Ming Lei396eaf22018-01-17 11:25:57 -05001958 blk_qc_t *cookie,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001959 bool bypass_insert, bool last)
Mike Snitzer0f955492018-01-17 11:25:56 -05001960{
1961 struct request_queue *q = rq->q;
Ming Leid964f042017-06-06 23:22:00 +08001962 bool run_queue = true;
1963
Ming Lei23d4ee12018-01-18 12:06:59 +08001964 /*
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001965 * RCU or SRCU read lock is needed before checking quiesced flag.
Ming Lei23d4ee12018-01-18 12:06:59 +08001966 *
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001967 * When queue is stopped or quiesced, ignore 'bypass_insert' from
1968 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
1969 * and avoid driver to try to dispatch again.
Ming Lei23d4ee12018-01-18 12:06:59 +08001970 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001971 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
Ming Leid964f042017-06-06 23:22:00 +08001972 run_queue = false;
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001973 bypass_insert = false;
1974 goto insert;
Ming Leid964f042017-06-06 23:22:00 +08001975 }
Shaohua Lif984df12015-05-08 10:51:32 -07001976
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001977 if (q->elevator && !bypass_insert)
1978 goto insert;
Bart Van Assche2253efc2016-10-28 17:20:02 -07001979
Ming Lei65c76362020-06-30 18:24:56 +08001980 if (!blk_mq_get_dispatch_budget(q))
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001981 goto insert;
Jens Axboebd166ef2017-01-17 06:03:22 -07001982
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001983 if (!blk_mq_get_driver_tag(rq)) {
Ming Lei65c76362020-06-30 18:24:56 +08001984 blk_mq_put_dispatch_budget(q);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001985 goto insert;
Ming Lei88022d72017-11-05 02:21:12 +08001986 }
Ming Leide148292017-10-14 17:22:29 +08001987
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001988 return __blk_mq_issue_directly(hctx, rq, cookie, last);
1989insert:
1990 if (bypass_insert)
1991 return BLK_STS_RESOURCE;
1992
Ming Lei01e99ae2020-02-25 09:04:32 +08001993 blk_mq_request_bypass_insert(rq, false, run_queue);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001994 return BLK_STS_OK;
1995}
1996
André Almeida105663f2020-01-06 15:08:18 -03001997/**
1998 * blk_mq_try_issue_directly - Try to send a request directly to device driver.
1999 * @hctx: Pointer of the associated hardware queue.
2000 * @rq: Pointer to request to be sent.
2001 * @cookie: Request queue cookie.
2002 *
2003 * If the device has enough resources to accept a new request now, send the
2004 * request directly to device driver. Else, insert at hctx->dispatch queue, so
2005 * we can try send it another time in the future. Requests inserted at this
2006 * queue have higher priority.
2007 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002008static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
2009 struct request *rq, blk_qc_t *cookie)
2010{
2011 blk_status_t ret;
2012 int srcu_idx;
2013
2014 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
2015
2016 hctx_lock(hctx, &srcu_idx);
2017
2018 ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false, true);
2019 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
Ming Lei01e99ae2020-02-25 09:04:32 +08002020 blk_mq_request_bypass_insert(rq, false, true);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002021 else if (ret != BLK_STS_OK)
2022 blk_mq_end_request(rq, ret);
2023
Jens Axboe04ced152018-01-09 08:29:46 -08002024 hctx_unlock(hctx, srcu_idx);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002025}
2026
2027blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
2028{
2029 blk_status_t ret;
2030 int srcu_idx;
2031 blk_qc_t unused_cookie;
2032 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
2033
2034 hctx_lock(hctx, &srcu_idx);
2035 ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true, last);
2036 hctx_unlock(hctx, srcu_idx);
Jianchao Wang7f556a42018-12-14 09:28:18 +08002037
2038 return ret;
Christoph Hellwig5eb61262017-03-22 15:01:51 -04002039}
2040
Ming Lei6ce3dd62018-07-10 09:03:31 +08002041void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
2042 struct list_head *list)
2043{
Keith Busch536167d42020-04-07 03:13:48 +09002044 int queued = 0;
2045
Ming Lei6ce3dd62018-07-10 09:03:31 +08002046 while (!list_empty(list)) {
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002047 blk_status_t ret;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002048 struct request *rq = list_first_entry(list, struct request,
2049 queuelist);
2050
2051 list_del_init(&rq->queuelist);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002052 ret = blk_mq_request_issue_directly(rq, list_empty(list));
2053 if (ret != BLK_STS_OK) {
2054 if (ret == BLK_STS_RESOURCE ||
2055 ret == BLK_STS_DEV_RESOURCE) {
Ming Lei01e99ae2020-02-25 09:04:32 +08002056 blk_mq_request_bypass_insert(rq, false,
Jens Axboec616cbe2018-12-06 22:17:44 -07002057 list_empty(list));
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002058 break;
2059 }
2060 blk_mq_end_request(rq, ret);
Keith Busch536167d42020-04-07 03:13:48 +09002061 } else
2062 queued++;
Ming Lei6ce3dd62018-07-10 09:03:31 +08002063 }
Jens Axboed666ba92018-11-27 17:02:25 -07002064
2065 /*
2066 * If we didn't flush the entire list, we could have told
2067 * the driver there was more coming, but that turned out to
2068 * be a lie.
2069 */
Keith Busch536167d42020-04-07 03:13:48 +09002070 if (!list_empty(list) && hctx->queue->mq_ops->commit_rqs && queued)
Jens Axboed666ba92018-11-27 17:02:25 -07002071 hctx->queue->mq_ops->commit_rqs(hctx);
Ming Lei6ce3dd62018-07-10 09:03:31 +08002072}
2073
Jens Axboece5b0092018-11-27 17:13:56 -07002074static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
2075{
2076 list_add_tail(&rq->queuelist, &plug->mq_list);
2077 plug->rq_count++;
2078 if (!plug->multiple_queues && !list_is_singular(&plug->mq_list)) {
2079 struct request *tmp;
2080
2081 tmp = list_first_entry(&plug->mq_list, struct request,
2082 queuelist);
2083 if (tmp->q != rq->q)
2084 plug->multiple_queues = true;
2085 }
2086}
2087
André Almeida105663f2020-01-06 15:08:18 -03002088/**
2089 * blk_mq_make_request - Create and send a request to block device.
2090 * @q: Request queue pointer.
2091 * @bio: Bio pointer.
2092 *
2093 * Builds up a request structure from @q and @bio and send to the device. The
2094 * request may not be queued directly to hardware if:
2095 * * This request can be merged with another one
2096 * * We want to place request at plug queue for possible future merging
2097 * * There is an IO scheduler active at this queue
2098 *
2099 * It will not queue the request if there is an error with the bio, or at the
2100 * request creation.
2101 *
2102 * Returns: Request queue cookie.
2103 */
Christoph Hellwig8cf79612020-04-25 09:53:36 +02002104blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06002105{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06002106 const int is_sync = op_is_sync(bio->bi_opf);
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07002107 const int is_flush_fua = op_is_flush(bio->bi_opf);
Christoph Hellwige6e7abf2020-05-29 15:53:09 +02002108 struct blk_mq_alloc_data data = {
2109 .q = q,
2110 };
Jens Axboe07068d52014-05-22 10:40:51 -06002111 struct request *rq;
Shaohua Lif984df12015-05-08 10:51:32 -07002112 struct blk_plug *plug;
Shaohua Li5b3f3412015-05-08 10:51:33 -07002113 struct request *same_queue_rq = NULL;
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002114 unsigned int nr_segs;
Jens Axboe7b371632015-11-05 10:41:40 -07002115 blk_qc_t cookie;
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002116 blk_status_t ret;
Jens Axboe07068d52014-05-22 10:40:51 -06002117
2118 blk_queue_bounce(q, &bio);
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002119 __blk_queue_split(q, &bio, &nr_segs);
Wen Xiongf36ea502017-05-10 08:54:11 -05002120
Dmitry Monakhove23947b2017-06-29 11:31:11 -07002121 if (!bio_integrity_prep(bio))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002122 goto queue_exit;
Jens Axboe07068d52014-05-22 10:40:51 -06002123
Omar Sandoval87c279e2016-06-01 22:18:48 -07002124 if (!is_flush_fua && !blk_queue_nomerges(q) &&
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002125 blk_attempt_plug_merge(q, bio, nr_segs, &same_queue_rq))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002126 goto queue_exit;
Shaohua Lif984df12015-05-08 10:51:32 -07002127
Christoph Hellwig14ccb662019-06-06 12:29:01 +02002128 if (blk_mq_sched_bio_merge(q, bio, nr_segs))
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002129 goto queue_exit;
Jens Axboebd166ef2017-01-17 06:03:22 -07002130
Christoph Hellwigd5337562018-11-14 17:02:09 +01002131 rq_qos_throttle(q, bio);
Jens Axboe87760e52016-11-09 12:38:14 -07002132
Ming Lei78091672019-01-16 19:08:15 +08002133 data.cmd_flags = bio->bi_opf;
Christoph Hellwige6e7abf2020-05-29 15:53:09 +02002134 rq = __blk_mq_alloc_request(&data);
Jens Axboe87760e52016-11-09 12:38:14 -07002135 if (unlikely(!rq)) {
Josef Bacikc1c80382018-07-03 11:14:59 -04002136 rq_qos_cleanup(q, bio);
Jens Axboe7b6620d2019-08-15 11:09:16 -06002137 if (bio->bi_opf & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -05002138 bio_wouldblock_error(bio);
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002139 goto queue_exit;
Jens Axboe87760e52016-11-09 12:38:14 -07002140 }
2141
Xiaoguang Wangd6f1dda2018-10-23 22:30:50 +08002142 trace_block_getrq(q, bio, bio->bi_opf);
2143
Josef Bacikc1c80382018-07-03 11:14:59 -04002144 rq_qos_track(q, rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06002145
Jens Axboefd2d3322017-01-12 10:04:45 -07002146 cookie = request_to_qc_t(data.hctx, rq);
Jens Axboe07068d52014-05-22 10:40:51 -06002147
Bart Van Assche970d1682019-07-01 08:47:30 -07002148 blk_mq_bio_to_request(rq, bio, nr_segs);
2149
Satya Tangiralaa892c8d2020-05-14 00:37:18 +00002150 ret = blk_crypto_init_request(rq);
2151 if (ret != BLK_STS_OK) {
2152 bio->bi_status = ret;
2153 bio_endio(bio);
2154 blk_mq_free_request(rq);
2155 return BLK_QC_T_NONE;
2156 }
2157
Damien Le Moalb49773e72019-07-11 01:18:31 +09002158 plug = blk_mq_plug(q, bio);
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04002159 if (unlikely(is_flush_fua)) {
André Almeida105663f2020-01-06 15:08:18 -03002160 /* Bypass scheduler for flush requests */
Ming Lei923218f2017-11-02 23:24:38 +08002161 blk_insert_flush(rq);
2162 blk_mq_run_hw_queue(data.hctx, true);
Ming Lei3154df22019-09-27 15:24:31 +08002163 } else if (plug && (q->nr_hw_queues == 1 || q->mq_ops->commit_rqs ||
2164 !blk_queue_nonrot(q))) {
Jens Axboeb2c5d162018-11-29 10:03:42 -07002165 /*
2166 * Use plugging if we have a ->commit_rqs() hook as well, as
2167 * we know the driver uses bd->last in a smart fashion.
Ming Lei3154df22019-09-27 15:24:31 +08002168 *
2169 * Use normal plugging if this disk is slow HDD, as sequential
2170 * IO may benefit a lot from plug merging.
Jens Axboeb2c5d162018-11-29 10:03:42 -07002171 */
Jens Axboe5f0ed772018-11-23 22:04:33 -07002172 unsigned int request_count = plug->rq_count;
Shaohua Li600271d2016-11-03 17:03:54 -07002173 struct request *last = NULL;
2174
Ming Lei676d0602015-10-20 23:13:56 +08002175 if (!request_count)
Jeff Moyere6c44382015-05-08 10:51:30 -07002176 trace_block_plug(q);
Shaohua Li600271d2016-11-03 17:03:54 -07002177 else
2178 last = list_entry_rq(plug->mq_list.prev);
Jens Axboeb094f892015-11-20 20:29:45 -07002179
Shaohua Li600271d2016-11-03 17:03:54 -07002180 if (request_count >= BLK_MAX_REQUEST_COUNT || (last &&
2181 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
Jeff Moyere6c44382015-05-08 10:51:30 -07002182 blk_flush_plug_list(plug, false);
2183 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01002184 }
Jens Axboeb094f892015-11-20 20:29:45 -07002185
Jens Axboece5b0092018-11-27 17:13:56 -07002186 blk_add_rq_to_plug(plug, rq);
Ming Leia12de1d2019-09-27 15:24:30 +08002187 } else if (q->elevator) {
André Almeida105663f2020-01-06 15:08:18 -03002188 /* Insert the request at the IO scheduler queue */
Ming Leia12de1d2019-09-27 15:24:30 +08002189 blk_mq_sched_insert_request(rq, false, true, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002190 } else if (plug && !blk_queue_nomerges(q)) {
Jens Axboe320ae512013-10-24 09:20:05 +01002191 /*
2192 * We do limited plugging. If the bio can be merged, do that.
2193 * Otherwise the existing request in the plug list will be
2194 * issued. So the plug list will have one request at most
Christoph Hellwig22997222017-03-22 15:01:52 -04002195 * The plug list might get flushed before this. If that happens,
2196 * the plug list is empty, and same_queue_rq is invalid.
Jens Axboe320ae512013-10-24 09:20:05 +01002197 */
Christoph Hellwig22997222017-03-22 15:01:52 -04002198 if (list_empty(&plug->mq_list))
2199 same_queue_rq = NULL;
Jens Axboe4711b572018-11-27 17:07:17 -07002200 if (same_queue_rq) {
Christoph Hellwig22997222017-03-22 15:01:52 -04002201 list_del_init(&same_queue_rq->queuelist);
Jens Axboe4711b572018-11-27 17:07:17 -07002202 plug->rq_count--;
2203 }
Jens Axboece5b0092018-11-27 17:13:56 -07002204 blk_add_rq_to_plug(plug, rq);
Yufen Yuff3b74b2019-03-26 21:19:25 +08002205 trace_block_plug(q);
Christoph Hellwig22997222017-03-22 15:01:52 -04002206
Ming Leidad7a3b2017-06-06 23:21:59 +08002207 if (same_queue_rq) {
Jens Axboeea4f9952018-10-29 15:06:13 -06002208 data.hctx = same_queue_rq->mq_hctx;
Yufen Yuff3b74b2019-03-26 21:19:25 +08002209 trace_block_unplug(q, 1, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002210 blk_mq_try_issue_directly(data.hctx, same_queue_rq,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002211 &cookie);
Ming Leidad7a3b2017-06-06 23:21:59 +08002212 }
Ming Leia12de1d2019-09-27 15:24:30 +08002213 } else if ((q->nr_hw_queues > 1 && is_sync) ||
2214 !data.hctx->dispatch_busy) {
André Almeida105663f2020-01-06 15:08:18 -03002215 /*
2216 * There is no scheduler and we can try to send directly
2217 * to the hardware.
2218 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002219 blk_mq_try_issue_directly(data.hctx, rq, &cookie);
Ming Leiab42f352017-05-26 19:53:19 +08002220 } else {
André Almeida105663f2020-01-06 15:08:18 -03002221 /* Default case. */
huhai8fa9f552018-05-16 08:21:21 -06002222 blk_mq_sched_insert_request(rq, false, true, true);
Ming Leiab42f352017-05-26 19:53:19 +08002223 }
Jens Axboe320ae512013-10-24 09:20:05 +01002224
Jens Axboe7b371632015-11-05 10:41:40 -07002225 return cookie;
Christoph Hellwigac7c5672020-05-16 20:28:01 +02002226queue_exit:
2227 blk_queue_exit(q);
2228 return BLK_QC_T_NONE;
Jens Axboe320ae512013-10-24 09:20:05 +01002229}
Christoph Hellwig8cf79612020-04-25 09:53:36 +02002230EXPORT_SYMBOL_GPL(blk_mq_make_request); /* only for request based dm */
Jens Axboe320ae512013-10-24 09:20:05 +01002231
Jens Axboecc71a6f2017-01-11 14:29:56 -07002232void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2233 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01002234{
2235 struct page *page;
2236
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002237 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002238 int i;
2239
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002240 for (i = 0; i < tags->nr_tags; i++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002241 struct request *rq = tags->static_rqs[i];
2242
2243 if (!rq)
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002244 continue;
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002245 set->ops->exit_request(set, rq, hctx_idx);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002246 tags->static_rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002247 }
2248 }
2249
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002250 while (!list_empty(&tags->page_list)) {
2251 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07002252 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002253 /*
2254 * Remove kmemleak object previously allocated in
Raul E Rangel273938b2019-05-02 13:48:11 -06002255 * blk_mq_alloc_rqs().
Catalin Marinasf75782e2015-09-14 18:16:02 +01002256 */
2257 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01002258 __free_pages(page, page->private);
2259 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002260}
Jens Axboe320ae512013-10-24 09:20:05 +01002261
Jens Axboecc71a6f2017-01-11 14:29:56 -07002262void blk_mq_free_rq_map(struct blk_mq_tags *tags)
2263{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002264 kfree(tags->rqs);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002265 tags->rqs = NULL;
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002266 kfree(tags->static_rqs);
2267 tags->static_rqs = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002268
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002269 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01002270}
2271
Jens Axboecc71a6f2017-01-11 14:29:56 -07002272struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
2273 unsigned int hctx_idx,
2274 unsigned int nr_tags,
2275 unsigned int reserved_tags)
Jens Axboe320ae512013-10-24 09:20:05 +01002276{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002277 struct blk_mq_tags *tags;
Shaohua Li59f082e2017-02-01 09:53:14 -08002278 int node;
Jens Axboe320ae512013-10-24 09:20:05 +01002279
Dongli Zhang7d76f852019-02-27 21:35:01 +08002280 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002281 if (node == NUMA_NO_NODE)
2282 node = set->numa_node;
2283
2284 tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
Shaohua Li24391c02015-01-23 14:18:00 -07002285 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002286 if (!tags)
2287 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002288
Kees Cook590b5b72018-06-12 14:04:20 -07002289 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002290 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
Shaohua Li59f082e2017-02-01 09:53:14 -08002291 node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002292 if (!tags->rqs) {
2293 blk_mq_free_tags(tags);
2294 return NULL;
2295 }
Jens Axboe320ae512013-10-24 09:20:05 +01002296
Kees Cook590b5b72018-06-12 14:04:20 -07002297 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2298 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2299 node);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002300 if (!tags->static_rqs) {
2301 kfree(tags->rqs);
2302 blk_mq_free_tags(tags);
2303 return NULL;
2304 }
2305
Jens Axboecc71a6f2017-01-11 14:29:56 -07002306 return tags;
2307}
2308
2309static size_t order_to_size(unsigned int order)
2310{
2311 return (size_t)PAGE_SIZE << order;
2312}
2313
Tejun Heo1d9bd512018-01-09 08:29:48 -08002314static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2315 unsigned int hctx_idx, int node)
2316{
2317 int ret;
2318
2319 if (set->ops->init_request) {
2320 ret = set->ops->init_request(set, rq, hctx_idx, node);
2321 if (ret)
2322 return ret;
2323 }
2324
Keith Busch12f5b932018-05-29 15:52:28 +02002325 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Tejun Heo1d9bd512018-01-09 08:29:48 -08002326 return 0;
2327}
2328
Jens Axboecc71a6f2017-01-11 14:29:56 -07002329int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2330 unsigned int hctx_idx, unsigned int depth)
2331{
2332 unsigned int i, j, entries_per_page, max_order = 4;
2333 size_t rq_size, left;
Shaohua Li59f082e2017-02-01 09:53:14 -08002334 int node;
2335
Dongli Zhang7d76f852019-02-27 21:35:01 +08002336 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002337 if (node == NUMA_NO_NODE)
2338 node = set->numa_node;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002339
2340 INIT_LIST_HEAD(&tags->page_list);
2341
Jens Axboe320ae512013-10-24 09:20:05 +01002342 /*
2343 * rq_size is the size of the request plus driver payload, rounded
2344 * to the cacheline size
2345 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002346 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01002347 cache_line_size());
Jens Axboecc71a6f2017-01-11 14:29:56 -07002348 left = rq_size * depth;
Jens Axboe320ae512013-10-24 09:20:05 +01002349
Jens Axboecc71a6f2017-01-11 14:29:56 -07002350 for (i = 0; i < depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01002351 int this_order = max_order;
2352 struct page *page;
2353 int to_do;
2354 void *p;
2355
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06002356 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01002357 this_order--;
2358
2359 do {
Shaohua Li59f082e2017-02-01 09:53:14 -08002360 page = alloc_pages_node(node,
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002361 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06002362 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01002363 if (page)
2364 break;
2365 if (!this_order--)
2366 break;
2367 if (order_to_size(this_order) < rq_size)
2368 break;
2369 } while (1);
2370
2371 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002372 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01002373
2374 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002375 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01002376
2377 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002378 /*
2379 * Allow kmemleak to scan these pages as they contain pointers
2380 * to additional allocations like via ops->init_request().
2381 */
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002382 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
Jens Axboe320ae512013-10-24 09:20:05 +01002383 entries_per_page = order_to_size(this_order) / rq_size;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002384 to_do = min(entries_per_page, depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01002385 left -= to_do * rq_size;
2386 for (j = 0; j < to_do; j++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002387 struct request *rq = p;
2388
2389 tags->static_rqs[i] = rq;
Tejun Heo1d9bd512018-01-09 08:29:48 -08002390 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
2391 tags->static_rqs[i] = NULL;
2392 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002393 }
2394
Jens Axboe320ae512013-10-24 09:20:05 +01002395 p += rq_size;
2396 i++;
2397 }
2398 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002399 return 0;
Jens Axboe320ae512013-10-24 09:20:05 +01002400
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002401fail:
Jens Axboecc71a6f2017-01-11 14:29:56 -07002402 blk_mq_free_rqs(set, tags, hctx_idx);
2403 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01002404}
2405
Ming Leibf0beec2020-05-29 15:53:15 +02002406struct rq_iter_data {
2407 struct blk_mq_hw_ctx *hctx;
2408 bool has_rq;
2409};
2410
2411static bool blk_mq_has_request(struct request *rq, void *data, bool reserved)
2412{
2413 struct rq_iter_data *iter_data = data;
2414
2415 if (rq->mq_hctx != iter_data->hctx)
2416 return true;
2417 iter_data->has_rq = true;
2418 return false;
2419}
2420
2421static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)
2422{
2423 struct blk_mq_tags *tags = hctx->sched_tags ?
2424 hctx->sched_tags : hctx->tags;
2425 struct rq_iter_data data = {
2426 .hctx = hctx,
2427 };
2428
2429 blk_mq_all_tag_iter(tags, blk_mq_has_request, &data);
2430 return data.has_rq;
2431}
2432
2433static inline bool blk_mq_last_cpu_in_hctx(unsigned int cpu,
2434 struct blk_mq_hw_ctx *hctx)
2435{
2436 if (cpumask_next_and(-1, hctx->cpumask, cpu_online_mask) != cpu)
2437 return false;
2438 if (cpumask_next_and(cpu, hctx->cpumask, cpu_online_mask) < nr_cpu_ids)
2439 return false;
2440 return true;
2441}
2442
2443static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node)
2444{
2445 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2446 struct blk_mq_hw_ctx, cpuhp_online);
2447
2448 if (!cpumask_test_cpu(cpu, hctx->cpumask) ||
2449 !blk_mq_last_cpu_in_hctx(cpu, hctx))
2450 return 0;
2451
2452 /*
2453 * Prevent new request from being allocated on the current hctx.
2454 *
2455 * The smp_mb__after_atomic() Pairs with the implied barrier in
2456 * test_and_set_bit_lock in sbitmap_get(). Ensures the inactive flag is
2457 * seen once we return from the tag allocator.
2458 */
2459 set_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2460 smp_mb__after_atomic();
2461
2462 /*
2463 * Try to grab a reference to the queue and wait for any outstanding
2464 * requests. If we could not grab a reference the queue has been
2465 * frozen and there are no requests.
2466 */
2467 if (percpu_ref_tryget(&hctx->queue->q_usage_counter)) {
2468 while (blk_mq_hctx_has_requests(hctx))
2469 msleep(5);
2470 percpu_ref_put(&hctx->queue->q_usage_counter);
2471 }
2472
2473 return 0;
2474}
2475
2476static int blk_mq_hctx_notify_online(unsigned int cpu, struct hlist_node *node)
2477{
2478 struct blk_mq_hw_ctx *hctx = hlist_entry_safe(node,
2479 struct blk_mq_hw_ctx, cpuhp_online);
2480
2481 if (cpumask_test_cpu(cpu, hctx->cpumask))
2482 clear_bit(BLK_MQ_S_INACTIVE, &hctx->state);
2483 return 0;
2484}
2485
Jens Axboee57690f2016-08-24 15:34:35 -06002486/*
2487 * 'cpu' is going away. splice any existing rq_list entries from this
2488 * software queue to the hw queue dispatch list, and ensure that it
2489 * gets run.
2490 */
Thomas Gleixner9467f852016-09-22 08:05:17 -06002491static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
Jens Axboe484b4062014-05-21 14:01:15 -06002492{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002493 struct blk_mq_hw_ctx *hctx;
Jens Axboe484b4062014-05-21 14:01:15 -06002494 struct blk_mq_ctx *ctx;
2495 LIST_HEAD(tmp);
Ming Leic16d6b52018-12-17 08:44:05 -07002496 enum hctx_type type;
Jens Axboe484b4062014-05-21 14:01:15 -06002497
Thomas Gleixner9467f852016-09-22 08:05:17 -06002498 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02002499 if (!cpumask_test_cpu(cpu, hctx->cpumask))
2500 return 0;
2501
Jens Axboee57690f2016-08-24 15:34:35 -06002502 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Ming Leic16d6b52018-12-17 08:44:05 -07002503 type = hctx->type;
Jens Axboe484b4062014-05-21 14:01:15 -06002504
2505 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002506 if (!list_empty(&ctx->rq_lists[type])) {
2507 list_splice_init(&ctx->rq_lists[type], &tmp);
Jens Axboe484b4062014-05-21 14:01:15 -06002508 blk_mq_hctx_clear_pending(hctx, ctx);
2509 }
2510 spin_unlock(&ctx->lock);
2511
2512 if (list_empty(&tmp))
Thomas Gleixner9467f852016-09-22 08:05:17 -06002513 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002514
Jens Axboee57690f2016-08-24 15:34:35 -06002515 spin_lock(&hctx->lock);
2516 list_splice_tail_init(&tmp, &hctx->dispatch);
2517 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06002518
2519 blk_mq_run_hw_queue(hctx, true);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002520 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002521}
2522
Thomas Gleixner9467f852016-09-22 08:05:17 -06002523static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
Jens Axboe484b4062014-05-21 14:01:15 -06002524{
Ming Leibf0beec2020-05-29 15:53:15 +02002525 if (!(hctx->flags & BLK_MQ_F_STACKING))
2526 cpuhp_state_remove_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2527 &hctx->cpuhp_online);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002528 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
2529 &hctx->cpuhp_dead);
Jens Axboe484b4062014-05-21 14:01:15 -06002530}
2531
Ming Leic3b4afc2015-06-04 22:25:04 +08002532/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08002533static void blk_mq_exit_hctx(struct request_queue *q,
2534 struct blk_mq_tag_set *set,
2535 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
2536{
Ming Lei8ab0b7d2018-01-09 21:28:29 +08002537 if (blk_mq_hw_queue_mapped(hctx))
2538 blk_mq_tag_idle(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002539
Ming Leif70ced02014-09-25 23:23:47 +08002540 if (set->ops->exit_request)
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002541 set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx);
Ming Leif70ced02014-09-25 23:23:47 +08002542
Ming Lei08e98fc2014-09-25 23:23:38 +08002543 if (set->ops->exit_hctx)
2544 set->ops->exit_hctx(hctx, hctx_idx);
2545
Thomas Gleixner9467f852016-09-22 08:05:17 -06002546 blk_mq_remove_cpuhp(hctx);
Ming Lei2f8f1332019-04-30 09:52:27 +08002547
2548 spin_lock(&q->unused_hctx_lock);
2549 list_add(&hctx->hctx_list, &q->unused_hctx_list);
2550 spin_unlock(&q->unused_hctx_lock);
Ming Lei08e98fc2014-09-25 23:23:38 +08002551}
2552
Ming Lei624dbe42014-05-27 23:35:13 +08002553static void blk_mq_exit_hw_queues(struct request_queue *q,
2554 struct blk_mq_tag_set *set, int nr_queue)
2555{
2556 struct blk_mq_hw_ctx *hctx;
2557 unsigned int i;
2558
2559 queue_for_each_hw_ctx(q, hctx, i) {
2560 if (i == nr_queue)
2561 break;
Jianchao Wang477e19d2018-10-12 18:07:25 +08002562 blk_mq_debugfs_unregister_hctx(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002563 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08002564 }
Ming Lei624dbe42014-05-27 23:35:13 +08002565}
2566
Ming Lei7c6c5b72019-04-30 09:52:26 +08002567static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
2568{
2569 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
2570
2571 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
2572 __alignof__(struct blk_mq_hw_ctx)) !=
2573 sizeof(struct blk_mq_hw_ctx));
2574
2575 if (tag_set->flags & BLK_MQ_F_BLOCKING)
2576 hw_ctx_size += sizeof(struct srcu_struct);
2577
2578 return hw_ctx_size;
2579}
2580
Ming Lei08e98fc2014-09-25 23:23:38 +08002581static int blk_mq_init_hctx(struct request_queue *q,
2582 struct blk_mq_tag_set *set,
2583 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
2584{
Ming Lei7c6c5b72019-04-30 09:52:26 +08002585 hctx->queue_num = hctx_idx;
Ming Lei08e98fc2014-09-25 23:23:38 +08002586
Ming Leibf0beec2020-05-29 15:53:15 +02002587 if (!(hctx->flags & BLK_MQ_F_STACKING))
2588 cpuhp_state_add_instance_nocalls(CPUHP_AP_BLK_MQ_ONLINE,
2589 &hctx->cpuhp_online);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002590 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
2591
2592 hctx->tags = set->tags[hctx_idx];
2593
2594 if (set->ops->init_hctx &&
2595 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
2596 goto unregister_cpu_notifier;
2597
2598 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
2599 hctx->numa_node))
2600 goto exit_hctx;
2601 return 0;
2602
2603 exit_hctx:
2604 if (set->ops->exit_hctx)
2605 set->ops->exit_hctx(hctx, hctx_idx);
2606 unregister_cpu_notifier:
2607 blk_mq_remove_cpuhp(hctx);
2608 return -1;
2609}
2610
2611static struct blk_mq_hw_ctx *
2612blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
2613 int node)
2614{
2615 struct blk_mq_hw_ctx *hctx;
2616 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
2617
2618 hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node);
2619 if (!hctx)
2620 goto fail_alloc_hctx;
2621
2622 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
2623 goto free_hctx;
2624
2625 atomic_set(&hctx->nr_active, 0);
Ming Lei08e98fc2014-09-25 23:23:38 +08002626 if (node == NUMA_NO_NODE)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002627 node = set->numa_node;
2628 hctx->numa_node = node;
Ming Lei08e98fc2014-09-25 23:23:38 +08002629
Jens Axboe9f993732017-04-10 09:54:54 -06002630 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08002631 spin_lock_init(&hctx->lock);
2632 INIT_LIST_HEAD(&hctx->dispatch);
2633 hctx->queue = q;
Jeff Moyer2404e602015-11-03 10:40:06 -05002634 hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08002635
Ming Lei2f8f1332019-04-30 09:52:27 +08002636 INIT_LIST_HEAD(&hctx->hctx_list);
2637
Ming Lei08e98fc2014-09-25 23:23:38 +08002638 /*
2639 * Allocate space for all possible cpus to avoid allocation at
2640 * runtime
2641 */
Johannes Thumshirnd904bfa2017-11-15 17:32:33 -08002642 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
Ming Lei7c6c5b72019-04-30 09:52:26 +08002643 gfp, node);
Ming Lei08e98fc2014-09-25 23:23:38 +08002644 if (!hctx->ctxs)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002645 goto free_cpumask;
Ming Lei08e98fc2014-09-25 23:23:38 +08002646
Jianchao Wang5b202852018-10-12 18:07:26 +08002647 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
Ming Lei7c6c5b72019-04-30 09:52:26 +08002648 gfp, node))
Ming Lei08e98fc2014-09-25 23:23:38 +08002649 goto free_ctxs;
Ming Lei08e98fc2014-09-25 23:23:38 +08002650 hctx->nr_ctx = 0;
2651
Ming Lei5815839b2018-06-25 19:31:47 +08002652 spin_lock_init(&hctx->dispatch_wait_lock);
Jens Axboeeb619fd2017-11-09 08:32:43 -07002653 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
2654 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
2655
Guoqing Jiang754a1572020-03-09 22:41:37 +01002656 hctx->fq = blk_alloc_flush_queue(hctx->numa_node, set->cmd_size, gfp);
Ming Leif70ced02014-09-25 23:23:47 +08002657 if (!hctx->fq)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002658 goto free_bitmap;
Ming Leif70ced02014-09-25 23:23:47 +08002659
Bart Van Assche6a83e742016-11-02 10:09:51 -06002660 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -08002661 init_srcu_struct(hctx->srcu);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002662 blk_mq_hctx_kobj_init(hctx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06002663
Ming Lei7c6c5b72019-04-30 09:52:26 +08002664 return hctx;
Ming Lei08e98fc2014-09-25 23:23:38 +08002665
2666 free_bitmap:
Omar Sandoval88459642016-09-17 08:38:44 -06002667 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08002668 free_ctxs:
2669 kfree(hctx->ctxs);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002670 free_cpumask:
2671 free_cpumask_var(hctx->cpumask);
2672 free_hctx:
2673 kfree(hctx);
2674 fail_alloc_hctx:
2675 return NULL;
Ming Lei08e98fc2014-09-25 23:23:38 +08002676}
2677
Jens Axboe320ae512013-10-24 09:20:05 +01002678static void blk_mq_init_cpu_queues(struct request_queue *q,
2679 unsigned int nr_hw_queues)
2680{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002681 struct blk_mq_tag_set *set = q->tag_set;
2682 unsigned int i, j;
Jens Axboe320ae512013-10-24 09:20:05 +01002683
2684 for_each_possible_cpu(i) {
2685 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
2686 struct blk_mq_hw_ctx *hctx;
Ming Leic16d6b52018-12-17 08:44:05 -07002687 int k;
Jens Axboe320ae512013-10-24 09:20:05 +01002688
Jens Axboe320ae512013-10-24 09:20:05 +01002689 __ctx->cpu = i;
2690 spin_lock_init(&__ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002691 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
2692 INIT_LIST_HEAD(&__ctx->rq_lists[k]);
2693
Jens Axboe320ae512013-10-24 09:20:05 +01002694 __ctx->queue = q;
2695
Jens Axboe320ae512013-10-24 09:20:05 +01002696 /*
2697 * Set local node, IFF we have more than one hw queue. If
2698 * not, we remain on the home node of the device
2699 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06002700 for (j = 0; j < set->nr_maps; j++) {
2701 hctx = blk_mq_map_queue_type(q, j, i);
2702 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
2703 hctx->numa_node = local_memory_node(cpu_to_node(i));
2704 }
Jens Axboe320ae512013-10-24 09:20:05 +01002705 }
2706}
2707
Weiping Zhang03b63b02020-05-07 21:04:22 +08002708static bool __blk_mq_alloc_map_and_request(struct blk_mq_tag_set *set,
2709 int hctx_idx)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002710{
2711 int ret = 0;
2712
2713 set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
2714 set->queue_depth, set->reserved_tags);
2715 if (!set->tags[hctx_idx])
2716 return false;
2717
2718 ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
2719 set->queue_depth);
2720 if (!ret)
2721 return true;
2722
2723 blk_mq_free_rq_map(set->tags[hctx_idx]);
2724 set->tags[hctx_idx] = NULL;
2725 return false;
2726}
2727
2728static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
2729 unsigned int hctx_idx)
2730{
Dan Carpenter4e6db0f2018-11-29 13:56:54 +03002731 if (set->tags && set->tags[hctx_idx]) {
Jens Axboebd166ef2017-01-17 06:03:22 -07002732 blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
2733 blk_mq_free_rq_map(set->tags[hctx_idx]);
2734 set->tags[hctx_idx] = NULL;
2735 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002736}
2737
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002738static void blk_mq_map_swqueue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01002739{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002740 unsigned int i, j, hctx_idx;
Jens Axboe320ae512013-10-24 09:20:05 +01002741 struct blk_mq_hw_ctx *hctx;
2742 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08002743 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002744
2745 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06002746 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01002747 hctx->nr_ctx = 0;
huhaid416c922018-05-18 08:32:30 -06002748 hctx->dispatch_from = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002749 }
2750
2751 /*
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002752 * Map software to hardware queues.
Ming Lei4412efe2018-04-25 04:01:44 +08002753 *
2754 * If the cpu isn't present, the cpu is mapped to first hctx.
Jens Axboe320ae512013-10-24 09:20:05 +01002755 */
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08002756 for_each_possible_cpu(i) {
Ming Lei4412efe2018-04-25 04:01:44 +08002757
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01002758 ctx = per_cpu_ptr(q->queue_ctx, i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06002759 for (j = 0; j < set->nr_maps; j++) {
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002760 if (!set->map[j].nr_queues) {
2761 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2762 HCTX_TYPE_DEFAULT, i);
Ming Leie5edd5f2018-12-18 01:28:56 +08002763 continue;
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002764 }
Ming Leifd689872020-05-07 21:04:08 +08002765 hctx_idx = set->map[j].mq_map[i];
2766 /* unmapped hw queue can be remapped after CPU topo changed */
2767 if (!set->tags[hctx_idx] &&
Weiping Zhang03b63b02020-05-07 21:04:22 +08002768 !__blk_mq_alloc_map_and_request(set, hctx_idx)) {
Ming Leifd689872020-05-07 21:04:08 +08002769 /*
2770 * If tags initialization fail for some hctx,
2771 * that hctx won't be brought online. In this
2772 * case, remap the current ctx to hctx[0] which
2773 * is guaranteed to always have tags allocated
2774 */
2775 set->map[j].mq_map[i] = 0;
2776 }
Ming Leie5edd5f2018-12-18 01:28:56 +08002777
Jens Axboeb3c661b2018-10-30 10:36:06 -06002778 hctx = blk_mq_map_queue_type(q, j, i);
Jianchao Wang8ccdf4a2019-01-24 18:25:32 +08002779 ctx->hctxs[j] = hctx;
Jens Axboeb3c661b2018-10-30 10:36:06 -06002780 /*
2781 * If the CPU is already set in the mask, then we've
2782 * mapped this one already. This can happen if
2783 * devices share queues across queue maps.
2784 */
2785 if (cpumask_test_cpu(i, hctx->cpumask))
2786 continue;
2787
2788 cpumask_set_cpu(i, hctx->cpumask);
2789 hctx->type = j;
2790 ctx->index_hw[hctx->type] = hctx->nr_ctx;
2791 hctx->ctxs[hctx->nr_ctx++] = ctx;
2792
2793 /*
2794 * If the nr_ctx type overflows, we have exceeded the
2795 * amount of sw queues we can support.
2796 */
2797 BUG_ON(!hctx->nr_ctx);
2798 }
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002799
2800 for (; j < HCTX_MAX_TYPES; j++)
2801 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2802 HCTX_TYPE_DEFAULT, i);
Jens Axboe320ae512013-10-24 09:20:05 +01002803 }
Jens Axboe506e9312014-05-07 10:26:44 -06002804
2805 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei4412efe2018-04-25 04:01:44 +08002806 /*
2807 * If no software queues are mapped to this hardware queue,
2808 * disable it and free the request entries.
2809 */
2810 if (!hctx->nr_ctx) {
2811 /* Never unmap queue 0. We need it as a
2812 * fallback in case of a new remap fails
2813 * allocation
2814 */
2815 if (i && set->tags[i])
2816 blk_mq_free_map_and_requests(set, i);
2817
2818 hctx->tags = NULL;
2819 continue;
2820 }
Jens Axboe484b4062014-05-21 14:01:15 -06002821
Ming Lei2a34c082015-04-21 10:00:20 +08002822 hctx->tags = set->tags[i];
2823 WARN_ON(!hctx->tags);
2824
Jens Axboe484b4062014-05-21 14:01:15 -06002825 /*
Chong Yuan889fa312015-04-15 11:39:29 -06002826 * Set the map size to the number of mapped software queues.
2827 * This is more accurate and more efficient than looping
2828 * over all possibly mapped software queues.
2829 */
Omar Sandoval88459642016-09-17 08:38:44 -06002830 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
Chong Yuan889fa312015-04-15 11:39:29 -06002831
2832 /*
Jens Axboe484b4062014-05-21 14:01:15 -06002833 * Initialize batch roundrobin counts
2834 */
Ming Leif82ddf12018-04-08 17:48:10 +08002835 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06002836 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
2837 }
Jens Axboe320ae512013-10-24 09:20:05 +01002838}
2839
Jens Axboe8e8320c2017-06-20 17:56:13 -06002840/*
2841 * Caller needs to ensure that we're either frozen/quiesced, or that
2842 * the queue isn't live yet.
2843 */
Jeff Moyer2404e602015-11-03 10:40:06 -05002844static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06002845{
2846 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06002847 int i;
2848
Jeff Moyer2404e602015-11-03 10:40:06 -05002849 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei97889f92018-06-25 19:31:48 +08002850 if (shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05002851 hctx->flags |= BLK_MQ_F_TAG_SHARED;
Ming Lei97889f92018-06-25 19:31:48 +08002852 else
Jeff Moyer2404e602015-11-03 10:40:06 -05002853 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
2854 }
2855}
2856
Jens Axboe8e8320c2017-06-20 17:56:13 -06002857static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
2858 bool shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05002859{
2860 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06002861
Bart Van Assche705cda92017-04-07 11:16:49 -07002862 lockdep_assert_held(&set->tag_list_lock);
2863
Jens Axboe0d2602c2014-05-13 15:10:52 -06002864 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2865 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05002866 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002867 blk_mq_unfreeze_queue(q);
2868 }
2869}
2870
2871static void blk_mq_del_queue_tag_set(struct request_queue *q)
2872{
2873 struct blk_mq_tag_set *set = q->tag_set;
2874
Jens Axboe0d2602c2014-05-13 15:10:52 -06002875 mutex_lock(&set->tag_list_lock);
Bart Van Assche705cda92017-04-07 11:16:49 -07002876 list_del_rcu(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05002877 if (list_is_singular(&set->tag_list)) {
2878 /* just transitioned to unshared */
2879 set->flags &= ~BLK_MQ_F_TAG_SHARED;
2880 /* update existing queue */
2881 blk_mq_update_tag_set_depth(set, false);
2882 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06002883 mutex_unlock(&set->tag_list_lock);
Roman Pena347c7a2018-06-10 22:38:24 +02002884 INIT_LIST_HEAD(&q->tag_set_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002885}
2886
2887static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
2888 struct request_queue *q)
2889{
Jens Axboe0d2602c2014-05-13 15:10:52 -06002890 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05002891
Jens Axboeff821d22017-11-10 22:05:12 -07002892 /*
2893 * Check to see if we're transitioning to shared (from 1 to 2 queues).
2894 */
2895 if (!list_empty(&set->tag_list) &&
2896 !(set->flags & BLK_MQ_F_TAG_SHARED)) {
Jeff Moyer2404e602015-11-03 10:40:06 -05002897 set->flags |= BLK_MQ_F_TAG_SHARED;
2898 /* update existing queue */
2899 blk_mq_update_tag_set_depth(set, true);
2900 }
2901 if (set->flags & BLK_MQ_F_TAG_SHARED)
2902 queue_set_hctx_shared(q, true);
Bart Van Assche705cda92017-04-07 11:16:49 -07002903 list_add_tail_rcu(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05002904
Jens Axboe0d2602c2014-05-13 15:10:52 -06002905 mutex_unlock(&set->tag_list_lock);
2906}
2907
Ming Lei1db49092018-11-20 09:44:35 +08002908/* All allocations will be freed in release handler of q->mq_kobj */
2909static int blk_mq_alloc_ctxs(struct request_queue *q)
2910{
2911 struct blk_mq_ctxs *ctxs;
2912 int cpu;
2913
2914 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
2915 if (!ctxs)
2916 return -ENOMEM;
2917
2918 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2919 if (!ctxs->queue_ctx)
2920 goto fail;
2921
2922 for_each_possible_cpu(cpu) {
2923 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
2924 ctx->ctxs = ctxs;
2925 }
2926
2927 q->mq_kobj = &ctxs->kobj;
2928 q->queue_ctx = ctxs->queue_ctx;
2929
2930 return 0;
2931 fail:
2932 kfree(ctxs);
2933 return -ENOMEM;
2934}
2935
Ming Leie09aae72015-01-29 20:17:27 +08002936/*
2937 * It is the actual release handler for mq, but we do it from
2938 * request queue's release handler for avoiding use-after-free
2939 * and headache because q->mq_kobj shouldn't have been introduced,
2940 * but we can't group ctx/kctx kobj without it.
2941 */
2942void blk_mq_release(struct request_queue *q)
2943{
Ming Lei2f8f1332019-04-30 09:52:27 +08002944 struct blk_mq_hw_ctx *hctx, *next;
2945 int i;
Ming Leie09aae72015-01-29 20:17:27 +08002946
Ming Lei2f8f1332019-04-30 09:52:27 +08002947 queue_for_each_hw_ctx(q, hctx, i)
2948 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
2949
2950 /* all hctx are in .unused_hctx_list now */
2951 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
2952 list_del_init(&hctx->hctx_list);
Ming Lei6c8b2322017-02-22 18:14:01 +08002953 kobject_put(&hctx->kobj);
Ming Leic3b4afc2015-06-04 22:25:04 +08002954 }
Ming Leie09aae72015-01-29 20:17:27 +08002955
2956 kfree(q->queue_hw_ctx);
2957
Ming Lei7ea5fe32017-02-22 18:14:00 +08002958 /*
2959 * release .mq_kobj and sw queue's kobject now because
2960 * both share lifetime with request queue.
2961 */
2962 blk_mq_sysfs_deinit(q);
Ming Leie09aae72015-01-29 20:17:27 +08002963}
2964
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01002965struct request_queue *blk_mq_init_queue_data(struct blk_mq_tag_set *set,
2966 void *queuedata)
Jens Axboe320ae512013-10-24 09:20:05 +01002967{
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002968 struct request_queue *uninit_q, *q;
2969
Christoph Hellwig3d745ea2020-03-27 09:30:11 +01002970 uninit_q = __blk_alloc_queue(set->numa_node);
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002971 if (!uninit_q)
2972 return ERR_PTR(-ENOMEM);
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01002973 uninit_q->queuedata = queuedata;
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002974
Damien Le Moal737eb782019-09-05 18:51:33 +09002975 /*
2976 * Initialize the queue without an elevator. device_add_disk() will do
2977 * the initialization.
2978 */
2979 q = blk_mq_init_allocated_queue(set, uninit_q, false);
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002980 if (IS_ERR(q))
2981 blk_cleanup_queue(uninit_q);
2982
2983 return q;
2984}
Christoph Hellwig2f227bb2020-03-27 09:30:08 +01002985EXPORT_SYMBOL_GPL(blk_mq_init_queue_data);
2986
2987struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
2988{
2989 return blk_mq_init_queue_data(set, NULL);
2990}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002991EXPORT_SYMBOL(blk_mq_init_queue);
2992
Jens Axboe9316a9e2018-10-15 08:40:37 -06002993/*
2994 * Helper for setting up a queue with mq ops, given queue depth, and
2995 * the passed in mq ops flags.
2996 */
2997struct request_queue *blk_mq_init_sq_queue(struct blk_mq_tag_set *set,
2998 const struct blk_mq_ops *ops,
2999 unsigned int queue_depth,
3000 unsigned int set_flags)
3001{
3002 struct request_queue *q;
3003 int ret;
3004
3005 memset(set, 0, sizeof(*set));
3006 set->ops = ops;
3007 set->nr_hw_queues = 1;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003008 set->nr_maps = 1;
Jens Axboe9316a9e2018-10-15 08:40:37 -06003009 set->queue_depth = queue_depth;
3010 set->numa_node = NUMA_NO_NODE;
3011 set->flags = set_flags;
3012
3013 ret = blk_mq_alloc_tag_set(set);
3014 if (ret)
3015 return ERR_PTR(ret);
3016
3017 q = blk_mq_init_queue(set);
3018 if (IS_ERR(q)) {
3019 blk_mq_free_tag_set(set);
3020 return q;
3021 }
3022
3023 return q;
3024}
3025EXPORT_SYMBOL(blk_mq_init_sq_queue);
3026
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003027static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
3028 struct blk_mq_tag_set *set, struct request_queue *q,
3029 int hctx_idx, int node)
3030{
Ming Lei2f8f1332019-04-30 09:52:27 +08003031 struct blk_mq_hw_ctx *hctx = NULL, *tmp;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003032
Ming Lei2f8f1332019-04-30 09:52:27 +08003033 /* reuse dead hctx first */
3034 spin_lock(&q->unused_hctx_lock);
3035 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
3036 if (tmp->numa_node == node) {
3037 hctx = tmp;
3038 break;
3039 }
3040 }
3041 if (hctx)
3042 list_del_init(&hctx->hctx_list);
3043 spin_unlock(&q->unused_hctx_lock);
3044
3045 if (!hctx)
3046 hctx = blk_mq_alloc_hctx(q, set, node);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003047 if (!hctx)
Ming Lei7c6c5b72019-04-30 09:52:26 +08003048 goto fail;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003049
Ming Lei7c6c5b72019-04-30 09:52:26 +08003050 if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
3051 goto free_hctx;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003052
3053 return hctx;
Ming Lei7c6c5b72019-04-30 09:52:26 +08003054
3055 free_hctx:
3056 kobject_put(&hctx->kobj);
3057 fail:
3058 return NULL;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003059}
3060
Keith Busch868f2f02015-12-17 17:08:14 -07003061static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
3062 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003063{
Jianchao Wange01ad462018-10-12 18:07:28 +08003064 int i, j, end;
Keith Busch868f2f02015-12-17 17:08:14 -07003065 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01003066
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003067 if (q->nr_hw_queues < set->nr_hw_queues) {
3068 struct blk_mq_hw_ctx **new_hctxs;
3069
3070 new_hctxs = kcalloc_node(set->nr_hw_queues,
3071 sizeof(*new_hctxs), GFP_KERNEL,
3072 set->numa_node);
3073 if (!new_hctxs)
3074 return;
3075 if (hctxs)
3076 memcpy(new_hctxs, hctxs, q->nr_hw_queues *
3077 sizeof(*hctxs));
3078 q->queue_hw_ctx = new_hctxs;
Bart Van Asscheac0d6b92019-10-25 09:50:09 -07003079 kfree(hctxs);
3080 hctxs = new_hctxs;
3081 }
3082
Ming Leifb350e02018-01-06 16:27:40 +08003083 /* protect against switching io scheduler */
3084 mutex_lock(&q->sysfs_lock);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003085 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003086 int node;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003087 struct blk_mq_hw_ctx *hctx;
Keith Busch868f2f02015-12-17 17:08:14 -07003088
Dongli Zhang7d76f852019-02-27 21:35:01 +08003089 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003090 /*
3091 * If the hw queue has been mapped to another numa node,
3092 * we need to realloc the hctx. If allocation fails, fallback
3093 * to use the previous one.
3094 */
3095 if (hctxs[i] && (hctxs[i]->numa_node == node))
3096 continue;
Jens Axboe320ae512013-10-24 09:20:05 +01003097
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003098 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
3099 if (hctx) {
Ming Lei2f8f1332019-04-30 09:52:27 +08003100 if (hctxs[i])
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003101 blk_mq_exit_hctx(q, set, hctxs[i], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003102 hctxs[i] = hctx;
3103 } else {
3104 if (hctxs[i])
3105 pr_warn("Allocate new hctx on node %d fails,\
3106 fallback to previous one on node %d\n",
3107 node, hctxs[i]->numa_node);
3108 else
3109 break;
Keith Busch868f2f02015-12-17 17:08:14 -07003110 }
Jens Axboe320ae512013-10-24 09:20:05 +01003111 }
Jianchao Wange01ad462018-10-12 18:07:28 +08003112 /*
3113 * Increasing nr_hw_queues fails. Free the newly allocated
3114 * hctxs and keep the previous q->nr_hw_queues.
3115 */
3116 if (i != set->nr_hw_queues) {
3117 j = q->nr_hw_queues;
3118 end = i;
3119 } else {
3120 j = i;
3121 end = q->nr_hw_queues;
3122 q->nr_hw_queues = set->nr_hw_queues;
3123 }
Jianchao Wang34d11ff2018-10-12 18:07:27 +08003124
Jianchao Wange01ad462018-10-12 18:07:28 +08003125 for (; j < end; j++) {
Keith Busch868f2f02015-12-17 17:08:14 -07003126 struct blk_mq_hw_ctx *hctx = hctxs[j];
3127
3128 if (hctx) {
Jens Axboecc71a6f2017-01-11 14:29:56 -07003129 if (hctx->tags)
3130 blk_mq_free_map_and_requests(set, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003131 blk_mq_exit_hctx(q, set, hctx, j);
Keith Busch868f2f02015-12-17 17:08:14 -07003132 hctxs[j] = NULL;
Keith Busch868f2f02015-12-17 17:08:14 -07003133 }
3134 }
Ming Leifb350e02018-01-06 16:27:40 +08003135 mutex_unlock(&q->sysfs_lock);
Keith Busch868f2f02015-12-17 17:08:14 -07003136}
3137
3138struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
Damien Le Moal737eb782019-09-05 18:51:33 +09003139 struct request_queue *q,
3140 bool elevator_init)
Keith Busch868f2f02015-12-17 17:08:14 -07003141{
Ming Lei66841672016-02-12 15:27:00 +08003142 /* mark the queue as mq asap */
3143 q->mq_ops = set->ops;
3144
Omar Sandoval34dbad52017-03-21 08:56:08 -07003145 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
Stephen Bates720b8cc2017-04-07 06:24:03 -06003146 blk_mq_poll_stats_bkt,
3147 BLK_MQ_POLL_STATS_BKTS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -07003148 if (!q->poll_cb)
3149 goto err_exit;
3150
Ming Lei1db49092018-11-20 09:44:35 +08003151 if (blk_mq_alloc_ctxs(q))
Jes Sorensen41de54c2019-04-19 16:35:44 -04003152 goto err_poll;
Keith Busch868f2f02015-12-17 17:08:14 -07003153
Ming Lei737f98c2017-02-22 18:13:59 +08003154 /* init q->mq_kobj and sw queues' kobjects */
3155 blk_mq_sysfs_init(q);
3156
Ming Lei2f8f1332019-04-30 09:52:27 +08003157 INIT_LIST_HEAD(&q->unused_hctx_list);
3158 spin_lock_init(&q->unused_hctx_lock);
3159
Keith Busch868f2f02015-12-17 17:08:14 -07003160 blk_mq_realloc_hw_ctxs(set, q);
3161 if (!q->nr_hw_queues)
3162 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01003163
Christoph Hellwig287922e2015-10-30 20:57:30 +08003164 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08003165 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01003166
Jens Axboea8908932018-10-16 14:23:06 -06003167 q->tag_set = set;
Jens Axboe320ae512013-10-24 09:20:05 +01003168
Jens Axboe94eddfb2013-11-19 09:25:07 -07003169 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Ming Leicd191812018-12-18 12:15:29 +08003170 if (set->nr_maps > HCTX_TYPE_POLL &&
3171 set->map[HCTX_TYPE_POLL].nr_queues)
Christoph Hellwig6544d222018-12-02 17:46:28 +01003172 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
Jens Axboe320ae512013-10-24 09:20:05 +01003173
Christoph Hellwig1be036e2014-02-07 10:22:39 -08003174 q->sg_reserved_size = INT_MAX;
3175
Mike Snitzer28494502016-09-14 13:28:30 -04003176 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06003177 INIT_LIST_HEAD(&q->requeue_list);
3178 spin_lock_init(&q->requeue_lock);
3179
Jens Axboeeba71762014-05-20 15:17:27 -06003180 q->nr_requests = set->queue_depth;
3181
Jens Axboe64f1c212016-11-14 13:03:03 -07003182 /*
3183 * Default to classic polling
3184 */
Yufen Yu29ece8b2019-03-18 22:44:41 +08003185 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
Jens Axboe64f1c212016-11-14 13:03:03 -07003186
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003187 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe0d2602c2014-05-13 15:10:52 -06003188 blk_mq_add_queue_tag_set(set, q);
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02003189 blk_mq_map_swqueue(q);
Akinobu Mita4593fdb2015-09-27 02:09:20 +09003190
Damien Le Moal737eb782019-09-05 18:51:33 +09003191 if (elevator_init)
3192 elevator_init_mq(q);
Jens Axboed3484992017-01-13 14:43:58 -07003193
Jens Axboe320ae512013-10-24 09:20:05 +01003194 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07003195
Jens Axboe320ae512013-10-24 09:20:05 +01003196err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07003197 kfree(q->queue_hw_ctx);
zhengbin73d9c8d2019-07-23 22:10:42 +08003198 q->nr_hw_queues = 0;
Ming Lei1db49092018-11-20 09:44:35 +08003199 blk_mq_sysfs_deinit(q);
Jes Sorensen41de54c2019-04-19 16:35:44 -04003200err_poll:
3201 blk_stat_free_callback(q->poll_cb);
3202 q->poll_cb = NULL;
Ming Linc7de5722016-05-25 23:23:27 -07003203err_exit:
3204 q->mq_ops = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01003205 return ERR_PTR(-ENOMEM);
3206}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04003207EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01003208
Ming Leic7e2d942019-04-30 09:52:25 +08003209/* tags can _not_ be used after returning from blk_mq_exit_queue */
3210void blk_mq_exit_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01003211{
Ming Lei624dbe42014-05-27 23:35:13 +08003212 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01003213
Jens Axboe0d2602c2014-05-13 15:10:52 -06003214 blk_mq_del_queue_tag_set(q);
Ming Lei624dbe42014-05-27 23:35:13 +08003215 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01003216}
Jens Axboe320ae512013-10-24 09:20:05 +01003217
Jens Axboea5164402014-09-10 09:02:03 -06003218static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
3219{
3220 int i;
3221
Jens Axboecc71a6f2017-01-11 14:29:56 -07003222 for (i = 0; i < set->nr_hw_queues; i++)
Weiping Zhang03b63b02020-05-07 21:04:22 +08003223 if (!__blk_mq_alloc_map_and_request(set, i))
Jens Axboea5164402014-09-10 09:02:03 -06003224 goto out_unwind;
Jens Axboea5164402014-09-10 09:02:03 -06003225
3226 return 0;
3227
3228out_unwind:
3229 while (--i >= 0)
Weiping Zhang2e194422020-05-07 21:03:39 +08003230 blk_mq_free_map_and_requests(set, i);
Jens Axboea5164402014-09-10 09:02:03 -06003231
Jens Axboea5164402014-09-10 09:02:03 -06003232 return -ENOMEM;
3233}
3234
3235/*
3236 * Allocate the request maps associated with this tag_set. Note that this
3237 * may reduce the depth asked for, if memory is tight. set->queue_depth
3238 * will be updated to reflect the allocated depth.
3239 */
Weiping Zhang79fab522020-05-07 21:04:42 +08003240static int blk_mq_alloc_map_and_requests(struct blk_mq_tag_set *set)
Jens Axboea5164402014-09-10 09:02:03 -06003241{
3242 unsigned int depth;
3243 int err;
3244
3245 depth = set->queue_depth;
3246 do {
3247 err = __blk_mq_alloc_rq_maps(set);
3248 if (!err)
3249 break;
3250
3251 set->queue_depth >>= 1;
3252 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
3253 err = -ENOMEM;
3254 break;
3255 }
3256 } while (set->queue_depth);
3257
3258 if (!set->queue_depth || err) {
3259 pr_err("blk-mq: failed to allocate request map\n");
3260 return -ENOMEM;
3261 }
3262
3263 if (depth != set->queue_depth)
3264 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
3265 depth, set->queue_depth);
3266
3267 return 0;
3268}
3269
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003270static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
3271{
Bart Van Assche6e66b492020-03-09 21:26:17 -07003272 /*
3273 * blk_mq_map_queues() and multiple .map_queues() implementations
3274 * expect that set->map[HCTX_TYPE_DEFAULT].nr_queues is set to the
3275 * number of hardware queues.
3276 */
3277 if (set->nr_maps == 1)
3278 set->map[HCTX_TYPE_DEFAULT].nr_queues = set->nr_hw_queues;
3279
Ming Lei59388702018-12-07 11:03:53 +08003280 if (set->ops->map_queues && !is_kdump_kernel()) {
Jens Axboeb3c661b2018-10-30 10:36:06 -06003281 int i;
3282
Ming Lei7d4901a2018-01-06 16:27:39 +08003283 /*
3284 * transport .map_queues is usually done in the following
3285 * way:
3286 *
3287 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
3288 * mask = get_cpu_mask(queue)
3289 * for_each_cpu(cpu, mask)
Jens Axboeb3c661b2018-10-30 10:36:06 -06003290 * set->map[x].mq_map[cpu] = queue;
Ming Lei7d4901a2018-01-06 16:27:39 +08003291 * }
3292 *
3293 * When we need to remap, the table has to be cleared for
3294 * killing stale mapping since one CPU may not be mapped
3295 * to any hw queue.
3296 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06003297 for (i = 0; i < set->nr_maps; i++)
3298 blk_mq_clear_mq_map(&set->map[i]);
Ming Lei7d4901a2018-01-06 16:27:39 +08003299
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003300 return set->ops->map_queues(set);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003301 } else {
3302 BUG_ON(set->nr_maps > 1);
Dongli Zhang7d76f852019-02-27 21:35:01 +08003303 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003304 }
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003305}
3306
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003307static int blk_mq_realloc_tag_set_tags(struct blk_mq_tag_set *set,
3308 int cur_nr_hw_queues, int new_nr_hw_queues)
3309{
3310 struct blk_mq_tags **new_tags;
3311
3312 if (cur_nr_hw_queues >= new_nr_hw_queues)
3313 return 0;
3314
3315 new_tags = kcalloc_node(new_nr_hw_queues, sizeof(struct blk_mq_tags *),
3316 GFP_KERNEL, set->numa_node);
3317 if (!new_tags)
3318 return -ENOMEM;
3319
3320 if (set->tags)
3321 memcpy(new_tags, set->tags, cur_nr_hw_queues *
3322 sizeof(*set->tags));
3323 kfree(set->tags);
3324 set->tags = new_tags;
3325 set->nr_hw_queues = new_nr_hw_queues;
3326
3327 return 0;
3328}
3329
Jens Axboea4391c62014-06-05 15:21:56 -06003330/*
3331 * Alloc a tag set to be associated with one or more request queues.
3332 * May fail with EINVAL for various error conditions. May adjust the
Minwoo Imc018c842018-06-30 22:12:41 +09003333 * requested depth down, if it's too large. In that case, the set
Jens Axboea4391c62014-06-05 15:21:56 -06003334 * value will be stored in set->queue_depth.
3335 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003336int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
3337{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003338 int i, ret;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003339
Bart Van Assche205fb5f2014-10-30 14:45:11 +01003340 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
3341
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003342 if (!set->nr_hw_queues)
3343 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06003344 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003345 return -EINVAL;
3346 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
3347 return -EINVAL;
3348
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02003349 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003350 return -EINVAL;
3351
Ming Leide148292017-10-14 17:22:29 +08003352 if (!set->ops->get_budget ^ !set->ops->put_budget)
3353 return -EINVAL;
3354
Jens Axboea4391c62014-06-05 15:21:56 -06003355 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
3356 pr_info("blk-mq: reduced tag depth to %u\n",
3357 BLK_MQ_MAX_DEPTH);
3358 set->queue_depth = BLK_MQ_MAX_DEPTH;
3359 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003360
Jens Axboeb3c661b2018-10-30 10:36:06 -06003361 if (!set->nr_maps)
3362 set->nr_maps = 1;
3363 else if (set->nr_maps > HCTX_MAX_TYPES)
3364 return -EINVAL;
3365
Shaohua Li6637fad2014-11-30 16:00:58 -08003366 /*
3367 * If a crashdump is active, then we are potentially in a very
3368 * memory constrained environment. Limit us to 1 queue and
3369 * 64 tags to prevent using too much memory.
3370 */
3371 if (is_kdump_kernel()) {
3372 set->nr_hw_queues = 1;
Ming Lei59388702018-12-07 11:03:53 +08003373 set->nr_maps = 1;
Shaohua Li6637fad2014-11-30 16:00:58 -08003374 set->queue_depth = min(64U, set->queue_depth);
3375 }
Keith Busch868f2f02015-12-17 17:08:14 -07003376 /*
Jens Axboe392546a2018-10-29 13:25:27 -06003377 * There is no use for more h/w queues than cpus if we just have
3378 * a single map
Keith Busch868f2f02015-12-17 17:08:14 -07003379 */
Jens Axboe392546a2018-10-29 13:25:27 -06003380 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003381 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08003382
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003383 if (blk_mq_realloc_tag_set_tags(set, 0, set->nr_hw_queues) < 0)
Jens Axboea5164402014-09-10 09:02:03 -06003384 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003385
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003386 ret = -ENOMEM;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003387 for (i = 0; i < set->nr_maps; i++) {
3388 set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
Ming Lei07b35eb2018-12-17 18:42:45 +08003389 sizeof(set->map[i].mq_map[0]),
Jens Axboeb3c661b2018-10-30 10:36:06 -06003390 GFP_KERNEL, set->numa_node);
3391 if (!set->map[i].mq_map)
3392 goto out_free_mq_map;
Ming Lei59388702018-12-07 11:03:53 +08003393 set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003394 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003395
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003396 ret = blk_mq_update_queue_map(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003397 if (ret)
3398 goto out_free_mq_map;
3399
Weiping Zhang79fab522020-05-07 21:04:42 +08003400 ret = blk_mq_alloc_map_and_requests(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003401 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003402 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003403
Jens Axboe0d2602c2014-05-13 15:10:52 -06003404 mutex_init(&set->tag_list_lock);
3405 INIT_LIST_HEAD(&set->tag_list);
3406
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003407 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003408
3409out_free_mq_map:
Jens Axboeb3c661b2018-10-30 10:36:06 -06003410 for (i = 0; i < set->nr_maps; i++) {
3411 kfree(set->map[i].mq_map);
3412 set->map[i].mq_map = NULL;
3413 }
Robert Elliott5676e7b2014-09-02 11:38:44 -05003414 kfree(set->tags);
3415 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003416 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003417}
3418EXPORT_SYMBOL(blk_mq_alloc_tag_set);
3419
3420void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
3421{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003422 int i, j;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003423
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003424 for (i = 0; i < set->nr_hw_queues; i++)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003425 blk_mq_free_map_and_requests(set, i);
Jens Axboe484b4062014-05-21 14:01:15 -06003426
Jens Axboeb3c661b2018-10-30 10:36:06 -06003427 for (j = 0; j < set->nr_maps; j++) {
3428 kfree(set->map[j].mq_map);
3429 set->map[j].mq_map = NULL;
3430 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003431
Ming Lei981bd182014-04-24 00:07:34 +08003432 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05003433 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003434}
3435EXPORT_SYMBOL(blk_mq_free_tag_set);
3436
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003437int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
3438{
3439 struct blk_mq_tag_set *set = q->tag_set;
3440 struct blk_mq_hw_ctx *hctx;
3441 int i, ret;
3442
Jens Axboebd166ef2017-01-17 06:03:22 -07003443 if (!set)
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003444 return -EINVAL;
3445
Aleksei Zakharove5fa8142019-02-08 19:14:05 +03003446 if (q->nr_requests == nr)
3447 return 0;
3448
Jens Axboe70f36b62017-01-19 10:59:07 -07003449 blk_mq_freeze_queue(q);
Ming Lei24f5a902018-01-06 16:27:38 +08003450 blk_mq_quiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003451
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003452 ret = 0;
3453 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07003454 if (!hctx->tags)
3455 continue;
Jens Axboebd166ef2017-01-17 06:03:22 -07003456 /*
3457 * If we're using an MQ scheduler, just update the scheduler
3458 * queue depth. This is similar to what the old code would do.
3459 */
Jens Axboe70f36b62017-01-19 10:59:07 -07003460 if (!hctx->sched_tags) {
weiping zhangc2e82a22017-09-22 23:36:28 +08003461 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
Jens Axboe70f36b62017-01-19 10:59:07 -07003462 false);
3463 } else {
3464 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
3465 nr, true);
3466 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003467 if (ret)
3468 break;
Jens Axboe77f1e0a2019-01-18 10:34:16 -07003469 if (q->elevator && q->elevator->type->ops.depth_updated)
3470 q->elevator->type->ops.depth_updated(hctx);
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003471 }
3472
3473 if (!ret)
3474 q->nr_requests = nr;
3475
Ming Lei24f5a902018-01-06 16:27:38 +08003476 blk_mq_unquiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003477 blk_mq_unfreeze_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003478
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003479 return ret;
3480}
3481
Jianchao Wangd48ece22018-08-21 15:15:03 +08003482/*
3483 * request_queue and elevator_type pair.
3484 * It is just used by __blk_mq_update_nr_hw_queues to cache
3485 * the elevator_type associated with a request_queue.
3486 */
3487struct blk_mq_qe_pair {
3488 struct list_head node;
3489 struct request_queue *q;
3490 struct elevator_type *type;
3491};
3492
3493/*
3494 * Cache the elevator_type in qe pair list and switch the
3495 * io scheduler to 'none'
3496 */
3497static bool blk_mq_elv_switch_none(struct list_head *head,
3498 struct request_queue *q)
3499{
3500 struct blk_mq_qe_pair *qe;
3501
3502 if (!q->elevator)
3503 return true;
3504
3505 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
3506 if (!qe)
3507 return false;
3508
3509 INIT_LIST_HEAD(&qe->node);
3510 qe->q = q;
3511 qe->type = q->elevator->type;
3512 list_add(&qe->node, head);
3513
3514 mutex_lock(&q->sysfs_lock);
3515 /*
3516 * After elevator_switch_mq, the previous elevator_queue will be
3517 * released by elevator_release. The reference of the io scheduler
3518 * module get by elevator_get will also be put. So we need to get
3519 * a reference of the io scheduler module here to prevent it to be
3520 * removed.
3521 */
3522 __module_get(qe->type->elevator_owner);
3523 elevator_switch_mq(q, NULL);
3524 mutex_unlock(&q->sysfs_lock);
3525
3526 return true;
3527}
3528
3529static void blk_mq_elv_switch_back(struct list_head *head,
3530 struct request_queue *q)
3531{
3532 struct blk_mq_qe_pair *qe;
3533 struct elevator_type *t = NULL;
3534
3535 list_for_each_entry(qe, head, node)
3536 if (qe->q == q) {
3537 t = qe->type;
3538 break;
3539 }
3540
3541 if (!t)
3542 return;
3543
3544 list_del(&qe->node);
3545 kfree(qe);
3546
3547 mutex_lock(&q->sysfs_lock);
3548 elevator_switch_mq(q, t);
3549 mutex_unlock(&q->sysfs_lock);
3550}
3551
Keith Busche4dc2b32017-05-30 14:39:11 -04003552static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
3553 int nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003554{
3555 struct request_queue *q;
Jianchao Wangd48ece22018-08-21 15:15:03 +08003556 LIST_HEAD(head);
Jianchao Wange01ad462018-10-12 18:07:28 +08003557 int prev_nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07003558
Bart Van Assche705cda92017-04-07 11:16:49 -07003559 lockdep_assert_held(&set->tag_list_lock);
3560
Jens Axboe392546a2018-10-29 13:25:27 -06003561 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003562 nr_hw_queues = nr_cpu_ids;
Weiping Zhangfe35ec52020-06-17 14:18:37 +08003563 if (nr_hw_queues < 1)
3564 return;
3565 if (set->nr_maps == 1 && nr_hw_queues == set->nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003566 return;
3567
3568 list_for_each_entry(q, &set->tag_list, tag_set_list)
3569 blk_mq_freeze_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +08003570 /*
3571 * Switch IO scheduler to 'none', cleaning up the data associated
3572 * with the previous scheduler. We will switch back once we are done
3573 * updating the new sw to hw queue mappings.
3574 */
3575 list_for_each_entry(q, &set->tag_list, tag_set_list)
3576 if (!blk_mq_elv_switch_none(&head, q))
3577 goto switch_back;
Keith Busch868f2f02015-12-17 17:08:14 -07003578
Jianchao Wang477e19d2018-10-12 18:07:25 +08003579 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3580 blk_mq_debugfs_unregister_hctxs(q);
3581 blk_mq_sysfs_unregister(q);
3582 }
3583
Weiping Zhanga2584e42020-05-07 21:03:56 +08003584 prev_nr_hw_queues = set->nr_hw_queues;
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003585 if (blk_mq_realloc_tag_set_tags(set, set->nr_hw_queues, nr_hw_queues) <
3586 0)
3587 goto reregister;
3588
Keith Busch868f2f02015-12-17 17:08:14 -07003589 set->nr_hw_queues = nr_hw_queues;
Jianchao Wange01ad462018-10-12 18:07:28 +08003590fallback:
Weiping Zhangaa880ad2020-05-13 08:44:05 +08003591 blk_mq_update_queue_map(set);
Keith Busch868f2f02015-12-17 17:08:14 -07003592 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3593 blk_mq_realloc_hw_ctxs(set, q);
Jianchao Wange01ad462018-10-12 18:07:28 +08003594 if (q->nr_hw_queues != set->nr_hw_queues) {
3595 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
3596 nr_hw_queues, prev_nr_hw_queues);
3597 set->nr_hw_queues = prev_nr_hw_queues;
Dongli Zhang7d76f852019-02-27 21:35:01 +08003598 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jianchao Wange01ad462018-10-12 18:07:28 +08003599 goto fallback;
3600 }
Jianchao Wang477e19d2018-10-12 18:07:25 +08003601 blk_mq_map_swqueue(q);
3602 }
3603
Bart Van Asschef7e76db2019-10-25 09:50:10 -07003604reregister:
Jianchao Wang477e19d2018-10-12 18:07:25 +08003605 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3606 blk_mq_sysfs_register(q);
3607 blk_mq_debugfs_register_hctxs(q);
Keith Busch868f2f02015-12-17 17:08:14 -07003608 }
3609
Jianchao Wangd48ece22018-08-21 15:15:03 +08003610switch_back:
3611 list_for_each_entry(q, &set->tag_list, tag_set_list)
3612 blk_mq_elv_switch_back(&head, q);
3613
Keith Busch868f2f02015-12-17 17:08:14 -07003614 list_for_each_entry(q, &set->tag_list, tag_set_list)
3615 blk_mq_unfreeze_queue(q);
3616}
Keith Busche4dc2b32017-05-30 14:39:11 -04003617
3618void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
3619{
3620 mutex_lock(&set->tag_list_lock);
3621 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
3622 mutex_unlock(&set->tag_list_lock);
3623}
Keith Busch868f2f02015-12-17 17:08:14 -07003624EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
3625
Omar Sandoval34dbad52017-03-21 08:56:08 -07003626/* Enable polling stats and return whether they were already enabled. */
3627static bool blk_poll_stats_enable(struct request_queue *q)
3628{
3629 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
Bart Van Assche7dfdbc72018-03-07 17:10:05 -08003630 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
Omar Sandoval34dbad52017-03-21 08:56:08 -07003631 return true;
3632 blk_stat_add_callback(q, q->poll_cb);
3633 return false;
3634}
3635
3636static void blk_mq_poll_stats_start(struct request_queue *q)
3637{
3638 /*
3639 * We don't arm the callback if polling stats are not enabled or the
3640 * callback is already active.
3641 */
3642 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
3643 blk_stat_is_active(q->poll_cb))
3644 return;
3645
3646 blk_stat_activate_msecs(q->poll_cb, 100);
3647}
3648
3649static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
3650{
3651 struct request_queue *q = cb->data;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003652 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -07003653
Stephen Bates720b8cc2017-04-07 06:24:03 -06003654 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
3655 if (cb->stat[bucket].nr_samples)
3656 q->poll_stat[bucket] = cb->stat[bucket];
3657 }
Omar Sandoval34dbad52017-03-21 08:56:08 -07003658}
3659
Jens Axboe64f1c212016-11-14 13:03:03 -07003660static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
Jens Axboe64f1c212016-11-14 13:03:03 -07003661 struct request *rq)
3662{
Jens Axboe64f1c212016-11-14 13:03:03 -07003663 unsigned long ret = 0;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003664 int bucket;
Jens Axboe64f1c212016-11-14 13:03:03 -07003665
3666 /*
3667 * If stats collection isn't on, don't sleep but turn it on for
3668 * future users
3669 */
Omar Sandoval34dbad52017-03-21 08:56:08 -07003670 if (!blk_poll_stats_enable(q))
Jens Axboe64f1c212016-11-14 13:03:03 -07003671 return 0;
3672
3673 /*
Jens Axboe64f1c212016-11-14 13:03:03 -07003674 * As an optimistic guess, use half of the mean service time
3675 * for this type of request. We can (and should) make this smarter.
3676 * For instance, if the completion latencies are tight, we can
3677 * get closer than just half the mean. This is especially
3678 * important on devices where the completion latencies are longer
Stephen Bates720b8cc2017-04-07 06:24:03 -06003679 * than ~10 usec. We do use the stats for the relevant IO size
3680 * if available which does lead to better estimates.
Jens Axboe64f1c212016-11-14 13:03:03 -07003681 */
Stephen Bates720b8cc2017-04-07 06:24:03 -06003682 bucket = blk_mq_poll_stats_bkt(rq);
3683 if (bucket < 0)
3684 return ret;
3685
3686 if (q->poll_stat[bucket].nr_samples)
3687 ret = (q->poll_stat[bucket].mean + 1) / 2;
Jens Axboe64f1c212016-11-14 13:03:03 -07003688
3689 return ret;
3690}
3691
Jens Axboe06426ad2016-11-14 13:01:59 -07003692static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
3693 struct request *rq)
3694{
3695 struct hrtimer_sleeper hs;
3696 enum hrtimer_mode mode;
Jens Axboe64f1c212016-11-14 13:03:03 -07003697 unsigned int nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003698 ktime_t kt;
3699
Jens Axboe76a86f92018-01-10 11:30:56 -07003700 if (rq->rq_flags & RQF_MQ_POLL_SLEPT)
Jens Axboe64f1c212016-11-14 13:03:03 -07003701 return false;
3702
3703 /*
Jens Axboe1052b8a2018-11-26 08:21:49 -07003704 * If we get here, hybrid polling is enabled. Hence poll_nsec can be:
Jens Axboe64f1c212016-11-14 13:03:03 -07003705 *
Jens Axboe64f1c212016-11-14 13:03:03 -07003706 * 0: use half of prev avg
3707 * >0: use this specific value
3708 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07003709 if (q->poll_nsec > 0)
Jens Axboe64f1c212016-11-14 13:03:03 -07003710 nsecs = q->poll_nsec;
3711 else
John Garrycae740a2020-02-26 20:10:15 +08003712 nsecs = blk_mq_poll_nsecs(q, rq);
Jens Axboe64f1c212016-11-14 13:03:03 -07003713
3714 if (!nsecs)
Jens Axboe06426ad2016-11-14 13:01:59 -07003715 return false;
3716
Jens Axboe76a86f92018-01-10 11:30:56 -07003717 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
Jens Axboe06426ad2016-11-14 13:01:59 -07003718
3719 /*
3720 * This will be replaced with the stats tracking code, using
3721 * 'avg_completion_time / 2' as the pre-sleep target.
3722 */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01003723 kt = nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003724
3725 mode = HRTIMER_MODE_REL;
Sebastian Andrzej Siewiordbc16252019-07-26 20:30:50 +02003726 hrtimer_init_sleeper_on_stack(&hs, CLOCK_MONOTONIC, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07003727 hrtimer_set_expires(&hs.timer, kt);
3728
Jens Axboe06426ad2016-11-14 13:01:59 -07003729 do {
Tejun Heo5a61c362018-01-09 08:29:52 -08003730 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
Jens Axboe06426ad2016-11-14 13:01:59 -07003731 break;
3732 set_current_state(TASK_UNINTERRUPTIBLE);
Thomas Gleixner9dd88132019-07-30 21:16:55 +02003733 hrtimer_sleeper_start_expires(&hs, mode);
Jens Axboe06426ad2016-11-14 13:01:59 -07003734 if (hs.task)
3735 io_schedule();
3736 hrtimer_cancel(&hs.timer);
3737 mode = HRTIMER_MODE_ABS;
3738 } while (hs.task && !signal_pending(current));
3739
3740 __set_current_state(TASK_RUNNING);
3741 destroy_hrtimer_on_stack(&hs.timer);
3742 return true;
3743}
3744
Jens Axboe1052b8a2018-11-26 08:21:49 -07003745static bool blk_mq_poll_hybrid(struct request_queue *q,
3746 struct blk_mq_hw_ctx *hctx, blk_qc_t cookie)
Jens Axboebbd7bb72016-11-04 09:34:34 -06003747{
Jens Axboe1052b8a2018-11-26 08:21:49 -07003748 struct request *rq;
3749
Yufen Yu29ece8b2019-03-18 22:44:41 +08003750 if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
Jens Axboe1052b8a2018-11-26 08:21:49 -07003751 return false;
3752
3753 if (!blk_qc_t_is_internal(cookie))
3754 rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
3755 else {
3756 rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
3757 /*
3758 * With scheduling, if the request has completed, we'll
3759 * get a NULL return here, as we clear the sched tag when
3760 * that happens. The request still remains valid, like always,
3761 * so we should be safe with just the NULL check.
3762 */
3763 if (!rq)
3764 return false;
3765 }
3766
John Garrycae740a2020-02-26 20:10:15 +08003767 return blk_mq_poll_hybrid_sleep(q, rq);
Jens Axboe1052b8a2018-11-26 08:21:49 -07003768}
3769
Christoph Hellwig529262d2018-12-02 17:46:26 +01003770/**
3771 * blk_poll - poll for IO completions
3772 * @q: the queue
3773 * @cookie: cookie passed back at IO submission time
3774 * @spin: whether to spin for completions
3775 *
3776 * Description:
3777 * Poll for completions on the passed in queue. Returns number of
3778 * completed entries found. If @spin is true, then blk_poll will continue
3779 * looping until at least one completion is found, unless the task is
3780 * otherwise marked running (or we need to reschedule).
3781 */
3782int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin)
Jens Axboe1052b8a2018-11-26 08:21:49 -07003783{
3784 struct blk_mq_hw_ctx *hctx;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003785 long state;
3786
Christoph Hellwig529262d2018-12-02 17:46:26 +01003787 if (!blk_qc_t_valid(cookie) ||
3788 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
Jens Axboe1052b8a2018-11-26 08:21:49 -07003789 return 0;
3790
Christoph Hellwig529262d2018-12-02 17:46:26 +01003791 if (current->plug)
3792 blk_flush_plug_list(current->plug, false);
3793
Jens Axboe1052b8a2018-11-26 08:21:49 -07003794 hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
3795
Jens Axboe06426ad2016-11-14 13:01:59 -07003796 /*
3797 * If we sleep, have the caller restart the poll loop to reset
3798 * the state. Like for the other success return cases, the
3799 * caller is responsible for checking if the IO completed. If
3800 * the IO isn't complete, we'll get called again and will go
3801 * straight to the busy poll loop.
3802 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07003803 if (blk_mq_poll_hybrid(q, hctx, cookie))
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003804 return 1;
Jens Axboe06426ad2016-11-14 13:01:59 -07003805
Jens Axboebbd7bb72016-11-04 09:34:34 -06003806 hctx->poll_considered++;
3807
3808 state = current->state;
Jens Axboeaa61bec2018-11-13 21:32:10 -07003809 do {
Jens Axboebbd7bb72016-11-04 09:34:34 -06003810 int ret;
3811
3812 hctx->poll_invoked++;
3813
Jens Axboe97431392018-11-16 09:48:21 -07003814 ret = q->mq_ops->poll(hctx);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003815 if (ret > 0) {
3816 hctx->poll_success++;
Jens Axboe849a3702018-11-16 08:37:34 -07003817 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003818 return ret;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003819 }
3820
3821 if (signal_pending_state(state, current))
Jens Axboe849a3702018-11-16 08:37:34 -07003822 __set_current_state(TASK_RUNNING);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003823
3824 if (current->state == TASK_RUNNING)
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003825 return 1;
Jens Axboe0a1b8b82018-11-26 08:24:43 -07003826 if (ret < 0 || !spin)
Jens Axboebbd7bb72016-11-04 09:34:34 -06003827 break;
3828 cpu_relax();
Jens Axboeaa61bec2018-11-13 21:32:10 -07003829 } while (!need_resched());
Jens Axboebbd7bb72016-11-04 09:34:34 -06003830
Nitesh Shetty67b41102018-02-13 21:18:12 +05303831 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003832 return 0;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003833}
Christoph Hellwig529262d2018-12-02 17:46:26 +01003834EXPORT_SYMBOL_GPL(blk_poll);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003835
Jens Axboe9cf2bab2018-10-31 17:01:22 -06003836unsigned int blk_mq_rq_cpu(struct request *rq)
3837{
3838 return rq->mq_ctx->cpu;
3839}
3840EXPORT_SYMBOL(blk_mq_rq_cpu);
3841
Jens Axboe320ae512013-10-24 09:20:05 +01003842static int __init blk_mq_init(void)
3843{
Christoph Hellwigc3077b52020-06-11 08:44:41 +02003844 int i;
3845
3846 for_each_possible_cpu(i)
3847 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
3848 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq);
3849
3850 cpuhp_setup_state_nocalls(CPUHP_BLOCK_SOFTIRQ_DEAD,
3851 "block/softirq:dead", NULL,
3852 blk_softirq_cpu_dead);
Thomas Gleixner9467f852016-09-22 08:05:17 -06003853 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
3854 blk_mq_hctx_notify_dead);
Ming Leibf0beec2020-05-29 15:53:15 +02003855 cpuhp_setup_state_multi(CPUHP_AP_BLK_MQ_ONLINE, "block/mq:online",
3856 blk_mq_hctx_notify_online,
3857 blk_mq_hctx_notify_offline);
Jens Axboe320ae512013-10-24 09:20:05 +01003858 return 0;
3859}
3860subsys_initcall(blk_mq_init);