blob: 08a6248d85365ea87fd414de9577885ee67838bf [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>
Jens Axboe320ae512013-10-24 09:20:05 +010029
30#include <trace/events/block.h>
31
32#include <linux/blk-mq.h>
33#include "blk.h"
34#include "blk-mq.h"
Omar Sandoval9c1051a2017-05-04 08:17:21 -060035#include "blk-mq-debugfs.h"
Jens Axboe320ae512013-10-24 09:20:05 +010036#include "blk-mq-tag.h"
Bart Van Assche986d4132018-09-26 14:01:10 -070037#include "blk-pm.h"
Jens Axboecf43e6b2016-11-07 21:32:37 -070038#include "blk-stat.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070039#include "blk-mq-sched.h"
Josef Bacikc1c80382018-07-03 11:14:59 -040040#include "blk-rq-qos.h"
Jens Axboe320ae512013-10-24 09:20:05 +010041
Omar Sandoval34dbad52017-03-21 08:56:08 -070042static void blk_mq_poll_stats_start(struct request_queue *q);
43static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb);
44
Stephen Bates720b8cc2017-04-07 06:24:03 -060045static int blk_mq_poll_stats_bkt(const struct request *rq)
46{
47 int ddir, bytes, bucket;
48
Jens Axboe99c749a2017-04-21 07:55:42 -060049 ddir = rq_data_dir(rq);
Stephen Bates720b8cc2017-04-07 06:24:03 -060050 bytes = blk_rq_bytes(rq);
51
52 bucket = ddir + 2*(ilog2(bytes) - 9);
53
54 if (bucket < 0)
55 return -1;
56 else if (bucket >= BLK_MQ_POLL_STATS_BKTS)
57 return ddir + BLK_MQ_POLL_STATS_BKTS - 2;
58
59 return bucket;
60}
61
Jens Axboe320ae512013-10-24 09:20:05 +010062/*
Yufen Yu85fae292019-03-24 17:57:08 +080063 * Check if any of the ctx, dispatch list or elevator
64 * have pending work in this hardware queue.
Jens Axboe320ae512013-10-24 09:20:05 +010065 */
Jens Axboe79f720a2017-11-10 09:13:21 -070066static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
Jens Axboe320ae512013-10-24 09:20:05 +010067{
Jens Axboe79f720a2017-11-10 09:13:21 -070068 return !list_empty_careful(&hctx->dispatch) ||
69 sbitmap_any_bit_set(&hctx->ctx_map) ||
Jens Axboebd166ef2017-01-17 06:03:22 -070070 blk_mq_sched_has_work(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +010071}
72
73/*
74 * Mark this ctx as having pending work in this hardware queue
75 */
76static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
77 struct blk_mq_ctx *ctx)
78{
Jens Axboef31967f2018-10-29 13:13:29 -060079 const int bit = ctx->index_hw[hctx->type];
80
81 if (!sbitmap_test_bit(&hctx->ctx_map, bit))
82 sbitmap_set_bit(&hctx->ctx_map, bit);
Jens Axboe1429d7c2014-05-19 09:23:55 -060083}
84
85static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
86 struct blk_mq_ctx *ctx)
87{
Jens Axboef31967f2018-10-29 13:13:29 -060088 const int bit = ctx->index_hw[hctx->type];
89
90 sbitmap_clear_bit(&hctx->ctx_map, bit);
Jens Axboe320ae512013-10-24 09:20:05 +010091}
92
Jens Axboef299b7c2017-08-08 17:51:45 -060093struct mq_inflight {
94 struct hd_struct *part;
95 unsigned int *inflight;
96};
97
Jens Axboe7baa8572018-11-08 10:24:07 -070098static bool blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx,
Jens Axboef299b7c2017-08-08 17:51:45 -060099 struct request *rq, void *priv,
100 bool reserved)
101{
102 struct mq_inflight *mi = priv;
103
Omar Sandoval61318372018-04-26 00:21:58 -0700104 /*
Mikulas Patockae016b782018-12-06 11:41:21 -0500105 * index[0] counts the specific partition that was asked for.
Omar Sandoval61318372018-04-26 00:21:58 -0700106 */
107 if (rq->part == mi->part)
108 mi->inflight[0]++;
Jens Axboe7baa8572018-11-08 10:24:07 -0700109
110 return true;
Jens Axboef299b7c2017-08-08 17:51:45 -0600111}
112
Mikulas Patockae016b782018-12-06 11:41:21 -0500113unsigned int blk_mq_in_flight(struct request_queue *q, struct hd_struct *part)
Jens Axboef299b7c2017-08-08 17:51:45 -0600114{
Mikulas Patockae016b782018-12-06 11:41:21 -0500115 unsigned inflight[2];
Jens Axboef299b7c2017-08-08 17:51:45 -0600116 struct mq_inflight mi = { .part = part, .inflight = inflight, };
117
Jens Axboeb8d62b32017-08-08 17:53:33 -0600118 inflight[0] = inflight[1] = 0;
Jens Axboef299b7c2017-08-08 17:51:45 -0600119 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
Mikulas Patockae016b782018-12-06 11:41:21 -0500120
121 return inflight[0];
Jens Axboef299b7c2017-08-08 17:51:45 -0600122}
123
Jens Axboe7baa8572018-11-08 10:24:07 -0700124static bool blk_mq_check_inflight_rw(struct blk_mq_hw_ctx *hctx,
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700125 struct request *rq, void *priv,
126 bool reserved)
127{
128 struct mq_inflight *mi = priv;
129
130 if (rq->part == mi->part)
131 mi->inflight[rq_data_dir(rq)]++;
Jens Axboe7baa8572018-11-08 10:24:07 -0700132
133 return true;
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700134}
135
136void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,
137 unsigned int inflight[2])
138{
139 struct mq_inflight mi = { .part = part, .inflight = inflight, };
140
141 inflight[0] = inflight[1] = 0;
142 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight_rw, &mi);
143}
144
Ming Lei1671d522017-03-27 20:06:57 +0800145void blk_freeze_queue_start(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +0800146{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200147 int freeze_depth;
Tejun Heocddd5d12014-08-16 08:02:24 -0400148
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200149 freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
150 if (freeze_depth == 1) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400151 percpu_ref_kill(&q->q_usage_counter);
Jens Axboe344e9ff2018-11-15 12:22:51 -0700152 if (queue_is_mq(q))
Ming Lei055f6e12017-11-09 10:49:53 -0800153 blk_mq_run_hw_queues(q, false);
Tejun Heocddd5d12014-08-16 08:02:24 -0400154 }
Tejun Heof3af0202014-11-04 13:52:27 -0500155}
Ming Lei1671d522017-03-27 20:06:57 +0800156EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
Tejun Heof3af0202014-11-04 13:52:27 -0500157
Keith Busch6bae3632017-03-01 14:22:10 -0500158void blk_mq_freeze_queue_wait(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500159{
Dan Williams3ef28e82015-10-21 13:20:12 -0400160 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +0800161}
Keith Busch6bae3632017-03-01 14:22:10 -0500162EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800163
Keith Buschf91328c2017-03-01 14:22:11 -0500164int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
165 unsigned long timeout)
166{
167 return wait_event_timeout(q->mq_freeze_wq,
168 percpu_ref_is_zero(&q->q_usage_counter),
169 timeout);
170}
171EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
Jens Axboe320ae512013-10-24 09:20:05 +0100172
Tejun Heof3af0202014-11-04 13:52:27 -0500173/*
174 * Guarantee no request is in use, so we can change any data structure of
175 * the queue afterward.
176 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400177void blk_freeze_queue(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500178{
Dan Williams3ef28e82015-10-21 13:20:12 -0400179 /*
180 * In the !blk_mq case we are only calling this to kill the
181 * q_usage_counter, otherwise this increases the freeze depth
182 * and waits for it to return to zero. For this reason there is
183 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
184 * exported to drivers as the only user for unfreeze is blk_mq.
185 */
Ming Lei1671d522017-03-27 20:06:57 +0800186 blk_freeze_queue_start(q);
Tejun Heof3af0202014-11-04 13:52:27 -0500187 blk_mq_freeze_queue_wait(q);
188}
Dan Williams3ef28e82015-10-21 13:20:12 -0400189
190void blk_mq_freeze_queue(struct request_queue *q)
191{
192 /*
193 * ...just an alias to keep freeze and unfreeze actions balanced
194 * in the blk_mq_* namespace
195 */
196 blk_freeze_queue(q);
197}
Jens Axboec761d962015-01-02 15:05:12 -0700198EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
Tejun Heof3af0202014-11-04 13:52:27 -0500199
Keith Buschb4c6a022014-12-19 17:54:14 -0700200void blk_mq_unfreeze_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +0100201{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200202 int freeze_depth;
Jens Axboe320ae512013-10-24 09:20:05 +0100203
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200204 freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
205 WARN_ON_ONCE(freeze_depth < 0);
206 if (!freeze_depth) {
Bart Van Asschebdd63162018-09-26 14:01:08 -0700207 percpu_ref_resurrect(&q->q_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100208 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600209 }
Jens Axboe320ae512013-10-24 09:20:05 +0100210}
Keith Buschb4c6a022014-12-19 17:54:14 -0700211EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
Jens Axboe320ae512013-10-24 09:20:05 +0100212
Bart Van Assche852ec802017-06-21 10:55:47 -0700213/*
214 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
215 * mpt3sas driver such that this function can be removed.
216 */
217void blk_mq_quiesce_queue_nowait(struct request_queue *q)
218{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800219 blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
Bart Van Assche852ec802017-06-21 10:55:47 -0700220}
221EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
222
Bart Van Assche6a83e742016-11-02 10:09:51 -0600223/**
Ming Lei69e07c42017-06-06 23:22:07 +0800224 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
Bart Van Assche6a83e742016-11-02 10:09:51 -0600225 * @q: request queue.
226 *
227 * Note: this function does not prevent that the struct request end_io()
Ming Lei69e07c42017-06-06 23:22:07 +0800228 * callback function is invoked. Once this function is returned, we make
229 * sure no dispatch can happen until the queue is unquiesced via
230 * blk_mq_unquiesce_queue().
Bart Van Assche6a83e742016-11-02 10:09:51 -0600231 */
232void blk_mq_quiesce_queue(struct request_queue *q)
233{
234 struct blk_mq_hw_ctx *hctx;
235 unsigned int i;
236 bool rcu = false;
237
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800238 blk_mq_quiesce_queue_nowait(q);
Ming Leif4560ff2017-06-18 14:24:27 -0600239
Bart Van Assche6a83e742016-11-02 10:09:51 -0600240 queue_for_each_hw_ctx(q, hctx, i) {
241 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -0800242 synchronize_srcu(hctx->srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -0600243 else
244 rcu = true;
245 }
246 if (rcu)
247 synchronize_rcu();
248}
249EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
250
Ming Leie4e73912017-06-06 23:22:03 +0800251/*
252 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
253 * @q: request queue.
254 *
255 * This function recovers queue into the state before quiescing
256 * which is done by blk_mq_quiesce_queue.
257 */
258void blk_mq_unquiesce_queue(struct request_queue *q)
259{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800260 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
Ming Leif4560ff2017-06-18 14:24:27 -0600261
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800262 /* dispatch requests which are inserted during quiescing */
263 blk_mq_run_hw_queues(q, true);
Ming Leie4e73912017-06-06 23:22:03 +0800264}
265EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
266
Jens Axboeaed3ea92014-12-22 14:04:42 -0700267void blk_mq_wake_waiters(struct request_queue *q)
268{
269 struct blk_mq_hw_ctx *hctx;
270 unsigned int i;
271
272 queue_for_each_hw_ctx(q, hctx, i)
273 if (blk_mq_hw_queue_mapped(hctx))
274 blk_mq_tag_wakeup_all(hctx->tags, true);
275}
276
Jens Axboe320ae512013-10-24 09:20:05 +0100277bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
278{
279 return blk_mq_has_free_tags(hctx->tags);
280}
281EXPORT_SYMBOL(blk_mq_can_queue);
282
Jens Axboefe1f4522018-11-28 10:50:07 -0700283/*
284 * Only need start/end time stamping if we have stats enabled, or using
285 * an IO scheduler.
286 */
287static inline bool blk_mq_need_time_stamp(struct request *rq)
288{
289 return (rq->rq_flags & RQF_IO_STAT) || rq->q->elevator;
290}
291
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200292static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
293 unsigned int tag, unsigned int op)
Jens Axboe320ae512013-10-24 09:20:05 +0100294{
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200295 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
296 struct request *rq = tags->static_rqs[tag];
Jens Axboebf9ae8c2018-01-14 10:40:45 -0700297 req_flags_t rq_flags = 0;
Bart Van Asschec3a148d2017-06-20 11:15:43 -0700298
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200299 if (data->flags & BLK_MQ_REQ_INTERNAL) {
300 rq->tag = -1;
301 rq->internal_tag = tag;
302 } else {
Jianchao Wangd263ed92018-08-09 08:34:17 -0600303 if (data->hctx->flags & BLK_MQ_F_TAG_SHARED) {
Jens Axboebf9ae8c2018-01-14 10:40:45 -0700304 rq_flags = RQF_MQ_INFLIGHT;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200305 atomic_inc(&data->hctx->nr_active);
306 }
307 rq->tag = tag;
308 rq->internal_tag = -1;
309 data->hctx->tags->rqs[rq->tag] = rq;
310 }
311
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200312 /* csd/requeue_work/fifo_time is initialized before use */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200313 rq->q = data->q;
314 rq->mq_ctx = data->ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600315 rq->mq_hctx = data->hctx;
Jens Axboebf9ae8c2018-01-14 10:40:45 -0700316 rq->rq_flags = rq_flags;
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600317 rq->cmd_flags = op;
Bart Van Assche1b6d65a2017-11-09 10:49:55 -0800318 if (data->flags & BLK_MQ_REQ_PREEMPT)
319 rq->rq_flags |= RQF_PREEMPT;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200320 if (blk_queue_io_stat(data->q))
Christoph Hellwige8064022016-10-20 15:12:13 +0200321 rq->rq_flags |= RQF_IO_STAT;
Jens Axboe7c3fb702018-01-10 11:46:39 -0700322 INIT_LIST_HEAD(&rq->queuelist);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200323 INIT_HLIST_NODE(&rq->hash);
324 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200325 rq->rq_disk = NULL;
326 rq->part = NULL;
Jens Axboefe1f4522018-11-28 10:50:07 -0700327 if (blk_mq_need_time_stamp(rq))
328 rq->start_time_ns = ktime_get_ns();
329 else
330 rq->start_time_ns = 0;
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700331 rq->io_start_time_ns = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200332 rq->nr_phys_segments = 0;
333#if defined(CONFIG_BLK_DEV_INTEGRITY)
334 rq->nr_integrity_segments = 0;
335#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200336 /* tag was already set */
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200337 rq->extra_len = 0;
Christoph Hellwig079076b2018-11-14 17:02:05 +0100338 WRITE_ONCE(rq->deadline, 0);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200339
Jens Axboef6be4fb2014-06-06 11:03:48 -0600340 rq->timeout = 0;
341
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200342 rq->end_io = NULL;
343 rq->end_io_data = NULL;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200344
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200345 data->ctx->rq_dispatched[op_is_sync(op)]++;
Keith Busch12f5b932018-05-29 15:52:28 +0200346 refcount_set(&rq->ref, 1);
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200347 return rq;
Jens Axboe320ae512013-10-24 09:20:05 +0100348}
349
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200350static struct request *blk_mq_get_request(struct request_queue *q,
Jens Axboef9afca42018-10-29 13:11:38 -0600351 struct bio *bio,
352 struct blk_mq_alloc_data *data)
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200353{
354 struct elevator_queue *e = q->elevator;
355 struct request *rq;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200356 unsigned int tag;
Bart Van Assche21e768b2017-10-16 16:32:26 -0700357 bool put_ctx_on_error = false;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200358
359 blk_queue_enter_live(q);
360 data->q = q;
Bart Van Assche21e768b2017-10-16 16:32:26 -0700361 if (likely(!data->ctx)) {
362 data->ctx = blk_mq_get_ctx(q);
363 put_ctx_on_error = true;
364 }
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200365 if (likely(!data->hctx))
Jens Axboef9afca42018-10-29 13:11:38 -0600366 data->hctx = blk_mq_map_queue(q, data->cmd_flags,
Jianchao Wang8ccdf4a2019-01-24 18:25:32 +0800367 data->ctx);
Jens Axboef9afca42018-10-29 13:11:38 -0600368 if (data->cmd_flags & REQ_NOWAIT)
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500369 data->flags |= BLK_MQ_REQ_NOWAIT;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200370
371 if (e) {
372 data->flags |= BLK_MQ_REQ_INTERNAL;
373
374 /*
375 * Flush requests are special and go directly to the
Jens Axboe17a51192018-05-09 13:28:50 -0600376 * dispatch list. Don't include reserved tags in the
377 * limiting, as it isn't useful.
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200378 */
Jens Axboef9afca42018-10-29 13:11:38 -0600379 if (!op_is_flush(data->cmd_flags) &&
380 e->type->ops.limit_depth &&
Jens Axboe17a51192018-05-09 13:28:50 -0600381 !(data->flags & BLK_MQ_REQ_RESERVED))
Jens Axboef9afca42018-10-29 13:11:38 -0600382 e->type->ops.limit_depth(data->cmd_flags, data);
Jianchao Wangd263ed92018-08-09 08:34:17 -0600383 } else {
384 blk_mq_tag_busy(data->hctx);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200385 }
386
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200387 tag = blk_mq_get_tag(data);
388 if (tag == BLK_MQ_TAG_FAIL) {
Bart Van Assche21e768b2017-10-16 16:32:26 -0700389 if (put_ctx_on_error) {
390 blk_mq_put_ctx(data->ctx);
Ming Lei1ad43c02017-08-02 08:01:45 +0800391 data->ctx = NULL;
392 }
Christoph Hellwig037cebb2017-06-16 18:15:23 +0200393 blk_queue_exit(q);
394 return NULL;
395 }
396
Jens Axboef9afca42018-10-29 13:11:38 -0600397 rq = blk_mq_rq_ctx_init(data, tag, data->cmd_flags);
398 if (!op_is_flush(data->cmd_flags)) {
Christoph Hellwig037cebb2017-06-16 18:15:23 +0200399 rq->elv.icq = NULL;
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600400 if (e && e->type->ops.prepare_request) {
Damien Le Moale2b3fa52018-11-20 10:52:34 +0900401 if (e->type->icq_cache)
402 blk_mq_sched_assign_ioc(rq);
Christoph Hellwig44e8c2b2017-06-16 18:15:25 +0200403
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600404 e->type->ops.prepare_request(rq, bio);
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200405 rq->rq_flags |= RQF_ELVPRIV;
Christoph Hellwig44e8c2b2017-06-16 18:15:25 +0200406 }
Christoph Hellwig037cebb2017-06-16 18:15:23 +0200407 }
408 data->hctx->queued++;
409 return rq;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200410}
411
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700412struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800413 blk_mq_req_flags_t flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100414{
Jens Axboef9afca42018-10-29 13:11:38 -0600415 struct blk_mq_alloc_data alloc_data = { .flags = flags, .cmd_flags = op };
Jens Axboebd166ef2017-01-17 06:03:22 -0700416 struct request *rq;
Joe Lawrencea492f072014-08-28 08:15:21 -0600417 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100418
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800419 ret = blk_queue_enter(q, flags);
Joe Lawrencea492f072014-08-28 08:15:21 -0600420 if (ret)
421 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100422
Jens Axboef9afca42018-10-29 13:11:38 -0600423 rq = blk_mq_get_request(q, NULL, &alloc_data);
Keith Busch3280d662017-08-14 16:40:11 -0400424 blk_queue_exit(q);
Jens Axboe841bac22016-09-21 10:08:43 -0600425
Jens Axboebd166ef2017-01-17 06:03:22 -0700426 if (!rq)
Joe Lawrencea492f072014-08-28 08:15:21 -0600427 return ERR_PTR(-EWOULDBLOCK);
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200428
Ming Lei1ad43c02017-08-02 08:01:45 +0800429 blk_mq_put_ctx(alloc_data.ctx);
Ming Lei1ad43c02017-08-02 08:01:45 +0800430
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200431 rq->__data_len = 0;
432 rq->__sector = (sector_t) -1;
433 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100434 return rq;
435}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600436EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100437
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700438struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800439 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
Ming Lin1f5bd332016-06-13 16:45:21 +0200440{
Jens Axboef9afca42018-10-29 13:11:38 -0600441 struct blk_mq_alloc_data alloc_data = { .flags = flags, .cmd_flags = op };
Ming Lin1f5bd332016-06-13 16:45:21 +0200442 struct request *rq;
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800443 unsigned int cpu;
Ming Lin1f5bd332016-06-13 16:45:21 +0200444 int ret;
445
446 /*
447 * If the tag allocator sleeps we could get an allocation for a
448 * different hardware context. No need to complicate the low level
449 * allocator for this for the rare use case of a command tied to
450 * a specific queue.
451 */
452 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
453 return ERR_PTR(-EINVAL);
454
455 if (hctx_idx >= q->nr_hw_queues)
456 return ERR_PTR(-EIO);
457
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800458 ret = blk_queue_enter(q, flags);
Ming Lin1f5bd332016-06-13 16:45:21 +0200459 if (ret)
460 return ERR_PTR(ret);
461
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600462 /*
463 * Check if the hardware context is actually mapped to anything.
464 * If not tell the caller that it should skip this queue.
465 */
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800466 alloc_data.hctx = q->queue_hw_ctx[hctx_idx];
467 if (!blk_mq_hw_queue_mapped(alloc_data.hctx)) {
468 blk_queue_exit(q);
469 return ERR_PTR(-EXDEV);
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600470 }
Christoph Hellwig20e4d8132018-01-12 10:53:06 +0800471 cpu = cpumask_first_and(alloc_data.hctx->cpumask, cpu_online_mask);
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800472 alloc_data.ctx = __blk_mq_get_ctx(q, cpu);
Ming Lin1f5bd332016-06-13 16:45:21 +0200473
Jens Axboef9afca42018-10-29 13:11:38 -0600474 rq = blk_mq_get_request(q, NULL, &alloc_data);
Keith Busch3280d662017-08-14 16:40:11 -0400475 blk_queue_exit(q);
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800476
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800477 if (!rq)
478 return ERR_PTR(-EWOULDBLOCK);
Ming Lin1f5bd332016-06-13 16:45:21 +0200479
480 return rq;
481}
482EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
483
Keith Busch12f5b932018-05-29 15:52:28 +0200484static void __blk_mq_free_request(struct request *rq)
485{
486 struct request_queue *q = rq->q;
487 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600488 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Keith Busch12f5b932018-05-29 15:52:28 +0200489 const int sched_tag = rq->internal_tag;
490
Bart Van Assche986d4132018-09-26 14:01:10 -0700491 blk_pm_mark_last_busy(rq);
Jens Axboeea4f9952018-10-29 15:06:13 -0600492 rq->mq_hctx = NULL;
Keith Busch12f5b932018-05-29 15:52:28 +0200493 if (rq->tag != -1)
494 blk_mq_put_tag(hctx, hctx->tags, ctx, rq->tag);
495 if (sched_tag != -1)
496 blk_mq_put_tag(hctx, hctx->sched_tags, ctx, sched_tag);
497 blk_mq_sched_restart(hctx);
498 blk_queue_exit(q);
499}
500
Christoph Hellwig6af54052017-06-16 18:15:22 +0200501void blk_mq_free_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100502{
Jens Axboe320ae512013-10-24 09:20:05 +0100503 struct request_queue *q = rq->q;
Christoph Hellwig6af54052017-06-16 18:15:22 +0200504 struct elevator_queue *e = q->elevator;
505 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboeea4f9952018-10-29 15:06:13 -0600506 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +0100507
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200508 if (rq->rq_flags & RQF_ELVPRIV) {
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600509 if (e && e->type->ops.finish_request)
510 e->type->ops.finish_request(rq);
Christoph Hellwig6af54052017-06-16 18:15:22 +0200511 if (rq->elv.icq) {
512 put_io_context(rq->elv.icq->ioc);
513 rq->elv.icq = NULL;
514 }
515 }
516
517 ctx->rq_completed[rq_is_sync(rq)]++;
Christoph Hellwige8064022016-10-20 15:12:13 +0200518 if (rq->rq_flags & RQF_MQ_INFLIGHT)
Jens Axboe0d2602c2014-05-13 15:10:52 -0600519 atomic_dec(&hctx->nr_active);
Jens Axboe87760e52016-11-09 12:38:14 -0700520
Jens Axboe7beb2f82017-09-30 02:08:24 -0600521 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
522 laptop_io_completion(q->backing_dev_info);
523
Josef Bacika7905042018-07-03 09:32:35 -0600524 rq_qos_done(q, rq);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600525
Keith Busch12f5b932018-05-29 15:52:28 +0200526 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
527 if (refcount_dec_and_test(&rq->ref))
528 __blk_mq_free_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100529}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700530EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100531
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200532inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
Jens Axboe320ae512013-10-24 09:20:05 +0100533{
Jens Axboefe1f4522018-11-28 10:50:07 -0700534 u64 now = 0;
535
536 if (blk_mq_need_time_stamp(rq))
537 now = ktime_get_ns();
Omar Sandoval522a7772018-05-09 02:08:53 -0700538
Omar Sandoval4bc63392018-05-09 02:08:52 -0700539 if (rq->rq_flags & RQF_STATS) {
540 blk_mq_poll_stats_start(rq->q);
Omar Sandoval522a7772018-05-09 02:08:53 -0700541 blk_stat_add(rq, now);
Omar Sandoval4bc63392018-05-09 02:08:52 -0700542 }
543
Omar Sandovaled886602018-09-27 15:55:51 -0700544 if (rq->internal_tag != -1)
545 blk_mq_sched_completed_request(rq, now);
546
Omar Sandoval522a7772018-05-09 02:08:53 -0700547 blk_account_io_done(rq, now);
Ming Lei0d11e6a2013-12-05 10:50:39 -0700548
Christoph Hellwig91b63632014-04-16 09:44:53 +0200549 if (rq->end_io) {
Josef Bacika7905042018-07-03 09:32:35 -0600550 rq_qos_done(rq->q, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100551 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200552 } else {
Jens Axboe320ae512013-10-24 09:20:05 +0100553 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200554 }
Jens Axboe320ae512013-10-24 09:20:05 +0100555}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700556EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200557
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200558void blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200559{
560 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
561 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700562 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200563}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700564EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100565
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800566static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100567{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800568 struct request *rq = data;
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600569 struct request_queue *q = rq->q;
Jens Axboe320ae512013-10-24 09:20:05 +0100570
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600571 q->mq_ops->complete(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100572}
573
Christoph Hellwig453f8342017-04-20 16:03:10 +0200574static void __blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100575{
576 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600577 struct request_queue *q = rq->q;
Christoph Hellwig38535202014-04-25 02:32:53 -0700578 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100579 int cpu;
580
Keith Buschaf78ff72018-11-26 09:54:30 -0700581 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
Ming Lei36e76532018-09-28 16:42:20 +0800582 /*
583 * Most of single queue controllers, there is only one irq vector
584 * for handling IO completion, and the only irq's affinity is set
585 * as all possible CPUs. On most of ARCHs, this affinity means the
586 * irq is handled on one specific CPU.
587 *
588 * So complete IO reqeust in softirq context in case of single queue
589 * for not degrading IO performance by irqsoff latency.
590 */
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600591 if (q->nr_hw_queues == 1) {
Ming Lei36e76532018-09-28 16:42:20 +0800592 __blk_complete_request(rq);
593 return;
594 }
595
Jens Axboe4ab32bf2018-11-18 16:15:35 -0700596 /*
597 * For a polled request, always complete locallly, it's pointless
598 * to redirect the completion.
599 */
600 if ((rq->cmd_flags & REQ_HIPRI) ||
601 !test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags)) {
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600602 q->mq_ops->complete(rq);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800603 return;
604 }
Jens Axboe320ae512013-10-24 09:20:05 +0100605
606 cpu = get_cpu();
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600607 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags))
Christoph Hellwig38535202014-04-25 02:32:53 -0700608 shared = cpus_share_cache(cpu, ctx->cpu);
609
610 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800611 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800612 rq->csd.info = rq;
613 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100614 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800615 } else {
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600616 q->mq_ops->complete(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800617 }
Jens Axboe320ae512013-10-24 09:20:05 +0100618 put_cpu();
619}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800620
Jens Axboe04ced152018-01-09 08:29:46 -0800621static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800622 __releases(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800623{
624 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
625 rcu_read_unlock();
626 else
Tejun Heo05707b62018-01-09 08:29:53 -0800627 srcu_read_unlock(hctx->srcu, srcu_idx);
Jens Axboe04ced152018-01-09 08:29:46 -0800628}
629
630static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800631 __acquires(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800632{
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700633 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
634 /* shut up gcc false positive */
635 *srcu_idx = 0;
Jens Axboe04ced152018-01-09 08:29:46 -0800636 rcu_read_lock();
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700637 } else
Tejun Heo05707b62018-01-09 08:29:53 -0800638 *srcu_idx = srcu_read_lock(hctx->srcu);
Jens Axboe04ced152018-01-09 08:29:46 -0800639}
640
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800641/**
642 * blk_mq_complete_request - end I/O on a request
643 * @rq: the request being processed
644 *
645 * Description:
646 * Ends all I/O on a request. It does not handle partial completions.
647 * The actual completion happens out-of-order, through a IPI handler.
648 **/
Keith Busch16c15eb2018-11-26 09:54:28 -0700649bool blk_mq_complete_request(struct request *rq)
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800650{
Keith Busch12f5b932018-05-29 15:52:28 +0200651 if (unlikely(blk_should_fake_timeout(rq->q)))
Keith Busch16c15eb2018-11-26 09:54:28 -0700652 return false;
Keith Busch12f5b932018-05-29 15:52:28 +0200653 __blk_mq_complete_request(rq);
Keith Busch16c15eb2018-11-26 09:54:28 -0700654 return true;
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800655}
656EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100657
Ming Lei1b8f21b72019-04-09 06:31:21 +0800658void blk_mq_complete_request_sync(struct request *rq)
659{
660 WRITE_ONCE(rq->state, MQ_RQ_COMPLETE);
661 rq->q->mq_ops->complete(rq);
662}
663EXPORT_SYMBOL_GPL(blk_mq_complete_request_sync);
664
Keith Busch973c0192015-01-07 18:55:43 -0700665int blk_mq_request_started(struct request *rq)
666{
Tejun Heo5a61c362018-01-09 08:29:52 -0800667 return blk_mq_rq_state(rq) != MQ_RQ_IDLE;
Keith Busch973c0192015-01-07 18:55:43 -0700668}
669EXPORT_SYMBOL_GPL(blk_mq_request_started);
670
Christoph Hellwige2490072014-09-13 16:40:09 -0700671void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100672{
673 struct request_queue *q = rq->q;
674
Jens Axboebd166ef2017-01-17 06:03:22 -0700675 blk_mq_sched_started_request(rq);
676
Jens Axboe320ae512013-10-24 09:20:05 +0100677 trace_block_rq_issue(q, rq);
678
Jens Axboecf43e6b2016-11-07 21:32:37 -0700679 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700680 rq->io_start_time_ns = ktime_get_ns();
681#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
682 rq->throtl_size = blk_rq_sectors(rq);
683#endif
Jens Axboecf43e6b2016-11-07 21:32:37 -0700684 rq->rq_flags |= RQF_STATS;
Josef Bacika7905042018-07-03 09:32:35 -0600685 rq_qos_issue(q, rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700686 }
687
Tejun Heo1d9bd512018-01-09 08:29:48 -0800688 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
Jens Axboe538b7532014-09-16 10:37:37 -0600689
Tejun Heo1d9bd512018-01-09 08:29:48 -0800690 blk_add_timer(rq);
Keith Busch12f5b932018-05-29 15:52:28 +0200691 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800692
693 if (q->dma_drain_size && blk_rq_bytes(rq)) {
694 /*
695 * Make sure space for the drain appears. We know we can do
696 * this because max_hw_segments has been adjusted to be one
697 * fewer than the device can handle.
698 */
699 rq->nr_phys_segments++;
700 }
Jens Axboe320ae512013-10-24 09:20:05 +0100701}
Christoph Hellwige2490072014-09-13 16:40:09 -0700702EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100703
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200704static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100705{
706 struct request_queue *q = rq->q;
707
Ming Lei923218f2017-11-02 23:24:38 +0800708 blk_mq_put_driver_tag(rq);
709
Jens Axboe320ae512013-10-24 09:20:05 +0100710 trace_block_rq_requeue(q, rq);
Josef Bacika7905042018-07-03 09:32:35 -0600711 rq_qos_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800712
Keith Busch12f5b932018-05-29 15:52:28 +0200713 if (blk_mq_request_started(rq)) {
714 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Christoph Hellwigda661262018-06-14 13:58:45 +0200715 rq->rq_flags &= ~RQF_TIMED_OUT;
Christoph Hellwige2490072014-09-13 16:40:09 -0700716 if (q->dma_drain_size && blk_rq_bytes(rq))
717 rq->nr_phys_segments--;
718 }
Jens Axboe320ae512013-10-24 09:20:05 +0100719}
720
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700721void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200722{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200723 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200724
Ming Lei105976f2018-02-23 23:36:56 +0800725 /* this request will be re-inserted to io scheduler queue */
726 blk_mq_sched_requeue_request(rq);
727
Jens Axboe7d692332018-10-24 10:48:12 -0600728 BUG_ON(!list_empty(&rq->queuelist));
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700729 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200730}
731EXPORT_SYMBOL(blk_mq_requeue_request);
732
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600733static void blk_mq_requeue_work(struct work_struct *work)
734{
735 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -0400736 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600737 LIST_HEAD(rq_list);
738 struct request *rq, *next;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600739
Jens Axboe18e97812017-07-27 08:03:57 -0600740 spin_lock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600741 list_splice_init(&q->requeue_list, &rq_list);
Jens Axboe18e97812017-07-27 08:03:57 -0600742 spin_unlock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600743
744 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
Jianchao Wangaef18972019-02-12 09:56:25 +0800745 if (!(rq->rq_flags & (RQF_SOFTBARRIER | RQF_DONTPREP)))
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600746 continue;
747
Christoph Hellwige8064022016-10-20 15:12:13 +0200748 rq->rq_flags &= ~RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600749 list_del_init(&rq->queuelist);
Jianchao Wangaef18972019-02-12 09:56:25 +0800750 /*
751 * If RQF_DONTPREP, rq has contained some driver specific
752 * data, so insert it to hctx dispatch list to avoid any
753 * merge.
754 */
755 if (rq->rq_flags & RQF_DONTPREP)
756 blk_mq_request_bypass_insert(rq, false);
757 else
758 blk_mq_sched_insert_request(rq, true, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600759 }
760
761 while (!list_empty(&rq_list)) {
762 rq = list_entry(rq_list.next, struct request, queuelist);
763 list_del_init(&rq->queuelist);
Mike Snitzer9e97d292018-01-17 11:25:58 -0500764 blk_mq_sched_insert_request(rq, false, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600765 }
766
Bart Van Assche52d7f1b2016-10-28 17:20:32 -0700767 blk_mq_run_hw_queues(q, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600768}
769
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700770void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
771 bool kick_requeue_list)
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600772{
773 struct request_queue *q = rq->q;
774 unsigned long flags;
775
776 /*
777 * We abuse this flag that is otherwise used by the I/O scheduler to
Jens Axboeff821d22017-11-10 22:05:12 -0700778 * request head insertion from the workqueue.
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600779 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200780 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600781
782 spin_lock_irqsave(&q->requeue_lock, flags);
783 if (at_head) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200784 rq->rq_flags |= RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600785 list_add(&rq->queuelist, &q->requeue_list);
786 } else {
787 list_add_tail(&rq->queuelist, &q->requeue_list);
788 }
789 spin_unlock_irqrestore(&q->requeue_lock, flags);
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700790
791 if (kick_requeue_list)
792 blk_mq_kick_requeue_list(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600793}
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600794
795void blk_mq_kick_requeue_list(struct request_queue *q)
796{
Bart Van Asscheae943d22018-01-19 08:58:55 -0800797 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600798}
799EXPORT_SYMBOL(blk_mq_kick_requeue_list);
800
Mike Snitzer28494502016-09-14 13:28:30 -0400801void blk_mq_delay_kick_requeue_list(struct request_queue *q,
802 unsigned long msecs)
803{
Bart Van Assched4acf362017-08-09 11:28:06 -0700804 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
805 msecs_to_jiffies(msecs));
Mike Snitzer28494502016-09-14 13:28:30 -0400806}
807EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
808
Jens Axboe0e62f512014-06-04 10:23:49 -0600809struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
810{
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600811 if (tag < tags->nr_tags) {
812 prefetch(tags->rqs[tag]);
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700813 return tags->rqs[tag];
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600814 }
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700815
816 return NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600817}
818EXPORT_SYMBOL(blk_mq_tag_to_rq);
819
Jens Axboe3c94d832018-12-17 21:11:17 -0700820static bool blk_mq_rq_inflight(struct blk_mq_hw_ctx *hctx, struct request *rq,
821 void *priv, bool reserved)
Jens Axboeae879912018-11-08 09:03:51 -0700822{
823 /*
Jens Axboe3c94d832018-12-17 21:11:17 -0700824 * If we find a request that is inflight and the queue matches,
825 * we know the queue is busy. Return false to stop the iteration.
Jens Axboeae879912018-11-08 09:03:51 -0700826 */
Jens Axboe3c94d832018-12-17 21:11:17 -0700827 if (rq->state == MQ_RQ_IN_FLIGHT && rq->q == hctx->queue) {
Jens Axboeae879912018-11-08 09:03:51 -0700828 bool *busy = priv;
829
830 *busy = true;
831 return false;
832 }
833
834 return true;
835}
836
Jens Axboe3c94d832018-12-17 21:11:17 -0700837bool blk_mq_queue_inflight(struct request_queue *q)
Jens Axboeae879912018-11-08 09:03:51 -0700838{
839 bool busy = false;
840
Jens Axboe3c94d832018-12-17 21:11:17 -0700841 blk_mq_queue_tag_busy_iter(q, blk_mq_rq_inflight, &busy);
Jens Axboeae879912018-11-08 09:03:51 -0700842 return busy;
843}
Jens Axboe3c94d832018-12-17 21:11:17 -0700844EXPORT_SYMBOL_GPL(blk_mq_queue_inflight);
Jens Axboeae879912018-11-08 09:03:51 -0700845
Tejun Heo358f70d2018-01-09 08:29:50 -0800846static void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100847{
Christoph Hellwigda661262018-06-14 13:58:45 +0200848 req->rq_flags |= RQF_TIMED_OUT;
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200849 if (req->q->mq_ops->timeout) {
850 enum blk_eh_timer_return ret;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600851
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200852 ret = req->q->mq_ops->timeout(req, reserved);
853 if (ret == BLK_EH_DONE)
854 return;
855 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700856 }
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200857
858 blk_add_timer(req);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600859}
Keith Busch5b3f25f2015-01-07 18:55:46 -0700860
Keith Busch12f5b932018-05-29 15:52:28 +0200861static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
862{
863 unsigned long deadline;
864
865 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
866 return false;
Christoph Hellwigda661262018-06-14 13:58:45 +0200867 if (rq->rq_flags & RQF_TIMED_OUT)
868 return false;
Keith Busch12f5b932018-05-29 15:52:28 +0200869
Christoph Hellwig079076b2018-11-14 17:02:05 +0100870 deadline = READ_ONCE(rq->deadline);
Keith Busch12f5b932018-05-29 15:52:28 +0200871 if (time_after_eq(jiffies, deadline))
872 return true;
873
874 if (*next == 0)
875 *next = deadline;
876 else if (time_after(*next, deadline))
877 *next = deadline;
878 return false;
879}
880
Jens Axboe7baa8572018-11-08 10:24:07 -0700881static bool blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700882 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100883{
Keith Busch12f5b932018-05-29 15:52:28 +0200884 unsigned long *next = priv;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700885
Keith Busch12f5b932018-05-29 15:52:28 +0200886 /*
887 * Just do a quick check if it is expired before locking the request in
888 * so we're not unnecessarilly synchronizing across CPUs.
889 */
890 if (!blk_mq_req_expired(rq, next))
Jens Axboe7baa8572018-11-08 10:24:07 -0700891 return true;
Jens Axboe320ae512013-10-24 09:20:05 +0100892
Tejun Heo1d9bd512018-01-09 08:29:48 -0800893 /*
Keith Busch12f5b932018-05-29 15:52:28 +0200894 * We have reason to believe the request may be expired. Take a
895 * reference on the request to lock this request lifetime into its
896 * currently allocated context to prevent it from being reallocated in
897 * the event the completion by-passes this timeout handler.
898 *
899 * If the reference was already released, then the driver beat the
900 * timeout handler to posting a natural completion.
Tejun Heo1d9bd512018-01-09 08:29:48 -0800901 */
Keith Busch12f5b932018-05-29 15:52:28 +0200902 if (!refcount_inc_not_zero(&rq->ref))
Jens Axboe7baa8572018-11-08 10:24:07 -0700903 return true;
Keith Busch12f5b932018-05-29 15:52:28 +0200904
905 /*
906 * The request is now locked and cannot be reallocated underneath the
907 * timeout handler's processing. Re-verify this exact request is truly
908 * expired; if it is not expired, then the request was completed and
909 * reallocated as a new request.
910 */
911 if (blk_mq_req_expired(rq, next))
Tejun Heo1d9bd512018-01-09 08:29:48 -0800912 blk_mq_rq_timed_out(rq, reserved);
Keith Busch12f5b932018-05-29 15:52:28 +0200913 if (refcount_dec_and_test(&rq->ref))
914 __blk_mq_free_request(rq);
Jens Axboe7baa8572018-11-08 10:24:07 -0700915
916 return true;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800917}
918
Christoph Hellwig287922e2015-10-30 20:57:30 +0800919static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100920{
Christoph Hellwig287922e2015-10-30 20:57:30 +0800921 struct request_queue *q =
922 container_of(work, struct request_queue, timeout_work);
Keith Busch12f5b932018-05-29 15:52:28 +0200923 unsigned long next = 0;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800924 struct blk_mq_hw_ctx *hctx;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700925 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100926
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600927 /* A deadlock might occur if a request is stuck requiring a
928 * timeout at the same time a queue freeze is waiting
929 * completion, since the timeout code would not be able to
930 * acquire the queue reference here.
931 *
932 * That's why we don't use blk_queue_enter here; instead, we use
933 * percpu_ref_tryget directly, because we need to be able to
934 * obtain a reference even in the short window between the queue
935 * starting to freeze, by dropping the first reference in
Ming Lei1671d522017-03-27 20:06:57 +0800936 * blk_freeze_queue_start, and the moment the last request is
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600937 * consumed, marked by the instant q_usage_counter reaches
938 * zero.
939 */
940 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +0800941 return;
942
Keith Busch12f5b932018-05-29 15:52:28 +0200943 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
Jens Axboe320ae512013-10-24 09:20:05 +0100944
Keith Busch12f5b932018-05-29 15:52:28 +0200945 if (next != 0) {
946 mod_timer(&q->timeout, next);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600947 } else {
Bart Van Asschefcd36c32018-01-10 08:33:33 -0800948 /*
949 * Request timeouts are handled as a forward rolling timer. If
950 * we end up here it means that no requests are pending and
951 * also that no request has been pending for a while. Mark
952 * each hctx as idle.
953 */
Ming Leif054b562015-04-21 10:00:19 +0800954 queue_for_each_hw_ctx(q, hctx, i) {
955 /* the hctx may be unmapped, so check it here */
956 if (blk_mq_hw_queue_mapped(hctx))
957 blk_mq_tag_idle(hctx);
958 }
Jens Axboe0d2602c2014-05-13 15:10:52 -0600959 }
Christoph Hellwig287922e2015-10-30 20:57:30 +0800960 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100961}
962
Omar Sandoval88459642016-09-17 08:38:44 -0600963struct flush_busy_ctx_data {
964 struct blk_mq_hw_ctx *hctx;
965 struct list_head *list;
966};
967
968static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
969{
970 struct flush_busy_ctx_data *flush_data = data;
971 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
972 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -0700973 enum hctx_type type = hctx->type;
Omar Sandoval88459642016-09-17 08:38:44 -0600974
Omar Sandoval88459642016-09-17 08:38:44 -0600975 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -0700976 list_splice_tail_init(&ctx->rq_lists[type], flush_data->list);
Omar Sandovale9a99a62018-02-27 16:56:42 -0800977 sbitmap_clear_bit(sb, bitnr);
Omar Sandoval88459642016-09-17 08:38:44 -0600978 spin_unlock(&ctx->lock);
979 return true;
980}
981
Jens Axboe320ae512013-10-24 09:20:05 +0100982/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600983 * Process software queues that have been marked busy, splicing them
984 * to the for-dispatch
985 */
Jens Axboe2c3ad662016-12-14 14:34:47 -0700986void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
Jens Axboe1429d7c2014-05-19 09:23:55 -0600987{
Omar Sandoval88459642016-09-17 08:38:44 -0600988 struct flush_busy_ctx_data data = {
989 .hctx = hctx,
990 .list = list,
991 };
Jens Axboe1429d7c2014-05-19 09:23:55 -0600992
Omar Sandoval88459642016-09-17 08:38:44 -0600993 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600994}
Jens Axboe2c3ad662016-12-14 14:34:47 -0700995EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600996
Ming Leib3476892017-10-14 17:22:30 +0800997struct dispatch_rq_data {
998 struct blk_mq_hw_ctx *hctx;
999 struct request *rq;
1000};
1001
1002static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
1003 void *data)
1004{
1005 struct dispatch_rq_data *dispatch_data = data;
1006 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
1007 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
Ming Leic16d6b52018-12-17 08:44:05 -07001008 enum hctx_type type = hctx->type;
Ming Leib3476892017-10-14 17:22:30 +08001009
1010 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001011 if (!list_empty(&ctx->rq_lists[type])) {
1012 dispatch_data->rq = list_entry_rq(ctx->rq_lists[type].next);
Ming Leib3476892017-10-14 17:22:30 +08001013 list_del_init(&dispatch_data->rq->queuelist);
Ming Leic16d6b52018-12-17 08:44:05 -07001014 if (list_empty(&ctx->rq_lists[type]))
Ming Leib3476892017-10-14 17:22:30 +08001015 sbitmap_clear_bit(sb, bitnr);
1016 }
1017 spin_unlock(&ctx->lock);
1018
1019 return !dispatch_data->rq;
1020}
1021
1022struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
1023 struct blk_mq_ctx *start)
1024{
Jens Axboef31967f2018-10-29 13:13:29 -06001025 unsigned off = start ? start->index_hw[hctx->type] : 0;
Ming Leib3476892017-10-14 17:22:30 +08001026 struct dispatch_rq_data data = {
1027 .hctx = hctx,
1028 .rq = NULL,
1029 };
1030
1031 __sbitmap_for_each_set(&hctx->ctx_map, off,
1032 dispatch_rq_from_ctx, &data);
1033
1034 return data.rq;
1035}
1036
Jens Axboe703fd1c2016-09-16 13:59:14 -06001037static inline unsigned int queued_to_index(unsigned int queued)
1038{
1039 if (!queued)
1040 return 0;
Jens Axboe1429d7c2014-05-19 09:23:55 -06001041
Jens Axboe703fd1c2016-09-16 13:59:14 -06001042 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
Jens Axboe1429d7c2014-05-19 09:23:55 -06001043}
1044
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001045bool blk_mq_get_driver_tag(struct request *rq)
Jens Axboebd166ef2017-01-17 06:03:22 -07001046{
1047 struct blk_mq_alloc_data data = {
1048 .q = rq->q,
Jens Axboeea4f9952018-10-29 15:06:13 -06001049 .hctx = rq->mq_hctx,
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001050 .flags = BLK_MQ_REQ_NOWAIT,
Jens Axboef9afca42018-10-29 13:11:38 -06001051 .cmd_flags = rq->cmd_flags,
Jens Axboebd166ef2017-01-17 06:03:22 -07001052 };
Jianchao Wangd263ed92018-08-09 08:34:17 -06001053 bool shared;
Jens Axboe5feeacd2017-04-20 17:23:13 -06001054
Omar Sandoval81380ca2017-04-07 08:56:26 -06001055 if (rq->tag != -1)
1056 goto done;
Jens Axboebd166ef2017-01-17 06:03:22 -07001057
Sagi Grimberg415b8062017-02-27 10:04:39 -07001058 if (blk_mq_tag_is_reserved(data.hctx->sched_tags, rq->internal_tag))
1059 data.flags |= BLK_MQ_REQ_RESERVED;
1060
Jianchao Wangd263ed92018-08-09 08:34:17 -06001061 shared = blk_mq_tag_busy(data.hctx);
Jens Axboebd166ef2017-01-17 06:03:22 -07001062 rq->tag = blk_mq_get_tag(&data);
1063 if (rq->tag >= 0) {
Jianchao Wangd263ed92018-08-09 08:34:17 -06001064 if (shared) {
Jens Axboe200e86b2017-01-25 08:11:38 -07001065 rq->rq_flags |= RQF_MQ_INFLIGHT;
1066 atomic_inc(&data.hctx->nr_active);
1067 }
Jens Axboebd166ef2017-01-17 06:03:22 -07001068 data.hctx->tags->rqs[rq->tag] = rq;
Jens Axboebd166ef2017-01-17 06:03:22 -07001069 }
1070
Omar Sandoval81380ca2017-04-07 08:56:26 -06001071done:
Omar Sandoval81380ca2017-04-07 08:56:26 -06001072 return rq->tag != -1;
Jens Axboebd166ef2017-01-17 06:03:22 -07001073}
1074
Jens Axboeeb619fd2017-11-09 08:32:43 -07001075static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1076 int flags, void *key)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001077{
1078 struct blk_mq_hw_ctx *hctx;
1079
1080 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1081
Ming Lei5815839b2018-06-25 19:31:47 +08001082 spin_lock(&hctx->dispatch_wait_lock);
Jens Axboee8618572019-03-25 12:34:10 -06001083 if (!list_empty(&wait->entry)) {
1084 struct sbitmap_queue *sbq;
1085
1086 list_del_init(&wait->entry);
1087 sbq = &hctx->tags->bitmap_tags;
1088 atomic_dec(&sbq->ws_active);
1089 }
Ming Lei5815839b2018-06-25 19:31:47 +08001090 spin_unlock(&hctx->dispatch_wait_lock);
1091
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001092 blk_mq_run_hw_queue(hctx, true);
1093 return 1;
1094}
1095
Jens Axboef906a6a2017-11-09 16:10:13 -07001096/*
1097 * Mark us waiting for a tag. For shared tags, this involves hooking us into
Bart Van Asscheee3e4de2018-01-09 10:09:15 -08001098 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1099 * restart. For both cases, take care to check the condition again after
Jens Axboef906a6a2017-11-09 16:10:13 -07001100 * marking us as waiting.
1101 */
Ming Lei2278d692018-06-25 19:31:46 +08001102static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
Jens Axboef906a6a2017-11-09 16:10:13 -07001103 struct request *rq)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001104{
Jens Axboee8618572019-03-25 12:34:10 -06001105 struct sbitmap_queue *sbq = &hctx->tags->bitmap_tags;
Ming Lei5815839b2018-06-25 19:31:47 +08001106 struct wait_queue_head *wq;
Jens Axboef906a6a2017-11-09 16:10:13 -07001107 wait_queue_entry_t *wait;
1108 bool ret;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001109
Ming Lei2278d692018-06-25 19:31:46 +08001110 if (!(hctx->flags & BLK_MQ_F_TAG_SHARED)) {
Yufen Yu684b7322019-03-15 11:05:10 +08001111 blk_mq_sched_mark_restart_hctx(hctx);
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001112
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001113 /*
1114 * It's possible that a tag was freed in the window between the
1115 * allocation failure and adding the hardware queue to the wait
1116 * queue.
1117 *
1118 * Don't clear RESTART here, someone else could have set it.
1119 * At most this will cost an extra queue run.
1120 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001121 return blk_mq_get_driver_tag(rq);
Jens Axboeeb619fd2017-11-09 08:32:43 -07001122 }
1123
Ming Lei2278d692018-06-25 19:31:46 +08001124 wait = &hctx->dispatch_wait;
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001125 if (!list_empty_careful(&wait->entry))
1126 return false;
1127
Jens Axboee8618572019-03-25 12:34:10 -06001128 wq = &bt_wait_ptr(sbq, hctx)->wait;
Ming Lei5815839b2018-06-25 19:31:47 +08001129
1130 spin_lock_irq(&wq->lock);
1131 spin_lock(&hctx->dispatch_wait_lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001132 if (!list_empty(&wait->entry)) {
Ming Lei5815839b2018-06-25 19:31:47 +08001133 spin_unlock(&hctx->dispatch_wait_lock);
1134 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001135 return false;
1136 }
1137
Jens Axboee8618572019-03-25 12:34:10 -06001138 atomic_inc(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001139 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1140 __add_wait_queue(wq, wait);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001141
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001142 /*
Jens Axboeeb619fd2017-11-09 08:32:43 -07001143 * It's possible that a tag was freed in the window between the
1144 * allocation failure and adding the hardware queue to the wait
1145 * queue.
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001146 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001147 ret = blk_mq_get_driver_tag(rq);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001148 if (!ret) {
Ming Lei5815839b2018-06-25 19:31:47 +08001149 spin_unlock(&hctx->dispatch_wait_lock);
1150 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001151 return false;
Jens Axboef906a6a2017-11-09 16:10:13 -07001152 }
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001153
1154 /*
1155 * We got a tag, remove ourselves from the wait queue to ensure
1156 * someone else gets the wakeup.
1157 */
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001158 list_del_init(&wait->entry);
Jens Axboee8618572019-03-25 12:34:10 -06001159 atomic_dec(&sbq->ws_active);
Ming Lei5815839b2018-06-25 19:31:47 +08001160 spin_unlock(&hctx->dispatch_wait_lock);
1161 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001162
1163 return true;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001164}
1165
Ming Lei6e7687172018-07-03 09:03:16 -06001166#define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1167#define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1168/*
1169 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1170 * - EWMA is one simple way to compute running average value
1171 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1172 * - take 4 as factor for avoiding to get too small(0) result, and this
1173 * factor doesn't matter because EWMA decreases exponentially
1174 */
1175static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1176{
1177 unsigned int ewma;
1178
1179 if (hctx->queue->elevator)
1180 return;
1181
1182 ewma = hctx->dispatch_busy;
1183
1184 if (!ewma && !busy)
1185 return;
1186
1187 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1188 if (busy)
1189 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1190 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1191
1192 hctx->dispatch_busy = ewma;
1193}
1194
Ming Lei86ff7c22018-01-30 22:04:57 -05001195#define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1196
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001197/*
1198 * Returns true if we did some work AND can potentially do more.
1199 */
Ming Leide148292017-10-14 17:22:29 +08001200bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list,
Jens Axboeeb619fd2017-11-09 08:32:43 -07001201 bool got_budget)
Jens Axboef04c3df2016-12-07 08:41:17 -07001202{
Omar Sandoval81380ca2017-04-07 08:56:26 -06001203 struct blk_mq_hw_ctx *hctx;
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001204 struct request *rq, *nxt;
Jens Axboeeb619fd2017-11-09 08:32:43 -07001205 bool no_tag = false;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001206 int errors, queued;
Ming Lei86ff7c22018-01-30 22:04:57 -05001207 blk_status_t ret = BLK_STS_OK;
Jens Axboef04c3df2016-12-07 08:41:17 -07001208
Omar Sandoval81380ca2017-04-07 08:56:26 -06001209 if (list_empty(list))
1210 return false;
1211
Ming Leide148292017-10-14 17:22:29 +08001212 WARN_ON(!list_is_singular(list) && got_budget);
1213
Jens Axboef04c3df2016-12-07 08:41:17 -07001214 /*
Jens Axboef04c3df2016-12-07 08:41:17 -07001215 * Now process all the entries, sending them to the driver.
1216 */
Jens Axboe93efe982017-03-24 12:04:19 -06001217 errors = queued = 0;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001218 do {
Jens Axboef04c3df2016-12-07 08:41:17 -07001219 struct blk_mq_queue_data bd;
1220
1221 rq = list_first_entry(list, struct request, queuelist);
Ming Lei0bca7992018-04-05 00:35:21 +08001222
Jens Axboeea4f9952018-10-29 15:06:13 -06001223 hctx = rq->mq_hctx;
Ming Lei0bca7992018-04-05 00:35:21 +08001224 if (!got_budget && !blk_mq_get_dispatch_budget(hctx))
1225 break;
1226
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001227 if (!blk_mq_get_driver_tag(rq)) {
Jens Axboe3c782d62017-01-26 12:50:36 -07001228 /*
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001229 * The initial allocation attempt failed, so we need to
Jens Axboeeb619fd2017-11-09 08:32:43 -07001230 * rerun the hardware queue when a tag is freed. The
1231 * waitqueue takes care of that. If the queue is run
1232 * before we add this entry back on the dispatch list,
1233 * we'll re-run it below.
Jens Axboe3c782d62017-01-26 12:50:36 -07001234 */
Ming Lei2278d692018-06-25 19:31:46 +08001235 if (!blk_mq_mark_tag_wait(hctx, rq)) {
Ming Lei0bca7992018-04-05 00:35:21 +08001236 blk_mq_put_dispatch_budget(hctx);
Jens Axboef906a6a2017-11-09 16:10:13 -07001237 /*
1238 * For non-shared tags, the RESTART check
1239 * will suffice.
1240 */
1241 if (hctx->flags & BLK_MQ_F_TAG_SHARED)
1242 no_tag = true;
Omar Sandoval807b1042017-04-05 12:01:35 -07001243 break;
Ming Leide148292017-10-14 17:22:29 +08001244 }
1245 }
1246
Jens Axboef04c3df2016-12-07 08:41:17 -07001247 list_del_init(&rq->queuelist);
1248
1249 bd.rq = rq;
Jens Axboe113285b2017-03-02 13:26:04 -07001250
1251 /*
1252 * Flag last if we have no more requests, or if we have more
1253 * but can't assign a driver tag to it.
1254 */
1255 if (list_empty(list))
1256 bd.last = true;
1257 else {
Jens Axboe113285b2017-03-02 13:26:04 -07001258 nxt = list_first_entry(list, struct request, queuelist);
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001259 bd.last = !blk_mq_get_driver_tag(nxt);
Jens Axboe113285b2017-03-02 13:26:04 -07001260 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001261
1262 ret = q->mq_ops->queue_rq(hctx, &bd);
Ming Lei86ff7c22018-01-30 22:04:57 -05001263 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) {
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001264 /*
1265 * If an I/O scheduler has been configured and we got a
Jens Axboeff821d22017-11-10 22:05:12 -07001266 * driver tag for the next request already, free it
1267 * again.
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001268 */
1269 if (!list_empty(list)) {
1270 nxt = list_first_entry(list, struct request, queuelist);
1271 blk_mq_put_driver_tag(nxt);
1272 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001273 list_add(&rq->queuelist, list);
1274 __blk_mq_requeue_request(rq);
1275 break;
Jens Axboef04c3df2016-12-07 08:41:17 -07001276 }
1277
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001278 if (unlikely(ret != BLK_STS_OK)) {
1279 errors++;
1280 blk_mq_end_request(rq, BLK_STS_IOERR);
1281 continue;
1282 }
1283
1284 queued++;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001285 } while (!list_empty(list));
Jens Axboef04c3df2016-12-07 08:41:17 -07001286
1287 hctx->dispatched[queued_to_index(queued)]++;
1288
1289 /*
1290 * Any items that need requeuing? Stuff them into hctx->dispatch,
1291 * that is where we will continue on next queue run.
1292 */
1293 if (!list_empty(list)) {
Ming Lei86ff7c22018-01-30 22:04:57 -05001294 bool needs_restart;
1295
Jens Axboed666ba92018-11-27 17:02:25 -07001296 /*
1297 * If we didn't flush the entire list, we could have told
1298 * the driver there was more coming, but that turned out to
1299 * be a lie.
1300 */
1301 if (q->mq_ops->commit_rqs)
1302 q->mq_ops->commit_rqs(hctx);
1303
Jens Axboef04c3df2016-12-07 08:41:17 -07001304 spin_lock(&hctx->lock);
Jens Axboec13660a2017-01-26 12:40:07 -07001305 list_splice_init(list, &hctx->dispatch);
Jens Axboef04c3df2016-12-07 08:41:17 -07001306 spin_unlock(&hctx->lock);
1307
1308 /*
Bart Van Assche710c7852017-04-07 11:16:51 -07001309 * If SCHED_RESTART was set by the caller of this function and
1310 * it is no longer set that means that it was cleared by another
1311 * thread and hence that a queue rerun is needed.
Jens Axboef04c3df2016-12-07 08:41:17 -07001312 *
Jens Axboeeb619fd2017-11-09 08:32:43 -07001313 * If 'no_tag' is set, that means that we failed getting
1314 * a driver tag with an I/O scheduler attached. If our dispatch
1315 * waitqueue is no longer active, ensure that we run the queue
1316 * AFTER adding our entries back to the list.
Jens Axboebd166ef2017-01-17 06:03:22 -07001317 *
Bart Van Assche710c7852017-04-07 11:16:51 -07001318 * If no I/O scheduler has been configured it is possible that
1319 * the hardware queue got stopped and restarted before requests
1320 * were pushed back onto the dispatch list. Rerun the queue to
1321 * avoid starvation. Notes:
1322 * - blk_mq_run_hw_queue() checks whether or not a queue has
1323 * been stopped before rerunning a queue.
1324 * - Some but not all block drivers stop a queue before
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001325 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
Bart Van Assche710c7852017-04-07 11:16:51 -07001326 * and dm-rq.
Ming Lei86ff7c22018-01-30 22:04:57 -05001327 *
1328 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1329 * bit is set, run queue after a delay to avoid IO stalls
1330 * that could otherwise occur if the queue is idle.
Jens Axboebd166ef2017-01-17 06:03:22 -07001331 */
Ming Lei86ff7c22018-01-30 22:04:57 -05001332 needs_restart = blk_mq_sched_needs_restart(hctx);
1333 if (!needs_restart ||
Jens Axboeeb619fd2017-11-09 08:32:43 -07001334 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
Jens Axboebd166ef2017-01-17 06:03:22 -07001335 blk_mq_run_hw_queue(hctx, true);
Ming Lei86ff7c22018-01-30 22:04:57 -05001336 else if (needs_restart && (ret == BLK_STS_RESOURCE))
1337 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001338
Ming Lei6e7687172018-07-03 09:03:16 -06001339 blk_mq_update_dispatch_busy(hctx, true);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001340 return false;
Ming Lei6e7687172018-07-03 09:03:16 -06001341 } else
1342 blk_mq_update_dispatch_busy(hctx, false);
Jens Axboef04c3df2016-12-07 08:41:17 -07001343
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001344 /*
1345 * If the host/device is unable to accept more work, inform the
1346 * caller of that.
1347 */
1348 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
1349 return false;
1350
Jens Axboe93efe982017-03-24 12:04:19 -06001351 return (queued + errors) != 0;
Jens Axboef04c3df2016-12-07 08:41:17 -07001352}
1353
Bart Van Assche6a83e742016-11-02 10:09:51 -06001354static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1355{
1356 int srcu_idx;
1357
Jens Axboeb7a71e62017-08-01 09:28:24 -06001358 /*
1359 * We should be running this queue from one of the CPUs that
1360 * are mapped to it.
Ming Lei7df938f2018-01-18 00:41:52 +08001361 *
1362 * There are at least two related races now between setting
1363 * hctx->next_cpu from blk_mq_hctx_next_cpu() and running
1364 * __blk_mq_run_hw_queue():
1365 *
1366 * - hctx->next_cpu is found offline in blk_mq_hctx_next_cpu(),
1367 * but later it becomes online, then this warning is harmless
1368 * at all
1369 *
1370 * - hctx->next_cpu is found online in blk_mq_hctx_next_cpu(),
1371 * but later it becomes offline, then the warning can't be
1372 * triggered, and we depend on blk-mq timeout handler to
1373 * handle dispatched requests to this hctx
Jens Axboeb7a71e62017-08-01 09:28:24 -06001374 */
Ming Lei7df938f2018-01-18 00:41:52 +08001375 if (!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
1376 cpu_online(hctx->next_cpu)) {
1377 printk(KERN_WARNING "run queue from wrong CPU %d, hctx %s\n",
1378 raw_smp_processor_id(),
1379 cpumask_empty(hctx->cpumask) ? "inactive": "active");
1380 dump_stack();
1381 }
Bart Van Assche6a83e742016-11-02 10:09:51 -06001382
Jens Axboeb7a71e62017-08-01 09:28:24 -06001383 /*
1384 * We can't run the queue inline with ints disabled. Ensure that
1385 * we catch bad users of this early.
1386 */
1387 WARN_ON_ONCE(in_interrupt());
1388
Jens Axboe04ced152018-01-09 08:29:46 -08001389 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
Jens Axboebf4907c2017-03-30 12:30:39 -06001390
Jens Axboe04ced152018-01-09 08:29:46 -08001391 hctx_lock(hctx, &srcu_idx);
1392 blk_mq_sched_dispatch_requests(hctx);
1393 hctx_unlock(hctx, srcu_idx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001394}
1395
Ming Leif82ddf12018-04-08 17:48:10 +08001396static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1397{
1398 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1399
1400 if (cpu >= nr_cpu_ids)
1401 cpu = cpumask_first(hctx->cpumask);
1402 return cpu;
1403}
1404
Jens Axboe506e9312014-05-07 10:26:44 -06001405/*
1406 * It'd be great if the workqueue API had a way to pass
1407 * in a mask and had some smarts for more clever placement.
1408 * For now we just round-robin here, switching for every
1409 * BLK_MQ_CPU_WORK_BATCH queued items.
1410 */
1411static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1412{
Ming Lei7bed4592018-01-18 00:41:51 +08001413 bool tried = false;
Ming Lei476f8c92018-04-08 17:48:09 +08001414 int next_cpu = hctx->next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001415
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001416 if (hctx->queue->nr_hw_queues == 1)
1417 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -06001418
1419 if (--hctx->next_cpu_batch <= 0) {
Ming Lei7bed4592018-01-18 00:41:51 +08001420select_cpu:
Ming Lei476f8c92018-04-08 17:48:09 +08001421 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08001422 cpu_online_mask);
Jens Axboe506e9312014-05-07 10:26:44 -06001423 if (next_cpu >= nr_cpu_ids)
Ming Leif82ddf12018-04-08 17:48:10 +08001424 next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06001425 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1426 }
1427
Ming Lei7bed4592018-01-18 00:41:51 +08001428 /*
1429 * Do unbound schedule if we can't find a online CPU for this hctx,
1430 * and it should only happen in the path of handling CPU DEAD.
1431 */
Ming Lei476f8c92018-04-08 17:48:09 +08001432 if (!cpu_online(next_cpu)) {
Ming Lei7bed4592018-01-18 00:41:51 +08001433 if (!tried) {
1434 tried = true;
1435 goto select_cpu;
1436 }
1437
1438 /*
1439 * Make sure to re-select CPU next time once after CPUs
1440 * in hctx->cpumask become online again.
1441 */
Ming Lei476f8c92018-04-08 17:48:09 +08001442 hctx->next_cpu = next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001443 hctx->next_cpu_batch = 1;
1444 return WORK_CPU_UNBOUND;
1445 }
Ming Lei476f8c92018-04-08 17:48:09 +08001446
1447 hctx->next_cpu = next_cpu;
1448 return next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -06001449}
1450
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001451static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1452 unsigned long msecs)
Jens Axboe320ae512013-10-24 09:20:05 +01001453{
Bart Van Assche5435c022017-06-20 11:15:49 -07001454 if (unlikely(blk_mq_hctx_stopped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +01001455 return;
1456
Jens Axboe1b792f22016-09-21 10:12:13 -06001457 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001458 int cpu = get_cpu();
1459 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +01001460 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001461 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +01001462 return;
1463 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001464
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001465 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -06001466 }
Paolo Bonzini398205b2014-11-07 23:03:59 +01001467
Bart Van Asscheae943d22018-01-19 08:58:55 -08001468 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1469 msecs_to_jiffies(msecs));
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001470}
1471
1472void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1473{
1474 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1475}
1476EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1477
Jens Axboe79f720a2017-11-10 09:13:21 -07001478bool blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001479{
Ming Lei24f5a902018-01-06 16:27:38 +08001480 int srcu_idx;
1481 bool need_run;
1482
1483 /*
1484 * When queue is quiesced, we may be switching io scheduler, or
1485 * updating nr_hw_queues, or other things, and we can't run queue
1486 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
1487 *
1488 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
1489 * quiesced.
1490 */
Jens Axboe04ced152018-01-09 08:29:46 -08001491 hctx_lock(hctx, &srcu_idx);
1492 need_run = !blk_queue_quiesced(hctx->queue) &&
1493 blk_mq_hctx_has_pending(hctx);
1494 hctx_unlock(hctx, srcu_idx);
Ming Lei24f5a902018-01-06 16:27:38 +08001495
1496 if (need_run) {
Jens Axboe79f720a2017-11-10 09:13:21 -07001497 __blk_mq_delay_run_hw_queue(hctx, async, 0);
1498 return true;
1499 }
1500
1501 return false;
Jens Axboe320ae512013-10-24 09:20:05 +01001502}
Omar Sandoval5b727272017-04-14 01:00:00 -07001503EXPORT_SYMBOL(blk_mq_run_hw_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01001504
Mike Snitzerb94ec292015-03-11 23:56:38 -04001505void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001506{
1507 struct blk_mq_hw_ctx *hctx;
1508 int i;
1509
1510 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe79f720a2017-11-10 09:13:21 -07001511 if (blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +01001512 continue;
1513
Mike Snitzerb94ec292015-03-11 23:56:38 -04001514 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001515 }
1516}
Mike Snitzerb94ec292015-03-11 23:56:38 -04001517EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001518
Bart Van Asschefd001442016-10-28 17:19:37 -07001519/**
1520 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1521 * @q: request queue.
1522 *
1523 * The caller is responsible for serializing this function against
1524 * blk_mq_{start,stop}_hw_queue().
1525 */
1526bool blk_mq_queue_stopped(struct request_queue *q)
1527{
1528 struct blk_mq_hw_ctx *hctx;
1529 int i;
1530
1531 queue_for_each_hw_ctx(q, hctx, i)
1532 if (blk_mq_hctx_stopped(hctx))
1533 return true;
1534
1535 return false;
1536}
1537EXPORT_SYMBOL(blk_mq_queue_stopped);
1538
Ming Lei39a70c72017-06-06 23:22:09 +08001539/*
1540 * This function is often used for pausing .queue_rq() by driver when
1541 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001542 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001543 *
1544 * We do not guarantee that dispatch can be drained or blocked
1545 * after blk_mq_stop_hw_queue() returns. Please use
1546 * blk_mq_quiesce_queue() for that requirement.
1547 */
Jens Axboe320ae512013-10-24 09:20:05 +01001548void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1549{
Ming Lei641a9ed2017-06-06 23:22:10 +08001550 cancel_delayed_work(&hctx->run_work);
1551
1552 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboe320ae512013-10-24 09:20:05 +01001553}
1554EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1555
Ming Lei39a70c72017-06-06 23:22:09 +08001556/*
1557 * This function is often used for pausing .queue_rq() by driver when
1558 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001559 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001560 *
1561 * We do not guarantee that dispatch can be drained or blocked
1562 * after blk_mq_stop_hw_queues() returns. Please use
1563 * blk_mq_quiesce_queue() for that requirement.
1564 */
Jens Axboe2719aa22017-05-03 11:08:14 -06001565void blk_mq_stop_hw_queues(struct request_queue *q)
1566{
Ming Lei641a9ed2017-06-06 23:22:10 +08001567 struct blk_mq_hw_ctx *hctx;
1568 int i;
1569
1570 queue_for_each_hw_ctx(q, hctx, i)
1571 blk_mq_stop_hw_queue(hctx);
Christoph Hellwig280d45f2013-10-25 14:45:58 +01001572}
1573EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1574
Jens Axboe320ae512013-10-24 09:20:05 +01001575void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1576{
1577 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -06001578
Jens Axboe0ffbce82014-06-25 08:22:34 -06001579 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001580}
1581EXPORT_SYMBOL(blk_mq_start_hw_queue);
1582
Christoph Hellwig2f268552014-04-16 09:44:56 +02001583void blk_mq_start_hw_queues(struct request_queue *q)
1584{
1585 struct blk_mq_hw_ctx *hctx;
1586 int i;
1587
1588 queue_for_each_hw_ctx(q, hctx, i)
1589 blk_mq_start_hw_queue(hctx);
1590}
1591EXPORT_SYMBOL(blk_mq_start_hw_queues);
1592
Jens Axboeae911c52016-12-08 13:19:30 -07001593void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1594{
1595 if (!blk_mq_hctx_stopped(hctx))
1596 return;
1597
1598 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1599 blk_mq_run_hw_queue(hctx, async);
1600}
1601EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1602
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001603void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001604{
1605 struct blk_mq_hw_ctx *hctx;
1606 int i;
1607
Jens Axboeae911c52016-12-08 13:19:30 -07001608 queue_for_each_hw_ctx(q, hctx, i)
1609 blk_mq_start_stopped_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001610}
1611EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1612
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001613static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001614{
1615 struct blk_mq_hw_ctx *hctx;
1616
Jens Axboe9f993732017-04-10 09:54:54 -06001617 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboe21c6e932017-04-10 09:54:56 -06001618
1619 /*
Ming Lei15fe8a902018-04-08 17:48:11 +08001620 * If we are stopped, don't run the queue.
Jens Axboe21c6e932017-04-10 09:54:56 -06001621 */
Ming Lei15fe8a902018-04-08 17:48:11 +08001622 if (test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jianchao Wang0196d6b2018-06-04 17:03:55 +08001623 return;
Jens Axboee4043dc2014-04-09 10:18:23 -06001624
Jens Axboe320ae512013-10-24 09:20:05 +01001625 __blk_mq_run_hw_queue(hctx);
1626}
1627
Ming Leicfd0c552015-10-20 23:13:57 +08001628static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08001629 struct request *rq,
1630 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01001631{
Jens Axboee57690f2016-08-24 15:34:35 -06001632 struct blk_mq_ctx *ctx = rq->mq_ctx;
Ming Leic16d6b52018-12-17 08:44:05 -07001633 enum hctx_type type = hctx->type;
Jens Axboee57690f2016-08-24 15:34:35 -06001634
Bart Van Assche7b607812017-06-20 11:15:47 -07001635 lockdep_assert_held(&ctx->lock);
1636
Jens Axboe01b983c2013-11-19 18:59:10 -07001637 trace_block_rq_insert(hctx->queue, rq);
1638
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001639 if (at_head)
Ming Leic16d6b52018-12-17 08:44:05 -07001640 list_add(&rq->queuelist, &ctx->rq_lists[type]);
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001641 else
Ming Leic16d6b52018-12-17 08:44:05 -07001642 list_add_tail(&rq->queuelist, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08001643}
Jens Axboe4bb659b2014-05-09 09:36:49 -06001644
Jens Axboe2c3ad662016-12-14 14:34:47 -07001645void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1646 bool at_head)
Ming Leicfd0c552015-10-20 23:13:57 +08001647{
1648 struct blk_mq_ctx *ctx = rq->mq_ctx;
1649
Bart Van Assche7b607812017-06-20 11:15:47 -07001650 lockdep_assert_held(&ctx->lock);
1651
Jens Axboee57690f2016-08-24 15:34:35 -06001652 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01001653 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001654}
1655
Jens Axboe157f3772017-09-11 16:43:57 -06001656/*
1657 * Should only be used carefully, when the caller knows we want to
1658 * bypass a potential IO scheduler on the target device.
1659 */
Ming Leib0850292017-11-02 23:24:34 +08001660void blk_mq_request_bypass_insert(struct request *rq, bool run_queue)
Jens Axboe157f3772017-09-11 16:43:57 -06001661{
Jens Axboeea4f9952018-10-29 15:06:13 -06001662 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
Jens Axboe157f3772017-09-11 16:43:57 -06001663
1664 spin_lock(&hctx->lock);
1665 list_add_tail(&rq->queuelist, &hctx->dispatch);
1666 spin_unlock(&hctx->lock);
1667
Ming Leib0850292017-11-02 23:24:34 +08001668 if (run_queue)
1669 blk_mq_run_hw_queue(hctx, false);
Jens Axboe157f3772017-09-11 16:43:57 -06001670}
1671
Jens Axboebd166ef2017-01-17 06:03:22 -07001672void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
1673 struct list_head *list)
Jens Axboe320ae512013-10-24 09:20:05 +01001674
1675{
Ming Lei3f0cedc2018-07-02 17:35:58 +08001676 struct request *rq;
Ming Leic16d6b52018-12-17 08:44:05 -07001677 enum hctx_type type = hctx->type;
Ming Lei3f0cedc2018-07-02 17:35:58 +08001678
Jens Axboe320ae512013-10-24 09:20:05 +01001679 /*
1680 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1681 * offline now
1682 */
Ming Lei3f0cedc2018-07-02 17:35:58 +08001683 list_for_each_entry(rq, list, queuelist) {
Jens Axboee57690f2016-08-24 15:34:35 -06001684 BUG_ON(rq->mq_ctx != ctx);
Ming Lei3f0cedc2018-07-02 17:35:58 +08001685 trace_block_rq_insert(hctx->queue, rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001686 }
Ming Lei3f0cedc2018-07-02 17:35:58 +08001687
1688 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07001689 list_splice_tail_init(list, &ctx->rq_lists[type]);
Ming Leicfd0c552015-10-20 23:13:57 +08001690 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001691 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001692}
1693
Jens Axboe3110fc72018-10-30 12:24:04 -06001694static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b)
Jens Axboe320ae512013-10-24 09:20:05 +01001695{
1696 struct request *rqa = container_of(a, struct request, queuelist);
1697 struct request *rqb = container_of(b, struct request, queuelist);
1698
Jens Axboe3110fc72018-10-30 12:24:04 -06001699 if (rqa->mq_ctx < rqb->mq_ctx)
1700 return -1;
1701 else if (rqa->mq_ctx > rqb->mq_ctx)
1702 return 1;
1703 else if (rqa->mq_hctx < rqb->mq_hctx)
1704 return -1;
1705 else if (rqa->mq_hctx > rqb->mq_hctx)
1706 return 1;
1707
1708 return blk_rq_pos(rqa) > blk_rq_pos(rqb);
Jens Axboe320ae512013-10-24 09:20:05 +01001709}
1710
1711void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1712{
Jens Axboe67cae4c2018-10-30 11:31:51 -06001713 struct blk_mq_hw_ctx *this_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001714 struct blk_mq_ctx *this_ctx;
1715 struct request_queue *this_q;
1716 struct request *rq;
1717 LIST_HEAD(list);
Jens Axboe67cae4c2018-10-30 11:31:51 -06001718 LIST_HEAD(rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001719 unsigned int depth;
1720
1721 list_splice_init(&plug->mq_list, &list);
1722
Jens Axboece5b0092018-11-27 17:13:56 -07001723 if (plug->rq_count > 2 && plug->multiple_queues)
1724 list_sort(NULL, &list, plug_rq_cmp);
Jens Axboe320ae512013-10-24 09:20:05 +01001725
Dongli Zhangbcc816d2019-04-04 10:57:44 +08001726 plug->rq_count = 0;
1727
Jens Axboe320ae512013-10-24 09:20:05 +01001728 this_q = NULL;
Jens Axboe67cae4c2018-10-30 11:31:51 -06001729 this_hctx = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001730 this_ctx = NULL;
1731 depth = 0;
1732
1733 while (!list_empty(&list)) {
1734 rq = list_entry_rq(list.next);
1735 list_del_init(&rq->queuelist);
1736 BUG_ON(!rq->q);
Jens Axboe67cae4c2018-10-30 11:31:51 -06001737 if (rq->mq_hctx != this_hctx || rq->mq_ctx != this_ctx) {
1738 if (this_hctx) {
Ilya Dryomov587562d2018-09-26 14:35:50 +02001739 trace_block_unplug(this_q, depth, !from_schedule);
Jens Axboe67cae4c2018-10-30 11:31:51 -06001740 blk_mq_sched_insert_requests(this_hctx, this_ctx,
1741 &rq_list,
Jens Axboebd166ef2017-01-17 06:03:22 -07001742 from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01001743 }
1744
Jens Axboe320ae512013-10-24 09:20:05 +01001745 this_q = rq->q;
Jens Axboe67cae4c2018-10-30 11:31:51 -06001746 this_ctx = rq->mq_ctx;
1747 this_hctx = rq->mq_hctx;
Jens Axboe320ae512013-10-24 09:20:05 +01001748 depth = 0;
1749 }
1750
1751 depth++;
Jens Axboe67cae4c2018-10-30 11:31:51 -06001752 list_add_tail(&rq->queuelist, &rq_list);
Jens Axboe320ae512013-10-24 09:20:05 +01001753 }
1754
1755 /*
Jens Axboe67cae4c2018-10-30 11:31:51 -06001756 * If 'this_hctx' is set, we know we have entries to complete
1757 * on 'rq_list'. Do those.
Jens Axboe320ae512013-10-24 09:20:05 +01001758 */
Jens Axboe67cae4c2018-10-30 11:31:51 -06001759 if (this_hctx) {
Ilya Dryomov587562d2018-09-26 14:35:50 +02001760 trace_block_unplug(this_q, depth, !from_schedule);
Jens Axboe67cae4c2018-10-30 11:31:51 -06001761 blk_mq_sched_insert_requests(this_hctx, this_ctx, &rq_list,
Jens Axboebd166ef2017-01-17 06:03:22 -07001762 from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01001763 }
1764}
1765
1766static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1767{
Bart Van Asscheda8d7f02017-04-19 14:01:24 -07001768 blk_init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001769
Jens Axboe6e85eaf2016-12-02 20:00:14 -07001770 blk_account_io_start(rq, true);
Jens Axboe320ae512013-10-24 09:20:05 +01001771}
1772
Mike Snitzer0f955492018-01-17 11:25:56 -05001773static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
1774 struct request *rq,
Jens Axboebe94f052018-11-24 10:15:46 -07001775 blk_qc_t *cookie, bool last)
Shaohua Lif984df12015-05-08 10:51:32 -07001776{
Shaohua Lif984df12015-05-08 10:51:32 -07001777 struct request_queue *q = rq->q;
Shaohua Lif984df12015-05-08 10:51:32 -07001778 struct blk_mq_queue_data bd = {
1779 .rq = rq,
Jens Axboebe94f052018-11-24 10:15:46 -07001780 .last = last,
Shaohua Lif984df12015-05-08 10:51:32 -07001781 };
Jens Axboebd166ef2017-01-17 06:03:22 -07001782 blk_qc_t new_cookie;
Jens Axboef06345a2017-06-12 11:22:46 -06001783 blk_status_t ret;
Mike Snitzer0f955492018-01-17 11:25:56 -05001784
1785 new_cookie = request_to_qc_t(hctx, rq);
1786
1787 /*
1788 * For OK queue, we are done. For error, caller may kill it.
1789 * Any other error (busy), just add it to our list as we
1790 * previously would have done.
1791 */
1792 ret = q->mq_ops->queue_rq(hctx, &bd);
1793 switch (ret) {
1794 case BLK_STS_OK:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001795 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001796 *cookie = new_cookie;
1797 break;
1798 case BLK_STS_RESOURCE:
Ming Lei86ff7c22018-01-30 22:04:57 -05001799 case BLK_STS_DEV_RESOURCE:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001800 blk_mq_update_dispatch_busy(hctx, true);
Mike Snitzer0f955492018-01-17 11:25:56 -05001801 __blk_mq_requeue_request(rq);
1802 break;
1803 default:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001804 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001805 *cookie = BLK_QC_T_NONE;
1806 break;
1807 }
1808
1809 return ret;
1810}
1811
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001812static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
Mike Snitzer0f955492018-01-17 11:25:56 -05001813 struct request *rq,
Ming Lei396eaf22018-01-17 11:25:57 -05001814 blk_qc_t *cookie,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001815 bool bypass_insert, bool last)
Mike Snitzer0f955492018-01-17 11:25:56 -05001816{
1817 struct request_queue *q = rq->q;
Ming Leid964f042017-06-06 23:22:00 +08001818 bool run_queue = true;
1819
Ming Lei23d4ee12018-01-18 12:06:59 +08001820 /*
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001821 * RCU or SRCU read lock is needed before checking quiesced flag.
Ming Lei23d4ee12018-01-18 12:06:59 +08001822 *
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001823 * When queue is stopped or quiesced, ignore 'bypass_insert' from
1824 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
1825 * and avoid driver to try to dispatch again.
Ming Lei23d4ee12018-01-18 12:06:59 +08001826 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001827 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
Ming Leid964f042017-06-06 23:22:00 +08001828 run_queue = false;
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001829 bypass_insert = false;
1830 goto insert;
Ming Leid964f042017-06-06 23:22:00 +08001831 }
Shaohua Lif984df12015-05-08 10:51:32 -07001832
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001833 if (q->elevator && !bypass_insert)
1834 goto insert;
Bart Van Assche2253efc2016-10-28 17:20:02 -07001835
Ming Lei0bca7992018-04-05 00:35:21 +08001836 if (!blk_mq_get_dispatch_budget(hctx))
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001837 goto insert;
Jens Axboebd166ef2017-01-17 06:03:22 -07001838
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001839 if (!blk_mq_get_driver_tag(rq)) {
Ming Lei0bca7992018-04-05 00:35:21 +08001840 blk_mq_put_dispatch_budget(hctx);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001841 goto insert;
Ming Lei88022d72017-11-05 02:21:12 +08001842 }
Ming Leide148292017-10-14 17:22:29 +08001843
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001844 return __blk_mq_issue_directly(hctx, rq, cookie, last);
1845insert:
1846 if (bypass_insert)
1847 return BLK_STS_RESOURCE;
1848
1849 blk_mq_request_bypass_insert(rq, run_queue);
1850 return BLK_STS_OK;
1851}
1852
1853static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
1854 struct request *rq, blk_qc_t *cookie)
1855{
1856 blk_status_t ret;
1857 int srcu_idx;
1858
1859 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
1860
1861 hctx_lock(hctx, &srcu_idx);
1862
1863 ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false, true);
1864 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
1865 blk_mq_request_bypass_insert(rq, true);
1866 else if (ret != BLK_STS_OK)
1867 blk_mq_end_request(rq, ret);
1868
Jens Axboe04ced152018-01-09 08:29:46 -08001869 hctx_unlock(hctx, srcu_idx);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001870}
1871
1872blk_status_t blk_mq_request_issue_directly(struct request *rq, bool last)
1873{
1874 blk_status_t ret;
1875 int srcu_idx;
1876 blk_qc_t unused_cookie;
1877 struct blk_mq_hw_ctx *hctx = rq->mq_hctx;
1878
1879 hctx_lock(hctx, &srcu_idx);
1880 ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true, last);
1881 hctx_unlock(hctx, srcu_idx);
Jianchao Wang7f556a42018-12-14 09:28:18 +08001882
1883 return ret;
Christoph Hellwig5eb61262017-03-22 15:01:51 -04001884}
1885
Ming Lei6ce3dd62018-07-10 09:03:31 +08001886void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
1887 struct list_head *list)
1888{
1889 while (!list_empty(list)) {
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001890 blk_status_t ret;
Ming Lei6ce3dd62018-07-10 09:03:31 +08001891 struct request *rq = list_first_entry(list, struct request,
1892 queuelist);
1893
1894 list_del_init(&rq->queuelist);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001895 ret = blk_mq_request_issue_directly(rq, list_empty(list));
1896 if (ret != BLK_STS_OK) {
1897 if (ret == BLK_STS_RESOURCE ||
1898 ret == BLK_STS_DEV_RESOURCE) {
1899 blk_mq_request_bypass_insert(rq,
Jens Axboec616cbe2018-12-06 22:17:44 -07001900 list_empty(list));
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001901 break;
1902 }
1903 blk_mq_end_request(rq, ret);
1904 }
Ming Lei6ce3dd62018-07-10 09:03:31 +08001905 }
Jens Axboed666ba92018-11-27 17:02:25 -07001906
1907 /*
1908 * If we didn't flush the entire list, we could have told
1909 * the driver there was more coming, but that turned out to
1910 * be a lie.
1911 */
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07001912 if (!list_empty(list) && hctx->queue->mq_ops->commit_rqs)
Jens Axboed666ba92018-11-27 17:02:25 -07001913 hctx->queue->mq_ops->commit_rqs(hctx);
Ming Lei6ce3dd62018-07-10 09:03:31 +08001914}
1915
Jens Axboece5b0092018-11-27 17:13:56 -07001916static void blk_add_rq_to_plug(struct blk_plug *plug, struct request *rq)
1917{
1918 list_add_tail(&rq->queuelist, &plug->mq_list);
1919 plug->rq_count++;
1920 if (!plug->multiple_queues && !list_is_singular(&plug->mq_list)) {
1921 struct request *tmp;
1922
1923 tmp = list_first_entry(&plug->mq_list, struct request,
1924 queuelist);
1925 if (tmp->q != rq->q)
1926 plug->multiple_queues = true;
1927 }
1928}
1929
Jens Axboedece1632015-11-05 10:41:16 -07001930static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06001931{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001932 const int is_sync = op_is_sync(bio->bi_opf);
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07001933 const int is_flush_fua = op_is_flush(bio->bi_opf);
Ming Lei78091672019-01-16 19:08:15 +08001934 struct blk_mq_alloc_data data = { .flags = 0};
Jens Axboe07068d52014-05-22 10:40:51 -06001935 struct request *rq;
Shaohua Lif984df12015-05-08 10:51:32 -07001936 struct blk_plug *plug;
Shaohua Li5b3f3412015-05-08 10:51:33 -07001937 struct request *same_queue_rq = NULL;
Jens Axboe7b371632015-11-05 10:41:40 -07001938 blk_qc_t cookie;
Jens Axboe07068d52014-05-22 10:40:51 -06001939
1940 blk_queue_bounce(q, &bio);
1941
NeilBrownaf67c312017-06-18 14:38:57 +10001942 blk_queue_split(q, &bio);
Wen Xiongf36ea502017-05-10 08:54:11 -05001943
Dmitry Monakhove23947b2017-06-29 11:31:11 -07001944 if (!bio_integrity_prep(bio))
Jens Axboedece1632015-11-05 10:41:16 -07001945 return BLK_QC_T_NONE;
Jens Axboe07068d52014-05-22 10:40:51 -06001946
Omar Sandoval87c279e2016-06-01 22:18:48 -07001947 if (!is_flush_fua && !blk_queue_nomerges(q) &&
Jens Axboe5f0ed772018-11-23 22:04:33 -07001948 blk_attempt_plug_merge(q, bio, &same_queue_rq))
Omar Sandoval87c279e2016-06-01 22:18:48 -07001949 return BLK_QC_T_NONE;
Shaohua Lif984df12015-05-08 10:51:32 -07001950
Jens Axboebd166ef2017-01-17 06:03:22 -07001951 if (blk_mq_sched_bio_merge(q, bio))
1952 return BLK_QC_T_NONE;
1953
Christoph Hellwigd5337562018-11-14 17:02:09 +01001954 rq_qos_throttle(q, bio);
Jens Axboe87760e52016-11-09 12:38:14 -07001955
Ming Lei78091672019-01-16 19:08:15 +08001956 data.cmd_flags = bio->bi_opf;
Jens Axboef9afca42018-10-29 13:11:38 -06001957 rq = blk_mq_get_request(q, bio, &data);
Jens Axboe87760e52016-11-09 12:38:14 -07001958 if (unlikely(!rq)) {
Josef Bacikc1c80382018-07-03 11:14:59 -04001959 rq_qos_cleanup(q, bio);
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -05001960 if (bio->bi_opf & REQ_NOWAIT)
1961 bio_wouldblock_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001962 return BLK_QC_T_NONE;
Jens Axboe87760e52016-11-09 12:38:14 -07001963 }
1964
Xiaoguang Wangd6f1dda2018-10-23 22:30:50 +08001965 trace_block_getrq(q, bio, bio->bi_opf);
1966
Josef Bacikc1c80382018-07-03 11:14:59 -04001967 rq_qos_track(q, rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06001968
Jens Axboefd2d3322017-01-12 10:04:45 -07001969 cookie = request_to_qc_t(data.hctx, rq);
Jens Axboe07068d52014-05-22 10:40:51 -06001970
Shaohua Lif984df12015-05-08 10:51:32 -07001971 plug = current->plug;
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001972 if (unlikely(is_flush_fua)) {
Shaohua Lif984df12015-05-08 10:51:32 -07001973 blk_mq_put_ctx(data.ctx);
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001974 blk_mq_bio_to_request(rq, bio);
Ming Lei923218f2017-11-02 23:24:38 +08001975
1976 /* bypass scheduler for flush rq */
1977 blk_insert_flush(rq);
1978 blk_mq_run_hw_queue(data.hctx, true);
Jens Axboeb2c5d162018-11-29 10:03:42 -07001979 } else if (plug && (q->nr_hw_queues == 1 || q->mq_ops->commit_rqs)) {
1980 /*
1981 * Use plugging if we have a ->commit_rqs() hook as well, as
1982 * we know the driver uses bd->last in a smart fashion.
1983 */
Jens Axboe5f0ed772018-11-23 22:04:33 -07001984 unsigned int request_count = plug->rq_count;
Shaohua Li600271d2016-11-03 17:03:54 -07001985 struct request *last = NULL;
1986
Jens Axboeb00c53e2017-04-20 16:40:36 -06001987 blk_mq_put_ctx(data.ctx);
Jeff Moyere6c44382015-05-08 10:51:30 -07001988 blk_mq_bio_to_request(rq, bio);
Ming Lei0a6219a2016-11-16 18:07:05 +08001989
Ming Lei676d0602015-10-20 23:13:56 +08001990 if (!request_count)
Jeff Moyere6c44382015-05-08 10:51:30 -07001991 trace_block_plug(q);
Shaohua Li600271d2016-11-03 17:03:54 -07001992 else
1993 last = list_entry_rq(plug->mq_list.prev);
Jens Axboeb094f892015-11-20 20:29:45 -07001994
Shaohua Li600271d2016-11-03 17:03:54 -07001995 if (request_count >= BLK_MAX_REQUEST_COUNT || (last &&
1996 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
Jeff Moyere6c44382015-05-08 10:51:30 -07001997 blk_flush_plug_list(plug, false);
1998 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001999 }
Jens Axboeb094f892015-11-20 20:29:45 -07002000
Jens Axboece5b0092018-11-27 17:13:56 -07002001 blk_add_rq_to_plug(plug, rq);
Christoph Hellwig22997222017-03-22 15:01:52 -04002002 } else if (plug && !blk_queue_nomerges(q)) {
Jens Axboe320ae512013-10-24 09:20:05 +01002003 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01002004
Jens Axboe320ae512013-10-24 09:20:05 +01002005 /*
2006 * We do limited plugging. If the bio can be merged, do that.
2007 * Otherwise the existing request in the plug list will be
2008 * issued. So the plug list will have one request at most
Christoph Hellwig22997222017-03-22 15:01:52 -04002009 * The plug list might get flushed before this. If that happens,
2010 * the plug list is empty, and same_queue_rq is invalid.
Jens Axboe320ae512013-10-24 09:20:05 +01002011 */
Christoph Hellwig22997222017-03-22 15:01:52 -04002012 if (list_empty(&plug->mq_list))
2013 same_queue_rq = NULL;
Jens Axboe4711b572018-11-27 17:07:17 -07002014 if (same_queue_rq) {
Christoph Hellwig22997222017-03-22 15:01:52 -04002015 list_del_init(&same_queue_rq->queuelist);
Jens Axboe4711b572018-11-27 17:07:17 -07002016 plug->rq_count--;
2017 }
Jens Axboece5b0092018-11-27 17:13:56 -07002018 blk_add_rq_to_plug(plug, rq);
Yufen Yuff3b74b2019-03-26 21:19:25 +08002019 trace_block_plug(q);
Christoph Hellwig22997222017-03-22 15:01:52 -04002020
Jens Axboebf4907c2017-03-30 12:30:39 -06002021 blk_mq_put_ctx(data.ctx);
2022
Ming Leidad7a3b2017-06-06 23:21:59 +08002023 if (same_queue_rq) {
Jens Axboeea4f9952018-10-29 15:06:13 -06002024 data.hctx = same_queue_rq->mq_hctx;
Yufen Yuff3b74b2019-03-26 21:19:25 +08002025 trace_block_unplug(q, 1, true);
Christoph Hellwig22997222017-03-22 15:01:52 -04002026 blk_mq_try_issue_directly(data.hctx, same_queue_rq,
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002027 &cookie);
Ming Leidad7a3b2017-06-06 23:21:59 +08002028 }
Ming Lei6ce3dd62018-07-10 09:03:31 +08002029 } else if ((q->nr_hw_queues > 1 && is_sync) || (!q->elevator &&
2030 !data.hctx->dispatch_busy)) {
Jens Axboebd166ef2017-01-17 06:03:22 -07002031 blk_mq_put_ctx(data.ctx);
2032 blk_mq_bio_to_request(rq, bio);
Bart Van Asschefd9c40f2019-04-04 10:08:43 -07002033 blk_mq_try_issue_directly(data.hctx, rq, &cookie);
Ming Leiab42f352017-05-26 19:53:19 +08002034 } else {
Jens Axboeb00c53e2017-04-20 16:40:36 -06002035 blk_mq_put_ctx(data.ctx);
Ming Leiab42f352017-05-26 19:53:19 +08002036 blk_mq_bio_to_request(rq, bio);
huhai8fa9f552018-05-16 08:21:21 -06002037 blk_mq_sched_insert_request(rq, false, true, true);
Ming Leiab42f352017-05-26 19:53:19 +08002038 }
Jens Axboe320ae512013-10-24 09:20:05 +01002039
Jens Axboe7b371632015-11-05 10:41:40 -07002040 return cookie;
Jens Axboe320ae512013-10-24 09:20:05 +01002041}
2042
Jens Axboecc71a6f2017-01-11 14:29:56 -07002043void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2044 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01002045{
2046 struct page *page;
2047
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002048 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002049 int i;
2050
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002051 for (i = 0; i < tags->nr_tags; i++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002052 struct request *rq = tags->static_rqs[i];
2053
2054 if (!rq)
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002055 continue;
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002056 set->ops->exit_request(set, rq, hctx_idx);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002057 tags->static_rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002058 }
2059 }
2060
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002061 while (!list_empty(&tags->page_list)) {
2062 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07002063 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002064 /*
2065 * Remove kmemleak object previously allocated in
Raul E Rangel273938b2019-05-02 13:48:11 -06002066 * blk_mq_alloc_rqs().
Catalin Marinasf75782e2015-09-14 18:16:02 +01002067 */
2068 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01002069 __free_pages(page, page->private);
2070 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002071}
Jens Axboe320ae512013-10-24 09:20:05 +01002072
Jens Axboecc71a6f2017-01-11 14:29:56 -07002073void blk_mq_free_rq_map(struct blk_mq_tags *tags)
2074{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002075 kfree(tags->rqs);
Jens Axboecc71a6f2017-01-11 14:29:56 -07002076 tags->rqs = NULL;
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002077 kfree(tags->static_rqs);
2078 tags->static_rqs = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002079
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002080 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01002081}
2082
Jens Axboecc71a6f2017-01-11 14:29:56 -07002083struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
2084 unsigned int hctx_idx,
2085 unsigned int nr_tags,
2086 unsigned int reserved_tags)
Jens Axboe320ae512013-10-24 09:20:05 +01002087{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002088 struct blk_mq_tags *tags;
Shaohua Li59f082e2017-02-01 09:53:14 -08002089 int node;
Jens Axboe320ae512013-10-24 09:20:05 +01002090
Dongli Zhang7d76f852019-02-27 21:35:01 +08002091 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002092 if (node == NUMA_NO_NODE)
2093 node = set->numa_node;
2094
2095 tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
Shaohua Li24391c02015-01-23 14:18:00 -07002096 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002097 if (!tags)
2098 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002099
Kees Cook590b5b72018-06-12 14:04:20 -07002100 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002101 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
Shaohua Li59f082e2017-02-01 09:53:14 -08002102 node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002103 if (!tags->rqs) {
2104 blk_mq_free_tags(tags);
2105 return NULL;
2106 }
Jens Axboe320ae512013-10-24 09:20:05 +01002107
Kees Cook590b5b72018-06-12 14:04:20 -07002108 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
2109 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2110 node);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002111 if (!tags->static_rqs) {
2112 kfree(tags->rqs);
2113 blk_mq_free_tags(tags);
2114 return NULL;
2115 }
2116
Jens Axboecc71a6f2017-01-11 14:29:56 -07002117 return tags;
2118}
2119
2120static size_t order_to_size(unsigned int order)
2121{
2122 return (size_t)PAGE_SIZE << order;
2123}
2124
Tejun Heo1d9bd512018-01-09 08:29:48 -08002125static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2126 unsigned int hctx_idx, int node)
2127{
2128 int ret;
2129
2130 if (set->ops->init_request) {
2131 ret = set->ops->init_request(set, rq, hctx_idx, node);
2132 if (ret)
2133 return ret;
2134 }
2135
Keith Busch12f5b932018-05-29 15:52:28 +02002136 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Tejun Heo1d9bd512018-01-09 08:29:48 -08002137 return 0;
2138}
2139
Jens Axboecc71a6f2017-01-11 14:29:56 -07002140int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2141 unsigned int hctx_idx, unsigned int depth)
2142{
2143 unsigned int i, j, entries_per_page, max_order = 4;
2144 size_t rq_size, left;
Shaohua Li59f082e2017-02-01 09:53:14 -08002145 int node;
2146
Dongli Zhang7d76f852019-02-27 21:35:01 +08002147 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], hctx_idx);
Shaohua Li59f082e2017-02-01 09:53:14 -08002148 if (node == NUMA_NO_NODE)
2149 node = set->numa_node;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002150
2151 INIT_LIST_HEAD(&tags->page_list);
2152
Jens Axboe320ae512013-10-24 09:20:05 +01002153 /*
2154 * rq_size is the size of the request plus driver payload, rounded
2155 * to the cacheline size
2156 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002157 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01002158 cache_line_size());
Jens Axboecc71a6f2017-01-11 14:29:56 -07002159 left = rq_size * depth;
Jens Axboe320ae512013-10-24 09:20:05 +01002160
Jens Axboecc71a6f2017-01-11 14:29:56 -07002161 for (i = 0; i < depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01002162 int this_order = max_order;
2163 struct page *page;
2164 int to_do;
2165 void *p;
2166
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06002167 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01002168 this_order--;
2169
2170 do {
Shaohua Li59f082e2017-02-01 09:53:14 -08002171 page = alloc_pages_node(node,
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002172 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06002173 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01002174 if (page)
2175 break;
2176 if (!this_order--)
2177 break;
2178 if (order_to_size(this_order) < rq_size)
2179 break;
2180 } while (1);
2181
2182 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002183 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01002184
2185 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002186 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01002187
2188 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002189 /*
2190 * Allow kmemleak to scan these pages as they contain pointers
2191 * to additional allocations like via ops->init_request().
2192 */
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002193 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
Jens Axboe320ae512013-10-24 09:20:05 +01002194 entries_per_page = order_to_size(this_order) / rq_size;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002195 to_do = min(entries_per_page, depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01002196 left -= to_do * rq_size;
2197 for (j = 0; j < to_do; j++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002198 struct request *rq = p;
2199
2200 tags->static_rqs[i] = rq;
Tejun Heo1d9bd512018-01-09 08:29:48 -08002201 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
2202 tags->static_rqs[i] = NULL;
2203 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002204 }
2205
Jens Axboe320ae512013-10-24 09:20:05 +01002206 p += rq_size;
2207 i++;
2208 }
2209 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002210 return 0;
Jens Axboe320ae512013-10-24 09:20:05 +01002211
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002212fail:
Jens Axboecc71a6f2017-01-11 14:29:56 -07002213 blk_mq_free_rqs(set, tags, hctx_idx);
2214 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01002215}
2216
Jens Axboee57690f2016-08-24 15:34:35 -06002217/*
2218 * 'cpu' is going away. splice any existing rq_list entries from this
2219 * software queue to the hw queue dispatch list, and ensure that it
2220 * gets run.
2221 */
Thomas Gleixner9467f852016-09-22 08:05:17 -06002222static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
Jens Axboe484b4062014-05-21 14:01:15 -06002223{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002224 struct blk_mq_hw_ctx *hctx;
Jens Axboe484b4062014-05-21 14:01:15 -06002225 struct blk_mq_ctx *ctx;
2226 LIST_HEAD(tmp);
Ming Leic16d6b52018-12-17 08:44:05 -07002227 enum hctx_type type;
Jens Axboe484b4062014-05-21 14:01:15 -06002228
Thomas Gleixner9467f852016-09-22 08:05:17 -06002229 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
Jens Axboee57690f2016-08-24 15:34:35 -06002230 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Ming Leic16d6b52018-12-17 08:44:05 -07002231 type = hctx->type;
Jens Axboe484b4062014-05-21 14:01:15 -06002232
2233 spin_lock(&ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002234 if (!list_empty(&ctx->rq_lists[type])) {
2235 list_splice_init(&ctx->rq_lists[type], &tmp);
Jens Axboe484b4062014-05-21 14:01:15 -06002236 blk_mq_hctx_clear_pending(hctx, ctx);
2237 }
2238 spin_unlock(&ctx->lock);
2239
2240 if (list_empty(&tmp))
Thomas Gleixner9467f852016-09-22 08:05:17 -06002241 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002242
Jens Axboee57690f2016-08-24 15:34:35 -06002243 spin_lock(&hctx->lock);
2244 list_splice_tail_init(&tmp, &hctx->dispatch);
2245 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06002246
2247 blk_mq_run_hw_queue(hctx, true);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002248 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002249}
2250
Thomas Gleixner9467f852016-09-22 08:05:17 -06002251static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
Jens Axboe484b4062014-05-21 14:01:15 -06002252{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002253 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
2254 &hctx->cpuhp_dead);
Jens Axboe484b4062014-05-21 14:01:15 -06002255}
2256
Ming Leic3b4afc2015-06-04 22:25:04 +08002257/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08002258static void blk_mq_exit_hctx(struct request_queue *q,
2259 struct blk_mq_tag_set *set,
2260 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
2261{
Ming Lei8ab0b7d2018-01-09 21:28:29 +08002262 if (blk_mq_hw_queue_mapped(hctx))
2263 blk_mq_tag_idle(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002264
Ming Leif70ced02014-09-25 23:23:47 +08002265 if (set->ops->exit_request)
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002266 set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx);
Ming Leif70ced02014-09-25 23:23:47 +08002267
Ming Lei08e98fc2014-09-25 23:23:38 +08002268 if (set->ops->exit_hctx)
2269 set->ops->exit_hctx(hctx, hctx_idx);
2270
Thomas Gleixner9467f852016-09-22 08:05:17 -06002271 blk_mq_remove_cpuhp(hctx);
Ming Lei2f8f1332019-04-30 09:52:27 +08002272
2273 spin_lock(&q->unused_hctx_lock);
2274 list_add(&hctx->hctx_list, &q->unused_hctx_list);
2275 spin_unlock(&q->unused_hctx_lock);
Ming Lei08e98fc2014-09-25 23:23:38 +08002276}
2277
Ming Lei624dbe42014-05-27 23:35:13 +08002278static void blk_mq_exit_hw_queues(struct request_queue *q,
2279 struct blk_mq_tag_set *set, int nr_queue)
2280{
2281 struct blk_mq_hw_ctx *hctx;
2282 unsigned int i;
2283
2284 queue_for_each_hw_ctx(q, hctx, i) {
2285 if (i == nr_queue)
2286 break;
Jianchao Wang477e19d2018-10-12 18:07:25 +08002287 blk_mq_debugfs_unregister_hctx(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002288 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08002289 }
Ming Lei624dbe42014-05-27 23:35:13 +08002290}
2291
Ming Lei7c6c5b72019-04-30 09:52:26 +08002292static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
2293{
2294 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
2295
2296 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
2297 __alignof__(struct blk_mq_hw_ctx)) !=
2298 sizeof(struct blk_mq_hw_ctx));
2299
2300 if (tag_set->flags & BLK_MQ_F_BLOCKING)
2301 hw_ctx_size += sizeof(struct srcu_struct);
2302
2303 return hw_ctx_size;
2304}
2305
Ming Lei08e98fc2014-09-25 23:23:38 +08002306static int blk_mq_init_hctx(struct request_queue *q,
2307 struct blk_mq_tag_set *set,
2308 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
2309{
Ming Lei7c6c5b72019-04-30 09:52:26 +08002310 hctx->queue_num = hctx_idx;
Ming Lei08e98fc2014-09-25 23:23:38 +08002311
Ming Lei7c6c5b72019-04-30 09:52:26 +08002312 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
2313
2314 hctx->tags = set->tags[hctx_idx];
2315
2316 if (set->ops->init_hctx &&
2317 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
2318 goto unregister_cpu_notifier;
2319
2320 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx,
2321 hctx->numa_node))
2322 goto exit_hctx;
2323 return 0;
2324
2325 exit_hctx:
2326 if (set->ops->exit_hctx)
2327 set->ops->exit_hctx(hctx, hctx_idx);
2328 unregister_cpu_notifier:
2329 blk_mq_remove_cpuhp(hctx);
2330 return -1;
2331}
2332
2333static struct blk_mq_hw_ctx *
2334blk_mq_alloc_hctx(struct request_queue *q, struct blk_mq_tag_set *set,
2335 int node)
2336{
2337 struct blk_mq_hw_ctx *hctx;
2338 gfp_t gfp = GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY;
2339
2340 hctx = kzalloc_node(blk_mq_hw_ctx_size(set), gfp, node);
2341 if (!hctx)
2342 goto fail_alloc_hctx;
2343
2344 if (!zalloc_cpumask_var_node(&hctx->cpumask, gfp, node))
2345 goto free_hctx;
2346
2347 atomic_set(&hctx->nr_active, 0);
Ming Lei08e98fc2014-09-25 23:23:38 +08002348 if (node == NUMA_NO_NODE)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002349 node = set->numa_node;
2350 hctx->numa_node = node;
Ming Lei08e98fc2014-09-25 23:23:38 +08002351
Jens Axboe9f993732017-04-10 09:54:54 -06002352 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08002353 spin_lock_init(&hctx->lock);
2354 INIT_LIST_HEAD(&hctx->dispatch);
2355 hctx->queue = q;
Jeff Moyer2404e602015-11-03 10:40:06 -05002356 hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08002357
Ming Lei2f8f1332019-04-30 09:52:27 +08002358 INIT_LIST_HEAD(&hctx->hctx_list);
2359
Ming Lei08e98fc2014-09-25 23:23:38 +08002360 /*
2361 * Allocate space for all possible cpus to avoid allocation at
2362 * runtime
2363 */
Johannes Thumshirnd904bfa2017-11-15 17:32:33 -08002364 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
Ming Lei7c6c5b72019-04-30 09:52:26 +08002365 gfp, node);
Ming Lei08e98fc2014-09-25 23:23:38 +08002366 if (!hctx->ctxs)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002367 goto free_cpumask;
Ming Lei08e98fc2014-09-25 23:23:38 +08002368
Jianchao Wang5b202852018-10-12 18:07:26 +08002369 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
Ming Lei7c6c5b72019-04-30 09:52:26 +08002370 gfp, node))
Ming Lei08e98fc2014-09-25 23:23:38 +08002371 goto free_ctxs;
Ming Lei08e98fc2014-09-25 23:23:38 +08002372 hctx->nr_ctx = 0;
2373
Ming Lei5815839b2018-06-25 19:31:47 +08002374 spin_lock_init(&hctx->dispatch_wait_lock);
Jens Axboeeb619fd2017-11-09 08:32:43 -07002375 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
2376 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
2377
Jianchao Wang5b202852018-10-12 18:07:26 +08002378 hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size,
Ming Lei7c6c5b72019-04-30 09:52:26 +08002379 gfp);
Ming Leif70ced02014-09-25 23:23:47 +08002380 if (!hctx->fq)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002381 goto free_bitmap;
Ming Leif70ced02014-09-25 23:23:47 +08002382
Bart Van Assche6a83e742016-11-02 10:09:51 -06002383 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -08002384 init_srcu_struct(hctx->srcu);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002385 blk_mq_hctx_kobj_init(hctx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06002386
Ming Lei7c6c5b72019-04-30 09:52:26 +08002387 return hctx;
Ming Lei08e98fc2014-09-25 23:23:38 +08002388
2389 free_bitmap:
Omar Sandoval88459642016-09-17 08:38:44 -06002390 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08002391 free_ctxs:
2392 kfree(hctx->ctxs);
Ming Lei7c6c5b72019-04-30 09:52:26 +08002393 free_cpumask:
2394 free_cpumask_var(hctx->cpumask);
2395 free_hctx:
2396 kfree(hctx);
2397 fail_alloc_hctx:
2398 return NULL;
Ming Lei08e98fc2014-09-25 23:23:38 +08002399}
2400
Jens Axboe320ae512013-10-24 09:20:05 +01002401static void blk_mq_init_cpu_queues(struct request_queue *q,
2402 unsigned int nr_hw_queues)
2403{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002404 struct blk_mq_tag_set *set = q->tag_set;
2405 unsigned int i, j;
Jens Axboe320ae512013-10-24 09:20:05 +01002406
2407 for_each_possible_cpu(i) {
2408 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
2409 struct blk_mq_hw_ctx *hctx;
Ming Leic16d6b52018-12-17 08:44:05 -07002410 int k;
Jens Axboe320ae512013-10-24 09:20:05 +01002411
Jens Axboe320ae512013-10-24 09:20:05 +01002412 __ctx->cpu = i;
2413 spin_lock_init(&__ctx->lock);
Ming Leic16d6b52018-12-17 08:44:05 -07002414 for (k = HCTX_TYPE_DEFAULT; k < HCTX_MAX_TYPES; k++)
2415 INIT_LIST_HEAD(&__ctx->rq_lists[k]);
2416
Jens Axboe320ae512013-10-24 09:20:05 +01002417 __ctx->queue = q;
2418
Jens Axboe320ae512013-10-24 09:20:05 +01002419 /*
2420 * Set local node, IFF we have more than one hw queue. If
2421 * not, we remain on the home node of the device
2422 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06002423 for (j = 0; j < set->nr_maps; j++) {
2424 hctx = blk_mq_map_queue_type(q, j, i);
2425 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
2426 hctx->numa_node = local_memory_node(cpu_to_node(i));
2427 }
Jens Axboe320ae512013-10-24 09:20:05 +01002428 }
2429}
2430
Jens Axboecc71a6f2017-01-11 14:29:56 -07002431static bool __blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, int hctx_idx)
2432{
2433 int ret = 0;
2434
2435 set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
2436 set->queue_depth, set->reserved_tags);
2437 if (!set->tags[hctx_idx])
2438 return false;
2439
2440 ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
2441 set->queue_depth);
2442 if (!ret)
2443 return true;
2444
2445 blk_mq_free_rq_map(set->tags[hctx_idx]);
2446 set->tags[hctx_idx] = NULL;
2447 return false;
2448}
2449
2450static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
2451 unsigned int hctx_idx)
2452{
Dan Carpenter4e6db0f2018-11-29 13:56:54 +03002453 if (set->tags && set->tags[hctx_idx]) {
Jens Axboebd166ef2017-01-17 06:03:22 -07002454 blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
2455 blk_mq_free_rq_map(set->tags[hctx_idx]);
2456 set->tags[hctx_idx] = NULL;
2457 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002458}
2459
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002460static void blk_mq_map_swqueue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01002461{
Jens Axboeb3c661b2018-10-30 10:36:06 -06002462 unsigned int i, j, hctx_idx;
Jens Axboe320ae512013-10-24 09:20:05 +01002463 struct blk_mq_hw_ctx *hctx;
2464 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08002465 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002466
Akinobu Mita60de0742015-09-27 02:09:25 +09002467 /*
2468 * Avoid others reading imcomplete hctx->cpumask through sysfs
2469 */
2470 mutex_lock(&q->sysfs_lock);
2471
Jens Axboe320ae512013-10-24 09:20:05 +01002472 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06002473 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01002474 hctx->nr_ctx = 0;
huhaid416c922018-05-18 08:32:30 -06002475 hctx->dispatch_from = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002476 }
2477
2478 /*
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002479 * Map software to hardware queues.
Ming Lei4412efe2018-04-25 04:01:44 +08002480 *
2481 * If the cpu isn't present, the cpu is mapped to first hctx.
Jens Axboe320ae512013-10-24 09:20:05 +01002482 */
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08002483 for_each_possible_cpu(i) {
Dongli Zhang7d76f852019-02-27 21:35:01 +08002484 hctx_idx = set->map[HCTX_TYPE_DEFAULT].mq_map[i];
Ming Lei4412efe2018-04-25 04:01:44 +08002485 /* unmapped hw queue can be remapped after CPU topo changed */
2486 if (!set->tags[hctx_idx] &&
2487 !__blk_mq_alloc_rq_map(set, hctx_idx)) {
2488 /*
2489 * If tags initialization fail for some hctx,
2490 * that hctx won't be brought online. In this
2491 * case, remap the current ctx to hctx[0] which
2492 * is guaranteed to always have tags allocated
2493 */
Dongli Zhang7d76f852019-02-27 21:35:01 +08002494 set->map[HCTX_TYPE_DEFAULT].mq_map[i] = 0;
Ming Lei4412efe2018-04-25 04:01:44 +08002495 }
2496
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01002497 ctx = per_cpu_ptr(q->queue_ctx, i);
Jens Axboeb3c661b2018-10-30 10:36:06 -06002498 for (j = 0; j < set->nr_maps; j++) {
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002499 if (!set->map[j].nr_queues) {
2500 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2501 HCTX_TYPE_DEFAULT, i);
Ming Leie5edd5f2018-12-18 01:28:56 +08002502 continue;
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002503 }
Ming Leie5edd5f2018-12-18 01:28:56 +08002504
Jens Axboeb3c661b2018-10-30 10:36:06 -06002505 hctx = blk_mq_map_queue_type(q, j, i);
Jianchao Wang8ccdf4a2019-01-24 18:25:32 +08002506 ctx->hctxs[j] = hctx;
Jens Axboeb3c661b2018-10-30 10:36:06 -06002507 /*
2508 * If the CPU is already set in the mask, then we've
2509 * mapped this one already. This can happen if
2510 * devices share queues across queue maps.
2511 */
2512 if (cpumask_test_cpu(i, hctx->cpumask))
2513 continue;
2514
2515 cpumask_set_cpu(i, hctx->cpumask);
2516 hctx->type = j;
2517 ctx->index_hw[hctx->type] = hctx->nr_ctx;
2518 hctx->ctxs[hctx->nr_ctx++] = ctx;
2519
2520 /*
2521 * If the nr_ctx type overflows, we have exceeded the
2522 * amount of sw queues we can support.
2523 */
2524 BUG_ON(!hctx->nr_ctx);
2525 }
Jianchao Wangbb94aea2019-01-24 18:25:33 +08002526
2527 for (; j < HCTX_MAX_TYPES; j++)
2528 ctx->hctxs[j] = blk_mq_map_queue_type(q,
2529 HCTX_TYPE_DEFAULT, i);
Jens Axboe320ae512013-10-24 09:20:05 +01002530 }
Jens Axboe506e9312014-05-07 10:26:44 -06002531
Akinobu Mita60de0742015-09-27 02:09:25 +09002532 mutex_unlock(&q->sysfs_lock);
2533
Jens Axboe506e9312014-05-07 10:26:44 -06002534 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei4412efe2018-04-25 04:01:44 +08002535 /*
2536 * If no software queues are mapped to this hardware queue,
2537 * disable it and free the request entries.
2538 */
2539 if (!hctx->nr_ctx) {
2540 /* Never unmap queue 0. We need it as a
2541 * fallback in case of a new remap fails
2542 * allocation
2543 */
2544 if (i && set->tags[i])
2545 blk_mq_free_map_and_requests(set, i);
2546
2547 hctx->tags = NULL;
2548 continue;
2549 }
Jens Axboe484b4062014-05-21 14:01:15 -06002550
Ming Lei2a34c082015-04-21 10:00:20 +08002551 hctx->tags = set->tags[i];
2552 WARN_ON(!hctx->tags);
2553
Jens Axboe484b4062014-05-21 14:01:15 -06002554 /*
Chong Yuan889fa312015-04-15 11:39:29 -06002555 * Set the map size to the number of mapped software queues.
2556 * This is more accurate and more efficient than looping
2557 * over all possibly mapped software queues.
2558 */
Omar Sandoval88459642016-09-17 08:38:44 -06002559 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
Chong Yuan889fa312015-04-15 11:39:29 -06002560
2561 /*
Jens Axboe484b4062014-05-21 14:01:15 -06002562 * Initialize batch roundrobin counts
2563 */
Ming Leif82ddf12018-04-08 17:48:10 +08002564 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06002565 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
2566 }
Jens Axboe320ae512013-10-24 09:20:05 +01002567}
2568
Jens Axboe8e8320c2017-06-20 17:56:13 -06002569/*
2570 * Caller needs to ensure that we're either frozen/quiesced, or that
2571 * the queue isn't live yet.
2572 */
Jeff Moyer2404e602015-11-03 10:40:06 -05002573static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06002574{
2575 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06002576 int i;
2577
Jeff Moyer2404e602015-11-03 10:40:06 -05002578 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei97889f92018-06-25 19:31:48 +08002579 if (shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05002580 hctx->flags |= BLK_MQ_F_TAG_SHARED;
Ming Lei97889f92018-06-25 19:31:48 +08002581 else
Jeff Moyer2404e602015-11-03 10:40:06 -05002582 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
2583 }
2584}
2585
Jens Axboe8e8320c2017-06-20 17:56:13 -06002586static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
2587 bool shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05002588{
2589 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06002590
Bart Van Assche705cda92017-04-07 11:16:49 -07002591 lockdep_assert_held(&set->tag_list_lock);
2592
Jens Axboe0d2602c2014-05-13 15:10:52 -06002593 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2594 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05002595 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002596 blk_mq_unfreeze_queue(q);
2597 }
2598}
2599
2600static void blk_mq_del_queue_tag_set(struct request_queue *q)
2601{
2602 struct blk_mq_tag_set *set = q->tag_set;
2603
Jens Axboe0d2602c2014-05-13 15:10:52 -06002604 mutex_lock(&set->tag_list_lock);
Bart Van Assche705cda92017-04-07 11:16:49 -07002605 list_del_rcu(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05002606 if (list_is_singular(&set->tag_list)) {
2607 /* just transitioned to unshared */
2608 set->flags &= ~BLK_MQ_F_TAG_SHARED;
2609 /* update existing queue */
2610 blk_mq_update_tag_set_depth(set, false);
2611 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06002612 mutex_unlock(&set->tag_list_lock);
Roman Pena347c7a2018-06-10 22:38:24 +02002613 INIT_LIST_HEAD(&q->tag_set_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002614}
2615
2616static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
2617 struct request_queue *q)
2618{
Jens Axboe0d2602c2014-05-13 15:10:52 -06002619 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05002620
Jens Axboeff821d22017-11-10 22:05:12 -07002621 /*
2622 * Check to see if we're transitioning to shared (from 1 to 2 queues).
2623 */
2624 if (!list_empty(&set->tag_list) &&
2625 !(set->flags & BLK_MQ_F_TAG_SHARED)) {
Jeff Moyer2404e602015-11-03 10:40:06 -05002626 set->flags |= BLK_MQ_F_TAG_SHARED;
2627 /* update existing queue */
2628 blk_mq_update_tag_set_depth(set, true);
2629 }
2630 if (set->flags & BLK_MQ_F_TAG_SHARED)
2631 queue_set_hctx_shared(q, true);
Bart Van Assche705cda92017-04-07 11:16:49 -07002632 list_add_tail_rcu(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05002633
Jens Axboe0d2602c2014-05-13 15:10:52 -06002634 mutex_unlock(&set->tag_list_lock);
2635}
2636
Ming Lei1db49092018-11-20 09:44:35 +08002637/* All allocations will be freed in release handler of q->mq_kobj */
2638static int blk_mq_alloc_ctxs(struct request_queue *q)
2639{
2640 struct blk_mq_ctxs *ctxs;
2641 int cpu;
2642
2643 ctxs = kzalloc(sizeof(*ctxs), GFP_KERNEL);
2644 if (!ctxs)
2645 return -ENOMEM;
2646
2647 ctxs->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2648 if (!ctxs->queue_ctx)
2649 goto fail;
2650
2651 for_each_possible_cpu(cpu) {
2652 struct blk_mq_ctx *ctx = per_cpu_ptr(ctxs->queue_ctx, cpu);
2653 ctx->ctxs = ctxs;
2654 }
2655
2656 q->mq_kobj = &ctxs->kobj;
2657 q->queue_ctx = ctxs->queue_ctx;
2658
2659 return 0;
2660 fail:
2661 kfree(ctxs);
2662 return -ENOMEM;
2663}
2664
Ming Leie09aae72015-01-29 20:17:27 +08002665/*
2666 * It is the actual release handler for mq, but we do it from
2667 * request queue's release handler for avoiding use-after-free
2668 * and headache because q->mq_kobj shouldn't have been introduced,
2669 * but we can't group ctx/kctx kobj without it.
2670 */
2671void blk_mq_release(struct request_queue *q)
2672{
Ming Lei2f8f1332019-04-30 09:52:27 +08002673 struct blk_mq_hw_ctx *hctx, *next;
2674 int i;
Ming Leie09aae72015-01-29 20:17:27 +08002675
Ming Leifbc2a152019-04-30 09:52:24 +08002676 cancel_delayed_work_sync(&q->requeue_work);
2677
Ming Lei2f8f1332019-04-30 09:52:27 +08002678 queue_for_each_hw_ctx(q, hctx, i)
2679 WARN_ON_ONCE(hctx && list_empty(&hctx->hctx_list));
2680
2681 /* all hctx are in .unused_hctx_list now */
2682 list_for_each_entry_safe(hctx, next, &q->unused_hctx_list, hctx_list) {
2683 list_del_init(&hctx->hctx_list);
Ming Lei6c8b2322017-02-22 18:14:01 +08002684 kobject_put(&hctx->kobj);
Ming Leic3b4afc2015-06-04 22:25:04 +08002685 }
Ming Leie09aae72015-01-29 20:17:27 +08002686
2687 kfree(q->queue_hw_ctx);
2688
Ming Lei7ea5fe32017-02-22 18:14:00 +08002689 /*
2690 * release .mq_kobj and sw queue's kobject now because
2691 * both share lifetime with request queue.
2692 */
2693 blk_mq_sysfs_deinit(q);
Ming Leie09aae72015-01-29 20:17:27 +08002694}
2695
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002696struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01002697{
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002698 struct request_queue *uninit_q, *q;
2699
Christoph Hellwig6d469642018-11-14 17:02:18 +01002700 uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node);
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002701 if (!uninit_q)
2702 return ERR_PTR(-ENOMEM);
2703
2704 q = blk_mq_init_allocated_queue(set, uninit_q);
2705 if (IS_ERR(q))
2706 blk_cleanup_queue(uninit_q);
2707
2708 return q;
2709}
2710EXPORT_SYMBOL(blk_mq_init_queue);
2711
Jens Axboe9316a9e2018-10-15 08:40:37 -06002712/*
2713 * Helper for setting up a queue with mq ops, given queue depth, and
2714 * the passed in mq ops flags.
2715 */
2716struct request_queue *blk_mq_init_sq_queue(struct blk_mq_tag_set *set,
2717 const struct blk_mq_ops *ops,
2718 unsigned int queue_depth,
2719 unsigned int set_flags)
2720{
2721 struct request_queue *q;
2722 int ret;
2723
2724 memset(set, 0, sizeof(*set));
2725 set->ops = ops;
2726 set->nr_hw_queues = 1;
Jens Axboeb3c661b2018-10-30 10:36:06 -06002727 set->nr_maps = 1;
Jens Axboe9316a9e2018-10-15 08:40:37 -06002728 set->queue_depth = queue_depth;
2729 set->numa_node = NUMA_NO_NODE;
2730 set->flags = set_flags;
2731
2732 ret = blk_mq_alloc_tag_set(set);
2733 if (ret)
2734 return ERR_PTR(ret);
2735
2736 q = blk_mq_init_queue(set);
2737 if (IS_ERR(q)) {
2738 blk_mq_free_tag_set(set);
2739 return q;
2740 }
2741
2742 return q;
2743}
2744EXPORT_SYMBOL(blk_mq_init_sq_queue);
2745
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002746static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
2747 struct blk_mq_tag_set *set, struct request_queue *q,
2748 int hctx_idx, int node)
2749{
Ming Lei2f8f1332019-04-30 09:52:27 +08002750 struct blk_mq_hw_ctx *hctx = NULL, *tmp;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002751
Ming Lei2f8f1332019-04-30 09:52:27 +08002752 /* reuse dead hctx first */
2753 spin_lock(&q->unused_hctx_lock);
2754 list_for_each_entry(tmp, &q->unused_hctx_list, hctx_list) {
2755 if (tmp->numa_node == node) {
2756 hctx = tmp;
2757 break;
2758 }
2759 }
2760 if (hctx)
2761 list_del_init(&hctx->hctx_list);
2762 spin_unlock(&q->unused_hctx_lock);
2763
2764 if (!hctx)
2765 hctx = blk_mq_alloc_hctx(q, set, node);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002766 if (!hctx)
Ming Lei7c6c5b72019-04-30 09:52:26 +08002767 goto fail;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002768
Ming Lei7c6c5b72019-04-30 09:52:26 +08002769 if (blk_mq_init_hctx(q, set, hctx, hctx_idx))
2770 goto free_hctx;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002771
2772 return hctx;
Ming Lei7c6c5b72019-04-30 09:52:26 +08002773
2774 free_hctx:
2775 kobject_put(&hctx->kobj);
2776 fail:
2777 return NULL;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002778}
2779
Keith Busch868f2f02015-12-17 17:08:14 -07002780static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
2781 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002782{
Jianchao Wange01ad462018-10-12 18:07:28 +08002783 int i, j, end;
Keith Busch868f2f02015-12-17 17:08:14 -07002784 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01002785
Ming Leifb350e02018-01-06 16:27:40 +08002786 /* protect against switching io scheduler */
2787 mutex_lock(&q->sysfs_lock);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002788 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07002789 int node;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002790 struct blk_mq_hw_ctx *hctx;
Keith Busch868f2f02015-12-17 17:08:14 -07002791
Dongli Zhang7d76f852019-02-27 21:35:01 +08002792 node = blk_mq_hw_queue_to_node(&set->map[HCTX_TYPE_DEFAULT], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002793 /*
2794 * If the hw queue has been mapped to another numa node,
2795 * we need to realloc the hctx. If allocation fails, fallback
2796 * to use the previous one.
2797 */
2798 if (hctxs[i] && (hctxs[i]->numa_node == node))
2799 continue;
Jens Axboe320ae512013-10-24 09:20:05 +01002800
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002801 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
2802 if (hctx) {
Ming Lei2f8f1332019-04-30 09:52:27 +08002803 if (hctxs[i])
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002804 blk_mq_exit_hctx(q, set, hctxs[i], i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002805 hctxs[i] = hctx;
2806 } else {
2807 if (hctxs[i])
2808 pr_warn("Allocate new hctx on node %d fails,\
2809 fallback to previous one on node %d\n",
2810 node, hctxs[i]->numa_node);
2811 else
2812 break;
Keith Busch868f2f02015-12-17 17:08:14 -07002813 }
Jens Axboe320ae512013-10-24 09:20:05 +01002814 }
Jianchao Wange01ad462018-10-12 18:07:28 +08002815 /*
2816 * Increasing nr_hw_queues fails. Free the newly allocated
2817 * hctxs and keep the previous q->nr_hw_queues.
2818 */
2819 if (i != set->nr_hw_queues) {
2820 j = q->nr_hw_queues;
2821 end = i;
2822 } else {
2823 j = i;
2824 end = q->nr_hw_queues;
2825 q->nr_hw_queues = set->nr_hw_queues;
2826 }
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002827
Jianchao Wange01ad462018-10-12 18:07:28 +08002828 for (; j < end; j++) {
Keith Busch868f2f02015-12-17 17:08:14 -07002829 struct blk_mq_hw_ctx *hctx = hctxs[j];
2830
2831 if (hctx) {
Jens Axboecc71a6f2017-01-11 14:29:56 -07002832 if (hctx->tags)
2833 blk_mq_free_map_and_requests(set, j);
Keith Busch868f2f02015-12-17 17:08:14 -07002834 blk_mq_exit_hctx(q, set, hctx, j);
Keith Busch868f2f02015-12-17 17:08:14 -07002835 hctxs[j] = NULL;
Keith Busch868f2f02015-12-17 17:08:14 -07002836 }
2837 }
Ming Leifb350e02018-01-06 16:27:40 +08002838 mutex_unlock(&q->sysfs_lock);
Keith Busch868f2f02015-12-17 17:08:14 -07002839}
2840
Jens Axboe392546a2018-10-29 13:25:27 -06002841/*
2842 * Maximum number of hardware queues we support. For single sets, we'll never
2843 * have more than the CPUs (software queues). For multiple sets, the tag_set
2844 * user may have set ->nr_hw_queues larger.
2845 */
2846static unsigned int nr_hw_queues(struct blk_mq_tag_set *set)
2847{
2848 if (set->nr_maps == 1)
2849 return nr_cpu_ids;
2850
2851 return max(set->nr_hw_queues, nr_cpu_ids);
2852}
2853
Keith Busch868f2f02015-12-17 17:08:14 -07002854struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
2855 struct request_queue *q)
2856{
Ming Lei66841672016-02-12 15:27:00 +08002857 /* mark the queue as mq asap */
2858 q->mq_ops = set->ops;
2859
Omar Sandoval34dbad52017-03-21 08:56:08 -07002860 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
Stephen Bates720b8cc2017-04-07 06:24:03 -06002861 blk_mq_poll_stats_bkt,
2862 BLK_MQ_POLL_STATS_BKTS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -07002863 if (!q->poll_cb)
2864 goto err_exit;
2865
Ming Lei1db49092018-11-20 09:44:35 +08002866 if (blk_mq_alloc_ctxs(q))
Ming Linc7de5722016-05-25 23:23:27 -07002867 goto err_exit;
Keith Busch868f2f02015-12-17 17:08:14 -07002868
Ming Lei737f98c2017-02-22 18:13:59 +08002869 /* init q->mq_kobj and sw queues' kobjects */
2870 blk_mq_sysfs_init(q);
2871
Jens Axboe392546a2018-10-29 13:25:27 -06002872 q->nr_queues = nr_hw_queues(set);
2873 q->queue_hw_ctx = kcalloc_node(q->nr_queues, sizeof(*(q->queue_hw_ctx)),
Keith Busch868f2f02015-12-17 17:08:14 -07002874 GFP_KERNEL, set->numa_node);
2875 if (!q->queue_hw_ctx)
Ming Lei1db49092018-11-20 09:44:35 +08002876 goto err_sys_init;
Keith Busch868f2f02015-12-17 17:08:14 -07002877
Ming Lei2f8f1332019-04-30 09:52:27 +08002878 INIT_LIST_HEAD(&q->unused_hctx_list);
2879 spin_lock_init(&q->unused_hctx_lock);
2880
Keith Busch868f2f02015-12-17 17:08:14 -07002881 blk_mq_realloc_hw_ctxs(set, q);
2882 if (!q->nr_hw_queues)
2883 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01002884
Christoph Hellwig287922e2015-10-30 20:57:30 +08002885 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08002886 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01002887
Jens Axboea8908932018-10-16 14:23:06 -06002888 q->tag_set = set;
Jens Axboe320ae512013-10-24 09:20:05 +01002889
Jens Axboe94eddfb2013-11-19 09:25:07 -07002890 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Ming Leicd191812018-12-18 12:15:29 +08002891 if (set->nr_maps > HCTX_TYPE_POLL &&
2892 set->map[HCTX_TYPE_POLL].nr_queues)
Christoph Hellwig6544d222018-12-02 17:46:28 +01002893 blk_queue_flag_set(QUEUE_FLAG_POLL, q);
Jens Axboe320ae512013-10-24 09:20:05 +01002894
Christoph Hellwig1be036e2014-02-07 10:22:39 -08002895 q->sg_reserved_size = INT_MAX;
2896
Mike Snitzer28494502016-09-14 13:28:30 -04002897 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06002898 INIT_LIST_HEAD(&q->requeue_list);
2899 spin_lock_init(&q->requeue_lock);
2900
Christoph Hellwig254d2592017-03-22 15:01:50 -04002901 blk_queue_make_request(q, blk_mq_make_request);
Jens Axboe07068d52014-05-22 10:40:51 -06002902
Jens Axboeeba71762014-05-20 15:17:27 -06002903 /*
2904 * Do this after blk_queue_make_request() overrides it...
2905 */
2906 q->nr_requests = set->queue_depth;
2907
Jens Axboe64f1c212016-11-14 13:03:03 -07002908 /*
2909 * Default to classic polling
2910 */
Yufen Yu29ece8b2019-03-18 22:44:41 +08002911 q->poll_nsec = BLK_MQ_POLL_CLASSIC;
Jens Axboe64f1c212016-11-14 13:03:03 -07002912
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002913 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002914 blk_mq_add_queue_tag_set(set, q);
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002915 blk_mq_map_swqueue(q);
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002916
Jens Axboed3484992017-01-13 14:43:58 -07002917 if (!(set->flags & BLK_MQ_F_NO_SCHED)) {
2918 int ret;
2919
Christoph Hellwig131d08e2018-05-31 19:11:40 +02002920 ret = elevator_init_mq(q);
Jens Axboed3484992017-01-13 14:43:58 -07002921 if (ret)
2922 return ERR_PTR(ret);
2923 }
2924
Jens Axboe320ae512013-10-24 09:20:05 +01002925 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07002926
Jens Axboe320ae512013-10-24 09:20:05 +01002927err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07002928 kfree(q->queue_hw_ctx);
Ming Lei1db49092018-11-20 09:44:35 +08002929err_sys_init:
2930 blk_mq_sysfs_deinit(q);
Ming Linc7de5722016-05-25 23:23:27 -07002931err_exit:
2932 q->mq_ops = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002933 return ERR_PTR(-ENOMEM);
2934}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002935EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01002936
Ming Leic7e2d942019-04-30 09:52:25 +08002937/* tags can _not_ be used after returning from blk_mq_exit_queue */
2938void blk_mq_exit_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01002939{
Ming Lei624dbe42014-05-27 23:35:13 +08002940 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002941
Jens Axboe0d2602c2014-05-13 15:10:52 -06002942 blk_mq_del_queue_tag_set(q);
Ming Lei624dbe42014-05-27 23:35:13 +08002943 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01002944}
Jens Axboe320ae512013-10-24 09:20:05 +01002945
Jens Axboea5164402014-09-10 09:02:03 -06002946static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2947{
2948 int i;
2949
Jens Axboecc71a6f2017-01-11 14:29:56 -07002950 for (i = 0; i < set->nr_hw_queues; i++)
2951 if (!__blk_mq_alloc_rq_map(set, i))
Jens Axboea5164402014-09-10 09:02:03 -06002952 goto out_unwind;
Jens Axboea5164402014-09-10 09:02:03 -06002953
2954 return 0;
2955
2956out_unwind:
2957 while (--i >= 0)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002958 blk_mq_free_rq_map(set->tags[i]);
Jens Axboea5164402014-09-10 09:02:03 -06002959
Jens Axboea5164402014-09-10 09:02:03 -06002960 return -ENOMEM;
2961}
2962
2963/*
2964 * Allocate the request maps associated with this tag_set. Note that this
2965 * may reduce the depth asked for, if memory is tight. set->queue_depth
2966 * will be updated to reflect the allocated depth.
2967 */
2968static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2969{
2970 unsigned int depth;
2971 int err;
2972
2973 depth = set->queue_depth;
2974 do {
2975 err = __blk_mq_alloc_rq_maps(set);
2976 if (!err)
2977 break;
2978
2979 set->queue_depth >>= 1;
2980 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
2981 err = -ENOMEM;
2982 break;
2983 }
2984 } while (set->queue_depth);
2985
2986 if (!set->queue_depth || err) {
2987 pr_err("blk-mq: failed to allocate request map\n");
2988 return -ENOMEM;
2989 }
2990
2991 if (depth != set->queue_depth)
2992 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2993 depth, set->queue_depth);
2994
2995 return 0;
2996}
2997
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06002998static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
2999{
Ming Lei59388702018-12-07 11:03:53 +08003000 if (set->ops->map_queues && !is_kdump_kernel()) {
Jens Axboeb3c661b2018-10-30 10:36:06 -06003001 int i;
3002
Ming Lei7d4901a2018-01-06 16:27:39 +08003003 /*
3004 * transport .map_queues is usually done in the following
3005 * way:
3006 *
3007 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
3008 * mask = get_cpu_mask(queue)
3009 * for_each_cpu(cpu, mask)
Jens Axboeb3c661b2018-10-30 10:36:06 -06003010 * set->map[x].mq_map[cpu] = queue;
Ming Lei7d4901a2018-01-06 16:27:39 +08003011 * }
3012 *
3013 * When we need to remap, the table has to be cleared for
3014 * killing stale mapping since one CPU may not be mapped
3015 * to any hw queue.
3016 */
Jens Axboeb3c661b2018-10-30 10:36:06 -06003017 for (i = 0; i < set->nr_maps; i++)
3018 blk_mq_clear_mq_map(&set->map[i]);
Ming Lei7d4901a2018-01-06 16:27:39 +08003019
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003020 return set->ops->map_queues(set);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003021 } else {
3022 BUG_ON(set->nr_maps > 1);
Dongli Zhang7d76f852019-02-27 21:35:01 +08003023 return blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jens Axboeb3c661b2018-10-30 10:36:06 -06003024 }
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003025}
3026
Jens Axboea4391c62014-06-05 15:21:56 -06003027/*
3028 * Alloc a tag set to be associated with one or more request queues.
3029 * May fail with EINVAL for various error conditions. May adjust the
Minwoo Imc018c842018-06-30 22:12:41 +09003030 * requested depth down, if it's too large. In that case, the set
Jens Axboea4391c62014-06-05 15:21:56 -06003031 * value will be stored in set->queue_depth.
3032 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003033int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
3034{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003035 int i, ret;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003036
Bart Van Assche205fb5f2014-10-30 14:45:11 +01003037 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
3038
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003039 if (!set->nr_hw_queues)
3040 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06003041 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003042 return -EINVAL;
3043 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
3044 return -EINVAL;
3045
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02003046 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003047 return -EINVAL;
3048
Ming Leide148292017-10-14 17:22:29 +08003049 if (!set->ops->get_budget ^ !set->ops->put_budget)
3050 return -EINVAL;
3051
Jens Axboea4391c62014-06-05 15:21:56 -06003052 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
3053 pr_info("blk-mq: reduced tag depth to %u\n",
3054 BLK_MQ_MAX_DEPTH);
3055 set->queue_depth = BLK_MQ_MAX_DEPTH;
3056 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003057
Jens Axboeb3c661b2018-10-30 10:36:06 -06003058 if (!set->nr_maps)
3059 set->nr_maps = 1;
3060 else if (set->nr_maps > HCTX_MAX_TYPES)
3061 return -EINVAL;
3062
Shaohua Li6637fad2014-11-30 16:00:58 -08003063 /*
3064 * If a crashdump is active, then we are potentially in a very
3065 * memory constrained environment. Limit us to 1 queue and
3066 * 64 tags to prevent using too much memory.
3067 */
3068 if (is_kdump_kernel()) {
3069 set->nr_hw_queues = 1;
Ming Lei59388702018-12-07 11:03:53 +08003070 set->nr_maps = 1;
Shaohua Li6637fad2014-11-30 16:00:58 -08003071 set->queue_depth = min(64U, set->queue_depth);
3072 }
Keith Busch868f2f02015-12-17 17:08:14 -07003073 /*
Jens Axboe392546a2018-10-29 13:25:27 -06003074 * There is no use for more h/w queues than cpus if we just have
3075 * a single map
Keith Busch868f2f02015-12-17 17:08:14 -07003076 */
Jens Axboe392546a2018-10-29 13:25:27 -06003077 if (set->nr_maps == 1 && set->nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003078 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08003079
Jens Axboe392546a2018-10-29 13:25:27 -06003080 set->tags = kcalloc_node(nr_hw_queues(set), sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003081 GFP_KERNEL, set->numa_node);
3082 if (!set->tags)
Jens Axboea5164402014-09-10 09:02:03 -06003083 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003084
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003085 ret = -ENOMEM;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003086 for (i = 0; i < set->nr_maps; i++) {
3087 set->map[i].mq_map = kcalloc_node(nr_cpu_ids,
Ming Lei07b35eb2018-12-17 18:42:45 +08003088 sizeof(set->map[i].mq_map[0]),
Jens Axboeb3c661b2018-10-30 10:36:06 -06003089 GFP_KERNEL, set->numa_node);
3090 if (!set->map[i].mq_map)
3091 goto out_free_mq_map;
Ming Lei59388702018-12-07 11:03:53 +08003092 set->map[i].nr_queues = is_kdump_kernel() ? 1 : set->nr_hw_queues;
Jens Axboeb3c661b2018-10-30 10:36:06 -06003093 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003094
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003095 ret = blk_mq_update_queue_map(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003096 if (ret)
3097 goto out_free_mq_map;
3098
3099 ret = blk_mq_alloc_rq_maps(set);
3100 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003101 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003102
Jens Axboe0d2602c2014-05-13 15:10:52 -06003103 mutex_init(&set->tag_list_lock);
3104 INIT_LIST_HEAD(&set->tag_list);
3105
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003106 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003107
3108out_free_mq_map:
Jens Axboeb3c661b2018-10-30 10:36:06 -06003109 for (i = 0; i < set->nr_maps; i++) {
3110 kfree(set->map[i].mq_map);
3111 set->map[i].mq_map = NULL;
3112 }
Robert Elliott5676e7b2014-09-02 11:38:44 -05003113 kfree(set->tags);
3114 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02003115 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003116}
3117EXPORT_SYMBOL(blk_mq_alloc_tag_set);
3118
3119void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
3120{
Jens Axboeb3c661b2018-10-30 10:36:06 -06003121 int i, j;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003122
Jens Axboe392546a2018-10-29 13:25:27 -06003123 for (i = 0; i < nr_hw_queues(set); i++)
Jens Axboecc71a6f2017-01-11 14:29:56 -07003124 blk_mq_free_map_and_requests(set, i);
Jens Axboe484b4062014-05-21 14:01:15 -06003125
Jens Axboeb3c661b2018-10-30 10:36:06 -06003126 for (j = 0; j < set->nr_maps; j++) {
3127 kfree(set->map[j].mq_map);
3128 set->map[j].mq_map = NULL;
3129 }
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02003130
Ming Lei981bd182014-04-24 00:07:34 +08003131 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05003132 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06003133}
3134EXPORT_SYMBOL(blk_mq_free_tag_set);
3135
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003136int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
3137{
3138 struct blk_mq_tag_set *set = q->tag_set;
3139 struct blk_mq_hw_ctx *hctx;
3140 int i, ret;
3141
Jens Axboebd166ef2017-01-17 06:03:22 -07003142 if (!set)
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003143 return -EINVAL;
3144
Aleksei Zakharove5fa8142019-02-08 19:14:05 +03003145 if (q->nr_requests == nr)
3146 return 0;
3147
Jens Axboe70f36b62017-01-19 10:59:07 -07003148 blk_mq_freeze_queue(q);
Ming Lei24f5a902018-01-06 16:27:38 +08003149 blk_mq_quiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003150
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003151 ret = 0;
3152 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07003153 if (!hctx->tags)
3154 continue;
Jens Axboebd166ef2017-01-17 06:03:22 -07003155 /*
3156 * If we're using an MQ scheduler, just update the scheduler
3157 * queue depth. This is similar to what the old code would do.
3158 */
Jens Axboe70f36b62017-01-19 10:59:07 -07003159 if (!hctx->sched_tags) {
weiping zhangc2e82a22017-09-22 23:36:28 +08003160 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
Jens Axboe70f36b62017-01-19 10:59:07 -07003161 false);
3162 } else {
3163 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
3164 nr, true);
3165 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003166 if (ret)
3167 break;
Jens Axboe77f1e0a2019-01-18 10:34:16 -07003168 if (q->elevator && q->elevator->type->ops.depth_updated)
3169 q->elevator->type->ops.depth_updated(hctx);
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003170 }
3171
3172 if (!ret)
3173 q->nr_requests = nr;
3174
Ming Lei24f5a902018-01-06 16:27:38 +08003175 blk_mq_unquiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003176 blk_mq_unfreeze_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07003177
Jens Axboee3a2b3f2014-05-20 11:49:02 -06003178 return ret;
3179}
3180
Jianchao Wangd48ece22018-08-21 15:15:03 +08003181/*
3182 * request_queue and elevator_type pair.
3183 * It is just used by __blk_mq_update_nr_hw_queues to cache
3184 * the elevator_type associated with a request_queue.
3185 */
3186struct blk_mq_qe_pair {
3187 struct list_head node;
3188 struct request_queue *q;
3189 struct elevator_type *type;
3190};
3191
3192/*
3193 * Cache the elevator_type in qe pair list and switch the
3194 * io scheduler to 'none'
3195 */
3196static bool blk_mq_elv_switch_none(struct list_head *head,
3197 struct request_queue *q)
3198{
3199 struct blk_mq_qe_pair *qe;
3200
3201 if (!q->elevator)
3202 return true;
3203
3204 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
3205 if (!qe)
3206 return false;
3207
3208 INIT_LIST_HEAD(&qe->node);
3209 qe->q = q;
3210 qe->type = q->elevator->type;
3211 list_add(&qe->node, head);
3212
3213 mutex_lock(&q->sysfs_lock);
3214 /*
3215 * After elevator_switch_mq, the previous elevator_queue will be
3216 * released by elevator_release. The reference of the io scheduler
3217 * module get by elevator_get will also be put. So we need to get
3218 * a reference of the io scheduler module here to prevent it to be
3219 * removed.
3220 */
3221 __module_get(qe->type->elevator_owner);
3222 elevator_switch_mq(q, NULL);
3223 mutex_unlock(&q->sysfs_lock);
3224
3225 return true;
3226}
3227
3228static void blk_mq_elv_switch_back(struct list_head *head,
3229 struct request_queue *q)
3230{
3231 struct blk_mq_qe_pair *qe;
3232 struct elevator_type *t = NULL;
3233
3234 list_for_each_entry(qe, head, node)
3235 if (qe->q == q) {
3236 t = qe->type;
3237 break;
3238 }
3239
3240 if (!t)
3241 return;
3242
3243 list_del(&qe->node);
3244 kfree(qe);
3245
3246 mutex_lock(&q->sysfs_lock);
3247 elevator_switch_mq(q, t);
3248 mutex_unlock(&q->sysfs_lock);
3249}
3250
Keith Busche4dc2b32017-05-30 14:39:11 -04003251static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
3252 int nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003253{
3254 struct request_queue *q;
Jianchao Wangd48ece22018-08-21 15:15:03 +08003255 LIST_HEAD(head);
Jianchao Wange01ad462018-10-12 18:07:28 +08003256 int prev_nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07003257
Bart Van Assche705cda92017-04-07 11:16:49 -07003258 lockdep_assert_held(&set->tag_list_lock);
3259
Jens Axboe392546a2018-10-29 13:25:27 -06003260 if (set->nr_maps == 1 && nr_hw_queues > nr_cpu_ids)
Keith Busch868f2f02015-12-17 17:08:14 -07003261 nr_hw_queues = nr_cpu_ids;
3262 if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
3263 return;
3264
3265 list_for_each_entry(q, &set->tag_list, tag_set_list)
3266 blk_mq_freeze_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +08003267 /*
Jianchao Wangf5bbbbe2018-08-21 15:15:04 +08003268 * Sync with blk_mq_queue_tag_busy_iter.
3269 */
3270 synchronize_rcu();
3271 /*
Jianchao Wangd48ece22018-08-21 15:15:03 +08003272 * Switch IO scheduler to 'none', cleaning up the data associated
3273 * with the previous scheduler. We will switch back once we are done
3274 * updating the new sw to hw queue mappings.
3275 */
3276 list_for_each_entry(q, &set->tag_list, tag_set_list)
3277 if (!blk_mq_elv_switch_none(&head, q))
3278 goto switch_back;
Keith Busch868f2f02015-12-17 17:08:14 -07003279
Jianchao Wang477e19d2018-10-12 18:07:25 +08003280 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3281 blk_mq_debugfs_unregister_hctxs(q);
3282 blk_mq_sysfs_unregister(q);
3283 }
3284
Jianchao Wange01ad462018-10-12 18:07:28 +08003285 prev_nr_hw_queues = set->nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07003286 set->nr_hw_queues = nr_hw_queues;
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003287 blk_mq_update_queue_map(set);
Jianchao Wange01ad462018-10-12 18:07:28 +08003288fallback:
Keith Busch868f2f02015-12-17 17:08:14 -07003289 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3290 blk_mq_realloc_hw_ctxs(set, q);
Jianchao Wange01ad462018-10-12 18:07:28 +08003291 if (q->nr_hw_queues != set->nr_hw_queues) {
3292 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
3293 nr_hw_queues, prev_nr_hw_queues);
3294 set->nr_hw_queues = prev_nr_hw_queues;
Dongli Zhang7d76f852019-02-27 21:35:01 +08003295 blk_mq_map_queues(&set->map[HCTX_TYPE_DEFAULT]);
Jianchao Wange01ad462018-10-12 18:07:28 +08003296 goto fallback;
3297 }
Jianchao Wang477e19d2018-10-12 18:07:25 +08003298 blk_mq_map_swqueue(q);
3299 }
3300
3301 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3302 blk_mq_sysfs_register(q);
3303 blk_mq_debugfs_register_hctxs(q);
Keith Busch868f2f02015-12-17 17:08:14 -07003304 }
3305
Jianchao Wangd48ece22018-08-21 15:15:03 +08003306switch_back:
3307 list_for_each_entry(q, &set->tag_list, tag_set_list)
3308 blk_mq_elv_switch_back(&head, q);
3309
Keith Busch868f2f02015-12-17 17:08:14 -07003310 list_for_each_entry(q, &set->tag_list, tag_set_list)
3311 blk_mq_unfreeze_queue(q);
3312}
Keith Busche4dc2b32017-05-30 14:39:11 -04003313
3314void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
3315{
3316 mutex_lock(&set->tag_list_lock);
3317 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
3318 mutex_unlock(&set->tag_list_lock);
3319}
Keith Busch868f2f02015-12-17 17:08:14 -07003320EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
3321
Omar Sandoval34dbad52017-03-21 08:56:08 -07003322/* Enable polling stats and return whether they were already enabled. */
3323static bool blk_poll_stats_enable(struct request_queue *q)
3324{
3325 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
Bart Van Assche7dfdbc72018-03-07 17:10:05 -08003326 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
Omar Sandoval34dbad52017-03-21 08:56:08 -07003327 return true;
3328 blk_stat_add_callback(q, q->poll_cb);
3329 return false;
3330}
3331
3332static void blk_mq_poll_stats_start(struct request_queue *q)
3333{
3334 /*
3335 * We don't arm the callback if polling stats are not enabled or the
3336 * callback is already active.
3337 */
3338 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
3339 blk_stat_is_active(q->poll_cb))
3340 return;
3341
3342 blk_stat_activate_msecs(q->poll_cb, 100);
3343}
3344
3345static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
3346{
3347 struct request_queue *q = cb->data;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003348 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -07003349
Stephen Bates720b8cc2017-04-07 06:24:03 -06003350 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
3351 if (cb->stat[bucket].nr_samples)
3352 q->poll_stat[bucket] = cb->stat[bucket];
3353 }
Omar Sandoval34dbad52017-03-21 08:56:08 -07003354}
3355
Jens Axboe64f1c212016-11-14 13:03:03 -07003356static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
3357 struct blk_mq_hw_ctx *hctx,
3358 struct request *rq)
3359{
Jens Axboe64f1c212016-11-14 13:03:03 -07003360 unsigned long ret = 0;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003361 int bucket;
Jens Axboe64f1c212016-11-14 13:03:03 -07003362
3363 /*
3364 * If stats collection isn't on, don't sleep but turn it on for
3365 * future users
3366 */
Omar Sandoval34dbad52017-03-21 08:56:08 -07003367 if (!blk_poll_stats_enable(q))
Jens Axboe64f1c212016-11-14 13:03:03 -07003368 return 0;
3369
3370 /*
Jens Axboe64f1c212016-11-14 13:03:03 -07003371 * As an optimistic guess, use half of the mean service time
3372 * for this type of request. We can (and should) make this smarter.
3373 * For instance, if the completion latencies are tight, we can
3374 * get closer than just half the mean. This is especially
3375 * important on devices where the completion latencies are longer
Stephen Bates720b8cc2017-04-07 06:24:03 -06003376 * than ~10 usec. We do use the stats for the relevant IO size
3377 * if available which does lead to better estimates.
Jens Axboe64f1c212016-11-14 13:03:03 -07003378 */
Stephen Bates720b8cc2017-04-07 06:24:03 -06003379 bucket = blk_mq_poll_stats_bkt(rq);
3380 if (bucket < 0)
3381 return ret;
3382
3383 if (q->poll_stat[bucket].nr_samples)
3384 ret = (q->poll_stat[bucket].mean + 1) / 2;
Jens Axboe64f1c212016-11-14 13:03:03 -07003385
3386 return ret;
3387}
3388
Jens Axboe06426ad2016-11-14 13:01:59 -07003389static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
Jens Axboe64f1c212016-11-14 13:03:03 -07003390 struct blk_mq_hw_ctx *hctx,
Jens Axboe06426ad2016-11-14 13:01:59 -07003391 struct request *rq)
3392{
3393 struct hrtimer_sleeper hs;
3394 enum hrtimer_mode mode;
Jens Axboe64f1c212016-11-14 13:03:03 -07003395 unsigned int nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003396 ktime_t kt;
3397
Jens Axboe76a86f92018-01-10 11:30:56 -07003398 if (rq->rq_flags & RQF_MQ_POLL_SLEPT)
Jens Axboe64f1c212016-11-14 13:03:03 -07003399 return false;
3400
3401 /*
Jens Axboe1052b8a2018-11-26 08:21:49 -07003402 * If we get here, hybrid polling is enabled. Hence poll_nsec can be:
Jens Axboe64f1c212016-11-14 13:03:03 -07003403 *
Jens Axboe64f1c212016-11-14 13:03:03 -07003404 * 0: use half of prev avg
3405 * >0: use this specific value
3406 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07003407 if (q->poll_nsec > 0)
Jens Axboe64f1c212016-11-14 13:03:03 -07003408 nsecs = q->poll_nsec;
3409 else
3410 nsecs = blk_mq_poll_nsecs(q, hctx, rq);
3411
3412 if (!nsecs)
Jens Axboe06426ad2016-11-14 13:01:59 -07003413 return false;
3414
Jens Axboe76a86f92018-01-10 11:30:56 -07003415 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
Jens Axboe06426ad2016-11-14 13:01:59 -07003416
3417 /*
3418 * This will be replaced with the stats tracking code, using
3419 * 'avg_completion_time / 2' as the pre-sleep target.
3420 */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01003421 kt = nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003422
3423 mode = HRTIMER_MODE_REL;
3424 hrtimer_init_on_stack(&hs.timer, CLOCK_MONOTONIC, mode);
3425 hrtimer_set_expires(&hs.timer, kt);
3426
3427 hrtimer_init_sleeper(&hs, current);
3428 do {
Tejun Heo5a61c362018-01-09 08:29:52 -08003429 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
Jens Axboe06426ad2016-11-14 13:01:59 -07003430 break;
3431 set_current_state(TASK_UNINTERRUPTIBLE);
3432 hrtimer_start_expires(&hs.timer, mode);
3433 if (hs.task)
3434 io_schedule();
3435 hrtimer_cancel(&hs.timer);
3436 mode = HRTIMER_MODE_ABS;
3437 } while (hs.task && !signal_pending(current));
3438
3439 __set_current_state(TASK_RUNNING);
3440 destroy_hrtimer_on_stack(&hs.timer);
3441 return true;
3442}
3443
Jens Axboe1052b8a2018-11-26 08:21:49 -07003444static bool blk_mq_poll_hybrid(struct request_queue *q,
3445 struct blk_mq_hw_ctx *hctx, blk_qc_t cookie)
Jens Axboebbd7bb72016-11-04 09:34:34 -06003446{
Jens Axboe1052b8a2018-11-26 08:21:49 -07003447 struct request *rq;
3448
Yufen Yu29ece8b2019-03-18 22:44:41 +08003449 if (q->poll_nsec == BLK_MQ_POLL_CLASSIC)
Jens Axboe1052b8a2018-11-26 08:21:49 -07003450 return false;
3451
3452 if (!blk_qc_t_is_internal(cookie))
3453 rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
3454 else {
3455 rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
3456 /*
3457 * With scheduling, if the request has completed, we'll
3458 * get a NULL return here, as we clear the sched tag when
3459 * that happens. The request still remains valid, like always,
3460 * so we should be safe with just the NULL check.
3461 */
3462 if (!rq)
3463 return false;
3464 }
3465
3466 return blk_mq_poll_hybrid_sleep(q, hctx, rq);
3467}
3468
Christoph Hellwig529262d2018-12-02 17:46:26 +01003469/**
3470 * blk_poll - poll for IO completions
3471 * @q: the queue
3472 * @cookie: cookie passed back at IO submission time
3473 * @spin: whether to spin for completions
3474 *
3475 * Description:
3476 * Poll for completions on the passed in queue. Returns number of
3477 * completed entries found. If @spin is true, then blk_poll will continue
3478 * looping until at least one completion is found, unless the task is
3479 * otherwise marked running (or we need to reschedule).
3480 */
3481int blk_poll(struct request_queue *q, blk_qc_t cookie, bool spin)
Jens Axboe1052b8a2018-11-26 08:21:49 -07003482{
3483 struct blk_mq_hw_ctx *hctx;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003484 long state;
3485
Christoph Hellwig529262d2018-12-02 17:46:26 +01003486 if (!blk_qc_t_valid(cookie) ||
3487 !test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
Jens Axboe1052b8a2018-11-26 08:21:49 -07003488 return 0;
3489
Christoph Hellwig529262d2018-12-02 17:46:26 +01003490 if (current->plug)
3491 blk_flush_plug_list(current->plug, false);
3492
Jens Axboe1052b8a2018-11-26 08:21:49 -07003493 hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
3494
Jens Axboe06426ad2016-11-14 13:01:59 -07003495 /*
3496 * If we sleep, have the caller restart the poll loop to reset
3497 * the state. Like for the other success return cases, the
3498 * caller is responsible for checking if the IO completed. If
3499 * the IO isn't complete, we'll get called again and will go
3500 * straight to the busy poll loop.
3501 */
Jens Axboe1052b8a2018-11-26 08:21:49 -07003502 if (blk_mq_poll_hybrid(q, hctx, cookie))
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003503 return 1;
Jens Axboe06426ad2016-11-14 13:01:59 -07003504
Jens Axboebbd7bb72016-11-04 09:34:34 -06003505 hctx->poll_considered++;
3506
3507 state = current->state;
Jens Axboeaa61bec2018-11-13 21:32:10 -07003508 do {
Jens Axboebbd7bb72016-11-04 09:34:34 -06003509 int ret;
3510
3511 hctx->poll_invoked++;
3512
Jens Axboe97431392018-11-16 09:48:21 -07003513 ret = q->mq_ops->poll(hctx);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003514 if (ret > 0) {
3515 hctx->poll_success++;
Jens Axboe849a3702018-11-16 08:37:34 -07003516 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003517 return ret;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003518 }
3519
3520 if (signal_pending_state(state, current))
Jens Axboe849a3702018-11-16 08:37:34 -07003521 __set_current_state(TASK_RUNNING);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003522
3523 if (current->state == TASK_RUNNING)
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003524 return 1;
Jens Axboe0a1b8b82018-11-26 08:24:43 -07003525 if (ret < 0 || !spin)
Jens Axboebbd7bb72016-11-04 09:34:34 -06003526 break;
3527 cpu_relax();
Jens Axboeaa61bec2018-11-13 21:32:10 -07003528 } while (!need_resched());
Jens Axboebbd7bb72016-11-04 09:34:34 -06003529
Nitesh Shetty67b41102018-02-13 21:18:12 +05303530 __set_current_state(TASK_RUNNING);
Jens Axboe85f4d4b2018-11-06 13:30:55 -07003531 return 0;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003532}
Christoph Hellwig529262d2018-12-02 17:46:26 +01003533EXPORT_SYMBOL_GPL(blk_poll);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003534
Jens Axboe9cf2bab2018-10-31 17:01:22 -06003535unsigned int blk_mq_rq_cpu(struct request *rq)
3536{
3537 return rq->mq_ctx->cpu;
3538}
3539EXPORT_SYMBOL(blk_mq_rq_cpu);
3540
Jens Axboe320ae512013-10-24 09:20:05 +01003541static int __init blk_mq_init(void)
3542{
Thomas Gleixner9467f852016-09-22 08:05:17 -06003543 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
3544 blk_mq_hctx_notify_dead);
Jens Axboe320ae512013-10-24 09:20:05 +01003545 return 0;
3546}
3547subsys_initcall(blk_mq_init);