blob: 5e7982918c54c9c7cf7304fcd434559de9df0309 [file] [log] [blame]
Jens Axboe75bb4622014-05-28 10:15:41 -06001/*
2 * Block multiqueue core code
3 *
4 * Copyright (C) 2013-2014 Jens Axboe
5 * Copyright (C) 2013-2014 Christoph Hellwig
6 */
Jens Axboe320ae512013-10-24 09:20:05 +01007#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/backing-dev.h>
10#include <linux/bio.h>
11#include <linux/blkdev.h>
Catalin Marinasf75782e2015-09-14 18:16:02 +010012#include <linux/kmemleak.h>
Jens Axboe320ae512013-10-24 09:20:05 +010013#include <linux/mm.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/workqueue.h>
17#include <linux/smp.h>
18#include <linux/llist.h>
19#include <linux/list_sort.h>
20#include <linux/cpu.h>
21#include <linux/cache.h>
22#include <linux/sched/sysctl.h>
Ingo Molnar105ab3d2017-02-01 16:36:40 +010023#include <linux/sched/topology.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010024#include <linux/sched/signal.h>
Jens Axboe320ae512013-10-24 09:20:05 +010025#include <linux/delay.h>
Jens Axboeaedcd722014-09-17 08:27:03 -060026#include <linux/crash_dump.h>
Jens Axboe88c7b2b2016-08-25 08:07:30 -060027#include <linux/prefetch.h>
Jens Axboe320ae512013-10-24 09:20:05 +010028
29#include <trace/events/block.h>
30
31#include <linux/blk-mq.h>
32#include "blk.h"
33#include "blk-mq.h"
Omar Sandoval9c1051a2017-05-04 08:17:21 -060034#include "blk-mq-debugfs.h"
Jens Axboe320ae512013-10-24 09:20:05 +010035#include "blk-mq-tag.h"
Bart Van Assche986d4132018-09-26 14:01:10 -070036#include "blk-pm.h"
Jens Axboecf43e6b2016-11-07 21:32:37 -070037#include "blk-stat.h"
Jens Axboebd166ef2017-01-17 06:03:22 -070038#include "blk-mq-sched.h"
Josef Bacikc1c80382018-07-03 11:14:59 -040039#include "blk-rq-qos.h"
Jens Axboe320ae512013-10-24 09:20:05 +010040
Christoph Hellwigea435e12017-11-02 21:29:54 +030041static bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie);
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/*
63 * Check if any of the ctx's have pending work in this hardware queue
64 */
Jens Axboe79f720a2017-11-10 09:13:21 -070065static bool blk_mq_hctx_has_pending(struct blk_mq_hw_ctx *hctx)
Jens Axboe320ae512013-10-24 09:20:05 +010066{
Jens Axboe79f720a2017-11-10 09:13:21 -070067 return !list_empty_careful(&hctx->dispatch) ||
68 sbitmap_any_bit_set(&hctx->ctx_map) ||
Jens Axboebd166ef2017-01-17 06:03:22 -070069 blk_mq_sched_has_work(hctx);
Jens Axboe320ae512013-10-24 09:20:05 +010070}
71
72/*
73 * Mark this ctx as having pending work in this hardware queue
74 */
75static void blk_mq_hctx_mark_pending(struct blk_mq_hw_ctx *hctx,
76 struct blk_mq_ctx *ctx)
77{
Omar Sandoval88459642016-09-17 08:38:44 -060078 if (!sbitmap_test_bit(&hctx->ctx_map, ctx->index_hw))
79 sbitmap_set_bit(&hctx->ctx_map, ctx->index_hw);
Jens Axboe1429d7c2014-05-19 09:23:55 -060080}
81
82static void blk_mq_hctx_clear_pending(struct blk_mq_hw_ctx *hctx,
83 struct blk_mq_ctx *ctx)
84{
Omar Sandoval88459642016-09-17 08:38:44 -060085 sbitmap_clear_bit(&hctx->ctx_map, ctx->index_hw);
Jens Axboe320ae512013-10-24 09:20:05 +010086}
87
Jens Axboef299b7c2017-08-08 17:51:45 -060088struct mq_inflight {
89 struct hd_struct *part;
90 unsigned int *inflight;
91};
92
93static void blk_mq_check_inflight(struct blk_mq_hw_ctx *hctx,
94 struct request *rq, void *priv,
95 bool reserved)
96{
97 struct mq_inflight *mi = priv;
98
Omar Sandoval61318372018-04-26 00:21:58 -070099 /*
100 * index[0] counts the specific partition that was asked for. index[1]
101 * counts the ones that are active on the whole device, so increment
102 * that if mi->part is indeed a partition, and not a whole device.
103 */
104 if (rq->part == mi->part)
105 mi->inflight[0]++;
106 if (mi->part->partno)
107 mi->inflight[1]++;
Jens Axboef299b7c2017-08-08 17:51:45 -0600108}
109
110void blk_mq_in_flight(struct request_queue *q, struct hd_struct *part,
111 unsigned int inflight[2])
112{
113 struct mq_inflight mi = { .part = part, .inflight = inflight, };
114
Jens Axboeb8d62b32017-08-08 17:53:33 -0600115 inflight[0] = inflight[1] = 0;
Jens Axboef299b7c2017-08-08 17:51:45 -0600116 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight, &mi);
117}
118
Omar Sandovalbf0ddab2018-04-26 00:21:59 -0700119static void blk_mq_check_inflight_rw(struct blk_mq_hw_ctx *hctx,
120 struct request *rq, void *priv,
121 bool reserved)
122{
123 struct mq_inflight *mi = priv;
124
125 if (rq->part == mi->part)
126 mi->inflight[rq_data_dir(rq)]++;
127}
128
129void blk_mq_in_flight_rw(struct request_queue *q, struct hd_struct *part,
130 unsigned int inflight[2])
131{
132 struct mq_inflight mi = { .part = part, .inflight = inflight, };
133
134 inflight[0] = inflight[1] = 0;
135 blk_mq_queue_tag_busy_iter(q, blk_mq_check_inflight_rw, &mi);
136}
137
Ming Lei1671d522017-03-27 20:06:57 +0800138void blk_freeze_queue_start(struct request_queue *q)
Ming Lei43a5e4e2013-12-26 21:31:35 +0800139{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200140 int freeze_depth;
Tejun Heocddd5d12014-08-16 08:02:24 -0400141
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200142 freeze_depth = atomic_inc_return(&q->mq_freeze_depth);
143 if (freeze_depth == 1) {
Dan Williams3ef28e82015-10-21 13:20:12 -0400144 percpu_ref_kill(&q->q_usage_counter);
Ming Lei055f6e12017-11-09 10:49:53 -0800145 if (q->mq_ops)
146 blk_mq_run_hw_queues(q, false);
Tejun Heocddd5d12014-08-16 08:02:24 -0400147 }
Tejun Heof3af0202014-11-04 13:52:27 -0500148}
Ming Lei1671d522017-03-27 20:06:57 +0800149EXPORT_SYMBOL_GPL(blk_freeze_queue_start);
Tejun Heof3af0202014-11-04 13:52:27 -0500150
Keith Busch6bae363e2017-03-01 14:22:10 -0500151void blk_mq_freeze_queue_wait(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500152{
Dan Williams3ef28e82015-10-21 13:20:12 -0400153 wait_event(q->mq_freeze_wq, percpu_ref_is_zero(&q->q_usage_counter));
Ming Lei43a5e4e2013-12-26 21:31:35 +0800154}
Keith Busch6bae363e2017-03-01 14:22:10 -0500155EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait);
Ming Lei43a5e4e2013-12-26 21:31:35 +0800156
Keith Buschf91328c2017-03-01 14:22:11 -0500157int blk_mq_freeze_queue_wait_timeout(struct request_queue *q,
158 unsigned long timeout)
159{
160 return wait_event_timeout(q->mq_freeze_wq,
161 percpu_ref_is_zero(&q->q_usage_counter),
162 timeout);
163}
164EXPORT_SYMBOL_GPL(blk_mq_freeze_queue_wait_timeout);
Jens Axboe320ae512013-10-24 09:20:05 +0100165
Tejun Heof3af0202014-11-04 13:52:27 -0500166/*
167 * Guarantee no request is in use, so we can change any data structure of
168 * the queue afterward.
169 */
Dan Williams3ef28e82015-10-21 13:20:12 -0400170void blk_freeze_queue(struct request_queue *q)
Tejun Heof3af0202014-11-04 13:52:27 -0500171{
Dan Williams3ef28e82015-10-21 13:20:12 -0400172 /*
173 * In the !blk_mq case we are only calling this to kill the
174 * q_usage_counter, otherwise this increases the freeze depth
175 * and waits for it to return to zero. For this reason there is
176 * no blk_unfreeze_queue(), and blk_freeze_queue() is not
177 * exported to drivers as the only user for unfreeze is blk_mq.
178 */
Ming Lei1671d522017-03-27 20:06:57 +0800179 blk_freeze_queue_start(q);
Tejun Heof3af0202014-11-04 13:52:27 -0500180 blk_mq_freeze_queue_wait(q);
181}
Dan Williams3ef28e82015-10-21 13:20:12 -0400182
183void blk_mq_freeze_queue(struct request_queue *q)
184{
185 /*
186 * ...just an alias to keep freeze and unfreeze actions balanced
187 * in the blk_mq_* namespace
188 */
189 blk_freeze_queue(q);
190}
Jens Axboec761d962015-01-02 15:05:12 -0700191EXPORT_SYMBOL_GPL(blk_mq_freeze_queue);
Tejun Heof3af0202014-11-04 13:52:27 -0500192
Keith Buschb4c6a022014-12-19 17:54:14 -0700193void blk_mq_unfreeze_queue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +0100194{
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200195 int freeze_depth;
Jens Axboe320ae512013-10-24 09:20:05 +0100196
Christoph Hellwig4ecd4fe2015-05-07 09:38:13 +0200197 freeze_depth = atomic_dec_return(&q->mq_freeze_depth);
198 WARN_ON_ONCE(freeze_depth < 0);
199 if (!freeze_depth) {
Bart Van Asschebdd63162018-09-26 14:01:08 -0700200 percpu_ref_resurrect(&q->q_usage_counter);
Jens Axboe320ae512013-10-24 09:20:05 +0100201 wake_up_all(&q->mq_freeze_wq);
Tejun Heoadd703f2014-07-01 10:34:38 -0600202 }
Jens Axboe320ae512013-10-24 09:20:05 +0100203}
Keith Buschb4c6a022014-12-19 17:54:14 -0700204EXPORT_SYMBOL_GPL(blk_mq_unfreeze_queue);
Jens Axboe320ae512013-10-24 09:20:05 +0100205
Bart Van Assche852ec802017-06-21 10:55:47 -0700206/*
207 * FIXME: replace the scsi_internal_device_*block_nowait() calls in the
208 * mpt3sas driver such that this function can be removed.
209 */
210void blk_mq_quiesce_queue_nowait(struct request_queue *q)
211{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800212 blk_queue_flag_set(QUEUE_FLAG_QUIESCED, q);
Bart Van Assche852ec802017-06-21 10:55:47 -0700213}
214EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue_nowait);
215
Bart Van Assche6a83e742016-11-02 10:09:51 -0600216/**
Ming Lei69e07c42017-06-06 23:22:07 +0800217 * blk_mq_quiesce_queue() - wait until all ongoing dispatches have finished
Bart Van Assche6a83e742016-11-02 10:09:51 -0600218 * @q: request queue.
219 *
220 * Note: this function does not prevent that the struct request end_io()
Ming Lei69e07c42017-06-06 23:22:07 +0800221 * callback function is invoked. Once this function is returned, we make
222 * sure no dispatch can happen until the queue is unquiesced via
223 * blk_mq_unquiesce_queue().
Bart Van Assche6a83e742016-11-02 10:09:51 -0600224 */
225void blk_mq_quiesce_queue(struct request_queue *q)
226{
227 struct blk_mq_hw_ctx *hctx;
228 unsigned int i;
229 bool rcu = false;
230
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800231 blk_mq_quiesce_queue_nowait(q);
Ming Leif4560ff2017-06-18 14:24:27 -0600232
Bart Van Assche6a83e742016-11-02 10:09:51 -0600233 queue_for_each_hw_ctx(q, hctx, i) {
234 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -0800235 synchronize_srcu(hctx->srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -0600236 else
237 rcu = true;
238 }
239 if (rcu)
240 synchronize_rcu();
241}
242EXPORT_SYMBOL_GPL(blk_mq_quiesce_queue);
243
Ming Leie4e73912017-06-06 23:22:03 +0800244/*
245 * blk_mq_unquiesce_queue() - counterpart of blk_mq_quiesce_queue()
246 * @q: request queue.
247 *
248 * This function recovers queue into the state before quiescing
249 * which is done by blk_mq_quiesce_queue.
250 */
251void blk_mq_unquiesce_queue(struct request_queue *q)
252{
Bart Van Assche8814ce82018-03-07 17:10:04 -0800253 blk_queue_flag_clear(QUEUE_FLAG_QUIESCED, q);
Ming Leif4560ff2017-06-18 14:24:27 -0600254
Ming Lei1d9e9bc2017-06-06 23:22:08 +0800255 /* dispatch requests which are inserted during quiescing */
256 blk_mq_run_hw_queues(q, true);
Ming Leie4e73912017-06-06 23:22:03 +0800257}
258EXPORT_SYMBOL_GPL(blk_mq_unquiesce_queue);
259
Jens Axboeaed3ea92014-12-22 14:04:42 -0700260void blk_mq_wake_waiters(struct request_queue *q)
261{
262 struct blk_mq_hw_ctx *hctx;
263 unsigned int i;
264
265 queue_for_each_hw_ctx(q, hctx, i)
266 if (blk_mq_hw_queue_mapped(hctx))
267 blk_mq_tag_wakeup_all(hctx->tags, true);
268}
269
Jens Axboe320ae512013-10-24 09:20:05 +0100270bool blk_mq_can_queue(struct blk_mq_hw_ctx *hctx)
271{
272 return blk_mq_has_free_tags(hctx->tags);
273}
274EXPORT_SYMBOL(blk_mq_can_queue);
275
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200276static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
277 unsigned int tag, unsigned int op)
Jens Axboe320ae512013-10-24 09:20:05 +0100278{
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200279 struct blk_mq_tags *tags = blk_mq_tags_from_data(data);
280 struct request *rq = tags->static_rqs[tag];
Jens Axboebf9ae8c2018-01-14 10:40:45 -0700281 req_flags_t rq_flags = 0;
Bart Van Asschec3a148d2017-06-20 11:15:43 -0700282
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200283 if (data->flags & BLK_MQ_REQ_INTERNAL) {
284 rq->tag = -1;
285 rq->internal_tag = tag;
286 } else {
Jianchao Wangd263ed92018-08-09 08:34:17 -0600287 if (data->hctx->flags & BLK_MQ_F_TAG_SHARED) {
Jens Axboebf9ae8c2018-01-14 10:40:45 -0700288 rq_flags = RQF_MQ_INFLIGHT;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200289 atomic_inc(&data->hctx->nr_active);
290 }
291 rq->tag = tag;
292 rq->internal_tag = -1;
293 data->hctx->tags->rqs[rq->tag] = rq;
294 }
295
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200296 /* csd/requeue_work/fifo_time is initialized before use */
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200297 rq->q = data->q;
298 rq->mq_ctx = data->ctx;
Jens Axboebf9ae8c2018-01-14 10:40:45 -0700299 rq->rq_flags = rq_flags;
Jens Axboe7c3fb702018-01-10 11:46:39 -0700300 rq->cpu = -1;
Christoph Hellwigef295ec2016-10-28 08:48:16 -0600301 rq->cmd_flags = op;
Bart Van Assche1b6d65a2017-11-09 10:49:55 -0800302 if (data->flags & BLK_MQ_REQ_PREEMPT)
303 rq->rq_flags |= RQF_PREEMPT;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200304 if (blk_queue_io_stat(data->q))
Christoph Hellwige8064022016-10-20 15:12:13 +0200305 rq->rq_flags |= RQF_IO_STAT;
Jens Axboe7c3fb702018-01-10 11:46:39 -0700306 INIT_LIST_HEAD(&rq->queuelist);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200307 INIT_HLIST_NODE(&rq->hash);
308 RB_CLEAR_NODE(&rq->rb_node);
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200309 rq->rq_disk = NULL;
310 rq->part = NULL;
Omar Sandoval522a7772018-05-09 02:08:53 -0700311 rq->start_time_ns = ktime_get_ns();
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700312 rq->io_start_time_ns = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200313 rq->nr_phys_segments = 0;
314#if defined(CONFIG_BLK_DEV_INTEGRITY)
315 rq->nr_integrity_segments = 0;
316#endif
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200317 rq->special = NULL;
318 /* tag was already set */
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200319 rq->extra_len = 0;
Jens Axboee14575b32018-01-10 11:34:25 -0700320 rq->__deadline = 0;
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200321
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200322 INIT_LIST_HEAD(&rq->timeout_list);
Jens Axboef6be4fb2014-06-06 11:03:48 -0600323 rq->timeout = 0;
324
Christoph Hellwigaf76e552014-05-06 12:12:45 +0200325 rq->end_io = NULL;
326 rq->end_io_data = NULL;
327 rq->next_rq = NULL;
328
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200329 data->ctx->rq_dispatched[op_is_sync(op)]++;
Keith Busch12f5b932018-05-29 15:52:28 +0200330 refcount_set(&rq->ref, 1);
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200331 return rq;
Jens Axboe320ae512013-10-24 09:20:05 +0100332}
333
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200334static struct request *blk_mq_get_request(struct request_queue *q,
335 struct bio *bio, unsigned int op,
336 struct blk_mq_alloc_data *data)
337{
338 struct elevator_queue *e = q->elevator;
339 struct request *rq;
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200340 unsigned int tag;
Bart Van Assche21e768b2017-10-16 16:32:26 -0700341 bool put_ctx_on_error = false;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200342
343 blk_queue_enter_live(q);
344 data->q = q;
Bart Van Assche21e768b2017-10-16 16:32:26 -0700345 if (likely(!data->ctx)) {
346 data->ctx = blk_mq_get_ctx(q);
347 put_ctx_on_error = true;
348 }
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200349 if (likely(!data->hctx))
350 data->hctx = blk_mq_map_queue(q, data->ctx->cpu);
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -0500351 if (op & REQ_NOWAIT)
352 data->flags |= BLK_MQ_REQ_NOWAIT;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200353
354 if (e) {
355 data->flags |= BLK_MQ_REQ_INTERNAL;
356
357 /*
358 * Flush requests are special and go directly to the
Jens Axboe17a51192018-05-09 13:28:50 -0600359 * dispatch list. Don't include reserved tags in the
360 * limiting, as it isn't useful.
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200361 */
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600362 if (!op_is_flush(op) && e->type->ops.limit_depth &&
Jens Axboe17a51192018-05-09 13:28:50 -0600363 !(data->flags & BLK_MQ_REQ_RESERVED))
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600364 e->type->ops.limit_depth(op, data);
Jianchao Wangd263ed92018-08-09 08:34:17 -0600365 } else {
366 blk_mq_tag_busy(data->hctx);
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200367 }
368
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200369 tag = blk_mq_get_tag(data);
370 if (tag == BLK_MQ_TAG_FAIL) {
Bart Van Assche21e768b2017-10-16 16:32:26 -0700371 if (put_ctx_on_error) {
372 blk_mq_put_ctx(data->ctx);
Ming Lei1ad43c02017-08-02 08:01:45 +0800373 data->ctx = NULL;
374 }
Christoph Hellwig037cebb2017-06-16 18:15:23 +0200375 blk_queue_exit(q);
376 return NULL;
377 }
378
Christoph Hellwige4cdf1a2017-06-16 18:15:27 +0200379 rq = blk_mq_rq_ctx_init(data, tag, op);
Christoph Hellwig037cebb2017-06-16 18:15:23 +0200380 if (!op_is_flush(op)) {
381 rq->elv.icq = NULL;
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600382 if (e && e->type->ops.prepare_request) {
Christoph Hellwig44e8c2b2017-06-16 18:15:25 +0200383 if (e->type->icq_cache && rq_ioc(bio))
384 blk_mq_sched_assign_ioc(rq, bio);
385
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600386 e->type->ops.prepare_request(rq, bio);
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200387 rq->rq_flags |= RQF_ELVPRIV;
Christoph Hellwig44e8c2b2017-06-16 18:15:25 +0200388 }
Christoph Hellwig037cebb2017-06-16 18:15:23 +0200389 }
390 data->hctx->queued++;
391 return rq;
Christoph Hellwigd2c0d382017-06-16 18:15:19 +0200392}
393
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700394struct request *blk_mq_alloc_request(struct request_queue *q, unsigned int op,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800395 blk_mq_req_flags_t flags)
Jens Axboe320ae512013-10-24 09:20:05 +0100396{
Jens Axboe5a797e02017-01-26 12:22:11 -0700397 struct blk_mq_alloc_data alloc_data = { .flags = flags };
Jens Axboebd166ef2017-01-17 06:03:22 -0700398 struct request *rq;
Joe Lawrencea492f072014-08-28 08:15:21 -0600399 int ret;
Jens Axboe320ae512013-10-24 09:20:05 +0100400
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800401 ret = blk_queue_enter(q, flags);
Joe Lawrencea492f072014-08-28 08:15:21 -0600402 if (ret)
403 return ERR_PTR(ret);
Jens Axboe320ae512013-10-24 09:20:05 +0100404
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700405 rq = blk_mq_get_request(q, NULL, op, &alloc_data);
Keith Busch3280d662017-08-14 16:40:11 -0400406 blk_queue_exit(q);
Jens Axboe841bac22016-09-21 10:08:43 -0600407
Jens Axboebd166ef2017-01-17 06:03:22 -0700408 if (!rq)
Joe Lawrencea492f072014-08-28 08:15:21 -0600409 return ERR_PTR(-EWOULDBLOCK);
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200410
Ming Lei1ad43c02017-08-02 08:01:45 +0800411 blk_mq_put_ctx(alloc_data.ctx);
Ming Lei1ad43c02017-08-02 08:01:45 +0800412
Christoph Hellwig0c4de0f2016-07-19 11:31:50 +0200413 rq->__data_len = 0;
414 rq->__sector = (sector_t) -1;
415 rq->bio = rq->biotail = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +0100416 return rq;
417}
Jens Axboe4bb659b2014-05-09 09:36:49 -0600418EXPORT_SYMBOL(blk_mq_alloc_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100419
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700420struct request *blk_mq_alloc_request_hctx(struct request_queue *q,
Bart Van Assche9a95e4e2017-11-09 10:49:59 -0800421 unsigned int op, blk_mq_req_flags_t flags, unsigned int hctx_idx)
Ming Lin1f5bd332016-06-13 16:45:21 +0200422{
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800423 struct blk_mq_alloc_data alloc_data = { .flags = flags };
Ming Lin1f5bd332016-06-13 16:45:21 +0200424 struct request *rq;
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800425 unsigned int cpu;
Ming Lin1f5bd332016-06-13 16:45:21 +0200426 int ret;
427
428 /*
429 * If the tag allocator sleeps we could get an allocation for a
430 * different hardware context. No need to complicate the low level
431 * allocator for this for the rare use case of a command tied to
432 * a specific queue.
433 */
434 if (WARN_ON_ONCE(!(flags & BLK_MQ_REQ_NOWAIT)))
435 return ERR_PTR(-EINVAL);
436
437 if (hctx_idx >= q->nr_hw_queues)
438 return ERR_PTR(-EIO);
439
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800440 ret = blk_queue_enter(q, flags);
Ming Lin1f5bd332016-06-13 16:45:21 +0200441 if (ret)
442 return ERR_PTR(ret);
443
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600444 /*
445 * Check if the hardware context is actually mapped to anything.
446 * If not tell the caller that it should skip this queue.
447 */
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800448 alloc_data.hctx = q->queue_hw_ctx[hctx_idx];
449 if (!blk_mq_hw_queue_mapped(alloc_data.hctx)) {
450 blk_queue_exit(q);
451 return ERR_PTR(-EXDEV);
Christoph Hellwigc8712c62016-09-23 10:25:48 -0600452 }
Christoph Hellwig20e4d8132018-01-12 10:53:06 +0800453 cpu = cpumask_first_and(alloc_data.hctx->cpumask, cpu_online_mask);
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800454 alloc_data.ctx = __blk_mq_get_ctx(q, cpu);
Ming Lin1f5bd332016-06-13 16:45:21 +0200455
Bart Van Asschecd6ce142017-06-20 11:15:39 -0700456 rq = blk_mq_get_request(q, NULL, op, &alloc_data);
Keith Busch3280d662017-08-14 16:40:11 -0400457 blk_queue_exit(q);
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800458
Omar Sandoval6d2809d2017-02-27 10:28:27 -0800459 if (!rq)
460 return ERR_PTR(-EWOULDBLOCK);
Ming Lin1f5bd332016-06-13 16:45:21 +0200461
462 return rq;
463}
464EXPORT_SYMBOL_GPL(blk_mq_alloc_request_hctx);
465
Keith Busch12f5b932018-05-29 15:52:28 +0200466static void __blk_mq_free_request(struct request *rq)
467{
468 struct request_queue *q = rq->q;
469 struct blk_mq_ctx *ctx = rq->mq_ctx;
470 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
471 const int sched_tag = rq->internal_tag;
472
Bart Van Assche986d4132018-09-26 14:01:10 -0700473 blk_pm_mark_last_busy(rq);
Keith Busch12f5b932018-05-29 15:52:28 +0200474 if (rq->tag != -1)
475 blk_mq_put_tag(hctx, hctx->tags, ctx, rq->tag);
476 if (sched_tag != -1)
477 blk_mq_put_tag(hctx, hctx->sched_tags, ctx, sched_tag);
478 blk_mq_sched_restart(hctx);
479 blk_queue_exit(q);
480}
481
Christoph Hellwig6af54052017-06-16 18:15:22 +0200482void blk_mq_free_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100483{
Jens Axboe320ae512013-10-24 09:20:05 +0100484 struct request_queue *q = rq->q;
Christoph Hellwig6af54052017-06-16 18:15:22 +0200485 struct elevator_queue *e = q->elevator;
486 struct blk_mq_ctx *ctx = rq->mq_ctx;
487 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(q, ctx->cpu);
Jens Axboe320ae512013-10-24 09:20:05 +0100488
Christoph Hellwig5bbf4e52017-06-16 18:15:26 +0200489 if (rq->rq_flags & RQF_ELVPRIV) {
Jens Axboef9cd4bf2018-11-01 16:41:41 -0600490 if (e && e->type->ops.finish_request)
491 e->type->ops.finish_request(rq);
Christoph Hellwig6af54052017-06-16 18:15:22 +0200492 if (rq->elv.icq) {
493 put_io_context(rq->elv.icq->ioc);
494 rq->elv.icq = NULL;
495 }
496 }
497
498 ctx->rq_completed[rq_is_sync(rq)]++;
Christoph Hellwige8064022016-10-20 15:12:13 +0200499 if (rq->rq_flags & RQF_MQ_INFLIGHT)
Jens Axboe0d2602c2014-05-13 15:10:52 -0600500 atomic_dec(&hctx->nr_active);
Jens Axboe87760e52016-11-09 12:38:14 -0700501
Jens Axboe7beb2f82017-09-30 02:08:24 -0600502 if (unlikely(laptop_mode && !blk_rq_is_passthrough(rq)))
503 laptop_io_completion(q->backing_dev_info);
504
Josef Bacika7905042018-07-03 09:32:35 -0600505 rq_qos_done(q, rq);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600506
Keith Busch12f5b932018-05-29 15:52:28 +0200507 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
508 if (refcount_dec_and_test(&rq->ref))
509 __blk_mq_free_request(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100510}
Jens Axboe1a3b5952014-11-17 10:40:48 -0700511EXPORT_SYMBOL_GPL(blk_mq_free_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100512
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200513inline void __blk_mq_end_request(struct request *rq, blk_status_t error)
Jens Axboe320ae512013-10-24 09:20:05 +0100514{
Omar Sandoval522a7772018-05-09 02:08:53 -0700515 u64 now = ktime_get_ns();
516
Omar Sandoval4bc63392018-05-09 02:08:52 -0700517 if (rq->rq_flags & RQF_STATS) {
518 blk_mq_poll_stats_start(rq->q);
Omar Sandoval522a7772018-05-09 02:08:53 -0700519 blk_stat_add(rq, now);
Omar Sandoval4bc63392018-05-09 02:08:52 -0700520 }
521
Omar Sandovaled886602018-09-27 15:55:51 -0700522 if (rq->internal_tag != -1)
523 blk_mq_sched_completed_request(rq, now);
524
Omar Sandoval522a7772018-05-09 02:08:53 -0700525 blk_account_io_done(rq, now);
Ming Lei0d11e6a2013-12-05 10:50:39 -0700526
Christoph Hellwig91b63632014-04-16 09:44:53 +0200527 if (rq->end_io) {
Josef Bacika7905042018-07-03 09:32:35 -0600528 rq_qos_done(rq->q, rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100529 rq->end_io(rq, error);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200530 } else {
531 if (unlikely(blk_bidi_rq(rq)))
532 blk_mq_free_request(rq->next_rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100533 blk_mq_free_request(rq);
Christoph Hellwig91b63632014-04-16 09:44:53 +0200534 }
Jens Axboe320ae512013-10-24 09:20:05 +0100535}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700536EXPORT_SYMBOL(__blk_mq_end_request);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200537
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200538void blk_mq_end_request(struct request *rq, blk_status_t error)
Christoph Hellwig63151a42014-04-16 09:44:52 +0200539{
540 if (blk_update_request(rq, error, blk_rq_bytes(rq)))
541 BUG();
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700542 __blk_mq_end_request(rq, error);
Christoph Hellwig63151a42014-04-16 09:44:52 +0200543}
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700544EXPORT_SYMBOL(blk_mq_end_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100545
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800546static void __blk_mq_complete_request_remote(void *data)
Jens Axboe320ae512013-10-24 09:20:05 +0100547{
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800548 struct request *rq = data;
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600549 struct request_queue *q = rq->q;
Jens Axboe320ae512013-10-24 09:20:05 +0100550
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600551 q->mq_ops->complete(rq);
Jens Axboe320ae512013-10-24 09:20:05 +0100552}
553
Christoph Hellwig453f8342017-04-20 16:03:10 +0200554static void __blk_mq_complete_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100555{
556 struct blk_mq_ctx *ctx = rq->mq_ctx;
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600557 struct request_queue *q = rq->q;
Christoph Hellwig38535202014-04-25 02:32:53 -0700558 bool shared = false;
Jens Axboe320ae512013-10-24 09:20:05 +0100559 int cpu;
560
Keith Busch0fc09f92018-07-23 08:37:50 -0600561 if (!blk_mq_mark_complete(rq))
Keith Busch12f5b932018-05-29 15:52:28 +0200562 return;
Christoph Hellwig453f8342017-04-20 16:03:10 +0200563
Ming Lei36e76532018-09-28 16:42:20 +0800564 /*
565 * Most of single queue controllers, there is only one irq vector
566 * for handling IO completion, and the only irq's affinity is set
567 * as all possible CPUs. On most of ARCHs, this affinity means the
568 * irq is handled on one specific CPU.
569 *
570 * So complete IO reqeust in softirq context in case of single queue
571 * for not degrading IO performance by irqsoff latency.
572 */
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600573 if (q->nr_hw_queues == 1) {
Ming Lei36e76532018-09-28 16:42:20 +0800574 __blk_complete_request(rq);
575 return;
576 }
577
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600578 if (!test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags)) {
579 q->mq_ops->complete(rq);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800580 return;
581 }
Jens Axboe320ae512013-10-24 09:20:05 +0100582
583 cpu = get_cpu();
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600584 if (!test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags))
Christoph Hellwig38535202014-04-25 02:32:53 -0700585 shared = cpus_share_cache(cpu, ctx->cpu);
586
587 if (cpu != ctx->cpu && !shared && cpu_online(ctx->cpu)) {
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800588 rq->csd.func = __blk_mq_complete_request_remote;
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800589 rq->csd.info = rq;
590 rq->csd.flags = 0;
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100591 smp_call_function_single_async(ctx->cpu, &rq->csd);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800592 } else {
Jens Axboec7bb9ad2018-10-31 09:43:30 -0600593 q->mq_ops->complete(rq);
Christoph Hellwig3d6efbf2014-01-08 09:33:37 -0800594 }
Jens Axboe320ae512013-10-24 09:20:05 +0100595 put_cpu();
596}
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800597
Jens Axboe04ced152018-01-09 08:29:46 -0800598static void hctx_unlock(struct blk_mq_hw_ctx *hctx, int srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800599 __releases(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800600{
601 if (!(hctx->flags & BLK_MQ_F_BLOCKING))
602 rcu_read_unlock();
603 else
Tejun Heo05707b62018-01-09 08:29:53 -0800604 srcu_read_unlock(hctx->srcu, srcu_idx);
Jens Axboe04ced152018-01-09 08:29:46 -0800605}
606
607static void hctx_lock(struct blk_mq_hw_ctx *hctx, int *srcu_idx)
Bart Van Asscheb7435db2018-01-10 11:34:27 -0800608 __acquires(hctx->srcu)
Jens Axboe04ced152018-01-09 08:29:46 -0800609{
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700610 if (!(hctx->flags & BLK_MQ_F_BLOCKING)) {
611 /* shut up gcc false positive */
612 *srcu_idx = 0;
Jens Axboe04ced152018-01-09 08:29:46 -0800613 rcu_read_lock();
Jens Axboe08b5a6e2018-01-09 09:32:25 -0700614 } else
Tejun Heo05707b62018-01-09 08:29:53 -0800615 *srcu_idx = srcu_read_lock(hctx->srcu);
Jens Axboe04ced152018-01-09 08:29:46 -0800616}
617
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800618/**
619 * blk_mq_complete_request - end I/O on a request
620 * @rq: the request being processed
621 *
622 * Description:
623 * Ends all I/O on a request. It does not handle partial completions.
624 * The actual completion happens out-of-order, through a IPI handler.
625 **/
Christoph Hellwig08e00292017-04-20 16:03:09 +0200626void blk_mq_complete_request(struct request *rq)
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800627{
Keith Busch12f5b932018-05-29 15:52:28 +0200628 if (unlikely(blk_should_fake_timeout(rq->q)))
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800629 return;
Keith Busch12f5b932018-05-29 15:52:28 +0200630 __blk_mq_complete_request(rq);
Christoph Hellwig30a91cb2014-02-10 03:24:38 -0800631}
632EXPORT_SYMBOL(blk_mq_complete_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100633
Keith Busch973c0192015-01-07 18:55:43 -0700634int blk_mq_request_started(struct request *rq)
635{
Tejun Heo5a61c362018-01-09 08:29:52 -0800636 return blk_mq_rq_state(rq) != MQ_RQ_IDLE;
Keith Busch973c0192015-01-07 18:55:43 -0700637}
638EXPORT_SYMBOL_GPL(blk_mq_request_started);
639
Christoph Hellwige2490072014-09-13 16:40:09 -0700640void blk_mq_start_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100641{
642 struct request_queue *q = rq->q;
643
Jens Axboebd166ef2017-01-17 06:03:22 -0700644 blk_mq_sched_started_request(rq);
645
Jens Axboe320ae512013-10-24 09:20:05 +0100646 trace_block_rq_issue(q, rq);
647
Jens Axboecf43e6b2016-11-07 21:32:37 -0700648 if (test_bit(QUEUE_FLAG_STATS, &q->queue_flags)) {
Omar Sandoval544ccc8d2018-05-09 02:08:50 -0700649 rq->io_start_time_ns = ktime_get_ns();
650#ifdef CONFIG_BLK_DEV_THROTTLING_LOW
651 rq->throtl_size = blk_rq_sectors(rq);
652#endif
Jens Axboecf43e6b2016-11-07 21:32:37 -0700653 rq->rq_flags |= RQF_STATS;
Josef Bacika7905042018-07-03 09:32:35 -0600654 rq_qos_issue(q, rq);
Jens Axboecf43e6b2016-11-07 21:32:37 -0700655 }
656
Tejun Heo1d9bd512018-01-09 08:29:48 -0800657 WARN_ON_ONCE(blk_mq_rq_state(rq) != MQ_RQ_IDLE);
Jens Axboe538b7532014-09-16 10:37:37 -0600658
Tejun Heo1d9bd512018-01-09 08:29:48 -0800659 blk_add_timer(rq);
Keith Busch12f5b932018-05-29 15:52:28 +0200660 WRITE_ONCE(rq->state, MQ_RQ_IN_FLIGHT);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800661
662 if (q->dma_drain_size && blk_rq_bytes(rq)) {
663 /*
664 * Make sure space for the drain appears. We know we can do
665 * this because max_hw_segments has been adjusted to be one
666 * fewer than the device can handle.
667 */
668 rq->nr_phys_segments++;
669 }
Jens Axboe320ae512013-10-24 09:20:05 +0100670}
Christoph Hellwige2490072014-09-13 16:40:09 -0700671EXPORT_SYMBOL(blk_mq_start_request);
Jens Axboe320ae512013-10-24 09:20:05 +0100672
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200673static void __blk_mq_requeue_request(struct request *rq)
Jens Axboe320ae512013-10-24 09:20:05 +0100674{
675 struct request_queue *q = rq->q;
676
Ming Lei923218f2017-11-02 23:24:38 +0800677 blk_mq_put_driver_tag(rq);
678
Jens Axboe320ae512013-10-24 09:20:05 +0100679 trace_block_rq_requeue(q, rq);
Josef Bacika7905042018-07-03 09:32:35 -0600680 rq_qos_requeue(q, rq);
Christoph Hellwig49f5baa2014-02-11 08:27:14 -0800681
Keith Busch12f5b932018-05-29 15:52:28 +0200682 if (blk_mq_request_started(rq)) {
683 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Christoph Hellwigda661262018-06-14 13:58:45 +0200684 rq->rq_flags &= ~RQF_TIMED_OUT;
Christoph Hellwige2490072014-09-13 16:40:09 -0700685 if (q->dma_drain_size && blk_rq_bytes(rq))
686 rq->nr_phys_segments--;
687 }
Jens Axboe320ae512013-10-24 09:20:05 +0100688}
689
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700690void blk_mq_requeue_request(struct request *rq, bool kick_requeue_list)
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200691{
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200692 __blk_mq_requeue_request(rq);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200693
Ming Lei105976f2018-02-23 23:36:56 +0800694 /* this request will be re-inserted to io scheduler queue */
695 blk_mq_sched_requeue_request(rq);
696
Jens Axboe7d692332018-10-24 10:48:12 -0600697 BUG_ON(!list_empty(&rq->queuelist));
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700698 blk_mq_add_to_requeue_list(rq, true, kick_requeue_list);
Christoph Hellwiged0791b2014-04-16 09:44:57 +0200699}
700EXPORT_SYMBOL(blk_mq_requeue_request);
701
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600702static void blk_mq_requeue_work(struct work_struct *work)
703{
704 struct request_queue *q =
Mike Snitzer28494502016-09-14 13:28:30 -0400705 container_of(work, struct request_queue, requeue_work.work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600706 LIST_HEAD(rq_list);
707 struct request *rq, *next;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600708
Jens Axboe18e97812017-07-27 08:03:57 -0600709 spin_lock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600710 list_splice_init(&q->requeue_list, &rq_list);
Jens Axboe18e97812017-07-27 08:03:57 -0600711 spin_unlock_irq(&q->requeue_lock);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600712
713 list_for_each_entry_safe(rq, next, &rq_list, queuelist) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200714 if (!(rq->rq_flags & RQF_SOFTBARRIER))
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600715 continue;
716
Christoph Hellwige8064022016-10-20 15:12:13 +0200717 rq->rq_flags &= ~RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600718 list_del_init(&rq->queuelist);
Mike Snitzer9e97d292018-01-17 11:25:58 -0500719 blk_mq_sched_insert_request(rq, true, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600720 }
721
722 while (!list_empty(&rq_list)) {
723 rq = list_entry(rq_list.next, struct request, queuelist);
724 list_del_init(&rq->queuelist);
Mike Snitzer9e97d292018-01-17 11:25:58 -0500725 blk_mq_sched_insert_request(rq, false, false, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600726 }
727
Bart Van Assche52d7f1b2016-10-28 17:20:32 -0700728 blk_mq_run_hw_queues(q, false);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600729}
730
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700731void blk_mq_add_to_requeue_list(struct request *rq, bool at_head,
732 bool kick_requeue_list)
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600733{
734 struct request_queue *q = rq->q;
735 unsigned long flags;
736
737 /*
738 * We abuse this flag that is otherwise used by the I/O scheduler to
Jens Axboeff821d22017-11-10 22:05:12 -0700739 * request head insertion from the workqueue.
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600740 */
Christoph Hellwige8064022016-10-20 15:12:13 +0200741 BUG_ON(rq->rq_flags & RQF_SOFTBARRIER);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600742
743 spin_lock_irqsave(&q->requeue_lock, flags);
744 if (at_head) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200745 rq->rq_flags |= RQF_SOFTBARRIER;
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600746 list_add(&rq->queuelist, &q->requeue_list);
747 } else {
748 list_add_tail(&rq->queuelist, &q->requeue_list);
749 }
750 spin_unlock_irqrestore(&q->requeue_lock, flags);
Bart Van Assche2b053ac2016-10-28 17:21:41 -0700751
752 if (kick_requeue_list)
753 blk_mq_kick_requeue_list(q);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600754}
755EXPORT_SYMBOL(blk_mq_add_to_requeue_list);
756
757void blk_mq_kick_requeue_list(struct request_queue *q)
758{
Bart Van Asscheae943d22018-01-19 08:58:55 -0800759 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work, 0);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -0600760}
761EXPORT_SYMBOL(blk_mq_kick_requeue_list);
762
Mike Snitzer28494502016-09-14 13:28:30 -0400763void blk_mq_delay_kick_requeue_list(struct request_queue *q,
764 unsigned long msecs)
765{
Bart Van Assched4acf362017-08-09 11:28:06 -0700766 kblockd_mod_delayed_work_on(WORK_CPU_UNBOUND, &q->requeue_work,
767 msecs_to_jiffies(msecs));
Mike Snitzer28494502016-09-14 13:28:30 -0400768}
769EXPORT_SYMBOL(blk_mq_delay_kick_requeue_list);
770
Jens Axboe0e62f512014-06-04 10:23:49 -0600771struct request *blk_mq_tag_to_rq(struct blk_mq_tags *tags, unsigned int tag)
772{
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600773 if (tag < tags->nr_tags) {
774 prefetch(tags->rqs[tag]);
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700775 return tags->rqs[tag];
Jens Axboe88c7b2b2016-08-25 08:07:30 -0600776 }
Hannes Reinecke4ee86ba2016-03-15 12:03:28 -0700777
778 return NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -0600779}
780EXPORT_SYMBOL(blk_mq_tag_to_rq);
781
Tejun Heo358f70d2018-01-09 08:29:50 -0800782static void blk_mq_rq_timed_out(struct request *req, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100783{
Christoph Hellwigda661262018-06-14 13:58:45 +0200784 req->rq_flags |= RQF_TIMED_OUT;
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200785 if (req->q->mq_ops->timeout) {
786 enum blk_eh_timer_return ret;
Jens Axboe87ee7b12014-04-24 08:51:47 -0600787
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200788 ret = req->q->mq_ops->timeout(req, reserved);
789 if (ret == BLK_EH_DONE)
790 return;
791 WARN_ON_ONCE(ret != BLK_EH_RESET_TIMER);
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700792 }
Christoph Hellwigd1210d52018-05-29 15:52:39 +0200793
794 blk_add_timer(req);
Jens Axboe87ee7b12014-04-24 08:51:47 -0600795}
Keith Busch5b3f25f2015-01-07 18:55:46 -0700796
Keith Busch12f5b932018-05-29 15:52:28 +0200797static bool blk_mq_req_expired(struct request *rq, unsigned long *next)
798{
799 unsigned long deadline;
800
801 if (blk_mq_rq_state(rq) != MQ_RQ_IN_FLIGHT)
802 return false;
Christoph Hellwigda661262018-06-14 13:58:45 +0200803 if (rq->rq_flags & RQF_TIMED_OUT)
804 return false;
Keith Busch12f5b932018-05-29 15:52:28 +0200805
806 deadline = blk_rq_deadline(rq);
807 if (time_after_eq(jiffies, deadline))
808 return true;
809
810 if (*next == 0)
811 *next = deadline;
812 else if (time_after(*next, deadline))
813 *next = deadline;
814 return false;
815}
816
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700817static void blk_mq_check_expired(struct blk_mq_hw_ctx *hctx,
818 struct request *rq, void *priv, bool reserved)
Jens Axboe320ae512013-10-24 09:20:05 +0100819{
Keith Busch12f5b932018-05-29 15:52:28 +0200820 unsigned long *next = priv;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700821
Keith Busch12f5b932018-05-29 15:52:28 +0200822 /*
823 * Just do a quick check if it is expired before locking the request in
824 * so we're not unnecessarilly synchronizing across CPUs.
825 */
826 if (!blk_mq_req_expired(rq, next))
Christoph Hellwig46f92d42014-09-13 16:40:12 -0700827 return;
Jens Axboe320ae512013-10-24 09:20:05 +0100828
Tejun Heo1d9bd512018-01-09 08:29:48 -0800829 /*
Keith Busch12f5b932018-05-29 15:52:28 +0200830 * We have reason to believe the request may be expired. Take a
831 * reference on the request to lock this request lifetime into its
832 * currently allocated context to prevent it from being reallocated in
833 * the event the completion by-passes this timeout handler.
834 *
835 * If the reference was already released, then the driver beat the
836 * timeout handler to posting a natural completion.
Tejun Heo1d9bd512018-01-09 08:29:48 -0800837 */
Keith Busch12f5b932018-05-29 15:52:28 +0200838 if (!refcount_inc_not_zero(&rq->ref))
839 return;
840
841 /*
842 * The request is now locked and cannot be reallocated underneath the
843 * timeout handler's processing. Re-verify this exact request is truly
844 * expired; if it is not expired, then the request was completed and
845 * reallocated as a new request.
846 */
847 if (blk_mq_req_expired(rq, next))
Tejun Heo1d9bd512018-01-09 08:29:48 -0800848 blk_mq_rq_timed_out(rq, reserved);
Keith Busch12f5b932018-05-29 15:52:28 +0200849 if (refcount_dec_and_test(&rq->ref))
850 __blk_mq_free_request(rq);
Tejun Heo1d9bd512018-01-09 08:29:48 -0800851}
852
Christoph Hellwig287922e2015-10-30 20:57:30 +0800853static void blk_mq_timeout_work(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +0100854{
Christoph Hellwig287922e2015-10-30 20:57:30 +0800855 struct request_queue *q =
856 container_of(work, struct request_queue, timeout_work);
Keith Busch12f5b932018-05-29 15:52:28 +0200857 unsigned long next = 0;
Tejun Heo1d9bd512018-01-09 08:29:48 -0800858 struct blk_mq_hw_ctx *hctx;
Christoph Hellwig81481eb2014-09-13 16:40:11 -0700859 int i;
Jens Axboe320ae512013-10-24 09:20:05 +0100860
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600861 /* A deadlock might occur if a request is stuck requiring a
862 * timeout at the same time a queue freeze is waiting
863 * completion, since the timeout code would not be able to
864 * acquire the queue reference here.
865 *
866 * That's why we don't use blk_queue_enter here; instead, we use
867 * percpu_ref_tryget directly, because we need to be able to
868 * obtain a reference even in the short window between the queue
869 * starting to freeze, by dropping the first reference in
Ming Lei1671d522017-03-27 20:06:57 +0800870 * blk_freeze_queue_start, and the moment the last request is
Gabriel Krisman Bertazi71f79fb2016-08-01 08:23:39 -0600871 * consumed, marked by the instant q_usage_counter reaches
872 * zero.
873 */
874 if (!percpu_ref_tryget(&q->q_usage_counter))
Christoph Hellwig287922e2015-10-30 20:57:30 +0800875 return;
876
Keith Busch12f5b932018-05-29 15:52:28 +0200877 blk_mq_queue_tag_busy_iter(q, blk_mq_check_expired, &next);
Jens Axboe320ae512013-10-24 09:20:05 +0100878
Keith Busch12f5b932018-05-29 15:52:28 +0200879 if (next != 0) {
880 mod_timer(&q->timeout, next);
Jens Axboe0d2602c2014-05-13 15:10:52 -0600881 } else {
Bart Van Asschefcd36c32018-01-10 08:33:33 -0800882 /*
883 * Request timeouts are handled as a forward rolling timer. If
884 * we end up here it means that no requests are pending and
885 * also that no request has been pending for a while. Mark
886 * each hctx as idle.
887 */
Ming Leif054b562015-04-21 10:00:19 +0800888 queue_for_each_hw_ctx(q, hctx, i) {
889 /* the hctx may be unmapped, so check it here */
890 if (blk_mq_hw_queue_mapped(hctx))
891 blk_mq_tag_idle(hctx);
892 }
Jens Axboe0d2602c2014-05-13 15:10:52 -0600893 }
Christoph Hellwig287922e2015-10-30 20:57:30 +0800894 blk_queue_exit(q);
Jens Axboe320ae512013-10-24 09:20:05 +0100895}
896
Omar Sandoval88459642016-09-17 08:38:44 -0600897struct flush_busy_ctx_data {
898 struct blk_mq_hw_ctx *hctx;
899 struct list_head *list;
900};
901
902static bool flush_busy_ctx(struct sbitmap *sb, unsigned int bitnr, void *data)
903{
904 struct flush_busy_ctx_data *flush_data = data;
905 struct blk_mq_hw_ctx *hctx = flush_data->hctx;
906 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
907
Omar Sandoval88459642016-09-17 08:38:44 -0600908 spin_lock(&ctx->lock);
909 list_splice_tail_init(&ctx->rq_list, flush_data->list);
Omar Sandovale9a99a62018-02-27 16:56:42 -0800910 sbitmap_clear_bit(sb, bitnr);
Omar Sandoval88459642016-09-17 08:38:44 -0600911 spin_unlock(&ctx->lock);
912 return true;
913}
914
Jens Axboe320ae512013-10-24 09:20:05 +0100915/*
Jens Axboe1429d7c2014-05-19 09:23:55 -0600916 * Process software queues that have been marked busy, splicing them
917 * to the for-dispatch
918 */
Jens Axboe2c3ad662016-12-14 14:34:47 -0700919void blk_mq_flush_busy_ctxs(struct blk_mq_hw_ctx *hctx, struct list_head *list)
Jens Axboe1429d7c2014-05-19 09:23:55 -0600920{
Omar Sandoval88459642016-09-17 08:38:44 -0600921 struct flush_busy_ctx_data data = {
922 .hctx = hctx,
923 .list = list,
924 };
Jens Axboe1429d7c2014-05-19 09:23:55 -0600925
Omar Sandoval88459642016-09-17 08:38:44 -0600926 sbitmap_for_each_set(&hctx->ctx_map, flush_busy_ctx, &data);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600927}
Jens Axboe2c3ad662016-12-14 14:34:47 -0700928EXPORT_SYMBOL_GPL(blk_mq_flush_busy_ctxs);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600929
Ming Leib3476892017-10-14 17:22:30 +0800930struct dispatch_rq_data {
931 struct blk_mq_hw_ctx *hctx;
932 struct request *rq;
933};
934
935static bool dispatch_rq_from_ctx(struct sbitmap *sb, unsigned int bitnr,
936 void *data)
937{
938 struct dispatch_rq_data *dispatch_data = data;
939 struct blk_mq_hw_ctx *hctx = dispatch_data->hctx;
940 struct blk_mq_ctx *ctx = hctx->ctxs[bitnr];
941
942 spin_lock(&ctx->lock);
huhaib4f6f382018-05-22 17:39:34 +0800943 if (!list_empty(&ctx->rq_list)) {
Ming Leib3476892017-10-14 17:22:30 +0800944 dispatch_data->rq = list_entry_rq(ctx->rq_list.next);
945 list_del_init(&dispatch_data->rq->queuelist);
946 if (list_empty(&ctx->rq_list))
947 sbitmap_clear_bit(sb, bitnr);
948 }
949 spin_unlock(&ctx->lock);
950
951 return !dispatch_data->rq;
952}
953
954struct request *blk_mq_dequeue_from_ctx(struct blk_mq_hw_ctx *hctx,
955 struct blk_mq_ctx *start)
956{
957 unsigned off = start ? start->index_hw : 0;
958 struct dispatch_rq_data data = {
959 .hctx = hctx,
960 .rq = NULL,
961 };
962
963 __sbitmap_for_each_set(&hctx->ctx_map, off,
964 dispatch_rq_from_ctx, &data);
965
966 return data.rq;
967}
968
Jens Axboe703fd1c2016-09-16 13:59:14 -0600969static inline unsigned int queued_to_index(unsigned int queued)
970{
971 if (!queued)
972 return 0;
Jens Axboe1429d7c2014-05-19 09:23:55 -0600973
Jens Axboe703fd1c2016-09-16 13:59:14 -0600974 return min(BLK_MQ_MAX_DISPATCH_ORDER - 1, ilog2(queued) + 1);
Jens Axboe1429d7c2014-05-19 09:23:55 -0600975}
976
Ming Lei8ab6bb9e2018-06-25 19:31:45 +0800977bool blk_mq_get_driver_tag(struct request *rq)
Jens Axboebd166ef2017-01-17 06:03:22 -0700978{
979 struct blk_mq_alloc_data data = {
980 .q = rq->q,
Jens Axboebd166ef2017-01-17 06:03:22 -0700981 .hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu),
Ming Lei8ab6bb9e2018-06-25 19:31:45 +0800982 .flags = BLK_MQ_REQ_NOWAIT,
Jens Axboebd166ef2017-01-17 06:03:22 -0700983 };
Jianchao Wangd263ed92018-08-09 08:34:17 -0600984 bool shared;
Jens Axboe5feeacd2017-04-20 17:23:13 -0600985
Omar Sandoval81380ca2017-04-07 08:56:26 -0600986 if (rq->tag != -1)
987 goto done;
Jens Axboebd166ef2017-01-17 06:03:22 -0700988
Sagi Grimberg415b8062017-02-27 10:04:39 -0700989 if (blk_mq_tag_is_reserved(data.hctx->sched_tags, rq->internal_tag))
990 data.flags |= BLK_MQ_REQ_RESERVED;
991
Jianchao Wangd263ed92018-08-09 08:34:17 -0600992 shared = blk_mq_tag_busy(data.hctx);
Jens Axboebd166ef2017-01-17 06:03:22 -0700993 rq->tag = blk_mq_get_tag(&data);
994 if (rq->tag >= 0) {
Jianchao Wangd263ed92018-08-09 08:34:17 -0600995 if (shared) {
Jens Axboe200e86b2017-01-25 08:11:38 -0700996 rq->rq_flags |= RQF_MQ_INFLIGHT;
997 atomic_inc(&data.hctx->nr_active);
998 }
Jens Axboebd166ef2017-01-17 06:03:22 -0700999 data.hctx->tags->rqs[rq->tag] = rq;
Jens Axboebd166ef2017-01-17 06:03:22 -07001000 }
1001
Omar Sandoval81380ca2017-04-07 08:56:26 -06001002done:
Omar Sandoval81380ca2017-04-07 08:56:26 -06001003 return rq->tag != -1;
Jens Axboebd166ef2017-01-17 06:03:22 -07001004}
1005
Jens Axboeeb619fd2017-11-09 08:32:43 -07001006static int blk_mq_dispatch_wake(wait_queue_entry_t *wait, unsigned mode,
1007 int flags, void *key)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001008{
1009 struct blk_mq_hw_ctx *hctx;
1010
1011 hctx = container_of(wait, struct blk_mq_hw_ctx, dispatch_wait);
1012
Ming Lei5815839b2018-06-25 19:31:47 +08001013 spin_lock(&hctx->dispatch_wait_lock);
Jens Axboeeb619fd2017-11-09 08:32:43 -07001014 list_del_init(&wait->entry);
Ming Lei5815839b2018-06-25 19:31:47 +08001015 spin_unlock(&hctx->dispatch_wait_lock);
1016
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001017 blk_mq_run_hw_queue(hctx, true);
1018 return 1;
1019}
1020
Jens Axboef906a6a2017-11-09 16:10:13 -07001021/*
1022 * Mark us waiting for a tag. For shared tags, this involves hooking us into
Bart Van Asscheee3e4de2018-01-09 10:09:15 -08001023 * the tag wakeups. For non-shared tags, we can simply mark us needing a
1024 * restart. For both cases, take care to check the condition again after
Jens Axboef906a6a2017-11-09 16:10:13 -07001025 * marking us as waiting.
1026 */
Ming Lei2278d692018-06-25 19:31:46 +08001027static bool blk_mq_mark_tag_wait(struct blk_mq_hw_ctx *hctx,
Jens Axboef906a6a2017-11-09 16:10:13 -07001028 struct request *rq)
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001029{
Ming Lei5815839b2018-06-25 19:31:47 +08001030 struct wait_queue_head *wq;
Jens Axboef906a6a2017-11-09 16:10:13 -07001031 wait_queue_entry_t *wait;
1032 bool ret;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001033
Ming Lei2278d692018-06-25 19:31:46 +08001034 if (!(hctx->flags & BLK_MQ_F_TAG_SHARED)) {
1035 if (!test_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state))
1036 set_bit(BLK_MQ_S_SCHED_RESTART, &hctx->state);
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001037
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001038 /*
1039 * It's possible that a tag was freed in the window between the
1040 * allocation failure and adding the hardware queue to the wait
1041 * queue.
1042 *
1043 * Don't clear RESTART here, someone else could have set it.
1044 * At most this will cost an extra queue run.
1045 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001046 return blk_mq_get_driver_tag(rq);
Jens Axboeeb619fd2017-11-09 08:32:43 -07001047 }
1048
Ming Lei2278d692018-06-25 19:31:46 +08001049 wait = &hctx->dispatch_wait;
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001050 if (!list_empty_careful(&wait->entry))
1051 return false;
1052
Ming Lei5815839b2018-06-25 19:31:47 +08001053 wq = &bt_wait_ptr(&hctx->tags->bitmap_tags, hctx)->wait;
1054
1055 spin_lock_irq(&wq->lock);
1056 spin_lock(&hctx->dispatch_wait_lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001057 if (!list_empty(&wait->entry)) {
Ming Lei5815839b2018-06-25 19:31:47 +08001058 spin_unlock(&hctx->dispatch_wait_lock);
1059 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001060 return false;
1061 }
1062
Ming Lei5815839b2018-06-25 19:31:47 +08001063 wait->flags &= ~WQ_FLAG_EXCLUSIVE;
1064 __add_wait_queue(wq, wait);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001065
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001066 /*
Jens Axboeeb619fd2017-11-09 08:32:43 -07001067 * It's possible that a tag was freed in the window between the
1068 * allocation failure and adding the hardware queue to the wait
1069 * queue.
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001070 */
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001071 ret = blk_mq_get_driver_tag(rq);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001072 if (!ret) {
Ming Lei5815839b2018-06-25 19:31:47 +08001073 spin_unlock(&hctx->dispatch_wait_lock);
1074 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001075 return false;
Jens Axboef906a6a2017-11-09 16:10:13 -07001076 }
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001077
1078 /*
1079 * We got a tag, remove ourselves from the wait queue to ensure
1080 * someone else gets the wakeup.
1081 */
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001082 list_del_init(&wait->entry);
Ming Lei5815839b2018-06-25 19:31:47 +08001083 spin_unlock(&hctx->dispatch_wait_lock);
1084 spin_unlock_irq(&wq->lock);
Bart Van Asschec27d53f2018-01-10 13:41:21 -08001085
1086 return true;
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001087}
1088
Ming Lei6e7687172018-07-03 09:03:16 -06001089#define BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT 8
1090#define BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR 4
1091/*
1092 * Update dispatch busy with the Exponential Weighted Moving Average(EWMA):
1093 * - EWMA is one simple way to compute running average value
1094 * - weight(7/8 and 1/8) is applied so that it can decrease exponentially
1095 * - take 4 as factor for avoiding to get too small(0) result, and this
1096 * factor doesn't matter because EWMA decreases exponentially
1097 */
1098static void blk_mq_update_dispatch_busy(struct blk_mq_hw_ctx *hctx, bool busy)
1099{
1100 unsigned int ewma;
1101
1102 if (hctx->queue->elevator)
1103 return;
1104
1105 ewma = hctx->dispatch_busy;
1106
1107 if (!ewma && !busy)
1108 return;
1109
1110 ewma *= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT - 1;
1111 if (busy)
1112 ewma += 1 << BLK_MQ_DISPATCH_BUSY_EWMA_FACTOR;
1113 ewma /= BLK_MQ_DISPATCH_BUSY_EWMA_WEIGHT;
1114
1115 hctx->dispatch_busy = ewma;
1116}
1117
Ming Lei86ff7c22018-01-30 22:04:57 -05001118#define BLK_MQ_RESOURCE_DELAY 3 /* ms units */
1119
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001120/*
1121 * Returns true if we did some work AND can potentially do more.
1122 */
Ming Leide148292017-10-14 17:22:29 +08001123bool blk_mq_dispatch_rq_list(struct request_queue *q, struct list_head *list,
Jens Axboeeb619fd2017-11-09 08:32:43 -07001124 bool got_budget)
Jens Axboef04c3df2016-12-07 08:41:17 -07001125{
Omar Sandoval81380ca2017-04-07 08:56:26 -06001126 struct blk_mq_hw_ctx *hctx;
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001127 struct request *rq, *nxt;
Jens Axboeeb619fd2017-11-09 08:32:43 -07001128 bool no_tag = false;
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001129 int errors, queued;
Ming Lei86ff7c22018-01-30 22:04:57 -05001130 blk_status_t ret = BLK_STS_OK;
Jens Axboef04c3df2016-12-07 08:41:17 -07001131
Omar Sandoval81380ca2017-04-07 08:56:26 -06001132 if (list_empty(list))
1133 return false;
1134
Ming Leide148292017-10-14 17:22:29 +08001135 WARN_ON(!list_is_singular(list) && got_budget);
1136
Jens Axboef04c3df2016-12-07 08:41:17 -07001137 /*
Jens Axboef04c3df2016-12-07 08:41:17 -07001138 * Now process all the entries, sending them to the driver.
1139 */
Jens Axboe93efe982017-03-24 12:04:19 -06001140 errors = queued = 0;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001141 do {
Jens Axboef04c3df2016-12-07 08:41:17 -07001142 struct blk_mq_queue_data bd;
1143
1144 rq = list_first_entry(list, struct request, queuelist);
Ming Lei0bca7992018-04-05 00:35:21 +08001145
1146 hctx = blk_mq_map_queue(rq->q, rq->mq_ctx->cpu);
1147 if (!got_budget && !blk_mq_get_dispatch_budget(hctx))
1148 break;
1149
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001150 if (!blk_mq_get_driver_tag(rq)) {
Jens Axboe3c782d62017-01-26 12:50:36 -07001151 /*
Omar Sandovalda55f2c2017-02-22 10:58:29 -08001152 * The initial allocation attempt failed, so we need to
Jens Axboeeb619fd2017-11-09 08:32:43 -07001153 * rerun the hardware queue when a tag is freed. The
1154 * waitqueue takes care of that. If the queue is run
1155 * before we add this entry back on the dispatch list,
1156 * we'll re-run it below.
Jens Axboe3c782d62017-01-26 12:50:36 -07001157 */
Ming Lei2278d692018-06-25 19:31:46 +08001158 if (!blk_mq_mark_tag_wait(hctx, rq)) {
Ming Lei0bca7992018-04-05 00:35:21 +08001159 blk_mq_put_dispatch_budget(hctx);
Jens Axboef906a6a2017-11-09 16:10:13 -07001160 /*
1161 * For non-shared tags, the RESTART check
1162 * will suffice.
1163 */
1164 if (hctx->flags & BLK_MQ_F_TAG_SHARED)
1165 no_tag = true;
Omar Sandoval807b1042017-04-05 12:01:35 -07001166 break;
Ming Leide148292017-10-14 17:22:29 +08001167 }
1168 }
1169
Jens Axboef04c3df2016-12-07 08:41:17 -07001170 list_del_init(&rq->queuelist);
1171
1172 bd.rq = rq;
Jens Axboe113285b2017-03-02 13:26:04 -07001173
1174 /*
1175 * Flag last if we have no more requests, or if we have more
1176 * but can't assign a driver tag to it.
1177 */
1178 if (list_empty(list))
1179 bd.last = true;
1180 else {
Jens Axboe113285b2017-03-02 13:26:04 -07001181 nxt = list_first_entry(list, struct request, queuelist);
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001182 bd.last = !blk_mq_get_driver_tag(nxt);
Jens Axboe113285b2017-03-02 13:26:04 -07001183 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001184
1185 ret = q->mq_ops->queue_rq(hctx, &bd);
Ming Lei86ff7c22018-01-30 22:04:57 -05001186 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE) {
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001187 /*
1188 * If an I/O scheduler has been configured and we got a
Jens Axboeff821d22017-11-10 22:05:12 -07001189 * driver tag for the next request already, free it
1190 * again.
Jianchao Wang6d6f167c2017-11-02 23:24:32 +08001191 */
1192 if (!list_empty(list)) {
1193 nxt = list_first_entry(list, struct request, queuelist);
1194 blk_mq_put_driver_tag(nxt);
1195 }
Jens Axboef04c3df2016-12-07 08:41:17 -07001196 list_add(&rq->queuelist, list);
1197 __blk_mq_requeue_request(rq);
1198 break;
Jens Axboef04c3df2016-12-07 08:41:17 -07001199 }
1200
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001201 if (unlikely(ret != BLK_STS_OK)) {
1202 errors++;
1203 blk_mq_end_request(rq, BLK_STS_IOERR);
1204 continue;
1205 }
1206
1207 queued++;
Omar Sandoval81380ca2017-04-07 08:56:26 -06001208 } while (!list_empty(list));
Jens Axboef04c3df2016-12-07 08:41:17 -07001209
1210 hctx->dispatched[queued_to_index(queued)]++;
1211
1212 /*
1213 * Any items that need requeuing? Stuff them into hctx->dispatch,
1214 * that is where we will continue on next queue run.
1215 */
1216 if (!list_empty(list)) {
Ming Lei86ff7c22018-01-30 22:04:57 -05001217 bool needs_restart;
1218
Jens Axboef04c3df2016-12-07 08:41:17 -07001219 spin_lock(&hctx->lock);
Jens Axboec13660a2017-01-26 12:40:07 -07001220 list_splice_init(list, &hctx->dispatch);
Jens Axboef04c3df2016-12-07 08:41:17 -07001221 spin_unlock(&hctx->lock);
1222
1223 /*
Bart Van Assche710c7852017-04-07 11:16:51 -07001224 * If SCHED_RESTART was set by the caller of this function and
1225 * it is no longer set that means that it was cleared by another
1226 * thread and hence that a queue rerun is needed.
Jens Axboef04c3df2016-12-07 08:41:17 -07001227 *
Jens Axboeeb619fd2017-11-09 08:32:43 -07001228 * If 'no_tag' is set, that means that we failed getting
1229 * a driver tag with an I/O scheduler attached. If our dispatch
1230 * waitqueue is no longer active, ensure that we run the queue
1231 * AFTER adding our entries back to the list.
Jens Axboebd166ef2017-01-17 06:03:22 -07001232 *
Bart Van Assche710c7852017-04-07 11:16:51 -07001233 * If no I/O scheduler has been configured it is possible that
1234 * the hardware queue got stopped and restarted before requests
1235 * were pushed back onto the dispatch list. Rerun the queue to
1236 * avoid starvation. Notes:
1237 * - blk_mq_run_hw_queue() checks whether or not a queue has
1238 * been stopped before rerunning a queue.
1239 * - Some but not all block drivers stop a queue before
Christoph Hellwigfc17b652017-06-03 09:38:05 +02001240 * returning BLK_STS_RESOURCE. Two exceptions are scsi-mq
Bart Van Assche710c7852017-04-07 11:16:51 -07001241 * and dm-rq.
Ming Lei86ff7c22018-01-30 22:04:57 -05001242 *
1243 * If driver returns BLK_STS_RESOURCE and SCHED_RESTART
1244 * bit is set, run queue after a delay to avoid IO stalls
1245 * that could otherwise occur if the queue is idle.
Jens Axboebd166ef2017-01-17 06:03:22 -07001246 */
Ming Lei86ff7c22018-01-30 22:04:57 -05001247 needs_restart = blk_mq_sched_needs_restart(hctx);
1248 if (!needs_restart ||
Jens Axboeeb619fd2017-11-09 08:32:43 -07001249 (no_tag && list_empty_careful(&hctx->dispatch_wait.entry)))
Jens Axboebd166ef2017-01-17 06:03:22 -07001250 blk_mq_run_hw_queue(hctx, true);
Ming Lei86ff7c22018-01-30 22:04:57 -05001251 else if (needs_restart && (ret == BLK_STS_RESOURCE))
1252 blk_mq_delay_run_hw_queue(hctx, BLK_MQ_RESOURCE_DELAY);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001253
Ming Lei6e7687172018-07-03 09:03:16 -06001254 blk_mq_update_dispatch_busy(hctx, true);
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001255 return false;
Ming Lei6e7687172018-07-03 09:03:16 -06001256 } else
1257 blk_mq_update_dispatch_busy(hctx, false);
Jens Axboef04c3df2016-12-07 08:41:17 -07001258
Jens Axboe1f57f8d2018-06-28 11:54:01 -06001259 /*
1260 * If the host/device is unable to accept more work, inform the
1261 * caller of that.
1262 */
1263 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
1264 return false;
1265
Jens Axboe93efe982017-03-24 12:04:19 -06001266 return (queued + errors) != 0;
Jens Axboef04c3df2016-12-07 08:41:17 -07001267}
1268
Bart Van Assche6a83e742016-11-02 10:09:51 -06001269static void __blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx)
1270{
1271 int srcu_idx;
1272
Jens Axboeb7a71e62017-08-01 09:28:24 -06001273 /*
1274 * We should be running this queue from one of the CPUs that
1275 * are mapped to it.
Ming Lei7df938f2018-01-18 00:41:52 +08001276 *
1277 * There are at least two related races now between setting
1278 * hctx->next_cpu from blk_mq_hctx_next_cpu() and running
1279 * __blk_mq_run_hw_queue():
1280 *
1281 * - hctx->next_cpu is found offline in blk_mq_hctx_next_cpu(),
1282 * but later it becomes online, then this warning is harmless
1283 * at all
1284 *
1285 * - hctx->next_cpu is found online in blk_mq_hctx_next_cpu(),
1286 * but later it becomes offline, then the warning can't be
1287 * triggered, and we depend on blk-mq timeout handler to
1288 * handle dispatched requests to this hctx
Jens Axboeb7a71e62017-08-01 09:28:24 -06001289 */
Ming Lei7df938f2018-01-18 00:41:52 +08001290 if (!cpumask_test_cpu(raw_smp_processor_id(), hctx->cpumask) &&
1291 cpu_online(hctx->next_cpu)) {
1292 printk(KERN_WARNING "run queue from wrong CPU %d, hctx %s\n",
1293 raw_smp_processor_id(),
1294 cpumask_empty(hctx->cpumask) ? "inactive": "active");
1295 dump_stack();
1296 }
Bart Van Assche6a83e742016-11-02 10:09:51 -06001297
Jens Axboeb7a71e62017-08-01 09:28:24 -06001298 /*
1299 * We can't run the queue inline with ints disabled. Ensure that
1300 * we catch bad users of this early.
1301 */
1302 WARN_ON_ONCE(in_interrupt());
1303
Jens Axboe04ced152018-01-09 08:29:46 -08001304 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
Jens Axboebf4907c2017-03-30 12:30:39 -06001305
Jens Axboe04ced152018-01-09 08:29:46 -08001306 hctx_lock(hctx, &srcu_idx);
1307 blk_mq_sched_dispatch_requests(hctx);
1308 hctx_unlock(hctx, srcu_idx);
Bart Van Assche6a83e742016-11-02 10:09:51 -06001309}
1310
Ming Leif82ddf12018-04-08 17:48:10 +08001311static inline int blk_mq_first_mapped_cpu(struct blk_mq_hw_ctx *hctx)
1312{
1313 int cpu = cpumask_first_and(hctx->cpumask, cpu_online_mask);
1314
1315 if (cpu >= nr_cpu_ids)
1316 cpu = cpumask_first(hctx->cpumask);
1317 return cpu;
1318}
1319
Jens Axboe506e9312014-05-07 10:26:44 -06001320/*
1321 * It'd be great if the workqueue API had a way to pass
1322 * in a mask and had some smarts for more clever placement.
1323 * For now we just round-robin here, switching for every
1324 * BLK_MQ_CPU_WORK_BATCH queued items.
1325 */
1326static int blk_mq_hctx_next_cpu(struct blk_mq_hw_ctx *hctx)
1327{
Ming Lei7bed4592018-01-18 00:41:51 +08001328 bool tried = false;
Ming Lei476f8c92018-04-08 17:48:09 +08001329 int next_cpu = hctx->next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001330
Christoph Hellwigb657d7e2014-11-24 09:27:23 +01001331 if (hctx->queue->nr_hw_queues == 1)
1332 return WORK_CPU_UNBOUND;
Jens Axboe506e9312014-05-07 10:26:44 -06001333
1334 if (--hctx->next_cpu_batch <= 0) {
Ming Lei7bed4592018-01-18 00:41:51 +08001335select_cpu:
Ming Lei476f8c92018-04-08 17:48:09 +08001336 next_cpu = cpumask_next_and(next_cpu, hctx->cpumask,
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08001337 cpu_online_mask);
Jens Axboe506e9312014-05-07 10:26:44 -06001338 if (next_cpu >= nr_cpu_ids)
Ming Leif82ddf12018-04-08 17:48:10 +08001339 next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06001340 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
1341 }
1342
Ming Lei7bed4592018-01-18 00:41:51 +08001343 /*
1344 * Do unbound schedule if we can't find a online CPU for this hctx,
1345 * and it should only happen in the path of handling CPU DEAD.
1346 */
Ming Lei476f8c92018-04-08 17:48:09 +08001347 if (!cpu_online(next_cpu)) {
Ming Lei7bed4592018-01-18 00:41:51 +08001348 if (!tried) {
1349 tried = true;
1350 goto select_cpu;
1351 }
1352
1353 /*
1354 * Make sure to re-select CPU next time once after CPUs
1355 * in hctx->cpumask become online again.
1356 */
Ming Lei476f8c92018-04-08 17:48:09 +08001357 hctx->next_cpu = next_cpu;
Ming Lei7bed4592018-01-18 00:41:51 +08001358 hctx->next_cpu_batch = 1;
1359 return WORK_CPU_UNBOUND;
1360 }
Ming Lei476f8c92018-04-08 17:48:09 +08001361
1362 hctx->next_cpu = next_cpu;
1363 return next_cpu;
Jens Axboe506e9312014-05-07 10:26:44 -06001364}
1365
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001366static void __blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async,
1367 unsigned long msecs)
Jens Axboe320ae512013-10-24 09:20:05 +01001368{
Bart Van Assche5435c022017-06-20 11:15:49 -07001369 if (unlikely(blk_mq_hctx_stopped(hctx)))
Jens Axboe320ae512013-10-24 09:20:05 +01001370 return;
1371
Jens Axboe1b792f22016-09-21 10:12:13 -06001372 if (!async && !(hctx->flags & BLK_MQ_F_BLOCKING)) {
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001373 int cpu = get_cpu();
1374 if (cpumask_test_cpu(cpu, hctx->cpumask)) {
Paolo Bonzini398205b2014-11-07 23:03:59 +01001375 __blk_mq_run_hw_queue(hctx);
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001376 put_cpu();
Paolo Bonzini398205b2014-11-07 23:03:59 +01001377 return;
1378 }
Jens Axboee4043dc2014-04-09 10:18:23 -06001379
Paolo Bonzini2a90d4a2014-11-07 23:04:00 +01001380 put_cpu();
Jens Axboee4043dc2014-04-09 10:18:23 -06001381 }
Paolo Bonzini398205b2014-11-07 23:03:59 +01001382
Bart Van Asscheae943d22018-01-19 08:58:55 -08001383 kblockd_mod_delayed_work_on(blk_mq_hctx_next_cpu(hctx), &hctx->run_work,
1384 msecs_to_jiffies(msecs));
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001385}
1386
1387void blk_mq_delay_run_hw_queue(struct blk_mq_hw_ctx *hctx, unsigned long msecs)
1388{
1389 __blk_mq_delay_run_hw_queue(hctx, true, msecs);
1390}
1391EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);
1392
Jens Axboe79f720a2017-11-10 09:13:21 -07001393bool blk_mq_run_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
Bart Van Assche7587a5a2017-04-07 11:16:52 -07001394{
Ming Lei24f5a902018-01-06 16:27:38 +08001395 int srcu_idx;
1396 bool need_run;
1397
1398 /*
1399 * When queue is quiesced, we may be switching io scheduler, or
1400 * updating nr_hw_queues, or other things, and we can't run queue
1401 * any more, even __blk_mq_hctx_has_pending() can't be called safely.
1402 *
1403 * And queue will be rerun in blk_mq_unquiesce_queue() if it is
1404 * quiesced.
1405 */
Jens Axboe04ced152018-01-09 08:29:46 -08001406 hctx_lock(hctx, &srcu_idx);
1407 need_run = !blk_queue_quiesced(hctx->queue) &&
1408 blk_mq_hctx_has_pending(hctx);
1409 hctx_unlock(hctx, srcu_idx);
Ming Lei24f5a902018-01-06 16:27:38 +08001410
1411 if (need_run) {
Jens Axboe79f720a2017-11-10 09:13:21 -07001412 __blk_mq_delay_run_hw_queue(hctx, async, 0);
1413 return true;
1414 }
1415
1416 return false;
Jens Axboe320ae512013-10-24 09:20:05 +01001417}
Omar Sandoval5b727272017-04-14 01:00:00 -07001418EXPORT_SYMBOL(blk_mq_run_hw_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01001419
Mike Snitzerb94ec292015-03-11 23:56:38 -04001420void blk_mq_run_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001421{
1422 struct blk_mq_hw_ctx *hctx;
1423 int i;
1424
1425 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboe79f720a2017-11-10 09:13:21 -07001426 if (blk_mq_hctx_stopped(hctx))
Jens Axboe320ae512013-10-24 09:20:05 +01001427 continue;
1428
Mike Snitzerb94ec292015-03-11 23:56:38 -04001429 blk_mq_run_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001430 }
1431}
Mike Snitzerb94ec292015-03-11 23:56:38 -04001432EXPORT_SYMBOL(blk_mq_run_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01001433
Bart Van Asschefd001442016-10-28 17:19:37 -07001434/**
1435 * blk_mq_queue_stopped() - check whether one or more hctxs have been stopped
1436 * @q: request queue.
1437 *
1438 * The caller is responsible for serializing this function against
1439 * blk_mq_{start,stop}_hw_queue().
1440 */
1441bool blk_mq_queue_stopped(struct request_queue *q)
1442{
1443 struct blk_mq_hw_ctx *hctx;
1444 int i;
1445
1446 queue_for_each_hw_ctx(q, hctx, i)
1447 if (blk_mq_hctx_stopped(hctx))
1448 return true;
1449
1450 return false;
1451}
1452EXPORT_SYMBOL(blk_mq_queue_stopped);
1453
Ming Lei39a70c72017-06-06 23:22:09 +08001454/*
1455 * This function is often used for pausing .queue_rq() by driver when
1456 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001457 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001458 *
1459 * We do not guarantee that dispatch can be drained or blocked
1460 * after blk_mq_stop_hw_queue() returns. Please use
1461 * blk_mq_quiesce_queue() for that requirement.
1462 */
Jens Axboe320ae512013-10-24 09:20:05 +01001463void blk_mq_stop_hw_queue(struct blk_mq_hw_ctx *hctx)
1464{
Ming Lei641a9ed2017-06-06 23:22:10 +08001465 cancel_delayed_work(&hctx->run_work);
1466
1467 set_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboe320ae512013-10-24 09:20:05 +01001468}
1469EXPORT_SYMBOL(blk_mq_stop_hw_queue);
1470
Ming Lei39a70c72017-06-06 23:22:09 +08001471/*
1472 * This function is often used for pausing .queue_rq() by driver when
1473 * there isn't enough resource or some conditions aren't satisfied, and
Bart Van Assche4d606212017-08-17 16:23:00 -07001474 * BLK_STS_RESOURCE is usually returned.
Ming Lei39a70c72017-06-06 23:22:09 +08001475 *
1476 * We do not guarantee that dispatch can be drained or blocked
1477 * after blk_mq_stop_hw_queues() returns. Please use
1478 * blk_mq_quiesce_queue() for that requirement.
1479 */
Jens Axboe2719aa22017-05-03 11:08:14 -06001480void blk_mq_stop_hw_queues(struct request_queue *q)
1481{
Ming Lei641a9ed2017-06-06 23:22:10 +08001482 struct blk_mq_hw_ctx *hctx;
1483 int i;
1484
1485 queue_for_each_hw_ctx(q, hctx, i)
1486 blk_mq_stop_hw_queue(hctx);
Christoph Hellwig280d45f2013-10-25 14:45:58 +01001487}
1488EXPORT_SYMBOL(blk_mq_stop_hw_queues);
1489
Jens Axboe320ae512013-10-24 09:20:05 +01001490void blk_mq_start_hw_queue(struct blk_mq_hw_ctx *hctx)
1491{
1492 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
Jens Axboee4043dc2014-04-09 10:18:23 -06001493
Jens Axboe0ffbce82014-06-25 08:22:34 -06001494 blk_mq_run_hw_queue(hctx, false);
Jens Axboe320ae512013-10-24 09:20:05 +01001495}
1496EXPORT_SYMBOL(blk_mq_start_hw_queue);
1497
Christoph Hellwig2f268552014-04-16 09:44:56 +02001498void blk_mq_start_hw_queues(struct request_queue *q)
1499{
1500 struct blk_mq_hw_ctx *hctx;
1501 int i;
1502
1503 queue_for_each_hw_ctx(q, hctx, i)
1504 blk_mq_start_hw_queue(hctx);
1505}
1506EXPORT_SYMBOL(blk_mq_start_hw_queues);
1507
Jens Axboeae911c52016-12-08 13:19:30 -07001508void blk_mq_start_stopped_hw_queue(struct blk_mq_hw_ctx *hctx, bool async)
1509{
1510 if (!blk_mq_hctx_stopped(hctx))
1511 return;
1512
1513 clear_bit(BLK_MQ_S_STOPPED, &hctx->state);
1514 blk_mq_run_hw_queue(hctx, async);
1515}
1516EXPORT_SYMBOL_GPL(blk_mq_start_stopped_hw_queue);
1517
Christoph Hellwig1b4a3252014-04-16 09:44:54 +02001518void blk_mq_start_stopped_hw_queues(struct request_queue *q, bool async)
Jens Axboe320ae512013-10-24 09:20:05 +01001519{
1520 struct blk_mq_hw_ctx *hctx;
1521 int i;
1522
Jens Axboeae911c52016-12-08 13:19:30 -07001523 queue_for_each_hw_ctx(q, hctx, i)
1524 blk_mq_start_stopped_hw_queue(hctx, async);
Jens Axboe320ae512013-10-24 09:20:05 +01001525}
1526EXPORT_SYMBOL(blk_mq_start_stopped_hw_queues);
1527
Christoph Hellwig70f4db62014-04-16 10:48:08 -06001528static void blk_mq_run_work_fn(struct work_struct *work)
Jens Axboe320ae512013-10-24 09:20:05 +01001529{
1530 struct blk_mq_hw_ctx *hctx;
1531
Jens Axboe9f993732017-04-10 09:54:54 -06001532 hctx = container_of(work, struct blk_mq_hw_ctx, run_work.work);
Jens Axboe21c6e932017-04-10 09:54:56 -06001533
1534 /*
Ming Lei15fe8a902018-04-08 17:48:11 +08001535 * If we are stopped, don't run the queue.
Jens Axboe21c6e932017-04-10 09:54:56 -06001536 */
Ming Lei15fe8a902018-04-08 17:48:11 +08001537 if (test_bit(BLK_MQ_S_STOPPED, &hctx->state))
Jianchao Wang0196d6b2018-06-04 17:03:55 +08001538 return;
Jens Axboee4043dc2014-04-09 10:18:23 -06001539
Jens Axboe320ae512013-10-24 09:20:05 +01001540 __blk_mq_run_hw_queue(hctx);
1541}
1542
Ming Leicfd0c552015-10-20 23:13:57 +08001543static inline void __blk_mq_insert_req_list(struct blk_mq_hw_ctx *hctx,
Ming Leicfd0c552015-10-20 23:13:57 +08001544 struct request *rq,
1545 bool at_head)
Jens Axboe320ae512013-10-24 09:20:05 +01001546{
Jens Axboee57690f2016-08-24 15:34:35 -06001547 struct blk_mq_ctx *ctx = rq->mq_ctx;
1548
Bart Van Assche7b607812017-06-20 11:15:47 -07001549 lockdep_assert_held(&ctx->lock);
1550
Jens Axboe01b983c2013-11-19 18:59:10 -07001551 trace_block_rq_insert(hctx->queue, rq);
1552
Christoph Hellwig72a0a362014-02-07 10:22:36 -08001553 if (at_head)
1554 list_add(&rq->queuelist, &ctx->rq_list);
1555 else
1556 list_add_tail(&rq->queuelist, &ctx->rq_list);
Ming Leicfd0c552015-10-20 23:13:57 +08001557}
Jens Axboe4bb659b2014-05-09 09:36:49 -06001558
Jens Axboe2c3ad662016-12-14 14:34:47 -07001559void __blk_mq_insert_request(struct blk_mq_hw_ctx *hctx, struct request *rq,
1560 bool at_head)
Ming Leicfd0c552015-10-20 23:13:57 +08001561{
1562 struct blk_mq_ctx *ctx = rq->mq_ctx;
1563
Bart Van Assche7b607812017-06-20 11:15:47 -07001564 lockdep_assert_held(&ctx->lock);
1565
Jens Axboee57690f2016-08-24 15:34:35 -06001566 __blk_mq_insert_req_list(hctx, rq, at_head);
Jens Axboe320ae512013-10-24 09:20:05 +01001567 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001568}
1569
Jens Axboe157f3772017-09-11 16:43:57 -06001570/*
1571 * Should only be used carefully, when the caller knows we want to
1572 * bypass a potential IO scheduler on the target device.
1573 */
Ming Leib0850292017-11-02 23:24:34 +08001574void blk_mq_request_bypass_insert(struct request *rq, bool run_queue)
Jens Axboe157f3772017-09-11 16:43:57 -06001575{
1576 struct blk_mq_ctx *ctx = rq->mq_ctx;
1577 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(rq->q, ctx->cpu);
1578
1579 spin_lock(&hctx->lock);
1580 list_add_tail(&rq->queuelist, &hctx->dispatch);
1581 spin_unlock(&hctx->lock);
1582
Ming Leib0850292017-11-02 23:24:34 +08001583 if (run_queue)
1584 blk_mq_run_hw_queue(hctx, false);
Jens Axboe157f3772017-09-11 16:43:57 -06001585}
1586
Jens Axboebd166ef2017-01-17 06:03:22 -07001587void blk_mq_insert_requests(struct blk_mq_hw_ctx *hctx, struct blk_mq_ctx *ctx,
1588 struct list_head *list)
Jens Axboe320ae512013-10-24 09:20:05 +01001589
1590{
Ming Lei3f0cedc2018-07-02 17:35:58 +08001591 struct request *rq;
1592
Jens Axboe320ae512013-10-24 09:20:05 +01001593 /*
1594 * preemption doesn't flush plug list, so it's possible ctx->cpu is
1595 * offline now
1596 */
Ming Lei3f0cedc2018-07-02 17:35:58 +08001597 list_for_each_entry(rq, list, queuelist) {
Jens Axboee57690f2016-08-24 15:34:35 -06001598 BUG_ON(rq->mq_ctx != ctx);
Ming Lei3f0cedc2018-07-02 17:35:58 +08001599 trace_block_rq_insert(hctx->queue, rq);
Jens Axboe320ae512013-10-24 09:20:05 +01001600 }
Ming Lei3f0cedc2018-07-02 17:35:58 +08001601
1602 spin_lock(&ctx->lock);
1603 list_splice_tail_init(list, &ctx->rq_list);
Ming Leicfd0c552015-10-20 23:13:57 +08001604 blk_mq_hctx_mark_pending(hctx, ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01001605 spin_unlock(&ctx->lock);
Jens Axboe320ae512013-10-24 09:20:05 +01001606}
1607
1608static int plug_ctx_cmp(void *priv, struct list_head *a, struct list_head *b)
1609{
1610 struct request *rqa = container_of(a, struct request, queuelist);
1611 struct request *rqb = container_of(b, struct request, queuelist);
1612
1613 return !(rqa->mq_ctx < rqb->mq_ctx ||
1614 (rqa->mq_ctx == rqb->mq_ctx &&
1615 blk_rq_pos(rqa) < blk_rq_pos(rqb)));
1616}
1617
1618void blk_mq_flush_plug_list(struct blk_plug *plug, bool from_schedule)
1619{
1620 struct blk_mq_ctx *this_ctx;
1621 struct request_queue *this_q;
1622 struct request *rq;
1623 LIST_HEAD(list);
1624 LIST_HEAD(ctx_list);
1625 unsigned int depth;
1626
1627 list_splice_init(&plug->mq_list, &list);
1628
1629 list_sort(NULL, &list, plug_ctx_cmp);
1630
1631 this_q = NULL;
1632 this_ctx = NULL;
1633 depth = 0;
1634
1635 while (!list_empty(&list)) {
1636 rq = list_entry_rq(list.next);
1637 list_del_init(&rq->queuelist);
1638 BUG_ON(!rq->q);
1639 if (rq->mq_ctx != this_ctx) {
1640 if (this_ctx) {
Ilya Dryomov587562d2018-09-26 14:35:50 +02001641 trace_block_unplug(this_q, depth, !from_schedule);
Jens Axboebd166ef2017-01-17 06:03:22 -07001642 blk_mq_sched_insert_requests(this_q, this_ctx,
1643 &ctx_list,
1644 from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01001645 }
1646
1647 this_ctx = rq->mq_ctx;
1648 this_q = rq->q;
1649 depth = 0;
1650 }
1651
1652 depth++;
1653 list_add_tail(&rq->queuelist, &ctx_list);
1654 }
1655
1656 /*
1657 * If 'this_ctx' is set, we know we have entries to complete
1658 * on 'ctx_list'. Do those.
1659 */
1660 if (this_ctx) {
Ilya Dryomov587562d2018-09-26 14:35:50 +02001661 trace_block_unplug(this_q, depth, !from_schedule);
Jens Axboebd166ef2017-01-17 06:03:22 -07001662 blk_mq_sched_insert_requests(this_q, this_ctx, &ctx_list,
1663 from_schedule);
Jens Axboe320ae512013-10-24 09:20:05 +01001664 }
1665}
1666
1667static void blk_mq_bio_to_request(struct request *rq, struct bio *bio)
1668{
Bart Van Asscheda8d7f02017-04-19 14:01:24 -07001669 blk_init_request_from_bio(rq, bio);
Jens Axboe4b570522014-05-29 11:00:11 -06001670
Jens Axboe6e85eaf2016-12-02 20:00:14 -07001671 blk_account_io_start(rq, true);
Jens Axboe320ae512013-10-24 09:20:05 +01001672}
1673
Jens Axboefd2d3322017-01-12 10:04:45 -07001674static blk_qc_t request_to_qc_t(struct blk_mq_hw_ctx *hctx, struct request *rq)
1675{
Jens Axboebd166ef2017-01-17 06:03:22 -07001676 if (rq->tag != -1)
1677 return blk_tag_to_qc_t(rq->tag, hctx->queue_num, false);
1678
1679 return blk_tag_to_qc_t(rq->internal_tag, hctx->queue_num, true);
Jens Axboefd2d3322017-01-12 10:04:45 -07001680}
1681
Mike Snitzer0f955492018-01-17 11:25:56 -05001682static blk_status_t __blk_mq_issue_directly(struct blk_mq_hw_ctx *hctx,
1683 struct request *rq,
1684 blk_qc_t *cookie)
Shaohua Lif984df12015-05-08 10:51:32 -07001685{
Shaohua Lif984df12015-05-08 10:51:32 -07001686 struct request_queue *q = rq->q;
Shaohua Lif984df12015-05-08 10:51:32 -07001687 struct blk_mq_queue_data bd = {
1688 .rq = rq,
Omar Sandovald945a362017-04-05 12:01:36 -07001689 .last = true,
Shaohua Lif984df12015-05-08 10:51:32 -07001690 };
Jens Axboebd166ef2017-01-17 06:03:22 -07001691 blk_qc_t new_cookie;
Jens Axboef06345a2017-06-12 11:22:46 -06001692 blk_status_t ret;
Mike Snitzer0f955492018-01-17 11:25:56 -05001693
1694 new_cookie = request_to_qc_t(hctx, rq);
1695
1696 /*
1697 * For OK queue, we are done. For error, caller may kill it.
1698 * Any other error (busy), just add it to our list as we
1699 * previously would have done.
1700 */
1701 ret = q->mq_ops->queue_rq(hctx, &bd);
1702 switch (ret) {
1703 case BLK_STS_OK:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001704 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001705 *cookie = new_cookie;
1706 break;
1707 case BLK_STS_RESOURCE:
Ming Lei86ff7c22018-01-30 22:04:57 -05001708 case BLK_STS_DEV_RESOURCE:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001709 blk_mq_update_dispatch_busy(hctx, true);
Mike Snitzer0f955492018-01-17 11:25:56 -05001710 __blk_mq_requeue_request(rq);
1711 break;
1712 default:
Ming Lei6ce3dd62018-07-10 09:03:31 +08001713 blk_mq_update_dispatch_busy(hctx, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001714 *cookie = BLK_QC_T_NONE;
1715 break;
1716 }
1717
1718 return ret;
1719}
1720
Mike Snitzer0f955492018-01-17 11:25:56 -05001721static blk_status_t __blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
1722 struct request *rq,
Ming Lei396eaf22018-01-17 11:25:57 -05001723 blk_qc_t *cookie,
1724 bool bypass_insert)
Mike Snitzer0f955492018-01-17 11:25:56 -05001725{
1726 struct request_queue *q = rq->q;
Ming Leid964f042017-06-06 23:22:00 +08001727 bool run_queue = true;
1728
Ming Lei23d4ee12018-01-18 12:06:59 +08001729 /*
1730 * RCU or SRCU read lock is needed before checking quiesced flag.
1731 *
1732 * When queue is stopped or quiesced, ignore 'bypass_insert' from
Bart Van Asschec77ff7f2018-01-19 08:58:54 -08001733 * blk_mq_request_issue_directly(), and return BLK_STS_OK to caller,
Ming Lei23d4ee12018-01-18 12:06:59 +08001734 * and avoid driver to try to dispatch again.
1735 */
Ming Leif4560ff2017-06-18 14:24:27 -06001736 if (blk_mq_hctx_stopped(hctx) || blk_queue_quiesced(q)) {
Ming Leid964f042017-06-06 23:22:00 +08001737 run_queue = false;
Ming Lei23d4ee12018-01-18 12:06:59 +08001738 bypass_insert = false;
Ming Leid964f042017-06-06 23:22:00 +08001739 goto insert;
1740 }
Shaohua Lif984df12015-05-08 10:51:32 -07001741
Ming Lei396eaf22018-01-17 11:25:57 -05001742 if (q->elevator && !bypass_insert)
Bart Van Assche2253efc2016-10-28 17:20:02 -07001743 goto insert;
1744
Ming Lei0bca7992018-04-05 00:35:21 +08001745 if (!blk_mq_get_dispatch_budget(hctx))
Jens Axboebd166ef2017-01-17 06:03:22 -07001746 goto insert;
1747
Ming Lei8ab6bb9e2018-06-25 19:31:45 +08001748 if (!blk_mq_get_driver_tag(rq)) {
Ming Lei0bca7992018-04-05 00:35:21 +08001749 blk_mq_put_dispatch_budget(hctx);
Ming Leide148292017-10-14 17:22:29 +08001750 goto insert;
Ming Lei88022d72017-11-05 02:21:12 +08001751 }
Ming Leide148292017-10-14 17:22:29 +08001752
Mike Snitzer0f955492018-01-17 11:25:56 -05001753 return __blk_mq_issue_directly(hctx, rq, cookie);
Bart Van Assche2253efc2016-10-28 17:20:02 -07001754insert:
Ming Lei396eaf22018-01-17 11:25:57 -05001755 if (bypass_insert)
1756 return BLK_STS_RESOURCE;
Mike Snitzer0f955492018-01-17 11:25:56 -05001757
Ming Lei23d4ee12018-01-18 12:06:59 +08001758 blk_mq_sched_insert_request(rq, false, run_queue, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001759 return BLK_STS_OK;
Shaohua Lif984df12015-05-08 10:51:32 -07001760}
1761
Christoph Hellwig5eb61262017-03-22 15:01:51 -04001762static void blk_mq_try_issue_directly(struct blk_mq_hw_ctx *hctx,
1763 struct request *rq, blk_qc_t *cookie)
1764{
Mike Snitzer0f955492018-01-17 11:25:56 -05001765 blk_status_t ret;
Jens Axboe04ced152018-01-09 08:29:46 -08001766 int srcu_idx;
Jens Axboebf4907c2017-03-30 12:30:39 -06001767
Jens Axboe04ced152018-01-09 08:29:46 -08001768 might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);
Jens Axboebf4907c2017-03-30 12:30:39 -06001769
Jens Axboe04ced152018-01-09 08:29:46 -08001770 hctx_lock(hctx, &srcu_idx);
Mike Snitzer0f955492018-01-17 11:25:56 -05001771
Ming Lei396eaf22018-01-17 11:25:57 -05001772 ret = __blk_mq_try_issue_directly(hctx, rq, cookie, false);
Ming Lei86ff7c22018-01-30 22:04:57 -05001773 if (ret == BLK_STS_RESOURCE || ret == BLK_STS_DEV_RESOURCE)
Ming Lei23d4ee12018-01-18 12:06:59 +08001774 blk_mq_sched_insert_request(rq, false, true, false);
Mike Snitzer0f955492018-01-17 11:25:56 -05001775 else if (ret != BLK_STS_OK)
1776 blk_mq_end_request(rq, ret);
1777
Jens Axboe04ced152018-01-09 08:29:46 -08001778 hctx_unlock(hctx, srcu_idx);
Christoph Hellwig5eb61262017-03-22 15:01:51 -04001779}
1780
Bart Van Asschec77ff7f2018-01-19 08:58:54 -08001781blk_status_t blk_mq_request_issue_directly(struct request *rq)
Ming Lei396eaf22018-01-17 11:25:57 -05001782{
1783 blk_status_t ret;
1784 int srcu_idx;
1785 blk_qc_t unused_cookie;
1786 struct blk_mq_ctx *ctx = rq->mq_ctx;
1787 struct blk_mq_hw_ctx *hctx = blk_mq_map_queue(rq->q, ctx->cpu);
1788
1789 hctx_lock(hctx, &srcu_idx);
1790 ret = __blk_mq_try_issue_directly(hctx, rq, &unused_cookie, true);
1791 hctx_unlock(hctx, srcu_idx);
1792
1793 return ret;
Jens Axboe07068d52014-05-22 10:40:51 -06001794}
1795
Ming Lei6ce3dd62018-07-10 09:03:31 +08001796void blk_mq_try_issue_list_directly(struct blk_mq_hw_ctx *hctx,
1797 struct list_head *list)
1798{
1799 while (!list_empty(list)) {
1800 blk_status_t ret;
1801 struct request *rq = list_first_entry(list, struct request,
1802 queuelist);
1803
1804 list_del_init(&rq->queuelist);
1805 ret = blk_mq_request_issue_directly(rq);
1806 if (ret != BLK_STS_OK) {
Ming Lei8824f622018-07-22 14:10:15 +08001807 if (ret == BLK_STS_RESOURCE ||
1808 ret == BLK_STS_DEV_RESOURCE) {
1809 list_add(&rq->queuelist, list);
1810 break;
1811 }
1812 blk_mq_end_request(rq, ret);
Ming Lei6ce3dd62018-07-10 09:03:31 +08001813 }
1814 }
1815}
1816
Jens Axboedece1632015-11-05 10:41:16 -07001817static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
Jens Axboe07068d52014-05-22 10:40:51 -06001818{
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001819 const int is_sync = op_is_sync(bio->bi_opf);
Christoph Hellwigf73f44e2017-01-27 08:30:47 -07001820 const int is_flush_fua = op_is_flush(bio->bi_opf);
Jens Axboe5a797e02017-01-26 12:22:11 -07001821 struct blk_mq_alloc_data data = { .flags = 0 };
Jens Axboe07068d52014-05-22 10:40:51 -06001822 struct request *rq;
Christoph Hellwig5eb61262017-03-22 15:01:51 -04001823 unsigned int request_count = 0;
Shaohua Lif984df12015-05-08 10:51:32 -07001824 struct blk_plug *plug;
Shaohua Li5b3f3412015-05-08 10:51:33 -07001825 struct request *same_queue_rq = NULL;
Jens Axboe7b371632015-11-05 10:41:40 -07001826 blk_qc_t cookie;
Jens Axboe07068d52014-05-22 10:40:51 -06001827
1828 blk_queue_bounce(q, &bio);
1829
NeilBrownaf67c312017-06-18 14:38:57 +10001830 blk_queue_split(q, &bio);
Wen Xiongf36ea502017-05-10 08:54:11 -05001831
Dmitry Monakhove23947b2017-06-29 11:31:11 -07001832 if (!bio_integrity_prep(bio))
Jens Axboedece1632015-11-05 10:41:16 -07001833 return BLK_QC_T_NONE;
Jens Axboe07068d52014-05-22 10:40:51 -06001834
Omar Sandoval87c279e2016-06-01 22:18:48 -07001835 if (!is_flush_fua && !blk_queue_nomerges(q) &&
1836 blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
1837 return BLK_QC_T_NONE;
Shaohua Lif984df12015-05-08 10:51:32 -07001838
Jens Axboebd166ef2017-01-17 06:03:22 -07001839 if (blk_mq_sched_bio_merge(q, bio))
1840 return BLK_QC_T_NONE;
1841
Josef Bacikc1c80382018-07-03 11:14:59 -04001842 rq_qos_throttle(q, bio, NULL);
Jens Axboe87760e52016-11-09 12:38:14 -07001843
Christoph Hellwigd2c0d382017-06-16 18:15:19 +02001844 rq = blk_mq_get_request(q, bio, bio->bi_opf, &data);
Jens Axboe87760e52016-11-09 12:38:14 -07001845 if (unlikely(!rq)) {
Josef Bacikc1c80382018-07-03 11:14:59 -04001846 rq_qos_cleanup(q, bio);
Goldwyn Rodrigues03a07c92017-06-20 07:05:46 -05001847 if (bio->bi_opf & REQ_NOWAIT)
1848 bio_wouldblock_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001849 return BLK_QC_T_NONE;
Jens Axboe87760e52016-11-09 12:38:14 -07001850 }
1851
Xiaoguang Wangd6f1dda2018-10-23 22:30:50 +08001852 trace_block_getrq(q, bio, bio->bi_opf);
1853
Josef Bacikc1c80382018-07-03 11:14:59 -04001854 rq_qos_track(q, rq, bio);
Jens Axboe07068d52014-05-22 10:40:51 -06001855
Jens Axboefd2d3322017-01-12 10:04:45 -07001856 cookie = request_to_qc_t(data.hctx, rq);
Jens Axboe07068d52014-05-22 10:40:51 -06001857
Shaohua Lif984df12015-05-08 10:51:32 -07001858 plug = current->plug;
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001859 if (unlikely(is_flush_fua)) {
Shaohua Lif984df12015-05-08 10:51:32 -07001860 blk_mq_put_ctx(data.ctx);
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001861 blk_mq_bio_to_request(rq, bio);
Ming Lei923218f2017-11-02 23:24:38 +08001862
1863 /* bypass scheduler for flush rq */
1864 blk_insert_flush(rq);
1865 blk_mq_run_hw_queue(data.hctx, true);
Christoph Hellwiga4d907b2017-03-22 15:01:53 -04001866 } else if (plug && q->nr_hw_queues == 1) {
Shaohua Li600271d2016-11-03 17:03:54 -07001867 struct request *last = NULL;
1868
Jens Axboeb00c53e2017-04-20 16:40:36 -06001869 blk_mq_put_ctx(data.ctx);
Jeff Moyere6c44382015-05-08 10:51:30 -07001870 blk_mq_bio_to_request(rq, bio);
Ming Lei0a6219a2016-11-16 18:07:05 +08001871
1872 /*
1873 * @request_count may become stale because of schedule
1874 * out, so check the list again.
1875 */
1876 if (list_empty(&plug->mq_list))
1877 request_count = 0;
Christoph Hellwig254d2592017-03-22 15:01:50 -04001878 else if (blk_queue_nomerges(q))
1879 request_count = blk_plug_queued_count(q);
1880
Ming Lei676d0602015-10-20 23:13:56 +08001881 if (!request_count)
Jeff Moyere6c44382015-05-08 10:51:30 -07001882 trace_block_plug(q);
Shaohua Li600271d2016-11-03 17:03:54 -07001883 else
1884 last = list_entry_rq(plug->mq_list.prev);
Jens Axboeb094f892015-11-20 20:29:45 -07001885
Shaohua Li600271d2016-11-03 17:03:54 -07001886 if (request_count >= BLK_MAX_REQUEST_COUNT || (last &&
1887 blk_rq_bytes(last) >= BLK_PLUG_FLUSH_SIZE)) {
Jeff Moyere6c44382015-05-08 10:51:30 -07001888 blk_flush_plug_list(plug, false);
1889 trace_block_plug(q);
Jens Axboe320ae512013-10-24 09:20:05 +01001890 }
Jens Axboeb094f892015-11-20 20:29:45 -07001891
Jeff Moyere6c44382015-05-08 10:51:30 -07001892 list_add_tail(&rq->queuelist, &plug->mq_list);
Christoph Hellwig22997222017-03-22 15:01:52 -04001893 } else if (plug && !blk_queue_nomerges(q)) {
Jens Axboe320ae512013-10-24 09:20:05 +01001894 blk_mq_bio_to_request(rq, bio);
Jens Axboe320ae512013-10-24 09:20:05 +01001895
Jens Axboe320ae512013-10-24 09:20:05 +01001896 /*
1897 * We do limited plugging. If the bio can be merged, do that.
1898 * Otherwise the existing request in the plug list will be
1899 * issued. So the plug list will have one request at most
Christoph Hellwig22997222017-03-22 15:01:52 -04001900 * The plug list might get flushed before this. If that happens,
1901 * the plug list is empty, and same_queue_rq is invalid.
Jens Axboe320ae512013-10-24 09:20:05 +01001902 */
Christoph Hellwig22997222017-03-22 15:01:52 -04001903 if (list_empty(&plug->mq_list))
1904 same_queue_rq = NULL;
1905 if (same_queue_rq)
1906 list_del_init(&same_queue_rq->queuelist);
1907 list_add_tail(&rq->queuelist, &plug->mq_list);
1908
Jens Axboebf4907c2017-03-30 12:30:39 -06001909 blk_mq_put_ctx(data.ctx);
1910
Ming Leidad7a3b2017-06-06 23:21:59 +08001911 if (same_queue_rq) {
1912 data.hctx = blk_mq_map_queue(q,
1913 same_queue_rq->mq_ctx->cpu);
Christoph Hellwig22997222017-03-22 15:01:52 -04001914 blk_mq_try_issue_directly(data.hctx, same_queue_rq,
1915 &cookie);
Ming Leidad7a3b2017-06-06 23:21:59 +08001916 }
Ming Lei6ce3dd62018-07-10 09:03:31 +08001917 } else if ((q->nr_hw_queues > 1 && is_sync) || (!q->elevator &&
1918 !data.hctx->dispatch_busy)) {
Jens Axboebd166ef2017-01-17 06:03:22 -07001919 blk_mq_put_ctx(data.ctx);
1920 blk_mq_bio_to_request(rq, bio);
Christoph Hellwig22997222017-03-22 15:01:52 -04001921 blk_mq_try_issue_directly(data.hctx, rq, &cookie);
Ming Leiab42f352017-05-26 19:53:19 +08001922 } else {
Jens Axboeb00c53e2017-04-20 16:40:36 -06001923 blk_mq_put_ctx(data.ctx);
Ming Leiab42f352017-05-26 19:53:19 +08001924 blk_mq_bio_to_request(rq, bio);
huhai8fa9f552018-05-16 08:21:21 -06001925 blk_mq_sched_insert_request(rq, false, true, true);
Ming Leiab42f352017-05-26 19:53:19 +08001926 }
Jens Axboe320ae512013-10-24 09:20:05 +01001927
Jens Axboe7b371632015-11-05 10:41:40 -07001928 return cookie;
Jens Axboe320ae512013-10-24 09:20:05 +01001929}
1930
Jens Axboecc71a6f2017-01-11 14:29:56 -07001931void blk_mq_free_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
1932 unsigned int hctx_idx)
Jens Axboe320ae512013-10-24 09:20:05 +01001933{
1934 struct page *page;
1935
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001936 if (tags->rqs && set->ops->exit_request) {
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001937 int i;
1938
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001939 for (i = 0; i < tags->nr_tags; i++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07001940 struct request *rq = tags->static_rqs[i];
1941
1942 if (!rq)
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001943 continue;
Christoph Hellwigd6296d392017-05-01 10:19:08 -06001944 set->ops->exit_request(set, rq, hctx_idx);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07001945 tags->static_rqs[i] = NULL;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06001946 }
1947 }
1948
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001949 while (!list_empty(&tags->page_list)) {
1950 page = list_first_entry(&tags->page_list, struct page, lru);
Dave Hansen67534712014-01-08 20:17:46 -07001951 list_del_init(&page->lru);
Catalin Marinasf75782e2015-09-14 18:16:02 +01001952 /*
1953 * Remove kmemleak object previously allocated in
1954 * blk_mq_init_rq_map().
1955 */
1956 kmemleak_free(page_address(page));
Jens Axboe320ae512013-10-24 09:20:05 +01001957 __free_pages(page, page->private);
1958 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07001959}
Jens Axboe320ae512013-10-24 09:20:05 +01001960
Jens Axboecc71a6f2017-01-11 14:29:56 -07001961void blk_mq_free_rq_map(struct blk_mq_tags *tags)
1962{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001963 kfree(tags->rqs);
Jens Axboecc71a6f2017-01-11 14:29:56 -07001964 tags->rqs = NULL;
Jens Axboe2af8cbe2017-01-13 14:39:30 -07001965 kfree(tags->static_rqs);
1966 tags->static_rqs = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001967
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001968 blk_mq_free_tags(tags);
Jens Axboe320ae512013-10-24 09:20:05 +01001969}
1970
Jens Axboecc71a6f2017-01-11 14:29:56 -07001971struct blk_mq_tags *blk_mq_alloc_rq_map(struct blk_mq_tag_set *set,
1972 unsigned int hctx_idx,
1973 unsigned int nr_tags,
1974 unsigned int reserved_tags)
Jens Axboe320ae512013-10-24 09:20:05 +01001975{
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001976 struct blk_mq_tags *tags;
Shaohua Li59f082e2017-02-01 09:53:14 -08001977 int node;
Jens Axboe320ae512013-10-24 09:20:05 +01001978
Shaohua Li59f082e2017-02-01 09:53:14 -08001979 node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx);
1980 if (node == NUMA_NO_NODE)
1981 node = set->numa_node;
1982
1983 tags = blk_mq_init_tags(nr_tags, reserved_tags, node,
Shaohua Li24391c02015-01-23 14:18:00 -07001984 BLK_MQ_FLAG_TO_ALLOC_POLICY(set->flags));
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001985 if (!tags)
1986 return NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01001987
Kees Cook590b5b72018-06-12 14:04:20 -07001988 tags->rqs = kcalloc_node(nr_tags, sizeof(struct request *),
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02001989 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
Shaohua Li59f082e2017-02-01 09:53:14 -08001990 node);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06001991 if (!tags->rqs) {
1992 blk_mq_free_tags(tags);
1993 return NULL;
1994 }
Jens Axboe320ae512013-10-24 09:20:05 +01001995
Kees Cook590b5b72018-06-12 14:04:20 -07001996 tags->static_rqs = kcalloc_node(nr_tags, sizeof(struct request *),
1997 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
1998 node);
Jens Axboe2af8cbe2017-01-13 14:39:30 -07001999 if (!tags->static_rqs) {
2000 kfree(tags->rqs);
2001 blk_mq_free_tags(tags);
2002 return NULL;
2003 }
2004
Jens Axboecc71a6f2017-01-11 14:29:56 -07002005 return tags;
2006}
2007
2008static size_t order_to_size(unsigned int order)
2009{
2010 return (size_t)PAGE_SIZE << order;
2011}
2012
Tejun Heo1d9bd512018-01-09 08:29:48 -08002013static int blk_mq_init_request(struct blk_mq_tag_set *set, struct request *rq,
2014 unsigned int hctx_idx, int node)
2015{
2016 int ret;
2017
2018 if (set->ops->init_request) {
2019 ret = set->ops->init_request(set, rq, hctx_idx, node);
2020 if (ret)
2021 return ret;
2022 }
2023
Keith Busch12f5b932018-05-29 15:52:28 +02002024 WRITE_ONCE(rq->state, MQ_RQ_IDLE);
Tejun Heo1d9bd512018-01-09 08:29:48 -08002025 return 0;
2026}
2027
Jens Axboecc71a6f2017-01-11 14:29:56 -07002028int blk_mq_alloc_rqs(struct blk_mq_tag_set *set, struct blk_mq_tags *tags,
2029 unsigned int hctx_idx, unsigned int depth)
2030{
2031 unsigned int i, j, entries_per_page, max_order = 4;
2032 size_t rq_size, left;
Shaohua Li59f082e2017-02-01 09:53:14 -08002033 int node;
2034
2035 node = blk_mq_hw_queue_to_node(set->mq_map, hctx_idx);
2036 if (node == NUMA_NO_NODE)
2037 node = set->numa_node;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002038
2039 INIT_LIST_HEAD(&tags->page_list);
2040
Jens Axboe320ae512013-10-24 09:20:05 +01002041 /*
2042 * rq_size is the size of the request plus driver payload, rounded
2043 * to the cacheline size
2044 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002045 rq_size = round_up(sizeof(struct request) + set->cmd_size,
Jens Axboe320ae512013-10-24 09:20:05 +01002046 cache_line_size());
Jens Axboecc71a6f2017-01-11 14:29:56 -07002047 left = rq_size * depth;
Jens Axboe320ae512013-10-24 09:20:05 +01002048
Jens Axboecc71a6f2017-01-11 14:29:56 -07002049 for (i = 0; i < depth; ) {
Jens Axboe320ae512013-10-24 09:20:05 +01002050 int this_order = max_order;
2051 struct page *page;
2052 int to_do;
2053 void *p;
2054
Bartlomiej Zolnierkiewiczb3a834b2016-05-16 09:54:47 -06002055 while (this_order && left < order_to_size(this_order - 1))
Jens Axboe320ae512013-10-24 09:20:05 +01002056 this_order--;
2057
2058 do {
Shaohua Li59f082e2017-02-01 09:53:14 -08002059 page = alloc_pages_node(node,
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002060 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO,
Jens Axboea5164402014-09-10 09:02:03 -06002061 this_order);
Jens Axboe320ae512013-10-24 09:20:05 +01002062 if (page)
2063 break;
2064 if (!this_order--)
2065 break;
2066 if (order_to_size(this_order) < rq_size)
2067 break;
2068 } while (1);
2069
2070 if (!page)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002071 goto fail;
Jens Axboe320ae512013-10-24 09:20:05 +01002072
2073 page->private = this_order;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002074 list_add_tail(&page->lru, &tags->page_list);
Jens Axboe320ae512013-10-24 09:20:05 +01002075
2076 p = page_address(page);
Catalin Marinasf75782e2015-09-14 18:16:02 +01002077 /*
2078 * Allow kmemleak to scan these pages as they contain pointers
2079 * to additional allocations like via ops->init_request().
2080 */
Gabriel Krisman Bertazi36e1f3d12016-12-06 13:31:44 -02002081 kmemleak_alloc(p, order_to_size(this_order), 1, GFP_NOIO);
Jens Axboe320ae512013-10-24 09:20:05 +01002082 entries_per_page = order_to_size(this_order) / rq_size;
Jens Axboecc71a6f2017-01-11 14:29:56 -07002083 to_do = min(entries_per_page, depth - i);
Jens Axboe320ae512013-10-24 09:20:05 +01002084 left -= to_do * rq_size;
2085 for (j = 0; j < to_do; j++) {
Jens Axboe2af8cbe2017-01-13 14:39:30 -07002086 struct request *rq = p;
2087
2088 tags->static_rqs[i] = rq;
Tejun Heo1d9bd512018-01-09 08:29:48 -08002089 if (blk_mq_init_request(set, rq, hctx_idx, node)) {
2090 tags->static_rqs[i] = NULL;
2091 goto fail;
Christoph Hellwige9b267d2014-04-15 13:59:10 -06002092 }
2093
Jens Axboe320ae512013-10-24 09:20:05 +01002094 p += rq_size;
2095 i++;
2096 }
2097 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002098 return 0;
Jens Axboe320ae512013-10-24 09:20:05 +01002099
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002100fail:
Jens Axboecc71a6f2017-01-11 14:29:56 -07002101 blk_mq_free_rqs(set, tags, hctx_idx);
2102 return -ENOMEM;
Jens Axboe320ae512013-10-24 09:20:05 +01002103}
2104
Jens Axboee57690f2016-08-24 15:34:35 -06002105/*
2106 * 'cpu' is going away. splice any existing rq_list entries from this
2107 * software queue to the hw queue dispatch list, and ensure that it
2108 * gets run.
2109 */
Thomas Gleixner9467f852016-09-22 08:05:17 -06002110static int blk_mq_hctx_notify_dead(unsigned int cpu, struct hlist_node *node)
Jens Axboe484b4062014-05-21 14:01:15 -06002111{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002112 struct blk_mq_hw_ctx *hctx;
Jens Axboe484b4062014-05-21 14:01:15 -06002113 struct blk_mq_ctx *ctx;
2114 LIST_HEAD(tmp);
2115
Thomas Gleixner9467f852016-09-22 08:05:17 -06002116 hctx = hlist_entry_safe(node, struct blk_mq_hw_ctx, cpuhp_dead);
Jens Axboee57690f2016-08-24 15:34:35 -06002117 ctx = __blk_mq_get_ctx(hctx->queue, cpu);
Jens Axboe484b4062014-05-21 14:01:15 -06002118
2119 spin_lock(&ctx->lock);
2120 if (!list_empty(&ctx->rq_list)) {
2121 list_splice_init(&ctx->rq_list, &tmp);
2122 blk_mq_hctx_clear_pending(hctx, ctx);
2123 }
2124 spin_unlock(&ctx->lock);
2125
2126 if (list_empty(&tmp))
Thomas Gleixner9467f852016-09-22 08:05:17 -06002127 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002128
Jens Axboee57690f2016-08-24 15:34:35 -06002129 spin_lock(&hctx->lock);
2130 list_splice_tail_init(&tmp, &hctx->dispatch);
2131 spin_unlock(&hctx->lock);
Jens Axboe484b4062014-05-21 14:01:15 -06002132
2133 blk_mq_run_hw_queue(hctx, true);
Thomas Gleixner9467f852016-09-22 08:05:17 -06002134 return 0;
Jens Axboe484b4062014-05-21 14:01:15 -06002135}
2136
Thomas Gleixner9467f852016-09-22 08:05:17 -06002137static void blk_mq_remove_cpuhp(struct blk_mq_hw_ctx *hctx)
Jens Axboe484b4062014-05-21 14:01:15 -06002138{
Thomas Gleixner9467f852016-09-22 08:05:17 -06002139 cpuhp_state_remove_instance_nocalls(CPUHP_BLK_MQ_DEAD,
2140 &hctx->cpuhp_dead);
Jens Axboe484b4062014-05-21 14:01:15 -06002141}
2142
Ming Leic3b4afc2015-06-04 22:25:04 +08002143/* hctx->ctxs will be freed in queue's release handler */
Ming Lei08e98fc2014-09-25 23:23:38 +08002144static void blk_mq_exit_hctx(struct request_queue *q,
2145 struct blk_mq_tag_set *set,
2146 struct blk_mq_hw_ctx *hctx, unsigned int hctx_idx)
2147{
Ming Lei8ab0b7d2018-01-09 21:28:29 +08002148 if (blk_mq_hw_queue_mapped(hctx))
2149 blk_mq_tag_idle(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002150
Ming Leif70ced02014-09-25 23:23:47 +08002151 if (set->ops->exit_request)
Christoph Hellwigd6296d392017-05-01 10:19:08 -06002152 set->ops->exit_request(set, hctx->fq->flush_rq, hctx_idx);
Ming Leif70ced02014-09-25 23:23:47 +08002153
Ming Lei08e98fc2014-09-25 23:23:38 +08002154 if (set->ops->exit_hctx)
2155 set->ops->exit_hctx(hctx, hctx_idx);
2156
Bart Van Assche6a83e742016-11-02 10:09:51 -06002157 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -08002158 cleanup_srcu_struct(hctx->srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -06002159
Thomas Gleixner9467f852016-09-22 08:05:17 -06002160 blk_mq_remove_cpuhp(hctx);
Ming Leif70ced02014-09-25 23:23:47 +08002161 blk_free_flush_queue(hctx->fq);
Omar Sandoval88459642016-09-17 08:38:44 -06002162 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08002163}
2164
Ming Lei624dbe42014-05-27 23:35:13 +08002165static void blk_mq_exit_hw_queues(struct request_queue *q,
2166 struct blk_mq_tag_set *set, int nr_queue)
2167{
2168 struct blk_mq_hw_ctx *hctx;
2169 unsigned int i;
2170
2171 queue_for_each_hw_ctx(q, hctx, i) {
2172 if (i == nr_queue)
2173 break;
Jianchao Wang477e19d2018-10-12 18:07:25 +08002174 blk_mq_debugfs_unregister_hctx(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002175 blk_mq_exit_hctx(q, set, hctx, i);
Ming Lei624dbe42014-05-27 23:35:13 +08002176 }
Ming Lei624dbe42014-05-27 23:35:13 +08002177}
2178
Ming Lei08e98fc2014-09-25 23:23:38 +08002179static int blk_mq_init_hctx(struct request_queue *q,
2180 struct blk_mq_tag_set *set,
2181 struct blk_mq_hw_ctx *hctx, unsigned hctx_idx)
2182{
2183 int node;
2184
2185 node = hctx->numa_node;
2186 if (node == NUMA_NO_NODE)
2187 node = hctx->numa_node = set->numa_node;
2188
Jens Axboe9f993732017-04-10 09:54:54 -06002189 INIT_DELAYED_WORK(&hctx->run_work, blk_mq_run_work_fn);
Ming Lei08e98fc2014-09-25 23:23:38 +08002190 spin_lock_init(&hctx->lock);
2191 INIT_LIST_HEAD(&hctx->dispatch);
2192 hctx->queue = q;
Jeff Moyer2404e602015-11-03 10:40:06 -05002193 hctx->flags = set->flags & ~BLK_MQ_F_TAG_SHARED;
Ming Lei08e98fc2014-09-25 23:23:38 +08002194
Thomas Gleixner9467f852016-09-22 08:05:17 -06002195 cpuhp_state_add_instance_nocalls(CPUHP_BLK_MQ_DEAD, &hctx->cpuhp_dead);
Ming Lei08e98fc2014-09-25 23:23:38 +08002196
2197 hctx->tags = set->tags[hctx_idx];
2198
2199 /*
2200 * Allocate space for all possible cpus to avoid allocation at
2201 * runtime
2202 */
Johannes Thumshirnd904bfa2017-11-15 17:32:33 -08002203 hctx->ctxs = kmalloc_array_node(nr_cpu_ids, sizeof(void *),
Jianchao Wang5b202852018-10-12 18:07:26 +08002204 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, node);
Ming Lei08e98fc2014-09-25 23:23:38 +08002205 if (!hctx->ctxs)
2206 goto unregister_cpu_notifier;
2207
Jianchao Wang5b202852018-10-12 18:07:26 +08002208 if (sbitmap_init_node(&hctx->ctx_map, nr_cpu_ids, ilog2(8),
2209 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY, node))
Ming Lei08e98fc2014-09-25 23:23:38 +08002210 goto free_ctxs;
2211
2212 hctx->nr_ctx = 0;
2213
Ming Lei5815839b2018-06-25 19:31:47 +08002214 spin_lock_init(&hctx->dispatch_wait_lock);
Jens Axboeeb619fd2017-11-09 08:32:43 -07002215 init_waitqueue_func_entry(&hctx->dispatch_wait, blk_mq_dispatch_wake);
2216 INIT_LIST_HEAD(&hctx->dispatch_wait.entry);
2217
Ming Lei08e98fc2014-09-25 23:23:38 +08002218 if (set->ops->init_hctx &&
2219 set->ops->init_hctx(hctx, set->driver_data, hctx_idx))
2220 goto free_bitmap;
2221
Jianchao Wang5b202852018-10-12 18:07:26 +08002222 hctx->fq = blk_alloc_flush_queue(q, hctx->numa_node, set->cmd_size,
2223 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
Ming Leif70ced02014-09-25 23:23:47 +08002224 if (!hctx->fq)
Jianchao Wangd48ece22018-08-21 15:15:03 +08002225 goto exit_hctx;
Ming Leif70ced02014-09-25 23:23:47 +08002226
Tejun Heo1d9bd512018-01-09 08:29:48 -08002227 if (blk_mq_init_request(set, hctx->fq->flush_rq, hctx_idx, node))
Ming Leif70ced02014-09-25 23:23:47 +08002228 goto free_fq;
2229
Bart Van Assche6a83e742016-11-02 10:09:51 -06002230 if (hctx->flags & BLK_MQ_F_BLOCKING)
Tejun Heo05707b62018-01-09 08:29:53 -08002231 init_srcu_struct(hctx->srcu);
Bart Van Assche6a83e742016-11-02 10:09:51 -06002232
Ming Lei08e98fc2014-09-25 23:23:38 +08002233 return 0;
2234
Ming Leif70ced02014-09-25 23:23:47 +08002235 free_fq:
2236 kfree(hctx->fq);
2237 exit_hctx:
2238 if (set->ops->exit_hctx)
2239 set->ops->exit_hctx(hctx, hctx_idx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002240 free_bitmap:
Omar Sandoval88459642016-09-17 08:38:44 -06002241 sbitmap_free(&hctx->ctx_map);
Ming Lei08e98fc2014-09-25 23:23:38 +08002242 free_ctxs:
2243 kfree(hctx->ctxs);
2244 unregister_cpu_notifier:
Thomas Gleixner9467f852016-09-22 08:05:17 -06002245 blk_mq_remove_cpuhp(hctx);
Ming Lei08e98fc2014-09-25 23:23:38 +08002246 return -1;
2247}
2248
Jens Axboe320ae512013-10-24 09:20:05 +01002249static void blk_mq_init_cpu_queues(struct request_queue *q,
2250 unsigned int nr_hw_queues)
2251{
2252 unsigned int i;
2253
2254 for_each_possible_cpu(i) {
2255 struct blk_mq_ctx *__ctx = per_cpu_ptr(q->queue_ctx, i);
2256 struct blk_mq_hw_ctx *hctx;
2257
Jens Axboe320ae512013-10-24 09:20:05 +01002258 __ctx->cpu = i;
2259 spin_lock_init(&__ctx->lock);
2260 INIT_LIST_HEAD(&__ctx->rq_list);
2261 __ctx->queue = q;
2262
Jens Axboe320ae512013-10-24 09:20:05 +01002263 /*
2264 * Set local node, IFF we have more than one hw queue. If
2265 * not, we remain on the home node of the device
2266 */
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08002267 hctx = blk_mq_map_queue(q, i);
Jens Axboe320ae512013-10-24 09:20:05 +01002268 if (nr_hw_queues > 1 && hctx->numa_node == NUMA_NO_NODE)
Raghavendra K Tbffed452015-12-02 16:59:05 +05302269 hctx->numa_node = local_memory_node(cpu_to_node(i));
Jens Axboe320ae512013-10-24 09:20:05 +01002270 }
2271}
2272
Jens Axboecc71a6f2017-01-11 14:29:56 -07002273static bool __blk_mq_alloc_rq_map(struct blk_mq_tag_set *set, int hctx_idx)
2274{
2275 int ret = 0;
2276
2277 set->tags[hctx_idx] = blk_mq_alloc_rq_map(set, hctx_idx,
2278 set->queue_depth, set->reserved_tags);
2279 if (!set->tags[hctx_idx])
2280 return false;
2281
2282 ret = blk_mq_alloc_rqs(set, set->tags[hctx_idx], hctx_idx,
2283 set->queue_depth);
2284 if (!ret)
2285 return true;
2286
2287 blk_mq_free_rq_map(set->tags[hctx_idx]);
2288 set->tags[hctx_idx] = NULL;
2289 return false;
2290}
2291
2292static void blk_mq_free_map_and_requests(struct blk_mq_tag_set *set,
2293 unsigned int hctx_idx)
2294{
Jens Axboebd166ef2017-01-17 06:03:22 -07002295 if (set->tags[hctx_idx]) {
2296 blk_mq_free_rqs(set, set->tags[hctx_idx], hctx_idx);
2297 blk_mq_free_rq_map(set->tags[hctx_idx]);
2298 set->tags[hctx_idx] = NULL;
2299 }
Jens Axboecc71a6f2017-01-11 14:29:56 -07002300}
2301
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002302static void blk_mq_map_swqueue(struct request_queue *q)
Jens Axboe320ae512013-10-24 09:20:05 +01002303{
Ming Lei4412efe2018-04-25 04:01:44 +08002304 unsigned int i, hctx_idx;
Jens Axboe320ae512013-10-24 09:20:05 +01002305 struct blk_mq_hw_ctx *hctx;
2306 struct blk_mq_ctx *ctx;
Ming Lei2a34c082015-04-21 10:00:20 +08002307 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002308
Akinobu Mita60de0742015-09-27 02:09:25 +09002309 /*
2310 * Avoid others reading imcomplete hctx->cpumask through sysfs
2311 */
2312 mutex_lock(&q->sysfs_lock);
2313
Jens Axboe320ae512013-10-24 09:20:05 +01002314 queue_for_each_hw_ctx(q, hctx, i) {
Jens Axboee4043dc2014-04-09 10:18:23 -06002315 cpumask_clear(hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01002316 hctx->nr_ctx = 0;
huhaid416c922018-05-18 08:32:30 -06002317 hctx->dispatch_from = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002318 }
2319
2320 /*
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002321 * Map software to hardware queues.
Ming Lei4412efe2018-04-25 04:01:44 +08002322 *
2323 * If the cpu isn't present, the cpu is mapped to first hctx.
Jens Axboe320ae512013-10-24 09:20:05 +01002324 */
Christoph Hellwig20e4d8132018-01-12 10:53:06 +08002325 for_each_possible_cpu(i) {
Ming Lei4412efe2018-04-25 04:01:44 +08002326 hctx_idx = q->mq_map[i];
2327 /* unmapped hw queue can be remapped after CPU topo changed */
2328 if (!set->tags[hctx_idx] &&
2329 !__blk_mq_alloc_rq_map(set, hctx_idx)) {
2330 /*
2331 * If tags initialization fail for some hctx,
2332 * that hctx won't be brought online. In this
2333 * case, remap the current ctx to hctx[0] which
2334 * is guaranteed to always have tags allocated
2335 */
2336 q->mq_map[i] = 0;
2337 }
2338
Thomas Gleixner897bb0c2016-03-19 11:30:33 +01002339 ctx = per_cpu_ptr(q->queue_ctx, i);
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02002340 hctx = blk_mq_map_queue(q, i);
Keith Busch868f2f02015-12-17 17:08:14 -07002341
Jens Axboee4043dc2014-04-09 10:18:23 -06002342 cpumask_set_cpu(i, hctx->cpumask);
Jens Axboe320ae512013-10-24 09:20:05 +01002343 ctx->index_hw = hctx->nr_ctx;
2344 hctx->ctxs[hctx->nr_ctx++] = ctx;
2345 }
Jens Axboe506e9312014-05-07 10:26:44 -06002346
Akinobu Mita60de0742015-09-27 02:09:25 +09002347 mutex_unlock(&q->sysfs_lock);
2348
Jens Axboe506e9312014-05-07 10:26:44 -06002349 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei4412efe2018-04-25 04:01:44 +08002350 /*
2351 * If no software queues are mapped to this hardware queue,
2352 * disable it and free the request entries.
2353 */
2354 if (!hctx->nr_ctx) {
2355 /* Never unmap queue 0. We need it as a
2356 * fallback in case of a new remap fails
2357 * allocation
2358 */
2359 if (i && set->tags[i])
2360 blk_mq_free_map_and_requests(set, i);
2361
2362 hctx->tags = NULL;
2363 continue;
2364 }
Jens Axboe484b4062014-05-21 14:01:15 -06002365
Ming Lei2a34c082015-04-21 10:00:20 +08002366 hctx->tags = set->tags[i];
2367 WARN_ON(!hctx->tags);
2368
Jens Axboe484b4062014-05-21 14:01:15 -06002369 /*
Chong Yuan889fa312015-04-15 11:39:29 -06002370 * Set the map size to the number of mapped software queues.
2371 * This is more accurate and more efficient than looping
2372 * over all possibly mapped software queues.
2373 */
Omar Sandoval88459642016-09-17 08:38:44 -06002374 sbitmap_resize(&hctx->ctx_map, hctx->nr_ctx);
Chong Yuan889fa312015-04-15 11:39:29 -06002375
2376 /*
Jens Axboe484b4062014-05-21 14:01:15 -06002377 * Initialize batch roundrobin counts
2378 */
Ming Leif82ddf12018-04-08 17:48:10 +08002379 hctx->next_cpu = blk_mq_first_mapped_cpu(hctx);
Jens Axboe506e9312014-05-07 10:26:44 -06002380 hctx->next_cpu_batch = BLK_MQ_CPU_WORK_BATCH;
2381 }
Jens Axboe320ae512013-10-24 09:20:05 +01002382}
2383
Jens Axboe8e8320c2017-06-20 17:56:13 -06002384/*
2385 * Caller needs to ensure that we're either frozen/quiesced, or that
2386 * the queue isn't live yet.
2387 */
Jeff Moyer2404e602015-11-03 10:40:06 -05002388static void queue_set_hctx_shared(struct request_queue *q, bool shared)
Jens Axboe0d2602c2014-05-13 15:10:52 -06002389{
2390 struct blk_mq_hw_ctx *hctx;
Jens Axboe0d2602c2014-05-13 15:10:52 -06002391 int i;
2392
Jeff Moyer2404e602015-11-03 10:40:06 -05002393 queue_for_each_hw_ctx(q, hctx, i) {
Ming Lei97889f92018-06-25 19:31:48 +08002394 if (shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05002395 hctx->flags |= BLK_MQ_F_TAG_SHARED;
Ming Lei97889f92018-06-25 19:31:48 +08002396 else
Jeff Moyer2404e602015-11-03 10:40:06 -05002397 hctx->flags &= ~BLK_MQ_F_TAG_SHARED;
2398 }
2399}
2400
Jens Axboe8e8320c2017-06-20 17:56:13 -06002401static void blk_mq_update_tag_set_depth(struct blk_mq_tag_set *set,
2402 bool shared)
Jeff Moyer2404e602015-11-03 10:40:06 -05002403{
2404 struct request_queue *q;
Jens Axboe0d2602c2014-05-13 15:10:52 -06002405
Bart Van Assche705cda92017-04-07 11:16:49 -07002406 lockdep_assert_held(&set->tag_list_lock);
2407
Jens Axboe0d2602c2014-05-13 15:10:52 -06002408 list_for_each_entry(q, &set->tag_list, tag_set_list) {
2409 blk_mq_freeze_queue(q);
Jeff Moyer2404e602015-11-03 10:40:06 -05002410 queue_set_hctx_shared(q, shared);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002411 blk_mq_unfreeze_queue(q);
2412 }
2413}
2414
2415static void blk_mq_del_queue_tag_set(struct request_queue *q)
2416{
2417 struct blk_mq_tag_set *set = q->tag_set;
2418
Jens Axboe0d2602c2014-05-13 15:10:52 -06002419 mutex_lock(&set->tag_list_lock);
Bart Van Assche705cda92017-04-07 11:16:49 -07002420 list_del_rcu(&q->tag_set_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05002421 if (list_is_singular(&set->tag_list)) {
2422 /* just transitioned to unshared */
2423 set->flags &= ~BLK_MQ_F_TAG_SHARED;
2424 /* update existing queue */
2425 blk_mq_update_tag_set_depth(set, false);
2426 }
Jens Axboe0d2602c2014-05-13 15:10:52 -06002427 mutex_unlock(&set->tag_list_lock);
Roman Pena347c7a2018-06-10 22:38:24 +02002428 INIT_LIST_HEAD(&q->tag_set_list);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002429}
2430
2431static void blk_mq_add_queue_tag_set(struct blk_mq_tag_set *set,
2432 struct request_queue *q)
2433{
2434 q->tag_set = set;
2435
2436 mutex_lock(&set->tag_list_lock);
Jeff Moyer2404e602015-11-03 10:40:06 -05002437
Jens Axboeff821d22017-11-10 22:05:12 -07002438 /*
2439 * Check to see if we're transitioning to shared (from 1 to 2 queues).
2440 */
2441 if (!list_empty(&set->tag_list) &&
2442 !(set->flags & BLK_MQ_F_TAG_SHARED)) {
Jeff Moyer2404e602015-11-03 10:40:06 -05002443 set->flags |= BLK_MQ_F_TAG_SHARED;
2444 /* update existing queue */
2445 blk_mq_update_tag_set_depth(set, true);
2446 }
2447 if (set->flags & BLK_MQ_F_TAG_SHARED)
2448 queue_set_hctx_shared(q, true);
Bart Van Assche705cda92017-04-07 11:16:49 -07002449 list_add_tail_rcu(&q->tag_set_list, &set->tag_list);
Jeff Moyer2404e602015-11-03 10:40:06 -05002450
Jens Axboe0d2602c2014-05-13 15:10:52 -06002451 mutex_unlock(&set->tag_list_lock);
2452}
2453
Ming Leie09aae72015-01-29 20:17:27 +08002454/*
2455 * It is the actual release handler for mq, but we do it from
2456 * request queue's release handler for avoiding use-after-free
2457 * and headache because q->mq_kobj shouldn't have been introduced,
2458 * but we can't group ctx/kctx kobj without it.
2459 */
2460void blk_mq_release(struct request_queue *q)
2461{
2462 struct blk_mq_hw_ctx *hctx;
2463 unsigned int i;
2464
2465 /* hctx kobj stays in hctx */
Ming Leic3b4afc2015-06-04 22:25:04 +08002466 queue_for_each_hw_ctx(q, hctx, i) {
2467 if (!hctx)
2468 continue;
Ming Lei6c8b2322017-02-22 18:14:01 +08002469 kobject_put(&hctx->kobj);
Ming Leic3b4afc2015-06-04 22:25:04 +08002470 }
Ming Leie09aae72015-01-29 20:17:27 +08002471
Akinobu Mitaa723bab2015-09-27 02:09:21 +09002472 q->mq_map = NULL;
2473
Ming Leie09aae72015-01-29 20:17:27 +08002474 kfree(q->queue_hw_ctx);
2475
Ming Lei7ea5fe32017-02-22 18:14:00 +08002476 /*
2477 * release .mq_kobj and sw queue's kobject now because
2478 * both share lifetime with request queue.
2479 */
2480 blk_mq_sysfs_deinit(q);
2481
Ming Leie09aae72015-01-29 20:17:27 +08002482 free_percpu(q->queue_ctx);
2483}
2484
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002485struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set)
Jens Axboe320ae512013-10-24 09:20:05 +01002486{
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002487 struct request_queue *uninit_q, *q;
2488
Bart Van Assche5ee05242018-02-28 10:15:31 -08002489 uninit_q = blk_alloc_queue_node(GFP_KERNEL, set->numa_node, NULL);
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002490 if (!uninit_q)
2491 return ERR_PTR(-ENOMEM);
2492
2493 q = blk_mq_init_allocated_queue(set, uninit_q);
2494 if (IS_ERR(q))
2495 blk_cleanup_queue(uninit_q);
2496
2497 return q;
2498}
2499EXPORT_SYMBOL(blk_mq_init_queue);
2500
Jens Axboe9316a9e2018-10-15 08:40:37 -06002501/*
2502 * Helper for setting up a queue with mq ops, given queue depth, and
2503 * the passed in mq ops flags.
2504 */
2505struct request_queue *blk_mq_init_sq_queue(struct blk_mq_tag_set *set,
2506 const struct blk_mq_ops *ops,
2507 unsigned int queue_depth,
2508 unsigned int set_flags)
2509{
2510 struct request_queue *q;
2511 int ret;
2512
2513 memset(set, 0, sizeof(*set));
2514 set->ops = ops;
2515 set->nr_hw_queues = 1;
2516 set->queue_depth = queue_depth;
2517 set->numa_node = NUMA_NO_NODE;
2518 set->flags = set_flags;
2519
2520 ret = blk_mq_alloc_tag_set(set);
2521 if (ret)
2522 return ERR_PTR(ret);
2523
2524 q = blk_mq_init_queue(set);
2525 if (IS_ERR(q)) {
2526 blk_mq_free_tag_set(set);
2527 return q;
2528 }
2529
2530 return q;
2531}
2532EXPORT_SYMBOL(blk_mq_init_sq_queue);
2533
Bart Van Assche07319672017-06-20 11:15:38 -07002534static int blk_mq_hw_ctx_size(struct blk_mq_tag_set *tag_set)
2535{
2536 int hw_ctx_size = sizeof(struct blk_mq_hw_ctx);
2537
Tejun Heo05707b62018-01-09 08:29:53 -08002538 BUILD_BUG_ON(ALIGN(offsetof(struct blk_mq_hw_ctx, srcu),
Bart Van Assche07319672017-06-20 11:15:38 -07002539 __alignof__(struct blk_mq_hw_ctx)) !=
2540 sizeof(struct blk_mq_hw_ctx));
2541
2542 if (tag_set->flags & BLK_MQ_F_BLOCKING)
2543 hw_ctx_size += sizeof(struct srcu_struct);
2544
2545 return hw_ctx_size;
2546}
2547
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002548static struct blk_mq_hw_ctx *blk_mq_alloc_and_init_hctx(
2549 struct blk_mq_tag_set *set, struct request_queue *q,
2550 int hctx_idx, int node)
2551{
2552 struct blk_mq_hw_ctx *hctx;
2553
2554 hctx = kzalloc_node(blk_mq_hw_ctx_size(set),
2555 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2556 node);
2557 if (!hctx)
2558 return NULL;
2559
2560 if (!zalloc_cpumask_var_node(&hctx->cpumask,
2561 GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY,
2562 node)) {
2563 kfree(hctx);
2564 return NULL;
2565 }
2566
2567 atomic_set(&hctx->nr_active, 0);
2568 hctx->numa_node = node;
2569 hctx->queue_num = hctx_idx;
2570
2571 if (blk_mq_init_hctx(q, set, hctx, hctx_idx)) {
2572 free_cpumask_var(hctx->cpumask);
2573 kfree(hctx);
2574 return NULL;
2575 }
2576 blk_mq_hctx_kobj_init(hctx);
2577
2578 return hctx;
2579}
2580
Keith Busch868f2f02015-12-17 17:08:14 -07002581static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,
2582 struct request_queue *q)
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002583{
Jianchao Wange01ad462018-10-12 18:07:28 +08002584 int i, j, end;
Keith Busch868f2f02015-12-17 17:08:14 -07002585 struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;
Jens Axboe320ae512013-10-24 09:20:05 +01002586
Ming Leifb350e02018-01-06 16:27:40 +08002587 /* protect against switching io scheduler */
2588 mutex_lock(&q->sysfs_lock);
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002589 for (i = 0; i < set->nr_hw_queues; i++) {
Keith Busch868f2f02015-12-17 17:08:14 -07002590 int node;
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002591 struct blk_mq_hw_ctx *hctx;
Keith Busch868f2f02015-12-17 17:08:14 -07002592
2593 node = blk_mq_hw_queue_to_node(q->mq_map, i);
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002594 /*
2595 * If the hw queue has been mapped to another numa node,
2596 * we need to realloc the hctx. If allocation fails, fallback
2597 * to use the previous one.
2598 */
2599 if (hctxs[i] && (hctxs[i]->numa_node == node))
2600 continue;
Jens Axboe320ae512013-10-24 09:20:05 +01002601
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002602 hctx = blk_mq_alloc_and_init_hctx(set, q, i, node);
2603 if (hctx) {
2604 if (hctxs[i]) {
2605 blk_mq_exit_hctx(q, set, hctxs[i], i);
2606 kobject_put(&hctxs[i]->kobj);
2607 }
2608 hctxs[i] = hctx;
2609 } else {
2610 if (hctxs[i])
2611 pr_warn("Allocate new hctx on node %d fails,\
2612 fallback to previous one on node %d\n",
2613 node, hctxs[i]->numa_node);
2614 else
2615 break;
Keith Busch868f2f02015-12-17 17:08:14 -07002616 }
Jens Axboe320ae512013-10-24 09:20:05 +01002617 }
Jianchao Wange01ad462018-10-12 18:07:28 +08002618 /*
2619 * Increasing nr_hw_queues fails. Free the newly allocated
2620 * hctxs and keep the previous q->nr_hw_queues.
2621 */
2622 if (i != set->nr_hw_queues) {
2623 j = q->nr_hw_queues;
2624 end = i;
2625 } else {
2626 j = i;
2627 end = q->nr_hw_queues;
2628 q->nr_hw_queues = set->nr_hw_queues;
2629 }
Jianchao Wang34d11ff2018-10-12 18:07:27 +08002630
Jianchao Wange01ad462018-10-12 18:07:28 +08002631 for (; j < end; j++) {
Keith Busch868f2f02015-12-17 17:08:14 -07002632 struct blk_mq_hw_ctx *hctx = hctxs[j];
2633
2634 if (hctx) {
Jens Axboecc71a6f2017-01-11 14:29:56 -07002635 if (hctx->tags)
2636 blk_mq_free_map_and_requests(set, j);
Keith Busch868f2f02015-12-17 17:08:14 -07002637 blk_mq_exit_hctx(q, set, hctx, j);
Keith Busch868f2f02015-12-17 17:08:14 -07002638 kobject_put(&hctx->kobj);
Keith Busch868f2f02015-12-17 17:08:14 -07002639 hctxs[j] = NULL;
2640
2641 }
2642 }
Ming Leifb350e02018-01-06 16:27:40 +08002643 mutex_unlock(&q->sysfs_lock);
Keith Busch868f2f02015-12-17 17:08:14 -07002644}
2645
2646struct request_queue *blk_mq_init_allocated_queue(struct blk_mq_tag_set *set,
2647 struct request_queue *q)
2648{
Ming Lei66841672016-02-12 15:27:00 +08002649 /* mark the queue as mq asap */
2650 q->mq_ops = set->ops;
2651
Omar Sandoval34dbad52017-03-21 08:56:08 -07002652 q->poll_cb = blk_stat_alloc_callback(blk_mq_poll_stats_fn,
Stephen Bates720b8cc2017-04-07 06:24:03 -06002653 blk_mq_poll_stats_bkt,
2654 BLK_MQ_POLL_STATS_BKTS, q);
Omar Sandoval34dbad52017-03-21 08:56:08 -07002655 if (!q->poll_cb)
2656 goto err_exit;
2657
Keith Busch868f2f02015-12-17 17:08:14 -07002658 q->queue_ctx = alloc_percpu(struct blk_mq_ctx);
2659 if (!q->queue_ctx)
Ming Linc7de5722016-05-25 23:23:27 -07002660 goto err_exit;
Keith Busch868f2f02015-12-17 17:08:14 -07002661
Ming Lei737f98c2017-02-22 18:13:59 +08002662 /* init q->mq_kobj and sw queues' kobjects */
2663 blk_mq_sysfs_init(q);
2664
Kees Cook590b5b72018-06-12 14:04:20 -07002665 q->queue_hw_ctx = kcalloc_node(nr_cpu_ids, sizeof(*(q->queue_hw_ctx)),
Keith Busch868f2f02015-12-17 17:08:14 -07002666 GFP_KERNEL, set->numa_node);
2667 if (!q->queue_hw_ctx)
2668 goto err_percpu;
2669
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002670 q->mq_map = set->mq_map;
Keith Busch868f2f02015-12-17 17:08:14 -07002671
2672 blk_mq_realloc_hw_ctxs(set, q);
2673 if (!q->nr_hw_queues)
2674 goto err_hctxs;
Jens Axboe320ae512013-10-24 09:20:05 +01002675
Christoph Hellwig287922e2015-10-30 20:57:30 +08002676 INIT_WORK(&q->timeout_work, blk_mq_timeout_work);
Ming Leie56f6982015-07-16 19:53:22 +08002677 blk_queue_rq_timeout(q, set->timeout ? set->timeout : 30 * HZ);
Jens Axboe320ae512013-10-24 09:20:05 +01002678
2679 q->nr_queues = nr_cpu_ids;
Jens Axboe320ae512013-10-24 09:20:05 +01002680
Jens Axboe94eddfb2013-11-19 09:25:07 -07002681 q->queue_flags |= QUEUE_FLAG_MQ_DEFAULT;
Jens Axboe320ae512013-10-24 09:20:05 +01002682
Jens Axboe05f1dd52014-05-29 09:53:32 -06002683 if (!(set->flags & BLK_MQ_F_SG_MERGE))
Bart Van Asschef78bac22018-03-07 17:10:03 -08002684 queue_flag_set_unlocked(QUEUE_FLAG_NO_SG_MERGE, q);
Jens Axboe05f1dd52014-05-29 09:53:32 -06002685
Christoph Hellwig1be036e2014-02-07 10:22:39 -08002686 q->sg_reserved_size = INT_MAX;
2687
Mike Snitzer28494502016-09-14 13:28:30 -04002688 INIT_DELAYED_WORK(&q->requeue_work, blk_mq_requeue_work);
Christoph Hellwig6fca6a62014-05-28 08:08:02 -06002689 INIT_LIST_HEAD(&q->requeue_list);
2690 spin_lock_init(&q->requeue_lock);
2691
Christoph Hellwig254d2592017-03-22 15:01:50 -04002692 blk_queue_make_request(q, blk_mq_make_request);
Christoph Hellwigea435e12017-11-02 21:29:54 +03002693 if (q->mq_ops->poll)
2694 q->poll_fn = blk_mq_poll;
Jens Axboe07068d52014-05-22 10:40:51 -06002695
Jens Axboeeba71762014-05-20 15:17:27 -06002696 /*
2697 * Do this after blk_queue_make_request() overrides it...
2698 */
2699 q->nr_requests = set->queue_depth;
2700
Jens Axboe64f1c212016-11-14 13:03:03 -07002701 /*
2702 * Default to classic polling
2703 */
2704 q->poll_nsec = -1;
2705
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002706 blk_mq_init_cpu_queues(q, set->nr_hw_queues);
Jens Axboe0d2602c2014-05-13 15:10:52 -06002707 blk_mq_add_queue_tag_set(set, q);
Christoph Hellwig4b855ad2017-06-26 12:20:57 +02002708 blk_mq_map_swqueue(q);
Akinobu Mita4593fdb2015-09-27 02:09:20 +09002709
Jens Axboed3484992017-01-13 14:43:58 -07002710 if (!(set->flags & BLK_MQ_F_NO_SCHED)) {
2711 int ret;
2712
Christoph Hellwig131d08e2018-05-31 19:11:40 +02002713 ret = elevator_init_mq(q);
Jens Axboed3484992017-01-13 14:43:58 -07002714 if (ret)
2715 return ERR_PTR(ret);
2716 }
2717
Jens Axboe320ae512013-10-24 09:20:05 +01002718 return q;
Christoph Hellwig18741982014-02-10 09:29:00 -07002719
Jens Axboe320ae512013-10-24 09:20:05 +01002720err_hctxs:
Keith Busch868f2f02015-12-17 17:08:14 -07002721 kfree(q->queue_hw_ctx);
Jens Axboe320ae512013-10-24 09:20:05 +01002722err_percpu:
Keith Busch868f2f02015-12-17 17:08:14 -07002723 free_percpu(q->queue_ctx);
Ming Linc7de5722016-05-25 23:23:27 -07002724err_exit:
2725 q->mq_ops = NULL;
Jens Axboe320ae512013-10-24 09:20:05 +01002726 return ERR_PTR(-ENOMEM);
2727}
Mike Snitzerb62c21b2015-03-12 23:56:02 -04002728EXPORT_SYMBOL(blk_mq_init_allocated_queue);
Jens Axboe320ae512013-10-24 09:20:05 +01002729
2730void blk_mq_free_queue(struct request_queue *q)
2731{
Ming Lei624dbe42014-05-27 23:35:13 +08002732 struct blk_mq_tag_set *set = q->tag_set;
Jens Axboe320ae512013-10-24 09:20:05 +01002733
Jens Axboe0d2602c2014-05-13 15:10:52 -06002734 blk_mq_del_queue_tag_set(q);
Ming Lei624dbe42014-05-27 23:35:13 +08002735 blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
Jens Axboe320ae512013-10-24 09:20:05 +01002736}
Jens Axboe320ae512013-10-24 09:20:05 +01002737
Jens Axboea5164402014-09-10 09:02:03 -06002738static int __blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2739{
2740 int i;
2741
Jens Axboecc71a6f2017-01-11 14:29:56 -07002742 for (i = 0; i < set->nr_hw_queues; i++)
2743 if (!__blk_mq_alloc_rq_map(set, i))
Jens Axboea5164402014-09-10 09:02:03 -06002744 goto out_unwind;
Jens Axboea5164402014-09-10 09:02:03 -06002745
2746 return 0;
2747
2748out_unwind:
2749 while (--i >= 0)
Jens Axboecc71a6f2017-01-11 14:29:56 -07002750 blk_mq_free_rq_map(set->tags[i]);
Jens Axboea5164402014-09-10 09:02:03 -06002751
Jens Axboea5164402014-09-10 09:02:03 -06002752 return -ENOMEM;
2753}
2754
2755/*
2756 * Allocate the request maps associated with this tag_set. Note that this
2757 * may reduce the depth asked for, if memory is tight. set->queue_depth
2758 * will be updated to reflect the allocated depth.
2759 */
2760static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)
2761{
2762 unsigned int depth;
2763 int err;
2764
2765 depth = set->queue_depth;
2766 do {
2767 err = __blk_mq_alloc_rq_maps(set);
2768 if (!err)
2769 break;
2770
2771 set->queue_depth >>= 1;
2772 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN) {
2773 err = -ENOMEM;
2774 break;
2775 }
2776 } while (set->queue_depth);
2777
2778 if (!set->queue_depth || err) {
2779 pr_err("blk-mq: failed to allocate request map\n");
2780 return -ENOMEM;
2781 }
2782
2783 if (depth != set->queue_depth)
2784 pr_info("blk-mq: reduced tag depth (%u -> %u)\n",
2785 depth, set->queue_depth);
2786
2787 return 0;
2788}
2789
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06002790static int blk_mq_update_queue_map(struct blk_mq_tag_set *set)
2791{
Ming Lei7d4901a2018-01-06 16:27:39 +08002792 if (set->ops->map_queues) {
Ming Lei7d4901a2018-01-06 16:27:39 +08002793 /*
2794 * transport .map_queues is usually done in the following
2795 * way:
2796 *
2797 * for (queue = 0; queue < set->nr_hw_queues; queue++) {
2798 * mask = get_cpu_mask(queue)
2799 * for_each_cpu(cpu, mask)
2800 * set->mq_map[cpu] = queue;
2801 * }
2802 *
2803 * When we need to remap, the table has to be cleared for
2804 * killing stale mapping since one CPU may not be mapped
2805 * to any hw queue.
2806 */
Minwoo Im0da73d02018-07-02 23:46:43 +09002807 blk_mq_clear_mq_map(set);
Ming Lei7d4901a2018-01-06 16:27:39 +08002808
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06002809 return set->ops->map_queues(set);
Ming Lei7d4901a2018-01-06 16:27:39 +08002810 } else
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06002811 return blk_mq_map_queues(set);
2812}
2813
Jens Axboea4391c62014-06-05 15:21:56 -06002814/*
2815 * Alloc a tag set to be associated with one or more request queues.
2816 * May fail with EINVAL for various error conditions. May adjust the
Minwoo Imc018c842018-06-30 22:12:41 +09002817 * requested depth down, if it's too large. In that case, the set
Jens Axboea4391c62014-06-05 15:21:56 -06002818 * value will be stored in set->queue_depth.
2819 */
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002820int blk_mq_alloc_tag_set(struct blk_mq_tag_set *set)
2821{
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002822 int ret;
2823
Bart Van Assche205fb5f2014-10-30 14:45:11 +01002824 BUILD_BUG_ON(BLK_MQ_MAX_DEPTH > 1 << BLK_MQ_UNIQUE_TAG_BITS);
2825
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002826 if (!set->nr_hw_queues)
2827 return -EINVAL;
Jens Axboea4391c62014-06-05 15:21:56 -06002828 if (!set->queue_depth)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002829 return -EINVAL;
2830 if (set->queue_depth < set->reserved_tags + BLK_MQ_TAG_MIN)
2831 return -EINVAL;
2832
Christoph Hellwig7d7e0f92016-09-14 16:18:54 +02002833 if (!set->ops->queue_rq)
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002834 return -EINVAL;
2835
Ming Leide148292017-10-14 17:22:29 +08002836 if (!set->ops->get_budget ^ !set->ops->put_budget)
2837 return -EINVAL;
2838
Jens Axboea4391c62014-06-05 15:21:56 -06002839 if (set->queue_depth > BLK_MQ_MAX_DEPTH) {
2840 pr_info("blk-mq: reduced tag depth to %u\n",
2841 BLK_MQ_MAX_DEPTH);
2842 set->queue_depth = BLK_MQ_MAX_DEPTH;
2843 }
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002844
Shaohua Li6637fad2014-11-30 16:00:58 -08002845 /*
2846 * If a crashdump is active, then we are potentially in a very
2847 * memory constrained environment. Limit us to 1 queue and
2848 * 64 tags to prevent using too much memory.
2849 */
2850 if (is_kdump_kernel()) {
2851 set->nr_hw_queues = 1;
2852 set->queue_depth = min(64U, set->queue_depth);
2853 }
Keith Busch868f2f02015-12-17 17:08:14 -07002854 /*
2855 * There is no use for more h/w queues than cpus.
2856 */
2857 if (set->nr_hw_queues > nr_cpu_ids)
2858 set->nr_hw_queues = nr_cpu_ids;
Shaohua Li6637fad2014-11-30 16:00:58 -08002859
Kees Cook590b5b72018-06-12 14:04:20 -07002860 set->tags = kcalloc_node(nr_cpu_ids, sizeof(struct blk_mq_tags *),
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002861 GFP_KERNEL, set->numa_node);
2862 if (!set->tags)
Jens Axboea5164402014-09-10 09:02:03 -06002863 return -ENOMEM;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002864
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002865 ret = -ENOMEM;
Kees Cook590b5b72018-06-12 14:04:20 -07002866 set->mq_map = kcalloc_node(nr_cpu_ids, sizeof(*set->mq_map),
2867 GFP_KERNEL, set->numa_node);
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002868 if (!set->mq_map)
2869 goto out_free_tags;
2870
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06002871 ret = blk_mq_update_queue_map(set);
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002872 if (ret)
2873 goto out_free_mq_map;
2874
2875 ret = blk_mq_alloc_rq_maps(set);
2876 if (ret)
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002877 goto out_free_mq_map;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002878
Jens Axboe0d2602c2014-05-13 15:10:52 -06002879 mutex_init(&set->tag_list_lock);
2880 INIT_LIST_HEAD(&set->tag_list);
2881
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002882 return 0;
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002883
2884out_free_mq_map:
2885 kfree(set->mq_map);
2886 set->mq_map = NULL;
2887out_free_tags:
Robert Elliott5676e7b2014-09-02 11:38:44 -05002888 kfree(set->tags);
2889 set->tags = NULL;
Christoph Hellwigda695ba2016-09-14 16:18:55 +02002890 return ret;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002891}
2892EXPORT_SYMBOL(blk_mq_alloc_tag_set);
2893
2894void blk_mq_free_tag_set(struct blk_mq_tag_set *set)
2895{
2896 int i;
2897
Jens Axboecc71a6f2017-01-11 14:29:56 -07002898 for (i = 0; i < nr_cpu_ids; i++)
2899 blk_mq_free_map_and_requests(set, i);
Jens Axboe484b4062014-05-21 14:01:15 -06002900
Christoph Hellwigbdd17e72016-09-14 16:18:53 +02002901 kfree(set->mq_map);
2902 set->mq_map = NULL;
2903
Ming Lei981bd182014-04-24 00:07:34 +08002904 kfree(set->tags);
Robert Elliott5676e7b2014-09-02 11:38:44 -05002905 set->tags = NULL;
Christoph Hellwig24d2f902014-04-15 14:14:00 -06002906}
2907EXPORT_SYMBOL(blk_mq_free_tag_set);
2908
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002909int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)
2910{
2911 struct blk_mq_tag_set *set = q->tag_set;
2912 struct blk_mq_hw_ctx *hctx;
2913 int i, ret;
2914
Jens Axboebd166ef2017-01-17 06:03:22 -07002915 if (!set)
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002916 return -EINVAL;
2917
Jens Axboe70f36b62017-01-19 10:59:07 -07002918 blk_mq_freeze_queue(q);
Ming Lei24f5a902018-01-06 16:27:38 +08002919 blk_mq_quiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07002920
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002921 ret = 0;
2922 queue_for_each_hw_ctx(q, hctx, i) {
Keith Busche9137d42016-02-18 14:56:35 -07002923 if (!hctx->tags)
2924 continue;
Jens Axboebd166ef2017-01-17 06:03:22 -07002925 /*
2926 * If we're using an MQ scheduler, just update the scheduler
2927 * queue depth. This is similar to what the old code would do.
2928 */
Jens Axboe70f36b62017-01-19 10:59:07 -07002929 if (!hctx->sched_tags) {
weiping zhangc2e82a22017-09-22 23:36:28 +08002930 ret = blk_mq_tag_update_depth(hctx, &hctx->tags, nr,
Jens Axboe70f36b62017-01-19 10:59:07 -07002931 false);
2932 } else {
2933 ret = blk_mq_tag_update_depth(hctx, &hctx->sched_tags,
2934 nr, true);
2935 }
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002936 if (ret)
2937 break;
2938 }
2939
2940 if (!ret)
2941 q->nr_requests = nr;
2942
Ming Lei24f5a902018-01-06 16:27:38 +08002943 blk_mq_unquiesce_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07002944 blk_mq_unfreeze_queue(q);
Jens Axboe70f36b62017-01-19 10:59:07 -07002945
Jens Axboee3a2b3f2014-05-20 11:49:02 -06002946 return ret;
2947}
2948
Jianchao Wangd48ece22018-08-21 15:15:03 +08002949/*
2950 * request_queue and elevator_type pair.
2951 * It is just used by __blk_mq_update_nr_hw_queues to cache
2952 * the elevator_type associated with a request_queue.
2953 */
2954struct blk_mq_qe_pair {
2955 struct list_head node;
2956 struct request_queue *q;
2957 struct elevator_type *type;
2958};
2959
2960/*
2961 * Cache the elevator_type in qe pair list and switch the
2962 * io scheduler to 'none'
2963 */
2964static bool blk_mq_elv_switch_none(struct list_head *head,
2965 struct request_queue *q)
2966{
2967 struct blk_mq_qe_pair *qe;
2968
2969 if (!q->elevator)
2970 return true;
2971
2972 qe = kmalloc(sizeof(*qe), GFP_NOIO | __GFP_NOWARN | __GFP_NORETRY);
2973 if (!qe)
2974 return false;
2975
2976 INIT_LIST_HEAD(&qe->node);
2977 qe->q = q;
2978 qe->type = q->elevator->type;
2979 list_add(&qe->node, head);
2980
2981 mutex_lock(&q->sysfs_lock);
2982 /*
2983 * After elevator_switch_mq, the previous elevator_queue will be
2984 * released by elevator_release. The reference of the io scheduler
2985 * module get by elevator_get will also be put. So we need to get
2986 * a reference of the io scheduler module here to prevent it to be
2987 * removed.
2988 */
2989 __module_get(qe->type->elevator_owner);
2990 elevator_switch_mq(q, NULL);
2991 mutex_unlock(&q->sysfs_lock);
2992
2993 return true;
2994}
2995
2996static void blk_mq_elv_switch_back(struct list_head *head,
2997 struct request_queue *q)
2998{
2999 struct blk_mq_qe_pair *qe;
3000 struct elevator_type *t = NULL;
3001
3002 list_for_each_entry(qe, head, node)
3003 if (qe->q == q) {
3004 t = qe->type;
3005 break;
3006 }
3007
3008 if (!t)
3009 return;
3010
3011 list_del(&qe->node);
3012 kfree(qe);
3013
3014 mutex_lock(&q->sysfs_lock);
3015 elevator_switch_mq(q, t);
3016 mutex_unlock(&q->sysfs_lock);
3017}
3018
Keith Busche4dc2b32017-05-30 14:39:11 -04003019static void __blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set,
3020 int nr_hw_queues)
Keith Busch868f2f02015-12-17 17:08:14 -07003021{
3022 struct request_queue *q;
Jianchao Wangd48ece22018-08-21 15:15:03 +08003023 LIST_HEAD(head);
Jianchao Wange01ad462018-10-12 18:07:28 +08003024 int prev_nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07003025
Bart Van Assche705cda92017-04-07 11:16:49 -07003026 lockdep_assert_held(&set->tag_list_lock);
3027
Keith Busch868f2f02015-12-17 17:08:14 -07003028 if (nr_hw_queues > nr_cpu_ids)
3029 nr_hw_queues = nr_cpu_ids;
3030 if (nr_hw_queues < 1 || nr_hw_queues == set->nr_hw_queues)
3031 return;
3032
3033 list_for_each_entry(q, &set->tag_list, tag_set_list)
3034 blk_mq_freeze_queue(q);
Jianchao Wangd48ece22018-08-21 15:15:03 +08003035 /*
Jianchao Wangf5bbbbe2018-08-21 15:15:04 +08003036 * Sync with blk_mq_queue_tag_busy_iter.
3037 */
3038 synchronize_rcu();
3039 /*
Jianchao Wangd48ece22018-08-21 15:15:03 +08003040 * Switch IO scheduler to 'none', cleaning up the data associated
3041 * with the previous scheduler. We will switch back once we are done
3042 * updating the new sw to hw queue mappings.
3043 */
3044 list_for_each_entry(q, &set->tag_list, tag_set_list)
3045 if (!blk_mq_elv_switch_none(&head, q))
3046 goto switch_back;
Keith Busch868f2f02015-12-17 17:08:14 -07003047
Jianchao Wang477e19d2018-10-12 18:07:25 +08003048 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3049 blk_mq_debugfs_unregister_hctxs(q);
3050 blk_mq_sysfs_unregister(q);
3051 }
3052
Jianchao Wange01ad462018-10-12 18:07:28 +08003053 prev_nr_hw_queues = set->nr_hw_queues;
Keith Busch868f2f02015-12-17 17:08:14 -07003054 set->nr_hw_queues = nr_hw_queues;
Omar Sandovalebe8bdd2017-04-07 08:53:11 -06003055 blk_mq_update_queue_map(set);
Jianchao Wange01ad462018-10-12 18:07:28 +08003056fallback:
Keith Busch868f2f02015-12-17 17:08:14 -07003057 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3058 blk_mq_realloc_hw_ctxs(set, q);
Jianchao Wange01ad462018-10-12 18:07:28 +08003059 if (q->nr_hw_queues != set->nr_hw_queues) {
3060 pr_warn("Increasing nr_hw_queues to %d fails, fallback to %d\n",
3061 nr_hw_queues, prev_nr_hw_queues);
3062 set->nr_hw_queues = prev_nr_hw_queues;
3063 blk_mq_map_queues(set);
3064 goto fallback;
3065 }
Jianchao Wang477e19d2018-10-12 18:07:25 +08003066 blk_mq_map_swqueue(q);
3067 }
3068
3069 list_for_each_entry(q, &set->tag_list, tag_set_list) {
3070 blk_mq_sysfs_register(q);
3071 blk_mq_debugfs_register_hctxs(q);
Keith Busch868f2f02015-12-17 17:08:14 -07003072 }
3073
Jianchao Wangd48ece22018-08-21 15:15:03 +08003074switch_back:
3075 list_for_each_entry(q, &set->tag_list, tag_set_list)
3076 blk_mq_elv_switch_back(&head, q);
3077
Keith Busch868f2f02015-12-17 17:08:14 -07003078 list_for_each_entry(q, &set->tag_list, tag_set_list)
3079 blk_mq_unfreeze_queue(q);
3080}
Keith Busche4dc2b32017-05-30 14:39:11 -04003081
3082void blk_mq_update_nr_hw_queues(struct blk_mq_tag_set *set, int nr_hw_queues)
3083{
3084 mutex_lock(&set->tag_list_lock);
3085 __blk_mq_update_nr_hw_queues(set, nr_hw_queues);
3086 mutex_unlock(&set->tag_list_lock);
3087}
Keith Busch868f2f02015-12-17 17:08:14 -07003088EXPORT_SYMBOL_GPL(blk_mq_update_nr_hw_queues);
3089
Omar Sandoval34dbad52017-03-21 08:56:08 -07003090/* Enable polling stats and return whether they were already enabled. */
3091static bool blk_poll_stats_enable(struct request_queue *q)
3092{
3093 if (test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
Bart Van Assche7dfdbc72018-03-07 17:10:05 -08003094 blk_queue_flag_test_and_set(QUEUE_FLAG_POLL_STATS, q))
Omar Sandoval34dbad52017-03-21 08:56:08 -07003095 return true;
3096 blk_stat_add_callback(q, q->poll_cb);
3097 return false;
3098}
3099
3100static void blk_mq_poll_stats_start(struct request_queue *q)
3101{
3102 /*
3103 * We don't arm the callback if polling stats are not enabled or the
3104 * callback is already active.
3105 */
3106 if (!test_bit(QUEUE_FLAG_POLL_STATS, &q->queue_flags) ||
3107 blk_stat_is_active(q->poll_cb))
3108 return;
3109
3110 blk_stat_activate_msecs(q->poll_cb, 100);
3111}
3112
3113static void blk_mq_poll_stats_fn(struct blk_stat_callback *cb)
3114{
3115 struct request_queue *q = cb->data;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003116 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -07003117
Stephen Bates720b8cc2017-04-07 06:24:03 -06003118 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS; bucket++) {
3119 if (cb->stat[bucket].nr_samples)
3120 q->poll_stat[bucket] = cb->stat[bucket];
3121 }
Omar Sandoval34dbad52017-03-21 08:56:08 -07003122}
3123
Jens Axboe64f1c212016-11-14 13:03:03 -07003124static unsigned long blk_mq_poll_nsecs(struct request_queue *q,
3125 struct blk_mq_hw_ctx *hctx,
3126 struct request *rq)
3127{
Jens Axboe64f1c212016-11-14 13:03:03 -07003128 unsigned long ret = 0;
Stephen Bates720b8cc2017-04-07 06:24:03 -06003129 int bucket;
Jens Axboe64f1c212016-11-14 13:03:03 -07003130
3131 /*
3132 * If stats collection isn't on, don't sleep but turn it on for
3133 * future users
3134 */
Omar Sandoval34dbad52017-03-21 08:56:08 -07003135 if (!blk_poll_stats_enable(q))
Jens Axboe64f1c212016-11-14 13:03:03 -07003136 return 0;
3137
3138 /*
Jens Axboe64f1c212016-11-14 13:03:03 -07003139 * As an optimistic guess, use half of the mean service time
3140 * for this type of request. We can (and should) make this smarter.
3141 * For instance, if the completion latencies are tight, we can
3142 * get closer than just half the mean. This is especially
3143 * important on devices where the completion latencies are longer
Stephen Bates720b8cc2017-04-07 06:24:03 -06003144 * than ~10 usec. We do use the stats for the relevant IO size
3145 * if available which does lead to better estimates.
Jens Axboe64f1c212016-11-14 13:03:03 -07003146 */
Stephen Bates720b8cc2017-04-07 06:24:03 -06003147 bucket = blk_mq_poll_stats_bkt(rq);
3148 if (bucket < 0)
3149 return ret;
3150
3151 if (q->poll_stat[bucket].nr_samples)
3152 ret = (q->poll_stat[bucket].mean + 1) / 2;
Jens Axboe64f1c212016-11-14 13:03:03 -07003153
3154 return ret;
3155}
3156
Jens Axboe06426ad2016-11-14 13:01:59 -07003157static bool blk_mq_poll_hybrid_sleep(struct request_queue *q,
Jens Axboe64f1c212016-11-14 13:03:03 -07003158 struct blk_mq_hw_ctx *hctx,
Jens Axboe06426ad2016-11-14 13:01:59 -07003159 struct request *rq)
3160{
3161 struct hrtimer_sleeper hs;
3162 enum hrtimer_mode mode;
Jens Axboe64f1c212016-11-14 13:03:03 -07003163 unsigned int nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003164 ktime_t kt;
3165
Jens Axboe76a86f92018-01-10 11:30:56 -07003166 if (rq->rq_flags & RQF_MQ_POLL_SLEPT)
Jens Axboe64f1c212016-11-14 13:03:03 -07003167 return false;
3168
3169 /*
3170 * poll_nsec can be:
3171 *
3172 * -1: don't ever hybrid sleep
3173 * 0: use half of prev avg
3174 * >0: use this specific value
3175 */
3176 if (q->poll_nsec == -1)
3177 return false;
3178 else if (q->poll_nsec > 0)
3179 nsecs = q->poll_nsec;
3180 else
3181 nsecs = blk_mq_poll_nsecs(q, hctx, rq);
3182
3183 if (!nsecs)
Jens Axboe06426ad2016-11-14 13:01:59 -07003184 return false;
3185
Jens Axboe76a86f92018-01-10 11:30:56 -07003186 rq->rq_flags |= RQF_MQ_POLL_SLEPT;
Jens Axboe06426ad2016-11-14 13:01:59 -07003187
3188 /*
3189 * This will be replaced with the stats tracking code, using
3190 * 'avg_completion_time / 2' as the pre-sleep target.
3191 */
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01003192 kt = nsecs;
Jens Axboe06426ad2016-11-14 13:01:59 -07003193
3194 mode = HRTIMER_MODE_REL;
3195 hrtimer_init_on_stack(&hs.timer, CLOCK_MONOTONIC, mode);
3196 hrtimer_set_expires(&hs.timer, kt);
3197
3198 hrtimer_init_sleeper(&hs, current);
3199 do {
Tejun Heo5a61c362018-01-09 08:29:52 -08003200 if (blk_mq_rq_state(rq) == MQ_RQ_COMPLETE)
Jens Axboe06426ad2016-11-14 13:01:59 -07003201 break;
3202 set_current_state(TASK_UNINTERRUPTIBLE);
3203 hrtimer_start_expires(&hs.timer, mode);
3204 if (hs.task)
3205 io_schedule();
3206 hrtimer_cancel(&hs.timer);
3207 mode = HRTIMER_MODE_ABS;
3208 } while (hs.task && !signal_pending(current));
3209
3210 __set_current_state(TASK_RUNNING);
3211 destroy_hrtimer_on_stack(&hs.timer);
3212 return true;
3213}
3214
Jens Axboebbd7bb72016-11-04 09:34:34 -06003215static bool __blk_mq_poll(struct blk_mq_hw_ctx *hctx, struct request *rq)
3216{
3217 struct request_queue *q = hctx->queue;
3218 long state;
3219
Jens Axboe06426ad2016-11-14 13:01:59 -07003220 /*
3221 * If we sleep, have the caller restart the poll loop to reset
3222 * the state. Like for the other success return cases, the
3223 * caller is responsible for checking if the IO completed. If
3224 * the IO isn't complete, we'll get called again and will go
3225 * straight to the busy poll loop.
3226 */
Jens Axboe64f1c212016-11-14 13:03:03 -07003227 if (blk_mq_poll_hybrid_sleep(q, hctx, rq))
Jens Axboe06426ad2016-11-14 13:01:59 -07003228 return true;
3229
Jens Axboebbd7bb72016-11-04 09:34:34 -06003230 hctx->poll_considered++;
3231
3232 state = current->state;
3233 while (!need_resched()) {
3234 int ret;
3235
3236 hctx->poll_invoked++;
3237
3238 ret = q->mq_ops->poll(hctx, rq->tag);
3239 if (ret > 0) {
3240 hctx->poll_success++;
3241 set_current_state(TASK_RUNNING);
3242 return true;
3243 }
3244
3245 if (signal_pending_state(state, current))
3246 set_current_state(TASK_RUNNING);
3247
3248 if (current->state == TASK_RUNNING)
3249 return true;
3250 if (ret < 0)
3251 break;
3252 cpu_relax();
3253 }
3254
Nitesh Shetty67b41102018-02-13 21:18:12 +05303255 __set_current_state(TASK_RUNNING);
Jens Axboebbd7bb72016-11-04 09:34:34 -06003256 return false;
3257}
3258
Christoph Hellwigea435e12017-11-02 21:29:54 +03003259static bool blk_mq_poll(struct request_queue *q, blk_qc_t cookie)
Jens Axboebbd7bb72016-11-04 09:34:34 -06003260{
3261 struct blk_mq_hw_ctx *hctx;
Jens Axboebbd7bb72016-11-04 09:34:34 -06003262 struct request *rq;
3263
Christoph Hellwigea435e12017-11-02 21:29:54 +03003264 if (!test_bit(QUEUE_FLAG_POLL, &q->queue_flags))
Jens Axboebbd7bb72016-11-04 09:34:34 -06003265 return false;
3266
Jens Axboebbd7bb72016-11-04 09:34:34 -06003267 hctx = q->queue_hw_ctx[blk_qc_t_to_queue_num(cookie)];
Jens Axboebd166ef2017-01-17 06:03:22 -07003268 if (!blk_qc_t_is_internal(cookie))
3269 rq = blk_mq_tag_to_rq(hctx->tags, blk_qc_t_to_tag(cookie));
Jens Axboe3a07bb12017-04-20 14:53:28 -06003270 else {
Jens Axboebd166ef2017-01-17 06:03:22 -07003271 rq = blk_mq_tag_to_rq(hctx->sched_tags, blk_qc_t_to_tag(cookie));
Jens Axboe3a07bb12017-04-20 14:53:28 -06003272 /*
3273 * With scheduling, if the request has completed, we'll
3274 * get a NULL return here, as we clear the sched tag when
3275 * that happens. The request still remains valid, like always,
3276 * so we should be safe with just the NULL check.
3277 */
3278 if (!rq)
3279 return false;
3280 }
Jens Axboebbd7bb72016-11-04 09:34:34 -06003281
3282 return __blk_mq_poll(hctx, rq);
3283}
Jens Axboebbd7bb72016-11-04 09:34:34 -06003284
Jens Axboe320ae512013-10-24 09:20:05 +01003285static int __init blk_mq_init(void)
3286{
Thomas Gleixner9467f852016-09-22 08:05:17 -06003287 cpuhp_setup_state_multi(CPUHP_BLK_MQ_DEAD, "block/mq:dead", NULL,
3288 blk_mq_hctx_notify_dead);
Jens Axboe320ae512013-10-24 09:20:05 +01003289 return 0;
3290}
3291subsys_initcall(blk_mq_init);